]> git.lyx.org Git - features.git/blob - src/buffer.h
Make the output of \inputencoding commands (and the change of output
[features.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 == "auto", and
157             the buffer contains text in different languages with more than
158             one encoding, then this method will change the encoding
159             associated to \p os. Therefore you must not call this method with
160             a string stream if the output is supposed to go to a file. \code
161             odocfstream ofs;
162             ofs.open("test.tex");
163             writeLaTeXSource(ofs, ...);
164             ofs.close();
165             \endcode is NOT equivalent to \code
166             odocstringstream oss;
167             writeLaTeXSource(oss, ...);
168             odocfstream ofs;
169             ofs.open("test.tex");
170             ofs << oss.str();
171             ofs.close();
172             \endcode
173          */
174         void writeLaTeXSource(odocstream & os,
175                            std::string const & original_path,
176                            OutputParams const &,
177                            bool output_preamble = true,
178                            bool output_body = true);
179         ///
180         void makeDocBookFile(support::FileName const & filename,
181                              OutputParams const & runparams_in,
182                              bool only_body = false);
183         ///
184         void writeDocBookSource(odocstream & os, std::string const & filename,
185                              OutputParams const & runparams_in,
186                              bool only_body = false);
187         /// returns the main language for the buffer (document)
188         Language const * getLanguage() const;
189         /// get l10n translated to the buffers language
190         docstring const B_(std::string const & l10n) const;
191         /// translate \p label to the buffer language if possible
192         docstring const translateLabel(docstring const & label) const;
193
194         ///
195         int runChktex();
196         /// return true if the main lyx file does not need saving
197         bool isClean() const;
198         ///
199         bool isBakClean() const;
200         ///
201         bool isDepClean(std::string const & name) const;
202
203         /// mark the main lyx file as not needing saving
204         void markClean() const;
205
206         ///
207         void markBakClean();
208
209         ///
210         void markDepClean(std::string const & name);
211
212         ///
213         void setUnnamed(bool flag = true);
214
215         ///
216         bool isUnnamed() const;
217
218         /// Mark this buffer as dirty.
219         void markDirty();
220
221         /// Returns the buffer's filename. It is always an absolute path.
222         std::string const fileName() const;
223
224         /// Returns the the path where the buffer lives.
225         /// It is always an absolute path.
226         std::string const & filePath() const;
227
228         /** A transformed version of the file name, adequate for LaTeX.
229             \param no_path optional if \c true then the path is stripped.
230         */
231         std::string const getLatexName(bool no_path = true) const;
232
233         /// Get thee name and type of the log.
234         std::pair<LogType, std::string> const getLogName() const;
235
236         /// Change name of buffer. Updates "read-only" flag.
237         void setFileName(std::string const & newfile);
238
239         /// Name of the document's parent
240         void setParentName(std::string const &);
241
242         /** Get the document's master (or \c this if this is not a
243             child document)
244          */
245         Buffer const * getMasterBuffer() const;
246         /** Get the document's master (or \c this if this is not a
247             child document)
248          */
249         Buffer * getMasterBuffer();
250
251         /// Is buffer read-only?
252         bool isReadonly() const;
253
254         /// Set buffer read-only flag
255         void setReadonly(bool flag = true);
256
257         /// returns \c true if the buffer contains a LaTeX document
258         bool isLatex() const;
259         /// returns \c true if the buffer contains a DocBook document
260         bool isDocBook() const;
261         /// returns \c true if the buffer contains a Wed document
262         bool isLiterate() const;
263
264         /** Validate a buffer for LaTeX.
265             This validates the buffer, and returns a struct for use by
266             #makeLaTeX# and others. Its main use is to figure out what
267             commands and packages need to be included in the LaTeX file.
268             It (should) also check that the needed constructs are there
269             (i.e. that the \refs points to coresponding \labels). It
270             should perhaps inset "error" insets to help the user correct
271             obvious mistakes.
272         */
273         void validate(LaTeXFeatures &) const;
274
275         /// return all bibkeys from buffer and its childs
276         void fillWithBibKeys(std::vector<std::pair<std::string, docstring> > & keys) const;
277         /// Update the cache with all bibfiles in use (including bibfiles
278         /// of loaded child documents).
279         void updateBibfilesCache();
280         /// Return the cache with all bibfiles in use (including bibfiles
281         /// of loaded child documents).
282         std::vector<support::FileName> const & getBibfilesCache() const;
283         ///
284         void getLabelList(std::vector<docstring> &) const;
285
286         ///
287         void changeLanguage(Language const * from, Language const * to);
288
289         ///
290         void updateDocLang(Language const * nlang);
291
292         ///
293         bool isMultiLingual() const;
294
295         /// Does this mean that this is buffer local?
296         limited_stack<Undo> & undostack();
297         limited_stack<Undo> const & undostack() const;
298
299         /// Does this mean that this is buffer local?
300         limited_stack<Undo> & redostack();
301         limited_stack<Undo> const & redostack() const;
302
303         ///
304         BufferParams & params();
305         BufferParams const & params() const;
306
307         /** The list of paragraphs.
308             This is a linked list of paragraph, this list holds the
309             whole contents of the document.
310          */
311         ParagraphList & paragraphs();
312         ParagraphList const & paragraphs() const;
313
314         /// LyX version control object.
315         LyXVC & lyxvc();
316         LyXVC const & lyxvc() const;
317
318         /// Where to put temporary files.
319         std::string const & temppath() const;
320
321         /// Used when typesetting to place errorboxes.
322         TexRow & texrow();
323         TexRow const & texrow() const;
324
325         ///
326         ParIterator par_iterator_begin();
327         ///
328         ParConstIterator par_iterator_begin() const;
329         ///
330         ParIterator par_iterator_end();
331         ///
332         ParConstIterator par_iterator_end() const;
333
334         /** \returns true only when the file is fully loaded.
335          *  Used to prevent the premature generation of previews
336          *  and by the citation inset.
337          */
338         bool fully_loaded() const;
339         /// Set by buffer_funcs' newFile.
340         void fully_loaded(bool);
341
342         /// Our main text (inside the top InsetText)
343         LyXText & text() const;
344
345         /// Our top InsetText!
346         InsetBase & inset() const;
347
348         //
349         // Macro handling
350         //
351         ///
352         void buildMacros();
353         ///
354         bool hasMacro(docstring const & name) const;
355         ///
356         MacroData const & getMacro(docstring const & name) const;
357         ///
358         void insertMacro(docstring const & name, MacroData const & data);
359
360         ///
361         void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
362         ///
363         StableDocIterator getCursor() const { return cursor_; }
364         ///
365         StableDocIterator getAnchor() const { return anchor_; }
366         ///
367         void changeRefsIfUnique(docstring const & from, docstring const & to,
368                 InsetBase::Code code);
369 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
370 /// including preamble
371         void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
372
373         /// errorLists_ accessors.
374         //@{
375         ErrorList const & errorList(std::string const & type) const;
376         ErrorList & errorList(std::string const & type);
377         //@}
378
379         //@{
380         TocBackend & tocBackend();
381         TocBackend const & tocBackend() const;
382         //@}
383
384 private:
385         /** Inserts a file into a document
386             \return \c false if method fails.
387         */
388         bool readFile(LyXLex &, support::FileName const & filename);
389
390         bool do_writeFile(std::ostream & ofs) const;
391
392         /// Use the Pimpl idiom to hide the internals.
393         class Impl;
394         /// The pointer never changes although *pimpl_'s contents may.
395         boost::scoped_ptr<Impl> const pimpl_;
396
397         /// Save the cursor Position on Buffer switch
398         /// this would not be needed if every Buffer would have
399         /// it's BufferView, this should be FIXED in future.
400         StableDocIterator cursor_;
401         StableDocIterator anchor_;
402         /// A cache for the bibfiles (including bibfiles of loaded child
403         /// documents), needed for appropriate update of natbib labels.
404         mutable std::vector<support::FileName> bibfilesCache_;
405
406         /// Container for all sort of Buffer dependant errors.
407         std::map<std::string, ErrorList> errorLists_;
408 };
409
410
411 } // namespace lyx
412
413 #endif