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::addName;
27 using lyx::support::package;
29 namespace fs = boost::filesystem;
39 using std::istringstream;
42 using std::ostream_iterator;
46 string const sec_lastfiles = "[recent files]";
47 string const sec_lastfilepos = "[cursor positions]";
48 string const sec_lastopened = "[last opened files]";
49 string const sec_bookmarks = "[bookmarks]";
50 string const sec_session = "[session info]";
57 LastFilesSection::LastFilesSection(unsigned int num) :
58 default_num_last_files(4),
59 absolute_max_last_files(100)
61 setNumberOfLastFiles(num);
65 void LastFilesSection::read(istream & is)
74 if (!fs::exists(tmp) || lastfiles.size() >= num_lastfiles)
76 lastfiles.push_back(tmp);
81 void LastFilesSection::write(ostream & os) const
83 os << '\n' << sec_lastfiles << '\n';
84 copy(lastfiles.begin(), lastfiles.end(),
85 ostream_iterator<string>(os, "\n"));
89 void LastFilesSection::add(string const & file)
91 // If file already exist, delete it and reinsert at front.
92 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
93 if (it != lastfiles.end())
95 lastfiles.push_front(file);
96 if (lastfiles.size() > num_lastfiles)
101 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
103 if (0 < no && no <= absolute_max_last_files)
106 lyxerr << "LyX: session: too many last files\n"
107 << "\tdefault (=" << default_num_last_files
108 << ") used." << endl;
109 num_lastfiles = default_num_last_files;
114 void LastOpenedSection::read(istream & is)
122 if (!fs::exists(tmp))
124 lastopened.push_back(tmp);
129 void LastOpenedSection::write(ostream & os) const
131 os << '\n' << sec_lastopened << '\n';
132 copy(lastopened.begin(), lastopened.end(),
133 ostream_iterator<string>(os, "\n"));
137 void LastOpenedSection::add(string const & file)
139 lastopened.push_back(file);
143 void LastOpenedSection::clear()
149 void LastFilePosSection::read(istream & is)
162 istringstream itmp(tmp);
164 itmp.ignore(2); // ignore ", "
166 itmp.ignore(2); // ignore ", "
168 if (!fs::exists(fname) || lastfilepos.size() >= num_lastfilepos)
170 lastfilepos[fname] = boost::tie(pit, pos);
175 void LastFilePosSection::write(ostream & os) const
177 os << '\n' << sec_lastfilepos << '\n';
178 for (FilePosMap::const_iterator file = lastfilepos.begin();
179 file != lastfilepos.end(); ++file) {
180 os << file->second.get<0>() << ", "
181 << file->second.get<1>() << ", "
182 << file->first << '\n';
187 void LastFilePosSection::save(string const & fname, FilePos pos)
189 lastfilepos[fname] = pos;
193 LastFilePosSection::FilePos LastFilePosSection::load(string const & fname) const
195 FilePosMap::const_iterator entry = lastfilepos.find(fname);
196 // Has position information, return it.
197 if (entry != lastfilepos.end())
198 return entry->second;
199 // Not found, return the first paragraph
205 void BookmarksSection::read(istream & is)
214 // bookmarkid, id, pos, file\n
219 istringstream itmp(tmp);
221 itmp.ignore(2); // ignore ", "
223 itmp.ignore(2); // ignore ", "
225 itmp.ignore(2); // ignore ", "
227 // only load valid bookmarks
228 if (fs::exists(fname))
229 bookmarks.push_back(boost::tie(num, fname, id, pos));
234 void BookmarksSection::write(ostream & os) const
236 os << '\n' << sec_bookmarks << '\n';
237 for (BookmarkList::const_iterator bm = bookmarks.begin();
238 bm != bookmarks.end(); ++bm) {
239 // save bookmark number, id, pos, fname
240 os << bm->get<0>() << ", "
241 << bm->get<2>() << ", "
242 << bm->get<3>() << ", "
243 << bm->get<1>() << '\n';
248 void BookmarksSection::save(Bookmark const & bookmark)
250 bookmarks.push_back(bookmark);
254 void SessionInfoSection::read(istream & is)
263 // Read session info, saved as key/value pairs
264 // would better yell if pos returns npos
265 string::size_type pos = tmp.find_first_of(" = ");
266 // silently ignore lines without " = "
267 if (pos != string::npos) {
268 string key = tmp.substr(0, pos);
269 string value = tmp.substr(pos + 3);
270 sessioninfo[key] = value;
276 void SessionInfoSection::write(ostream & os) const
278 os << '\n' << sec_session << '\n';
279 for (MiscInfo::const_iterator val = sessioninfo.begin();
280 val != sessioninfo.end(); ++val) {
281 os << val->first << " = " << val->second << '\n';
286 void SessionInfoSection::save(string const & key, string const & value)
288 sessioninfo[key] = value;
292 string const SessionInfoSection::load(string const & key, bool release)
294 MiscInfo::const_iterator pos = sessioninfo.find(key);
296 if (pos != sessioninfo.end())
299 sessioninfo.erase(key);
305 Session::Session(unsigned int num) :
308 // locate the session file
309 // note that the session file name 'session' is hard-coded
310 session_file = addName(package().user_support(), "session");
316 void Session::readFile()
318 // we will not complain if we can't find session_file nor will
319 // we issue a warning. (Lgb)
320 ifstream is(session_file.c_str());
323 while (getline(is, tmp)) {
324 // Ignore comments, empty line or line stats with ' '
325 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
328 // Determine section id
329 if (tmp == sec_lastfiles)
330 lastFiles().read(is);
331 else if (tmp == sec_lastopened)
332 lastOpened().read(is);
333 else if (tmp == sec_lastfilepos)
334 lastFilePos().read(is);
335 else if (tmp == sec_bookmarks)
336 bookmarks().read(is);
337 else if (tmp == sec_session)
338 sessionInfo().read(is);
340 lyxerr << "LyX: Warning: unknown Session section: " << tmp << endl;
345 void Session::writeFile() const
347 ofstream os(session_file.c_str());
349 os << "## Automatically generated lyx session file \n"
350 << "## Editing this file manually may cause lyx to crash.\n";
352 lastFiles().write(os);
353 lastOpened().write(os);
354 lastFilePos().write(os);
355 bookmarks().write(os);
356 sessionInfo().write(os);
358 lyxerr << "LyX: Warning: unable to save Session: "
359 << session_file << endl;