File: mod_query.lua

package info (click to toggle)
notion 4.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,656 kB
  • sloc: ansic: 47,365; sh: 2,093; makefile: 594; perl: 270
file content (1361 lines) | stat: -rw-r--r-- 36,018 bytes parent folder | download | duplicates (3)
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
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
--
-- ion/query/mod_query.lua -- Some common queries for Ion
--
-- Copyright (c) Tuomo Valkonen 2004-2007.
--
-- See the included file LICENSE for details.
--


-- This is a slight abuse of the package.loaded variable perhaps, but
-- library-like packages should handle checking if they're loaded instead of
-- confusing the user with require/include differences.
if package.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 loadstring = loadstring or load

local DIE_TIMEOUT_ERRORCODE=10 -- 10 seconds
local DIE_TIMEOUT_NO_ERRORCODE=2 -- 2 seconds


-- Generic helper functions {{{


--DOC
-- Display an error message box in the multiplexer \var{mplex}.
function mod_query.warn(mplex, str)
    ioncore.unsqueeze(mod_query.do_warn(mplex, str))
end


--DOC
-- Display a message in \var{mplex}.
function mod_query.message(mplex, str)
    ioncore.unsqueeze(mod_query.do_message(mplex, str))
end


--DOC
-- Low-level query routine. \var{mplex} is the \type{WMPlex} to display
-- the query in, \var{prompt} the prompt string, and \var{initvalue}
-- the initial contents of the query box. \var{handler} is a function
-- that receives (\var{mplex}, result string) as parameter when the
-- query has been succesfully completed, \var{completor} the completor
-- routine which receives a (\var{cp}, \var{str}, \var{point}) as parameters.
-- The parameter \var{str} is the string to be completed and \var{point}
-- cursor's location within it. Completions should be eventually,
-- possibly asynchronously, set with \fnref{WComplProxy.set_completions}
-- on \var{cp}.
function mod_query.query(mplex, prompt, initvalue, handler, completor,
                         context)
    local function handle_it(str)
        handler(mplex, str)
    end
    local function cycle(wedln)
        wedln:complete('next', 'normal')
    end
    local function bcycle(wedln)
        wedln:complete('prev', 'normal')
    end

    -- Check that no other queries are open in the mplex.
    local ok=mplex:managed_i(function(r)
                                 return not obj_is(r, "WEdln")
                             end)
    if not ok then
        return
    end

    local wedln=mod_query.do_query(mplex, prompt, initvalue,
                                   handle_it, completor, cycle, bcycle)

    if wedln then
        ioncore.unsqueeze(wedln)

        if context then
            wedln:set_context(context)
        end
    end

    return wedln
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 maybe_finish(pid)
    local t=errdata[pid]

    if t and t.closed and t.dietime then
        errdata[pid]=nil
        local tmd=os.difftime(t.dietime, t.starttime)
        --if tmd<DIE_TIMEOUT_ERRORCODE and t.signaled then
        --    local msg=TR("Program received signal ")..t.termsig.."\n"
        --    mod_query.warn(t.mplex, msg..(t.errs or ""))
        --else
        if ((tmd<DIE_TIMEOUT_ERRORCODE and (t.hadcode or t.signaled)) or
                (tmd<DIE_TIMEOUT_NO_ERRORCODE)) and t.errs then
            mod_query.warn(t.mplex, t.errs)
        end
    end
end


local badsig_={4, 5, 6, 7, 8, 11}
local badsig={}
for _, v in pairs(badsig_) do
    badsig[v]=true
end

local function chld_handler(p)
    local t=errdata[p.pid]
    if t then
        t.dietime=os.time()
        t.signaled=(p.signaled and badsig[p.termsig])
        t.termsig=p.termsig
        t.hadcode=(p.exited and p.exitstatus~=0)
        maybe_finish(pid)
    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
                else
                    t.closed=true
                    maybe_finish(pid)
                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();
    local tmd=math.max(DIE_TIMEOUT_NO_ERRORCODE, DIE_TIMEOUT_ERRORCODE)
    local now=os.time()
    tmr:set(tmd*1000, timeout)

    errdata[pid]={tmr=tmr, mplex=mplex, starttime=now}
end


function mod_query.file_completor(wedln, str)
    local ic=ioncore.lookup_script("ion-completefile")
    if ic then
        mod_query.popen_completions(wedln,
                                   ic.." "..string.shell_safe(str))
    end
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


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, noquote)
    local function handle_execwith(frame, str)
        if not str or str=="" then
            str=dflt
        end
        local args=(noquote and str or string.shell_safe(str))
        mod_query.exec_on_merr(mplex, prog.." "..args)
    end
    return mod_query.query(mplex, prompt, nil, handle_execwith, completor,
                           context)
end


-- }}}


