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 /** This session file maintains
25 1. the latest documents loaded (lastfiles)
26 2. cursor positions of files closed (lastfilepos)
27 3. opened files when a lyx session is closed (lastopened)
29 5. general purpose session info in the form of key/value pairs
33 /* base class for all sections in the session file
41 virtual ~SessionSection() {}
43 /// read section from std::istream
44 virtual void read(std::istream & is) = 0;
46 /// write to std::ostream
47 virtual void write(std::ostream & os) const = 0;
51 SessionSection(SessionSection const &);
52 void operator=(SessionSection const &);
56 class LastFilesSection : SessionSection
60 typedef std::deque<support::FileName> LastFiles;
64 explicit LastFilesSection(unsigned int num = 4);
67 void read(std::istream & is);
70 void write(std::ostream & os) const;
72 /// Return lastfiles container (deque)
73 LastFiles const lastFiles() const { return lastfiles; }
75 /** Insert #file# into the lastfile dequeue.
76 This funtion inserts #file# into the last files list. If the file
77 already exists it is moved to the top of the list, else exist it
78 is placed on the top of the list. If the list is full the last
79 file in the list is popped from the end.
80 @param file the file to insert in the lastfile list.
82 void add(support::FileName const & file);
85 /// Default number of lastfiles.
86 unsigned int const default_num_last_files;
88 /// Max number of lastfiles.
89 unsigned int const absolute_max_last_files;
91 /// a list of lastfiles
94 /// number of files in the lastfiles list.
95 unsigned int num_lastfiles;
97 /** Used by the constructor to set the number of stored last files.
98 @param num the number of lastfiles to set.
100 void setNumberOfLastFiles(unsigned int num);
104 class LastOpenedSection : SessionSection
108 typedef std::vector<support::FileName> LastOpened;
112 void read(std::istream & is);
115 void write(std::ostream & os) const;
117 /// Return lastopened container (vector)
118 LastOpened const getfiles() const { return lastopened; }
120 /** add file to lastopened file list
121 @param file filename to add
123 void add(support::FileName const & file);
125 /** clear lastopened file list
130 /// a list of lastopened files
131 LastOpened lastopened;
135 class LastFilePosSection : SessionSection
140 FilePos() : pit(0), pos(0) {}
146 typedef std::map<support::FileName, FilePos> FilePosMap;
150 LastFilePosSection() : num_lastfilepos(100) {}
153 void read(std::istream & is);
156 void write(std::ostream & os) const;
158 /** add cursor position to the fname entry in the filepos map
159 @param fname file entry for which to save position information
160 @param pos position of the cursor when the file is closed.
162 void save(support::FileName const & fname, FilePos const & pos);
164 /** load saved cursor position from the fname entry in the filepos map
165 @param fname file entry for which to load position information
167 FilePos load(support::FileName const & fname) const;
170 /// default number of lastfilepos to save */
171 unsigned int const num_lastfilepos;
174 /// a map of file positions
175 FilePosMap lastfilepos;
179 class BookmarksSection : SessionSection
182 /// A bookmark is composed of three parts
184 /// 2. bottom (whole document) level pit and pos, used to (inaccurately) save/restore a bookmark
185 /// 3. top level id and pos, used to accurately locate bookmark when lyx is running
186 /// top and bottom level information sometimes needs to be sync'ed. In particular,
187 /// top_id is determined when a bookmark is restored from session; and
188 /// bottom_pit and bottom_pos are determined from top_id when a bookmark
189 /// is save to session. (What a mess! :-)
191 /// TODO: bottom level pit and pos will be replaced by StableDocIterator
195 support::FileName filename;
196 /// Bottom level cursor pit, will be saved/restored by .lyx/session
198 /// Bottom level cursor position, will be saved/restore by .lyx/session
200 /// Top level cursor id, used to lcoate bookmarks for opened files
202 /// Top level cursor position within a paragraph
205 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
207 Bookmark(support::FileName const & f, pit_type pit, pos_type pos, int id, pos_type tpos)
208 : filename(f), bottom_pit(pit), bottom_pos(pos), top_id(id), top_pos(tpos) {}
209 /// set bookmark top_id, this is because newly loaded bookmark
210 /// may have zero par_id and par_pit can change during editing, see bug 3092
211 void updatePos(pit_type pit, pos_type pos, int id) {
219 typedef std::vector<Bookmark> BookmarkList;
222 /// constructor, set max_bookmarks
223 /// allow 9 regular bookmarks, bookmark 0 is temporary
224 BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
226 /// Save the current position as bookmark
227 void save(support::FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
228 int top_id, pos_type top_pos, unsigned int idx);
230 /// return bookmark 0-9, bookmark 0 is the temporary bookmark
231 Bookmark const & bookmark(unsigned int i) const;
233 /// does the given bookmark have a saved position ?
234 bool isValid(unsigned int i) const;
237 unsigned int size() const { return max_bookmarks; }
239 /// clear all bookmarks
243 void read(std::istream & is);
246 void write(std::ostream & os) const;
248 /** return bookmark list. Non-const container is used since
249 bookmarks will be cleaned after use.
251 BookmarkList & load() { return bookmarks; }
255 /// a list of bookmarks
256 BookmarkList bookmarks;
259 unsigned int const max_bookmarks;
266 /// Read the session file. @param num length of lastfiles
267 explicit Session(unsigned int num = 4);
268 /// Write the session file.
269 void writeFile() const;
271 LastFilesSection & lastFiles() { return last_files; }
273 LastFilesSection const & lastFiles() const { return last_files; }
275 LastOpenedSection & lastOpened() { return last_opened; }
277 LastOpenedSection const & lastOpened() const { return last_opened; }
279 LastFilePosSection & lastFilePos() { return last_file_pos; }
281 LastFilePosSection const & lastFilePos() const { return last_file_pos; }
283 BookmarksSection & bookmarks() { return bookmarks_; }
285 BookmarksSection const & bookmarks() const { return bookmarks_; }
290 Session(Session const &);
291 void operator=(Session const &);
293 /// file to save session, determined in the constructor.
294 support::FileName session_file;
296 /** Read the session file.
297 Reads the #.lyx/session# at the beginning of the LyX session.
298 This will read the session file (usually #.lyx/session#).
299 @param file the file containing the session.
304 LastFilesSection last_files;
306 LastOpenedSection last_opened;
308 LastFilePosSection last_file_pos;
310 BookmarksSection bookmarks_;
313 /// This is a singleton class. Get the instance.
314 /// Implemented in LyX.cpp.
315 Session & theSession();