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