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