4 A Programmer's Text Editor
8 Copyright (C) 1991-2008 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>"));
41 mp.actions['next'] = sub (d) { mp.next(); };
42 mp.actions['prev'] = sub (d) { mp.prev(); };
44 mp.actions['save_as'] = sub (d) {
48 if ((t = mp.savefile(L("Save file as:"))) == NULL)
54 if (mp.long_op(mp.save, d) == -1)
55 mp.alert(sprintf(L("Error saving file: %s"), ERRNO));
60 mp.actions['save'] = sub (d) {
62 /* name is <unnamed> or something similar; ask for one */
63 if (regex("/^<.+>$/", d.name))
64 mp.actions.save_as(d);
66 if (mp.long_op(mp.save, d) == -1)
67 mp.alert(sprintf(L("Error saving file: %s"), ERRNO));
70 mp.actions['close'] = sub (d) {
74 r = mp.confirm(L("File has changed. Save changes?"));
76 /* cancel? don't close */
86 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));
106 mp.actions['revert'] = sub (d) {
107 /* save current name */
112 r = mp.confirm(L("File has changed. Are you sure?"));
114 /* cancel? don't close */
115 if (r == 0 || r == 2)
120 if (mp.long_op(mp.open, p) == NULL && ERRNO != NULL)
121 mp.alert(sprintf("Error opening '%s': %s", p, ERRNO));
124 mp.actions['open_config_file'] = sub (d) {
126 mp.open(HOMEDIR ~ ".mp.mpsl");
129 mp.actions['sync'] = sub (d) {
131 /* save all modified documents */
132 foreach (d, grep(sub (e) { e.txt.mod; }, mp.docs))
136 mp.actions['exec_command'] = sub (d) {
139 { 'label' => L("System command:"),
141 'history' => 'system' }
147 /* does it start with a pipe? */
148 if (regex('/^\|/', cmd)) {
151 /* yes; current document should be fed to it */
152 cmd = sregex('/^\|/', cmd, NULL);
154 if ((p = popen(cmd, "w")) != NULL) {
155 foreach (l, mp.get_active_area(d))
156 write(p, l ~ mp.config.eol);
162 sprintf(L("Error writing to command '%s'"), cmd));
165 /* no; execute command and insert into cursor */
168 if ((p = popen(cmd, "r")) != NULL) {
173 while ((l = read(p)) != NULL)
180 sprintf(L("Error reading from command '%s'"), cmd));
185 mp.actions['close_all'] = sub {
189 while (s = size(mp.docs)) {
190 local doc = mp.docs[mp.active_i];
192 /* close current document */
193 mp.actions.close(doc);
195 /* if the size of the list hasn't changed,
196 action was cancelled, so don't exit */
197 if (s == size(mp.docs))
204 mp.actions['open_under_cursor'] = sub (d) {
207 /* is the word under cursor file:line: ? */
208 if ((w = mp.get_word(d, '/[a-z\.\_0-9\/-]+:[0-9]+: ?/i')) != NULL) {
212 local n = mp.open(w[0]);
214 /* now move to the line */
215 mp.set_y(n, w[1] - 1);
218 if ((w = mp.get_word(d, '/[a-z\.\_0-9\/-]+/i')) != NULL) {
223 /** default key bindings **/
225 mp.keycodes['ctrl-n'] = 'next';
226 mp.keycodes['ctrl-o'] = 'open';
227 mp.keycodes['ctrl-q'] = 'exit';
228 mp.keycodes['ctrl-s'] = 'save';
229 mp.keycodes['ctrl-w'] = 'close';
230 mp.keycodes['ctrl-enter'] = 'open_under_cursor';
232 mp.keycodes['close-window'] = 'exit';
234 /** action descriptions **/
236 mp.actdesc['new'] = LL("New");
237 mp.actdesc['save'] = LL("Save...");
238 mp.actdesc['save_as'] = LL("Save as...");
239 mp.actdesc['next'] = LL("Next");
240 mp.actdesc['prev'] = LL("Previous");
241 mp.actdesc['open'] = LL("Open...");
242 mp.actdesc['exit'] = LL("Exit");
243 mp.actdesc['close'] = LL("Close");
244 mp.actdesc['revert'] = LL("Revert");
245 mp.actdesc['close_all'] = LL("Close all");
247 mp.actdesc['open_config_file'] = LL("Edit configuration file");
248 mp.actdesc['open_templates_file'] = LL("Edit templates file");
249 mp.actdesc['sync'] = LL("Save modified texts");
250 mp.actdesc['exec_command'] = LL("Run system command...");
251 mp.actdesc['open_under_cursor'] = LL("Open file under cursor");
256 /* chomps the end of file chars from a string */
258 sregex("/\r?\n$/", str, NULL);
269 /* if unlink before write is desired, do it */
270 if (mp.config.unlink && (s = stat(doc.name)) != NULL)
273 if ((f = open(doc.name, "wb")) == NULL) {
274 /* can't write? delete name */
275 doc.name = L("<unnamed>");
279 /* if the document has a password, save it encrypted */
281 nl = mp.crypt1_save(f, doc.txt.lines, doc.password);
283 /* save as a plain text file */
284 foreach (l, doc.txt.lines) {
285 /* write a line separator if it's not the first line */
287 write(f, mp.config.eol);
298 /* set back the permissions and ownership, if available */
300 chmod(doc.name, s[2]);
301 chown(doc.name, s[4], s[5]);
308 sub mp.new(filename, lines)
309 /* creates a new document */
318 txt.lines = lines || [ '' ];
322 doc.name = filename || L("<unnamed>");
330 /* store in the list and set as active */
332 mp.active_i = size(mp.docs) - 1;
334 /* autodetect syntax */
335 mp.detect_syntax(doc);
342 /* rotates through the document list */
344 if (++mp.active_i == size(mp.docs))
350 /* rotates through the document list, backwards */
352 if (--mp.active_i == -1)
353 mp.active_i = size(mp.docs) - 1;
358 /* closes the active document */
360 local k = mp.active_i;
362 /* delete from the list */
363 adel(mp.docs, mp.active_i);
365 /* rotate if it was the last one */
366 if (mp.active_i == size(mp.docs))
371 sub mp.find_file_by_name(filename)
372 /* finds an open file by its name */
374 seek(map(sub(d) { d.name; }, mp.docs), filename);
378 sub mp.open(filename)
379 /* opens a new document (uses UI) */
383 /* looks first if the file is already open */
384 if ((s = mp.find_file_by_name(filename)) != -1) {
389 if ((s = stat(filename)) == NULL) {
391 'timeout' => time() + 2,
392 'string' => sprintf(L("New file '%s'"), filename)
395 return mp.new(filename);
398 /* canonicalize, if possible */
402 /* look again for this filename in the open files */
403 if ((s = mp.find_file_by_name(filename)) != -1) {
411 if ((f = open(filename, "rb")) == NULL)
414 if (mp.crypt1_detect(f)) {
415 /* password needed; ask for it */
419 { 'label' => L("Password:"),
420 'type' => 'password' }
422 /* cancel? fail, but not on error */
426 /* get the password */
429 /* an empty password is equal to cancellation */
433 /* and load the file */
434 d = mp.new(filename, mp.crypt1_load(f, p));
438 /* close file (needed for rewinding AND
439 possible encoding autodetection) */
442 /* reopen and read */
443 f = open(filename, "rb");
444 d = mp.new(filename, mp.plain_load(f));