Deleted undefined variable in mp.repeated_words().
[mp-5.x.git] / mp_move.mpsl
blob47c2b0a42b63a3595e625c5b72afa03bc848377c
1 /*
3     Minimum Profit 5.x
4     A Programmer's Text Editor
6     Movement.
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) {
44         local y = mp.form( [
45                 { 'label'       => L("Line to go to:"),
46                   'type'        => 'text',
47                   'history'     => 'goto' }
48                 ]);
50         if (y != NULL && y[0] >= 1) {
51                 d.txt.x = 0;
52                 mp.set_y(d, y[0] - 1);
53         }
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 */
62         mp.unmark(d);
65 mp.actions['move_mouse_wheel_up']       = sub (d) {
66         mp.move(d, sub (d) {
67                 mp.move_up(d);
68                 mp.move_up(d);
69                 mp.move_up(d);
70                 mp.move_up(d);
71         });
74 mp.actions['move_mouse_wheel_down']     = sub (d) {
75         mp.move(d, sub(d) {
76                 mp.move_down(d);
77                 mp.move_down(d);
78                 mp.move_down(d);
79                 mp.move_down(d);
80         });
83 mp.actions['document_list'] = sub (d) {
85         local r = mp.form( [
86                 { 'label'       =>      L("Document list"),
87                   'type'        =>      'list',
88                   'list'        =>      mp.get_doc_names(60),
89                   'value'       =>      mp.active_i
90                 } ]
91         );
93         if (r[0] != NULL)
94                 mp.active_i = r[0];
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");
138 /** code **/
140 sub mp.move(doc, func)
141 /* wrapper for movement functions, with possible shift selection */
143         if (func != NULL) {
144                 if (mp.shift_pressed) {
145                         /* shift pressed? move selecting */
146                         if (doc.txt.mark == NULL)
147                                 mp.mark(doc);
149                         func(doc);
150                         mp.mark(doc);
151                 }
152                 else
153                         func(doc);
154         }
158 sub mp.split_by_words(s, r)
159 /* splits a string by words */
161         /* if no special-purpose regex set, take global one */
162         if (r == NULL)
163                 r = mp.word_regex;
165         return regex(r ~ 'g', s);
169 sub mp.split_line_by_words(doc, r)
170 /* splits current line by words and returns a three element array containing
171    the list of words, the list of offsets and the current position */
173         local txt, l, w, c, ol, oc, p;
175         txt = doc.txt;
176         l = txt.lines[txt.y];
177         ol = [];
178         oc = [];
179         p = -1;
181         /* if no special-purpose regex set, take global one */
182         if (r == NULL)
183                 r = mp.word_regex;
185         while ((w = regex(r, l, c[0] + c[1])) != NULL) {
186                 /* store the word */
187                 push(ol, w);
189                 /* get coordinates */
190                 c = regex();
192                 /* push the starting column */
193                 push(oc, c[0]);
195                 /* if matching coords are between the cursor, store it */
196                 if (c[0] <= txt.x && c[0] + c[1] >= txt.x)
197                         p = size(ol) - 1;
198         }
200         /* it txt.x is still further than the last match, it means
201            that the 'current' position is beyond the last word */
202         if (txt.x > c[0] + c[1])
203                 p = size(ol);
205         /* return the list of words, the list of
206            coordinates and the current one */
207         return [ ol, oc, p ];
211 sub mp.get_word(doc, r)
212 /* returns the word under the cursor */
214         local l = mp.split_line_by_words(doc, r);
216         /* no word over cursor? */
217         if (l[2] == -1)
218                 return NULL;
220         return l[0][l[2]];
224 sub mp.get_range(doc, bx, by, ex, ey, v)
225 /* gets a range or characters from a document */
227         local txt = doc.txt;
229         local r = [];
231         if (by == ey) {
232                 local w;
234                 /* block is just one line; take the inside
235                    part and push it onto the clipboard */
237                 w = splice(txt.lines[by], NULL, bx, ex - bx);
239                 push(r, w[1]);
240         }
241         else {
242                 local w, n;
244                 /* block has more than one line */
246                 n = by;
247                 if (v == 0) {
248                         /* use normal selection block */
250                         /* take from the beginning to the end of the first line */
251                         w = splice(txt.lines[n], NULL, bx, -1);
253                         push(r, w[1] || '');
254                         n++;
256                         /* take the central lines */
257                         while (n < ey)
258                                 push(r, txt.lines[n++]);
260                         /* take the last line */
261                         w = splice(txt.lines[n], NULL, 0, ex);
262                         push(r, w[1] || '');
263                 }
264                 else {
265                         /* use vertical selection block */
266                         while (n <= ey) {
267                                 w = splice(txt.lines[n++], NULL, bx, ex - bx + 1);
268                                 local l = w[1];
269                                 local p = ex - bx - size(l);
271                                 /* pad out to end of block line */
272                                 while (p-- >= 0)
273                                         l = l ~ " ";
275                                 push(r, l);
276                         }
277                 }
278         }
280         return r;
284 sub mp.set_x(doc, x)
285 /* sets the x position */
287         local txt = doc.txt;
289         if (x < 0) {
290                 /* cursor moved left of the bol; effective cursor up + eol */
291                 if (txt.y > 0) {
292                         /* one line up */
293                         txt.y--;
295                         /* set x to the end of the line */
296                         txt.x = size(txt.lines[txt.y]);
297                 }
298         }
299         else {
300                 /* test if moved beyond end of line */
301                 if (x > size(txt.lines[txt.y])) {
302                         if (txt.y < size(txt.lines) - 1) {
303                                 /* cursor moved right of eol;
304                                    effective cursor down + bol */
305                                 txt.x = 0;
306                                 txt.y++;
307                         }
308                 }
309                 else
310                         txt.x = x;
311         }
315 sub mp.set_y(doc, y)
316 /* sets the y position */
318         local txt = doc.txt;
319         local vx;
321         /* get current visual x position */
322         vx = mp.x2vx(txt.lines[txt.y], txt.x);
324         /* set boundaries */
325         if (y < 0)
326                 y = 0;
327         if (y >= size(txt.lines))
328                 y = size(txt.lines) - 1;
330         /* move there */
331         txt.y = y;
333         /* adjust new x to match previously one */
334         txt.x = mp.vx2x(txt.lines[txt.y], vx);
338 sub mp.move_up(doc)
339 /* moves one line up */
341         mp.set_y(doc, doc.txt.y - 1);
345 sub mp.move_down(doc)
346 /* moves one line down */
348         mp.set_y(doc, doc.txt.y + 1);
352 sub mp.move_pgup(doc)
353 /* moves one page up */
355         mp.set_y(doc, doc.txt.y - mp.window.ty);
359 sub mp.move_pgdn(doc)
360 /* moves one page down */
362         mp.set_y(doc, doc.txt.y + mp.window.ty);
366 sub mp.move_left(doc)
367 /* moves one char left */
369         /* return 0 if on BOF */
370         if (doc.txt.x + doc.txt.y == 0)
371                 return 0;
373         mp.set_x(doc, doc.txt.x - 1);
374         return 1;
378 sub mp.move_right(doc)
379 /* moves one char right */
381         mp.set_x(doc, doc.txt.x + 1);
385 sub mp.move_bol(doc)
386 /* moves to the beginning of the line */
388         doc.txt.x = 0;
392 sub mp.move_eol(doc)
393 /* moves to the end of the line */
395         doc.txt.x = size(doc.txt.lines[doc.txt.y]);
399 sub mp.move_bof(doc)
400 /* moves to the beginning of the file */
402         doc.txt.x = 0;
403         doc.txt.y = 0;
407 sub mp.move_eof(doc)
408 /* moves to the end of the file */
410         doc.txt.y = size(doc.txt.lines) - 1;
411         mp.move_eol(doc);
415 sub mp.move_word_left(doc)
416 /* moves a word to the left */
418         local txt = doc.txt;
420         while (1) {
421                 /* split by words */
422                 local l = mp.split_line_by_words(doc);
424                 /* get current word */
425                 local i = l[2];
427                 if (i >= 0) {
428                         /* if it's not at the beginning of a word,
429                            move there and go */
430                         if (i < size(l[1]) && txt.x != l[1][i]) {
431                                 txt.x = l[1][i];
432                                 break;
433                         }
435                         /* go to previous word */
436                         i = l[2] - 1;
438                         /* if that position exists, move there */
439                         if (i >= 0) {
440                                 txt.x = l[1][i];
441                                 break;
442                         }
443                 }
445                 /* no lines up? exit */
446                 if (txt.y == 0)
447                         break;
449                 txt.y--;
450                 txt.x = size(txt.lines[txt.y]);
451         }
455 sub mp.move_word_right(doc)
456 /* moves a word to the right */
458         local txt = doc.txt;
460         while (txt.y < size(txt.lines) - 1) {
461                 /* split by words */
462                 local l = mp.split_line_by_words(doc);
464                 /* get next position */
465                 local i = l[2] + 1;
467                 /* if that position exists, move there */
468                 if (i < size(l[1])) {
469                         txt.x = l[1][i];
470                         break;
471                 }
473                 /* try next line */
474                 txt.y++;
475                 txt.x = 0;
476         }
480 sub mp.move_to_coords_xy(doc, x, y)
481 /* move the cursor to the character on the visual coords x and y */
483         /* set y */
484         mp.set_y(doc, doc.txt.vy + y);
486         /* calculate the real position in that line
487            where the mouse click seem to be */
488         x = mp.vx2x(doc.txt.lines[doc.txt.y], doc.txt.vx + x);
490         /* move there */
491         mp.set_x(doc, x);