]> git.lyx.org Git - lyx.git/blob - src/Buffer.h
Restor 1.4.x behaviour: Don't touch the preamble QTextEdit if the preamble text is...
[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 "ErrorList.h"
16 #include "InsetList.h"
17
18 #include "DocIterator.h"
19
20 #include "support/FileName.h"
21 #include "support/limited_stack.h"
22 #include "support/types.h"
23 #include "support/docstring.h"
24 #include "support/docstream.h"
25
26 #include <boost/scoped_ptr.hpp>
27 #include <boost/signal.hpp>
28
29 #include <iosfwd>
30 #include <string>
31 #include <map>
32 #include <utility>
33 #include <vector>
34
35
36 namespace lyx {
37
38 class BufferParams;
39 class ErrorItem;
40 class FuncRequest;
41 class Inset;
42 class InsetText;
43 class Font;
44 class Lexer;
45 class LyXRC;
46 class Text;
47 class LyXVC;
48 class LaTeXFeatures;
49 class Language;
50 class MacroData;
51 class OutputParams;
52 class ParConstIterator;
53 class ParIterator;
54 class ParagraphList;
55 class StableDocIterator;
56 class TeXErrors;
57 class TexRow;
58 class TocBackend;
59 class Undo;
60
61
62 /** The buffer object.
63  * This is the buffer object. It contains all the informations about
64  * a document loaded into LyX.
65  * The buffer object owns the Text (wrapped in an InsetText), which
66  * contains the individual paragraphs of the document.
67  *
68  *
69  * I am not sure if the class is complete or
70  * minimal, probably not.
71  * \author Lars Gullik Bjønnes
72   */
73 class Buffer {
74 public:
75         /// What type of log will \c getLogName() return?
76         enum LogType {
77                 latexlog, ///< LaTeX log
78                 buildlog  ///< Literate build log
79         };
80
81         /// Result of \c readFile()
82         enum ReadStatus {
83                 failure, ///< The file could not be read
84                 success, ///< The file could not be read
85                 wrongversion ///< The version of the file does not match ours
86         };
87
88         /** Constructor
89             \param file
90             \param b  optional \c false by default
91         */
92         explicit Buffer(std::string const & file, bool b = false);
93
94         /// Destructor
95         ~Buffer();
96
97         /** High-level interface to buffer functionality.
98             This function parses a command string and executes it
99         */
100         bool dispatch(std::string const & command, bool * result = 0);
101
102         /// Maybe we know the function already by number...
103         bool dispatch(FuncRequest const & func, bool * result = 0);
104
105         /// Load the autosaved file.
106         void loadAutoSaveFile();
107
108         /// read a new document from a string
109         bool readString(std::string const &);
110         /// load a new file
111         bool readFile(support::FileName const & filename);
112
113         /// read the header, returns number of unknown tokens
114         int readHeader(Lexer & lex);
115
116         /** Reads a file without header.
117             \param par if != 0 insert the file.
118             \return \c false if file is not completely read.
119         */
120         bool readDocument(Lexer &);
121
122         ///
123         void insertStringAsLines(ParagraphList & plist,
124                 pit_type &, pos_type &,
125                 Font const &, docstring const &, bool);
126         ///
127         ParIterator getParFromID(int id) const;
128         /// do we have a paragraph with this id?
129         bool hasParWithID(int id) const;
130
131         /// This signal is emitted when the buffer is changed.
132         boost::signal<void()> changed;
133         /// This signal is emitted when the buffer structure is changed.
134         boost::signal<void()> structureChanged;
135         /// This signal is emitted when some parsing error shows up.
136         boost::signal<void(std::string)> errors;
137         /// This signal is emitted when some message shows up.
138         boost::signal<void(docstring)> message;
139         /// This signal is emitted when the buffer busy status change.
140         boost::signal<void(bool)> busy;
141         /// This signal is emitted when the buffer readonly status change.
142         boost::signal<void(bool)> readonly;
143         /// Update window titles of all users.
144         boost::signal<void()> updateTitles;
145         /// Reset autosave timers for all users.
146         boost::signal<void()> resetAutosaveTimers;
147         /// This signal is emitting if the buffer is being closed.
148         boost::signal<void()> closing;
149
150
151         /** Save file.
152             Takes care of auto-save files and backup file if requested.
153             Returns \c true if the save is successful, \c false otherwise.
154         */
155         bool save() const;
156
157         /// Write document to stream. Returns \c false if unsuccesful.
158         bool write(std::ostream &) const;
159         /// Write file. Returns \c false if unsuccesful.
160         bool writeFile(support::FileName const &) const;
161
162         /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
163         bool makeLaTeXFile(support::FileName const & filename,
164                            std::string const & original_path,
165                            OutputParams const &,
166                            bool output_preamble = true,
167                            bool output_body = true);
168         /** Export the buffer to LaTeX.
169             If \p os is a file stream, and params().inputenc is "auto" or
170             "default", and the buffer contains text in different languages
171             with more than one encoding, then this method will change the
172             encoding associated to \p os. Therefore you must not call this
173             method with a string stream if the output is supposed to go to a
174             file. \code
175             odocfstream ofs;
176             ofs.open("test.tex");
177             writeLaTeXSource(ofs, ...);
178             ofs.close();
179             \endcode is NOT equivalent to \code
180             odocstringstream oss;
181             writeLaTeXSource(oss, ...);
182             odocfstream ofs;
183             ofs.open("test.tex");
184             ofs << oss.str();
185             ofs.close();
186             \endcode
187          */
188         void writeLaTeXSource(odocstream & os,
189                            std::string const & original_path,
190                            OutputParams const &,
191                            bool output_preamble = true,
192                            bool output_body = true);
193         ///
194         void makeDocBookFile(support::FileName const & filename,
195                              OutputParams const & runparams_in,
196                              bool only_body = false);
197         ///
198         void writeDocBookSource(odocstream & os, std::string const & filename,
199                              OutputParams const & runparams_in,
200                              bool only_body = false);
201         /// returns the main language for the buffer (document)
202         Language const * getLanguage() const;
203         /// get l10n translated to the buffers language
204         docstring const B_(std::string const & l10n) const;
205
206         ///
207         int runChktex();
208         /// return true if the main lyx file does not need saving
209         bool isClean() const;
210         ///
211         bool isBakClean() const;
212         ///
213         bool isDepClean(std::string const & name) const;
214
215         /// mark the main lyx file as not needing saving
216         void markClean() const;
217
218         ///
219         void markBakClean();
220
221         ///
222         void markDepClean(std::string const & name);
223
224         ///
225         void setUnnamed(bool flag = true);
226
227         ///
228         bool isUnnamed() const;
229
230         /// Mark this buffer as dirty.
231         void markDirty();
232
233         /// Returns the buffer's filename. It is always an absolute path.
234         std::string const fileName() const;
235
236         /// Returns the the path where the buffer lives.
237         /// It is always an absolute path.
238         std::string const & filePath() const;
239
240         /** A transformed version of the file name, adequate for LaTeX.
241             \param no_path optional if \c true then the path is stripped.
242         */
243         std::string const getLatexName(bool no_path = true) const;
244
245         /// Get thee name and type of the log.
246         std::pair<LogType, std::string> const getLogName() const;
247
248         /// Change name of buffer. Updates "read-only" flag.
249         void setFileName(std::string const & newfile);
250
251         /// Name of the document's parent
252         void setParentName(std::string const &);
253
254         /** Get the document's master (or \c this if this is not a
255             child document)
256          */
257         Buffer const * getMasterBuffer() const;
258         /** Get the document's master (or \c this if this is not a
259             child document)
260          */
261         Buffer * getMasterBuffer();
262
263         /// Is buffer read-only?
264         bool isReadonly() const;
265
266         /// Set buffer read-only flag
267         void setReadonly(bool flag = true);
268
269         /// returns \c true if the buffer contains a LaTeX document
270         bool isLatex() const;
271         /// returns \c true if the buffer contains a DocBook document
272         bool isDocBook() const;
273         /// returns \c true if the buffer contains a Wed document
274         bool isLiterate() const;
275
276         /** Validate a buffer for LaTeX.
277             This validates the buffer, and returns a struct for use by
278             #makeLaTeX# and others. Its main use is to figure out what
279             commands and packages need to be included in the LaTeX file.
280             It (should) also check that the needed constructs are there
281             (i.e. that the \refs points to coresponding \labels). It
282             should perhaps inset "error" insets to help the user correct
283             obvious mistakes.
284         */
285         void validate(LaTeXFeatures &) const;
286
287         /// return all bibkeys from buffer and its childs
288         void fillWithBibKeys(std::vector<std::pair<std::string, docstring> > & keys) const;
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 saveCursor(StableDocIterator cursor, StableDocIterator anchor);
371         ///
372         StableDocIterator getCursor() const { return cursor_; }
373         ///
374         StableDocIterator getAnchor() const { return anchor_; }
375         ///
376         void changeRefsIfUnique(docstring const & from, docstring const & to,
377                 Inset::Code code);
378 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
379 /// including preamble
380         void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
381
382         /// errorLists_ accessors.
383         //@{
384         ErrorList const & errorList(std::string const & type) const;
385         ErrorList & errorList(std::string const & type);
386         //@}
387
388         //@{
389         TocBackend & tocBackend();
390         TocBackend const & tocBackend() const;
391         //@}
392
393 private:
394         /** Inserts a file into a document
395             \return \c false if method fails.
396         */
397         ReadStatus readFile(Lexer &, support::FileName const & filename,
398                             bool fromString = false);
399
400         /// Use the Pimpl idiom to hide the internals.
401         class Impl;
402         /// The pointer never changes although *pimpl_'s contents may.
403         boost::scoped_ptr<Impl> const pimpl_;
404
405         /// Save the cursor Position on Buffer switch
406         /// this would not be needed if every Buffer would have
407         /// it's BufferView, this should be FIXED in future.
408         StableDocIterator cursor_;
409         StableDocIterator anchor_;
410         /// A cache for the bibfiles (including bibfiles of loaded child
411         /// documents), needed for appropriate update of natbib labels.
412         mutable std::vector<support::FileName> bibfilesCache_;
413
414         /// Container for all sort of Buffer dependant errors.
415         std::map<std::string, ErrorList> errorLists_;
416 };
417
418
419 } // namespace lyx
420
421 #endif