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/FileNameList.h"
18 #include "support/filetools.h"
19 #include "support/Package.h"
27 using namespace lyx::support;
31 string const sec_lastfiles = "[recent files]";
32 string const sec_lastfilepos = "[cursor positions]";
33 string const sec_lastopened = "[last opened files]";
34 string const sec_bookmarks = "[bookmarks]";
35 string const sec_session = "[session info]";
36 string const sec_toolbars = "[toolbars]";
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*/)
109 FileNameList list;// = theApp()->fileNameListFromSession("last_opened");
110 for (size_t i = 0; i != list.size(); ++i) {
111 FileName const & file = list[i];
112 if (!file.isAbsolute() || !file.exists() || file.isDirectory())
113 LYXERR(Debug::INIT, "Warning: invalid last opened file: " << file);
115 lastopened.push_back(file);
120 void LastOpenedSection::write(ostream & /*os*/) const
122 //theApp()->toSession(lastopened);
126 void LastOpenedSection::add(FileName const & file)
128 lastopened.push_back(file);
132 void LastOpenedSection::clear()
138 void LastFilePosSection::read(istream & is)
146 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
154 istringstream itmp(tmp);
156 itmp.ignore(2); // ignore ", "
158 itmp.ignore(2); // ignore ", "
159 getline(itmp, fname);
160 FileName const file(fname);
161 if (!file.isAbsolute())
163 if (file.exists() && !file.isDirectory()
164 && lastfilepos.size() < num_lastfilepos)
165 lastfilepos[file] = filepos;
167 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
169 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
175 void LastFilePosSection::write(ostream & os) const
177 os << '\n' << sec_lastfilepos << '\n';
178 for (FilePosMap::const_iterator file = lastfilepos.begin();
179 file != lastfilepos.end(); ++file) {
180 os << file->second.pit << ", " << file->second.pos << ", "
181 << file->first << '\n';
186 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
188 lastfilepos[fname] = pos;
192 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
194 FilePosMap::const_iterator entry = lastfilepos.find(fname);
195 // Has position information, return it.
196 if (entry != lastfilepos.end())
197 return entry->second;
198 // Not found, return the first paragraph
203 void BookmarksSection::clear()
205 // keep bookmark[0], the temporary one
207 bookmarks.resize(max_bookmarks + 1);
211 void BookmarksSection::read(istream & is)
219 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
224 // idx, pit, pos, file\n
229 istringstream itmp(tmp);
231 itmp.ignore(2); // ignore ", "
233 itmp.ignore(2); // ignore ", "
235 itmp.ignore(2); // ignore ", "
236 getline(itmp, fname);
237 FileName const file(fname);
238 if (!file.isAbsolute())
240 // only load valid bookmarks
241 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
242 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
244 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
246 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
252 void BookmarksSection::write(ostream & os) const
254 os << '\n' << sec_bookmarks << '\n';
255 for (size_t i = 1; i <= max_bookmarks; ++i) {
258 << bookmarks[i].bottom_pit << ", "
259 << bookmarks[i].bottom_pos << ", "
260 << bookmarks[i].filename << '\n';
265 void BookmarksSection::save(FileName const & fname,
266 pit_type bottom_pit, pos_type bottom_pos,
267 int top_id, pos_type top_pos, unsigned int idx)
269 // silently ignore bookmarks when idx is out of range
270 if (idx <= max_bookmarks)
271 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
275 bool BookmarksSection::isValid(unsigned int i) const
277 return i <= max_bookmarks && !bookmarks[i].filename.empty();
281 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
287 Session::Session(unsigned int num) :
290 // locate the session file
291 // note that the session file name 'session' is hard-coded
292 session_file = FileName(addName(package().user_support().absFilename(), "session"));
298 void Session::readFile()
300 // we will not complain if we can't find session_file nor will
301 // we issue a warning. (Lgb)
302 ifstream is(session_file.toFilesystemEncoding().c_str());
305 while (getline(is, tmp)) {
306 // Ignore comments, empty line or line stats with ' '
307 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
310 // Determine section id
311 if (tmp == sec_lastfiles)
312 lastFiles().read(is);
313 else if (tmp == sec_lastopened)
314 lastOpened().read(is);
315 else if (tmp == sec_lastfilepos)
316 lastFilePos().read(is);
317 else if (tmp == sec_bookmarks)
318 bookmarks().read(is);
320 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
325 void Session::writeFile() const
327 ofstream os(session_file.toFilesystemEncoding().c_str());
329 os << "## Automatically generated lyx session file \n"
330 << "## Editing this file manually may cause lyx to crash.\n";
332 lastFiles().write(os);
333 lastOpened().write(os);
334 lastFilePos().write(os);
335 bookmarks().write(os);
337 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "