4 A Programmer's Text Editor
6 Copyright (C) 1991-2011 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 */
44 case_sensitive_search: 1,
53 /* default end of line, system dependent */
54 if (mp.drv.id eq 'win32')
55 mp.config.eol = "\r\n";
60 mp.config.status_format = "%m%n %x,%y [%l] %R%O %s %e %t";
61 mp.status_line_info = {
63 '%m' => sub { mp.active.disk_op && '!' || (mp.active.txt.mod && '*' || ''); },
64 '%x' => sub { mp.active.txt.x + 1; },
65 '%y' => sub { mp.active.txt.y + 1; },
66 '%l' => sub { size(mp.active.txt.lines); },
67 '%R' => sub { mp.macro.process_event && 'R' || ''; },
68 '%O' => sub { mp.config.insert && 'O' || ''; },
69 '%s' => sub { mp.active.syntax.name; },
70 '%t' => sub { mp.tags[mp.get_word(mp.active())].label; },
71 '%n' => sub { mp.active.name; },
72 '%w' => sub { mp.word_count(mp.active()); },
73 '%e' => sub { mp.active.encoding || ''; },
77 /* a regex for selecting words */
78 mp.word_regex = "/[[:alnum:]_]+/i";
80 /* if it does not work (i.e. not GNU regex), fall back */
81 if (regex("test", mp.word_regex) == NULL)
82 mp.word_regex = '/[A-Z_][A-Z0-9_]*/i';
88 /* allowed color names (order matters, match the Unix curses one) */
91 "black", "red", "green", "yellow",
92 "blue", "magenta", "cyan", "white"
95 /* color definitions */
98 text: [ 'default', 'default' ],
99 gui: [ 0x000000, 0xffffff ]
102 text: [ 'default', 'default' ],
103 gui: [ 0x000000, 0xffffff ],
107 text: [ 'red', 'default' ],
108 gui: [ 0xff0000, 0xffffff ],
112 text: [ 'green', 'default' ],
113 gui: [ 0x00cc77, 0xffffff ]
116 text: [ 'cyan', 'default' ],
117 gui: [ 0x8888ff, 0xffffff ]
120 text: [ 'blue', 'default' ],
121 gui: [ 0x0000ff, 0xffffff ],
125 text: [ 'black', 'cyan' ],
126 gui: [ 0x000000, 0xffff00 ]
129 text: [ 'green', 'default' ],
130 gui: [ 0x00aa00, 0xffffff ],
134 text: [ 'red', 'default' ],
135 gui: [ 0xff6666, 0xffffff ],
139 text: [ 'cyan', 'default' ],
140 gui: [ 0x8888ff, 0xffffff ],
141 flags: [ 'underline' ]
144 text: [ 'red', 'default' ],
145 gui: [ 0xff8888, 0xffffff ],
146 flags: [ 'bright', 'underline' ]
149 text: [ 'black', 'green' ],
150 gui: [ 0x000000, 0x00cc77 ]
154 /* hash of specially coloured words */
170 'new', 'open', 'save', 'save_as', 'close', 'revert',
174 '-', 'open_config_file', 'open_templates_file',
176 '-', 'save_session', 'load_session',
183 'cut_mark', 'copy_mark', 'paste_mark', 'delete_mark',
184 'delete_line', 'cut_lines_with_string', '-',
185 'mark', 'mark_vertical', 'unmark', 'mark_all', '-',
186 'insert_template', 'insert_next_item', '-',
187 'word_wrap_paragraph', 'join_paragraph', '-',
188 'exec_command', 'filter_selection', '-',
189 'exec_action', 'eval', 'eval_doc'
194 'seek', 'seek_next', 'seek_prev', 'replace', '-',
196 'seek_misspelled', 'ignore_last_misspell', '-',
197 'seek_repeated_word', '-',
198 'find_tag', 'complete_symbol', '-', 'grep'
204 'move_bof', 'move_eof', 'move_bol', 'move_eol',
205 'goto', 'move_word_right', 'move_word_left',
212 'record_macro', 'play_macro', '-',
213 'encoding', 'tab_options', 'line_options',
214 'repeated_words_options', 'toggle_spellcheck', '-',
216 'zoom_in', 'zoom_out', '-',
222 mp.actions_by_menu_label = {};
227 * mp.redraw - Triggers a redraw on the next cycle.
229 * Triggers a full document redraw in the next cycle.
233 /* just increment the redraw trigger */
239 /* returns the active document */
243 /* empty document list? create a new, empty one */
244 if (size(mp.docs) == 0)
247 /* get active document */
248 d = mp.docs[mp.active_i];
250 /* if it's read only but has modifications, revert them */
251 if (d.read_only && size(d.undo)) {
256 'timeout' => time() + 2,
257 'string' => '*' ~ L("Read-only document") ~ '*'
265 sub mp.process_action(a)
266 /* processes an action */
272 if ((f = mp.actions[a]) != NULL)
276 'timeout' => time() + 2,
277 'string' => sprintf(L("Unknown action '%s'"), a)
285 sub mp.process_event(k)
286 /* processes a key event */
290 /* empty document list? do nothing */
291 if (size(mp.docs) == 0)
296 if (mp.keycodes_t == NULL)
297 mp.keycodes_t = mp.keycodes;
299 /* get the action asociated to the keycode */
300 if ((a = mp.keycodes_t[k]) != NULL) {
302 /* if it's a hash, store for further testing */
306 /* if it's executable, run it */
310 /* if it's an array, process it sequentially */
313 mp.process_action(l);
315 mp.process_action(a);
317 mp.keycodes_t = NULL;
321 mp.insert_keystroke(d, k);
322 mp.keycodes_t = NULL;
325 mp.shift_pressed = NULL;
327 /* if there is a keypress notifier function, call it */
328 if (is_exec(d.keypress))
335 sub mp.build_status_line()
336 /* returns the string to be drawn in the status line */
339 /* is the message still active? */
340 if (mp.message.timeout > time())
341 return mp.message.string;
346 return sregex(mp.config.status_format, "/%./g", mp.status_line_info);
350 sub mp.backslash_codes(s, d)
351 /* encodes (d == 0) or decodes (d == 1) backslash codes
352 (like \n, \r, etc.) */
354 d && sregex(s, "/[\r\n\t]/g", { "\r" => '\r', "\n" => '\n', "\t" => '\t'}) ||
355 sregex(s, "/\\\\[rnt]/g", { '\r' => "\r", '\n' => "\n", '\t' => "\t"});
359 sub mp.long_op(func, a1, a2, a3, a4)
360 /* executes a potentially long function */
365 r = func(a1, a2, a3, a4);
372 sub mp.get_history(key)
373 /* returns a history for the specified key */
377 if (mp.history == NULL)
379 if (mp.history[key] == NULL)
380 mp.history[key] = [];
382 return mp.history[key];
386 sub mp.menu_label(action)
387 /* returns a label for the menu for an action */
391 /* if action is '-', it's a menu separator */
395 /* no recognized action? return */
396 if (!exists(mp.actions, action))
399 /* get the translated description */
400 l = L(mp.actdesc[action]) || action;
402 /* is there a keycode that generates this action? */
403 foreach (i, sort(keys(mp.keycodes))) {
404 if (mp.keycodes[i] eq action) {
405 /* avoid mouse and window pseudo-keycodes */
406 if (!regex(i, "/window/") && !regex(i, "/mouse/")) {
407 l = l ~ ' [' ~ i ~ ']';
413 mp.actions_by_menu_label[l] = action;
419 sub mp.trim_with_ellipsis(str, max)
420 /* trims the string to the last max characters, adding ellipsis if done */
422 local v = regex(str, '/.{' ~ max ~ '}$/');
423 return v && '...' ~ v || str;
427 sub mp.get_doc_names(max)
428 /* returns an array with the trimmed names of the documents */
433 (e.txt.mod && '* ' || '') ~ mp.trim_with_ellipsis(e.name, (max || 24));
440 /* set mp.exit_message with an usage message (--help) */
444 "Minimum Profit %s - Programmer Text Editor\n"\
445 "Copyright (C) Angel Ortega <angel@triptico.com>\n"\
446 "This software is covered by the GPL license. NO WARRANTY.\n"\
448 "Usage: mp-5 [options] [files...]\n"\
452 " -t {tag} Edits the file where tag is defined\n"\
453 " -e {mpsl_code} Executes MPSL code\n"\
454 " -f {mpsl_script} Executes MPSL script file\n"\
455 " -d {directory} Set current directory\n"\
456 " -x {file} Open file in the hexadecimal viewer\n"\
457 " -txt Use text mode instead of GUI\n"\
458 " +NNN Moves to line number NNN of last file\n"\
460 "Homepage: http://triptico.com/software/mp.html\n"\
461 "Mailing list: mp-subscribe@lists.triptico.com\n"
466 sub mp.process_cmdline()
467 /* process the command line arguments (ARGV) */
476 while (o = shift(ARGV)) {
477 if (o eq '-h' || o eq '--help') {
485 local c = shift(ARGV);
487 if (! regex(c, '/;\s*$/'))
495 local s = shift(ARGV);
498 ERROR = sprintf(L("Cannot open '%s'"), s);
501 eval(join(mp.active.txt.lines, "\n"));
510 mp.open_tag(shift(ARGV));
513 local s = shift(ARGV);
515 if (mp.hex_view(s) == NULL)
516 ERROR = sprintf(L("Cannot open '%s'"), s);
520 mp.config.text_mode = 1;
522 if (regex(o, '/^\+/')) {
531 mp.exit_message = ERROR ~ "\n";
537 /* if no files are loaded, try a session */
538 if (size(mp.docs) == 0 && mp.config.auto_sessions) {
542 /* set the first as the active one */
548 /* if there is a line defined, move there */
550 mp.set_y(mp.active(), line);
554 sub mp.load_profile()
555 /* loads ~/.mp.mpsl */
557 /* if /etc/mp.mpsl exists, execute it */
558 if (stat('/etc/mp.mpsl') != NULL) {
560 local INC = [ '/etc' ];
565 /* if ~/.mp.mpsl exists, execute it */
566 if (ERROR == NULL && stat(HOMEDIR ~ '.mp.mpsl') != NULL) {
568 local INC = [ HOMEDIR ];
573 /* errors? show in a message */
576 'timeout' => time() + 20,
585 sub mp.setup_language()
586 /* sets up the language */
588 /* set gettext() domain */
589 gettext_domain('minimum-profit', APPDIR ~ 'locale');
591 /* test if gettext() can do a basic translation */
592 if (gettext('&File') eq '&File' && ENV.LANG) {
593 /* no; try alternatives using the LANG variable */
594 local v = [ sregex(ENV.LANG, '!/!g') ]; /* es_ES.UTF-8 */
595 push(v, shift(split(v[-1], '.'))); /* es_ES */
596 push(v, shift(split(v[-1], '_'))); /* es */
599 eval('load("lang/' ~ l ~ '.mpsl");');
610 sub mp.normalize_version(vs)
611 /* converts a version string to something usable with cmp() */
613 vs->sregex('/-.+$/')->split('.')->map(sub(e) { sprintf("%03d", e); });
617 sub mp.assert_version(found, minimal, package)
618 /* asserts that 'found' version of 'package' is at least 'minimal',
619 or generate a warning otherwise */
621 if (cmp(mp.normalize_version(found),
622 mp.normalize_version(minimal)) < 0) {
623 mp.alert(sprintf(L("WARNING: %s version found is %s, but %s is needed"),
624 package, found, minimal));
629 sub mp.test_versions()
630 /* tests component versions */
634 mp.assert_version(mpdm.version, '2.0.0', 'MPDM');
635 mp.assert_version(MPSL.VERSION, '2.0.1', 'MPSL');
662 foreach (m, mp_modules) {
663 eval('load("mp_' ~ m ~ '.mpsl");');
666 mp.exit_message = mp.exit_message ~ ERROR ~ "\n";
676 mp.process_cmdline();