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 (auto const & last : lastopened)
148 os << last.active << ", " << last.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 (auto const & last : lastopened) {
162 if (last.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 filepos.file = FileName(fname);
200 if (filepos.file.exists() && !filepos.file.isDirectory()
201 && lastfilepos.size() < num_lastfilepos)
202 lastfilepos.push_back(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 (auto const & file_p : lastfilepos)
216 os << file_p.pit << ", " << file_p.pos << ", " << file_p.file << '\n';
220 void LastFilePosSection::save(FilePos const & pos)
222 // Remove element if it was already present. Iterating should
223 // not be a problem since the list is small (<100 elements).
224 for (FilePosList::iterator it = lastfilepos.begin();
225 it != lastfilepos.end(); ++it)
226 if (it->file == pos.file) {
227 lastfilepos.erase(it);
231 // insert new element at front.
232 lastfilepos.push_front(pos);
236 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
238 for (auto const & fp : lastfilepos)
239 if (fp.file == fname)
240 // Has position information, return it.
243 // Not found, return the first paragraph
248 void BookmarksSection::clear()
250 // keep bookmark[0], the temporary one
252 bookmarks.resize(max_bookmarks + 1);
256 void BookmarksSection::read(istream & is)
264 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
269 // idx, pit, pos, file\n
274 istringstream itmp(tmp);
276 itmp.ignore(2); // ignore ", "
278 itmp.ignore(2); // ignore ", "
280 itmp.ignore(2); // ignore ", "
281 getline(itmp, fname);
282 if (!FileName::isAbsolute(fname))
284 FileName const file(fname);
285 // only load valid bookmarks
286 if (file.exists() && !file.isDirectory() && idx < bookmarks.size())
287 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
289 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
291 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
297 void BookmarksSection::write(ostream & os) const
299 os << '\n' << sec_bookmarks << '\n';
300 for (size_t i = 0; i < bookmarks.size(); ++i) {
303 << bookmarks[i].bottom_pit << ", "
304 << bookmarks[i].bottom_pos << ", "
305 << bookmarks[i].filename << '\n';
310 void BookmarksSection::save(FileName const & fname,
311 pit_type bottom_pit, pos_type bottom_pos,
312 int top_id, pos_type top_pos, unsigned int idx)
314 // silently ignore bookmarks when idx is out of range
315 if (idx < bookmarks.size())
316 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
320 bool BookmarksSection::isValid(unsigned int i) const
322 return i < bookmarks.size() && !bookmarks[i].filename.empty();
326 bool BookmarksSection::hasValid() const
328 for (size_t i = 1; i < bookmarks.size(); ++i) {
336 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
342 BookmarksSection::BookmarkPosList
343 BookmarksSection::bookmarksInPar(FileName const & fn, int const par_id) const
345 // FIXME: we do not consider the case of bottom_pit.
346 // This is probably not a problem.
347 BookmarksSection::BookmarkPosList bip;
348 for (size_t i = 1; i < bookmarks.size(); ++i)
349 if (bookmarks[i].filename == fn && bookmarks[i].top_id == par_id)
350 bip.push_back({i, bookmarks[i].top_pos});
356 LastCommandsSection::LastCommandsSection(unsigned int num) :
357 default_num_last_commands(30),
358 absolute_max_last_commands(100)
360 setNumberOfLastCommands(num);
364 void LastCommandsSection::read(istream & is)
372 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
375 lastcommands.push_back(tmp);
380 void LastCommandsSection::write(ostream & os) const
382 os << '\n' << sec_lastcommands << '\n';
383 copy(lastcommands.begin(), lastcommands.end(),
384 ostream_iterator<std::string>(os, "\n"));
388 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
390 if (0 < no && no <= absolute_max_last_commands)
391 num_lastcommands = no;
393 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
394 << "\tdefault (=" << default_num_last_commands << ") used.");
395 num_lastcommands = default_num_last_commands;
400 void LastCommandsSection::add(std::string const & command)
402 lastcommands.push_back(command);
406 void LastCommandsSection::clear()
408 lastcommands.clear();
412 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
413 last_files(num_last_files), last_commands(num_last_commands)
415 // locate the session file
416 // note that the session file name 'session' is hard-coded
417 session_file = FileName(addName(package().user_support().absFileName(), "session"));
423 void Session::readFile()
425 // we will not complain if we can't find session_file nor will
426 // we issue a warning. (Lgb)
427 ifstream is(session_file.toFilesystemEncoding().c_str());
430 while (getline(is, tmp)) {
431 // Ignore comments, empty line or line stats with ' '
432 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
435 // Determine section id
436 if (tmp == sec_lastfiles)
437 lastFiles().read(is);
438 else if (tmp == sec_lastopened)
439 lastOpened().read(is);
440 else if (tmp == sec_lastfilepos)
441 lastFilePos().read(is);
442 else if (tmp == sec_bookmarks)
443 bookmarks().read(is);
444 else if (tmp == sec_lastcommands)
445 lastCommands().read(is);
446 else if (tmp == sec_authfiles)
447 authFiles().read(is);
448 else if (tmp == sec_shellescape)
449 shellescapeFiles().read(is);
452 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
457 void Session::writeFile() const
459 ofstream os(session_file.toFilesystemEncoding().c_str());
461 os << "## Automatically generated lyx session file \n"
462 << "## Editing this file manually may cause lyx to crash.\n";
464 lastFiles().write(os);
465 lastOpened().write(os);
466 lastFilePos().write(os);
467 lastCommands().write(os);
468 bookmarks().write(os);
469 authFiles().write(os);
470 shellescapeFiles().write(os);
472 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
477 AuthFilesSection::AuthFilesSection() { }
480 void AuthFilesSection::read(istream & is)
488 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
492 FileName const file(tmp);
493 if (file.exists() && !file.isDirectory())
494 auth_files_.insert(tmp);
496 LYXERR(Debug::INIT, "LyX: Warning: Ignore auth file: " << tmp);
501 void AuthFilesSection::write(ostream & os) const
503 os << '\n' << sec_authfiles << '\n';
504 copy(auth_files_.begin(), auth_files_.end(),
505 ostream_iterator<std::string>(os, "\n"));
509 bool AuthFilesSection::find(string const & name) const
511 return auth_files_.find(name) != auth_files_.end();
515 void AuthFilesSection::insert(string const & name)
517 auth_files_.insert(name);
521 void ShellEscapeSection::read(istream & is)
529 if (s.empty() || s[0] == '#' || s[0] == ' ' || !FileName::isAbsolute(s))
532 // read shellescape files
533 FileName const file(s.substr(0, s.length() - 2));
534 if (file.exists() && !file.isDirectory())
535 shellescape_files_.insert(s);
537 LYXERR(Debug::INIT, "LyX: Warning: Ignore shellescape file: " << file);
542 void ShellEscapeSection::write(ostream & os) const
544 os << '\n' << sec_shellescape << '\n';
545 copy(shellescape_files_.begin(), shellescape_files_.end(),
546 ostream_iterator<std::string>(os, "\n"));
550 bool ShellEscapeSection::find(string const & name) const
552 if (shellescape_files_.find(name + ",0") != shellescape_files_.end())
555 return findAuth(name);
559 bool ShellEscapeSection::findAuth(string const & name) const
561 return shellescape_files_.find(name + ",1") != shellescape_files_.end();
565 void ShellEscapeSection::insert(string const & name, bool auth)
567 set<string>::iterator it;
568 string const name0 = name + ",0";
569 string const name1 = name + ",1";
572 it = shellescape_files_.find(name0);
573 if (it != shellescape_files_.end())
574 shellescape_files_.erase(it);
575 shellescape_files_.insert(name1);
577 it = shellescape_files_.find(name1);
578 if (it != shellescape_files_.end())
579 shellescape_files_.erase(it);
580 shellescape_files_.insert(name0);
585 void ShellEscapeSection::remove(string const & name)
587 set<string>::iterator it = shellescape_files_.find(name + ",0");
588 if (it == shellescape_files_.end())
589 it = shellescape_files_.find(name + ",1");
590 if (it != shellescape_files_.end())
591 shellescape_files_.erase(it);