2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
8 * This file is Copyleft 1996
11 * ====================================================== */
22 #include "support/limited_stack.h"
25 #include "bufferparams.h"
27 #include "ParagraphList.h"
28 #include "paragraph.h"
30 #include <boost/shared_ptr.hpp>
50 /** The buffer object.
51 This is the buffer object. It contains all the informations about
52 a document loaded into LyX. I am not sure if the class is complete or
53 minimal, probably not.
54 \author Lars Gullik Bjønnes
58 /// What type of log will \c getLogName() return?
60 latexlog, ///< LaTeX log
61 buildlog ///< Literate build log
66 \param b optional \c false by default
68 explicit Buffer(string const & file, bool b = false);
73 /** High-level interface to buffer functionality.
74 This function parses a command string and executes it
76 bool dispatch(string const & command, bool * result = 0);
78 /// Maybe we know the function already by number...
79 bool dispatch(int ac, string const & argument, bool * result = 0);
82 void resizeInsets(BufferView *);
84 /// Update window titles of all users.
85 void updateTitles() const;
87 /// Reset autosave timers for all users.
88 void resetAutosaveTimers() const;
90 /** Adds the BufferView to the users list.
91 Later this func will insert the \c BufferView into a real list,
92 not just setting a pointer.
94 void addUser(BufferView * u);
96 /** Removes the #BufferView# from the users list.
97 Since we only can have one at the moment, we just reset it.
99 void delUser(BufferView *);
104 /// Load the autosaved file.
105 void loadAutoSaveFile();
108 \param par if != 0 insert the file.
109 \return \c false if method fails.
111 bool readFile(LyXLex &, string const &, Paragraph * par = 0);
113 /** Reads a file without header.
114 \param par if != 0 insert the file.
115 \return \c false if file is not completely read.
117 bool readLyXformat2(LyXLex &, Paragraph * par = 0);
119 /// This parses a single LyXformat-Token.
120 bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
121 Paragraph *& return_par,
122 string const & token, int & pos,
123 Paragraph::depth_type & depth,
126 void insertStringAsLines(Paragraph *&, lyx::pos_type &,
127 LyXFont const &, string const &) const;
129 Paragraph * getParFromID(int id) const;
131 /// Parse a single inset.
132 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
135 Takes care of auto-save files and backup file if requested.
136 Returns \c true if the save is successful, \c false otherwise.
140 /// Write file. Returns \c false if unsuccesful.
141 bool writeFile(string const &) const;
144 void writeFileAscii(string const & , int);
146 void writeFileAscii(std::ostream &, int);
148 string const asciiParagraph(Paragraph const &, unsigned int linelen,
149 bool noparbreak = false) const;
150 /// Just a wrapper for the method below, first creating the ofstream.
151 void makeLaTeXFile(string const & filename,
152 string const & original_path,
154 bool only_body = false,
155 bool only_preamble = false);
157 void makeLaTeXFile(std::ostream & os,
158 string const & original_path,
160 bool only_body = false,
161 bool only_preamble = false);
162 /** LaTeX all paragraphs from par to endpar.
163 \param \a endpar if == 0 then to the end
165 void latexParagraphs(std::ostream & os, Paragraph * par,
166 Paragraph * endpar, TexRow & texrow, bool moving_arg = false) const;
168 void simpleDocBookOnePar(std::ostream &,
169 Paragraph * par, int & desc_on,
170 Paragraph::depth_type depth) const ;
172 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
173 Paragraph::depth_type depth);
175 void makeLinuxDocFile(string const & filename,
176 bool nice, bool only_body = false);
178 void makeDocBookFile(string const & filename,
179 bool nice, bool only_body = false);
180 /// Open SGML/XML tag.
181 int sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
182 string const & latexname) const;
183 /// Closes SGML/XML tag.
184 int sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
185 string const & latexname) const;
187 void sgmlError(Paragraph * par, int pos, string const & message) const;
189 /// returns the main language for the buffer (document)
190 Language const * getLanguage() const;
193 /// return true if the main lyx file does not need saving
194 bool isClean() const;
196 bool isBakClean() const;
198 bool isDepClean(string const & name) const;
200 /// mark the main lyx file as not needing saving
201 void markClean() const;
207 void markDepClean(string const & name);
210 void setUnnamed(bool flag = true);
215 /// Mark this buffer as dirty.
218 /// Returns the buffer's filename. It is always an absolute path.
219 string const & fileName() const;
221 /// Returns the the path where the buffer lives.
222 /// It is always an absolute path.
223 string const & filePath() const;
225 /** A transformed version of the file name, adequate for LaTeX.
226 \param no_path optional if \c true then the path is stripped.
228 string const getLatexName(bool no_path = true) const;
230 /// Get the name and type of the log.
231 std::pair<LogType, string> const getLogName() const;
233 /// Change name of buffer. Updates "read-only" flag.
234 void setFileName(string const & newfile);
236 /// Name of the document's parent
237 void setParentName(string const &);
239 /// Is buffer read-only?
240 bool isReadonly() const;
242 /// Set buffer read-only flag
243 void setReadonly(bool flag = true);
245 /// returns \c true if the buffer contains a LaTeX document
246 bool isLatex() const;
247 /// returns \c true if the buffer contains a LinuxDoc document
248 bool isLinuxDoc() const;
249 /// returns \c true if the buffer contains a DocBook document
250 bool isDocBook() const;
251 /** returns \c true if the buffer contains either a LinuxDoc
252 or DocBook document */
254 /// returns \c true if the buffer contains a Wed document
255 bool isLiterate() const;
257 /** Validate a buffer for LaTeX.
258 This validates the buffer, and returns a struct for use by
259 #makeLaTeX# and others. Its main use is to figure out what
260 commands and packages need to be included in the LaTeX file.
261 It (should) also check that the needed constructs are there
262 (i.e. that the \refs points to coresponding \labels). It
263 should perhaps inset "error" insets to help the user correct
266 void validate(LaTeXFeatures &) const;
269 std::vector<std::pair<string, string> > const getBibkeyList() const;
271 std::vector<string> const getLabelList() const;
273 /** This will clearly have to change later. Later we can have more
274 than one user per buffer. */
275 BufferView * getUser() const;
278 void changeLanguage(Language const * from, Language const * to);
280 bool isMultiLingual();
282 /// Does this mean that this is buffer local?
283 limited_stack<boost::shared_ptr<Undo> > undostack;
285 /// Does this mean that this is buffer local?
286 limited_stack<boost::shared_ptr<Undo> > redostack;
291 /** The list of paragraphs.
292 This is a linked list of paragraph, this list holds the
293 whole contents of the document.
295 ParagraphList paragraphs;
297 /// LyX version control object.
300 /// Where to put temporary files.
303 /** If we are writing a nice LaTeX file or not.
304 While writing as LaTeX, tells whether we are
305 doing a 'nice' LaTeX file */
308 /// Used when typesetting to place errorboxes.
312 mutable bool lyx_clean;
314 /// is autosave needed
315 mutable bool bak_clean;
317 /// is this a unnamed file (New...)
320 /// is regenerating #.tex# necessary
321 DEPCLEAN * dep_clean;
326 /// name of the file the buffer is associated with.
329 /// The path to the document file.
332 /// Format number of buffer
334 /** A list of views using this buffer.
335 Why not keep a list of the BufferViews that use this buffer?
337 At least then we don't have to do a lot of magic like:
338 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
339 of the buffers in the list of users to do a #updateLayoutChoice#.
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 Paragraph * getPar();
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;
389 inset_iterator inset_iterator_begin() {
390 return inset_iterator(paragraphs.begin(), paragraphs.end());
394 inset_iterator inset_iterator_end() {
395 return inset_iterator();
399 inset_iterator inset_const_iterator_begin() const {
400 return inset_iterator(paragraphs.begin(), paragraphs.end());
404 inset_iterator inset_const_iterator_end() const {
405 return inset_iterator();
409 ParIterator par_iterator_begin();
411 ParIterator par_iterator_end();
414 Inset * getInsetFromID(int id_arg) const;
417 bool operator==(Buffer::inset_iterator const & iter1,
418 Buffer::inset_iterator const & iter2);
420 bool operator!=(Buffer::inset_iterator const & iter1,
421 Buffer::inset_iterator const & iter2);