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 "paragraph.h"
29 #include <boost/shared_ptr.hpp>
38 #define NO_COMPABILITY 1
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 &, 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;
128 #ifndef NO_COMPABILITY
130 Inset * isErtInset(Paragraph * par, int pos) const;
132 void insertErtContents(Paragraph * par, int & pos,
133 bool set_inactive = true);
136 Paragraph * getParFromID(int id) const;
138 /// Parse a single inset.
139 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
142 Takes care of auto-save files and backup file if requested.
143 Returns \c true if the save is successful, \c false otherwise.
147 /// Write file. Returns \c false if unsuccesful.
148 bool writeFile(string const &) const;
151 void writeFileAscii(string const & , int);
153 void writeFileAscii(std::ostream &, int);
155 string const asciiParagraph(Paragraph const *, unsigned int linelen,
156 bool noparbreak = false) const;
157 /// Just a wrapper for the method below, first creating the ofstream.
158 void makeLaTeXFile(string const & filename,
159 string const & original_path,
161 bool only_body = false,
162 bool only_preamble = false);
164 void makeLaTeXFile(std::ostream & os,
165 string const & original_path,
167 bool only_body = false,
168 bool only_preamble = false);
169 /** LaTeX all paragraphs from par to endpar.
170 \param \a endpar if == 0 then to the end
172 void latexParagraphs(std::ostream & os, Paragraph * par,
173 Paragraph * endpar, TexRow & texrow, bool moving_arg = false) const;
175 void simpleDocBookOnePar(std::ostream &,
176 Paragraph * par, int & desc_on,
177 Paragraph::depth_type depth) const ;
179 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
180 Paragraph::depth_type depth);
182 void makeLinuxDocFile(string const & filename,
183 bool nice, bool only_body = false);
185 void makeDocBookFile(string const & filename,
186 bool nice, bool only_body = false);
187 /// Open SGML/XML tag.
188 int sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
189 string const & latexname) const;
190 /// Closes SGML/XML tag.
191 int sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
192 string const & latexname) const;
194 void sgmlError(Paragraph * par, int pos, string const & message) const;
196 /// returns the main language for the buffer (document)
197 Language const * getLanguage() const;
200 /// return true if the main lyx file does not need saving
201 bool isClean() const;
203 bool isBakClean() const;
205 bool isDepClean(string const & name) const;
207 /// mark the main lyx file as not needing saving
208 void markClean() const;
214 void markDepClean(string const & name);
217 void setUnnamed(bool flag = true);
222 /// Mark this buffer as dirty.
225 /// Returns the buffer's filename. It is always an absolute path.
226 string const & fileName() const;
228 /// Returns the the path where the buffer lives.
229 /// It is always an absolute path.
230 string const & filePath() const;
232 /** A transformed version of the file name, adequate for LaTeX.
233 \param no_path optional if \c true then the path is stripped.
235 string const getLatexName(bool no_path = true) const;
237 /// Get the name and type of the log.
238 std::pair<LogType, string> const getLogName() const;
240 /// Change name of buffer. Updates "read-only" flag.
241 void setFileName(string const & newfile);
243 /// Name of the document's parent
244 void setParentName(string const &);
246 /// Is buffer read-only?
247 bool isReadonly() const;
249 /// Set buffer read-only flag
250 void setReadonly(bool flag = true);
252 /// returns \c true if the buffer contains a LaTeX document
253 bool isLatex() const;
254 /// returns \c true if the buffer contains a LinuxDoc document
255 bool isLinuxDoc() const;
256 /// returns \c true if the buffer contains a DocBook document
257 bool isDocBook() const;
258 /** returns \c true if the buffer contains either a LinuxDoc
259 or DocBook document */
261 /// returns \c true if the buffer contains a Wed document
262 bool isLiterate() const;
264 /** Validate a buffer for LaTeX.
265 This validates the buffer, and returns a struct for use by
266 #makeLaTeX# and others. Its main use is to figure out what
267 commands and packages need to be included in the LaTeX file.
268 It (should) also check that the needed constructs are there
269 (i.e. that the \refs points to coresponding \labels). It
270 should perhaps inset "error" insets to help the user correct
273 void validate(LaTeXFeatures &) const;
276 string const getIncludeonlyList(char delim = ',');
278 std::vector<std::pair<string, string> > const getBibkeyList() const;
280 std::vector<string> const getLabelList();
282 /** This will clearly have to change later. Later we can have more
283 than one user per buffer. */
284 BufferView * getUser() const;
287 void changeLanguage(Language const * from, Language const * to);
289 bool isMultiLingual();
291 /// Does this mean that this is buffer local?
292 limited_stack<boost::shared_ptr<Undo> > undostack;
294 /// Does this mean that this is buffer local?
295 limited_stack<boost::shared_ptr<Undo> > redostack;
300 /** The list of paragraphs.
301 This is a linked list of paragraph, this list holds the
302 whole contents of the document.
304 Paragraph * paragraph;
306 /// LyX version control object.
309 /// Where to put temporary files.
312 /** If we are writing a nice LaTeX file or not.
313 While writing as LaTeX, tells whether we are
314 doing a 'nice' LaTeX file */
317 /// Used when typesetting to place errorboxes.
321 mutable bool lyx_clean;
323 /// is autosave needed
324 mutable bool bak_clean;
326 /// is this a unnamed file (New...)
329 /// is regenerating #.tex# necessary
330 DEPCLEAN * dep_clean;
335 /// name of the file the buffer is associated with.
338 /// The path to the document file.
341 /// Format number of buffer
343 /** A list of views using this buffer.
344 Why not keep a list of the BufferViews that use this buffer?
346 At least then we don't have to do a lot of magic like:
347 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
348 of the buffers in the list of users to do a #updateLayoutChoice#.
354 class inset_iterator {
356 typedef std::input_iterator_tag iterator_category;
357 typedef Inset value_type;
358 typedef ptrdiff_t difference_type;
359 typedef Inset * pointer;
360 typedef Inset & reference;
364 inset_iterator() : par(0) /*, it(0)*/ {}
366 inset_iterator(Paragraph * paragraph) : par(paragraph) {
370 inset_iterator(Paragraph * paragraph, lyx::pos_type pos);
372 inset_iterator & operator++() { // prefix ++
375 if (it == par->insetlist.end()) {
383 inset_iterator operator++(int) { // postfix ++
384 inset_iterator tmp(par, it.getPos());
387 if (it == par->insetlist.end()) {
396 Inset * operator*() { return it.getInset(); }
399 Paragraph * getPar() { return par; }
401 lyx::pos_type getPos() const { return it.getPos(); }
404 bool operator==(inset_iterator const & iter1,
405 inset_iterator const & iter2);
412 InsetList::iterator it;
416 inset_iterator inset_iterator_begin() {
417 return inset_iterator(paragraph);
420 inset_iterator inset_iterator_end() {
421 return inset_iterator();
424 inset_iterator inset_const_iterator_begin() const {
425 return inset_iterator(paragraph);
428 inset_iterator inset_const_iterator_end() const {
429 return inset_iterator();
433 ParIterator par_iterator_begin();
435 ParIterator par_iterator_end();
438 Inset * getInsetFromID(int id_arg) const;
443 void Buffer::addUser(BufferView * u)
450 void Buffer::delUser(BufferView *)
457 Language const * Buffer::getLanguage() const
459 return params.language;
464 bool Buffer::isClean() const
471 bool Buffer::isBakClean() const
478 void Buffer::markClean() const
484 // if the .lyx file has been saved, we don't need an
491 void Buffer::markBakClean()
498 void Buffer::setUnnamed(bool flag)
505 bool Buffer::isUnnamed()
512 void Buffer::markDirty()
519 DEPCLEAN * tmp = dep_clean;
528 string const & Buffer::fileName() const
535 string const & Buffer::filePath() const
542 bool Buffer::isReadonly() const
549 BufferView * Buffer::getUser() const
556 void Buffer::setParentName(string const & name)
558 params.parentname = name;
564 bool operator==(Buffer::inset_iterator const & iter1,
565 Buffer::inset_iterator const & iter2)
567 return iter1.par == iter2.par
568 && (iter1.par == 0 || iter1.it == iter2.it);
574 bool operator!=(Buffer::inset_iterator const & iter1,
575 Buffer::inset_iterator const & iter2)
577 return !(iter1 == iter2);