1 # git-gui spellchecking support through ispell/aspell
2 # Copyright (C) 2008 Shawn Pearce
6 field s_fd
{} ; # pipe to ispell/aspell
7 field s_version
{} ; # ispell/aspell version string
8 field s_lang
{} ; # current language code
10 field w_text
; # text widget we are spelling
11 field w_menu
; # context menu for the widget
12 field s_menuidx
0 ; # last index of insertion into $w_menu
14 field s_i
{} ; # timer registration for _run callbacks
15 field s_clear
0 ; # did we erase mispelled tags yet?
16 field s_seen
[list] ; # lines last seen from $w_text in _run
17 field s_checked
[list] ; # lines already checked
18 field s_pending
[list] ; # [$line $data] sent to ispell/aspell
19 field s_suggest
; # array, list of suggestions, keyed by misspelling
21 constructor init
{pipe_fd ui_text ui_menu
} {
26 bind_button3
$w_text [cb _popup_suggest
%X
%Y
@%x
,%y
]
27 _connect
$this $pipe_fd
31 method _connect
{pipe_fd
} {
37 if {[gets $pipe_fd s_version
] <= 0} {
38 if {[catch {close $pipe_fd} err
]} {
39 regsub -nocase {^Error
: } $err {} err
41 error_popup
[strcat
[mc
"Spell checking is unavailable"] ":\n\n$err"]
44 [mc
"Invalid spell checking configuration"] \
46 [mc
"Reverting dictionary to %s." $s_lang]]
49 error_popup
[mc
"Spell checker sliently failed on startup"]
53 if {{@(#) } ne [string range $s_version 0 4]} {
54 catch {close $pipe_fd}
55 error_popup
[strcat
[mc
"Unrecognized spell checker"] ":\n\n$s_version"]
58 set s_version
[string range
$s_version 5 end
]
60 puts $pipe_fd ! ; # enable terse mode
61 puts $pipe_fd {$$cr master
} ; # fetch the language
65 regexp {[/\\]([^
/\\]+)\.
[^
\.
]+$} $s_lang _ s_lang
67 if {$::default_config(gui.spellingdictionary
) eq
{}
68 && [get_config gui.spellingdictionary
] eq
{}} {
69 set ::default_config(gui.spellingdictionary
) $s_lang
77 fconfigure $s_fd -blocking 0
78 fileevent $s_fd readable
[cb _read
]
80 $w_text tag conf misspelled
\
91 method lang
{{n
{}}} {
92 if {$n ne
{} && $s_lang ne
$n} {
93 set spell_cmd
[list |
]
94 lappend spell_cmd aspell
95 lappend spell_cmd
--master=$n
96 lappend spell_cmd
--mode=none
97 lappend spell_cmd
--encoding=UTF-8
98 lappend spell_cmd pipe
99 _connect
$this [open $spell_cmd r
+]
105 if {$s_version ne
{}} {
106 return "$s_version, $s_lang"
112 while {$s_menuidx > 0} {
116 $w_text tag delete misspelled
119 catch {after cancel
$s_i}
125 method _popup_suggest
{X Y pos
} {
126 while {$s_menuidx > 0} {
131 set b_loc
[$w_text index
"$pos wordstart"]
132 set e_loc
[_wordend
$this $b_loc]
133 set orig
[$w_text get
$b_loc $e_loc]
134 set tags
[$w_text tag names
$b_loc]
136 if {[lsearch -exact $tags misspelled
] >= 0} {
137 if {[info exists s_suggest
($orig)]} {
139 foreach s
$s_suggest($orig) {
141 $w_menu insert
$s_menuidx command
\
143 -command [cb _replace
$b_loc $e_loc $s]
151 $w_menu insert
$s_menuidx command
\
152 -label [mc
"No Suggestions"] \
156 $w_menu insert
$s_menuidx separator
160 $w_text mark
set saved-insert insert
161 tk_popup $w_menu $X $Y
164 method _replace
{b_loc e_loc word
} {
165 $w_text configure
-autoseparators 0
166 $w_text edit separator
168 $w_text delete
$b_loc $e_loc
169 $w_text insert
$b_loc $word
171 $w_text edit separator
172 $w_text configure
-autoseparators 1
173 $w_text mark
set insert saved-insert
176 method _restart_timer
{} {
177 set s_i
[after 300 [cb _run
]]
180 proc _match_length
{max_line arr_name
} {
183 if {[llength $a] > $max_line} {
184 set a
[lrange $a 0 $max_line]
186 while {[llength $a] <= $max_line} {
191 method _wordend
{pos
} {
192 set pos
[$w_text index
"$pos wordend"]
193 set tags
[$w_text tag names
$pos]
194 while {[lsearch -exact $tags misspelled
] >= 0} {
195 set pos
[$w_text index
"$pos +1c"]
196 set tags
[$w_text tag names
$pos]
202 set cur_pos
[$w_text index
{insert
-1c}]
203 set cur_line
[lindex [split $cur_pos .
] 0]
204 set max_line
[lindex [split [$w_text index end
] .
] 0]
205 _match_length
$max_line s_seen
206 _match_length
$max_line s_checked
208 # Nothing in the message buffer? Nothing to spellcheck.
212 && [$w_text get
1.0 end
] eq
"\n"} {
213 array unset s_suggest
219 for {set n
1} {$n <= $max_line} {incr n
} {
220 set s
[$w_text get
"$n.0" "$n.end"]
222 # Don't spellcheck the current line unless we are at
223 # a word boundary. The user might be typing on it.
226 && ![regexp {^
\W
$} [$w_text get
$cur_pos insert
]]} {
228 # If the current word is mispelled remove the tag
229 # but force a spellcheck later.
231 set tags
[$w_text tag names
$cur_pos]
232 if {[lsearch -exact $tags misspelled
] >= 0} {
233 $w_text tag remove misspelled
\
234 "$cur_pos wordstart" \
235 [_wordend
$this $cur_pos]
243 if {[lindex $s_seen $n] eq
$s
244 && [lindex $s_checked $n] ne
$s} {
245 # Don't send empty lines to Aspell it doesn't check them.
252 # Don't send typical s-b-o lines as the emails are
253 # almost always misspelled according to Aspell.
255 if {[regexp -nocase {^
[a-z-
]+-by:.
*<.
*@.
*>$} $s]} {
256 $w_text tag remove misspelled
"$n.0" "$n.end"
262 lappend s_pending
[list $n $s]
265 # Delay until another idle loop to make sure we don't
266 # spellcheck lines the user is actively changing.
281 while {[gets $s_fd line
] >= 0} {
282 set lineno
[lindex $s_pending 0 0]
285 $w_text tag remove misspelled
"$lineno.0" "$lineno.end"
290 lset s_checked
$lineno [lindex $s_pending 0 1]
291 set s_pending
[lrange $s_pending 1 end
]
297 switch -- [string range
$line 0 1] {
299 set line
[split [string range
$line 2 end
] :]
300 set info [split [lindex $line 0] { }]
301 set orig
[lindex $info 0]
302 set offs
[lindex $info 2]
303 foreach s
[split [lindex $line 1] ,] {
304 lappend sugg
[string range
$s 1 end
]
308 set info [split [string range
$line 2 end
] { }]
309 set orig
[lindex $info 0]
310 set offs
[lindex $info 1]
313 puts stderr
"<spell> $line"
319 set b_loc
"$lineno.$offs"
320 set e_loc
[$w_text index
"$lineno.$offs wordend"]
321 set curr
[$w_text get
$b_loc $e_loc]
323 # At least for English curr = "bob", orig = "bob's"
324 # so Tk didn't include the 's but Aspell did. We
325 # try to round out the word.
327 while {$curr ne
$orig
328 && [string equal
-length [string length
$curr] $curr $orig]} {
329 set n_loc
[$w_text index
"$e_loc +1c"]
330 set n_curr
[$w_text get
$b_loc $n_loc]
331 if {$n_curr eq
$curr} {
338 if {$curr eq
$orig} {
339 $w_text tag add misspelled
$b_loc $e_loc
340 if {[llength $sugg] > 0} {
341 set s_suggest
($orig) $sugg
343 unset -nocomplain s_suggest
($orig)
346 unset -nocomplain s_suggest
($orig)
350 fconfigure $s_fd -block 1
352 if {![catch {close $s_fd} err
]} {
353 set err
[mc
"Unexpected EOF from spell checker"]
355 catch {after cancel
$s_i}
356 $w_text tag remove misspelled
1.0 end
357 error_popup
[strcat
[mc
"Spell Checker Failed"] "\n\n" $err]
360 fconfigure $s_fd -block 0
362 if {[llength $s_pending] == 0} {
367 proc available_langs
{} {
370 set fd
[open [list | aspell dump dicts
] r
]
371 while {[gets $fd line
] >= 0} {
372 if {$line eq
{}} continue