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]";
44 LastFilesSection::LastFilesSection(unsigned int num) :
45 default_num_last_files(4),
46 absolute_max_last_files(100)
48 setNumberOfLastFiles(num);
52 void LastFilesSection::read(istream & is)
60 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
64 FileName const file(tmp);
65 if (file.exists() && !file.isDirectory()
66 && lastfiles.size() < num_lastfiles)
67 lastfiles.push_back(file);
69 LYXERR(Debug::INIT, "LyX: Warning: Ignore last file: " << tmp);
74 void LastFilesSection::write(ostream & os) const
76 os << '\n' << sec_lastfiles << '\n';
77 copy(lastfiles.begin(), lastfiles.end(),
78 ostream_iterator<FileName>(os, "\n"));
82 void LastFilesSection::add(FileName const & file)
84 // If file already exist, delete it and reinsert at front.
85 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
86 if (it != lastfiles.end())
88 lastfiles.insert(lastfiles.begin(), file);
89 if (lastfiles.size() > num_lastfiles)
94 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
96 if (0 < no && no <= absolute_max_last_files)
99 LYXERR(Debug::INIT, "LyX: session: too many last files\n"
100 << "\tdefault (=" << default_num_last_files << ") used.");
101 num_lastfiles = default_num_last_files;
106 void LastOpenedSection::read(istream & is)
114 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ')
119 istringstream itmp(tmp);
121 itmp.ignore(2); // ignore ", "
123 getline(itmp, fname);
124 if (!FileName::isAbsolute(fname))
127 FileName const file(fname);
128 if (file.exists() && !file.isDirectory()) {
129 lof.file_name = file;
130 lastopened.push_back(lof);
133 "LyX: Warning: Ignore last opened file: " << tmp);
137 "LyX: Warning: unknown state of last opened file: " << tmp);
143 void LastOpenedSection::write(ostream & os) const
145 os << '\n' << sec_lastopened << '\n';
146 for (size_t i = 0; i < lastopened.size(); ++i)
147 os << lastopened[i].active << ", " << lastopened[i].file_name << '\n';
151 void LastOpenedSection::add(FileName const & file, bool active)
153 LastOpenedFile lof(file, active);
154 // check if file is already recorded (this can happen
155 // with multiple buffer views). We do only record each
156 // file once, since we cannot restore multiple views
157 // currently, we even crash in some cases (see #9483).
158 // FIXME: Add session support for multiple views of
159 // the same buffer (split-view etc.).
160 for (size_t i = 0; i < lastopened.size(); ++i) {
161 if (lastopened[i].file_name == file)
164 lastopened.push_back(lof);
168 void LastOpenedSection::clear()
174 void LastFilePosSection::read(istream & is)
182 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
190 istringstream itmp(tmp);
192 itmp.ignore(2); // ignore ", "
194 itmp.ignore(2); // ignore ", "
195 getline(itmp, fname);
196 if (!FileName::isAbsolute(fname))
198 FileName const file(fname);
199 if (file.exists() && !file.isDirectory()
200 && lastfilepos.size() < num_lastfilepos)
201 lastfilepos[file] = filepos;
203 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
205 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
211 void LastFilePosSection::write(ostream & os) const
213 os << '\n' << sec_lastfilepos << '\n';
214 for (FilePosMap::const_iterator file = lastfilepos.begin();
215 file != lastfilepos.end(); ++file) {
216 os << file->second.pit << ", " << file->second.pos << ", "
217 << file->first << '\n';
222 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
224 lastfilepos[fname] = pos;
228 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
230 FilePosMap::const_iterator entry = lastfilepos.find(fname);
231 // Has position information, return it.
232 if (entry != lastfilepos.end())
233 return entry->second;
234 // Not found, return the first paragraph
239 void BookmarksSection::clear()
241 // keep bookmark[0], the temporary one
243 bookmarks.resize(max_bookmarks + 1);
247 void BookmarksSection::read(istream & is)
255 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
260 // idx, pit, pos, file\n
265 istringstream itmp(tmp);
267 itmp.ignore(2); // ignore ", "
269 itmp.ignore(2); // ignore ", "
271 itmp.ignore(2); // ignore ", "
272 getline(itmp, fname);
273 if (!FileName::isAbsolute(fname))
275 FileName const file(fname);
276 // only load valid bookmarks
277 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
278 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
280 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
282 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
288 void BookmarksSection::write(ostream & os) const
290 os << '\n' << sec_bookmarks << '\n';
291 for (size_t i = 0; i <= max_bookmarks; ++i) {
294 << bookmarks[i].bottom_pit << ", "
295 << bookmarks[i].bottom_pos << ", "
296 << bookmarks[i].filename << '\n';
301 void BookmarksSection::save(FileName const & fname,
302 pit_type bottom_pit, pos_type bottom_pos,
303 int top_id, pos_type top_pos, unsigned int idx)
305 // silently ignore bookmarks when idx is out of range
306 if (idx <= max_bookmarks)
307 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
311 bool BookmarksSection::isValid(unsigned int i) const
313 return i <= max_bookmarks && !bookmarks[i].filename.empty();
317 bool BookmarksSection::hasValid() const
319 for (size_t i = 1; i <= size(); ++i) {
327 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
333 LastCommandsSection::LastCommandsSection(unsigned int num) :
334 default_num_last_commands(30),
335 absolute_max_last_commands(100)
337 setNumberOfLastCommands(num);
341 void LastCommandsSection::read(istream & is)
349 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
352 lastcommands.push_back(tmp);
357 void LastCommandsSection::write(ostream & os) const
359 os << '\n' << sec_lastcommands << '\n';
360 copy(lastcommands.begin(), lastcommands.end(),
361 ostream_iterator<std::string>(os, "\n"));
365 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
367 if (0 < no && no <= absolute_max_last_commands)
368 num_lastcommands = no;
370 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
371 << "\tdefault (=" << default_num_last_commands << ") used.");
372 num_lastcommands = default_num_last_commands;
377 void LastCommandsSection::add(std::string const & string)
379 lastcommands.push_back(string);
383 void LastCommandsSection::clear()
385 lastcommands.clear();
389 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
390 last_files(num_last_files), last_commands(num_last_commands)
392 // locate the session file
393 // note that the session file name 'session' is hard-coded
394 session_file = FileName(addName(package().user_support().absFileName(), "session"));
400 void Session::readFile()
402 // we will not complain if we can't find session_file nor will
403 // we issue a warning. (Lgb)
404 ifstream is(session_file.toFilesystemEncoding().c_str());
407 while (getline(is, tmp)) {
408 // Ignore comments, empty line or line stats with ' '
409 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
412 // Determine section id
413 if (tmp == sec_lastfiles)
414 lastFiles().read(is);
415 else if (tmp == sec_lastopened)
416 lastOpened().read(is);
417 else if (tmp == sec_lastfilepos)
418 lastFilePos().read(is);
419 else if (tmp == sec_bookmarks)
420 bookmarks().read(is);
421 else if (tmp == sec_lastcommands)
422 lastCommands().read(is);
423 else if (tmp == sec_authfiles)
424 authFiles().read(is);
427 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
432 void Session::writeFile() const
434 ofstream os(session_file.toFilesystemEncoding().c_str());
436 os << "## Automatically generated lyx session file \n"
437 << "## Editing this file manually may cause lyx to crash.\n";
439 lastFiles().write(os);
440 lastOpened().write(os);
441 lastFilePos().write(os);
442 lastCommands().write(os);
443 bookmarks().write(os);
444 authFiles().write(os);
446 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
451 AuthFilesSection::AuthFilesSection() { }
454 void AuthFilesSection::read(istream & is)
462 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
466 FileName const file(tmp);
467 if (file.exists() && !file.isDirectory())
468 auth_files_.insert(tmp);
470 LYXERR(Debug::INIT, "LyX: Warning: Ignore auth file: " << tmp);
475 void AuthFilesSection::write(ostream & os) const
477 os << '\n' << sec_authfiles << '\n';
478 copy(auth_files_.begin(), auth_files_.end(),
479 ostream_iterator<std::string>(os, "\n"));