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