4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
17 #include "support/limited_stack.h"
20 #include "bufferparams.h"
22 #include "ParagraphList.h"
24 #include "iterators.h"
25 #include "errorlist.h"
27 #include <boost/scoped_ptr.hpp>
28 #include <boost/signals/signal0.hpp>
29 #include <boost/signals/signal1.hpp>
37 class ParConstIterator;
41 /** The buffer object.
42 This is the buffer object. It contains all the informations about
43 a document loaded into LyX. I am not sure if the class is complete or
44 minimal, probably not.
45 \author Lars Gullik Bjønnes
49 /// What type of log will \c getLogName() return?
51 latexlog, ///< LaTeX log
52 buildlog ///< Literate build log
57 \param b optional \c false by default
59 explicit Buffer(string const & file, bool b = false);
64 /** High-level interface to buffer functionality.
65 This function parses a command string and executes it
67 bool dispatch(string const & command, bool * result = 0);
69 /// Maybe we know the function already by number...
70 bool dispatch(int ac, string const & argument, bool * result = 0);
72 /// Load the autosaved file.
73 void loadAutoSaveFile();
76 /** Inserts a file into a document
77 \param par if != 0 insert the file.
78 \return \c false if method fails.
80 bool readFile(LyXLex &, string const & filename,
81 ParagraphList::iterator pit);
85 bool readFile(string const & filename);
87 bool readFile(string const & filename, ParagraphList::iterator pit);
89 /// read the header, returns number of unknown tokens
90 int readHeader(LyXLex & lex);
92 /** Reads a file without header.
93 \param par if != 0 insert the file.
94 \return \c false if file is not completely read.
96 bool readBody(LyXLex &, ParagraphList::iterator pit);
98 /// This parses a single token
99 int readParagraph(LyXLex &, string const & token,
100 ParagraphList & pars, ParagraphList::iterator & pit,
101 Paragraph::depth_type & depth);
104 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
105 LyXFont const &, string const &);
107 ParIterator getParFromID(int id) const;
108 /// do we have a paragraph with this id?
109 bool hasParWithID(int id) const;
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, 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;
129 Takes care of auto-save files and backup file if requested.
130 Returns \c true if the save is successful, \c false otherwise.
134 /// Write file. Returns \c false if unsuccesful.
135 bool writeFile(string const &) const;
138 void writeFileAscii(string const & , int);
140 void writeFileAscii(std::ostream &, int);
142 string const asciiParagraph(Paragraph const &, unsigned int linelen,
143 bool noparbreak = false) const;
144 /// Just a wrapper for the method below, first creating the ofstream.
145 void makeLaTeXFile(string const & filename,
146 string const & original_path,
147 LatexRunParams const &,
148 bool output_preamble = true,
149 bool output_body = true);
151 void makeLaTeXFile(std::ostream & os,
152 string const & original_path,
153 LatexRunParams const &,
154 bool output_preamble = true,
155 bool output_body = true);
157 void simpleDocBookOnePar(std::ostream &,
158 ParagraphList::iterator par, int & desc_on,
159 Paragraph::depth_type depth) const;
161 void simpleLinuxDocOnePar(std::ostream & os,
162 ParagraphList::iterator par,
163 Paragraph::depth_type depth) const;
165 void makeLinuxDocFile(string const & filename,
166 bool nice, bool only_body = false);
168 void makeDocBookFile(string const & filename,
169 bool nice, bool only_body = false);
170 /// returns the main language for the buffer (document)
171 Language const * getLanguage() const;
172 /// get l10n translated to the buffers language
173 string const B_(string const & l10n) const;
177 /// return true if the main lyx file does not need saving
178 bool isClean() const;
180 bool isBakClean() const;
182 bool isDepClean(string const & name) const;
184 /// mark the main lyx file as not needing saving
185 void markClean() const;
191 void markDepClean(string const & name);
194 void setUnnamed(bool flag = true);
199 /// Mark this buffer as dirty.
202 /// Returns the buffer's filename. It is always an absolute path.
203 string const & fileName() const;
205 /// Returns the the path where the buffer lives.
206 /// It is always an absolute path.
207 string const & filePath() const;
209 /** A transformed version of the file name, adequate for LaTeX.
210 \param no_path optional if \c true then the path is stripped.
212 string const getLatexName(bool no_path = true) const;
214 /// Get the name and type of the log.
215 std::pair<LogType, string> const getLogName() const;
217 /// Change name of buffer. Updates "read-only" flag.
218 void setFileName(string const & newfile);
220 /// Name of the document's parent
221 void setParentName(string const &);
223 /// Is buffer read-only?
224 bool isReadonly() const;
226 /// Set buffer read-only flag
227 void setReadonly(bool flag = true);
229 /// returns \c true if the buffer contains a LaTeX document
230 bool isLatex() const;
231 /// returns \c true if the buffer contains a LinuxDoc document
232 bool isLinuxDoc() const;
233 /// returns \c true if the buffer contains a DocBook document
234 bool isDocBook() const;
235 /** returns \c true if the buffer contains either a LinuxDoc
236 or DocBook document */
238 /// returns \c true if the buffer contains a Wed document
239 bool isLiterate() const;
241 /** Validate a buffer for LaTeX.
242 This validates the buffer, and returns a struct for use by
243 #makeLaTeX# and others. Its main use is to figure out what
244 commands and packages need to be included in the LaTeX file.
245 It (should) also check that the needed constructs are there
246 (i.e. that the \refs points to coresponding \labels). It
247 should perhaps inset "error" insets to help the user correct
250 void validate(LaTeXFeatures &) const;
252 /// return all bibkeys from buffer and its childs
253 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
255 void getLabelList(std::vector<string> &) const;
258 void changeLanguage(Language const * from, Language const * to);
261 void updateDocLang(Language const * nlang);
264 bool isMultiLingual();
266 /// Does this mean that this is buffer local?
267 limited_stack<Undo> undostack;
269 /// Does this mean that this is buffer local?
270 limited_stack<Undo> redostack;
275 /** The list of paragraphs.
276 This is a linked list of paragraph, this list holds the
277 whole contents of the document.
279 ParagraphList paragraphs;
281 /// LyX version control object.
284 /// Where to put temporary files.
287 /** If we are writing a nice LaTeX file or not.
288 While writing as LaTeX, tells whether we are
289 doing a 'nice' LaTeX file */
292 /// Used when typesetting to place errorboxes.
295 /// the author list for the document
296 AuthorList & authors();
299 bool do_writeFile(std::ostream & ofs) const;
301 typedef std::map<string, bool> DepClean;
303 /// need to regenerate .tex ?
307 mutable bool lyx_clean;
309 /// is autosave needed
310 mutable bool bak_clean;
312 /// is this a unnamed file (New...)
318 /// name of the file the buffer is associated with.
321 /// The path to the document file.
325 boost::scoped_ptr<Messages> messages_;
328 class inset_iterator {
330 typedef std::input_iterator_tag iterator_category;
331 typedef InsetOld value_type;
332 typedef ptrdiff_t difference_type;
333 typedef InsetOld * pointer;
334 typedef InsetOld & reference;
335 typedef ParagraphList::iterator base_type;
340 inset_iterator(base_type p, base_type e);
342 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
345 inset_iterator & operator++();
347 inset_iterator operator++(int);
349 reference operator*();
351 pointer operator->();
354 ParagraphList::iterator getPar() const;
356 lyx::pos_type getPos() const;
359 bool operator==(inset_iterator const & iter1,
360 inset_iterator const & iter2);
365 ParagraphList::iterator pit;
367 ParagraphList::iterator pend;
369 InsetList::iterator it;
372 /// return an iterator to all *top-level* insets in the buffer
373 inset_iterator inset_iterator_begin();
375 /// return the end of all *top-level* insets in the buffer
376 inset_iterator inset_iterator_end();
378 /// return a const iterator to all *top-level* insets in the buffer
379 inset_iterator inset_const_iterator_begin() const;
381 /// return the const end of all *top-level* insets in the buffer
382 inset_iterator inset_const_iterator_end() const;
385 ParIterator par_iterator_begin();
387 ParConstIterator par_iterator_begin() const;
389 ParIterator par_iterator_end();
391 ParConstIterator par_iterator_end() const;
394 InsetOld * getInsetFromID(int id_arg) const;
397 bool operator==(Buffer::inset_iterator const & iter1,
398 Buffer::inset_iterator const & iter2);
400 bool operator!=(Buffer::inset_iterator const & iter1,
401 Buffer::inset_iterator const & iter2);