]> git.lyx.org Git - lyx.git/blob - src/buffer.h
fix bug 2089: Touching Navigate menu crashes Lyx when a TOC inset is in a section...
[lyx.git] / src / buffer.h
1 // -*- C++ -*-
2 /**
3  * \file buffer.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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef BUFFER_H
13 #define BUFFER_H
14
15 #include "InsetList.h"
16
17 #include "dociterator.h"
18
19 #include "support/limited_stack.h"
20 #include "support/types.h"
21
22 #include <boost/scoped_ptr.hpp>
23 #include <boost/signal.hpp>
24
25 #include <iosfwd>
26 #include <map>
27 #include <utility>
28 #include <vector>
29
30
31 class BufferParams;
32 class ErrorItem;
33 class FuncRequest;
34 class InsetBase;
35 class InsetText;
36 class LyXFont;
37 class LyXLex;
38 class LyXRC;
39 class LyXText;
40 class LyXVC;
41 class LaTeXFeatures;
42 class Language;
43 class MacroData;
44 class OutputParams;
45 class ParagraphList;
46 class ParConstIterator;
47 class ParIterator;
48 class TeXErrors;
49 class TexRow;
50 class Undo;
51 class StableDocIterator;
52
53
54 /** The buffer object.
55  * This is the buffer object. It contains all the informations about
56  * a document loaded into LyX.
57  * The buffer object owns the LyXText (wrapped in an InsetText), which
58  * contains the individual paragraphs of the document.
59  *
60  *
61  * I am not sure if the class is complete or
62  * minimal, probably not.
63  * \author Lars Gullik Bjønnes
64   */
65 class Buffer {
66 public:
67         /// What type of log will \c getLogName() return?
68         enum LogType {
69                 latexlog, ///< LaTeX log
70                 buildlog  ///< Literate build log
71         };
72
73         /** Constructor
74             \param file
75             \param b  optional \c false by default
76         */
77         explicit Buffer(std::string const & file, bool b = false);
78
79         /// Destructor
80         ~Buffer();
81
82         /** High-level interface to buffer functionality.
83             This function parses a command string and executes it
84         */
85         bool dispatch(std::string const & command, bool * result = 0);
86
87         /// Maybe we know the function already by number...
88         bool dispatch(FuncRequest const & func, bool * result = 0);
89
90         /// Load the autosaved file.
91         void loadAutoSaveFile();
92
93         /// load a new file
94         bool readFile(std::string const & filename);
95
96         bool readFile(std::string const & filename, lyx::pit_type pit);
97
98         /// read the header, returns number of unknown tokens
99         int readHeader(LyXLex & lex);
100
101         /** Reads a file without header.
102             \param par if != 0 insert the file.
103             \return \c false if file is not completely read.
104         */
105         bool readDocument(LyXLex &);
106
107         ///
108         void insertStringAsLines(ParagraphList & plist,
109                 lyx::pit_type &, lyx::pos_type &,
110                 LyXFont const &, std::string const &, bool);
111         ///
112         ParIterator getParFromID(int id) const;
113         /// do we have a paragraph with this id?
114         bool hasParWithID(int id) const;
115
116         /// This signal is emitted when a parsing error shows up.
117         boost::signal<void(ErrorItem)> error;
118         /// This signal is emitted when some message shows up.
119         boost::signal<void(std::string)> message;
120         /// This signal is emitted when the buffer busy status change.
121         boost::signal<void(bool)> busy;
122         /// This signal is emitted when the buffer readonly status change.
123         boost::signal<void(bool)> readonly;
124         /// Update window titles of all users.
125         boost::signal<void()> updateTitles;
126         /// Reset autosave timers for all users.
127         boost::signal<void()> resetAutosaveTimers;
128         /// This signal is emitting if the buffer is being closed.
129         boost::signal<void()> closing;
130
131
132         /** Save file.
133             Takes care of auto-save files and backup file if requested.
134             Returns \c true if the save is successful, \c false otherwise.
135         */
136         bool save() const;
137
138         /// Write file. Returns \c false if unsuccesful.
139         bool writeFile(std::string const &) const;
140
141         ///
142         void writeFileAscii(std::string const &, OutputParams const &);
143         ///
144         void writeFileAscii(std::ostream &, OutputParams const &);
145         /// Just a wrapper for the method below, first creating the ofstream.
146         void makeLaTeXFile(std::string const & filename,
147                            std::string const & original_path,
148                            OutputParams const &,
149                            bool output_preamble = true,
150                            bool output_body = true);
151         ///
152         void makeLaTeXFile(std::ostream & os,
153                            std::string const & original_path,
154                            OutputParams const &,
155                            bool output_preamble = true,
156                            bool output_body = true);
157         ///
158         void makeLinuxDocFile(std::string const & filename,
159                               OutputParams const & runparams_in,
160                               bool only_body = false);
161         ///
162         void makeDocBookFile(std::string const & filename,
163                              OutputParams const & runparams_in,
164                              bool only_body = false);
165         /// returns the main language for the buffer (document)
166         Language const * getLanguage() const;
167         /// get l10n translated to the buffers language
168         std::string const B_(std::string const & l10n) const;
169
170         ///
171         int runChktex();
172         /// return true if the main lyx file does not need saving
173         bool isClean() const;
174         ///
175         bool isBakClean() const;
176         ///
177         bool isDepClean(std::string const & name) const;
178
179         /// mark the main lyx file as not needing saving
180         void markClean() const;
181
182         ///
183         void markBakClean();
184
185         ///
186         void markDepClean(std::string const & name);
187
188         ///
189         void setUnnamed(bool flag = true);
190
191         ///
192         bool isUnnamed() const;
193
194         /// Mark this buffer as dirty.
195         void markDirty();
196
197         /// Returns the buffer's filename. It is always an absolute path.
198         std::string const & fileName() const;
199
200         /// Returns the the path where the buffer lives.
201         /// It is always an absolute path.
202         std::string const & filePath() const;
203
204         /** A transformed version of the file name, adequate for LaTeX.
205             \param no_path optional if \c true then the path is stripped.
206         */
207         std::string const getLatexName(bool no_path = true) const;
208
209         /// Get the name and type of the log.
210         std::pair<LogType, std::string> const getLogName() const;
211
212         /// Change name of buffer. Updates "read-only" flag.
213         void setFileName(std::string const & newfile);
214
215         /// Name of the document's parent
216         void setParentName(std::string const &);
217
218         /** Get the document's master (or \c this if this is not a
219             child document)
220          */
221         Buffer const * getMasterBuffer() const;
222
223         /// Is buffer read-only?
224         bool isReadonly() const;
225
226         /// Set buffer read-only flag
227         void setReadonly(bool flag = true);
228
229         /// returns \c true if the buffer contains a LaTeX document
230         bool isLatex() const;
231         /// returns \c true if the buffer contains a LinuxDoc document
232         bool isLinuxDoc() const;
233         /// returns \c true if the buffer contains a DocBook document
234         bool isDocBook() const;
235         /** returns \c true if the buffer contains either a LinuxDoc
236             or DocBook document */
237         bool isSGML() const;
238         /// returns \c true if the buffer contains a Wed document
239         bool isLiterate() const;
240
241         /** Validate a buffer for LaTeX.
242             This validates the buffer, and returns a struct for use by
243             #makeLaTeX# and others. Its main use is to figure out what
244             commands and packages need to be included in the LaTeX file.
245             It (should) also check that the needed constructs are there
246             (i.e. that the \refs points to coresponding \labels). It
247             should perhaps inset "error" insets to help the user correct
248             obvious mistakes.
249         */
250         void validate(LaTeXFeatures &) const;
251
252         /// return all bibkeys from buffer and its childs
253         void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
254         ///
255         void getLabelList(std::vector<std::string> &) const;
256
257         ///
258         void changeLanguage(Language const * from, Language const * to);
259
260         ///
261         void updateDocLang(Language const * nlang);
262
263         ///
264         bool isMultiLingual() const;
265
266         /// Does this mean that this is buffer local?
267         limited_stack<Undo> & undostack();
268         limited_stack<Undo> const & undostack() const;
269
270         /// Does this mean that this is buffer local?
271         limited_stack<Undo> & redostack();
272         limited_stack<Undo> const & redostack() const;
273
274         ///
275         BufferParams & params();
276         BufferParams const & params() const;
277
278         /** The list of paragraphs.
279             This is a linked list of paragraph, this list holds the
280             whole contents of the document.
281          */
282         ParagraphList & paragraphs();
283         ParagraphList const & paragraphs() const;
284
285         /// LyX version control object.
286         LyXVC & lyxvc();
287         LyXVC const & lyxvc() const;
288
289         /// Where to put temporary files.
290         std::string const & temppath() const;
291
292         /// Used when typesetting to place errorboxes.
293         TexRow & texrow();
294         TexRow const & texrow() const;
295
296         ///
297         ParIterator par_iterator_begin();
298         ///
299         ParConstIterator par_iterator_begin() const;
300         ///
301         ParIterator par_iterator_end();
302         ///
303         ParConstIterator par_iterator_end() const;
304
305         /** \returns true only when the file is fully loaded.
306          *  Used to prevent the premature generation of previews
307          *  and by the citation inset.
308          */
309         bool fully_loaded() const;
310         /// Set by buffer_funcs' newFile.
311         void fully_loaded(bool);
312
313         /// Our main text (inside the top InsetText)
314         LyXText & text() const;
315
316         /// Our top InsetText!
317         InsetBase & inset() const;
318
319         //
320         // Macro handling
321         //
322         ///
323         void buildMacros();
324         ///
325         bool hasMacro(std::string const & name) const;
326         ///
327         MacroData const & getMacro(std::string const & name) const;
328         ///
329         void insertMacro(std::string const & name, MacroData const & data);
330         ///
331         void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
332         ///
333         StableDocIterator getCursor() const { return cursor_; }
334         ///
335         StableDocIterator getAnchor() const { return anchor_; }
336         ///
337         void changeRefsIfUnique(std::string const & from, std::string const & to);
338
339 private:
340         /** Inserts a file into a document
341             \param par if != 0 insert the file.
342             \return \c false if method fails.
343         */
344         bool readFile(LyXLex &, std::string const & filename,
345                       lyx::pit_type pit);
346
347         bool do_writeFile(std::ostream & ofs) const;
348
349         /// Use the Pimpl idiom to hide the internals.
350         class Impl;
351         /// The pointer never changes although *pimpl_'s contents may.
352         boost::scoped_ptr<Impl> const pimpl_;
353
354         /// Save the cursor Position on Buffer switch
355         /// this would not be needed if every Buffer would have
356         /// it's BufferView, this should be FIXED in future.
357         StableDocIterator cursor_;
358         StableDocIterator anchor_;
359 };
360
361 #endif