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