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] == ' ')
175 istringstream itmp(tmp);
177 itmp.ignore(2); // ignore ", "
179 itmp.ignore(2); // ignore ", "
180 getline(itmp, fname);
181 if (!absolutePath(fname))
183 FileName const file(fname);
184 if (file.exists() && !file.isDirectory()
185 && lastfilepos.size() < num_lastfilepos)
186 lastfilepos[file] = filepos;
188 LYXERR(Debug::INIT) << "LyX: Warning: Ignore pos of last file: " << fname << endl;
190 LYXERR(Debug::INIT) << "LyX: Warning: unknown pos of last file: " << tmp << endl;
196 void LastFilePosSection::write(ostream & os) const
198 os << '\n' << sec_lastfilepos << '\n';
199 for (FilePosMap::const_iterator file = lastfilepos.begin();
200 file != lastfilepos.end(); ++file) {
201 os << file->second.pit << ", " << file->second.pos << ", "
202 << file->first << '\n';
207 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
209 lastfilepos[fname] = pos;
213 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
215 FilePosMap::const_iterator entry = lastfilepos.find(fname);
216 // Has position information, return it.
217 if (entry != lastfilepos.end())
218 return entry->second;
219 // Not found, return the first paragraph
224 void BookmarksSection::clear()
226 // keep bookmark[0], the temporary one
228 bookmarks.resize(max_bookmarks + 1);
232 void BookmarksSection::read(istream & is)
240 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
245 // idx, pit, pos, file\n
250 istringstream itmp(tmp);
252 itmp.ignore(2); // ignore ", "
254 itmp.ignore(2); // ignore ", "
256 itmp.ignore(2); // ignore ", "
257 getline(itmp, fname);
258 if (!absolutePath(fname))
260 FileName const file(fname);
261 // only load valid bookmarks
262 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
263 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
265 LYXERR(Debug::INIT) << "LyX: Warning: Ignore bookmark of file: " << fname << endl;
267 LYXERR(Debug::INIT) << "LyX: Warning: unknown Bookmark info: " << tmp << endl;
273 void BookmarksSection::write(ostream & os) const
275 os << '\n' << sec_bookmarks << '\n';
276 for (size_t i = 1; i <= max_bookmarks; ++i) {
279 << bookmarks[i].bottom_pit << ", "
280 << bookmarks[i].bottom_pos << ", "
281 << bookmarks[i].filename << '\n';
286 void BookmarksSection::save(FileName const & fname,
287 pit_type bottom_pit, pos_type bottom_pos,
288 int top_id, pos_type top_pos, unsigned int idx)
290 // silently ignore bookmarks when idx is out of range
291 if (idx <= max_bookmarks)
292 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
296 bool BookmarksSection::isValid(unsigned int i) const
298 return i <= max_bookmarks && !bookmarks[i].filename.empty();
302 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
308 void ToolbarSection::read(istream & is)
316 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
320 // Read session info, saved as key/value pairs
321 // would better yell if pos returns npos
322 size_t pos = tmp.find_first_of(" = ");
323 // silently ignore lines without " = "
324 if (pos != string::npos) {
326 item.key = tmp.substr(0, pos);
329 istringstream value(tmp.substr(pos + 3));
332 value >> item.info.posx;
333 value >> item.info.posy;
334 item.info.state = ToolbarInfo::State(state);
335 item.info.location = ToolbarInfo::Location(location);
336 toolbars.push_back(item);
338 LYXERR(Debug::INIT) << "LyX: Warning: Ignore toolbar info: " << tmp << endl;
340 LYXERR(Debug::INIT) << "LyX: Warning: unknown Toolbar info: " << tmp << endl;
343 // sort the toolbars by location, line and position
344 std::sort(toolbars.begin(), toolbars.end());
348 void ToolbarSection::write(ostream & os) const
350 os << '\n' << sec_toolbars << '\n';
351 for (ToolbarList::const_iterator tb = toolbars.begin();
352 tb != toolbars.end(); ++tb) {
353 os << tb->key << " = "
354 << static_cast<int>(tb->info.state) << " "
355 << static_cast<int>(tb->info.location) << " "
356 << tb->info.posx << " "
357 << tb->info.posy << '\n';
362 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
364 for (ToolbarList::iterator tb = toolbars.begin();
365 tb != toolbars.end(); ++tb)
372 toolbars.push_back(item);
373 return toolbars.back().info;
377 bool operator<(ToolbarSection::ToolbarItem const & a, ToolbarSection::ToolbarItem const & b)
379 ToolbarSection::ToolbarInfo lhs = a.info;
380 ToolbarSection::ToolbarInfo rhs = b.info;
382 if (lhs.state != rhs.state)
383 return static_cast<int>(lhs.state) < static_cast<int>(rhs.state);
384 // order of dock does not really matter
385 if (lhs.location != rhs.location)
386 return static_cast<int>(lhs.location) < static_cast<int>(rhs.location);
387 // if the same dock, the order depends on position
388 if (lhs.location == ToolbarSection::ToolbarInfo::TOP ||
389 lhs.location == ToolbarSection::ToolbarInfo::BOTTOM)
390 return lhs.posy < rhs.posy || (lhs.posy == rhs.posy && lhs.posx < rhs.posx);
391 else if (lhs.location == ToolbarSection::ToolbarInfo::LEFT ||
392 lhs.location == ToolbarSection::ToolbarInfo::RIGHT)
393 return lhs.posx < rhs.posx || (lhs.posx == rhs.posx && lhs.posy < rhs.posy);
398 void SessionInfoSection::read(istream & is)
406 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
410 // Read session info, saved as key/value pairs
411 // would better yell if pos returns npos
412 string::size_type pos = tmp.find_first_of(" = ");
413 // silently ignore lines without " = "
414 if (pos != string::npos) {
415 string key = tmp.substr(0, pos);
416 string value = tmp.substr(pos + 3);
417 sessioninfo[key] = value;
419 LYXERR(Debug::INIT) << "LyX: Warning: Ignore session info: " << tmp << endl;
421 LYXERR(Debug::INIT) << "LyX: Warning: unknown Session info: " << tmp << endl;
427 void SessionInfoSection::write(ostream & os) const
429 os << '\n' << sec_session << '\n';
430 for (MiscInfo::const_iterator val = sessioninfo.begin();
431 val != sessioninfo.end(); ++val) {
432 os << val->first << " = " << val->second << '\n';
437 void SessionInfoSection::save(string const & key, string const & value)
439 sessioninfo[key] = value;
443 string const SessionInfoSection::load(string const & key, bool release)
445 MiscInfo::const_iterator pos = sessioninfo.find(key);
447 if (pos != sessioninfo.end())
450 sessioninfo.erase(key);
455 Session::Session(unsigned int num) :
458 // locate the session file
459 // note that the session file name 'session' is hard-coded
460 session_file = FileName(addName(package().user_support().absFilename(), "session"));
466 void Session::readFile()
468 // we will not complain if we can't find session_file nor will
469 // we issue a warning. (Lgb)
470 ifstream is(session_file.toFilesystemEncoding().c_str());
473 while (getline(is, tmp)) {
474 // Ignore comments, empty line or line stats with ' '
475 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
478 // Determine section id
479 if (tmp == sec_lastfiles)
480 lastFiles().read(is);
481 else if (tmp == sec_lastopened)
482 lastOpened().read(is);
483 else if (tmp == sec_lastfilepos)
484 lastFilePos().read(is);
485 else if (tmp == sec_bookmarks)
486 bookmarks().read(is);
487 else if (tmp == sec_toolbars)
489 else if (tmp == sec_session)
490 sessionInfo().read(is);
492 LYXERR(Debug::INIT) << "LyX: Warning: unknown Session section: " << tmp << endl;
497 void Session::writeFile() const
499 ofstream os(session_file.toFilesystemEncoding().c_str());
501 os << "## Automatically generated lyx session file \n"
502 << "## Editing this file manually may cause lyx to crash.\n";
504 lastFiles().write(os);
505 lastOpened().write(os);
506 lastFilePos().write(os);
507 bookmarks().write(os);
508 toolbars().write(os);
509 sessionInfo().write(os);
511 LYXERR(Debug::INIT) << "LyX: Warning: unable to save Session: "
512 << session_file << endl;