]> git.lyx.org Git - lyx.git/blob - src/buffer.h
This commit creates a error_lists map member inside the Buffer class.
[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
24 #include <boost/scoped_ptr.hpp>
25 #include <boost/signal.hpp>
26
27 #include <iosfwd>
28 #include <string>
29 #include <map>
30 #include <utility>
31 #include <vector>
32
33
34 class BufferParams;
35 class ErrorItem;
36 class FuncRequest;
37 class InsetBase;
38 class InsetText;
39 class LyXFont;
40 class LyXLex;
41 class LyXRC;
42 class LyXText;
43 class LyXVC;
44 class LaTeXFeatures;
45 class Language;
46 class MacroData;
47 class OutputParams;
48 class ParConstIterator;
49 class ParIterator;
50 class TeXErrors;
51 class TexRow;
52 class Undo;
53 class StableDocIterator;
54
55
56 /** The buffer object.
57  * This is the buffer object. It contains all the informations about
58  * a document loaded into LyX.
59  * The buffer object owns the LyXText (wrapped in an InsetText), which
60  * contains the individual paragraphs of the document.
61  *
62  *
63  * I am not sure if the class is complete or
64  * minimal, probably not.
65  * \author Lars Gullik Bjønnes
66   */
67 class Buffer {
68 public:
69         /// What type of log will \c getLogName() return?
70         enum LogType {
71                 latexlog, ///< LaTeX log
72                 buildlog  ///< Literate build log
73         };
74
75         /** Constructor
76             \param file
77             \param b  optional \c false by default
78         */
79         explicit Buffer(std::string const & file, bool b = false);
80
81         /// Destructor
82         ~Buffer();
83
84         /** High-level interface to buffer functionality.
85             This function parses a command string and executes it
86         */
87         bool dispatch(std::string const & command, bool * result = 0);
88
89         /// Maybe we know the function already by number...
90         bool dispatch(FuncRequest const & func, bool * result = 0);
91
92         /// Load the autosaved file.
93         void loadAutoSaveFile();
94
95         /// load a new file
96         bool readFile(std::string const & filename);
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 some parsing error shows up.
117         boost::signal<void(std::string)> errors;
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 writeLaTeXSource(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 writeLinuxDocSource(std::ostream & os, std::string const & filename,
163                               OutputParams const & runparams_in,
164                               bool only_body = false);
165         ///
166         void makeDocBookFile(std::string const & filename,
167                              OutputParams const & runparams_in,
168                              bool only_body = false);
169         ///
170         void writeDocBookSource(std::ostream & os, std::string const & filename,
171                              OutputParams const & runparams_in,
172                              bool only_body = false);
173         /// returns the main language for the buffer (document)
174         Language const * getLanguage() const;
175         /// get l10n translated to the buffers language
176         std::string const B_(std::string const & l10n) const;
177
178         ///
179         int runChktex();
180         /// return true if the main lyx file does not need saving
181         bool isClean() const;
182         ///
183         bool isBakClean() const;
184         ///
185         bool isDepClean(std::string const & name) const;
186
187         /// mark the main lyx file as not needing saving
188         void markClean() const;
189
190         ///
191         void markBakClean();
192
193         ///
194         void markDepClean(std::string const & name);
195
196         ///
197         void setUnnamed(bool flag = true);
198
199         ///
200         bool isUnnamed() const;
201
202         /// Mark this buffer as dirty.
203         void markDirty();
204
205         /// Returns the buffer's filename. It is always an absolute path.
206         std::string const & fileName() const;
207
208         /// Returns the the path where the buffer lives.
209         /// It is always an absolute path.
210         std::string const & filePath() const;
211
212         /** A transformed version of the file name, adequate for LaTeX.
213             \param no_path optional if \c true then the path is stripped.
214         */
215         std::string const getLatexName(bool no_path = true) const;
216
217         /// Get the name and type of the log.
218         std::pair<LogType, std::string> const getLogName() const;
219
220         /// Change name of buffer. Updates "read-only" flag.
221         void setFileName(std::string const & newfile);
222
223         /// Name of the document's parent
224         void setParentName(std::string const &);
225
226         /** Get the document's master (or \c this if this is not a
227             child document)
228          */
229         Buffer const * getMasterBuffer() const;
230         /** Get the document's master (or \c this if this is not a
231             child document)
232          */
233         Buffer * getMasterBuffer();
234
235         /// Is buffer read-only?
236         bool isReadonly() const;
237
238         /// Set buffer read-only flag
239         void setReadonly(bool flag = true);
240
241         /// returns \c true if the buffer contains a LaTeX document
242         bool isLatex() const;
243         /// returns \c true if the buffer contains a LinuxDoc document
244         bool isLinuxDoc() const;
245         /// returns \c true if the buffer contains a DocBook document
246         bool isDocBook() const;
247         /** returns \c true if the buffer contains either a LinuxDoc
248             or DocBook document */
249         bool isSGML() const;
250         /// returns \c true if the buffer contains a Wed document
251         bool isLiterate() const;
252
253         /** Validate a buffer for LaTeX.
254             This validates the buffer, and returns a struct for use by
255             #makeLaTeX# and others. Its main use is to figure out what
256             commands and packages need to be included in the LaTeX file.
257             It (should) also check that the needed constructs are there
258             (i.e. that the \refs points to coresponding \labels). It
259             should perhaps inset "error" insets to help the user correct
260             obvious mistakes.
261         */
262         void validate(LaTeXFeatures &) const;
263
264         /// return all bibkeys from buffer and its childs
265         void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
266         /// Update the cache with all bibfiles in use (including bibfiles
267         /// of loaded child documents).
268         void updateBibfilesCache();
269         /// Return the cache with all bibfiles in use (including bibfiles
270         /// of loaded child documents).
271         std::vector<std::string> const & getBibfilesCache() const;
272         ///
273         void getLabelList(std::vector<std::string> &) const;
274
275         ///
276         void changeLanguage(Language const * from, Language const * to);
277
278         ///
279         void updateDocLang(Language const * nlang);
280
281         ///
282         bool isMultiLingual() const;
283
284         /// Does this mean that this is buffer local?
285         limited_stack<Undo> & undostack();
286         limited_stack<Undo> const & undostack() const;
287
288         /// Does this mean that this is buffer local?
289         limited_stack<Undo> & redostack();
290         limited_stack<Undo> const & redostack() const;
291
292         ///
293         BufferParams & params();
294         BufferParams const & params() const;
295
296         /** The list of paragraphs.
297             This is a linked list of paragraph, this list holds the
298             whole contents of the document.
299          */
300         ParagraphList & paragraphs();
301         ParagraphList const & paragraphs() const;
302
303         /// LyX version control object.
304         LyXVC & lyxvc();
305         LyXVC const & lyxvc() const;
306
307         /// Where to put temporary files.
308         std::string const & temppath() const;
309
310         /// Used when typesetting to place errorboxes.
311         TexRow & texrow();
312         TexRow const & texrow() const;
313
314         ///
315         ParIterator par_iterator_begin();
316         ///
317         ParConstIterator par_iterator_begin() const;
318         ///
319         ParIterator par_iterator_end();
320         ///
321         ParConstIterator par_iterator_end() const;
322
323         /** \returns true only when the file is fully loaded.
324          *  Used to prevent the premature generation of previews
325          *  and by the citation inset.
326          */
327         bool fully_loaded() const;
328         /// Set by buffer_funcs' newFile.
329         void fully_loaded(bool);
330
331         /// Our main text (inside the top InsetText)
332         LyXText & text() const;
333
334         /// Our top InsetText!
335         InsetBase & inset() const;
336
337         //
338         // Macro handling
339         //
340         ///
341         void buildMacros();
342         ///
343         bool hasMacro(std::string const & name) const;
344         ///
345         MacroData const & getMacro(std::string const & name) const;
346         ///
347         void insertMacro(std::string const & name, MacroData const & data);
348         ///
349         void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
350         ///
351         StableDocIterator getCursor() const { return cursor_; }
352         ///
353         StableDocIterator getAnchor() const { return anchor_; }
354         ///
355         void changeRefsIfUnique(std::string const & from, std::string const & to);
356         /// get source code (latex/docbook/linuxdoc) for some paragraphs, or all paragraphs
357         /// including preamble
358         void getSourceCode(std::ostream & os, lyx::pit_type par_begin, lyx::pit_type par_end, bool full_source);
359
360         /// errorLists_ accessors.
361         //@{
362         ErrorList const & errorList(std::string const & type) const;
363         ErrorList & errorList(std::string const & type);
364         //@}
365
366 private:
367         /** Inserts a file into a document
368             \return \c false if method fails.
369         */
370         bool readFile(LyXLex &, std::string const & filename);
371
372         bool do_writeFile(std::ostream & ofs) const;
373
374         /// Use the Pimpl idiom to hide the internals.
375         class Impl;
376         /// The pointer never changes although *pimpl_'s contents may.
377         boost::scoped_ptr<Impl> const pimpl_;
378
379         /// Save the cursor Position on Buffer switch
380         /// this would not be needed if every Buffer would have
381         /// it's BufferView, this should be FIXED in future.
382         StableDocIterator cursor_;
383         StableDocIterator anchor_;
384         /// A cache for the bibfiles (including bibfiles of loaded child
385         /// documents), needed for appropriate update of natbib labels.
386         std::vector<std::string> bibfilesCache_;
387
388         /// Container for all sort of Buffer dependant errors.
389         std::map<std::string, ErrorList> errorLists_;
390
391         /// Empty Error List
392         ErrorList const emptyErrorList_;
393 };
394
395 #endif