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, lc(w)))
105 return mp.spelling_cache[w];
107 if (mp.ispell_pipe == NULL) {
108 if (mp.open_ispell_pipe() == NULL) {
114 write(mp.ispell_pipe, w ~ "\n");
116 /* wait for the response */
117 if ((l = read(mp.ispell_pipe)) == NULL) {
118 mp.close_ispell_pipe();
124 /* drop all lines until an empty one */
126 t = read(mp.ispell_pipe);
128 /* take first char of the response */
129 l = regex('/^./', l);
131 /* if it's not a '*' nor a '+', it's misspelled */
132 if (l ne '*' && l ne '+')
135 mp.spelling_cache[lc(w)] = ret;
141 sub mp.ispell_word_color_func(w)
142 /* mp.word_color_func() for ispell */
148 /* attributes must exist before entering here */
149 if (mp.colors.spell.attr != NULL) {
150 ret = mp.is_word_misspelled(w);
152 /* error? disable further misspelling color */
154 mp.word_color_func = NULL;
157 a = mp.colors.spell.attr;
165 * mp.ispell - Changes spelling highlight.
168 * Changes the status of the highlighting of misspelled words.
169 * If @b is 0, it's disabled (default value); if it's 1,
170 * misspelled words will be highlighted using a special
171 * attribute color. If @b is -1, the status is toggled.
176 b = mp.word_color_func == NULL && 1 || 0;
178 mp.word_color_func = b && mp.ispell_word_color_func || NULL;
183 * mp.search_misspelled - Searches for the next misspelled word.
184 * @doc: the document to search
186 * Searches the document for the next misspelled word. If no
187 * more misspelled words can be found, returns 0 and does nothing;
188 * otherwise, the cursor is moved just after that word and
191 sub mp.search_misspelled(doc)
197 while (y < size(txt.lines)) {
198 local l = txt.lines[y];
201 while ((w = regex(mp.word_regex, l, x)) != NULL) {
203 local r = mp.is_word_misspelled(w);
205 /* error? fail immediately */
212 /* store word for later */
213 mp.last_misspelled_word = lc(w);
230 * mp.ignore_last_misspell - Ignores last misspelled word.
232 * Ignores the last misspelled word found by mp.search_misspelled()
233 * by adding it to a whitelist, so it won't be found again.
235 sub mp.ignore_last_misspell()
237 if (mp.last_misspelled_word)
238 mp.spelling_cache[mp.last_misspelled_word] = 0;