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 /** Save the buffer's parameters as user default.
74 This function saves a file \c user_lyxdir/templates/defaults.lyx
75 which parameters are those of the current buffer. This file
76 is used as a default template when creating a new
77 file. Returns \c true on success.
79 bool saveParamsAsDefaults();
81 /** High-level interface to buffer functionality.
82 This function parses a command string and executes it
84 bool dispatch(string const & command);
86 /// Maybe we know the function already by number...
87 bool dispatch(int ac, string const & argument);
90 void resizeInsets(BufferView *);
92 /// Update window titles of all users.
93 void updateTitles() const;
95 /// Reset autosave timers for all users.
96 void resetAutosaveTimers() const;
98 /** Adds the BufferView to the users list.
99 Later this func will insert the \c BufferView into a real list,
100 not just setting a pointer.
102 void addUser(BufferView * u);
104 /** Removes the #BufferView# from the users list.
105 Since we only can have one at the moment, we just reset it.
107 void delUser(BufferView *);
112 /// Load the autosaved file.
113 void loadAutoSaveFile();
116 \param par if != 0 insert the file.
117 \return \c false if method fails.
119 bool readFile(LyXLex &, Paragraph * par = 0);
121 /** Reads a file without header.
122 \param par if != 0 insert the file.
123 \return \c false if file is not completely read.
125 bool readLyXformat2(LyXLex &, Paragraph * par = 0);
127 /// This parses a single LyXformat-Token.
128 bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
129 Paragraph *& return_par,
130 string const & token, int & pos,
131 Paragraph::depth_type & depth,
134 void insertStringAsLines(Paragraph *&, Paragraph::size_type &,
135 LyXFont const &, string const &) const;
136 #ifndef NO_COMPABILITY
138 void insertErtContents(Paragraph * par, int & pos,
139 LyXFont const & font,
140 bool set_inactive = true);
143 Paragraph * getParFromID(int id) const;
145 /// Parse a single inset.
146 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
149 Takes care of auto-save files and backup file if requested.
150 Returns \c true if the save is successful, \c false otherwise.
154 /// Write file. Returns \c false if unsuccesful.
155 bool writeFile(string const &, bool) const;
158 void writeFileAscii(string const & , int);
160 void writeFileAscii(std::ostream &, int);
162 string const asciiParagraph(Paragraph const *,
163 unsigned int linelen) const;
165 void makeLaTeXFile(string const & filename,
166 string const & original_path,
167 bool nice, bool only_body = false);
168 /** LaTeX all paragraphs from par to endpar.
169 \param \a endpar if == 0 then to the end
171 void latexParagraphs(std::ostream & os, Paragraph * par,
172 Paragraph * endpar, TexRow & texrow) const;
175 void simpleDocBookOnePar(std::ostream &, string & extra,
176 Paragraph * par, int & desc_on,
177 Paragraph::depth_type depth) const ;
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);
189 /// returns the main language for the buffer (document)
190 Language const * getLanguage() const;
192 bool isLyxClean() const;
194 bool isBakClean() const;
196 bool isDepClean(string const & name) const;
199 void markLyxClean() const;
205 void markDepClean(string const & name);
208 void setUnnamed(bool flag = true);
213 /// Mark this buffer as dirty.
216 /// Returns the buffers filename.
217 string const & fileName() const;
219 /** A transformed version of the file name, adequate for LaTeX.
220 \param no_path optional if \c true then the path is stripped.
222 string const getLatexName(bool no_path = true) const;
224 /// Get the name and type of the log.
225 std::pair<LogType, string> const getLogName() const;
227 /// Change name of buffer. Updates "read-only" flag.
228 void setFileName(string const & newfile);
230 /// Name of the document's parent
231 void setParentName(string const &);
233 /// Is buffer read-only?
234 bool isReadonly() const;
236 /// Set buffer read-only flag
237 void setReadonly(bool flag = true);
239 /// returns \c true if the buffer contains a LaTeX document
240 bool isLatex() const;
241 /// returns \c true if the buffer contains a LinuxDoc document
242 bool isLinuxDoc() const;
243 /// returns \c true if the buffer contains a DocBook document
244 bool isDocBook() const;
245 /** returns \c true if the buffer contains either a LinuxDoc
246 or DocBook document */
248 /// returns \c true if the buffer contains a Wed document
249 bool isLiterate() const;
252 void setPaperStuff();
254 /** Validate a buffer for LaTeX.
255 This validates the buffer, and returns a struct for use by
256 #makeLaTeX# and others. Its main use is to figure out what
257 commands and packages need to be included in the LaTeX file.
258 It (should) also check that the needed constructs are there
259 (i.e. that the \refs points to coresponding \labels). It
260 should perhaps inset "error" insets to help the user correct
263 void validate(LaTeXFeatures &) const;
266 string const getIncludeonlyList(char delim = ',');
268 std::vector<std::pair<string, string> > const getBibkeyList();
271 TocItem(Paragraph * p, int d, string const & s)
272 : par(p), depth(d), str(s) {}
281 typedef std::vector<TocItem> SingleList;
283 typedef std::map<string, SingleList> Lists;
285 Lists const getLists() const;
287 std::vector<string> const getLabelList();
289 /** This will clearly have to change later. Later we can have more
290 than one user per buffer. */
291 BufferView * getUser() const;
294 void changeLanguage(Language const * from, Language const * to);
296 bool isMultiLingual();
298 /// Does this mean that this is buffer local?
301 /// Does this mean that this is buffer local?
307 /** The list of paragraphs.
308 This is a linked list of paragraph, this list holds the
309 whole contents of the document.
311 Paragraph * paragraph;
313 /// LyX version control object.
316 /// Where to put temporary files.
319 /// The path to the document file.
322 /** If we are writing a nice LaTeX file or not.
323 While writing as LaTeX, tells whether we are
324 doing a 'nice' LaTeX file */
327 /// Used when typesetting to place errorboxes.
330 /// Open SGML/XML tag.
331 void sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth,
332 string const & latexname) const;
333 /// Closes SGML/XML tag.
334 void sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth,
335 string const & latexname) const;
337 void linuxDocError(Paragraph * par, int pos,
338 string const & message);
340 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
341 Paragraph::depth_type depth);
344 mutable bool lyx_clean;
346 /// is autosave needed
347 mutable bool bak_clean;
349 /// is this a unnamed file (New...)
352 /// is regenerating #.tex# necessary
353 DEPCLEAN * dep_clean;
358 /// name of the file the buffer is associated with.
361 /// Format number of buffer
363 /** A list of views using this buffer.
364 Why not keep a list of the BufferViews that use this buffer?
366 At least then we don't have to do a lot of magic like:
367 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
368 of the buffers in the list of users to do a #updateLayoutChoice#.
374 class inset_iterator {
376 typedef std::input_iterator_tag iterator_category;
377 typedef Inset value_type;
378 typedef ptrdiff_t difference_type;
379 typedef Inset * pointer;
380 typedef Inset & reference;
384 inset_iterator() : par(0) /*, it(0)*/ {}
386 inset_iterator(Paragraph * paragraph) : par(paragraph) {
390 inset_iterator(Paragraph * paragraph,
391 Paragraph::size_type pos);
393 inset_iterator & operator++() { // prefix ++
396 if (it == par->inset_iterator_end()) {
404 inset_iterator operator++(int) { // postfix ++
405 inset_iterator tmp(par, it.getPos());
408 if (it == par->inset_iterator_end()) {
416 Inset * operator*() { return *it; }
419 Paragraph * getPar() { return par; }
421 Paragraph::size_type getPos() const { return it.getPos(); }
424 bool operator==(inset_iterator const & iter1,
425 inset_iterator const & iter2);
432 Paragraph::inset_iterator it;
436 inset_iterator inset_iterator_begin() {
437 return inset_iterator(paragraph);
440 inset_iterator inset_iterator_end() {
441 return inset_iterator();
444 inset_iterator inset_const_iterator_begin() const {
445 return inset_iterator(paragraph);
448 inset_iterator inset_const_iterator_end() const {
449 return inset_iterator();
453 ParIterator par_iterator_begin();
455 ParIterator par_iterator_end();
458 Inset * getInsetFromID(int id_arg) const;
463 void Buffer::addUser(BufferView * u)
470 void Buffer::delUser(BufferView *)
477 Language const * Buffer::getLanguage() const
479 return params.language;
484 bool Buffer::isLyxClean() const
491 bool Buffer::isBakClean() const
498 void Buffer::markLyxClean() const
504 // if the .lyx file has been saved, we don't need an
511 void Buffer::markBakClean()
518 void Buffer::setUnnamed(bool flag)
525 bool Buffer::isUnnamed()
532 void Buffer::markDirty()
539 DEPCLEAN * tmp = dep_clean;
548 string const & Buffer::fileName() const
555 bool Buffer::isReadonly() const
562 BufferView * Buffer::getUser() const
569 void Buffer::setParentName(string const & name)
571 params.parentname = name;
577 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
578 return a.par == b.par && a.str == b.str;
579 // No need to compare depth.
585 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
587 // No need to compare depth.
593 bool operator==(Buffer::inset_iterator const & iter1,
594 Buffer::inset_iterator const & iter2) {
595 return iter1.par == iter2.par
596 && (iter1.par == 0 || iter1.it == iter2.it);
602 bool operator!=(Buffer::inset_iterator const & iter1,
603 Buffer::inset_iterator const & iter2) {
604 return !(iter1 == iter2);