]> git.lyx.org Git - lyx.git/blob - src/buffer.h
another fix
[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 &, 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         string const getIncludeonlyList(char delim = ',');
270         ///
271         std::vector<std::pair<string, string> > const getBibkeyList() const;
272         ///
273         std::vector<string> const getLabelList() const;
274
275         /** This will clearly have to change later. Later we can have more
276             than one user per buffer. */
277         BufferView * getUser() const;
278
279         ///
280         void changeLanguage(Language const * from, Language const * to);
281         ///
282         bool isMultiLingual();
283
284         /// Does this mean that this is buffer local?
285         limited_stack<boost::shared_ptr<Undo> > undostack;
286
287         /// Does this mean that this is buffer local?
288         limited_stack<boost::shared_ptr<Undo> > redostack;
289
290         ///
291         BufferParams params;
292
293         /** The list of paragraphs.
294             This is a linked list of paragraph, this list holds the
295             whole contents of the document.
296          */
297         ParagraphList paragraphs;
298
299         /// LyX version control object.
300         LyXVC lyxvc;
301
302         /// Where to put temporary files.
303         string tmppath;
304
305         /** If we are writing a nice LaTeX file or not.
306             While writing as LaTeX, tells whether we are
307             doing a 'nice' LaTeX file */
308         bool niceFile;
309
310         /// Used when typesetting to place errorboxes.
311         TexRow texrow;
312 private:
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         /// is regenerating #.tex# necessary
323         DEPCLEAN * dep_clean;
324
325         /// buffer is r/o
326         bool read_only;
327
328         /// name of the file the buffer is associated with.
329         string filename_;
330
331         /// The path to the document file.
332         string filepath_;
333
334         /// Format number of buffer
335         int file_format;
336         /** A list of views using this buffer.
337             Why not keep a list of the BufferViews that use this buffer?
338
339             At least then we don't have to do a lot of magic like:
340             #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
341             of the buffers in the list of users to do a #updateLayoutChoice#.
342         */
343         BufferView * users;
344 public:
345         ///
346         class inset_iterator {
347         public:
348                 typedef std::input_iterator_tag iterator_category;
349                 typedef Inset value_type;
350                 typedef ptrdiff_t difference_type;
351                 typedef Inset * pointer;
352                 typedef Inset & reference;
353                 typedef ParagraphList::iterator base_type;
354
355                 ///
356                 inset_iterator();
357                 ///
358                 inset_iterator(base_type p, base_type e);
359                 ///
360                 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
361
362                 /// prefix ++
363                 inset_iterator & operator++();
364                 /// postfix ++
365                 inset_iterator operator++(int);
366                 ///
367                 reference operator*();
368                 ///
369                 pointer operator->();
370
371                 ///
372                 Paragraph * getPar();
373                 ///
374                 lyx::pos_type getPos() const;
375                 ///
376                 friend
377                 bool operator==(inset_iterator const & iter1,
378                                 inset_iterator const & iter2);
379         private:
380                 ///
381                 void setParagraph();
382                 ///
383                 ParagraphList::iterator pit;
384                 ///
385                 ParagraphList::iterator pend;
386                 ///
387                 InsetList::iterator it;
388         };
389
390         ///
391         inset_iterator inset_iterator_begin() {
392                 return inset_iterator(paragraphs.begin(), paragraphs.end());
393         }
394
395         ///
396         inset_iterator inset_iterator_end() {
397                 return inset_iterator();
398         }
399
400         ///
401         inset_iterator inset_const_iterator_begin() const {
402                 return inset_iterator(paragraphs.begin(), paragraphs.end());
403         }
404
405         ///
406         inset_iterator inset_const_iterator_end() const {
407                 return inset_iterator();
408         }
409
410         ///
411         ParIterator par_iterator_begin();
412         ///
413         ParIterator par_iterator_end();
414
415         ///
416         Inset * getInsetFromID(int id_arg) const;
417 };
418
419 bool operator==(Buffer::inset_iterator const & iter1,
420                 Buffer::inset_iterator const & iter2);
421
422 bool operator!=(Buffer::inset_iterator const & iter1,
423                 Buffer::inset_iterator const & iter2);
424 #endif