Added a default value for WINDRES in config.sh.
[mp-5.x.git] / mp_file.mpsl
blob95fa0412c74a04a5652df8245d60836b229e6241
1 /*
3     Minimum Profit 5.x
4     A Programmer's Text Editor
6     File manipulation.
8     Copyright (C) 1991-2009 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>"));
33         if (d != -1) {
34                 mp.active_i = d;
35                 d = mp.active();
36         }
37         else
38                 d = mp.new();
41 mp.actions['next']      = sub (d) { mp.next(); };
42 mp.actions['prev']      = sub (d) { mp.prev(); };
44 mp.actions['save_as']   = sub (d) {
46         local t;
48         if ((t = mp.savefile(L("Save file as:"))) == NULL)
49                 return;
51         /* store new name */
52         d.name = t;
54         if (mp.long_op(mp.save, d) == -1)
55                 mp.alert(sprintf(L("Error saving file: %s"), ERRNO));
56         else
57                 mp.detect_syntax(d);
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);
65         else
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) {
72         if (d.txt.mod) {
73                 local r;
74                 r = mp.confirm(L("File has changed. Save changes?"));
76                 /* cancel? don't close */
77                 if (r == 0)
78                         return;
79                 if (r == 1)
80                         mp.actions.save(d);
81         }
83         mp.close();
86 mp.actions['exit']      = sub (d) {
88         local s;
90         if (mp.config.auto_sessions)
91                 mp.save_session();
93         if (mp.actions.close_all())
94                 mp.exit();
97 mp.actions['open']      = sub (d) {
99         local n;
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 */
108         local p = d.name;
110         if (d.txt.mod) {
111                 local r;
112                 r = mp.confirm(L("File has changed. Are you sure?"));
114                 /* cancel? don't close */
115                 if (r == 0 || r == 2)
116                         return;
117         }
119         mp.close();
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))
133                 mp.actions.save(d);
136 mp.actions['exec_command']      = sub (d) {
138         local t = mp.form( [
139                 { 'label' => L("System command:"),
140                   'type' => 'text',
141                   'history' => 'system' }
142                 ]);
144         if (t != NULL) {
145                 local cmd = t[0];
147                 /* does it start with a pipe? */
148                 if (regex('/^\|/', cmd)) {
149                         local p;
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);
158                                 pclose(p);
159                         }
160                         else
161                                 mp.drv.alert(
162                                         sprintf(L("Error writing to command '%s'"), cmd));
163                 }
164                 else {
165                         /* no; execute command and insert into cursor */
166                         local p;
168                         if ((p = popen(cmd, "r")) != NULL) {
169                                 local l;
171                                 mp.store_undo(d);
173                                 while ((l = read(p)) != NULL) {
174                                         mp.insert(d, mp.chomp(l));
175                                         mp.insert_newline(d);
176                                 }
178                                 pclose(p);
179                         }
180                         else
181                                 mp.drv.alert(
182                                         sprintf(L("Error reading from command '%s'"), cmd));
183                 }
184         }
187 mp.actions['close_all'] = sub {
189         local s;
191         while (s = size(mp.docs)) {
192                 local doc = mp.docs[mp.active_i];
193                 
194                 /* close current document */
195                 mp.actions.close(doc);
197                 /* if the size of the list hasn't changed,
198                    action was cancelled, so don't exit */
199                 if (s == size(mp.docs))
200                         return 0;
201         }
203         return 1;
206 mp.actions['open_under_cursor'] = sub (d) {
207         local w;
209         /* is the word under cursor file:line: ? */
210         if ((w = mp.get_word(d, '/[a-z\._0-9\/ :-]+:[0-9]+: ?/i')) != NULL) {
211                 w = split(':', w);
213                 /* drop garbage */
214                 pop(w);
216                 /* pick the line */
217                 local l = pop(w) - 1;
219                 /* open the file, rejoining with : */
220                 local n = mp.open(join(':', w));
222                 /* now move to the line */
223                 mp.search_set_y(n, l);
224         }
225         else
226         if ((w = mp.get_word(d, '/[a-z\._0-9\/ :-]+/i')) != NULL) {
227                 mp.open(w);
228         }
231 /** default key bindings **/
233 mp.keycodes['ctrl-n']           = 'next';
234 mp.keycodes['ctrl-o']           = 'open';
235 mp.keycodes['ctrl-q']           = 'exit';
236 mp.keycodes['ctrl-s']           = 'save';
237 mp.keycodes['ctrl-w']           = 'close';
238 mp.keycodes['ctrl-enter']       = 'open_under_cursor';
240 mp.keycodes['close-window']     = 'exit';
242 /** action descriptions **/
244 mp.actdesc['new']       = LL("New");
245 mp.actdesc['save']      = LL("Save...");
246 mp.actdesc['save_as']   = LL("Save as...");
247 mp.actdesc['next']      = LL("Next");
248 mp.actdesc['prev']      = LL("Previous");
249 mp.actdesc['open']      = LL("Open...");
250 mp.actdesc['exit']      = LL("Exit");
251 mp.actdesc['close']     = LL("Close");
252 mp.actdesc['revert']    = LL("Revert");
253 mp.actdesc['close_all'] = LL("Close all");
255 mp.actdesc['open_config_file']          = LL("Edit configuration file");
256 mp.actdesc['sync']                      = LL("Save modified texts");
257 mp.actdesc['exec_command']              = LL("Run system command...");
258 mp.actdesc['open_under_cursor']         = LL("Open file under cursor");
260 /** code **/
262 sub mp.chomp(str)
263 /* chomps the end of file chars from a string */
265         sregex("/\r*\n*$/", str, NULL);
269 sub mp.save(doc)
270 /* saves a file */
272         local f;
273         local s = NULL;
274         local nl = 0;
276         /* if unlink before write is desired, do it */
277         if (mp.config.unlink && (s = stat(doc.name)) != NULL)
278                 unlink(doc.name);
280         if ((f = open(doc.name, "wb")) == NULL) {
281                 /* can't write? delete name */
282                 doc.name = L("<unnamed>");
283                 return -1;
284         }
286         /* if the document has a password, save it encrypted */
287         if (doc.password)
288                 nl = mp.crypt1_save(f, doc.txt.lines, doc.password);
289         else {
290                 /* save as a plain text file */
291                 foreach (l, doc.txt.lines) {
292                         /* write a line separator if it's not the first line */
293                         if (nl)
294                                 write(f, mp.config.eol);
296                         write(f, l);
297                         nl++;
298                 }
299         }
301         close(f);
303         doc.txt.mod = 0;
305         /* set back the permissions and ownership, if available */
306         if (s != NULL) {
307                 chmod(doc.name, s[2]);
308                 chown(doc.name, s[4], s[5]);
309         }
311         return nl;
315 sub mp.new(filename, lines)
316 /* creates a new document */
318         local doc, txt;
320         txt = {};
321         txt.x = 0;
322         txt.y = 0;
323         txt.vx = 0;
324         txt.vy = 0;
325         txt.lines = lines || [ '' ];
326         txt.mod = 0;
328         doc = {};
329         doc.name = filename || L("<unnamed>");
330         doc.txt = txt;
332         doc.undo = [];
333         doc.redo = [];
335         doc.syntax = NULL;
337         /* store in the list and set as active */
338         push(mp.docs, doc);
339         mp.active_i = size(mp.docs) - 1;
341         /* autodetect syntax */
342         mp.detect_syntax(doc);
344         return doc;
348 sub mp.next()
349 /* rotates through the document list */
351         if (++mp.active_i == size(mp.docs))
352                 mp.active_i = 0;
356 sub mp.prev()
357 /* rotates through the document list, backwards */
359         if (--mp.active_i == -1)
360                 mp.active_i = size(mp.docs) - 1;
364 sub mp.close()
365 /* closes the active document */
367         local k = mp.active_i;
369         /* delete from the list */
370         adel(mp.docs, mp.active_i);
372         /* rotate if it was the last one */
373         if (mp.active_i == size(mp.docs))
374                 mp.active_i = 0;
378 sub mp.find_file_by_name(filename)
379 /* finds an open file by its name */
381         seek(map(sub(d) { d.name; }, mp.docs), filename);
385 sub mp.open(filename)
386 /* opens a new document (uses UI) */
388         local s;
390         /* looks first if the file is already open */
391         if ((s = mp.find_file_by_name(filename)) != -1) {
392                 mp.active_i = s;
393                 return mp.active();
394         }
396         if ((s = stat(filename)) == NULL) {
397                 mp.message = {
398                         'timeout' => time() + 2,
399                         'string'  => sprintf(L("New file '%s'"), filename)
400                 };
402                 return mp.new(filename);
403         }
405         /* canonicalize, if possible */
406         if (s[13] != NULL) {
407                 filename = s[13];
409                 /* look again for this filename in the open files */
410                 if ((s = mp.find_file_by_name(filename)) != -1) {
411                         mp.active_i = s;
412                         return mp.active();
413                 }
414         }
416         local d, f;
418         if ((f = open(filename, "rb")) == NULL)
419                 return NULL;
420         else {
421                 if (mp.crypt1_detect(f)) {
422                         /* password needed; ask for it */
423                         local p;
425                         if ((p = mp.form( [
426                                 { 'label'       => L("Password:"),
427                                   'type'        => 'password' }
428                                 ])) == NULL) {
429                                 /* cancel? fail, but not on error */
430                                 return NULL;
431                         }
433                         /* get the password */
434                         p = p[0];
436                         /* an empty password is equal to cancellation */
437                         if (p eq '')
438                                 return NULL;
440                         /* and load the file */
441                         d = mp.new(filename, mp.crypt1_load(f, p));
442                         d.password = p;
443                 }
444                 else {
445                         /* close file (needed for rewinding AND
446                            possible encoding autodetection) */
447                         close(f);
449                         /* reopen and read */
450                         f = open(filename, "rb");
451                         d = mp.new(filename, mp.plain_load(f));
452                 }
454                 close(f);
455         }
457         return d;