4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
15 #include "bufferparams.h"
16 #include "InsetList.h"
18 #include "ParagraphList_fwd.h"
21 #include "support/limited_stack.h"
22 #include "support/types.h"
24 #include <boost/scoped_ptr.hpp>
25 #include <boost/signals/signal0.hpp>
26 #include <boost/signals/signal1.hpp>
28 #include "support/std_string.h"
43 class ParConstIterator;
48 /** The buffer object.
49 This is the buffer object. It contains all the informations about
50 a document loaded into LyX. I am not sure if the class is complete or
51 minimal, probably not.
52 \author Lars Gullik Bjønnes
56 /// What type of log will \c getLogName() return?
58 latexlog, ///< LaTeX log
59 buildlog ///< Literate build log
64 \param b optional \c false by default
66 explicit Buffer(string const & file, bool b = false);
71 /** High-level interface to buffer functionality.
72 This function parses a command string and executes it
74 bool dispatch(string const & command, bool * result = 0);
76 /// Maybe we know the function already by number...
77 bool dispatch(int ac, string const & argument, bool * result = 0);
79 /// Load the autosaved file.
80 void loadAutoSaveFile();
83 /** Inserts a file into a document
84 \param par if != 0 insert the file.
85 \return \c false if method fails.
87 bool readFile(LyXLex &, string const & filename,
88 ParagraphList::iterator pit);
92 bool readFile(string const & filename);
94 bool readFile(string const & filename, ParagraphList::iterator pit);
96 /// read the header, returns number of unknown tokens
97 int readHeader(LyXLex & lex);
99 /** Reads a file without header.
100 \param par if != 0 insert the file.
101 \return \c false if file is not completely read.
103 bool readBody(LyXLex &, ParagraphList::iterator pit);
105 /// This parses a single token
106 int readParagraph(LyXLex &, string const & token,
107 ParagraphList & pars,
108 ParagraphList::iterator & pit,
109 lyx::depth_type & depth);
112 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
113 LyXFont const &, string const &);
115 ParIterator getParFromID(int id) const;
116 /// do we have a paragraph with this id?
117 bool hasParWithID(int id) const;
120 /// This signal is emitted when a parsing error shows up.
121 boost::signal1<void, ErrorItem> error;
122 /// This signal is emitted when some message shows up.
123 boost::signal1<void, string> message;
124 /// This signal is emitted when the buffer busy status change.
125 boost::signal1<void, bool> busy;
126 /// This signal is emitted when the buffer readonly status change.
127 boost::signal1<void, bool> readonly;
128 /// Update window titles of all users.
129 boost::signal0<void> updateTitles;
130 /// Reset autosave timers for all users.
131 boost::signal0<void> resetAutosaveTimers;
132 /// This signal is emitting if the buffer is being closed.
133 boost::signal0<void> closing;
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,
155 LatexRunParams const &,
156 bool output_preamble = true,
157 bool output_body = true);
159 void makeLaTeXFile(std::ostream & os,
160 string const & original_path,
161 LatexRunParams const &,
162 bool output_preamble = true,
163 bool output_body = true);
165 void simpleDocBookOnePar(std::ostream &,
166 ParagraphList::iterator par,
168 lyx::depth_type depth) const;
170 void simpleLinuxDocOnePar(std::ostream & os,
171 ParagraphList::iterator par,
172 lyx::depth_type depth) const;
174 void makeLinuxDocFile(string const & filename,
175 bool nice, bool only_body = false);
177 void makeDocBookFile(string const & filename,
178 bool nice, bool only_body = false);
179 /// returns the main language for the buffer (document)
180 Language const * getLanguage() const;
181 /// get l10n translated to the buffers language
182 string const B_(string const & l10n) const;
186 /// return true if the main lyx file does not need saving
187 bool isClean() const;
189 bool isBakClean() const;
191 bool isDepClean(string const & name) const;
193 /// mark the main lyx file as not needing saving
194 void markClean() const;
200 void markDepClean(string const & name);
203 void setUnnamed(bool flag = true);
208 /// Mark this buffer as dirty.
211 /// Returns the buffer's filename. It is always an absolute path.
212 string const & fileName() const;
214 /// Returns the the path where the buffer lives.
215 /// It is always an absolute path.
216 string const & filePath() 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;
250 /** Validate a buffer for LaTeX.
251 This validates the buffer, and returns a struct for use by
252 #makeLaTeX# and others. Its main use is to figure out what
253 commands and packages need to be included in the LaTeX file.
254 It (should) also check that the needed constructs are there
255 (i.e. that the \refs points to coresponding \labels). It
256 should perhaps inset "error" insets to help the user correct
259 void validate(LaTeXFeatures &) const;
261 /// return all bibkeys from buffer and its childs
262 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
264 void getLabelList(std::vector<string> &) const;
267 void changeLanguage(Language const * from, Language const * to);
270 void updateDocLang(Language const * nlang);
273 bool isMultiLingual();
275 /// Does this mean that this is buffer local?
276 limited_stack<Undo> undostack;
278 /// Does this mean that this is buffer local?
279 limited_stack<Undo> redostack;
284 /** The list of paragraphs.
285 This is a linked list of paragraph, this list holds the
286 whole contents of the document.
288 ParagraphList paragraphs;
290 /// LyX version control object.
293 /// Where to put temporary files.
296 /** If we are writing a nice LaTeX file or not.
297 While writing as LaTeX, tells whether we are
298 doing a 'nice' LaTeX file */
301 /// Used when typesetting to place errorboxes.
304 /// the author list for the document
305 AuthorList & authors();
308 bool do_writeFile(std::ostream & ofs) const;
310 typedef std::map<string, bool> DepClean;
312 /// need to regenerate .tex ?
316 mutable bool lyx_clean;
318 /// is autosave needed
319 mutable bool bak_clean;
321 /// is this a unnamed file (New...)
327 /// name of the file the buffer is associated with.
330 /// The path to the document file.
334 boost::scoped_ptr<Messages> messages_;
337 class inset_iterator {
339 typedef std::input_iterator_tag iterator_category;
340 typedef InsetOld value_type;
341 typedef ptrdiff_t difference_type;
342 typedef InsetOld * pointer;
343 typedef InsetOld & reference;
344 typedef ParagraphList::iterator base_type;
349 inset_iterator(base_type p, base_type e);
351 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
354 inset_iterator & operator++();
356 inset_iterator operator++(int);
358 reference operator*();
360 pointer operator->();
363 ParagraphList::iterator getPar() const;
365 lyx::pos_type getPos() const;
368 bool operator==(inset_iterator const & iter1,
369 inset_iterator const & iter2);
374 ParagraphList::iterator pit;
376 ParagraphList::iterator pend;
378 InsetList::iterator it;
381 /// return an iterator to all *top-level* insets in the buffer
382 inset_iterator inset_iterator_begin();
384 /// return the end of all *top-level* insets in the buffer
385 inset_iterator inset_iterator_end();
387 /// return a const iterator to all *top-level* insets in the buffer
388 inset_iterator inset_const_iterator_begin() const;
390 /// return the const end of all *top-level* insets in the buffer
391 inset_iterator inset_const_iterator_end() const;
394 ParIterator par_iterator_begin();
396 ParConstIterator par_iterator_begin() const;
398 ParIterator par_iterator_end();
400 ParConstIterator par_iterator_end() const;
403 InsetOld * getInsetFromID(int id_arg) const;
406 bool operator==(Buffer::inset_iterator const & iter1,
407 Buffer::inset_iterator const & iter2);
409 bool operator!=(Buffer::inset_iterator const & iter1,
410 Buffer::inset_iterator const & iter2);