]> git.lyx.org Git - lyx.git/blob - src/buffer.h
* output_latex.h:
[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 "InsetList.h"
16
17 #include "dociterator.h"
18 #include "ParagraphList_fwd.h"
19
20 #include "support/limited_stack.h"
21 #include "support/types.h"
22
23 #include <boost/scoped_ptr.hpp>
24 #include <boost/signal.hpp>
25
26 #include <iosfwd>
27 #include <map>
28 #include <utility>
29 #include <vector>
30
31
32 class BufferParams;
33 class ErrorItem;
34 class FuncRequest;
35 class InsetBase;
36 class InsetText;
37 class LyXFont;
38 class LyXLex;
39 class LyXRC;
40 class LyXText;
41 class LyXVC;
42 class LaTeXFeatures;
43 class Language;
44 class MacroData;
45 class OutputParams;
46 class ParConstIterator;
47 class ParIterator;
48 class TeXErrors;
49 class TexRow;
50 class Undo;
51 class StableDocIterator;
52
53
54 /** The buffer object.
55  * This is the buffer object. It contains all the informations about
56  * a document loaded into LyX.
57  * The buffer object owns the LyXText (wrapped in an InsetText), which
58  * contains the individual paragraphs of the document.
59  *
60  *
61  * I am not sure if the class is complete or
62  * minimal, probably not.
63  * \author Lars Gullik Bjønnes
64   */
65 class Buffer {
66 public:
67         /// What type of log will \c getLogName() return?
68         enum LogType {
69                 latexlog, ///< LaTeX log
70                 buildlog  ///< Literate build log
71         };
72
73         /** Constructor
74             \param file
75             \param b  optional \c false by default
76         */
77         explicit Buffer(std::string const & file, bool b = false);
78
79         /// Destructor
80         ~Buffer();
81
82         /** High-level interface to buffer functionality.
83             This function parses a command string and executes it
84         */
85         bool dispatch(std::string const & command, bool * result = 0);
86
87         /// Maybe we know the function already by number...
88         bool dispatch(FuncRequest const & func, bool * result = 0);
89
90         /// Load the autosaved file.
91         void loadAutoSaveFile();
92
93         /// load a new file
94         bool readFile(std::string const & filename);
95
96         /// read the header, returns number of unknown tokens
97         int readHeader(LyXLex & lex);
98
99         /** Reads a file without header.
100             \param par if != 0 insert the file.
101             \return \c false if file is not completely read.
102         */
103         bool readDocument(LyXLex &);
104
105         ///
106         void insertStringAsLines(ParagraphList & plist,
107                 lyx::pit_type &, lyx::pos_type &,
108                 LyXFont const &, std::string const &, bool);
109         ///
110         ParIterator getParFromID(int id) const;
111         /// do we have a paragraph with this id?
112         bool hasParWithID(int id) const;
113
114         /// This signal is emitted when a parsing error shows up.
115         boost::signal<void(ErrorItem)> error;
116         /// This signal is emitted when some message shows up.
117         boost::signal<void(std::string)> message;
118         /// This signal is emitted when the buffer busy status change.
119         boost::signal<void(bool)> busy;
120         /// This signal is emitted when the buffer readonly status change.
121         boost::signal<void(bool)> readonly;
122         /// Update window titles of all users.
123         boost::signal<void()> updateTitles;
124         /// Reset autosave timers for all users.
125         boost::signal<void()> resetAutosaveTimers;
126         /// This signal is emitting if the buffer is being closed.
127         boost::signal<void()> closing;
128
129
130         /** Save file.
131             Takes care of auto-save files and backup file if requested.
132             Returns \c true if the save is successful, \c false otherwise.
133         */
134         bool save() const;
135
136         /// Write file. Returns \c false if unsuccesful.
137         bool writeFile(std::string const &) const;
138
139         ///
140         void writeFileAscii(std::string const &, OutputParams const &);
141         ///
142         void writeFileAscii(std::ostream &, OutputParams const &);
143         /// Just a wrapper for the method below, first creating the ofstream.
144         void makeLaTeXFile(std::string const & filename,
145                            std::string const & original_path,
146                            OutputParams const &,
147                            bool output_preamble = true,
148                            bool output_body = true);
149         ///
150         void makeLaTeXFile(std::ostream & os,
151                            std::string const & original_path,
152                            OutputParams const &,
153                            bool output_preamble = true,
154                            bool output_body = true);
155         ///
156         void makeLinuxDocFile(std::string const & filename,
157                               OutputParams const & runparams_in,
158                               bool only_body = false);
159         ///
160         void makeDocBookFile(std::string const & filename,
161                              OutputParams const & runparams_in,
162                              bool only_body = false);
163         /// returns the main language for the buffer (document)
164         Language const * getLanguage() const;
165         /// get l10n translated to the buffers language
166         std::string const B_(std::string const & l10n) const;
167
168         ///
169         int runChktex();
170         /// return true if the main lyx file does not need saving
171         bool isClean() const;
172         ///
173         bool isBakClean() const;
174         ///
175         bool isDepClean(std::string const & name) const;
176
177         /// mark the main lyx file as not needing saving
178         void markClean() const;
179
180         ///
181         void markBakClean();
182
183         ///
184         void markDepClean(std::string const & name);
185
186         ///
187         void setUnnamed(bool flag = true);
188
189         ///
190         bool isUnnamed() const;
191
192         /// Mark this buffer as dirty.
193         void markDirty();
194
195         /// Returns the buffer's filename. It is always an absolute path.
196         std::string const & fileName() const;
197
198         /// Returns the the path where the buffer lives.
199         /// It is always an absolute path.
200         std::string const & filePath() const;
201
202         /** A transformed version of the file name, adequate for LaTeX.
203             \param no_path optional if \c true then the path is stripped.
204         */
205         std::string const getLatexName(bool no_path = true) const;
206
207         /// Get the name and type of the log.
208         std::pair<LogType, std::string> const getLogName() const;
209
210         /// Change name of buffer. Updates "read-only" flag.
211         void setFileName(std::string const & newfile);
212
213         /// Name of the document's parent
214         void setParentName(std::string const &);
215
216         /** Get the document's master (or \c this if this is not a
217             child document)
218          */
219         Buffer const * getMasterBuffer() const;
220
221         /// Is buffer read-only?
222         bool isReadonly() const;
223
224         /// Set buffer read-only flag
225         void setReadonly(bool flag = true);
226
227         /// returns \c true if the buffer contains a LaTeX document
228         bool isLatex() const;
229         /// returns \c true if the buffer contains a LinuxDoc document
230         bool isLinuxDoc() const;
231         /// returns \c true if the buffer contains a DocBook document
232         bool isDocBook() const;
233         /** returns \c true if the buffer contains either a LinuxDoc
234             or DocBook document */
235         bool isSGML() const;
236         /// returns \c true if the buffer contains a Wed document
237         bool isLiterate() const;
238
239         /** Validate a buffer for LaTeX.
240             This validates the buffer, and returns a struct for use by
241             #makeLaTeX# and others. Its main use is to figure out what
242             commands and packages need to be included in the LaTeX file.
243             It (should) also check that the needed constructs are there
244             (i.e. that the \refs points to coresponding \labels). It
245             should perhaps inset "error" insets to help the user correct
246             obvious mistakes.
247         */
248         void validate(LaTeXFeatures &) const;
249
250         /// return all bibkeys from buffer and its childs
251         void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
252         ///
253         void getLabelList(std::vector<std::string> &) const;
254
255         ///
256         void changeLanguage(Language const * from, Language const * to);
257
258         ///
259         void updateDocLang(Language const * nlang);
260
261         ///
262         bool isMultiLingual() const;
263
264         /// Does this mean that this is buffer local?
265         limited_stack<Undo> & undostack();
266         limited_stack<Undo> const & undostack() const;
267
268         /// Does this mean that this is buffer local?
269         limited_stack<Undo> & redostack();
270         limited_stack<Undo> const & redostack() const;
271
272         ///
273         BufferParams & params();
274         BufferParams const & params() const;
275
276         /** The list of paragraphs.
277             This is a linked list of paragraph, this list holds the
278             whole contents of the document.
279          */
280         ParagraphList & paragraphs();
281         ParagraphList const & paragraphs() const;
282
283         /// LyX version control object.
284         LyXVC & lyxvc();
285         LyXVC const & lyxvc() const;
286
287         /// Where to put temporary files.
288         std::string const & temppath() const;
289
290         /// Used when typesetting to place errorboxes.
291         TexRow & texrow();
292         TexRow const & texrow() const;
293
294         ///
295         ParIterator par_iterator_begin();
296         ///
297         ParConstIterator par_iterator_begin() const;
298         ///
299         ParIterator par_iterator_end();
300         ///
301         ParConstIterator par_iterator_end() const;
302
303         /** \returns true only when the file is fully loaded.
304          *  Used to prevent the premature generation of previews
305          *  and by the citation inset.
306          */
307         bool fully_loaded() const;
308         /// Set by buffer_funcs' newFile.
309         void fully_loaded(bool);
310
311         /// Our main text (inside the top InsetText)
312         LyXText & text() const;
313
314         /// Our top InsetText!
315         InsetBase & inset() const;
316
317         //
318         // Macro handling
319         //
320         ///
321         void buildMacros();
322         ///
323         bool hasMacro(std::string const & name) const;
324         ///
325         MacroData const & getMacro(std::string const & name) const;
326         ///
327         void insertMacro(std::string const & name, MacroData const & data);
328         ///
329         void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
330         ///
331         StableDocIterator getCursor() const { return cursor_; }
332         ///
333         StableDocIterator getAnchor() const { return anchor_; }
334         ///
335         void changeRefsIfUnique(std::string const & from, std::string const & to);
336
337 private:
338         /** Inserts a file into a document
339             \return \c false if method fails.
340         */
341         bool readFile(LyXLex &, std::string const & filename);
342
343         bool do_writeFile(std::ostream & ofs) const;
344
345         /// Use the Pimpl idiom to hide the internals.
346         class Impl;
347         /// The pointer never changes although *pimpl_'s contents may.
348         boost::scoped_ptr<Impl> const pimpl_;
349
350         /// Save the cursor Position on Buffer switch
351         /// this would not be needed if every Buffer would have
352         /// it's BufferView, this should be FIXED in future.
353         StableDocIterator cursor_;
354         StableDocIterator anchor_;
355 };
356
357 #endif