4 A Programmer's Text Editor
8 Copyright (C) 1991-2010 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['insert_real_tab'] = sub (d) { mp.store_undo(d); mp.insert(d, "\t"); };
42 mp.actions['delete'] = sub (d) { mp.store_undo(d); mp.delete_char(d); };
44 mp.actions['delete_left'] = sub (d) {
45 if (d.txt.x + d.txt.y) {
52 mp.actions['undo'] = sub (d) { mp.undo(d); };
53 mp.actions['redo'] = sub (d) { mp.redo(d); };
55 mp.actions['join_paragraph'] = sub (d) {
62 /* create a working document */
63 local p = mp.create('<wrk>', mp.clipboard);
65 /* while not at EOF, word wrap everything */
66 while (p.txt.y < size(p.txt.lines) - 1) {
72 /* insert the content */
73 mp.insert(d, p.txt.lines);
80 mp.actions['word_wrap_paragraph'] = sub (d) {
82 if(mp.config.word_wrap == 0)
83 mp.alert(L("Word wrapping must be set"));
91 /* create a working document */
92 local p = mp.create('<wrk>', mp.clipboard);
94 /* while not at EOF, word wrap everything */
95 while (p.txt.y < size(p.txt.lines) - 1) {
96 mp.word_wrap_paragraph(p);
101 /* insert the content */
102 mp.insert(d, p.txt.lines);
106 mp.word_wrap_paragraph(d);
110 mp.actions['line_options'] = sub (d) {
112 /* convert special characters on end of line */
113 local lt = mp.backslash_codes(mp.config.eol, 1);
116 { 'label' => L("Word wrap on column (0, no word wrap):"),
118 'value' => mp.config.word_wrap,
119 'history' => 'wordwrap' },
120 { 'label' => L("Automatic indentation") ~ ':',
121 'type' => 'checkbox',
122 'value' => mp.config.auto_indent },
123 { 'label' => L("Line termination") ~ ':',
126 { 'label' => L("Keep original end of lines") ~ ':',
127 'value' => mp.config.keep_eol,
128 'type' => 'checkbox' },
129 { 'label' => L("Mark end of lines") ~ ':',
130 'value' => mp.config.mark_eol,
131 'type' => 'checkbox' }
135 mp.config.word_wrap = t[0];
136 mp.config.auto_indent = t[1];
137 mp.config.eol = mp.backslash_codes(t[2], 0);
138 mp.config.keep_eol = t[3];
139 mp.config.mark_eol = t[4];
143 mp.actions['tab_options'] = sub (d) {
146 { 'label' => L("Tab size") ~ ':',
148 'value' => mp.config.tab_size,
149 'history' => 'tabsize' },
150 { 'label' => L("Convert tabs to spaces") ~ ':',
151 'type' => 'checkbox',
152 'value' => mp.config.tabs_as_spaces },
153 { 'label' => L("Use previous line for tab columns") ~ ':',
154 'type' => 'checkbox',
155 'value' => mp.config.dynamic_tabs }
159 mp.config.tab_size = t[0];
160 mp.config.tabs_as_spaces = t[1];
161 mp.config.dynamic_tabs = t[2];
165 mp.actions['toggle_insert'] = sub (d) { mp.config.insert = !mp.config.insert; };
167 /** default key bindings **/
169 mp.keycodes['enter'] = "insert_line";
170 mp.keycodes['tab'] = "insert_tab";
171 mp.keycodes['shift-tab'] = "insert_real_tab";
172 mp.keycodes['space'] = "insert_space";
173 mp.keycodes['delete'] = "delete";
174 mp.keycodes['backspace'] = "delete_left";
175 mp.keycodes['ctrl-i'] = "insert_tab";
176 mp.keycodes['ctrl-m'] = "insert_line";
177 mp.keycodes['ctrl-y'] = "delete_line";
178 mp.keycodes['ctrl-z'] = "undo";
179 mp.keycodes['f4'] = "word_wrap_paragraph";
180 mp.keycodes['insert'] = "toggle_insert";
182 /** action descriptions **/
184 mp.actdesc['insert_line'] = LL("Insert line");
185 mp.actdesc['delete_line'] = LL("Delete line");
186 mp.actdesc['insert_space'] = LL("Insert space");
187 mp.actdesc['insert_tab'] = LL("Insert tab");
188 mp.actdesc['insert_real_tab'] = LL("Insert real tab character");
189 mp.actdesc['delete'] = LL("Delete character");
190 mp.actdesc['delete_left'] = LL("Delete character to the left");
191 mp.actdesc['undo'] = LL("Undo");
192 mp.actdesc['redo'] = LL("Redo");
193 mp.actdesc['join_paragraph'] = LL("Join a paragraph in one line");
194 mp.actdesc['word_wrap_paragraph'] = LL("Word-wrap a paragraph");
195 mp.actdesc['line_options'] = LL("Line options...");
196 mp.actdesc['tab_options'] = LL("Tab options...");
197 mp.actdesc['toggle_insert'] = LL("Toggle insert/overwrite mode");
202 * mp.break_line - Breaks current line in two (inserts a newline).
204 * @col: column where the newline will be inserted
206 * Breaks current line in two by inserting a newline character in between.
207 * If @col is not NULL, the newline will be inserted in that column; otherwise,
208 * the current x position will be used.
210 sub mp.break_line(doc, col)
215 /* if col is NULL, set it to be the x cursor */
219 /* gets line where cursor is */
220 c = txt.lines[txt.y];
222 /* deletes from col to the end of line */
223 w = splice(c, NULL, col, -1);
225 /* set first part as current line */
226 txt.lines[txt.y] = w[0];
228 /* move to next line */
231 /* insert a new line here */
232 expand(txt.lines, txt.y, 1);
234 /* fix the x cursor position */
237 /* if autoindenting... */
238 if (mp.config.auto_indent) {
239 /* extract leading blanks in the original line
240 to prepend them to the line to be inserted */
241 local i = regex(c, "/^[ \t]*[-\+\*]?[ \t]+/", 0);
243 /* substitute all non-tab characters with spaces */
244 i = sregex(i, "/[^\t]/g", " ");
246 /* delete any blank in the new line */
247 w[1] = sregex(w[1], "/^[ \t]*/");
252 /* the x position is further the length of that */
256 /* put second part there (or an empty string if NULL) */
257 txt.lines[txt.y] = w[1] || '';
265 sub mp.join_line(doc)
266 /* joins the current line with the next one */
270 if (txt.y < size(txt.lines)) {
271 /* concats current line with the next one */
272 txt.lines[txt.y] = txt.lines[txt.y] ~ txt.lines[txt.y + 1];
275 adel(txt.lines, txt.y + 1);
284 sub mp.delete_line(doc)
285 /* deletes the current line */
290 /* take current position */
291 vx = mp.x2vx(txt.lines[txt.y], txt.x);
293 /* if it's the only line, just replace it */
294 if (size(txt.lines) == 1)
297 /* destroy the line */
298 adel(txt.lines, txt.y);
301 /* fix if it was the last line */
302 if (txt.y >= size(txt.lines))
303 txt.y = size(txt.lines) - 1;
305 /* move to previous x position */
306 txt.x = mp.vx2x(txt.lines[txt.y], vx);
314 sub mp.delete_char(doc)
315 /* deletes the current char */
319 if (txt.mark != NULL) {
324 /* is it over the end of line? */
325 if (txt.x == size(txt.lines[txt.y]))
330 w = splice(txt.lines[txt.y], NULL, txt.x, 1);
331 txt.lines[txt.y] = w[0];
340 sub mp.delete_range(doc, bx, by, ex, ey, v)
341 /* deletes a range of characters from a document */
345 /* move to the start of the range */
352 /* block is just one line; delete the middle part */
353 w = splice(txt.lines[by], NULL, bx, ex - bx);
355 txt.lines[by] = w[0];
358 /* block has more than one line */
362 /* delete using normal selection block */
364 /* delete from the beginning to the end of the first line */
365 w = splice(txt.lines[by], NULL, bx, -1);
366 txt.lines[by] = w[0];
368 /* delete from the beginning of the last line to
369 the end of the block */
370 w = splice(txt.lines[ey], NULL, 0, ex);
371 txt.lines[ey] = w[0];
373 /* collapse the lines in between */
374 collapse(txt.lines, by + 1, ey - by - 1);
376 /* finally join both lines */
380 /* delete using vertical selection block */
382 w = splice(txt.lines[by], NULL, bx, ex - bx);
383 txt.lines[by] = w[0];
395 sub mp.insert_string(doc, str)
396 /* inserts a string into the cursor position */
403 /* splice and change */
404 w = splice(txt.lines[txt.y], str, txt.x, mp.config.insert && size(str) || 0);
405 txt.lines[txt.y] = w[0];
416 sub mp.insert(doc, a)
417 /* inserts an array of text into a document */
422 /* if a is not an array, split it */
426 /* empty array? return */
427 if ((s = size(a)) == 0)
430 /* paste first line into current position */
431 mp.insert_string(doc, a[0]);
433 /* more than just one line? */
435 /* break current line in two */
438 /* insert last line */
439 mp.insert_string(doc, a[s - 1]);
442 /* more than two lines? */
447 expand(txt.lines, txt.y, s - 2);
449 /* transfer middle lines */
451 txt.lines[txt.y++] = a[n++];
458 sub mp.wrap_words(doc)
459 /* do the word wrapping */
463 if (mp.config.word_wrap == 0)
466 /* take the column where the cursor is */
467 local c = mp.x2vx(txt.lines[txt.y], txt.x);
469 if (c >= mp.config.word_wrap &&
470 regex(txt.lines[txt.y], "/^.{1," ~ mp.config.word_wrap ~ "}[ \t]/")) {
473 /* take the coordinates */
476 /* break the line there */
477 mp.break_line(doc, w[1]);
479 /* delete the space at the end of the line */
480 txt.lines[txt.y - 1] = sregex(txt.lines[txt.y - 1], "/[ \t]$/", NULL);
487 sub mp.insert_space(doc)
488 /* inserts a space, taking wordwrapping into account */
495 sub mp.insert_tab(doc)
498 if (doc.txt.y && mp.config.dynamic_tabs) {
499 local pl = doc.txt.lines[doc.txt.y - 1];
501 if ((pl = regex(pl, "/[^ \t]*[ \t]+/", doc.txt.x)) != NULL) {
502 pl = sregex(pl, "/[^\t]/g", ' ');
508 if (mp.config.tabs_as_spaces) {
509 /* number of spaces to insert */
510 local n = mp.config.tab_size -
511 ((doc.txt.x) % mp.config.tab_size);
513 while(n--) mp.insert(doc, ' ');
516 mp.insert(doc, "\t");
522 sub mp.insert_newline(doc)
523 /* inserts a newline */
530 sub mp.insert_keystroke(doc, key)
531 /* inserts from a keystroke (with undo) */
533 if (size(key) == 1) {
540 'timeout' => time() + 2,
541 'string' => sprintf(L("Unbound keystroke '%s'"), key)
551 sub mp.store_undo(doc)
552 /* stores the current txt in the undo queue */
554 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
562 /* undoes last operation */
566 if (txt = pop(doc.undo)) {
567 queue(doc.redo, clone(doc.txt), mp.config.undo_levels);
576 /* redoes last undid operation */
580 if (txt = pop(doc.redo)) {
581 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
591 sub mp.join_paragraph(doc)
592 /* joins current paragraph in just one line */
597 while ((l = txt.lines[txt.y + 1]) && size(l)) {
598 /* delete all leading blanks in the next line */
599 txt.lines[txt.y + 1] = sregex(txt.lines[txt.y + 1], "/^[ \t]+/");
601 /* move to end of line and add a space separator */
613 sub mp.word_wrap_paragraph(doc)
614 /* word wraps current paragraph */
618 if (mp.config.word_wrap == 0)
621 mp.join_paragraph(doc);
625 while (size(txt.lines[txt.y]) > mp.config.word_wrap) {
626 mp.insert_space(doc);