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 insertStringAsLines(Paragraph *&, Paragraph::size_type &,
129 LyXFont const &, string const &) const;
131 void insertErtContents(Paragraph * par, int & pos,
132 LyXFont const & font,
133 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;
244 void setPaperStuff();
246 /** Validate a buffer for LaTeX.
247 This validates the buffer, and returns a struct for use by
248 #makeLaTeX# and others. Its main use is to figure out what
249 commands and packages need to be included in the LaTeX file.
250 It (should) also check that the needed constructs are there
251 (i.e. that the \refs points to coresponding \labels). It
252 should perhaps inset "error" insets to help the user correct
255 void validate(LaTeXFeatures &) const;
258 string const getIncludeonlyList(char delim = ',');
260 std::vector<std::pair<string, string> > const getBibkeyList();
263 TocItem(Paragraph * p, int d, string const & s)
264 : par(p), depth(d), str(s) {}
273 typedef std::vector<TocItem> SingleList;
275 typedef std::map<string, SingleList> Lists;
277 Lists const getLists() const;
279 std::vector<string> const getLabelList();
281 /** This will clearly have to change later. Later we can have more
282 than one user per buffer. */
283 BufferView * getUser() const;
286 void changeLanguage(Language const * from, Language const * to);
288 bool isMultiLingual();
290 /// Does this mean that this is buffer local?
293 /// Does this mean that this is buffer local?
299 /** The list of paragraphs.
300 This is a linked list of paragraph, this list holds the
301 whole contents of the document.
303 Paragraph * paragraph;
305 /// LyX version control object.
308 /// Where to put temporary files.
311 /// The path to the document file.
314 /** If we are writing a nice LaTeX file or not.
315 While writing as LaTeX, tells whether we are
316 doing a 'nice' LaTeX file */
319 /// Used when typesetting to place errorboxes.
323 void docBookHandleCaption(std::ostream & os, string & inner_tag,
324 Paragraph::depth_type depth, int desc_on,
326 /// Open SGML/XML tag.
327 void sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth,
328 string const & latexname) const;
329 /// Closes SGML/XML tag.
330 void sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth,
331 string const & latexname) const;
333 void linuxDocError(Paragraph * par, int pos,
334 string const & message);
336 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
337 Paragraph::depth_type depth);
340 mutable bool lyx_clean;
342 /// is autosave needed
343 mutable bool bak_clean;
345 /// is this a unnamed file (New...)
348 /// is regenerating #.tex# necessary
349 DEPCLEAN * dep_clean;
354 /// name of the file the buffer is associated with.
357 /// Format number of buffer
359 /** A list of views using this buffer.
360 Why not keep a list of the BufferViews that use this buffer?
362 At least then we don't have to do a lot of magic like:
363 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
364 of the buffers in the list of users to do a #updateLayoutChoice#.
370 class inset_iterator {
372 typedef std::input_iterator_tag iterator_category;
373 typedef Inset value_type;
374 typedef ptrdiff_t difference_type;
375 typedef Inset * pointer;
376 typedef Inset & reference;
380 inset_iterator() : par(0) /*, it(0)*/ {}
382 inset_iterator(Paragraph * paragraph) : par(paragraph) {
386 inset_iterator(Paragraph * paragraph,
387 Paragraph::size_type pos);
389 inset_iterator & operator++() { // prefix ++
392 if (it == par->inset_iterator_end()) {
400 inset_iterator operator++(int) { // postfix ++
401 inset_iterator tmp(par, it.getPos());
404 if (it == par->inset_iterator_end()) {
412 Inset * operator*() { return *it; }
415 Paragraph * getPar() { return par; }
417 Paragraph::size_type getPos() const { return it.getPos(); }
420 bool operator==(inset_iterator const & iter1,
421 inset_iterator const & iter2);
428 Paragraph::inset_iterator it;
432 inset_iterator inset_iterator_begin() {
433 return inset_iterator(paragraph);
436 inset_iterator inset_iterator_end() {
437 return inset_iterator();
440 inset_iterator inset_const_iterator_begin() const {
441 return inset_iterator(paragraph);
444 inset_iterator inset_const_iterator_end() const {
445 return inset_iterator();
448 Inset * getInsetFromID(int id_arg) const;
453 void Buffer::addUser(BufferView * u)
460 void Buffer::delUser(BufferView *)
467 Language const * Buffer::getLanguage() const
469 return params.language;
474 bool Buffer::isLyxClean() const
481 bool Buffer::isBakClean() const
488 void Buffer::markLyxClean() const
494 // if the .lyx file has been saved, we don't need an
501 void Buffer::markBakClean()
508 void Buffer::setUnnamed(bool flag)
515 bool Buffer::isUnnamed()
522 void Buffer::markDirty()
529 DEPCLEAN * tmp = dep_clean;
538 string const & Buffer::fileName() const
545 bool Buffer::isReadonly() const
552 BufferView * Buffer::getUser() const
559 void Buffer::setParentName(string const & name)
561 params.parentname = name;
567 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
568 return a.par == b.par && a.str == b.str;
569 // No need to compare depth.
575 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
577 // No need to compare depth.
583 bool operator==(Buffer::inset_iterator const & iter1,
584 Buffer::inset_iterator const & iter2) {
585 return iter1.par == iter2.par
586 && (iter1.par == 0 || iter1.it == iter2.it);
592 bool operator!=(Buffer::inset_iterator const & iter1,
593 Buffer::inset_iterator const & iter2) {
594 return !(iter1 == iter2);