]> git.lyx.org Git - lyx.git/blob - src/buffer.h
copy some code over to allow work to start on prefs
[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/shared_ptr.hpp>
31
32 class BufferView;
33 class LyXRC;
34 class TeXErrors;
35 class LaTeXFeatures;
36 class Language;
37 class ParIterator;
38 class ParConstIterator;
39
40
41 ///
42 struct DEPCLEAN {
43         ///
44         bool clean;
45         ///
46         string master;
47         ///
48         DEPCLEAN * next;
49 };
50
51 /** The buffer object.
52   This is the buffer object. It contains all the informations about
53   a document loaded into LyX. I am not sure if the class is complete or
54   minimal, probably not.
55   \author Lars Gullik Bjønnes
56   */
57 class Buffer {
58 public:
59         /// What type of log will \c getLogName() return?
60         enum LogType {
61                 latexlog, ///< LaTeX log
62                 buildlog  ///< Literate build log
63         };
64
65         /** Constructor
66             \param file
67             \param b  optional \c false by default
68         */
69         explicit Buffer(string const & file, bool b = false);
70
71         /// Destructor
72         ~Buffer();
73
74         /** High-level interface to buffer functionality.
75             This function parses a command string and executes it
76         */
77         bool dispatch(string const & command, bool * result = 0);
78
79         /// Maybe we know the function already by number...
80         bool dispatch(int ac, string const & argument, bool * result = 0);
81
82         ///
83         void resizeInsets(BufferView *);
84
85         /// Update window titles of all users.
86         void updateTitles() const;
87
88         /// Reset autosave timers for all users.
89         void resetAutosaveTimers() const;
90
91         /** Adds the BufferView to the users list.
92             Later this func will insert the \c BufferView into a real list,
93             not just setting a pointer.
94         */
95         void addUser(BufferView * u);
96
97         /** Removes the #BufferView# from the users list.
98             Since we only can have one at the moment, we just reset it.
99         */
100         void delUser(BufferView *);
101
102         ///
103         void redraw();
104
105         /// Load the autosaved file.
106         void loadAutoSaveFile();
107
108         /** Reads a file.
109             \param par if != 0 insert the file.
110             \return \c false if method fails.
111         */
112         bool readFile(LyXLex &, string const &, Paragraph * par = 0);
113
114         /** Reads a file without header.
115             \param par if != 0 insert the file.
116             \return \c false if file is not completely read.
117         */
118         bool readLyXformat2(LyXLex &, Paragraph * par = 0);
119
120         /// This parses a single LyXformat-Token.
121         bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
122                                         Paragraph *& return_par,
123                                         string const & token, int & pos,
124                                         Paragraph::depth_type & depth,
125                                         LyXFont &);
126         ///
127         void insertStringAsLines(Paragraph *&, lyx::pos_type &,
128                                  LyXFont const &, string const &) const;
129         ///
130         Paragraph * getParFromID(int id) const;
131 private:
132         /// Parse a single inset.
133         void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
134 public:
135         /** Save file.
136             Takes care of auto-save files and backup file if requested.
137             Returns \c true if the save is successful, \c false otherwise.
138         */
139         bool save() const;
140
141         /// Write file. Returns \c false if unsuccesful.
142         bool writeFile(string const &) const;
143
144         ///
145         void writeFileAscii(string const & , int);
146         ///
147         void writeFileAscii(std::ostream &, int);
148         ///
149         string const asciiParagraph(Paragraph const &, unsigned int linelen,
150                                     bool noparbreak = false) const;
151         /// Just a wrapper for the method below, first creating the ofstream.
152         void makeLaTeXFile(string const & filename,
153                            string const & original_path,
154                            bool nice,
155                            bool only_body = false,
156                            bool only_preamble = false);
157         ///
158         void makeLaTeXFile(std::ostream & os,
159                            string const & original_path,
160                            bool nice,
161                            bool only_body = false,
162                            bool only_preamble = false);
163         /** LaTeX all paragraphs from par to endpar.
164             \param \a endpar if == 0 then to the end
165         */
166         void latexParagraphs(std::ostream & os, Paragraph * par,
167                              Paragraph * endpar, TexRow & texrow, bool moving_arg = false) const;
168         ///
169         void simpleDocBookOnePar(std::ostream &,
170                                  Paragraph * par, int & desc_on,
171                                  Paragraph::depth_type depth) const ;
172         ///
173         void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
174                                   Paragraph::depth_type depth);
175         ///
176         void makeLinuxDocFile(string const & filename,
177                               bool nice, bool only_body = false);
178         ///
179         void makeDocBookFile(string const & filename,
180                              bool nice, bool only_body = false);
181         ///
182         void sgmlError(Paragraph * par, int pos, string const & message) const;
183
184         /// returns the main language for the buffer (document)
185         Language const * getLanguage() const;
186         ///
187         int runChktex();
188         /// return true if the main lyx file does not need saving
189         bool isClean() const;
190         ///
191         bool isBakClean() const;
192         ///
193         bool isDepClean(string const & name) const;
194
195         /// mark the main lyx file as not needing saving
196         void markClean() const;
197
198         ///
199         void markBakClean();
200
201         ///
202         void markDepClean(string const & name);
203
204         ///
205         void setUnnamed(bool flag = true);
206
207         ///
208         bool isUnnamed();
209
210         /// Mark this buffer as dirty.
211         void markDirty();
212
213         /// Returns the buffer's filename. It is always an absolute path.
214         string const & fileName() const;
215
216         /// Returns the the path where the buffer lives.
217         /// It is always an absolute path.
218         string const & filePath() const;
219
220         /** A transformed version of the file name, adequate for LaTeX.
221             \param no_path optional if \c true then the path is stripped.
222         */
223         string const getLatexName(bool no_path = true) const;
224
225         /// Get the name and type of the log.
226         std::pair<LogType, string> const getLogName() const;
227
228         /// Change name of buffer. Updates "read-only" flag.
229         void setFileName(string const & newfile);
230
231         /// Name of the document's parent
232         void setParentName(string const &);
233
234         /// Is buffer read-only?
235         bool isReadonly() const;
236
237         /// Set buffer read-only flag
238         void setReadonly(bool flag = true);
239
240         /// returns \c true if the buffer contains a LaTeX document
241         bool isLatex() const;
242         /// returns \c true if the buffer contains a LinuxDoc document
243         bool isLinuxDoc() const;
244         /// returns \c true if the buffer contains a DocBook document
245         bool isDocBook() const;
246         /** returns \c true if the buffer contains either a LinuxDoc
247             or DocBook document */
248         bool isSGML() const;
249         /// returns \c true if the buffer contains a Wed document
250         bool isLiterate() const;
251
252         /** Validate a buffer for LaTeX.
253             This validates the buffer, and returns a struct for use by
254             #makeLaTeX# and others. Its main use is to figure out what
255             commands and packages need to be included in the LaTeX file.
256             It (should) also check that the needed constructs are there
257             (i.e. that the \refs points to coresponding \labels). It
258             should perhaps inset "error" insets to help the user correct
259             obvious mistakes.
260         */
261         void validate(LaTeXFeatures &) const;
262
263         ///
264         std::vector<std::pair<string, string> > const getBibkeyList() const;
265         ///
266         std::vector<string> const getLabelList() const;
267
268         /** This will clearly have to change later. Later we can have more
269             than one user per buffer. */
270         BufferView * getUser() const;
271
272         ///
273         void changeLanguage(Language const * from, Language const * to);
274         ///
275         bool isMultiLingual();
276
277         /// Does this mean that this is buffer local?
278         limited_stack<boost::shared_ptr<Undo> > undostack;
279
280         /// Does this mean that this is buffer local?
281         limited_stack<boost::shared_ptr<Undo> > redostack;
282
283         ///
284         BufferParams params;
285
286         /** The list of paragraphs.
287             This is a linked list of paragraph, this list holds the
288             whole contents of the document.
289          */
290         ParagraphList paragraphs;
291
292         /// LyX version control object.
293         LyXVC lyxvc;
294
295         /// Where to put temporary files.
296         string tmppath;
297
298         /** If we are writing a nice LaTeX file or not.
299             While writing as LaTeX, tells whether we are
300             doing a 'nice' LaTeX file */
301         bool niceFile;
302
303         /// Used when typesetting to place errorboxes.
304         TexRow texrow;
305 private:
306         /// is save needed
307         mutable bool lyx_clean;
308
309         /// is autosave needed
310         mutable bool bak_clean;
311
312         /// is this a unnamed file (New...)
313         bool unnamed;
314
315         /// is regenerating #.tex# necessary
316         DEPCLEAN * dep_clean;
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