2 -- ion/query/mod_query.lua -- Some common queries for Ion
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
6 -- Ion is free software; you can redistribute it and/or modify it under
7 -- the terms of the GNU Lesser General Public License as published by
8 -- the Free Software Foundation; either version 2.1 of the License, or
9 -- (at your option) any later version.
13 -- This is a slight abuse of the package.loaded variable perhaps, but
14 -- library-like packages should handle checking if they're loaded instead of
15 -- confusing the user with require/include differences.
16 if package.loaded["mod_query"] then return end
18 if not ioncore.load_module("mod_query") then
22 local mod_query=_G["mod_query"]
27 local DIE_TIMEOUT_ERRORCODE=10 -- 10 seconds
28 local DIE_TIMEOUT_NO_ERRORCODE=2 -- 2 seconds
31 -- Generic helper functions {{{
35 -- Display an error message box in the multiplexer \var{mplex}.
36 function mod_query.warn(mplex, str)
37 ioncore.unsqueeze(mod_query.do_warn(mplex, str))
42 -- Display a message in \var{mplex}.
43 function mod_query.message(mplex, str)
44 ioncore.unsqueeze(mod_query.do_message(mplex, str))
49 -- Low-level query routine. \var{mplex} is the \type{WMPlex} to display
50 -- the query in, \var{prompt} the prompt string, and \var{initvalue}
51 -- the initial contents of the query box. \var{handler} is a function
52 -- that receives (\var{mplex}, result string) as parameter when the
53 -- query has been succesfully completed, \var{completor} the completor
54 -- routine which receives a (\var{cp}, \var{str}, \var{point}) as parameters.
55 -- The parameter \var{str} is the string to be completed and \var{point}
56 -- cursor's location within it. Completions should be eventually,
57 -- possibly asynchronously, set with \fnref{WComplProxy.set_completions}
59 function mod_query.query(mplex, prompt, initvalue, handler, completor,
61 local function handle_it(str)
64 local function cycle(wedln)
65 wedln:complete('next', 'normal')
67 local function bcycle(wedln)
68 wedln:complete('prev', 'normal')
71 -- Check that no other queries are open in the mplex.
72 local ok=mplex:managed_i(function(r)
73 return not obj_is(r, "WEdln")
79 local wedln=mod_query.do_query(mplex, prompt, initvalue,
80 handle_it, completor, cycle, bcycle)
83 ioncore.unsqueeze(wedln)
86 wedln:set_context(context)
95 -- This function query will display a query with prompt \var{prompt} in
96 -- \var{mplex} and if the user answers affirmately, call \var{handler}
97 -- with \var{mplex} as parameter.
98 function mod_query.query_yesno(mplex, prompt, handler)
99 local function handler_yesno(mplex, str)
100 if str=="y" or str=="Y" or str=="yes" then
104 return mod_query.query(mplex, prompt, nil, handler_yesno, nil,
111 local function maybe_finish(pid)
114 if t and t.closed and t.dietime then
116 local tmd=os.difftime(t.dietime, t.starttime)
117 --if tmd<DIE_TIMEOUT_ERRORCODE and t.signaled then
118 -- local msg=TR("Program received signal ")..t.termsig.."\n"
119 -- mod_query.warn(t.mplex, msg..(t.errs or ""))
121 if ((tmd<DIE_TIMEOUT_ERRORCODE and (t.hadcode or t.signaled)) or
122 (tmd<DIE_TIMEOUT_NO_ERRORCODE)) and t.errs then
123 mod_query.warn(t.mplex, t.errs)
129 local badsig_={4, 5, 6, 7, 8, 11}
131 for _, v in pairs(badsig_) do
135 local function chld_handler(p)
136 local t=errdata[p.pid]
139 t.signaled=(p.signaled and badsig[p.termsig])
141 t.hadcode=(p.exited and p.exitstatus~=0)
146 ioncore.get_hook("ioncore_sigchld_hook"):add(chld_handler)
148 function mod_query.exec_on_merr(mplex, cmd)
151 local function monitor(str)
156 t.errs=(t.errs or "")..str
165 local function timeout()
169 pid=ioncore.exec_on(mplex, cmd, monitor)
175 local tmr=ioncore.create_timer();
176 local tmd=math.max(DIE_TIMEOUT_NO_ERRORCODE, DIE_TIMEOUT_ERRORCODE)
178 tmr:set(tmd*1000, timeout)
180 errdata[pid]={tmr=tmr, mplex=mplex, starttime=now}
184 function mod_query.file_completor(wedln, str)
185 local ic=ioncore.lookup_script("ion-completefile")
187 mod_query.popen_completions(wedln,
188 ic.." "..string.shell_safe(str))
193 function mod_query.get_initdir(mplex)
194 --if mod_query.last_dir then
195 -- return mod_query.last_dir
197 local wd=(ioncore.get_dir_for(mplex) or os.getenv("PWD"))
200 elseif string.sub(wd, -1)~="/" then
207 function mod_query.query_execfile(mplex, prompt, prog)
209 local function handle_execwith(mplex, str)
210 mod_query.exec_on_merr(mplex, prog.." "..string.shell_safe(str))
212 return mod_query.query(mplex, prompt, mod_query.get_initdir(mplex),
213 handle_execwith, mod_query.file_completor,
218 function mod_query.query_execwith(mplex, prompt, dflt, prog, completor,
220 local function handle_execwith(frame, str)
221 if not str or str=="" then
224 local args=(noquote and str or string.shell_safe(str))
225 mod_query.exec_on_merr(mplex, prog.." "..args)
227 return mod_query.query(mplex, prompt, nil, handle_execwith, completor,
235 -- Completion helpers {{{
239 mod_query.COLLECT_THRESHOLD=2000
242 -- This function can be used to read completions from an external source.
243 -- The parameter \var{cp} is the completion proxy to be used,
244 -- and the string \var{cmd} the shell command to be executed. To its stdout,
245 -- the command should on the first line write the \var{common_beg}
246 -- parameter of \fnref{WComplProxy.set_completions} (which \var{fn} maybe used
247 -- to override) and a single actual completion on each of the successive lines.
248 -- The function \var{reshnd} may be used to override a result table
250 function mod_query.popen_completions(cp, cmd, fn, reshnd)
252 local pst={cp=cp, maybe_stalled=0}
255 reshnd = function(rs, a)
256 if not rs.common_beg then
264 local function rcv(str)
271 if pst.maybe_stalled>=2 then
277 totallen=totallen+string.len(str)
278 if totallen>ioncore.RESULT_DATA_LIMIT then
279 error(TR("Too much result data"))
283 data=string.gsub(data..str, "([^\n]*)\n",
290 if lines>mod_query.COLLECT_THRESHOLD then
295 str=coroutine.yield()
298 if not results.common_beg then
299 results.common_beg=beg
302 (fn or WComplProxy.set_completions)(cp, results)
310 local found_clean=false
312 for k, v in pairs(pipes) do
314 if v.maybe_stalled<2 then
315 v.maybe_stalled=v.maybe_stalled+1
321 if not found_clean then
323 ioncore.popen_bgread(cmd, coroutine.wrap(rcv))
328 local function mk_completion_test(str, sub_ok, casei_ok)
330 return function(s) return true end
333 local function mk(str, sub_ok)
335 return function(s) return string.find(s, str, 1, true) end
337 local len=string.len(str)
338 return function(s) return string.sub(s, 1, len)==str end
342 local casei=(casei_ok and mod_query.get().caseicompl)
345 return mk(str, sub_ok)
347 local fn=mk(string.lower(str), sub_ok)
348 return function(s) return fn(string.lower(s)) end
353 local function mk_completion_add(entries, str, sub_ok, casei_ok)
354 local tst=mk_completion_test(str, sub_ok, casei_ok)
358 table.insert(entries, s)
364 function mod_query.complete_keys(list, str, sub_ok, casei_ok)
366 local test_add=mk_completion_add(results, str, sub_ok, casei_ok)
368 for m, _ in pairs(list) do
376 function mod_query.complete_name(str, iter)
377 local sub_ok_first=true
380 local tst_add=mk_completion_add(entries, str, sub_ok_first, casei_ok)
387 if #entries==0 and not sub_ok_first then
388 local tst_add2=mk_completion_add(entries, str, true, casei_ok)
399 function mod_query.make_completor(completefn)
400 local function completor(cp, str, point)
401 cp:set_completions(completefn(str, point))
410 -- Simple queries for internal actions {{{
413 function mod_query.call_warn(mplex, fn)
414 local err = collect_errors(fn)
416 mod_query.warn(mplex, err)
422 function mod_query.complete_clientwin(str)
423 return mod_query.complete_name(str, ioncore.clientwin_i)
427 function mod_query.complete_workspace(str)
428 local function iter(fn)
429 return ioncore.region_i(function(obj)
430 return (not obj_is(obj, "WGroupWS")
434 return mod_query.complete_name(str, iter)
438 function mod_query.complete_region(str)
439 return mod_query.complete_name(str, ioncore.region_i)
443 function mod_query.gotoclient_handler(frame, str)
444 local cwin=ioncore.lookup_clientwin(str)
447 mod_query.warn(frame, TR("Could not find client window %s.", str))
454 function mod_query.attachclient_handler(frame, str)
455 local cwin=ioncore.lookup_clientwin(str)
458 mod_query.warn(frame, TR("Could not find client window %s.", str))
462 local reg=cwin:groupleader_of()
464 local function attach()
465 frame:attach(reg, { switchto = true })
468 if frame:rootwin_of()~=reg:rootwin_of() then
469 mod_query.warn(frame, TR("Cannot attach: different root windows."))
470 elseif reg:manager()==frame then
473 mod_query.call_warn(frame, attach)
478 function mod_query.workspace_handler(mplex, name)
479 local ws=ioncore.lookup_region(name, "WGroupWS")
483 local function create_handler(mplex_, layout)
484 if not layout or layout=="" then
488 if not ioncore.getlayout(layout) then
489 mod_query.warn(mplex_, TR("Unknown layout"))
491 local scr=mplex:screen_of()
493 local function mkws()
494 local tmpl={name=name, switchto=true}
495 if not ioncore.create_ws(scr, tmpl, layout) then
496 error(TR("Unknown error"))
500 mod_query.call_warn(mplex, mkws)
504 local function compl_layout(str)
505 local los=ioncore.getlayout(nil, true)
506 return mod_query.complete_keys(los, str, true, true)
509 mod_query.query(mplex, TR("New workspace layout (default):"), nil,
510 create_handler, mod_query.make_completor(compl_layout),
517 -- This query asks for the name of a client window and attaches
518 -- it to the frame the query was opened in. It uses the completion
519 -- function \fnref{ioncore.complete_clientwin}.
520 function mod_query.query_gotoclient(mplex)
521 mod_query.query(mplex, TR("Go to window:"), nil,
522 mod_query.gotoclient_handler,
523 mod_query.make_completor(mod_query.complete_clientwin),
528 -- This query asks for the name of a client window and switches
529 -- focus to the one entered. It uses the completion function
530 -- \fnref{ioncore.complete_clientwin}.
531 function mod_query.query_attachclient(mplex)
532 mod_query.query(mplex, TR("Attach window:"), nil,
533 mod_query.attachclient_handler,
534 mod_query.make_completor(mod_query.complete_clientwin),
540 -- This query asks for the name of a workspace. If a workspace
541 -- (an object inheriting \type{WGroupWS}) with such a name exists,
542 -- it will be switched to. Otherwise a new workspace with the
543 -- entered name will be created and the user will be queried for
544 -- the type of the workspace.
545 function mod_query.query_workspace(mplex)
546 mod_query.query(mplex, TR("Go to or create workspace:"), nil,
547 mod_query.workspace_handler,
548 mod_query.make_completor(mod_query.complete_workspace),
554 -- This query asks whether the user wants to exit Ion (no session manager)
555 -- or close the session (running under a session manager that supports such
556 -- requests). If the answer is 'y', 'Y' or 'yes', so will happen.
557 function mod_query.query_shutdown(mplex)
558 mod_query.query_yesno(mplex, TR("Exit Ion/Shutdown session (y/n)?"),
564 -- This query asks whether the user wants restart Ioncore.
565 -- If the answer is 'y', 'Y' or 'yes', so will happen.
566 function mod_query.query_restart(mplex)
567 mod_query.query_yesno(mplex, TR("Restart Ion (y/n)?"), ioncore.restart)
572 -- This function asks for a name new for the frame where the query
574 function mod_query.query_renameframe(frame)
575 mod_query.query(frame, TR("Frame name:"), frame:name(),
576 function(frame, str) frame:set_name(str) end,
582 -- This function asks for a name new for the workspace \var{ws},
583 -- or the one on which \var{mplex} resides, if it is not set.
584 -- If \var{mplex} is not set, one is looked for.
585 function mod_query.query_renameworkspace(mplex, ws)
588 mplex=ioncore.find_manager(ws, "WMPlex")
591 ws=ioncore.find_manager(mplex, "WGroupWS")
596 mod_query.query(mplex, TR("Workspace name:"), ws:name(),
597 function(mplex, str) ws:set_name(str) end,
609 -- Asks for a file to be edited. This script uses
610 -- \command{run-mailcap --mode=edit} by default, but you may provide an
611 -- alternative script to use. The default prompt is "Edit file:" (translated).
612 function mod_query.query_editfile(mplex, script, prompt)
613 mod_query.query_execfile(mplex,
614 prompt or TR("Edit file:"),
615 script or "run-mailcap --action=edit")
620 -- Asks for a file to be viewed. This script uses
621 -- \command{run-mailcap --action=view} by default, but you may provide an
622 -- alternative script to use. The default prompt is "View file:" (translated).
623 function mod_query.query_runfile(mplex, script, prompt)
624 mod_query.query_execfile(mplex,
625 prompt or TR("View file:"),
626 script or "run-mailcap --action=view")
631 local function isspace(s)
632 return string.find(s, "^%s*$")~=nil
636 local function break_cmdline(str, no_ws)
637 local st, en, beg, rest, ch, rem
640 local function ins(str)
642 if string.find(res[n], "^%s+$") then
643 table.insert(res, str)
649 local function ins_space(str)
653 table.insert(res, "")
656 if isspace(res[n]) then
659 table.insert(res, str)
664 -- Handle terminal startup syntax
665 st, en, beg, ch, rest=string.find(str, "^(%s*)(:+)(.*)")
667 if string.len(beg)>0 then
676 st, en, beg, rest, ch=string.find(str, "^(.-)(([%s'\"\\|])(.*))")
688 st, en, beg, rest=string.find(str, "^(\\.)(.*)")
690 st, en, beg, rest=string.find(str, "^(\".-[^\\]\")(.*)")
693 st, en, beg, rest=string.find(str, "^(\"\")(.*)")
696 st, en, beg, rest=string.find(str, "^('.-')(.*)")
704 st, en, beg, rest=string.find(str, "^.(%s*)(.*)")
725 local function unquote(str)
726 str=string.gsub(str, "^['\"]", "")
727 str=string.gsub(str, "([^\\])['\"]", "%1")
728 str=string.gsub(str, "\\(.)", "%1")
733 local function quote(str)
734 return string.gsub(str, "([%(%)\"'\\%*%?%[%]%| ])", "\\%1")
738 local function find_point(strs, point)
739 for i, s in ipairs(strs) do
740 point=point-string.len(s)
749 function mod_query.exec_completor(wedln, str, point)
750 local parts=break_cmdline(str)
751 local complidx=find_point(parts, point+1)
753 local s_compl, s_beg, s_end="", "", ""
755 if complidx==1 and string.find(parts[1], "^:+$") then
759 if string.find(parts[complidx], "[^%s]") then
760 s_compl=unquote(parts[complidx])
763 for i=1, complidx-1 do
764 s_beg=s_beg..parts[i]
767 for i=complidx+1, #parts do
768 s_end=s_end..parts[i]
772 if complidx==1 or (complidx==2 and isspace(parts[1])) then
774 elseif string.find(parts[1], "^:+$") then
777 elseif string.find(parts[2], "^%s*$") then
784 local function set_fn(cp, res)
785 res=table.map(quote, res)
786 res.common_beg=s_beg..(res.common_beg or "")
787 res.common_end=(res.common_end or "")..s_end
788 cp:set_completions(res)
791 local function filter_fn(res, s)
792 if not res.common_beg then
803 local ic=ioncore.lookup_script("ion-completefile")
805 mod_query.popen_completions(wedln,
806 ic..wp..string.shell_safe(s_compl),
812 local cmd_overrides={}
816 -- Define a command override for the \fnrefx{mod_query}{query_exec} query.
817 function mod_query.defcmd(cmd, fn)
818 cmd_overrides[cmd]=fn
822 function mod_query.exec_handler(mplex, cmdline)
823 local parts=break_cmdline(cmdline, true)
824 local cmd=table.remove(parts, 1)
826 if cmd_overrides[cmd] then
827 cmd_overrides[cmd](mplex, table.map(unquote, parts))
829 mod_query.exec_on_merr(mplex, cmdline)
835 -- This function asks for a command to execute with \file{/bin/sh}.
836 -- If the command is prefixed with a colon (':'), the command will
837 -- be run in an XTerm (or other terminal emulator) using the script
838 -- \file{ion-runinxterm}. Two colons ('::') will ask you to press
839 -- enter after the command has finished.
840 function mod_query.query_exec(mplex)
841 mod_query.query(mplex, TR("Run:"), nil, mod_query.exec_handler,
842 mod_query.exec_completor,
853 mod_query.known_hosts={}
854 mod_query.hostnicks={}
855 mod_query.ssh_completions={}
858 function mod_query.get_known_hosts(mplex)
859 mod_query.known_hosts={}
861 local h=os.getenv("HOME")
863 f=io.open(h.."/.ssh/known_hosts")
866 warn(TR("Failed to open ~/.ssh/known_hosts"))
869 for l in f:lines() do
870 local st, en, hostname=string.find(l, "^([^%s,]+)")
872 table.insert(mod_query.known_hosts, hostname)
879 function mod_query.get_hostnicks(mplex)
880 mod_query.hostnicks={}
882 local substr, pat, patterns
883 local h=os.getenv("HOME")
886 f=io.open(h.."/.ssh/config")
889 warn(TR("Failed to open ~/.ssh/config"))
893 for l in f:lines() do
894 _, _, substr=string.find(l, "^%s*[hH][oO][sS][tT](.*)")
896 _, _, pat=string.find(substr, "^%s*[=%s]%s*(%S.*)")
899 elseif string.find(substr, "^[nN][aA][mM][eE]")
901 for s in string.gfind(patterns, "%S+") do
902 if not string.find(s, "[*?]") then
903 table.insert(mod_query.hostnicks, s)
913 function mod_query.complete_ssh(str)
914 local st, en, user, at, host=string.find(str, "^([^@]*)(@?)(.*)$")
916 if string.len(at)==0 and string.len(host)==0 then
917 host = user; user = ""
925 local tst = mk_completion_test(host, true, false)
927 for _, v in ipairs(mod_query.ssh_completions) do
929 table.insert(res, user .. v)
938 -- This query asks for a host to connect to with SSH.
939 -- Hosts to tab-complete are read from \file{\~{}/.ssh/known\_hosts}.
940 function mod_query.query_ssh(mplex, ssh)
941 mod_query.get_known_hosts(mplex)
942 mod_query.get_hostnicks(mplex)
944 for _, v in ipairs(mod_query.known_hosts) do
945 table.insert(mod_query.ssh_completions, v)
947 for _, v in ipairs(mod_query.hostnicks) do
948 table.insert(mod_query.ssh_completions, v)
953 local function handle_exec(mplex, str)
954 if not (str and string.find(str, "[^%s]")) then
958 mod_query.exec_on_merr(mplex, ssh.." "..string.shell_safe(str))
961 return mod_query.query(mplex, TR("SSH to:"), nil, handle_exec,
962 mod_query.make_completor(mod_query.complete_ssh),
972 function mod_query.man_completor(wedln, str)
973 local mc=ioncore.lookup_script("ion-completeman")
974 local icase=(mod_query.get().caseicompl and " -icase" or "")
977 mod_query.popen_completions(wedln, (mc..icase..mid.." -complete "
978 ..string.shell_safe(str)))
984 -- This query asks for a manual page to display. By default it runs the
985 -- \command{man} command in an \command{xterm} using \command{ion-runinxterm},
986 -- but it is possible to pass another program as the \var{prog} argument.
987 function mod_query.query_man(mplex, prog)
988 local dflt=ioncore.progname()
989 mod_query.query_execwith(mplex, TR("Manual page (%s):", dflt),
990 dflt, prog or ":man",
991 mod_query.man_completor, "man",
992 true --[[ no quoting ]])
999 -- Lua code execution {{{
1002 function mod_query.create_run_env(mplex)
1003 local origenv=getfenv()
1004 local meta={__index=origenv, __newindex=origenv}
1007 _sub=mplex:current(),
1010 setmetatable(env, meta)
1014 function mod_query.do_handle_lua(mplex, env, code)
1016 local function collect_print(...)
1020 tmp=tmp..tostring(arg[i])..(i==l and "\n" or "\t")
1022 print_res=(print_res and print_res..tmp or tmp)
1025 local f, err=loadstring(code)
1027 mod_query.warn(mplex, err)
1031 env.print=collect_print
1034 err=collect_errors(f)
1036 mod_query.warn(mplex, err)
1037 elseif print_res then
1038 mod_query.message(mplex, print_res)
1042 local function getindex(t)
1043 local mt=getmetatable(t)
1044 if mt then return mt.__index end
1048 function mod_query.do_complete_lua(env, str)
1049 -- Get the variable to complete, including containing tables.
1050 -- This will also match string concatenations and such because
1051 -- Lua's regexps don't support optional subexpressions, but we
1052 -- handle them in the next step.
1055 local _, _, tocomp=string.find(str, "([%w_.:]*)$")
1057 -- Descend into tables
1058 if tocomp and string.len(tocomp)>=1 then
1059 for t in string.gfind(tocomp, "([^.:]*)[.:]") do
1061 if string.len(t)==0 then
1064 if type(comptab[t])=="table" then
1066 elseif type(comptab[t])=="userdata" then
1067 comptab=getindex(comptab[t])
1076 if not comptab then return {} end
1080 -- Get the actual variable to complete without containing tables
1081 _, _, compl.common_beg, tocomp=string.find(str, "(.-)([%w_]*)$")
1083 local l=string.len(tocomp)
1088 if type(tab) == "table" then
1089 for k in pairs(tab) do
1090 if type(k)=="string" then
1091 if string.sub(k, 1, l)==tocomp then
1092 table.insert(compl, k)
1098 -- We only want to display full list of functions for objects, not
1099 -- the tables representing the classes.
1100 --if not metas then break end
1104 if not tab or seen[tab] then break end
1107 -- If there was only one completion and it is a string or function,
1108 -- concatenate it with "." or "(", respectively.
1110 if type(comptab[compl[1]])=="table" then
1111 compl[1]=compl[1] .. "."
1112 elseif type(comptab[compl[1]])=="function" then
1113 compl[1]=compl[1] .. "("
1122 -- This query asks for Lua code to execute. It sets the variable '\var{\_}'
1123 -- in the local environment of the string to point to the mplex where the
1124 -- query was created. It also sets the table \var{arg} in the local
1125 -- environment to \code{\{_, _:current()\}}.
1126 function mod_query.query_lua(mplex)
1127 local env=mod_query.create_run_env(mplex)
1129 local function complete(cp, code)
1130 cp:set_completions(mod_query.do_complete_lua(env, code))
1133 local function handler(mplex, code)
1134 return mod_query.do_handle_lua(mplex, env, code)
1137 mod_query.query(mplex, TR("Lua code:"), nil, handler, complete, "lua")
1146 -- This query can be used to create a query of a defined menu.
1147 function mod_query.query_menu(mplex, sub, themenu, prompt)
1148 if type(sub)=="string" then
1149 -- Backwards compat. shift
1155 local menu=ioncore.evalmenu(themenu, mplex, sub)
1156 local menuname=(type(themenu)=="string" and themenu or "?")
1159 mod_query.warn(mplex, TR("Unknown menu %s.", tostring(themenu)))
1164 prompt=menuname..":"
1169 local function xform_name(n, is_submenu)
1170 return string.lower(string.gsub(n, "[-/%s]+", "-"))
1173 local function xform_menu(t, m, p)
1174 for _, v in ipairs(m) do
1176 local is_submenu=v.submenu_fn
1177 local n=p..xform_name(v.name)
1179 while t[n] or t[n..'/'] do
1189 if is_submenu and not v.noautoexpand then
1190 local sm=v.submenu_fn()
1192 xform_menu(t, sm, n)
1194 ioncore.warn_traced(TR("Missing submenu ")
1203 local ntab=xform_menu({}, menu, "")
1205 local function complete(str)
1206 -- casei_ok false, because everything is already in lower case
1207 return mod_query.complete_keys(ntab, str, true, false)
1210 local function handle(mplex, str)
1214 local err=collect_errors(function()
1218 mod_query.warn(mplex, err)
1220 elseif e.submenu_fn then
1221 mod_query.query_menu(mplex, e.submenu_fn(),
1225 mod_query.warn(mplex, TR("No entry '%s'", str))
1229 mod_query.query(mplex, prompt, nil, handle,
1230 mod_query.make_completor(complete), "menu."..menuname)
1236 -- Miscellaneous {{{
1240 -- Display an "About Ion" message in \var{mplex}.
1241 function mod_query.show_about_ion(mplex)
1242 mod_query.message(mplex, ioncore.aboutmsg())
1247 -- Show information about a region tree
1248 function mod_query.show_tree(mplex, reg, max_depth)
1249 local function indent(s)
1251 return i..string.gsub(s, "\n", "\n"..i)
1254 local function get_info(reg, indent, d)
1256 return (indent .. "No region")
1259 local function n(s) return (s or "") end
1261 local s=string.format("%s%s \"%s\"", indent, obj_typename(reg),
1263 indent = indent .. " "
1264 if obj_is(reg, "WClientWin") then
1265 local i=reg:get_ident()
1266 s=s .. TR("\n%sClass: %s\n%sRole: %s\n%sInstance: %s\n%sXID: 0x%x",
1269 indent, n(i.instance),
1273 if (not max_depth or max_depth > d) and reg.managed_i then
1275 reg:managed_i(function(sub)
1277 s=s .. "\n" .. indent .. "---"
1280 s=s .. "\n" .. get_info(sub, indent, d+1)
1288 mod_query.message(mplex, get_info(reg, "", 0))
1294 dopath('mod_query_chdir')
1296 -- Mark ourselves loaded.
1297 package.loaded["mod_query"]=true
1300 -- Load configuration file
1301 dopath('cfg_query', true)