-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtkcon.tcl
More file actions
6895 lines (6390 loc) · 207 KB
/
tkcon.tcl
File metadata and controls
6895 lines (6390 loc) · 207 KB
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
#!/usr/bin/env wish
# @@ Meta Begin
# Application tkcon 3.0
# Meta platform tcl
# Meta summary Enhanced Tk Console
# Meta description Enhanced Tk Console
# Meta description Originally based off Brent Welch's
# Meta description Tcl Shell Widget
# Meta category Shell
# Meta subject console tk remote
# Meta require {Tk 8} {Tcl 8} {http 2}
# Meta recommend ctext base64 Trf ActiveTcl
# Meta author Jeff Hobbs
# Meta license Tcl (+ bourbonware clause).
# @@ Meta End
#
## tkcon.tcl
## Enhanced Tk Console, part of the VerTcl system
##
## Originally based off Brent Welch's Tcl Shell Widget
## (from "Practical Programming in Tcl and Tk")
##
## Thanks to the following (among many) for early bug reports & code ideas:
## Steven Wahl, Jan Nijtmans, Mark Crimmins, Wart
##
## Copyright (c) 1995-2016 Jeffrey Hobbs.
## Initiated: Thu Aug 17 15:36:47 PDT 1995
##
## source standard_disclaimer.tcl
## source bourbon_ware.tcl
##
## Modifications for version 3.0 release Copyright (c) 2025 Bandoti Ltd.
## Major enhancements include dark-mode detection & colors (based upon
## Adobe Spectrum color palette—see https://github.com/tclmonster/spectrum-tk);
## cffi functionality for setting window color on Win32; tabbed consoles; and
## new object-oriented widget types.
package require Tk 8.6-
# We need to load some package to get what's available, and we
# choose ctext because we'll use it if it's available in the editor
catch {package require ctext}
foreach pkg [info loaded {}] {
set file [lindex $pkg 0]
set name [lindex $pkg 1]
if {![catch {set version [package require $name]}]} {
if {[package ifneeded $name $version] eq ""} {
package ifneeded $name $version [list load $file $name]
}
}
}
# Unset temporary global vars
catch {unset pkg file name version}
# Initialize the ::tkcon namespace
#
namespace eval ::tkcon {
# when modifying this line, make sure that the auto-upgrade check
# for version still works.
variable VERSION "3.0"
# The OPT variable is an array containing most of the optional
# info to configure. COLOR has the color data.
variable OPT
variable COLOR
# PRIV is used for internal data that only tkcon should fiddle with.
variable PRIV
set PRIV(WWW) [info exists embed_args]
set PRIV(AQUA) [expr {[tk windowingsystem] eq "aqua"}]
set PRIV(WIN32) [expr {[tk windowingsystem] eq "win32"}]
set PRIV(CTRL) [expr {$PRIV(AQUA) ? "Command-" : "Control-"}]
set PRIV(ACC) [expr {$PRIV(AQUA) ? "Command-" : "Ctrl+"}]
set PRIV(MOD) [expr {$PRIV(AQUA) ? "Shift-" : "Shift+"}]
set PRIV(console) ""
set PRIV(nexttabbutton) 0
variable EXPECT 0
}
oo::class create ::tkcon::Widget {
variable Path
constructor {path} {
rename ::$path [self namespace]::$path
interp alias {} ::$path {} [self]
bind $path <Destroy> [list ::apply {{obj} {
if {[info object isa object $obj]} {
$obj destroy
}
}} [self]]
set Path $path
}
destructor {
interp alias {} ::$Path {}
if {[winfo exists $Path]} {
destroy $Path
}
}
method unknown {subcmd args} {
[self namespace]::$Path $subcmd {*}$args
}
}
oo::class create ::tkcon::Dialog {
superclass ::tkcon::Widget
constructor {path {title ""} {relative_to ""}} {
if {[winfo exists $path]} {
destroy $path
}
toplevel $path
wm withdraw $path
catch {wm attributes $path -type dialog}
wm resizable $path 0 0
set focus [expr {[focus] ne "" ? [focus] : "."}]
set relative_to [expr {$relative_to eq "" ? $focus : $relative_to}]
set x [expr {[winfo rootx $relative_to] + [winfo width $relative_to]/4}]
set y [expr {[winfo rooty $relative_to] + [winfo height $relative_to]/4}]
wm geometry $path [format %+d%+d $x $y]
wm title $path $title
wm deiconify $path
raise $path
next $path
}
}
oo::class create ::tkcon::TabButton {
superclass ::tkcon::Widget
variable Console
variable Container
variable Content
variable CloseButton
method container {} { return $Container }
method content {} { return $Content }
method closebutton {} { return $CloseButton }
method console {} { return $Console }
method selected {} {
namespace upvar ::tkcon PRIV PRIV
expr {$Console eq $PRIV(console)}
}
constructor {con} {
namespace upvar ::tkcon PRIV PRIV
set Console $con
set Container "$PRIV(tabframe).cb[winfo name $con]"
set Content "$Container.selectBtn"
set CloseButton "$Container.closeBtn"
set tabname "Console [incr PRIV(nexttabname)]"
frame $Container
radiobutton $Content -borderwidth 0 -indicatoron 0 \
-variable ::tkcon::PRIV(curtab) -value $con \
-text $tabname -command [list ::tkcon::GotoTab $con]
label $CloseButton -text "\u00D7"
# Force the close button to a square
set current_width [winfo reqwidth $CloseButton]
set current_height [winfo reqheight $CloseButton]
set target_size [expr {max($current_width, $current_height)}]
set ipad_x [expr {($target_size - $current_width) / 2}]
set ipad_y [expr {($target_size - $current_height) / 2}]
grid $CloseButton -row 0 -column 0 -ipadx $ipad_x -ipady $ipad_y -sticky nsew
grid $Content -row 0 -column 1 -sticky nsew
grid columnconfigure $Container 1 -weight 1
grid rowconfigure $Container 0 -weight 1
bind $CloseButton <ButtonRelease-1> [list [self] onReleaseCloseButton]
bind $CloseButton <Enter> +[list [self] onEnterCloseButton]
bind $CloseButton <Leave> +[list [self] onLeaveCloseButton]
bind $Container <Enter> +[list [self] onEnterContainer]
bind $Container <Leave> +[list [self] onLeaveContainer]
next $Container
}
method refreshColors {} {
namespace upvar ::tkcon COLOR C
set sel [my selected]
$Container configure \
-background [expr {$sel ? $C(tab-selected-bg) : $C(tab-bg)}]
$Content configure \
-background [expr {$sel ? $C(tab-selected-bg) : $C(tab-bg)}] \
-foreground [expr {$sel ? $C(tab-selected-fg) : $C(tab-fg)}] \
-activebackground [expr {$sel ? $C(tab-selected-bg) : $C(tab-bg)}] \
-activeforeground [expr {$sel ? $C(tab-selected-fg) : $C(tab-fg)}] \
-selectcolor [expr {$sel ? $C(tab-selected-bg) : $C(tab-bg)}]
$CloseButton configure \
-background [expr {$sel ? $C(tab-selected-bg) : $C(tab-bg)}] \
-foreground [expr {$sel ? $C(tab-selected-fg) : $C(tab-fg)}]
}
method onLeaveContainer {} {
namespace upvar ::tkcon COLOR C
set sel [my selected]
set bg [expr {$sel ? $C(tab-selected-bg) : $C(tab-bg)}]
set fg [expr {$sel ? $C(tab-selected-fg) : $C(tab-fg)}]
$Content configure \
-background $bg -foreground $fg \
-activebackground $bg -activeforeground $fg \
-selectcolor $bg
$CloseButton configure -background $bg -foreground $fg
}
method onReleaseCloseButton {} {
if {[winfo containing {*}[winfo pointerxy .]] eq $CloseButton} {
::tkcon::DeleteTab $Console
}
}
method onEnterCloseButton {} {
namespace upvar ::tkcon COLOR C
set sel [my selected]
event generate $Container <Enter>
$CloseButton configure \
-background [expr {$sel ? $C(tab-hover-bg) : $C(tab-bg)}] \
-foreground [expr {$sel ? $C(tab-hover-fg) : $C(tab-fg)}]
}
method onLeaveCloseButton {} {
$CloseButton configure -background [$Content cget -background] \
-foreground [$Content cget -foreground]
}
method onEnterContainer {} {
namespace upvar ::tkcon COLOR C
set sel [my selected]
set bg [expr {$sel ? $C(tab-selected-bg) : $C(tab-hover-bg)}]
set fg [expr {$sel ? $C(tab-selected-fg) : $C(tab-hover-fg)}]
$Content configure \
-background $bg -foreground $fg \
-activebackground $bg -activeforeground $fg \
-selectcolor $bg
$CloseButton configure -background $bg -foreground $fg
}
}
proc ::tkcon::tabbutton {con} {
set obj [TabButton new $con]
return [$obj container]
}
proc ::tkcon::TabButtonFromConsole {console} {
set container ""
foreach instance [info class instances ::tkcon::TabButton] {
if {[$instance console] eq $console} {
set container [$instance container]
break
}
}
return $container
}
proc ::tkcon::RefreshAllTabButtons {} {
foreach instance [info class instances ::tkcon::TabButton] {
$instance refreshColors
}
}
proc ::tkcon::CalcRowsFromCols {cols} {
# Ensure the console defaults to an aspect ratio harmonious with
# screen proportions.
set char_width [font measure tkcon-fixed "0"]
set char_height [font metrics tkcon-fixed -linespace]
set char_aspect [expr {double($char_height) / $char_width}]
set screen_width [winfo screenwidth .]
set screen_height [winfo screenheight .]
set screen_aspect [expr {double($screen_height) / $screen_width}]
set rows [expr {int($cols * $screen_aspect / $char_aspect)}]
return $rows
}
## ::tkcon::InitFonts - determine best available fixed & sans-serif fonts used by TkCon.
# Creates fonts tkcon-fixed, tkcon-fixed-bold, tkcon-fixed-large, tkcon-fixed-small,
# tkcon-fixed-extra-small, tkcon-sans-serif, tkcon-sans-serif-bold, tkcon-sans-serif-large,
# tkcon-sans-serif-small, tkcon-sans-serif-extra-small.
##
proc ::tkcon::InitFonts {} {
variable OPT
variable PRIV
set font_size_default [expr {-int([tk scaling] * 12)}] ;# Note: 12 px (not point)
if {![info exists OPT(font)]} {
set fixed_family [::apply {{} {
set families [switch -- [tk windowingsystem] {
win32 {expr {{"Cascadia Code" "Consolas" "Lucida Console" "Courier New"}}}
aqua {expr {{"SF Mono" "Menlo" "Monaco"}}}
default {expr {{"Noto Sans Mono" "DejaVu Sans Mono" "Liberation Mono" "Ubuntu Mono"}}}
}]
foreach fam $families {
if {$fam in [font families]} {
return $fam
}
}
return "Courier"
}}]
font create tkcon-fixed -family $fixed_family -size $font_size_default
set OPT(font) tkcon-fixed
} else {
font create tkcon-fixed -family [font configure $OPT(font) -family] \
-size [font configure $OPT(font) -size]
}
font create tkcon-fixed-bold -family [font configure tkcon-fixed -family] \
-size [font configure tkcon-fixed -size] \
-weight bold
font create tkcon-fixed-extra-small -family [font configure tkcon-fixed -family] \
-size [expr {int(0.6875 * [font configure tkcon-fixed -size])}]
font create tkcon-fixed-small -family [font configure tkcon-fixed -family] \
-size [expr {int(0.875 * [font configure tkcon-fixed -size])}]
font create tkcon-fixed-large -family [font configure tkcon-fixed -family] \
-size [expr {int(1.125 * [font configure tkcon-fixed -size])}]
set PRIV(fontsize) [expr {abs([font configure tkcon-fixed -size])}]
if {![info exists OPT(font-sans-serif)]} {
set sans_serif_family [::apply {{} {
set families [switch -- [tk windowingsystem] {
win32 {expr {{"Segoe UI" "Tahoma" "MS Sans Serif" "Arial"}}}
aqua {expr {{"SF Pro Text" "Lucida Grande" "Geneva"}}}
default {expr {{"Noto Sans" "DejaVu Sans" "Liberation Sans" "Ubuntu"}}}
}]
foreach fam $families {
if {$fam in [font families]} {
return $fam
}
}
return "Helvetica"
}}]
font create tkcon-sans-serif -family $sans_serif_family -size $font_size_default
set OPT(font-sans-serif) tkcon-sans-serif
} else {
font create tkcon-sans-serif -family [font configure $OPT(font-sans-serif) -family] \
-size [font configure $OPT(font-sans-serif) -size]
}
font create tkcon-sans-serif-bold -family [font configure tkcon-sans-serif -family] \
-size [font configure tkcon-sans-serif -size] \
-weight bold
font create tkcon-sans-serif-extra-small -family [font configure tkcon-sans-serif -family] \
-size [expr {int(0.6875 * [font configure tkcon-sans-serif -size])}]
font create tkcon-sans-serif-small -family [font configure tkcon-sans-serif -family] \
-size [expr {int(0.875 * [font configure tkcon-sans-serif -size])}]
font create tkcon-sans-serif-large -family [font configure tkcon-sans-serif -family] \
-size [expr {int(1.125 * [font configure tkcon-sans-serif -size])}]
}
## ::tkcon::DarkModeSetting - detects dark mode
# Outputs: true if dark mode is enabled, otherwise false
##
proc ::tkcon::DarkModeSetting {} {
variable PRIV
set darkmode 0
catch {
if {$PRIV(WIN32)} {
package require registry
set keypath {HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize}
set darkmode [expr {[registry get $keypath AppsUseLightTheme] == 0}]
} elseif {$PRIV(AQUA)} {
set istyle [exec defaults read -g AppleInterfaceStyle]
set darkmode [expr {$istyle eq "Dark"}]
} else {
set colorscheme_query {qdbus org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop
org.freedesktop.portal.Settings.Read "org.freedesktop.appearance" "color-scheme"
}
set darkmode [expr {1 == [exec {*}$colorscheme_query]}]
}
}
return $darkmode
}
proc ::tkcon::HexToBGR {color} {
if {[scan $color "#%2x%2x%2x" r g b] != 3} {
return -code error "Invalid hex color format: $color"
}
return [expr {($b << 16) | ($g << 8) | $r}]
}
proc ::tkcon::SetWindowColor {window color} {
variable PRIV
if {!$PRIV(WIN32) || [catch {package require cffi}]} {
return
}
cffi::alias load win32
cffi::Wrapper create dwmapi [file join $::env(windir) system32 dwmapi.dll]
cffi::Wrapper create user32 [file join $::env(windir) system32 user32.dll]
cffi::alias define HRESULT {long nonnegative winerror}
dwmapi stdcall DwmSetWindowAttribute HRESULT {
hwnd pointer.HWND dwAttribute DWORD pvAttribute pointer cbAttribute DWORD
}
user32 stdcall GetParent pointer.HWND {
hwnd pointer.HWND
}
proc ::tkcon::SetWindowColor {window color} {
set DWMWA_CAPTION_COLOR 35
set hwndptr [cffi::pointer make [winfo id $window] HWND]
cffi::pointer safe $hwndptr
set parentptr [GetParent $hwndptr]
set colorptr [cffi::arena pushframe DWORD]
cffi::memory set $colorptr DWORD [HexToBGR $color]
set size [cffi::type size DWORD]
DwmSetWindowAttribute $parentptr $DWMWA_CAPTION_COLOR $colorptr $size
cffi::arena popframe
cffi::pointer dispose $hwndptr
cffi::pointer dispose $parentptr
}
tailcall ::tkcon::SetWindowColor $window $color
}
## ::tkcon::Init - inits tkcon
#
# Calls: ::tkcon::InitUI
# Outputs: errors found in tkcon's resource file
##
proc ::tkcon::Init {args} {
variable VERSION
variable OPT
variable COLOR
variable PRIV
global errorInfo
set ::tcl_interactive 1
set argc [llength $args]
##
## When setting up all the default values, we always check for
## prior existence. This allows users who embed tkcon to modify
## the initial state before tkcon initializes itself.
##
if {! [info exists OPT(darkmode)]} {
set OPT(darkmode) [DarkModeSetting]
}
if {$PRIV(WIN32) && $OPT(darkmode)} {
set window_color "#2C2C2C"
foreach class [list [winfo class .] Toplevel] {
bind $class <Map> [list ::tkcon::SetWindowColor %W $window_color]
}
}
set bg_color [expr {$OPT(darkmode) ? "#222222" : "#FFFFFF"}]
set body_color [expr {$OPT(darkmode) ? "#DBDBDB" : "#292929"}]
set accent_color [expr {$OPT(darkmode) ? "#5681FF" : "#4B75FF"}]
set color_defaults [list stderr [expr {$OPT(darkmode) ? "#D73220" : "#F03823"}] \
stdin $body_color \
stdout $body_color \
prompt $body_color \
var $accent_color \
proc $accent_color \
find-bg [expr {$OPT(darkmode) ? "#E06400" : "#D45B00"}] \
find-fg "#000000" \
blink-bg $accent_color \
blink-fg "#FFFFFF" \
tab-fg [expr {$OPT(darkmode) ? "#8A8A8A" : "#717171"}] \
tab-hover-fg [expr {$OPT(darkmode) ? "#AFAFAF" : "#505050"}] \
tab-selected-fg [expr {$OPT(darkmode) ? "#DBDBDB" : "#292929"}] \
tab-bg [expr {$OPT(darkmode) ? "#111111" : "#E9E9E9"}] \
tab-hover-bg [expr {$OPT(darkmode) ? "#1B1B1B" : "#F8F8F8"}] \
tab-selected-bg [expr {$OPT(darkmode) ? "#222222" : "#FFFFFF"}] \
status-fg [expr {$OPT(darkmode) ? "#AFAFAF" : "#505050"}] \
]
foreach {key default} $color_defaults {
if {![info exists COLOR($key)]} { set COLOR($key) $default }
}
foreach {key default} {
autoload {}
blinktime 500
blinkrange 1
buffer 2048
maxlinelen 0
calcmode 0
confirmExit 1
debugPrompt {(level \#$level) debug [history nextid] > }
dead {}
edit edit
expandorder {Methodname Pathname Variable Procname}
history 48
hoterrors 1
library {}
lightbrace 1
lightcmd 1
maineval {}
maxmenu 18
nontcl 0
prompt1 {ignore this, it's set below}
scrollypos right
showmenu 1
showmultiple 1
showstatusbar 1
childeval {}
childexit close
subhistory 1
tabspace 8
gc-delay 60000
gets {congets}
overrideexit 1
usehistory 1
resultfilter {}
runcmd {}
exec child
} {
if {![info exists OPT($key)]} { set OPT($key) $default }
}
InitFonts
set OPT(cols) [expr {[info exists OPT(cols)] ? $OPT(cols) : 100}]
set OPT(rows) [expr {[info exists OPT(rows)] ? $OPT(rows) : [CalcRowsFromCols $OPT(cols)]}]
foreach {key default} {
app {}
appname {}
apptype child
namesp ::
cmd {}
cmdbuf {}
cmdsave {}
event 1
deadapp 0
deadsock 0
debugging 0
displayWin .
histid 0
find {}
find,case 0
find,reg 0
errorInfo {}
protocol exit
showOnStartup 1
childprocs {
alias tkcon_clear tkcon_dir dump echo idebug tkcon_lremove
tkcon_puts tkcon_gets observe observe_var unalias which what
}
docs "file:%%DOCSDIR%%/index.html"
root .
uid 0
tabs {}
} {
if {![info exists PRIV($key)]} { set PRIV($key) $default }
}
foreach {key default} {
childalias { $OPT(edit) more less tkcon }
} {
if {![info exists PRIV($key)]} { set PRIV($key) [subst $default] }
}
set PRIV(version) $VERSION
if {![info exists OPT(title)]} {
set OPT(title) "tkcon $PRIV(version)"
}
if {[info exists PRIV(name)]} {
set title $PRIV(name)
} else {
MainInit
# some main initialization occurs later in this proc,
# to go after the UI init
set MainInit 1
set title Main
}
## NOTES FOR STAYING IN PRIMARY INTERPRETER:
##
## If you set ::tkcon::OPT(exec) to {}, then instead of a multiple
## interp model, you get tkcon operating in the main interp by default.
## This can be useful when attaching to programs that like to operate
## in the main interpreter (for example, based on special wish'es).
## You can set this from the command line with -exec ""
## A side effect is that all tkcon command line args will be used
## by the first console only.
#set OPT(exec) {}
if {$PRIV(WWW)} {
lappend PRIV(childalias) history
set OPT(prompt1) {[history nextid] % }
} else {
lappend PRIV(childprocs) tcl_unknown unknown
set OPT(prompt1) {([file tail [pwd]]) [history nextid] % }
}
## If we are using the default '.' toplevel, and there appear to be
## children of '.', then make sure we use a disassociated toplevel.
if {$PRIV(root) eq "." && [llength [winfo children .]]} {
set PRIV(root) .tkcon
}
## Do platform specific configuration here, other than defaults
### Use tkcon.cfg filename for resource filename on non-unix systems
### Determine what directory the resource file should be in
switch $::tcl_platform(platform) {
macintosh {
if {![interp issafe]} {cd [file dirname [info script]]}
set envHome PREF_FOLDER
set rcfile tkcon.cfg
set histfile tkcon.hst
catch {console hide}
}
windows {
set envHome HOME
set rcfile tkcon.cfg
set histfile tkcon.hst
}
unix {
set envHome HOME
set rcfile .tkconrc
set histfile .tkcon_history
}
}
if {[info exists ::env($envHome)]} {
set home $::env($envHome)
if {[file pathtype $home] eq "volumerelative"} {
# Convert 'C:' to 'C:/' if necessary, innocuous otherwise
append home /
}
if {![info exists PRIV(rcfile)]} {
set PRIV(rcfile) [file join $home $rcfile]
}
if {![info exists PRIV(histfile)]} {
set PRIV(histfile) [file join $home $histfile]
}
}
## Handle command line arguments before sourcing resource file to
## find if resource file is being specified (let other args pass).
if {[set i [lsearch -exact $args -rcfile]] != -1} {
set PRIV(rcfile) [lindex $args [incr i]]
}
if {!$PRIV(WWW) && [file exists $PRIV(rcfile)]} {
set code [catch {uplevel \#0 [list source $PRIV(rcfile)]} err]
}
if {[info exists ::env(TK_CON_LIBRARY)]} {
lappend ::auto_path $::env(TK_CON_LIBRARY)
} elseif {$OPT(library) ne ""} {
lappend ::auto_path $OPT(library)
}
if {![info exists ::tcl_pkgPath]} {
set dir [file join [file dirname [info nameofexec]] lib]
if {[llength [info commands @scope]]} {
set dir [file join $dir itcl]
}
catch {source [file join $dir pkgIndex.tcl]}
}
catch {tclPkgUnknown dummy-name dummy-version}
## Handle rest of command line arguments after sourcing resource file
## and child is created, but before initializing UI or setting packages.
set childargs {}
set childfiles {}
set childargv0 {}
set truth {^(1|yes|true|on)$}
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $args $i]
if {[string match {-*} $arg]} {
set val [lindex $args [incr i]]
## Handle arg based options
switch -glob -- $arg {
-- - -argv - -args {
set childargs [concat $childargs [lrange $args $i end]]
set ::argv $childargs
set ::argc [llength $::argv]
break
}
-color-* { set COLOR([string range $arg 7 end]) $val }
-exec { set OPT(exec) $val }
-runcmd { set OPT(runcmd) $val }
-main - -e - -eval { append OPT(maineval) \n$val\n }
-package - -load {
lappend OPT(autoload) $val
}
-child { append OPT(childeval) \n$val\n }
-nontcl { set OPT(nontcl) [regexp -nocase $truth $val]}
-root { set PRIV(root) $val }
-font { set OPT(font) $val }
-rcfile {}
default { lappend childargs $arg; incr i -1 }
}
} elseif {[file isfile $arg]} {
if {$i == 0} {
set childargv0 $arg
}
lappend childfiles $arg
} else {
lappend childargs $arg
}
}
## Create child executable
if {$OPT(exec) ne ""} {
InitChild $OPT(exec) $childargs $childargv0
} else {
set argc [llength $childargs]
set args $childargs
uplevel \#0 $childargs
}
# Try not to make tkcon override too many standard defaults, and only
# do it for the tkcon bits
set optclass [tk appname]$PRIV(root)
option add $optclass*Menu.tearOff 0
option add $optclass*Menu.borderWidth 1
option add $optclass*Menu.activeBorderWidth 1
option add $optclass*font tkcon-sans-serif-small
option add $optclass*Text.font tkcon-fixed 100 ;# High priority to override themes
option add $optclass*Text.background $bg_color 100
option add $optclass*Text.foreground $body_color 100
option add $optclass*Text.insertBackground $body_color 100
option add $optclass*Text.relief flat
if {!$PRIV(AQUA)} {
option add $optclass*Scrollbar.borderWidth 1
}
## Attach to the child, EvalAttached will then be effective
Attach $PRIV(appname) $PRIV(apptype)
InitUI $title
if {$OPT(exec) ne ""} {
# override exit to DeleteTab now that tab has been created
$OPT(exec) alias exit ::tkcon::DeleteTab $PRIV(curtab) $OPT(exec)
}
## swap puts and gets with the tkcon versions to make sure all
## input and output is handled by tkcon
if {![catch {rename ::puts ::tkcon_tcl_puts}]} {
interp alias {} ::puts {} ::tkcon_puts
if {[llength [info commands ::tcl::chan::puts]]} {
interp alias {} ::tcl::chan::puts {} ::tkcon_puts
}
}
if {($OPT(gets) ne "") && ![catch {rename ::gets ::tkcon_tcl_gets}]} {
interp alias {} ::gets {} ::tkcon_gets
if {[llength [info commands ::tcl::chan::gets]]} {
interp alias {} ::tcl::chan::gets {} ::tkcon_gets
}
}
EvalChild history keep $OPT(history)
if {[info exists MainInit]} {
# Source history file only for the main console, as all child
# consoles will adopt from the main's history, but still
# keep separate histories
if {!$PRIV(WWW) && $OPT(usehistory) && [file exists $PRIV(histfile)]} {
puts -nonewline "loading history file ... "
# The history file is built to be loaded in and
# understood by tkcon
if {[catch {uplevel \#0 [list source $PRIV(histfile)]} herr]} {
puts stderr "error:\n$herr"
append PRIV(errorInfo) $::errorInfo\n
}
set PRIV(event) [EvalChild history nextid]
puts "[expr {$PRIV(event)-1}] events added"
}
}
## Autoload specified packages in child
set pkgs [EvalChild package names]
foreach pkg $OPT(autoload) {
puts -nonewline "autoloading package \"$pkg\" ... "
if {[lsearch -exact $pkgs $pkg]>-1} {
if {[catch {EvalChild package require [list $pkg]} pkgerr]} {
puts stderr "error:\n$pkgerr"
append PRIV(errorInfo) $::errorInfo\n
} else { puts "OK" }
} else {
puts stderr "error: package does not exist"
}
}
## Evaluate maineval in child
if {($OPT(maineval) ne "") && [catch {uplevel \#0 $OPT(maineval)} merr]} {
puts stderr "error in eval:\n$merr"
append PRIV(errorInfo) $::errorInfo\n
}
## Source extra command line argument files into child executable
foreach fn $childfiles {
puts -nonewline "child sourcing \"$fn\" ... "
if {[catch {EvalChild uplevel \#0 [list source $fn]} fnerr]} {
puts stderr "error:\n$fnerr"
append PRIV(errorInfo) $::errorInfo\n
} else { puts "OK" }
}
## Evaluate childeval in child
if {($OPT(childeval) ne "")
&& [catch {interp eval $OPT(exec) $OPT(childeval)} serr]} {
puts stderr "error in child eval:\n$serr"
append PRIV(errorInfo) $::errorInfo\n
}
## Output any error/output that may have been returned from rcfile
if {[info exists code] && $code && ($err ne "")} {
puts stderr "error in $PRIV(rcfile):\n$err"
append PRIV(errorInfo) $::errorInfo
}
if {$OPT(exec) ne ""} {
StateCheckpoint [concat $PRIV(name) $OPT(exec)] child
}
StateCheckpoint $PRIV(name) child
puts "buffer line limit:\
[expr {$OPT(buffer)?$OPT(buffer):{unlimited}}] \
max line length:\
[expr {$OPT(maxlinelen)?$OPT(maxlinelen):{unlimited}}]"
Prompt "$title console display active (Tcl$::tcl_patchLevel / Tk$::tk_patchLevel)\n"
if {$OPT(runcmd) ne ""} {
::tkcon::EvalCmd $PRIV(console) $OPT(runcmd)
}
}
## ::tkcon::InitChild - inits the child by placing key procs and aliases in it
## It's arg[cv] are based on passed in options, while argv0 is the same as
## the master. tcl_interactive is the same as the master as well.
# ARGS: child - name of child to init. If it does not exist, it is created.
# args - args to pass to a child as argv/argc
##
proc ::tkcon::InitChild {child {childargs {}} {childargv0 {}}} {
variable OPT
variable COLOR
variable PRIV
global argv0 env auto_path
if {$child eq ""} {
return -code error "Don't init the master interpreter, goofball"
}
if {![interp exists $child]} { interp create $child }
if {[interp eval $child info command source] eq ""} {
$child alias source SafeSource $child
$child alias load SafeLoad $child
$child alias open SafeOpen $child
$child alias file file
interp eval $child \
[list set auto_path [tkcon_lremove $auto_path $::tk_library]]
interp eval $child [dump var -nocomplain ::tcl_library env]
interp eval $child { catch {source [file join $::tcl_library init.tcl]} }
interp eval $child { catch unknown }
}
# This will likely be overridden to call DeleteTab where possible
$child alias exit exit
interp eval $child {
# Do package require before changing around puts/gets
catch {set __tkcon_error ""; set __tkcon_error $errorInfo}
catch {package require bogus-package-name}
catch {rename ::puts ::tkcon_tcl_puts}
set errorInfo ${__tkcon_error}
unset __tkcon_error
}
foreach cmd $PRIV(childprocs) { $child eval [dump proc $cmd] }
foreach cmd $PRIV(childalias) { $child alias $cmd $cmd }
interp alias $child ::ls $child ::tkcon_dir -full
interp alias $child ::puts $child ::tkcon_puts
if {[llength [info commands ::tcl::chan::puts]]} {
interp alias $child ::tcl::chan::puts $child ::tkcon_puts
}
if {$OPT(gets) ne ""} {
interp eval $child { catch {rename ::gets ::tkcon_tcl_gets} }
interp alias $child ::gets $child ::tkcon_gets
if {[llength [info commands ::tcl::chan::gets]]} {
interp alias $child ::tcl::chan::gets $child ::tkcon_gets
}
}
if {$childargv0 ne ""} {
# If tkcon was invoked with 1 or more filenames, then make the
# first filename argv0 in the child, as tclsh/wish would do it.
interp eval $child [list set argv0 $childargv0]
} else {
if {[info exists argv0]} {interp eval $child [list set argv0 $argv0]}
}
interp eval $child set tcl_interactive $::tcl_interactive \; \
set auto_path [list [tkcon_lremove $auto_path $::tk_library]] \; \
set argc [llength $childargs] \; \
set argv [list $childargs] \; {
if {![llength [info command bgerror]]} {
proc bgerror err {
set body [info body bgerror]
rename ::bgerror {}
if {[auto_load bgerror]} { return [bgerror $err] }
proc bgerror err $body
tkcon bgerror $err $::errorInfo
}
}
}
foreach pkg [tkcon_lremove [package names] Tcl] {
foreach v [package versions $pkg] {
interp eval $child [list package ifneeded $pkg $v \
[package ifneeded $pkg $v]]
}
}
}
## ::tkcon::InitInterp - inits an interpreter by placing key
## procs and aliases in it.
# ARGS: name - interp name
# type - interp type (child|interp)
##
proc ::tkcon::InitInterp {name type} {
variable OPT
variable PRIV
## Don't allow messing up a local master interpreter
if {($type eq "namespace")
|| (($type eq "child") &&
[regexp {^([Mm]ain|Interp[0-9]+)$} $name])} { return }
set old [Attach]
set oldname $PRIV(namesp)
catch {
Attach $name $type
EvalAttached { catch {rename ::puts ::tkcon_tcl_puts} }
foreach cmd $PRIV(childprocs) { EvalAttached [dump proc $cmd] }
switch -exact $type {
child {
foreach cmd $PRIV(childalias) {
Main [list interp alias $name ::$cmd $PRIV(name) ::$cmd]
}
}
interp {
set thistkcon [::send::appname]
foreach cmd $PRIV(childalias) {
EvalAttached "proc $cmd args { ::send::send [list $thistkcon] $cmd \$args }"
}
}
}
## Catch in case it's a 7.4 (no 'interp alias') interp
EvalAttached {
catch {interp alias {} ::ls {} ::tkcon_dir -full}
if {[catch {interp alias {} ::puts {} ::tkcon_puts}]} {
catch {rename ::tkcon_puts ::puts}
} elseif {[llength [info commands ::tcl::chan::puts]]} {
catch {interp alias {} ::tcl::chan::puts {} ::tkcon_puts}
}
}
if {$OPT(gets) ne ""} {
EvalAttached {
catch {rename ::gets ::tkcon_tcl_gets}
if {[catch {interp alias {} ::gets {} ::tkcon_gets}]} {
catch {rename ::tkcon_gets ::gets}
} elseif {[llength [info commands ::tcl::chan::gets]]} {
catch {interp alias {} ::tcl::chan::gets {} ::tkcon_gets}
}
}
}
return
} {err}
eval Attach $old
AttachNamespace $oldname