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.
15 #include "support/std_string.h"
17 #include "support/limited_stack.h"
20 #include "bufferparams.h"
22 #include "ParagraphList.h"
23 #include "errorlist.h"
25 #include <boost/scoped_ptr.hpp>
26 #include <boost/signals/signal0.hpp>
27 #include <boost/signals/signal1.hpp>
36 class ParConstIterator;
40 /** The buffer object.
41 This is the buffer object. It contains all the informations about
42 a document loaded into LyX. I am not sure if the class is complete or
43 minimal, probably not.
44 \author Lars Gullik Bjønnes
48 /// What type of log will \c getLogName() return?
50 latexlog, ///< LaTeX log
51 buildlog ///< Literate build log
56 \param b optional \c false by default
58 explicit Buffer(string const & file, bool b = false);
63 /** High-level interface to buffer functionality.
64 This function parses a command string and executes it
66 bool dispatch(string const & command, bool * result = 0);
68 /// Maybe we know the function already by number...
69 bool dispatch(int ac, string const & argument, bool * result = 0);
71 /// Load the autosaved file.
72 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 & filename,
80 ParagraphList::iterator pit);
84 bool readFile(string const & filename);
86 bool readFile(string const & filename, ParagraphList::iterator pit);
88 /// read the header, returns number of unknown tokens
89 int readHeader(LyXLex & lex);
91 /** Reads a file without header.
92 \param par if != 0 insert the file.
93 \return \c false if file is not completely read.
95 bool readBody(LyXLex &, ParagraphList::iterator pit);
97 /// This parses a single token
98 int readParagraph(LyXLex &, string const & token,
99 ParagraphList & pars, ParagraphList::iterator & pit,
100 Paragraph::depth_type & depth);
103 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
104 LyXFont const &, string const &);
106 ParIterator getParFromID(int id) const;
107 /// do we have a paragraph with this id?
108 bool hasParWithID(int id) const;
111 /// This signal is emitted when a parsing error shows up.
112 boost::signal1<void, ErrorItem> error;
113 /// This signal is emitted when some message shows up.
114 boost::signal1<void, string> message;
115 /// This signal is emitted when the buffer busy status change.
116 boost::signal1<void, bool> busy;
117 /// This signal is emitted when the buffer readonly status change.
118 boost::signal1<void, bool> readonly;
119 /// Update window titles of all users.
120 boost::signal0<void> updateTitles;
121 /// Reset autosave timers for all users.
122 boost::signal0<void> resetAutosaveTimers;
123 /// This signal is emitting if the buffer is being closed.
124 boost::signal0<void> closing;
128 Takes care of auto-save files and backup file if requested.
129 Returns \c true if the save is successful, \c false otherwise.
133 /// Write file. Returns \c false if unsuccesful.
134 bool writeFile(string const &) const;
137 void writeFileAscii(string const & , int);
139 void writeFileAscii(std::ostream &, int);
141 string const asciiParagraph(Paragraph const &, unsigned int linelen,
142 bool noparbreak = false) const;
143 /// Just a wrapper for the method below, first creating the ofstream.
144 void makeLaTeXFile(string const & filename,
145 string const & original_path,
146 LatexRunParams const &,
147 bool output_preamble = true,
148 bool output_body = true);
150 void makeLaTeXFile(std::ostream & os,
151 string const & original_path,
152 LatexRunParams const &,
153 bool output_preamble = true,
154 bool output_body = true);
156 void simpleDocBookOnePar(std::ostream &,
157 ParagraphList::iterator par, int & desc_on,
158 Paragraph::depth_type depth) const;
160 void simpleLinuxDocOnePar(std::ostream & os,
161 ParagraphList::iterator par,
162 Paragraph::depth_type depth) const;
164 void makeLinuxDocFile(string const & filename,
165 bool nice, bool only_body = false);
167 void makeDocBookFile(string const & filename,
168 bool nice, bool only_body = false);
169 /// returns the main language for the buffer (document)
170 Language const * getLanguage() const;
171 /// get l10n translated to the buffers language
172 string const B_(string const & l10n) const;
176 /// return true if the main lyx file does not need saving
177 bool isClean() const;
179 bool isBakClean() const;
181 bool isDepClean(string const & name) const;
183 /// mark the main lyx file as not needing saving
184 void markClean() const;
190 void markDepClean(string const & name);
193 void setUnnamed(bool flag = true);
198 /// Mark this buffer as dirty.
201 /// Returns the buffer's filename. It is always an absolute path.
202 string const & fileName() const;
204 /// Returns the the path where the buffer lives.
205 /// It is always an absolute path.
206 string const & filePath() const;
208 /** A transformed version of the file name, adequate for LaTeX.
209 \param no_path optional if \c true then the path is stripped.
211 string const getLatexName(bool no_path = true) const;
213 /// Get the name and type of the log.
214 std::pair<LogType, string> const getLogName() const;
216 /// Change name of buffer. Updates "read-only" flag.
217 void setFileName(string const & newfile);
219 /// Name of the document's parent
220 void setParentName(string const &);
222 /// Is buffer read-only?
223 bool isReadonly() const;
225 /// Set buffer read-only flag
226 void setReadonly(bool flag = true);
228 /// returns \c true if the buffer contains a LaTeX document
229 bool isLatex() const;
230 /// returns \c true if the buffer contains a LinuxDoc document
231 bool isLinuxDoc() const;
232 /// returns \c true if the buffer contains a DocBook document
233 bool isDocBook() const;
234 /** returns \c true if the buffer contains either a LinuxDoc
235 or DocBook document */
237 /// returns \c true if the buffer contains a Wed document
238 bool isLiterate() const;
240 /** Validate a buffer for LaTeX.
241 This validates the buffer, and returns a struct for use by
242 #makeLaTeX# and others. Its main use is to figure out what
243 commands and packages need to be included in the LaTeX file.
244 It (should) also check that the needed constructs are there
245 (i.e. that the \refs points to coresponding \labels). It
246 should perhaps inset "error" insets to help the user correct
249 void validate(LaTeXFeatures &) const;
251 /// return all bibkeys from buffer and its childs
252 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
254 void getLabelList(std::vector<string> &) const;
257 void changeLanguage(Language const * from, Language const * to);
260 void updateDocLang(Language const * nlang);
263 bool isMultiLingual();
265 /// Does this mean that this is buffer local?
266 limited_stack<Undo> undostack;
268 /// Does this mean that this is buffer local?
269 limited_stack<Undo> redostack;
274 /** The list of paragraphs.
275 This is a linked list of paragraph, this list holds the
276 whole contents of the document.
278 ParagraphList paragraphs;
280 /// LyX version control object.
283 /// Where to put temporary files.
286 /** If we are writing a nice LaTeX file or not.
287 While writing as LaTeX, tells whether we are
288 doing a 'nice' LaTeX file */
291 /// Used when typesetting to place errorboxes.
294 /// the author list for the document
295 AuthorList & authors();
298 bool do_writeFile(std::ostream & ofs) const;
300 typedef std::map<string, bool> DepClean;
302 /// need to regenerate .tex ?
306 mutable bool lyx_clean;
308 /// is autosave needed
309 mutable bool bak_clean;
311 /// is this a unnamed file (New...)
317 /// name of the file the buffer is associated with.
320 /// The path to the document file.
324 boost::scoped_ptr<Messages> messages_;
327 class inset_iterator {
329 typedef std::input_iterator_tag iterator_category;
330 typedef InsetOld value_type;
331 typedef ptrdiff_t difference_type;
332 typedef InsetOld * pointer;
333 typedef InsetOld & reference;
334 typedef ParagraphList::iterator base_type;
339 inset_iterator(base_type p, base_type e);
341 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
344 inset_iterator & operator++();
346 inset_iterator operator++(int);
348 reference operator*();
350 pointer operator->();
353 ParagraphList::iterator getPar() const;
355 lyx::pos_type getPos() const;
358 bool operator==(inset_iterator const & iter1,
359 inset_iterator const & iter2);
364 ParagraphList::iterator pit;
366 ParagraphList::iterator pend;
368 InsetList::iterator it;
371 /// return an iterator to all *top-level* insets in the buffer
372 inset_iterator inset_iterator_begin();
374 /// return the end of all *top-level* insets in the buffer
375 inset_iterator inset_iterator_end();
377 /// return a const iterator to all *top-level* insets in the buffer
378 inset_iterator inset_const_iterator_begin() const;
380 /// return the const end of all *top-level* insets in the buffer
381 inset_iterator inset_const_iterator_end() const;
384 ParIterator par_iterator_begin();
386 ParConstIterator par_iterator_begin() const;
388 ParIterator par_iterator_end();
390 ParConstIterator par_iterator_end() const;
393 InsetOld * getInsetFromID(int id_arg) const;
396 bool operator==(Buffer::inset_iterator const & iter1,
397 Buffer::inset_iterator const & iter2);
399 bool operator!=(Buffer::inset_iterator const & iter1,
400 Buffer::inset_iterator const & iter2);