4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
16 #include "support/FileName.h"
17 #include "support/types.h"
24 // used by at least frontends/qt4/GuiPref.cpp
25 const long maxlastfiles = 20;
27 /** This session file maintains
28 1. the latest documents loaded (lastfiles)
29 2. cursor positions of files closed (lastfilepos)
30 3. opened files when a lyx session is closed (lastopened)
32 5. general purpose session info in the form of key/value pairs
36 /* base class for all sections in the session file
44 virtual ~SessionSection() {}
46 /// read section from std::istream
47 virtual void read(std::istream & is) = 0;
49 /// write to std::ostream
50 virtual void write(std::ostream & os) const = 0;
54 SessionSection(SessionSection const &);
55 void operator=(SessionSection const &);
59 class LastFilesSection : SessionSection
63 typedef std::deque<support::FileName> LastFiles;
67 explicit LastFilesSection(unsigned int num = 4);
70 void read(std::istream & is);
73 void write(std::ostream & os) const;
75 /// Return lastfiles container (deque)
76 LastFiles const lastFiles() const { return lastfiles; }
78 /** Insert #file# into the lastfile dequeue.
79 This funtion inserts #file# into the last files list. If the file
80 already exists it is moved to the top of the list, else exist it
81 is placed on the top of the list. If the list is full the last
82 file in the list is popped from the end.
83 @param file the file to insert in the lastfile list.
85 void add(support::FileName const & file);
88 /// Default number of lastfiles.
89 unsigned int const default_num_last_files;
91 /// Max number of lastfiles.
92 unsigned int const absolute_max_last_files;
94 /// a list of lastfiles
97 /// number of files in the lastfiles list.
98 unsigned int num_lastfiles;
100 /** Used by the constructor to set the number of stored last files.
101 @param num the number of lastfiles to set.
103 void setNumberOfLastFiles(unsigned int num);
107 class LastOpenedSection : SessionSection
111 typedef std::vector<support::FileName> LastOpened;
115 void read(std::istream & is);
118 void write(std::ostream & os) const;
120 /// Return lastopened container (vector)
121 LastOpened const getfiles() const { return lastopened; }
123 /** add file to lastopened file list
124 @param file filename to add
126 void add(support::FileName const & file);
128 /** clear lastopened file list
133 /// a list of lastopened files
134 LastOpened lastopened;
138 class LastFilePosSection : SessionSection
143 FilePos() : pit(0), pos(0) {}
149 typedef std::map<support::FileName, FilePos> FilePosMap;
153 LastFilePosSection() : num_lastfilepos(100) {}
156 void read(std::istream & is);
159 void write(std::ostream & os) const;
161 /** add cursor position to the fname entry in the filepos map
162 @param fname file entry for which to save position information
163 @param pos position of the cursor when the file is closed.
165 void save(support::FileName const & fname, FilePos const & pos);
167 /** load saved cursor position from the fname entry in the filepos map
168 @param fname file entry for which to load position information
170 FilePos load(support::FileName const & fname) const;
173 /// default number of lastfilepos to save */
174 unsigned int const num_lastfilepos;
177 /// a map of file positions
178 FilePosMap lastfilepos;
182 class BookmarksSection : SessionSection
185 /// A bookmark is composed of three parts
187 /// 2. bottom (whole document) level pit and pos, used to (inaccurately) save/restore a bookmark
188 /// 3. top level id and pos, used to accurately locate bookmark when lyx is running
189 /// top and bottom level information sometimes needs to be sync'ed. In particular,
190 /// top_id is determined when a bookmark is restored from session; and
191 /// bottom_pit and bottom_pos are determined from top_id when a bookmark
192 /// is save to session. (What a mess! :-)
194 /// TODO: bottom level pit and pos will be replaced by StableDocIterator
198 support::FileName filename;
199 /// Bottom level cursor pit, will be saved/restored by .lyx/session
201 /// Bottom level cursor position, will be saved/restore by .lyx/session
203 /// Top level cursor id, used to lcoate bookmarks for opened files
205 /// Top level cursor position within a paragraph
208 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
210 Bookmark(support::FileName const & f, pit_type pit, pos_type pos, int id, pos_type tpos)
211 : filename(f), bottom_pit(pit), bottom_pos(pos), top_id(id), top_pos(tpos) {}
212 /// set bookmark top_id, this is because newly loaded bookmark
213 /// may have zero par_id and par_pit can change during editing, see bug 3092
214 void updatePos(pit_type pit, pos_type pos, int id) {
222 typedef std::vector<Bookmark> BookmarkList;
225 /// constructor, set max_bookmarks
226 /// allow 9 regular bookmarks, bookmark 0 is temporary
227 BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
229 /// Save the current position as bookmark
230 void save(support::FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
231 int top_id, pos_type top_pos, unsigned int idx);
233 /// return bookmark 0-9, bookmark 0 is the temporary bookmark
234 Bookmark const & bookmark(unsigned int i) const;
236 /// does the given bookmark have a saved position ?
237 bool isValid(unsigned int i) const;
240 unsigned int size() const { return max_bookmarks; }
242 /// clear all bookmarks
246 void read(std::istream & is);
249 void write(std::ostream & os) const;
251 /** return bookmark list. Non-const container is used since
252 bookmarks will be cleaned after use.
254 BookmarkList & load() { return bookmarks; }
258 /// a list of bookmarks
259 BookmarkList bookmarks;
262 unsigned int const max_bookmarks;
266 class SessionInfoSection : SessionSection
270 typedef std::map<std::string, std::string> MiscInfo;
274 void read(std::istream & is);
277 void write(std::ostream & os) const;
280 @param key key of the value to store
281 @param value value, a string without newline ('\n')
283 void save(std::string const & key, std::string const & value);
285 /** load session info
286 @param key a key to extract value from the session file
287 @param release whether or not clear the value. Default to true
288 since most of such values are supposed to be used only once.
290 std::string const load(std::string const & key, bool release = true);
293 /// a map to save session info
294 MiscInfo sessioninfo;
301 /// Read the session file. @param num length of lastfiles
302 explicit Session(unsigned int num = 4);
303 /// Write the session file.
304 void writeFile() const;
306 LastFilesSection & lastFiles() { return last_files; }
308 LastFilesSection const & lastFiles() const { return last_files; }
310 LastOpenedSection & lastOpened() { return last_opened; }
312 LastOpenedSection const & lastOpened() const { return last_opened; }
314 LastFilePosSection & lastFilePos() { return last_file_pos; }
316 LastFilePosSection const & lastFilePos() const { return last_file_pos; }
318 BookmarksSection & bookmarks() { return bookmarks_; }
320 BookmarksSection const & bookmarks() const { return bookmarks_; }
322 SessionInfoSection & sessionInfo() { return session_info; }
324 SessionInfoSection const & sessionInfo() const { return session_info; }
329 Session(Session const &);
330 void operator=(Session const &);
332 /// file to save session, determined in the constructor.
333 support::FileName session_file;
335 /** Read the session file.
336 Reads the #.lyx/session# at the beginning of the LyX session.
337 This will read the session file (usually #.lyx/session#).
338 @param file the file containing the session.
343 LastFilesSection last_files;
345 LastOpenedSection last_opened;
347 LastFilePosSection last_file_pos;
349 BookmarksSection bookmarks_;
351 SessionInfoSection session_info;
354 /// This is a singleton class. Get the instance.
355 /// Implemented in LyX.cpp.
356 Session & theSession();