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