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 "support/limited_stack.h"
25 #include "bufferparams.h"
27 #include "ParagraphList.h"
28 #include "paragraph.h"
30 #include <boost/scoped_ptr.hpp>
31 #include <boost/shared_ptr.hpp>
52 /** The buffer object.
53 This 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 /** High-level interface to buffer functionality.
76 This function parses a command string and executes it
78 bool dispatch(string const & command, bool * result = 0);
80 /// Maybe we know the function already by number...
81 bool dispatch(int ac, string const & argument, bool * result = 0);
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 *&, lyx::pos_type &,
129 LyXFont const &, string const &) const;
131 Paragraph * getParFromID(int id) const;
133 /// Parse a single inset.
134 void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
137 Takes care of auto-save files and backup file if requested.
138 Returns \c true if the save is successful, \c false otherwise.
142 /// Write file. Returns \c false if unsuccesful.
143 bool writeFile(string const &) const;
146 void writeFileAscii(string const & , int);
148 void writeFileAscii(std::ostream &, int);
150 string const asciiParagraph(Paragraph const &, unsigned int linelen,
151 bool noparbreak = false) const;
152 /// Just a wrapper for the method below, first creating the ofstream.
153 void makeLaTeXFile(string const & filename,
154 string const & original_path,
156 bool only_body = false,
157 bool only_preamble = false);
159 void makeLaTeXFile(std::ostream & os,
160 string const & original_path,
162 bool only_body = false,
163 bool only_preamble = false);
164 /** LaTeX all paragraphs from par to endpar.
165 \param \a endpar if == 0 then to the end
167 void latexParagraphs(std::ostream & os, Paragraph * par,
168 Paragraph * endpar, TexRow & texrow, bool moving_arg = false) const;
170 void simpleDocBookOnePar(std::ostream &,
171 Paragraph * par, int & desc_on,
172 Paragraph::depth_type depth) const ;
174 void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
175 Paragraph::depth_type depth);
177 void makeLinuxDocFile(string const & filename,
178 bool nice, bool only_body = false);
180 void makeDocBookFile(string const & filename,
181 bool nice, bool only_body = false);
182 /// Open SGML/XML tag.
183 int sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
184 string const & latexname) const;
185 /// Closes SGML/XML tag.
186 int sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
187 string const & latexname) const;
189 void sgmlError(Paragraph * par, int pos, string const & message) const;
191 /// returns the main language for the buffer (document)
192 Language const * getLanguage() const;
195 /// return true if the main lyx file does not need saving
196 bool isClean() const;
198 bool isBakClean() const;
200 bool isDepClean(string const & name) const;
202 /// mark the main lyx file as not needing saving
203 void markClean() const;
209 void markDepClean(string const & name);
212 void setUnnamed(bool flag = true);
217 /// Mark this buffer as dirty.
220 /// Returns the buffer's filename. It is always an absolute path.
221 string const & fileName() const;
223 /// Returns the the path where the buffer lives.
224 /// It is always an absolute path.
225 string const & filePath() const;
227 /** A transformed version of the file name, adequate for LaTeX.
228 \param no_path optional if \c true then the path is stripped.
230 string const getLatexName(bool no_path = true) const;
232 /// Get the name and type of the log.
233 std::pair<LogType, string> const getLogName() const;
235 /// Change name of buffer. Updates "read-only" flag.
236 void setFileName(string const & newfile);
238 /// Name of the document's parent
239 void setParentName(string const &);
241 /// Is buffer read-only?
242 bool isReadonly() const;
244 /// Set buffer read-only flag
245 void setReadonly(bool flag = true);
247 /// returns \c true if the buffer contains a LaTeX document
248 bool isLatex() const;
249 /// returns \c true if the buffer contains a LinuxDoc document
250 bool isLinuxDoc() const;
251 /// returns \c true if the buffer contains a DocBook document
252 bool isDocBook() const;
253 /** returns \c true if the buffer contains either a LinuxDoc
254 or DocBook document */
256 /// returns \c true if the buffer contains a Wed document
257 bool isLiterate() const;
259 /** Validate a buffer for LaTeX.
260 This validates the buffer, and returns a struct for use by
261 #makeLaTeX# and others. Its main use is to figure out what
262 commands and packages need to be included in the LaTeX file.
263 It (should) also check that the needed constructs are there
264 (i.e. that the \refs points to coresponding \labels). It
265 should perhaps inset "error" insets to help the user correct
268 void validate(LaTeXFeatures &) const;
271 string const getIncludeonlyList(char delim = ',');
273 std::vector<std::pair<string, string> > const getBibkeyList() const;
275 std::vector<string> const getLabelList() const;
277 /** This will clearly have to change later. Later we can have more
278 than one user per buffer. */
279 BufferView * getUser() const;
282 void changeLanguage(Language const * from, Language const * to);
284 bool isMultiLingual();
286 /// Does this mean that this is buffer local?
287 limited_stack<boost::shared_ptr<Undo> > undostack;
289 /// Does this mean that this is buffer local?
290 limited_stack<boost::shared_ptr<Undo> > redostack;
295 /** The list of paragraphs.
296 This is a linked list of paragraph, this list holds the
297 whole contents of the document.
299 ParagraphList paragraphs;
301 /// LyX version control object.
304 /// Where to put temporary files.
307 /** If we are writing a nice LaTeX file or not.
308 While writing as LaTeX, tells whether we are
309 doing a 'nice' LaTeX file */
312 /// Used when typesetting to place errorboxes.
314 /// Buffer-wide counter array
315 Counters & counters() const;
319 mutable bool lyx_clean;
321 /// is autosave needed
322 mutable bool bak_clean;
324 /// is this a unnamed file (New...)
327 /// is regenerating #.tex# necessary
328 DEPCLEAN * dep_clean;
333 /// name of the file the buffer is associated with.
336 /// The path to the document file.
339 /// Format number of buffer
341 /** A list of views using this buffer.
342 Why not keep a list of the BufferViews that use this buffer?
344 At least then we don't have to do a lot of magic like:
345 #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
346 of the buffers in the list of users to do a #updateLayoutChoice#.
350 /// The pointer is const although its contents may not be
351 boost::scoped_ptr<Counters> const ctrs;
355 class inset_iterator {
357 typedef std::input_iterator_tag iterator_category;
358 typedef Inset value_type;
359 typedef ptrdiff_t difference_type;
360 typedef Inset * pointer;
361 typedef Inset & reference;
362 typedef ParagraphList::iterator base_type;
367 inset_iterator(base_type p, base_type e);
369 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
372 inset_iterator & operator++();
374 inset_iterator operator++(int);
376 reference operator*();
378 pointer operator->();
381 Paragraph * getPar();
383 lyx::pos_type getPos() const;
386 bool operator==(inset_iterator const & iter1,
387 inset_iterator const & iter2);
392 ParagraphList::iterator pit;
394 ParagraphList::iterator pend;
396 InsetList::iterator it;
400 inset_iterator inset_iterator_begin() {
401 return inset_iterator(paragraphs.begin(), paragraphs.end());
405 inset_iterator inset_iterator_end() {
406 return inset_iterator();
410 inset_iterator inset_const_iterator_begin() const {
411 return inset_iterator(paragraphs.begin(), paragraphs.end());
415 inset_iterator inset_const_iterator_end() const {
416 return inset_iterator();
420 ParIterator par_iterator_begin();
422 ParIterator par_iterator_end();
425 Inset * getInsetFromID(int id_arg) const;
428 bool operator==(Buffer::inset_iterator const & iter1,
429 Buffer::inset_iterator const & iter2);
431 bool operator!=(Buffer::inset_iterator const & iter1,
432 Buffer::inset_iterator const & iter2);