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"
19 #include <boost/filesystem/operations.hpp>
26 using lyx::support::absolutePath;
27 using lyx::support::addName;
28 using lyx::support::FileName;
29 using lyx::support::package;
31 namespace fs = boost::filesystem;
41 using std::istringstream;
44 using std::ostream_iterator;
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]";
60 LastFilesSection::LastFilesSection(unsigned int num) :
61 default_num_last_files(4),
62 absolute_max_last_files(100)
64 setNumberOfLastFiles(num);
68 void LastFilesSection::read(istream & is)
76 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !absolutePath(tmp))
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);
86 lyxerr[Debug::INIT] << "LyX: Warning: Ignore last file: " << tmp << endl;
91 void LastFilesSection::write(ostream & os) const
93 os << '\n' << sec_lastfiles << '\n';
94 copy(lastfiles.begin(), lastfiles.end(),
95 ostream_iterator<FileName>(os, "\n"));
99 void LastFilesSection::add(FileName const & file)
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())
105 lastfiles.push_front(file);
106 if (lastfiles.size() > num_lastfiles)
107 lastfiles.pop_back();
111 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
113 if (0 < no && no <= absolute_max_last_files)
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;
124 void LastOpenedSection::read(istream & is)
132 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !absolutePath(tmp))
135 FileName const file(tmp);
136 if (fs::exists(file.toFilesystemEncoding()) &&
137 !fs::is_directory(file.toFilesystemEncoding()))
138 lastopened.push_back(file);
140 lyxerr[Debug::INIT] << "LyX: Warning: Ignore last opened file: " << tmp << endl;
145 void LastOpenedSection::write(ostream & os) const
147 os << '\n' << sec_lastopened << '\n';
148 copy(lastopened.begin(), lastopened.end(),
149 ostream_iterator<FileName>(os, "\n"));
153 void LastOpenedSection::add(FileName const & file)
155 lastopened.push_back(file);
159 void LastOpenedSection::clear()
165 void LastFilePosSection::read(istream & is)
173 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
182 istringstream itmp(tmp);
184 itmp.ignore(2); // ignore ", "
186 itmp.ignore(2); // ignore ", "
188 if (!absolutePath(fname))
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);
196 lyxerr[Debug::INIT] << "LyX: Warning: Ignore pos of last file: " << fname << endl;
198 lyxerr[Debug::INIT] << "LyX: Warning: unknown pos of last file: " << tmp << endl;
204 void LastFilePosSection::write(ostream & os) const
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';
216 void LastFilePosSection::save(FileName const & fname, FilePos pos)
218 lastfilepos[fname] = pos;
222 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
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
234 void BookmarksSection::read(istream & is)
242 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
251 istringstream itmp(tmp);
253 itmp.ignore(2); // ignore ", "
255 itmp.ignore(2); // ignore ", "
257 if (!absolutePath(fname))
259 FileName const file(fname);
260 // only load valid bookmarks
261 if (fs::exists(file.toFilesystemEncoding()) &&
262 !fs::is_directory(file.toFilesystemEncoding()) &&
263 bookmarks.size() < max_bookmarks)
264 bookmarks.push_back(Bookmark(file, id, pos));
266 lyxerr[Debug::INIT] << "LyX: Warning: Ignore bookmark of file: " << fname << endl;
268 lyxerr[Debug::INIT] << "LyX: Warning: unknown Bookmark info: " << tmp << endl;
274 void BookmarksSection::write(ostream & os) const
276 os << '\n' << sec_bookmarks << '\n';
277 for (size_t i = 0; i < bookmarks.size(); ++i) {
278 os << bookmarks[i].par_id << ", "
279 << bookmarks[i].par_pos << ", "
280 << bookmarks[i].filename << '\n';
285 void BookmarksSection::save(FileName const & fname, int par_id, pos_type par_pos, bool persistent)
288 bookmarks.push_back(Bookmark(fname, par_id, par_pos));
289 if (bookmarks.size() > max_bookmarks)
290 bookmarks.pop_back();
293 temp_bookmark = Bookmark(fname, par_id, par_pos);
297 bool BookmarksSection::isValid(unsigned int i) const
300 return !temp_bookmark.filename.empty();
302 return i <= bookmarks.size() && !bookmarks[i-1].filename.empty();
306 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
309 return temp_bookmark;
311 return bookmarks[i-1];
315 void ToolbarSection::read(istream & is)
323 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
327 // Read session info, saved as key/value pairs
328 // would better yell if pos returns npos
329 string::size_type pos = tmp.find_first_of(" = ");
330 // silently ignore lines without " = "
331 if (pos != string::npos) {
332 string key = tmp.substr(0, pos);
335 istringstream value(tmp.substr(pos + 3));
337 value.ignore(1); // ignore " "
339 toolbars[key] = ToolbarInfo(state, location);
341 lyxerr[Debug::INIT] << "LyX: Warning: Ignore toolbar info: " << tmp << endl;
343 lyxerr[Debug::INIT] << "LyX: Warning: unknown Toolbar info: " << tmp << endl;
349 void ToolbarSection::write(ostream & os) const
351 os << '\n' << sec_toolbars << '\n';
352 for (ToolbarMap::const_iterator tb = toolbars.begin();
353 tb != toolbars.end(); ++tb) {
354 os << tb->first << " = "
355 << static_cast<int>(tb->second.state) << " "
356 << static_cast<int>(tb->second.location) << '\n';
361 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
363 return toolbars[name];
367 void SessionInfoSection::read(istream & is)
375 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
379 // Read session info, saved as key/value pairs
380 // would better yell if pos returns npos
381 string::size_type pos = tmp.find_first_of(" = ");
382 // silently ignore lines without " = "
383 if (pos != string::npos) {
384 string key = tmp.substr(0, pos);
385 string value = tmp.substr(pos + 3);
386 sessioninfo[key] = value;
388 lyxerr[Debug::INIT] << "LyX: Warning: Ignore session info: " << tmp << endl;
390 lyxerr[Debug::INIT] << "LyX: Warning: unknown Session info: " << tmp << endl;
396 void SessionInfoSection::write(ostream & os) const
398 os << '\n' << sec_session << '\n';
399 for (MiscInfo::const_iterator val = sessioninfo.begin();
400 val != sessioninfo.end(); ++val) {
401 os << val->first << " = " << val->second << '\n';
406 void SessionInfoSection::save(string const & key, string const & value)
408 sessioninfo[key] = value;
412 string const SessionInfoSection::load(string const & key, bool release)
414 MiscInfo::const_iterator pos = sessioninfo.find(key);
416 if (pos != sessioninfo.end())
419 sessioninfo.erase(key);
424 Session::Session(unsigned int num) :
427 // locate the session file
428 // note that the session file name 'session' is hard-coded
429 session_file = FileName(addName(package().user_support(), "session"));
435 void Session::readFile()
437 // we will not complain if we can't find session_file nor will
438 // we issue a warning. (Lgb)
439 ifstream is(session_file.toFilesystemEncoding().c_str());
442 while (getline(is, tmp)) {
443 // Ignore comments, empty line or line stats with ' '
444 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
447 // Determine section id
448 if (tmp == sec_lastfiles)
449 lastFiles().read(is);
450 else if (tmp == sec_lastopened)
451 lastOpened().read(is);
452 else if (tmp == sec_lastfilepos)
453 lastFilePos().read(is);
454 else if (tmp == sec_bookmarks)
455 bookmarks().read(is);
456 else if (tmp == sec_toolbars)
458 else if (tmp == sec_session)
459 sessionInfo().read(is);
461 lyxerr[Debug::INIT] << "LyX: Warning: unknown Session section: " << tmp << endl;
466 void Session::writeFile() const
468 ofstream os(session_file.toFilesystemEncoding().c_str());
470 os << "## Automatically generated lyx session file \n"
471 << "## Editing this file manually may cause lyx to crash.\n";
473 lastFiles().write(os);
474 lastOpened().write(os);
475 lastFilePos().write(os);
476 bookmarks().write(os);
477 toolbars().write(os);
478 sessionInfo().write(os);
480 lyxerr[Debug::INIT] << "LyX: Warning: unable to save Session: "
481 << session_file << endl;