]> git.lyx.org Git - lyx.git/blob - src/buffer.h
use more unicode in math
[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 some parsing error shows up.
122         boost::signal<void(std::string)> errors;
123         /// This signal is emitted when some message shows up.
124         boost::signal<void(docstring)> message;
125         /// This signal is emitted when the buffer busy status change.
126         boost::signal<void(bool)> busy;
127         /// This signal is emitted when the buffer readonly status change.
128         boost::signal<void(bool)> readonly;
129         /// Update window titles of all users.
130         boost::signal<void()> updateTitles;
131         /// Reset autosave timers for all users.
132         boost::signal<void()> resetAutosaveTimers;
133         /// This signal is emitting if the buffer is being closed.
134         boost::signal<void()> closing;
135
136
137         /** Save file.
138             Takes care of auto-save files and backup file if requested.
139             Returns \c true if the save is successful, \c false otherwise.
140         */
141         bool save() const;
142
143         /// Write file. Returns \c false if unsuccesful.
144         bool writeFile(std::string const &) const;
145
146         /// Just a wrapper for the method below, first creating the ofstream.
147         void makeLaTeXFile(std::string const & filename,
148                            std::string const & original_path,
149                            OutputParams const &,
150                            bool output_preamble = true,
151                            bool output_body = true);
152         ///
153         void writeLaTeXSource(odocstream & os,
154                            std::string const & original_path,
155                            OutputParams const &,
156                            bool output_preamble = true,
157                            bool output_body = true);
158         ///
159         void makeDocBookFile(std::string const & filename,
160                              OutputParams const & runparams_in,
161                              bool only_body = false);
162         ///
163         void writeDocBookSource(odocstream & os, std::string const & filename,
164                              OutputParams const & runparams_in,
165                              bool only_body = false);
166         /// returns the main language for the buffer (document)
167         Language const * getLanguage() const;
168         /// get l10n translated to the buffers language
169         docstring const B_(std::string const & l10n) const;
170
171         ///
172         int runChktex();
173         /// return true if the main lyx file does not need saving
174         bool isClean() const;
175         ///
176         bool isBakClean() const;
177         ///
178         bool isDepClean(std::string const & name) const;
179
180         /// mark the main lyx file as not needing saving
181         void markClean() const;
182
183         ///
184         void markBakClean();
185
186         ///
187         void markDepClean(std::string const & name);
188
189         ///
190         void setUnnamed(bool flag = true);
191
192         ///
193         bool isUnnamed() const;
194
195         /// Mark this buffer as dirty.
196         void markDirty();
197
198         /// Returns the buffer's filename. It is always an absolute path.
199         std::string const & fileName() const;
200
201         /// Returns the the path where the buffer lives.
202         /// It is always an absolute path.
203         std::string const & filePath() const;
204
205         /** A transformed version of the file name, adequate for LaTeX.
206             \param no_path optional if \c true then the path is stripped.
207         */
208         std::string const getLatexName(bool no_path = true) const;
209
210         /// Get thee name and type of the log.
211         std::pair<LogType, std::string> const getLogName() const;
212
213         /// Change name of buffer. Updates "read-only" flag.
214         void setFileName(std::string const & newfile);
215
216         /// Name of the document's parent
217         void setParentName(std::string const &);
218
219         /** Get the document's master (or \c this if this is not a
220             child document)
221          */
222         Buffer const * getMasterBuffer() const;
223         /** Get the document's master (or \c this if this is not a
224             child document)
225          */
226         Buffer * getMasterBuffer();
227
228         /// Is buffer read-only?
229         bool isReadonly() const;
230
231         /// Set buffer read-only flag
232         void setReadonly(bool flag = true);
233
234         /// returns \c true if the buffer contains a LaTeX document
235         bool isLatex() const;
236         /// returns \c true if the buffer contains a DocBook document
237         bool isDocBook() 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         /// Update the cache with all bibfiles in use (including bibfiles
255         /// of loaded child documents).
256         void updateBibfilesCache();
257         /// Return the cache with all bibfiles in use (including bibfiles
258         /// of loaded child documents).
259         std::vector<std::string> const & getBibfilesCache() const;
260         ///
261         void getLabelList(std::vector<docstring> &) const;
262
263         ///
264         void changeLanguage(Language const * from, Language const * to);
265
266         ///
267         void updateDocLang(Language const * nlang);
268
269         ///
270         bool isMultiLingual() const;
271
272         /// Does this mean that this is buffer local?
273         limited_stack<Undo> & undostack();
274         limited_stack<Undo> const & undostack() const;
275
276         /// Does this mean that this is buffer local?
277         limited_stack<Undo> & redostack();
278         limited_stack<Undo> const & redostack() const;
279
280         ///
281         BufferParams & params();
282         BufferParams const & params() const;
283
284         /** The list of paragraphs.
285             This is a linked list of paragraph, this list holds the
286             whole contents of the document.
287          */
288         ParagraphList & paragraphs();
289         ParagraphList const & paragraphs() const;
290
291         /// LyX version control object.
292         LyXVC & lyxvc();
293         LyXVC const & lyxvc() const;
294
295         /// Where to put temporary files.
296         std::string const & temppath() const;
297
298         /// Used when typesetting to place errorboxes.
299         TexRow & texrow();
300         TexRow const & texrow() const;
301
302         ///
303         ParIterator par_iterator_begin();
304         ///
305         ParConstIterator par_iterator_begin() const;
306         ///
307         ParIterator par_iterator_end();
308         ///
309         ParConstIterator par_iterator_end() const;
310
311         /** \returns true only when the file is fully loaded.
312          *  Used to prevent the premature generation of previews
313          *  and by the citation inset.
314          */
315         bool fully_loaded() const;
316         /// Set by buffer_funcs' newFile.
317         void fully_loaded(bool);
318
319         /// Our main text (inside the top InsetText)
320         LyXText & text() const;
321
322         /// Our top InsetText!
323         InsetBase & inset() const;
324
325         //
326         // Macro handling
327         //
328         ///
329         void buildMacros();
330         ///
331         bool hasMacro(docstring const & name) const;
332         ///
333         MacroData const & getMacro(docstring const & name) const;
334         ///
335         void insertMacro(docstring const & name, MacroData const & data);
336
337         ///
338         void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
339         ///
340         StableDocIterator getCursor() const { return cursor_; }
341         ///
342         StableDocIterator getAnchor() const { return anchor_; }
343         ///
344         void changeRefsIfUnique(std::string const & from, std::string const & to, InsetBase::Code code);
345         /// get source code (latex/docbook) for some paragraphs, or all paragraphs
346         /// including preamble
347         void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
348
349         /// errorLists_ accessors.
350         //@{
351         ErrorList const & errorList(std::string const & type) const;
352         ErrorList & errorList(std::string const & type);
353         //@}
354
355 private:
356         /** Inserts a file into a document
357             \return \c false if method fails.
358         */
359         bool readFile(LyXLex &, std::string const & filename);
360
361         bool do_writeFile(std::ostream & ofs) const;
362
363         /// Use the Pimpl idiom to hide the internals.
364         class Impl;
365         /// The pointer never changes although *pimpl_'s contents may.
366         boost::scoped_ptr<Impl> const pimpl_;
367
368         /// Save the cursor Position on Buffer switch
369         /// this would not be needed if every Buffer would have
370         /// it's BufferView, this should be FIXED in future.
371         StableDocIterator cursor_;
372         StableDocIterator anchor_;
373         /// A cache for the bibfiles (including bibfiles of loaded child
374         /// documents), needed for appropriate update of natbib labels.
375         std::vector<std::string> bibfilesCache_;
376
377         /// Container for all sort of Buffer dependant errors.
378         std::map<std::string, ErrorList> errorLists_;
379 };
380
381
382 } // namespace lyx
383
384 #endif