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"
44 /** The buffer object.
45 This is the buffer object. It contains all the informations about
46 a document loaded into LyX. I am not sure if the class is complete or
47 minimal, probably not.
48 \author Lars Gullik Bjønnes
52 /// What type of log will \c getLogName() return?
54 latexlog, ///< LaTeX log
55 buildlog ///< Literate build log
60 \param b optional \c false by default
62 explicit Buffer(string const & file, bool b = false);
67 /** Save the buffer's parameters as user default.
68 This function saves a file \c user_lyxdir/templates/defaults.lyx
69 which parameters are those of the current buffer. This file
70 is used as a default template when creating a new
71 file. Returns \c true on success.
73 bool saveParamsAsDefaults();
75 /** High-level interface to buffer functionality.
76 This function parses a command string and executes it
78 bool dispatch(string const & command);
80 /// Maybe we know the function already by number...
81 bool dispatch(int ac, string const & argument);
84 void resizeInsets(BufferView *);
86 /// Update window titles of all users.
87 void updateTitles() const;
89 /// Reset autosave timers for all users.
90 void resetAutosaveTimers() const;
92 /** Adds the BufferView to the users list.
93 Later this func will insert the \c BufferView into a real list,
94 not just setting a pointer.
96 void addUser(BufferView * u);
98 /** Removes the #BufferView# from the users list.
99 Since we only can have one at the moment, we just reset it.
101 void delUser(BufferView *);
106 /// Load the autosaved file.
107 void loadAutoSaveFile();
110 \param par if != 0 insert the file.
111 \return \c false if method fails.
113 bool readFile(LyXLex &, Paragraph * par = 0);
115 /** Reads a file without header.
116 \param par if != 0 insert the file.
117 \return \c false if file is not completely read.
119 bool readLyXformat2(LyXLex &, Paragraph * par = 0);
121 /// This parses a single LyXformat-Token.
122 bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
123 Paragraph *& return_par,
124 string const & token, int & pos,
125 Paragraph::depth_type & depth,
128 void insertErtContents(Paragraph * par, int & pos,
129 LyXFont const & font,
130 bool set_inactive = true);
132 Paragraph * getParFromID(int id) const;
134 /// Parse a single inset.
135 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
138 Takes care of auto-save files and backup file if requested.
139 Returns \c true if the save is successful, \c false otherwise.
143 /// Write file. Returns \c false if unsuccesful.
144 bool writeFile(string const &, bool) const;
147 void writeFileAscii(string const & , int);
149 void writeFileAscii(std::ostream &, int);
151 string const asciiParagraph(Paragraph const *,
152 unsigned int linelen) const;
154 void makeLaTeXFile(string const & filename,
155 string const & original_path,
156 bool nice, bool only_body = false);
157 /** LaTeX all paragraphs from par to endpar.
158 \param \a endpar if == 0 then to the end
160 void latexParagraphs(std::ostream & os, Paragraph * par,
161 Paragraph * endpar, TexRow & texrow) const;
164 void simpleDocBookOnePar(std::ostream &, string & extra,
165 Paragraph * par, int & desc_on,
166 Paragraph::depth_type depth) const ;
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);
178 /// returns the main language for the buffer (document)
179 Language const * getLanguage() const;
181 bool isLyxClean() const;
183 bool isBakClean() const;
185 bool isDepClean(string const & name) const;
188 void markLyxClean() const;
194 void markDepClean(string const & name);
197 void setUnnamed(bool flag = true);
202 /// Mark this buffer as dirty.
205 /// Returns the buffers filename.
206 string const & fileName() const;
208 /** A transformed version of the file name, adequate for LaTeX.
209 \param no_path optional if \c true then the path is stripped.
211 string const getLatexName(bool no_path = true) const;
213 /// Get the name and type of the log.
214 std::pair<LogType, string> const getLogName() const;
216 /// Change name of buffer. Updates "read-only" flag.
217 void setFileName(string const & newfile);
219 /// Name of the document's parent
220 void setParentName(string const &);
222 /// Is buffer read-only?
223 bool isReadonly() const;
225 /// Set buffer read-only flag
226 void setReadonly(bool flag = true);
228 /// returns \c true if the buffer contains a LaTeX document
229 bool isLatex() const;
230 /// returns \c true if the buffer contains a LinuxDoc document
231 bool isLinuxDoc() const;
232 /// returns \c true if the buffer contains a DocBook document
233 bool isDocBook() const;
234 /** returns \c true if the buffer contains either a LinuxDoc
235 or DocBook document */
237 /// returns \c true if the buffer contains a Wed document
238 bool isLiterate() const;
241 void setPaperStuff();
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.
320 void docBookHandleCaption(std::ostream & os, string & inner_tag,
321 Paragraph::depth_type depth, int desc_on,
323 /// Open SGML/XML tag.
324 void sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth,
325 string const & latexname) const;
326 /// Closes SGML/XML tag.
327 void sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth,
328 string const & latexname) const;
330 void linuxDocError(Paragraph * par, int pos,
331 string const & message);
333 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
334 Paragraph::depth_type depth);
337 mutable bool lyx_clean;
339 /// is autosave needed
340 mutable bool bak_clean;
342 /// is this a unnamed file (New...)
345 /// is regenerating #.tex# necessary
346 DEPCLEAN * dep_clean;
351 /// name of the file the buffer is associated with.
354 /// Format number of buffer
356 /** A list of views using this buffer.
357 Why not keep a list of the BufferViews that use this buffer?
359 At least then we don't have to do a lot of magic like:
360 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
361 of the buffers in the list of users to do a #updateLayoutChoice#.
367 class inset_iterator {
369 typedef std::input_iterator_tag iterator_category;
370 typedef Inset value_type;
371 typedef ptrdiff_t difference_type;
372 typedef Inset * pointer;
373 typedef Inset & reference;
377 inset_iterator() : par(0) /*, it(0)*/ {}
379 inset_iterator(Paragraph * paragraph) : par(paragraph) {
383 inset_iterator(Paragraph * paragraph,
384 Paragraph::size_type pos);
386 inset_iterator & operator++() { // prefix ++
389 if (it == par->inset_iterator_end()) {
397 inset_iterator operator++(int) { // postfix ++
398 inset_iterator tmp(par, it.getPos());
401 if (it == par->inset_iterator_end()) {
409 Inset * operator*() { return *it; }
412 Paragraph * getPar() { return par; }
414 Paragraph::size_type getPos() const { return it.getPos(); }
417 bool operator==(inset_iterator const & iter1,
418 inset_iterator const & iter2);
425 Paragraph::inset_iterator it;
429 inset_iterator inset_iterator_begin() {
430 return inset_iterator(paragraph);
433 inset_iterator inset_iterator_end() {
434 return inset_iterator();
437 inset_iterator inset_const_iterator_begin() const {
438 return inset_iterator(paragraph);
441 inset_iterator inset_const_iterator_end() const {
442 return inset_iterator();
445 Inset * getInsetFromID(int id_arg) const;
450 void Buffer::addUser(BufferView * u)
457 void Buffer::delUser(BufferView *)
464 Language const * Buffer::getLanguage() const
466 return params.language;
471 bool Buffer::isLyxClean() const
478 bool Buffer::isBakClean() const
485 void Buffer::markLyxClean() const
491 // if the .lyx file has been saved, we don't need an
498 void Buffer::markBakClean()
505 void Buffer::setUnnamed(bool flag)
512 bool Buffer::isUnnamed()
519 void Buffer::markDirty()
526 DEPCLEAN * tmp = dep_clean;
535 string const & Buffer::fileName() 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::TocItem const & a, Buffer::TocItem const & b) {
565 return a.par == b.par && a.str == b.str;
566 // No need to compare depth.
572 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
574 // No need to compare depth.
580 bool operator==(Buffer::inset_iterator const & iter1,
581 Buffer::inset_iterator const & iter2) {
582 return iter1.par == iter2.par
583 && (iter1.par == 0 || iter1.it == iter2.it);
589 bool operator!=(Buffer::inset_iterator const & iter1,
590 Buffer::inset_iterator const & iter2) {
591 return !(iter1 == iter2);