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)
218 istringstream itmp(tmp);
220 itmp.ignore(2); // ignore ", "
222 itmp.ignore(2); // ignore ", "
224 // only load valid bookmarks
225 if (bookmarks.size() < max_bookmarks && fs::exists(fname))
226 bookmarks.push_back(Bookmark(fname, id, pos));
231 void BookmarksSection::write(ostream & os) const
233 os << '\n' << sec_bookmarks << '\n';
234 for (size_t i = 0; i < bookmarks.size(); ++i) {
235 os << bookmarks[i].par_id << ", "
236 << bookmarks[i].par_pos << ", "
237 << bookmarks[i].filename << '\n';
242 void BookmarksSection::save(std::string const & fname, int par_id, pos_type par_pos, bool persistent)
245 bookmarks.push_front(Bookmark(fname, par_id, par_pos));
246 if (bookmarks.size() > max_bookmarks)
247 bookmarks.pop_back();
250 temp_bookmark = Bookmark(fname, par_id, par_pos);
254 bool BookmarksSection::isValid(unsigned int i) const
256 // i == 0, or in the queue
257 return i <= bookmarks.size();
261 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
264 return temp_bookmark;
266 return bookmarks[i-1];
270 void SessionInfoSection::read(istream & is)
279 // Read session info, saved as key/value pairs
280 // would better yell if pos returns npos
281 string::size_type pos = tmp.find_first_of(" = ");
282 // silently ignore lines without " = "
283 if (pos != string::npos) {
284 string key = tmp.substr(0, pos);
285 string value = tmp.substr(pos + 3);
286 sessioninfo[key] = value;
292 void SessionInfoSection::write(ostream & os) const
294 os << '\n' << sec_session << '\n';
295 for (MiscInfo::const_iterator val = sessioninfo.begin();
296 val != sessioninfo.end(); ++val) {
297 os << val->first << " = " << val->second << '\n';
302 void SessionInfoSection::save(string const & key, string const & value)
304 sessioninfo[key] = value;
308 string const SessionInfoSection::load(string const & key, bool release)
310 MiscInfo::const_iterator pos = sessioninfo.find(key);
312 if (pos != sessioninfo.end())
315 sessioninfo.erase(key);
320 Session::Session(unsigned int num) :
323 // locate the session file
324 // note that the session file name 'session' is hard-coded
325 session_file = addName(package().user_support(), "session");
331 void Session::readFile()
333 // we will not complain if we can't find session_file nor will
334 // we issue a warning. (Lgb)
335 ifstream is(session_file.c_str());
338 while (getline(is, tmp)) {
339 // Ignore comments, empty line or line stats with ' '
340 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
343 // Determine section id
344 if (tmp == sec_lastfiles)
345 lastFiles().read(is);
346 else if (tmp == sec_lastopened)
347 lastOpened().read(is);
348 else if (tmp == sec_lastfilepos)
349 lastFilePos().read(is);
350 else if (tmp == sec_bookmarks)
351 bookmarks().read(is);
352 else if (tmp == sec_session)
353 sessionInfo().read(is);
355 lyxerr << "LyX: Warning: unknown Session section: " << tmp << endl;
360 void Session::writeFile() const
362 ofstream os(session_file.c_str());
364 os << "## Automatically generated lyx session file \n"
365 << "## Editing this file manually may cause lyx to crash.\n";
367 lastFiles().write(os);
368 lastOpened().write(os);
369 lastFilePos().write(os);
370 bookmarks().write(os);
371 sessionInfo().write(os);
373 lyxerr << "LyX: Warning: unable to save Session: "
374 << session_file << endl;