4 A Programmer's Text Editor
8 Copyright (C) 1991-2011 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) {
41 mp.actions['delete_line'] = sub (d) { mp.store_undo(d); mp.delete_line(d); };
42 mp.actions['insert_space'] = sub (d) { mp.store_undo(d); mp.insert_space(d); };
43 mp.actions['insert_tab'] = sub (d) { mp.store_undo(d); mp.insert_tab(d); };
44 mp.actions['insert_real_tab'] = sub (d) { mp.store_undo(d); mp.insert(d, "\t"); };
45 mp.actions['delete'] = sub (d) { mp.store_undo(d); mp.delete_char(d); };
47 mp.actions['delete_left'] = sub (d) {
48 if (d.txt.x + d.txt.y) {
57 mp.actions['indent_block'] = sub (d) {
60 if (d.txt.mark == NULL) {
61 mp.move(d, mp.move_bol);
65 local currentY = d.txt.y;
66 local startY = d.txt.mark.by;
67 local endY = d.txt.mark.ey;
68 local times = endY - startY;
74 /* use to be while d.txt.y <= endY, but that entered an endless loop when
75 you were indenting a block including the very last line in the file */
77 mp.move(d, mp.move_bol);
79 mp.move(d, mp.move_down);
85 mp.move(d, mp.move_bol);
89 mp.move(d.mp_move_eol);
92 mp.set_y(d, currentY);
98 mp.actions['unindent_block'] = sub(d) {
101 if (d.txt.mark == NULL) {
105 local currentY = d.txt.y;
106 local startY = d.txt.mark.by;
107 local endY = d.txt.mark.ey;
108 local times = endY - startY;
114 /* use to be while d.txt.y <= endY, but that entered an endless loop when
115 you were unindenting a block including the very last line in the file */
118 mp.move(d, mp.move_down);
124 mp.move(d, mp.move_bol);
128 mp.move(d.mp_move_eol);
131 mp.set_y(d, currentY);
137 mp.actions['undo'] = sub (d) { mp.undo(d); };
138 mp.actions['redo'] = sub (d) { mp.redo(d); };
140 mp.actions['join_paragraph'] = sub (d) {
147 /* create a working document */
148 local p = mp.create('<wrk>', mp.clipboard);
150 /* while not at EOF, word wrap everything */
151 while (p.txt.y < size(p.txt.lines) - 1) {
152 mp.join_paragraph(p);
157 /* insert the content */
158 mp.insert(d, p.txt.lines);
162 mp.join_paragraph(d);
167 mp.actions['word_wrap_paragraph'] = sub (d) {
169 if(mp.config.word_wrap == 0)
170 mp.alert(L("Word wrapping must be set"));
178 /* create a working document */
179 local p = mp.create('<wrk>', mp.clipboard);
181 /* while not at EOF, word wrap everything */
182 while (p.txt.y < size(p.txt.lines) - 1) {
183 mp.word_wrap_paragraph(p);
188 /* insert the content */
189 mp.insert(d, p.txt.lines);
193 mp.word_wrap_paragraph(d);
199 mp.actions['line_options'] = sub (d) {
201 /* convert special characters on end of line */
202 local lt = mp.backslash_codes(mp.config.eol, 1);
207 label: L("Word wrap on column (0, no word wrap):"),
209 value: mp.config.word_wrap,
213 label: L("Automatic indentation") ~ ':',
215 value: mp.config.auto_indent
218 label: L("Line termination") ~ ':',
223 label: L("Keep original end of lines") ~ ':',
224 value: mp.config.keep_eol,
228 label: L("Mark end of lines") ~ ':',
229 value: mp.config.mark_eol,
233 label: L("'Smart' move to beginning of line:"),
234 value: mp.config.smart_bol,
241 mp.config.word_wrap = t[0];
242 mp.config.auto_indent = t[1];
243 mp.config.eol = mp.backslash_codes(t[2], 0);
244 mp.config.keep_eol = t[3];
245 mp.config.mark_eol = t[4];
246 mp.config.smart_bol = t[5];
250 mp.actions['tab_options'] = sub (d) {
253 { 'label' => L("Tab size") ~ ':',
255 'value' => mp.config.tab_size,
256 'history' => 'tabsize' },
257 { 'label' => L("Convert tabs to spaces") ~ ':',
258 'type' => 'checkbox',
259 'value' => mp.config.tabs_as_spaces },
260 { 'label' => L("Use previous line for tab columns") ~ ':',
261 'type' => 'checkbox',
262 'value' => mp.config.dynamic_tabs }
266 mp.config.tab_size = t[0];
267 mp.config.tabs_as_spaces = t[1];
268 mp.config.dynamic_tabs = t[2];
272 mp.actions['toggle_insert'] = sub (d) { mp.config.insert = !mp.config.insert; };
274 /** default key bindings **/
276 mp.keycodes['enter'] = "insert_line";
277 mp.keycodes['tab'] = "insert_tab";
278 mp.keycodes['shift-tab'] = "insert_real_tab";
279 mp.keycodes['space'] = "insert_space";
280 mp.keycodes['delete'] = "delete";
281 mp.keycodes['backspace'] = "delete_left";
282 mp.keycodes['ctrl-i'] = "insert_tab";
283 mp.keycodes['ctrl-m'] = "insert_line";
284 mp.keycodes['ctrl-y'] = "delete_line";
285 mp.keycodes['alt-cursor-right'] = "indent_block";
286 mp.keycodes['alt-cursor-left'] = "unindent_block";
287 mp.keycodes['ctrl-z'] = "undo";
288 mp.keycodes['f4'] = "word_wrap_paragraph";
289 mp.keycodes['insert'] = "toggle_insert";
291 /** action descriptions **/
293 mp.actdesc['insert_line'] = LL("Insert line");
294 mp.actdesc['delete_line'] = LL("Delete line");
295 mp.actdesc['insert_space'] = LL("Insert space");
296 mp.actdesc['insert_tab'] = LL("Insert tab");
297 mp.actdesc['insert_real_tab'] = LL("Insert real tab character");
298 mp.actdesc['delete'] = LL("Delete character");
299 mp.actdesc['delete_left'] = LL("Delete character to the left");
300 mp.actdesc['indent_block'] = LL("Indent block");
301 mp.actdesc['unindent_block'] = LL("Unindent block");
302 mp.actdesc['undo'] = LL("Undo");
303 mp.actdesc['redo'] = LL("Redo");
304 mp.actdesc['join_paragraph'] = LL("Join a paragraph in one line");
305 mp.actdesc['word_wrap_paragraph'] = LL("Word-wrap a paragraph");
306 mp.actdesc['line_options'] = LL("Line options...");
307 mp.actdesc['tab_options'] = LL("Tab options...");
308 mp.actdesc['toggle_insert'] = LL("Toggle insert/overwrite mode");
313 * mp.break_line - Breaks current line in two (inserts a newline).
315 * @col: column where the newline will be inserted
317 * Breaks current line in two by inserting a newline character in between.
318 * If @col is not NULL, the newline will be inserted in that column; otherwise,
319 * the current x position will be used.
321 sub mp.break_line(doc, col)
326 /* if col is NULL, set it to be the x cursor */
330 /* gets line where cursor is */
331 c = txt.lines[txt.y];
333 /* deletes from col to the end of line */
334 w = splice(c, NULL, col, -1);
336 /* set first part as current line */
337 txt.lines[txt.y] = w[0];
339 /* move to next line */
342 /* insert a new line here */
343 expand(txt.lines, txt.y, 1);
345 /* fix the x cursor position */
348 /* if autoindenting... */
349 if (mp.config.auto_indent) {
350 /* extract leading blanks in the original line
351 to prepend them to the line to be inserted */
352 local i = regex(c, "/^[ \t]*[-\+\*]?[ \t]+/", 0);
354 /* substitute all non-tab characters with spaces */
355 i = sregex(i, "/[^\t]/g", " ");
357 /* delete any blank in the new line */
358 w[1] = sregex(w[1], "/^[ \t]*/");
363 /* the x position is further the length of that */
367 /* put second part there (or an empty string if NULL) */
368 txt.lines[txt.y] = w[1] || '';
376 sub mp.join_line(doc)
377 /* joins the current line with the next one */
381 if (txt.y < size(txt.lines)) {
382 /* concats current line with the next one */
383 txt.lines[txt.y] = txt.lines[txt.y] ~ txt.lines[txt.y + 1];
386 adel(txt.lines, txt.y + 1);
395 sub mp.delete_line(doc)
396 /* deletes the current line */
401 /* take current position */
402 vx = mp.x2vx(txt.lines[txt.y], txt.x);
404 /* if it's the only line, just replace it */
405 if (size(txt.lines) == 1)
408 /* destroy the line */
409 adel(txt.lines, txt.y);
412 /* fix if it was the last line */
413 if (txt.y >= size(txt.lines))
414 txt.y = size(txt.lines) - 1;
416 /* move to previous x position */
417 txt.x = mp.vx2x(txt.lines[txt.y], vx);
425 sub mp.delete_char(doc)
426 /* deletes the current char */
430 if (txt.mark != NULL) {
435 /* is it over the end of line? */
436 if (txt.x == size(txt.lines[txt.y]))
441 w = splice(txt.lines[txt.y], NULL, txt.x, 1);
442 txt.lines[txt.y] = w[0];
451 sub mp.delete_range(doc, bx, by, ex, ey, v)
452 /* deletes a range of characters from a document */
456 /* move to the start of the range */
463 /* block is just one line; delete the middle part */
464 w = splice(txt.lines[by], NULL, bx, ex - bx);
466 txt.lines[by] = w[0];
469 /* block has more than one line */
473 /* delete using normal selection block */
475 /* delete from the beginning to the end of the first line */
476 w = splice(txt.lines[by], NULL, bx, -1);
477 txt.lines[by] = w[0];
479 /* delete from the beginning of the last line to
480 the end of the block */
481 w = splice(txt.lines[ey], NULL, 0, ex);
482 txt.lines[ey] = w[0];
484 /* collapse the lines in between */
485 collapse(txt.lines, by + 1, ey - by - 1);
487 /* finally join both lines */
491 /* delete using vertical selection block */
493 w = splice(txt.lines[by], NULL, bx, ex - bx);
494 txt.lines[by] = w[0];
506 sub mp.insert_string(doc, str)
507 /* inserts a string into the cursor position */
514 /* splice and change */
515 w = splice(txt.lines[txt.y], str, txt.x, mp.config.insert && size(str) || 0);
516 txt.lines[txt.y] = w[0];
527 sub mp.insert(doc, a)
528 /* inserts an array of text into a document */
533 /* if a is not an array, split it */
537 /* empty array? return */
538 if ((s = size(a)) == 0)
541 /* paste first line into current position */
542 mp.insert_string(doc, a[0]);
544 /* more than just one line? */
546 /* break current line in two */
549 /* insert last line */
550 mp.insert_string(doc, a[s - 1]);
553 /* more than two lines? */
558 expand(txt.lines, txt.y, s - 2);
560 /* transfer middle lines */
562 txt.lines[txt.y++] = a[n++];
569 sub mp.wrap_words(doc)
570 /* do the word wrapping */
574 if (mp.config.word_wrap == 0)
577 /* take the column where the cursor is */
578 local c = mp.x2vx(txt.lines[txt.y], txt.x);
580 if (c >= mp.config.word_wrap &&
581 regex(txt.lines[txt.y], "/^.{1," ~ mp.config.word_wrap ~ "}[ \t]/")) {
584 /* take the coordinates */
587 /* break the line there */
588 mp.break_line(doc, w[1]);
590 /* delete the space at the end of the line */
591 txt.lines[txt.y - 1] = sregex(txt.lines[txt.y - 1], "/[ \t]$/", NULL);
598 sub mp.insert_space(doc)
599 /* inserts a space, taking wordwrapping into account */
606 sub mp.insert_tab(doc)
609 if (doc.txt.y && mp.config.dynamic_tabs) {
610 local pl = doc.txt.lines[doc.txt.y - 1];
612 if ((pl = regex(pl, "/[^ \t]*[ \t]+/", doc.txt.x)) != NULL) {
613 pl = sregex(pl, "/[^\t]/g", ' ');
619 if (mp.config.tabs_as_spaces) {
620 /* number of spaces to insert */
621 local n = mp.config.tab_size -
622 ((doc.txt.x) % mp.config.tab_size);
624 while(n--) mp.insert(doc, ' ');
627 mp.insert(doc, "\t");
633 sub mp.insert_newline(doc)
634 /* inserts a newline */
641 sub mp.insert_keystroke(doc, key)
642 /* inserts from a keystroke (with undo) */
644 if (size(key) == 1) {
651 'timeout' => time() + 2,
652 'string' => sprintf(L("Unbound keystroke '%s'"), key)
662 sub mp.store_undo(doc)
663 /* stores the current txt in the undo queue */
665 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
673 /* undoes last operation */
677 if (txt = pop(doc.undo)) {
678 queue(doc.redo, clone(doc.txt), mp.config.undo_levels);
687 /* redoes last undid operation */
691 if (txt = pop(doc.redo)) {
692 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
702 sub mp.join_paragraph(doc)
703 /* joins current paragraph in just one line */
708 while ((l = txt.lines[txt.y + 1]) && size(l)) {
709 /* delete all leading blanks in the next line */
710 txt.lines[txt.y + 1] = sregex(txt.lines[txt.y + 1], "/^[ \t]+/");
712 /* move to end of line and add a space separator */
724 sub mp.word_wrap_paragraph(doc)
725 /* word wraps current paragraph */
729 if (mp.config.word_wrap == 0)
732 mp.join_paragraph(doc);
736 while (size(txt.lines[txt.y]) > mp.config.word_wrap) {
737 mp.insert_space(doc);
745 /* indent/unindent support functions */
747 sub mp.unindent_line(d)
748 /* Unindent the current line by 1 tab or the indent size */
750 local l = split(d.txt.lines[d.txt.y]);
752 mp.move(d, mp.move_bol);
754 if (cmp(l[0], "\t") == 0) {
758 while (i < mp.config.tab_size) {
759 if (cmp(l[i], " ") == 0) {