* lyx_1_6.py:
[lyx.git] / src / Session.h
blob7fabfa8909c7fd9adc72aea2550f0517acd7a2df
1 // -*- C++ -*-
2 /**
3 * \file Session.h
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
8 * \author Bo Peng
10 * Full author contact details are available in file CREDITS.
13 #ifndef SESSION_H
14 #define SESSION_H
16 #include "support/FileName.h"
17 #include "support/types.h"
19 #include <string>
20 #include <deque>
21 #include <vector>
22 #include <map>
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)
28 4. bookmarks
29 5. general purpose session info in the form of key/value pairs
30 6. the latest commands entered in the command buffer (lastcommands)
32 namespace lyx {
34 /* base class for all sections in the session file
36 class SessionSection
38 public:
39 ///
40 SessionSection() {}
41 ///
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;
50 private:
51 /// uncopiable
52 SessionSection(SessionSection const &);
53 void operator=(SessionSection const &);
57 class LastFilesSection : SessionSection
59 public:
60 ///
61 typedef std::deque<support::FileName> LastFiles;
63 public:
64 ///
65 explicit LastFilesSection(unsigned int num = 4);
67 ///
68 void read(std::istream & is);
70 ///
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);
85 private:
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
93 LastFiles 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
107 public:
109 typedef std::vector<support::FileName> LastOpened;
111 public:
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
128 void clear();
130 private:
131 /// a list of lastopened files
132 LastOpened lastopened;
136 class LastFilePosSection : SessionSection
138 public:
140 struct FilePos {
141 FilePos() : pit(0), pos(0) {}
142 pit_type pit;
143 pos_type pos;
147 typedef std::map<support::FileName, FilePos> FilePosMap;
149 public:
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;
170 private:
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
182 public:
183 /// A bookmark is composed of three parts
184 /// 1. filename
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
193 class Bookmark {
194 public:
195 /// Filename
196 support::FileName filename;
197 /// Bottom level cursor pit, will be saved/restored by .lyx/session
198 pit_type bottom_pit;
199 /// Bottom level cursor position, will be saved/restore by .lyx/session
200 pos_type bottom_pos;
201 /// Top level cursor id, used to lcoate bookmarks for opened files
202 int top_id;
203 /// Top level cursor position within a paragraph
204 pos_type top_pos;
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) {
213 bottom_pit = pit;
214 bottom_pos = pos;
215 top_id = id;
220 typedef std::vector<Bookmark> BookmarkList;
222 public:
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
244 void clear();
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; }
257 private:
259 /// a list of bookmarks
260 BookmarkList bookmarks;
263 unsigned int const max_bookmarks;
267 class LastCommandsSection : SessionSection
269 public:
271 typedef std::vector<std::string> LastCommands;
273 public:
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
292 void clear();
294 private:
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;
314 class Session
316 public:
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; }
343 private:
344 friend class LyX;
345 /// uncopiable
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.
357 void readFile();
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();
375 } // lyx
377 #endif