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