]> git.lyx.org Git - lyx.git/blob - src/buffer.h
The Buffer::LyXText -> Buffer::InsetText patch
[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 InsetText;
36 class LyXFont;
37 class LyXLex;
38 class LyXRC;
39 class LyXText;
40 class LyXVC;
41 class LaTeXFeatures;
42 class Language;
43 class Messages;
44 class OutputParams;
45 class ParIterator;
46 class PosIterator;
47 class ParConstIterator;
48 class TeXErrors;
49 class TexRow;
50 class Undo;
51
52
53 /** The buffer object.
54   This is the buffer object. It contains all the informations about
55   a document loaded into LyX. I am not sure if the class is complete or
56   minimal, probably not.
57   \author Lars Gullik Bjønnes
58   */
59 class Buffer {
60 public:
61         /// What type of log will \c getLogName() return?
62         enum LogType {
63                 latexlog, ///< LaTeX log
64                 buildlog  ///< Literate build log
65         };
66
67         /** Constructor
68             \param file
69             \param b  optional \c false by default
70         */
71         explicit Buffer(std::string const & file, bool b = false);
72
73         /// Destructor
74         ~Buffer();
75
76         /** High-level interface to buffer functionality.
77             This function parses a command string and executes it
78         */
79         bool dispatch(std::string const & command, bool * result = 0);
80
81         /// Maybe we know the function already by number...
82         bool dispatch(FuncRequest const & func, bool * result = 0);
83
84         /// Load the autosaved file.
85         void loadAutoSaveFile();
86
87         /// load a new file
88         bool readFile(std::string const & filename);
89
90         bool readFile(std::string const & filename, ParagraphList::iterator pit);
91
92         /// read the header, returns number of unknown tokens
93         int readHeader(LyXLex & lex);
94
95         /** Reads a file without header.
96             \param par if != 0 insert the file.
97             \return \c false if file is not completely read.
98         */
99         bool readBody(LyXLex &);
100
101         ///
102         void insertStringAsLines(ParagraphList::iterator &, 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         /// Is buffer read-only?
212         bool isReadonly() const;
213
214         /// Set buffer read-only flag
215         void setReadonly(bool flag = true);
216
217         /// returns \c true if the buffer contains a LaTeX document
218         bool isLatex() const;
219         /// returns \c true if the buffer contains a LinuxDoc document
220         bool isLinuxDoc() const;
221         /// returns \c true if the buffer contains a DocBook document
222         bool isDocBook() const;
223         /** returns \c true if the buffer contains either a LinuxDoc
224             or DocBook document */
225         bool isSGML() const;
226         /// returns \c true if the buffer contains a Wed document
227         bool isLiterate() const;
228
229         /** Validate a buffer for LaTeX.
230             This validates the buffer, and returns a struct for use by
231             #makeLaTeX# and others. Its main use is to figure out what
232             commands and packages need to be included in the LaTeX file.
233             It (should) also check that the needed constructs are there
234             (i.e. that the \refs points to coresponding \labels). It
235             should perhaps inset "error" insets to help the user correct
236             obvious mistakes.
237         */
238         void validate(LaTeXFeatures &) const;
239
240         /// return all bibkeys from buffer and its childs
241         void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
242         ///
243         void getLabelList(std::vector<std::string> &) const;
244
245         ///
246         void changeLanguage(Language const * from, Language const * to);
247
248         ///
249         void updateDocLang(Language const * nlang);
250
251         ///
252         bool isMultiLingual() const;
253
254         /// Does this mean that this is buffer local?
255         limited_stack<Undo> & undostack();
256         limited_stack<Undo> const & undostack() const;
257
258         /// Does this mean that this is buffer local?
259         limited_stack<Undo> & redostack();
260         limited_stack<Undo> const & redostack() const;
261
262         ///
263         BufferParams & params();
264         BufferParams const & params() const;
265
266         /** The list of paragraphs.
267             This is a linked list of paragraph, this list holds the
268             whole contents of the document.
269          */
270         ParagraphList & paragraphs();
271         ParagraphList const & paragraphs() const;
272
273         /// LyX version control object.
274         LyXVC & lyxvc();
275         LyXVC const & lyxvc() const;
276
277         /// Where to put temporary files.
278         std::string const & temppath() const;
279
280         /// Used when typesetting to place errorboxes.
281         TexRow & texrow();
282         TexRow const & texrow() const;
283
284         class inset_iterator {
285         public:
286                 typedef std::input_iterator_tag iterator_category;
287                 typedef InsetBase value_type;
288                 typedef ptrdiff_t difference_type;
289                 typedef InsetBase * pointer;
290                 typedef InsetBase & reference;
291                 typedef ParagraphList::iterator base_type;
292
293                 ///
294                 inset_iterator();
295                 ///
296                 inset_iterator(base_type p, base_type e);
297                 ///
298                 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
299
300                 /// prefix ++
301                 inset_iterator & operator++();
302                 /// postfix ++
303                 inset_iterator operator++(int);
304                 ///
305                 reference operator*();
306                 ///
307                 pointer operator->();
308
309                 ///
310                 ParagraphList::iterator getPar() const;
311                 ///
312                 lyx::pos_type getPos() const;
313                 ///
314                 friend
315                 bool operator==(inset_iterator const & iter1,
316                                 inset_iterator const & iter2);
317         private:
318                 ///
319                 void setParagraph();
320                 ///
321                 ParagraphList::iterator pit;
322                 ///
323                 ParagraphList::iterator pend;
324                 ///
325                 InsetList::iterator it;
326         };
327
328         /// return an iterator to all *top-level* insets in the buffer
329         inset_iterator inset_iterator_begin();
330
331         /// return the end of all *top-level* insets in the buffer
332         inset_iterator inset_iterator_end();
333
334         /// return a const iterator to all *top-level* insets in the buffer
335         inset_iterator inset_const_iterator_begin() const;
336
337         /// return the const end of all *top-level* insets in the buffer
338         inset_iterator inset_const_iterator_end() const;
339
340         ///
341         PosIterator pos_iterator_begin();
342         ///
343         PosIterator pos_iterator_end();
344         ///
345         ParIterator par_iterator_begin();
346         ///
347         ParConstIterator par_iterator_begin() const;
348         ///
349         ParIterator par_iterator_end();
350         ///
351         ParConstIterator par_iterator_end() const;
352
353         /** \returns true only when the file is fully loaded.
354          *  Used to prevent the premature generation of previews
355          *  and by the citation inset.
356          */
357         bool fully_loaded() const;
358         /// Set by buffer_funcs' newFile.
359         void fully_loaded(bool);
360
361         ///
362         LyXText & text() const;
363         ///
364         InsetBase & inset() const;
365
366 private:
367         /** Inserts a file into a document
368             \param par if != 0 insert the file.
369             \return \c false if method fails.
370         */
371         bool readFile(LyXLex &, std::string const & filename,
372                       ParagraphList::iterator pit);
373
374         bool do_writeFile(std::ostream & ofs) const;
375
376         /// Use the Pimpl idiom to hide the internals.
377         class Impl;
378         /// The pointer never changes although *pimpl_'s contents may.
379         boost::scoped_ptr<Impl> const pimpl_;
380 };
381
382 bool operator==(Buffer::inset_iterator const & iter1,
383                 Buffer::inset_iterator const & iter2);
384
385 bool operator!=(Buffer::inset_iterator const & iter1,
386                 Buffer::inset_iterator const & iter2);
387
388 #endif