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