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