]> git.lyx.org Git - lyx.git/blob - src/session.C
hopefully fix tex2lyx linking.
[lyx.git] / src / session.C
1 /**
2  * \file session.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Bo Peng
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "session.h"
15 #include "debug.h"
16 #include "support/package.h"
17 #include "support/filetools.h"
18
19 #include <boost/filesystem/operations.hpp>
20
21 #include <fstream>
22 #include <sstream>
23 #include <algorithm>
24 #include <iterator>
25
26 using lyx::support::addName;
27 using lyx::support::package;
28
29 namespace fs = boost::filesystem;
30
31 using std::vector;
32 using std::getline;
33 using std::string;
34 using std::ifstream;
35 using std::ofstream;
36 using std::istream;
37 using std::ostream;
38 using std::endl;
39 using std::istringstream;
40 using std::copy;
41 using std::find;
42 using std::ostream_iterator;
43
44 namespace {
45
46 string const sec_lastfiles = "[recent files]";
47 string const sec_lastfilepos = "[cursor positions]";
48 string const sec_lastopened = "[last opened files]";
49 string const sec_bookmarks = "[bookmarks]";
50 string const sec_session = "[session info]";
51 string const sec_toolbars = "[toolbars]";
52
53 } // anon namespace
54
55
56 namespace lyx {
57
58 LastFilesSection::LastFilesSection(unsigned int num) :
59         default_num_last_files(4),
60         absolute_max_last_files(100)
61 {
62         setNumberOfLastFiles(num);
63 }
64
65
66 void LastFilesSection::read(istream & is)
67 {
68         string tmp;
69         do {
70                 char c = is.peek();
71                 if (c == '[')
72                         break;
73                 getline(is, tmp);
74                 // read lastfiles
75                 if (!fs::exists(tmp) || lastfiles.size() >= num_lastfiles)
76                         continue;
77                 lastfiles.push_back(tmp);
78         } while (is.good());
79 }
80
81
82 void LastFilesSection::write(ostream & os) const
83 {
84         os << '\n' << sec_lastfiles << '\n';
85         copy(lastfiles.begin(), lastfiles.end(),
86              ostream_iterator<string>(os, "\n"));
87 }
88
89
90 void LastFilesSection::add(string const & file)
91 {
92         // If file already exist, delete it and reinsert at front.
93         LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
94         if (it != lastfiles.end())
95                 lastfiles.erase(it);
96         lastfiles.push_front(file);
97         if (lastfiles.size() > num_lastfiles)
98                 lastfiles.pop_back();
99 }
100
101
102 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
103 {
104         if (0 < no && no <= absolute_max_last_files)
105                 num_lastfiles = no;
106         else {
107                 lyxerr << "LyX: session: too many last files\n"
108                        << "\tdefault (=" << default_num_last_files
109                        << ") used." << endl;
110                 num_lastfiles = default_num_last_files;
111         }
112 }
113
114
115 void LastOpenedSection::read(istream & is)
116 {
117         string tmp;
118         do {
119                 char c = is.peek();
120                 if (c == '[')
121                         break;
122                 getline(is, tmp);
123                 if (!fs::exists(tmp))
124                         continue;
125                 lastopened.push_back(tmp);
126         } while (is.good());
127 }
128
129
130 void LastOpenedSection::write(ostream & os) const
131 {
132         os << '\n' << sec_lastopened << '\n';
133         copy(lastopened.begin(), lastopened.end(),
134              ostream_iterator<string>(os, "\n"));
135 }
136
137
138 void LastOpenedSection::add(string const & file)
139 {
140         lastopened.push_back(file);
141 }
142
143
144 void LastOpenedSection::clear()
145 {
146         lastopened.clear();
147 }
148
149
150 void LastFilePosSection::read(istream & is)
151 {
152         string tmp;
153         do {
154                 char c = is.peek();
155                 if (c == '[')
156                         break;
157                 getline(is, tmp);
158                 // read lastfilepos
159                 // pos, file\n
160                 pit_type pit;
161                 pos_type pos;
162                 string fname;
163                 istringstream itmp(tmp);
164                 itmp >> pit;
165                 itmp.ignore(2);  // ignore ", "
166                 itmp >> pos;
167                 itmp.ignore(2);  // ignore ", "
168                 itmp >> fname;
169                 if (!fs::exists(fname) || lastfilepos.size() >= num_lastfilepos)
170                         continue;
171                 lastfilepos[fname] = boost::tie(pit, pos);
172         } while (is.good());
173 }
174
175
176 void LastFilePosSection::write(ostream & os) const
177 {
178         os << '\n' << sec_lastfilepos << '\n';
179         for (FilePosMap::const_iterator file = lastfilepos.begin();
180                 file != lastfilepos.end(); ++file) {
181                 os << file->second.get<0>() << ", "
182                     << file->second.get<1>() << ", "
183                     << file->first << '\n';
184         }
185 }
186
187
188 void LastFilePosSection::save(string const & fname, FilePos pos)
189 {
190         lastfilepos[fname] = pos;
191 }
192
193
194 LastFilePosSection::FilePos LastFilePosSection::load(string const & fname) const
195 {
196         FilePosMap::const_iterator entry = lastfilepos.find(fname);
197         // Has position information, return it.
198         if (entry != lastfilepos.end())
199                 return entry->second;
200         // Not found, return the first paragraph
201         else
202                 return 0;
203 }
204
205
206 void BookmarksSection::read(istream & is)
207 {
208         string tmp;
209         do {
210                 char c = is.peek();
211                 if (c == '[')
212                         break;
213                 getline(is, tmp);
214                 // read bookmarks
215                 // id, pos, file\n
216                 unsigned int id;
217                 pos_type pos;
218                 string fname;
219                 istringstream itmp(tmp);
220                 itmp >> id;
221                 itmp.ignore(2);  // ignore ", "
222                 itmp >> pos;
223                 itmp.ignore(2);  // ignore ", "
224                 itmp >> fname;
225                 // only load valid bookmarks
226                 if (bookmarks.size() < max_bookmarks && fs::exists(fname))
227                         bookmarks.push_back(Bookmark(fname, id, pos));
228         } while (is.good());
229 }
230
231
232 void BookmarksSection::write(ostream & os) const
233 {
234         os << '\n' << sec_bookmarks << '\n';
235         for (size_t i = 0; i < bookmarks.size(); ++i) {
236                 os << bookmarks[i].par_id << ", "
237                    << bookmarks[i].par_pos << ", "
238                    << bookmarks[i].filename << '\n';
239         }
240 }
241
242
243 void BookmarksSection::save(std::string const & fname, int par_id, pos_type par_pos, bool persistent)
244 {
245         if (persistent) {
246                 bookmarks.push_front(Bookmark(fname, par_id, par_pos));
247                 if (bookmarks.size() > max_bookmarks)
248                         bookmarks.pop_back();
249                 }
250         else
251                 temp_bookmark = Bookmark(fname, par_id, par_pos);
252 }
253
254
255 bool BookmarksSection::isValid(unsigned int i) const
256 {
257         // i == 0, or in the queue
258         return i <= bookmarks.size();
259 }
260
261
262 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
263 {
264         if (i == 0)
265                 return temp_bookmark;
266         else
267                 return bookmarks[i-1];
268 }
269
270
271 void ToolbarSection::read(istream & is)
272 {
273         string tmp;
274         do {
275                 char c = is.peek();
276                 if (c == '[')
277                         break;
278                 getline(is, tmp);
279
280                 // Read session info, saved as key/value pairs
281                 // would better yell if pos returns npos
282                 string::size_type pos = tmp.find_first_of(" = ");
283                 // silently ignore lines without " = "
284                 if (pos != string::npos) {
285                         string key = tmp.substr(0, pos);
286                         int state;
287                         int location;
288                         istringstream value(tmp.substr(pos + 3));
289                         value >> state;
290                         value.ignore(1); // ignore " "
291                         value >> location;
292                         toolbars[key] = ToolbarInfo(state, location);
293                 }
294         } while (is.good());
295 }
296
297
298 void ToolbarSection::write(ostream & os) const
299 {
300         os << '\n' << sec_toolbars << '\n';
301         for (ToolbarMap::const_iterator tb = toolbars.begin();
302                 tb != toolbars.end(); ++tb) {
303                 os << tb->first << " = "
304                   << static_cast<int>(tb->second.state) << " "
305                   << static_cast<int>(tb->second.location) << '\n';
306         }
307 }
308
309
310 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
311 {
312         return toolbars[name];
313 }
314
315
316 void SessionInfoSection::read(istream & is)
317 {
318         string tmp;
319         do {
320                 char c = is.peek();
321                 if (c == '[')
322                         break;
323                 getline(is, tmp);
324
325                 // Read session info, saved as key/value pairs
326                 // would better yell if pos returns npos
327                 string::size_type pos = tmp.find_first_of(" = ");
328                 // silently ignore lines without " = "
329                 if (pos != string::npos) {
330                         string key = tmp.substr(0, pos);
331                         string value = tmp.substr(pos + 3);
332                         sessioninfo[key] = value;
333                 }
334         } while (is.good());
335 }
336
337
338 void SessionInfoSection::write(ostream & os) const
339 {
340         os << '\n' << sec_session << '\n';
341         for (MiscInfo::const_iterator val = sessioninfo.begin();
342                 val != sessioninfo.end(); ++val) {
343                 os << val->first << " = " << val->second << '\n';
344         }
345 }
346
347
348 void SessionInfoSection::save(string const & key, string const & value)
349 {
350         sessioninfo[key] = value;
351 }
352
353
354 string const SessionInfoSection::load(string const & key, bool release)
355 {
356         MiscInfo::const_iterator pos = sessioninfo.find(key);
357         string value;
358         if (pos != sessioninfo.end())
359                 value = pos->second;
360         if (release)
361                 sessioninfo.erase(key);
362         return value;
363 }
364
365
366 Session::Session(unsigned int num) :
367         last_files(num)
368 {
369         // locate the session file
370         // note that the session file name 'session' is hard-coded
371         session_file = addName(package().user_support(), "session");
372         //
373         readFile();
374 }
375
376
377 void Session::readFile()
378 {
379         // we will not complain if we can't find session_file nor will
380         // we issue a warning. (Lgb)
381         ifstream is(session_file.c_str());
382         string tmp;
383
384         while (getline(is, tmp)) {
385                 // Ignore comments, empty line or line stats with ' '
386                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
387                         continue;
388
389                 // Determine section id
390                 if (tmp == sec_lastfiles)
391                         lastFiles().read(is);
392                 else if (tmp == sec_lastopened)
393                         lastOpened().read(is);
394                 else if (tmp == sec_lastfilepos)
395                         lastFilePos().read(is);
396                 else if (tmp == sec_bookmarks)
397                         bookmarks().read(is);
398                 else if (tmp == sec_toolbars)
399                         toolbars().read(is);
400                 else if (tmp == sec_session)
401                         sessionInfo().read(is);
402                 else
403                         lyxerr << "LyX: Warning: unknown Session section: " << tmp << endl;
404         }
405 }
406
407
408 void Session::writeFile() const
409 {
410         ofstream os(session_file.c_str());
411         if (os) {
412                 os << "## Automatically generated lyx session file \n"
413                     << "## Editing this file manually may cause lyx to crash.\n";
414
415                 lastFiles().write(os);
416                 lastOpened().write(os);
417                 lastFilePos().write(os);
418                 bookmarks().write(os);
419                 toolbars().write(os);
420                 sessionInfo().write(os);
421         } else
422                 lyxerr << "LyX: Warning: unable to save Session: "
423                        << session_file << endl;
424 }
425
426 }