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"
44 class ParConstIterator;
50 /** The buffer object.
51 This is the buffer object. It contains all the informations about
52 a document loaded into LyX. I am not sure if the class is complete or
53 minimal, probably not.
54 \author Lars Gullik Bjønnes
58 /// What type of log will \c getLogName() return?
60 latexlog, ///< LaTeX log
61 buildlog ///< Literate build log
66 \param b optional \c false by default
68 explicit Buffer(string const & file, bool b = false);
73 /** High-level interface to buffer functionality.
74 This function parses a command string and executes it
76 bool dispatch(string const & command, bool * result = 0);
78 /// Maybe we know the function already by number...
79 bool dispatch(int ac, string const & argument, bool * result = 0);
81 /// Load the autosaved file.
82 void loadAutoSaveFile();
85 bool readFile(string const & filename);
87 bool readFile(string const & filename, ParagraphList::iterator pit);
89 /// read the header, returns number of unknown tokens
90 int readHeader(LyXLex & lex);
92 /** Reads a file without header.
93 \param par if != 0 insert the file.
94 \return \c false if file is not completely read.
96 bool readBody(LyXLex &, ParagraphList::iterator pit);
98 /// This parses a single token
99 int readParagraph(LyXLex &, string const & token,
100 ParagraphList & pars,
101 ParagraphList::iterator & pit,
102 lyx::depth_type & depth);
105 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
106 LyXFont const &, string const &);
108 ParIterator getParFromID(int id) const;
109 /// do we have a paragraph with this id?
110 bool hasParWithID(int id) const;
112 /// This signal is emitted when a parsing error shows up.
113 boost::signal1<void, ErrorItem> error;
114 /// This signal is emitted when some message shows up.
115 boost::signal1<void, string> message;
116 /// This signal is emitted when the buffer busy status change.
117 boost::signal1<void, bool> busy;
118 /// This signal is emitted when the buffer readonly status change.
119 boost::signal1<void, bool> readonly;
120 /// Update window titles of all users.
121 boost::signal0<void> updateTitles;
122 /// Reset autosave timers for all users.
123 boost::signal0<void> resetAutosaveTimers;
124 /// This signal is emitting if the buffer is being closed.
125 boost::signal0<void> closing;
129 Takes care of auto-save files and backup file if requested.
130 Returns \c true if the save is successful, \c false otherwise.
134 /// Write file. Returns \c false if unsuccesful.
135 bool writeFile(string const &) const;
138 void writeFileAscii(string const &, int);
140 void writeFileAscii(std::ostream &, int);
142 string const asciiParagraph(Paragraph const &, unsigned int linelen,
143 bool noparbreak = false) const;
144 /// Just a wrapper for the method below, first creating the ofstream.
145 void makeLaTeXFile(string const & filename,
146 string const & original_path,
147 LatexRunParams const &,
148 bool output_preamble = true,
149 bool output_body = true);
151 void makeLaTeXFile(std::ostream & os,
152 string const & original_path,
153 LatexRunParams const &,
154 bool output_preamble = true,
155 bool output_body = true);
157 void simpleDocBookOnePar(std::ostream &,
158 ParagraphList::iterator par,
160 lyx::depth_type depth) const;
162 void simpleLinuxDocOnePar(std::ostream & os,
163 ParagraphList::iterator par,
164 lyx::depth_type depth) const;
166 void makeLinuxDocFile(string const & filename,
167 bool nice, bool only_body = false);
169 void makeDocBookFile(string const & filename,
170 bool nice, bool only_body = false);
171 /// returns the main language for the buffer (document)
172 Language const * getLanguage() const;
173 /// get l10n translated to the buffers language
174 string const B_(string const & l10n) const;
178 /// return true if the main lyx file does not need saving
179 bool isClean() const;
181 bool isBakClean() const;
183 bool isDepClean(string const & name) const;
185 /// mark the main lyx file as not needing saving
186 void markClean() const;
192 void markDepClean(string const & name);
195 void setUnnamed(bool flag = true);
200 /// Mark this buffer as dirty.
203 /// Returns the buffer's filename. It is always an absolute path.
204 string const & fileName() const;
206 /// Returns the the path where the buffer lives.
207 /// It is always an absolute path.
208 string const & filePath() const;
210 /** A transformed version of the file name, adequate for LaTeX.
211 \param no_path optional if \c true then the path is stripped.
213 string const getLatexName(bool no_path = true) const;
215 /// Get the name and type of the log.
216 std::pair<LogType, string> const getLogName() const;
218 /// Change name of buffer. Updates "read-only" flag.
219 void setFileName(string const & newfile);
221 /// Name of the document's parent
222 void setParentName(string const &);
224 /// Is buffer read-only?
225 bool isReadonly() const;
227 /// Set buffer read-only flag
228 void setReadonly(bool flag = true);
230 /// returns \c true if the buffer contains a LaTeX document
231 bool isLatex() const;
232 /// returns \c true if the buffer contains a LinuxDoc document
233 bool isLinuxDoc() const;
234 /// returns \c true if the buffer contains a DocBook document
235 bool isDocBook() const;
236 /** returns \c true if the buffer contains either a LinuxDoc
237 or DocBook document */
239 /// returns \c true if the buffer contains a Wed document
240 bool isLiterate() const;
242 /** Validate a buffer for LaTeX.
243 This validates the buffer, and returns a struct for use by
244 #makeLaTeX# and others. Its main use is to figure out what
245 commands and packages need to be included in the LaTeX file.
246 It (should) also check that the needed constructs are there
247 (i.e. that the \refs points to coresponding \labels). It
248 should perhaps inset "error" insets to help the user correct
251 void validate(LaTeXFeatures &) const;
253 /// return all bibkeys from buffer and its childs
254 void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
256 void getLabelList(std::vector<string> &) const;
259 void changeLanguage(Language const * from, Language const * to);
262 void updateDocLang(Language const * nlang);
265 bool isMultiLingual();
267 /// Does this mean that this is buffer local?
268 limited_stack<Undo> & undostack();
269 limited_stack<Undo> const & undostack() const;
271 /// Does this mean that this is buffer local?
272 limited_stack<Undo> & redostack();
273 limited_stack<Undo> const & redostack() const;
276 BufferParams & params();
277 BufferParams const & params() const;
279 /** The list of paragraphs.
280 This is a linked list of paragraph, this list holds the
281 whole contents of the document.
283 ParagraphList & paragraphs();
284 ParagraphList const & paragraphs() const;
286 /// LyX version control object.
288 LyXVC const & lyxvc() const;
290 /// Where to put temporary files.
291 string const & temppath() const;
293 /** If we are writing a nice LaTeX file or not.
294 While writing as LaTeX, tells whether we are
295 doing a 'nice' LaTeX file */
297 bool niceFile() const;
299 /// Used when typesetting to place errorboxes.
301 TexRow const & texrow() const;
303 /// the author list for the document
304 AuthorList & authors();
307 class inset_iterator {
309 typedef std::input_iterator_tag iterator_category;
310 typedef InsetOld value_type;
311 typedef ptrdiff_t difference_type;
312 typedef InsetOld * pointer;
313 typedef InsetOld & reference;
314 typedef ParagraphList::iterator base_type;
319 inset_iterator(base_type p, base_type e);
321 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
324 inset_iterator & operator++();
326 inset_iterator operator++(int);
328 reference operator*();
330 pointer operator->();
333 ParagraphList::iterator getPar() const;
335 lyx::pos_type getPos() const;
338 bool operator==(inset_iterator const & iter1,
339 inset_iterator const & iter2);
344 ParagraphList::iterator pit;
346 ParagraphList::iterator pend;
348 InsetList::iterator it;
351 /// return an iterator to all *top-level* insets in the buffer
352 inset_iterator inset_iterator_begin();
354 /// return the end of all *top-level* insets in the buffer
355 inset_iterator inset_iterator_end();
357 /// return a const iterator to all *top-level* insets in the buffer
358 inset_iterator inset_const_iterator_begin() const;
360 /// return the const end of all *top-level* insets in the buffer
361 inset_iterator inset_const_iterator_end() const;
364 ParIterator par_iterator_begin();
366 ParConstIterator par_iterator_begin() const;
368 ParIterator par_iterator_end();
370 ParConstIterator par_iterator_end() const;
373 InsetOld * getInsetFromID(int id_arg) const;
376 /** Inserts a file into a document
377 \param par if != 0 insert the file.
378 \return \c false if method fails.
380 bool readFile(LyXLex &, string const & filename,
381 ParagraphList::iterator pit);
383 bool do_writeFile(std::ostream & ofs) const;
385 /// Use the Pimpl idiom to hide the internals.
387 /// The pointer never changes although *pimpl_'s contents may.
388 boost::scoped_ptr<Impl> const pimpl_;
391 bool operator==(Buffer::inset_iterator const & iter1,
392 Buffer::inset_iterator const & iter2);
394 bool operator!=(Buffer::inset_iterator const & iter1,
395 Buffer::inset_iterator const & iter2);