Do not use backslash-b to select backspaces.
[mp-5.x.git] / mp_session.mpsl
blobe4bdbdcbb65ad870e15337a5d1bc69b637203fa3
1 /*
3     Minimum Profit 5.x
4     A Programmer's Text Editor
6     Session handling.
8     Copyright (C) 1991-2007 Jeremy Cowgar, Angel Ortega
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 /* automatically load / save sessions */
29 mp.config.auto_sessions = 0;
31 /* local sessions */
32 mp.config.local_sessions = 0;
34 /** session actions **/
36 mp.actions['save_session'] = sub (d) { mp.long_op(mp.save_session); };
37 mp.actions['load_session'] = sub (d) { mp.long_op(mp.load_session); };
39 /** default keybindings **/
41 /** action descriptions **/
43 mp.actdesc['load_session'] = LL("Load session");
44 mp.actdesc['save_session'] = LL("Save session");
46 /** code **/
48 sub mp.session_file()
49 /* returns the appropriate session saving file */
51         return (mp.config.local_sessions && './' || HOMEDIR) ~ '.mp_session';
55 sub mp.save_session()
56 /* saves currently open files as a session. Returns: -1, nothing to save;
57    -2, error saving; or 0, session correctly saved */
59         local fh, filenames;
60         
61         filenames = [];
62         
63         /* no named files? exit doing nothing */
64         if (grep(mp.docs, sub (e) { e.name ne L("<unnamed>"); }) == NULL)
65                 return -1;
67         if ((fh = open(mp.session_file(), "w")) == NULL) {
68                 return -2;
69         }
70         
71         foreach (doc, mp.docs) {
72                 if (doc.name ne L("<unnamed>"))
73                         write (fh,
74                                 sprintf("%s\t%i\t%i\t%s\n",
75                                         doc.name, doc.txt.x, doc.txt.y,
76                                         (cmp(doc, mp.active()) == 0 && 'a' || '')
77                                 )
78                         );
79         }
80         
81         close(fh);
83         return 0;
87 sub mp.load_session()
88 /* loads a session. Returns: -1, no session, -2, user cancellation on closing
89    currently open documents and 0, ok */
91         local fh, l;
92         
93         /* check to see if a session file can actually be opened before closing
94            all the editing windows */
95         if ((fh = open(mp.session_file(), "r")) == NULL) {
96                 return -1;
97         }
99         if (!mp.actions.close_all()) {
100                 close(fh);
101                 return -2;
102         }
104         local active = -1;
106         while (l = read(fh)) {
107                 local data, doc;
108                 data = split (mp.chomp(l), "\t");
109                 
110                 if (stat(data[0]) != NULL) {
111                         doc = mp.open(data[0]);
112                         
113                         mp.set_y(doc, data[2]);
114                         mp.set_x(doc, data[1]);
116                         /* if it has the 'a' flag, set as active */
117                         if (regex(data[3], '/a/'))
118                                 active = seek(mp.docs, doc);
119                 }
120         }
122         if (active != -1)
123                 mp.active_i = active;
124         
125         close(fh);
127         return 0;