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]";
43 LastFilesSection::LastFilesSection(unsigned int num) :
44 default_num_last_files(4),
45 absolute_max_last_files(100)
47 setNumberOfLastFiles(num);
51 void LastFilesSection::read(istream & is)
59 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
63 FileName const file(tmp);
64 if (file.exists() && !file.isDirectory()
65 && lastfiles.size() < num_lastfiles)
66 lastfiles.push_back(file);
68 LYXERR(Debug::INIT, "LyX: Warning: Ignore last file: " << tmp);
73 void LastFilesSection::write(ostream & os) const
75 os << '\n' << sec_lastfiles << '\n';
76 copy(lastfiles.begin(), lastfiles.end(),
77 ostream_iterator<FileName>(os, "\n"));
81 void LastFilesSection::add(FileName const & file)
83 // If file already exist, delete it and reinsert at front.
84 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
85 if (it != lastfiles.end())
87 lastfiles.insert(lastfiles.begin(), file);
88 if (lastfiles.size() > num_lastfiles)
93 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
95 if (0 < no && no <= absolute_max_last_files)
98 LYXERR(Debug::INIT, "LyX: session: too many last files\n"
99 << "\tdefault (=" << default_num_last_files << ") used.");
100 num_lastfiles = default_num_last_files;
105 void LastOpenedSection::read(istream & is)
113 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ')
118 istringstream itmp(tmp);
120 itmp.ignore(2); // ignore ", "
122 getline(itmp, fname);
123 if (!FileName::isAbsolute(fname))
126 FileName const file(fname);
127 if (file.exists() && !file.isDirectory()) {
128 lof.file_name = file;
129 lastopened.push_back(lof);
132 "LyX: Warning: Ignore last opened file: " << tmp);
136 "LyX: Warning: unknown state of last opened file: " << tmp);
142 void LastOpenedSection::write(ostream & os) const
144 os << '\n' << sec_lastopened << '\n';
145 for (size_t i = 0; i < lastopened.size(); ++i)
146 os << lastopened[i].active << ", " << lastopened[i].file_name << '\n';
150 void LastOpenedSection::add(FileName const & file, bool active)
152 LastOpenedFile lof(file, active);
153 // check if file is already recorded (this can happen
154 // with multiple buffer views). We do only record each
155 // file once, since we cannot restore multiple views
156 // currently, we even crash in some cases (see #9483).
157 // FIXME: Add session support for multiple views of
158 // the same buffer (split-view etc.).
159 for (size_t i = 0; i < lastopened.size(); ++i) {
160 if (lastopened[i].file_name == file)
163 lastopened.push_back(lof);
167 void LastOpenedSection::clear()
173 void LastFilePosSection::read(istream & is)
181 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
189 istringstream itmp(tmp);
191 itmp.ignore(2); // ignore ", "
193 itmp.ignore(2); // ignore ", "
194 getline(itmp, fname);
195 if (!FileName::isAbsolute(fname))
197 FileName const file(fname);
198 if (file.exists() && !file.isDirectory()
199 && lastfilepos.size() < num_lastfilepos)
200 lastfilepos[file] = filepos;
202 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
204 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
210 void LastFilePosSection::write(ostream & os) const
212 os << '\n' << sec_lastfilepos << '\n';
213 for (FilePosMap::const_iterator file = lastfilepos.begin();
214 file != lastfilepos.end(); ++file) {
215 os << file->second.pit << ", " << file->second.pos << ", "
216 << file->first << '\n';
221 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
223 lastfilepos[fname] = pos;
227 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
229 FilePosMap::const_iterator entry = lastfilepos.find(fname);
230 // Has position information, return it.
231 if (entry != lastfilepos.end())
232 return entry->second;
233 // Not found, return the first paragraph
238 void BookmarksSection::clear()
240 // keep bookmark[0], the temporary one
242 bookmarks.resize(max_bookmarks + 1);
246 void BookmarksSection::read(istream & is)
254 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
259 // idx, pit, pos, file\n
264 istringstream itmp(tmp);
266 itmp.ignore(2); // ignore ", "
268 itmp.ignore(2); // ignore ", "
270 itmp.ignore(2); // ignore ", "
271 getline(itmp, fname);
272 if (!FileName::isAbsolute(fname))
274 FileName const file(fname);
275 // only load valid bookmarks
276 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
277 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
279 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
281 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
287 void BookmarksSection::write(ostream & os) const
289 os << '\n' << sec_bookmarks << '\n';
290 for (size_t i = 0; i <= max_bookmarks; ++i) {
293 << bookmarks[i].bottom_pit << ", "
294 << bookmarks[i].bottom_pos << ", "
295 << bookmarks[i].filename << '\n';
300 void BookmarksSection::save(FileName const & fname,
301 pit_type bottom_pit, pos_type bottom_pos,
302 int top_id, pos_type top_pos, unsigned int idx)
304 // silently ignore bookmarks when idx is out of range
305 if (idx <= max_bookmarks)
306 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
310 bool BookmarksSection::isValid(unsigned int i) const
312 return i <= max_bookmarks && !bookmarks[i].filename.empty();
316 bool BookmarksSection::hasValid() const
318 for (size_t i = 1; i <= size(); ++i) {
326 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
332 LastCommandsSection::LastCommandsSection(unsigned int num) :
333 default_num_last_commands(30),
334 absolute_max_last_commands(100)
336 setNumberOfLastCommands(num);
340 void LastCommandsSection::read(istream & is)
348 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
351 lastcommands.push_back(tmp);
356 void LastCommandsSection::write(ostream & os) const
358 os << '\n' << sec_lastcommands << '\n';
359 copy(lastcommands.begin(), lastcommands.end(),
360 ostream_iterator<std::string>(os, "\n"));
364 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
366 if (0 < no && no <= absolute_max_last_commands)
367 num_lastcommands = no;
369 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
370 << "\tdefault (=" << default_num_last_commands << ") used.");
371 num_lastcommands = default_num_last_commands;
376 void LastCommandsSection::add(std::string const & string)
378 lastcommands.push_back(string);
382 void LastCommandsSection::clear()
384 lastcommands.clear();
388 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
389 last_files(num_last_files), last_commands(num_last_commands)
391 // locate the session file
392 // note that the session file name 'session' is hard-coded
393 session_file = FileName(addName(package().user_support().absFileName(), "session"));
399 void Session::readFile()
401 // we will not complain if we can't find session_file nor will
402 // we issue a warning. (Lgb)
403 ifstream is(session_file.toFilesystemEncoding().c_str());
406 while (getline(is, tmp)) {
407 // Ignore comments, empty line or line stats with ' '
408 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
411 // Determine section id
412 if (tmp == sec_lastfiles)
413 lastFiles().read(is);
414 else if (tmp == sec_lastopened)
415 lastOpened().read(is);
416 else if (tmp == sec_lastfilepos)
417 lastFilePos().read(is);
418 else if (tmp == sec_bookmarks)
419 bookmarks().read(is);
420 else if (tmp == sec_lastcommands)
421 lastCommands().read(is);
424 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
429 void Session::writeFile() const
431 ofstream os(session_file.toFilesystemEncoding().c_str());
433 os << "## Automatically generated lyx session file \n"
434 << "## Editing this file manually may cause lyx to crash.\n";
436 lastFiles().write(os);
437 lastOpened().write(os);
438 lastFilePos().write(os);
439 lastCommands().write(os);
440 bookmarks().write(os);
442 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "