-
Notifications
You must be signed in to change notification settings - Fork 4
/
tedit
executable file
·1722 lines (1628 loc) · 60.4 KB
/
tedit
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 tclsh
package require Tk
# force utf-8 encoding (freewrap workaround)
if {[encoding system] ne "utf-8"} {encoding system "utf-8"}
################################################################################
# VARIABLES
#
namespace eval tEdit {
array set filename {}
array set folds {}
array set marks {}
array set marknum {}
array set modified {}
variable version {1.7.11}
variable safe [interp create -safe]
variable filepath {}
variable message {}
variable encoding [encoding system]
variable tabnum 0
variable untitlednum 1
variable savename {}
variable recent [list]
variable filetypes {{{All Files} {*}}
{{Text Files} {*.txt *.TXT}} {{Log Files} {*.log *.LOG}}
{{C Files} {*.c *.C}} {{C++ Files} {*.cpp *.CPP}} {{Header Files} {*.h *.H}}
{{Go Files} {*.go *.GO}}
{{Perl Files} {*.pl *.PL}} {{Perl Modules} {*.pm *.PM}}
{{Python Files} {*.py *.PY}}
{{Ruby Files} {*.rb *.RB}}
{{Rust Files} {*.rs *.RS}}
{{Tcl Files} {*.tcl *.TCL}}
}
variable goto {}
variable hgoto [list]
variable showgoto {false}
variable blockselect [list]
variable blockpaste {false}
variable search {}
variable hsearch [list]
variable regexp {-exact}
variable replace {}
variable hreplace [list]
variable showsearch {false}
variable showreplace {false}
variable showmarks {false}
variable browser {false}
variable linenumbers {true}
variable showmenu {.menu}
variable showstatus {true}
variable showyscroll {true}
variable showxscroll {true}
variable ontop {false}
variable precision 3
variable autoindent {true}
variable wrap {none}
variable blinkcursor 500
variable blockcursor {false}
variable textfont {TkFixedFont}
variable spacing1 0
variable spacing2 0
variable spacing3 0
variable colorscheme {default}
variable theme [ttk::style theme use]
variable tabsize 2
variable softtabs {true}
namespace eval DnD {
variable timer {}
variable start {}
}
}
# add precision and math functions
# for use with [tEdit::Calculate]
namespace eval tcl::mathfunc {
proc precision {number precision} {
expr {double(round(10 ** $precision * $number)) / 10 ** $precision}
}
# e = exp(1)
proc e {} {return 2.718281828459045}
# π = acos(-1)
proc pi {} {return 3.141592653589793}
# φ = (1+sqrt(5))/2
proc phi {} {return 1.618033988749895}
# trigonometric functions with degrees
proc sind {x} {expr {sin($x * pi() / 180)}}
proc cosd {x} {expr {cos($x * pi() / 180)}}
proc tand {x} {expr {tan($x * pi() / 180)}}
proc asind {x} {expr {asin($x) * 180 / pi()}}
proc acosd {x} {expr {acos($x) * 180 / pi()}}
proc atand {x} {expr {atan($x) * 180 / pi()}}
# teach safe interpreter the new functions
foreach function [info procs [namespace current]::*] {
$tEdit::safe alias $function $function
}
# use ln() instead of log()
$tEdit::safe alias [namespace current]::ln [namespace current]::log
# use log() instead of log10()
$tEdit::safe alias [namespace current]::log [namespace current]::log10
}
################################################################################
# PROCEDURES
#
proc tEdit::Message {msg {timer 5000}} {
after cancel {set tEdit::message ""}
set tEdit::message $msg
after $timer {set tEdit::message ""}
}
proc tEdit::RecentAdd {filename} {
if {$filename ni $tEdit::recent} {
lappend tEdit::recent $filename
.menu.file entryconfigure "Recent Files" -state normal
.menu.file.recent add command -label $filename -command [list tEdit::OpenFile $filename]
}
}
proc tEdit::Modified {text} {
set tEdit::modified(text) [expr {[$text edit modified] ? "\u2022" : ""}]
}
proc tEdit::NewTab {{position ""} {filename ""}} {
set nb .nb
set tabnum $tEdit::tabnum
# set array for tabbed widgets
array set tab [list \
{tab} "${nb}.tab${tabnum}"\
{canvas} "${nb}.tab${tabnum}.canvas"\
{text} "${nb}.tab${tabnum}.text"\
{yscroll} "${nb}.tab${tabnum}.yscroll"\
{xscroll} "${nb}.tab${tabnum}.xscroll"\
{modified} "${nb}.tab${tabnum}.modified"\
]
# set arrays specific to this tab
array set tEdit::filename [list $tab(tab) $filename]
array set tEdit::folds [list $tab(text) [list]]
array set tEdit::marks [list $tab(text) [list]]
array set tEdit::marknum [list $tab(text) 0]
array set tEdit::modified [list $tab(text) ""]
# increase tab number for future use
incr tEdit::tabnum
ttk::frame $tab(tab)
tk::canvas $tab(canvas) -width 10 -highlightthickness 0 -background "#FFFF00"
tk::text $tab(text) -relief sunken -highlightthickness 0 -takefocus 1 \
-font $tEdit::textfont -undo true -maxundo 0 -autoseparators true \
-insertunfocussed hollow -wrap $tEdit::wrap \
-tabstyle wordprocessor -tabs $tEdit::tabsize \
-blockcursor $tEdit::blockcursor -insertofftime $tEdit::blinkcursor \
-spacing1 $tEdit::spacing1 -spacing2 $tEdit::spacing2 -spacing3 $tEdit::spacing3 \
-xscrollcommand [list $tab(xscroll) set] -yscrollcommand [list $tab(yscroll) set]
# text tags management and starting colors
$tab(text) tag configure tag_search -foreground "#FFFF00" -background "#000000"
$tab(text) tag configure tag_search_all -foreground "#000000" -background "#FFFF00"
$tab(text) tag raise tag_search tag_search_all
ttk::scrollbar $tab(yscroll) -orient vertical -command [list $tab(text) yview]
ttk::scrollbar $tab(xscroll) -orient horizontal -command [list $tab(text) xview]
ttk::label $tab(modified) -textvariable tEdit::modified(text)
grid $tab(canvas) -in $tab(tab) -row 0 -column 0 -sticky ns
grid $tab(text) -in $tab(tab) -row 0 -column 1 -sticky nswe
grid $tab(yscroll) -in $tab(tab) -row 0 -column 2 -sticky ns
grid $tab(xscroll) -in $tab(tab) -row 1 -column 0 -sticky we -columnspan 2
grid $tab(modified) -in $tab(tab) -row 1 -column 2
grid rowconfigure $tab(tab) $tab(text) -weight 1
grid columnconfigure $tab(tab) $tab(text) -weight 1
# remove canvas from view
grid remove $tab(canvas)
# need to disable this for Tabs Traversal
bind $tab(text) <Control-Tab> {return 0}
# bindings for block selections
bind Text <Control-ButtonPress-1> {
set tEdit::blockselect [list %x %y]
%W mark set insert @%x,%y
%W see insert
break
}
bind $tab(text) <Control-Motion> {
if {$tEdit::blockselect ne ""} {
tEdit::BlockSelect %W %x %y
%W mark set insert @%x,%y
%W see insert
break
}
}
bind Text <Control-ButtonRelease-1> {
tEdit::BlockSelect %W %x %y
set tEdit::blockselect [list]
break
}
if {$filename eq ""} {
set filename "Untitled $tEdit::untitlednum"
incr tEdit::untitlednum
} else {
set filename [file tail $filename]
}
# where to insert new tab
if {$position eq "" || ($position + 1) >= [$nb index end]} {
set position end
} else {
incr position
}
$nb insert $position $tab(tab) -text $filename
tEdit::TabOptions new $tab(tab)
$nb select $tab(tab)
focus $tab(text)
return $tab(text)
}
proc tEdit::CloseTab {tab {nb ".nb"}} {
if {[llength $tab] > 1} {set tab [$nb select]}
set text "${tab}.text"
if {[tEdit::AskSave $tab $text] != 0} {return 2}
$nb forget $tab
destroy $tab
if {[$nb index end] < 1} {exit}
}
proc tEdit::OpenFileDialog {} {
set filenames [tk_getOpenFile -title "Open File" -parent . \
-filetypes $tEdit::filetypes -multiple true]
if {$filenames eq ""} {return 2}
foreach filename $filenames {
tEdit::OpenFile $filename
}
}
proc tEdit::OpenFile {filename} {
foreach tab [.nb tabs] {
if {$filename eq $tEdit::filename($tab)} {
.nb select $tab
tEdit::Message "[file tail $filename] already open"
return 1
}
}
if {[file type $filename] ne "file"} {
puts stderr "Cannot open: $filename is [file type $filename]"
tk_messageBox -title "Cannot open file!" -icon error -type ok \
-parent . -message "Cannot open file:" \
-detail "${filename}\nis a [file type $filename]."
return 1
}
if {[catch {set data [open $filename r]} msg]} {
puts stderr $msg
tEdit::Message [lindex [split $msg ":"] 1]
return 1
}
chan configure $data -translation auto
set text [tEdit::NewTab [.nb index current] $filename]
$text insert end [chan read -nonewline $data]
chan close $data
$text mark set insert 0.0
$text see insert
$text edit modified 0
$text edit reset
tEdit::RecentAdd $filename
}
proc tEdit::GetPipedData {} {
chan event stdin readable {}
set data ""
while {[chan gets stdin line] >= 0} {
append data $line "\n"
}
set data [string range $data 0 end-1]
if {[string length $data] > 0} {
set text [tEdit::NewTab {} "<<STDIN>>"]
$text insert end $data
}
}
proc tEdit::StoreFilename {filename} {
set tab [.nb select]
set tEdit::filename($tab) $filename
.nb tab $tab -text [file tail $filename]
}
proc tEdit::SaveFileDialog {} {
set tabname [.nb tab [.nb select] -text]
set filename [tk_getSaveFile -title "Save As" -parent . \
-filetypes $tEdit::filetypes -initialfile $tabname]
if {$filename eq ""} {return 2}
if {[file exists $filename]} {
if {[file type $filename] ne "file"} {
puts stderr "Cannot save: $filename is [file type $filename]"
tk_messageBox -title "Cannot save file!" -icon error -type ok \
-parent . -message "Cannot save [file tail $filename]:" \
-detail "$filename\nis a [file type $filename]."
return 1
} elseif {[file writable $filename] == 0} {
puts stderr "Cannot save: $filename is read only"
tk_messageBox -title "Cannot save file!" -icon error -type ok \
-parent . -message "Cannot save [file tail $filename]:" \
-detail "${filename}\nis read only."
return 1
}
}
set tEdit::savename $filename
return 0
}
proc tEdit::SaveFile {filename text} {
if {$filename eq ""} {
if {[tEdit::SaveFileDialog] == 0} {
set filename $tEdit::savename
} else {
return 2
}
}
if {[catch {set data [open $filename w]} msg]} {
puts stderr $msg
tEdit::Message [lindex [split $msg ":"] 1]
return 1
}
chan puts -nonewline $data [$text get 0.0 end]
chan flush $data
chan close $data
$text edit modified 0
tEdit::StoreFilename $filename
tEdit::RecentAdd $filename
tEdit::Message "[file tail $filename] saved"
set tEdit::filepath [file nativename $filename]
return 0
}
proc tEdit::SaveAs {text} {
if {[tEdit::SaveFileDialog] == 0} {
tEdit::SaveFile $tEdit::savename $text
}
}
proc tEdit::AskSave {tab text} {
if {[$text edit modified]} {
set answer [tk_messageBox -title "Save file?" -icon warning -parent . \
-type yesnocancel -default yes \
-message "[.nb tab $tab -text] has changed!" -detail "Save changes?"]
switch -- $answer {
yes {
if {$tEdit::filename($tab) eq ""} {
if {[tEdit::SaveFileDialog] == 0} {
return [tEdit::SaveFile $tEdit::savename $text]
} else {
return 2
}
} else {
return [tEdit::SaveFile $tEdit::filename($tab) $text]
}
}
no {return 0}
cancel {return 2}
}
}
return 0
}
proc tEdit::CloseAll {{noclose ""}} {
foreach tab [.nb tabs] {
# 'close others' tabs menu command
if {$tab eq $noclose} {
continue
}
.nb select $tab
if {[tEdit::CloseTab $tab] == 2} {return 2}
}
}
proc tEdit::UndoStack {text} {
foreach {item action} {Undo canundo Redo canredo} {
.menu.edit entryconfigure $item \
-state [expr {[$text edit $action] ? "normal" : "disabled"}]
}
}
proc tEdit::UndoRedo {text action} {
if {[catch {$text edit $action} msg]} {
tEdit::Message $msg
}
}
proc tEdit::SelectAll {text} {
if {[$text get 0.0 "end -1 indices"] eq ""} {return 1}
$text tag add sel 0.0 "end -1 indices"
}
proc tEdit::PopulateBrowser {node {tree ".tree"}} {
set path [$tree set $node fullpath]
$tree delete [$tree children $node]
if {[catch {glob -nocomplain -directory $path *} msg]} {
tEdit::Message [lindex [split $msg ":"] 1]
return 1
}
foreach f [lsort -dictionary [glob -nocomplain -directory $path *]] {
set type [file type $f]
set id [$tree insert $node end -text [file tail $f] \
-values [list $f $type]]
if {$type eq "directory"} {
$tree insert $id 0
$tree item $id -text [file tail $f]/
}
}
}
proc tEdit::StartBrowser {{tree ".tree"}} {
foreach dir [lsort -dictionary [file volumes]] {
tEdit::PopulateBrowser [$tree insert {} end -text $dir \
-values [list $dir directory]] $tree
}
}
proc tEdit::OpenFileBrowser {node {tree ".tree"}} {
if {$node eq ""} {return 2}
if {[$tree set $node type] eq "directory"} {
tEdit::PopulateBrowser $node $tree
} else {
tEdit::OpenFile [$tree set $node fullpath]
}
}
proc tEdit::ToggleBrowser {show} {
if {$show eq "true"} {
.main insert 0 .browser
} else {
.main forget .browser
}
}
proc tEdit::Copy {text action} {
if {[set selected [$text tag ranges sel]] eq ""} {return 1}
if {[llength $selected] > 2} {
set prev [$text index sel.first]
foreach {first last} $selected {
set lines [expr {$first - $prev}]
if {$lines >= 2} {
set length [string length [$text get $first $last]]
for {set i 1} {$i < $lines} {incr i} {
append data [string repeat " " $length] "\n"
}
}
append data [$text get $first $last] "\n"
if {$action eq "Cut"} {
$text delete $first $last
}
set prev $first
}
set data [string trimright $data "\n"]
clipboard clear
clipboard append -type STRING -- $data
set tEdit::blockpaste "true"
} else {
if {$action eq "Cut"} {
tk_textCut $text
} else {
tk_textCopy $text
}
set tEdit::blockpaste "false"
}
}
proc tEdit::Paste {text} {
if {$tEdit::blockpaste eq "true"} {
set type [expr {[tk windowingsystem] eq "x11" ? "UTF8_STRING" : "STRING"}]
set data [split [clipboard get -type $type] "\n"]
set cursor [$text index insert]
set ycursor [lindex [split $cursor "."] end]
set lastline [$text index end]
set i 0
foreach block $data {
set line [expr {$cursor + $i}]
if {$line >= $lastline} {
$text insert end "\n"
$text see "$cursor +${i} lines"
}
set eol [$text index "$line lineend"]
set yeol [lindex [split $eol "."] end]
set missing [expr {$ycursor - $yeol}]
if {$missing > 0} {
$text insert $eol [string repeat " " $missing]
}
$text insert "$cursor +${i} lines" $block
incr i
}
} else {
tk_textPaste $text
$text see insert
}
}
proc tEdit::BlockSelect {text x2 y2} {
selection clear
set x1 [lindex $tEdit::blockselect 0]
set y1 [lindex $tEdit::blockselect 1]
if {$x1 > $x2} {set x $x2;set endx $x1} else {set x $x1;set endx $x2}
if {$y1 > $y2} {set y $y2;set endy $y1} else {set y $y1;set endy $y2}
while {$y <= $endy} {
$text tag add sel "@$x,$y" "@$endx,$y"
incr y
}
}
proc tEdit::DeleteSelection {text key} {
if {[set selected [$text tag ranges sel]] eq ""} {
switch -- $key {
Delete {$text delete "insert"}
Backspace {$text delete "insert -1 indices"}
default {return 1}
}
} else {
foreach {first last} $selected {
$text delete $first $last
}
}
}
proc tEdit::BlockIndent {text action} {
if {$tEdit::softtabs eq "true"} {
set indent [string repeat " " $tEdit::tabsize]
} else {
set indent "\t"
}
# check if a selection exists
if {[set selected [$text tag ranges sel]] eq ""} {
foreach idx {linestart lineend} {
lappend blockindent [$text index "insert $idx"]
}
} else {
set first [expr {int([lindex $selected 0])}]
set last [expr {int([lindex $selected end])}]
for {set line $first} {$line <= $last} {incr line} {
if {[$text get "${line}.0" "${line}.end"] ne ""} {
lappend blockindent "${line}.0" "${line}.end"
}
}
}
switch -- $action {
increase {
foreach {first last} $blockindent {
$text insert $first $indent
}
tEdit::Message "increased indentation"
}
decrease {
set indent [string length $indent]
set whitespace ""
foreach {first last} $blockindent {
$text search -regexp -all -count whitespace -- {^([ \t]*)} $first $last
if {$whitespace >= $indent} {
$text delete [$text index $first] [$text index "$first +${indent} indices"]
} elseif {$whitespace >= 1} {
$text delete [$text index $first] [$text index "$first +${whitespace} indices"]
} else {
set whitespace ""
}
}
if {$whitespace eq ""} {
tEdit::Message "cannot decrease further"
} else {
tEdit::Message "decreased indentation"
}
}
default {
return 1
}
}
}
proc tEdit::AutoIndent {text} {
if {$tEdit::autoindent eq "true"} {
bind $text <Return> {
# find any whitespace at the start of the current line
set first [%W index "insert linestart"]
set last [%W index "insert lineend"]
regexp {^([ \t]*)} [%W get $first $last] -> whitespace
# create a newline, insert whitespace
%W insert insert "\n${whitespace}"
# if necessary, scroll the view so cursor is visible
%W see insert
break
}
} else {
bind $text <Return> {
%W insert insert "\n"
%W see insert
break
}
}
}
proc tEdit::Calculate {text} {
if {[$text tag ranges sel] eq ""} {
set first [$text index "insert linestart"]
set last [$text index "insert lineend"]
} else {
set first [$text index sel.first]
set last [$text index sel.last]
}
set math [$text get $first $last]
try {
$tEdit::safe eval [list expr [string map {/ *1.0/} $math]]
} on error msg {
tEdit::Message [lindex [split $msg "\n"] 0]
} on ok result {
# round to user defined decimal points
set result [expr {precision($result,$tEdit::precision)}]
# avoid printing extra decimal zeros
set result0 [expr {round($result)}]
if {$result0 == $result} {
set result $result0
}
$text insert $last " = $result"
}
}
proc tEdit::Fold {text} {
if {[set selected [$text tag ranges sel]] eq ""} {
tEdit::Message "select lines to fold"
return 1
}
set first [$text index "sel.first linestart"]
set firsteol [$text index "sel.first lineend"]
set last [$text index "sel.last lineend"]
set dfirst [expr {int($first)}]
set dlast [expr {int($last)}]
if {[set foldsum [expr {($dlast - $dfirst) + 1}]] == 1} {
tEdit::Message "cannot fold a single line"
return 1
}
# check if any older folds exist in current selection
# and unfold them
foreach old $tEdit::folds($text) {
set oldtag [$text tag ranges $old]
set oldfirst [lindex $oldtag 0]
set oldlast [lindex $oldtag end]
if {[$text compare $first <= $oldfirst] && [$text compare $last >= $oldlast]} {
$old invoke
}
}
# get first line to show in fold button text
set buttontext "\u25b6\u25b6 $foldsum lines: [$text get $first $firsteol]"
set fold [string cat $text ".fold" $dfirst "-" $dlast]
tk::button $fold -text $buttontext -relief flat -padx 0 -pady 0 \
-takefocus 0 -cursor "hand2" -font $tEdit::textfont \
-background [$text cget -background] \
-foreground [$text cget -foreground] \
-command [list tEdit::Unfold $text $fold $first]
$text tag add $fold $first $last
$text tag configure $fold -elide true
$text window create $first -window $fold
$text mark set insert "$last +1 lines"
selection clear
lappend tEdit::folds($text) $fold
.menu.edit entryconfigure "Unfold All" -state normal
}
proc tEdit::Unfold {text fold {index ""}} {
# store modified flag, to restore it later
set modified [$text edit modified]
switch -- $fold {
all {
foreach fold $tEdit::folds($text) {
$fold invoke
}
}
default {
$text tag configure $fold -elide false
$text tag delete $fold
# destroy fold button (same name with fold tag)
destroy $fold
# delete the extra index which held the button
$text delete $index
set tEdit::folds($text) \
[lsearch -inline -all -not -exact $tEdit::folds($text) $fold]
}
}
# restore modified flag ('$text delete $index' modifies $text)
$text edit modified $modified
if {[llength $tEdit::folds($text)] == 0} {
.menu.edit entryconfigure "Unfold All" -state disabled
}
}
proc tEdit::Search {what {direction "nextrange"}} {
if {$what ni $tEdit::hsearch} {
lappend tEdit::hsearch $what
}
if {[tEdit::SearchAll $what] == 1} {return 1}
set text "[.nb select].text"
if {$direction eq "prevrange"} {
set newpos "insert -1 indices"
set loop [list end-1 end]
} else {
set newpos "insert +1 indices"
set loop [list 0 1]
}
set taglist [$text tag ranges tag_search_all]
set lastsearch [$text tag ranges tag_search]
set tagsearch [$text tag $direction tag_search_all insert]
# if same search, move insert by one
if {$tagsearch eq $lastsearch} {
set tagsearch [$text tag $direction tag_search_all $newpos]
}
# if search returns null, search from top/bottom
if {$tagsearch eq ""} {
set tagsearch [lrange $taglist {*}$loop]
}
tEdit::ClearHighlight "tag_search"
$text tag add tag_search {*}$tagsearch
$text mark set insert tag_search.first
$text see tag_search.first
set sum [expr {[llength $taglist] / 2}]
set count [lsearch -exact $taglist [lindex $tagsearch 0]]
set count [expr {($count / 2) + 1}]
set matches [expr {$sum == 1 ? "match" : "matches"}]
tEdit::Message "$count of $sum $matches"
}
proc tEdit::SearchAll {what} {
if {$what eq ""} {
tEdit::Message "nothing to search for"
return 1
}
set text "[.nb select].text"
# search only in selection if exists
if {[$text tag ranges sel] ne ""} {
set first "sel.first"
set last "sel.last"
} else {
set first "0.0"
set last "end"
}
set length 0
set search_cmd [list $text search $tEdit::regexp -all -count length -- $what $first $last]
if {[catch {set taglist [{*}$search_cmd]} msg]} {
.cbsearch configure -style error.TCombobox
tEdit::Message [lindex [split $msg ":"] 1]
return 1
} elseif {$taglist eq ""} {
.cbsearch configure -style error.TCombobox
tEdit::Message "no matches found"
return 1
} else {
tEdit::ClearHighlight "tag_search_all"
.cbsearch configure -style valid.TCombobox
set count 0
foreach first $taglist {
set last "$first +[lindex $length $count] indices"
$text tag add tag_search_all $first $last
incr count
}
}
}
proc tEdit::Replace {what with} {
if {$with ni $tEdit::hreplace} {
lappend tEdit::hreplace $with
}
if {$what eq $with} {
tEdit::Message "search and replace are same"
return 1
}
set text "[.nb select].text"
# if tag_search is empty, search for the first time
if {[set taglist [$text tag ranges tag_search]] eq ""} {
if {[tEdit::Search $what] == 1} {
tEdit::Message "nothing to replace"
return 1
}
# recreate $taglist after search
set taglist [$text tag ranges tag_search]
}
$text edit separator
$text replace {*}$taglist $with
tEdit::Search $what
}
proc tEdit::ReplaceAll {what with} {
if {$with ni $tEdit::hreplace} {
lappend tEdit::hreplace $with
}
if {$what eq $with} {
tEdit::Message "search and replace are same"
return 1
}
if {[tEdit::SearchAll $what] == 1} {
tEdit::Message "nothing to replace"
return 1
}
set text "[.nb select].text"
set taglist [$text tag ranges tag_search_all]
$text edit separator
set count 0
foreach {first last} $taglist {
$text replace $first $last $with
incr count
}
tEdit::Message "replace finished: $count substitutions"
}
proc tEdit::ClearHighlight {tags} {
set text "[.nb select].text"
foreach tag $tags {
foreach {first last} [$text tag ranges $tag] {
$text tag remove $tag $first $last
}
}
}
proc tEdit::MarkToggle {} {
set text "[.nb select].text"
set first [$text index "insert linestart"]
set last [$text index "insert lineend"]
set mark [lsearch -inline -all -glob [$text tag names $first] "Mark#*"]
if {$mark eq ""} {
if {[$text get $first $last] eq ""} {
tEdit::Message "cannot mark: empty line"
return 1
}
set mark "Mark#[incr tEdit::marknum($text)]"
$text mark set $mark $first
$text tag configure $mark -underline true
$text tag raise $mark sel
$text tag add $mark $first $last
lappend tEdit::marks($text) $mark
foreach item {"Next Mark" "Clear Marks"} {
.menu.marks entryconfigure $item -state normal
}
} else {
tEdit::MarkDelete $mark
}
}
proc tEdit::MarkSee {mark} {
set text "[.nb select].text"
if {$mark eq ""} {return 1}
$text mark set insert $mark
$text see $mark
focus $text
}
proc tEdit::MarkNext {markbox} {
set text "[.nb select].text"
if {[llength $tEdit::marks($text)] == 0} {
tEdit::Message "no marks found"
return 1
}
set mark [$text mark next "insert +1 indices"]
while {$mark ni $tEdit::marks($text)} {
if {$mark eq ""} {set mark 0.0}
set mark [$text mark next $mark]
}
set index [lsearch -exact $tEdit::marks($text) $mark]
$markbox selection clear 0 end
$markbox selection set $index
$markbox activate $index
$text mark set insert $mark
$text see $mark
}
proc tEdit::MarkDelete {which} {
set text "[.nb select].text"
set markbox .marks
switch -- $which {
all {
foreach mark $tEdit::marks($text) {
$text mark unset $mark
$text tag delete $mark
}
set tEdit::marks($text) [list]
set tEdit::marknum($text) 0
}
active {
set mark [$markbox get active]
$text mark unset $mark
$text tag delete $mark
$markbox delete active
}
default {
$text mark unset $which
$text tag delete $which
$markbox delete [lsearch -exact $tEdit::marks($text) $which]
}
}
if {[llength $tEdit::marks($text)] == 0} {
foreach item {"Next Mark" "Clear Marks"} {
.menu.marks entryconfigure $item -state disabled
}
}
}
proc tEdit::GotoLine {text line} {
if {$line eq ""} {return 1}
# $line is always digit, from combobox validation
if {$line ni $tEdit::hgoto} {
lappend tEdit::hgoto $line
}
if {$line <= [$text index "end -1 indices"]} {
set mark "${line}.0"
$text mark set insert $mark
$text see $mark
.cbgoto configure -style valid.TCombobox
tEdit::Message "gone to line $line"
} else {
.cbgoto configure -style error.TCombobox
tEdit::Message "line $line does not exist"
}
}
proc tEdit::LineNumbers {text canvas args} {
set checklist "configure delete insert see yview"
# canvas font don't need italics, underline or overstrike
# family and size are enough
set font [lrange $tEdit::textfont 0 1]
# check only for items in checklist and elide tags
# $args == 0 when opening a file
if {[llength $args] == 0 || [lindex $args 0 1] in $checklist \
|| [lindex $args 0 4] eq "-elide"} {
$canvas delete all
set i [$text index @0,0]
while true {
set dline [$text dlineinfo $i]
if {[llength $dline] == 0} {break}
set height [lindex $dline 3]
set y [expr {[lindex $dline 1] + $tEdit::spacing1}]
set linenum [lindex [split $i "."] 0]
if {$linenum > 999} {
set width [expr {[font measure $tEdit::textfont $linenum] + 5}]
} else {
set width [expr {[font measure $tEdit::textfont "000"] + 5}]
}
$canvas configure -width $width
$canvas create text 2 $y -anchor nw -text $linenum \
-font $font -fill [$text cget -foreground]
set i [$text index "$i +1 lines linestart"]
}
}
}
proc tEdit::ToggleLineNumbers {text canvas show} {
if {$show eq "true"} {
bind $text <Configure> [list tEdit::LineNumbers $text $canvas]
if {[trace info execution $text] eq ""} {
trace add execution $text leave [list tEdit::LineNumbers $text $canvas]
}
grid $canvas
} else {
bind $text <Configure> {return 0}
trace remove execution $text leave [list tEdit::LineNumbers $text $canvas]
grid remove $canvas
}
}
proc tEdit::ToggleView {widget show} {
if {$show eq "false"} {
grid remove $widget
switch -- $widget {
.lfreplace {focus .cbsearch}
default {focus [.nb select].text}
}
} else {
grid $widget
switch -- $widget {
.fgoto {focus .cbgoto}
.fsearch {focus .cbsearch}
.lfreplace {focus .cbreplace}
default {focus [.nb select].text}
}
}
}
proc tEdit::ToggleSoftTabs {text} {
if {$tEdit::softtabs eq "true"} {
set tabstring [string repeat " " $tEdit::tabsize]
} else {
set tabstring "\t"
}
bind $text <Tab> [list $text insert insert $tabstring]
tEdit::ConfigureTabSize $text
}
proc tEdit::ConfigureTabSize {text} {
$text configure -tabs "[expr {$tEdit::tabsize * [font measure $tEdit::textfont 0]}] left"
}
proc tEdit::SetFont {font} {
set tEdit::textfont $font
foreach tab [.nb tabs] {
set text "${tab}.text"
$text configure -font $font
tEdit::ConfigureTabSize $text
if {$tEdit::folds($text) ne ""} {
foreach fold $tEdit::folds($text) {
$fold configure -font $font
}
}
}
}