2 # show your vessels on a map
4 # This is part of ypp-sc-tools, a set of third-party tools for assisting
5 # players of Yohoho Puzzle Pirates.
7 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
23 # are used without permission. This program is not endorsed or
24 # sponsored by Three Rings.
32 #---------- general utilities ----------
37 if {$debug} { puts "DEBUG $m" }
41 puts stderr "where-vessels: bad usage: $m"
50 #---------- expecting certain errors ----------
52 proc errexpect-setline {lno line} {
53 glset errexpect_lno $lno
54 glset errexpect_line $line
57 proc errexpect-error {m} {
58 global errexpect_line errexpect_lno
59 error $m "$errexpect_line\n" [list YARRG-ERREXPECT $errexpect_lno]
62 proc errexpect-arrayget {arrayvar key} {
64 upvar 1 ${arrayvar}($key) v
65 if {[info exists v]} { return $v }
66 errexpect-error "undefined $key"
69 proc errexpect-arrayget-boolean {arrayvar key} {
70 switch -exact [uplevel 1 [list errexpect-arrayget $arrayvar $key]] {
73 default { errexpect-error "unexpected $key" }
77 proc errexpect-catch {code} {
78 global errorInfo errorCode
82 debug "ERREXPECT CATCH |$rc|$rv|$errorCode|$errorInfo|"
83 if {$rc==1 && ![string compare YARRG-ERREXPECT [lindex $errorCode 0]]} {
84 return [list 1 $rv [lindex $errorCode 1] $errorInfo]
88 return -code $rc -errorinfo $errorInfo -errorcode $errorCode $rv
92 #---------- argument parsing ----------
96 if {$ai >= [llength $argv]} {
97 badusage "option [lindex $argv [expr {$ai-1}]] needs a value"
99 set v [lindex $argv $ai]
104 set notes_loc vessel-notes
105 set scraper {./yppedia-ocean-scraper --chart}
106 set info_cache _vessel-info-cache
107 set info_source rsync.yarrg.chiark.net::yarrg/vessel-info
114 while {[regexp {^\-} [set arg [lindex $argv $ai]]]} {
116 switch -exact -- $arg {
118 --pirate { glset pirate [string totitle [nextarg]] }
119 --ocean { glset ocean [string totitle [nextarg]] }
120 --clipboard-file { load-clipboard-file [nextarg] }
121 --local-html-dir { lappend scraper --local-html-dir=[nextarg] }
122 --notes { glset notes_loc [nextarg] }
123 --vessel-info-source { glset info_source [nextarg] }
124 --debug { incr debug }
125 default { badusage "unknown option $arg" }
128 set argv [lrange $argv $ai end]
129 if {[llength $argv]} { badusage "non-option args not allowed" }
132 proc argdefaults {} {
133 global ocean notes_loc pirate scraper
134 if {![info exists ocean] || ![info exists pirate]} {
135 set cmd {./yarrg --find-window-only --quiet}
136 if {[info exists ocean]} { lappend cmd --ocean $ocean }
137 if {[info exists pirate]} { lappend cmd --pirate $pirate }
138 manyset [split [eval exec $cmd] " "] ocean pirate
139 if {![llength $ocean] || ![llength $pirate]} {
140 error "$ocean $pirate ?"
143 lappend scraper $ocean
147 #---------- loading and parsing the vessel notes ----------
150 global notes_loc notes_data
151 if {[regexp {^\w+\:} $notes_loc]} {
153 debug "FETCHING NOTES $notes_loc"
154 set req [::http::geturl $notes_loc]
155 switch -glob [::http::status $req].[::http::ncode $req] {
157 ok.* { error "retrieving vessel-notes: [::http::code $req]" }
158 * { error "Retrieving vessel-notes: [::http::error $req]" }
160 set newdata [::http::data $req]
163 debug "READING NOTES $notes_loc"
164 set vn [open $notes_loc]
165 set newdata [read $vn]
168 set notes_data $newdata
171 proc parse-notes {} {
172 global notes_data notes
173 catch { unset notes }
176 foreach l [split $notes_data "\n"] {
178 errexpect-setline $lno $l
179 set l [string trim $l]
180 if {![string length $l]} continue
181 if {[regexp {^\#} $l]} continue
182 if {![regexp -expanded \
183 {^ (\d+) (?: \s+([^=]*?) )? \s* =
185 (?: \s+ (\S+) )?)? $} \
186 $l dummy vid vname owner note]} {
187 errexpect-error "badly formatted"
189 set vname [string trim $vname]
190 if {[info exists notes($vid)]} {
191 errexpect-error "duplicate vesselid $vid"
193 set notes($vid) [list $lno $vname $owner $note]
197 proc note-info {lno vid name island description} {
199 lappend note_infos [list $lno $vid $name $island $description]
202 proc display-note-infos {} {
203 global note_infos note_missings notes
205 set nmissing [llength $note_missings]
206 debug "display-note-infos $nmissing [array size notes]"
208 if {[llength $note_infos]} {
209 set tiny "[llength $note_infos] warning(s)"
210 } elseif {$nmissing && [array size notes]} {
211 set tiny "$nmissing missing"
218 foreach info $note_infos {
219 manyset $info lno vid name island description
220 append infodata "vessel"
221 append infodata " $vid"
222 if {[string length $name]} { append infodata " $name" }
223 if {[string length $island]} { append infodata " ($island)" }
224 append infodata ": " $description "\n"
228 if {[string length $infodata]} { append infodata "\n" }
229 append infodata "$nmissing vessel(s) not mentioned in notes:\n"
231 foreach info [lsort $note_missings] {
232 manyset $info island name vid
233 if {[string compare $island $last_island]} {
234 append infodata "# $island:\n"
235 set last_island $island
237 append infodata [format "%-9d %-29s =\n" $vid $name]
241 parser-control-failed-core .cp.ctrl.notes notes \
244 "[llength $note_infos] warning(s);\
245 $nmissing vessel(s) missing" \
246 "Full description of warnings and missing vessels:" \
250 #---------- vessel properties ----------
252 proc info-cache-update {} {
253 global info_source info_cache
254 file mkdir $info_cache
255 exec sh -c "cp -u icons/* $info_cache/."
257 if {[string length $info_source]} {
260 --exclude=*~ --exclude=*.bak --exclude=.* --exclude=*.tmp \
261 $info_source/ $info_cache 2>@ stderr]
262 debug "INFO-CACHE $cmdl"
266 set f [open $info_cache/vessel-info]
267 glset vessel_class_data [read $f]
271 proc vesselclasses-init {} {
272 global vc_game2code vc_code2abbrev vc_code2full vc_codes
274 global vessel_class_data
275 manyset $vessel_class_data classinfos subclassinfos
278 foreach {game code abbrev full} $classinfos {
279 if {![regexp {^[a-z][a-z]$} $code code]} { error "bad code" }
280 if {![regexp {^[a-z][a-z]$} $abbrev abbrev]} { error "bad abbrev" }
281 lappend vc_codes $code
282 set vc_game2code($game) $code
283 set vc_code2abbrev($code) $abbrev
284 set vc_code2full($code) $full
288 global vsc_code2report
290 set vsc_game2code(null) {}
291 set vsc_code2report() Ordinary
292 set vsc_code2report(!) "(Special/L.E.)"
293 foreach {game code full} $subclassinfos {
294 if {![regexp {^[A-Z]$} $code code]} { error "bad code" }
295 set vsc_game2code($game) $code
296 set vsc_code2report($code) $full
300 set owners {ours dot query}
301 foreach b $owners { load-icon $b }
302 foreach a {battle borrow dot} {
304 foreach b $owners { load-icon-combine $a $b }
308 proc load-icon {icon} {
310 image create bitmap icon/$icon -file $info_cache/$icon.xbm
313 proc load-icon-combine {args} {
316 set delim "pnmcat -lr "
318 append cmd $delim " <(xbmtopbm $info_cache/$icon.xbm)"
319 set delim " <(pbmmake -white 1 1)"
321 append cmd " | pbmtoxbm"
322 debug "load-icon-combine $cmd"
323 image create bitmap icon/[join $args +] -data [exec bash -c $cmd]
326 proc code-lockown2icon {lockown} {
327 manyset [split $lockown ""] lock notown
329 [lindex {battle borrow dot} $lock]
330 [lindex {ours dot query {} {} dot} $notown]
332 if {[llength $l]} { return icon/[join $l +] } { return {} }
335 proc canvas-horiz-stack {xvar xoff y bind type args} {
337 upvar 1 canvas canvas
338 set id [eval $canvas create $type [expr {$x+$xoff}] $y $args]
339 set bbox [$canvas bbox $id]
340 # debug "CANVAS-HORIZ-STACK $type $x $xoff $id $bbox [list $args]"
341 set x [lindex $bbox 2]
342 $canvas bind $id <ButtonPress> $bind
346 proc code2canvas {code canvas x yvar qty qtylen bind} {
347 global vc_code2abbrev
350 manyset [split $code _] inport class subclass lockown xabbrev
354 set imy [expr {$y+2}]
356 if {!$inport} { incr qtylen -1 }
357 if {$qtylen<=0} { set qtylen {} }
358 set qty [format "%${qtylen}s" $qty]
360 set qtyid [canvas-horiz-stack stackx 0 $y $bind \
361 text -anchor nw -font fixed -text $qty]
364 canvas-horiz-stack stackx 0 $imy $bind \
365 image -anchor nw -image icon/atsea
369 upvar #0 vc_code2abbrev($class) vcabb
370 if {![info exists vcabb]} {
372 image create bitmap icon/$vcabb -data \
373 [exec pbmtext -builtin fixed $class | pnminvert | pnmcrop >t.pnm]
375 canvas-horiz-stack stackx -1 $imy $bind \
376 image -anchor nw -image icon/$vcabb
378 if {[string length $subclass]} {
379 canvas-horiz-stack stackx 0 $y $bind \
380 text -anchor nw -font fixed -text \
385 set lockownicon [code-lockown2icon $lockown]
386 if {[string length $lockownicon]} {
387 canvas-horiz-stack stackx 0 $imy $bind \
388 image -anchor nw -image $lockownicon
392 if {[string length $xabbrev]} {
393 canvas-horiz-stack stackx 0 $y $bind \
394 text -anchor nw -font fixed -text \
398 set bbox [$canvas bbox $qtyid]
399 set ny [lindex $bbox 3]
400 set bid [$canvas create rectangle \
405 $canvas lower $bid $qtyid
407 $canvas bind $bid <ButtonPress> $bind
410 proc show-report-decode {code} {
413 manyset [split $code _] inport classcode subclass lockown xabbrev
414 manyset [split $lockown ""] lock notown
416 report-set inport [lindex {{At Sea} {In port}} $inport]
417 report-set class $vc_code2full($classcode)
419 global smash_subclass
420 if {$smash_subclass >= 2} {
421 report-set subclass "(Any subclass)"
423 upvar #0 vsc_code2report($subclass) subclass_report
424 info exists subclass_report
426 report-set subclass $subclass_report
428 report-set subclass "Subclass \"$subclass\""
431 report-set lock [lindex {
432 {Battle ready} {Unlocked} {Locked}
433 {(All lock states)} {(Not battle ready)}
436 switch -exact $notown {
437 0 { report-set own "Yours" }
438 1 { report-set own "Other pirate's" }
439 2 { report-set own "Owner unknown" }
440 3 { report-set own "(All ownerships)" }
441 4 - 5 { report-set own "(Yours/unknown)" }
442 default { report-set own "?? $notown" }
445 if {[string length $xabbrev]} {
446 report-set xabbrev "Notes flags: $xabbrev"
448 report-set xabbrev "No flags in notes"
452 #---------- common to smashing and filtering ----------
454 proc make-control {parent ctrl label ekind} {
455 debug "MAKE-CONTROL [list $parent $ctrl $label $ekind]"
456 label $parent.lab_$ctrl -text $label -justify left
458 manyset [grid size $parent] dummy row
460 grid configure $parent.lab_$ctrl -row $row -column 0 -sticky nw -pady 4
461 grid configure $parent.$ctrl -row $row -column 1 -sticky w -pady 3
465 proc begin-control-grid {cw count rows inrow} {
466 if {!$inrow} { set inrow [expr {($count + $rows) / $rows}] }
467 upvar #0 control_grid_properties($cw) props
468 set props [list $rows $inrow]
472 proc make-control-grid-entry {cw kind ix ekind args} {
473 upvar #0 control_grid_properties($cw) props
474 manyset $props rows inrow
477 eval [list $ekind $ew] $args
479 switch -exact $kind {
481 grid configure $ew -sticky sw \
482 -row [expr {$ix / $inrow}] \
483 -column [expr {$ix % $inrow}]
486 grid configure $ew -sticky se \
487 -row [expr {$rows-1}] \
488 -column [expr {$inrow-1}]
497 #---------- smashing ----------
502 proc smash-code {code} {
503 manyset [split $code _] inport class subclass lockown xabbrev
505 global smash_subclass
506 if {$smash_subclass > 1} {
508 } elseif {$smash_subclass && [string length $subclass]} {
513 switch $smash_owner {
515 1 { regsub {[12]$} $lockown 5 lockown }
517 if {[regexp {^0.} $lockown]} {
518 # battle ready / all lock states
520 } elseif {[regexp {^.0} $lockown]} {
521 # not battle ready / yours
524 # state (not battle ready) / not known to be yours
525 regsub {.$} $lockown 4 lockown
528 3 { regsub {.$} $lockown {3} lockown }
532 return [join [list $inport $class $subclass $lockown $xabbrev] _]
535 proc make-smasher {sma label ekind} {
536 return [make-control .smash $sma $label $ekind]
539 proc make-radio-smasher {sma label variable descs rows inrow} {
540 set w [make-smasher $sma $label frame]
541 begin-control-grid $w [llength $descs] $rows $inrow
542 for {set i 0} {$i < [llength $descs]} {incr i} {
543 make-control-grid-entry $w ix $i \
545 -variable $variable -value $i -command redraw-needed \
546 -text [lindex $descs $i]
550 proc make-smashers {} {
551 make-radio-smasher subclass Subclass smash_subclass \
552 {Show Normal/LE Hide} 1 0
554 make-radio-smasher owner Owner smash_owner \
555 {Show Yours? {For you} Lock Hide} 2 3
557 global vc_codes vc_code2abbrev
559 set cw [make-smasher size "Size\nround down" frame]
560 populate-control-grid-tickboxes $cw 2 0 smash_size $vc_codes \
561 image val { return 0 } { return $vc_code2abbrev($val) }
564 #---------- filtering ----------
568 proc filter-values/size {} { global vc_codes; return $vc_codes }
569 proc filter-icon/size {code} {
570 upvar #0 vc_code2abbrev($code) abb
573 proc filter-default/size {code} { return 1 }
574 proc filter-says-yes/size {codel} {
575 set sizecode [lindex $codel 1]
576 upvar #0 filter_size($sizecode) yes
580 proc filter-values/lockown {} {
583 lappend vals "$lv$ov"
588 proc filter-icon/lockown {lockown} { return [code-lockown2icon $lockown] }
589 proc filter-default/lockown {lockown} {
590 return [regexp {^[01]|^2[^1]} $lockown]
592 proc filter-says-yes/lockown {codel} {
593 set lockown [lindex $codel 3]
594 upvar #0 filter_lockown($lockown) yes
595 debug "FILTER-SAYS-YES/LOCKOWN $codel $lockown $yes"
599 proc filter-validate/xabbre {re} {
603 regsub {^.*:\s*} $emsg {} emsg
604 regsub {^.*(.{30})$} $emsg {\1} emsg
609 proc filter-says-yes/xabbre {codel} {
611 set xabbrev [lindex $codel 4]
612 return [regexp -- $filter_xabbre $xabbrev]
615 proc filter-tickbox-flip {fil} {
616 upvar #0 filter_$fil vars
617 set values [filter-values/$fil]
618 foreach val $values {
619 set vars($val) [expr {!$vars($val)}]
624 proc make-tickbox-filter {fil label rows inrow} {
625 upvar #0 filter_$fil vars
626 set values [filter-values/$fil]
627 set count [llength $values]
628 set noicons [catch { info args filter-icon/$fil }]
630 set fw [make-filter $fil $label frame]
631 begin-control-grid $fw $count $rows $inrow
633 for {set ix 0} {$ix < $count} {incr ix} {
634 set val [lindex $values $ix]
635 set vars($val) [filter-default/$fil $val]
636 set ew [make-control-grid-entry $fw ix $ix checkbutton \
637 -variable filter_${fil}($val) \
639 -command [list redraw-needed tickbox-filter $fil $val]]
641 $fw.$ix configure -image [filter-icon/$fil $val] -height 16
643 $fw.$ix configure -text [filter-map/$fil $val]
646 [make-control-grid-entry $fw final invert button] \
648 -text flip -command [list filter-tickbox-flip $fil] \
652 proc entry-filter-changed {fw fil n1 n2 op} {
654 upvar #0 filter_$fil realvar
655 upvar #0 filterentered_$fil entryvar
656 global def_background
657 debug "entry-filter-changed $fw $fil $entryvar"
659 set error [filter-validate/$fil $entryvar]
660 if {[string length $error]} {
661 $fw.error configure -text $error -foreground white -background red
663 $fw.error configure -text { } -background $def_background
664 set realvar $entryvar
668 puts stderr "FILTER CHECK ERROR $emsg $errorInfo"
672 proc make-entry-filter {fil label def} {
673 global filterentered_$fil
674 upvar #0 filter_$fil realvar
676 set fw [make-filter $fil $label frame]
677 entry $fw.entry -textvariable filterentered_$fil
679 glset def_background [$fw.error cget -background]
680 trace add variable filterentered_$fil write \
681 [list entry-filter-changed $fw $fil]
682 pack $fw.entry $fw.error -side top -anchor w
685 proc make-filter {fil label ekind} {
688 return [make-control .filter $fil $label $ekind]
691 proc make-filters {} {
692 make-tickbox-filter size Size 2 0
693 make-tickbox-filter lockown "Lock/\nowner" 2 6
694 make-entry-filter xabbre "Flags\n regexp" {}
697 proc filterstyle-changed {n1 n2 op} {
699 debug "filterstyle-changed $filterstyle"
703 proc filters-say-yes {code} {
704 global filters filterstyle
705 set codel [split $code _]
706 set lockown [lindex $codel 3]
707 switch -exact $filterstyle {
709 1 { return [filter-default/lockown $lockown] }
710 2 { return [regexp {^.0} $lockown] }
712 default { error $filterstyle }
715 foreach fil $filters {
716 if {![filter-says-yes/$fil $codel]} {
717 debug "FILTERS-SAY-YES $code NO $fil"
721 debug "FILTERS-SAY-YES $code YES $filters"
725 #---------- loading and parsing the clipboard (vessel locations) ----------
728 global pirate notes_used note_missings newnotes
732 lappend codel [errexpect-arrayget-boolean vi inPort]
734 set gameclass [errexpect-arrayget vi vesselClass]
735 upvar #0 vc_game2code($gameclass) class
736 if {![info exists class]} {
737 set class "($gameclass)"
738 upvar #0 vc_code2abbrev($class) vcabb
740 set data [exec pbmtext -builtin fixed " $gameclass " \
741 | pnminvert | pnmcrop | pbmtoxbm]
742 debug "INVENTED ICON $vcabb $data"
743 image create bitmap icon/$vcabb -data $data
746 set vc_code2full($class) "Type \"$gameclass\""
750 set gamesubclass [errexpect-arrayget vi vesselSubclass]
751 upvar #0 vsc_game2code($gamesubclass) subclass
752 if {[info exists subclass]} {
753 lappend codel $subclass
755 lappend codel ($gamesubclass)
758 switch -exact [errexpect-arrayget vi isLocked]/[ \
759 errexpect-arrayget vi isBattleReady] {
760 true/false { set lock 2 }
761 false/false { set lock 1 }
762 false/true { set lock 0 }
763 default { errexpect-error "unexpected isLocked/isBattleReady" }
766 set vid [errexpect-arrayget vi vesselId]
767 upvar #0 notes($vid) note
768 set realname [errexpect-arrayget vi vesselName]
769 set island [errexpect-arrayget vi islandName]
773 if {[info exists note]} {
774 manyset $note lno notename owner xabbrev
775 if {[string compare -nocase $realname $notename]} {
776 note-info $lno $vid $realname $island \
777 "notes say name is $notename"
779 if {[string length $owner]} {
780 if {![string compare $owner $pirate]} {
788 append abbrev $xabbrev
789 set notes_used($vid) 1
793 lappend note_missings [list $island $realname $vid]
796 lappend codel "$lock$notown" $xabbrev
797 lappend newnotes [list $vid $realname $owner $xabbrev]
798 set kk "$island [join $codel _]"
799 upvar #0 found($kk) k
800 lappend k [list $vid $realname $owner]
802 debug "CODED $kk $vid $realname"
806 proc parse-clipboard {} {
807 global clipboard found notes notes_used newnotes
809 catch { unset found }
810 catch { unset notes_used }
812 glset note_missings {}
816 set itemre { (\w+) = ([^=]*) }
817 set manyitemre "^\\\[ $itemre ( (?: ,\\ $itemre)* ) \\]\$"
821 foreach l [split $clipboard "\n"] {
823 errexpect-setline $lno $l
824 if {![string length $l]} continue
827 if {![regexp -expanded $manyitemre $l dummy \
828 thiskey thisval rhs]} {
829 errexpect-error "badly formatted"
831 set vi($thiskey) $thisval
832 if {![string length $rhs]} break
833 regsub {^, } $rhs {} rhs
839 if {[llength $newnotes]} {
840 foreach vid [lsort [array names notes]] {
841 if {![info exists notes_used($vid)]} {
842 manyset $notes($vid) lno notename
843 note-info $lno $vid $notename {} \
844 "vessel in notes no longer found"
850 proc load-clipboard-file {fn} {
852 glset clipboard [read $f]
857 #---------- loading and parsing the chart ----------
861 debug "FETCHING CHART"
862 set chart [eval exec $scraper [list | perl -we {
867 yppedia_chart_parse(\*STDIN, (new IO::File ">/dev/null"),
868 sub { sprintf "%d %d", @_; },
869 sub { printf "archlabel %d %d %s\n", @_; },
870 sub { printf "island %s {%s} %s\n", @_; },
871 sub { printf "league %s %s %s.\n", @_; },
872 sub { printf STDERR "warning: %s: incomprehensible: %s", @_; }
874 STDOUT->error and die $!;
883 return [expr {$c * $scale}]
886 proc chart-got/archlabel {args} { }
887 proc chart-got/island {x y isle sizecol} {
888 debug "ISLE $x $y $isle $sizecol"
889 global canvas isleloc
890 set isleloc($isle) [list $x $y]
892 # $canvas create oval \
893 # [expr {[coord $x] - $sz}] [expr {[coord $y] - $sz}] \
894 # [expr {[coord $x] + $sz}] [expr {[coord $y] + $sz}] \
897 if {[string match *_col $sizecol]} { set colour black }
898 $canvas create text [coord $x] [coord $y] \
899 -text $isle -anchor s -fill $colour
901 proc chart-got/league {x1 y1 x2 y2 kind} {
902 # debug "LEAGUE $x1 $y1 $x2 $y2 $kind"
904 set l [$canvas create line \
905 [coord $x1] [coord $y1] \
906 [coord $x2] [coord $y2]]
907 if {![string compare $kind .]} {
908 $canvas itemconfigure $l -dash .
912 proc debug-filter-array {array} {
914 set m " FILTER $array"
915 foreach k [lsort [array names a]] {
916 append m " $k=$a($k)"
921 proc redraw-needed {args} {
923 debug "REDRAW NEEDED $args"
924 if {[info exists redraw_after]} return
927 debug " FILTER style $filterstyle"
928 debug-filter-array filter_size
929 debug-filter-array filter_lockown
931 debug " FILTER xabbre $filter_xabbre"
933 set redraw_after [after 250 draw]
937 global chart found isleloc canvas redraw_after islandnames smfound
939 catch { after cancel $redraw_after }
940 catch { unset redraw_after }
944 foreach l [split $chart "\n"] {
945 # debug "CHART-GOT $l"
946 set proc [lindex $l 0]
947 eval chart-got/$proc [lrange $l 1 end]
950 catch { unset smfound }
951 foreach key [lsort [array names found]] {
952 regexp {^(.*) (\S+)$} $key dummy islandname code
954 if {![filters-say-yes $code]} continue
956 set smcode [smash-code $code]
957 debug "smashed $code => $smcode"
958 set smkey "$islandname $smcode"
959 foreach vessel $found($key) { lappend smfound($smkey) $vessel }
963 set lastislandname {}
964 foreach smkey [lsort [array names smfound]] {
965 set c [llength $smfound($smkey)]
966 regexp {^(.*) (\S+)$} $smkey dummy islandname code
967 debug "SHOWING [list $smkey $c $islandname $code l=$lastislandname]"
969 if {[string compare $lastislandname $islandname]} {
970 manyset $isleloc($islandname) x y
973 set lastislandname $islandname
974 lappend islandnames $islandname
978 if {$c > 1} { set qty [format %d $c] } else { set qty {} }
979 code2canvas $code $canvas $x y $qty 2 \
980 [list show-report $islandname $code]
984 panner::updatecanvas-bbox .cp.ctrl.pan
990 #---------- parser error reporting ----------
992 proc parser-control-create {w base invokebuttontext etl_title} {
994 button $w.do -text $invokebuttontext -command invoke_$base -pady 3
996 frame $w.resframe -width 120 -height 32
997 button $w.resframe.res -text {} -anchor nw \
998 -padx 1 -pady 1 -borderwidth 0 -justify left
999 glset deffont_$base [$w.resframe.res cget -font]
1000 place $w.resframe.res -relx 0.5 -y 0 -anchor n
1002 pack $w.do -side top
1003 pack $w.resframe -side top -expand y -fill both
1008 wm title $eb "where-vessels - $etl_title"
1009 wm protocol $eb WM_DELETE_WINDOW [list wm withdraw $eb]
1011 label $eb.title -text $etl_title
1012 pack $eb.title -side top
1014 button $eb.close -text Close -command [list wm withdraw $eb]
1015 pack $eb.close -side bottom
1017 frame $eb.emsg -bd 2 -relief groove
1018 label $eb.emsg.lab -anchor nw -text "Error:"
1019 text $eb.emsg.text -height 1
1020 pack $eb.emsg.text -side bottom -fill x
1021 pack $eb.emsg.lab -side left
1023 pack $eb.emsg -side top -pady 2 -fill x
1025 frame $eb.text -bd 2 -relief groove
1026 pack $eb.text -side bottom -pady 2 -fill both -expand y
1028 label $eb.text.lab -anchor nw
1030 text $eb.text.text -width 85 \
1031 -xscrollcommand [list $eb.text.xscroll set] \
1032 -yscrollcommand [list $eb.text.yscroll set]
1033 $eb.text.text tag configure error \
1034 -background red -foreground white
1036 scrollbar $eb.text.xscroll -orient horizontal \
1037 -command [list $eb.text.text xview]
1038 scrollbar $eb.text.yscroll -orient vertical \
1039 -command [list $eb.text.text yview]
1041 grid configure $eb.text.lab -row 0 -column 0 -sticky w -columnspan 2
1042 grid configure $eb.text.text -row 1 -column 0 -sticky news
1043 grid configure $eb.text.yscroll -sticky ns -row 1 -column 1
1044 grid configure $eb.text.xscroll -sticky ew -row 2 -column 0
1045 grid rowconfigure $eb.text 0 -weight 0
1046 grid rowconfigure $eb.text 1 -weight 1
1047 grid rowconfigure $eb.text 2 -weight 0
1048 grid columnconfigure $eb.text 0 -weight 1
1049 grid columnconfigure $eb.text 1 -weight 0
1052 proc parser-control-ok-core {w base background show} {
1053 debug "parser-control-ok-core $w $base $background $show"
1054 upvar #0 deffont_$base deffont
1055 $w.resframe.res configure \
1056 -background $background -disabledforeground black -font $deffont \
1057 -state disabled -command {} \
1060 proc parser-control-ok {w base show} {
1061 parser-control-ok-core $w $base green $show
1063 proc parser-control-none {w base show} {
1064 parser-control-ok-core $w $base blue $show
1066 proc parser-control-failed-core {w base foreground background smallfont
1067 tiny summary fulldesc fulldata} {
1068 debug "parser-control-failed-core $w $base $summary $fulldesc"
1069 upvar #0 deffont_$base deffont
1072 $eb.emsg.text delete 0.0 end
1073 $eb.emsg.text insert end $summary
1075 $eb.text.lab configure -text $fulldesc
1076 $eb.text.text delete 0.0 end
1077 $eb.text.text insert end $fulldata
1079 regsub -all {.{18}} $tiny "&\n" ewrap
1087 $w.resframe.res configure \
1088 -background $background -foreground $foreground -font $font \
1089 -state normal -command [list wm deiconify $eb] \
1093 proc parser-control-failed-expected {w base emsg lno ei fulldesc newdata} {
1096 set line [lindex [split $ei "\n"] 0]
1097 debug "parser-control-failed-expected: $w $base: $lno: $emsg\n $line"
1099 parser-control-failed-core $w $base \
1101 "err: [string trim $emsg]: \"$line\"" \
1102 "at line $lno: $emsg" \
1105 $eb.text.text tag add error $lno.0 $lno.end
1106 $eb.text.text see $lno.0
1108 proc parser-control-failed-unexpected {w base emsg ei} {
1110 parser-control-failed-core $w $base \
1112 $emsg $emsg "Details and stack trace:" $ei
1115 proc reparse {base varname old fulldesc okshow noneshow parse ok} {
1116 upvar #0 $varname var
1117 manyset [errexpect-catch {
1119 if {[string length [string trim $var]]} {
1120 parser-control-ok .cp.ctrl.$base $base $okshow
1122 parser-control-none .cp.ctrl.$base $base $noneshow
1124 }] failed emsg lno ei
1126 parser-control-failed-expected .cp.ctrl.$base $base \
1127 $emsg $lno $ei $fulldesc $var
1135 #---------- island names selection etc. ----------
1137 proc islandnames-update {} {
1139 .islands.count configure -text [format "ships at %d island(s)" \
1140 [llength $islandnames]]
1143 proc islandnames-select {} {
1144 .islands.clip configure -relief sunken -state disabled
1145 selection own -command islandnames-deselect .islands.clip
1147 proc islandnames-deselect {} {
1148 .islands.clip configure -relief raised -state normal
1151 proc islandnames-handler {offset maxchars} {
1153 return [string range [join $islandnames ", "] \
1154 $offset [expr {$offset+$maxchars-1}]]
1157 #---------- main user interface ----------
1159 proc widgets-setup {} {
1160 global canvas debug pirate ocean filterstyle
1162 wm geometry . 1200x800
1163 wm title . "where-vessels - $pirate on the $ocean ocean"
1167 frame .f -border 1 -relief groove
1170 pack $canvas -expand 1 -fill both
1171 pack .f -expand 1 -fill both -side left
1173 #----- control panels and filter -----
1176 frame .smash -relief groove -bd 2 -padx 1
1177 frame .filter -relief groove -bd 2 -padx 1
1178 frame .islands -pady 2
1179 pack .cp .filter .islands .smash -side top
1181 label .smash.title -text {Display/combine details}
1182 grid .smash.title -row 0 -column 0 -columnspan 2
1185 trace add variable filterstyle write filterstyle-changed
1188 label .filter.title.title -text Show
1189 pack .filter.title.title -side left
1190 for {set fing 0} {$fing < 4} {incr fing} {
1191 radiobutton .filter.title.f$fing \
1192 -variable filterstyle -value $fing \
1193 -text [lindex {All Useable Mine These:} $fing]
1194 pack .filter.title.f$fing -side left
1197 grid configure .filter.title -row 0 -column 0 -columnspan 2
1199 #----- control panel -----
1202 pack .cp.ctrl -side left -anchor n
1204 debug "BBOX [$canvas bbox all]"
1206 panner::canvas-scroll-bbox .f.c
1207 panner::create .cp.ctrl.pan .f.c 120 120 $debug
1209 pack .cp.ctrl.pan -side top -pady 0 -padx 5
1211 pack .cp.ctrl.zoom -side top
1213 button .cp.ctrl.zoom.out -text - -font {Courier 16} -command {zoom /2} -pady 0
1214 button .cp.ctrl.zoom.in -text + -font {Courier 16} -command {zoom *2} -pady 0
1215 pack .cp.ctrl.zoom.out .cp.ctrl.zoom.in -side left
1217 parser-control-create .cp.ctrl.acquire \
1219 "Clipboard parsing error" \
1221 pack .cp.ctrl.acquire -side top -pady 2
1223 parser-control-create .cp.ctrl.notes \
1224 notes "Reload notes" \
1225 "Vessel notes loading report" \
1227 pack .cp.ctrl.notes -side top -pady 2
1229 #----- island name count and copy -----
1231 label .islands.count
1232 button .islands.clip -text "copy island names" -pady 2 -padx 2 \
1233 -command islandnames-select
1234 selection handle .islands.clip islandnames-handler
1235 pack .islands.count .islands.clip -side left
1237 #----- decoding etc. report -----
1240 pack .cp.report -side left -anchor n
1242 label .cp.report.island -text { }
1244 canvas .cp.report.abbrev -width 1 -height 15
1246 frame .cp.report.code
1247 label .cp.report.code.lab -text Code:
1248 glset report_code { }
1249 entry .cp.report.code.code -state readonly \
1250 -textvariable report_code -width 15
1251 pack .cp.report.code.lab .cp.report.code.code -side left
1252 frame .cp.report.details -bd 2 -relief groove -padx 2 -pady 2
1254 listbox .cp.report.list -height 5
1256 pack .cp.report.island .cp.report.abbrev .cp.report.details \
1257 .cp.report.list -side top
1258 #pack .cp.report.code -side top
1259 pack configure .cp.report.details -fill x
1261 foreach sw {inport class subclass lock own xabbrev} {
1262 label .cp.report.details.$sw -text { }
1263 pack .cp.report.details.$sw -side top -anchor w
1267 proc report-set {sw val} { .cp.report.details.$sw configure -text $val }
1269 proc show-report {islandname code} {
1270 .cp.report.island configure -text $islandname
1272 .cp.report.abbrev delete all
1274 code2canvas $code .cp.report.abbrev 5 y {} 0 {}
1275 manyset [.cp.report.abbrev bbox all] minx dummy maxx dummy
1276 .cp.report.abbrev configure -width [expr {$maxx-$minx+4}]
1278 glset report_code $code
1279 show-report-decode $code
1281 set kk "$islandname $code"
1282 upvar #0 smfound($kk) k
1284 .cp.report.list delete 0 end
1287 manyset $entry vid name owner
1288 lappend owned($owner) $name
1291 foreach owner [lsort [array names owned]] {
1292 if {[string length $owner]} {
1293 set owndesc "$owner's"
1295 set owndesc "Owner unknown"
1297 .cp.report.list insert end "$owndesc:"
1298 foreach name $owned($owner) {
1299 .cp.report.list insert end " $name"
1304 proc zoom {extail} {
1306 set nscale [expr "\$scale $extail"]
1307 debug "ZOOM $scale $nscale"
1308 if {$nscale < 1 || $nscale > 200} return
1313 proc invoke_acquire {} {
1314 global clipboard errorInfo
1318 set clipboard [clipboard get]
1320 parser-control-failed-unexpected .cp.ctrl.acquire acquire \
1321 $emsg "fetching clipboard:\n\n$errorInfo"
1326 clipboard $old "Clipboard contents:" { acquired ok } { no vessels } {
1334 proc invoke_notes {} {
1335 global notes_data errorInfo notes_loc
1341 parser-control-failed-unexpected .cp.ctrl.notes notes \
1342 $emsg "loading $notes_loc:\n\n$errorInfo"
1347 notes_data $old "Vessel notes:" "loaded ok" { no notes } {
1356 #---------- main program ----------
1360 httpclientsetup where-vessels
1369 if {[catch { parse-clipboard } emsg]} {
1370 puts stderr "$emsg\n$errorInfo"
1373 after idle invoke_notes
1378 package require Tclx
1379 commandloop -async \
1380 -prompt1 { return "where-vessels% " } \
1381 -prompt2 { return "> " }
1384 # rsync -r --exclude=\*~ yarrg/icons/. ijackson@chiark.greenend.org.uk:/home/ftp/users/ijackson/yarrg/vessel-info/.