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