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_lastcommands = "[last commands]";
35 string const sec_authfiles = "[auth files]";
36 string const sec_shellescape = "[shell escape files]";
38 //string const sec_session = "[session info]";
39 //string const sec_toolbars = "[toolbars]";
46 LastFilesSection::LastFilesSection(unsigned int num) :
47 default_num_last_files(4),
48 absolute_max_last_files(100)
50 setNumberOfLastFiles(num);
54 void LastFilesSection::read(istream & is)
62 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
66 FileName const file(tmp);
67 if (file.exists() && !file.isDirectory()
68 && lastfiles.size() < num_lastfiles)
69 lastfiles.push_back(file);
71 LYXERR(Debug::INIT, "LyX: Warning: Ignore last file: " << tmp);
76 void LastFilesSection::write(ostream & os) const
78 os << '\n' << sec_lastfiles << '\n';
79 copy(lastfiles.begin(), lastfiles.end(),
80 ostream_iterator<FileName>(os, "\n"));
84 void LastFilesSection::add(FileName const & file)
86 // If file already exist, delete it and reinsert at front.
87 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
88 if (it != lastfiles.end())
90 lastfiles.insert(lastfiles.begin(), file);
91 if (lastfiles.size() > num_lastfiles)
96 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
98 if (0 < no && no <= absolute_max_last_files)
101 LYXERR(Debug::INIT, "LyX: session: too many last files\n"
102 << "\tdefault (=" << default_num_last_files << ") used.");
103 num_lastfiles = default_num_last_files;
108 void LastOpenedSection::read(istream & is)
116 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ')
121 istringstream itmp(tmp);
123 itmp.ignore(2); // ignore ", "
125 getline(itmp, fname);
126 if (!FileName::isAbsolute(fname))
129 FileName const file(fname);
130 if (file.exists() && !file.isDirectory()) {
131 lof.file_name = file;
132 lastopened.push_back(lof);
135 "LyX: Warning: Ignore last opened file: " << tmp);
139 "LyX: Warning: unknown state of last opened file: " << tmp);
145 void LastOpenedSection::write(ostream & os) const
147 os << '\n' << sec_lastopened << '\n';
148 for (auto const & last : lastopened)
149 os << last.active << ", " << last.file_name << '\n';
153 void LastOpenedSection::add(FileName const & file, bool active)
155 LastOpenedFile lof(file, active);
156 // check if file is already recorded (this can happen
157 // with multiple buffer views). We do only record each
158 // file once, since we cannot restore multiple views
159 // currently, we even crash in some cases (see #9483).
160 // FIXME: Add session support for multiple views of
161 // the same buffer (split-view etc.).
162 for (auto const & last : lastopened) {
163 if (last.file_name == file)
166 lastopened.push_back(lof);
170 void LastOpenedSection::clear()
176 void LastFilePosSection::read(istream & is)
184 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
192 istringstream itmp(tmp);
194 itmp.ignore(2); // ignore ", "
196 itmp.ignore(2); // ignore ", "
197 getline(itmp, fname);
198 if (!FileName::isAbsolute(fname))
200 filepos.file = FileName(fname);
201 if (filepos.file.exists() && !filepos.file.isDirectory()
202 && lastfilepos.size() < num_lastfilepos)
203 lastfilepos.push_back(filepos);
205 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
207 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
213 void LastFilePosSection::write(ostream & os) const
215 os << '\n' << sec_lastfilepos << '\n';
216 for (auto const & file_p : lastfilepos)
217 os << file_p.pit << ", " << file_p.pos << ", " << file_p.file << '\n';
221 void LastFilePosSection::save(FilePos const & pos)
223 // Remove element if it was already present. Iterating should
224 // not be a problem since the list is small (<100 elements).
225 for (FilePosList::iterator it = lastfilepos.begin();
226 it != lastfilepos.end(); ++it)
227 if (it->file == pos.file) {
228 lastfilepos.erase(it);
232 // insert new element at front.
233 lastfilepos.push_front(pos);
237 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
239 for (auto const & fp : lastfilepos)
240 if (fp.file == fname)
241 // Has position information, return it.
244 // Not found, return the first paragraph
249 void BookmarksSection::clear()
251 // keep bookmark[0], the temporary one
253 bookmarks.resize(max_bookmarks + 1);
257 void BookmarksSection::read(istream & is)
265 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
270 // idx, pit, pos, file\n
275 istringstream itmp(tmp);
277 itmp.ignore(2); // ignore ", "
279 itmp.ignore(2); // ignore ", "
281 itmp.ignore(2); // ignore ", "
282 getline(itmp, fname);
283 if (!FileName::isAbsolute(fname))
285 FileName const file(fname);
286 // only load valid bookmarks
287 if (file.exists() && !file.isDirectory() && idx < bookmarks.size())
288 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
290 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
292 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
298 void BookmarksSection::write(ostream & os) const
300 os << '\n' << sec_bookmarks << '\n';
301 for (size_t i = 0; i < bookmarks.size(); ++i) {
304 << bookmarks[i].bottom_pit << ", "
305 << bookmarks[i].bottom_pos << ", "
306 << bookmarks[i].filename << '\n';
311 void BookmarksSection::save(FileName const & fname,
312 pit_type bottom_pit, pos_type bottom_pos,
313 int top_id, pos_type top_pos, unsigned int idx)
315 // silently ignore bookmarks when idx is out of range
316 if (idx < bookmarks.size())
317 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
321 bool BookmarksSection::isValid(unsigned int i) const
323 return i < bookmarks.size() && !bookmarks[i].filename.empty();
327 bool BookmarksSection::hasValid() const
329 for (size_t i = 1; i < bookmarks.size(); ++i) {
337 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
343 BookmarksSection::BookmarkPosList
344 BookmarksSection::bookmarksInPar(FileName const & fn, int const par_id) const
346 // FIXME: we do not consider the case of bottom_pit.
347 // This is probably not a problem.
348 BookmarksSection::BookmarkPosList bip;
349 for (size_t i = 1; i < bookmarks.size(); ++i)
350 if (bookmarks[i].filename == fn && bookmarks[i].top_id == par_id)
351 bip.push_back({i, bookmarks[i].top_pos});
357 void BookmarksSection::adjustPosAfterPos(FileName const & fn,
358 int const par_id, pos_type pos, int offset)
360 for (Bookmark & bkm : bookmarks)
361 if (bkm.filename == fn && bkm.top_id == par_id && bkm.top_pos > pos)
362 bkm.top_pos += offset;
366 LastCommandsSection::LastCommandsSection(unsigned int num) :
367 default_num_last_commands(30),
368 absolute_max_last_commands(100)
370 setNumberOfLastCommands(num);
374 void LastCommandsSection::read(istream & is)
382 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
385 lastcommands.push_back(tmp);
390 void LastCommandsSection::write(ostream & os) const
392 os << '\n' << sec_lastcommands << '\n';
393 copy(lastcommands.begin(), lastcommands.end(),
394 ostream_iterator<std::string>(os, "\n"));
398 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
400 if (0 < no && no <= absolute_max_last_commands)
401 num_lastcommands = no;
403 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
404 << "\tdefault (=" << default_num_last_commands << ") used.");
405 num_lastcommands = default_num_last_commands;
410 void LastCommandsSection::add(std::string const & command)
412 // remove traces of 'command' in history using the erase-remove idiom
413 // https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
414 lastcommands.erase(remove(lastcommands.begin(), lastcommands.end(), command),
416 // add it at the end of the list.
417 lastcommands.push_back(command);
421 void LastCommandsSection::clear()
423 lastcommands.clear();
427 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
428 last_files(num_last_files), last_commands(num_last_commands)
430 // locate the session file
431 // note that the session file name 'session' is hard-coded
432 session_file = FileName(addName(package().user_support().absFileName(), "session"));
438 void Session::readFile()
440 // we will not complain if we can't find session_file nor will
441 // we issue a warning. (Lgb)
442 ifstream is(session_file.toFilesystemEncoding().c_str());
445 while (getline(is, tmp)) {
446 // Ignore comments, empty line or line stats with ' '
447 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
450 // Determine section id
451 if (tmp == sec_lastfiles)
452 lastFiles().read(is);
453 else if (tmp == sec_lastopened)
454 lastOpened().read(is);
455 else if (tmp == sec_lastfilepos)
456 lastFilePos().read(is);
457 else if (tmp == sec_bookmarks)
458 bookmarks().read(is);
459 else if (tmp == sec_lastcommands)
460 lastCommands().read(is);
461 else if (tmp == sec_authfiles)
462 authFiles().read(is);
463 else if (tmp == sec_shellescape)
464 shellescapeFiles().read(is);
467 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
472 void Session::writeFile() const
474 ofstream os(session_file.toFilesystemEncoding().c_str());
476 os << "## Automatically generated lyx session file \n"
477 << "## Editing this file manually may cause lyx to crash.\n";
479 lastFiles().write(os);
480 lastOpened().write(os);
481 lastFilePos().write(os);
482 lastCommands().write(os);
483 bookmarks().write(os);
484 authFiles().write(os);
485 shellescapeFiles().write(os);
487 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
492 AuthFilesSection::AuthFilesSection() { }
495 void AuthFilesSection::read(istream & is)
503 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
507 FileName const file(tmp);
508 if (file.exists() && !file.isDirectory())
509 auth_files_.insert(tmp);
511 LYXERR(Debug::INIT, "LyX: Warning: Ignore auth file: " << tmp);
516 void AuthFilesSection::write(ostream & os) const
518 os << '\n' << sec_authfiles << '\n';
519 copy(auth_files_.begin(), auth_files_.end(),
520 ostream_iterator<std::string>(os, "\n"));
524 bool AuthFilesSection::find(string const & name) const
526 return auth_files_.find(name) != auth_files_.end();
530 void AuthFilesSection::insert(string const & name)
532 auth_files_.insert(name);
536 void ShellEscapeSection::read(istream & is)
544 if (s.empty() || s[0] == '#' || s[0] == ' ' || !FileName::isAbsolute(s))
547 // read shellescape files
548 FileName const file(s.substr(0, s.length() - 2));
549 if (file.exists() && !file.isDirectory())
550 shellescape_files_.insert(s);
552 LYXERR(Debug::INIT, "LyX: Warning: Ignore shellescape file: " << file);
557 void ShellEscapeSection::write(ostream & os) const
559 os << '\n' << sec_shellescape << '\n';
560 copy(shellescape_files_.begin(), shellescape_files_.end(),
561 ostream_iterator<std::string>(os, "\n"));
565 bool ShellEscapeSection::find(string const & name) const
567 if (shellescape_files_.find(name + ",0") != shellescape_files_.end())
570 return findAuth(name);
574 bool ShellEscapeSection::findAuth(string const & name) const
576 return shellescape_files_.find(name + ",1") != shellescape_files_.end();
580 void ShellEscapeSection::insert(string const & name, bool auth)
582 set<string>::iterator it;
583 string const name0 = name + ",0";
584 string const name1 = name + ",1";
587 it = shellescape_files_.find(name0);
588 if (it != shellescape_files_.end())
589 shellescape_files_.erase(it);
590 shellescape_files_.insert(name1);
592 it = shellescape_files_.find(name1);
593 if (it != shellescape_files_.end())
594 shellescape_files_.erase(it);
595 shellescape_files_.insert(name0);
600 void ShellEscapeSection::remove(string const & name)
602 set<string>::iterator it = shellescape_files_.find(name + ",0");
603 if (it == shellescape_files_.end())
604 it = shellescape_files_.find(name + ",1");
605 if (it != shellescape_files_.end())
606 shellescape_files_.erase(it);