4 A Programmer's Text Editor
6 Copyright (C) 1991-2010 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;
42 mp.config.dynamic_tabs = 0;
44 mp.config.case_sensitive_search = 1;
45 mp.config.global_replace = 0;
46 mp.config.preread_lines = 60;
47 mp.config.mark_eol = 0;
48 mp.config.maximize = 0;
49 mp.config.keep_eol = 1;
51 /* default end of line, system dependent */
52 if (mp.drv.id eq 'win32')
53 mp.config.eol = "\r\n";
58 mp.config.status_format = "%m%n %x,%y [%l] %R%O %s %e %t";
59 mp.status_line_info = {
61 '%m' => sub { mp.active.txt.mod && '*' || ''; },
62 '%x' => sub { mp.active.txt.x + 1; },
63 '%y' => sub { mp.active.txt.y + 1; },
64 '%l' => sub { size(mp.active.txt.lines); },
65 '%R' => sub { mp.macro.process_event && 'R' || ''; },
66 '%O' => sub { mp.config.insert && 'O' || ''; },
67 '%s' => sub { mp.active.syntax.name; },
68 '%t' => sub { mp.tags[mp.get_word(mp.active())].label; },
69 '%n' => sub { mp.active.name; },
70 '%w' => sub { mp.word_count(mp.active()); },
71 '%e' => sub { mp.active.encoding || ''; },
75 /* a regex for selecting words */
76 mp.word_regex = "/[[:alnum:]_]+/i";
78 /* if it does not work (i.e. not GNU regex), fall back */
79 if (regex(mp.word_regex, "test") == NULL)
80 mp.word_regex = '/[A-Z_][A-Z0-9_]*/i';
86 /* allowed color names (order matters, match the Unix curses one) */
87 mp.color_names = [ "default", "black", "red", "green",
88 "yellow", "blue", "magenta", "cyan", "white" ];
90 /* color definitions */
92 'normal' => { 'text' => [ 'default', 'default' ],
93 'gui' => [ 0x000000, 0xffffff ] },
94 'cursor' => { 'text' => [ 'default', 'default' ],
95 'gui' => [ 0x000000, 0xffffff ],
96 'flags' => [ 'reverse' ] },
97 'selection' => { 'text' => [ 'red', 'default' ],
98 'gui' => [ 0xff0000, 0xffffff ],
99 'flags' => [ 'reverse'] },
100 'comments' => { 'text' => [ 'green', 'default' ],
101 'gui' => [ 0x00cc77, 0xffffff ] },
102 'documentation' => { 'text' => [ 'cyan', 'default' ],
103 'gui' => [ 0x8888ff, 0xffffff ] },
104 'quotes' => { 'text' => [ 'blue', 'default' ],
105 'gui' => [ 0x0000ff, 0xffffff ],
106 'flags' => [ 'bright' ] },
107 'matching' => { 'text' => [ 'black', 'cyan' ],
108 'gui' => [ 0x000000, 0xffff00 ] },
109 'word1' => { 'text' => [ 'green', 'default' ],
110 'gui' => [ 0x00aa00, 0xffffff ],
111 'flags' => [ 'bright' ] },
112 'word2' => { 'text' => [ 'red', 'default' ],
113 'gui' => [ 0xff6666, 0xffffff ],
114 'flags' => [ 'bright' ] },
115 'tag' => { 'text' => [ 'cyan', 'default' ],
116 'gui' => [ 0x8888ff, 0xffffff ],
117 'flags' => [ 'underline' ] },
118 'spell' => { 'text' => [ 'red', 'default' ],
119 'gui' => [ 0xff8888, 0xffffff ],
120 'flags' => [ 'bright', 'underline' ] },
121 'search' => { 'text' => [ 'black', 'green' ],
122 'gui' => [ 0x000000, 0x00cc77 ] }
125 /* hash of specially coloured words */
141 [ 'new', 'open', 'save', 'save_as', 'close', 'revert',
145 '-', 'open_config_file', 'open_templates_file',
147 '-', 'save_session', 'load_session',
153 [ 'undo', 'redo', '-',
154 'cut_mark', 'copy_mark', 'paste_mark', 'delete_mark',
156 'mark', 'mark_vertical', 'unmark', 'mark_all', '-',
157 'insert_template', 'insert_next_item', '-',
158 'word_wrap_paragraph', 'join_paragraph', '-',
159 'exec_command', 'filter_selection', '-',
165 [ 'seek', 'seek_next', 'seek_prev', 'replace', '-',
167 'seek_misspelled', 'ignore_last_misspell', '-',
168 'seek_repeated_word', '-',
169 'find_tag', 'complete_symbol', '-', 'grep'
175 'move_bof', 'move_eof', 'move_bol', 'move_eol',
176 'goto', 'move_word_right', 'move_word_left',
183 [ 'record_macro', 'play_macro', '-',
184 'encoding', 'tab_options', 'line_options', 'repeated_words_options',
185 'toggle_spellcheck', '-',
187 'zoom_in', 'zoom_out', '-',
193 mp.actions_by_menu_label = {};
198 * mp.redraw - Triggers a redraw on the next cycle.
200 * Triggers a full document redraw in the next cycle.
204 /* just increment the redraw trigger */
210 /* returns the active document */
214 /* empty document list? create a new, empty one */
215 if (size(mp.docs) == 0)
218 /* get active document */
219 d = mp.docs[mp.active_i];
221 /* if it's read only but has modifications, revert them */
222 if (d.read_only && size(d.undo)) {
227 'timeout' => time() + 2,
228 'string' => '*' ~ L("Read-only document") ~ '*'
236 sub mp.process_action(a)
237 /* processes an action */
243 if ((f = mp.actions[a]) != NULL)
247 'timeout' => time() + 2,
248 'string' => sprintf(L("Unknown action '%s'"), a)
256 sub mp.process_event(k)
257 /* processes a key event */
261 /* empty document list? do nothing */
262 if (size(mp.docs) == 0)
267 if (mp.keycodes_t == NULL)
268 mp.keycodes_t = mp.keycodes;
270 /* get the action asociated to the keycode */
271 if ((a = mp.keycodes_t[k]) != NULL) {
273 /* if it's a hash, store for further testing */
277 /* if it's executable, run it */
281 /* if it's an array, process it sequentially */
284 mp.process_action(l);
286 mp.process_action(a);
288 mp.keycodes_t = NULL;
292 mp.insert_keystroke(d, k);
293 mp.keycodes_t = NULL;
296 mp.shift_pressed = NULL;
298 /* if there is a keypress notifier function, call it */
299 if (is_exec(d.keypress))
306 sub mp.build_status_line()
307 /* returns the string to be drawn in the status line */
310 /* is the message still active? */
311 if (mp.message.timeout > time())
312 return mp.message.string;
317 return sregex("/%./g", mp.config.status_format, mp.status_line_info);
321 sub mp.backslash_codes(s, d)
322 /* encodes (d == 0) or decodes (d == 1) backslash codes
323 (like \n, \r, etc.) */
325 d && sregex("/[\r\n\t]/g", s, { "\r" => '\r', "\n" => '\n', "\t" => '\t'}) ||
326 sregex("/\\\\[rnt]/g", s, { '\r' => "\r", '\n' => "\n", '\t' => "\t"});
330 sub mp.long_op(func, a1, a2, a3, a4)
331 /* executes a potentially long function */
336 r = func(a1, a2, a3, a4);
343 sub mp.get_history(key)
344 /* returns a history for the specified key */
348 if (mp.history == NULL)
350 if (mp.history[key] == NULL)
351 mp.history[key] = [];
353 return mp.history[key];
357 sub mp.menu_label(action)
358 /* returns a label for the menu for an action */
362 /* if action is '-', it's a menu separator */
366 /* no recognized action? return */
367 if (!exists(mp.actions, action))
370 /* get the translated description */
371 l = L(mp.actdesc[action]) || action;
373 /* is there a keycode that generates this action? */
374 foreach (i, sort(keys(mp.keycodes))) {
375 if (mp.keycodes[i] eq action) {
376 /* avoid mouse and window pseudo-keycodes */
377 if (!regex("/window/", i) && !regex("/mouse/", i)) {
378 l = l ~ ' [' ~ i ~ ']';
384 mp.actions_by_menu_label[l] = action;
390 sub mp.trim_with_ellipsis(str, max)
391 /* trims the string to the last max characters, adding ellipsis if done */
393 local v = regex('/.{' ~ max ~ '}$/', str);
394 return v && '...' ~ v || str;
398 sub mp.get_doc_names(max)
399 /* returns an array with the trimmed names of the documents */
402 (e.txt.mod && '* ' || '') ~ mp.trim_with_ellipsis(e.name, (max || 24));
408 /* set mp.exit_message with an usage message (--help) */
412 "Minimum Profit %s - Programmer Text Editor\n"\
413 "Copyright (C) Angel Ortega <angel@triptico.com>\n"\
414 "This software is covered by the GPL license. NO WARRANTY.\n"\
416 "Usage: mp-5 [options] [files...]\n"\
420 " -t {tag} Edits the file where tag is defined\n"\
421 " -e {mpsl_code} Executes MPSL code\n"\
422 " -f {mpsl_script} Executes MPSL script file\n"\
423 " -d {directory} Set current directory\n"\
424 " -x {file} Open file in the hexadecimal viewer\n"\
425 " -txt Use text mode instead of GUI\n"\
426 " +NNN Moves to line number NNN of last file\n"\
428 "Homepage: http://triptico.com/software/mp.html\n"\
429 "Mailing list: mp-subscribe@lists.triptico.com\n"
434 sub mp.process_cmdline()
435 /* process the command line arguments (ARGV) */
444 while (o = shift(ARGV)) {
445 if (o eq '-h' || o eq '--help') {
453 local c = shift(ARGV);
455 if (! regex('/;\s*$/', c))
463 local s = shift(ARGV);
466 ERROR = sprintf(L("Cannot open '%s'"), s);
469 eval(join("\n", mp.active.txt.lines));
478 mp.open_tag(shift(ARGV));
481 local s = shift(ARGV);
483 if (mp.hex_view(s) == NULL)
484 ERROR = sprintf(L("Cannot open '%s'"), s);
488 mp.config.text_mode = 1;
490 if (regex('/^\+/', o)) {
499 mp.exit_message = ERROR ~ "\n";
505 /* if no files are loaded, try a session */
506 if (size(mp.docs) == 0 && mp.config.auto_sessions) {
510 /* set the first as the active one */
516 /* if there is a line defined, move there */
518 mp.set_y(mp.active(), line);
522 sub mp.load_profile()
523 /* loads ~/.mp.mpsl */
525 /* if /etc/mp.mpsl exists, execute it */
526 if (stat('/etc/mp.mpsl') != NULL) {
528 local INC = [ '/etc' ];
533 /* if ~/.mp.mpsl exists, execute it */
534 if (ERROR == NULL && stat(HOMEDIR ~ '.mp.mpsl') != NULL) {
536 local INC = [ HOMEDIR ];
541 /* errors? show in a message */
544 'timeout' => time() + 20,
553 sub mp.setup_language()
554 /* sets up the language */
556 /* set gettext() domain */
557 gettext_domain('minimum-profit', APPDIR ~ 'locale');
559 /* test if gettext() can do a basic translation */
560 if (gettext('&File') eq '&File' && ENV.LANG) {
561 /* no; try alternatives using the LANG variable */
562 local v = [ sregex('!/!g', ENV.LANG) ]; /* es_ES.UTF-8 */
563 push(v, shift(split('.', v[-1]))); /* es_ES */
564 push(v, shift(split('_', v[-1]))); /* es */
567 eval('load("lang/' ~ l ~ '.mpsl");');
578 sub mp.normalize_version(vs)
579 /* converts a version string to something usable with cmp() */
581 map(sub(e) { sprintf("%03d", e); },
583 sregex('/-.+$/', vs)));
587 sub mp.assert_version(found, minimal, package)
588 /* asserts that 'found' version of 'package' is at least 'minimal',
589 or generate a warning otherwise */
591 if (cmp(mp.normalize_version(found),
592 mp.normalize_version(minimal)) < 0) {
593 mp.alert(sprintf(L("WARNING: %s version found is %s, but %s is needed"),
594 package, found, minimal));
599 sub mp.test_versions()
600 /* tests component versions */
604 mp.assert_version(mpdm.version, '2.0.0', 'MPDM');
605 mp.assert_version(MPSL.VERSION, '2.0.0', 'MPSL');
612 load("mp_move.mpsl");
613 load("mp_edit.mpsl");
614 load("mp_file.mpsl");
615 load("mp_clipboard.mpsl");
616 load("mp_search.mpsl");
617 load("mp_tags.mpsl");
618 load("mp_syntax.mpsl");
619 load("mp_macro.mpsl");
620 load("mp_templates.mpsl");
621 load("mp_spell.mpsl");
622 load("mp_misc.mpsl");
623 load("mp_crypt.mpsl");
624 load("mp_keyseq.mpsl");
625 load("mp_session.mpsl");
626 load("mp_build.mpsl");
627 load("mp_writing.mpsl");
628 load("mp_toys.mpsl");
633 mp.process_cmdline();