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