2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
8 * This file is Copyleft 1996
11 * ====================================================== */
18 #include "support/limited_stack.h"
21 #include "bufferparams.h"
23 #include "ParagraphList.h"
24 #include "paragraph.h"
27 #include <boost/shared_ptr.hpp>
28 #include <boost/scoped_ptr.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);
72 void resizeInsets(BufferView *);
74 /// Update window titles of all users.
75 void updateTitles() const;
77 /// Reset autosave timers for all users.
78 void resetAutosaveTimers() const;
80 /** Adds the BufferView to the users list.
81 Later this func will insert the \c BufferView into a real list,
82 not just setting a pointer.
84 void addUser(BufferView * u);
86 /** Removes the #BufferView# from the users list.
87 Since we only can have one at the moment, we just reset it.
89 void delUser(BufferView *);
94 /// Load the autosaved file.
95 void loadAutoSaveFile();
97 /** Inserts a file into a document
98 \param par if != 0 insert the file.
99 \return \c false if method fails.
101 bool readFile(LyXLex &, string const &, ParagraphList::iterator pit);
103 // FIXME: it's very silly to pass a lex in here
105 bool readFile(LyXLex &, string const &);
107 /// read the header, returns number of unknown tokens
108 int readHeader(LyXLex & lex);
110 /** Reads a file without header.
111 \param par if != 0 insert the file.
112 \return \c false if file is not completely read.
114 bool readBody(LyXLex &, ParagraphList::iterator pit);
116 /// This parses a single token
117 int readParagraph(LyXLex &, string const & token,
118 ParagraphList & pars, ParagraphList::iterator & pit,
119 Paragraph::depth_type & depth);
122 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
123 LyXFont const &, string const &);
125 ParagraphList::iterator getParFromID(int id) const;
129 Takes care of auto-save files and backup file if requested.
130 Returns \c true if the save is successful, \c false otherwise.
134 /// Write file. Returns \c false if unsuccesful.
135 bool writeFile(string const &) const;
138 void writeFileAscii(string const & , int);
140 void writeFileAscii(std::ostream &, int);
142 string const asciiParagraph(Paragraph const &, unsigned int linelen,
143 bool noparbreak = false) const;
144 /// Just a wrapper for the method below, first creating the ofstream.
145 void makeLaTeXFile(string const & filename,
146 string const & original_path,
148 bool only_body = false,
149 bool only_preamble = false);
151 void makeLaTeXFile(std::ostream & os,
152 string const & original_path,
154 bool only_body = false,
155 bool only_preamble = false);
157 void simpleDocBookOnePar(std::ostream &,
158 ParagraphList::iterator par, int & desc_on,
159 Paragraph::depth_type depth) const ;
161 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
162 Paragraph::depth_type depth);
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);
170 void sgmlError(ParagraphList::iterator par, int pos, string const & message) const;
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 std::vector<string> const getLabelList() const;
259 /** This will clearly have to change later. Later we can have more
260 than one user per buffer. */
261 BufferView * getUser() const;
264 void changeLanguage(Language const * from, Language const * to);
267 void updateDocLang(Language const * nlang);
270 bool isMultiLingual();
272 /// Does this mean that this is buffer local?
273 limited_stack<boost::shared_ptr<Undo> > undostack;
275 /// Does this mean that this is buffer local?
276 limited_stack<boost::shared_ptr<Undo> > redostack;
281 /** The list of paragraphs.
282 This is a linked list of paragraph, this list holds the
283 whole contents of the document.
285 ParagraphList paragraphs;
287 /// LyX version control object.
290 /// Where to put temporary files.
293 /** If we are writing a nice LaTeX file or not.
294 While writing as LaTeX, tells whether we are
295 doing a 'nice' LaTeX file */
298 /// Used when typesetting to place errorboxes.
301 /// the author list for the document
302 AuthorList & authors();
305 typedef std::map<string, bool> DepClean;
307 /// need to regenerate .tex ?
311 mutable bool lyx_clean;
313 /// is autosave needed
314 mutable bool bak_clean;
316 /// is this a unnamed file (New...)
322 /// name of the file the buffer is associated with.
325 /// The path to the document file.
328 /// Format number of buffer
330 /** A list of views using this buffer.
331 Why not keep a list of the BufferViews that use this buffer?
333 At least then we don't have to do a lot of magic like:
334 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
335 of the buffers in the list of users to do a #updateLayoutChoice#.
339 boost::scoped_ptr<Messages> messages_;
342 class inset_iterator {
344 typedef std::input_iterator_tag iterator_category;
345 typedef Inset value_type;
346 typedef ptrdiff_t difference_type;
347 typedef Inset * pointer;
348 typedef Inset & reference;
349 typedef ParagraphList::iterator base_type;
354 inset_iterator(base_type p, base_type e);
356 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
359 inset_iterator & operator++();
361 inset_iterator operator++(int);
363 reference operator*();
365 pointer operator->();
368 Paragraph * getPar();
370 lyx::pos_type getPos() const;
373 bool operator==(inset_iterator const & iter1,
374 inset_iterator const & iter2);
379 ParagraphList::iterator pit;
381 ParagraphList::iterator pend;
383 InsetList::iterator it;
387 inset_iterator inset_iterator_begin() {
388 return inset_iterator(paragraphs.begin(), paragraphs.end());
392 inset_iterator inset_iterator_end() {
393 return inset_iterator();
397 inset_iterator inset_const_iterator_begin() const {
398 return inset_iterator(paragraphs.begin(), paragraphs.end());
402 inset_iterator inset_const_iterator_end() const {
403 return inset_iterator();
407 ParIterator par_iterator_begin();
409 ParConstIterator par_iterator_begin() const;
411 ParIterator par_iterator_end();
413 ParConstIterator par_iterator_end() const;
416 Inset * getInsetFromID(int id_arg) const;
419 bool operator==(Buffer::inset_iterator const & iter1,
420 Buffer::inset_iterator const & iter2);
422 bool operator!=(Buffer::inset_iterator const & iter1,
423 Buffer::inset_iterator const & iter2);