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