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