4 A Programmer's Text Editor
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
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"));
55 mp.word_wrap_paragraph(d);
59 mp.actions['line_options'] = sub (d) {
61 /* convert special characters on end of line */
62 local lt = mp.backslash_codes(mp.config.eol, 1);
65 { 'label' => L("Word wrap on column (0, no word wrap):"),
67 'value' => mp.config.word_wrap,
68 'history' => 'wordwrap' },
69 { 'label' => L("Automatic indentation") ~ ':',
71 'value' => mp.config.auto_indent },
72 { 'label' => L("Line termination") ~ ':',
79 mp.config.word_wrap = t[0];
80 mp.config.auto_indent = t[1];
81 mp.config.eol = mp.backslash_codes(t[2], 0);
85 mp.actions['tab_options'] = sub (d) {
88 { 'label' => L("Tab size") ~ ':',
90 'value' => mp.config.tab_size,
91 'history' => 'tabsize' },
92 { 'label' => L("Convert tabs to spaces") ~ ':',
94 'value' => mp.config.tabs_as_spaces }
99 mp.config.tab_size = t[0];
100 mp.config.tabs_as_spaces = t[1];
104 /* default key bindings */
106 mp.keycodes['enter'] = "insert_line";
107 mp.keycodes['tab'] = "insert_tab";
108 mp.keycodes['space'] = "insert_space";
109 mp.keycodes['delete'] = "delete";
110 mp.keycodes['backspace']= "delete_left";
111 mp.keycodes['ctrl-i'] = "insert_tab";
112 mp.keycodes['ctrl-m'] = "insert_line";
113 mp.keycodes['ctrl-y'] = "delete_line";
114 mp.keycodes['ctrl-z'] = "undo";
115 mp.keycodes['f4'] = "word_wrap_paragraph";
116 mp.keycodes['f6'] = "join_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...");
135 sub mp.break_line(doc, col)
136 /* breaks current line in two (inserts a newline) */
141 /* if col is NULL, set it to be the x cursor */
142 if(col == NULL) col = txt.x;
144 /* gets line where cursor is */
145 c = txt.lines[txt.y];
147 /* deletes from col to the end of line */
148 w = splice(c, NULL, col, -1);
150 /* set first part as current line */
151 txt.lines[txt.y] = w[0];
153 /* move to next line */
156 /* insert a new line here */
157 expand(txt.lines, txt.y, 1);
159 /* fix the x cursor position */
162 /* if autoindenting... */
163 if(mp.config.auto_indent)
165 /* extract leading blanks in the original line
166 to prepend them to the line to be inserted */
167 local i = regex("/^[ \t]*[-\+\*]?[ \t]+/", c, 0);
169 /* substitute all non-tab characters with spaces */
170 i = sregex("/[^\t]/g", i, " ");
172 /* delete any blank in the new line */
173 w[1] = sregex("/^[ \t]*/", w[1]);
178 /* the x position is further the length of that */
182 /* put second part there (or an empty string if NULL) */
183 txt.lines[txt.y] = w[1] || '';
189 sub mp.join_line(doc)
190 /* joins the current line with the next one */
194 if(txt.y < size(txt.lines))
196 /* concats current line with the next one */
197 txt.lines[txt.y] = txt.lines[txt.y] ~ txt.lines[txt.y + 1];
200 adel(txt.lines, txt.y + 1);
207 sub mp.delete_line(doc)
208 /* deletes the current line */
213 /* take current position */
214 vx = mp.x2vx(txt.lines[txt.y], txt.x);
216 /* if it's the only line, just replace it */
217 if(size(txt.lines) == 1)
221 /* destroy the line */
222 adel(txt.lines, txt.y);
225 /* fix if it was the last line */
226 if(txt.y >= size(txt.lines))
227 txt.y = size(txt.lines) - 1;
229 /* move to previous x position */
230 txt.x = mp.vx2x(txt.lines[txt.y], vx);
236 sub mp.delete_char(doc)
237 /* deletes the current char */
241 /* is it over the end of line? */
242 if(txt.x == size(txt.lines[txt.y]))
248 w = splice(txt.lines[txt.y], NULL, txt.x, 1);
249 txt.lines[txt.y] = w[0];
256 sub mp.delete_range(doc, bx, by, ex, ey)
257 /* deletes a range of characters from a document */
261 /* move to the start of the range */
269 /* block is just one line; delete the middle part */
270 w = splice(txt.lines[by], NULL, bx, ex - bx);
272 txt.lines[by] = w[0];
278 /* block has more than one line */
280 /* delete from the beginning to the end of the first line */
281 w = splice(txt.lines[by], NULL, bx, -1);
282 txt.lines[by] = w[0];
284 /* delete from the beginning of the last line to
285 the end of the block */
286 w = splice(txt.lines[ey], NULL, 0, ex);
287 txt.lines[ey] = w[0];
289 /* collapse the lines in between */
290 collapse(txt.lines, by + 1, ey - by - 1);
292 /* finally join both lines */
300 sub mp.insert_string(doc, str)
301 /* inserts a string into the cursor position */
306 /* splice and change */
307 w = splice(txt.lines[txt.y], str, txt.x, 0);
308 txt.lines[txt.y] = w[0];
317 sub mp.insert(doc, a)
318 /* inserts an array of text into a document */
323 /* if a is not an array, split it */
327 /* empty array? return */
328 if((s = size(a)) == 0) return;
330 /* paste first line into current position */
331 mp.insert_string(doc, a[0]);
333 /* more than just one line? */
336 /* break current line in two */
339 /* insert last line */
340 mp.insert_string(doc, a[s - 1]);
343 /* more than two lines? */
349 expand(txt.lines, txt.y, s - 2);
351 /* transfer middle lines */
353 txt.lines[txt.y++] = a[n++];
358 sub mp.wrap_words(doc)
359 /* do the word wrapping */
363 if(mp.config.word_wrap == 0) return;
365 /* take the column where the cursor is */
366 local c = mp.x2vx(txt.lines[txt.y], txt.x);
368 if(c >= mp.config.word_wrap &&
369 regex("/^.{1," ~ mp.config.word_wrap ~ "}\s/", txt.lines[txt.y]))
373 /* take the coordinates */
376 /* break the line there */
377 mp.break_line(doc, w[1]);
379 /* delete the space at the end of the line */
380 txt.lines[txt.y - 1] = sregex("/\s$/", txt.lines[txt.y - 1], NULL);
385 sub mp.insert_space(doc)
386 /* inserts a space, taking wordwrapping into account */
395 sub mp.insert_tab(doc)
398 if(mp.config.tabs_as_spaces)
400 /* number of spaces to insert */
401 local n = mp.config.tab_size -
402 ((doc.txt.x) % mp.config.tab_size);
404 while(n--) mp.insert(doc, ' ');
407 mp.insert(doc, "\t");
411 sub mp.insert_newline(doc)
412 /* inserts a newline */
419 sub mp.insert_keystroke(doc, key)
420 /* inserts from a keystroke (with undo) */
429 sub mp.store_undo(doc)
430 /* stores the current txt in the undo queue */
432 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
438 /* undoes last operation */
442 if(txt = pop(doc.undo))
444 queue(doc.redo, clone(doc.txt), mp.config.undo_levels);
451 /* redoes last undid operation */
455 if(txt = pop(doc.redo))
457 queue(doc.undo, clone(doc.txt), mp.config.undo_levels);
464 sub mp.join_paragraph(doc)
465 /* joins current paragraph in just one line */
470 while((l = txt.lines[txt.y + 1]) && size(l))
472 /* delete all leading blanks in the next line */
473 txt.lines[txt.y + 1] = sregex("/^[ \t]+/", txt.lines[txt.y + 1]);
475 /* move to end of line and add a space separator */
485 sub mp.word_wrap_paragraph(doc)
486 /* word wraps current paragraph */
490 if(mp.config.word_wrap == 0)
493 mp.join_paragraph(doc);
497 while(size(txt.lines[txt.y]) > mp.config.word_wrap)
499 mp.insert_space(doc);