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>
39 #define NO_COMPABILITY 1
51 /** The buffer object.
52 This is the buffer object. It contains all the informations about
53 a document loaded into LyX. I am not sure if the class is complete or
54 minimal, probably not.
55 \author Lars Gullik Bjønnes
59 /// What type of log will \c getLogName() return?
61 latexlog, ///< LaTeX log
62 buildlog ///< Literate build log
67 \param b optional \c false by default
69 explicit Buffer(string const & file, bool b = false);
74 /** High-level interface to buffer functionality.
75 This function parses a command string and executes it
77 bool dispatch(string const & command, bool * result = 0);
79 /// Maybe we know the function already by number...
80 bool dispatch(int ac, string const & argument, bool * result = 0);
83 void resizeInsets(BufferView *);
85 /// Update window titles of all users.
86 void updateTitles() const;
88 /// Reset autosave timers for all users.
89 void resetAutosaveTimers() const;
91 /** Adds the BufferView to the users list.
92 Later this func will insert the \c BufferView into a real list,
93 not just setting a pointer.
95 void addUser(BufferView * u);
97 /** Removes the #BufferView# from the users list.
98 Since we only can have one at the moment, we just reset it.
100 void delUser(BufferView *);
105 /// Load the autosaved file.
106 void loadAutoSaveFile();
109 \param par if != 0 insert the file.
110 \return \c false if method fails.
112 bool readFile(LyXLex &, Paragraph * par = 0);
114 /** Reads a file without header.
115 \param par if != 0 insert the file.
116 \return \c false if file is not completely read.
118 bool readLyXformat2(LyXLex &, Paragraph * par = 0);
120 /// This parses a single LyXformat-Token.
121 bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
122 Paragraph *& return_par,
123 string const & token, int & pos,
124 Paragraph::depth_type & depth,
127 void insertStringAsLines(Paragraph *&, lyx::pos_type &,
128 LyXFont const &, string const &) const;
129 #ifndef NO_COMPABILITY
131 Inset * isErtInset(Paragraph * par, int pos) const;
133 void insertErtContents(Paragraph * par, int & pos,
134 bool set_inactive = true);
137 Paragraph * getParFromID(int id) const;
139 /// Parse a single inset.
140 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
143 Takes care of auto-save files and backup file if requested.
144 Returns \c true if the save is successful, \c false otherwise.
148 /// Write file. Returns \c false if unsuccesful.
149 bool writeFile(string const &) const;
152 void writeFileAscii(string const & , int);
154 void writeFileAscii(std::ostream &, int);
156 string const asciiParagraph(Paragraph const *, unsigned int linelen,
157 bool noparbreak = false) const;
158 /// Just a wrapper for the method below, first creating the ofstream.
159 void makeLaTeXFile(string const & filename,
160 string const & original_path,
162 bool only_body = false,
163 bool only_preamble = false);
165 void makeLaTeXFile(std::ostream & os,
166 string const & original_path,
168 bool only_body = false,
169 bool only_preamble = false);
170 /** LaTeX all paragraphs from par to endpar.
171 \param \a endpar if == 0 then to the end
173 void latexParagraphs(std::ostream & os, Paragraph * par,
174 Paragraph * endpar, TexRow & texrow, bool moving_arg = false) const;
176 void simpleDocBookOnePar(std::ostream &,
177 Paragraph * par, int & desc_on,
178 Paragraph::depth_type depth) const ;
180 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
181 Paragraph::depth_type depth);
183 void makeLinuxDocFile(string const & filename,
184 bool nice, bool only_body = false);
186 void makeDocBookFile(string const & filename,
187 bool nice, bool only_body = false);
188 /// Open SGML/XML tag.
189 int sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
190 string const & latexname) const;
191 /// Closes SGML/XML tag.
192 int sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
193 string const & latexname) const;
195 void sgmlError(Paragraph * par, int pos, string const & message) const;
197 /// returns the main language for the buffer (document)
198 Language const * getLanguage() const;
201 /// return true if the main lyx file does not need saving
202 bool isClean() const;
204 bool isBakClean() const;
206 bool isDepClean(string const & name) const;
208 /// mark the main lyx file as not needing saving
209 void markClean() const;
215 void markDepClean(string const & name);
218 void setUnnamed(bool flag = true);
223 /// Mark this buffer as dirty.
226 /// Returns the buffer's filename. It is always an absolute path.
227 string const & fileName() const;
229 /// Returns the the path where the buffer lives.
230 /// It is always an absolute path.
231 string const & filePath() const;
233 /** A transformed version of the file name, adequate for LaTeX.
234 \param no_path optional if \c true then the path is stripped.
236 string const getLatexName(bool no_path = true) const;
238 /// Get the name and type of the log.
239 std::pair<LogType, string> const getLogName() const;
241 /// Change name of buffer. Updates "read-only" flag.
242 void setFileName(string const & newfile);
244 /// Name of the document's parent
245 void setParentName(string const &);
247 /// Is buffer read-only?
248 bool isReadonly() const;
250 /// Set buffer read-only flag
251 void setReadonly(bool flag = true);
253 /// returns \c true if the buffer contains a LaTeX document
254 bool isLatex() const;
255 /// returns \c true if the buffer contains a LinuxDoc document
256 bool isLinuxDoc() const;
257 /// returns \c true if the buffer contains a DocBook document
258 bool isDocBook() const;
259 /** returns \c true if the buffer contains either a LinuxDoc
260 or DocBook document */
262 /// returns \c true if the buffer contains a Wed document
263 bool isLiterate() const;
265 /** Validate a buffer for LaTeX.
266 This validates the buffer, and returns a struct for use by
267 #makeLaTeX# and others. Its main use is to figure out what
268 commands and packages need to be included in the LaTeX file.
269 It (should) also check that the needed constructs are there
270 (i.e. that the \refs points to coresponding \labels). It
271 should perhaps inset "error" insets to help the user correct
274 void validate(LaTeXFeatures &) const;
277 string const getIncludeonlyList(char delim = ',');
279 std::vector<std::pair<string, string> > const getBibkeyList() const;
281 std::vector<string> const getLabelList();
283 /** This will clearly have to change later. Later we can have more
284 than one user per buffer. */
285 BufferView * getUser() const;
288 void changeLanguage(Language const * from, Language const * to);
290 bool isMultiLingual();
292 /// Does this mean that this is buffer local?
293 limited_stack<boost::shared_ptr<Undo> > undostack;
295 /// Does this mean that this is buffer local?
296 limited_stack<boost::shared_ptr<Undo> > redostack;
301 /** The list of paragraphs.
302 This is a linked list of paragraph, this list holds the
303 whole contents of the document.
305 ParagraphList paragraphs;
307 /// LyX version control object.
310 /// Where to put temporary files.
313 /** If we are writing a nice LaTeX file or not.
314 While writing as LaTeX, tells whether we are
315 doing a 'nice' LaTeX file */
318 /// Used when typesetting to place errorboxes.
322 mutable bool lyx_clean;
324 /// is autosave needed
325 mutable bool bak_clean;
327 /// is this a unnamed file (New...)
330 /// is regenerating #.tex# necessary
331 DEPCLEAN * dep_clean;
336 /// name of the file the buffer is associated with.
339 /// The path to the document file.
342 /// Format number of buffer
344 /** A list of views using this buffer.
345 Why not keep a list of the BufferViews that use this buffer?
347 At least then we don't have to do a lot of magic like:
348 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
349 of the buffers in the list of users to do a #updateLayoutChoice#.
355 class inset_iterator {
357 typedef std::input_iterator_tag iterator_category;
358 typedef Inset value_type;
359 typedef ptrdiff_t difference_type;
360 typedef Inset * pointer;
361 typedef Inset & reference;
365 inset_iterator() : par(0) /*, it(0)*/ {}
367 inset_iterator(Paragraph * paragraph) : par(paragraph) {
371 inset_iterator(Paragraph * paragraph, lyx::pos_type pos);
373 inset_iterator & operator++() { // prefix ++
376 if (it == par->insetlist.end()) {
384 inset_iterator operator++(int) { // postfix ++
385 inset_iterator tmp(par, it.getPos());
388 if (it == par->insetlist.end()) {
397 Inset * operator*() { return it.getInset(); }
400 Paragraph * getPar() { return par; }
402 lyx::pos_type getPos() const { return it.getPos(); }
405 bool operator==(inset_iterator const & iter1,
406 inset_iterator const & iter2);
413 InsetList::iterator it;
417 inset_iterator inset_iterator_begin() {
418 return inset_iterator(&*paragraphs.begin());
421 inset_iterator inset_iterator_end() {
422 return inset_iterator();
425 inset_iterator inset_const_iterator_begin() const {
426 return inset_iterator(&*paragraphs.begin());
429 inset_iterator inset_const_iterator_end() const {
430 return inset_iterator();
434 ParIterator par_iterator_begin();
436 ParIterator par_iterator_end();
439 Inset * getInsetFromID(int id_arg) const;
444 void Buffer::addUser(BufferView * u)
451 void Buffer::delUser(BufferView *)
458 Language const * Buffer::getLanguage() const
460 return params.language;
465 bool Buffer::isClean() const
472 bool Buffer::isBakClean() const
479 void Buffer::markClean() const
485 // if the .lyx file has been saved, we don't need an
492 void Buffer::markBakClean()
499 void Buffer::setUnnamed(bool flag)
506 bool Buffer::isUnnamed()
513 void Buffer::markDirty()
520 DEPCLEAN * tmp = dep_clean;
529 string const & Buffer::fileName() const
536 string const & Buffer::filePath() const
543 bool Buffer::isReadonly() const
550 BufferView * Buffer::getUser() const
557 void Buffer::setParentName(string const & name)
559 params.parentname = name;
565 bool operator==(Buffer::inset_iterator const & iter1,
566 Buffer::inset_iterator const & iter2)
568 return iter1.par == iter2.par
569 && (iter1.par == 0 || iter1.it == iter2.it);
575 bool operator!=(Buffer::inset_iterator const & iter1,
576 Buffer::inset_iterator const & iter2)
578 return !(iter1 == iter2);