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