]> git.lyx.org Git - lyx.git/blob - src/Buffer.h
81e52d2517da0839b8cfcc67ce9cf2609bb476f5
[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/signal.hpp>
24
25 #include <iosfwd>
26 #include <string>
27 #include <utility>
28 #include <vector>
29
30
31 namespace lyx {
32
33 class BufferParams;
34 class ErrorItem;
35 class ErrorList;
36 class FuncRequest;
37 class Inset;
38 class InsetText;
39 class Font;
40 class Lexer;
41 class LyXRC;
42 class Text;
43 class LyXVC;
44 class LaTeXFeatures;
45 class Language;
46 class MacroData;
47 class OutputParams;
48 class ParConstIterator;
49 class ParIterator;
50 class ParagraphList;
51 class TeXErrors;
52 class TexRow;
53 class TocBackend;
54 class Undo;
55
56 namespace frontend {
57 class WorkAreaManager;
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         void changed();
147
148         ///
149         frontend::WorkAreaManager * workAreaManager() const;
150
151         /// This signal is emitted when the buffer structure is changed.
152         boost::signal<void()> structureChanged;
153         /// This signal is emitted when an embedded file is changed
154         boost::signal<void()> embeddingChanged;
155         /// This signal is emitted when some parsing error shows up.
156         boost::signal<void(std::string)> errors;
157         /// This signal is emitted when some message shows up.
158         boost::signal<void(docstring)> message;
159         /// This signal is emitted when the buffer busy status change.
160         boost::signal<void(bool)> busy;
161         /// This signal is emitted when the buffer readonly status change.
162         boost::signal<void(bool)> readonly;
163         /// Update window titles of all users.
164         boost::signal<void()> updateTitles;
165         /// Reset autosave timers for all users.
166         boost::signal<void()> resetAutosaveTimers;
167
168
169         /** Save file.
170             Takes care of auto-save files and backup file if requested.
171             Returns \c true if the save is successful, \c false otherwise.
172         */
173         bool save() const;
174
175         /// Write document to stream. Returns \c false if unsuccesful.
176         bool write(std::ostream &) const;
177         /// Write file. Returns \c false if unsuccesful.
178         bool writeFile(support::FileName const &) const;
179
180         /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
181         bool makeLaTeXFile(support::FileName const & filename,
182                            std::string const & original_path,
183                            OutputParams const &,
184                            bool output_preamble = true,
185                            bool output_body = true);
186         /** Export the buffer to LaTeX.
187             If \p os is a file stream, and params().inputenc is "auto" or
188             "default", and the buffer contains text in different languages
189             with more than one encoding, then this method will change the
190             encoding associated to \p os. Therefore you must not call this
191             method with a string stream if the output is supposed to go to a
192             file. \code
193             odocfstream ofs;
194             ofs.open("test.tex");
195             writeLaTeXSource(ofs, ...);
196             ofs.close();
197             \endcode is NOT equivalent to \code
198             odocstringstream oss;
199             writeLaTeXSource(oss, ...);
200             odocfstream ofs;
201             ofs.open("test.tex");
202             ofs << oss.str();
203             ofs.close();
204             \endcode
205          */
206         void writeLaTeXSource(odocstream & os,
207                            std::string const & original_path,
208                            OutputParams const &,
209                            bool output_preamble = true,
210                            bool output_body = true);
211         ///
212         void makeDocBookFile(support::FileName const & filename,
213                              OutputParams const & runparams_in,
214                              bool only_body = false);
215         ///
216         void writeDocBookSource(odocstream & os, std::string const & filename,
217                              OutputParams const & runparams_in,
218                              bool only_body = false);
219         /// returns the main language for the buffer (document)
220         Language const * getLanguage() const;
221         /// get l10n translated to the buffers language
222         docstring const B_(std::string const & l10n) const;
223
224         ///
225         int runChktex();
226         /// return true if the main lyx file does not need saving
227         bool isClean() const;
228         ///
229         bool isBakClean() const;
230         ///
231         bool isDepClean(std::string const & name) const;
232
233         /// whether or not disk file has been externally modified
234         bool isExternallyModified(CheckMethod method) const;
235
236         /// save timestamp and checksum of the given file.
237         void saveCheckSum(std::string const & file) const;
238
239         /// mark the main lyx file as not needing saving
240         void markClean() const;
241
242         ///
243         void markBakClean();
244
245         ///
246         void markDepClean(std::string const & name);
247
248         ///
249         void setUnnamed(bool flag = true);
250
251         ///
252         bool isUnnamed() const;
253
254         /// Mark this buffer as dirty.
255         void markDirty();
256
257         /// Returns the buffer's filename. It is always an absolute path.
258         std::string const fileName() const;
259
260         /// Returns the the path where the buffer lives.
261         /// It is always an absolute path.
262         std::string const & filePath() const;
263
264         /** A transformed version of the file name, adequate for LaTeX.
265             \param no_path optional if \c true then the path is stripped.
266         */
267         std::string const getLatexName(bool no_path = true) const;
268
269         /// Get thee name and type of the log.
270         std::pair<LogType, std::string> const getLogName() const;
271
272         /// Change name of buffer. Updates "read-only" flag.
273         void setFileName(std::string const & newfile);
274
275         /// Name of the document's parent
276         void setParentName(std::string const &);
277
278         /** Get the document's master (or \c this if this is not a
279             child document)
280          */
281         Buffer const * getMasterBuffer() const;
282         /** Get the document's master (or \c this if this is not a
283             child document)
284          */
285         Buffer * getMasterBuffer();
286
287         /// Is buffer read-only?
288         bool isReadonly() const;
289
290         /// Set buffer read-only flag
291         void setReadonly(bool flag = true);
292
293         /// returns \c true if the buffer contains a LaTeX document
294         bool isLatex() const;
295         /// returns \c true if the buffer contains a DocBook document
296         bool isDocBook() const;
297         /// returns \c true if the buffer contains a Wed document
298         bool isLiterate() const;
299
300         /** Validate a buffer for LaTeX.
301             This validates the buffer, and returns a struct for use by
302             #makeLaTeX# and others. Its main use is to figure out what
303             commands and packages need to be included in the LaTeX file.
304             It (should) also check that the needed constructs are there
305             (i.e. that the \refs points to coresponding \labels). It
306             should perhaps inset "error" insets to help the user correct
307             obvious mistakes.
308         */
309         void validate(LaTeXFeatures &) const;
310
311         /// Update the cache with all bibfiles in use (including bibfiles
312         /// of loaded child documents).
313         void updateBibfilesCache();
314         /// Return the cache with all bibfiles in use (including bibfiles
315         /// of loaded child documents).
316         std::vector<support::FileName> const & getBibfilesCache() const;
317         ///
318         void getLabelList(std::vector<docstring> &) const;
319
320         ///
321         void changeLanguage(Language const * from, Language const * to);
322
323         ///
324         bool isMultiLingual() const;
325
326         /// Does this mean that this is buffer local?
327         limited_stack<Undo> & undostack();
328         limited_stack<Undo> const & undostack() const;
329
330         /// Does this mean that this is buffer local?
331         limited_stack<Undo> & redostack();
332         limited_stack<Undo> const & redostack() const;
333
334         ///
335         BufferParams & params();
336         BufferParams const & params() const;
337
338         /** The list of paragraphs.
339             This is a linked list of paragraph, this list holds the
340             whole contents of the document.
341          */
342         ParagraphList & paragraphs();
343         ParagraphList const & paragraphs() const;
344
345         /// LyX version control object.
346         LyXVC & lyxvc();
347         LyXVC const & lyxvc() const;
348
349         /// Where to put temporary files.
350         std::string const & temppath() const;
351
352         /// Used when typesetting to place errorboxes.
353         TexRow & texrow();
354         TexRow const & texrow() const;
355
356         ///
357         ParIterator par_iterator_begin();
358         ///
359         ParConstIterator par_iterator_begin() const;
360         ///
361         ParIterator par_iterator_end();
362         ///
363         ParConstIterator par_iterator_end() const;
364
365         /** \returns true only when the file is fully loaded.
366          *  Used to prevent the premature generation of previews
367          *  and by the citation inset.
368          */
369         bool fully_loaded() const;
370         /// Set by buffer_funcs' newFile.
371         void fully_loaded(bool);
372
373         /// Our main text (inside the top InsetText)
374         Text & text() const;
375
376         /// Our top InsetText!
377         Inset & inset() const;
378
379         //
380         // Macro handling
381         //
382         ///
383         void buildMacros();
384         ///
385         bool hasMacro(docstring const & name) const;
386         ///
387         MacroData const & getMacro(docstring const & name) const;
388         ///
389         void insertMacro(docstring const & name, MacroData const & data);
390
391         ///
392         void changeRefsIfUnique(docstring const & from, docstring const & to,
393                 Inset::Code code);
394 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
395 /// including preamble
396         void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
397
398         /// errorLists_ accessors.
399         //@{
400         ErrorList const & errorList(std::string const & type) const;
401         ErrorList & errorList(std::string const & type);
402         //@}
403
404         //@{
405         TocBackend & tocBackend();
406         TocBackend const & tocBackend() const;
407         //@}
408         
409         //@{
410         EmbeddedFiles & embeddedFiles();
411         EmbeddedFiles const & embeddedFiles() const;
412         //@}
413
414 private:
415         /** Inserts a file into a document
416             \return \c false if method fails.
417         */
418         ReadStatus readFile(Lexer &, support::FileName const & filename,
419                             bool fromString = false);
420
421         /// Use the Pimpl idiom to hide the internals.
422         class Impl;
423         /// The pointer never changes although *pimpl_'s contents may.
424         Impl * const pimpl_;
425
426         /// A cache for the bibfiles (including bibfiles of loaded child
427         /// documents), needed for appropriate update of natbib labels.
428         mutable std::vector<support::FileName> bibfilesCache_;
429 };
430
431
432 } // namespace lyx
433
434 #endif