/
gui.tcl
executable file
·2127 lines (1879 loc) · 60.4 KB
/
gui.tcl
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
# Simple GUI for Spin
# Copyright 2018-2020 Total Spectrum Software
# Distributed under the terms of the MIT license;
# see License.txt for details.
#
#
# The guts of the IDE GUI
#
set aboutMsg "
GUI tool for FlexProp
Version $spin2gui_version
Copyright 2018-2021 Total Spectrum Software Inc.
------
There is no warranty and no guarantee that
output will be correct.
"
# some Tcl/Tk config
# make sure tcl_wordchars is set
catch {tcl_endOfWord}
# change it
set tcl_wordchars {[[:alnum:]_]}
set tcl_nonwordchars {[^[:alnum:]_]}
#
# global variables
# ROOTDIR was set by our caller to be the directory from which all other
# files are relative (usually the location of the program)
#
# prefix for shortcut keys (Command on Mac, Control elsewhere)
if { [tk windowingsystem] == "aqua" } {
set CTRL_PREFIX "Command"
} else {
set CTRL_PREFIX "Control"
}
# executable prefix; on Windows .exe is automatically appended, so we don't
# have to explicitly specify it
set EXE ""
if { $tcl_platform(os) == "Darwin" && [file exists "$ROOTDIR/bin/flexspin.mac"] && [file exists "$ROOTDIR/bin/loadp2.mac"] } {
set EXE ".mac"
}
if { [file exists "$ROOTDIR/.flexprop.config"] } {
# portable installation
set CONFIGDIR $ROOTDIR
} elseif { [info exists ::env(HOME) ] && [file isdirectory $::env(HOME)] } {
set CONFIGDIR $::env(HOME)
} else {
set CONFIGDIR $ROOTDIR
}
# prefix for starting a command in a window
if { $tcl_platform(platform) == "windows" } {
set WINPREFIX "cmd.exe /c start \"Propeller Output %p\""
} elseif { [tk windowingsystem] == "aqua" } {
set WINPREFIX $ROOTDIR/bin/mac_terminal.sh
} elseif { [file executable /etc/alternatives/x-terminal-emulator] } {
set WINPREFIX "/etc/alternatives/x-terminal-emulator -T \"Propeller Output %p\" -e"
} else {
set WINPREFIX "xterm -fs 14 -T \"Propeller Output %p\" -e"
}
# config file name
set CONFIG_FILE "$CONFIGDIR/.flexprop.config"
# default configuration variables
# the config() array is written to the config file and read
# back at run time
set config(library) "$ROOTDIR/include"
set config(liblist) [list $config(library)]
set config(spinext) ".spin"
set config(lastdir) [pwd]
set config(font) "TkFixedFont"
set config(botfont) "courier 10"
set config(sash) ""
set config(tabwidth) 4
set config(autoreload) 0
set COMPORT " "
set OPT "-O1"
set COMPRESS "-z0"
set WARNFLAGS "-Wnone"
set FIXEDREAL "--floatreal"
set DEBUG_OPT "-gnone"
set PROP_VERSION ""
set OPENFILES ""
set config(showlinenumbers) 1
set config(savesession) 1
set config(syntaxhighlight) 1
set config(autoindent) 1
#
# filenames($w) gives the file name in window $w, for all of the various tabs
# filetimes($w) gives the last modified time for that file
#
proc getWindowFile { w } {
global filenames
while { "$w" != "" } {
if { [info exists filenames($w)] } {
#puts "return $filenames($w)"
return $filenames($w)
}
set w [winfo parent $w]
}
#puts "getWindowFile: $w: no answer"
#puts [parray filenames]
return ""
}
# provide some default settings
proc setShadowP1Defaults {} {
global shadow
global WINPREFIX
global ROOTDIR
global EXE
set shadow(compilecmd) "\"%D/bin/flexspin$EXE\" -D_BAUD=%r -l %O %I \"%S\""
set shadow(runcmd) "$WINPREFIX \"%D/bin/proploader$EXE\" -Dbaudrate=%r %P \"%B\" -r -t -k"
set shadow(flashprogram) "$ROOTDIR/board/P2ES_flashloader.bin"
set shadow(flashcmd) "$WINPREFIX \"%D/bin/proploader$EXE\" -Dbaudrate=%r %P \"%B\" -e -k"
set shadow(baud) 115200
}
proc setShadowP2aDefaults {} {
global shadow
global WINPREFIX
global ROOTDIR
global EXE
set shadow(compilecmd) "\"%D/bin/flexspin$EXE\" -2a -l -D_BAUD=%r %O %I \"%S\""
set shadow(runcmd) "$WINPREFIX \"%D/bin/loadp2$EXE\" %P -b%r \"%B\" \"-9%b\" -k"
set shadow(flashprogram) "$ROOTDIR/board/P2ES_flashloader.bin"
set shadow(flashcmd) "$WINPREFIX \"%D/bin/loadp2$EXE\" %P -b%r \"@0=%F,@8000+%B\" -t -k"
set shadow(baud) 230400
}
proc setShadowP2bDefaults {} {
global shadow
global WINPREFIX
global ROOTDIR
global EXE
set shadow(compilecmd) "\"%D/bin/flexspin$EXE\" -2 -l -D_BAUD=%r %O %I \"%S\""
set shadow(runcmd) "$WINPREFIX \"%D/bin/loadp2$EXE\" %P -b%r \"%B\" \"-9%b\" -k"
set shadow(flashprogram) "$ROOTDIR/board/P2ES_flashloader.bin"
set shadow(flashcmd) "$WINPREFIX \"%D/bin/loadp2$EXE\" %P -b%r \"@0=%F,@8000+%B\" -t -k"
set shadow(baud) 230400
}
proc copyShadowToConfig {} {
global config
global shadow
set config(compilecmd) $shadow(compilecmd)
set config(runcmd) $shadow(runcmd)
set config(flashcmd) $shadow(flashcmd)
set config(flashprogram) $shadow(flashprogram)
set config(baud) $shadow(baud)
checkPropVersion
}
proc checkPropVersion {} {
global config
global PROP_VERSION
if {[string first " -2a " $config(compilecmd)] != -1} {
set PROP_VERSION "P2a"
set otherProp "P1"
} elseif {[string first " -2" $config(compilecmd)] != -1} {
set PROP_VERSION "P2"
set otherProp "P1"
} else {
set PROP_VERSION "P1"
set otherProp "P2"
}
if { [winfo exists .toolbar] } {
.toolbar.compile configure -text "Compile for $PROP_VERSION"
.toolbar.compileRun configure -text "Compile & Run on $PROP_VERSION"
.toolbar.configmsg configure -text " Use Commands>Configure Commands... to switch to $otherProp"
}
}
setShadowP2bDefaults
copyShadowToConfig
#
# see if there's already a tab which contains a file
# if so, return the name of the tab
# otherwise return ""
#
proc getTabFor { fname } {
global filenames
set tablist [.p.nb tabs]
foreach w $tablist {
if { "$filenames($w)" == "$fname" } {
return $w
}
}
return ""
}
#
# set font and tab stops for a window
#
proc setfont { w fnt } {
global config
if { $fnt eq "" } {
return
}
set t1 [font measure $fnt " "]
set t2 [expr $config(tabwidth) * $t1]
set stops $t2
$w configure -font $fnt -tabs $stops
}
#
# set font and tab stops for all notebook tabs
#
proc setnbfonts { fnt } {
global config
set nbtablist [.p.nb tabs]
foreach w $nbtablist {
setfont $w.txt $fnt
}
if {[winfo exists .list]} {
setfont .list.f.txt $fnt
}
}
# configuration settings
proc config_open {} {
global config
global CONFIG_FILE
global OPT
global COMPRESS
global WARNFLAGS
global FIXEDREAL
global DEBUG_OPT
global COMPORT
global OPENFILES
if {[file exists $CONFIG_FILE]} {
set fp [open $CONFIG_FILE r]
} else {
checkPropVersion
return 0
}
# read config values
while {![eof $fp]} {
set data [gets $fp]
switch [lindex $data 0] {
\# {
# ignore the comment
}
geometry {
# restore last position on screen
wm geometry [winfo toplevel .] [lindex $data 1]
}
opt {
# set optimize level
set OPT [lindex $data 1]
}
compress {
# set compression level
set COMPRESS [lindex $data 1]
}
warnflags {
# set warning flags
set WARNFLAGS [lindex $data 1]
}
fixedreal {
# set warning flags
set FIXEDREAL [lindex $data 1]
}
debugopt {
# set warning flags
set DEBUG_OPT [lindex $data 1]
}
comport {
# set optimize level
set COMPORT [lindex $data 1]
# convert old COMPORT entries
if { $COMPORT ne " " && [string index "$COMPORT" 0] ne "-" } {
set COMPORT "-p $COMPORT"
}
}
openfiles {
# record open files
set OPENFILES [lindex $data 1]
}
default {
set config([lindex $data 0]) [lindex $data 1]
}
}
}
close $fp
checkPropVersion
# some sanity checks
if { "$config(font)" eq "" } {
set config(font) "TkFixedFont"
}
return 1
}
proc config_save {} {
global config
global CONFIG_FILE
global OPT
global COMPRESS
global WARNFLAGS
global FIXEDREAL
global DEBUG_OPT
global COMPORT
global OPENFILES
updateLibraryList
updateOpenFiles
set config(sash) [.p sash coord 0]
set fp [open $CONFIG_FILE w]
puts $fp "# flexprop config info"
puts $fp "geometry\t[winfo geometry [winfo toplevel .]]"
puts $fp "opt\t\{$OPT\}"
puts $fp "compress\t\{$COMPRESS\}"
puts $fp "comport\t\{$COMPORT\}"
puts $fp "openfiles\t\{$OPENFILES\}"
puts $fp "warnflags\t\{$WARNFLAGS\}"
puts $fp "fixedreal\t\{$FIXEDREAL\}"
puts $fp "debugopt\t\{$DEBUG_OPT\}"
foreach i [array names config] {
if {$i != ""} {
puts $fp "$i\t\{$config($i)\}"
}
}
close $fp
}
#
# read a file and return its text
# does UCS-16 to UTF-8 conversion
#
proc uread {name} {
set encoding ""
set len [file size $name]
set f [open $name r]
gets $f line
if {[regexp \xFE\xFF $line] || [regexp \xFF\xFE $line]} {
fconfigure $f -encoding unicode
set encoding unicode
} else {
fconfigure $f -encoding utf-8
}
seek $f 0 start ;# rewind
set text [read $f $len]
close $f
if {$encoding=="unicode"} {
regsub -all "\uFEFF|\uFFFE" $text "" text
}
return $text
}
# exit the program
proc exitProgram { } {
catch {
checkAllChanges
config_save
} errMsg
exit
}
# set list of open files
proc updateOpenFiles { } {
global OPENFILES
global filenames
set t [.p.nb tabs]
set i 0
set w [lindex $t $i]
set OPENFILES [list]
while { $w ne "" } {
set s $filenames($w)
lappend OPENFILES $s
set i [expr "$i + 1"]
set w [lindex $t $i]
}
}
# close tab
proc closeTab { } {
global filenames
set w [.p.nb select]
if { $w ne "" } {
checkChanges $w
set filenames($w) ""
.p.nb forget $w
destroy $w
}
}
# load a file into a text (or ctext) window
proc loadFileToWindow { fname win } {
global filetimes
set file_data [uread $fname]
$win delete 1.0 end
$win insert end $file_data
$win edit modified false
$win mark set insert 1.0
set filetimes($fname) [file mtime $fname]
setHighlightingForFile $win $fname
focus $win
}
# save contents of a window to a file
proc saveFileFromWindow { fname win } {
global filetimes
global config
# check for other programs changing the file
if { [file exists $fname] } {
set disktime [file mtime $fname]
if { $disktime > $filetimes($fname) } {
set answer $config(autoreload)
if { ! $answer } {
set answer [tk_messageBox -icon question -type yesno -message "File $fname has changed on disk; overwrite it?" -default no]
}
if { ! $answer } {
return
}
}
}
set fp [open $fname w]
set file_data [$win get 1.0 end]
# encode as UTF-8
fconfigure $fp -encoding utf-8
# HACK: the text widget inserts an extra \n at end of file
set file_data [string trimright $file_data]
set len [string len $file_data]
#puts " writing $len bytes"
# we trimmed away all the \n above, so put one back here
# by leaving off the -nonewline to puts
puts $fp $file_data
close $fp
set filetimes($fname) [file mtime $fname]
$win edit modified false
}
#
# tag text containing "error:" in a text widget w
#
proc tagerrors { w } {
$w tag remove errtxt 0.0 end
$w tag remove warntxt 0.0 end
$w tag remove hyperlink 0.0 end
$w tag configure errtxt -foreground red
$w tag configure warntxt -foreground orange
$w tag configure hyperlink -foreground blue -underline true
# set current position at beginning of file
set cur 1.0
# search through looking for error:
while 1 {
set cur [$w search -count length "error:" $cur end]
if {$cur eq ""} {break}
$w tag add errtxt $cur "$cur lineend"
$w tag add hyperlink "$cur linestart" "$cur - 2 chars"
set cur [$w index "$cur + $length char"]
}
# set current position at beginning of file
set cur 1.0
# search through looking for warning:
while 1 {
set cur [$w search -count length "warning:" $cur end]
if {$cur eq ""} {break}
$w tag add warntxt $cur "$cur lineend"
$w tag add hyperlink "$cur linestart" "$cur - 2 chars"
set cur [$w index "$cur + $length char"]
}
}
set SpinTypes {
{{FlexSpin files} {.bas .bi .c .cc .cpp .h .spin2 .spin .spinh} }
{{Interpreter files} {.py .lsp .fth} }
{{C/C++ files} {.c .cpp .cxx .cc .h .hh .hpp} }
{{All files} *}
}
set BinTypes {
{{Binary files} {.binary .bin .bin2} }
{{All files} *}
}
#
# see if anything has changed in window w
#
proc checkChanges {w} {
global filenames
set s $filenames($w)
if { $s eq "" } {
return
}
if {[$w.txt edit modified]==1} {
set answer [tk_messageBox -icon question -type yesno -message "Save file $s?" -default yes]
if { $answer eq "yes" } {
saveFile $w
}
}
}
# check all windows for changes
proc checkAllChanges {} {
set t [.p.nb tabs]
set i 0
set w [lindex $t $i]
while { $w ne "" } {
checkChanges $w
set i [expr "$i + 1"]
set w [lindex $t $i]
}
}
# clear all search tags
proc clearAllSearchTags {} {
set t [.p.nb tabs]
set i 0
set w [lindex $t $i]
while { $w ne "" } {
set t $w.txt
foreach {from to} [$t tag ranges hilite] {
$t tag remove hilite $from $to
}
set i [expr "$i + 1"]
set w [lindex $t $i]
}
}
# choose the library directory
proc getLibrary {} {
global config
if { [winfo exists .pb] } {
raise .pb
} else {
do_pb_create
wm title .pb "Library Paths"
}
}
# proc retrieve the library list
proc updateLibraryList {} {
global config
if { [winfo exists .pb] } {
set config(liblist) [.pb.pathbox get 0 end]
}
}
set TABCOUNTER 0
proc newTabName {} {
global TABCOUNTER
set s "f$TABCOUNTER"
set TABCOUNTER [expr "$TABCOUNTER + 1"]
return ".p.nb.$s"
}
proc createNewTab {} {
global filenames
global config
global tabEnterScript
global tabLeaveScript
set w [newTabName]
#.p.bot.txt delete 1.0 end
set filenames($w) ""
setupFramedText $w
setHighlightingForFile $w.txt ""
setfont $w.txt $config(font)
.p.nb add $w
.p.nb tab $w -text "New File"
.p.nb select $w
bind $w <Enter> $tabLeaveScript
bind $w <Leave> $tabEnterScript
return $w
}
#
# set up a framed text window
#
proc setupFramedText {w} {
global config
global CTRL_PREFIX
frame $w
set yscmd "$w.v set"
set xscmd "$w.h set"
set yvcmd "$w.txt yview"
set xvcmd "$w.txt xview"
set searchcmd "searchrep $w.txt 0"
set replacecmd "searchrep $w.txt 1"
ctext $w.txt -wrap none -yscrollcommand $yscmd -xscroll $xscmd -tabstyle wordprocessor -linemap $config(showlinenumbers) -undo 1
scrollbar $w.v -orient vertical -command $yvcmd
scrollbar $w.h -orient horizontal -command $xvcmd
grid $w.txt $w.v -sticky nsew
grid $w.h -sticky nsew
grid rowconfigure $w $w.txt -weight 1
grid columnconfigure $w $w.txt -weight 1
bind $w.txt <$CTRL_PREFIX-f> $searchcmd
bind $w.txt <$CTRL_PREFIX-k> $replacecmd
bind $w.txt <Return> {do_indent %W; break}
# for some reason on my linux system the selection doesn't show
# up correctly
if { [tk windowingsystem] == "x11" } {
$w.txt configure -selectbackground blue -selectforeground white
}
}
#
# load a file into a toplevel window .list
#
proc loadListingFile {filename} {
global config
set viewpos 0
if {[winfo exists .list]} {
raise .list
set viewpos [.list.f.txt yview]
set viewpos [lindex $viewpos 0]
} else {
toplevel .list
setupFramedText .list.f
grid columnconfigure .list 0 -weight 1
grid rowconfigure .list 0 -weight 1
grid .list.f -sticky nsew
}
setfont .list.f.txt $config(font)
loadFileToWindow $filename .list.f.txt
.list.f.txt yview moveto $viewpos
wm title .list [file tail $filename]
}
#
# load a file into a toplevel window .help
#
proc loadHelpFile {filename title} {
global config
set viewpos 0
if {[winfo exists .help]} {
raise .help
set viewpos [.help.f.txt yview]
set viewpos [lindex $viewpos 0]
.help.f.txt configure -state enabled
} else {
toplevel .help
setupFramedText .help.f
grid columnconfigure .help 0 -weight 1
grid rowconfigure .help 0 -weight 1
grid .help.f -sticky nsew
}
setfont .help.f.txt $config(font)
.help.f.txt configure -wrap word
loadFileToWindow $filename .help.f.txt
.help.f.txt yview moveto $viewpos
wm title .help [file tail $filename]
.help.f.txt configure -state disabled
.help.f.txt configure -linemap 0
}
#
# load a file into a tab
# the tab name is w
# its title is title
# if title is "" then set the title based on the file name
#
proc loadFileToTab {w filename title} {
global config
global filenames
global filetimes
if {$title eq ""} {
set title [file tail $filename]
}
if {[winfo exists $w]} {
.p.nb select $w
} else {
setupFramedText $w
.p.nb add $w -text "$title"
setHighlightingForFile $w.txt $filename
}
setfont $w.txt $config(font)
loadFileToWindow $filename $w.txt
$w.txt highlight 1.0 end
ctext::comments $w.txt
ctext::linemapUpdate $w.txt
.p.nb tab $w -text $title
.p.nb select $w
set filenames($w) $filename
set filetimes($filename) [file mtime $filename]
}
proc findFileOnPath { filename startdir } {
global config
# normalize file name
if { [file pathtype $filename] != "absolute" } {
# first check startdir
set s [file join $startdir $filename]
if { [file exists $s] } {
return [file normalize $s]
}
# look for the file name down the include path
foreach d $config(liblist) {
set s [file join $d $filename]
if { [file exists $s] } {
set filename [file normalize $s]
break
}
}
}
return $filename
}
proc loadSourceFile { filename } {
global filenames
# sanity check
if { ![file exists $filename] } {
tk_messageBox -icon error -type ok -message "$filename\nis not found"
return
}
# fetch
set w [getTabFor $filename]
if { $w ne "" } {
.p.nb select $w
} else {
set w [.p.nb select]
if { $w eq "" } {
set w [createNewTab]
} elseif { $filenames($w) ne "" } {
set w [createNewTab]
} else {
# check for any data in the window
if { [$w.txt edit modified]==1 } {
set w [createNewTab]
}
}
checkChanges $w
loadFileToTab $w $filename ""
}
return $w
}
proc doOpenFile {} {
global config
global SpinTypes
global BINFILE
set filename [tk_getOpenFile -filetypes $SpinTypes -defaultextension $config(spinext) -initialdir $config(lastdir) -title "Open File" ]
if { [string length $filename] == 0 } {
return ""
}
set config(lastdir) [file dirname $filename]
set config(spinext) [file extension $filename]
set BINFILE ""
return [loadSourceFile $filename]
}
proc openLastFiles {} {
global OPENFILES
set i 0
set t $OPENFILES
set w [lindex $t $i]
while { $w ne "" } {
loadSourceFile [file normalize $w]
set i [expr "$i + 1"]
set w [lindex $t $i]
}
}
proc pickFlashProgram {} {
global config
global BinTypes
global ROOTDIR
set filename [tk_getOpenFile -filetypes $BinTypes -initialdir $ROOTDIR/board -title "Select Flash Program"]
if { [string length $filename] == 0 } {
return
}
set config(flashprogram) $filename
}
# maybe save the current file; used for compilation
# if no changes, then do not save
proc saveFilesForCompile {} {
global filenames
global filetimes
global config
set t [.p.nb tabs]
set i 0
set w [lindex $t $i]
while { $w ne "" } {
set s $filenames($w)
set needWrite "no"
set needRead "no"
if { $s eq "" } {
# need to ask the user for the file name
saveFile $w
} else {
if {[$w.txt edit modified]==1} {
set needWrite "yes"
}
if { [file exists $s] } {
set disktime [file mtime $s]
if {$disktime > $filetimes($s)} {
set needRead "yes"
}
} else {
set needWrite "yes"
}
if { $needWrite eq "yes" } {
saveFileFromWindow $s $w.txt
}
if { $needRead eq "yes" } {
set answer $config(autoreload)
if { ! $answer } {
set answer [tk_messageBox -icon question -type yesno -message "File $s has changed on disk. Reload it?" -default yes]
}
if { $answer } {
loadFileToWindow $s $w.txt
set needRead "no"
set needWrite "no"
} else {
set needWrite "no"
}
}
}
set i [expr "$i + 1"]
set w [lindex $t $i]
}
}
# save the current file
# returns the file name
proc saveCurFile {} {
set w [.p.nb select]
return [saveFile $w]
}
# save the file belonging to window w
# returns the file name or "" if aborted
proc saveFile {w} {
global filenames
global filetimes
global BINFILE
global SpinTypes
global config
if { [string length $filenames($w)] == 0 } {
set filename [tk_getSaveFile -initialfile $filenames($w) -filetypes $SpinTypes -defaultextension $config(spinext) -title "Save File" ]
if { [string length $filename] == 0 } {
return ""
}
set config(lastdir) [file dirname $filename]
set config(spinext) [file extension $filename]
set filenames($w) $filename
.p.nb tab $w -text [file root $filename]
set BINFILE ""
} else {
set filename $filenames($w)
}
saveFileFromWindow $filename $w.txt
return $filename
}
proc saveFileAs {w} {
global filenames
global BINFILE
global SpinTypes
global config
if { [string length $filenames($w)] == 0 } {
set initdir $config(lastdir)
set initfilename ""
} else {
set initdir [file dirname $filenames($w)]
set initfilename [file tail $filenames($w)]
}
set filename [tk_getSaveFile -filetypes $SpinTypes -defaultextension $config(spinext) -initialdir $initdir -initialfile $initfilename -title "Save As" ]
if { [string length $filename] == 0 } {
return
}
set config(lastdir) [file dirname $filename]
set config(spinext) [file extension $filename]
set BINFILE ""
set filenames($w) $filename
.p.nb tab $w -text [file tail $filename]
return [saveFile $w]
}
# get a loadp2 script to send a particular file
proc scriptSendCurFile {} {
set fname [saveCurFile]
# if { $fname == "" } {
# return ""
# }
return "-epausems(1500)textfile($fname)"
}
# show the about message
proc doAbout {} {
global aboutMsg
tk_messageBox -icon info -type ok -message "FlexProp" -detail $aboutMsg
}
proc doHelp { file title } {
global ROOTDIR
loadHelpFile $file $title
}
proc doSpecial {name extraargs} {
global ROOTDIR
global BINFILE
global PROP_VERSION
if { $PROP_VERSION eq "P1" } {
tk_messageBox -icon error -type ok -message "Hardware not supported" -detail "Special features only work on P2 boards"
return 0
}
if { [string equal -length 1 "$name" "-"] } {
set BINFILE $name
} else {
set BINFILE "$ROOTDIR/$name"
}
.p.bot.txt delete 1.0 end
doJustRun "$extraargs"
return 1
}
#
# click on an error message and find the corresponding line
# parameter is text coordinates like 2.72
#
proc doClickOnError { w coord } {
global filenames
set first "$coord linestart"
set last "$coord lineend"
set linkptr [$w tag prevrange hyperlink $coord]
set link1 [lindex $linkptr 0]
set link2 [lindex $linkptr 1]
set linedata [$w get $link1 $link2]
set colonptr [string last ":" $linedata]
#puts "doClickOnError $w $coord"
#puts "first=|$first|"
#puts "linkptr=|$linkptr|"
#puts "linedata=|$linedata|"
#puts "colonptr=|$colonptr|"
if { $colonptr == -1 } {
set fname "$linedata"
set line ""
} else {
set fname [string range $linedata 0 [expr $colonptr - 1]]
set line [string range $linedata [expr $colonptr + 1] end]
}
if { $fname != "" } {
set startdir $filenames([.p.nb select])
if { $startdir != "" } {
set startdir [file dirname $startdir]
} else {
set startdir [file normalize "."]
}
set fname [findFileOnPath $fname $startdir]
set w [loadSourceFile $fname ]
if { $w == "" } {
return
}
set t $w.txt
$t tag config hilite -background yellow
# remove hilight
foreach {from to} [$t tag ranges hilite] {
$t tag remove hilite $from $to
}
$t tag config hilite -background yellow
if { $line != "" } {