4 A Programmer's Text Editor
6 Copyright (C) 1991-2009 Angel Ortega <angel@triptico.com>
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
28 /* L(x) is the same as gettext(x) */
31 /* LL(x) is the same as x */
37 mp.config.undo_levels = 100;
38 mp.config.word_wrap = 0;
39 mp.config.auto_indent = 0;
40 mp.config.tab_size = 8;
41 mp.config.tabs_as_spaces = 0;
43 mp.config.case_sensitive_search = 1;
44 mp.config.global_replace = 0;
45 mp.config.preread_lines = 60;
46 mp.config.mark_eol = 0;
48 /* default end of line, system dependent */
49 if (mp.drv.id eq 'win32')
50 mp.config.eol = "\r\n";
55 mp.config.status_format = "%m%n %x,%y [%l] %R%O %s %t";
56 mp.status_line_info = {
58 '%m' => sub { mp.active.txt.mod && '*' || ''; },
59 '%x' => sub { mp.active.txt.x + 1; },
60 '%y' => sub { mp.active.txt.y + 1; },
61 '%l' => sub { size(mp.active.txt.lines); },
62 '%R' => sub { mp.macro.process_event && 'R' || ''; },
64 '%s' => sub { mp.active.syntax.name; },
65 '%t' => sub { mp.tags[mp.get_word(mp.active())].label; },
66 '%n' => sub { mp.active.name; },
67 '%w' => sub { mp.word_count(mp.active()); },
71 /* a regex for selection words */
72 mp.word_regex = '/[A-Z_][A-Z0-9_]*/i';
78 /* allowed color names (order matters, match the Unix curses one) */
79 mp.color_names = [ "default", "black", "red", "green",
80 "yellow", "blue", "magenta", "cyan", "white" ];
82 /* color definitions */
84 'normal' => { 'text' => [ 'default', 'default' ],
85 'gui' => [ 0x000000, 0xffffff ] },
86 'cursor' => { 'text' => [ 'default', 'default' ],
87 'gui' => [ 0x000000, 0xffffff ],
88 'flags' => [ 'reverse' ] },
89 'selection' => { 'text' => [ 'red', 'default' ],
90 'gui' => [ 0xff0000, 0xffffff ],
91 'flags' => [ 'reverse'] },
92 'comments' => { 'text' => [ 'green', 'default' ],
93 'gui' => [ 0x00cc77, 0xffffff ] },
94 'documentation' => { 'text' => [ 'cyan', 'default' ],
95 'gui' => [ 0x8888ff, 0xffffff ] },
96 'quotes' => { 'text' => [ 'blue', 'default' ],
97 'gui' => [ 0x0000ff, 0xffffff ],
98 'flags' => [ 'bright' ] },
99 'matching' => { 'text' => [ 'black', 'cyan' ],
100 'gui' => [ 0x000000, 0xffff00 ] },
101 'word1' => { 'text' => [ 'green', 'default' ],
102 'gui' => [ 0x00aa00, 0xffffff ],
103 'flags' => [ 'bright' ] },
104 'word2' => { 'text' => [ 'red', 'default' ],
105 'gui' => [ 0xff6666, 0xffffff ],
106 'flags' => [ 'bright' ] },
107 'tag' => { 'text' => [ 'cyan', 'default' ],
108 'gui' => [ 0x8888ff, 0xffffff ],
109 'flags' => [ 'underline' ] },
110 'spell' => { 'text' => [ 'red', 'default' ],
111 'gui' => [ 0xff8888, 0xffffff ],
112 'flags' => [ 'bright', 'underline' ] },
113 'search' => { 'text' => [ 'black', 'green' ],
114 'gui' => [ 0x000000, 0x00cc77 ] }
117 /* hash of specially coloured words */
133 [ 'new', 'open', 'save', 'save_as', 'close', 'revert',
136 '-', 'open_config_file', 'open_templates_file',
138 '-', 'save_session', 'load_session',
144 [ 'undo', 'redo', '-',
145 'cut_mark', 'copy_mark', 'paste_mark', 'delete_line', '-',
146 'mark', 'mark_vertical', 'unmark', '-',
147 'insert_template', '-',
148 'word_wrap_paragraph', 'join_paragraph', '-',
155 [ 'seek', 'seek_next', 'seek_prev', 'replace', '-',
157 'seek_misspelled', 'ignore_last_misspell', '-',
158 'seek_repeated_word', '-',
159 'find_tag', 'complete_symbol', '-', 'grep'
165 'move_bof', 'move_eof', 'move_bol', 'move_eol',
166 'goto', 'move_word_right', 'move_word_left',
173 [ 'record_macro', 'play_macro', '-',
174 'encoding', 'tab_options', 'line_options', 'toggle_spellcheck', '-',
176 'zoom_in', 'zoom_out', '-',
182 mp.actions_by_menu_label = {};
187 * mp.redraw - Triggers a redraw on the next cycle.
189 * Triggers a full document redraw in the next cycle.
193 /* just increment the redraw trigger */
199 /* returns the active document */
203 /* empty document list? create a new, empty one */
204 if (size(mp.docs) == 0)
207 /* get active document */
208 d = mp.docs[mp.active_i];
210 /* if it's read only but has modifications, revert them */
211 if (d.read_only && size(d.undo)) {
216 'timeout' => time() + 2,
217 'string' => '*' ~ L("Read-only document") ~ '*'
225 sub mp.process_action(a)
226 /* processes an action */
232 if ((f = mp.actions[a]) != NULL)
236 'timeout' => time() + 2,
237 'string' => sprintf(L("Unknown action '%s'"), a)
243 sub mp.process_event(k)
244 /* processes a key event */
248 /* empty document list? do nothing */
249 if (size(mp.docs) == 0)
254 if (mp.keycodes_t == NULL)
255 mp.keycodes_t = mp.keycodes;
257 /* get the action asociated to the keycode */
258 if ((a = mp.keycodes_t[k]) != NULL) {
260 /* if it's a hash, store for further testing */
264 /* if it's executable, run it */
268 /* if it's an array, process it sequentially */
271 mp.process_action(l);
273 mp.process_action(a);
275 mp.keycodes_t = NULL;
279 mp.insert_keystroke(d, k);
280 mp.keycodes_t = NULL;
283 mp.shift_pressed = NULL;
287 sub mp.build_status_line()
288 /* returns the string to be drawn in the status line */
291 /* is the message still active? */
292 if (mp.message.timeout > time())
293 return mp.message.string;
298 return sregex("/%./g", mp.config.status_format, mp.status_line_info);
302 sub mp.backslash_codes(s, d)
303 /* encodes (d == 0) or decodes (d == 1) backslash codes
304 (like \n, \r, etc.) */
306 d && sregex("/[\r\n\t]/g", s, { "\r" => '\r', "\n" => '\n', "\t" => '\t'}) ||
307 sregex("/\\\\[rnt]/g", s, { '\r' => "\r", '\n' => "\n", '\t' => "\t"});
311 sub mp.long_op(func, a1, a2, a3, a4)
312 /* executes a potentially long function */
317 r = func(a1, a2, a3, a4);
324 sub mp.get_history(key)
325 /* returns a history for the specified key */
329 if (mp.history == NULL)
331 if (mp.history[key] == NULL)
332 mp.history[key] = [];
334 return mp.history[key];
338 sub mp.menu_label(action)
339 /* returns a label for the menu for an action */
343 /* if action is '-', it's a menu separator */
347 /* no recognized action? return */
348 if (!exists(mp.actions, action))
351 /* get the translated description */
352 l = L(mp.actdesc[action]) || action;
354 /* is there a keycode that generates this action? */
355 foreach (i, sort(keys(mp.keycodes))) {
356 if (mp.keycodes[i] eq action) {
357 /* avoid mouse and window pseudo-keycodes */
358 if (!regex("/window/", i) && !regex("/mouse/", i)) {
359 l = l ~ ' [' ~ i ~ ']';
365 mp.actions_by_menu_label[l] = action;
371 sub mp.trim_with_ellipsis(str, max)
372 /* trims the string to the last max characters, adding ellipsis if done */
374 local v = regex('/.{' ~ max ~ '}$/', str);
375 return v && '...' ~ v || str;
379 sub mp.get_doc_names(max)
380 /* returns an array with the trimmed names of the documents */
383 (e.txt.mod && '* ' || '') ~ mp.trim_with_ellipsis(e.name, (max || 24));
389 /* set mp.exit_message with an usage message (--help) */
393 "Minimum Profit %s - Programmer Text Editor\n"\
394 "Copyright (C) Angel Ortega <angel@triptico.com>\n"\
395 "This software is covered by the GPL license. NO WARRANTY.\n"\
397 "Usage: mp-5 [options] [files...]\n"\
401 " -t {tag} Edits the file where tag is defined\n"\
402 " -e {mpsl_code} Executes MPSL code\n"\
403 " -f {mpsl_script} Executes MPSL script file\n"\
404 " +NNN Moves to line number NNN of last file\n"\
406 "Homepage: http://www.triptico.com/software/mp.html\n"\
407 "Mailing list: mp-subscribe@lists.triptico.com\n"
412 sub mp.process_cmdline()
413 /* process the command line arguments (ARGV) */
422 while (o = shift(ARGV)) {
423 if (o eq '-h' || o eq '--help') {
431 local c = shift(ARGV);
433 if (! regex('/;\s*$/', c))
441 local s = shift(ARGV);
444 ERROR = sprintf(L("Cannot open '%s'"), s);
447 eval(join("\n", mp.active.txt.lines));
456 mp.open_tag(shift(ARGV));
458 if (regex('/^\+/', o)) {
467 mp.exit_message = ERROR ~ "\n";
473 /* if no files are loaded, try a session */
474 if (size(mp.docs) == 0 && mp.config.auto_sessions) {
478 /* set the first as the active one */
484 /* if there is a line defined, move there */
486 mp.set_y(mp.active(), line);
490 sub mp.load_profile()
491 /* loads ~/.mp.mpsl */
493 /* if /etc/mp.mpsl exists, execute it */
494 if (stat('/etc/mp.mpsl') != NULL) {
496 local INC = [ '/etc' ];
501 /* if ~/.mp.mpsl exists, execute it */
502 if (ERROR == NULL && stat(HOMEDIR ~ '.mp.mpsl') != NULL) {
504 local INC = [ HOMEDIR ];
509 /* errors? show in a message */
512 'timeout' => time() + 20,
521 sub mp.setup_language()
522 /* sets up the language */
524 /* set gettext() domain */
525 gettext_domain('minimum-profit', APPDIR ~ 'locale');
527 /* test if gettext() can do a basic translation */
528 if (gettext('&File') eq '&File' && ENV.LANG) {
529 /* no; try alternatives using the LANG variable */
530 local v = [ sregex('!/!g', ENV.LANG) ]; /* es_ES.UTF-8 */
531 push(v, shift(split('.', v[-1]))); /* es_ES */
532 push(v, shift(split('_', v[-1]))); /* es */
535 eval('load("lang/' ~ l ~ '.mpsl");');
546 sub mp.normalize_version(vs)
547 /* converts a version string to something usable with cmp() */
549 map(sub(e) { sprintf("%03d", e); },
551 sregex('/-.+$/', vs)));
555 sub mp.assert_version(found, minimal, package)
556 /* asserts that 'found' version of 'package' is at least 'minimal',
557 or generate a warning otherwise */
559 if (cmp(mp.normalize_version(found),
560 mp.normalize_version(minimal)) < 0) {
561 mp.alert(sprintf(L("WARNING: %s version found is %s, but %s is needed"),
562 package, found, minimal));
567 sub mp.test_versions()
568 /* tests component versions */
572 mp.assert_version(mpdm.version, '1.0.7', 'MPDM');
573 mp.assert_version(MPSL.VERSION, '1.0.7', 'MPSL');
580 load("mp_move.mpsl");
581 load("mp_edit.mpsl");
582 load("mp_file.mpsl");
583 load("mp_clipboard.mpsl");
584 load("mp_search.mpsl");
585 load("mp_tags.mpsl");
586 load("mp_syntax.mpsl");
587 load("mp_macro.mpsl");
588 load("mp_templates.mpsl");
589 load("mp_spell.mpsl");
590 load("mp_misc.mpsl");
591 load("mp_crypt.mpsl");
592 load("mp_keyseq.mpsl");
593 load("mp_session.mpsl");
594 load("mp_build.mpsl");
595 load("mp_writing.mpsl");
600 mp.process_cmdline();