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