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>
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(std::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(std::string const & command, bool * result = 0);
77 /// Maybe we know the function already by number...
78 bool dispatch(FuncRequest const & func, bool * result = 0);
80 /// Load the autosaved file.
81 void loadAutoSaveFile();
84 bool readFile(std::string const & filename);
86 bool readFile(std::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 &, std::string const & token,
100 ParagraphList::iterator & pit,
101 lyx::depth_type & depth);
104 void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
105 LyXFont const &, std::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, std::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(std::string const &) const;
137 void writeFileAscii(std::string const &, LatexRunParams const &);
139 void writeFileAscii(std::ostream &, LatexRunParams const &);
140 /// Just a wrapper for the method below, first creating the ofstream.
141 void makeLaTeXFile(std::string const & filename,
142 std::string const & original_path,
143 LatexRunParams const &,
144 bool output_preamble = true,
145 bool output_body = true);
147 void makeLaTeXFile(std::ostream & os,
148 std::string const & original_path,
149 LatexRunParams const &,
150 bool output_preamble = true,
151 bool output_body = true);
153 void makeLinuxDocFile(std::string const & filename,
154 LatexRunParams const & runparams_in,
155 bool only_body = false);
157 void makeDocBookFile(std::string const & filename,
158 LatexRunParams const & runparams_in,
159 bool only_body = false);
160 /// returns the main language for the buffer (document)
161 Language const * getLanguage() const;
162 /// get l10n translated to the buffers language
163 std::string const B_(std::string const & l10n) const;
167 /// return true if the main lyx file does not need saving
168 bool isClean() const;
170 bool isBakClean() const;
172 bool isDepClean(std::string const & name) const;
174 /// mark the main lyx file as not needing saving
175 void markClean() const;
181 void markDepClean(std::string const & name);
184 void setUnnamed(bool flag = true);
189 /// Mark this buffer as dirty.
192 /// Returns the buffer's filename. It is always an absolute path.
193 std::string const & fileName() const;
195 /// Returns the the path where the buffer lives.
196 /// It is always an absolute path.
197 std::string const & filePath() const;
199 /** A transformed version of the file name, adequate for LaTeX.
200 \param no_path optional if \c true then the path is stripped.
202 std::string const getLatexName(bool no_path = true) const;
204 /// Get the name and type of the log.
205 std::pair<LogType, std::string> const getLogName() const;
207 /// Change name of buffer. Updates "read-only" flag.
208 void setFileName(std::string const & newfile);
210 /// Name of the document's parent
211 void setParentName(std::string const &);
213 /// Is buffer read-only?
214 bool isReadonly() const;
216 /// Set buffer read-only flag
217 void setReadonly(bool flag = true);
219 /// returns \c true if the buffer contains a LaTeX document
220 bool isLatex() const;
221 /// returns \c true if the buffer contains a LinuxDoc document
222 bool isLinuxDoc() const;
223 /// returns \c true if the buffer contains a DocBook document
224 bool isDocBook() const;
225 /** returns \c true if the buffer contains either a LinuxDoc
226 or DocBook document */
228 /// returns \c true if the buffer contains a Wed document
229 bool isLiterate() const;
231 /** Validate a buffer for LaTeX.
232 This validates the buffer, and returns a struct for use by
233 #makeLaTeX# and others. Its main use is to figure out what
234 commands and packages need to be included in the LaTeX file.
235 It (should) also check that the needed constructs are there
236 (i.e. that the \refs points to coresponding \labels). It
237 should perhaps inset "error" insets to help the user correct
240 void validate(LaTeXFeatures &) const;
242 /// return all bibkeys from buffer and its childs
243 void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
245 void getLabelList(std::vector<std::string> &) const;
248 void changeLanguage(Language const * from, Language const * to);
251 void updateDocLang(Language const * nlang);
254 bool isMultiLingual();
256 /// Does this mean that this is buffer local?
257 limited_stack<Undo> & undostack();
258 limited_stack<Undo> const & undostack() const;
260 /// Does this mean that this is buffer local?
261 limited_stack<Undo> & redostack();
262 limited_stack<Undo> const & redostack() const;
265 BufferParams & params();
266 BufferParams const & params() const;
268 /** The list of paragraphs.
269 This is a linked list of paragraph, this list holds the
270 whole contents of the document.
272 ParagraphList & paragraphs();
273 ParagraphList const & paragraphs() const;
275 /// LyX version control object.
277 LyXVC const & lyxvc() const;
279 /// Where to put temporary files.
280 std::string const & temppath() const;
282 /** If we are writing a nice LaTeX file or not.
283 While writing as LaTeX, tells whether we are
284 doing a 'nice' LaTeX file */
286 bool niceFile() const;
288 /// Used when typesetting to place errorboxes.
290 TexRow const & texrow() const;
292 class inset_iterator {
294 typedef std::input_iterator_tag iterator_category;
295 typedef InsetOld value_type;
296 typedef ptrdiff_t difference_type;
297 typedef InsetOld * pointer;
298 typedef InsetOld & reference;
299 typedef ParagraphList::iterator base_type;
304 inset_iterator(base_type p, base_type e);
306 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
309 inset_iterator & operator++();
311 inset_iterator operator++(int);
313 reference operator*();
315 pointer operator->();
318 ParagraphList::iterator getPar() const;
320 lyx::pos_type getPos() const;
323 bool operator==(inset_iterator const & iter1,
324 inset_iterator const & iter2);
329 ParagraphList::iterator pit;
331 ParagraphList::iterator pend;
333 InsetList::iterator it;
336 /// return an iterator to all *top-level* insets in the buffer
337 inset_iterator inset_iterator_begin();
339 /// return the end of all *top-level* insets in the buffer
340 inset_iterator inset_iterator_end();
342 /// return a const iterator to all *top-level* insets in the buffer
343 inset_iterator inset_const_iterator_begin() const;
345 /// return the const end of all *top-level* insets in the buffer
346 inset_iterator inset_const_iterator_end() const;
349 ParIterator par_iterator_begin();
351 ParConstIterator par_iterator_begin() const;
353 ParIterator par_iterator_end();
355 ParConstIterator par_iterator_end() const;
358 InsetOld * getInsetFromID(int id_arg) const;
360 /** \returns true only when the file is fully loaded.
361 * Used to prevent the premature generation of previews
362 * and by the citation inset.
364 bool fully_loaded() const;
365 /// Set by buffer_funcs' newFile.
366 void fully_loaded(bool);
369 /** Inserts a file into a document
370 \param par if != 0 insert the file.
371 \return \c false if method fails.
373 bool readFile(LyXLex &, std::string const & filename,
374 ParagraphList::iterator pit);
376 bool do_writeFile(std::ostream & ofs) const;
378 /// Use the Pimpl idiom to hide the internals.
380 /// The pointer never changes although *pimpl_'s contents may.
381 boost::scoped_ptr<Impl> const pimpl_;
384 bool operator==(Buffer::inset_iterator const & iter1,
385 Buffer::inset_iterator const & iter2);
387 bool operator!=(Buffer::inset_iterator const & iter1,
388 Buffer::inset_iterator const & iter2);