tcl-hacks

Check-in [79692df96b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:make all instances belong to Getline, support -options in constructor
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:79692df96b8b940ad954f6d20ae8df78e8b476ba
User & Date: aspect 2018-05-15 09:39:43
Context
2018-05-15
10:05
put some utils where they belong check-in: 0f12101f26 user: aspect tags: trunk
09:39
make all instances belong to Getline, support -options in constructor check-in: 79692df96b user: aspect tags: trunk
08:31
sketch how the interface should look check-in: 4bf331f466 user: aspect tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to getline/getline.tcl.

6
7
8
9
10
11
12

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
..
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
...
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
...
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
...
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
...
274
275
276
277
278
279
280

281
282
283
284
285
286

287
288
289
290
291
292
293
...
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
...
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
...
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
#  x history (basic)
#  x history-search
#  x basic yank
#  x C-x C-e EDITOR (get smarter)
#  x flash message
#  x tcloo'ify
#  x multi-line input (debug further)

#   - continuation prompts
#   - multi-line redraw (just a keymap / action naming thing?)
#  x fix up history
#  x objectify keymap
#  - work out lifetimes properly (long-lived Getline contains all other objs)
#  - chan independence
#  - use throw for accept .. and beep?
#  - prefix keymaps (eg: ^L=redraw-line; ^L^L=redraw-all-lines)
#  - history-incremental-search .. this is a mode!
#  - output attrs
#  - completion ... with ui!
#  ? numeric arguments
#  - cumulative yank (mode?)
#  - yank-last-arg?  Not yank ring, stuff that.
#  ? mark
................................................................................
}
proc alias {alias cmd args} {
    set ns [uplevel 1 {namespace current}]
    set cmd [uplevel 1 namespace which $cmd]
    interp alias ${ns}::$alias $cmd {*}$args
}

package require sqlite3
sqlite3 db {}

keymap::KeyMapper create keymap stdin
History create history
Input create input
Output create output stdout

proc beep {msg} {
    puts -nonewline \x07
    flash-message $msg
}

# probably belongs to output
proc flash-message {msg} {
    variable flashid
    catch {after cancel $flashid}
    output emit [tty::save]
    lassign [exec stty size] rows cols
    output emit [tty::goto 0 [expr {$cols - [string length $msg] - 2}]]
    output emit [tty::attr bold]
    output emit " $msg "
    output emit [tty::attr]
    output emit [tty::restore]
    if {[string is space $msg]} return
    regsub -all . $msg " " msg
    set flashid [after 1000 [list flash-message $msg]]
}

proc rep {c} {
    if {[string length $c] != 1} {error "Bad input: [binary encode hex $c]"}
    if {[string is print $c]} {return $c}
    return "\\x[binary encode hex $c]"
}

proc srep {s} {
................................................................................
}

