1 # git-gui spellchecking support through aspell
2 # Copyright (C) 2008 Shawn Pearce
6 field s_fd
{} ; # pipe to aspell
7 field s_version
; # 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 aspell
19 field s_suggest
; # array, list of suggestions, keyed by misspelling
21 constructor init
{pipe_fd ui_text ui_menu
} {
25 _connect
$this $pipe_fd
29 method _connect
{pipe_fd
} {
35 if {[gets $pipe_fd s_version
] <= 0} {
37 error [mc
"Not connected to aspell"]
39 if {{@(#) } ne [string range $s_version 0 4]} {
41 error [strcat
[mc
"Unrecognized aspell version"] ": $s_version"]
43 set s_version
[string range
$s_version 5 end
]
45 puts $pipe_fd ! ; # enable terse mode
46 puts $pipe_fd {$$cr master
} ; # fetch the language
50 regexp {[/\\]([^
/\\]+)\.
[^
\.
]+$} $s_lang _ s_lang
52 if {$::default_config(gui.spellingdictionary
) eq
{}
53 && [get_config gui.spellingdictionary
] eq
{}} {
54 set ::default_config(gui.spellingdictionary
) $s_lang
62 fconfigure $s_fd -blocking 0
63 fileevent $s_fd readable
[cb _read
]
65 $w_text tag conf misspelled
\
68 bind_button3
$w_text [cb _popup_suggest
%X
%Y
@%x
,%y
]
77 method lang
{{n
{}}} {
78 if {$n ne
{} && $s_lang ne
$n} {
79 set spell_cmd
[list |
]
80 lappend spell_cmd aspell
81 lappend spell_cmd
--master=$n
82 lappend spell_cmd
--mode=none
83 lappend spell_cmd
--encoding=UTF-8
84 lappend spell_cmd pipe
85 _connect
$this [open $spell_cmd r
+]
91 return "$s_version, $s_lang"
95 while {$s_menuidx > 0} {
99 $w_text tag delete misspelled
102 catch {after cancel
$s_i}
108 method _popup_suggest
{X Y pos
} {
109 while {$s_menuidx > 0} {
114 set b_loc
[$w_text index
"$pos wordstart"]
115 set e_loc
[_wordend
$this $b_loc]
116 set orig
[$w_text get
$b_loc $e_loc]
117 set tags
[$w_text tag names
$b_loc]
119 if {[lsearch -exact $tags misspelled
] >= 0} {
120 if {[info exists s_suggest
($orig)]} {
122 foreach s
$s_suggest($orig) {
124 $w_menu insert
$s_menuidx command
\
126 -command [cb _replace
$b_loc $e_loc $s]
134 $w_menu insert
$s_menuidx command
\
135 -label [mc
"No Suggestions"] \
139 $w_menu insert
$s_menuidx separator
143 $w_text mark
set saved-insert insert
144 tk_popup $w_menu $X $Y
147 method _replace
{b_loc e_loc word
} {
148 $w_text configure
-autoseparators 0
149 $w_text edit separator
151 $w_text delete
$b_loc $e_loc
152 $w_text insert
$b_loc $word
154 $w_text edit separator
155 $w_text configure
-autoseparators 1
156 $w_text mark
set insert saved-insert
159 method _restart_timer
{} {
160 set s_i
[after 300 [cb _run
]]
163 proc _match_length
{max_line arr_name
} {
166 if {[llength $a] > $max_line} {
167 set a
[lrange $a 0 $max_line]
169 while {[llength $a] <= $max_line} {
174 method _wordend
{pos
} {
175 set pos
[$w_text index
"$pos wordend"]
176 set tags
[$w_text tag names
$pos]
177 while {[lsearch -exact $tags misspelled
] >= 0} {
178 set pos
[$w_text index
"$pos +1c"]
179 set tags
[$w_text tag names
$pos]
185 set cur_pos
[$w_text index
{insert
-1c}]
186 set cur_line
[lindex [split $cur_pos .
] 0]
187 set max_line
[lindex [split [$w_text index end
] .
] 0]
188 _match_length
$max_line s_seen
189 _match_length
$max_line s_checked
191 # Nothing in the message buffer? Nothing to spellcheck.
195 && [$w_text get
1.0 end
] eq
"\n"} {
196 array unset s_suggest
202 for {set n
1} {$n <= $max_line} {incr n
} {
203 set s
[$w_text get
"$n.0" "$n.end"]
205 # Don't spellcheck the current line unless we are at
206 # a word boundary. The user might be typing on it.
209 && ![regexp {^
\W
$} [$w_text get
$cur_pos insert
]]} {
211 # If the current word is mispelled remove the tag
212 # but force a spellcheck later.
214 set tags
[$w_text tag names
$cur_pos]
215 if {[lsearch -exact $tags misspelled
] >= 0} {
216 $w_text tag remove misspelled
\
217 "$cur_pos wordstart" \
218 [_wordend
$this $cur_pos]
226 if {[lindex $s_seen $n] eq
$s
227 && [lindex $s_checked $n] ne
$s} {
228 # Don't send empty lines to Aspell it doesn't check them.
235 # Don't send typical s-b-o lines as the emails are
236 # almost always misspelled according to Aspell.
238 if {[regexp -nocase {^
[a-z-
]+-by:.
*<.
*@.
*>$} $s]} {
239 $w_text tag remove misspelled
"$n.0" "$n.end"
245 lappend s_pending
[list $n $s]
248 # Delay until another idle loop to make sure we don't
249 # spellcheck lines the user is actively changing.
264 while {[gets $s_fd line
] >= 0} {
265 set lineno
[lindex $s_pending 0 0]
268 $w_text tag remove misspelled
"$lineno.0" "$lineno.end"
273 lset s_checked
$lineno [lindex $s_pending 0 1]
274 set s_pending
[lrange $s_pending 1 end
]
280 switch -- [string range
$line 0 1] {
282 set line
[split [string range
$line 2 end
] :]
283 set info [split [lindex $line 0] { }]
284 set orig
[lindex $info 0]
285 set offs
[lindex $info 2]
286 foreach s
[split [lindex $line 1] ,] {
287 lappend sugg
[string range
$s 1 end
]
291 set info [split [string range
$line 2 end
] { }]
292 set orig
[lindex $info 0]
293 set offs
[lindex $info 1]
296 puts stderr
"<spell> $line"
302 set b_loc
"$lineno.$offs"
303 set e_loc
[$w_text index
"$lineno.$offs wordend"]
304 set curr
[$w_text get
$b_loc $e_loc]
306 # At least for English curr = "bob", orig = "bob's"
307 # so Tk didn't include the 's but Aspell did. We
308 # try to round out the word.
310 while {$curr ne
$orig
311 && [string equal
-length [string length
$curr] $curr $orig]} {
312 set n_loc
[$w_text index
"$e_loc +1c"]
313 set n_curr
[$w_text get
$b_loc $n_loc]
314 if {$n_curr eq
$curr} {
321 if {$curr eq
$orig} {
322 $w_text tag add misspelled
$b_loc $e_loc
323 if {[llength $sugg] > 0} {
324 set s_suggest
($orig) $sugg
326 unset -nocomplain s_suggest
($orig)
329 unset -nocomplain s_suggest
($orig)
333 fconfigure $s_fd -block 1
335 if {![catch {close $s_fd} err
]} {
336 set err
[mc
"Unexpected EOF from aspell"]
338 catch {after cancel
$s_i}
339 $w_text tag remove misspelled
1.0 end
340 error_popup
[strcat
[mc
"Spell Checker Failed"] "\n\n" $err]
343 fconfigure $s_fd -block 0
345 if {[llength $s_pending] == 0} {
350 proc available_langs
{} {
353 set fd
[open [list | aspell dump dicts
] r
]
354 while {[gets $fd line
] >= 0} {
355 if {$line eq
{}} continue