1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
|
--
-- ion/query/mod_query.lua -- Some common queries for Ion
--
-- Copyright (c) Tuomo Valkonen 2004-2005.
--
-- Ion is free software; you can redistribute it and/or modify it under
-- the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--
-- This is a slight abuse of the _LOADED variable perhaps, but library-like
-- packages should handle checking if they're loaded instead of confusing
-- the user with require/include differences.
if _LOADED["mod_query"] then return end
if not ioncore.load_module("mod_query") then
return
end
local mod_query=_G["mod_query"]
assert(mod_query)
local DIE_TIMEOUT=30*1000 -- 30 seconds
-- Generic helper functions {{{
function mod_query.make_completor(completefn)
local function completor(wedln, str)
wedln:set_completions(completefn(str))
end
return completor
end
function mod_query.query(mplex, prompt, initvalue, handler, completor,
context)
local function handle_it(str)
handler(mplex, str)
end
-- Check that no other queries are open in the mplex.
local l=mplex:llist(2)
for i, r in l do
if obj_is(r, "WEdln") then
return
end
end
wedln=mod_query.do_query(mplex, prompt, initvalue, handle_it, completor)
if context then
wedln:set_context(context)
end
end
--DOC
-- This function query will display a query with prompt \var{prompt} in
-- \var{mplex} and if the user answers affirmately, call \var{handler}
-- with \var{mplex} as parameter.
function mod_query.query_yesno(mplex, prompt, handler)
local function handler_yesno(mplex, str)
if str=="y" or str=="Y" or str=="yes" then
handler(mplex)
end
end
return mod_query.query(mplex, prompt, nil, handler_yesno, nil,
"yesno")
end
local errdata={}
--[[
local function chld_handler(pid)
local t=errdata[pid]
if t then
errdata[pid]=nil
if t.errs then
mod_query.warn(t.mplex, t.errs)
end
end
end
ioncore.get_hook("ioncore_sigchld_hook"):add(chld_handler)
]]
function mod_query.exec_on_merr(mplex, cmd)
local pid
local function monitor(str)
if pid then
local t=errdata[pid]
if t then
if str then
t.errs=(t.errs or "")..str
elseif t.errs then
errdata[pid]=nil
mod_query.warn(t.mplex, t.errs)
end
end
end
end
local function timeout()
errdata[pid]=nil
end
pid=ioncore.exec_on(mplex, cmd, monitor)
if pid<=0 then
return
end
local tmr=ioncore.create_timer();
tmr:set(DIE_TIMEOUT, timeout)
errdata[pid]={tmr=tmr, mplex=mplex}
end
function mod_query.query_execfile(mplex, prompt, prog)
assert(prog~=nil)
local function handle_execwith(mplex, str)
mod_query.exec_on_merr(mplex, prog.." "..string.shell_safe(str))
end
return mod_query.query(mplex, prompt, mod_query.get_initdir(mplex),
handle_execwith, mod_query.file_completor,
"filename")
end
function mod_query.query_execwith(mplex, prompt, dflt, prog, completor,
context)
local function handle_execwith(frame, str)
if not str or str=="" then
str=dflt
end
mod_query.exec_on_merr(mplex, prog.." "..string.shell_safe(str))
end
return mod_query.query(mplex, prompt, nil, handle_execwith, completor,
context)
end
function mod_query.get_initdir(mplex)
--if mod_query.last_dir then
-- return mod_query.last_dir
--end
local wd=(ioncore.get_dir_for(mplex) or os.getenv("PWD"))
if wd==nil then
wd="/"
elseif string.sub(wd, -1)~="/" then
wd=wd .. "/"
end
return wd
end
local MAXDEPTH=10
function mod_query.lookup_workspace_classes()
local classes={}
for k, v in _G do
if type(v)=="table" and v.__typename then
v2=v.__parentclass
for i=1, MAXDEPTH do
if not v2 then
break
end
if v2.__typename=="WGenWS" then
table.insert(classes, v.__typename)
break
end
v2=v2.__parentclass
end
end
end
return classes
end
function mod_query.complete_from_list(list, str)
local results={}
local len=string.len(str)
if len==0 then
results=list
else
for _, m in list do
if string.sub(m, 1, len)==str then
table.insert(results, m)
end
end
end
return results
end
local pipes={}
mod_query.COLLECT_THRESHOLD=2000
--DOC
-- This function can be used to read completions from an external source.
-- The string \var{cmd} is a shell command to be executed. To its stdout,
-- the command should on the first line write the \var{common_part}
-- parameter of \fnref{WEdln.set_completions} and a single actual completion
-- on each of the successive lines.
function mod_query.popen_completions(wedln, cmd, beg, fn)
local pst={wedln=wedln, maybe_stalled=0}
local function rcv(str)
local data=""
local results={}
local totallen=0
local lines=0
while str do
if pst.maybe_stalled>=2 then
pipes[rcv]=nil
return
end
pst.maybe_stalled=0
totallen=totallen+string.len(str)
if totallen>ioncore.RESULT_DATA_LIMIT then
error(TR("Too much result data"))
end
data=string.gsub(data..str, "([^\n]*)\n",
function(a)
-- ion-completefile will return possible
-- common part of path on the first line
-- and the entries in that directory on the
-- following lines.
if not results.common_part then
results.common_part=(beg or "")..a
else
table.insert(results, a)
end
lines=lines+1
end)
if lines>mod_query.COLLECT_THRESHOLD then
collectgarbage()
lines=0
end
str=coroutine.yield()
end
if not results.common_part then
results.common_part=beg
end
(fn or WEdln.set_completions)(wedln, results)
pipes[rcv]=nil
results={}
collectgarbage()
end
local found_clean=false
for k, v in pipes do
if v.wedln==wedln then
if v.maybe_stalled<2 then
v.maybe_stalled=v.maybe_stalled+1
found_clean=true
end
end
end
if not found_clean then
pipes[rcv]=pst
ioncore.popen_bgread(cmd, coroutine.wrap(rcv))
end
end
--}}}
-- Simple queries for internal actions {{{
function mod_query.complete_name(str, list)
local entries={}
local l=string.len(str)
for i, reg in list do
local nm=reg:name()
if nm and string.sub(nm, 1, l)==str then
table.insert(entries, nm)
end
end
if table.getn(entries)==0 then
for i, reg in list do
local nm=reg:name()
if nm and string.find(nm, str, 1, true) then
table.insert(entries, nm)
end
end
end
return entries
end
function mod_query.complete_clientwin(str)
return mod_query.complete_name(str, ioncore.clientwin_list())
end
function mod_query.complete_workspace(str)
return mod_query.complete_name(str, ioncore.region_list("WGenWS"))
end
function mod_query.complete_region(str)
return mod_query.complete_name(str, ioncore.region_list())
end
function mod_query.gotoclient_handler(frame, str)
local cwin=ioncore.lookup_clientwin(str)
if cwin==nil then
mod_query.warn(frame, TR("Could not find client window %s.", str))
else
cwin:goto()
end
end
function mod_query.attachclient_handler(frame, str)
local cwin=ioncore.lookup_clientwin(str)
if not cwin then
mod_query.warn(frame, TR("Could not find client window %s.", str))
elseif frame:rootwin_of()~=cwin:rootwin_of() then
mod_query.warn(frame, TR("Cannot attach: different root windows."))
else
frame:attach(cwin, { switchto = true })
end
end
function mod_query.workspace_handler(mplex, name)
local ws=ioncore.lookup_region(name, "WGenWS")
if ws then
ws:goto()
return
end
local classes=mod_query.lookup_workspace_classes()
local function completor(wedln, what)
local results=mod_query.complete_from_list(classes, what)
wedln:set_completions(results)
end
local function handler(mplex, cls)
local scr=mplex:screen_of()
if not scr then
mod_query.warn(mplex, TR("Unable to create workspace: no screen."))
return
end
if not cls or cls=="" then
cls=ioncore.get().default_ws_type
end
local err=collect_errors(function()
ws=scr:attach_new({
type=cls,
name=name,
switchto=true
})
end)
if not ws then
mod_query.warn(mplex, err or TR("Unknown error"))
end
end
local defcls=ioncore.get().default_ws_type
local prompt=TR("Workspace type (%s):", defcls or TR("none"))
mod_query.query(mplex, prompt, "", handler, completor,
"workspacename")
end
--DOC
-- This query asks for the name of a client window and attaches
-- it to the frame the query was opened in. It uses the completion
-- function \fnref{ioncore.complete_clientwin}.
function mod_query.query_gotoclient(mplex)
mod_query.query(mplex, TR("Go to window:"), nil,
mod_query.gotoclient_handler,
mod_query.make_completor(mod_query.complete_clientwin),
"windowname")
end
--DOC
-- This query asks for the name of a client window and switches
-- focus to the one entered. It uses the completion function
-- \fnref{ioncore.complete_clientwin}.
function mod_query.query_attachclient(mplex)
mod_query.query(mplex, TR("Attach window:"), nil,
mod_query.attachclient_handler,
mod_query.make_completor(mod_query.complete_clientwin),
"windowname")
end
--DOC
-- This query asks for the name of a workspace. If a workspace
-- (an object inheriting \type{WGenWS}) with such a name exists,
-- it will be switched to. Otherwise a new workspace with the
-- entered name will be created and the user will be queried for
-- the type of the workspace.
function mod_query.query_workspace(mplex)
mod_query.query(mplex, TR("Go to or create workspace:"), nil,
mod_query.workspace_handler,
mod_query.make_completor(mod_query.complete_workspace),
"workspacename")
end
--DOC
-- This query asks whether the user wants to exit Ion (no session manager)
-- or close the session (running under a session manager that supports such
-- requests). If the answer is 'y', 'Y' or 'yes', so will happen.
function mod_query.query_shutdown(mplex)
mod_query.query_yesno(mplex, TR("Exit Ion/Shutdown session (y/n)?"),
ioncore.shutdown)
end
--DOC
-- This query asks whether the user wants restart Ioncore.
-- If the answer is 'y', 'Y' or 'yes', so will happen.
function mod_query.query_restart(mplex)
mod_query.query_yesno(mplex, TR("Restart Ion (y/n)?"), ioncore.restart)
end
--DOC
-- This function asks for a name new for the frame where the query
-- was created.
function mod_query.query_renameframe(frame)
mod_query.query(frame, TR("Frame name:"), frame:name(),
function(frame, str) frame:set_name(str) end,
nil, "framename")
end
--DOC
-- This function asks for a name new for the workspace on which the
-- query resides.
function mod_query.query_renameworkspace(mplex)
local ws=ioncore.find_manager(mplex, "WGenWS")
mod_query.query(mplex, TR("Workspace name:"), ws:name(),
function(mplex, str) ws:set_name(str) end,
nil, "framename")
end
--}}}
-- Run/view/edit {{{
function mod_query.file_completor(wedln, str, wp, beg)
local ic=ioncore.lookup_script("ion-completefile")
if ic then
mod_query.popen_completions(wedln,
ic..(wp or " ")..string.shell_safe(str),
beg)
end
end
--DOC
-- Asks for a file to be edited. This script uses
-- \command{run-mailcap --mode=edit} by default, but you may provide an
-- alternative script to use. The default prompt is "Edit file:" (translated).
function mod_query.query_editfile(mplex, script, prompt)
mod_query.query_execfile(mplex,
prompt or TR("Edit file:"),
script or "run-mailcap --action=edit")
end
--DOC
-- Asks for a file to be viewed. This script uses
-- \command{run-mailcap --action=view} by default, but you may provide an
-- alternative script to use. The default prompt is "View file:" (translated).
function mod_query.query_runfile(mplex, script, prompt)
mod_query.query_execfile(mplex,
prompt or TR("View file:"),
script or "run-mailcap --action=view")
end
function break_cmdline(str, no_ws)
local st, en, beg, rest, ch
local res={""}
local concat=1
local function ins(str)
res[concat]=res[concat]..str
end
local function ins_space(str)
if no_ws then
if res[table.getn(res)]=="" then
return
end
else
table.insert(res, str)
end
table.insert(res, "")
concat=table.getn(res)
end
st, en, beg, ch, rest=string.find(str, "^(%s*)(:+)(.*)")
if beg then
if string.len(beg)>0 then
ins_space(beg)
end
ins(ch)
ins_space("")
str=rest
end
while str~="" do
st, en, beg, rest, ch=string.find(str, "^(.-)(([%s'\"\\]).*)")
if not beg then
ins(str)
break
end
ins(beg)
str=rest
local sp=false
if ch=="\\" then
st, en, beg, rest=string.find(str, "^(\\.)(.*)")
elseif ch=='"' then
st, en, beg, rest=string.find(str, "^(\".-[^\\]\")(.*)")
if not beg then
st, en, beg, rest=string.find(str, "^(\"\")(.*)")
end
elseif ch=="'" then
st, en, beg, rest=string.find(str, "^('.-')(.*)")
else
st, en, beg, rest=string.find(str, "^(%s*)(.*)")
assert(beg and rest)
ins_space(beg)
sp=true
str=rest
end
if not sp then
if not beg then
beg=str
rest=""
end
ins(beg)
str=rest
end
end
return res
end
local function unquote(str)
str=string.gsub(str, "^['\"]", "")
str=string.gsub(str, "([^\\])['\"]", "%1")
str=string.gsub(str, "\\(.)", "%1")
return str
end
local function quote(str)
return string.gsub(str, "([%(%)\"'\\%*%?%[%] ])", "\\%1")
end
function mod_query.exec_completor(wedln, str)
local parts=break_cmdline(str)
local tocompl=unquote(table.remove(parts))
local beg=table.concat(parts)
local wp=" "
if string.find(beg, "^%s*:*%s*$") then
wp=" -wp "
end
local function set_fn(wedln, res)
res=table.map(quote, res)
res.common_part=beg..res.common_part
wedln:set_completions(res)
end
local ic=ioncore.lookup_script("ion-completefile")
if ic then
mod_query.popen_completions(wedln,
ic..(wp or " ")..string.shell_safe(tocompl),
"", set_fn)
end
end
local cmd_overrides={}
--DOC
-- Define a command override for the \fnrefx{mod_query}{query_exec} query.
function mod_query.defcmd(cmd, fn)
cmd_overrides[cmd]=fn
end
function mod_query.exec_handler(mplex, cmdline)
local parts=break_cmdline(cmdline, true)
local cmd=table.remove(parts, 1)
if cmd_overrides[cmd] then
cmd_overrides[cmd](mplex, table.map(unquote, parts))
elseif cmd~="" then
mod_query.exec_on_merr(mplex, cmdline)
end
end
--DOC
-- This function asks for a command to execute with \file{/bin/sh}.
-- If the command is prefixed with a colon (':'), the command will
-- be run in an XTerm (or other terminal emulator) using the script
-- \file{ion-runinxterm}. Two colons ('::') will ask you to press
-- enter after the command has finished.
function mod_query.query_exec(mplex)
mod_query.query(mplex, TR("Run:"), nil, mod_query.exec_handler,
mod_query.exec_completor,
"run")
end
-- }}}
-- SSH {{{
mod_query.known_hosts={}
function mod_query.get_known_hosts(mplex)
mod_query.known_hosts={}
local f
local h=os.getenv("HOME")
if h then
f=io.open(h.."/.ssh/known_hosts")
end
if not f then
warn(TR("Failed to open ~/.ssh/known_hosts"))
return
end
for l in f:lines() do
local st, en, hostname=string.find(l, "^([^%s,]+)")
if hostname then
table.insert(mod_query.known_hosts, hostname)
end
end
f:close()
end
function mod_query.complete_ssh(str)
local st, en, user, at, host=string.find(str, "^([^@]*)(@?)(.*)$")
if string.len(at)==0 and string.len(host)==0 then
host = user; user = ""
end
if at=="@" then
user = user .. at
end
local res = {}
if string.len(host)==0 then
if string.len(user)==0 then
return mod_query.known_hosts
end
for _, v in ipairs(mod_query.known_hosts) do
table.insert(res, user .. v)
end
return res
end
for _, v in ipairs(mod_query.known_hosts) do
local s, e=string.find(v, host, 1, true)
if s==1 and e>=1 then
table.insert(res, user .. v)
end
end
return res
end
--DOC
-- This query asks for a host to connect to with SSH.
-- Hosts to tab-complete are read from \file{\~{}/.ssh/known\_hosts}.
function mod_query.query_ssh(mplex, ssh)
mod_query.get_known_hosts(mplex)
ssh=(ssh or ":ssh")
local function handle_exec(mplex, str)
if not (str and string.find(str, "[^%s]")) then
return
end
mod_query.exec_on_merr(mplex, ssh.." "..string.shell_safe(str))
end
return mod_query.query(mplex, TR("SSH to:"), nil, handle_exec,
mod_query.make_completor(mod_query.complete_ssh),
"ssh")
end
-- }}}
-- Man pages {{{{
function mod_query.man_completor(wedln, str)
local mc=ioncore.lookup_script("ion-completeman")
if mc then
mod_query.popen_completions(wedln, mc.." -complete "..
string.shell_safe(str))
end
end
--DOC
-- This query asks for a manual page to display. By default it runs the
-- \command{man} command in an \command{xterm} using \command{ion-runinxterm},
-- but it is possible to pass another program as the \var{prog} argument.
function mod_query.query_man(mplex, prog)
local dflt=ioncore.progname()
mod_query.query_execwith(mplex, TR("Manual page (%s):", dflt),
dflt, prog or ":man",
mod_query.man_completor, "man")
end
-- }}}
-- Lua code execution {{{
function mod_query.create_run_env(mplex)
local origenv=getfenv()
local meta={__index=origenv, __newindex=origenv}
local env={
_=mplex,
_sub=mplex:current(),
}
setmetatable(env, meta)
return env
end
function mod_query.do_handle_lua(mplex, env, code)
local f, err=loadstring(code)
if not f then
mod_query.warn(mplex, err)
return
end
setfenv(f, env)
err=collect_errors(f)
if err then
mod_query.warn(mplex, err)
end
end
local function getindex(t)
local mt=getmetatable(t)
if mt then return mt.__index end
return nil
end
function mod_query.do_complete_lua(env, str)
-- Get the variable to complete, including containing tables.
-- This will also match string concatenations and such because
-- Lua's regexps don't support optional subexpressions, but we
-- handle them in the next step.
local comptab=env
local metas=true
local _, _, tocomp=string.find(str, "([%w_.:]*)$")
-- Descend into tables
if tocomp and string.len(tocomp)>=1 then
for t in string.gfind(tocomp, "([^.:]*)[.:]") do
metas=false
if string.len(t)==0 then
comptab=env;
elseif comptab then
if type(comptab[t])=="table" then
comptab=comptab[t]
elseif type(comptab[t])=="userdata" then
comptab=getindex(comptab[t])
metas=true
else
comptab=nil
end
end
end
end
if not comptab then return {} end
local compl={}
-- Get the actual variable to complete without containing tables
_, _, compl.common_part, tocomp=string.find(str, "(.-)([%w_]*)$")
local l=string.len(tocomp)
local tab=comptab
local seen={}
while true do
for k in tab do
if type(k)=="string" then
if string.sub(k, 1, l)==tocomp then
table.insert(compl, k)
end
end
end
-- We only want to display full list of functions for objects, not
-- the tables representing the classes.
--if not metas then break end
seen[tab]=true
tab=getindex(tab)
if not tab or seen[tab] then break end
end
-- If there was only one completion and it is a string or function,
-- concatenate it with "." or "(", respectively.
if table.getn(compl)==1 then
if type(comptab[compl[1]])=="table" then
compl[1]=compl[1] .. "."
elseif type(comptab[compl[1]])=="function" then
compl[1]=compl[1] .. "("
end
end
return compl
end
--DOC
-- This query asks for Lua code to execute. It sets the variable '\var{\_}'
-- in the local environment of the string to point to the mplex where the
-- query was created. It also sets the table \var{arg} in the local
-- environment to \code{\{_, _:current()\}}.
function mod_query.query_lua(mplex)
local env=mod_query.create_run_env(mplex)
local function complete(wedln, code)
wedln:set_completions(mod_query.do_complete_lua(env, code))
end
local function handler(mplex, code)
return mod_query.do_handle_lua(mplex, env, code)
end
mod_query.query(mplex, TR("Lua code: "), nil, handler, complete, "lua")
end
-- }}}
-- Menu query {{{
--DOC
-- This query can be used to create a query of a defined menu.
function mod_query.query_menu(mplex, prompt, menuname)
local _sub=mplex:current()
local menu=mod_menu.evalmenu(menuname, {mplex, _sub})
if not menu then
mod_query.warn(mplex, TR("Unknown menu %s.", tostring(menuname)))
return
end
function complete(str)
local results={}
local len=string.len(str)
for _, m in menu do
local mn=m.name
if len==0 or string.sub(mn, 1, len)==str then
table.insert(results, mn)
end
end
return results
end
local function handle(mplex, str)
local e
for k, v in menu do
if v.name==str then
e=v
break
end
end
if e then
if e.func then
local err=collect_errors(function()
e.func(mplex, _sub)
end)
if err then
mod_query.warn(mplex, err)
end
elseif e.submenu_fn then
mod_query.query_menu(mplex, TR("%s menu:", e.name),
e.submenu_fn())
end
else
mod_query.warn(mplex, TR("No entry '%s'", str))
end
end
mod_query.query(mplex, prompt, nil, handle,
mod_query.make_completor(complete), "menu")
end
-- }}}
-- Miscellaneous {{{
--DOC
-- Display an "About Ion" message in \var{mplex}.
function mod_query.show_about_ion(mplex)
mod_query.message(mplex, ioncore.aboutmsg())
end
--DOC
-- Show information about a client window.
function mod_query.show_clientwin(mplex, cwin)
local function indent(s)
local i=" "
return i..string.gsub(s, "\n", "\n"..i)
end
local function get_info(cwin)
local function n(s) return (s or "") end
local i=cwin:get_ident()
local s=TR("Title: %s\nClass: %s\nRole: %s\nInstance: %s\nXID: 0x%x",
n(cwin:name()), n(i.class), n(i.role), n(i.instance),
cwin:xid())
local t=TR("\nTransients:\n")
for k, v in cwin:managed_list() do
if obj_is(v, "WClientWin") then
s=s..t..indent(get_info(v))
t="\n"
end
end
return s
end
mod_query.message(mplex, get_info(cwin))
end
-- }}}
-- Load extras
dopath('mod_query_chdir')
-- Mark ourselves loaded.
_LOADED["mod_query"]=true
-- Load configuration file
dopath('cfg_query', true)
|