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