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]";
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 FileName const file(tmp);
61 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !file.isAbsolute())
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.push_front(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 FileName const file(tmp);
115 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !file.isAbsolute())
118 if (file.exists() && !file.isDirectory())
119 lastopened.push_back(file);
121 LYXERR(Debug::INIT, "LyX: Warning: Ignore last opened file: " << tmp);
126 void LastOpenedSection::write(ostream & os) const
128 os << '\n' << sec_lastopened << '\n';
129 copy(lastopened.begin(), lastopened.end(),
130 ostream_iterator<FileName>(os, "\n"));
134 void LastOpenedSection::add(FileName const & file)
136 lastopened.push_back(file);
140 void LastOpenedSection::clear()
146 void LastFilePosSection::read(istream & is)
154 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
162 istringstream itmp(tmp);
164 itmp.ignore(2); // ignore ", "
166 itmp.ignore(2); // ignore ", "
167 getline(itmp, fname);
168 FileName const file(fname);
169 if (!file.isAbsolute())
171 if (file.exists() && !file.isDirectory()
172 && lastfilepos.size() < num_lastfilepos)
173 lastfilepos[file] = filepos;
175 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
177 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
183 void LastFilePosSection::write(ostream & os) const
185 os << '\n' << sec_lastfilepos << '\n';
186 for (FilePosMap::const_iterator file = lastfilepos.begin();
187 file != lastfilepos.end(); ++file) {
188 os << file->second.pit << ", " << file->second.pos << ", "
189 << file->first << '\n';
194 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
196 lastfilepos[fname] = pos;
200 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
202 FilePosMap::const_iterator entry = lastfilepos.find(fname);
203 // Has position information, return it.
204 if (entry != lastfilepos.end())
205 return entry->second;
206 // Not found, return the first paragraph
211 void BookmarksSection::clear()
213 // keep bookmark[0], the temporary one
215 bookmarks.resize(max_bookmarks + 1);
219 void BookmarksSection::read(istream & is)
227 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
232 // idx, pit, pos, file\n
237 istringstream itmp(tmp);
239 itmp.ignore(2); // ignore ", "
241 itmp.ignore(2); // ignore ", "
243 itmp.ignore(2); // ignore ", "
244 getline(itmp, fname);
245 FileName const file(fname);
246 if (!file.isAbsolute())
248 // only load valid bookmarks
249 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
250 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
252 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
254 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
260 void BookmarksSection::write(ostream & os) const
262 os << '\n' << sec_bookmarks << '\n';
263 for (size_t i = 1; i <= max_bookmarks; ++i) {
266 << bookmarks[i].bottom_pit << ", "
267 << bookmarks[i].bottom_pos << ", "
268 << bookmarks[i].filename << '\n';
273 void BookmarksSection::save(FileName const & fname,
274 pit_type bottom_pit, pos_type bottom_pos,
275 int top_id, pos_type top_pos, unsigned int idx)
277 // silently ignore bookmarks when idx is out of range
278 if (idx <= max_bookmarks)
279 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
283 bool BookmarksSection::isValid(unsigned int i) const
285 return i <= max_bookmarks && !bookmarks[i].filename.empty();
289 bool BookmarksSection::hasValid() const
291 for (size_t i = 1; i <= size(); ++i) {
299 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
305 LastCommandsSection::LastCommandsSection(unsigned int num) :
306 default_num_last_commands(30),
307 absolute_max_last_commands(100)
309 setNumberOfLastCommands(num);
313 void LastCommandsSection::read(istream & is)
321 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
324 lastcommands.push_back(tmp);
329 void LastCommandsSection::write(ostream & os) const
331 os << '\n' << sec_lastcommands << '\n';
332 copy(lastcommands.begin(), lastcommands.end(),
333 ostream_iterator<std::string>(os, "\n"));
337 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
339 if (0 < no && no <= absolute_max_last_commands)
340 num_lastcommands = no;
342 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
343 << "\tdefault (=" << default_num_last_commands << ") used.");
344 num_lastcommands = default_num_last_commands;
349 void LastCommandsSection::add(std::string const & string)
351 lastcommands.push_back(string);
355 void LastCommandsSection::clear()
357 lastcommands.clear();
361 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
362 last_files(num_last_files), last_commands(num_last_commands)
364 // locate the session file
365 // note that the session file name 'session' is hard-coded
366 session_file = FileName(addName(package().user_support().absFilename(), "session"));
372 void Session::readFile()
374 // we will not complain if we can't find session_file nor will
375 // we issue a warning. (Lgb)
376 ifstream is(session_file.toFilesystemEncoding().c_str());
379 while (getline(is, tmp)) {
380 // Ignore comments, empty line or line stats with ' '
381 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
384 // Determine section id
385 if (tmp == sec_lastfiles)
386 lastFiles().read(is);
387 else if (tmp == sec_lastopened)
388 lastOpened().read(is);
389 else if (tmp == sec_lastfilepos)
390 lastFilePos().read(is);
391 else if (tmp == sec_bookmarks)
392 bookmarks().read(is);
393 else if (tmp == sec_lastcommands)
394 lastCommands().read(is);
397 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
402 void Session::writeFile() const
404 ofstream os(session_file.toFilesystemEncoding().c_str());
406 os << "## Automatically generated lyx session file \n"
407 << "## Editing this file manually may cause lyx to crash.\n";
409 lastFiles().write(os);
410 lastOpened().write(os);
411 lastFilePos().write(os);
412 lastCommands().write(os);
413 bookmarks().write(os);
415 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "