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"
25 using namespace lyx::support;
29 string const sec_lastfiles = "[recent files]";
30 string const sec_lastfilepos = "[cursor positions]";
31 string const sec_lastopened = "[last opened files]";
32 string const sec_bookmarks = "[bookmarks]";
33 string const sec_session = "[session info]";
34 string const sec_toolbars = "[toolbars]";
42 LastFilesSection::LastFilesSection(unsigned int num) :
43 default_num_last_files(4),
44 absolute_max_last_files(100)
46 setNumberOfLastFiles(num);
50 void LastFilesSection::read(istream & is)
58 FileName const file(tmp);
59 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !file.isAbsolute())
63 if (file.exists() && !file.isDirectory()
64 && lastfiles.size() < num_lastfiles)
65 lastfiles.push_back(file);
67 LYXERR(Debug::INIT, "LyX: Warning: Ignore last file: " << tmp);
72 void LastFilesSection::write(ostream & os) const
74 os << '\n' << sec_lastfiles << '\n';
75 copy(lastfiles.begin(), lastfiles.end(),
76 ostream_iterator<FileName>(os, "\n"));
80 void LastFilesSection::add(FileName const & file)
82 // If file already exist, delete it and reinsert at front.
83 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
84 if (it != lastfiles.end())
86 lastfiles.push_front(file);
87 if (lastfiles.size() > num_lastfiles)
92 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
94 if (0 < no && no <= absolute_max_last_files)
97 LYXERR(Debug::INIT, "LyX: session: too many last files\n"
98 << "\tdefault (=" << default_num_last_files << ") used.");
99 num_lastfiles = default_num_last_files;
104 void LastOpenedSection::read(istream & is)
112 FileName const file(tmp);
113 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !file.isAbsolute())
116 if (file.exists() && !file.isDirectory())
117 lastopened.push_back(file);
119 LYXERR(Debug::INIT, "LyX: Warning: Ignore last opened file: " << tmp);
124 void LastOpenedSection::write(ostream & os) const
126 os << '\n' << sec_lastopened << '\n';
127 copy(lastopened.begin(), lastopened.end(),
128 ostream_iterator<FileName>(os, "\n"));
132 void LastOpenedSection::add(FileName const & file)
134 lastopened.push_back(file);
138 void LastOpenedSection::clear()
144 void LastFilePosSection::read(istream & is)
152 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
160 istringstream itmp(tmp);
162 itmp.ignore(2); // ignore ", "
164 itmp.ignore(2); // ignore ", "
165 getline(itmp, fname);
166 FileName const file(fname);
167 if (!file.isAbsolute())
169 if (file.exists() && !file.isDirectory()
170 && lastfilepos.size() < num_lastfilepos)
171 lastfilepos[file] = filepos;
173 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
175 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
181 void LastFilePosSection::write(ostream & os) const
183 os << '\n' << sec_lastfilepos << '\n';
184 for (FilePosMap::const_iterator file = lastfilepos.begin();
185 file != lastfilepos.end(); ++file) {
186 os << file->second.pit << ", " << file->second.pos << ", "
187 << file->first << '\n';
192 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
194 lastfilepos[fname] = pos;
198 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
200 FilePosMap::const_iterator entry = lastfilepos.find(fname);
201 // Has position information, return it.
202 if (entry != lastfilepos.end())
203 return entry->second;
204 // Not found, return the first paragraph
209 void BookmarksSection::clear()
211 // keep bookmark[0], the temporary one
213 bookmarks.resize(max_bookmarks + 1);
217 void BookmarksSection::read(istream & is)
225 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
230 // idx, pit, pos, file\n
235 istringstream itmp(tmp);
237 itmp.ignore(2); // ignore ", "
239 itmp.ignore(2); // ignore ", "
241 itmp.ignore(2); // ignore ", "
242 getline(itmp, fname);
243 FileName const file(fname);
244 if (!file.isAbsolute())
246 // only load valid bookmarks
247 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
248 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
250 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
252 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
258 void BookmarksSection::write(ostream & os) const
260 os << '\n' << sec_bookmarks << '\n';
261 for (size_t i = 1; i <= max_bookmarks; ++i) {
264 << bookmarks[i].bottom_pit << ", "
265 << bookmarks[i].bottom_pos << ", "
266 << bookmarks[i].filename << '\n';
271 void BookmarksSection::save(FileName const & fname,
272 pit_type bottom_pit, pos_type bottom_pos,
273 int top_id, pos_type top_pos, unsigned int idx)
275 // silently ignore bookmarks when idx is out of range
276 if (idx <= max_bookmarks)
277 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
281 bool BookmarksSection::isValid(unsigned int i) const
283 return i <= max_bookmarks && !bookmarks[i].filename.empty();
287 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
293 void ToolbarSection::read(istream & is)
301 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
305 // Read session info, saved as key/value pairs
306 // would better yell if pos returns npos
307 size_t pos = tmp.find_first_of(" = ");
308 // silently ignore lines without " = "
309 if (pos != string::npos) {
311 item.key = tmp.substr(0, pos);
314 istringstream value(tmp.substr(pos + 3));
317 value >> item.info.posx;
318 value >> item.info.posy;
319 item.info.state = ToolbarInfo::State(state);
320 item.info.location = ToolbarInfo::Location(location);
321 toolbars.push_back(item);
323 LYXERR(Debug::INIT, "LyX: Warning: Ignore toolbar info: " << tmp);
325 LYXERR(Debug::INIT, "LyX: Warning: unknown Toolbar info: " << tmp);
328 // sort the toolbars by location, line and position
329 sort(toolbars.begin(), toolbars.end());
333 void ToolbarSection::write(ostream & os) const
335 os << '\n' << sec_toolbars << '\n';
336 for (ToolbarList::const_iterator tb = toolbars.begin();
337 tb != toolbars.end(); ++tb) {
338 os << tb->key << " = "
339 << static_cast<int>(tb->info.state) << " "
340 << static_cast<int>(tb->info.location) << " "
341 << tb->info.posx << " "
342 << tb->info.posy << '\n';
347 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
349 for (ToolbarList::iterator tb = toolbars.begin();
350 tb != toolbars.end(); ++tb)
357 toolbars.push_back(item);
358 return toolbars.back().info;
362 bool operator<(ToolbarSection::ToolbarItem const & a, ToolbarSection::ToolbarItem const & b)
364 ToolbarSection::ToolbarInfo lhs = a.info;
365 ToolbarSection::ToolbarInfo rhs = b.info;
367 if (lhs.state != rhs.state)
368 return static_cast<int>(lhs.state) < static_cast<int>(rhs.state);
369 // order of dock does not really matter
370 if (lhs.location != rhs.location)
371 return static_cast<int>(lhs.location) < static_cast<int>(rhs.location);
372 // if the same dock, the order depends on position
373 if (lhs.location == ToolbarSection::ToolbarInfo::TOP ||
374 lhs.location == ToolbarSection::ToolbarInfo::BOTTOM)
375 return lhs.posy < rhs.posy || (lhs.posy == rhs.posy && lhs.posx < rhs.posx);
376 else if (lhs.location == ToolbarSection::ToolbarInfo::LEFT ||
377 lhs.location == ToolbarSection::ToolbarInfo::RIGHT)
378 return lhs.posx < rhs.posx || (lhs.posx == rhs.posx && lhs.posy < rhs.posy);
383 void SessionInfoSection::read(istream & is)
391 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
395 // Read session info, saved as key/value pairs
396 // would better yell if pos returns npos
397 string::size_type pos = tmp.find_first_of(" = ");
398 // silently ignore lines without " = "
399 if (pos != string::npos) {
400 string key = tmp.substr(0, pos);
401 string value = tmp.substr(pos + 3);
402 sessioninfo[key] = value;
404 LYXERR(Debug::INIT, "LyX: Warning: Ignore session info: " << tmp);
406 LYXERR(Debug::INIT, "LyX: Warning: unknown Session info: " << tmp);
412 void SessionInfoSection::write(ostream & os) const
414 os << '\n' << sec_session << '\n';
415 for (MiscInfo::const_iterator val = sessioninfo.begin();
416 val != sessioninfo.end(); ++val) {
417 os << val->first << " = " << val->second << '\n';
422 void SessionInfoSection::save(string const & key, string const & value)
424 sessioninfo[key] = value;
428 string const SessionInfoSection::load(string const & key, bool release)
430 MiscInfo::const_iterator pos = sessioninfo.find(key);
432 if (pos != sessioninfo.end())
435 sessioninfo.erase(key);
440 Session::Session(unsigned int num) :
443 // locate the session file
444 // note that the session file name 'session' is hard-coded
445 session_file = FileName(addName(package().user_support().absFilename(), "session"));
451 void Session::readFile()
453 // we will not complain if we can't find session_file nor will
454 // we issue a warning. (Lgb)
455 ifstream is(session_file.toFilesystemEncoding().c_str());
458 while (getline(is, tmp)) {
459 // Ignore comments, empty line or line stats with ' '
460 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
463 // Determine section id
464 if (tmp == sec_lastfiles)
465 lastFiles().read(is);
466 else if (tmp == sec_lastopened)
467 lastOpened().read(is);
468 else if (tmp == sec_lastfilepos)
469 lastFilePos().read(is);
470 else if (tmp == sec_bookmarks)
471 bookmarks().read(is);
472 else if (tmp == sec_toolbars)
474 else if (tmp == sec_session)
475 sessionInfo().read(is);
477 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
482 void Session::writeFile() const
484 ofstream os(session_file.toFilesystemEncoding().c_str());
486 os << "## Automatically generated lyx session file \n"
487 << "## Editing this file manually may cause lyx to crash.\n";
489 lastFiles().write(os);
490 lastOpened().write(os);
491 lastFilePos().write(os);
492 bookmarks().write(os);
493 toolbars().write(os);
494 sessionInfo().write(os);
496 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "