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;
47 mp.config.maximize = 0;
49 /* default end of line, system dependent */
50 if (mp.drv.id eq 'win32')
51 mp.config.eol = "\r\n";
56 mp.config.status_format = "%m%n %x,%y [%l] %R%O %s %e %t";
57 mp.status_line_info = {
59 '%m' => sub { mp.active.txt.mod && '*' || ''; },
60 '%x' => sub { mp.active.txt.x + 1; },
61 '%y' => sub { mp.active.txt.y + 1; },
62 '%l' => sub { size(mp.active.txt.lines); },
63 '%R' => sub { mp.macro.process_event && 'R' || ''; },
65 '%s' => sub { mp.active.syntax.name; },
66 '%t' => sub { mp.tags[mp.get_word(mp.active())].label; },
67 '%n' => sub { mp.active.name; },
68 '%w' => sub { mp.word_count(mp.active()); },
69 '%e' => sub { mp.active.encoding || ''; },
73 /* a regex for selecting words */
74 mp.word_regex = "/[[:alnum:]_]+/i";
76 /* if it does not work (i.e. not GNU regex), fall back */
77 if (regex(mp.word_regex, "test") == NULL)
78 mp.word_regex = '/[A-Z_][A-Z0-9_]*/i';
84 /* allowed color names (order matters, match the Unix curses one) */
85 mp.color_names = [ "default", "black", "red", "green",
86 "yellow", "blue", "magenta", "cyan", "white" ];
88 /* color definitions */
90 'normal' => { 'text' => [ 'default', 'default' ],
91 'gui' => [ 0x000000, 0xffffff ] },
92 'cursor' => { 'text' => [ 'default', 'default' ],
93 'gui' => [ 0x000000, 0xffffff ],
94 'flags' => [ 'reverse' ] },
95 'selection' => { 'text' => [ 'red', 'default' ],
96 'gui' => [ 0xff0000, 0xffffff ],
97 'flags' => [ 'reverse'] },
98 'comments' => { 'text' => [ 'green', 'default' ],
99 'gui' => [ 0x00cc77, 0xffffff ] },
100 'documentation' => { 'text' => [ 'cyan', 'default' ],
101 'gui' => [ 0x8888ff, 0xffffff ] },
102 'quotes' => { 'text' => [ 'blue', 'default' ],
103 'gui' => [ 0x0000ff, 0xffffff ],
104 'flags' => [ 'bright' ] },
105 'matching' => { 'text' => [ 'black', 'cyan' ],
106 'gui' => [ 0x000000, 0xffff00 ] },
107 'word1' => { 'text' => [ 'green', 'default' ],
108 'gui' => [ 0x00aa00, 0xffffff ],
109 'flags' => [ 'bright' ] },
110 'word2' => { 'text' => [ 'red', 'default' ],
111 'gui' => [ 0xff6666, 0xffffff ],
112 'flags' => [ 'bright' ] },
113 'tag' => { 'text' => [ 'cyan', 'default' ],
114 'gui' => [ 0x8888ff, 0xffffff ],
115 'flags' => [ 'underline' ] },
116 'spell' => { 'text' => [ 'red', 'default' ],
117 'gui' => [ 0xff8888, 0xffffff ],
118 'flags' => [ 'bright', 'underline' ] },
119 'search' => { 'text' => [ 'black', 'green' ],
120 'gui' => [ 0x000000, 0x00cc77 ] }
123 /* hash of specially coloured words */
139 [ 'new', 'open', 'save', 'save_as', 'close', 'revert',
143 '-', 'open_config_file', 'open_templates_file',
145 '-', 'save_session', 'load_session',
151 [ 'undo', 'redo', '-',
152 'cut_mark', 'copy_mark', 'paste_mark', 'delete_line', '-',
153 'mark', 'mark_vertical', 'unmark', '-',
154 'insert_template', '-',
155 'word_wrap_paragraph', 'join_paragraph', '-',
162 [ 'seek', 'seek_next', 'seek_prev', 'replace', '-',
164 'seek_misspelled', 'ignore_last_misspell', '-',
165 'seek_repeated_word', '-',
166 'find_tag', 'complete_symbol', '-', 'grep'
172 'move_bof', 'move_eof', 'move_bol', 'move_eol',
173 'goto', 'move_word_right', 'move_word_left',
180 [ 'record_macro', 'play_macro', '-',
181 'encoding', 'tab_options', 'line_options', 'repeated_words_options',
182 'toggle_spellcheck', '-',
184 'zoom_in', 'zoom_out', '-',
190 mp.actions_by_menu_label = {};
195 * mp.redraw - Triggers a redraw on the next cycle.
197 * Triggers a full document redraw in the next cycle.
201 /* just increment the redraw trigger */
207 /* returns the active document */
211 /* empty document list? create a new, empty one */
212 if (size(mp.docs) == 0)
215 /* get active document */
216 d = mp.docs[mp.active_i];
218 /* if it's read only but has modifications, revert them */
219 if (d.read_only && size(d.undo)) {
224 'timeout' => time() + 2,
225 'string' => '*' ~ L("Read-only document") ~ '*'
233 sub mp.process_action(a)
234 /* processes an action */
240 if ((f = mp.actions[a]) != NULL)
244 'timeout' => time() + 2,
245 'string' => sprintf(L("Unknown action '%s'"), a)
251 sub mp.process_event(k)
252 /* processes a key event */
256 /* empty document list? do nothing */
257 if (size(mp.docs) == 0)
262 if (mp.keycodes_t == NULL)
263 mp.keycodes_t = mp.keycodes;
265 /* get the action asociated to the keycode */
266 if ((a = mp.keycodes_t[k]) != NULL) {
268 /* if it's a hash, store for further testing */
272 /* if it's executable, run it */
276 /* if it's an array, process it sequentially */
279 mp.process_action(l);
281 mp.process_action(a);
283 mp.keycodes_t = NULL;
287 mp.insert_keystroke(d, k);
288 mp.keycodes_t = NULL;
291 mp.shift_pressed = NULL;
295 sub mp.build_status_line()
296 /* returns the string to be drawn in the status line */
299 /* is the message still active? */
300 if (mp.message.timeout > time())
301 return mp.message.string;
306 return sregex("/%./g", mp.config.status_format, mp.status_line_info);
310 sub mp.backslash_codes(s, d)
311 /* encodes (d == 0) or decodes (d == 1) backslash codes
312 (like \n, \r, etc.) */
314 d && sregex("/[\r\n\t]/g", s, { "\r" => '\r', "\n" => '\n', "\t" => '\t'}) ||
315 sregex("/\\\\[rnt]/g", s, { '\r' => "\r", '\n' => "\n", '\t' => "\t"});
319 sub mp.long_op(func, a1, a2, a3, a4)
320 /* executes a potentially long function */
325 r = func(a1, a2, a3, a4);
332 sub mp.get_history(key)
333 /* returns a history for the specified key */
337 if (mp.history == NULL)
339 if (mp.history[key] == NULL)
340 mp.history[key] = [];
342 return mp.history[key];
346 sub mp.menu_label(action)
347 /* returns a label for the menu for an action */
351 /* if action is '-', it's a menu separator */
355 /* no recognized action? return */
356 if (!exists(mp.actions, action))
359 /* get the translated description */
360 l = L(mp.actdesc[action]) || action;
362 /* is there a keycode that generates this action? */
363 foreach (i, sort(keys(mp.keycodes))) {
364 if (mp.keycodes[i] eq action) {
365 /* avoid mouse and window pseudo-keycodes */
366 if (!regex("/window/", i) && !regex("/mouse/", i)) {
367 l = l ~ ' [' ~ i ~ ']';
373 mp.actions_by_menu_label[l] = action;
379 sub mp.trim_with_ellipsis(str, max)
380 /* trims the string to the last max characters, adding ellipsis if done */
382 local v = regex('/.{' ~ max ~ '}$/', str);
383 return v && '...' ~ v || str;
387 sub mp.get_doc_names(max)
388 /* returns an array with the trimmed names of the documents */
391 (e.txt.mod && '* ' || '') ~ mp.trim_with_ellipsis(e.name, (max || 24));
397 /* set mp.exit_message with an usage message (--help) */
401 "Minimum Profit %s - Programmer Text Editor\n"\
402 "Copyright (C) Angel Ortega <angel@triptico.com>\n"\
403 "This software is covered by the GPL license. NO WARRANTY.\n"\
405 "Usage: mp-5 [options] [files...]\n"\
409 " -t {tag} Edits the file where tag is defined\n"\
410 " -e {mpsl_code} Executes MPSL code\n"\
411 " -f {mpsl_script} Executes MPSL script file\n"\
412 " -d {directory} Set current directory\n"\
413 " +NNN Moves to line number NNN of last file\n"\
415 "Homepage: http://www.triptico.com/software/mp.html\n"\
416 "Mailing list: mp-subscribe@lists.triptico.com\n"
421 sub mp.process_cmdline()
422 /* process the command line arguments (ARGV) */
431 while (o = shift(ARGV)) {
432 if (o eq '-h' || o eq '--help') {
440 local c = shift(ARGV);
442 if (! regex('/;\s*$/', c))
450 local s = shift(ARGV);
453 ERROR = sprintf(L("Cannot open '%s'"), s);
456 eval(join("\n", mp.active.txt.lines));
465 mp.open_tag(shift(ARGV));
468 local s = shift(ARGV);
470 if (mp.hex_view(s) == NULL)
471 ERROR = sprintf(L("Cannot open '%s'"), s);
474 if (regex('/^\+/', o)) {
483 mp.exit_message = ERROR ~ "\n";
489 /* if no files are loaded, try a session */
490 if (size(mp.docs) == 0 && mp.config.auto_sessions) {
494 /* set the first as the active one */
500 /* if there is a line defined, move there */
502 mp.set_y(mp.active(), line);
506 sub mp.load_profile()
507 /* loads ~/.mp.mpsl */
509 /* if /etc/mp.mpsl exists, execute it */
510 if (stat('/etc/mp.mpsl') != NULL) {
512 local INC = [ '/etc' ];
517 /* if ~/.mp.mpsl exists, execute it */
518 if (ERROR == NULL && stat(HOMEDIR ~ '.mp.mpsl') != NULL) {
520 local INC = [ HOMEDIR ];
525 /* errors? show in a message */
528 'timeout' => time() + 20,
537 sub mp.setup_language()
538 /* sets up the language */
540 /* set gettext() domain */
541 gettext_domain('minimum-profit', APPDIR ~ 'locale');
543 /* test if gettext() can do a basic translation */
544 if (gettext('&File') eq '&File' && ENV.LANG) {
545 /* no; try alternatives using the LANG variable */
546 local v = [ sregex('!/!g', ENV.LANG) ]; /* es_ES.UTF-8 */
547 push(v, shift(split('.', v[-1]))); /* es_ES */
548 push(v, shift(split('_', v[-1]))); /* es */
551 eval('load("lang/' ~ l ~ '.mpsl");');
562 sub mp.normalize_version(vs)
563 /* converts a version string to something usable with cmp() */
565 map(sub(e) { sprintf("%03d", e); },
567 sregex('/-.+$/', vs)));
571 sub mp.assert_version(found, minimal, package)
572 /* asserts that 'found' version of 'package' is at least 'minimal',
573 or generate a warning otherwise */
575 if (cmp(mp.normalize_version(found),
576 mp.normalize_version(minimal)) < 0) {
577 mp.alert(sprintf(L("WARNING: %s version found is %s, but %s is needed"),
578 package, found, minimal));
583 sub mp.test_versions()
584 /* tests component versions */
588 mp.assert_version(mpdm.version, '1.0.7', 'MPDM');
589 mp.assert_version(MPSL.VERSION, '1.0.7', 'MPSL');
596 load("mp_move.mpsl");
597 load("mp_edit.mpsl");
598 load("mp_file.mpsl");
599 load("mp_clipboard.mpsl");
600 load("mp_search.mpsl");
601 load("mp_tags.mpsl");
602 load("mp_syntax.mpsl");
603 load("mp_macro.mpsl");
604 load("mp_templates.mpsl");
605 load("mp_spell.mpsl");
606 load("mp_misc.mpsl");
607 load("mp_crypt.mpsl");
608 load("mp_keyseq.mpsl");
609 load("mp_session.mpsl");
610 load("mp_build.mpsl");
611 load("mp_writing.mpsl");
616 mp.process_cmdline();