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_fwd.h"
23 #include "errorlist.h"
24 #include "InsetList.h"
26 #include "support/types.h"
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/signals/signal0.hpp>
30 #include <boost/signals/signal1.hpp>
39 class ParConstIterator;
43 /** The buffer object.
44 This is the buffer object. It contains all the informations about
45 a document loaded into LyX. I am not sure if the class is complete or
46 minimal, probably not.
47 \author Lars Gullik Bjønnes
51 /// What type of log will \c getLogName() return?
53 latexlog, ///< LaTeX log
54 buildlog ///< Literate build log
59 \param b optional \c false by default
61 explicit Buffer(string const & file, bool b = false);
66 /** High-level interface to buffer functionality.
67 This function parses a command string and executes it
69 bool dispatch(string const & command, bool * result = 0);
71 /// Maybe we know the function already by number...
72 bool dispatch(int ac, string const & argument, bool * result = 0);
74 /// Load the autosaved file.
75 void loadAutoSaveFile();
78 /** Inserts a file into a document
79 \param par if != 0 insert the file.
80 \return \c false if method fails.
82 bool readFile(LyXLex &, string const & filename,
83 ParagraphList::iterator pit);
87 bool readFile(string const & filename);
89 bool readFile(string const & filename, ParagraphList::iterator pit);
91 /// read the header, returns number of unknown tokens
92 int readHeader(LyXLex & lex);
94 /** Reads a file without header.
95 \param par if != 0 insert the file.
96 \return \c false if file is not completely read.
98 bool readBody(LyXLex &, ParagraphList::iterator pit);
100 /// This parses a single token
101 int readParagraph(LyXLex &, string const & token,
102 ParagraphList & pars, ParagraphList::iterator & pit,
103 lyx::depth_type & depth);
106 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
107 LyXFont const &, string const &);
109 ParIterator getParFromID(int id) const;
110 /// do we have a paragraph with this id?
111 bool hasParWithID(int id) const;
114 /// This signal is emitted when a parsing error shows up.
115 boost::signal1<void, ErrorItem> error;
116 /// This signal is emitted when some message shows up.
117 boost::signal1<void, string> message;
118 /// This signal is emitted when the buffer busy status change.
119 boost::signal1<void, bool> busy;
120 /// This signal is emitted when the buffer readonly status change.
121 boost::signal1<void, bool> readonly;
122 /// Update window titles of all users.
123 boost::signal0<void> updateTitles;
124 /// Reset autosave timers for all users.
125 boost::signal0<void> resetAutosaveTimers;
126 /// This signal is emitting if the buffer is being closed.
127 boost::signal0<void> closing;
131 Takes care of auto-save files and backup file if requested.
132 Returns \c true if the save is successful, \c false otherwise.
136 /// Write file. Returns \c false if unsuccesful.
137 bool writeFile(string const &) const;
140 void writeFileAscii(string const & , int);
142 void writeFileAscii(std::ostream &, int);
144 string const asciiParagraph(Paragraph const &, unsigned int linelen,
145 bool noparbreak = false) const;
146 /// Just a wrapper for the method below, first creating the ofstream.
147 void makeLaTeXFile(string const & filename,
148 string const & original_path,
149 LatexRunParams const &,
150 bool output_preamble = true,
151 bool output_body = true);
153 void makeLaTeXFile(std::ostream & os,
154 string const & original_path,
155 LatexRunParams const &,
156 bool output_preamble = true,
157 bool output_body = true);
159 void simpleDocBookOnePar(std::ostream &,
160 ParagraphList::iterator par, int & desc_on,
161 lyx::depth_type depth) const;
163 void simpleLinuxDocOnePar(std::ostream & os,
164 ParagraphList::iterator par,
165 lyx::depth_type depth) const;
167 void makeLinuxDocFile(string const & filename,
168 bool nice, bool only_body = false);
170 void makeDocBookFile(string const & filename,
171 bool nice, bool only_body = false);
172 /// returns the main language for the buffer (document)
173 Language const * getLanguage() const;
174 /// get l10n translated to the buffers language
175 string const B_(string const & l10n) const;
179 /// return true if the main lyx file does not need saving
180 bool isClean() const;
182 bool isBakClean() const;
184 bool isDepClean(string const & name) const;
186 /// mark the main lyx file as not needing saving
187 void markClean() const;
193 void markDepClean(string const & name);
196 void setUnnamed(bool flag = true);
201 /// Mark this buffer as dirty.
204 /// Returns the buffer's filename. It is always an absolute path.
205 string const & fileName() const;
207 /// Returns the the path where the buffer lives.
208 /// It is always an absolute path.
209 string const & filePath() const;
211 /** A transformed version of the file name, adequate for LaTeX.
212 \param no_path optional if \c true then the path is stripped.
214 string const getLatexName(bool no_path = true) const;
216 /// Get the name and type of the log.
217 std::pair<LogType, string> const getLogName() const;
219 /// Change name of buffer. Updates "read-only" flag.
220 void setFileName(string const & newfile);
222 /// Name of the document's parent
223 void setParentName(string const &);
225 /// Is buffer read-only?
226 bool isReadonly() const;
228 /// Set buffer read-only flag
229 void setReadonly(bool flag = true);
231 /// returns \c true if the buffer contains a LaTeX document
232 bool isLatex() const;
233 /// returns \c true if the buffer contains a LinuxDoc document
234 bool isLinuxDoc() const;
235 /// returns \c true if the buffer contains a DocBook document
236 bool isDocBook() const;
237 /** returns \c true if the buffer contains either a LinuxDoc
238 or DocBook document */
240 /// returns \c true if the buffer contains a Wed document
241 bool isLiterate() const;
243 /** Validate a buffer for LaTeX.
244 This validates the buffer, and returns a struct for use by
245 #makeLaTeX# and others. Its main use is to figure out what
246 commands and packages need to be included in the LaTeX file.
247 It (should) also check that the needed constructs are there
248 (i.e. that the \refs points to coresponding \labels). It
249 should perhaps inset "error" insets to help the user correct
252 void validate(LaTeXFeatures &) const;
254 /// return all bibkeys from buffer and its childs
255 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
257 void getLabelList(std::vector<string> &) const;
260 void changeLanguage(Language const * from, Language const * to);
263 void updateDocLang(Language const * nlang);
266 bool isMultiLingual();
268 /// Does this mean that this is buffer local?
269 limited_stack<Undo> undostack;
271 /// Does this mean that this is buffer local?
272 limited_stack<Undo> redostack;
277 /** The list of paragraphs.
278 This is a linked list of paragraph, this list holds the
279 whole contents of the document.
281 ParagraphList paragraphs;
283 /// LyX version control object.
286 /// Where to put temporary files.
289 /** If we are writing a nice LaTeX file or not.
290 While writing as LaTeX, tells whether we are
291 doing a 'nice' LaTeX file */
294 /// Used when typesetting to place errorboxes.
297 /// the author list for the document
298 AuthorList & authors();
301 bool do_writeFile(std::ostream & ofs) const;
303 typedef std::map<string, bool> DepClean;
305 /// need to regenerate .tex ?
309 mutable bool lyx_clean;
311 /// is autosave needed
312 mutable bool bak_clean;
314 /// is this a unnamed file (New...)
320 /// name of the file the buffer is associated with.
323 /// The path to the document file.
327 boost::scoped_ptr<Messages> messages_;
330 class inset_iterator {
332 typedef std::input_iterator_tag iterator_category;
333 typedef InsetOld value_type;
334 typedef ptrdiff_t difference_type;
335 typedef InsetOld * pointer;
336 typedef InsetOld & reference;
337 typedef ParagraphList::iterator base_type;
342 inset_iterator(base_type p, base_type e);
344 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
347 inset_iterator & operator++();
349 inset_iterator operator++(int);
351 reference operator*();
353 pointer operator->();
356 ParagraphList::iterator getPar() const;
358 lyx::pos_type getPos() const;
361 bool operator==(inset_iterator const & iter1,
362 inset_iterator const & iter2);
367 ParagraphList::iterator pit;
369 ParagraphList::iterator pend;
371 InsetList::iterator it;
374 /// return an iterator to all *top-level* insets in the buffer
375 inset_iterator inset_iterator_begin();
377 /// return the end of all *top-level* insets in the buffer
378 inset_iterator inset_iterator_end();
380 /// return a const iterator to all *top-level* insets in the buffer
381 inset_iterator inset_const_iterator_begin() const;
383 /// return the const end of all *top-level* insets in the buffer
384 inset_iterator inset_const_iterator_end() const;
387 ParIterator par_iterator_begin();
389 ParConstIterator par_iterator_begin() const;
391 ParIterator par_iterator_end();
393 ParConstIterator par_iterator_end() const;
396 InsetOld * getInsetFromID(int id_arg) const;
399 bool operator==(Buffer::inset_iterator const & iter1,
400 Buffer::inset_iterator const & iter2);
402 bool operator!=(Buffer::inset_iterator const & iter1,
403 Buffer::inset_iterator const & iter2);