-- Completion helpers {{{

local pipes={}

mod_query.COLLECT_THRESHOLD=2000

--DOC
-- This function can be used to read completions from an external source.
-- The parameter \var{cp} is the completion proxy to be used,
-- and the string \var{cmd} the shell command to be executed. To its stdout,
-- the command should on the first line write the \var{common_beg}
-- parameter of \fnref{WComplProxy.set_completions} (which \var{fn} maybe used
-- to override) and a single actual completion on each of the successive lines.
-- The function \var{reshnd} may be used to override a result table
-- building routine.
function mod_query.popen_completions(cp, cmd, fn, reshnd)

    local pst={cp=cp, maybe_stalled=0}

    if not reshnd then
        reshnd = function(rs, a)
                     if not rs.common_beg then
                         rs.common_beg=a
                     else
                         table.insert(rs, a)
                     end
                 end
    end

    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(s)
                                 reshnd(results, s)
                                 lines=lines+1
                                 return ""
                             end)

            if lines>mod_query.COLLECT_THRESHOLD then
                collectgarbage()
                lines=0
            end

            str=coroutine.yield()
        end

        if not results.common_beg then
            results.common_beg=beg
        end

        (fn or WComplProxy.set_completions)(cp, results)

        pipes[rcv]=nil
        results={}

        collectgarbage()
    end

    local found_clean=false

    for k, v in pairs(pipes) do
        if v.cp==cp 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


local function mk_completion_test(str, sub_ok, casei_ok)
    local settings=mod_query.get()

    if not str then
        return function(s) return true end
    end

    local function mk(str, sub_ok)
        if sub_ok then
            return function(s) return string.find(s, str, 1, true) end
        else
            local len=string.len(str)
            return function(s) return string.sub(s, 1, len)==str end
        end
    end

    local casei=(casei_ok and mod_query.get().caseicompl)

    if not casei then
        return mk(str, sub_ok)
    else
        local fn=mk(string.lower(str), sub_ok)
        return function(s) return fn(string.lower(s)) end
    end
end


local function mk_completion_add(entries, str, sub_ok, casei_ok)
    local tst=mk_completion_test(str, sub_ok, casei_ok)

    return function(s)
               if s and tst(s) then
                   table.insert(entries, s)
               end
           end
end


function mod_query.complete_keys(list, str, sub_ok, casei_ok)
    local results={}
    local test_add=mk_completion_add(results, str, sub_ok, casei_ok)

    for m, _ in pairs(list) do
        test_add(m)
    end

    return results
end


function mod_query.complete_name(str, iter)
    local sub_ok_first=true
    local casei_ok=true
    local entries={}
    local tst_add=mk_completion_add(entries, str, sub_ok_first, casei_ok)

    iter(function(reg)
             tst_add(reg:name())
             return true
         end)

    if #entries==0 and not sub_ok_first then
        local tst_add2=mk_completion_add(entries, str, true, casei_ok)
        iter(function(reg)
                 tst_add2(reg:name())
                 return true
             end)
    end

    return entries
end


function mod_query.make_completor(completefn)
    local function completor(cp, str, point)
        cp:set_completions(completefn(str, point))
    end
    return completor
end


-- }}}


