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/debug.h"
17 #include "support/filetools.h"
18 #include "support/Package.h"
26 using namespace lyx::support;
30 string const sec_lastfiles = "[recent files]";
31 string const sec_lastfilepos = "[cursor positions]";
32 string const sec_lastopened = "[last opened files]";
33 string const sec_bookmarks = "[bookmarks]";
34 string const sec_session = "[session info]";
35 string const sec_toolbars = "[toolbars]";
36 string const sec_lastcommands = "[last commands]";
37 string const sec_authfiles = "[auth files]";
38 string const sec_shellescape = "[shell escape files]";
45 LastFilesSection::LastFilesSection(unsigned int num) :
46 default_num_last_files(4),
47 absolute_max_last_files(100)
49 setNumberOfLastFiles(num);
53 void LastFilesSection::read(istream & is)
61 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
65 FileName const file(tmp);
66 if (file.exists() && !file.isDirectory()
67 && lastfiles.size() < num_lastfiles)
68 lastfiles.push_back(file);
70 LYXERR(Debug::INIT, "LyX: Warning: Ignore last file: " << tmp);
75 void LastFilesSection::write(ostream & os) const
77 os << '\n' << sec_lastfiles << '\n';
78 copy(lastfiles.begin(), lastfiles.end(),
79 ostream_iterator<FileName>(os, "\n"));
83 void LastFilesSection::add(FileName const & file)
85 // If file already exist, delete it and reinsert at front.
86 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
87 if (it != lastfiles.end())
89 lastfiles.insert(lastfiles.begin(), file);
90 if (lastfiles.size() > num_lastfiles)
95 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
97 if (0 < no && no <= absolute_max_last_files)
100 LYXERR(Debug::INIT, "LyX: session: too many last files\n"
101 << "\tdefault (=" << default_num_last_files << ") used.");
102 num_lastfiles = default_num_last_files;
107 void LastOpenedSection::read(istream & is)
115 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ')
120 istringstream itmp(tmp);
122 itmp.ignore(2); // ignore ", "
124 getline(itmp, fname);
125 if (!FileName::isAbsolute(fname))
128 FileName const file(fname);
129 if (file.exists() && !file.isDirectory()) {
130 lof.file_name = file;
131 lastopened.push_back(lof);
134 "LyX: Warning: Ignore last opened file: " << tmp);
138 "LyX: Warning: unknown state of last opened file: " << tmp);
144 void LastOpenedSection::write(ostream & os) const
146 os << '\n' << sec_lastopened << '\n';
147 for (size_t i = 0; i < lastopened.size(); ++i)
148 os << lastopened[i].active << ", " << lastopened[i].file_name << '\n';
152 void LastOpenedSection::add(FileName const & file, bool active)
154 LastOpenedFile lof(file, active);
155 // check if file is already recorded (this can happen
156 // with multiple buffer views). We do only record each
157 // file once, since we cannot restore multiple views
158 // currently, we even crash in some cases (see #9483).
159 // FIXME: Add session support for multiple views of
160 // the same buffer (split-view etc.).
161 for (size_t i = 0; i < lastopened.size(); ++i) {
162 if (lastopened[i].file_name == file)
165 lastopened.push_back(lof);
169 void LastOpenedSection::clear()
175 void LastFilePosSection::read(istream & is)
183 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
191 istringstream itmp(tmp);
193 itmp.ignore(2); // ignore ", "
195 itmp.ignore(2); // ignore ", "
196 getline(itmp, fname);
197 if (!FileName::isAbsolute(fname))
199 FileName const file(fname);
200 if (file.exists() && !file.isDirectory()
201 && lastfilepos.size() < num_lastfilepos)
202 lastfilepos[file] = filepos;
204 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
206 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
212 void LastFilePosSection::write(ostream & os) const
214 os << '\n' << sec_lastfilepos << '\n';
215 for (FilePosMap::const_iterator file = lastfilepos.begin();
216 file != lastfilepos.end(); ++file) {
217 os << file->second.pit << ", " << file->second.pos << ", "
218 << file->first << '\n';
223 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
225 lastfilepos[fname] = pos;
229 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
231 FilePosMap::const_iterator entry = lastfilepos.find(fname);
232 // Has position information, return it.
233 if (entry != lastfilepos.end())
234 return entry->second;
235 // Not found, return the first paragraph
240 void BookmarksSection::clear()
242 // keep bookmark[0], the temporary one
244 bookmarks.resize(max_bookmarks + 1);
248 void BookmarksSection::read(istream & is)
256 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
261 // idx, pit, pos, file\n
266 istringstream itmp(tmp);
268 itmp.ignore(2); // ignore ", "
270 itmp.ignore(2); // ignore ", "
272 itmp.ignore(2); // ignore ", "
273 getline(itmp, fname);
274 if (!FileName::isAbsolute(fname))
276 FileName const file(fname);
277 // only load valid bookmarks
278 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
279 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
281 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
283 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
289 void BookmarksSection::write(ostream & os) const
291 os << '\n' << sec_bookmarks << '\n';
292 for (size_t i = 0; i <= max_bookmarks; ++i) {
295 << bookmarks[i].bottom_pit << ", "
296 << bookmarks[i].bottom_pos << ", "
297 << bookmarks[i].filename << '\n';
302 void BookmarksSection::save(FileName const & fname,
303 pit_type bottom_pit, pos_type bottom_pos,
304 int top_id, pos_type top_pos, unsigned int idx)
306 // silently ignore bookmarks when idx is out of range
307 if (idx <= max_bookmarks)
308 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
312 bool BookmarksSection::isValid(unsigned int i) const
314 return i <= max_bookmarks && !bookmarks[i].filename.empty();
318 bool BookmarksSection::hasValid() const
320 for (size_t i = 1; i <= size(); ++i) {
328 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
334 LastCommandsSection::LastCommandsSection(unsigned int num) :
335 default_num_last_commands(30),
336 absolute_max_last_commands(100)
338 setNumberOfLastCommands(num);
342 void LastCommandsSection::read(istream & is)
350 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
353 lastcommands.push_back(tmp);
358 void LastCommandsSection::write(ostream & os) const
360 os << '\n' << sec_lastcommands << '\n';
361 copy(lastcommands.begin(), lastcommands.end(),
362 ostream_iterator<std::string>(os, "\n"));
366 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
368 if (0 < no && no <= absolute_max_last_commands)
369 num_lastcommands = no;
371 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
372 << "\tdefault (=" << default_num_last_commands << ") used.");
373 num_lastcommands = default_num_last_commands;
378 void LastCommandsSection::add(std::string const & string)
380 lastcommands.push_back(string);
384 void LastCommandsSection::clear()
386 lastcommands.clear();
390 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
391 last_files(num_last_files), last_commands(num_last_commands)
393 // locate the session file
394 // note that the session file name 'session' is hard-coded
395 session_file = FileName(addName(package().user_support().absFileName(), "session"));
401 void Session::readFile()
403 // we will not complain if we can't find session_file nor will
404 // we issue a warning. (Lgb)
405 ifstream is(session_file.toFilesystemEncoding().c_str());
408 while (getline(is, tmp)) {
409 // Ignore comments, empty line or line stats with ' '
410 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
413 // Determine section id
414 if (tmp == sec_lastfiles)
415 lastFiles().read(is);
416 else if (tmp == sec_lastopened)
417 lastOpened().read(is);
418 else if (tmp == sec_lastfilepos)
419 lastFilePos().read(is);
420 else if (tmp == sec_bookmarks)
421 bookmarks().read(is);
422 else if (tmp == sec_lastcommands)
423 lastCommands().read(is);
424 else if (tmp == sec_authfiles)
425 authFiles().read(is);
426 else if (tmp == sec_shellescape)
427 shellescapeFiles().read(is);
430 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
435 void Session::writeFile() const
437 ofstream os(session_file.toFilesystemEncoding().c_str());
439 os << "## Automatically generated lyx session file \n"
440 << "## Editing this file manually may cause lyx to crash.\n";
442 lastFiles().write(os);
443 lastOpened().write(os);
444 lastFilePos().write(os);
445 lastCommands().write(os);
446 bookmarks().write(os);
447 authFiles().write(os);
448 shellescapeFiles().write(os);
450 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
455 AuthFilesSection::AuthFilesSection() { }
458 void AuthFilesSection::read(istream & is)
466 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
470 FileName const file(tmp);
471 if (file.exists() && !file.isDirectory())
472 auth_files_.insert(tmp);
474 LYXERR(Debug::INIT, "LyX: Warning: Ignore auth file: " << tmp);
479 void AuthFilesSection::write(ostream & os) const
481 os << '\n' << sec_authfiles << '\n';
482 copy(auth_files_.begin(), auth_files_.end(),
483 ostream_iterator<std::string>(os, "\n"));
487 bool AuthFilesSection::find(string const & name) const
489 if (auth_files_.find(name) != auth_files_.end())
496 void AuthFilesSection::insert(string const & name)
498 auth_files_.insert(name);
502 void ShellEscapeSection::read(istream & is)
510 if (s.empty() || s[0] == '#' || s[0] == ' ' || !FileName::isAbsolute(s))
513 // read shellescape files
514 FileName const file(s.substr(0, s.length() - 2));
515 if (file.exists() && !file.isDirectory())
516 shellescape_files_.insert(s);
518 LYXERR(Debug::INIT, "LyX: Warning: Ignore shellescape file: " << file);
523 void ShellEscapeSection::write(ostream & os) const
525 os << '\n' << sec_shellescape << '\n';
526 copy(shellescape_files_.begin(), shellescape_files_.end(),
527 ostream_iterator<std::string>(os, "\n"));
531 bool ShellEscapeSection::find(string const & name) const
533 if (shellescape_files_.find(name + ",0") != shellescape_files_.end())
536 return findAuth(name);
540 bool ShellEscapeSection::findAuth(string const & name) const
542 if (shellescape_files_.find(name + ",1") != shellescape_files_.end())
549 void ShellEscapeSection::insert(string const & name, bool auth)
551 set<string>::iterator it;
552 string const name0 = name + ",0";
553 string const name1 = name + ",1";
556 it = shellescape_files_.find(name0);
557 if (it != shellescape_files_.end())
558 shellescape_files_.erase(it);
559 shellescape_files_.insert(name1);
561 it = shellescape_files_.find(name1);
562 if (it != shellescape_files_.end())
563 shellescape_files_.erase(it);
564 shellescape_files_.insert(name0);
569 void ShellEscapeSection::remove(string const & name)
571 set<string>::iterator it = shellescape_files_.find(name + ",0");
572 if (it == shellescape_files_.end())
573 it = shellescape_files_.find(name + ",1");
574 if (it != shellescape_files_.end())
575 shellescape_files_.erase(it);