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