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