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