]> git.lyx.org Git - lyx.git/blob - src/Session.h
Avoid full metrics computation with Update:FitCursor
[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 <list>
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) override;
68
69         ///
70         void write(std::ostream & os) const override;
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) override;
123
124         ///
125         void write(std::ostream & os) const override;
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                 support::FileName file;
152                 pit_type pit;
153                 pos_type pos;
154         };
155
156         ///
157         typedef std::list<FilePos> FilePosList;
158
159 public:
160         ///
161         LastFilePosSection() : num_lastfilepos(100) {}
162
163         ///
164         void read(std::istream & is) override;
165
166         ///
167         void write(std::ostream & os) const override;
168
169         /** add cursor position to the fname entry in the filepos list
170             @param pos file name and position of the cursor when the BufferView is closed.
171         */
172         void save(FilePos const & pos);
173
174         /** load saved cursor position from the fname entry in the filepos list
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 list of file positions
185         FilePosList 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 saved 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         ///
233         BookmarksSection() : bookmarks(max_bookmarks + 1) {}
234
235         /// Save the current position as bookmark
236         void save(support::FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
237                 int top_id, pos_type top_pos, unsigned int idx);
238
239         /// return bookmark 0-9, bookmark 0 is the temporary bookmark
240         Bookmark const & bookmark(unsigned int i) const;
241
242         /// does the given bookmark have a saved position ?
243         bool isValid(unsigned int i) const;
244
245         /// is there at least one bookmark that has a saved position ?
246         bool hasValid() const;
247
248         ///
249         unsigned int size() const { return bookmarks.size(); }
250
251         /// clear all bookmarks
252         void clear();
253
254         ///
255         void read(std::istream & is) override;
256
257         ///
258         void write(std::ostream & os) const override;
259
260         /** return bookmark list. Non-const container is used since
261                 bookmarks will be cleaned after use.
262         */
263         BookmarkList & load() { return bookmarks; }
264
265         ///
266         typedef std::vector<std::pair<unsigned int, pos_type>> BookmarkPosList;
267
268         /// return a list of bookmarks and position for this paragraph
269         BookmarkPosList bookmarksInPar(support::FileName const & fn, int par_id) const;
270
271         /* An insertion/deletion in paragraph \c par_id of buffer \c fn
272          * lead to an offset \c offset after position \c pos. Update
273          * bookmarks accordingly.
274          */
275         void adjustPosAfterPos(support::FileName const & fn,
276                                int const par_id, pos_type pos, int offset);
277
278 private:
279
280         /// allow 9 regular bookmarks, bookmark 0 is temporary
281         unsigned int const max_bookmarks = 9;
282
283         /// a list of bookmarks
284         BookmarkList bookmarks;
285
286 };
287
288
289 class LastCommandsSection : SessionSection
290 {
291 public:
292         ///
293         typedef std::vector<std::string> LastCommands;
294
295 public:
296         ///
297         LastCommandsSection(unsigned int num);
298         ///
299         void read(std::istream & is) override;
300
301         ///
302         void write(std::ostream & os) const override;
303
304         /// Return lastcommands container (vector)
305         LastCommands const & getcommands() const { return lastcommands; }
306
307         /** add command to lastcommands list
308             @param command command to add
309         */
310         void add(std::string const & command);
311
312         /** clear lastcommands list
313          */
314         void clear();
315
316 private:
317         /// number of commands in the lastcommands list.
318         unsigned int num_lastcommands;
319
320         /** Used by the constructor to set the number of stored last commands.
321             @param num the number of lastcommands to set.
322         */
323         void setNumberOfLastCommands(unsigned int num);
324
325         /// a list of lastopened commands
326         LastCommands lastcommands;
327
328         /// Default number of lastcommands.
329         unsigned int const default_num_last_commands;
330
331         /// Max number of lastcommands.
332         unsigned int const absolute_max_last_commands;
333 };
334
335
336 class AuthFilesSection : SessionSection
337 {
338 public:
339         ///
340         explicit AuthFilesSection();
341
342         ///
343         void read(std::istream & is) override;
344
345         ///
346         void write(std::ostream & os) const override;
347
348         ///
349         bool find(std::string const & name) const;
350
351         ///
352         void insert(std::string const & name);
353
354 private:
355         /// set of document files authorized for external conversion
356         std::set<std::string> auth_files_;
357 };
358
359
360 class ShellEscapeSection : SessionSection
361 {
362 public:
363         ///
364         explicit ShellEscapeSection() {}
365
366         ///
367         void read(std::istream & is) override;
368
369         ///
370         void write(std::ostream & os) const override;
371
372         ///
373         bool find(std::string const & name) const;
374
375         ///
376         bool findAuth(std::string const & name) const;
377
378         ///
379         void insert(std::string const & name, bool auth = false);
380
381         ///
382         void remove(std::string const & name);
383
384 private:
385         /// set of document files authorized for external conversion
386         std::set<std::string> shellescape_files_;
387 };
388
389
390 class Session
391 {
392 public:
393         /// Read the session file.  @param num length of lastfiles
394         explicit Session(unsigned int num_last_files = 4,
395                 unsigned int num_last_commands = 30);
396         /// Write the session file.
397         void writeFile() const;
398         ///
399         LastFilesSection & lastFiles() { return last_files; }
400         ///
401         LastFilesSection const & lastFiles() const { return last_files; }
402         ///
403         LastOpenedSection & lastOpened() { return last_opened; }
404         ///
405         LastOpenedSection const & lastOpened() const { return last_opened; }
406         ///
407         LastFilePosSection & lastFilePos() { return last_file_pos; }
408         ///
409         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
410         ///
411         BookmarksSection & bookmarks() { return bookmarks_; }
412         ///
413         BookmarksSection const & bookmarks() const { return bookmarks_; }
414         ///
415         LastCommandsSection & lastCommands() { return last_commands; }
416         ///
417         LastCommandsSection const & lastCommands() const { return last_commands; }
418         ///
419         AuthFilesSection & authFiles() { return auth_files; }
420         ///
421         AuthFilesSection const & authFiles() const { return auth_files; }
422         ///
423         ShellEscapeSection & shellescapeFiles() { return shellescape_files; }
424         ///
425         ShellEscapeSection const & shellescapeFiles() const { return shellescape_files; }
426
427 private:
428         friend class LyX;
429         /// uncopiable
430         Session(Session const &);
431         void operator=(Session const &);
432
433         /// file to save session, determined in the constructor.
434         support::FileName session_file;
435
436         /** Read the session file.
437             Reads the #.lyx/session# at the beginning of the LyX session.
438             This will read the session file (usually #.lyx/session#).
439             @param file the file containing the session.
440         */
441         void readFile();
442
443         ///
444         LastFilesSection last_files;
445         ///
446         LastOpenedSection last_opened;
447         ///
448         LastFilePosSection last_file_pos;
449         ///
450         BookmarksSection bookmarks_;
451         ///
452         LastCommandsSection last_commands;
453         ///
454         AuthFilesSection auth_files;
455         ///
456         ShellEscapeSection shellescape_files;
457 };
458
459 /// This is a singleton class. Get the instance.
460 /// Implemented in LyX.cpp.
461 Session & theSession();
462
463 } // namespace lyx
464
465 #endif