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