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 lastopened.push_back(lof);
157 void LastOpenedSection::clear()
163 void LastFilePosSection::read(istream & is)
171 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
179 istringstream itmp(tmp);
181 itmp.ignore(2); // ignore ", "
183 itmp.ignore(2); // ignore ", "
184 getline(itmp, fname);
185 if (!FileName::isAbsolute(fname))
187 FileName const file(fname);
188 if (file.exists() && !file.isDirectory()
189 && lastfilepos.size() < num_lastfilepos)
190 lastfilepos[file] = filepos;
192 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
194 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
200 void LastFilePosSection::write(ostream & os) const
202 os << '\n' << sec_lastfilepos << '\n';
203 for (FilePosMap::const_iterator file = lastfilepos.begin();
204 file != lastfilepos.end(); ++file) {
205 os << file->second.pit << ", " << file->second.pos << ", "
206 << file->first << '\n';
211 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
213 lastfilepos[fname] = pos;
217 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
219 FilePosMap::const_iterator entry = lastfilepos.find(fname);
220 // Has position information, return it.
221 if (entry != lastfilepos.end())
222 return entry->second;
223 // Not found, return the first paragraph
228 void BookmarksSection::clear()
230 // keep bookmark[0], the temporary one
232 bookmarks.resize(max_bookmarks + 1);
236 void BookmarksSection::read(istream & is)
244 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
249 // idx, pit, pos, file\n
254 istringstream itmp(tmp);
256 itmp.ignore(2); // ignore ", "
258 itmp.ignore(2); // ignore ", "
260 itmp.ignore(2); // ignore ", "
261 getline(itmp, fname);
262 if (!FileName::isAbsolute(fname))
264 FileName const file(fname);
265 // only load valid bookmarks
266 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
267 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
269 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
271 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
277 void BookmarksSection::write(ostream & os) const
279 os << '\n' << sec_bookmarks << '\n';
280 for (size_t i = 0; i <= max_bookmarks; ++i) {
283 << bookmarks[i].bottom_pit << ", "
284 << bookmarks[i].bottom_pos << ", "
285 << bookmarks[i].filename << '\n';
290 void BookmarksSection::save(FileName const & fname,
291 pit_type bottom_pit, pos_type bottom_pos,
292 int top_id, pos_type top_pos, unsigned int idx)
294 // silently ignore bookmarks when idx is out of range
295 if (idx <= max_bookmarks)
296 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
300 bool BookmarksSection::isValid(unsigned int i) const
302 return i <= max_bookmarks && !bookmarks[i].filename.empty();
306 bool BookmarksSection::hasValid() const
308 for (size_t i = 1; i <= size(); ++i) {
316 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
322 LastCommandsSection::LastCommandsSection(unsigned int num) :
323 default_num_last_commands(30),
324 absolute_max_last_commands(100)
326 setNumberOfLastCommands(num);
330 void LastCommandsSection::read(istream & is)
338 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
341 lastcommands.push_back(tmp);
346 void LastCommandsSection::write(ostream & os) const
348 os << '\n' << sec_lastcommands << '\n';
349 copy(lastcommands.begin(), lastcommands.end(),
350 ostream_iterator<std::string>(os, "\n"));
354 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
356 if (0 < no && no <= absolute_max_last_commands)
357 num_lastcommands = no;
359 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
360 << "\tdefault (=" << default_num_last_commands << ") used.");
361 num_lastcommands = default_num_last_commands;
366 void LastCommandsSection::add(std::string const & string)
368 lastcommands.push_back(string);
372 void LastCommandsSection::clear()
374 lastcommands.clear();
378 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
379 last_files(num_last_files), last_commands(num_last_commands)
381 // locate the session file
382 // note that the session file name 'session' is hard-coded
383 session_file = FileName(addName(package().user_support().absFileName(), "session"));
389 void Session::readFile()
391 // we will not complain if we can't find session_file nor will
392 // we issue a warning. (Lgb)
393 ifstream is(session_file.toFilesystemEncoding().c_str());
396 while (getline(is, tmp)) {
397 // Ignore comments, empty line or line stats with ' '
398 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
401 // Determine section id
402 if (tmp == sec_lastfiles)
403 lastFiles().read(is);
404 else if (tmp == sec_lastopened)
405 lastOpened().read(is);
406 else if (tmp == sec_lastfilepos)
407 lastFilePos().read(is);
408 else if (tmp == sec_bookmarks)
409 bookmarks().read(is);
410 else if (tmp == sec_lastcommands)
411 lastCommands().read(is);
414 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
419 void Session::writeFile() const
421 ofstream os(session_file.toFilesystemEncoding().c_str());
423 os << "## Automatically generated lyx session file \n"
424 << "## Editing this file manually may cause lyx to crash.\n";
426 lastFiles().write(os);
427 lastOpened().write(os);
428 lastFilePos().write(os);
429 lastCommands().write(os);
430 bookmarks().write(os);
432 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "