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