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