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/scoped_ptr.hpp>
31 #include <boost/shared_ptr.hpp>
41 #define NO_COMPABILITY 1
53 /** The buffer object.
54 This is the buffer object. It contains all the informations about
55 a document loaded into LyX. I am not sure if the class is complete or
56 minimal, probably not.
57 \author Lars Gullik Bjønnes
61 /// What type of log will \c getLogName() return?
63 latexlog, ///< LaTeX log
64 buildlog ///< Literate build log
69 \param b optional \c false by default
71 explicit Buffer(string const & file, bool b = false);
76 /** High-level interface to buffer functionality.
77 This function parses a command string and executes it
79 bool dispatch(string const & command, bool * result = 0);
81 /// Maybe we know the function already by number...
82 bool dispatch(int ac, string const & argument, bool * result = 0);
85 void resizeInsets(BufferView *);
87 /// Update window titles of all users.
88 void updateTitles() const;
90 /// Reset autosave timers for all users.
91 void resetAutosaveTimers() const;
93 /** Adds the BufferView to the users list.
94 Later this func will insert the \c BufferView into a real list,
95 not just setting a pointer.
97 void addUser(BufferView * u);
99 /** Removes the #BufferView# from the users list.
100 Since we only can have one at the moment, we just reset it.
102 void delUser(BufferView *);
107 /// Load the autosaved file.
108 void loadAutoSaveFile();
111 \param par if != 0 insert the file.
112 \return \c false if method fails.
114 bool readFile(LyXLex &, Paragraph * par = 0);
116 /** Reads a file without header.
117 \param par if != 0 insert the file.
118 \return \c false if file is not completely read.
120 bool readLyXformat2(LyXLex &, Paragraph * par = 0);
122 /// This parses a single LyXformat-Token.
123 bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
124 Paragraph *& return_par,
125 string const & token, int & pos,
126 Paragraph::depth_type & depth,
129 void insertStringAsLines(Paragraph *&, lyx::pos_type &,
130 LyXFont const &, string const &) const;
131 #ifndef NO_COMPABILITY
133 Inset * isErtInset(Paragraph * par, int pos) const;
135 void insertErtContents(Paragraph * par, int & pos,
136 bool set_inactive = true);
139 Paragraph * getParFromID(int id) const;
141 /// Parse a single inset.
142 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
145 Takes care of auto-save files and backup file if requested.
146 Returns \c true if the save is successful, \c false otherwise.
150 /// Write file. Returns \c false if unsuccesful.
151 bool writeFile(string const &) const;
154 void writeFileAscii(string const & , int);
156 void writeFileAscii(std::ostream &, int);
158 string const asciiParagraph(Paragraph const *, unsigned int linelen,
159 bool noparbreak = false) const;
160 /// Just a wrapper for the method below, first creating the ofstream.
161 void makeLaTeXFile(string const & filename,
162 string const & original_path,
164 bool only_body = false,
165 bool only_preamble = false);
167 void makeLaTeXFile(std::ostream & os,
168 string const & original_path,
170 bool only_body = false,
171 bool only_preamble = false);
172 /** LaTeX all paragraphs from par to endpar.
173 \param \a endpar if == 0 then to the end
175 void latexParagraphs(std::ostream & os, Paragraph * par,
176 Paragraph * endpar, TexRow & texrow, bool moving_arg = false) const;
178 void simpleDocBookOnePar(std::ostream &,
179 Paragraph * par, int & desc_on,
180 Paragraph::depth_type depth) const ;
182 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
183 Paragraph::depth_type depth);
185 void makeLinuxDocFile(string const & filename,
186 bool nice, bool only_body = false);
188 void makeDocBookFile(string const & filename,
189 bool nice, bool only_body = false);
190 /// Open SGML/XML tag.
191 int sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
192 string const & latexname) const;
193 /// Closes SGML/XML tag.
194 int sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
195 string const & latexname) const;
197 void sgmlError(Paragraph * par, int pos, string const & message) const;
199 /// returns the main language for the buffer (document)
200 Language const * getLanguage() const;
203 /// return true if the main lyx file does not need saving
204 bool isClean() const;
206 bool isBakClean() const;
208 bool isDepClean(string const & name) const;
210 /// mark the main lyx file as not needing saving
211 void markClean() const;
217 void markDepClean(string const & name);
220 void setUnnamed(bool flag = true);
225 /// Mark this buffer as dirty.
228 /// Returns the buffer's filename. It is always an absolute path.
229 string const & fileName() const;
231 /// Returns the the path where the buffer lives.
232 /// It is always an absolute path.
233 string const & filePath() const;
235 /** A transformed version of the file name, adequate for LaTeX.
236 \param no_path optional if \c true then the path is stripped.
238 string const getLatexName(bool no_path = true) const;
240 /// Get the name and type of the log.
241 std::pair<LogType, string> const getLogName() const;
243 /// Change name of buffer. Updates "read-only" flag.
244 void setFileName(string const & newfile);
246 /// Name of the document's parent
247 void setParentName(string const &);
249 /// Is buffer read-only?
250 bool isReadonly() const;
252 /// Set buffer read-only flag
253 void setReadonly(bool flag = true);
255 /// returns \c true if the buffer contains a LaTeX document
256 bool isLatex() const;
257 /// returns \c true if the buffer contains a LinuxDoc document
258 bool isLinuxDoc() const;
259 /// returns \c true if the buffer contains a DocBook document
260 bool isDocBook() const;
261 /** returns \c true if the buffer contains either a LinuxDoc
262 or DocBook document */
264 /// returns \c true if the buffer contains a Wed document
265 bool isLiterate() const;
267 /** Validate a buffer for LaTeX.
268 This validates the buffer, and returns a struct for use by
269 #makeLaTeX# and others. Its main use is to figure out what
270 commands and packages need to be included in the LaTeX file.
271 It (should) also check that the needed constructs are there
272 (i.e. that the \refs points to coresponding \labels). It
273 should perhaps inset "error" insets to help the user correct
276 void validate(LaTeXFeatures &) const;
279 string const getIncludeonlyList(char delim = ',');
281 std::vector<std::pair<string, string> > const getBibkeyList() const;
283 std::vector<string> const getLabelList() const;
285 /** This will clearly have to change later. Later we can have more
286 than one user per buffer. */
287 BufferView * getUser() const;
290 void changeLanguage(Language const * from, Language const * to);
292 bool isMultiLingual();
294 /// Does this mean that this is buffer local?
295 limited_stack<boost::shared_ptr<Undo> > undostack;
297 /// Does this mean that this is buffer local?
298 limited_stack<boost::shared_ptr<Undo> > redostack;
303 /** The list of paragraphs.
304 This is a linked list of paragraph, this list holds the
305 whole contents of the document.
307 ParagraphList paragraphs;
309 /// LyX version control object.
312 /// Where to put temporary files.
315 /** If we are writing a nice LaTeX file or not.
316 While writing as LaTeX, tells whether we are
317 doing a 'nice' LaTeX file */
320 /// Used when typesetting to place errorboxes.
322 /// Buffer-wide counter array
323 Counters & counters() const;
327 mutable bool lyx_clean;
329 /// is autosave needed
330 mutable bool bak_clean;
332 /// is this a unnamed file (New...)
335 /// is regenerating #.tex# necessary
336 DEPCLEAN * dep_clean;
341 /// name of the file the buffer is associated with.
344 /// The path to the document file.
347 /// Format number of buffer
349 /** A list of views using this buffer.
350 Why not keep a list of the BufferViews that use this buffer?
352 At least then we don't have to do a lot of magic like:
353 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
354 of the buffers in the list of users to do a #updateLayoutChoice#.
358 /// The pointer is const although its contents may not be
359 boost::scoped_ptr<Counters> const ctrs;
363 class inset_iterator {
365 typedef std::input_iterator_tag iterator_category;
366 typedef Inset value_type;
367 typedef ptrdiff_t difference_type;
368 typedef Inset * pointer;
369 typedef Inset & reference;
373 inset_iterator() : par(0) /*, it(0)*/ {}
375 inset_iterator(Paragraph * paragraph) : par(paragraph) {
379 inset_iterator(Paragraph * paragraph, lyx::pos_type pos);
381 inset_iterator & operator++() { // prefix ++
384 if (it == par->insetlist.end()) {
392 inset_iterator operator++(int) { // postfix ++
393 inset_iterator tmp(par, it.getPos());
396 if (it == par->insetlist.end()) {
405 Inset * operator*() { return it.getInset(); }
408 Paragraph * getPar() { return par; }
410 lyx::pos_type getPos() const { return it.getPos(); }
413 bool operator==(inset_iterator const & iter1,
414 inset_iterator const & iter2);
421 InsetList::iterator it;
425 inset_iterator inset_iterator_begin() {
426 return inset_iterator(&*paragraphs.begin());
429 inset_iterator inset_iterator_end() {
430 return inset_iterator();
433 inset_iterator inset_const_iterator_begin() const {
434 return inset_iterator(&*paragraphs.begin());
437 inset_iterator inset_const_iterator_end() const {
438 return inset_iterator();
442 ParIterator par_iterator_begin();
444 ParIterator par_iterator_end();
447 Inset * getInsetFromID(int id_arg) const;
452 void Buffer::addUser(BufferView * u)
459 void Buffer::delUser(BufferView *)
466 Language const * Buffer::getLanguage() const
468 return params.language;
473 bool Buffer::isClean() const
480 bool Buffer::isBakClean() const
487 void Buffer::markClean() const
493 // if the .lyx file has been saved, we don't need an
500 void Buffer::markBakClean()
507 void Buffer::setUnnamed(bool flag)
514 bool Buffer::isUnnamed()
521 void Buffer::markDirty()
528 DEPCLEAN * tmp = dep_clean;
537 string const & Buffer::fileName() const
544 string const & Buffer::filePath() const
551 bool Buffer::isReadonly() const
558 BufferView * Buffer::getUser() const
565 void Buffer::setParentName(string const & name)
567 params.parentname = name;
573 bool operator==(Buffer::inset_iterator const & iter1,
574 Buffer::inset_iterator const & iter2)
576 return iter1.par == iter2.par
577 && (iter1.par == 0 || iter1.it == iter2.it);
583 bool operator!=(Buffer::inset_iterator const & iter1,
584 Buffer::inset_iterator const & iter2)
586 return !(iter1 == iter2);