/
.tclshrc
546 lines (486 loc) · 13.8 KB
/
.tclshrc
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
global tcl_interactive
if {[info exists tcl_interactive] && $tcl_interactive > 0} {
# ripped from http://wiki.tcl.tk/16139
foreach {var val} {
PROMPT {tclsh$::tcl_patchLevel> }
HISTORY ""
HISTORY_BUFFER 500
COMPLETION_MATCH ""
} {
if {![info exists env($var)]} {
set env($var) $val
}
}
foreach {var val} {
CMDLINE ""
CMDLINE_CURSOR 0
CMDLINE_LINES 0
HISTORY_LEVEL -1
} {
set env($var) $val
}
unset var val
set forever 0
# Resource & history files:
set HISTFILE $env(HOME)/.tclsh_history
proc ESC {} {
return "\033"
}
proc readbuf {txt} {
upvar 1 $txt STRING
set ret [string index $STRING 0]
set STRING [string range $STRING 1 end]
return $ret
}
proc goto {row {col 1}} {
switch -- $row {
"home" {set row 1}
}
print "[ESC]\[${row};${col}H" nowait
}
proc gotocol {col} {
print "\r" nowait
if {$col > 0} {
print "[ESC]\[${col}C" nowait
}
}
proc clear {} {
print "[ESC]\[2J" nowait
goto home
}
proc clearline {} {
print "[ESC]\[2K\r" nowait
}
proc getColumns {} {
set cols 0
if {![catch {exec stty -a} err]} {
regexp {rows \d+; columns (\d+)} $err -> cols
}
return $cols
}
proc prompt {{txt ""}} {
global env
set prompt [subst $env(PROMPT)]
set txt "$prompt$txt"
foreach {end mid} $env(CMDLINE_LINES) break
# Calculate how many extra lines we need to display.
# Also calculate cursor position:
set n -1
set totalLen 0
set cursorLen [expr {$env(CMDLINE_CURSOR)+[string length $prompt]}]
set row 0
set col 0
# Render output line-by-line to $out then copy back to $txt:
set found 0
set out [list]
foreach line [split $txt "\n"] {
set len [expr {[string length $line]+1}]
incr totalLen $len
if {$found == 0 && $totalLen >= $cursorLen} {
set cursorLen [expr {$cursorLen - ($totalLen - $len)}]
set col [expr {$cursorLen % $env(COLUMNS)}]
set row [expr {$n + ($cursorLen / $env(COLUMNS)) + 1}]
if {$cursorLen >= $len} {
set col 0
incr row
}
set found 1
}
incr n [expr {int(ceil(double($len)/$env(COLUMNS)))}]
while {$len > 0} {
lappend out [string range $line 0 [expr {$env(COLUMNS)-1}]]
set line [string range $line $env(COLUMNS) end]
set len [expr {$len-$env(COLUMNS)}]
}
}
set txt [join $out "\n"]
set row [expr {$n-$row}]
# Reserve spaces for display:
if {$end} {
if {$mid} {
print "[ESC]\[${mid}B" nowait
}
for {set x 0} {$x < $end} {incr x} {
clearline
print "[ESC]\[1A" nowait
}
}
clearline
set env(CMDLINE_LINES) $n
# Output line(s):
print "\r$txt"
if {$row} {
print "[ESC]\[${row}A" nowait
}
gotocol $col
lappend env(CMDLINE_LINES) $row
}
proc print {txt {wait wait}} {
# Sends output to stdout chunks at a time.
# This is to prevent the terminal from
# hanging if we output too much:
while {[string length $txt]} {
puts -nonewline [string range $txt 0 2047]
set txt [string range $txt 2048 end]
if {$wait == "wait"} {
after 1
}
}
}
################################
# Key bindings
################################
proc handleEscapes {} {
global env
upvar 1 keybuffer keybuffer
set seq ""
set found 0
while {[set ch [readbuf keybuffer]] != ""} {
append seq $ch
switch -exact -- $seq {
"\[A" { ;# Cursor Up (cuu1,up)
handleHistory 1
set found 1; break
}
"\[B" { ;# Cursor Down
handleHistory -1
set found 1; break
}
"\[C" { ;# Cursor Right (cuf1,nd)
if {$env(CMDLINE_CURSOR) < [string length $env(CMDLINE)]} {
incr env(CMDLINE_CURSOR)
}
set found 1; break
}
"\[D" { ;# Cursor Left
if {$env(CMDLINE_CURSOR) > 0} {
incr env(CMDLINE_CURSOR) -1
}
set found 1; break
}
"\[H" -
"\[7~" -
"\[1~" { ;# home
set env(CMDLINE_CURSOR) 0
set found 1; break
}
"\[3~" { ;# delete
if {$env(CMDLINE_CURSOR) < [string length $env(CMDLINE)]} {
set env(CMDLINE) [string replace $env(CMDLINE) \
$env(CMDLINE_CURSOR) $env(CMDLINE_CURSOR)]
}
set found 1; break
}
"\[F" -
"\[K" -
"\[8~" -
"\[4~" { ;# end
set env(CMDLINE_CURSOR) [string length $env(CMDLINE)]
set found 1; break
}
"\[5~" { ;# Page Up }
"\[6~" { ;# Page Down }
}
}
return $found
}
proc handleControls {} {
global env
upvar 1 char char
upvar 1 keybuffer keybuffer
# Control chars start at a == \u0001 and count up.
switch -exact -- $char {
\u0004 { ;# ^d
if { $env(CMDLINE_CURSOR) <= 0 } {
print "exit\n" nowait
doExit
}
}
\u0003 { ;# ^c
print "^C\n" nowait
doExit
}
\u0008 -
\u007f { ;# ^h && backspace ?
if {$env(CMDLINE_CURSOR) > 0} {
incr env(CMDLINE_CURSOR) -1
set env(CMDLINE) [string replace $env(CMDLINE) \
$env(CMDLINE_CURSOR) $env(CMDLINE_CURSOR)]
}
}
\u001b { ;# ESC - handle escape sequences
handleEscapes
}
}
# Rate limiter:
set keybuffer ""
}
proc shortMatch {maybe} {
# Find the shortest matching substring:
set maybe [lsort $maybe]
set shortest [lindex $maybe 0]
foreach x $maybe {
while {![string match $shortest* $x]} {
set shortest [string range $shortest 0 end-1]
}
}
return $shortest
}
proc handleCompletion {} {
global env
set vars ""
set cmds ""
# First find out what kind of word we need to complete:
set wordstart [string last " " $env(CMDLINE) \
[expr {$env(CMDLINE_CURSOR)-1}]]
incr wordstart
set wordend [string first " " $env(CMDLINE) $wordstart]
if {$wordend == -1} {
set wordend end
} else {
incr wordend -1
}
set word [string range $env(CMDLINE) $wordstart $wordend]
if {[string trim $word] == ""} return
set firstchar [string index $word 0]
# Check if word is a variable:
if {$firstchar == "\$"} {
set word [string range $word 1 end]
incr wordstart
# Check if it is an array key:
set x [string first "(" $word]
if {$x != -1} {
set v [string range $word 0 [expr {$x-1}]]
incr x
set word [string range $word $x end]
incr wordstart $x
if {[uplevel #0 "array exists $v"]} {
set vars [uplevel #0 "array names $v $word*"]
}
} else {
foreach x [uplevel #0 {info vars}] {
if {[string match $word* $x]} {
lappend vars $x
}
}
}
} else {
if {$firstchar == "\[" || $wordstart == 0} {
if {$firstchar == "\["} {
set word [string range $word 1 end]
incr wordstart
}
# Check commands:
foreach x [info commands] {
if {[string match $word* $x]} {
lappend cmds $x
}
}
} else {
# Check commands anyway:
foreach x [info commands] {
if {[string match $word* $x]} {
lappend cmds $x
}
}
}
if {$wordstart != 0} {
# Check variables anyway:
set x [string first "(" $word]
if {$x != -1} {
set v [string range $word 0 [expr {$x-1}]]
incr x
set word [string range $word $x end]
incr wordstart $x
if {[uplevel #0 "array exists $v"]} {
set vars [uplevel #0 "array names $v $word*"]
}
} else {
foreach x [uplevel #0 {info vars}] {
if {[string match $word* $x]} {
lappend vars $x
}
}
}
}
}
set maybe [concat $vars $cmds]
set shortest [shortMatch $maybe]
if {"$word" == "$shortest"} {
if {[llength $maybe] > 1 && $env(COMPLETION_MATCH) != $maybe} {
set env(COMPLETION_MATCH) $maybe
clearline
set temp ""
foreach {match format} {
vars "35"
cmds "1;32"
} {
if {[llength $match]} {
append temp "[ESC]\[${format}m"
foreach x [set $match] {
append temp "[file tail $x] "
}
append temp "[ESC]\[0m"
}
}
print "\n$temp\n"
}
}
}
proc handleHistory {x} {
global env
set hlen [llength $env(HISTORY)]
incr env(HISTORY_LEVEL) $x
if {$env(HISTORY_LEVEL) > -1} {
set env(CMDLINE) [lindex $env(HISTORY) end-$env(HISTORY_LEVEL)]
set env(CMDLINE_CURSOR) [string length $env(CMDLINE)]
}
if {$env(HISTORY_LEVEL) <= -1} {
set env(HISTORY_LEVEL) -1
set env(CMDLINE) ""
set env(CMDLINE_CURSOR) 0
} elseif {$env(HISTORY_LEVEL) > $hlen} {
set env(HISTORY_LEVEL) $hlen
}
}
################################
# History handling functions
################################
proc getHistory {} {
global env
return $env(HISTORY)
}
proc setHistory {hlist} {
global env
set env(HISTORY) $hlist
}
proc appendHistory {cmdline} {
global env
set old [lsearch -exact $env(HISTORY) $cmdline]
if {$old != -1} {
set env(HISTORY) [lreplace $env(HISTORY) $old $old]
}
lappend env(HISTORY) $cmdline
set env(HISTORY) \
[lrange $env(HISTORY) end-$env(HISTORY_BUFFER) end]
}
################################
# main()
################################
proc rawInput {} {
fconfigure stdin -buffering none -blocking 0
fconfigure stdout -buffering none -translation crlf
exec stty raw -echo
}
proc lineInput {} {
fconfigure stdin -buffering line -blocking 1
fconfigure stdout -buffering line
exec stty -raw echo
}
proc doExit {{code 0}} {
global env HISTFILE
# Reset terminal:
# print "[ESC]c[ESC]\[2J" nowait
lineInput
set hlist [getHistory]
if {[llength $hlist] > 0} {
set f [open $HISTFILE w]
foreach x $hlist {
# Escape newlines:
puts $f [string map {
\n "\\n"
"\\" "\\b"
} $x]
}
close $f
}
___exit $code
}
# Load history if available:
if {[llength $env(HISTORY)] == 0} {
if {[file exists $HISTFILE]} {
set f [open $HISTFILE r]
set hlist [list]
foreach x [split [read $f] "\n"] {
if {$x != ""} {
# Undo newline escapes:
lappend hlist [string map {
"\\n" \n
"\\\\" "\\"
"\\b" "\\"
} $x]
}
}
setHistory $hlist
unset hlist
close $f
}
}
rawInput
rename exit ___exit
proc exit args doExit
proc tclline {} {
global env
set char ""
set keybuffer [read stdin]
set env(COLUMNS) [getColumns]
while {$keybuffer != ""} {
if {[eof stdin]} return
set char [readbuf keybuffer]
if {$char == ""} {
# Sleep for a bit to reduce CPU time:
after 40
continue
}
if {[string is print $char]} {
set x $env(CMDLINE_CURSOR)
if {$x < 1 && [string trim $char] == ""} continue
set trailing [string range $env(CMDLINE) $x end]
set env(CMDLINE) [string replace $env(CMDLINE) $x end]
append env(CMDLINE) $char
append env(CMDLINE) $trailing
incr env(CMDLINE_CURSOR)
} elseif {$char == "\t"} {
handleCompletion
} elseif {$char == "\n" || $char == "\r"} {
if {[info complete $env(CMDLINE)] &&
[string index $env(CMDLINE) end] != "\\"} {
lineInput
print "\n" nowait
uplevel #0 {
global env
# Run the command:
if { [catch $env(CMDLINE) res] } {
print "[ESC]\[1;31m\[[ESC]\[0;31mError[ESC]\[1;31m\][ESC]\[0m "
}
if {$res != ""} {
print "$res\n"
}
# Append HISTORY:
set env(HISTORY_LEVEL) -1
appendHistory $env(CMDLINE)
set env(CMDLINE) ""
set env(CMDLINE_CURSOR) 0
set env(CMDLINE_LINES) {0 0}
}
rawInput
} else {
set x $env(CMDLINE_CURSOR)
if {$x < 1 && [string trim $char] == ""} continue
set trailing [string range $env(CMDLINE) $x end]
set env(CMDLINE) [string replace $env(CMDLINE) $x end]
append env(CMDLINE) $char
append env(CMDLINE) $trailing
incr env(CMDLINE_CURSOR)
}
} else {
handleControls
}
}
prompt $env(CMDLINE)
}
tclline
fileevent stdin readable tclline
vwait forever
doExit
}