-- Simple queries for internal actions {{{


function mod_query.call_warn(mplex, fn)
    local err = collect_errors(fn)
    if err then
        mod_query.warn(mplex, err)
    end
    return err
end


function mod_query.complete_clientwin(str)
    return mod_query.complete_name(str, ioncore.clientwin_i)
end


function mod_query.complete_workspace(str)
    local function iter(fn)
        return ioncore.region_i(function(obj)
                                    return (not obj_is(obj, "WGroupWS")
                                            or fn(obj))
                                end)
    end
    return mod_query.complete_name(str, iter)
end


function mod_query.complete_region(str)
    return mod_query.complete_name(str, ioncore.region_i)
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_focus()
    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))
        return
    end

    local reg=cwin:groupleader_of()

    local function attach()
        frame:attach(reg, { switchto = true })
    end

    if frame:rootwin_of()~=reg:rootwin_of() then
        mod_query.warn(frame, TR("Cannot attach: different root windows."))
    elseif reg:manager()==frame then
        reg:goto_focus()
    else
        mod_query.call_warn(frame, attach)
    end
end


function mod_query.workspace_handler(mplex, name)
    local ws=ioncore.lookup_region(name, "WGroupWS")
    if ws then
        ws:goto_focus()
    else
        local function create_handler(mplex_, layout)
            if not layout or layout=="" then
                layout="default"
            end

            if not ioncore.getlayout(layout) then
                mod_query.warn(mplex_, TR("Unknown layout"))
            else
                local scr=mplex:screen_of()

                local function mkws()
                    local tmpl={name=name, switchto=true}
                    if not ioncore.create_ws(scr, tmpl, layout) then
                        error(TR("Unknown error"))
                    end
                end

                mod_query.call_warn(mplex, mkws)
            end
        end

        local function compl_layout(str)
            local los=ioncore.getlayout(nil, true)
            return mod_query.complete_keys(los, str, true, true)
        end

        mod_query.query(mplex, TR("New workspace layout (default):"), nil,
                        create_handler, mod_query.make_completor(compl_layout),
                        "workspacelayout")
    end
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_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 attaches
-- it to the frame the query was opened in. 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{WGroupWS}) 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 Notion/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 Notion (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 \var{ws},
-- or the one on which \var{mplex} resides, if it is not set.
-- If \var{mplex} is not set, one is looked for.
function mod_query.query_renameworkspace(mplex, ws)
    if not mplex then
        assert(ws)
        mplex=ioncore.find_manager(ws, "WMPlex")
    elseif not ws then
        assert(mplex)
        ws=ioncore.find_manager(mplex, "WGroupWS")
    end

    assert(mplex and ws)

    mod_query.query(mplex, TR("Workspace name:"), ws:name(),
                    function(mplex, str) ws:set_name(str) end,
                    nil, "framename")
end


-- }}}


