]> git.lyx.org Git - lyx.git/blob - src/buffer.h
Enable convertDefault.sh to run even if its executable bit is not set.
[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 "LString.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.h"
23 #include "author.h"
24 #include "iterators.h"
25 #include "errorlist.h"
26
27 #include <boost/scoped_ptr.hpp>
28 #include <boost/signals/signal0.hpp>
29 #include <boost/signals/signal1.hpp>
30
31 class LyXRC;
32 class TeXErrors;
33 class LaTeXFeatures;
34 class LatexRunParams;
35 class Language;
36 class ParIterator;
37 class ParConstIterator;
38 class Messages;
39
40
41 /** The buffer object.
42   This is the buffer object. It contains all the informations about
43   a document loaded into LyX. I am not sure if the class is complete or
44   minimal, probably not.
45   \author Lars Gullik Bjønnes
46   */
47 class Buffer {
48 public:
49         /// What type of log will \c getLogName() return?
50         enum LogType {
51                 latexlog, ///< LaTeX log
52                 buildlog  ///< Literate build log
53         };
54
55         /** Constructor
56             \param file
57             \param b  optional \c false by default
58         */
59         explicit Buffer(string const & file, bool b = false);
60
61         /// Destructor
62         ~Buffer();
63
64         /** High-level interface to buffer functionality.
65             This function parses a command string and executes it
66         */
67         bool dispatch(string const & command, bool * result = 0);
68
69         /// Maybe we know the function already by number...
70         bool dispatch(int ac, string const & argument, bool * result = 0);
71
72         /// Load the autosaved file.
73         void loadAutoSaveFile();
74
75         /** Inserts a file into a document
76             \param par if != 0 insert the file.
77             \return \c false if method fails.
78         */
79         bool readFile(LyXLex &, string const &, ParagraphList::iterator pit);
80
81         // FIXME: it's very silly to pass a lex in here
82         /// load a new file
83         bool readFile(string const &);
84
85         /// read the header, returns number of unknown tokens
86         int readHeader(LyXLex & lex);
87
88         /** Reads a file without header.
89             \param par if != 0 insert the file.
90             \return \c false if file is not completely read.
91         */
92         bool readBody(LyXLex &, ParagraphList::iterator pit);
93
94         /// This parses a single token
95         int readParagraph(LyXLex &, string const & token,
96                       ParagraphList & pars, ParagraphList::iterator & pit,
97                       Paragraph::depth_type & depth);
98
99         ///
100         void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
101                                  LyXFont const &, string const &);
102         ///
103         ParIterator getParFromID(int id) const;
104         /// do we have a paragraph with this id?
105         bool hasParWithID(int id) const;
106
107 public:
108         /// This signal is emitted when a parsing error shows up.
109         boost::signal1<void, ErrorItem> error;
110         /// This signal is emitted when some message shows up.
111         boost::signal1<void, string> message;
112         /// This signal is emitted when the buffer busy status change.
113         boost::signal1<void, bool> busy;
114         /// This signal is emitted when the buffer readonly status change.
115         boost::signal1<void, bool> readonly;
116         /// Update window titles of all users.
117         boost::signal0<void> updateTitles;
118         /// Reset autosave timers for all users.
119         boost::signal0<void> resetAutosaveTimers;
120         /// This signal is emitting if the buffer is being closed.
121         boost::signal0<void> closing;
122
123
124         /** Save file.
125             Takes care of auto-save files and backup file if requested.
126             Returns \c true if the save is successful, \c false otherwise.
127         */
128         bool save() const;
129
130         /// Write file. Returns \c false if unsuccesful.
131         bool writeFile(string const &) const;
132
133         ///
134         void writeFileAscii(string const & , int);
135         ///
136         void writeFileAscii(std::ostream &, int);
137         ///
138         string const asciiParagraph(Paragraph const &, unsigned int linelen,
139                                     bool noparbreak = false) const;
140         /// Just a wrapper for the method below, first creating the ofstream.
141         void makeLaTeXFile(string const & filename,
142                            string const & original_path,
143                            LatexRunParams const &,
144                            bool output_preamble = true,
145                            bool output_body = true);
146         ///
147         void makeLaTeXFile(std::ostream & os,
148                            string const & original_path,
149                            LatexRunParams const &,
150                            bool output_preamble = true,
151                            bool output_body = true);
152         ///
153         void simpleDocBookOnePar(std::ostream &,
154                                  ParagraphList::iterator par, int & desc_on,
155                                  Paragraph::depth_type depth) const;
156         ///
157         void simpleLinuxDocOnePar(std::ostream & os,
158                ParagraphList::iterator par,
159                                  Paragraph::depth_type depth) const;
160         ///
161         void makeLinuxDocFile(string const & filename,
162                               bool nice, bool only_body = false);
163         ///
164         void makeDocBookFile(string const & filename,
165                              bool nice, bool only_body = false);
166         /// returns the main language for the buffer (document)
167         Language const * getLanguage() const;
168         /// get l10n translated to the buffers language
169         string const B_(string const & l10n) const;
170
171         ///
172         int runChktex();
173         /// return true if the main lyx file does not need saving
174         bool isClean() const;
175         ///
176         bool isBakClean() const;
177         ///
178         bool isDepClean(string const & name) const;
179
180         /// mark the main lyx file as not needing saving
181         void markClean() const;
182
183         ///
184         void markBakClean();
185
186         ///
187         void markDepClean(string const & name);
188
189         ///
190         void setUnnamed(bool flag = true);
191
192         ///
193         bool isUnnamed();
194
195         /// Mark this buffer as dirty.
196         void markDirty();
197
198         /// Returns the buffer's filename. It is always an absolute path.
199         string const & fileName() const;
200
201         /// Returns the the path where the buffer lives.
202         /// It is always an absolute path.
203         string const & filePath() const;
204
205         /** A transformed version of the file name, adequate for LaTeX.
206             \param no_path optional if \c true then the path is stripped.
207         */
208         string const getLatexName(bool no_path = true) const;
209
210         /// Get the name and type of the log.
211         std::pair<LogType, string> const getLogName() const;
212
213         /// Change name of buffer. Updates "read-only" flag.
214         void setFileName(string const & newfile);
215
216         /// Name of the document's parent
217         void setParentName(string const &);
218
219         /// Is buffer read-only?
220         bool isReadonly() const;
221
222         /// Set buffer read-only flag
223         void setReadonly(bool flag = true);
224
225         /// returns \c true if the buffer contains a LaTeX document
226         bool isLatex() const;
227         /// returns \c true if the buffer contains a LinuxDoc document
228         bool isLinuxDoc() const;
229         /// returns \c true if the buffer contains a DocBook document
230         bool isDocBook() const;
231         /** returns \c true if the buffer contains either a LinuxDoc
232             or DocBook document */
233         bool isSGML() const;
234         /// returns \c true if the buffer contains a Wed document
235         bool isLiterate() const;
236
237         /** Validate a buffer for LaTeX.
238             This validates the buffer, and returns a struct for use by
239             #makeLaTeX# and others. Its main use is to figure out what
240             commands and packages need to be included in the LaTeX file.
241             It (should) also check that the needed constructs are there
242             (i.e. that the \refs points to coresponding \labels). It
243             should perhaps inset "error" insets to help the user correct
244             obvious mistakes.
245         */
246         void validate(LaTeXFeatures &) const;
247
248         /// return all bibkeys from buffer and its childs
249         void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
250         ///
251         void getLabelList(std::vector<string> &) const;
252
253         ///
254         void changeLanguage(Language const * from, Language const * to);
255
256         ///
257         void updateDocLang(Language const * nlang);
258
259         ///
260         bool isMultiLingual();
261
262         /// Does this mean that this is buffer local?
263         limited_stack<Undo> undostack;
264
265         /// Does this mean that this is buffer local?
266         limited_stack<Undo> redostack;
267
268         ///
269         BufferParams params;
270
271         /** The list of paragraphs.
272             This is a linked list of paragraph, this list holds the
273             whole contents of the document.
274          */
275         ParagraphList paragraphs;
276
277         /// LyX version control object.
278         LyXVC lyxvc;
279
280         /// Where to put temporary files.
281         string tmppath;
282
283         /** If we are writing a nice LaTeX file or not.
284             While writing as LaTeX, tells whether we are
285             doing a 'nice' LaTeX file */
286         bool niceFile;
287
288         /// Used when typesetting to place errorboxes.
289         TexRow texrow;
290
291         /// the author list for the document
292         AuthorList & authors();
293
294 private:
295         bool do_writeFile(std::ostream & ofs) const;
296
297         typedef std::map<string, bool> DepClean;
298
299         /// need to regenerate .tex ?
300         DepClean dep_clean_;
301
302         /// is save needed
303         mutable bool lyx_clean;
304
305         /// is autosave needed
306         mutable bool bak_clean;
307
308         /// is this a unnamed file (New...)
309         bool unnamed;
310
311         /// buffer is r/o
312         bool read_only;
313
314         /// name of the file the buffer is associated with.
315         string filename_;
316
317         /// The path to the document file.
318         string filepath_;
319
320         ///
321         boost::scoped_ptr<Messages> messages_;
322 public:
323         ///
324         class inset_iterator {
325         public:
326                 typedef std::input_iterator_tag iterator_category;
327                 typedef InsetOld value_type;
328                 typedef ptrdiff_t difference_type;
329                 typedef InsetOld * pointer;
330                 typedef InsetOld & reference;
331                 typedef ParagraphList::iterator base_type;
332
333                 ///
334                 inset_iterator();
335                 ///
336                 inset_iterator(base_type p, base_type e);
337                 ///
338                 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
339
340                 /// prefix ++
341                 inset_iterator & operator++();
342                 /// postfix ++
343                 inset_iterator operator++(int);
344                 ///
345                 reference operator*();
346                 ///
347                 pointer operator->();
348
349                 ///
350                 ParagraphList::iterator getPar() const;
351                 ///
352                 lyx::pos_type getPos() const;
353                 ///
354                 friend
355                 bool operator==(inset_iterator const & iter1,
356                                 inset_iterator const & iter2);
357         private:
358                 ///
359                 void setParagraph();
360                 ///
361                 ParagraphList::iterator pit;
362                 ///
363                 ParagraphList::iterator pend;
364                 ///
365                 InsetList::iterator it;
366         };
367
368         /// return an iterator to all *top-level* insets in the buffer
369         inset_iterator inset_iterator_begin();
370
371         /// return the end of all *top-level* insets in the buffer
372         inset_iterator inset_iterator_end();
373
374         /// return a const iterator to all *top-level* insets in the buffer
375         inset_iterator inset_const_iterator_begin() const;
376
377         /// return the const end of all *top-level* insets in the buffer
378         inset_iterator inset_const_iterator_end() const;
379
380         ///
381         ParIterator par_iterator_begin();
382         ///
383         ParConstIterator par_iterator_begin() const;
384         ///
385         ParIterator par_iterator_end();
386         ///
387         ParConstIterator par_iterator_end() const;
388
389         ///
390         InsetOld * getInsetFromID(int id_arg) const;
391 };
392
393 bool operator==(Buffer::inset_iterator const & iter1,
394                 Buffer::inset_iterator const & iter2);
395
396 bool operator!=(Buffer::inset_iterator const & iter1,
397                 Buffer::inset_iterator const & iter2);
398 #endif