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