-- Run/view/edit {{{


--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{xdg-open} 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 "xdg-open")

end


local function isspace(s)
    return string.find(s, "^%s*$")~=nil
end


local function break_cmdline(str, no_ws)
    local st, en, beg, rest, ch, rem
    local res={""}

    local function ins(str)
        local n=#res
        if string.find(res[n], "^%s+$") then
            table.insert(res, str)
        else
            res[n]=res[n]..str
        end
    end

    local function ins_space(str)
        local n=#res
        if no_ws then
            if res[n]~="" then
                table.insert(res, "")
            end
        else
            if isspace(res[n]) then
                res[n]=res[n]..str
            else
                table.insert(res, str)
            end
        end
    end

    -- Handle terminal startup syntax
    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
            if ch=='|' then
                ins_space('')
                ins(ch)
            else -- ch==' '
                ins_space(ch)
            end
            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


local function find_point(strs, point)
    for i, s in ipairs(strs) do
        point=point-string.len(s)
        if point<=1 then
            return i
        end
    end
    return #strs
end


function mod_query.exec_completor(wedln, str, point)
    local parts=break_cmdline(str)
    local complidx=find_point(parts, point+1)

    local s_compl, s_beg, s_end="", "", ""

    if complidx==1 and string.find(parts[1], "^:+$") then
        complidx=complidx+1
    end

    if string.find(parts[complidx], "[^%s]") then
        s_compl=unquote(parts[complidx])
    end

    for i=1, complidx-1 do
        s_beg=s_beg..parts[i]
    end

    for i=complidx+1, #parts do
        s_end=s_end..parts[i]
    end

    local wp=" "
    if complidx==1 or (complidx==2 and isspace(parts[1])) then
        wp=" -wp "
    elseif string.find(parts[1], "^:+$") then
        if complidx==2 then
            wp=" -wp "
        elseif string.find(parts[2], "^%s*$") then
            if complidx==3 then
                wp=" -wp "
            end
        end
    end

    local function set_fn(cp, res)
        res=table.map(quote, res)
        res.common_beg=s_beg..(res.common_beg or "")
        res.common_end=(res.common_end or "")..s_end
        cp:set_completions(res)
    end

    local function filter_fn(res, s)
        if not res.common_beg then
            if s=="./" then
                res.common_beg=""
            else
                res.common_beg=s
            end
        else
            table.insert(res, s)
        end
    end

    local ic=ioncore.lookup_script("ion-completefile")
    if ic then
        mod_query.popen_completions(wedln,
                                   ic..wp..string.shell_safe(s_compl),
                                   set_fn, filter_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={}
mod_query.hostnicks={}
mod_query.ssh_completions={}


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.get_hostnicks(mplex)
    mod_query.hostnicks={}
    local f
    local substr, pat, patterns
    local h=os.getenv("HOME")

    if h then
        f=io.open(h.."/.ssh/config")
    end
    if not f then
        warn(TR("Failed to open ~/.ssh/config"))
        return
    end

    for l in f:lines() do
        _, _, substr=string.find(l, "^%s*[hH][oO][sS][tT](.*)")
        if substr then
            _, _, pat=string.find(substr, "^%s*[=%s]%s*(%S.*)")
            if pat then
                patterns=pat
            elseif string.find(substr, "^[nN][aA][mM][eE]")
                and patterns then
                for s in string.gmatch(patterns, "%S+") do
                    if not string.find(s, "[*?]") then
                        table.insert(mod_query.hostnicks, s)
                    end
                end
            end
        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 = {}
    local tst = mk_completion_test(host, true, false)

    for _, v in ipairs(mod_query.ssh_completions) do
        if tst(v) 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)
    mod_query.get_hostnicks(mplex)

    for _, v in ipairs(mod_query.known_hosts) do
        table.insert(mod_query.ssh_completions, v)
    end
    for _, v in ipairs(mod_query.hostnicks) do
        table.insert(mod_query.ssh_completions, v)
    end

    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")
    local icase=(mod_query.get().caseicompl and " -icase" or "")
    local mid=""
    if mc then
        mod_query.popen_completions(wedln, (mc..icase..mid.." -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",
                             true --[[ no quoting ]])
end


-- }}}


