]> git.lyx.org Git - lyx.git/blob - src/Session.h
Generate the magic label always. We'll need it other times, when we do
[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         struct LastOpenedFile {
110                 LastOpenedFile() : file_name(), active(false) {}
111
112                 LastOpenedFile(support::FileName file_name_, bool active_)
113                         : file_name(file_name_), active(active_) {}
114
115                 support::FileName file_name;
116                 bool active;
117         };
118         ///
119         typedef std::vector<LastOpenedFile> LastOpened;
120
121 public:
122         ///
123         void read(std::istream & is);
124
125         ///
126         void write(std::ostream & os) const;
127
128         /// Return lastopened container (vector)
129         LastOpened const getfiles() const { return lastopened; }
130
131         /** add file to lastopened file list
132             @param file filename to add
133         */
134         void add(support::FileName const & file, bool active = false);
135
136         /** clear lastopened file list
137          */
138         void clear();
139
140 private:
141         /// a list of lastopened files
142         LastOpened lastopened;
143 };
144
145
146 class LastFilePosSection : SessionSection
147 {
148 public:
149         ///
150         struct FilePos {
151                 FilePos() : pit(0), pos(0) {}
152                 pit_type pit;
153                 pos_type pos;
154         };
155
156         ///
157         typedef std::map<support::FileName, FilePos> FilePosMap;
158
159 public:
160         ///
161         LastFilePosSection() : num_lastfilepos(100) {}
162
163         ///
164         void read(std::istream & is);
165
166         ///
167         void write(std::ostream & os) const;
168
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.
172         */
173         void save(support::FileName const & fname, FilePos const & pos);
174
175         /** load saved cursor position from the fname entry in the filepos map
176             @param fname file entry for which to load position information
177         */
178         FilePos load(support::FileName const & fname) const;
179
180 private:
181         /// default number of lastfilepos to save */
182         unsigned int const num_lastfilepos;
183
184
185         /// a map of file positions
186         FilePosMap lastfilepos;
187 };
188
189
190 class BookmarksSection : SessionSection
191 {
192 public:
193         /// A bookmark is composed of three parts
194         /// 1. filename
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! :-)
201         ///
202         /// TODO: bottom level pit and pos will be replaced by StableDocIterator
203         class Bookmark {
204         public:
205                 /// Filename
206                 support::FileName filename;
207                 /// Bottom level cursor pit, will be saved/restored by .lyx/session
208                 pit_type bottom_pit;
209                 /// Bottom level cursor position, will be saved/restore by .lyx/session
210                 pos_type bottom_pos;
211                 /// Top level cursor id, used to lcoate bookmarks for opened files
212                 int top_id;
213                 /// Top level cursor position within a paragraph
214                 pos_type top_pos;
215                 ///
216                 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
217                 ///
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) {
223                         bottom_pit = pit;
224                         bottom_pos = pos;
225                         top_id = id;
226                 }
227         };
228
229         ///
230         typedef std::vector<Bookmark> BookmarkList;
231
232 public:
233         /// constructor, set max_bookmarks
234         /// allow 9 regular bookmarks, bookmark 0 is temporary
235         BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
236
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);
240
241         /// return bookmark 0-9, bookmark 0 is the temporary bookmark
242         Bookmark const & bookmark(unsigned int i) const;
243
244         /// does the given bookmark have a saved position ?
245         bool isValid(unsigned int i) const;
246
247         /// is there at least one bookmark that has a saved position ?
248         bool hasValid() const;
249
250         ///
251         unsigned int size() const { return max_bookmarks; }
252
253         /// clear all bookmarks
254         void clear();
255
256         ///
257         void read(std::istream & is);
258
259         ///
260         void write(std::ostream & os) const;
261
262         /** return bookmark list. Non-const container is used since
263                 bookmarks will be cleaned after use.
264         */
265         BookmarkList & load() { return bookmarks; }
266
267 private:
268
269         /// a list of bookmarks
270         BookmarkList bookmarks;
271
272         ///
273         unsigned int const max_bookmarks;
274 };
275
276
277 class LastCommandsSection : SessionSection
278 {
279 public:
280         ///
281         typedef std::vector<std::string> LastCommands;
282
283 public:
284         ///
285         LastCommandsSection(unsigned int num);
286         ///
287         void read(std::istream & is);
288
289         ///
290         void write(std::ostream & os) const;
291
292         /// Return lastcommands container (vector)
293         LastCommands const getcommands() const { return lastcommands; }
294
295         /** add command to lastcommands list
296             @param command command to add
297         */
298         void add(std::string const & command);
299
300         /** clear lastcommands list
301          */
302         void clear();
303
304 private:
305         /// number of commands in the lastcommands list.
306         unsigned int num_lastcommands;
307
308         /** Used by the constructor to set the number of stored last commands.
309             @param num the number of lastcommands to set.
310         */
311         void setNumberOfLastCommands(unsigned int num);
312
313         /// a list of lastopened commands
314         LastCommands lastcommands;
315
316         /// Default number of lastcommands.
317         unsigned int const default_num_last_commands;
318
319         /// Max number of lastcommands.
320         unsigned int const absolute_max_last_commands;
321 };
322
323
324 class Session
325 {
326 public:
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;
332         ///
333         LastFilesSection & lastFiles() { return last_files; }
334         ///
335         LastFilesSection const & lastFiles() const { return last_files; }
336         ///
337         LastOpenedSection & lastOpened() { return last_opened; }
338         ///
339         LastOpenedSection const & lastOpened() const { return last_opened; }
340         ///
341         LastFilePosSection & lastFilePos() { return last_file_pos; }
342         ///
343         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
344         ///
345         BookmarksSection & bookmarks() { return bookmarks_; }
346         ///
347         BookmarksSection const & bookmarks() const { return bookmarks_; }
348         ///
349         LastCommandsSection & lastCommands() { return last_commands; }
350         ///
351         LastCommandsSection const & lastCommands() const { return last_commands; }
352
353 private:
354         friend class LyX;
355         /// uncopiable
356         Session(Session const &);
357         void operator=(Session const &);
358
359         /// file to save session, determined in the constructor.
360         support::FileName session_file;
361
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.
366         */
367         void readFile();
368
369         ///
370         LastFilesSection last_files;
371         ///
372         LastOpenedSection last_opened;
373         ///
374         LastFilePosSection last_file_pos;
375         ///
376         BookmarksSection bookmarks_;
377         ///
378         LastCommandsSection last_commands;
379 };
380
381 /// This is a singleton class. Get the instance.
382 /// Implemented in LyX.cpp.
383 Session & theSession();
384
385 } // lyx
386
387 #endif