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>
35 class ParConstIterator;
38 /** The buffer object.
39 This is the buffer object. It contains all the informations about
40 a document loaded into LyX. I am not sure if the class is complete or
41 minimal, probably not.
42 \author Lars Gullik Bjønnes
46 /// What type of log will \c getLogName() return?
48 latexlog, ///< LaTeX log
49 buildlog ///< Literate build log
54 \param b optional \c false by default
56 explicit Buffer(string const & file, bool b = false);
61 /** High-level interface to buffer functionality.
62 This function parses a command string and executes it
64 bool dispatch(string const & command, bool * result = 0);
66 /// Maybe we know the function already by number...
67 bool dispatch(int ac, string const & argument, bool * result = 0);
70 void resizeInsets(BufferView *);
72 /// Update window titles of all users.
73 void updateTitles() const;
75 /// Reset autosave timers for all users.
76 void resetAutosaveTimers() const;
78 /** Adds the BufferView to the users list.
79 Later this func will insert the \c BufferView into a real list,
80 not just setting a pointer.
82 void addUser(BufferView * u);
84 /** Removes the #BufferView# from the users list.
85 Since we only can have one at the moment, we just reset it.
87 void delUser(BufferView *);
92 /// Load the autosaved file.
93 void loadAutoSaveFile();
96 \param par if != 0 insert the file.
97 \return \c false if method fails.
99 bool readFile(LyXLex &, string const &, Paragraph * par = 0);
101 /** Reads a file without header.
102 \param par if != 0 insert the file.
103 \return \c false if file is not completely read.
105 bool readLyXformat2(LyXLex &, Paragraph * par = 0);
107 /// This parses a single LyXformat-Token.
108 bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
109 Paragraph *& return_par,
110 string const & token, int & pos,
111 Paragraph::depth_type & depth,
114 void insertStringAsLines(Paragraph *&, lyx::pos_type &,
115 LyXFont const &, string const &) const;
117 Paragraph * getParFromID(int id) const;
119 /// Parse a single inset.
120 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
123 Takes care of auto-save files and backup file if requested.
124 Returns \c true if the save is successful, \c false otherwise.
128 /// Write file. Returns \c false if unsuccesful.
129 bool writeFile(string const &) const;
132 void writeFileAscii(string const & , int);
134 void writeFileAscii(std::ostream &, int);
136 string const asciiParagraph(Paragraph const &, unsigned int linelen,
137 bool noparbreak = false) const;
138 /// Just a wrapper for the method below, first creating the ofstream.
139 void makeLaTeXFile(string const & filename,
140 string const & original_path,
142 bool only_body = false,
143 bool only_preamble = false);
145 void makeLaTeXFile(std::ostream & os,
146 string const & original_path,
148 bool only_body = false,
149 bool only_preamble = false);
150 /** LaTeX all paragraphs from par to endpar.
151 \param \a endpar if == 0 then to the end
153 void latexParagraphs(std::ostream & os,
154 ParagraphList::iterator par,
155 ParagraphList::iterator endpar,
156 TexRow & texrow, bool moving_arg = false) const;
158 void simpleDocBookOnePar(std::ostream &,
159 Paragraph * par, int & desc_on,
160 Paragraph::depth_type depth) const ;
162 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
163 Paragraph::depth_type depth);
165 void makeLinuxDocFile(string const & filename,
166 bool nice, bool only_body = false);
168 void makeDocBookFile(string const & filename,
169 bool nice, bool only_body = false);
171 void sgmlError(Paragraph * par, int pos, string const & message) const;
173 /// returns the main language for the buffer (document)
174 Language const * getLanguage() const;
177 /// return true if the main lyx file does not need saving
178 bool isClean() const;
180 bool isBakClean() const;
182 bool isDepClean(string const & name) const;
184 /// mark the main lyx file as not needing saving
185 void markClean() const;
191 void markDepClean(string const & name);
194 void setUnnamed(bool flag = true);
199 /// Mark this buffer as dirty.
202 /// Returns the buffer's filename. It is always an absolute path.
203 string const & fileName() const;
205 /// Returns the the path where the buffer lives.
206 /// It is always an absolute path.
207 string const & filePath() const;
209 /** A transformed version of the file name, adequate for LaTeX.
210 \param no_path optional if \c true then the path is stripped.
212 string const getLatexName(bool no_path = true) const;
214 /// Get the name and type of the log.
215 std::pair<LogType, string> const getLogName() const;
217 /// Change name of buffer. Updates "read-only" flag.
218 void setFileName(string const & newfile);
220 /// Name of the document's parent
221 void setParentName(string const &);
223 /// Is buffer read-only?
224 bool isReadonly() const;
226 /// Set buffer read-only flag
227 void setReadonly(bool flag = true);
229 /// returns \c true if the buffer contains a LaTeX document
230 bool isLatex() const;
231 /// returns \c true if the buffer contains a LinuxDoc document
232 bool isLinuxDoc() const;
233 /// returns \c true if the buffer contains a DocBook document
234 bool isDocBook() const;
235 /** returns \c true if the buffer contains either a LinuxDoc
236 or DocBook document */
238 /// returns \c true if the buffer contains a Wed document
239 bool isLiterate() const;
241 /** Validate a buffer for LaTeX.
242 This validates the buffer, and returns a struct for use by
243 #makeLaTeX# and others. Its main use is to figure out what
244 commands and packages need to be included in the LaTeX file.
245 It (should) also check that the needed constructs are there
246 (i.e. that the \refs points to coresponding \labels). It
247 should perhaps inset "error" insets to help the user correct
250 void validate(LaTeXFeatures &) const;
252 /// return all bibkeys from buffer and its childs
253 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
255 std::vector<string> const getLabelList() const;
257 /** This will clearly have to change later. Later we can have more
258 than one user per buffer. */
259 BufferView * getUser() const;
262 void changeLanguage(Language const * from, Language const * to);
264 bool isMultiLingual();
266 /// Does this mean that this is buffer local?
267 limited_stack<boost::shared_ptr<Undo> > undostack;
269 /// Does this mean that this is buffer local?
270 limited_stack<boost::shared_ptr<Undo> > redostack;
275 /** The list of paragraphs.
276 This is a linked list of paragraph, this list holds the
277 whole contents of the document.
279 ParagraphList paragraphs;
281 /// LyX version control object.
284 /// Where to put temporary files.
287 /** If we are writing a nice LaTeX file or not.
288 While writing as LaTeX, tells whether we are
289 doing a 'nice' LaTeX file */
292 /// Used when typesetting to place errorboxes.
295 /// the author list for the document
296 AuthorList & authors();
299 typedef std::map<string, bool> DepClean;
301 /// need to regenerate .tex ?
305 AuthorList authorlist;
308 mutable bool lyx_clean;
310 /// is autosave needed
311 mutable bool bak_clean;
313 /// is this a unnamed file (New...)
319 /// name of the file the buffer is associated with.
322 /// The path to the document file.
325 /// Format number of buffer
327 /** A list of views using this buffer.
328 Why not keep a list of the BufferViews that use this buffer?
330 At least then we don't have to do a lot of magic like:
331 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
332 of the buffers in the list of users to do a #updateLayoutChoice#.
337 class inset_iterator {
339 typedef std::input_iterator_tag iterator_category;
340 typedef Inset value_type;
341 typedef ptrdiff_t difference_type;
342 typedef Inset * pointer;
343 typedef Inset & reference;
344 typedef ParagraphList::iterator base_type;
349 inset_iterator(base_type p, base_type e);
351 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
354 inset_iterator & operator++();
356 inset_iterator operator++(int);
358 reference operator*();
360 pointer operator->();
363 Paragraph * getPar();
365 lyx::pos_type getPos() const;
368 bool operator==(inset_iterator const & iter1,
369 inset_iterator const & iter2);
374 ParagraphList::iterator pit;
376 ParagraphList::iterator pend;
378 InsetList::iterator it;
382 inset_iterator inset_iterator_begin() {
383 return inset_iterator(paragraphs.begin(), paragraphs.end());
387 inset_iterator inset_iterator_end() {
388 return inset_iterator();
392 inset_iterator inset_const_iterator_begin() const {
393 return inset_iterator(paragraphs.begin(), paragraphs.end());
397 inset_iterator inset_const_iterator_end() const {
398 return inset_iterator();
402 ParIterator par_iterator_begin();
404 ParConstIterator par_iterator_begin() const;
406 ParIterator par_iterator_end();
408 ParConstIterator par_iterator_end() const;
411 Inset * getInsetFromID(int id_arg) const;
414 bool operator==(Buffer::inset_iterator const & iter1,
415 Buffer::inset_iterator const & iter2);
417 bool operator!=(Buffer::inset_iterator const & iter1,
418 Buffer::inset_iterator const & iter2);