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 button .b-anagram -text "Anagram" -underline 0 \
180 -command { run-search "-anagram" $word }
181 button .b-subgram -text "Subgram" -underline 0 \
182 -command { run-search "-subgram" $word }
183 button .b-glob -text "Crossword" -underline 5 \
184 -command { run-search "-wildcard" $word }
185 button .b-track -text "Trackword" -underline 0 \
186 -command { run-search "-trackword" $word }
187 button .b-regexp -text "Regexp" -underline 0 \
188 -command { run-search "-regexp" $word }
190 button .b-custom -text "Custom" -underline 0 \
191 -command { run-search-v [wordlist $word] }
194 -xscrollcommand { .f-list.xscroll set } \
195 -yscrollcommand { .f-list.yscroll set }
196 scrollbar .f-list.xscroll -orient horizontal -command { .list xview }
197 scrollbar .f-list.yscroll -orient vertical -command { .list yview }
199 entry .e-word -textvariable word
201 grid .list -in .f-list -row 0 -column 0 -sticky nsew
202 grid .f-list.xscroll -row 1 -column 0 -sticky ew
203 grid .f-list.yscroll -row 0 -column 1 -sticky ns
204 grid rowconfigure .f-list 0 -weight 1
205 grid columnconfigure .f-list 0 -weight 1
207 pack .b-custom .b-track .b-regexp .b-glob .b-subgram .b-anagram \
208 -in .f-buttons -fill x -padx 2 -pady 2
210 pack .e-word -in .f-entry -expand yes -fill x -padx 2 -pady 2
212 pack .f-entry -fill x
213 pack .f-list -side left -expand yes -fill both
214 pack .f-buttons -side left -anchor s
217 .menu add cascade -label "File" -underline 0 -menu .menu.file
219 .menu.file add command -label "Options..." -underline 0 -command { options }
220 .menu.file add command -label "Quit" -underline 0 -command { destroy . }
221 . configure -menu .menu
224 bind .e-word <Return> { tkButtonInvoke .b-anagram }
226 bind . <Alt-a> { tkButtonInvoke .b-anagram }
227 bind . <Alt-t> { tkButtonInvoke .b-track }
228 bind . <Alt-s> { tkButtonInvoke .b-subgram }
229 bind . <Alt-w> { tkButtonInvoke .b-glob }
230 bind . <Alt-r> { tkButtonInvoke .b-regexp }
231 bind . <Alt-c> { tkButtonInvoke .b-custom }