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"
34 // When lyx 1.3.x starts we should enable this
35 // btw. we should also test this with 1.2 so that we
36 // do not get any surprises. (Lgb)
37 //#define NO_COMPABILITY 1
49 /** The buffer object.
50 This is the buffer object. It contains all the informations about
51 a document loaded into LyX. I am not sure if the class is complete or
52 minimal, probably not.
53 \author Lars Gullik Bjønnes
57 /// What type of log will \c getLogName() return?
59 latexlog, ///< LaTeX log
60 buildlog ///< Literate build log
65 \param b optional \c false by default
67 explicit Buffer(string const & file, bool b = false);
72 /** Save the buffer's parameters as user default.
73 This function saves a file \c user_lyxdir/templates/defaults.lyx
74 which parameters are those of the current buffer. This file
75 is used as a default template when creating a new
76 file. Returns \c true on success.
78 bool saveParamsAsDefaults();
80 /** High-level interface to buffer functionality.
81 This function parses a command string and executes it
83 bool dispatch(string const & command);
85 /// Maybe we know the function already by number...
86 bool dispatch(int ac, string const & argument);
89 void resizeInsets(BufferView *);
91 /// Update window titles of all users.
92 void updateTitles() const;
94 /// Reset autosave timers for all users.
95 void resetAutosaveTimers() const;
97 /** Adds the BufferView to the users list.
98 Later this func will insert the \c BufferView into a real list,
99 not just setting a pointer.
101 void addUser(BufferView * u);
103 /** Removes the #BufferView# from the users list.
104 Since we only can have one at the moment, we just reset it.
106 void delUser(BufferView *);
111 /// Load the autosaved file.
112 void loadAutoSaveFile();
115 \param par if != 0 insert the file.
116 \return \c false if method fails.
118 bool readFile(LyXLex &, Paragraph * par = 0);
120 /** Reads a file without header.
121 \param par if != 0 insert the file.
122 \return \c false if file is not completely read.
124 bool readLyXformat2(LyXLex &, Paragraph * par = 0);
126 /// This parses a single LyXformat-Token.
127 bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
128 Paragraph *& return_par,
129 string const & token, int & pos,
130 Paragraph::depth_type & depth,
133 void insertStringAsLines(Paragraph *&, Paragraph::size_type &,
134 LyXFont const &, string const &) const;
135 #ifndef NO_COMPABILITY
137 void insertErtContents(Paragraph * par, int & pos,
138 LyXFont const & font,
139 bool set_inactive = true);
142 Paragraph * getParFromID(int id) const;
144 /// Parse a single inset.
145 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
148 Takes care of auto-save files and backup file if requested.
149 Returns \c true if the save is successful, \c false otherwise.
153 /// Write file. Returns \c false if unsuccesful.
154 bool writeFile(string const &, bool) const;
157 void writeFileAscii(string const & , int);
159 void writeFileAscii(std::ostream &, int);
161 string const asciiParagraph(Paragraph const *,
162 unsigned int linelen) const;
164 void makeLaTeXFile(string const & filename,
165 string const & original_path,
166 bool nice, bool only_body = false);
167 /** LaTeX all paragraphs from par to endpar.
168 \param \a endpar if == 0 then to the end
170 void latexParagraphs(std::ostream & os, Paragraph * par,
171 Paragraph * endpar, TexRow & texrow) const;
174 void simpleDocBookOnePar(std::ostream &, string & extra,
175 Paragraph * par, int & desc_on,
176 Paragraph::depth_type depth) const ;
182 void makeLinuxDocFile(string const & filename,
183 bool nice, bool only_body = false);
185 void makeDocBookFile(string const & filename,
186 bool nice, bool only_body = false);
188 /// returns the main language for the buffer (document)
189 Language const * getLanguage() const;
191 bool isLyxClean() const;
193 bool isBakClean() const;
195 bool isDepClean(string const & name) const;
198 void markLyxClean() const;
204 void markDepClean(string const & name);
207 void setUnnamed(bool flag = true);
212 /// Mark this buffer as dirty.
215 /// Returns the buffers filename.
216 string const & fileName() const;
218 /** A transformed version of the file name, adequate for LaTeX.
219 \param no_path optional if \c true then the path is stripped.
221 string const getLatexName(bool no_path = true) const;
223 /// Get the name and type of the log.
224 std::pair<LogType, string> const getLogName() const;
226 /// Change name of buffer. Updates "read-only" flag.
227 void setFileName(string const & newfile);
229 /// Name of the document's parent
230 void setParentName(string const &);
232 /// Is buffer read-only?
233 bool isReadonly() const;
235 /// Set buffer read-only flag
236 void setReadonly(bool flag = true);
238 /// returns \c true if the buffer contains a LaTeX document
239 bool isLatex() const;
240 /// returns \c true if the buffer contains a LinuxDoc document
241 bool isLinuxDoc() const;
242 /// returns \c true if the buffer contains a DocBook document
243 bool isDocBook() const;
244 /** returns \c true if the buffer contains either a LinuxDoc
245 or DocBook document */
247 /// returns \c true if the buffer contains a Wed document
248 bool isLiterate() const;
251 void setPaperStuff();
253 /** Validate a buffer for LaTeX.
254 This validates the buffer, and returns a struct for use by
255 #makeLaTeX# and others. Its main use is to figure out what
256 commands and packages need to be included in the LaTeX file.
257 It (should) also check that the needed constructs are there
258 (i.e. that the \refs points to coresponding \labels). It
259 should perhaps inset "error" insets to help the user correct
262 void validate(LaTeXFeatures &) const;
265 string const getIncludeonlyList(char delim = ',');
267 std::vector<std::pair<string, string> > const getBibkeyList();
270 TocItem(Paragraph * p, int d, string const & s)
271 : par(p), depth(d), str(s) {}
280 typedef std::vector<TocItem> SingleList;
282 typedef std::map<string, SingleList> Lists;
284 Lists const getLists() const;
286 std::vector<string> const getLabelList();
288 /** This will clearly have to change later. Later we can have more
289 than one user per buffer. */
290 BufferView * getUser() const;
293 void changeLanguage(Language const * from, Language const * to);
295 bool isMultiLingual();
297 /// Does this mean that this is buffer local?
300 /// Does this mean that this is buffer local?
306 /** The list of paragraphs.
307 This is a linked list of paragraph, this list holds the
308 whole contents of the document.
310 Paragraph * paragraph;
312 /// LyX version control object.
315 /// Where to put temporary files.
318 /// The path to the document file.
321 /** If we are writing a nice LaTeX file or not.
322 While writing as LaTeX, tells whether we are
323 doing a 'nice' LaTeX file */
326 /// Used when typesetting to place errorboxes.
330 void docBookHandleCaption(std::ostream & os, string & inner_tag,
331 Paragraph::depth_type depth, int desc_on,
333 /// Open SGML/XML tag.
334 void sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth,
335 string const & latexname) const;
336 /// Closes SGML/XML tag.
337 void sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth,
338 string const & latexname) const;
340 void linuxDocError(Paragraph * par, int pos,
341 string const & message);
343 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
344 Paragraph::depth_type depth);
347 mutable bool lyx_clean;
349 /// is autosave needed
350 mutable bool bak_clean;
352 /// is this a unnamed file (New...)
355 /// is regenerating #.tex# necessary
356 DEPCLEAN * dep_clean;
361 /// name of the file the buffer is associated with.
364 /// Format number of buffer
366 /** A list of views using this buffer.
367 Why not keep a list of the BufferViews that use this buffer?
369 At least then we don't have to do a lot of magic like:
370 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
371 of the buffers in the list of users to do a #updateLayoutChoice#.
377 class inset_iterator {
379 typedef std::input_iterator_tag iterator_category;
380 typedef Inset value_type;
381 typedef ptrdiff_t difference_type;
382 typedef Inset * pointer;
383 typedef Inset & reference;
387 inset_iterator() : par(0) /*, it(0)*/ {}
389 inset_iterator(Paragraph * paragraph) : par(paragraph) {
393 inset_iterator(Paragraph * paragraph,
394 Paragraph::size_type pos);
396 inset_iterator & operator++() { // prefix ++
399 if (it == par->inset_iterator_end()) {
407 inset_iterator operator++(int) { // postfix ++
408 inset_iterator tmp(par, it.getPos());
411 if (it == par->inset_iterator_end()) {
419 Inset * operator*() { return *it; }
422 Paragraph * getPar() { return par; }
424 Paragraph::size_type getPos() const { return it.getPos(); }
427 bool operator==(inset_iterator const & iter1,
428 inset_iterator const & iter2);
435 Paragraph::inset_iterator it;
439 inset_iterator inset_iterator_begin() {
440 return inset_iterator(paragraph);
443 inset_iterator inset_iterator_end() {
444 return inset_iterator();
447 inset_iterator inset_const_iterator_begin() const {
448 return inset_iterator(paragraph);
451 inset_iterator inset_const_iterator_end() const {
452 return inset_iterator();
455 Inset * getInsetFromID(int id_arg) const;
460 void Buffer::addUser(BufferView * u)
467 void Buffer::delUser(BufferView *)
474 Language const * Buffer::getLanguage() const
476 return params.language;
481 bool Buffer::isLyxClean() const
488 bool Buffer::isBakClean() const
495 void Buffer::markLyxClean() const
501 // if the .lyx file has been saved, we don't need an
508 void Buffer::markBakClean()
515 void Buffer::setUnnamed(bool flag)
522 bool Buffer::isUnnamed()
529 void Buffer::markDirty()
536 DEPCLEAN * tmp = dep_clean;
545 string const & Buffer::fileName() const
552 bool Buffer::isReadonly() const
559 BufferView * Buffer::getUser() const
566 void Buffer::setParentName(string const & name)
568 params.parentname = name;
574 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
575 return a.par == b.par && a.str == b.str;
576 // No need to compare depth.
582 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
584 // No need to compare depth.
590 bool operator==(Buffer::inset_iterator const & iter1,
591 Buffer::inset_iterator const & iter2) {
592 return iter1.par == iter2.par
593 && (iter1.par == 0 || iter1.it == iter2.it);
599 bool operator!=(Buffer::inset_iterator const & iter1,
600 Buffer::inset_iterator const & iter2) {
601 return !(iter1 == iter2);