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
29 /** editor actions **/
31 mp.actions['insert_line'] = sub (d) {
35 if (d.syntax == NULL) mp.detect_syntax(d);
38 mp.actions['delete_line'] = sub (d) { mp.store_undo(d); mp.delete_line(d); };
39 mp.actions['insert_space'] = sub (d) { mp.store_undo(d); mp.insert_space(d); };
40 mp.actions['insert_tab'] = sub (d) { mp.store_undo(d); mp.insert_tab(d); };
41 mp.actions['delete'] = sub (d) { mp.store_undo(d); mp.delete_char(d); };
42 mp.actions['delete_left'] = sub (d) { mp.store_undo(d); mp.move_left(d) &&
44 mp.actions['undo'] = sub (d) { mp.undo(d); };
45 mp.actions['redo'] = sub (d) { mp.redo(d); };
46 mp.actions['join_paragraph'] = sub (d) { mp.store_undo(d); mp.join_paragraph(d); };
48 mp.actions['word_wrap_paragraph'] = sub (d) {
50 if(mp.config.word_wrap == 0)
51 mp.alert(L("Word wrapping must be set"));
54 mp.word_wrap_paragraph(d);
58 mp.actions['line_options'] = sub (d) {
60 /* convert special characters on end of line */
61 local lt = mp.backslash_codes(mp.config.eol, 1);
64 { 'label' => L("Word wrap on column (0, no word wrap):"),
66 'value' => mp.config.word_wrap,
67 'history' => 'wordwrap' },
68 { 'label' => L("Automatic indentation") ~ ':',
70 'value' => mp.config.auto_indent },
71 { 'label' => L("Line termination") ~ ':',
74 { 'label' => L("Mark end of lines") ~ ':',
75 'value' => mp.config.mark_eol,
76 'type' => 'checkbox' }
80 mp.config.word_wrap = t[0];
81 mp.config.auto_indent = t[1];
82 mp.config.eol = mp.backslash_codes(t[2], 0);
83 mp.config.mark_eol = t[3];
87 mp.actions['tab_options'] = sub (d) {
90 { 'label' => L("Tab size") ~ ':',
92 'value' => mp.config.tab_size,
93 'history' => 'tabsize' },
94 { 'label' => L("Convert tabs to spaces") ~ ':',
96 'value' => mp.config.tabs_as_spaces }
100 mp.config.tab_size = t[0];
101 mp.config.tabs_as_spaces = t[1];
105 /** default key bindings **/
107 mp.keycodes['enter'] = "insert_line";
108 mp.keycodes['tab'] = "insert_tab";
109 mp.keycodes['space'] = "insert_space";
110 mp.keycodes['delete'] = "delete";
111 mp.keycodes['backspace']= "delete_left";
112 mp.keycodes['ctrl-i'] = "insert_tab";
113 mp.keycodes['ctrl-m'] = "insert_line";
114 mp.keycodes['ctrl-y'] = "delete_line";
115 mp.keycodes['ctrl-z'] = "undo";
116 mp.keycodes['f4'] = "word_wrap_paragraph";
118 /** action descriptions **/
120 mp.actdesc['insert_line'] = LL("Insert line");
121 mp.actdesc['delete_line'] = LL("Delete line");
122 mp.actdesc['insert_space'] = LL("Insert space");
123 mp.actdesc['insert_tab'] = LL("Insert tab");
124 mp.actdesc['delete'] = LL("Delete character");
125 mp.actdesc['delete_left'] = LL("Delete character to the left");
126 mp.actdesc['undo'] = LL("Undo");
127 mp.actdesc['redo'] = LL("Redo");
128 mp.actdesc['join_paragraph'] = LL("Join a paragraph in one line");
129 mp.actdesc['word_wrap_paragraph'] = LL("Word-wrap a paragraph");
130 mp.actdesc['line_options'] = LL("Line options...");
131 mp.actdesc['tab_options'] = LL("Tab options...");
136 * mp.break_line - Breaks current line in two (inserts a newline).
138 * @col: column where the newline will be inserted
140 * Breaks current line in two by inserting a newline character in between.
141 * If @col is not NULL, the newline will be inserted in that column; otherwise,
142 * the current x position will be used.
144 sub mp.break_line(doc, col)
149 /* if col is NULL, set it to be the x cursor */
153 /* gets line where cursor is */
154 c = txt.lines[txt.y];
156 /* deletes from col to the end of line */
157 w = splice(c, NULL, col, -1);
159 /* set first part as current line */
160 txt.lines[txt.y] = w[0];
162 /* move to next line */
165 /* insert a new line here */
166 expand(txt.lines, txt.y, 1);
168 /* fix the x cursor position */
171 /* if autoindenting... */
172 if (mp.config.auto_indent) {
173 /* extract leading blanks in the original line
174 to prepend them to the line to be inserted */
175 local i = regex("/^[ \t]*[-\+\*]?[ \t]+/", c, 0);
177 /* substitute all non-tab characters with spaces */
178 i = sregex("/[^\t]/g", i, " ");
180 /* delete any blank in the new line */
181 w[1] = sregex("/^[ \t]*/", w[1]);
186 /* the x position is further the length of that */
190 /* put second part there (or an empty string if NULL) */
191 txt.lines[txt.y] = w[1] || '';
197 sub mp.join_line(doc)
198 /* joins the current line with the next one */
202 if (txt.y < size(txt.lines)) {
203 /* concats current line with the next one */
204 txt.lines[txt.y] = txt.lines[txt.y] ~ txt.lines[txt.y + 1];
207 adel(txt.lines, txt.y + 1);
214 sub mp.delete_line(doc)
215 /* deletes the current line */
220 /* take current position */
221 vx = mp.x2vx(txt.lines[txt.y], txt.x);
223 /* if it's the only line, just replace it */
224 if (size(txt.lines) == 1)
227 /* destroy the line */
228 adel(txt.lines, txt.y);
231 /* fix if it was the last line */
232 if (txt.y >= size(txt.lines))
233 txt.y = size(txt.lines) - 1;
235 /* move to previous x position */
236 txt.x = mp.vx2x(txt.lines[txt.y], vx);
242 sub mp.delete_char(doc)
243 /* deletes the current char */
247 /* is it over the end of line? */
248 if (txt.x == size(txt.lines[txt.y]))
253 w = splice(txt.lines[txt.y], NULL, txt.x, 1);
254 txt.lines[txt.y] = w[0];
261 sub mp.delete_range(doc, bx, by, ex, ey, v)
262 /* deletes a range of characters from a document */
266 /* move to the start of the range */
273 /* block is just one line; delete the middle part */
274 w = splice(txt.lines[by], NULL, bx, ex - bx);
276 txt.lines[by] = w[0];
279 /* block has more than one line */
283 /* delete using normal selection block */
285 /* delete from the beginning to the end of the first line */
286 w = splice(txt.lines[by], NULL, bx, -1);
287 txt.lines[by] = w[0];
289 /* delete from the beginning of the last line to
290 the end of the block */
291 w = splice(txt.lines[ey], NULL, 0, ex);
292 txt.lines[ey] = w[0];
294 /* collapse the lines in between */
295 collapse(txt.lines, by + 1, ey - by - 1);
297 /* finally join both lines */
301 /* delete using vertical selection block */
303 w = splice(txt.lines[by], NULL, bx, ex - bx);
304 txt.lines[by] = w[0];
314 sub mp.insert_string(doc, str)
315 /* inserts a string into the cursor position */
320 /* splice and change */
321 w = splice(txt.lines[txt.y], str, txt.x, 0);
322 txt.lines[txt.y] = w[0];
331 sub mp.insert(doc, a)
332 /* inserts an array of text into a document */
337 /* if a is not an array, split it */
341 /* empty array? return */
342 if ((s = size(a)) == 0)
345 /* paste first line into current position */
346 mp.insert_string(doc, a[0]);
348 /* more than just one line? */
350 /* break current line in two */
353 /* insert last line */
354 mp.insert_string(doc, a[s - 1]);
357 /* more than two lines? */
362 expand(txt.lines, txt.y, s - 2);
364 /* transfer middle lines */
366 txt.lines[txt.y++] = a[n++];
371 sub mp.wrap_words(doc)
372 /* do the word wrapping */
376 if (mp.config.word_wrap == 0)
379 /* take the column where the cursor is */
380 local c = mp.x2vx(txt.lines[txt.y], txt.x);
382 if (c >= mp.config.word_wrap &&
383 regex("/^.{1," ~ mp.config.word_wrap ~ "}[ \t]/", txt.lines[txt.y])) {
386 /* take the coordinates */
389 /* break the line there */
390 mp.break_line(doc, w[1]);
392 /* delete the space at the end of the line */
393 txt.lines[txt.y - 1] = sregex("/[ \t]$/", txt.lines[txt.y - 1], NULL);
398 sub mp.insert_space(doc)
399 /* inserts a space, taking wordwrapping into account */
408 sub mp.insert_tab(doc)
411 if (mp.config.tabs_as_spaces) {
412 /* number of spaces to insert */
413 local n = mp.config.tab_size -
414 ((doc.txt.x) % mp.config.tab_size);
416 while(n--) mp.insert(doc, ' ');
419 mp.insert(doc, "\t");
423 sub mp.insert_newline(doc)
424 /* inserts a newline */
431 sub mp.insert_keystroke(doc, key)
432 /* inserts from a keystroke (with undo) */
434 if (size(key) == 1) {
442 'timeout' => time() + 2,
443 'string' => sprintf(L("Unbound keystroke '%s'"), key)
451 sub mp.store_undo(doc)
452 /* stores the current txt in the undo queue */
454 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
460 /* undoes last operation */
464 if (txt = pop(doc.undo)) {
465 queue(doc.redo, clone(doc.txt), mp.config.undo_levels);
472 /* redoes last undid operation */
476 if (txt = pop(doc.redo)) {
477 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
484 sub mp.join_paragraph(doc)
485 /* joins current paragraph in just one line */
490 while ((l = txt.lines[txt.y + 1]) && size(l)) {
491 /* delete all leading blanks in the next line */
492 txt.lines[txt.y + 1] = sregex("/^[ \t]+/", txt.lines[txt.y + 1]);
494 /* move to end of line and add a space separator */
504 sub mp.word_wrap_paragraph(doc)
505 /* word wraps current paragraph */
509 if (mp.config.word_wrap == 0)
512 mp.join_paragraph(doc);
516 while (size(txt.lines[txt.y]) > mp.config.word_wrap) {
517 mp.insert_space(doc);