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]";
51 string const sec_toolbars = "[toolbars]";
58 LastFilesSection::LastFilesSection(unsigned int num) :
59 default_num_last_files(4),
60 absolute_max_last_files(100)
62 setNumberOfLastFiles(num);
66 void LastFilesSection::read(istream & is)
75 if (!fs::exists(tmp) || lastfiles.size() >= num_lastfiles)
77 lastfiles.push_back(tmp);
82 void LastFilesSection::write(ostream & os) const
84 os << '\n' << sec_lastfiles << '\n';
85 copy(lastfiles.begin(), lastfiles.end(),
86 ostream_iterator<string>(os, "\n"));
90 void LastFilesSection::add(string const & file)
92 // If file already exist, delete it and reinsert at front.
93 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
94 if (it != lastfiles.end())
96 lastfiles.push_front(file);
97 if (lastfiles.size() > num_lastfiles)
102 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
104 if (0 < no && no <= absolute_max_last_files)
107 lyxerr << "LyX: session: too many last files\n"
108 << "\tdefault (=" << default_num_last_files
109 << ") used." << endl;
110 num_lastfiles = default_num_last_files;
115 void LastOpenedSection::read(istream & is)
123 if (!fs::exists(tmp))
125 lastopened.push_back(tmp);
130 void LastOpenedSection::write(ostream & os) const
132 os << '\n' << sec_lastopened << '\n';
133 copy(lastopened.begin(), lastopened.end(),
134 ostream_iterator<string>(os, "\n"));
138 void LastOpenedSection::add(string const & file)
140 lastopened.push_back(file);
144 void LastOpenedSection::clear()
150 void LastFilePosSection::read(istream & is)
163 istringstream itmp(tmp);
165 itmp.ignore(2); // ignore ", "
167 itmp.ignore(2); // ignore ", "
169 if (!fs::exists(fname) || lastfilepos.size() >= num_lastfilepos)
171 lastfilepos[fname] = boost::tie(pit, pos);
176 void LastFilePosSection::write(ostream & os) const
178 os << '\n' << sec_lastfilepos << '\n';
179 for (FilePosMap::const_iterator file = lastfilepos.begin();
180 file != lastfilepos.end(); ++file) {
181 os << file->second.get<0>() << ", "
182 << file->second.get<1>() << ", "
183 << file->first << '\n';
188 void LastFilePosSection::save(string const & fname, FilePos pos)
190 lastfilepos[fname] = pos;
194 LastFilePosSection::FilePos LastFilePosSection::load(string const & fname) const
196 FilePosMap::const_iterator entry = lastfilepos.find(fname);
197 // Has position information, return it.
198 if (entry != lastfilepos.end())
199 return entry->second;
200 // Not found, return the first paragraph
206 void BookmarksSection::read(istream & is)
219 istringstream itmp(tmp);
221 itmp.ignore(2); // ignore ", "
223 itmp.ignore(2); // ignore ", "
225 // only load valid bookmarks
226 if (bookmarks.size() < max_bookmarks && fs::exists(fname))
227 bookmarks.push_back(Bookmark(fname, id, pos));
232 void BookmarksSection::write(ostream & os) const
234 os << '\n' << sec_bookmarks << '\n';
235 for (size_t i = 0; i < bookmarks.size(); ++i) {
236 os << bookmarks[i].par_id << ", "
237 << bookmarks[i].par_pos << ", "
238 << bookmarks[i].filename << '\n';
243 void BookmarksSection::save(std::string const & fname, int par_id, pos_type par_pos, bool persistent)
246 bookmarks.push_front(Bookmark(fname, par_id, par_pos));
247 if (bookmarks.size() > max_bookmarks)
248 bookmarks.pop_back();
251 temp_bookmark = Bookmark(fname, par_id, par_pos);
255 bool BookmarksSection::isValid(unsigned int i) const
257 // i == 0, or in the queue
258 return i <= bookmarks.size();
262 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
265 return temp_bookmark;
267 return bookmarks[i-1];
271 void ToolbarSection::read(istream & is)
280 // Read session info, saved as key/value pairs
281 // would better yell if pos returns npos
282 string::size_type pos = tmp.find_first_of(" = ");
283 // silently ignore lines without " = "
284 if (pos != string::npos) {
285 string key = tmp.substr(0, pos);
288 istringstream value(tmp.substr(pos + 3));
290 value.ignore(1); // ignore " "
292 toolbars[key] = ToolbarInfo(state, location);
298 void ToolbarSection::write(ostream & os) const
300 os << '\n' << sec_toolbars << '\n';
301 for (ToolbarMap::const_iterator tb = toolbars.begin();
302 tb != toolbars.end(); ++tb) {
303 os << tb->first << " = "
304 << static_cast<int>(tb->second.state) << " "
305 << static_cast<int>(tb->second.location) << '\n';
310 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
312 return toolbars[name];
316 void SessionInfoSection::read(istream & is)
325 // Read session info, saved as key/value pairs
326 // would better yell if pos returns npos
327 string::size_type pos = tmp.find_first_of(" = ");
328 // silently ignore lines without " = "
329 if (pos != string::npos) {
330 string key = tmp.substr(0, pos);
331 string value = tmp.substr(pos + 3);
332 sessioninfo[key] = value;
338 void SessionInfoSection::write(ostream & os) const
340 os << '\n' << sec_session << '\n';
341 for (MiscInfo::const_iterator val = sessioninfo.begin();
342 val != sessioninfo.end(); ++val) {
343 os << val->first << " = " << val->second << '\n';
348 void SessionInfoSection::save(string const & key, string const & value)
350 sessioninfo[key] = value;
354 string const SessionInfoSection::load(string const & key, bool release)
356 MiscInfo::const_iterator pos = sessioninfo.find(key);
358 if (pos != sessioninfo.end())
361 sessioninfo.erase(key);
366 Session::Session(unsigned int num) :
369 // locate the session file
370 // note that the session file name 'session' is hard-coded
371 session_file = addName(package().user_support(), "session");
377 void Session::readFile()
379 // we will not complain if we can't find session_file nor will
380 // we issue a warning. (Lgb)
381 ifstream is(session_file.c_str());
384 while (getline(is, tmp)) {
385 // Ignore comments, empty line or line stats with ' '
386 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
389 // Determine section id
390 if (tmp == sec_lastfiles)
391 lastFiles().read(is);
392 else if (tmp == sec_lastopened)
393 lastOpened().read(is);
394 else if (tmp == sec_lastfilepos)
395 lastFilePos().read(is);
396 else if (tmp == sec_bookmarks)
397 bookmarks().read(is);
398 else if (tmp == sec_toolbars)
400 else if (tmp == sec_session)
401 sessionInfo().read(is);
403 lyxerr << "LyX: Warning: unknown Session section: " << tmp << endl;
408 void Session::writeFile() const
410 ofstream os(session_file.c_str());
412 os << "## Automatically generated lyx session file \n"
413 << "## Editing this file manually may cause lyx to crash.\n";
415 lastFiles().write(os);
416 lastOpened().write(os);
417 lastFilePos().write(os);
418 bookmarks().write(os);
419 toolbars().write(os);
420 sessionInfo().write(os);
422 lyxerr << "LyX: Warning: unable to save Session: "
423 << session_file << endl;