-
Notifications
You must be signed in to change notification settings - Fork 5
/
mp_session.mpsl
127 lines (90 loc) · 3.08 KB
/
mp_session.mpsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Minimum Profit 5.x
A Programmer's Text Editor
Session handling.
Copyright (C) 1991-2007 Jeremy Cowgar, Angel Ortega
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
http://triptico.com
*/
/* automatically load / save sessions */
mp.config.auto_sessions = 0;
/* local sessions */
mp.config.local_sessions = 0;
/** session actions **/
mp.actions['save_session'] = sub { mp.long_op(mp.save_session); };
mp.actions['load_session'] = sub { mp.long_op(mp.load_session); };
/** default keybindings **/
/** action descriptions **/
mp.actdesc['load_session'] = LL("Load session");
mp.actdesc['save_session'] = LL("Save session");
/** code **/
sub mp.session_file()
/* returns the appropriate session saving file */
{
return (mp.config.local_sessions && './' || HOMEDIR) ~ '.mp_session';
}
sub mp.save_session()
/* saves currently open files as a session. Returns: -1, nothing to save;
-2, error saving; or 0, session correctly saved */
{
local fh, filenames;
filenames = [];
/* no named files? exit doing nothing */
if (grep(mp.docs, sub (e) { e.name ne L("<unnamed>"); }) == NULL)
return -1;
if ((fh = open(mp.session_file(), "w")) == NULL) {
return -2;
}
foreach (doc, mp.docs) {
if (doc.name ne L("<unnamed>"))
write (fh,
sprintf("%s\t%i\t%i\t%s\n",
doc.name, doc.txt.x, doc.txt.y,
(cmp(doc, mp.active()) == 0 && 'a' || '')
)
);
}
close(fh);
return 0;
}
sub mp.load_session()
/* loads a session. Returns: -1, no session, -2, user cancellation on closing
currently open documents and 0, ok */
{
local fh, l;
/* check to see if a session file can actually be opened before closing
all the editing windows */
if ((fh = open(mp.session_file(), "r")) == NULL) {
return -1;
}
if (!mp.actions.close_all()) {
close(fh);
return -2;
}
local active = -1;
while (l = read(fh)) {
local data, doc;
data = split (mp.chomp(l), "\t");
if (stat(data[0]) != NULL) {
doc = mp.open(data[0]);
doc->set_y(data[2])->set_x(data[1]);
/* if it has the 'a' flag, set as active */
if (regex(data[3], '/a/'))
active = seek(mp.docs, doc);
}
}
if (active != -1)
mp.active_i = active;
close(fh);
return 0;
}