]> git.lyx.org Git - lyx.git/blob - src/Session.h
* remove outdated RC_CUSTOM_EXPORT_COMMAND and RC_CUSTOM_EXPORT_FORMAT.
[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 <string>
20 #include <deque>
21 #include <vector>
22 #include <map>
23
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)
31  */
32 namespace lyx {
33
34 /* base class for all sections in the session file
35 */
36 class SessionSection
37 {
38 public:
39         ///
40         SessionSection() {}
41         ///
42         virtual ~SessionSection() {}
43
44         /// read section from std::istream
45         virtual void read(std::istream & is) = 0;
46
47         /// write to std::ostream
48         virtual void write(std::ostream & os) const = 0;
49
50 private:
51         /// uncopiable
52         SessionSection(SessionSection const &);
53         void operator=(SessionSection const &);
54 };
55
56
57 class LastFilesSection : SessionSection
58 {
59 public:
60         ///
61         typedef std::deque<support::FileName> LastFiles;
62
63 public:
64         ///
65         explicit LastFilesSection(unsigned int num = 4);
66
67         ///
68         void read(std::istream & is);
69
70         ///
71         void write(std::ostream & os) const;
72
73         /// Return lastfiles container (deque)
74         LastFiles const lastFiles() const { return lastfiles; }
75
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.
82         */
83         void add(support::FileName const & file);
84
85 private:
86         /// Default number of lastfiles.
87         unsigned int const default_num_last_files;
88
89         /// Max number of lastfiles.
90         unsigned int const absolute_max_last_files;
91
92         /// a list of lastfiles
93         LastFiles lastfiles;
94
95         /// number of files in the lastfiles list.
96         unsigned int num_lastfiles;
97
98         /** Used by the constructor to set the number of stored last files.
99             @param num the number of lastfiles to set.
100         */
101         void setNumberOfLastFiles(unsigned int num);
102 };
103
104
105 class LastOpenedSection : SessionSection
106 {
107 public:
108         ///
109         typedef std::vector<support::FileName> LastOpened;
110
111 public:
112         ///
113         void read(std::istream & is);
114
115         ///
116         void write(std::ostream & os) const;
117
118         /// Return lastopened container (vector)
119         LastOpened const getfiles() const { return lastopened; }
120
121         /** add file to lastopened file list
122             @param file filename to add
123         */
124         void add(support::FileName const & file);
125
126         /** clear lastopened file list
127          */
128         void clear();
129
130 private:
131         /// a list of lastopened files
132         LastOpened lastopened;
133 };
134
135
136 class LastFilePosSection : SessionSection
137 {
138 public:
139         ///
140         struct FilePos {
141                 FilePos() : pit(0), pos(0) {}
142                 pit_type pit;
143                 pos_type pos;
144         };
145
146         ///
147         typedef std::map<support::FileName, FilePos> FilePosMap;
148
149 public:
150         ///
151         LastFilePosSection() : num_lastfilepos(100) {}
152
153         ///
154         void read(std::istream & is);
155
156         ///
157         void write(std::ostream & os) const;
158
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.
162         */
163         void save(support::FileName const & fname, FilePos const & pos);
164
165         /** load saved cursor position from the fname entry in the filepos map
166             @param fname file entry for which to load position information
167         */
168         FilePos load(support::FileName const & fname) const;
169
170 private:
171         /// default number of lastfilepos to save */
172         unsigned int const num_lastfilepos;
173
174
175         /// a map of file positions
176         FilePosMap lastfilepos;
177 };
178
179
180 class BookmarksSection : SessionSection
181 {
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! :-)
191         ///
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;
205                 ///
206                 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
207                 ///
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;
216                 }
217         };
218
219         ///
220         typedef std::vector<Bookmark> BookmarkList;
221
222 public:
223         /// constructor, set max_bookmarks
224         /// allow 9 regular bookmarks, bookmark 0 is temporary
225         BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
226
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);
230
231         /// return bookmark 0-9, bookmark 0 is the temporary bookmark
232         Bookmark const & bookmark(unsigned int i) const;
233
234         /// does the given bookmark have a saved position ?
235         bool isValid(unsigned int i) const;
236
237         /// is there at least one bookmark that has a saved position ?
238         bool hasValid() const;
239
240         ///
241         unsigned int size() const { return max_bookmarks; }
242
243         /// clear all bookmarks
244         void clear();
245
246         ///
247         void read(std::istream & is);
248
249         ///
250         void write(std::ostream & os) const;
251
252         /** return bookmark list. Non-const container is used since
253                 bookmarks will be cleaned after use.
254         */
255         BookmarkList & load() { return bookmarks; }
256
257 private:
258
259         /// a list of bookmarks
260         BookmarkList bookmarks;
261
262         ///
263         unsigned int const max_bookmarks;
264 };
265
266
267 class LastCommandsSection : SessionSection
268 {
269 public:
270         ///
271         typedef std::vector<std::string> LastCommands;
272
273 public:
274         ///
275         LastCommandsSection(unsigned int num);
276         ///
277         void read(std::istream & is);
278
279         ///
280         void write(std::ostream & os) const;
281
282         /// Return lastcommands container (vector)
283         LastCommands const getcommands() const { return lastcommands; }
284
285         /** add command to lastcommands list
286             @param command command to add
287         */
288         void add(std::string const & command);
289
290         /** clear lastcommands list
291          */
292         void clear();
293
294 private:
295         /// number of commands in the lastcommands list.
296         unsigned int num_lastcommands;
297
298         /** Used by the constructor to set the number of stored last commands.
299             @param num the number of lastcommands to set.
300         */
301         void setNumberOfLastCommands(unsigned int num);
302
303         /// a list of lastopened commands
304         LastCommands lastcommands;
305
306         /// Default number of lastcommands.
307         unsigned int const default_num_last_commands;
308
309         /// Max number of lastcommands.
310         unsigned int const absolute_max_last_commands;
311 };
312
313
314 class Session
315 {
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;
322         ///
323         LastFilesSection & lastFiles() { return last_files; }
324         ///
325         LastFilesSection const & lastFiles() const { return last_files; }
326         ///
327         LastOpenedSection & lastOpened() { return last_opened; }
328         ///
329         LastOpenedSection const & lastOpened() const { return last_opened; }
330         ///
331         LastFilePosSection & lastFilePos() { return last_file_pos; }
332         ///
333         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
334         ///
335         BookmarksSection & bookmarks() { return bookmarks_; }
336         ///
337         BookmarksSection const & bookmarks() const { return bookmarks_; }
338         ///
339         LastCommandsSection & lastCommands() { return last_commands; }
340         ///
341         LastCommandsSection const & lastCommands() const { return last_commands; }
342
343 private:
344         friend class LyX;
345         /// uncopiable
346         Session(Session const &);
347         void operator=(Session const &);
348
349         /// file to save session, determined in the constructor.
350         support::FileName session_file;
351
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.
356         */
357         void readFile();
358
359         ///
360         LastFilesSection last_files;
361         ///
362         LastOpenedSection last_opened;
363         ///
364         LastFilePosSection last_file_pos;
365         ///
366         BookmarksSection bookmarks_;
367         ///
368         LastCommandsSection last_commands;
369 };
370
371 /// This is a singleton class. Get the instance.
372 /// Implemented in LyX.cpp.
373 Session & theSession();
374
375 } // lyx
376
377 #endif