tcl-hacks

Check-in [71103c9573]
Login

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

Overview
Comment:more tidy, note some introduced bugs found interactively
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:71103c9573341a3147ebe0a03c7258554eb8e7a0
User & Date: aspect 2018-05-15 10:52:30
Context
2018-05-15
11:31
split out the classes. Now use main.tcl check-in: fafcde2df6 user: aspect tags: trunk
10:52
more tidy, note some introduced bugs found interactively check-in: 71103c9573 user: aspect tags: trunk
10:20
a bit of tidy check-in: d67dd249f2 user: aspect tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to getline/getline.tcl.

1

2
3
4
5
6
7
8
..
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
...
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
...
309
310
311
312
313
314
315
316






317

318
319
320
321
322
323
324
...
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
...
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
...
491
492
493
494
495
496
497


498
499
500
501
502
503
504
505
506
# TODO:

#  x char-wise nav
#  x line-wise nav
#  x wrap handling
#  x word-wise nav
#  x history (basic)
#  x history-search
#  x basic yank
................................................................................
        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]] }
................................................................................
            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
            }
        }
    }

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






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

    method sigpipe {} {
................................................................................

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







    method getline {} {

        try {
            next
        } on break {} {
            return -code break
        } on continue {} {
            return -code continue
        }
................................................................................
        }
    }

    method insert-newline {} {
        set rest [my kill-after]
        set Lines [linsert $Lines $Lineidx+1 $rest]
        set rows [output wrap [output pos] [output rpos]]
        output emit [tty::down $rows]          ;# hmmm
        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 kill-next-line {} {
        set r [lindex $Lines $Lineidx+1]
        set Lines [lreplace $Lines $Lineidx+1 $Lineidx+1]
        return $r
................................................................................
            my forth $pos
        }
    }

}


proc getline {{prompt "> "}} {

    # prompt history inchan outchan
    Getlines create engine -prompt \[[info patchlevel]\]%\ 
    finally engine destroy
    try {
        return [engine getline]
    } on break {} {
................................................................................
proc main {args} {
    exec stty raw -echo <@ stdin
    trace add variable args unset {apply {args {exec stty -raw echo <@ stdin}}}
    chan configure stdin -blocking 0
    chan configure stdout -buffering none
    chan event stdin readable [info coroutine]
    set prompt "\[[info patch]\]% "


    while 1 {
        set input [getline]             ;# can return -code break/continue
        puts " -> {[srep $input]}"
    }
}

if 0 {
    proc complete? {s} {info complete $s\n}
    proc complete-tcl-command {s} {

>







 







<
<
<







 







|
>






|




|








>
>
>
>
>







 








>
>
>
>
>
>

>







 







|













|
|








|
>







 







|







 







>
>

|







1
2
3
4
5
6
7
8
9
..
90
91
92
93
94
95
96



97
98
99
100
101
102
103
...
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
...
313
314
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
...
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
...
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# TODO:
#  _ eliminate the bugs I've added with multiline + history + state
#  x char-wise nav
#  x line-wise nav
#  x wrap handling
#  x word-wise nav
#  x history (basic)
#  x history-search
#  x basic yank
................................................................................
        set Chan stdout

        my Configure {*}$args

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



    }

    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]] }
................................................................................
            if {!$matched} {
                return -code error "Unknown option; expected one of [join [dict keys $OptSpec] ", "]."
            }
        }
    }

    method getline {} {

        my reset

        while 1 {
            # {TOKEN tok {c c c}} or {LITERAL "" {c c c}}
            lassign [keymap gettok] kind tok chars
            if {$kind eq "TOKEN"} {
                try {
                    my $tok
                    continue
                } trap {TCL LOOKUP METHOD *} {} { }
            }
            foreach char $chars {
                my insert $char
            }
        }
    }

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

    method reset {} {
        input reset
        output reset $Prompt
    }

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

    method sigpipe {} {
................................................................................

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

    method reset {} {
        set Lines [list ""]
        set Lineidx 0
        next
    }

    method getline {} {
        my reset
        try {
            next
        } on break {} {
            return -code break
        } on continue {} {
            return -code continue
        }
................................................................................
        }
    }

    method insert-newline {} {
        set rest [my kill-after]
        set Lines [linsert $Lines $Lineidx+1 $rest]
        set rows [output wrap [output pos] [output rpos]]
        output emit [tty::down $rows]           ;# hmmm
        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 flash-message [my get]
        my redraw
    }

    method kill-next-line {} {
        set r [lindex $Lines $Lineidx+1]
        set Lines [lreplace $Lines $Lineidx+1 $Lineidx+1]
        return $r
................................................................................
            my forth $pos
        }
    }

}


proc _getline {{prompt "> "}} {

    # prompt history inchan outchan
    Getlines create engine -prompt \[[info patchlevel]\]%\ 
    finally engine destroy
    try {
        return [engine getline]
    } on break {} {
................................................................................
proc main {args} {
    exec stty raw -echo <@ stdin
    trace add variable args unset {apply {args {exec stty -raw echo <@ stdin}}}
    chan configure stdin -blocking 0
    chan configure stdout -buffering none
    chan event stdin readable [info coroutine]
    set prompt "\[[info patch]\]% "
    Getlines create getline -prompt $prompt
    finally getline destroy
    while 1 {
        set input [getline getline]             ;# can return -code break/continue
        puts " -> {[srep $input]}"
    }
}

if 0 {
    proc complete? {s} {info complete $s\n}
    proc complete-tcl-command {s} {