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 typedef std::vector
<support::FileName
> LastOpened
;
113 void read(std::istream
& is
);
116 void write(std::ostream
& os
) const;
118 /// Return lastopened container (vector)
119 LastOpened
const getfiles() const { return lastopened
; }
121 /** add file to lastopened file list
122 @param file filename to add
124 void add(support::FileName
const & file
);
126 /** clear lastopened file list
131 /// a list of lastopened files
132 LastOpened lastopened
;
136 class LastFilePosSection
: SessionSection
141 FilePos() : pit(0), pos(0) {}
147 typedef std::map
<support::FileName
, FilePos
> FilePosMap
;
151 LastFilePosSection() : num_lastfilepos(100) {}
154 void read(std::istream
& is
);
157 void write(std::ostream
& os
) const;
159 /** add cursor position to the fname entry in the filepos map
160 @param fname file entry for which to save position information
161 @param pos position of the cursor when the file is closed.
163 void save(support::FileName
const & fname
, FilePos
const & pos
);
165 /** load saved cursor position from the fname entry in the filepos map
166 @param fname file entry for which to load position information
168 FilePos
load(support::FileName
const & fname
) const;
171 /// default number of lastfilepos to save */
172 unsigned int const num_lastfilepos
;
175 /// a map of file positions
176 FilePosMap lastfilepos
;
180 class BookmarksSection
: SessionSection
183 /// A bookmark is composed of three parts
185 /// 2. bottom (whole document) level pit and pos, used to (inaccurately) save/restore a bookmark
186 /// 3. top level id and pos, used to accurately locate bookmark when lyx is running
187 /// top and bottom level information sometimes needs to be sync'ed. In particular,
188 /// top_id is determined when a bookmark is restored from session; and
189 /// bottom_pit and bottom_pos are determined from top_id when a bookmark
190 /// is save to session. (What a mess! :-)
192 /// TODO: bottom level pit and pos will be replaced by StableDocIterator
196 support::FileName filename
;
197 /// Bottom level cursor pit, will be saved/restored by .lyx/session
199 /// Bottom level cursor position, will be saved/restore by .lyx/session
201 /// Top level cursor id, used to lcoate bookmarks for opened files
203 /// Top level cursor position within a paragraph
206 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
208 Bookmark(support::FileName
const & f
, pit_type pit
, pos_type pos
, int id
, pos_type tpos
)
209 : filename(f
), bottom_pit(pit
), bottom_pos(pos
), top_id(id
), top_pos(tpos
) {}
210 /// set bookmark top_id, this is because newly loaded bookmark
211 /// may have zero par_id and par_pit can change during editing, see bug 3092
212 void updatePos(pit_type pit
, pos_type pos
, int id
) {
220 typedef std::vector
<Bookmark
> BookmarkList
;
223 /// constructor, set max_bookmarks
224 /// allow 9 regular bookmarks, bookmark 0 is temporary
225 BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
227 /// Save the current position as bookmark
228 void save(support::FileName
const & fname
, pit_type bottom_pit
, pos_type bottom_pos
,
229 int top_id
, pos_type top_pos
, unsigned int idx
);
231 /// return bookmark 0-9, bookmark 0 is the temporary bookmark
232 Bookmark
const & bookmark(unsigned int i
) const;
234 /// does the given bookmark have a saved position ?
235 bool isValid(unsigned int i
) const;
237 /// is there at least one bookmark that has a saved position ?
238 bool hasValid() const;
241 unsigned int size() const { return max_bookmarks
; }
243 /// clear all bookmarks
247 void read(std::istream
& is
);
250 void write(std::ostream
& os
) const;
252 /** return bookmark list. Non-const container is used since
253 bookmarks will be cleaned after use.
255 BookmarkList
& load() { return bookmarks
; }
259 /// a list of bookmarks
260 BookmarkList bookmarks
;
263 unsigned int const max_bookmarks
;
267 class LastCommandsSection
: SessionSection
271 typedef std::vector
<std::string
> LastCommands
;
275 LastCommandsSection(unsigned int num
);
277 void read(std::istream
& is
);
280 void write(std::ostream
& os
) const;
282 /// Return lastcommands container (vector)
283 LastCommands
const getcommands() const { return lastcommands
; }
285 /** add command to lastcommands list
286 @param command command to add
288 void add(std::string
const & command
);
290 /** clear lastcommands list
295 /// number of commands in the lastcommands list.
296 unsigned int num_lastcommands
;
298 /** Used by the constructor to set the number of stored last commands.
299 @param num the number of lastcommands to set.
301 void setNumberOfLastCommands(unsigned int num
);
303 /// a list of lastopened commands
304 LastCommands lastcommands
;
306 /// Default number of lastcommands.
307 unsigned int const default_num_last_commands
;
309 /// Max number of lastcommands.
310 unsigned int const absolute_max_last_commands
;
317 /// Read the session file. @param num length of lastfiles
318 explicit Session(unsigned int num_last_files
= 4,
319 unsigned int num_last_commands
= 30);
320 /// Write the session file.
321 void writeFile() const;
323 LastFilesSection
& lastFiles() { return last_files
; }
325 LastFilesSection
const & lastFiles() const { return last_files
; }
327 LastOpenedSection
& lastOpened() { return last_opened
; }
329 LastOpenedSection
const & lastOpened() const { return last_opened
; }
331 LastFilePosSection
& lastFilePos() { return last_file_pos
; }
333 LastFilePosSection
const & lastFilePos() const { return last_file_pos
; }
335 BookmarksSection
& bookmarks() { return bookmarks_
; }
337 BookmarksSection
const & bookmarks() const { return bookmarks_
; }
339 LastCommandsSection
& lastCommands() { return last_commands
; }
341 LastCommandsSection
const & lastCommands() const { return last_commands
; }
346 Session(Session
const &);
347 void operator=(Session
const &);
349 /// file to save session, determined in the constructor.
350 support::FileName session_file
;
352 /** Read the session file.
353 Reads the #.lyx/session# at the beginning of the LyX session.
354 This will read the session file (usually #.lyx/session#).
355 @param file the file containing the session.
360 LastFilesSection last_files
;
362 LastOpenedSection last_opened
;
364 LastFilePosSection last_file_pos
;
366 BookmarksSection bookmarks_
;
368 LastCommandsSection last_commands
;
371 /// This is a singleton class. Get the instance.
372 /// Implemented in LyX.cpp.
373 Session
& theSession();