]> git.lyx.org Git - lyx.git/blob - src/buffer.h
Fix reading of old format templates
[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 #ifdef __GNUG__
17 #pragma interface
18 #endif
19
20 #include "LString.h"
21 #include "undo.h"
22 #include "support/limited_stack.h"
23
24 #include "lyxvc.h"
25 #include "bufferparams.h"
26 #include "texrow.h"
27 #include "ParagraphList.h"
28 #include "paragraph.h"
29
30 #include <boost/shared_ptr.hpp>
31
32 class BufferView;
33 class LyXRC;
34 class TeXErrors;
35 class LaTeXFeatures;
36 class Language;
37 class ParIterator;
38
39
40 ///
41 struct DEPCLEAN {
42         ///
43         bool clean;
44         ///
45         string master;
46         ///
47         DEPCLEAN * next;
48 };
49
50 /** The buffer object.
51   This is the buffer object. It contains all the informations about
52   a document loaded into LyX. I am not sure if the class is complete or
53   minimal, probably not.
54   \author Lars Gullik Bjønnes
55   */
56 class Buffer {
57 public:
58         /// What type of log will \c getLogName() return?
59         enum LogType {
60                 latexlog, ///< LaTeX log
61                 buildlog  ///< Literate build log
62         };
63
64         /** Constructor
65             \param file
66             \param b  optional \c false by default
67         */
68         explicit Buffer(string const & file, bool b = false);
69
70         /// Destructor
71         ~Buffer();
72
73         /** High-level interface to buffer functionality.
74             This function parses a command string and executes it
75         */
76         bool dispatch(string const & command, bool * result = 0);
77
78         /// Maybe we know the function already by number...
79         bool dispatch(int ac, string const & argument, bool * result = 0);
80
81         ///
82         void resizeInsets(BufferView *);
83
84         /// Update window titles of all users.
85         void updateTitles() const;
86
87         /// Reset autosave timers for all users.
88         void resetAutosaveTimers() const;
89
90         /** Adds the BufferView to the users list.
91             Later this func will insert the \c BufferView into a real list,
92             not just setting a pointer.
93         */
94         void addUser(BufferView * u);
95
96         /** Removes the #BufferView# from the users list.
97             Since we only can have one at the moment, we just reset it.
98         */
99         void delUser(BufferView *);
100
101         ///
102         void redraw();
103
104         /// Load the autosaved file.
105         void loadAutoSaveFile();
106
107         /** Reads a file.
108             \param par if != 0 insert the file.
109             \return \c false if method fails.
110         */
111         bool readFile(LyXLex &, string const &, Paragraph * par = 0);
112
113         /** Reads a file without header.
114             \param par if != 0 insert the file.
115             \return \c false if file is not completely read.
116         */
117         bool readLyXformat2(LyXLex &, Paragraph * par = 0);
118
119         /// This parses a single LyXformat-Token.
120         bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
121                                         Paragraph *& return_par,
122                                         string const & token, int & pos,
123                                         Paragraph::depth_type & depth,
124                                         LyXFont &);
125         ///
126         void insertStringAsLines(Paragraph *&, lyx::pos_type &,
127                                  LyXFont const &, string const &) const;
128         ///
129         Paragraph * getParFromID(int id) const;
130 private:
131         /// Parse a single inset.
132         void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
133 public:
134         /** Save file.
135             Takes care of auto-save files and backup file if requested.
136             Returns \c true if the save is successful, \c false otherwise.
137         */
138         bool save() const;
139
140         /// Write file. Returns \c false if unsuccesful.
141         bool writeFile(string const &) const;
142
143         ///
144         void writeFileAscii(string const & , int);
145         ///
146         void writeFileAscii(std::ostream &, int);
147         ///
148         string const asciiParagraph(Paragraph const &, unsigned int linelen,
149                                     bool noparbreak = false) const;
150         /// Just a wrapper for the method below, first creating the ofstream.
151         void makeLaTeXFile(string const & filename,
152                            string const & original_path,
153                            bool nice,
154                            bool only_body = false,
155                            bool only_preamble = false);
156         ///
157         void makeLaTeXFile(std::ostream & os,
158                            string const & original_path,
159                            bool nice,
160                            bool only_body = false,
161                            bool only_preamble = false);
162         /** LaTeX all paragraphs from par to endpar.
163             \param \a endpar if == 0 then to the end
164         */
165         void latexParagraphs(std::ostream & os, Paragraph * par,
166                              Paragraph * endpar, TexRow & texrow, bool moving_arg = false) const;
167         ///
168         void simpleDocBookOnePar(std::ostream &,
169                                  Paragraph * par, int & desc_on,
170                                  Paragraph::depth_type depth) const ;
171         ///
172         void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
173                                   Paragraph::depth_type depth);
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         /// Open SGML/XML tag.
181         int sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
182                 string const & latexname) const;
183         /// Closes SGML/XML tag.
184         int sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
185                 string const & latexname) const;
186         ///
187         void sgmlError(Paragraph * par, int pos, string const & message) const;
188
189         /// returns the main language for the buffer (document)
190         Language const * getLanguage() const;
191         ///
192         int runChktex();
193         /// return true if the main lyx file does not need saving
194         bool isClean() const;
195         ///
196         bool isBakClean() const;
197         ///
198         bool isDepClean(string const & name) const;
199
200         /// mark the main lyx file as not needing saving
201         void markClean() const;
202
203         ///
204         void markBakClean();
205
206         ///
207         void markDepClean(string const & name);
208
209         ///
210         void setUnnamed(bool flag = true);
211
212         ///
213         bool isUnnamed();
214
215         /// Mark this buffer as dirty.
216         void markDirty();
217
218         /// Returns the buffer's filename. It is always an absolute path.
219         string const & fileName() const;
220
221         /// Returns the the path where the buffer lives.
222         /// It is always an absolute path.
223         string const & filePath() const;
224
225         /** A transformed version of the file name, adequate for LaTeX.
226             \param no_path optional if \c true then the path is stripped.
227         */
228         string const getLatexName(bool no_path = true) const;
229
230         /// Get the name and type of the log.
231         std::pair<LogType, string> const getLogName() const;
232
233         /// Change name of buffer. Updates "read-only" flag.
234         void setFileName(string const & newfile);
235
236         /// Name of the document's parent
237         void setParentName(string const &);
238
239         /// Is buffer read-only?
240         bool isReadonly() const;
241
242         /// Set buffer read-only flag
243         void setReadonly(bool flag = true);
244
245         /// returns \c true if the buffer contains a LaTeX document
246         bool isLatex() const;
247         /// returns \c true if the buffer contains a LinuxDoc document
248         bool isLinuxDoc() const;
249         /// returns \c true if the buffer contains a DocBook document
250         bool isDocBook() const;
251         /** returns \c true if the buffer contains either a LinuxDoc
252             or DocBook document */
253         bool isSGML() const;
254         /// returns \c true if the buffer contains a Wed document
255         bool isLiterate() const;
256
257         /** Validate a buffer for LaTeX.
258             This validates the buffer, and returns a struct for use by
259             #makeLaTeX# and others. Its main use is to figure out what
260             commands and packages need to be included in the LaTeX file.
261             It (should) also check that the needed constructs are there
262             (i.e. that the \refs points to coresponding \labels). It
263             should perhaps inset "error" insets to help the user correct
264             obvious mistakes.
265         */
266         void validate(LaTeXFeatures &) const;
267
268         ///
269         std::vector<std::pair<string, string> > const getBibkeyList() const;
270         ///
271         std::vector<string> const getLabelList() const;
272
273         /** This will clearly have to change later. Later we can have more
274             than one user per buffer. */
275         BufferView * getUser() const;
276
277         ///
278         void changeLanguage(Language const * from, Language const * to);
279         ///
280         bool isMultiLingual();
281
282         /// Does this mean that this is buffer local?
283         limited_stack<boost::shared_ptr<Undo> > undostack;
284
285         /// Does this mean that this is buffer local?
286         limited_stack<boost::shared_ptr<Undo> > redostack;
287
288         ///
289         BufferParams params;
290
291         /** The list of paragraphs.
292             This is a linked list of paragraph, this list holds the
293             whole contents of the document.
294          */
295         ParagraphList paragraphs;
296
297         /// LyX version control object.
298         LyXVC lyxvc;
299
300         /// Where to put temporary files.
301         string tmppath;
302
303         /** If we are writing a nice LaTeX file or not.
304             While writing as LaTeX, tells whether we are
305             doing a 'nice' LaTeX file */
306         bool niceFile;
307
308         /// Used when typesetting to place errorboxes.
309         TexRow texrow;
310 private:
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         /// is regenerating #.tex# necessary
321         DEPCLEAN * dep_clean;
322
323         /// buffer is r/o
324         bool read_only;
325
326         /// name of the file the buffer is associated with.
327         string filename_;
328
329         /// The path to the document file.
330         string filepath_;
331
332         /// Format number of buffer
333         int file_format;
334         /** A list of views using this buffer.
335             Why not keep a list of the BufferViews that use this buffer?
336
337             At least then we don't have to do a lot of magic like:
338             #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
339             of the buffers in the list of users to do a #updateLayoutChoice#.
340         */
341         BufferView * users;
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                 Paragraph * getPar();
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         ///
389         inset_iterator inset_iterator_begin() {
390                 return inset_iterator(paragraphs.begin(), paragraphs.end());
391         }
392
393         ///
394         inset_iterator inset_iterator_end() {
395                 return inset_iterator();
396         }
397
398         ///
399         inset_iterator inset_const_iterator_begin() const {
400                 return inset_iterator(paragraphs.begin(), paragraphs.end());
401         }
402
403         ///
404         inset_iterator inset_const_iterator_end() const {
405                 return inset_iterator();
406         }
407
408         ///
409         ParIterator par_iterator_begin();
410         ///
411         ParIterator par_iterator_end();
412
413         ///
414         Inset * getInsetFromID(int id_arg) const;
415 };
416
417 bool operator==(Buffer::inset_iterator const & iter1,
418                 Buffer::inset_iterator const & iter2);
419
420 bool operator!=(Buffer::inset_iterator const & iter1,
421                 Buffer::inset_iterator const & iter2);
422 #endif