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
10 * Full author contact details are available in file CREDITS.
16 #include "support/FileName.h"
17 #include "support/types.h"
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)
28 5. general purpose session info in the form of key/value pairs
29 6. the latest commands entered in the command buffer (lastcommands)
33 /* base class for all sections in the session file
41 virtual ~SessionSection() {}
43 /// read section from std::istream
44 virtual void read(std::istream & is) = 0;
46 /// write to std::ostream
47 virtual void write(std::ostream & os) const = 0;
51 SessionSection(SessionSection const &);
52 void operator=(SessionSection const &);
56 class LastFilesSection : SessionSection
60 typedef std::vector<support::FileName> LastFiles;
64 explicit LastFilesSection(unsigned int num = 4);
67 void read(std::istream & is) override;
70 void write(std::ostream & os) const override;
72 /// Return lastfiles container (vector)
73 LastFiles const lastFiles() const { return lastfiles; }
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.
82 void add(support::FileName const & file);
85 /// Default number of lastfiles.
86 unsigned int const default_num_last_files;
88 /// Max number of lastfiles.
89 unsigned int const absolute_max_last_files;
91 /// a list of lastfiles
94 /// number of files in the lastfiles list.
95 unsigned int num_lastfiles;
97 /** Used by the constructor to set the number of stored last files.
98 @param num the number of lastfiles to set.
100 void setNumberOfLastFiles(unsigned int num);
104 class LastOpenedSection : SessionSection
108 struct LastOpenedFile {
109 LastOpenedFile() : file_name(), active(false) {}
111 LastOpenedFile(support::FileName file_name_, bool active_)
112 : file_name(file_name_), active(active_) {}
114 support::FileName file_name;
118 typedef std::vector<LastOpenedFile> LastOpened;
122 void read(std::istream & is) override;
125 void write(std::ostream & os) const override;
127 /// Return lastopened container (vector)
128 LastOpened const getfiles() const { return lastopened; }
130 /** add file to lastopened file list
131 @param file filename to add
133 void add(support::FileName const & file, bool active = false);
135 /** clear lastopened file list
140 /// a list of lastopened files
141 LastOpened lastopened;
145 class LastFilePosSection : SessionSection
150 FilePos() : pit(0), pos(0) {}
151 support::FileName file;
157 typedef std::list<FilePos> FilePosList;
161 LastFilePosSection() : num_lastfilepos(100) {}
164 void read(std::istream & is) override;
167 void write(std::ostream & os) const override;
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.
172 void save(FilePos const & pos);
174 /** load saved cursor position from the fname entry in the filepos list
175 @param fname file entry for which to load position information
177 FilePos load(support::FileName const & fname) const;
180 /// default number of lastfilepos to save */
181 unsigned int const num_lastfilepos;
184 /// a list of file positions
185 FilePosList lastfilepos;
189 class BookmarksSection : SessionSection
192 /// A bookmark is composed of three parts
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! :-)
201 /// TODO: bottom level pit and pos will be replaced by StableDocIterator
205 support::FileName filename;
206 /// Bottom level cursor pit, will be saved/restored by .lyx/session
208 /// Bottom level cursor position, will be saved/restore by .lyx/session
210 /// Top level cursor id, used to lcoate bookmarks for opened files
212 /// Top level cursor position within a paragraph
215 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
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) {
229 typedef std::vector<Bookmark> BookmarkList;
233 BookmarksSection() : bookmarks(max_bookmarks + 1) {}
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);
239 /// return bookmark 0-9, bookmark 0 is the temporary bookmark
240 Bookmark const & bookmark(unsigned int i) const;
242 /// does the given bookmark have a saved position ?
243 bool isValid(unsigned int i) const;
245 /// is there at least one bookmark that has a saved position ?
246 bool hasValid() const;
249 unsigned int size() const { return bookmarks.size(); }
251 /// clear all bookmarks
255 void read(std::istream & is) override;
258 void write(std::ostream & os) const override;
260 /** return bookmark list. Non-const container is used since
261 bookmarks will be cleaned after use.
263 BookmarkList & load() { return bookmarks; }
266 typedef std::vector<std::pair<unsigned int, pos_type>> BookmarkPosList;
268 /// return a list of bookmarks and position for this paragraph
269 BookmarkPosList bookmarksInPar(support::FileName const & fn, int par_id) const;
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.
275 void adjustPosAfterPos(support::FileName const & fn,
276 int const par_id, pos_type pos, int offset);
280 /// allow 9 regular bookmarks, bookmark 0 is temporary
281 unsigned int const max_bookmarks = 9;
283 /// a list of bookmarks
284 BookmarkList bookmarks;
289 class LastCommandsSection : SessionSection
293 typedef std::vector<std::string> LastCommands;
297 LastCommandsSection(unsigned int num);
299 void read(std::istream & is) override;
302 void write(std::ostream & os) const override;
304 /// Return lastcommands container (vector)
305 LastCommands const getcommands() const { return lastcommands; }
307 /** add command to lastcommands list
308 @param command command to add
310 void add(std::string const & command);
312 /** clear lastcommands list
317 /// number of commands in the lastcommands list.
318 unsigned int num_lastcommands;
320 /** Used by the constructor to set the number of stored last commands.
321 @param num the number of lastcommands to set.
323 void setNumberOfLastCommands(unsigned int num);
325 /// a list of lastopened commands
326 LastCommands lastcommands;
328 /// Default number of lastcommands.
329 unsigned int const default_num_last_commands;
331 /// Max number of lastcommands.
332 unsigned int const absolute_max_last_commands;
336 class AuthFilesSection : SessionSection
340 explicit AuthFilesSection();
343 void read(std::istream & is) override;
346 void write(std::ostream & os) const override;
349 bool find(std::string const & name) const;
352 void insert(std::string const & name);
355 /// set of document files authorized for external conversion
356 std::set<std::string> auth_files_;
360 class ShellEscapeSection : SessionSection
364 explicit ShellEscapeSection() {}
367 void read(std::istream & is) override;
370 void write(std::ostream & os) const override;
373 bool find(std::string const & name) const;
376 bool findAuth(std::string const & name) const;
379 void insert(std::string const & name, bool auth = false);
382 void remove(std::string const & name);
385 /// set of document files authorized for external conversion
386 std::set<std::string> shellescape_files_;
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;
399 LastFilesSection & lastFiles() { return last_files; }
401 LastFilesSection const & lastFiles() const { return last_files; }
403 LastOpenedSection & lastOpened() { return last_opened; }
405 LastOpenedSection const & lastOpened() const { return last_opened; }
407 LastFilePosSection & lastFilePos() { return last_file_pos; }
409 LastFilePosSection const & lastFilePos() const { return last_file_pos; }
411 BookmarksSection & bookmarks() { return bookmarks_; }
413 BookmarksSection const & bookmarks() const { return bookmarks_; }
415 LastCommandsSection & lastCommands() { return last_commands; }
417 LastCommandsSection const & lastCommands() const { return last_commands; }
419 AuthFilesSection & authFiles() { return auth_files; }
421 AuthFilesSection const & authFiles() const { return auth_files; }
423 ShellEscapeSection & shellescapeFiles() { return shellescape_files; }
425 ShellEscapeSection const & shellescapeFiles() const { return shellescape_files; }
430 Session(Session const &);
431 void operator=(Session const &);
433 /// file to save session, determined in the constructor.
434 support::FileName session_file;
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.
444 LastFilesSection last_files;
446 LastOpenedSection last_opened;
448 LastFilePosSection last_file_pos;
450 BookmarksSection bookmarks_;
452 LastCommandsSection last_commands;
454 AuthFilesSection auth_files;
456 ShellEscapeSection shellescape_files;
459 /// This is a singleton class. Get the instance.
460 /// Implemented in LyX.cpp.
461 Session & theSession();