Merge git://git.kernel.org/pub/scm/gitk/gitk
[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
88         puts $pipe_fd !             ; # enable terse mode
89         puts $pipe_fd {$$cr master} ; # fetch the language
90         flush $pipe_fd
91
92         gets $pipe_fd s_lang
93         regexp {[/\\]([^/\\]+)\.[^\.]+$} $s_lang _ s_lang
94
95         if {$::default_config(gui.spellingdictionary) eq {}
96          && [get_config gui.spellingdictionary] eq {}} {
97                 set ::default_config(gui.spellingdictionary) $s_lang
98         }
99
100         if {$s_fd ne {}} {
101                 catch {close $s_fd}
102         }
103         set s_fd $pipe_fd
104
105         fconfigure $s_fd -blocking 0
106         fileevent $s_fd readable [cb _read]
107
108         $w_text tag conf misspelled \
109                 -foreground red \
110                 -underline 1
111
112         array unset s_suggest
113         set s_seen    [list]
114         set s_checked [list]
115         set s_pending [list]
116         _run $this
117 }
118
119 method lang {{n {}}} {
120         if {$n ne {} && $s_lang ne $n && !$s_failed} {
121                 set spell_cmd [list |]
122                 lappend spell_cmd aspell
123                 lappend spell_cmd --master=$n
124                 lappend spell_cmd --mode=none
125                 lappend spell_cmd --encoding=UTF-8
126                 lappend spell_cmd pipe
127                 _connect $this [open $spell_cmd r+]
128         }
129         return $s_lang
130 }
131
132 method version {} {
133         if {$s_version ne {}} {
134                 return "$s_version, $s_lang"
135         }
136         return {}
137 }
138
139 method stop {} {
140         while {$s_menuidx > 0} {
141                 $w_menu delete 0
142                 incr s_menuidx -1
143         }
144         $w_text tag delete misspelled
145
146         catch {close $s_fd}
147         catch {after cancel $s_i}
148         set s_fd {}
149         set s_i {}
150         set s_lang {}
151 }
152
153 method _popup_suggest {X Y pos} {
154         while {$s_menuidx > 0} {
155                 $w_menu delete 0
156                 incr s_menuidx -1
157         }
158
159         set b_loc [$w_text index "$pos wordstart"]
160         set e_loc [_wordend $this $b_loc]
161         set orig  [$w_text get $b_loc $e_loc]
162         set tags  [$w_text tag names $b_loc]
163
164         if {[lsearch -exact $tags misspelled] >= 0} {
165                 if {[info exists s_suggest($orig)]} {
166                         set cnt 0
167                         foreach s $s_suggest($orig) {
168                                 if {$cnt < 5} {
169                                         $w_menu insert $s_menuidx command \
170                                                 -label $s \
171                                                 -command [cb _replace $b_loc $e_loc $s]
172                                         incr s_menuidx
173                                         incr cnt
174                                 } else {
175                                         break
176                                 }
177                         }
178                 } else {
179                         $w_menu insert $s_menuidx command \
180                                 -label [mc "No Suggestions"] \
181                                 -state disabled
182                         incr s_menuidx
183                 }
184                 $w_menu insert $s_menuidx separator
185                 incr s_menuidx
186         }
187
188         $w_text mark set saved-insert insert
189         tk_popup $w_menu $X $Y
190 }
191
192 method _replace {b_loc e_loc word} {
193         $w_text configure -autoseparators 0
194         $w_text edit separator
195
196         $w_text delete $b_loc $e_loc
197         $w_text insert $b_loc $word
198
199         $w_text edit separator
200         $w_text configure -autoseparators 1
201         $w_text mark set insert saved-insert
202 }
203
204 method _restart_timer {} {
205         set s_i [after 300 [cb _run]]
206 }
207
208 proc _match_length {max_line arr_name} {
209         upvar $arr_name a
210
211         if {[llength $a] > $max_line} {
212                 set a [lrange $a 0 $max_line]
213         }
214         while {[llength $a] <= $max_line} {
215                 lappend a {}
216         }
217 }
218
219 method _wordend {pos} {
220         set pos  [$w_text index "$pos wordend"]
221         set tags [$w_text tag names $pos]
222         while {[lsearch -exact $tags misspelled] >= 0} {
223                 set pos  [$w_text index "$pos +1c"]
224                 set tags [$w_text tag names $pos]
225         }
226         return $pos
227 }
228
229 method _run {} {
230         set cur_pos  [$w_text index {insert -1c}]
231         set cur_line [lindex [split $cur_pos .] 0]
232         set max_line [lindex [split [$w_text index end] .] 0]
233         _match_length $max_line s_seen
234         _match_length $max_line s_checked
235
236         # Nothing in the message buffer?  Nothing to spellcheck.
237         #
238         if {$cur_line == 1
239          && $max_line == 2
240          && [$w_text get 1.0 end] eq "\n"} {
241                 array unset s_suggest
242                 _restart_timer $this
243                 return
244         }
245
246         set active 0
247         for {set n 1} {$n <= $max_line} {incr n} {
248                 set s [$w_text get "$n.0" "$n.end"]
249
250                 # Don't spellcheck the current line unless we are at
251                 # a word boundary.  The user might be typing on it.
252                 #
253                 if {$n == $cur_line
254                  && ![regexp {^\W$} [$w_text get $cur_pos insert]]} {
255
256                         # If the current word is mispelled remove the tag
257                         # but force a spellcheck later.
258                         #
259                         set tags [$w_text tag names $cur_pos]
260                         if {[lsearch -exact $tags misspelled] >= 0} {
261                                 $w_text tag remove misspelled \
262                                         "$cur_pos wordstart" \
263                                         [_wordend $this $cur_pos]
264                                 lset s_seen    $n $s
265                                 lset s_checked $n {}
266                         }
267
268                         continue
269                 }
270
271                 if {[lindex $s_seen    $n] eq $s
272                  && [lindex $s_checked $n] ne $s} {
273                         # Don't send empty lines to Aspell it doesn't check them.
274                         #
275                         if {$s eq {}} {
276                                 lset s_checked $n $s
277                                 continue
278                         }
279
280                         # Don't send typical s-b-o lines as the emails are
281                         # almost always misspelled according to Aspell.
282                         #
283                         if {[regexp -nocase {^[a-z-]+-by:.*<.*@.*>$} $s]} {
284                                 $w_text tag remove misspelled "$n.0" "$n.end"
285                                 lset s_checked $n $s
286                                 continue
287                         }
288
289                         puts $s_fd ^$s
290                         lappend s_pending [list $n $s]
291                         set active 1
292                 } else {
293                         # Delay until another idle loop to make sure we don't
294                         # spellcheck lines the user is actively changing.
295                         #
296                         lset s_seen $n $s
297                 }
298         }
299
300         if {$active} {
301                 set s_clear 1
302                 flush $s_fd
303         } else {
304                 _restart_timer $this
305         }
306 }
307
308 method _read {} {
309         while {[gets $s_fd line] >= 0} {
310                 set lineno [lindex $s_pending 0 0]
311
312                 if {$s_clear} {
313                         $w_text tag remove misspelled "$lineno.0" "$lineno.end"
314                         set s_clear 0
315                 }
316
317                 if {$line eq {}} {
318                         lset s_checked $lineno [lindex $s_pending 0 1]
319                         set s_pending [lrange $s_pending 1 end]
320                         set s_clear 1
321                         continue
322                 }
323
324                 set sugg [list]
325                 switch -- [string range $line 0 1] {
326                 {& } {
327                         set line [split [string range $line 2 end] :]
328                         set info [split [lindex $line 0] { }]
329                         set orig [lindex $info 0]
330                         set offs [lindex $info 2]
331                         foreach s [split [lindex $line 1] ,] {
332                                 lappend sugg [string range $s 1 end]
333                         }
334                 }
335                 {# } {
336                         set info [split [string range $line 2 end] { }]
337                         set orig [lindex $info 0]
338                         set offs [lindex $info 1]
339                 }
340                 default {
341                         puts stderr "<spell> $line"
342                         continue
343                 }
344                 }
345
346                 incr offs -1
347                 set b_loc "$lineno.$offs"
348                 set e_loc [$w_text index "$lineno.$offs wordend"]
349                 set curr [$w_text get $b_loc $e_loc]
350
351                 # At least for English curr = "bob", orig = "bob's"
352                 # so Tk didn't include the 's but Aspell did.  We
353                 # try to round out the word.
354                 #
355                 while {$curr ne $orig
356                  && [string equal -length [string length $curr] $curr $orig]} {
357                         set n_loc  [$w_text index "$e_loc +1c"]
358                         set n_curr [$w_text get $b_loc $n_loc]
359                         if {$n_curr eq $curr} {
360                                 break
361                         }
362                         set curr  $n_curr
363                         set e_loc $n_loc
364                 }
365
366                 if {$curr eq $orig} {
367                         $w_text tag add misspelled $b_loc $e_loc
368                         if {[llength $sugg] > 0} {
369                                 set s_suggest($orig) $sugg
370                         } else {
371                                 unset -nocomplain s_suggest($orig)
372                         }
373                 } else {
374                         unset -nocomplain s_suggest($orig)
375                 }
376         }
377
378         fconfigure $s_fd -block 1
379         if {[eof $s_fd]} {
380                 if {![catch {close $s_fd} err]} {
381                         set err [mc "Unexpected EOF from spell checker"]
382                 }
383                 catch {after cancel $s_i}
384                 $w_text tag remove misspelled 1.0 end
385                 error_popup [strcat [mc "Spell Checker Failed"] "\n\n" $err]
386                 return
387         }
388         fconfigure $s_fd -block 0
389
390         if {[llength $s_pending] == 0} {
391                 _restart_timer $this
392         }
393 }
394
395 proc available_langs {} {
396         set langs [list]
397         catch {
398                 set fd [open [list | aspell dump dicts] r]
399                 while {[gets $fd line] >= 0} {
400                         if {$line eq {}} continue
401                         lappend langs $line
402                 }
403                 close $fd
404         }
405         return $langs
406 }
407
408 }