4 A Programmer's Text Editor
8 Copyright (C) 1991-2011 Angel Ortega <angel@triptico.com>
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 http://www.triptico.com
28 /** editor actions **/
30 mp.actions['new'] = sub (d) {
31 d = mp.find_file_by_name(L("<unnamed>"));
43 mp.actions['next'] = sub (d) { mp.next(); };
44 mp.actions['prev'] = sub (d) { mp.prev(); };
46 mp.actions['save_as'] = sub (d, newname) {
49 newname = mp.savefile(L("Save file as:"));
51 if (newname != NULL) {
55 if (mp.long_op(mp.save, d) == -1)
56 mp.alert(sprintf(L("Error saving file: %s"), ERRNO));
64 mp.actions['save'] = sub (d) {
66 /* name is <unnamed> or something similar; ask for one */
67 if (regex(d.name, "/^<.+>$/"))
68 mp.actions.save_as(d);
70 if (mp.long_op(mp.save, d) == -1)
71 mp.alert(sprintf(L("Error saving file: %s"), ERRNO));
76 mp.actions['close'] = sub (d) {
78 /* call all 'on_close' functions */
79 foreach (f, d.on_close) {
81 /* if it returns 0, cancel close */
89 mp.actions['exit'] = sub (d) {
90 if (mp.config.auto_sessions)
93 if (mp.actions.close_all())
97 mp.actions['open'] = sub (d) {
101 if ((n = mp.openfile(L("File to open:"))) != NULL && n ne "")
102 if (mp.long_op(mp.open, n) == NULL && ERRNO != NULL)
103 mp.alert(sprintf("Error opening '%s': %s", n, ERRNO));
108 mp.actions['revert'] = sub (d) {
109 /* save current name */
114 r = mp.confirm(L("File has changed. Are you sure?"));
116 /* cancel? don't close */
117 if (r == 0 || r == 2)
126 if (mp.long_op(mp.open, p) == NULL && ERRNO != NULL)
127 mp.alert(sprintf("Error opening '%s': %s", p, ERRNO));
138 mp.actions['open_config_file'] = sub (d) {
140 mp.open(HOMEDIR ~ ".mp.mpsl");
143 mp.actions['sync'] = sub (d) {
145 /* save all modified documents */
146 foreach (d, grep(mp.docs, sub (e) { e.txt.mod; }))
152 mp.actions['exec_command'] = sub (d, cmd) {
158 'label' => L("System command:"),
160 'history' => 'system'
170 /* does it start with a pipe? */
171 if (regex(cmd, '/^\|/')) {
174 /* yes; current document should be fed to it */
175 cmd = sregex(cmd, '/^\|/');
177 if ((p = popen(cmd, "w")) != NULL) {
180 foreach (l, mp.get_active_area(d))
181 write(p, l ~ mp.config.eol);
188 sprintf(L("Error writing to command '%s'"), cmd));
191 /* no; execute command and insert into cursor */
194 if ((p = popen(cmd, "r")) != NULL) {
200 while ((l = read(p)) != NULL) {
201 mp.insert(d, mp.chomp(l));
202 mp.insert_newline(d);
210 sprintf(L("Error reading from command '%s'"), cmd));
217 mp.actions['filter_selection'] = sub (d, cmd) {
223 'label' => L("System command:"),
225 'history' => 'system2'
237 /* if there is no selection, take full document */
238 if (d.txt.mark == NULL) {
249 /* now feed it to the command */
250 local p = popen2(cmd);
253 write(p[1], join(mp.clipboard, "\n"));
257 while ((l = read(p[0])) != NULL)
267 mp.actions['close_all'] = sub {
271 while (s = size(mp.docs)) {
272 local doc = mp.docs[mp.active_i];
274 /* close current document */
275 mp.actions.close(doc);
277 /* if the size of the list hasn't changed,
278 action was cancelled, so don't exit */
279 if (s == size(mp.docs))
286 mp.actions['open_under_cursor'] = sub (d) {
289 /* is the word under cursor file:line: ? */
290 if ((w = mp.get_word(d, '/[a-z\._0-9\/ -]+:[0-9]+:/i')) != NULL) {
297 local l = pop(w) - 1;
299 /* open the file, rejoining with : */
300 local n = mp.open(join(w, ':'));
302 /* now move to the line */
303 mp.search_set_y(n, l);
308 if ((w = mp.get_word(d, '/[a-z\._0-9\/:-]+/i')) != NULL) {
315 mp.actions['hex_view'] = sub (d) {
318 if ((n = mp.openfile(L("File to open:"))) != NULL && n ne "")
319 if (mp.long_op(mp.hex_view, n) == NULL && ERRNO != NULL)
320 mp.alert(sprintf("Error opening '%s': %s", n, ERRNO));
327 mp.actions['open_dropped_files'] = sub (d) {
328 while (size(mp.dropped_files))
329 mp.open(shift(mp.dropped_files));
333 /** default key bindings **/
335 mp.keycodes['ctrl-n'] = 'next';
336 mp.keycodes['ctrl-o'] = 'open';
337 mp.keycodes['ctrl-q'] = 'exit';
338 mp.keycodes['ctrl-s'] = 'save';
339 mp.keycodes['ctrl-w'] = 'close';
340 mp.keycodes['ctrl-enter'] = 'open_under_cursor';
341 mp.keycodes['alt-enter'] = 'open_under_cursor';
342 mp.keycodes['dropped-files'] = 'open_dropped_files';
344 mp.keycodes['close-window'] = 'exit';
346 /** action descriptions **/
348 mp.actdesc['new'] = LL("New");
349 mp.actdesc['save'] = LL("Save...");
350 mp.actdesc['save_as'] = LL("Save as...");
351 mp.actdesc['next'] = LL("Next");
352 mp.actdesc['prev'] = LL("Previous");
353 mp.actdesc['open'] = LL("Open...");
354 mp.actdesc['exit'] = LL("Exit");
355 mp.actdesc['close'] = LL("Close");
356 mp.actdesc['revert'] = LL("Revert");
357 mp.actdesc['close_all'] = LL("Close all");
359 mp.actdesc['open_config_file'] = LL("Edit configuration file");
360 mp.actdesc['sync'] = LL("Save modified texts");
361 mp.actdesc['exec_command'] = LL("Run system command...");
362 mp.actdesc['filter_selection'] = LL("Filter selection through system command...");
363 mp.actdesc['open_under_cursor'] = LL("Open file under cursor");
364 mp.actdesc['hex_view'] = LL("Hexadecimal viewer...");
365 mp.actdesc['open_dropped_files'] = LL("Open dropped files");
370 /* chomps the end of file chars from a string */
372 sregex(str, "/\r*\n*$/");
376 sub mp.save_th(f, doc)
377 /* mp.save() helper */
380 local eol = doc.eol || mp.config.eol;
384 /* save as a plain text file */
385 foreach (l, doc.txt.lines) {
386 /* write a line separator if it's not the first line */
409 /* if unlink before write is desired, do it */
410 if (mp.config.unlink && (s = stat(doc.name)) != NULL)
413 /* set the encoding for this file opening */
414 TEMP_ENCODING = doc.encoding;
416 if ((f = open(doc.name, "wb")) == NULL) {
417 /* can't write? delete name */
418 doc.name = L("<unnamed>");
424 /* if the document has a password, save it encrypted */
426 mp.crypt1_save(f, doc.txt.lines, doc.password);
432 /* set back the permissions and ownership, if available */
434 chmod(doc.name, s[2]);
435 chown(doc.name, s[4], s[5]);
446 sub mp.save_on_close(doc)
447 /* on_close function to save modified files */
452 r = mp.confirm(L("File has changed. Save changes?"));
463 sub mp.create(filename, lines)
464 /* creates a document */
473 lines: lines || [ '' ]
475 name: filename || L("<unnamed>"),
479 on_close: [ mp.save_on_close ]
486 sub mp.new(filename, lines)
487 /* creates a new document */
489 local doc = mp.create(filename, lines);
491 /* store in the list and set as active */
492 ins(mp.docs, doc, mp.active_i);
494 mp.detect_syntax(doc);
501 /* rotates through the document list */
503 if (++mp.active_i == size(mp.docs))
511 /* rotates through the document list, backwards */
513 if (--mp.active_i == -1)
514 mp.active_i = size(mp.docs) - 1;
521 /* closes the active document */
523 local k = mp.active_i;
525 /* delete from the list */
526 adel(mp.docs, mp.active_i);
528 /* rotate if it was the last one */
529 if (mp.active_i == size(mp.docs))
532 /* cannot call mp.active() */
536 sub mp.find_file_by_name(filename)
537 /* finds an open file by its name */
549 sub mp.open(filename)
550 /* opens a new document (uses UI) */
554 /* looks first if the file is already open */
555 if ((s = mp.find_file_by_name(filename)) != -1) {
560 if ((s = stat(filename)) == NULL) {
562 'timeout' => time() + 2,
563 'string' => sprintf(L("New file '%s'"), filename)
566 return mp.new(filename);
569 /* canonicalize, if possible */
573 /* look again for this filename in the open files */
574 if ((s = mp.find_file_by_name(filename)) != -1) {
582 if ((f = open(filename, "rb")) == NULL)
585 if (mp.crypt1_detect(f)) {
586 /* password needed; ask for it */
590 { 'label' => L("Password:"),
591 'type' => 'password' }
593 /* cancel? fail, but not on error */
597 /* get the password */
600 /* an empty password is equal to cancellation */
604 /* and load the file */
605 d = mp.new(filename, mp.crypt1_load(f, p));
609 /* close file (needed for rewinding AND
610 possible encoding autodetection) */
613 /* reopen and read */
614 f = open(filename, "rb");
615 d = mp.new(filename, mp.plain_load(f));
624 /* store the encoding */
625 d.encoding = DETECTED_ENCODING || ENCODING || '';
627 /* if original EOL is to be kept, store it */
628 if (mp.config.keep_eol)
629 d.eol = mp.last_seen_eol;
635 sub mp.hex_view_th(filename, d)
636 /* mp.hex_view() helper */
642 local lines = d.txt.lines;
644 local f = open(filename, 'rb');
647 if ((c = getchar(f)) != NULL)
650 if (size(l) == 16 || c == NULL) {
656 h = h ~ sprintf(' %02X', ord(v));
661 if (ord(v) < 32 || ord(v) > 126)
667 local n = 16 - size(l);
675 push(lines, join([ sprintf('| %06X', offset), h, a, ''], ' |'));
690 sub mp.hex_view(filename)
691 /* shows a file as an hex dump */
696 if ((f = open(filename, "rb")) != NULL) {
699 d = mp.new('<' ~ filename ~ ' hex view>', []);
701 d.syntax = mp.syntax.hex_view;
704 mp.hex_view_th(filename, d);