3 # --- Configuration ---
5 if {[info exists env(HOME)]} {
8 set bin [info nameofexecutable]
9 if {[string compare $bin ""] == 0} {
12 set home [file dirname $bin]
15 set conffile [list "/etc/anagrc" \
16 [file join $home "anagrc"]]
17 if {[string compare "unix" $tcl_platform(platform)] == 0} {
18 lappend conffile [file join $home ".anagrc"]
20 set C_tags {anag wordlist}
22 set C(wordlist) "@DICTIONARY@"
25 if {![info exists C($i)]} {
26 error "internal error: unset configuration option `$i'"
31 if {[catch { set fh [open $f] } err]} { continue }
32 while {[gets $fh line] >= 0} {
33 if {[regexp {^[[:space:]]*(\#|$)} $line]} continue
34 regexp {^\s*([[:alnum:]]\w*)\s*=?\s*(|.*\S)\s*$} $line - n v
41 # --- Other setting up ---
43 if {[string compare "windows" $tcl_platform(platform)] == 0} {
45 {Executables {.exe} {}}
52 # --- Handy subroutines ---
54 proc wordlist {args} {
57 while {![regexp {^\s*$} $s]} {
58 regexp {^\s*(\S+)(.*)$} $s - w s
66 tk_messageBox -type ok -icon error \
67 -title "Error from [wm title .]" -message $msg
72 proc conf-copyout {} {
89 global C C_tags tcl_platform home conffile
91 if {[winfo exists .opt]} {
97 wm title .opt "[wm title .] options"
101 label .opt.anag.l -text "Anagram solver binary: "
102 entry .opt.anag.e -textvariable C:anag
103 button .opt.anag.b -text "..." -command {
104 set C:anag [tk_getOpenFile -parent .opt \
105 -title "Anagram solver binary" -filetypes $exetypes \
106 -initialdir [file dirname ${C:anag}]]
108 pack .opt.anag.l -side left -padx 2 -pady 2
109 pack .opt.anag.e -side left -expand yes -fill x -padx 2 -pady 2
110 pack .opt.anag.b -side left -padx 2 -pady 2
113 label .opt.wordlist.l -text "Wordlist file: "
114 entry .opt.wordlist.e -textvariable C:wordlist
115 button .opt.wordlist.b -text "..." -command {
116 set C:wordlist [tk_getOpenFile -parent .opt \
117 -title "Wordlist file" \
118 -initialdir [file dirname ${C:wordlist}]]
120 pack .opt.wordlist.l -side left -padx 2 -pady 2
121 pack .opt.wordlist.e -side left -expand yes -fill x -padx 2 -pady 2
122 pack .opt.wordlist.b -side left -padx 2 -pady 2
125 button .opt.b.cancel -text "Cancel" -command { destroy .opt }
126 button .opt.b.ok -text "OK" -command { conf-copyin; destroy .opt }
127 button .opt.b.save -default active -text "Save" -command {
128 set tf [lindex $conffile end]
130 set date [clock format [clock seconds] -format "%Y-%m-%s %H:%M:%S"]
131 set fh [open "$tf.new" w]
132 puts $fh "# Anagram settings, written $date"
139 file copy -force -- $tf "$tf.old"
140 file rename -force -- "$tf.new" $tf
142 catch { close $fh; file delete -- "$tf.new" }
149 pack .opt.b.cancel .opt.b.ok .opt.b.save -side left -padx 2 -pady 2
151 bind .opt <Return> { tkButtonInvoke .opt.b.save }
152 bind .opt <Escape> { tkButtonInvoke .opt.b.cancel }
154 pack .opt.anag .opt.wordlist -expand yes -fill x
155 pack .opt.b -anchor e
158 # --- Run the command ---
160 proc run-search {args} { run-search-v $args }
161 proc run-search-v {v} {
163 set v [linsert $v 0 | $C(anag) "--file" $C(wordlist)]
164 if {[catch {set fh [open $v]} err]} { report $err; return }
166 while {[gets $fh line] >= 0} { lappend l $line }
167 if {[catch {close $fh} err]} { report $err; return }
169 foreach i $l { .list insert end $i }
172 # --- Construct the main window ---
174 wm title . "Anagram solver"
179 foreach {opt text mnem} {
183 trackword Trackword t
184 mono Monoalphabetic m
185 regexp "Regular expression" r
188 button .b-$opt -text $text \
189 -underline [string first $mnem [string tolower $text]] \
190 -command [concat [list run-search -$opt] \$word]
191 bind . <Alt-$mnem> [list tkButtonInvoke .b-$opt]
192 pack .b-$opt -in .f-buttons -fill x -padx 2 -pady 2
195 button .b-custom -text "Custom" -underline 0 \
196 -command { run-search-v [wordlist $word] }
197 bind . <Alt-c> { tkButtonInvoke .b-custom }
198 pack .b-custom -in .f-buttons -fill x -padx 2 -pady 2
201 -xscrollcommand { .f-list.xscroll set } \
202 -yscrollcommand { .f-list.yscroll set }
203 scrollbar .f-list.xscroll -orient horizontal -command { .list xview }
204 scrollbar .f-list.yscroll -orient vertical -command { .list yview }
206 entry .e-word -textvariable word
208 grid .list -in .f-list -row 0 -column 0 -sticky nsew
209 grid .f-list.xscroll -row 1 -column 0 -sticky ew
210 grid .f-list.yscroll -row 0 -column 1 -sticky ns
211 grid rowconfigure .f-list 0 -weight 1
212 grid columnconfigure .f-list 0 -weight 1
214 pack .e-word -in .f-entry -expand yes -fill x -padx 2 -pady 2
216 pack .f-entry -fill x
217 pack .f-list -side left -expand yes -fill both
218 pack .f-buttons -side left -anchor s
221 .menu add cascade -label "File" -underline 0 -menu .menu.file
223 .menu.file add command -label "Options..." -underline 0 -command { options }
224 .menu.file add command -label "Quit" -underline 0 -command { destroy . }
225 . configure -menu .menu
228 bind .e-word <Return> { tkButtonInvoke .b-anagram }