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