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