]> git.lyx.org Git - lyx.git/blob - src/Session.h
add short git hash to name of devel disk images to get different names for devel...
[lyx.git] / src / Session.h
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.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Bo Peng
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef SESSION_H
14 #define SESSION_H
15
16 #include "support/FileName.h"
17 #include "support/types.h"
18
19 #include <map>
20 #include <string>
21 #include <vector>
22
23 /** This session file maintains
24   1. the latest documents loaded (lastfiles)
25   2. cursor positions of files closed (lastfilepos)
26   3. opened files when a lyx session is closed (lastopened)
27   4. bookmarks
28   5. general purpose session info in the form of key/value pairs
29   6. the latest commands entered in the command buffer (lastcommands)
30  */
31 namespace lyx {
32
33 /* base class for all sections in the session file
34 */
35 class SessionSection
36 {
37 public:
38         ///
39         SessionSection() {}
40         ///
41         virtual ~SessionSection() {}
42
43         /// read section from std::istream
44         virtual void read(std::istream & is) = 0;
45
46         /// write to std::ostream
47         virtual void write(std::ostream & os) const = 0;
48
49 private:
50         /// uncopiable
51         SessionSection(SessionSection const &);
52         void operator=(SessionSection const &);
53 };
54
55
56 class LastFilesSection : SessionSection
57 {
58 public:
59         ///
60         typedef std::vector<support::FileName> LastFiles;
61
62 public:
63         ///
64         explicit LastFilesSection(unsigned int num = 4);
65
66         ///
67         void read(std::istream & is);
68
69         ///
70         void write(std::ostream & os) const;
71
72         /// Return lastfiles container (vector)
73         LastFiles const lastFiles() const { return lastfiles; }
74
75         /** Insert #file# into the lastfile vector.
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.
81         */
82         void add(support::FileName const & file);
83
84 private:
85         /// Default number of lastfiles.
86         unsigned int const default_num_last_files;
87
88         /// Max number of lastfiles.
89         unsigned int const absolute_max_last_files;
90
91         /// a list of lastfiles
92         LastFiles lastfiles;
93
94         /// number of files in the lastfiles list.
95         unsigned int num_lastfiles;
96
97         /** Used by the constructor to set the number of stored last files.
98             @param num the number of lastfiles to set.
99         */
100         void setNumberOfLastFiles(unsigned int num);
101 };
102
103
104 class LastOpenedSection : SessionSection
105 {
106 public:
107         ///
108         struct LastOpenedFile {
109                 LastOpenedFile() : file_name(), active(false) {}
110
111                 LastOpenedFile(support::FileName file_name_, bool active_)
112                         : file_name(file_name_), active(active_) {}
113
114                 support::FileName file_name;
115                 bool active;
116         };
117         ///
118         typedef std::vector<LastOpenedFile> LastOpened;
119
120 public:
121         ///
122         void read(std::istream & is);
123
124         ///
125         void write(std::ostream & os) const;
126
127         /// Return lastopened container (vector)
128         LastOpened const getfiles() const { return lastopened; }
129
130         /** add file to lastopened file list
131             @param file filename to add
132         */
133         void add(support::FileName const & file, bool active = false);
134
135         /** clear lastopened file list
136          */
137         void clear();
138
139 private:
140         /// a list of lastopened files
141         LastOpened lastopened;
142 };
143
144
145 class LastFilePosSection : SessionSection
146 {
147 public:
148         ///
149         struct FilePos {
150                 FilePos() : pit(0), pos(0) {}
151                 pit_type pit;
152                 pos_type pos;
153         };
154
155         ///
156         typedef std::map<support::FileName, FilePos> FilePosMap;
157
158 public:
159         ///
160         LastFilePosSection() : num_lastfilepos(100) {}
161
162         ///
163         void read(std::istream & is);
164
165         ///
166         void write(std::ostream & os) const;
167
168         /** add cursor position to the fname entry in the filepos map
169             @param fname file entry for which to save position information
170             @param pos position of the cursor when the BufferView is closed.
171         */
172         void save(support::FileName const & fname, FilePos const & pos);
173
174         /** load saved cursor position from the fname entry in the filepos map
175             @param fname file entry for which to load position information
176         */
177         FilePos load(support::FileName const & fname) const;
178
179 private:
180         /// default number of lastfilepos to save */
181         unsigned int const num_lastfilepos;
182
183
184         /// a map of file positions
185         FilePosMap lastfilepos;
186 };
187
188
189 class BookmarksSection : SessionSection
190 {
191 public:
192         /// A bookmark is composed of three parts
193         /// 1. filename
194         /// 2. bottom (whole document) level pit and pos, used to (inaccurately) save/restore a bookmark
195         /// 3. top level id and pos, used to accurately locate bookmark when lyx is running
196         /// top and bottom level information sometimes needs to be sync'ed. In particular,
197         /// top_id is determined when a bookmark is restored from session; and
198         /// bottom_pit and bottom_pos are determined from top_id when a bookmark
199         /// is save to session. (What a mess! :-)
200         ///
201         /// TODO: bottom level pit and pos will be replaced by StableDocIterator
202         class Bookmark {
203         public:
204                 /// Filename
205                 support::FileName filename;
206                 /// Bottom level cursor pit, will be saved/restored by .lyx/session
207                 pit_type bottom_pit;
208                 /// Bottom level cursor position, will be saved/restore by .lyx/session
209                 pos_type bottom_pos;
210                 /// Top level cursor id, used to lcoate bookmarks for opened files
211                 int top_id;
212                 /// Top level cursor position within a paragraph
213                 pos_type top_pos;
214                 ///
215                 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
216                 ///
217                 Bookmark(support::FileName const & f, pit_type pit, pos_type pos, int id, pos_type tpos)
218                         : filename(f), bottom_pit(pit), bottom_pos(pos), top_id(id), top_pos(tpos) {}
219                 /// set bookmark top_id, this is because newly loaded bookmark
220                 /// may have zero par_id and par_pit can change during editing, see bug 3092
221                 void updatePos(pit_type pit, pos_type pos, int id) {
222                         bottom_pit = pit;
223                         bottom_pos = pos;
224                         top_id = id;
225                 }
226         };
227
228         ///
229         typedef std::vector<Bookmark> BookmarkList;
230
231 public:
232         /// constructor, set max_bookmarks
233         /// allow 9 regular bookmarks, bookmark 0 is temporary
234         BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
235
236         /// Save the current position as bookmark
237         void save(support::FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
238                 int top_id, pos_type top_pos, unsigned int idx);
239
240         /// return bookmark 0-9, bookmark 0 is the temporary bookmark
241         Bookmark const & bookmark(unsigned int i) const;
242
243         /// does the given bookmark have a saved position ?
244         bool isValid(unsigned int i) const;
245
246         /// is there at least one bookmark that has a saved position ?
247         bool hasValid() const;
248
249         ///
250         unsigned int size() const { return max_bookmarks; }
251
252         /// clear all bookmarks
253         void clear();
254
255         ///
256         void read(std::istream & is);
257
258         ///
259         void write(std::ostream & os) const;
260
261         /** return bookmark list. Non-const container is used since
262                 bookmarks will be cleaned after use.
263         */
264         BookmarkList & load() { return bookmarks; }
265
266 private:
267
268         /// a list of bookmarks
269         BookmarkList bookmarks;
270
271         ///
272         unsigned int const max_bookmarks;
273 };
274
275
276 class LastCommandsSection : SessionSection
277 {
278 public:
279         ///
280         typedef std::vector<std::string> LastCommands;
281
282 public:
283         ///
284         LastCommandsSection(unsigned int num);
285         ///
286         void read(std::istream & is);
287
288         ///
289         void write(std::ostream & os) const;
290
291         /// Return lastcommands container (vector)
292         LastCommands const getcommands() const { return lastcommands; }
293
294         /** add command to lastcommands list
295             @param command command to add
296         */
297         void add(std::string const & command);
298
299         /** clear lastcommands list
300          */
301         void clear();
302
303 private:
304         /// number of commands in the lastcommands list.
305         unsigned int num_lastcommands;
306
307         /** Used by the constructor to set the number of stored last commands.
308             @param num the number of lastcommands to set.
309         */
310         void setNumberOfLastCommands(unsigned int num);
311
312         /// a list of lastopened commands
313         LastCommands lastcommands;
314
315         /// Default number of lastcommands.
316         unsigned int const default_num_last_commands;
317
318         /// Max number of lastcommands.
319         unsigned int const absolute_max_last_commands;
320 };
321
322
323 class Session
324 {
325 public:
326         /// Read the session file.  @param num length of lastfiles
327         explicit Session(unsigned int num_last_files = 4,
328                 unsigned int num_last_commands = 30);
329         /// Write the session file.
330         void writeFile() const;
331         ///
332         LastFilesSection & lastFiles() { return last_files; }
333         ///
334         LastFilesSection const & lastFiles() const { return last_files; }
335         ///
336         LastOpenedSection & lastOpened() { return last_opened; }
337         ///
338         LastOpenedSection const & lastOpened() const { return last_opened; }
339         ///
340         LastFilePosSection & lastFilePos() { return last_file_pos; }
341         ///
342         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
343         ///
344         BookmarksSection & bookmarks() { return bookmarks_; }
345         ///
346         BookmarksSection const & bookmarks() const { return bookmarks_; }
347         ///
348         LastCommandsSection & lastCommands() { return last_commands; }
349         ///
350         LastCommandsSection const & lastCommands() const { return last_commands; }
351
352 private:
353         friend class LyX;
354         /// uncopiable
355         Session(Session const &);
356         void operator=(Session const &);
357
358         /// file to save session, determined in the constructor.
359         support::FileName session_file;
360
361         /** Read the session file.
362             Reads the #.lyx/session# at the beginning of the LyX session.
363             This will read the session file (usually #.lyx/session#).
364             @param file the file containing the session.
365         */
366         void readFile();
367
368         ///
369         LastFilesSection last_files;
370         ///
371         LastOpenedSection last_opened;
372         ///
373         LastFilePosSection last_file_pos;
374         ///
375         BookmarksSection bookmarks_;
376         ///
377         LastCommandsSection last_commands;
378 };
379
380 /// This is a singleton class. Get the instance.
381 /// Implemented in LyX.cpp.
382 Session & theSession();
383
384 } // lyx
385
386 #endif