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