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