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