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
30 6. the latest commands entered in the command buffer (lastcommands)
34 /* base class for all sections in the session file
42 virtual ~SessionSection() {}
44 /// read section from std::istream
45 virtual void read(std::istream & is) = 0;
47 /// write to std::ostream
48 virtual void write(std::ostream & os) const = 0;
52 SessionSection(SessionSection const &);
53 void operator=(SessionSection const &);
57 class LastFilesSection : SessionSection
61 typedef std::deque<support::FileName> LastFiles;
65 explicit LastFilesSection(unsigned int num = 4);
68 void read(std::istream & is);
71 void write(std::ostream & os) const;
73 /// Return lastfiles container (deque)
74 LastFiles const lastFiles() const { return lastfiles; }
76 /** Insert #file# into the lastfile dequeue.
77 This funtion inserts #file# into the last files list. If the file
78 already exists it is moved to the top of the list, else exist it
79 is placed on the top of the list. If the list is full the last
80 file in the list is popped from the end.
81 @param file the file to insert in the lastfile list.
83 void add(support::FileName const & file);
86 /// Default number of lastfiles.
87 unsigned int const default_num_last_files;
89 /// Max number of lastfiles.
90 unsigned int const absolute_max_last_files;
92 /// a list of lastfiles
95 /// number of files in the lastfiles list.
96 unsigned int num_lastfiles;
98 /** Used by the constructor to set the number of stored last files.
99 @param num the number of lastfiles to set.
101 void setNumberOfLastFiles(unsigned int num);
105 class LastOpenedSection : SessionSection
109 struct LastOpenedFile {
110 LastOpenedFile() : file_name(), active(false) {}
112 LastOpenedFile(support::FileName file_name_, bool active_)
113 : file_name(file_name_), active(active_) {}
115 support::FileName file_name;
119 typedef std::vector<LastOpenedFile> LastOpened;
123 void read(std::istream & is);
126 void write(std::ostream & os) const;
128 /// Return lastopened container (vector)
129 LastOpened const getfiles() const { return lastopened; }
131 /** add file to lastopened file list
132 @param file filename to add
134 void add(support::FileName const & file, bool active = false);
136 /** clear lastopened file list
141 /// a list of lastopened files
142 LastOpened lastopened;
146 class LastFilePosSection : SessionSection
151 FilePos() : pit(0), pos(0) {}
157 typedef std::map<support::FileName, FilePos> FilePosMap;
161 LastFilePosSection() : num_lastfilepos(100) {}
164 void read(std::istream & is);
167 void write(std::ostream & os) const;
169 /** add cursor position to the fname entry in the filepos map
170 @param fname file entry for which to save position information
171 @param pos position of the cursor when the file is closed.
173 void save(support::FileName const & fname, FilePos const & pos);
175 /** load saved cursor position from the fname entry in the filepos map
176 @param fname file entry for which to load position information
178 FilePos load(support::FileName const & fname) const;
181 /// default number of lastfilepos to save */
182 unsigned int const num_lastfilepos;
185 /// a map of file positions
186 FilePosMap lastfilepos;
190 class BookmarksSection : SessionSection
193 /// A bookmark is composed of three parts
195 /// 2. bottom (whole document) level pit and pos, used to (inaccurately) save/restore a bookmark
196 /// 3. top level id and pos, used to accurately locate bookmark when lyx is running
197 /// top and bottom level information sometimes needs to be sync'ed. In particular,
198 /// top_id is determined when a bookmark is restored from session; and
199 /// bottom_pit and bottom_pos are determined from top_id when a bookmark
200 /// is save to session. (What a mess! :-)
202 /// TODO: bottom level pit and pos will be replaced by StableDocIterator
206 support::FileName filename;
207 /// Bottom level cursor pit, will be saved/restored by .lyx/session
209 /// Bottom level cursor position, will be saved/restore by .lyx/session
211 /// Top level cursor id, used to lcoate bookmarks for opened files
213 /// Top level cursor position within a paragraph
216 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
218 Bookmark(support::FileName const & f, pit_type pit, pos_type pos, int id, pos_type tpos)
219 : filename(f), bottom_pit(pit), bottom_pos(pos), top_id(id), top_pos(tpos) {}
220 /// set bookmark top_id, this is because newly loaded bookmark
221 /// may have zero par_id and par_pit can change during editing, see bug 3092
222 void updatePos(pit_type pit, pos_type pos, int id) {
230 typedef std::vector<Bookmark> BookmarkList;
233 /// constructor, set max_bookmarks
234 /// allow 9 regular bookmarks, bookmark 0 is temporary
235 BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
237 /// Save the current position as bookmark
238 void save(support::FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
239 int top_id, pos_type top_pos, unsigned int idx);
241 /// return bookmark 0-9, bookmark 0 is the temporary bookmark
242 Bookmark const & bookmark(unsigned int i) const;
244 /// does the given bookmark have a saved position ?
245 bool isValid(unsigned int i) const;
247 /// is there at least one bookmark that has a saved position ?
248 bool hasValid() const;
251 unsigned int size() const { return max_bookmarks; }
253 /// clear all bookmarks
257 void read(std::istream & is);
260 void write(std::ostream & os) const;
262 /** return bookmark list. Non-const container is used since
263 bookmarks will be cleaned after use.
265 BookmarkList & load() { return bookmarks; }
269 /// a list of bookmarks
270 BookmarkList bookmarks;
273 unsigned int const max_bookmarks;
277 class LastCommandsSection : SessionSection
281 typedef std::vector<std::string> LastCommands;
285 LastCommandsSection(unsigned int num);
287 void read(std::istream & is);
290 void write(std::ostream & os) const;
292 /// Return lastcommands container (vector)
293 LastCommands const getcommands() const { return lastcommands; }
295 /** add command to lastcommands list
296 @param command command to add
298 void add(std::string const & command);
300 /** clear lastcommands list
305 /// number of commands in the lastcommands list.
306 unsigned int num_lastcommands;
308 /** Used by the constructor to set the number of stored last commands.
309 @param num the number of lastcommands to set.
311 void setNumberOfLastCommands(unsigned int num);
313 /// a list of lastopened commands
314 LastCommands lastcommands;
316 /// Default number of lastcommands.
317 unsigned int const default_num_last_commands;
319 /// Max number of lastcommands.
320 unsigned int const absolute_max_last_commands;
327 /// Read the session file. @param num length of lastfiles
328 explicit Session(unsigned int num_last_files = 4,
329 unsigned int num_last_commands = 30);
330 /// Write the session file.
331 void writeFile() const;
333 LastFilesSection & lastFiles() { return last_files; }
335 LastFilesSection const & lastFiles() const { return last_files; }
337 LastOpenedSection & lastOpened() { return last_opened; }
339 LastOpenedSection const & lastOpened() const { return last_opened; }
341 LastFilePosSection & lastFilePos() { return last_file_pos; }
343 LastFilePosSection const & lastFilePos() const { return last_file_pos; }
345 BookmarksSection & bookmarks() { return bookmarks_; }
347 BookmarksSection const & bookmarks() const { return bookmarks_; }
349 LastCommandsSection & lastCommands() { return last_commands; }
351 LastCommandsSection const & lastCommands() const { return last_commands; }
356 Session(Session const &);
357 void operator=(Session const &);
359 /// file to save session, determined in the constructor.
360 support::FileName session_file;
362 /** Read the session file.
363 Reads the #.lyx/session# at the beginning of the LyX session.
364 This will read the session file (usually #.lyx/session#).
365 @param file the file containing the session.
370 LastFilesSection last_files;
372 LastOpenedSection last_opened;
374 LastFilePosSection last_file_pos;
376 BookmarksSection bookmarks_;
378 LastCommandsSection last_commands;
381 /// This is a singleton class. Get the instance.
382 /// Implemented in LyX.cpp.
383 Session & theSession();