2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
8 * This file is Copyleft 1996
11 * ====================================================== */
15 // 23/03/98 Heinrich Bauer (heinrich.bauer@t-mobil.de)
16 // Spots marked "changed Heinrich Bauer, 23/03/98" modified due to the
17 // following bug: dvi file export did not work after printing (or previewing)
18 // and vice versa as long as the same file was concerned. This happened
19 // every time the LyX-file was left unchanged between the two actions mentioned
31 #include "BufferView.h"
33 #include "bufferparams.h"
52 /** The buffer object.
53 The is is the buffer object. It contains all the informations about
54 a document loaded into LyX. I am not sure if the class is complete or
55 minimal, probably not.
56 \author Lars Gullik Bjønnes
60 /// What type of log will \c getLogName() return?
62 latexlog, ///< LaTeX log
63 buildlog ///< Literate build log
68 \param b optional \c false by default
70 explicit Buffer(string const & file, bool b = false);
75 /** Save the buffer's parameters as user default.
76 This function saves a file \c user_lyxdir/templates/defaults.lyx
77 which parameters are those of the current buffer. This file
78 is used as a default template when creating a new
79 file. Returns \c true on success.
81 bool saveParamsAsDefaults();
83 /** High-level interface to buffer functionality.
84 This function parses a command string and executes it
86 bool Dispatch(string const & command);
88 /// Maybe we know the function already by number...
89 bool Dispatch(int ac, string const & argument);
91 /// Should be changed to work for a list.
94 void resizeInsets(BufferView *);
96 /// Update window titles of all users.
97 void updateTitles() const;
99 /// Reset autosave timers for all users.
100 void resetAutosaveTimers() const;
102 /** Adds the BufferView to the users list.
103 Later this func will insert the \c BufferView into a real list,
104 not just setting a pointer.
106 void addUser(BufferView * u);
108 /** Removes the #BufferView# from the users list.
109 Since we only can have one at the moment, we just reset it.
111 void delUser(BufferView *);
116 /// Load the autosaved file.
117 void loadAutoSaveFile();
120 \param par if != 0 insert the file.
121 \return \c false if method fails.
123 bool readFile(LyXLex &, LyXParagraph * par = 0);
125 /** Reads a file without header.
126 \param par if != 0 insert the file.
127 \return \c false if file is not completely read.
129 bool readLyXformat2(LyXLex &, LyXParagraph * par = 0);
131 /// This parses a single LyXformat-Token.
132 bool parseSingleLyXformat2Token(LyXLex &, LyXParagraph *& par,
133 LyXParagraph *& return_par,
134 string const & token, int & pos,
135 char & depth, LyXFont &
137 ,LyXParagraph::footnote_flag &,
138 LyXParagraph::footnote_kind &
142 /// Parse a single inset.
143 void readInset(LyXLex &, LyXParagraph *& par, int & pos, LyXFont &);
146 Takes care of auto-save files and backup file if requested.
147 Returns \c true if the save is successful, \c false otherwise.
151 /// Write file. Returns \c false if unsuccesful.
152 bool writeFile(string const &, bool) const;
155 void writeFileAscii(string const & , int);
157 void writeFileAscii(std::ostream &, int);
159 string const asciiParagraph(LyXParagraph const *,
160 unsigned int linelen) const;
162 void makeLaTeXFile(string const & filename,
163 string const & original_path,
164 bool nice, bool only_body = false);
165 /** LaTeX all paragraphs from par to endpar.
166 \param \a endpar if == 0 then to the end
168 void latexParagraphs(std::ostream & os, LyXParagraph * par,
169 LyXParagraph * endpar, TexRow & texrow) const;
172 void SimpleDocBookOnePar(std::ostream &, string & extra,
173 LyXParagraph * par, int & desc_on,
180 void makeLinuxDocFile(string const & filename,
181 bool nice, bool only_body = false);
183 void makeDocBookFile(string const & filename,
184 bool nice, bool only_body = false);
186 /// returns the main language for the buffer (document)
187 Language const * GetLanguage() const;
189 bool isLyxClean() const;
191 bool isBakClean() const;
193 bool isDepClean(string const & name) const;
196 void markLyxClean() const;
202 void markDepClean(string const & name);
205 void setUnnamed(bool flag=true);
210 /// Mark this buffer as dirty.
213 /// Returns the buffers filename.
214 string const & fileName() const;
216 /** A transformed version of the file name, adequate for LaTeX.
217 \param no_path optional if \c true then the path is stripped.
219 string const getLatexName(bool no_path = true) const;
221 /// Get the name and type of the log.
222 std::pair<LogType, string> const getLogName() const;
224 /// Change name of buffer. Updates "read-only" flag.
225 void setFileName(string const & newfile);
227 /// Name of the document's parent
228 void setParentName(string const &);
230 /// Is buffer read-only?
231 bool isReadonly() const;
233 /// Set buffer read-only flag
234 void setReadonly(bool flag = true);
236 /// returns \c true if the buffer contains a LaTeX document
237 bool isLatex() const;
238 /// returns \c true if the buffer contains a LinuxDoc document
239 bool isLinuxDoc() const;
240 /// returns \c true if the buffer contains a DocBook document
241 bool isDocBook() const;
242 /** returns \c true if the buffer contains either a LinuxDoc
243 or DocBook document */
245 /// returns \c true if the buffer contains a Wed document
246 bool isLiterate() const;
249 void setPaperStuff();
251 /** Validate a buffer for LaTeX.
252 This validates the buffer, and returns a struct for use by
253 #makeLaTeX# and others. Its main use is to figure out what
254 commands and packages need to be included in the LaTeX file.
255 It (should) also check that the needed constructs are there
256 (i.e. that the \refs points to coresponding \labels). It
257 should perhaps inset "error" insets to help the user correct
260 void validate(LaTeXFeatures &) const;
263 string const getIncludeonlyList(char delim = ',');
265 std::vector<std::pair<string, string> > const getBibkeyList();
268 TocItem(LyXParagraph * p, int d, string const & s)
269 : par(p), depth(d), str(s) {}
278 typedef std::vector<TocItem> SingleList;
280 typedef std::map<string, SingleList> Lists;
282 Lists const getLists() const;
284 std::vector<string> const getLabelList();
286 /** This will clearly have to change later. Later we can have more
287 than one user per buffer. */
288 BufferView * getUser() const;
291 void ChangeLanguage(Language const * from, Language const * to);
293 bool isMultiLingual();
295 /// Does this mean that this is buffer local?
298 /// Does this mean that this is buffer local?
304 /** The list of paragraphs.
305 This is a linked list of paragraph, this list holds the
306 whole contents of the document.
308 LyXParagraph * paragraph;
310 /// LyX version control object.
313 /// Where to put temporary files.
316 /// The path to the document file.
319 /** If we are writing a nice LaTeX file or not.
320 While writing as LaTeX, tells whether we are
321 doing a 'nice' LaTeX file */
324 /// Used when typesetting to place errorboxes.
329 void linuxDocHandleFootnote(std::ostream & os,
330 LyXParagraph * & par, int depth);
333 void DocBookHandleCaption(std::ostream & os, string & inner_tag,
334 int depth, int desc_on,
335 LyXParagraph * & par);
338 void DocBookHandleFootnote(std::ostream & os,
339 LyXParagraph * & par, int depth);
342 void sgmlOpenTag(std::ostream & os, int depth,
343 string const & latexname) const;
345 void sgmlCloseTag(std::ostream & os, int depth,
346 string const & latexname) const;
348 void LinuxDocError(LyXParagraph * par, int pos,
349 string const & message);
351 void SimpleLinuxDocOnePar(std::ostream & os, LyXParagraph * par,
352 int desc_on, int depth);
355 void push_tag(std::ostream & os, string const & tag,
356 int & pos, char stack[5][3]);
359 void pop_tag(std::ostream & os, string const & tag,
360 int & pos, char stack[5][3]);
363 mutable bool lyx_clean;
365 /// is autosave needed
366 mutable bool bak_clean;
368 /// is this a unnamed file (New...)
371 /// is regenerating #.tex# necessary
372 DEPCLEAN * dep_clean;
377 /// name of the file the buffer is associated with.
380 /// Format number of buffer
382 /** A list of views using this buffer.
383 Why not keep a list of the BufferViews that use this buffer?
385 At least then we don't have to do a lot of magic like:
386 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
387 of the buffers in the list of users to do a #updateLayoutChoice#.
393 class inset_iterator {
395 typedef std::input_iterator_tag iterator_category;
396 typedef Inset value_type;
397 typedef ptrdiff_t difference_type;
398 typedef Inset * pointer;
399 typedef Inset & reference;
403 inset_iterator() : par(0) /*, it(0)*/ {}
405 inset_iterator(LyXParagraph * paragraph) : par(paragraph) {
409 inset_iterator(LyXParagraph * paragraph,
410 LyXParagraph::size_type pos);
412 inset_iterator & operator++() { // prefix ++
415 if (it == par->inset_iterator_end()) {
427 inset_iterator operator++(int) { // postfix ++
428 inset_iterator tmp(par, it.getPos());
431 if (it == par->inset_iterator_end()) {
443 Inset * operator*() { return *it; }
446 LyXParagraph * getPar() { return par; }
448 LyXParagraph::size_type getPos() const { return it.getPos(); }
451 bool operator==(inset_iterator const & iter1,
452 inset_iterator const & iter2);
459 LyXParagraph::inset_iterator it;
463 inset_iterator inset_iterator_begin() {
464 return inset_iterator(paragraph);
467 inset_iterator inset_iterator_end() {
468 return inset_iterator();
474 void Buffer::addUser(BufferView * u)
481 void Buffer::delUser(BufferView *)
488 void Buffer::redraw()
491 users->fitCursor(users->text);
496 Language const * Buffer::GetLanguage() const
498 return params.language;
503 bool Buffer::isLyxClean() const
510 bool Buffer::isBakClean() const
517 void Buffer::markLyxClean() const
523 // if the .lyx file has been saved, we don't need an
530 void Buffer::markBakClean()
537 void Buffer::setUnnamed(bool flag)
544 bool Buffer::isUnnamed()
551 void Buffer::markDirty()
558 DEPCLEAN * tmp = dep_clean;
567 string const & Buffer::fileName() const
574 bool Buffer::isReadonly() const
581 BufferView * Buffer::getUser() const
588 void Buffer::setParentName(string const & name)
590 params.parentname = name;
596 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
597 return a.par == b.par && a.str == b.str;
598 // No need to compare depth.
604 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
606 // No need to compare depth.
611 bool operator==(Buffer::inset_iterator const & iter1,
612 Buffer::inset_iterator const & iter2) {
613 return iter1.par == iter2.par
614 && (iter1.par == 0 || iter1.it == iter2.it);
619 bool operator!=(Buffer::inset_iterator const & iter1,
620 Buffer::inset_iterator const & iter2) {
621 return !(iter1 == iter2);