-- Lua code execution {{{


function mod_query.create_run_env(mplex)
    local origenv
    if _ENV then origenv=_ENV else origenv=getfenv() end
    local meta={__index=origenv, __newindex=origenv}
    local env={
        _=mplex,
        _sub=mplex:current(),
        print=my_print
    }
    setmetatable(env, meta)
    return env
end

function mod_query.do_handle_lua(mplex, env, code)
    local print_res
    local function collect_print(...)
        local tmp=""
        local arg={...}
        local l=#arg
        for i=1,l do
            tmp=tmp..tostring(arg[i])..(i==l and "\n" or "\t")
        end
        print_res=(print_res and print_res..tmp or tmp)
    end


    if _ENV then
        env.print=collect_print
        local f, err=load(code,nil, nil, env)
        if not f then
            mod_query.warn(mplex, err)
            return
        end
        err=collect_errors(f)
    else
        local f, err=loadstring(code)
        if not f then
            mod_query.warn(mplex, err)
            return
        end
        env.print=collect_print
        setfenv(f, env)
        err=collect_errors(f)
    end
    if err then
        mod_query.warn(mplex, err)
    elseif print_res then
        mod_query.message(mplex, print_res)
    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.gmatch(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_beg, tocomp=string.find(str, "(.-)([%w_]*)$")

    local l=string.len(tocomp)

    local tab=comptab
    local seen={}
    while true do
        if type(tab) == "table" then
            for k in pairs(tab) do
                if type(k)=="string" then
                    if string.sub(k, 1, l)==tocomp then
                        table.insert(compl, k)
                    end
                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 #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(cp, code)
        cp: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, sub, themenu, prompt)
    if type(sub)=="string" then
        -- Backwards compat. shift
        prompt=themenu
        themenu=sub
        sub=nil
    end

    local menu=ioncore.evalmenu(themenu, mplex, sub)
    local menuname=(type(themenu)=="string" and themenu or "?")

    if not menu then
        mod_query.warn(mplex, TR("Unknown menu %s.", tostring(themenu)))
        return
    end

    if not prompt then
        prompt=menuname..":"
    else
        prompt=TR(prompt)
    end

    local function xform_name(n, is_submenu)
        return string.lower(string.gsub(n, "[-/%s]+", "-"))
    end

    local function xform_menu(t, m, p)
        for _, v in ipairs(m) do
            if v.name then
                local is_submenu=v.submenu_fn
                local n=p..xform_name(v.name)

                while t[n] or t[n..'/'] do
                    n=n.."'"
                end

                if is_submenu then
                    n=n..'/'
                end

                t[n]=v

                if is_submenu and not v.noautoexpand then
                    local sm=v.submenu_fn()
                    if sm then
                        xform_menu(t, sm, n)
                    else
                        ioncore.warn_traced(TR("Missing submenu ")
                                            ..(v.name or ""))
                    end
                end
            end
        end
        return t
    end

    local ntab=xform_menu({}, menu, "")

    local function complete(str)
        -- casei_ok false, because everything is already in lower case
        return mod_query.complete_keys(ntab, str, true, false)
    end

    local function handle(mplex, str)
        local e=ntab[str]
        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, e.submenu_fn(),
                                     TR("%s:", e.name))
            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."..menuname)
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 region tree
function mod_query.show_tree(mplex, reg, max_depth)
    local function indent(s)
        local i="    "
        return i..string.gsub(s, "\n", "\n"..i)
    end

    local function get_info(reg, indent, d)
        if not reg then
            return (indent .. "No region")
        end

        local function n(s) return (s or "") end

        local s=string.format("%s%s \"%s\"", indent, obj_typename(reg),
                              n(reg:name()))
        indent = indent .. "  "
        if obj_is(reg, "WClientWin") then
            local i=reg:get_ident()
            s=s .. TR("\n%sClass: %s\n%sRole: %s\n%sInstance: %s\n%sXID: 0x%x",
                      indent, n(i.class),
                      indent, n(i.role),
                      indent, n(i.instance),
                      indent, reg:xid())
        end

        if (not max_depth or max_depth > d) and reg.managed_i then
            local first=true
            reg:managed_i(function(sub)
                              if first then
                                  s=s .. "\n" .. indent .. "---"
                                  first=false
                              end
                              s=s .. "\n" .. get_info(sub, indent, d+1)
                              return true
                          end)
        end

        return s
    end

    mod_query.message(mplex, get_info(reg, "", 0))
end

function binding_to_str(ctx, binding)
    if binding.kcb then
        return binding.kcb .. "@" .. ctx .. " " .. binding.doc
    else
        return ctx .. " " .. binding.doc
    end
end

function str_to_binding(str)
    for ctx,bindmap in pairs(notioncore.do_getbindings()) do
        for _,binding in pairs(bindmap) do
            if binding.doc then
                local b_str = binding_to_str(ctx, binding)
                if str==b_str then
                    return binding
                end
            end
        end
    end
end

function mod_query.binding_handler(frame, str, binding)
    str_to_binding(str).func(frame)
end

function mod_query.complete_binding(str)
    local entries={}
    local test_add=mk_completion_add(entries, str, true, true)

    for ctx,bindmap in pairs(notioncore.do_getbindings()) do
        for _,binding in pairs(bindmap) do
            if binding.doc then
                test_add(binding_to_str(ctx, binding))
            end
        end
    end

    return entries
end

--DOC
-- Find keybindings
function mod_query.query_binding(mplex, sub)
    mod_query.query(mplex, TR("Find keybinding:"), nil,
        mod_query.binding_handler,
        mod_query.make_completor(mod_query.complete_binding),
        ""
)
end

-- }}}

-- Load extras
dopath('mod_query_chdir')

-- Mark ourselves loaded.
package.loaded["mod_query"]=true


-- Load configuration file
dopath('cfg_query', true)