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