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);
205 { 'label' => L("Word wrap on column (0, no word wrap):"),
207 'value' => mp.config.word_wrap,
208 'history' => 'wordwrap' },
209 { 'label' => L("Automatic indentation") ~ ':',
210 'type' => 'checkbox',
211 'value' => mp.config.auto_indent },
212 { 'label' => L("Line termination") ~ ':',
215 { 'label' => L("Keep original end of lines") ~ ':',
216 'value' => mp.config.keep_eol,
217 'type' => 'checkbox' },
218 { 'label' => L("Mark end of lines") ~ ':',
219 'value' => mp.config.mark_eol,
220 'type' => 'checkbox' }
224 mp.config.word_wrap = t[0];
225 mp.config.auto_indent = t[1];
226 mp.config.eol = mp.backslash_codes(t[2], 0);
227 mp.config.keep_eol = t[3];
228 mp.config.mark_eol = t[4];
232 mp.actions['tab_options'] = sub (d) {
235 { 'label' => L("Tab size") ~ ':',
237 'value' => mp.config.tab_size,
238 'history' => 'tabsize' },
239 { 'label' => L("Convert tabs to spaces") ~ ':',
240 'type' => 'checkbox',
241 'value' => mp.config.tabs_as_spaces },
242 { 'label' => L("Use previous line for tab columns") ~ ':',
243 'type' => 'checkbox',
244 'value' => mp.config.dynamic_tabs }
248 mp.config.tab_size = t[0];
249 mp.config.tabs_as_spaces = t[1];
250 mp.config.dynamic_tabs = t[2];
254 mp.actions['toggle_insert'] = sub (d) { mp.config.insert = !mp.config.insert; };
256 /** default key bindings **/
258 mp.keycodes['enter'] = "insert_line";
259 mp.keycodes['tab'] = "insert_tab";
260 mp.keycodes['shift-tab'] = "insert_real_tab";
261 mp.keycodes['space'] = "insert_space";
262 mp.keycodes['delete'] = "delete";
263 mp.keycodes['backspace'] = "delete_left";
264 mp.keycodes['ctrl-i'] = "insert_tab";
265 mp.keycodes['ctrl-m'] = "insert_line";
266 mp.keycodes['ctrl-y'] = "delete_line";
267 mp.keycodes['alt-cursor-right'] = "indent_block";
268 mp.keycodes['alt-cursor-left'] = "unindent_block";
269 mp.keycodes['ctrl-z'] = "undo";
270 mp.keycodes['f4'] = "word_wrap_paragraph";
271 mp.keycodes['insert'] = "toggle_insert";
273 /** action descriptions **/
275 mp.actdesc['insert_line'] = LL("Insert line");
276 mp.actdesc['delete_line'] = LL("Delete line");
277 mp.actdesc['insert_space'] = LL("Insert space");
278 mp.actdesc['insert_tab'] = LL("Insert tab");
279 mp.actdesc['insert_real_tab'] = LL("Insert real tab character");
280 mp.actdesc['delete'] = LL("Delete character");
281 mp.actdesc['delete_left'] = LL("Delete character to the left");
282 mp.actdesc['indent_block'] = LL("Indent block");
283 mp.actdesc['unindent_block'] = LL("Unindent block");
284 mp.actdesc['undo'] = LL("Undo");
285 mp.actdesc['redo'] = LL("Redo");
286 mp.actdesc['join_paragraph'] = LL("Join a paragraph in one line");
287 mp.actdesc['word_wrap_paragraph'] = LL("Word-wrap a paragraph");
288 mp.actdesc['line_options'] = LL("Line options...");
289 mp.actdesc['tab_options'] = LL("Tab options...");
290 mp.actdesc['toggle_insert'] = LL("Toggle insert/overwrite mode");
295 * mp.break_line - Breaks current line in two (inserts a newline).
297 * @col: column where the newline will be inserted
299 * Breaks current line in two by inserting a newline character in between.
300 * If @col is not NULL, the newline will be inserted in that column; otherwise,
301 * the current x position will be used.
303 sub mp.break_line(doc, col)
308 /* if col is NULL, set it to be the x cursor */
312 /* gets line where cursor is */
313 c = txt.lines[txt.y];
315 /* deletes from col to the end of line */
316 w = splice(c, NULL, col, -1);
318 /* set first part as current line */
319 txt.lines[txt.y] = w[0];
321 /* move to next line */
324 /* insert a new line here */
325 expand(txt.lines, txt.y, 1);
327 /* fix the x cursor position */
330 /* if autoindenting... */
331 if (mp.config.auto_indent) {
332 /* extract leading blanks in the original line
333 to prepend them to the line to be inserted */
334 local i = regex(c, "/^[ \t]*[-\+\*]?[ \t]+/", 0);
336 /* substitute all non-tab characters with spaces */
337 i = sregex(i, "/[^\t]/g", " ");
339 /* delete any blank in the new line */
340 w[1] = sregex(w[1], "/^[ \t]*/");
345 /* the x position is further the length of that */
349 /* put second part there (or an empty string if NULL) */
350 txt.lines[txt.y] = w[1] || '';
358 sub mp.join_line(doc)
359 /* joins the current line with the next one */
363 if (txt.y < size(txt.lines)) {
364 /* concats current line with the next one */
365 txt.lines[txt.y] = txt.lines[txt.y] ~ txt.lines[txt.y + 1];
368 adel(txt.lines, txt.y + 1);
377 sub mp.delete_line(doc)
378 /* deletes the current line */
383 /* take current position */
384 vx = mp.x2vx(txt.lines[txt.y], txt.x);
386 /* if it's the only line, just replace it */
387 if (size(txt.lines) == 1)
390 /* destroy the line */
391 adel(txt.lines, txt.y);
394 /* fix if it was the last line */
395 if (txt.y >= size(txt.lines))
396 txt.y = size(txt.lines) - 1;
398 /* move to previous x position */
399 txt.x = mp.vx2x(txt.lines[txt.y], vx);
407 sub mp.delete_char(doc)
408 /* deletes the current char */
412 if (txt.mark != NULL) {
417 /* is it over the end of line? */
418 if (txt.x == size(txt.lines[txt.y]))
423 w = splice(txt.lines[txt.y], NULL, txt.x, 1);
424 txt.lines[txt.y] = w[0];
433 sub mp.delete_range(doc, bx, by, ex, ey, v)
434 /* deletes a range of characters from a document */
438 /* move to the start of the range */
445 /* block is just one line; delete the middle part */
446 w = splice(txt.lines[by], NULL, bx, ex - bx);
448 txt.lines[by] = w[0];
451 /* block has more than one line */
455 /* delete using normal selection block */
457 /* delete from the beginning to the end of the first line */
458 w = splice(txt.lines[by], NULL, bx, -1);
459 txt.lines[by] = w[0];
461 /* delete from the beginning of the last line to
462 the end of the block */
463 w = splice(txt.lines[ey], NULL, 0, ex);
464 txt.lines[ey] = w[0];
466 /* collapse the lines in between */
467 collapse(txt.lines, by + 1, ey - by - 1);
469 /* finally join both lines */
473 /* delete using vertical selection block */
475 w = splice(txt.lines[by], NULL, bx, ex - bx);
476 txt.lines[by] = w[0];
488 sub mp.insert_string(doc, str)
489 /* inserts a string into the cursor position */
496 /* splice and change */
497 w = splice(txt.lines[txt.y], str, txt.x, mp.config.insert && size(str) || 0);
498 txt.lines[txt.y] = w[0];
509 sub mp.insert(doc, a)
510 /* inserts an array of text into a document */
515 /* if a is not an array, split it */
519 /* empty array? return */
520 if ((s = size(a)) == 0)
523 /* paste first line into current position */
524 mp.insert_string(doc, a[0]);
526 /* more than just one line? */
528 /* break current line in two */
531 /* insert last line */
532 mp.insert_string(doc, a[s - 1]);
535 /* more than two lines? */
540 expand(txt.lines, txt.y, s - 2);
542 /* transfer middle lines */
544 txt.lines[txt.y++] = a[n++];
551 sub mp.wrap_words(doc)
552 /* do the word wrapping */
556 if (mp.config.word_wrap == 0)
559 /* take the column where the cursor is */
560 local c = mp.x2vx(txt.lines[txt.y], txt.x);
562 if (c >= mp.config.word_wrap &&
563 regex(txt.lines[txt.y], "/^.{1," ~ mp.config.word_wrap ~ "}[ \t]/")) {
566 /* take the coordinates */
569 /* break the line there */
570 mp.break_line(doc, w[1]);
572 /* delete the space at the end of the line */
573 txt.lines[txt.y - 1] = sregex(txt.lines[txt.y - 1], "/[ \t]$/", NULL);
580 sub mp.insert_space(doc)
581 /* inserts a space, taking wordwrapping into account */
588 sub mp.insert_tab(doc)
591 if (doc.txt.y && mp.config.dynamic_tabs) {
592 local pl = doc.txt.lines[doc.txt.y - 1];
594 if ((pl = regex(pl, "/[^ \t]*[ \t]+/", doc.txt.x)) != NULL) {
595 pl = sregex(pl, "/[^\t]/g", ' ');
601 if (mp.config.tabs_as_spaces) {
602 /* number of spaces to insert */
603 local n = mp.config.tab_size -
604 ((doc.txt.x) % mp.config.tab_size);
606 while(n--) mp.insert(doc, ' ');
609 mp.insert(doc, "\t");
615 sub mp.insert_newline(doc)
616 /* inserts a newline */
623 sub mp.insert_keystroke(doc, key)
624 /* inserts from a keystroke (with undo) */
626 if (size(key) == 1) {
633 'timeout' => time() + 2,
634 'string' => sprintf(L("Unbound keystroke '%s'"), key)
644 sub mp.store_undo(doc)
645 /* stores the current txt in the undo queue */
647 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
655 /* undoes last operation */
659 if (txt = pop(doc.undo)) {
660 queue(doc.redo, clone(doc.txt), mp.config.undo_levels);
669 /* redoes last undid operation */
673 if (txt = pop(doc.redo)) {
674 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
684 sub mp.join_paragraph(doc)
685 /* joins current paragraph in just one line */
690 while ((l = txt.lines[txt.y + 1]) && size(l)) {
691 /* delete all leading blanks in the next line */
692 txt.lines[txt.y + 1] = sregex(txt.lines[txt.y + 1], "/^[ \t]+/");
694 /* move to end of line and add a space separator */
706 sub mp.word_wrap_paragraph(doc)
707 /* word wraps current paragraph */
711 if (mp.config.word_wrap == 0)
714 mp.join_paragraph(doc);
718 while (size(txt.lines[txt.y]) > mp.config.word_wrap) {
719 mp.insert_space(doc);
727 /* indent/unindent support functions */
729 sub mp.unindent_line(d)
730 /* Unindent the current line by 1 tab or the indent size */
732 local l = split(d.txt.lines[d.txt.y]);
734 mp.move(d, mp.move_bol);
736 if (cmp(l[0], "\t") == 0) {
740 while (i < mp.config.tab_size) {
741 if (cmp(l[i], " ") == 0) {