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 *&, Paragraph::size_type &,
127 LyXFont const &, string const &) const;
128 #ifndef NO_COMPABILITY
130 void insertErtContents(Paragraph * par, int & pos,
131 LyXFont const & font,
132 bool set_inactive = true);
135 Paragraph * getParFromID(int id) const;
137 /// Parse a single inset.
138 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
141 Takes care of auto-save files and backup file if requested.
142 Returns \c true if the save is successful, \c false otherwise.
146 /// Write file. Returns \c false if unsuccesful.
147 bool writeFile(string const &, bool) const;
150 void writeFileAscii(string const & , int);
152 void writeFileAscii(std::ostream &, int);
154 string const asciiParagraph(Paragraph const *,
155 unsigned int linelen) const;
157 void makeLaTeXFile(string const & filename,
158 string const & original_path,
159 bool nice, bool only_body = false);
160 /** LaTeX all paragraphs from par to endpar.
161 \param \a endpar if == 0 then to the end
163 void latexParagraphs(std::ostream & os, Paragraph * par,
164 Paragraph * endpar, TexRow & texrow) const;
167 void simpleDocBookOnePar(std::ostream &, string & extra,
168 Paragraph * par, int & desc_on,
169 Paragraph::depth_type depth) const ;
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);
181 /// returns the main language for the buffer (document)
182 Language const * getLanguage() const;
184 bool isLyxClean() const;
186 bool isBakClean() const;
188 bool isDepClean(string const & name) const;
191 void markLyxClean() const;
197 void markDepClean(string const & name);
200 void setUnnamed(bool flag = true);
205 /// Mark this buffer as dirty.
208 /// Returns the buffers filename.
209 string const & fileName() const;
211 /** A transformed version of the file name, adequate for LaTeX.
212 \param no_path optional if \c true then the path is stripped.
214 string const getLatexName(bool no_path = true) const;
216 /// Get the name and type of the log.
217 std::pair<LogType, string> const getLogName() const;
219 /// Change name of buffer. Updates "read-only" flag.
220 void setFileName(string const & newfile);
222 /// Name of the document's parent
223 void setParentName(string const &);
225 /// Is buffer read-only?
226 bool isReadonly() const;
228 /// Set buffer read-only flag
229 void setReadonly(bool flag = true);
231 /// returns \c true if the buffer contains a LaTeX document
232 bool isLatex() const;
233 /// returns \c true if the buffer contains a LinuxDoc document
234 bool isLinuxDoc() const;
235 /// returns \c true if the buffer contains a DocBook document
236 bool isDocBook() const;
237 /** returns \c true if the buffer contains either a LinuxDoc
238 or DocBook document */
240 /// returns \c true if the buffer contains a Wed document
241 bool isLiterate() const;
243 /** Validate a buffer for LaTeX.
244 This validates the buffer, and returns a struct for use by
245 #makeLaTeX# and others. Its main use is to figure out what
246 commands and packages need to be included in the LaTeX file.
247 It (should) also check that the needed constructs are there
248 (i.e. that the \refs points to coresponding \labels). It
249 should perhaps inset "error" insets to help the user correct
252 void validate(LaTeXFeatures &) const;
255 string const getIncludeonlyList(char delim = ',');
257 std::vector<std::pair<string, string> > const getBibkeyList();
260 TocItem(Paragraph * p, int d, string const & s)
261 : par(p), depth(d), str(s) {}
270 typedef std::vector<TocItem> SingleList;
272 typedef std::map<string, SingleList> Lists;
274 Lists const getLists() const;
276 std::vector<string> const getLabelList();
278 /** This will clearly have to change later. Later we can have more
279 than one user per buffer. */
280 BufferView * getUser() const;
283 void changeLanguage(Language const * from, Language const * to);
285 bool isMultiLingual();
287 /// Does this mean that this is buffer local?
290 /// Does this mean that this is buffer local?
296 /** The list of paragraphs.
297 This is a linked list of paragraph, this list holds the
298 whole contents of the document.
300 Paragraph * paragraph;
302 /// LyX version control object.
305 /// Where to put temporary files.
308 /// The path to the document file.
311 /** If we are writing a nice LaTeX file or not.
312 While writing as LaTeX, tells whether we are
313 doing a 'nice' LaTeX file */
316 /// Used when typesetting to place errorboxes.
319 /// Open SGML/XML tag.
320 void sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth,
321 string const & latexname) const;
322 /// Closes SGML/XML tag.
323 void sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth,
324 string const & latexname) const;
326 void linuxDocError(Paragraph * par, int pos,
327 string const & message);
329 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
330 Paragraph::depth_type depth);
333 mutable bool lyx_clean;
335 /// is autosave needed
336 mutable bool bak_clean;
338 /// is this a unnamed file (New...)
341 /// is regenerating #.tex# necessary
342 DEPCLEAN * dep_clean;
347 /// name of the file the buffer is associated with.
350 /// Format number of buffer
352 /** A list of views using this buffer.
353 Why not keep a list of the BufferViews that use this buffer?
355 At least then we don't have to do a lot of magic like:
356 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
357 of the buffers in the list of users to do a #updateLayoutChoice#.
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,
380 Paragraph::size_type pos);
382 inset_iterator & operator++() { // prefix ++
385 if (it == par->inset_iterator_end()) {
393 inset_iterator operator++(int) { // postfix ++
394 inset_iterator tmp(par, it.getPos());
397 if (it == par->inset_iterator_end()) {
405 Inset * operator*() { return *it; }
408 Paragraph * getPar() { return par; }
410 Paragraph::size_type getPos() const { return it.getPos(); }
413 bool operator==(inset_iterator const & iter1,
414 inset_iterator const & iter2);
421 Paragraph::inset_iterator it;
425 inset_iterator inset_iterator_begin() {
426 return inset_iterator(paragraph);
429 inset_iterator inset_iterator_end() {
430 return inset_iterator();
433 inset_iterator inset_const_iterator_begin() const {
434 return inset_iterator(paragraph);
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::isLyxClean() const
480 bool Buffer::isBakClean() const
487 void Buffer::markLyxClean() 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 bool Buffer::isReadonly() const
551 BufferView * Buffer::getUser() const
558 void Buffer::setParentName(string const & name)
560 params.parentname = name;
566 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
567 return a.par == b.par && a.str == b.str;
568 // No need to compare depth.
574 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
576 // No need to compare depth.
582 bool operator==(Buffer::inset_iterator const & iter1,
583 Buffer::inset_iterator const & iter2) {
584 return iter1.par == iter2.par
585 && (iter1.par == 0 || iter1.it == iter2.it);
591 bool operator!=(Buffer::inset_iterator const & iter1,
592 Buffer::inset_iterator const & iter2) {
593 return !(iter1 == iter2);