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 /// Parse a single inset.
133 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
136 Takes care of auto-save files and backup file if requested.
137 Returns \c true if the save is successful, \c false otherwise.
141 /// Write file. Returns \c false if unsuccesful.
142 bool writeFile(string const &, bool) const;
145 void writeFileAscii(string const & , int);
147 void writeFileAscii(std::ostream &, int);
149 string const asciiParagraph(Paragraph const *,
150 unsigned int linelen) const;
152 void makeLaTeXFile(string const & filename,
153 string const & original_path,
154 bool nice, bool only_body = false);
155 /** LaTeX all paragraphs from par to endpar.
156 \param \a endpar if == 0 then to the end
158 void latexParagraphs(std::ostream & os, Paragraph * par,
159 Paragraph * endpar, TexRow & texrow) const;
162 void simpleDocBookOnePar(std::ostream &, string & extra,
163 Paragraph * par, int & desc_on,
164 Paragraph::depth_type depth) const ;
170 void makeLinuxDocFile(string const & filename,
171 bool nice, bool only_body = false);
173 void makeDocBookFile(string const & filename,
174 bool nice, bool only_body = false);
176 /// returns the main language for the buffer (document)
177 Language const * getLanguage() const;
179 bool isLyxClean() const;
181 bool isBakClean() const;
183 bool isDepClean(string const & name) const;
186 void markLyxClean() const;
192 void markDepClean(string const & name);
195 void setUnnamed(bool flag = true);
200 /// Mark this buffer as dirty.
203 /// Returns the buffers filename.
204 string const & fileName() const;
206 /** A transformed version of the file name, adequate for LaTeX.
207 \param no_path optional if \c true then the path is stripped.
209 string const getLatexName(bool no_path = true) const;
211 /// Get the name and type of the log.
212 std::pair<LogType, string> const getLogName() const;
214 /// Change name of buffer. Updates "read-only" flag.
215 void setFileName(string const & newfile);
217 /// Name of the document's parent
218 void setParentName(string const &);
220 /// Is buffer read-only?
221 bool isReadonly() const;
223 /// Set buffer read-only flag
224 void setReadonly(bool flag = true);
226 /// returns \c true if the buffer contains a LaTeX document
227 bool isLatex() const;
228 /// returns \c true if the buffer contains a LinuxDoc document
229 bool isLinuxDoc() const;
230 /// returns \c true if the buffer contains a DocBook document
231 bool isDocBook() const;
232 /** returns \c true if the buffer contains either a LinuxDoc
233 or DocBook document */
235 /// returns \c true if the buffer contains a Wed document
236 bool isLiterate() const;
239 void setPaperStuff();
241 /** Validate a buffer for LaTeX.
242 This validates the buffer, and returns a struct for use by
243 #makeLaTeX# and others. Its main use is to figure out what
244 commands and packages need to be included in the LaTeX file.
245 It (should) also check that the needed constructs are there
246 (i.e. that the \refs points to coresponding \labels). It
247 should perhaps inset "error" insets to help the user correct
250 void validate(LaTeXFeatures &) const;
253 string const getIncludeonlyList(char delim = ',');
255 std::vector<std::pair<string, string> > const getBibkeyList();
258 TocItem(Paragraph * p, int d, string const & s)
259 : par(p), depth(d), str(s) {}
268 typedef std::vector<TocItem> SingleList;
270 typedef std::map<string, SingleList> Lists;
272 Lists const getLists() const;
274 std::vector<string> const getLabelList();
276 /** This will clearly have to change later. Later we can have more
277 than one user per buffer. */
278 BufferView * getUser() const;
281 void changeLanguage(Language const * from, Language const * to);
283 bool isMultiLingual();
285 /// Does this mean that this is buffer local?
288 /// Does this mean that this is buffer local?
294 /** The list of paragraphs.
295 This is a linked list of paragraph, this list holds the
296 whole contents of the document.
298 Paragraph * paragraph;
300 /// LyX version control object.
303 /// Where to put temporary files.
306 /// The path to the document file.
309 /** If we are writing a nice LaTeX file or not.
310 While writing as LaTeX, tells whether we are
311 doing a 'nice' LaTeX file */
314 /// Used when typesetting to place errorboxes.
318 void docBookHandleCaption(std::ostream & os, string & inner_tag,
319 Paragraph::depth_type depth, int desc_on,
321 /// Open SGML/XML tag.
322 void sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth,
323 string const & latexname) const;
324 /// Closes SGML/XML tag.
325 void sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth,
326 string const & latexname) const;
328 void linuxDocError(Paragraph * par, int pos,
329 string const & message);
331 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
332 Paragraph::depth_type depth);
335 mutable bool lyx_clean;
337 /// is autosave needed
338 mutable bool bak_clean;
340 /// is this a unnamed file (New...)
343 /// is regenerating #.tex# necessary
344 DEPCLEAN * dep_clean;
349 /// name of the file the buffer is associated with.
352 /// Format number of buffer
354 /** A list of views using this buffer.
355 Why not keep a list of the BufferViews that use this buffer?
357 At least then we don't have to do a lot of magic like:
358 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
359 of the buffers in the list of users to do a #updateLayoutChoice#.
365 class inset_iterator {
367 typedef std::input_iterator_tag iterator_category;
368 typedef Inset value_type;
369 typedef ptrdiff_t difference_type;
370 typedef Inset * pointer;
371 typedef Inset & reference;
375 inset_iterator() : par(0) /*, it(0)*/ {}
377 inset_iterator(Paragraph * paragraph) : par(paragraph) {
381 inset_iterator(Paragraph * paragraph,
382 Paragraph::size_type pos);
384 inset_iterator & operator++() { // prefix ++
387 if (it == par->inset_iterator_end()) {
395 inset_iterator operator++(int) { // postfix ++
396 inset_iterator tmp(par, it.getPos());
399 if (it == par->inset_iterator_end()) {
407 Inset * operator*() { return *it; }
410 Paragraph * getPar() { return par; }
412 Paragraph::size_type getPos() const { return it.getPos(); }
415 bool operator==(inset_iterator const & iter1,
416 inset_iterator const & iter2);
423 Paragraph::inset_iterator it;
427 inset_iterator inset_iterator_begin() {
428 return inset_iterator(paragraph);
431 inset_iterator inset_iterator_end() {
432 return inset_iterator();
435 inset_iterator inset_const_iterator_begin() const {
436 return inset_iterator(paragraph);
439 inset_iterator inset_const_iterator_end() const {
440 return inset_iterator();
443 Inset * getInsetFromID(int id_arg) const;
448 void Buffer::addUser(BufferView * u)
455 void Buffer::delUser(BufferView *)
462 Language const * Buffer::getLanguage() const
464 return params.language;
469 bool Buffer::isLyxClean() const
476 bool Buffer::isBakClean() const
483 void Buffer::markLyxClean() const
489 // if the .lyx file has been saved, we don't need an
496 void Buffer::markBakClean()
503 void Buffer::setUnnamed(bool flag)
510 bool Buffer::isUnnamed()
517 void Buffer::markDirty()
524 DEPCLEAN * tmp = dep_clean;
533 string const & Buffer::fileName() const
540 bool Buffer::isReadonly() const
547 BufferView * Buffer::getUser() const
554 void Buffer::setParentName(string const & name)
556 params.parentname = name;
562 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
563 return a.par == b.par && a.str == b.str;
564 // No need to compare depth.
570 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
572 // No need to compare depth.
578 bool operator==(Buffer::inset_iterator const & iter1,
579 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) {
589 return !(iter1 == iter2);