]> git.lyx.org Git - lyx.git/blob - src/buffer.h
Use ParList iterator to insert lyxfile and read the document.
[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
29 class BufferView;
30 class LyXRC;
31 class TeXErrors;
32 class LaTeXFeatures;
33 class Language;
34 class ParIterator;
35 class ParConstIterator;
36
37
38 /** The buffer object.
39   This is the buffer object. It contains all the informations about
40   a document loaded into LyX. I am not sure if the class is complete or
41   minimal, probably not.
42   \author Lars Gullik Bjønnes
43   */
44 class Buffer {
45 public:
46         /// What type of log will \c getLogName() return?
47         enum LogType {
48                 latexlog, ///< LaTeX log
49                 buildlog  ///< Literate build log
50         };
51
52         /** Constructor
53             \param file
54             \param b  optional \c false by default
55         */
56         explicit Buffer(string const & file, bool b = false);
57
58         /// Destructor
59         ~Buffer();
60
61         /** High-level interface to buffer functionality.
62             This function parses a command string and executes it
63         */
64         bool dispatch(string const & command, bool * result = 0);
65
66         /// Maybe we know the function already by number...
67         bool dispatch(int ac, string const & argument, bool * result = 0);
68
69         ///
70         void resizeInsets(BufferView *);
71
72         /// Update window titles of all users.
73         void updateTitles() const;
74
75         /// Reset autosave timers for all users.
76         void resetAutosaveTimers() const;
77
78         /** Adds the BufferView to the users list.
79             Later this func will insert the \c BufferView into a real list,
80             not just setting a pointer.
81         */
82         void addUser(BufferView * u);
83
84         /** Removes the #BufferView# from the users list.
85             Since we only can have one at the moment, we just reset it.
86         */
87         void delUser(BufferView *);
88
89         ///
90         void redraw();
91
92         /// Load the autosaved file.
93         void loadAutoSaveFile();
94
95         /** Inserts a file into a document
96             \param par if != 0 insert the file.
97             \return \c false if method fails.
98         */
99         bool readFile(LyXLex &, string const &, ParagraphList::iterator pit);
100
101         // FIXME: it's very silly to pass a lex in here
102         /// load a new file
103         bool readFile(LyXLex &, string const &);
104
105         /// read the header, returns number of unknown tokens
106         int readHeader(LyXLex & lex);
107
108         /** Reads a file without header.
109             \param par if != 0 insert the file.
110             \return \c false if file is not completely read.
111         */
112         bool readBody(LyXLex &, ParagraphList::iterator pit);
113
114         /// This parses a single token
115         int readToken(LyXLex &, ParagraphList & pars,
116                       ParagraphList::iterator & pit,
117                       string const & token, int & pos,
118                       Paragraph::depth_type & depth,
119                       LyXFont &);
120
121         ///
122         void insertStringAsLines(Paragraph *&, lyx::pos_type &,
123                                  LyXFont const &, string const &);
124         ///
125         Paragraph * getParFromID(int id) const;
126
127 private:
128         /// Parse a single inset.
129         void readInset(LyXLex &, ParagraphList::iterator pit, int & pos, LyXFont &, Change current_change);
130
131 public:
132         /** Save file.
133             Takes care of auto-save files and backup file if requested.
134             Returns \c true if the save is successful, \c false otherwise.
135         */
136         bool save() const;
137
138         /// Write file. Returns \c false if unsuccesful.
139         bool writeFile(string const &) const;
140
141         ///
142         void writeFileAscii(string const & , int);
143         ///
144         void writeFileAscii(std::ostream &, int);
145         ///
146         string const asciiParagraph(Paragraph const &, unsigned int linelen,
147                                     bool noparbreak = false) const;
148         /// Just a wrapper for the method below, first creating the ofstream.
149         void makeLaTeXFile(string const & filename,
150                            string const & original_path,
151                            bool nice,
152                            bool only_body = false,
153                            bool only_preamble = false);
154         ///
155         void makeLaTeXFile(std::ostream & os,
156                            string const & original_path,
157                            bool nice,
158                            bool only_body = false,
159                            bool only_preamble = false);
160         ///
161         void simpleDocBookOnePar(std::ostream &,
162                                  Paragraph * par, int & desc_on,
163                                  Paragraph::depth_type depth) const ;
164         ///
165         void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
166                                   Paragraph::depth_type depth);
167         ///
168         void makeLinuxDocFile(string const & filename,
169                               bool nice, bool only_body = false);
170         ///
171         void makeDocBookFile(string const & filename,
172                              bool nice, bool only_body = false);
173         ///
174         void sgmlError(Paragraph * par, int pos, string const & message) const;
175
176         /// returns the main language for the buffer (document)
177         Language const * getLanguage() const;
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         bool isMultiLingual();
268
269         /// Does this mean that this is buffer local?
270         limited_stack<boost::shared_ptr<Undo> > undostack;
271
272         /// Does this mean that this is buffer local?
273         limited_stack<boost::shared_ptr<Undo> > redostack;
274
275         ///
276         BufferParams params;
277
278         /** The list of paragraphs.
279             This is a linked list of paragraph, this list holds the
280             whole contents of the document.
281          */
282         ParagraphList paragraphs;
283
284         /// LyX version control object.
285         LyXVC lyxvc;
286
287         /// Where to put temporary files.
288         string tmppath;
289
290         /** If we are writing a nice LaTeX file or not.
291             While writing as LaTeX, tells whether we are
292             doing a 'nice' LaTeX file */
293         bool niceFile;
294
295         /// Used when typesetting to place errorboxes.
296         TexRow texrow;
297
298         /// the author list for the document
299         AuthorList & authors();
300
301 private:
302         typedef std::map<string, bool> DepClean;
303
304         /// need to regenerate .tex ?
305         DepClean dep_clean_;
306
307         /// is save needed
308         mutable bool lyx_clean;
309
310         /// is autosave needed
311         mutable bool bak_clean;
312
313         /// is this a unnamed file (New...)
314         bool unnamed;
315
316         /// buffer is r/o
317         bool read_only;
318
319         /// name of the file the buffer is associated with.
320         string filename_;
321
322         /// The path to the document file.
323         string filepath_;
324
325         /// Format number of buffer
326         int file_format;
327         /** A list of views using this buffer.
328             Why not keep a list of the BufferViews that use this buffer?
329
330             At least then we don't have to do a lot of magic like:
331             #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
332             of the buffers in the list of users to do a #updateLayoutChoice#.
333         */
334         BufferView * users;
335 public:
336         ///
337         class inset_iterator {
338         public:
339                 typedef std::input_iterator_tag iterator_category;
340                 typedef Inset value_type;
341                 typedef ptrdiff_t difference_type;
342                 typedef Inset * pointer;
343                 typedef Inset & reference;
344                 typedef ParagraphList::iterator base_type;
345
346                 ///
347                 inset_iterator();
348                 ///
349                 inset_iterator(base_type p, base_type e);
350                 ///
351                 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
352
353                 /// prefix ++
354                 inset_iterator & operator++();
355                 /// postfix ++
356                 inset_iterator operator++(int);
357                 ///
358                 reference operator*();
359                 ///
360                 pointer operator->();
361
362                 ///
363                 Paragraph * getPar();
364                 ///
365                 lyx::pos_type getPos() const;
366                 ///
367                 friend
368                 bool operator==(inset_iterator const & iter1,
369                                 inset_iterator const & iter2);
370         private:
371                 ///
372                 void setParagraph();
373                 ///
374                 ParagraphList::iterator pit;
375                 ///
376                 ParagraphList::iterator pend;
377                 ///
378                 InsetList::iterator it;
379         };
380
381         ///
382         inset_iterator inset_iterator_begin() {
383                 return inset_iterator(paragraphs.begin(), paragraphs.end());
384         }
385
386         ///
387         inset_iterator inset_iterator_end() {
388                 return inset_iterator();
389         }
390
391         ///
392         inset_iterator inset_const_iterator_begin() const {
393                 return inset_iterator(paragraphs.begin(), paragraphs.end());
394         }
395
396         ///
397         inset_iterator inset_const_iterator_end() const {
398                 return inset_iterator();
399         }
400
401         ///
402         ParIterator par_iterator_begin();
403         ///
404         ParConstIterator par_iterator_begin() const;
405         ///
406         ParIterator par_iterator_end();
407         ///
408         ParConstIterator par_iterator_end() const;
409
410         ///
411         Inset * getInsetFromID(int id_arg) const;
412 };
413
414 bool operator==(Buffer::inset_iterator const & iter1,
415                 Buffer::inset_iterator const & iter2);
416
417 bool operator!=(Buffer::inset_iterator const & iter1,
418                 Buffer::inset_iterator const & iter2);
419 #endif