3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
15 #include "support/debug.h"
16 #include "support/Package.h"
17 #include "support/filetools.h"
28 string const sec_lastfiles = "[recent files]";
29 string const sec_lastfilepos = "[cursor positions]";
30 string const sec_lastopened = "[last opened files]";
31 string const sec_bookmarks = "[bookmarks]";
32 string const sec_session = "[session info]";
33 string const sec_toolbars = "[toolbars]";
40 using support::absolutePath;
41 using support::addName;
42 using support::FileName;
43 using support::package;
45 LastFilesSection::LastFilesSection(unsigned int num) :
46 default_num_last_files(4),
47 absolute_max_last_files(100)
49 setNumberOfLastFiles(num);
53 void LastFilesSection::read(istream & is)
61 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !absolutePath(tmp))
65 FileName const file(tmp);
66 if (file.exists() && !file.isDirectory()
67 && lastfiles.size() < num_lastfiles)
68 lastfiles.push_back(file);
70 LYXERR(Debug::INIT, "LyX: Warning: Ignore last file: " << tmp);
75 void LastFilesSection::write(ostream & os) const
77 os << '\n' << sec_lastfiles << '\n';
78 copy(lastfiles.begin(), lastfiles.end(),
79 ostream_iterator<FileName>(os, "\n"));
83 void LastFilesSection::add(FileName const & file)
85 // If file already exist, delete it and reinsert at front.
86 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
87 if (it != lastfiles.end())
89 lastfiles.push_front(file);
90 if (lastfiles.size() > num_lastfiles)
95 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
97 if (0 < no && no <= absolute_max_last_files)
100 LYXERR(Debug::INIT, "LyX: session: too many last files\n"
101 << "\tdefault (=" << default_num_last_files << ") used.");
102 num_lastfiles = default_num_last_files;
107 void LastOpenedSection::read(istream & is)
115 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !absolutePath(tmp))
118 FileName const file(tmp);
119 if (file.exists() && !file.isDirectory())
120 lastopened.push_back(file);
122 LYXERR(Debug::INIT, "LyX: Warning: Ignore last opened file: " << tmp);
127 void LastOpenedSection::write(ostream & os) const
129 os << '\n' << sec_lastopened << '\n';
130 copy(lastopened.begin(), lastopened.end(),
131 ostream_iterator<FileName>(os, "\n"));
135 void LastOpenedSection::add(FileName const & file)
137 lastopened.push_back(file);
141 void LastOpenedSection::clear()
147 void LastFilePosSection::read(istream & is)
155 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
163 istringstream itmp(tmp);
165 itmp.ignore(2); // ignore ", "
167 itmp.ignore(2); // ignore ", "
168 getline(itmp, fname);
169 if (!absolutePath(fname))
171 FileName const file(fname);
172 if (file.exists() && !file.isDirectory()
173 && lastfilepos.size() < num_lastfilepos)
174 lastfilepos[file] = filepos;
176 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
178 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
184 void LastFilePosSection::write(ostream & os) const
186 os << '\n' << sec_lastfilepos << '\n';
187 for (FilePosMap::const_iterator file = lastfilepos.begin();
188 file != lastfilepos.end(); ++file) {
189 os << file->second.pit << ", " << file->second.pos << ", "
190 << file->first << '\n';
195 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
197 lastfilepos[fname] = pos;
201 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
203 FilePosMap::const_iterator entry = lastfilepos.find(fname);
204 // Has position information, return it.
205 if (entry != lastfilepos.end())
206 return entry->second;
207 // Not found, return the first paragraph
212 void BookmarksSection::clear()
214 // keep bookmark[0], the temporary one
216 bookmarks.resize(max_bookmarks + 1);
220 void BookmarksSection::read(istream & is)
228 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
233 // idx, pit, pos, file\n
238 istringstream itmp(tmp);
240 itmp.ignore(2); // ignore ", "
242 itmp.ignore(2); // ignore ", "
244 itmp.ignore(2); // ignore ", "
245 getline(itmp, fname);
246 if (!absolutePath(fname))
248 FileName const file(fname);
249 // only load valid bookmarks
250 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
251 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
253 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
255 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
261 void BookmarksSection::write(ostream & os) const
263 os << '\n' << sec_bookmarks << '\n';
264 for (size_t i = 1; i <= max_bookmarks; ++i) {
267 << bookmarks[i].bottom_pit << ", "
268 << bookmarks[i].bottom_pos << ", "
269 << bookmarks[i].filename << '\n';
274 void BookmarksSection::save(FileName const & fname,
275 pit_type bottom_pit, pos_type bottom_pos,
276 int top_id, pos_type top_pos, unsigned int idx)
278 // silently ignore bookmarks when idx is out of range
279 if (idx <= max_bookmarks)
280 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
284 bool BookmarksSection::isValid(unsigned int i) const
286 return i <= max_bookmarks && !bookmarks[i].filename.empty();
290 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
296 void ToolbarSection::read(istream & is)
304 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
308 // Read session info, saved as key/value pairs
309 // would better yell if pos returns npos
310 size_t pos = tmp.find_first_of(" = ");
311 // silently ignore lines without " = "
312 if (pos != string::npos) {
314 item.key = tmp.substr(0, pos);
317 istringstream value(tmp.substr(pos + 3));
320 value >> item.info.posx;
321 value >> item.info.posy;
322 item.info.state = ToolbarInfo::State(state);
323 item.info.location = ToolbarInfo::Location(location);
324 toolbars.push_back(item);
326 LYXERR(Debug::INIT, "LyX: Warning: Ignore toolbar info: " << tmp);
328 LYXERR(Debug::INIT, "LyX: Warning: unknown Toolbar info: " << tmp);
331 // sort the toolbars by location, line and position
332 std::sort(toolbars.begin(), toolbars.end());
336 void ToolbarSection::write(ostream & os) const
338 os << '\n' << sec_toolbars << '\n';
339 for (ToolbarList::const_iterator tb = toolbars.begin();
340 tb != toolbars.end(); ++tb) {
341 os << tb->key << " = "
342 << static_cast<int>(tb->info.state) << " "
343 << static_cast<int>(tb->info.location) << " "
344 << tb->info.posx << " "
345 << tb->info.posy << '\n';
350 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
352 for (ToolbarList::iterator tb = toolbars.begin();
353 tb != toolbars.end(); ++tb)
360 toolbars.push_back(item);
361 return toolbars.back().info;
365 bool operator<(ToolbarSection::ToolbarItem const & a, ToolbarSection::ToolbarItem const & b)
367 ToolbarSection::ToolbarInfo lhs = a.info;
368 ToolbarSection::ToolbarInfo rhs = b.info;
370 if (lhs.state != rhs.state)
371 return static_cast<int>(lhs.state) < static_cast<int>(rhs.state);
372 // order of dock does not really matter
373 if (lhs.location != rhs.location)
374 return static_cast<int>(lhs.location) < static_cast<int>(rhs.location);
375 // if the same dock, the order depends on position
376 if (lhs.location == ToolbarSection::ToolbarInfo::TOP ||
377 lhs.location == ToolbarSection::ToolbarInfo::BOTTOM)
378 return lhs.posy < rhs.posy || (lhs.posy == rhs.posy && lhs.posx < rhs.posx);
379 else if (lhs.location == ToolbarSection::ToolbarInfo::LEFT ||
380 lhs.location == ToolbarSection::ToolbarInfo::RIGHT)
381 return lhs.posx < rhs.posx || (lhs.posx == rhs.posx && lhs.posy < rhs.posy);
386 void SessionInfoSection::read(istream & is)
394 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
398 // Read session info, saved as key/value pairs
399 // would better yell if pos returns npos
400 string::size_type pos = tmp.find_first_of(" = ");
401 // silently ignore lines without " = "
402 if (pos != string::npos) {
403 string key = tmp.substr(0, pos);
404 string value = tmp.substr(pos + 3);
405 sessioninfo[key] = value;
407 LYXERR(Debug::INIT, "LyX: Warning: Ignore session info: " << tmp);
409 LYXERR(Debug::INIT, "LyX: Warning: unknown Session info: " << tmp);
415 void SessionInfoSection::write(ostream & os) const
417 os << '\n' << sec_session << '\n';
418 for (MiscInfo::const_iterator val = sessioninfo.begin();
419 val != sessioninfo.end(); ++val) {
420 os << val->first << " = " << val->second << '\n';
425 void SessionInfoSection::save(string const & key, string const & value)
427 sessioninfo[key] = value;
431 string const SessionInfoSection::load(string const & key, bool release)
433 MiscInfo::const_iterator pos = sessioninfo.find(key);
435 if (pos != sessioninfo.end())
438 sessioninfo.erase(key);
443 Session::Session(unsigned int num) :
446 // locate the session file
447 // note that the session file name 'session' is hard-coded
448 session_file = FileName(addName(package().user_support().absFilename(), "session"));
454 void Session::readFile()
456 // we will not complain if we can't find session_file nor will
457 // we issue a warning. (Lgb)
458 ifstream is(session_file.toFilesystemEncoding().c_str());
461 while (getline(is, tmp)) {
462 // Ignore comments, empty line or line stats with ' '
463 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
466 // Determine section id
467 if (tmp == sec_lastfiles)
468 lastFiles().read(is);
469 else if (tmp == sec_lastopened)
470 lastOpened().read(is);
471 else if (tmp == sec_lastfilepos)
472 lastFilePos().read(is);
473 else if (tmp == sec_bookmarks)
474 bookmarks().read(is);
475 else if (tmp == sec_toolbars)
477 else if (tmp == sec_session)
478 sessionInfo().read(is);
480 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
485 void Session::writeFile() const
487 ofstream os(session_file.toFilesystemEncoding().c_str());
489 os << "## Automatically generated lyx session file \n"
490 << "## Editing this file manually may cause lyx to crash.\n";
492 lastFiles().write(os);
493 lastOpened().write(os);
494 lastFilePos().write(os);
495 bookmarks().write(os);
496 toolbars().write(os);
497 sessionInfo().write(os);
499 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "