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 <= max_bookmarks)
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 <= max_bookmarks; ++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 <= max_bookmarks)
316 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
320 bool BookmarksSection::isValid(unsigned int i) const
322 return i <= max_bookmarks && !bookmarks[i].filename.empty();
326 bool BookmarksSection::hasValid() const
328 for (size_t i = 1; i <= size(); ++i) {
336 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
342 LastCommandsSection::LastCommandsSection(unsigned int num) :
343 default_num_last_commands(30),
344 absolute_max_last_commands(100)
346 setNumberOfLastCommands(num);
350 void LastCommandsSection::read(istream & is)
358 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
361 lastcommands.push_back(tmp);
366 void LastCommandsSection::write(ostream & os) const
368 os << '\n' << sec_lastcommands << '\n';
369 copy(lastcommands.begin(), lastcommands.end(),
370 ostream_iterator<std::string>(os, "\n"));
374 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
376 if (0 < no && no <= absolute_max_last_commands)
377 num_lastcommands = no;
379 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
380 << "\tdefault (=" << default_num_last_commands << ") used.");
381 num_lastcommands = default_num_last_commands;
386 void LastCommandsSection::add(std::string const & command)
388 lastcommands.push_back(command);
392 void LastCommandsSection::clear()
394 lastcommands.clear();
398 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
399 last_files(num_last_files), last_commands(num_last_commands)
401 // locate the session file
402 // note that the session file name 'session' is hard-coded
403 session_file = FileName(addName(package().user_support().absFileName(), "session"));
409 void Session::readFile()
411 // we will not complain if we can't find session_file nor will
412 // we issue a warning. (Lgb)
413 ifstream is(session_file.toFilesystemEncoding().c_str());
416 while (getline(is, tmp)) {
417 // Ignore comments, empty line or line stats with ' '
418 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
421 // Determine section id
422 if (tmp == sec_lastfiles)
423 lastFiles().read(is);
424 else if (tmp == sec_lastopened)
425 lastOpened().read(is);
426 else if (tmp == sec_lastfilepos)
427 lastFilePos().read(is);
428 else if (tmp == sec_bookmarks)
429 bookmarks().read(is);
430 else if (tmp == sec_lastcommands)
431 lastCommands().read(is);
432 else if (tmp == sec_authfiles)
433 authFiles().read(is);
434 else if (tmp == sec_shellescape)
435 shellescapeFiles().read(is);
438 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
443 void Session::writeFile() const
445 ofstream os(session_file.toFilesystemEncoding().c_str());
447 os << "## Automatically generated lyx session file \n"
448 << "## Editing this file manually may cause lyx to crash.\n";
450 lastFiles().write(os);
451 lastOpened().write(os);
452 lastFilePos().write(os);
453 lastCommands().write(os);
454 bookmarks().write(os);
455 authFiles().write(os);
456 shellescapeFiles().write(os);
458 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
463 AuthFilesSection::AuthFilesSection() { }
466 void AuthFilesSection::read(istream & is)
474 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
478 FileName const file(tmp);
479 if (file.exists() && !file.isDirectory())
480 auth_files_.insert(tmp);
482 LYXERR(Debug::INIT, "LyX: Warning: Ignore auth file: " << tmp);
487 void AuthFilesSection::write(ostream & os) const
489 os << '\n' << sec_authfiles << '\n';
490 copy(auth_files_.begin(), auth_files_.end(),
491 ostream_iterator<std::string>(os, "\n"));
495 bool AuthFilesSection::find(string const & name) const
497 return auth_files_.find(name) != auth_files_.end();
501 void AuthFilesSection::insert(string const & name)
503 auth_files_.insert(name);
507 void ShellEscapeSection::read(istream & is)
515 if (s.empty() || s[0] == '#' || s[0] == ' ' || !FileName::isAbsolute(s))
518 // read shellescape files
519 FileName const file(s.substr(0, s.length() - 2));
520 if (file.exists() && !file.isDirectory())
521 shellescape_files_.insert(s);
523 LYXERR(Debug::INIT, "LyX: Warning: Ignore shellescape file: " << file);
528 void ShellEscapeSection::write(ostream & os) const
530 os << '\n' << sec_shellescape << '\n';
531 copy(shellescape_files_.begin(), shellescape_files_.end(),
532 ostream_iterator<std::string>(os, "\n"));
536 bool ShellEscapeSection::find(string const & name) const
538 if (shellescape_files_.find(name + ",0") != shellescape_files_.end())
541 return findAuth(name);
545 bool ShellEscapeSection::findAuth(string const & name) const
547 return shellescape_files_.find(name + ",1") != shellescape_files_.end();
551 void ShellEscapeSection::insert(string const & name, bool auth)
553 set<string>::iterator it;
554 string const name0 = name + ",0";
555 string const name1 = name + ",1";
558 it = shellescape_files_.find(name0);
559 if (it != shellescape_files_.end())
560 shellescape_files_.erase(it);
561 shellescape_files_.insert(name1);
563 it = shellescape_files_.find(name1);
564 if (it != shellescape_files_.end())
565 shellescape_files_.erase(it);
566 shellescape_files_.insert(name0);
571 void ShellEscapeSection::remove(string const & name)
573 set<string>::iterator it = shellescape_files_.find(name + ",0");
574 if (it == shellescape_files_.end())
575 it = shellescape_files_.find(name + ",1");
576 if (it != shellescape_files_.end())
577 shellescape_files_.erase(it);