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"
26 #include <boost/scoped_ptr.hpp>
35 class ParConstIterator;
39 /** The buffer object.
40 This is the buffer object. It contains all the informations about
41 a document loaded into LyX. I am not sure if the class is complete or
42 minimal, probably not.
43 \author Lars Gullik Bjønnes
47 /// What type of log will \c getLogName() return?
49 latexlog, ///< LaTeX log
50 buildlog ///< Literate build log
55 \param b optional \c false by default
57 explicit Buffer(string const & file, bool b = false);
62 /** High-level interface to buffer functionality.
63 This function parses a command string and executes it
65 bool dispatch(string const & command, bool * result = 0);
67 /// Maybe we know the function already by number...
68 bool dispatch(int ac, string const & argument, bool * result = 0);
71 void resizeInsets(BufferView *);
73 /// Update window titles of all users.
74 void updateTitles() const;
76 /// Reset autosave timers for all users.
77 void resetAutosaveTimers() const;
79 /** Adds the BufferView to the users list.
80 Later this func will insert the \c BufferView into a real list,
81 not just setting a pointer.
83 void addUser(BufferView * u);
85 /** Removes the #BufferView# from the users list.
86 Since we only can have one at the moment, we just reset it.
88 void delUser(BufferView *);
93 /// Load the autosaved file.
94 void loadAutoSaveFile();
96 /** Inserts a file into a document
97 \param par if != 0 insert the file.
98 \return \c false if method fails.
100 bool readFile(LyXLex &, string const &, ParagraphList::iterator pit);
102 // FIXME: it's very silly to pass a lex in here
104 bool readFile(LyXLex &, string const &);
106 /// read the header, returns number of unknown tokens
107 int readHeader(LyXLex & lex);
109 /** Reads a file without header.
110 \param par if != 0 insert the file.
111 \return \c false if file is not completely read.
113 bool readBody(LyXLex &, ParagraphList::iterator pit);
115 /// This parses a single token
116 int readParagraph(LyXLex &, string const & token,
117 ParagraphList & pars, ParagraphList::iterator & pit,
118 Paragraph::depth_type & depth);
121 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
122 LyXFont const &, string const &);
124 ParIterator getParFromID(int id) const;
125 /// do we have a paragraph with this id?
126 bool hasParWithID(int id) const;
130 Takes care of auto-save files and backup file if requested.
131 Returns \c true if the save is successful, \c false otherwise.
135 /// Write file. Returns \c false if unsuccesful.
136 bool writeFile(string const &) const;
139 void writeFileAscii(string const & , int);
141 void writeFileAscii(std::ostream &, int);
143 string const asciiParagraph(Paragraph const &, unsigned int linelen,
144 bool noparbreak = false) const;
145 /// Just a wrapper for the method below, first creating the ofstream.
146 void makeLaTeXFile(string const & filename,
147 string const & original_path,
148 LatexRunParams const &,
149 bool only_body = false,
150 bool only_preamble = false);
152 void makeLaTeXFile(std::ostream & os,
153 string const & original_path,
154 LatexRunParams const &,
155 bool only_body = false,
156 bool only_preamble = false);
158 void simpleDocBookOnePar(std::ostream &,
159 ParagraphList::iterator par, int & desc_on,
160 Paragraph::depth_type depth) const;
162 void simpleLinuxDocOnePar(std::ostream & os,
163 ParagraphList::iterator par,
164 Paragraph::depth_type depth) const;
166 void makeLinuxDocFile(string const & filename,
167 bool nice, bool only_body = false);
169 void makeDocBookFile(string const & filename,
170 bool nice, bool only_body = false);
172 void sgmlError(ParagraphList::iterator par, int pos, string const & message) const;
174 /// returns the main language for the buffer (document)
175 Language const * getLanguage() const;
176 /// get l10n translated to the buffers language
177 string const B_(string const & l10n) const;
181 /// return true if the main lyx file does not need saving
182 bool isClean() const;
184 bool isBakClean() const;
186 bool isDepClean(string const & name) const;
188 /// mark the main lyx file as not needing saving
189 void markClean() const;
195 void markDepClean(string const & name);
198 void setUnnamed(bool flag = true);
203 /// Mark this buffer as dirty.
206 /// Returns the buffer's filename. It is always an absolute path.
207 string const & fileName() const;
209 /// Returns the the path where the buffer lives.
210 /// It is always an absolute path.
211 string const & filePath() const;
213 /** A transformed version of the file name, adequate for LaTeX.
214 \param no_path optional if \c true then the path is stripped.
216 string const getLatexName(bool no_path = true) const;
218 /// Get the name and type of the log.
219 std::pair<LogType, string> const getLogName() const;
221 /// Change name of buffer. Updates "read-only" flag.
222 void setFileName(string const & newfile);
224 /// Name of the document's parent
225 void setParentName(string const &);
227 /// Is buffer read-only?
228 bool isReadonly() const;
230 /// Set buffer read-only flag
231 void setReadonly(bool flag = true);
233 /// returns \c true if the buffer contains a LaTeX document
234 bool isLatex() const;
235 /// returns \c true if the buffer contains a LinuxDoc document
236 bool isLinuxDoc() const;
237 /// returns \c true if the buffer contains a DocBook document
238 bool isDocBook() const;
239 /** returns \c true if the buffer contains either a LinuxDoc
240 or DocBook document */
242 /// returns \c true if the buffer contains a Wed document
243 bool isLiterate() const;
245 /** Validate a buffer for LaTeX.
246 This validates the buffer, and returns a struct for use by
247 #makeLaTeX# and others. Its main use is to figure out what
248 commands and packages need to be included in the LaTeX file.
249 It (should) also check that the needed constructs are there
250 (i.e. that the \refs points to coresponding \labels). It
251 should perhaps inset "error" insets to help the user correct
254 void validate(LaTeXFeatures &) const;
256 /// return all bibkeys from buffer and its childs
257 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
259 std::vector<string> const getLabelList() const;
261 /** This will clearly have to change later. Later we can have more
262 than one user per buffer. */
263 BufferView * getUser() const;
266 void changeLanguage(Language const * from, Language const * to);
269 void updateDocLang(Language const * nlang);
272 bool isMultiLingual();
274 /// Does this mean that this is buffer local?
275 limited_stack<Undo> undostack;
277 /// Does this mean that this is buffer local?
278 limited_stack<Undo> redostack;
283 /** The list of paragraphs.
284 This is a linked list of paragraph, this list holds the
285 whole contents of the document.
287 ParagraphList paragraphs;
289 /// LyX version control object.
292 /// Where to put temporary files.
295 /** If we are writing a nice LaTeX file or not.
296 While writing as LaTeX, tells whether we are
297 doing a 'nice' LaTeX file */
300 /// Used when typesetting to place errorboxes.
303 /// the author list for the document
304 AuthorList & authors();
307 typedef std::map<string, bool> DepClean;
309 /// need to regenerate .tex ?
313 mutable bool lyx_clean;
315 /// is autosave needed
316 mutable bool bak_clean;
318 /// is this a unnamed file (New...)
324 /// name of the file the buffer is associated with.
327 /// The path to the document file.
330 /// Format number of buffer
332 /** A list of views using this buffer.
333 Why not keep a list of the BufferViews that use this buffer?
335 At least then we don't have to do a lot of magic like:
336 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
337 of the buffers in the list of users to do a #updateLayoutChoice#.
341 boost::scoped_ptr<Messages> messages_;
344 class inset_iterator {
346 typedef std::input_iterator_tag iterator_category;
347 typedef Inset value_type;
348 typedef ptrdiff_t difference_type;
349 typedef Inset * pointer;
350 typedef Inset & reference;
351 typedef ParagraphList::iterator base_type;
356 inset_iterator(base_type p, base_type e);
358 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
361 inset_iterator & operator++();
363 inset_iterator operator++(int);
365 reference operator*();
367 pointer operator->();
370 ParagraphList::iterator getPar() const;
372 lyx::pos_type getPos() const;
375 bool operator==(inset_iterator const & iter1,
376 inset_iterator const & iter2);
381 ParagraphList::iterator pit;
383 ParagraphList::iterator pend;
385 InsetList::iterator it;
388 /// return an iterator to all *top-level* insets in the buffer
389 inset_iterator inset_iterator_begin();
391 /// return the end of all *top-level* insets in the buffer
392 inset_iterator inset_iterator_end();
394 /// return a const iterator to all *top-level* insets in the buffer
395 inset_iterator inset_const_iterator_begin() const;
397 /// return the const end of all *top-level* insets in the buffer
398 inset_iterator inset_const_iterator_end() const;
401 ParIterator par_iterator_begin();
403 ParConstIterator par_iterator_begin() const;
405 ParIterator par_iterator_end();
407 ParConstIterator par_iterator_end() const;
410 Inset * getInsetFromID(int id_arg) const;
413 bool operator==(Buffer::inset_iterator const & iter1,
414 Buffer::inset_iterator const & iter2);
416 bool operator!=(Buffer::inset_iterator const & iter1,
417 Buffer::inset_iterator const & iter2);