]> git.lyx.org Git - lyx.git/blob - src/buffer.h
revert Buffer LyxText->InsetText commit
[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 #include "ParagraphList_fwd.h"
17
18 #include "support/limited_stack.h"
19 #include "support/types.h"
20
21 #include <boost/scoped_ptr.hpp>
22 #include <boost/signals/signal0.hpp>
23 #include <boost/signals/signal1.hpp>
24
25 #include <iosfwd>
26 #include <map>
27 #include <utility>
28 #include <vector>
29
30
31 class BufferParams;
32 class ErrorItem;
33 class FuncRequest;
34 class InsetBase;
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 ParIterator;
45 class PosIterator;
46 class ParConstIterator;
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, 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 &);
99
100         ///
101         void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
102                                  LyXFont const &, std::string const &);
103         ///
104         ParIterator getParFromID(int id) const;
105         /// do we have a paragraph with this id?
106         bool hasParWithID(int id) const;
107
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, std::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(std::string const &) const;
132
133         ///
134         void writeFileAscii(std::string const &, OutputParams const &);
135         ///
136         void writeFileAscii(std::ostream &, OutputParams const &);
137         /// Just a wrapper for the method below, first creating the ofstream.
138         void makeLaTeXFile(std::string const & filename,
139                            std::string const & original_path,
140                            OutputParams const &,
141                            bool output_preamble = true,
142                            bool output_body = true);
143         ///
144         void makeLaTeXFile(std::ostream & os,
145                            std::string const & original_path,
146                            OutputParams const &,
147                            bool output_preamble = true,
148                            bool output_body = true);
149         ///
150         void makeLinuxDocFile(std::string const & filename,
151                               OutputParams const & runparams_in,
152                               bool only_body = false);
153         ///
154         void makeDocBookFile(std::string const & filename,
155                              OutputParams const & runparams_in,
156                              bool only_body = false);
157         /// returns the main language for the buffer (document)
158         Language const * getLanguage() const;
159         /// get l10n translated to the buffers language
160         std::string const B_(std::string const & l10n) const;
161
162         ///
163         int runChktex();
164         /// return true if the main lyx file does not need saving
165         bool isClean() const;
166         ///
167         bool isBakClean() const;
168         ///
169         bool isDepClean(std::string const & name) const;
170
171         /// mark the main lyx file as not needing saving
172         void markClean() const;
173
174         ///
175         void markBakClean();
176
177         ///
178         void markDepClean(std::string const & name);
179
180         ///
181         void setUnnamed(bool flag = true);
182
183         ///
184         bool isUnnamed() const;
185
186         /// Mark this buffer as dirty.
187         void markDirty();
188
189         /// Returns the buffer's filename. It is always an absolute path.
190         std::string const & fileName() const;
191
192         /// Returns the the path where the buffer lives.
193         /// It is always an absolute path.
194         std::string const & filePath() const;
195
196         /** A transformed version of the file name, adequate for LaTeX.
197             \param no_path optional if \c true then the path is stripped.
198         */
199         std::string const getLatexName(bool no_path = true) const;
200
201         /// Get the name and type of the log.
202         std::pair<LogType, std::string> const getLogName() const;
203
204         /// Change name of buffer. Updates "read-only" flag.
205         void setFileName(std::string const & newfile);
206
207         /// Name of the document's parent
208         void setParentName(std::string const &);
209
210         /// Is buffer read-only?
211         bool isReadonly() const;
212
213         /// Set buffer read-only flag
214         void setReadonly(bool flag = true);
215
216         /// returns \c true if the buffer contains a LaTeX document
217         bool isLatex() const;
218         /// returns \c true if the buffer contains a LinuxDoc document
219         bool isLinuxDoc() const;
220         /// returns \c true if the buffer contains a DocBook document
221         bool isDocBook() const;
222         /** returns \c true if the buffer contains either a LinuxDoc
223             or DocBook document */
224         bool isSGML() const;
225         /// returns \c true if the buffer contains a Wed document
226         bool isLiterate() const;
227
228         /** Validate a buffer for LaTeX.
229             This validates the buffer, and returns a struct for use by
230             #makeLaTeX# and others. Its main use is to figure out what
231             commands and packages need to be included in the LaTeX file.
232             It (should) also check that the needed constructs are there
233             (i.e. that the \refs points to coresponding \labels). It
234             should perhaps inset "error" insets to help the user correct
235             obvious mistakes.
236         */
237         void validate(LaTeXFeatures &) const;
238
239         /// return all bibkeys from buffer and its childs
240         void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
241         ///
242         void getLabelList(std::vector<std::string> &) const;
243
244         ///
245         void changeLanguage(Language const * from, Language const * to);
246
247         ///
248         void updateDocLang(Language const * nlang);
249
250         ///
251         bool isMultiLingual() const;
252
253         /// Does this mean that this is buffer local?
254         limited_stack<Undo> & undostack();
255         limited_stack<Undo> const & undostack() const;
256
257         /// Does this mean that this is buffer local?
258         limited_stack<Undo> & redostack();
259         limited_stack<Undo> const & redostack() const;
260
261         ///
262         BufferParams & params();
263         BufferParams const & params() const;
264
265         /** The list of paragraphs.
266             This is a linked list of paragraph, this list holds the
267             whole contents of the document.
268          */
269         ParagraphList & paragraphs();
270         ParagraphList const & paragraphs() const;
271
272         /// LyX version control object.
273         LyXVC & lyxvc();
274         LyXVC const & lyxvc() const;
275
276         /// Where to put temporary files.
277         std::string const & temppath() const;
278
279         /// Used when typesetting to place errorboxes.
280         TexRow & texrow();
281         TexRow const & texrow() const;
282
283         class inset_iterator {
284         public:
285                 typedef std::input_iterator_tag iterator_category;
286                 typedef InsetBase value_type;
287                 typedef ptrdiff_t difference_type;
288                 typedef InsetBase * pointer;
289                 typedef InsetBase & reference;
290                 typedef ParagraphList::iterator base_type;
291
292                 ///
293                 inset_iterator();
294                 ///
295                 inset_iterator(base_type p, base_type e);
296                 ///
297                 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
298
299                 /// prefix ++
300                 inset_iterator & operator++();
301                 /// postfix ++
302                 inset_iterator operator++(int);
303                 ///
304                 reference operator*();
305                 ///
306                 pointer operator->();
307
308                 ///
309                 ParagraphList::iterator getPar() const;
310                 ///
311                 lyx::pos_type getPos() const;
312                 ///
313                 friend
314                 bool operator==(inset_iterator const & iter1,
315                                 inset_iterator const & iter2);
316         private:
317                 ///
318                 void setParagraph();
319                 ///
320                 ParagraphList::iterator pit;
321                 ///
322                 ParagraphList::iterator pend;
323                 ///
324                 InsetList::iterator it;
325         };
326
327         /// return an iterator to all *top-level* insets in the buffer
328         inset_iterator inset_iterator_begin();
329
330         /// return the end of all *top-level* insets in the buffer
331         inset_iterator inset_iterator_end();
332
333         /// return a const iterator to all *top-level* insets in the buffer
334         inset_iterator inset_const_iterator_begin() const;
335
336         /// return the const end of all *top-level* insets in the buffer
337         inset_iterator inset_const_iterator_end() const;
338
339         ///
340         PosIterator pos_iterator_begin();
341         ///
342         PosIterator pos_iterator_end();
343         ///
344         ParIterator par_iterator_begin();
345         ///
346         ParConstIterator par_iterator_begin() const;
347         ///
348         ParIterator par_iterator_end();
349         ///
350         ParConstIterator par_iterator_end() const;
351
352         /** \returns true only when the file is fully loaded.
353          *  Used to prevent the premature generation of previews
354          *  and by the citation inset.
355          */
356         bool fully_loaded() const;
357         /// Set by buffer_funcs' newFile.
358         void fully_loaded(bool);
359
360         ///
361         LyXText & text() 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                       ParagraphList::iterator 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