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.
16 #include "support/Package.h"
17 #include "support/filetools.h"
24 using lyx::support::absolutePath;
25 using lyx::support::addName;
26 using lyx::support::FileName;
27 using lyx::support::package;
37 using std::istringstream;
40 using std::ostream_iterator;
44 string const sec_lastfiles = "[recent files]";
45 string const sec_lastfilepos = "[cursor positions]";
46 string const sec_lastopened = "[last opened files]";
47 string const sec_bookmarks = "[bookmarks]";
48 string const sec_session = "[session info]";
49 string const sec_toolbars = "[toolbars]";
56 LastFilesSection::LastFilesSection(unsigned int num) :
57 default_num_last_files(4),
58 absolute_max_last_files(100)
60 setNumberOfLastFiles(num);
64 void LastFilesSection::read(istream & is)
72 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !absolutePath(tmp))
76 FileName const file(tmp);
77 if (file.exists() && !file.isDirectory()
78 && lastfiles.size() < num_lastfiles)
79 lastfiles.push_back(file);
81 LYXERR(Debug::INIT) << "LyX: Warning: Ignore last file: " << tmp << endl;
86 void LastFilesSection::write(ostream & os) const
88 os << '\n' << sec_lastfiles << '\n';
89 copy(lastfiles.begin(), lastfiles.end(),
90 ostream_iterator<FileName>(os, "\n"));
94 void LastFilesSection::add(FileName const & file)
96 // If file already exist, delete it and reinsert at front.
97 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
98 if (it != lastfiles.end())
100 lastfiles.push_front(file);
101 if (lastfiles.size() > num_lastfiles)
102 lastfiles.pop_back();
106 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
108 if (0 < no && no <= absolute_max_last_files)
111 LYXERR(Debug::INIT) << "LyX: session: too many last files\n"
112 << "\tdefault (=" << default_num_last_files
113 << ") used." << endl;
114 num_lastfiles = default_num_last_files;
119 void LastOpenedSection::read(istream & is)
127 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !absolutePath(tmp))
130 FileName const file(tmp);
131 if (file.exists() && !file.isDirectory())
132 lastopened.push_back(file);
134 LYXERR(Debug::INIT) << "LyX: Warning: Ignore last opened file: " << tmp << endl;
139 void LastOpenedSection::write(ostream & os) const
141 os << '\n' << sec_lastopened << '\n';
142 copy(lastopened.begin(), lastopened.end(),
143 ostream_iterator<FileName>(os, "\n"));
147 void LastOpenedSection::add(FileName const & file)
149 lastopened.push_back(file);
153 void LastOpenedSection::clear()
159 void LastFilePosSection::read(istream & is)
167 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
176 istringstream itmp(tmp);
178 itmp.ignore(2); // ignore ", "
180 itmp.ignore(2); // ignore ", "
181 getline(itmp, fname);
182 if (!absolutePath(fname))
184 FileName const file(fname);
185 if (file.exists() && !file.isDirectory()
186 && lastfilepos.size() < num_lastfilepos)
187 lastfilepos[file] = boost::tie(pit, pos);
189 LYXERR(Debug::INIT) << "LyX: Warning: Ignore pos of last file: " << fname << endl;
191 LYXERR(Debug::INIT) << "LyX: Warning: unknown pos of last file: " << tmp << endl;
197 void LastFilePosSection::write(ostream & os) const
199 os << '\n' << sec_lastfilepos << '\n';
200 for (FilePosMap::const_iterator file = lastfilepos.begin();
201 file != lastfilepos.end(); ++file) {
202 os << file->second.get<0>() << ", "
203 << file->second.get<1>() << ", "
204 << file->first << '\n';
209 void LastFilePosSection::save(FileName const & fname, FilePos pos)
211 lastfilepos[fname] = pos;
215 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
217 FilePosMap::const_iterator entry = lastfilepos.find(fname);
218 // Has position information, return it.
219 if (entry != lastfilepos.end())
220 return entry->second;
221 // Not found, return the first paragraph
227 void BookmarksSection::clear()
229 // keep bookmark[0], the temporary one
231 bookmarks.resize(max_bookmarks + 1);
235 void BookmarksSection::read(istream & is)
243 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
248 // idx, pit, pos, file\n
253 istringstream itmp(tmp);
255 itmp.ignore(2); // ignore ", "
257 itmp.ignore(2); // ignore ", "
259 itmp.ignore(2); // ignore ", "
260 getline(itmp, fname);
261 if (!absolutePath(fname))
263 FileName const file(fname);
264 // only load valid bookmarks
265 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
266 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
268 LYXERR(Debug::INIT) << "LyX: Warning: Ignore bookmark of file: " << fname << endl;
270 LYXERR(Debug::INIT) << "LyX: Warning: unknown Bookmark info: " << tmp << endl;
276 void BookmarksSection::write(ostream & os) const
278 os << '\n' << sec_bookmarks << '\n';
279 for (size_t i = 1; i <= max_bookmarks; ++i) {
282 << bookmarks[i].bottom_pit << ", "
283 << bookmarks[i].bottom_pos << ", "
284 << bookmarks[i].filename << '\n';
289 void BookmarksSection::save(FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
290 int top_id, pos_type top_pos, unsigned int idx)
292 // silently ignore bookmarks when idx is out of range
293 if (idx <= max_bookmarks)
294 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
298 bool BookmarksSection::isValid(unsigned int i) const
300 return i <= max_bookmarks && !bookmarks[i].filename.empty();
304 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
310 void ToolbarSection::read(istream & is)
318 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
322 // Read session info, saved as key/value pairs
323 // would better yell if pos returns npos
324 string::size_type pos = tmp.find_first_of(" = ");
325 // silently ignore lines without " = "
326 if (pos != string::npos) {
327 string key = tmp.substr(0, pos);
332 istringstream value(tmp.substr(pos + 3));
337 toolbars.push_back(boost::make_tuple(key, ToolbarInfo(state, location, posx, posy)));
339 LYXERR(Debug::INIT) << "LyX: Warning: Ignore toolbar info: " << tmp << endl;
341 LYXERR(Debug::INIT) << "LyX: Warning: unknown Toolbar info: " << tmp << endl;
344 // sort the toolbars by location, line and position
345 std::sort(toolbars.begin(), toolbars.end());
349 void ToolbarSection::write(ostream & os) const
351 os << '\n' << sec_toolbars << '\n';
352 for (ToolbarList::const_iterator tb = toolbars.begin();
353 tb != toolbars.end(); ++tb) {
354 os << tb->get<0>() << " = "
355 << static_cast<int>(tb->get<1>().state) << " "
356 << static_cast<int>(tb->get<1>().location) << " "
357 << tb->get<1>().posx << " "
358 << tb->get<1>().posy << '\n';
363 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
365 for (ToolbarList::iterator tb = toolbars.begin();
366 tb != toolbars.end(); ++tb)
367 if (tb->get<0>() == name)
370 toolbars.push_back(boost::make_tuple(name, ToolbarSection::ToolbarInfo()));
371 return toolbars.back().get<1>();
375 bool operator<(ToolbarSection::ToolbarItem const & a, ToolbarSection::ToolbarItem const & b)
377 ToolbarSection::ToolbarInfo lhs = a.get<1>();
378 ToolbarSection::ToolbarInfo rhs = b.get<1>();
380 if (lhs.state != rhs.state)
381 return static_cast<int>(lhs.state) < static_cast<int>(rhs.state);
382 // order of dock does not really matter
383 if (lhs.location != rhs.location)
384 return static_cast<int>(lhs.location) < static_cast<int>(rhs.location);
385 // if the same dock, the order depends on position
386 if (lhs.location == ToolbarSection::ToolbarInfo::TOP ||
387 lhs.location == ToolbarSection::ToolbarInfo::BOTTOM)
388 return lhs.posy < rhs.posy || (lhs.posy == rhs.posy && lhs.posx < rhs.posx);
389 else if (lhs.location == ToolbarSection::ToolbarInfo::LEFT ||
390 lhs.location == ToolbarSection::ToolbarInfo::RIGHT)
391 return lhs.posx < rhs.posx || (lhs.posx == rhs.posx && lhs.posy < rhs.posy);
397 void SessionInfoSection::read(istream & is)
405 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
409 // Read session info, saved as key/value pairs
410 // would better yell if pos returns npos
411 string::size_type pos = tmp.find_first_of(" = ");
412 // silently ignore lines without " = "
413 if (pos != string::npos) {
414 string key = tmp.substr(0, pos);
415 string value = tmp.substr(pos + 3);
416 sessioninfo[key] = value;
418 LYXERR(Debug::INIT) << "LyX: Warning: Ignore session info: " << tmp << endl;
420 LYXERR(Debug::INIT) << "LyX: Warning: unknown Session info: " << tmp << endl;
426 void SessionInfoSection::write(ostream & os) const
428 os << '\n' << sec_session << '\n';
429 for (MiscInfo::const_iterator val = sessioninfo.begin();
430 val != sessioninfo.end(); ++val) {
431 os << val->first << " = " << val->second << '\n';
436 void SessionInfoSection::save(string const & key, string const & value)
438 sessioninfo[key] = value;
442 string const SessionInfoSection::load(string const & key, bool release)
444 MiscInfo::const_iterator pos = sessioninfo.find(key);
446 if (pos != sessioninfo.end())
449 sessioninfo.erase(key);
454 Session::Session(unsigned int num) :
457 // locate the session file
458 // note that the session file name 'session' is hard-coded
459 session_file = FileName(addName(package().user_support().absFilename(), "session"));
465 void Session::readFile()
467 // we will not complain if we can't find session_file nor will
468 // we issue a warning. (Lgb)
469 ifstream is(session_file.toFilesystemEncoding().c_str());
472 while (getline(is, tmp)) {
473 // Ignore comments, empty line or line stats with ' '
474 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
477 // Determine section id
478 if (tmp == sec_lastfiles)
479 lastFiles().read(is);
480 else if (tmp == sec_lastopened)
481 lastOpened().read(is);
482 else if (tmp == sec_lastfilepos)
483 lastFilePos().read(is);
484 else if (tmp == sec_bookmarks)
485 bookmarks().read(is);
486 else if (tmp == sec_toolbars)
488 else if (tmp == sec_session)
489 sessionInfo().read(is);
491 LYXERR(Debug::INIT) << "LyX: Warning: unknown Session section: " << tmp << endl;
496 void Session::writeFile() const
498 ofstream os(session_file.toFilesystemEncoding().c_str());
500 os << "## Automatically generated lyx session file \n"
501 << "## Editing this file manually may cause lyx to crash.\n";
503 lastFiles().write(os);
504 lastOpened().write(os);
505 lastFilePos().write(os);
506 bookmarks().write(os);
507 toolbars().write(os);
508 sessionInfo().write(os);
510 LYXERR(Debug::INIT) << "LyX: Warning: unable to save Session: "
511 << session_file << endl;