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 selecting words */
72 mp.word_regex = "/[[:alnum:]_]+/i";
74 /* if it does not work (i.e. not GNU regex), fall back */
75 if (regex(mp.word_regex, "test") == NULL)
76 mp.word_regex = '/[A-Z_][A-Z0-9_]*/i';
82 /* allowed color names (order matters, match the Unix curses one) */
83 mp.color_names = [ "default", "black", "red", "green",
84 "yellow", "blue", "magenta", "cyan", "white" ];
86 /* color definitions */
88 'normal' => { 'text' => [ 'default', 'default' ],
89 'gui' => [ 0x000000, 0xffffff ] },
90 'cursor' => { 'text' => [ 'default', 'default' ],
91 'gui' => [ 0x000000, 0xffffff ],
92 'flags' => [ 'reverse' ] },
93 'selection' => { 'text' => [ 'red', 'default' ],
94 'gui' => [ 0xff0000, 0xffffff ],
95 'flags' => [ 'reverse'] },
96 'comments' => { 'text' => [ 'green', 'default' ],
97 'gui' => [ 0x00cc77, 0xffffff ] },
98 'documentation' => { 'text' => [ 'cyan', 'default' ],
99 'gui' => [ 0x8888ff, 0xffffff ] },
100 'quotes' => { 'text' => [ 'blue', 'default' ],
101 'gui' => [ 0x0000ff, 0xffffff ],
102 'flags' => [ 'bright' ] },
103 'matching' => { 'text' => [ 'black', 'cyan' ],
104 'gui' => [ 0x000000, 0xffff00 ] },
105 'word1' => { 'text' => [ 'green', 'default' ],
106 'gui' => [ 0x00aa00, 0xffffff ],
107 'flags' => [ 'bright' ] },
108 'word2' => { 'text' => [ 'red', 'default' ],
109 'gui' => [ 0xff6666, 0xffffff ],
110 'flags' => [ 'bright' ] },
111 'tag' => { 'text' => [ 'cyan', 'default' ],
112 'gui' => [ 0x8888ff, 0xffffff ],
113 'flags' => [ 'underline' ] },
114 'spell' => { 'text' => [ 'red', 'default' ],
115 'gui' => [ 0xff8888, 0xffffff ],
116 'flags' => [ 'bright', 'underline' ] },
117 'search' => { 'text' => [ 'black', 'green' ],
118 'gui' => [ 0x000000, 0x00cc77 ] }
121 /* hash of specially coloured words */
137 [ 'new', 'open', 'save', 'save_as', 'close', 'revert',
140 '-', 'open_config_file', 'open_templates_file',
142 '-', 'save_session', 'load_session',
148 [ 'undo', 'redo', '-',
149 'cut_mark', 'copy_mark', 'paste_mark', 'delete_line', '-',
150 'mark', 'mark_vertical', 'unmark', '-',
151 'insert_template', '-',
152 'word_wrap_paragraph', 'join_paragraph', '-',
159 [ 'seek', 'seek_next', 'seek_prev', 'replace', '-',
161 'seek_misspelled', 'ignore_last_misspell', '-',
162 'seek_repeated_word', '-',
163 'find_tag', 'complete_symbol', '-', 'grep'
169 'move_bof', 'move_eof', 'move_bol', 'move_eol',
170 'goto', 'move_word_right', 'move_word_left',
177 [ 'record_macro', 'play_macro', '-',
178 'encoding', 'tab_options', 'line_options', 'repeated_words_options',
179 'toggle_spellcheck', '-',
181 'zoom_in', 'zoom_out', '-',
187 mp.actions_by_menu_label = {};
192 * mp.redraw - Triggers a redraw on the next cycle.
194 * Triggers a full document redraw in the next cycle.
198 /* just increment the redraw trigger */
204 /* returns the active document */
208 /* empty document list? create a new, empty one */
209 if (size(mp.docs) == 0)
212 /* get active document */
213 d = mp.docs[mp.active_i];
215 /* if it's read only but has modifications, revert them */
216 if (d.read_only && size(d.undo)) {
221 'timeout' => time() + 2,
222 'string' => '*' ~ L("Read-only document") ~ '*'
230 sub mp.process_action(a)
231 /* processes an action */
237 if ((f = mp.actions[a]) != NULL)
241 'timeout' => time() + 2,
242 'string' => sprintf(L("Unknown action '%s'"), a)
248 sub mp.process_event(k)
249 /* processes a key event */
253 /* empty document list? do nothing */
254 if (size(mp.docs) == 0)
259 if (mp.keycodes_t == NULL)
260 mp.keycodes_t = mp.keycodes;
262 /* get the action asociated to the keycode */
263 if ((a = mp.keycodes_t[k]) != NULL) {
265 /* if it's a hash, store for further testing */
269 /* if it's executable, run it */
273 /* if it's an array, process it sequentially */
276 mp.process_action(l);
278 mp.process_action(a);
280 mp.keycodes_t = NULL;
284 mp.insert_keystroke(d, k);
285 mp.keycodes_t = NULL;
288 mp.shift_pressed = NULL;
292 sub mp.build_status_line()
293 /* returns the string to be drawn in the status line */
296 /* is the message still active? */
297 if (mp.message.timeout > time())
298 return mp.message.string;
303 return sregex("/%./g", mp.config.status_format, mp.status_line_info);
307 sub mp.backslash_codes(s, d)
308 /* encodes (d == 0) or decodes (d == 1) backslash codes
309 (like \n, \r, etc.) */
311 d && sregex("/[\r\n\t]/g", s, { "\r" => '\r', "\n" => '\n', "\t" => '\t'}) ||
312 sregex("/\\\\[rnt]/g", s, { '\r' => "\r", '\n' => "\n", '\t' => "\t"});
316 sub mp.long_op(func, a1, a2, a3, a4)
317 /* executes a potentially long function */
322 r = func(a1, a2, a3, a4);
329 sub mp.get_history(key)
330 /* returns a history for the specified key */
334 if (mp.history == NULL)
336 if (mp.history[key] == NULL)
337 mp.history[key] = [];
339 return mp.history[key];
343 sub mp.menu_label(action)
344 /* returns a label for the menu for an action */
348 /* if action is '-', it's a menu separator */
352 /* no recognized action? return */
353 if (!exists(mp.actions, action))
356 /* get the translated description */
357 l = L(mp.actdesc[action]) || action;
359 /* is there a keycode that generates this action? */
360 foreach (i, sort(keys(mp.keycodes))) {
361 if (mp.keycodes[i] eq action) {
362 /* avoid mouse and window pseudo-keycodes */
363 if (!regex("/window/", i) && !regex("/mouse/", i)) {
364 l = l ~ ' [' ~ i ~ ']';
370 mp.actions_by_menu_label[l] = action;
376 sub mp.trim_with_ellipsis(str, max)
377 /* trims the string to the last max characters, adding ellipsis if done */
379 local v = regex('/.{' ~ max ~ '}$/', str);
380 return v && '...' ~ v || str;
384 sub mp.get_doc_names(max)
385 /* returns an array with the trimmed names of the documents */
388 (e.txt.mod && '* ' || '') ~ mp.trim_with_ellipsis(e.name, (max || 24));
394 /* set mp.exit_message with an usage message (--help) */
398 "Minimum Profit %s - Programmer Text Editor\n"\
399 "Copyright (C) Angel Ortega <angel@triptico.com>\n"\
400 "This software is covered by the GPL license. NO WARRANTY.\n"\
402 "Usage: mp-5 [options] [files...]\n"\
406 " -t {tag} Edits the file where tag is defined\n"\
407 " -e {mpsl_code} Executes MPSL code\n"\
408 " -f {mpsl_script} Executes MPSL script file\n"\
409 " -d {directory} Set current directory\n"\
410 " +NNN Moves to line number NNN of last file\n"\
412 "Homepage: http://www.triptico.com/software/mp.html\n"\
413 "Mailing list: mp-subscribe@lists.triptico.com\n"
418 sub mp.process_cmdline()
419 /* process the command line arguments (ARGV) */
428 while (o = shift(ARGV)) {
429 if (o eq '-h' || o eq '--help') {
437 local c = shift(ARGV);
439 if (! regex('/;\s*$/', c))
447 local s = shift(ARGV);
450 ERROR = sprintf(L("Cannot open '%s'"), s);
453 eval(join("\n", mp.active.txt.lines));
462 mp.open_tag(shift(ARGV));
464 if (regex('/^\+/', o)) {
473 mp.exit_message = ERROR ~ "\n";
479 /* if no files are loaded, try a session */
480 if (size(mp.docs) == 0 && mp.config.auto_sessions) {
484 /* set the first as the active one */
490 /* if there is a line defined, move there */
492 mp.set_y(mp.active(), line);
496 sub mp.load_profile()
497 /* loads ~/.mp.mpsl */
499 /* if /etc/mp.mpsl exists, execute it */
500 if (stat('/etc/mp.mpsl') != NULL) {
502 local INC = [ '/etc' ];
507 /* if ~/.mp.mpsl exists, execute it */
508 if (ERROR == NULL && stat(HOMEDIR ~ '.mp.mpsl') != NULL) {
510 local INC = [ HOMEDIR ];
515 /* errors? show in a message */
518 'timeout' => time() + 20,
527 sub mp.setup_language()
528 /* sets up the language */
530 /* set gettext() domain */
531 gettext_domain('minimum-profit', APPDIR ~ 'locale');
533 /* test if gettext() can do a basic translation */
534 if (gettext('&File') eq '&File' && ENV.LANG) {
535 /* no; try alternatives using the LANG variable */
536 local v = [ sregex('!/!g', ENV.LANG) ]; /* es_ES.UTF-8 */
537 push(v, shift(split('.', v[-1]))); /* es_ES */
538 push(v, shift(split('_', v[-1]))); /* es */
541 eval('load("lang/' ~ l ~ '.mpsl");');
552 sub mp.normalize_version(vs)
553 /* converts a version string to something usable with cmp() */
555 map(sub(e) { sprintf("%03d", e); },
557 sregex('/-.+$/', vs)));
561 sub mp.assert_version(found, minimal, package)
562 /* asserts that 'found' version of 'package' is at least 'minimal',
563 or generate a warning otherwise */
565 if (cmp(mp.normalize_version(found),
566 mp.normalize_version(minimal)) < 0) {
567 mp.alert(sprintf(L("WARNING: %s version found is %s, but %s is needed"),
568 package, found, minimal));
573 sub mp.test_versions()
574 /* tests component versions */
578 mp.assert_version(mpdm.version, '1.0.7', 'MPDM');
579 mp.assert_version(MPSL.VERSION, '1.0.7', 'MPSL');
586 load("mp_move.mpsl");
587 load("mp_edit.mpsl");
588 load("mp_file.mpsl");
589 load("mp_clipboard.mpsl");
590 load("mp_search.mpsl");
591 load("mp_tags.mpsl");
592 load("mp_syntax.mpsl");
593 load("mp_macro.mpsl");
594 load("mp_templates.mpsl");
595 load("mp_spell.mpsl");
596 load("mp_misc.mpsl");
597 load("mp_crypt.mpsl");
598 load("mp_keyseq.mpsl");
599 load("mp_session.mpsl");
600 load("mp_build.mpsl");
601 load("mp_writing.mpsl");
606 mp.process_cmdline();