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
28 /** editor actions **/
30 mp.actions['move_left'] = sub (d) { mp.move(d, mp.move_left); };
31 mp.actions['move_right'] = sub (d) { mp.move(d, mp.move_right); };
32 mp.actions['move_up'] = sub (d) { mp.move(d, mp.move_up); };
33 mp.actions['move_down'] = sub (d) { mp.move(d, mp.move_down); };
34 mp.actions['move_pgup'] = sub (d) { mp.move(d, mp.move_pgup); };
35 mp.actions['move_pgdn'] = sub (d) { mp.move(d, mp.move_pgdn); };
36 mp.actions['move_bol'] = sub (d) { mp.move(d, mp.move_bol); };
37 mp.actions['move_eol'] = sub (d) { mp.move(d, mp.move_eol); };
38 mp.actions['move_bof'] = sub (d) { mp.move(d, mp.move_bof); };
39 mp.actions['move_eof'] = sub (d) { mp.move(d, mp.move_eof); };
40 mp.actions['move_word_left'] = sub (d) { mp.move(d, mp.move_word_left); };
41 mp.actions['move_word_right'] = sub (d) { mp.move(d, mp.move_word_right); };
43 mp.actions['goto'] = sub (d) {
45 { 'label' => L("Line to go to:"),
50 if (y != NULL && y[0] >= 1) {
52 mp.set_y(d, y[0] - 1);
56 mp.actions['move_to_mouse_position'] = sub (d) {
58 /* move the cursor there */
59 mp.move_to_coords_xy(d, mp.mouse_x, mp.mouse_y);
61 /* mouse click always unmarks */
65 mp.actions['move_mouse_wheel_up'] = sub (d) {
74 mp.actions['move_mouse_wheel_down'] = sub (d) {
83 mp.actions['document_list'] = sub (d) {
86 { 'label' => L("Document list"),
88 'list' => mp.get_doc_names(60),
89 'value' => mp.active_i
97 /** default key bindings **/
99 mp.keycodes['cursor-left'] = "move_left";
100 mp.keycodes['cursor-right'] = "move_right";
101 mp.keycodes['cursor-up'] = "move_up";
102 mp.keycodes['cursor-down'] = "move_down";
103 mp.keycodes['page-up'] = "move_pgup";
104 mp.keycodes['page-down'] = "move_pgdn";
105 mp.keycodes['home'] = "move_bol";
106 mp.keycodes['end'] = "move_eol";
107 mp.keycodes['ctrl-home'] = "move_bof";
108 mp.keycodes['ctrl-end'] = "move_eof";
109 mp.keycodes['ctrl-cursor-left'] = "move_word_left";
110 mp.keycodes['ctrl-cursor-right'] = "move_word_right";
111 mp.keycodes['ctrl-g'] = "goto";
112 mp.keycodes['mouse-left-button'] = "move_to_mouse_position";
113 mp.keycodes['mouse-right-button'] = "move_to_mouse_position";
114 mp.keycodes['mouse-middle-button'] = "move_to_mouse_position";
115 mp.keycodes['mouse-wheel-up'] = "move_mouse_wheel_up";
116 mp.keycodes['mouse-wheel-down'] = "move_mouse_wheel_down";
118 /** action descriptions **/
120 mp.actdesc['move_left'] = LL("Character left");
121 mp.actdesc['move_right'] = LL("Character right");
122 mp.actdesc['move_up'] = LL("Line up");
123 mp.actdesc['move_down'] = LL("Line down");
124 mp.actdesc['move_pgup'] = LL("Page up");
125 mp.actdesc['move_pgdn'] = LL("Page down");
126 mp.actdesc['move_bol'] = LL("Beginning of line");
127 mp.actdesc['move_eol'] = LL("End of line");
128 mp.actdesc['move_bof'] = LL("Beginning of document");
129 mp.actdesc['move_eof'] = LL("End of document");
130 mp.actdesc['move_word_left'] = LL("Word left");
131 mp.actdesc['move_word_right'] = LL("Word right");
132 mp.actdesc['goto'] = LL("Go to line...");
133 mp.actdesc['move_to_mouse_position'] = LL("Move cursor to mouse click");
134 mp.actdesc['move_mouse_wheel_down'] = LL("Mouse wheel up");
135 mp.actdesc['move_mouse_wheel_up'] = LL("Mouse wheel down");
136 mp.actdesc['document_list'] = LL("Document list");
140 sub mp.move(doc, func)
141 /* wrapper for movement functions, with possible shift selection */
144 if (mp.shift_pressed) {
145 /* shift pressed? move selecting */
146 if (doc.txt.mark == NULL)
158 sub mp.split_by_words(s)
159 /* splits a string by words */
161 return regex(mp.word_regex ~ 'g', s);
165 sub mp.split_line_by_words(doc, r)
166 /* splits current line by words and returns a three element array containing
167 the list of words, the list of offsets and the current position */
169 local txt, l, w, c, ol, oc, p;
172 l = txt.lines[txt.y];
177 /* if no special-purpose regex set, take global one */
181 while ((w = regex(r, l, c[0] + c[1])) != NULL) {
185 /* get coordinates */
188 /* push the starting column */
191 /* if matching coords are between the cursor, store it */
192 if (c[0] <= txt.x && c[0] + c[1] >= txt.x)
196 /* it txt.x is still further than the last match, it means
197 that the 'current' position is beyond the last word */
198 if (txt.x > c[0] + c[1])
201 /* return the list of words, the list of
202 coordinates and the current one */
203 return [ ol, oc, p ];
207 sub mp.get_word(doc, r)
208 /* returns the word under the cursor */
210 local l = mp.split_line_by_words(doc, r);
212 /* no word over cursor? */
220 sub mp.get_range(doc, bx, by, ex, ey, v)
221 /* gets a range or characters from a document */
230 /* block is just one line; take the inside
231 part and push it onto the clipboard */
233 w = splice(txt.lines[by], NULL, bx, ex - bx);
240 /* block has more than one line */
244 /* use normal selection block */
246 /* take from the beginning to the end of the first line */
247 w = splice(txt.lines[n], NULL, bx, -1);
252 /* take the central lines */
254 push(r, txt.lines[n++]);
256 /* take the last line */
257 w = splice(txt.lines[n], NULL, 0, ex);
261 /* use vertical selection block */
263 w = splice(txt.lines[n++], NULL, bx, ex - bx + 1);
265 local p = ex - bx - size(l);
267 /* pad out to end of block line */
281 /* sets the x position */
286 /* cursor moved left of the bol; effective cursor up + eol */
291 /* set x to the end of the line */
292 txt.x = size(txt.lines[txt.y]);
296 /* test if moved beyond end of line */
297 if (x > size(txt.lines[txt.y])) {
298 if (txt.y < size(txt.lines) - 1) {
299 /* cursor moved right of eol;
300 effective cursor down + bol */
312 /* sets the y position */
317 /* get current visual x position */
318 vx = mp.x2vx(txt.lines[txt.y], txt.x);
323 if (y >= size(txt.lines))
324 y = size(txt.lines) - 1;
329 /* adjust new x to match previously one */
330 txt.x = mp.vx2x(txt.lines[txt.y], vx);
335 /* moves one line up */
337 mp.set_y(doc, doc.txt.y - 1);
341 sub mp.move_down(doc)
342 /* moves one line down */
344 mp.set_y(doc, doc.txt.y + 1);
348 sub mp.move_pgup(doc)
349 /* moves one page up */
351 mp.set_y(doc, doc.txt.y - mp.window.ty);
355 sub mp.move_pgdn(doc)
356 /* moves one page down */
358 mp.set_y(doc, doc.txt.y + mp.window.ty);
362 sub mp.move_left(doc)
363 /* moves one char left */
365 /* return 0 if on BOF */
366 if (doc.txt.x + doc.txt.y == 0)
369 mp.set_x(doc, doc.txt.x - 1);
374 sub mp.move_right(doc)
375 /* moves one char right */
377 mp.set_x(doc, doc.txt.x + 1);
382 /* moves to the beginning of the line */
389 /* moves to the end of the line */
391 doc.txt.x = size(doc.txt.lines[doc.txt.y]);
396 /* moves to the beginning of the file */
404 /* moves to the end of the file */
406 doc.txt.y = size(doc.txt.lines) - 1;
411 sub mp.move_word_left(doc)
412 /* moves a word to the left */
418 local l = mp.split_line_by_words(doc);
420 /* get current word */
424 /* if it's not at the beginning of a word,
426 if (i < size(l[1]) && txt.x != l[1][i]) {
431 /* go to previous word */
434 /* if that position exists, move there */
441 /* no lines up? exit */
446 txt.x = size(txt.lines[txt.y]);
451 sub mp.move_word_right(doc)
452 /* moves a word to the right */
456 while (txt.y < size(txt.lines) - 1) {
458 local l = mp.split_line_by_words(doc);
460 /* get next position */
463 /* if that position exists, move there */
464 if (i < size(l[1])) {
476 sub mp.move_to_coords_xy(doc, x, y)
477 /* move the cursor to the character on the visual coords x and y */
480 mp.set_y(doc, doc.txt.vy + y);
482 /* calculate the real position in that line
483 where the mouse click seem to be */
484 x = mp.vx2x(doc.txt.lines[doc.txt.y], doc.txt.vx + x);