]> git.lyx.org Git - lyx.git/blob - src/buffer.h
Move depth_type to support/types.h.
[lyx.git] / src / buffer.h
1 // -*- C++ -*-
2 /**
3  * \file buffer.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef BUFFER_H
13 #define BUFFER_H
14
15 #include "support/std_string.h"
16 #include "undo.h"
17 #include "support/limited_stack.h"
18
19 #include "lyxvc.h"
20 #include "bufferparams.h"
21 #include "texrow.h"
22 #include "ParagraphList_fwd.h"
23 #include "errorlist.h"
24 #include "InsetList.h"
25
26 #include "support/types.h"
27
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/signals/signal0.hpp>
30 #include <boost/signals/signal1.hpp>
31
32 class AuthorList;
33 class LyXRC;
34 class LaTeXFeatures;
35 class LatexRunParams;
36 class Language;
37 class Messages;
38 class ParIterator;
39 class ParConstIterator;
40 class TeXErrors;
41
42
43 /** The buffer object.
44   This is the buffer object. It contains all the informations about
45   a document loaded into LyX. I am not sure if the class is complete or
46   minimal, probably not.
47   \author Lars Gullik Bjønnes
48   */
49 class Buffer {
50 public:
51         /// What type of log will \c getLogName() return?
52         enum LogType {
53                 latexlog, ///< LaTeX log
54                 buildlog  ///< Literate build log
55         };
56
57         /** Constructor
58             \param file
59             \param b  optional \c false by default
60         */
61         explicit Buffer(string const & file, bool b = false);
62
63         /// Destructor
64         ~Buffer();
65
66         /** High-level interface to buffer functionality.
67             This function parses a command string and executes it
68         */
69         bool dispatch(string const & command, bool * result = 0);
70
71         /// Maybe we know the function already by number...
72         bool dispatch(int ac, string const & argument, bool * result = 0);
73
74         /// Load the autosaved file.
75         void loadAutoSaveFile();
76
77 private:
78         /** Inserts a file into a document
79             \param par if != 0 insert the file.
80             \return \c false if method fails.
81         */
82         bool readFile(LyXLex &, string const & filename,
83                       ParagraphList::iterator pit);
84
85 public:
86         /// load a new file
87         bool readFile(string const & filename);
88
89         bool readFile(string const & filename, ParagraphList::iterator pit);
90
91         /// read the header, returns number of unknown tokens
92         int readHeader(LyXLex & lex);
93
94         /** Reads a file without header.
95             \param par if != 0 insert the file.
96             \return \c false if file is not completely read.
97         */
98         bool readBody(LyXLex &, ParagraphList::iterator pit);
99
100         /// This parses a single token
101         int readParagraph(LyXLex &, string const & token,
102                       ParagraphList & pars, ParagraphList::iterator & pit,
103                       lyx::depth_type & depth);
104
105         ///
106         void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
107                                  LyXFont const &, string const &);
108         ///
109         ParIterator getParFromID(int id) const;
110         /// do we have a paragraph with this id?
111         bool hasParWithID(int id) const;
112
113 public:
114         /// This signal is emitted when a parsing error shows up.
115         boost::signal1<void, ErrorItem> error;
116         /// This signal is emitted when some message shows up.
117         boost::signal1<void, string> message;
118         /// This signal is emitted when the buffer busy status change.
119         boost::signal1<void, bool> busy;
120         /// This signal is emitted when the buffer readonly status change.
121         boost::signal1<void, bool> readonly;
122         /// Update window titles of all users.
123         boost::signal0<void> updateTitles;
124         /// Reset autosave timers for all users.
125         boost::signal0<void> resetAutosaveTimers;
126         /// This signal is emitting if the buffer is being closed.
127         boost::signal0<void> closing;
128
129
130         /** Save file.
131             Takes care of auto-save files and backup file if requested.
132             Returns \c true if the save is successful, \c false otherwise.
133         */
134         bool save() const;
135
136         /// Write file. Returns \c false if unsuccesful.
137         bool writeFile(string const &) const;
138
139         ///
140         void writeFileAscii(string const & , int);
141         ///
142         void writeFileAscii(std::ostream &, int);
143         ///
144         string const asciiParagraph(Paragraph const &, unsigned int linelen,
145                                     bool noparbreak = false) const;
146         /// Just a wrapper for the method below, first creating the ofstream.
147         void makeLaTeXFile(string const & filename,
148                            string const & original_path,
149                            LatexRunParams const &,
150                            bool output_preamble = true,
151                            bool output_body = true);
152         ///
153         void makeLaTeXFile(std::ostream & os,
154                            string const & original_path,
155                            LatexRunParams const &,
156                            bool output_preamble = true,
157                            bool output_body = true);
158         ///
159         void simpleDocBookOnePar(std::ostream &,
160                                  ParagraphList::iterator par, int & desc_on,
161                                  lyx::depth_type depth) const;
162         ///
163         void simpleLinuxDocOnePar(std::ostream & os,
164                ParagraphList::iterator par,
165                                  lyx::depth_type depth) const;
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         /// returns the main language for the buffer (document)
173         Language const * getLanguage() const;
174         /// get l10n translated to the buffers language
175         string const B_(string const & l10n) const;
176
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         /// return all bibkeys from buffer and its childs
255         void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
256         ///
257         void getLabelList(std::vector<string> &) const;
258
259         ///
260         void changeLanguage(Language const * from, Language const * to);
261
262         ///
263         void updateDocLang(Language const * nlang);
264
265         ///
266         bool isMultiLingual();
267
268         /// Does this mean that this is buffer local?
269         limited_stack<Undo> undostack;
270
271         /// Does this mean that this is buffer local?
272         limited_stack<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         bool do_writeFile(std::ostream & ofs) const;
302
303         typedef std::map<string, bool> DepClean;
304
305         /// need to regenerate .tex ?
306         DepClean dep_clean_;
307
308         /// is save needed
309         mutable bool lyx_clean;
310
311         /// is autosave needed
312         mutable bool bak_clean;
313
314         /// is this a unnamed file (New...)
315         bool unnamed;
316
317         /// buffer is r/o
318         bool read_only;
319
320         /// name of the file the buffer is associated with.
321         string filename_;
322
323         /// The path to the document file.
324         string filepath_;
325
326         ///
327         boost::scoped_ptr<Messages> messages_;
328 public:
329         ///
330         class inset_iterator {
331         public:
332                 typedef std::input_iterator_tag iterator_category;
333                 typedef InsetOld value_type;
334                 typedef ptrdiff_t difference_type;
335                 typedef InsetOld * pointer;
336                 typedef InsetOld & reference;
337                 typedef ParagraphList::iterator base_type;
338
339                 ///
340                 inset_iterator();
341                 ///
342                 inset_iterator(base_type p, base_type e);
343                 ///
344                 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
345
346                 /// prefix ++
347                 inset_iterator & operator++();
348                 /// postfix ++
349                 inset_iterator operator++(int);
350                 ///
351                 reference operator*();
352                 ///
353                 pointer operator->();
354
355                 ///
356                 ParagraphList::iterator getPar() const;
357                 ///
358                 lyx::pos_type getPos() const;
359                 ///
360                 friend
361                 bool operator==(inset_iterator const & iter1,
362                                 inset_iterator const & iter2);
363         private:
364                 ///
365                 void setParagraph();
366                 ///
367                 ParagraphList::iterator pit;
368                 ///
369                 ParagraphList::iterator pend;
370                 ///
371                 InsetList::iterator it;
372         };
373
374         /// return an iterator to all *top-level* insets in the buffer
375         inset_iterator inset_iterator_begin();
376
377         /// return the end of all *top-level* insets in the buffer
378         inset_iterator inset_iterator_end();
379
380         /// return a const iterator to all *top-level* insets in the buffer
381         inset_iterator inset_const_iterator_begin() const;
382
383         /// return the const end of all *top-level* insets in the buffer
384         inset_iterator inset_const_iterator_end() const;
385
386         ///
387         ParIterator par_iterator_begin();
388         ///
389         ParConstIterator par_iterator_begin() const;
390         ///
391         ParIterator par_iterator_end();
392         ///
393         ParConstIterator par_iterator_end() const;
394
395         ///
396         InsetOld * getInsetFromID(int id_arg) const;
397 };
398
399 bool operator==(Buffer::inset_iterator const & iter1,
400                 Buffer::inset_iterator const & iter2);
401
402 bool operator!=(Buffer::inset_iterator const & iter1,
403                 Buffer::inset_iterator const & iter2);
404 #endif