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