4 A Programmer's Text Editor
8 Copyright (C) 1991-2009 Angel Ortega <angel@triptico.com>
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 http://www.triptico.com
28 /** editor actions **/
30 mp.actions['toggle_spellcheck'] = sub(d) { mp.ispell(-1); };
31 mp.actions['seek_misspelled'] = sub(d) { mp.long_op(mp.search_misspelled, d) ||
32 mp.alert(L("Text not found."));
34 mp.actions['ignore_last_misspell'] = sub(d) { mp.ignore_last_misspell(); };
36 /** default key bindings **/
38 mp.keycodes['ctrl-f7'] = 'seek_misspelled';
40 /** action descriptions **/
42 mp.actdesc['toggle_spellcheck'] = LL("Toggle spellchecking");
43 mp.actdesc['seek_misspelled'] = LL("Search misspelled word");
44 mp.actdesc['ignore_last_misspell'] = LL("Ignore last misspelled word");
48 /* spellchecking command */
49 mp.config.ispell_cmd = "ispell -a";
51 /* the spelling cache */
52 mp.spelling_cache = {};
56 sub mp.open_ispell_pipe
57 /* opens the pipe to ispell */
62 if ((p = popen(mp.config.ispell_cmd, "r+")) == NULL)
65 /* read the first line */
68 /* check for the signature */
69 if (! regex('/^@\(#\) International Ispell/', l)) {
79 sub mp.close_ispell_pipe
80 /* closes the pipe to ispell */
82 if (mp.ispell_pipe == NULL)
85 /* close and delete */
86 pclose(mp.ispell_pipe);
87 mp.ispell_pipe = NULL;
92 * mp.is_word_misspelled - Tests if a word is misspelled.
95 * Tests if a word is misspelled. Returns a negative value if
96 * there was an error when querying the external spelling program,
97 * 1 if the word is misspelled, or 0 if not.
99 sub mp.is_word_misspelled(w)
104 if (exists(mp.spelling_cache, w))
105 return mp.spelling_cache[w];
107 if (mp.ispell_pipe == NULL) {
108 if (mp.open_ispell_pipe() == NULL) {
113 /* NULL is never misspelled */
118 write(mp.ispell_pipe, w ~ "\n");
120 /* wait for the response */
121 if ((l = read(mp.ispell_pipe)) == NULL) {
122 mp.close_ispell_pipe();
128 /* drop all lines until an empty one */
130 t = read(mp.ispell_pipe);
132 /* take first char of the response */
133 l = regex('/^./', l);
135 /* if it's not a '*' nor a '+', it's misspelled */
136 if (l ne '*' && l ne '+')
139 mp.spelling_cache[w] = ret;
145 sub mp.ispell_word_color_func(w)
146 /* mp.word_color_func() for ispell */
152 /* attributes must exist before entering here */
153 if (mp.colors.spell.attr != NULL) {
154 ret = mp.is_word_misspelled(w);
156 /* error? disable further misspelling color */
158 mp.word_color_func = NULL;
161 a = mp.colors.spell.attr;
169 * mp.ispell - Changes spelling highlight.
172 * Changes the status of the highlighting of misspelled words.
173 * If @b is 0, it's disabled (default value); if it's 1,
174 * misspelled words will be highlighted using a special
175 * attribute color. If @b is -1, the status is toggled.
180 b = mp.word_color_func == NULL && 1 || 0;
182 if (b && mp.is_word_misspelled() != -1)
183 mp.word_color_func = mp.ispell_word_color_func;
185 mp.word_color_func = NULL;
190 * mp.search_misspelled - Searches for the next misspelled word.
191 * @doc: the document to search
193 * Searches the document for the next misspelled word. If no
194 * more misspelled words can be found, returns 0 and does nothing;
195 * otherwise, the cursor is moved just after that word and
198 sub mp.search_misspelled(doc)
204 while (y < size(txt.lines)) {
205 local l = txt.lines[y];
208 while ((w = regex(mp.word_regex, l, x)) != NULL) {
210 local r = mp.is_word_misspelled(w);
212 /* error? fail immediately */
219 /* store word for later */
220 mp.last_misspelled_word = w;
237 * mp.ignore_last_misspell - Ignores last misspelled word.
239 * Ignores the last misspelled word found by mp.search_misspelled()
240 * by adding it to a whitelist, so it won't be found again.
242 sub mp.ignore_last_misspell()
244 if (mp.last_misspelled_word)
245 mp.spelling_cache[mp.last_misspelled_word] = 0;