## class Getline is an "engine".  Methods on it may be addressed in the keymap.
# Getline is a single-line-only getter; Getlines extends on it with line continuation capability
#
oo::class create Getline {


    variable Yank


    variable Prompt








    constructor {pr} {  ;# input output history iscomplete accept? completer


        set Yank ""
        set Prompt $pr








        input reset
        output reset $Prompt
    }



















































    method get {} {
        input get
    }

    method sigpipe {} {
        if {[input get] ne ""}  { beep "sigpipe with [string length [input get]] chars"; return }
        return -level 2 -code break
    }
    method sigint {}      { return -level 2 -code continue }
    method redraw {}      { output redraw }

    method insert {s} {
        foreach c [split $s ""] {
................................................................................
        } else {
            my forth [expr {$i - [input pos]}]
        }
    }

    method back {{n 1}} {
        if {$n == 0} return
        if {[input pos] < 1} {beep "back at BOL"; return}
        set n [expr {min($n, [input pos])}]
        if {$n == 0} return
        output back [string length [srep [input back $n]]]
    }
    method forth {{n 1}} {
        if {$n == 0} return
        if {[input rpos] < 1} {beep "forth at EOL"; return}
        set n [expr {min($n, [input rpos])}]
        if {$n == 0} return
        output forth [string length [srep [input forth $n]]]
    }

    method backspace {{n 1}} {
        if {$n == 0} return
        if {[input pos] < 1} {beep "backspace at BOL"; return}
        set n [expr {min($n, [input pos])}]
        if {$n == 0} return
        set in [input backspace $n]
        output backspace [string length [srep $in]]
        return $in
    }
    method delete {{n 1}} {
        if {$n == 0} return
        if {[input rpos] < 1} {beep "delete at EOL"; return}
        set n [expr {min($n, [input rpos])}]
        if {$n == 0} return
        set in [input delete $n]
        output delete [string length [srep $in]]
        return $in
    }

................................................................................
    method back-word {}        { my back      [word-length-before [input get] [input pos]] }
    method forth-word {}       { my forth     [word-length-after  [input get] [input pos]] }
    method kill-word-before {} { my backspace [word-length-before [input get] [input pos]] }
    method kill-word-after {}  { my delete    [word-length-after  [input get] [input pos]] }
    # softbreak tab

    method history-prev {} {
        set s [history prev [my get]]
        if {$s eq ""}   { beep "no more history!"; return }
        my replace-input $s
    }
    method history-next {} {
        set s [history next [my get]]
        if {$s eq ""}   { beep "no more history!"; return }
        my replace-input $s
    }
    method history-prev-starting {} {
        set pos [input pos]
        set s [history prev-starting [input pre] [my get]]
        if {$s eq ""}   { beep "no more matching history!"; return }
        my kill-after
        my insert [string range $s $pos end]
        my goto $pos
    }
    method history-next-starting {} {
        set pos [input pos]
        set s [history next-starting [input pre] [my get]]
        if {$s eq ""}   { beep "no more matching history!"; return }
        my kill-after
        my insert [string range $s $pos end]
        my goto $pos
    }

    method accept {} {
        set input [my get]
        if {![string is space $input]}  { history add $input }
        my end
        output emit \n
        return -code return $input  ;# FIXME: forcing [tailcall accept] is terrible
    }

    method newline {} {
        tailcall my accept
................................................................................

oo::class create Getlines {
    superclass Getline

    variable Lines
    variable Lineidx
    variable Prompts        ;# for getlines, there must be a list of prompts!


    constructor {pr} {
        set Prompts [list $pr]
        set Lines   [list ""]
        set Lineidx 0
        next $pr

    }

    method get {} {
        lset Lines $Lineidx [input get]
        join $Lines \n
    }

................................................................................
        output emit \n
        incr Lineidx
        my set-state [lindex $Lines $Lineidx]
        my redraw
    }

    method prior-line {} {
        if {$Lineidx == 0} {beep "no prev line"; return}
        my home
        output emit [tty::up 1]
        lset Lines $Lineidx [input get]
        incr Lineidx -1
        my set-state [lindex $Lines $Lineidx]
        set nrows [output wrap 0 [output len]]    ;# hmmm
        output emit [tty::up $nrows]               ;# hmmm
        my redraw
    }
    method next-line {} {
        if {$Lineidx + 1 == [llength $Lines]} {beep "no next line"; return}
        my end
        lset Lines $Lineidx [input get]
        incr Lineidx 1
        my set-state [lindex $Lines $Lineidx]
        output emit [tty::down 1]                  ;# hmmm
        my redraw
    }
................................................................................

    method back {{n 1}} {
        if {$n <= [input pos]} {
            next $n
        } elseif {$Lineidx > 0} {
            my prior-line
            my end
        } else {beep "back at beginning of input"}
    }
    method forth {{n 1}} {
        if {$n <= [input rpos]} {
            next $n
        } elseif {$Lineidx+1 < [llength $Lines]} {
            my next-line
            my home
        } else {beep "forth at end of input"}
    }

    method backspace {{n 1}} {
        if {$n <= [input pos]} {
            next $n
        } elseif {$Lineidx > 0} {
            my prior-line
            my end
            set s [my kill-next-line]
            my insert $s
            my redraw
            my redraw-following
        } else {beep "backspace at beginning of input"}
    }
    method delete {{n 1}} {
        if {$n <= [input rpos]} {
            next $n
        } elseif {$Lineidx+1 < [llength $Lines]} {
            set rest [my kill-next-line]
            my insert $rest
            my back [string length $rest]
            my redraw
            my redraw-following
        } else {beep "delete at end of input"}
    }

    method up {{n 1}} {
        if {$Lineidx > 0} {
            set pos [input pos]
            my prior-line
            my home
................................................................................

}


proc getline {{prompt "> "}} {

    # prompt history inchan outchan
    Getlines create engine $prompt
    finally engine destroy
    set cmds [info object methods engine -all]


    while 1 {
        # {TOKEN tok {c c c}} or {LITERAL "" {c c c}}
        lassign [keymap gettok] kind tok chars
        if {$kind eq "TOKEN" && $tok in $cmds} {
            engine $tok                    ;# can return -level 1
        } else {
            foreach char $chars {
                engine insert $char
            }
        }
        # if [getline display-rows] has changed, redraw-following
    }
    error "Must not get here!  [input reset]"
}

proc main {args} {
    exec stty raw -echo <@ stdin
    trace add variable args unset {apply {args {exec stty -raw echo <@ stdin}}}







>




|
|

|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>

>
>

>
>
>
>
>
>
>
|
<
>
>

|
>
>
>
>
>
>
>
>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





|







 







|






|







|








|







 







|
|



|
|




|
|






|
|







|







 







>

|
<


|
>







 







|










|







 







|







|












|










|







 







|

|
<
>
|
|
|
|
<
<
<
<
<
<
<







6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
..
45
46
47
48
49
50
51





























52
53
54
55
56
57
58
..
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
...
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
...
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
...
315
316
317
318
319
320
321
322
323
324

325
326
327
328
329
330
331
332
333
334
335
...
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
...
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
...
475
476
477
478
479
480
481
482
483
484

485
486
487
488
489







490
491
492
493
494
495
496
#  x history (basic)
#  x history-search
#  x basic yank
#  x C-x C-e EDITOR (get smarter)
#  x flash message
#  x tcloo'ify
#  x multi-line input (debug further)
#   - fix line joinage: too much redraw by far
#   - continuation prompts
#   - multi-line redraw (just a keymap / action naming thing?)
#  x fix up history
#  x objectify keymap
#  x -options to Getline, move history etc into components
#  x chan independence
#  - use throw for accept .. and beep?
#  ? prefix keymaps (eg: ^L=redraw-line; ^L^L=redraw-all-lines)
#  - history-incremental-search .. this is a mode!
#  - output attrs
#  - completion ... with ui!
#  ? numeric arguments
#  - cumulative yank (mode?)
#  - yank-last-arg?  Not yank ring, stuff that.
#  ? mark
................................................................................
}
proc alias {alias cmd args} {
    set ns [uplevel 1 {namespace current}]
    set cmd [uplevel 1 namespace which $cmd]
    interp alias ${ns}::$alias $cmd {*}$args
}






























proc rep {c} {
    if {[string length $c] != 1} {error "Bad input: [binary encode hex $c]"}
    if {[string is print $c]} {return $c}
    return "\\x[binary encode hex $c]"
}

proc srep {s} {
................................................................................
}

## class Getline is an "engine".  Methods on it may be addressed in the keymap.
# Getline is a single-line-only getter; Getlines extends on it with line continuation capability
#
oo::class create Getline {

    # state:
    variable Yank

    # options:
    variable Prompt
    variable Chan
    method Complete? {input} { info complete $input\n }
    method Completions {s}   { return "" }
    method History {args} {
        History create History
        oo::objdefine [self] forward History History
        tailcall my History {*}$args
    }


    constructor {args} {
        set Yank ""
        set Prompt "getline> "
        set Chan stdout

        my Configure {*}$args

        Input create             input
        Output create            output $Chan
        keymap::KeyMapper create keymap [expr {$Chan eq "stdout" ? "stdin" : $Chan}]

        input reset
        output reset $Prompt
    }

    method Configure {args} {
        set OptSpec {
            -prompt     { set Prompt $val }
            -chan       { set Chan $val }
            -history    { oo::objdefine [self] forward History [uplevel 1 [list namespace which $val]] }
            -iscomplete { oo::objdefine [self] forward Complete? [uplevel 1 [list namespace which $val]] }
            -completer  { oo::objdefine [self] forward Completions [uplevel 1 [list namespace which $val]] }
        }

        dict for {opt val} $args {
            set pat $opt*
            set matched 0
            dict for {key script} $OptSpec {
                if {[string match $pat* $opt]} {
                    try $script
                    set matched 1
                    break
                }
            }
            if {!$matched} {
                return -code error "Unknown option; expected one of [join [dict keys $OptSpec] ", "]."
            }
        }
    }

    method getline {} {
        set cmds [info object methods [self] -all]

        while 1 {
            # {TOKEN tok {c c c}} or {LITERAL "" {c c c}}
            lassign [keymap gettok] kind tok chars
            if {$kind eq "TOKEN"} {
                try {
                    engine $tok
                    continue
                } trap {TCL LOOKUP METHOD *} {} { }
            }
            foreach char $chars {
                engine insert $char
            }
            # if [getline display-rows] has changed, redraw-following
        }
    }

    method beep {msg} {
        output beep
        if {$msg ne ""} {tailcall output flash-message $msg}
    }

    # action methods:
    method get {} {
        input get
    }

    method sigpipe {} {
        if {[input get] ne ""}  { my beep "sigpipe with [string length [input get]] chars"; return }
        return -level 2 -code break
    }
    method sigint {}      { return -level 2 -code continue }
    method redraw {}      { output redraw }

    method insert {s} {
        foreach c [split $s ""] {
................................................................................
        } else {
            my forth [expr {$i - [input pos]}]
        }
    }

    method back {{n 1}} {
        if {$n == 0} return
        if {[input pos] < 1} {my beep "back at BOL"; return}
        set n [expr {min($n, [input pos])}]
        if {$n == 0} return
        output back [string length [srep [input back $n]]]
    }
    method forth {{n 1}} {
        if {$n == 0} return
        if {[input rpos] < 1} {my beep "forth at EOL"; return}
        set n [expr {min($n, [input rpos])}]
        if {$n == 0} return
        output forth [string length [srep [input forth $n]]]
    }

    method backspace {{n 1}} {
        if {$n == 0} return
        if {[input pos] < 1} {my beep "backspace at BOL"; return}
        set n [expr {min($n, [input pos])}]
        if {$n == 0} return
        set in [input backspace $n]
        output backspace [string length [srep $in]]
        return $in
    }
    method delete {{n 1}} {
        if {$n == 0} return
        if {[input rpos] < 1} {my beep "delete at EOL"; return}
        set n [expr {min($n, [input rpos])}]
        if {$n == 0} return
        set in [input delete $n]
        output delete [string length [srep $in]]
        return $in
    }

................................................................................
    method back-word {}        { my back      [word-length-before [input get] [input pos]] }
    method forth-word {}       { my forth     [word-length-after  [input get] [input pos]] }
    method kill-word-before {} { my backspace [word-length-before [input get] [input pos]] }
    method kill-word-after {}  { my delete    [word-length-after  [input get] [input pos]] }
    # softbreak tab

    method history-prev {} {
        set s [my History prev [my get]]
        if {$s eq ""}   { my beep "no more history!"; return }
        my replace-input $s
    }
    method history-next {} {
        set s [my History next [my get]]
        if {$s eq ""}   { my beep "no more history!"; return }
        my replace-input $s
    }
    method history-prev-starting {} {
        set pos [input pos]
        set s [my History prev-starting [input pre] [my get]]
        if {$s eq ""}   { my beep "no more matching history!"; return }
        my kill-after
        my insert [string range $s $pos end]
        my goto $pos
    }
    method history-next-starting {} {
        set pos [input pos]
        set s [my History next-starting [input pre] [my get]]
        if {$s eq ""}   { my beep "no more matching history!"; return }
        my kill-after
        my insert [string range $s $pos end]
        my goto $pos
    }

    method accept {} {
        set input [my get]
        if {![string is space $input]}  { my History add $input }
        my end
        output emit \n
        return -code return $input  ;# FIXME: forcing [tailcall accept] is terrible
    }

    method newline {} {
        tailcall my accept
................................................................................

oo::class create Getlines {
    superclass Getline

    variable Lines
    variable Lineidx
    variable Prompts        ;# for getlines, there must be a list of prompts!
    variable Prompt         ;# actually belongs to Getline

    constructor {args} {

        set Lines   [list ""]
        set Lineidx 0
        next {*}$args
        set Prompts [list $Prompt]
    }

    method get {} {
        lset Lines $Lineidx [input get]
        join $Lines \n
    }

................................................................................
        output emit \n
        incr Lineidx
        my set-state [lindex $Lines $Lineidx]
        my redraw
    }

    method prior-line {} {
        if {$Lineidx == 0} {my beep "no prev line"; return}
        my home
        output emit [tty::up 1]
        lset Lines $Lineidx [input get]
        incr Lineidx -1
        my set-state [lindex $Lines $Lineidx]
        set nrows [output wrap 0 [output len]]    ;# hmmm
        output emit [tty::up $nrows]               ;# hmmm
        my redraw
    }
    method next-line {} {
        if {$Lineidx + 1 == [llength $Lines]} {my beep "no next line"; return}
        my end
        lset Lines $Lineidx [input get]
        incr Lineidx 1
        my set-state [lindex $Lines $Lineidx]
        output emit [tty::down 1]                  ;# hmmm
        my redraw
    }
................................................................................

    method back {{n 1}} {
        if {$n <= [input pos]} {
            next $n
        } elseif {$Lineidx > 0} {
            my prior-line
            my end
        } else {my beep "back at beginning of input"}
    }
    method forth {{n 1}} {
        if {$n <= [input rpos]} {
            next $n
        } elseif {$Lineidx+1 < [llength $Lines]} {
            my next-line
            my home
        } else {my beep "forth at end of input"}
    }

    method backspace {{n 1}} {
        if {$n <= [input pos]} {
            next $n
        } elseif {$Lineidx > 0} {
            my prior-line
            my end
            set s [my kill-next-line]
            my insert $s
            my redraw
            my redraw-following
        } else {my beep "backspace at beginning of input"}
    }
    method delete {{n 1}} {
        if {$n <= [input rpos]} {
            next $n
        } elseif {$Lineidx+1 < [llength $Lines]} {
            set rest [my kill-next-line]
            my insert $rest
            my back [string length $rest]
            my redraw
            my redraw-following
        } else {my beep "delete at end of input"}
    }

    method up {{n 1}} {
        if {$Lineidx > 0} {
            set pos [input pos]
            my prior-line
            my home
................................................................................

}


proc getline {{prompt "> "}} {

    # prompt history inchan outchan
    Getlines create engine -prompt \[[info patchlevel]\]%\ 
    finally engine destroy
    try {

        return [engine getline]
    } on break {} {
        return -code break
    } on continue {} {
        return -code continue







    }
    error "Must not get here!  [input reset]"
}

proc main {args} {
    exec stty raw -echo <@ stdin
    trace add variable args unset {apply {args {exec stty -raw echo <@ stdin}}}

Changes to getline/history.tcl.

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        set hit [lsearch -inline -sorted -bisect -integer $hits [expr {$Index-1}]]
        if {$hit eq ""} {return}
        set Index $hit
        lindex $Items $Index
    }
    method next-matching {glob {curr ""}} {
        if {$curr ne ""} {lset Items $Index $curr}
        set hit [lsearch -start $index -glob $glob $items]
        if {$hit == -1} {return}
        set Index $hit
        lindex $Items $Index
    }
}

oo::class create History {







|







42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        set hit [lsearch -inline -sorted -bisect -integer $hits [expr {$Index-1}]]
        if {$hit eq ""} {return}
        set Index $hit
        lindex $Items $Index
    }
    method next-matching {glob {curr ""}} {
        if {$curr ne ""} {lset Items $Index $curr}
        set hit [lsearch -start $Index -glob $glob $Items]
        if {$hit == -1} {return}
        set Index $hit
        lindex $Items $Index
    }
}

oo::class create History {

Changes to getline/output.tcl.

123
124
125
126
127
128
129
130


















            my emit [tty::delete $n]
        }
    }
    method backspace {{n 1}} {
        my back $n
        my delete $n
    }
}

























|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
            my emit [tty::delete $n]
        }
    }
    method backspace {{n 1}} {
        my back $n
        my delete $n
    }

    method beep {} {
        my emit \x07
    }
    method flash-message {msg} {
        variable flashid
        catch {after cancel $flashid}
        my emit [tty::save]
        lassign [exec stty size] rows cols
        my emit [tty::goto 0 [expr {$cols - [string length $msg] - 2}]]
        my emit [tty::attr bold]
        my emit " $msg "
        my emit [tty::attr]
        my emit [tty::restore]
        if {[string is space $msg]} return
        regsub -all . $msg " " msg
        set flashid [after 1000 [list [self] flash-message $msg]]
    }
}