Merge git://repo.or.cz/git-gui
[git/git.git] / git-gui / lib / spellcheck.tcl
1 # git-gui spellchecking support through ispell/aspell
2 # Copyright (C) 2008 Shawn Pearce
3
4 class spellcheck {
5
6 field s_fd {} ; # pipe to ispell/aspell
7 field s_version {} ; # ispell/aspell version string
8 field s_lang {} ; # current language code
9 field s_prog aspell; # are we actually old ispell?
10 field s_failed 0 ; # is $s_prog bogus and not working?
11
12 field w_text ; # text widget we are spelling
13 field w_menu ; # context menu for the widget
14 field s_menuidx 0 ; # last index of insertion into $w_menu
15
16 field s_i {} ; # timer registration for _run callbacks
17 field s_clear 0 ; # did we erase mispelled tags yet?
18 field s_seen [list] ; # lines last seen from $w_text in _run
19 field s_checked [list] ; # lines already checked
20 field s_pending [list] ; # [$line $data] sent to ispell/aspell
21 field s_suggest ; # array, list of suggestions, keyed by misspelling
22
23 constructor init {pipe_fd ui_text ui_menu} {
24 set w_text $ui_text
25 set w_menu $ui_menu
26 array unset s_suggest
27
28 bind_button3 $w_text [cb _popup_suggest %X %Y @%x,%y]
29 _connect $this $pipe_fd
30 return $this
31 }
32
33 method _connect {pipe_fd} {
34 fconfigure $pipe_fd \
35 -encoding utf-8 \
36 -eofchar {} \
37 -translation lf
38
39 if {[gets $pipe_fd s_version] <= 0} {
40 if {[catch {close $pipe_fd} err]} {
41
42 # Eh? Is this actually ispell choking on aspell options?
43 #
44 if {$s_prog eq {aspell}
45 && [regexp -nocase {^Usage: } $err]
46 && ![catch {
47 set pipe_fd [open [list | $s_prog -v] r]
48 gets $pipe_fd s_version
49 close $pipe_fd
50 }]
51 && $s_version ne {}} {
52 if {{@(#) } eq [string range $s_version 0 4]} {
53 set s_version [string range $s_version 5 end]
54 }
55 set s_failed 1
56 error_popup [strcat \
57 [mc "Unsupported spell checker"] \
58 ":\n\n$s_version"]
59 set s_version {}
60 return
61 }
62
63 regsub -nocase {^Error: } $err {} err
64 if {$s_fd eq {}} {
65 error_popup [strcat [mc "Spell checking is unavailable"] ":\n\n$err"]
66 } else {
67 error_popup [strcat \
68 [mc "Invalid spell checking configuration"] \
69 ":\n\n$err\n\n" \
70 [mc "Reverting dictionary to %s." $s_lang]]
71 }
72 } else {
73 error_popup [mc "Spell checker silently failed on startup"]
74 }
75 return
76 }
77
78 if {{@(#) } ne [string range $s_version 0 4]} {
79 catch {close $pipe_fd}
80 error_popup [strcat [mc "Unrecognized spell checker"] ":\n\n$s_version"]
81 return
82 }
83 set s_version [string range $s_version 5 end]
84 regexp \
85 {International Ispell Version .* \(but really (Aspell .*?)\)$} \
86 $s_version _junk s_version
87 regexp {^Aspell (\d)+\.(\d+)} $s_version _junk major minor
88
89 puts $pipe_fd ! ; # enable terse mode
90
91 # fetch the language
92 if {$major > 0 || ($major == 0 && $minor >= 60)} {
93 puts $pipe_fd {$$cr master}
94 flush $pipe_fd
95 gets $pipe_fd s_lang
96 regexp {[/\\]([^/\\]+)\.[^\.]+$} $s_lang _ s_lang
97 } else {
98 set s_lang {}
99 }
100
101 if {$::default_config(gui.spellingdictionary) eq {}
102 && [get_config gui.spellingdictionary] eq {}} {
103 set ::default_config(gui.spellingdictionary) $s_lang
104 }
105
106 if {$s_fd ne {}} {
107 catch {close $s_fd}
108 }
109 set s_fd $pipe_fd
110
111 fconfigure $s_fd -blocking 0
112 fileevent $s_fd readable [cb _read]
113
114 $w_text tag conf misspelled \
115 -foreground red \
116 -underline 1
117
118 array unset s_suggest
119 set s_seen [list]
120 set s_checked [list]
121 set s_pending [list]
122 _run $this
123 }
124
125 method lang {{n {}}} {
126 if {$n ne {} && $s_lang ne $n && !$s_failed} {
127 set spell_cmd [list |]
128 lappend spell_cmd aspell
129 lappend spell_cmd --master=$n
130 lappend spell_cmd --mode=none
131 lappend spell_cmd --encoding=UTF-8
132 lappend spell_cmd pipe
133 _connect $this [open $spell_cmd r+]
134 }
135 return $s_lang
136 }
137
138 method version {} {
139 if {$s_version ne {}} {
140 return "$s_version, $s_lang"
141 }
142 return {}
143 }
144
145 method stop {} {
146 while {$s_menuidx > 0} {
147 $w_menu delete 0
148 incr s_menuidx -1
149 }
150 $w_text tag delete misspelled
151
152 catch {close $s_fd}
153 catch {after cancel $s_i}
154 set s_fd {}
155 set s_i {}
156 set s_lang {}
157 }
158
159 method _popup_suggest {X Y pos} {
160 while {$s_menuidx > 0} {
161 $w_menu delete 0
162 incr s_menuidx -1
163 }
164
165 set b_loc [$w_text index "$pos wordstart"]
166 set e_loc [_wordend $this $b_loc]
167 set orig [$w_text get $b_loc $e_loc]
168 set tags [$w_text tag names $b_loc]
169
170 if {[lsearch -exact $tags misspelled] >= 0} {
171 if {[info exists s_suggest($orig)]} {
172 set cnt 0
173 foreach s $s_suggest($orig) {
174 if {$cnt < 5} {
175 $w_menu insert $s_menuidx command \
176 -label $s \
177 -command [cb _replace $b_loc $e_loc $s]
178 incr s_menuidx
179 incr cnt
180 } else {
181 break
182 }
183 }
184 } else {
185 $w_menu insert $s_menuidx command \
186 -label [mc "No Suggestions"] \
187 -state disabled
188 incr s_menuidx
189 }
190 $w_menu insert $s_menuidx separator
191 incr s_menuidx
192 }
193
194 $w_text mark set saved-insert insert
195 tk_popup $w_menu $X $Y
196 }
197
198 method _replace {b_loc e_loc word} {
199 $w_text configure -autoseparators 0
200 $w_text edit separator
201
202 $w_text delete $b_loc $e_loc
203 $w_text insert $b_loc $word
204
205 $w_text edit separator
206 $w_text configure -autoseparators 1
207 $w_text mark set insert saved-insert
208 }
209
210 method _restart_timer {} {
211 set s_i [after 300 [cb _run]]
212 }
213
214 proc _match_length {max_line arr_name} {
215 upvar $arr_name a
216
217 if {[llength $a] > $max_line} {
218 set a [lrange $a 0 $max_line]
219 }
220 while {[llength $a] <= $max_line} {
221 lappend a {}
222 }
223 }
224
225 method _wordend {pos} {
226 set pos [$w_text index "$pos wordend"]
227 set tags [$w_text tag names $pos]
228 while {[lsearch -exact $tags misspelled] >= 0} {
229 set pos [$w_text index "$pos +1c"]
230 set tags [$w_text tag names $pos]
231 }
232 return $pos
233 }
234
235 method _run {} {
236 set cur_pos [$w_text index {insert -1c}]
237 set cur_line [lindex [split $cur_pos .] 0]
238 set max_line [lindex [split [$w_text index end] .] 0]
239 _match_length $max_line s_seen
240 _match_length $max_line s_checked
241
242 # Nothing in the message buffer? Nothing to spellcheck.
243 #
244 if {$cur_line == 1
245 && $max_line == 2
246 && [$w_text get 1.0 end] eq "\n"} {
247 array unset s_suggest
248 _restart_timer $this
249 return
250 }
251
252 set active 0
253 for {set n 1} {$n <= $max_line} {incr n} {
254 set s [$w_text get "$n.0" "$n.end"]
255
256 # Don't spellcheck the current line unless we are at
257 # a word boundary. The user might be typing on it.
258 #
259 if {$n == $cur_line
260 && ![regexp {^\W$} [$w_text get $cur_pos insert]]} {
261
262 # If the current word is mispelled remove the tag
263 # but force a spellcheck later.
264 #
265 set tags [$w_text tag names $cur_pos]
266 if {[lsearch -exact $tags misspelled] >= 0} {
267 $w_text tag remove misspelled \
268 "$cur_pos wordstart" \
269 [_wordend $this $cur_pos]
270 lset s_seen $n $s
271 lset s_checked $n {}
272 }
273
274 continue
275 }
276
277 if {[lindex $s_seen $n] eq $s
278 && [lindex $s_checked $n] ne $s} {
279 # Don't send empty lines to Aspell it doesn't check them.
280 #
281 if {$s eq {}} {
282 lset s_checked $n $s
283 continue
284 }
285
286 # Don't send typical s-b-o lines as the emails are
287 # almost always misspelled according to Aspell.
288 #
289 if {[regexp -nocase {^[a-z-]+-by:.*<.*@.*>$} $s]} {
290 $w_text tag remove misspelled "$n.0" "$n.end"
291 lset s_checked $n $s
292 continue
293 }
294
295 puts $s_fd ^$s
296 lappend s_pending [list $n $s]
297 set active 1
298 } else {
299 # Delay until another idle loop to make sure we don't
300 # spellcheck lines the user is actively changing.
301 #
302 lset s_seen $n $s
303 }
304 }
305
306 if {$active} {
307 set s_clear 1
308 flush $s_fd
309 } else {
310 _restart_timer $this
311 }
312 }
313
314 method _read {} {
315 while {[gets $s_fd line] >= 0} {
316 set lineno [lindex $s_pending 0 0]
317
318 if {$s_clear} {
319 $w_text tag remove misspelled "$lineno.0" "$lineno.end"
320 set s_clear 0
321 }
322
323 if {$line eq {}} {
324 lset s_checked $lineno [lindex $s_pending 0 1]
325 set s_pending [lrange $s_pending 1 end]
326 set s_clear 1
327 continue
328 }
329
330 set sugg [list]
331 switch -- [string range $line 0 1] {
332 {& } {
333 set line [split [string range $line 2 end] :]
334 set info [split [lindex $line 0] { }]
335 set orig [lindex $info 0]
336 set offs [lindex $info 2]
337 foreach s [split [lindex $line 1] ,] {
338 lappend sugg [string range $s 1 end]
339 }
340 }
341 {# } {
342 set info [split [string range $line 2 end] { }]
343 set orig [lindex $info 0]
344 set offs [lindex $info 1]
345 }
346 default {
347 puts stderr "<spell> $line"
348 continue
349 }
350 }
351
352 incr offs -1
353 set b_loc "$lineno.$offs"
354 set e_loc [$w_text index "$lineno.$offs wordend"]
355 set curr [$w_text get $b_loc $e_loc]
356
357 # At least for English curr = "bob", orig = "bob's"
358 # so Tk didn't include the 's but Aspell did. We
359 # try to round out the word.
360 #
361 while {$curr ne $orig
362 && [string equal -length [string length $curr] $curr $orig]} {
363 set n_loc [$w_text index "$e_loc +1c"]
364 set n_curr [$w_text get $b_loc $n_loc]
365 if {$n_curr eq $curr} {
366 break
367 }
368 set curr $n_curr
369 set e_loc $n_loc
370 }
371
372 if {$curr eq $orig} {
373 $w_text tag add misspelled $b_loc $e_loc
374 if {[llength $sugg] > 0} {
375 set s_suggest($orig) $sugg
376 } else {
377 unset -nocomplain s_suggest($orig)
378 }
379 } else {
380 unset -nocomplain s_suggest($orig)
381 }
382 }
383
384 fconfigure $s_fd -block 1
385 if {[eof $s_fd]} {
386 if {![catch {close $s_fd} err]} {
387 set err [mc "Unexpected EOF from spell checker"]
388 }
389 catch {after cancel $s_i}
390 $w_text tag remove misspelled 1.0 end
391 error_popup [strcat [mc "Spell Checker Failed"] "\n\n" $err]
392 return
393 }
394 fconfigure $s_fd -block 0
395
396 if {[llength $s_pending] == 0} {
397 _restart_timer $this
398 }
399 }
400
401 proc available_langs {} {
402 set langs [list]
403 catch {
404 set fd [open [list | aspell dump dicts] r]
405 while {[gets $fd line] >= 0} {
406 if {$line eq {}} continue
407 lappend langs $line
408 }
409 close $fd
410 }
411 return $langs
412 }
413
414 }