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 bool readFile(string const & filename);
85 bool readFile(string const & filename, ParagraphList::iterator pit);
87 /// read the header, returns number of unknown tokens
88 int readHeader(LyXLex & lex);
90 /** Reads a file without header.
91 \param par if != 0 insert the file.
92 \return \c false if file is not completely read.
94 bool readBody(LyXLex &, ParagraphList::iterator pit);
96 /// This parses a single token
97 int readParagraph(LyXLex &, string const & token,
99 ParagraphList::iterator & pit,
100 lyx::depth_type & depth);
103 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
104 LyXFont const &, string const &);
106 ParIterator getParFromID(int id) const;
107 /// do we have a paragraph with this id?
108 bool hasParWithID(int id) const;
110 /// This signal is emitted when a parsing error shows up.
111 boost::signal1<void, ErrorItem> error;
112 /// This signal is emitted when some message shows up.
113 boost::signal1<void, string> message;
114 /// This signal is emitted when the buffer busy status change.
115 boost::signal1<void, bool> busy;
116 /// This signal is emitted when the buffer readonly status change.
117 boost::signal1<void, bool> readonly;
118 /// Update window titles of all users.
119 boost::signal0<void> updateTitles;
120 /// Reset autosave timers for all users.
121 boost::signal0<void> resetAutosaveTimers;
122 /// This signal is emitting if the buffer is being closed.
123 boost::signal0<void> closing;
127 Takes care of auto-save files and backup file if requested.
128 Returns \c true if the save is successful, \c false otherwise.
132 /// Write file. Returns \c false if unsuccesful.
133 bool writeFile(string const &) const;
136 void writeFileAscii(string const &, int);
138 void writeFileAscii(std::ostream &, int);
140 string const asciiParagraph(Paragraph const &, unsigned int linelen,
141 bool noparbreak = false) const;
142 /// Just a wrapper for the method below, first creating the ofstream.
143 void makeLaTeXFile(string const & filename,
144 string const & original_path,
145 LatexRunParams const &,
146 bool output_preamble = true,
147 bool output_body = true);
149 void makeLaTeXFile(std::ostream & os,
150 string const & original_path,
151 LatexRunParams const &,
152 bool output_preamble = true,
153 bool output_body = true);
155 void simpleDocBookOnePar(std::ostream &,
156 ParagraphList::iterator par,
158 lyx::depth_type depth) const;
160 void simpleLinuxDocOnePar(std::ostream & os,
161 ParagraphList::iterator par,
162 lyx::depth_type depth) const;
164 void makeLinuxDocFile(string const & filename,
165 bool nice, bool only_body = false);
167 void makeDocBookFile(string const & filename,
168 bool nice, bool only_body = false);
169 /// returns the main language for the buffer (document)
170 Language const * getLanguage() const;
171 /// get l10n translated to the buffers language
172 string const B_(string const & l10n) const;
176 /// return true if the main lyx file does not need saving
177 bool isClean() const;
179 bool isBakClean() const;
181 bool isDepClean(string const & name) const;
183 /// mark the main lyx file as not needing saving
184 void markClean() const;
190 void markDepClean(string const & name);
193 void setUnnamed(bool flag = true);
198 /// Mark this buffer as dirty.
201 /// Returns the buffer's filename. It is always an absolute path.
202 string const & fileName() const;
204 /// Returns the the path where the buffer lives.
205 /// It is always an absolute path.
206 string const & filePath() const;
208 /** A transformed version of the file name, adequate for LaTeX.
209 \param no_path optional if \c true then the path is stripped.
211 string const getLatexName(bool no_path = true) const;
213 /// Get the name and type of the log.
214 std::pair<LogType, string> const getLogName() const;
216 /// Change name of buffer. Updates "read-only" flag.
217 void setFileName(string const & newfile);
219 /// Name of the document's parent
220 void setParentName(string const &);
222 /// Is buffer read-only?
223 bool isReadonly() const;
225 /// Set buffer read-only flag
226 void setReadonly(bool flag = true);
228 /// returns \c true if the buffer contains a LaTeX document
229 bool isLatex() const;
230 /// returns \c true if the buffer contains a LinuxDoc document
231 bool isLinuxDoc() const;
232 /// returns \c true if the buffer contains a DocBook document
233 bool isDocBook() const;
234 /** returns \c true if the buffer contains either a LinuxDoc
235 or DocBook document */
237 /// returns \c true if the buffer contains a Wed document
238 bool isLiterate() const;
240 /** Validate a buffer for LaTeX.
241 This validates the buffer, and returns a struct for use by
242 #makeLaTeX# and others. Its main use is to figure out what
243 commands and packages need to be included in the LaTeX file.
244 It (should) also check that the needed constructs are there
245 (i.e. that the \refs points to coresponding \labels). It
246 should perhaps inset "error" insets to help the user correct
249 void validate(LaTeXFeatures &) const;
251 /// return all bibkeys from buffer and its childs
252 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
254 void getLabelList(std::vector<string> &) const;
257 void changeLanguage(Language const * from, Language const * to);
260 void updateDocLang(Language const * nlang);
263 bool isMultiLingual();
265 /// Does this mean that this is buffer local?
266 limited_stack<Undo> & undostack();
267 limited_stack<Undo> const & undostack() const;
269 /// Does this mean that this is buffer local?
270 limited_stack<Undo> & redostack();
271 limited_stack<Undo> const & redostack() const;
274 BufferParams & params();
275 BufferParams const & params() const;
277 /** The list of paragraphs.
278 This is a linked list of paragraph, this list holds the
279 whole contents of the document.
281 ParagraphList & paragraphs();
282 ParagraphList const & paragraphs() const;
284 /// LyX version control object.
286 LyXVC const & lyxvc() const;
288 /// Where to put temporary files.
289 string const & temppath() const;
291 /** If we are writing a nice LaTeX file or not.
292 While writing as LaTeX, tells whether we are
293 doing a 'nice' LaTeX file */
295 bool niceFile() const;
297 /// Used when typesetting to place errorboxes.
299 TexRow const & texrow() const;
301 /// the author list for the document
302 AuthorList & authors();
305 /** Inserts a file into a document
306 \param par if != 0 insert the file.
307 \return \c false if method fails.
309 bool readFile(LyXLex &, string const & filename,
310 ParagraphList::iterator pit);
312 bool do_writeFile(std::ostream & ofs) const;
314 limited_stack<Undo> undostack_;
315 limited_stack<Undo> redostack_;
316 BufferParams params_;
317 ParagraphList paragraphs_;
323 typedef std::map<string, bool> DepClean;
325 /// need to regenerate .tex ?
329 mutable bool lyx_clean;
331 /// is autosave needed
332 mutable bool bak_clean;
334 /// is this a unnamed file (New...)
340 /// name of the file the buffer is associated with.
343 /// The path to the document file.
347 boost::scoped_ptr<Messages> messages_;
350 class inset_iterator {
352 typedef std::input_iterator_tag iterator_category;
353 typedef InsetOld value_type;
354 typedef ptrdiff_t difference_type;
355 typedef InsetOld * pointer;
356 typedef InsetOld & reference;
357 typedef ParagraphList::iterator base_type;
362 inset_iterator(base_type p, base_type e);
364 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
367 inset_iterator & operator++();
369 inset_iterator operator++(int);
371 reference operator*();
373 pointer operator->();
376 ParagraphList::iterator getPar() const;
378 lyx::pos_type getPos() const;
381 bool operator==(inset_iterator const & iter1,
382 inset_iterator const & iter2);
387 ParagraphList::iterator pit;
389 ParagraphList::iterator pend;
391 InsetList::iterator it;
394 /// return an iterator to all *top-level* insets in the buffer
395 inset_iterator inset_iterator_begin();
397 /// return the end of all *top-level* insets in the buffer
398 inset_iterator inset_iterator_end();
400 /// return a const iterator to all *top-level* insets in the buffer
401 inset_iterator inset_const_iterator_begin() const;
403 /// return the const end of all *top-level* insets in the buffer
404 inset_iterator inset_const_iterator_end() const;
407 ParIterator par_iterator_begin();
409 ParConstIterator par_iterator_begin() const;
411 ParIterator par_iterator_end();
413 ParConstIterator par_iterator_end() const;
416 InsetOld * getInsetFromID(int id_arg) const;
419 bool operator==(Buffer::inset_iterator const & iter1,
420 Buffer::inset_iterator const & iter2);
422 bool operator!=(Buffer::inset_iterator const & iter1,
423 Buffer::inset_iterator const & iter2);