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