]> git.lyx.org Git - lyx.git/blob - src/session.C
Add margin to paragraph dialog.
[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::absolutePath;
27 using lyx::support::addName;
28 using lyx::support::FileName;
29 using lyx::support::package;
30
31 namespace fs = boost::filesystem;
32
33 using std::vector;
34 using std::getline;
35 using std::string;
36 using std::ifstream;
37 using std::ofstream;
38 using std::istream;
39 using std::ostream;
40 using std::endl;
41 using std::istringstream;
42 using std::copy;
43 using std::find;
44 using std::ostream_iterator;
45
46 namespace {
47
48 string const sec_lastfiles = "[recent files]";
49 string const sec_lastfilepos = "[cursor positions]";
50 string const sec_lastopened = "[last opened files]";
51 string const sec_bookmarks = "[bookmarks]";
52 string const sec_session = "[session info]";
53 string const sec_toolbars = "[toolbars]";
54
55 } // anon namespace
56
57
58 namespace lyx {
59
60 LastFilesSection::LastFilesSection(unsigned int num) :
61         default_num_last_files(4),
62         absolute_max_last_files(100)
63 {
64         setNumberOfLastFiles(num);
65 }
66
67
68 void LastFilesSection::read(istream & is)
69 {
70         string tmp;
71         do {
72                 char c = is.peek();
73                 if (c == '[')
74                         break;
75                 getline(is, tmp);
76                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !absolutePath(tmp))
77                         continue;
78                 
79                 // read lastfiles
80                 FileName const file(tmp);
81                 if (fs::exists(file.toFilesystemEncoding()) &&
82                     !fs::is_directory(file.toFilesystemEncoding()) &&
83                     lastfiles.size() < num_lastfiles)
84                         lastfiles.push_back(file);
85                 else 
86                         LYXERR(Debug::INIT) << "LyX: Warning: Ignore last file: " << tmp << endl;
87         } while (is.good());
88 }
89
90
91 void LastFilesSection::write(ostream & os) const
92 {
93         os << '\n' << sec_lastfiles << '\n';
94         copy(lastfiles.begin(), lastfiles.end(),
95              ostream_iterator<FileName>(os, "\n"));
96 }
97
98
99 void LastFilesSection::add(FileName const & file)
100 {
101         // If file already exist, delete it and reinsert at front.
102         LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
103         if (it != lastfiles.end())
104                 lastfiles.erase(it);
105         lastfiles.push_front(file);
106         if (lastfiles.size() > num_lastfiles)
107                 lastfiles.pop_back();
108 }
109
110
111 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
112 {
113         if (0 < no && no <= absolute_max_last_files)
114                 num_lastfiles = no;
115         else {
116                 LYXERR(Debug::INIT) << "LyX: session: too many last files\n"
117                        << "\tdefault (=" << default_num_last_files
118                        << ") used." << endl;
119                 num_lastfiles = default_num_last_files;
120         }
121 }
122
123
124 void LastOpenedSection::read(istream & is)
125 {
126         string tmp;
127         do {
128                 char c = is.peek();
129                 if (c == '[')
130                         break;
131                 getline(is, tmp);
132                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !absolutePath(tmp))
133                         continue;
134
135                 FileName const file(tmp);
136                 if (fs::exists(file.toFilesystemEncoding()) &&
137                     !fs::is_directory(file.toFilesystemEncoding()))
138                         lastopened.push_back(file);
139                 else
140                         LYXERR(Debug::INIT) << "LyX: Warning: Ignore last opened file: " << tmp << endl;
141         } while (is.good());
142 }
143
144
145 void LastOpenedSection::write(ostream & os) const
146 {
147         os << '\n' << sec_lastopened << '\n';
148         copy(lastopened.begin(), lastopened.end(),
149              ostream_iterator<FileName>(os, "\n"));
150 }
151
152
153 void LastOpenedSection::add(FileName const & file)
154 {
155         lastopened.push_back(file);
156 }
157
158
159 void LastOpenedSection::clear()
160 {
161         lastopened.clear();
162 }
163
164
165 void LastFilePosSection::read(istream & is)
166 {
167         string tmp;
168         do {
169                 char c = is.peek();
170                 if (c == '[')
171                         break;
172                 getline(is, tmp);
173                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
174                         continue;
175
176                 try {
177                         // read lastfilepos
178                         // pos, file\n
179                         pit_type pit;
180                         pos_type pos;
181                         string fname;
182                         istringstream itmp(tmp);
183                         itmp >> pit;
184                         itmp.ignore(2);  // ignore ", "
185                         itmp >> pos;
186                         itmp.ignore(2);  // ignore ", "
187                         getline(itmp, fname);
188                         if (!absolutePath(fname))
189                                 continue;
190                         FileName const file(fname);
191                         if (fs::exists(file.toFilesystemEncoding()) &&
192                             !fs::is_directory(file.toFilesystemEncoding()) &&
193                             lastfilepos.size() < num_lastfilepos)
194                                 lastfilepos[file] = boost::tie(pit, pos);
195                         else
196                                 LYXERR(Debug::INIT) << "LyX: Warning: Ignore pos of last file: " << fname << endl;
197                 } catch (...) {
198                         LYXERR(Debug::INIT) << "LyX: Warning: unknown pos of last file: " << tmp << endl;
199                 }
200         } while (is.good());
201 }
202
203
204 void LastFilePosSection::write(ostream & os) const
205 {
206         os << '\n' << sec_lastfilepos << '\n';
207         for (FilePosMap::const_iterator file = lastfilepos.begin();
208                 file != lastfilepos.end(); ++file) {
209                 os << file->second.get<0>() << ", "
210                     << file->second.get<1>() << ", "
211                     << file->first << '\n';
212         }
213 }
214
215
216 void LastFilePosSection::save(FileName const & fname, FilePos pos)
217 {
218         lastfilepos[fname] = pos;
219 }
220
221
222 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
223 {
224         FilePosMap::const_iterator entry = lastfilepos.find(fname);
225         // Has position information, return it.
226         if (entry != lastfilepos.end())
227                 return entry->second;
228         // Not found, return the first paragraph
229         else
230                 return 0;
231 }
232
233
234 void BookmarksSection::clear()
235 {
236         // keep bookmark[0], the temporary one
237         bookmarks.resize(1);
238         bookmarks.resize(max_bookmarks + 1);
239 }
240
241
242 void BookmarksSection::read(istream & is)
243 {
244         string tmp;
245         do {
246                 char c = is.peek();
247                 if (c == '[')
248                         break;
249                 getline(is, tmp);
250                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
251                         continue;
252
253                 try {
254                         // read bookmarks
255                         // idx, pit, pos, file\n
256                         unsigned int idx;
257                         pit_type pit;
258                         pos_type pos;
259                         string fname;
260                         istringstream itmp(tmp);
261                         itmp >> idx;
262                         itmp.ignore(2);  // ignore ", "
263                         itmp >> pit;
264                         itmp.ignore(2);  // ignore ", "
265                         itmp >> pos;
266                         itmp.ignore(2);  // ignore ", "
267                         getline(itmp, fname);
268                         if (!absolutePath(fname))
269                                 continue;
270                         FileName const file(fname);
271                         // only load valid bookmarks
272                         if (fs::exists(file.toFilesystemEncoding()) &&
273                             !fs::is_directory(file.toFilesystemEncoding()) &&
274                             idx <= max_bookmarks)
275                                 bookmarks[idx] = Bookmark(file, pit, 0, pos);
276                         else
277                                 LYXERR(Debug::INIT) << "LyX: Warning: Ignore bookmark of file: " << fname << endl;
278                 } catch (...) {
279                         LYXERR(Debug::INIT) << "LyX: Warning: unknown Bookmark info: " << tmp << endl;
280                 }
281         } while (is.good());
282 }
283
284
285 void BookmarksSection::write(ostream & os) const
286 {
287         os << '\n' << sec_bookmarks << '\n';
288         for (size_t i = 1; i <= max_bookmarks; ++i) {
289                 if (isValid(i))
290                         os << i << ", "
291                            << bookmarks[i].par_pit << ", "
292                            << bookmarks[i].par_pos << ", "
293                            << bookmarks[i].filename << '\n';
294         }
295 }
296
297
298 void BookmarksSection::save(FileName const & fname, pit_type par_pit, int par_id, pos_type par_pos, unsigned int idx)
299 {
300         // silently ignore bookmarks when idx is out of range
301         if (idx <= max_bookmarks)
302                 bookmarks[idx] = Bookmark(fname, par_pit, par_id, par_pos);
303 }
304
305
306 bool BookmarksSection::isValid(unsigned int i) const
307 {
308         return i <= max_bookmarks && !bookmarks[i].filename.empty();
309 }
310
311
312 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
313 {
314         return bookmarks[i];
315 }
316
317
318 void ToolbarSection::read(istream & is)
319 {
320         string tmp;
321         do {
322                 char c = is.peek();
323                 if (c == '[')
324                         break;
325                 getline(is, tmp);
326                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
327                         continue;
328
329                 try {
330                         // Read session info, saved as key/value pairs
331                         // would better yell if pos returns npos
332                         string::size_type pos = tmp.find_first_of(" = ");
333                         // silently ignore lines without " = "
334                         if (pos != string::npos) {
335                                 string key = tmp.substr(0, pos);
336                                 int state;
337                                 int location;
338                                 int posx;
339                                 int posy;
340                                 istringstream value(tmp.substr(pos + 3));
341                                 value >> state;
342                                 value >> location;
343                                 value >> posx;
344                                 value >> posy;
345                                 toolbars.push_back(boost::make_tuple(key, ToolbarInfo(state, location, posx, posy)));
346                         } else 
347                                 LYXERR(Debug::INIT) << "LyX: Warning: Ignore toolbar info: " << tmp << endl;
348                 } catch (...) {
349                         LYXERR(Debug::INIT) << "LyX: Warning: unknown Toolbar info: " << tmp << endl;
350                 }
351         } while (is.good());
352         // sort the toolbars by location, line and position
353         std::sort(toolbars.begin(), toolbars.end());
354 }
355
356
357 void ToolbarSection::write(ostream & os) const
358 {
359         os << '\n' << sec_toolbars << '\n';
360         for (ToolbarList::const_iterator tb = toolbars.begin();
361                 tb != toolbars.end(); ++tb) {
362                 os << tb->get<0>() << " = "
363                   << static_cast<int>(tb->get<1>().state) << " "
364                   << static_cast<int>(tb->get<1>().location) << " "
365                   << tb->get<1>().posx << " "
366                   << tb->get<1>().posy << '\n';
367         }
368 }
369
370
371 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
372 {
373         for (ToolbarList::iterator tb = toolbars.begin();
374                 tb != toolbars.end(); ++tb)
375                 if (tb->get<0>() == name)
376                         return tb->get<1>();
377         // add a new item
378         toolbars.push_back(boost::make_tuple(name, ToolbarSection::ToolbarInfo()));
379         return toolbars.back().get<1>();
380 }
381
382
383 bool operator<(ToolbarSection::ToolbarItem const & a, ToolbarSection::ToolbarItem const & b)
384 {
385         ToolbarSection::ToolbarInfo lhs = a.get<1>();
386         ToolbarSection::ToolbarInfo rhs = b.get<1>();
387         // on if before off
388         if (lhs.state != rhs.state)
389                 return static_cast<int>(lhs.state) < static_cast<int>(rhs.state);
390         // order of dock does not really matter
391         if (lhs.location != rhs.location)
392                 return static_cast<int>(lhs.location) < static_cast<int>(rhs.location);
393         // if the same dock, the order depends on position
394         if (lhs.location == ToolbarSection::ToolbarInfo::TOP ||
395                 lhs.location == ToolbarSection::ToolbarInfo::BOTTOM)
396                 return lhs.posy < rhs.posy || (lhs.posy == rhs.posy && lhs.posx < rhs.posx);
397         else if (lhs.location == ToolbarSection::ToolbarInfo::LEFT ||
398                 lhs.location == ToolbarSection::ToolbarInfo::RIGHT)
399                 return lhs.posx < rhs.posx || (lhs.posx == rhs.posx && lhs.posy < rhs.posy);
400         else
401                 return true;
402 }
403
404
405 void SessionInfoSection::read(istream & is)
406 {
407         string tmp;
408         do {
409                 char c = is.peek();
410                 if (c == '[')
411                         break;
412                 getline(is, tmp);
413                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
414                         continue;
415
416                 try {
417                         // Read session info, saved as key/value pairs
418                         // would better yell if pos returns npos
419                         string::size_type pos = tmp.find_first_of(" = ");
420                         // silently ignore lines without " = "
421                         if (pos != string::npos) {
422                                 string key = tmp.substr(0, pos);
423                                 string value = tmp.substr(pos + 3);
424                                 sessioninfo[key] = value;
425                         } else
426                                 LYXERR(Debug::INIT) << "LyX: Warning: Ignore session info: " << tmp << endl;
427                 } catch (...) {
428                         LYXERR(Debug::INIT) << "LyX: Warning: unknown Session info: " << tmp << endl;
429                 }
430         } while (is.good());
431 }
432
433
434 void SessionInfoSection::write(ostream & os) const
435 {
436         os << '\n' << sec_session << '\n';
437         for (MiscInfo::const_iterator val = sessioninfo.begin();
438                 val != sessioninfo.end(); ++val) {
439                 os << val->first << " = " << val->second << '\n';
440         }
441 }
442
443
444 void SessionInfoSection::save(string const & key, string const & value)
445 {
446         sessioninfo[key] = value;
447 }
448
449
450 string const SessionInfoSection::load(string const & key, bool release)
451 {
452         MiscInfo::const_iterator pos = sessioninfo.find(key);
453         string value;
454         if (pos != sessioninfo.end())
455                 value = pos->second;
456         if (release)
457                 sessioninfo.erase(key);
458         return value;
459 }
460
461
462 Session::Session(unsigned int num) :
463         last_files(num)
464 {
465         // locate the session file
466         // note that the session file name 'session' is hard-coded
467         session_file = FileName(addName(package().user_support().absFilename(), "session"));
468         //
469         readFile();
470 }
471
472
473 void Session::readFile()
474 {
475         // we will not complain if we can't find session_file nor will
476         // we issue a warning. (Lgb)
477         ifstream is(session_file.toFilesystemEncoding().c_str());
478         string tmp;
479
480         while (getline(is, tmp)) {
481                 // Ignore comments, empty line or line stats with ' '
482                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
483                         continue;
484
485                 // Determine section id
486                 if (tmp == sec_lastfiles)
487                         lastFiles().read(is);
488                 else if (tmp == sec_lastopened)
489                         lastOpened().read(is);
490                 else if (tmp == sec_lastfilepos)
491                         lastFilePos().read(is);
492                 else if (tmp == sec_bookmarks)
493                         bookmarks().read(is);
494                 else if (tmp == sec_toolbars)
495                         toolbars().read(is);
496                 else if (tmp == sec_session)
497                         sessionInfo().read(is);
498                 else
499                         LYXERR(Debug::INIT) << "LyX: Warning: unknown Session section: " << tmp << endl;
500         }
501 }
502
503
504 void Session::writeFile() const
505 {
506         ofstream os(session_file.toFilesystemEncoding().c_str());
507         if (os) {
508                 os << "## Automatically generated lyx session file \n"
509                     << "## Editing this file manually may cause lyx to crash.\n";
510
511                 lastFiles().write(os);
512                 lastOpened().write(os);
513                 lastFilePos().write(os);
514                 bookmarks().write(os);
515                 toolbars().write(os);
516                 sessionInfo().write(os);
517         } else
518                 LYXERR(Debug::INIT) << "LyX: Warning: unable to save Session: "
519                        << session_file << endl;
520 }
521
522 }