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;
120 /// This signal is emitting if the buffer is being closed.
121 boost::signal0<void> closing;
125 Takes care of auto-save files and backup file if requested.
126 Returns \c true if the save is successful, \c false otherwise.
130 /// Write file. Returns \c false if unsuccesful.
131 bool writeFile(string const &) const;
134 void writeFileAscii(string const & , int);
136 void writeFileAscii(std::ostream &, int);
138 string const asciiParagraph(Paragraph const &, unsigned int linelen,
139 bool noparbreak = false) const;
140 /// Just a wrapper for the method below, first creating the ofstream.
141 void makeLaTeXFile(string const & filename,
142 string const & original_path,
143 LatexRunParams const &,
144 bool only_body = false,
145 bool only_preamble = false);
147 void makeLaTeXFile(std::ostream & os,
148 string const & original_path,
149 LatexRunParams const &,
150 bool only_body = false,
151 bool only_preamble = false);
153 void simpleDocBookOnePar(std::ostream &,
154 ParagraphList::iterator par, int & desc_on,
155 Paragraph::depth_type depth) const;
157 void simpleLinuxDocOnePar(std::ostream & os,
158 ParagraphList::iterator par,
159 Paragraph::depth_type depth) const;
161 void makeLinuxDocFile(string const & filename,
162 bool nice, bool only_body = false);
164 void makeDocBookFile(string const & filename,
165 bool nice, bool only_body = false);
166 /// returns the main language for the buffer (document)
167 Language const * getLanguage() const;
168 /// get l10n translated to the buffers language
169 string const B_(string const & l10n) const;
173 /// return true if the main lyx file does not need saving
174 bool isClean() const;
176 bool isBakClean() const;
178 bool isDepClean(string const & name) const;
180 /// mark the main lyx file as not needing saving
181 void markClean() const;
187 void markDepClean(string const & name);
190 void setUnnamed(bool flag = true);
195 /// Mark this buffer as dirty.
198 /// Returns the buffer's filename. It is always an absolute path.
199 string const & fileName() const;
201 /// Returns the the path where the buffer lives.
202 /// It is always an absolute path.
203 string const & filePath() const;
205 /** A transformed version of the file name, adequate for LaTeX.
206 \param no_path optional if \c true then the path is stripped.
208 string const getLatexName(bool no_path = true) const;
210 /// Get the name and type of the log.
211 std::pair<LogType, string> const getLogName() const;
213 /// Change name of buffer. Updates "read-only" flag.
214 void setFileName(string const & newfile);
216 /// Name of the document's parent
217 void setParentName(string const &);
219 /// Is buffer read-only?
220 bool isReadonly() const;
222 /// Set buffer read-only flag
223 void setReadonly(bool flag = true);
225 /// returns \c true if the buffer contains a LaTeX document
226 bool isLatex() const;
227 /// returns \c true if the buffer contains a LinuxDoc document
228 bool isLinuxDoc() const;
229 /// returns \c true if the buffer contains a DocBook document
230 bool isDocBook() const;
231 /** returns \c true if the buffer contains either a LinuxDoc
232 or DocBook document */
234 /// returns \c true if the buffer contains a Wed document
235 bool isLiterate() const;
237 /** Validate a buffer for LaTeX.
238 This validates the buffer, and returns a struct for use by
239 #makeLaTeX# and others. Its main use is to figure out what
240 commands and packages need to be included in the LaTeX file.
241 It (should) also check that the needed constructs are there
242 (i.e. that the \refs points to coresponding \labels). It
243 should perhaps inset "error" insets to help the user correct
246 void validate(LaTeXFeatures &) const;
248 /// return all bibkeys from buffer and its childs
249 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
251 void getLabelList(std::vector<string> &) const;
254 void changeLanguage(Language const * from, Language const * to);
257 void updateDocLang(Language const * nlang);
260 bool isMultiLingual();
262 /// Does this mean that this is buffer local?
263 limited_stack<Undo> undostack;
265 /// Does this mean that this is buffer local?
266 limited_stack<Undo> redostack;
271 /** The list of paragraphs.
272 This is a linked list of paragraph, this list holds the
273 whole contents of the document.
275 ParagraphList paragraphs;
277 /// LyX version control object.
280 /// Where to put temporary files.
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 */
288 /// Used when typesetting to place errorboxes.
291 /// the author list for the document
292 AuthorList & authors();
295 typedef std::map<string, bool> DepClean;
297 /// need to regenerate .tex ?
301 mutable bool lyx_clean;
303 /// is autosave needed
304 mutable bool bak_clean;
306 /// is this a unnamed file (New...)
312 /// name of the file the buffer is associated with.
315 /// The path to the document file.
318 /// Format number of buffer
321 boost::scoped_ptr<Messages> messages_;
324 class inset_iterator {
326 typedef std::input_iterator_tag iterator_category;
327 typedef Inset value_type;
328 typedef ptrdiff_t difference_type;
329 typedef Inset * pointer;
330 typedef Inset & reference;
331 typedef ParagraphList::iterator base_type;
336 inset_iterator(base_type p, base_type e);
338 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
341 inset_iterator & operator++();
343 inset_iterator operator++(int);
345 reference operator*();
347 pointer operator->();
350 ParagraphList::iterator getPar() const;
352 lyx::pos_type getPos() const;
355 bool operator==(inset_iterator const & iter1,
356 inset_iterator const & iter2);
361 ParagraphList::iterator pit;
363 ParagraphList::iterator pend;
365 InsetList::iterator it;
368 /// return an iterator to all *top-level* insets in the buffer
369 inset_iterator inset_iterator_begin();
371 /// return the end of all *top-level* insets in the buffer
372 inset_iterator inset_iterator_end();
374 /// return a const iterator to all *top-level* insets in the buffer
375 inset_iterator inset_const_iterator_begin() const;
377 /// return the const end of all *top-level* insets in the buffer
378 inset_iterator inset_const_iterator_end() const;
381 ParIterator par_iterator_begin();
383 ParConstIterator par_iterator_begin() const;
385 ParIterator par_iterator_end();
387 ParConstIterator par_iterator_end() const;
390 Inset * getInsetFromID(int id_arg) const;
393 bool operator==(Buffer::inset_iterator const & iter1,
394 Buffer::inset_iterator const & iter2);
396 bool operator!=(Buffer::inset_iterator const & iter1,
397 Buffer::inset_iterator const & iter2);