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 "undostack.h"
24 #include "bufferparams.h"
26 #include "paragraph.h"
35 // When lyx 1.3.x starts we should enable this
36 // btw. we should also test this with 1.2 so that we
37 // do not get any surprises. (Lgb)
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);
78 /// Maybe we know the function already by number...
79 bool dispatch(int ac, string const & argument);
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 void insertErtContents(Paragraph * par, int & pos,
131 bool set_inactive = true);
134 Paragraph * getParFromID(int id) const;
136 /// Parse a single inset.
137 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
140 Takes care of auto-save files and backup file if requested.
141 Returns \c true if the save is successful, \c false otherwise.
145 /// Write file. Returns \c false if unsuccesful.
146 bool writeFile(string const &, bool) const;
149 void writeFileAscii(string const & , int);
151 void writeFileAscii(std::ostream &, int);
153 string const asciiParagraph(Paragraph const *, unsigned int linelen,
154 bool noparbreak = false) const;
156 void makeLaTeXFile(string const & filename,
157 string const & original_path,
158 bool nice, bool only_body = false);
159 /** LaTeX all paragraphs from par to endpar.
160 \param \a endpar if == 0 then to the end
162 void latexParagraphs(std::ostream & os, Paragraph * par,
163 Paragraph * endpar, TexRow & texrow) const;
165 void simpleDocBookOnePar(std::ostream &,
166 Paragraph * par, int & desc_on,
167 Paragraph::depth_type depth) const ;
169 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
170 Paragraph::depth_type depth);
172 void makeLinuxDocFile(string const & filename,
173 bool nice, bool only_body = false);
175 void makeDocBookFile(string const & filename,
176 bool nice, bool only_body = false);
177 /// Open SGML/XML tag.
178 void sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth,
179 string const & latexname) const;
180 /// Closes SGML/XML tag.
181 void sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth,
182 string const & latexname) const;
184 void sgmlError(Paragraph * par, int pos, string const & message) const;
186 /// returns the main language for the buffer (document)
187 Language const * getLanguage() const;
191 bool isLyxClean() const;
193 bool isBakClean() const;
195 bool isDepClean(string const & name) const;
198 void markLyxClean() const;
204 void markDepClean(string const & name);
207 void setUnnamed(bool flag = true);
212 /// Mark this buffer as dirty.
215 /// Returns the buffers filename.
216 string const & fileName() const;
218 /** A transformed version of the file name, adequate for LaTeX.
219 \param no_path optional if \c true then the path is stripped.
221 string const getLatexName(bool no_path = true) const;
223 /// Get the name and type of the log.
224 std::pair<LogType, string> const getLogName() const;
226 /// Change name of buffer. Updates "read-only" flag.
227 void setFileName(string const & newfile);
229 /// Name of the document's parent
230 void setParentName(string const &);
232 /// Is buffer read-only?
233 bool isReadonly() const;
235 /// Set buffer read-only flag
236 void setReadonly(bool flag = true);
238 /// returns \c true if the buffer contains a LaTeX document
239 bool isLatex() const;
240 /// returns \c true if the buffer contains a LinuxDoc document
241 bool isLinuxDoc() const;
242 /// returns \c true if the buffer contains a DocBook document
243 bool isDocBook() const;
244 /** returns \c true if the buffer contains either a LinuxDoc
245 or DocBook document */
247 /// returns \c true if the buffer contains a Wed document
248 bool isLiterate() const;
250 /** Validate a buffer for LaTeX.
251 This validates the buffer, and returns a struct for use by
252 #makeLaTeX# and others. Its main use is to figure out what
253 commands and packages need to be included in the LaTeX file.
254 It (should) also check that the needed constructs are there
255 (i.e. that the \refs points to coresponding \labels). It
256 should perhaps inset "error" insets to help the user correct
259 void validate(LaTeXFeatures &) const;
262 string const getIncludeonlyList(char delim = ',');
264 std::vector<std::pair<string, string> > const getBibkeyList();
267 TocItem(Paragraph * p, int d, string const & s)
268 : par(p), depth(d), str(s) {}
277 typedef std::vector<TocItem> SingleList;
279 typedef std::map<string, SingleList> Lists;
281 Lists const getLists() const;
283 std::vector<string> const getLabelList();
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?
297 /// Does this mean that this is buffer local?
303 /** The list of paragraphs.
304 This is a linked list of paragraph, this list holds the
305 whole contents of the document.
307 Paragraph * paragraph;
309 /// LyX version control object.
312 /// Where to put temporary files.
315 /// The path to the document file.
318 /** If we are writing a nice LaTeX file or not.
319 While writing as LaTeX, tells whether we are
320 doing a 'nice' LaTeX file */
323 /// Used when typesetting to place errorboxes.
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 /// Format number of buffer
346 /** A list of views using this buffer.
347 Why not keep a list of the BufferViews that use this buffer?
349 At least then we don't have to do a lot of magic like:
350 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
351 of the buffers in the list of users to do a #updateLayoutChoice#.
357 class inset_iterator {
359 typedef std::input_iterator_tag iterator_category;
360 typedef Inset value_type;
361 typedef ptrdiff_t difference_type;
362 typedef Inset * pointer;
363 typedef Inset & reference;
367 inset_iterator() : par(0) /*, it(0)*/ {}
369 inset_iterator(Paragraph * paragraph) : par(paragraph) {
373 inset_iterator(Paragraph * paragraph, lyx::pos_type pos);
375 inset_iterator & operator++() { // prefix ++
378 if (it == par->inset_iterator_end()) {
386 inset_iterator operator++(int) { // postfix ++
387 inset_iterator tmp(par, it.getPos());
390 if (it == par->inset_iterator_end()) {
398 Inset * operator*() { return *it; }
401 Paragraph * getPar() { return par; }
403 lyx::pos_type getPos() const { return it.getPos(); }
406 bool operator==(inset_iterator const & iter1,
407 inset_iterator const & iter2);
414 Paragraph::inset_iterator it;
418 inset_iterator inset_iterator_begin() {
419 return inset_iterator(paragraph);
422 inset_iterator inset_iterator_end() {
423 return inset_iterator();
426 inset_iterator inset_const_iterator_begin() const {
427 return inset_iterator(paragraph);
430 inset_iterator inset_const_iterator_end() const {
431 return inset_iterator();
435 ParIterator par_iterator_begin();
437 ParIterator par_iterator_end();
440 Inset * getInsetFromID(int id_arg) const;
445 void Buffer::addUser(BufferView * u)
452 void Buffer::delUser(BufferView *)
459 Language const * Buffer::getLanguage() const
461 return params.language;
466 bool Buffer::isLyxClean() const
473 bool Buffer::isBakClean() const
480 void Buffer::markLyxClean() const
486 // if the .lyx file has been saved, we don't need an
493 void Buffer::markBakClean()
500 void Buffer::setUnnamed(bool flag)
507 bool Buffer::isUnnamed()
514 void Buffer::markDirty()
521 DEPCLEAN * tmp = dep_clean;
530 string const & Buffer::fileName() const
537 bool Buffer::isReadonly() const
544 BufferView * Buffer::getUser() const
551 void Buffer::setParentName(string const & name)
553 params.parentname = name;
559 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b)
561 return a.par == b.par && a.str == b.str;
562 // No need to compare depth.
568 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b)
571 // No need to compare depth.
577 bool operator==(Buffer::inset_iterator const & iter1,
578 Buffer::inset_iterator const & iter2)
580 return iter1.par == iter2.par
581 && (iter1.par == 0 || iter1.it == iter2.it);
587 bool operator!=(Buffer::inset_iterator const & iter1,
588 Buffer::inset_iterator const & iter2)
590 return !(iter1 == iter2);