4 A Programmer's Text Editor
6 Miscellaneous editor actions.
8 Copyright (C) 1991-2007 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['eval'] = sub (d) {
32 { 'label' => L("Code to execute:"),
40 if (! regex('/;\s*$/', t))
46 mp.alert(L("Error: ") ~ ERROR);
51 mp.alert(L("Exit value:\n") ~ t);
55 mp.actions['eval_doc'] = sub (d) {
56 local t = join("\n", d.txt.lines);
62 mp.alert(L("Error: ") ~ ERROR);
64 /* try to move the cursor to the line
65 where the error was */
68 if ((l = regex( [ '/, line /', '/[0-9]+/' ], ERROR)) != NULL)
76 mp.actions['encoding'] = sub (d) {
78 { 'label' => L("Encoding (utf-8, iso8859-1, etc.; empty, current locale)") ~ ':',
80 'history' => 'encoding' }
84 if (encoding(t[0]) == -1)
85 mp.alert(L("Invalid encoding ") ~ t[0]);
88 mp.actions['zoom_in'] = sub (d) {
90 mp.config.font_size++;
94 mp.actions['zoom_out'] = sub (d) {
96 if (mp.config.font_size > 4) {
97 mp.config.font_size--;
102 mp.actions['word_count'] = sub (d) {
105 local c = mp.word_count(d);
108 mp.alert(sprintf(L("Lines: %d Words: %d"),
109 size(mp.get_active_area(d)), c));
113 mp.actions['repeated_words'] = sub (d) {
115 { 'label' => L("Number of letters at the start or end") ~ ':',
118 'history' => 'num_char' },
119 { 'label' => L("Maximum distance") ~ ':',
122 'history' => 'max_dist' }
127 local c = mp.repeated_words(d, r[0], r[1]);
131 mp.alert(L("Text not found."));
136 mp.actions['seek_repeated_word'] = sub (d) {
137 if (!mp.repeated_words(d, mp.config.rw_num_chars, mp.config.rw_distance))
138 mp.alert(L("Text not found."));
142 mp.actions['about'] = sub (d) {
144 "\nMinimum Profit %s - Programmer Text Editor\n\n"\
145 "Components: MPDM %s, MPSL %s\n\n"\
146 "Copyright (C) 1991-2009 Angel Ortega <angel@triptico.com>\n"\
148 "This program is free software; you can redistribute it and/or\n"\
149 "modify it under the terms of the GNU General Public License\n"\
150 "as published by the Free Software Foundation; either version 2\n"\
151 "of the License, or (at your option) any later version.\n"\
153 "This program is distributed in the hope that it will be useful,\n"\
154 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"\
155 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"\
156 "See the GNU General Public License for more details.\n"\
158 "You should have received a copy of the GNU General Public License\n"\
159 "along with this program; if not, write to the Free Software Foundation,\n"\
160 "Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n"\
162 "Home page: http://www.triptico.com/software/mp.html\n"\
163 "Mailing list: mp-subscribe@lists.triptico.com\n");
166 msg = sprintf(msg, mp.VERSION, mpdm.version, MPSL.VERSION);
168 d = mp.open("<about>");
170 if (size(d.txt.lines) == 1) {
177 /** default key bindings **/
179 mp.keycodes['escape'] = 'eval';
180 mp.keycodes['f12'] = 'zoom_in';
181 mp.keycodes['ctrl-kp-plus' ] = 'zoom_in';
182 mp.keycodes['f11'] = 'zoom_out';
183 mp.keycodes['ctrl-kp-minus' ] = 'zoom_out';
184 mp.keycodes['ctrl-f4'] = 'repeated_words';
186 /** action descriptions **/
188 mp.actdesc['eval'] = LL("Execute MPSL code...");
189 mp.actdesc['eval_doc'] = LL("Execute document as MPSL");
190 mp.actdesc['encoding'] = LL("Set charset encoding...");
191 mp.actdesc['zoom_in'] = LL("Bigger font");
192 mp.actdesc['zoom_out'] = LL("Smaller font");
193 mp.actdesc['word_count'] = LL("Count words");
194 mp.actdesc['repeated_words'] = LL("Search repeated words...");
195 mp.actdesc['seek_repeated_word'] = LL("Search repeated word");
196 mp.actdesc['about'] = LL("About...");
200 mp.config.rw_num_chars = 4;
201 mp.config.rw_distance = 40;
206 /* overwrite of the MPSL dump() function, dumping over a text document */
208 local d = mp.open("<dump>");
211 mp.insert(d, dumper(v));
217 sub mp.word_count(doc)
218 /* counts the number of words in doc */
222 foreach (l, mp.get_active_area(doc))
223 w += size(mp.split_by_words(l));
230 * mp.repeated_words - Finds words starting or ending the same in a range.
232 * @num_chars: minimum length for the word to be tested
233 * @max_dist: maximum distance the word must have
235 * Finds words starting or ending the same to a maximum of @num_chars
236 * and that are less than @max_dist words apart. If a pair of these words
237 * is found, 1 is returned, the cursor positioned over the first one
238 * and both highlighted as spelling errors. Otherwise, 0 is returned
239 * and nothing is done.
241 sub mp.repeated_words(doc, num_chars, max_dist)
249 local s_rx = sprintf('/^.{1,%d}/i', num_chars);
250 local e_rx = sprintf('/.{1,%d}$/i', num_chars);
252 /* if there were previous repeated words, no longer
253 consider them as 'typos' */
254 if (mp.last_repeated_words) {
255 hdel(mp.word_color, mp.last_repeated_words[0]);
256 hdel(mp.word_color, mp.last_repeated_words[1]);
259 while ((l = doc.txt.lines[y]) != NULL && !ret) {
263 while ((w = regex(mp.word_regex, l, x)) != NULL) {
265 /* get matching position */
268 /* does the word measure at lest num_chars? */
269 if (size(w) >= num_chars) {
270 /* enqueue this word, and dequeue another */
271 local w1 = queue(q, [
279 /* a word has been dequeued? */
281 /* seek each word in the queue */
283 /* does the word and any other in
284 the queue match the regexes? */
285 if ((w1[3] eq w2[3]) || (w1[4] eq w2[4])) {
287 /* add both to the word color hash */
288 mp.word_color[w1[0]] =
289 mp.word_color[w2[0]] =
290 mp.colors.spell.attr;
292 /* store for later removal */
293 mp.last_repeated_words =
296 /* move cursor there */
297 mp.set_y(doc, w1[2]);
298 mp.set_x(doc, w1[1]);
300 /* set the first visible line */
301 doc.txt.vy = doc.txt.y;
303 /* trigger a redraw */
312 /* move offset to next word */