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