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