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();
75 /** Inserts a file into a document
76 \param par if != 0 insert the file.
77 \return \c false if method fails.
79 bool readFile(LyXLex &, string const &, ParagraphList::iterator pit);
81 // FIXME: it's very silly to pass a lex in here
83 bool readFile(LyXLex &, string const &);
85 /// read the header, returns number of unknown tokens
86 int readHeader(LyXLex & lex);
88 /** Reads a file without header.
89 \param par if != 0 insert the file.
90 \return \c false if file is not completely read.
92 bool readBody(LyXLex &, ParagraphList::iterator pit);
94 /// This parses a single token
95 int readParagraph(LyXLex &, string const & token,
96 ParagraphList & pars, ParagraphList::iterator & pit,
97 Paragraph::depth_type & depth);
100 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
101 LyXFont const &, string const &);
103 ParIterator getParFromID(int id) const;
104 /// do we have a paragraph with this id?
105 bool hasParWithID(int id) const;
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, 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;
123 Takes care of auto-save files and backup file if requested.
124 Returns \c true if the save is successful, \c false otherwise.
128 /// Write file. Returns \c false if unsuccesful.
129 bool writeFile(string const &) const;
132 void writeFileAscii(string const & , int);
134 void writeFileAscii(std::ostream &, int);
136 string const asciiParagraph(Paragraph const &, unsigned int linelen,
137 bool noparbreak = false) const;
138 /// Just a wrapper for the method below, first creating the ofstream.
139 void makeLaTeXFile(string const & filename,
140 string const & original_path,
141 LatexRunParams const &,
142 bool only_body = false,
143 bool only_preamble = false);
145 void makeLaTeXFile(std::ostream & os,
146 string const & original_path,
147 LatexRunParams const &,
148 bool only_body = false,
149 bool only_preamble = false);
151 void simpleDocBookOnePar(std::ostream &,
152 ParagraphList::iterator par, int & desc_on,
153 Paragraph::depth_type depth) const;
155 void simpleLinuxDocOnePar(std::ostream & os,
156 ParagraphList::iterator par,
157 Paragraph::depth_type depth) const;
159 void makeLinuxDocFile(string const & filename,
160 bool nice, bool only_body = false);
162 void makeDocBookFile(string const & filename,
163 bool nice, bool only_body = false);
164 /// returns the main language for the buffer (document)
165 Language const * getLanguage() const;
166 /// get l10n translated to the buffers language
167 string const B_(string const & l10n) const;
171 /// return true if the main lyx file does not need saving
172 bool isClean() const;
174 bool isBakClean() const;
176 bool isDepClean(string const & name) const;
178 /// mark the main lyx file as not needing saving
179 void markClean() const;
185 void markDepClean(string const & name);
188 void setUnnamed(bool flag = true);
193 /// Mark this buffer as dirty.
196 /// Returns the buffer's filename. It is always an absolute path.
197 string const & fileName() const;
199 /// Returns the the path where the buffer lives.
200 /// It is always an absolute path.
201 string const & filePath() const;
203 /** A transformed version of the file name, adequate for LaTeX.
204 \param no_path optional if \c true then the path is stripped.
206 string const getLatexName(bool no_path = true) const;
208 /// Get the name and type of the log.
209 std::pair<LogType, string> const getLogName() const;
211 /// Change name of buffer. Updates "read-only" flag.
212 void setFileName(string const & newfile);
214 /// Name of the document's parent
215 void setParentName(string const &);
217 /// Is buffer read-only?
218 bool isReadonly() const;
220 /// Set buffer read-only flag
221 void setReadonly(bool flag = true);
223 /// returns \c true if the buffer contains a LaTeX document
224 bool isLatex() const;
225 /// returns \c true if the buffer contains a LinuxDoc document
226 bool isLinuxDoc() const;
227 /// returns \c true if the buffer contains a DocBook document
228 bool isDocBook() const;
229 /** returns \c true if the buffer contains either a LinuxDoc
230 or DocBook document */
232 /// returns \c true if the buffer contains a Wed document
233 bool isLiterate() const;
235 /** Validate a buffer for LaTeX.
236 This validates the buffer, and returns a struct for use by
237 #makeLaTeX# and others. Its main use is to figure out what
238 commands and packages need to be included in the LaTeX file.
239 It (should) also check that the needed constructs are there
240 (i.e. that the \refs points to coresponding \labels). It
241 should perhaps inset "error" insets to help the user correct
244 void validate(LaTeXFeatures &) const;
246 /// return all bibkeys from buffer and its childs
247 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
249 void getLabelList(std::vector<string> &) const;
252 void changeLanguage(Language const * from, Language const * to);
255 void updateDocLang(Language const * nlang);
258 bool isMultiLingual();
260 /// Does this mean that this is buffer local?
261 limited_stack<Undo> undostack;
263 /// Does this mean that this is buffer local?
264 limited_stack<Undo> redostack;
269 /** The list of paragraphs.
270 This is a linked list of paragraph, this list holds the
271 whole contents of the document.
273 ParagraphList paragraphs;
275 /// LyX version control object.
278 /// Where to put temporary files.
281 /** If we are writing a nice LaTeX file or not.
282 While writing as LaTeX, tells whether we are
283 doing a 'nice' LaTeX file */
286 /// Used when typesetting to place errorboxes.
289 /// the author list for the document
290 AuthorList & authors();
293 typedef std::map<string, bool> DepClean;
295 /// need to regenerate .tex ?
299 mutable bool lyx_clean;
301 /// is autosave needed
302 mutable bool bak_clean;
304 /// is this a unnamed file (New...)
310 /// name of the file the buffer is associated with.
313 /// The path to the document file.
316 /// Format number of buffer
319 boost::scoped_ptr<Messages> messages_;
322 class inset_iterator {
324 typedef std::input_iterator_tag iterator_category;
325 typedef Inset value_type;
326 typedef ptrdiff_t difference_type;
327 typedef Inset * pointer;
328 typedef Inset & reference;
329 typedef ParagraphList::iterator base_type;
334 inset_iterator(base_type p, base_type e);
336 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
339 inset_iterator & operator++();
341 inset_iterator operator++(int);
343 reference operator*();
345 pointer operator->();
348 ParagraphList::iterator getPar() const;
350 lyx::pos_type getPos() const;
353 bool operator==(inset_iterator const & iter1,
354 inset_iterator const & iter2);
359 ParagraphList::iterator pit;
361 ParagraphList::iterator pend;
363 InsetList::iterator it;
366 /// return an iterator to all *top-level* insets in the buffer
367 inset_iterator inset_iterator_begin();
369 /// return the end of all *top-level* insets in the buffer
370 inset_iterator inset_iterator_end();
372 /// return a const iterator to all *top-level* insets in the buffer
373 inset_iterator inset_const_iterator_begin() const;
375 /// return the const end of all *top-level* insets in the buffer
376 inset_iterator inset_const_iterator_end() const;
379 ParIterator par_iterator_begin();
381 ParConstIterator par_iterator_begin() const;
383 ParIterator par_iterator_end();
385 ParConstIterator par_iterator_end() const;
388 Inset * getInsetFromID(int id_arg) const;
391 bool operator==(Buffer::inset_iterator const & iter1,
392 Buffer::inset_iterator const & iter2);
394 bool operator!=(Buffer::inset_iterator const & iter1,
395 Buffer::inset_iterator const & iter2);