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 "InsetList.h"
16 #include "ParagraphList_fwd.h"
18 #include "support/limited_stack.h"
19 #include "support/types.h"
21 #include <boost/scoped_ptr.hpp>
22 #include <boost/signals/signal0.hpp>
23 #include <boost/signals/signal1.hpp>
25 #include "support/std_string.h"
43 class ParConstIterator;
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 /** High-level interface to buffer functionality.
73 This function parses a command string and executes it
75 bool dispatch(string const & command, bool * result = 0);
77 /// Maybe we know the function already by number...
78 bool dispatch(int ac, string const & argument, bool * result = 0);
80 /// Load the autosaved file.
81 void loadAutoSaveFile();
84 bool readFile(string const & filename);
86 bool readFile(string const & filename, ParagraphList::iterator pit);
88 /// read the header, returns number of unknown tokens
89 int readHeader(LyXLex & lex);
91 /** Reads a file without header.
92 \param par if != 0 insert the file.
93 \return \c false if file is not completely read.
95 bool readBody(LyXLex &, ParagraphList::iterator pit);
97 /// This parses a single token
98 int readParagraph(LyXLex &, string const & token,
100 ParagraphList::iterator & pit,
101 lyx::depth_type & depth);
104 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
105 LyXFont const &, string const &);
107 ParIterator getParFromID(int id) const;
108 /// do we have a paragraph with this id?
109 bool hasParWithID(int id) const;
111 /// This signal is emitted when a parsing error shows up.
112 boost::signal1<void, ErrorItem> error;
113 /// This signal is emitted when some message shows up.
114 boost::signal1<void, string> message;
115 /// This signal is emitted when the buffer busy status change.
116 boost::signal1<void, bool> busy;
117 /// This signal is emitted when the buffer readonly status change.
118 boost::signal1<void, bool> readonly;
119 /// Update window titles of all users.
120 boost::signal0<void> updateTitles;
121 /// Reset autosave timers for all users.
122 boost::signal0<void> resetAutosaveTimers;
123 /// This signal is emitting if the buffer is being closed.
124 boost::signal0<void> closing;
128 Takes care of auto-save files and backup file if requested.
129 Returns \c true if the save is successful, \c false otherwise.
133 /// Write file. Returns \c false if unsuccesful.
134 bool writeFile(string const &) const;
137 void writeFileAscii(string const &, int);
139 void writeFileAscii(std::ostream &, int);
141 string const asciiParagraph(Paragraph const &, unsigned int linelen,
142 bool noparbreak = false) const;
143 /// Just a wrapper for the method below, first creating the ofstream.
144 void makeLaTeXFile(string const & filename,
145 string const & original_path,
146 LatexRunParams const &,
147 bool output_preamble = true,
148 bool output_body = true);
150 void makeLaTeXFile(std::ostream & os,
151 string const & original_path,
152 LatexRunParams const &,
153 bool output_preamble = true,
154 bool output_body = true);
156 void simpleDocBookOnePar(std::ostream &,
157 ParagraphList::iterator par,
159 lyx::depth_type depth) const;
161 void simpleLinuxDocOnePar(std::ostream & os,
162 ParagraphList::iterator par,
163 lyx::depth_type depth) const;
165 void makeLinuxDocFile(string const & filename,
166 bool nice, bool only_body = false);
168 void makeDocBookFile(string const & filename,
169 bool nice, bool only_body = false);
170 /// returns the main language for the buffer (document)
171 Language const * getLanguage() const;
172 /// get l10n translated to the buffers language
173 string const B_(string const & l10n) const;
177 /// return true if the main lyx file does not need saving
178 bool isClean() const;
180 bool isBakClean() const;
182 bool isDepClean(string const & name) const;
184 /// mark the main lyx file as not needing saving
185 void markClean() const;
191 void markDepClean(string const & name);
194 void setUnnamed(bool flag = true);
199 /// Mark this buffer as dirty.
202 /// Returns the buffer's filename. It is always an absolute path.
203 string const & fileName() const;
205 /// Returns the the path where the buffer lives.
206 /// It is always an absolute path.
207 string const & filePath() const;
209 /** A transformed version of the file name, adequate for LaTeX.
210 \param no_path optional if \c true then the path is stripped.
212 string const getLatexName(bool no_path = true) const;
214 /// Get the name and type of the log.
215 std::pair<LogType, string> const getLogName() const;
217 /// Change name of buffer. Updates "read-only" flag.
218 void setFileName(string const & newfile);
220 /// Name of the document's parent
221 void setParentName(string const &);
223 /// Is buffer read-only?
224 bool isReadonly() const;
226 /// Set buffer read-only flag
227 void setReadonly(bool flag = true);
229 /// returns \c true if the buffer contains a LaTeX document
230 bool isLatex() const;
231 /// returns \c true if the buffer contains a LinuxDoc document
232 bool isLinuxDoc() const;
233 /// returns \c true if the buffer contains a DocBook document
234 bool isDocBook() const;
235 /** returns \c true if the buffer contains either a LinuxDoc
236 or DocBook document */
238 /// returns \c true if the buffer contains a Wed document
239 bool isLiterate() const;
241 /** Validate a buffer for LaTeX.
242 This validates the buffer, and returns a struct for use by
243 #makeLaTeX# and others. Its main use is to figure out what
244 commands and packages need to be included in the LaTeX file.
245 It (should) also check that the needed constructs are there
246 (i.e. that the \refs points to coresponding \labels). It
247 should perhaps inset "error" insets to help the user correct
250 void validate(LaTeXFeatures &) const;
252 /// return all bibkeys from buffer and its childs
253 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
255 void getLabelList(std::vector<string> &) const;
258 void changeLanguage(Language const * from, Language const * to);
261 void updateDocLang(Language const * nlang);
264 bool isMultiLingual();
266 /// Does this mean that this is buffer local?
267 limited_stack<Undo> & undostack();
268 limited_stack<Undo> const & undostack() const;
270 /// Does this mean that this is buffer local?
271 limited_stack<Undo> & redostack();
272 limited_stack<Undo> const & redostack() const;
275 BufferParams & params();
276 BufferParams const & params() const;
278 /** The list of paragraphs.
279 This is a linked list of paragraph, this list holds the
280 whole contents of the document.
282 ParagraphList & paragraphs();
283 ParagraphList const & paragraphs() const;
285 /// LyX version control object.
287 LyXVC const & lyxvc() const;
289 /// Where to put temporary files.
290 string const & temppath() const;
292 /** If we are writing a nice LaTeX file or not.
293 While writing as LaTeX, tells whether we are
294 doing a 'nice' LaTeX file */
296 bool niceFile() const;
298 /// Used when typesetting to place errorboxes.
300 TexRow const & texrow() const;
302 class inset_iterator {
304 typedef std::input_iterator_tag iterator_category;
305 typedef InsetOld value_type;
306 typedef ptrdiff_t difference_type;
307 typedef InsetOld * pointer;
308 typedef InsetOld & reference;
309 typedef ParagraphList::iterator base_type;
314 inset_iterator(base_type p, base_type e);
316 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
319 inset_iterator & operator++();
321 inset_iterator operator++(int);
323 reference operator*();
325 pointer operator->();
328 ParagraphList::iterator getPar() const;
330 lyx::pos_type getPos() const;
333 bool operator==(inset_iterator const & iter1,
334 inset_iterator const & iter2);
339 ParagraphList::iterator pit;
341 ParagraphList::iterator pend;
343 InsetList::iterator it;
346 /// return an iterator to all *top-level* insets in the buffer
347 inset_iterator inset_iterator_begin();
349 /// return the end of all *top-level* insets in the buffer
350 inset_iterator inset_iterator_end();
352 /// return a const iterator to all *top-level* insets in the buffer
353 inset_iterator inset_const_iterator_begin() const;
355 /// return the const end of all *top-level* insets in the buffer
356 inset_iterator inset_const_iterator_end() const;
359 ParIterator par_iterator_begin();
361 ParConstIterator par_iterator_begin() const;
363 ParIterator par_iterator_end();
365 ParConstIterator par_iterator_end() const;
368 InsetOld * getInsetFromID(int id_arg) const;
371 /** Inserts a file into a document
372 \param par if != 0 insert the file.
373 \return \c false if method fails.
375 bool readFile(LyXLex &, string const & filename,
376 ParagraphList::iterator pit);
378 bool do_writeFile(std::ostream & ofs) const;
380 /// Use the Pimpl idiom to hide the internals.
382 /// The pointer never changes although *pimpl_'s contents may.
383 boost::scoped_ptr<Impl> const pimpl_;
386 bool operator==(Buffer::inset_iterator const & iter1,
387 Buffer::inset_iterator const & iter2);
389 bool operator!=(Buffer::inset_iterator const & iter1,
390 Buffer::inset_iterator const & iter2);