-
Notifications
You must be signed in to change notification settings - Fork 25
/
tclreadlineCompleter.tcl
6422 lines (5960 loc) · 229 KB
/
tclreadlineCompleter.tcl
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
# -*- tclsh -*-
# FILE: tclreadlineCompleter.tcl
# $Id$
# ---
# tclreadline -- gnu readline for tcl
# https://github.com/flightaware/tclreadline/
# Copyright (c) 1998 - 2014, Johannes Zellner <johannes@zellner.org>
# This software is copyright under the BSD license.
# ---
# TODO:
#
# - tcltest is missing
# - better completion for CompleteListFromList:
# RemoveUsedOptions ...
# - namespace eval fred {... <-- continue with a
# substitution in fred.
# - set tclreadline::pro<tab> doesn't work
# set ::tclreadline::pro<tab> does
#
# - TextObj ...
#
namespace eval tclreadline {
# the following three are from the icccm
# and used in complete(selection) and
# descendants.
#
variable selection-selections {
PRIMARY SECONDARY CLIPBOARD
}
variable selection-types {
ADOBE_PORTABLE_DOCUMENT_FORMAT
APPLE_PICT
BACKGROUND
BITMAP
CHARACTER_POSITION
CLASS
CLIENT_WINDOW
COLORMAP
COLUMN_NUMBER
COMPOUND_TEXT
DELETE
DRAWABLE
ENCAPSULATED_POSTSCRIPT
ENCAPSULATED_POSTSCRIPT_INTERCHANGE
FILE_NAME
FOREGROUND
HOST_NAME
INSERT_PROPERTY
INSERT_SELECTION
LENGTH
LINE_NUMBER
LIST_LENGTH
MODULE
MULTIPLE
NAME
ODIF
OWNER_OS
PIXMAP
POSTSCRIPT
PROCEDURE
PROCESS
STRING
TARGETS
TASK
TEXT
TIMESTAMP
USER
}
variable selection-formats {
APPLE_PICT
ATOM
ATOM_PAIR
BITMAP
COLORMAP
COMPOUND_TEXT
DRAWABLE
INTEGER
NULL
PIXEL
PIXMAP7
SPAN
STRING
TEXT
WINDOW
}
namespace export \
TryFromList CompleteFromList DisplayHints Rehash \
PreviousWord CommandCompletion RemoveUsedOptions \
HostList ChannelId InChannelId OutChannelId \
Lindex Llength CompleteBoolean WidgetChildren
# set tclreadline::trace to 1, if you
# want to enable explicit trace calls.
#
variable trace
# set tclreadline::trace_procs to 1, if you
# want to enable tracing every entry to a proc.
#
variable trace_procs
if {[info exists trace_procs] && $trace_procs} {
::proc proc {name arguments body} {
::proc $name $arguments [subst -nocommands {
TraceText [lrange [info level 0] 1 end]
$body
}]
}
} else { ;# !$trace_procs
catch {rename ::tclreadline::proc ""}
}
if {[info exists trace] && $trace} {
::proc TraceReconf {args} {
eval .tclreadline_trace.scroll set $args
.tclreadline_trace.text see end
}
::proc AssureTraceWindow {} {
variable trace
if {![info exists trace]} {
return 0
}
if {!$trace} {
return 0
}
if {[catch {package require Tk}]} {
return 0
}
if {![winfo exists .tclreadline_trace.text]} {
toplevel .tclreadline_trace
text .tclreadline_trace.text \
-yscrollcommand { tclreadline::TraceReconf } \
-wrap none
scrollbar .tclreadline_trace.scroll \
-orient vertical \
-command { .tclreadline_trace.text yview }
pack .tclreadline_trace.text -side left -expand yes -fill both
pack .tclreadline_trace.scroll -side right -expand yes -fill y
} else {
raise .tclreadline_trace
}
return 1
}
::proc TraceVar vT {
if {![AssureTraceWindow]} {
return
}
upvar $vT v
if {[info exists v]} {
.tclreadline_trace.text insert end \
"([lindex [info level -1] 0]) $vT=|$v|\n"
}
# silently ignore unset variables.
}
::proc TraceText txt {
if {![AssureTraceWindow]} {
return
}
.tclreadline_trace.text insert end \
[format {%32s %s} ([lindex [info level -1] 0]) $txt\n]
}
} else {
::proc TraceReconf args {}
::proc AssureTraceWindow args {}
::proc TraceVar args {}
::proc TraceText args {}
}
#**
# TryFromList will return an empty string, if
# the text typed so far does not match any of the
# elements in list. This might be used to allow
# subsequent filename completion by the builtin
# completer.
# If inhibit is non-zero, the result will be
# formatted such that readline will not insert
# a space after a complete (single) match.
#
proc TryFromList {text lst {allow ""} {inhibit 0}} {
set pre [GetQuotedPrefix $text]
set matches [MatchesFromList $text $lst $allow]
if {1 == [llength $matches]} { ; # unique match
set null [string index $matches 0]
if {("<" == $null || "?" == $null)
&& -1 == [string first $null $allow]} {
set completion [string trim "[list $text] $lst"]
} else {
set completion [string trim ${pre}${matches}[Right $pre]]
}
if {$inhibit} {
return [list $completion {}]
} else {
return $completion
}
} elseif {"" != $matches} {
set longest [CompleteLongest $matches]
if {"" == $longest} {
return [string trim "[list $text] $matches"]
} else {
return [string trim "${pre}${longest} $matches"]
}
} else {
return ""; # nothing to complete
}
}
#**
# CompleteFromList will never return an empty string.
# completes, if a completion can be done, or ring
# the bell if not. If inhibit is non-zero, the result
# will be formatted such that readline will not insert
# a space after a complete (single) match.
#
proc CompleteFromList {text lst {allow ""} {inhibit 0}} {
set result [TryFromList $text $lst $allow $inhibit]
if {![llength $result]} {
Alert
# return [string trim [list $text] $lst"]
if {[llength $lst]} {
return [string trim "$text $lst"]
} else {
return [string trim [list $text {}]]
}
} else {
return $result
}
}
#**
# CompleteBoolean does a CompleteFromList
# with a list of all valid boolean values.
#
proc CompleteBoolean {text} {
return [CompleteFromList $text {yes no true false 1 0}]
}
#**
# build a list of all executables which can be
# found in $env(PATH). This is (naturally) a bit
# slow, and should not called frequently. Instead
# it is a good idea to check if the variable
# `executables' exists and then just use it's
# content instead of calling Rehash.
# (see complete(exec)).
#
proc Rehash {} {
global env
variable executables
if {![info exists env] || ![array exists env]} {
return
}
if {![info exists env(PATH)]} {
return
}
set executables 0
foreach dir [split $env(PATH) :] {
if {[catch [list set files [glob -nocomplain ${dir}/*]]]} { continue }
foreach file $files {
if {[file executable $file]} {
lappend executables [file tail $file]
}
}
}
}
#**
# build a list hosts from the /etc/hosts file.
# this is only done once. This is sort of a
# dirty hack, /etc/hosts is hardcoded ...
# But on the other side, if the user supplies
# a valid host table in tclreadline::hosts
# before entering the event loop, this proc
# will return this list.
#
proc HostList {} {
# read the host table only once.
#
variable hosts
if {![info exists hosts]} {
catch {
set hosts ""
set id [open /etc/hosts r]
if {0 != $id} {
while {-1 != [gets $id line]} {
regsub {#.*} $line {} line
if {[llength $line] >= 2} {
lappend hosts [lindex $line 1]
}
}
close $id
}
}
}
return $hosts
}
#**
# never return an empty string, never complete.
# This is useful for showing options lists for example.
#
proc DisplayHints {lst} {
return [string trim "{} $lst"]
}
#**
# find (partial) matches for `text' in `lst'. Ring
# the bell and return the whole list, if the user
# tries to complete ?..? options or <..> hints.
#
# MatchesFromList returns a list which is not suitable
# for passing to the readline completer. Thus,
# MatchesFromList should not be called directly but
# from formatting routines as TryFromList.
#
proc MatchesFromList {text lst {allow ""}} {
set result ""
set text [StripPrefix $text]
set null [string index $text 0]
foreach char {< ?} {
if {$char == $null && -1 == [string first $char $allow]} {
Alert
return $lst
}
}
foreach word $lst {
if {[string match ${text}* $word]} {
lappend result $word
}
}
return [string trim $result]
}
#**
# invoke cmd with a (hopefully) invalid string and
# parse the error message to get an option list.
# The strings are carefully chosen to match the
# results produced by known tcl routines. It's a
# pity, that not all object commands generate
# standard error messages!
#
# @param cmd
# @return list of options for cmd
#
proc TrySubCmds {text cmd} {
set trystring ----
# try the command with and w/o trystring.
# Some commands, e.g.
# .canvas bind
# return an error if invoked w/o arguments
# but not, if invoked with arguments. Breaking
# the loop is eventually done at the end ...
#
for {set str $trystring} {1} {set str ""} {
set code [catch {set result [eval $cmd $str]} msg]
set result ""
if {$code} {
set tcmd [string trim $cmd]
# XXX see
# tclIndexObj.c
# tkImgPhoto.c
# XXX
if {[regexp \
{(bad|ambiguous|unrecognized) .*"----": *must *be( .*$)} \
$msg all junk raw]} {
regsub -all -- , $raw { } raw
set len [llength $raw]
set len_2 [expr {$len - 2}]
for {set i 0} {$i < $len} {incr i} {
set word [lindex $raw $i]
if {"or" != $word && $i != $len_2} {
lappend result $word
}
}
if {[string length $result]
&& -1 == [string first $trystring $result]} {
return [TryFromList $text $result]
}
} elseif {[regexp \
"wrong # args: should be \"?${tcmd}\[^ \t\]*\(.*\[^\"\]\)" \
$msg all hint]} {
# XXX see tclIndexObj.c XXX
if {-1 == [string first $trystring $hint]} {
return [DisplayHints [list <[string trim $hint]>]]
}
} else {
# check, if it's a blt error msg ...
#
set msglst [split $msg \n]
foreach line $msglst {
if {[regexp "${tcmd}\[ \t\]\+\(\[^ \t\]*\)\[^:\]*$" \
$line all sub]} {
lappend result [list $sub]
}
}
if {[string length $result]
&& -1 == [string first $trystring $result]} {
return [TryFromList $text $result]
}
}
}
if {"" == $str} {
break
}
}
return ""
}
#**
# try to get classes for commands which
# allow `configure' (cget).
# @param command.
# @param optionsT where the table will be stored.
# @return number of options
#
proc ClassTable {cmd} {
# first we build an option table.
# We always use `configure' here,
# because cget will not return the
# option table.
#
if {[catch [list set option_table [eval $cmd configure]] msg]} {
return ""
}
set classes ""
foreach optline $option_table {
if {5 != [llength $optline]} {
continue
} else {
lappend classes [lindex $optline 2]
}
}
return $classes
}
#**
# try to get options for commands which
# allow `configure' (cget).
# @param command.
# @param optionsT where the table will be stored.
# @return number of options
#
proc OptionTable {cmd optionsT} {
upvar $optionsT options
# first we build an option table.
# We always use `configure' here,
# because cget will not return the
# option table.
#
if {[catch [list set option_table [eval $cmd configure]] msg]} {
return 0
}
set retval 0
foreach optline $option_table {
if {5 == [llength $optline]} {
# tk returns a list of length 5
lappend options(switches) [lindex $optline 0]
lappend options(value) [lindex $optline 4]
incr retval
} elseif {3 == [llength $optline]} {
# itcl returns a list of length 3
lappend options(switches) [lindex $optline 0]
lappend options(value) [lindex $optline 2]
incr retval
}
}
return $retval
}
#**
# try to complete a `cmd configure|cget ..' from the command's options.
# @param text start line cmd, standard tclreadlineCompleter arguments.
# @return -- a flag indicating, if (cget|configure) was found.
# @return resultT -- a tclreadline completer formatted string.
#
proc CompleteFromOptions {text start line resultT} {
upvar $resultT result
set result ""
# check if either `configure' or `cget' is present.
#
set lst [ProperList $line]
foreach keyword {configure cget} {
set idx [lsearch $lst $keyword]
if {-1 != $idx} {
break
}
}
if {-1 == $idx} {
return 0
}
if {[regexp {(cget|configure)$} $line]} {
# we are at the end of (configure|cget)
# but there's no space yet.
#
set result $text
return 1
}
# separate the command, but exclude (cget|configure)
# because cget won't return the option table. Instead
# OptionTable always uses `configure' to get the
# option table.
#
set cmd [lrange $lst 0 [expr {$idx - 1}]]
TraceText $cmd
if {0 < [OptionTable $cmd options]} {
set prev [PreviousWord $start $line]
if {-1 != [set found [lsearch -exact $options(switches) $prev]]} {
# complete only if the user has not
# already entered something here.
#
if {![llength $text]} {
# check first, if the SpecificSwitchCompleter
# knows something about this switch. (note that
# `prev' contains the switch). The `0' as last
# argument makes the SpecificSwitchCompleter
# returning "" if it knows nothing specific
# about this switch.
#
set values [SpecificSwitchCompleter \
$text $start $line $prev 0]
if [string length $values] {
set result $values
return 1
} else {
set val [lindex $options(value) $found]
if [string length $val] {
# return the old value only, if it's non-empty.
# Use this double list to quote option
# values which have to be quoted.
#
set result [list [list $val]]
} else {
set result ""
}
return 1
}
} else {
set result [SpecificSwitchCompleter \
$text $start $line $prev 1]
return 1
}
} else {
set result [CompleteFromList $text \
[RemoveUsedOptions $line $options(switches)]]
return 1
}
}
return 1
}
proc ObjectClassCompleter {text start end line pos resultT} {
upvar $resultT result
set cmd [Lindex $line 0]
if {"." == [string index $line 0]} {
# it's a widget. Try to get it's class name.
#
if {![catch [list set class [winfo class [Lindex $line 0]]]]} {
if {[string length [info proc ${class}Obj]]} {
set result [${class}Obj $text $start $end $line $pos]
if {[string length $result]} {
return 1
} else {
return 0
}
} else {
return 0
}
}
}
if {![catch {set type [image type $cmd]}]} {
switch -- $type {
photo {
set result [PhotoObj $text $start $end $line $pos]
return 1
}
default {
# let the fallback completers do the job.
return 0
}
}
}
return 0
}
proc CompleteFromOptionsOrSubCmds {text start end line pos} {
if [CompleteFromOptions $text $start $line from_opts] {
# always return, if CompleteFromOptions returns non-zero,
# that means (configure|cget) were present. This ensures
# that TrySubCmds will not configure something by chance.
#
return $from_opts
} else {
return [TrySubCmds $text \
[lrange [ProperList $line] 0 [expr {$pos - 1}]]]
}
return ""
}
#**
# TODO: shit. make this better!
# @param text, a std completer argument (current word).
# @param fullpart, the full text of the current position.
# @param lst, the list to complete from.
# @param pre, leading `quote'.
# @param sep, word separator.
# @param post, trailing `quote'.
# @return a formatted completer string.
#
proc CompleteListFromList {text fullpart lst pre sep post} {
if {![string length $fullpart]} {
# nothing typed so far. Insert a $pre
# and inhibit further completion.
#
return [list $pre {}]
} elseif {$post == [String index $text end]} {
# finalize, append the post and a space.
#
set diff \
[expr {[CountChar $fullpart $pre] - [CountChar $fullpart $post]}]
for {set i 0} {$i < $diff} {incr i} {
append text $post
}
append text " "
return $text
} elseif {![regexp -- ^\(.*\[${pre}${sep}\]\)\(\[^${pre}${sep}\]*\)$ \
$text all left right]} {
set left {}
set right $text
}
# TraceVar left
# TraceVar right
set exact_matches [MatchesFromList $right $lst]
# TODO this is awkward. Think of making it better!
#
if {1 == [llength $exact_matches] && -1 != [lsearch $lst $right]
} {
#set completion [CompleteFromList $right [list $sep $post] 1]
return [list ${left}${right}${sep} {}]
} else {
set completion [CompleteFromList $right $lst "" 1]
}
if {![string length [lindex $completion 0]]} {
return [concat [list $left] [lrange $completion 1 end]]
} elseif {[string length $left]} {
return [list $left]$completion
} else {
return $completion
}
return ""
}
proc FirstNonOption {line} {
set expr_pos 1
foreach word [lrange $line 1 end] {; # 0 is the command itself
if {"-" != [string index $word 0]} {
break
} else {
incr expr_pos
}
}
return $expr_pos
}
proc RemoveUsedOptions {line opts {terminate {}}} {
if {[llength $terminate]} {
if {[regexp -- $terminate $line]} {
return ""
}
}
set new ""
foreach word $opts {
if {-1 == [string first $word $line]} {
lappend new $word
}
}
# check if the last word in the line is an options
# and if this word is at the very end of the line,
# that means no space after.
# If this is so, the word is stuffed into the result,
# so that it can be completed -- probably with a space.
#
set last [Lindex $line end]
if {[string last $last $line] + [string length $last]
== [string length $line]} {
if {-1 != [lsearch $opts $last]} {
lappend new $last
}
}
return [string trim $new]
}
proc Alert {} {
::tclreadline::readline bell
}
#**
# get the longest common completion
# e.g. str == {tcl_version tclreadline_version tclreadline_library}
# --> [CompleteLongest $str] == "tcl"
#
proc CompleteLongest {str} {
set match0 [lindex $str 0]
set len0 [string length $match0]
set no_matches [llength $str]
set part ""
for {set i 0} {$i < $len0} {incr i} {
set char [string index $match0 $i]
for {set j 1} {$j < $no_matches} {incr j} {
if {$char != [string index [lindex $str $j] $i]} {
break
}
}
if {$j < $no_matches} {
break
} else {
append part $char
}
}
return $part
}
proc SplitLine {start line} {
set depth 0
for {set i $start} {$i >= 0} {incr i -1} {
set c [string index $line $i]
if {{;} == $c} {
incr i; # discard command break character
return [list [expr {$start - $i}] [String range $line $i end]]
} elseif {{]} == $c} {
incr depth
} elseif {{[} == $c} {
incr depth -1
if {$depth < 0} {
incr i; # discard command break character
return [list [expr {$start - $i}] [String range $line $i end]]
}
}
}
return ""
}
proc IsWhite {char} {
if {" " == $char || "\n" == $char || "\t" == $char} {
return 1
} else {
return 0
}
}
proc PreviousWordOfIncompletePosition {start line} {
return [lindex [ProperList [string range $line 0 $start]] end]
}
proc PreviousWord {start line} {
incr start -1
set found 0
for {set i $start} {$i > 0} {incr i -1} {
set c [string index $line $i]
if {$found && [IsWhite $c]} {
break
} elseif {!$found && ![IsWhite $c]} {
set found 1
}
}
return [string trim [string range $line $i $start]]
}
proc Quote {value left} {
set right [Right $left]
if {1 < [llength $value] && "" == $right} {
return [list \"$value\"]
} else {
return [list ${left}${value}${right}]
}
}
# the following two channel proc's make use of
# the brandnew (Sep 99) `file channels' command
# but have some fallback behaviour for older
# tcl version.
#
proc InChannelId {text {switches ""}} {
if [catch {set chs [file channels]}] {
set chs {stdin}
}
set result ""
foreach ch $chs {
if {![catch {fileevent $ch readable}]} {
lappend result $ch
}
}
return [ChannelId $text <inChannel> $result $switches]
}
proc OutChannelId {text {switches ""}} {
if [catch {set chs [file channels]}] {
set chs {stdout stderr}
}
set result ""
foreach ch $chs {
if {![catch {fileevent $ch writable}]} {
lappend result $ch
}
}
return [ChannelId $text <outChannel> $result $switches]
}
proc ChannelId {text {descript <channelId>} {chs ""} {switches ""}} {
if {"" == $chs} {
# the `file channels' command is present
# only in pretty new versions.
#
if [catch {set chs [file channels]}] {
set chs {stdin stdout stderr}
}
}
if {[llength [set channel [TryFromList $text "$chs $switches"]]]} {
return $channel
} else {
return [DisplayHints [string trim "$descript $switches"]]
}
}
proc QuoteQuotes {line} {
regsub -all -- \" $line {\"} line
regsub -all -- \{ $line {\{} line; # \}\} (keep the editor happy)
return $line
}
#**
# get the word position.
# @return the word position
# @note will returned modified values.
# @sa EventuallyEvaluateFirst
#
# % p<TAB>
# % bla put<TAB> $b
# % put<TAB> $b
# part == put
# start == 0
# end == 3
# line == "put $b"
# [PartPosition] should return 0
#
proc PartPosition {partT startT endT lineT} {
upvar $partT part $startT start $endT end $lineT line
EventuallyEvaluateFirst part start end line
return [Llength [string range $line 0 [expr {$start - 1}]]]
#
# set local_start [expr {$start - 1}]
# set local_start_chr [string index $line $local_start]
# if {"\"" == $local_start_chr || "\{" == $local_start_chr} {
# incr local_start -1
# }
#
# set pre_text [QuoteQuotes [string range $line 0 $local_start]]
# return [llength $pre_text]
#
}
proc Right {left} {
if {"\"" == $left} {
return "\""
} elseif {"\\\"" == $left} {
return "\\\""
} elseif {"\{" == $left} {
return "\}"
} elseif {"\\\{" == $left} {
return "\\\}"
}
return ""
}
proc GetQuotedPrefix {text} {
set null [string index $text 0]
if {"\"" == $null || "\{" == $null} {
return \\$null
} else {
return {}
}
}
proc CountChar {line char} {
set found 0
set pos 0
while {-1 != [set pos [string first $char $line $pos]]} {
incr pos
incr found
}
return $found
}
#**
# make a proper tcl list from an icomplete
# string, that is: remove the junk. This is
# complementary to `IncompleteListRemainder'.
# e.g.:
# for {set i 1} "
# --> for {set i 1}
#
proc ProperList {line} {
set last [expr {[string length $line] - 1}]
for {set i $last} {$i >= 0} {incr i -1} {
if {![catch {llength [string range $line 0 $i]}]} {
break
}
}
return [string range $line 0 $i]
}
#**
# return the last part of a line which
# prevents the line from being a list.
# This is complementary to `ProperList'.
#
proc IncompleteListRemainder {line} {
set last [expr {[string length $line] - 1}]
for {set i $last} {$i >= 0} {incr i -1} {
if {![catch {llength [string range $line 0 $i]}]} {
break
}
}
incr i
return [String range $line $i end]
}
#**
# save `lindex'. works also for non-complete lines
# with opening parentheses or quotes.
# usage as `lindex'.
# Eventually returns the Rest of an incomplete line,
# if the index is `end' or == [Llength $line].
#
proc Lindex {line pos} {
if {[catch [list set sub [lindex $line $pos]]]} {
if {"end" == $pos || [Llength $line] == $pos} {
return [IncompleteListRemainder $line]
}
set line [ProperList $line]
if {[catch [list set sub [lindex $line $pos]]]} { return {} }
}
return $sub
}
#**
# save `llength' (see above).
#
proc Llength {line} {
if {[catch [list set len [llength $line]]]} {
set line [ProperList $line]
if {[catch [list set len [llength $line]]]} { return {} }
}
return $len
}
#**
# save `lrange' (see above).