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 /// Parse a single inset.
129 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
132 Takes care of auto-save files and backup file if requested.
133 Returns \c true if the save is successful, \c false otherwise.
137 /// Write file. Returns \c false if unsuccesful.
138 bool writeFile(string const &, bool) const;
141 void writeFileAscii(string const & , int);
143 void writeFileAscii(std::ostream &, int);
145 string const asciiParagraph(Paragraph const *,
146 unsigned int linelen) const;
148 void makeLaTeXFile(string const & filename,
149 string const & original_path,
150 bool nice, bool only_body = false);
151 /** LaTeX all paragraphs from par to endpar.
152 \param \a endpar if == 0 then to the end
154 void latexParagraphs(std::ostream & os, Paragraph * par,
155 Paragraph * endpar, TexRow & texrow) const;
158 void simpleDocBookOnePar(std::ostream &, string & extra,
159 Paragraph * par, int & desc_on,
160 Paragraph::depth_type depth) const ;
166 void makeLinuxDocFile(string const & filename,
167 bool nice, bool only_body = false);
169 void makeDocBookFile(string const & filename,
170 bool nice, bool only_body = false);
172 /// returns the main language for the buffer (document)
173 Language const * getLanguage() const;
175 bool isLyxClean() const;
177 bool isBakClean() const;
179 bool isDepClean(string const & name) const;
182 void markLyxClean() const;
188 void markDepClean(string const & name);
191 void setUnnamed(bool flag = true);
196 /// Mark this buffer as dirty.
199 /// Returns the buffers filename.
200 string const & fileName() const;
202 /** A transformed version of the file name, adequate for LaTeX.
203 \param no_path optional if \c true then the path is stripped.
205 string const getLatexName(bool no_path = true) const;
207 /// Get the name and type of the log.
208 std::pair<LogType, string> const getLogName() const;
210 /// Change name of buffer. Updates "read-only" flag.
211 void setFileName(string const & newfile);
213 /// Name of the document's parent
214 void setParentName(string const &);
216 /// Is buffer read-only?
217 bool isReadonly() const;
219 /// Set buffer read-only flag
220 void setReadonly(bool flag = true);
222 /// returns \c true if the buffer contains a LaTeX document
223 bool isLatex() const;
224 /// returns \c true if the buffer contains a LinuxDoc document
225 bool isLinuxDoc() const;
226 /// returns \c true if the buffer contains a DocBook document
227 bool isDocBook() const;
228 /** returns \c true if the buffer contains either a LinuxDoc
229 or DocBook document */
231 /// returns \c true if the buffer contains a Wed document
232 bool isLiterate() const;
235 void setPaperStuff();
237 /** Validate a buffer for LaTeX.
238 This validates the buffer, and returns a struct for use by
239 #makeLaTeX# and others. Its main use is to figure out what
240 commands and packages need to be included in the LaTeX file.
241 It (should) also check that the needed constructs are there
242 (i.e. that the \refs points to coresponding \labels). It
243 should perhaps inset "error" insets to help the user correct
246 void validate(LaTeXFeatures &) const;
249 string const getIncludeonlyList(char delim = ',');
251 std::vector<std::pair<string, string> > const getBibkeyList();
254 TocItem(Paragraph * p, int d, string const & s)
255 : par(p), depth(d), str(s) {}
264 typedef std::vector<TocItem> SingleList;
266 typedef std::map<string, SingleList> Lists;
268 Lists const getLists() const;
270 std::vector<string> const getLabelList();
272 /** This will clearly have to change later. Later we can have more
273 than one user per buffer. */
274 BufferView * getUser() const;
277 void changeLanguage(Language const * from, Language const * to);
279 bool isMultiLingual();
281 /// Does this mean that this is buffer local?
284 /// Does this mean that this is buffer local?
290 /** The list of paragraphs.
291 This is a linked list of paragraph, this list holds the
292 whole contents of the document.
294 Paragraph * paragraph;
296 /// LyX version control object.
299 /// Where to put temporary files.
302 /// The path to the document file.
305 /** If we are writing a nice LaTeX file or not.
306 While writing as LaTeX, tells whether we are
307 doing a 'nice' LaTeX file */
310 /// Used when typesetting to place errorboxes.
314 void docBookHandleCaption(std::ostream & os, string & inner_tag,
315 Paragraph::depth_type depth, int desc_on,
317 /// Open SGML/XML tag.
318 void sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth,
319 string const & latexname) const;
320 /// Closes SGML/XML tag.
321 void sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth,
322 string const & latexname) const;
324 void linuxDocError(Paragraph * par, int pos,
325 string const & message);
327 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
328 Paragraph::depth_type depth);
331 mutable bool lyx_clean;
333 /// is autosave needed
334 mutable bool bak_clean;
336 /// is this a unnamed file (New...)
339 /// is regenerating #.tex# necessary
340 DEPCLEAN * dep_clean;
345 /// name of the file the buffer is associated with.
348 /// Format number of buffer
350 /** A list of views using this buffer.
351 Why not keep a list of the BufferViews that use this buffer?
353 At least then we don't have to do a lot of magic like:
354 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
355 of the buffers in the list of users to do a #updateLayoutChoice#.
361 class inset_iterator {
363 typedef std::input_iterator_tag iterator_category;
364 typedef Inset value_type;
365 typedef ptrdiff_t difference_type;
366 typedef Inset * pointer;
367 typedef Inset & reference;
371 inset_iterator() : par(0) /*, it(0)*/ {}
373 inset_iterator(Paragraph * paragraph) : par(paragraph) {
377 inset_iterator(Paragraph * paragraph,
378 Paragraph::size_type pos);
380 inset_iterator & operator++() { // prefix ++
383 if (it == par->inset_iterator_end()) {
391 inset_iterator operator++(int) { // postfix ++
392 inset_iterator tmp(par, it.getPos());
395 if (it == par->inset_iterator_end()) {
403 Inset * operator*() { return *it; }
406 Paragraph * getPar() { return par; }
408 Paragraph::size_type getPos() const { return it.getPos(); }
411 bool operator==(inset_iterator const & iter1,
412 inset_iterator const & iter2);
419 Paragraph::inset_iterator it;
423 inset_iterator inset_iterator_begin() {
424 return inset_iterator(paragraph);
427 inset_iterator inset_iterator_end() {
428 return inset_iterator();
434 void Buffer::addUser(BufferView * u)
441 void Buffer::delUser(BufferView *)
448 Language const * Buffer::getLanguage() const
450 return params.language;
455 bool Buffer::isLyxClean() const
462 bool Buffer::isBakClean() const
469 void Buffer::markLyxClean() const
475 // if the .lyx file has been saved, we don't need an
482 void Buffer::markBakClean()
489 void Buffer::setUnnamed(bool flag)
496 bool Buffer::isUnnamed()
503 void Buffer::markDirty()
510 DEPCLEAN * tmp = dep_clean;
519 string const & Buffer::fileName() const
526 bool Buffer::isReadonly() const
533 BufferView * Buffer::getUser() const
540 void Buffer::setParentName(string const & name)
542 params.parentname = name;
548 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
549 return a.par == b.par && a.str == b.str;
550 // No need to compare depth.
556 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
558 // No need to compare depth.
564 bool operator==(Buffer::inset_iterator const & iter1,
565 Buffer::inset_iterator const & iter2) {
566 return iter1.par == iter2.par
567 && (iter1.par == 0 || iter1.it == iter2.it);
573 bool operator!=(Buffer::inset_iterator const & iter1,
574 Buffer::inset_iterator const & iter2) {
575 return !(iter1 == iter2);