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 "errorlist.h"
16 #include "InsetList.h"
18 #include "dociterator.h"
19 #include "ParagraphList_fwd.h"
21 #include "support/limited_stack.h"
22 #include "support/types.h"
23 #include "support/docstring.h"
24 #include "support/docstream.h"
26 #include <boost/scoped_ptr.hpp>
27 #include <boost/signal.hpp>
53 class ParConstIterator;
58 class StableDocIterator;
61 /** The buffer object.
62 * This is the buffer object. It contains all the informations about
63 * a document loaded into LyX.
64 * The buffer object owns the LyXText (wrapped in an InsetText), which
65 * contains the individual paragraphs of the document.
68 * I am not sure if the class is complete or
69 * minimal, probably not.
70 * \author Lars Gullik Bjønnes
74 /// What type of log will \c getLogName() return?
76 latexlog, ///< LaTeX log
77 buildlog ///< Literate build log
82 \param b optional \c false by default
84 explicit Buffer(std::string const & file, bool b = false);
89 /** High-level interface to buffer functionality.
90 This function parses a command string and executes it
92 bool dispatch(std::string const & command, bool * result = 0);
94 /// Maybe we know the function already by number...
95 bool dispatch(FuncRequest const & func, bool * result = 0);
97 /// Load the autosaved file.
98 void loadAutoSaveFile();
101 bool readFile(std::string const & filename);
103 /// read the header, returns number of unknown tokens
104 int readHeader(LyXLex & lex);
106 /** Reads a file without header.
107 \param par if != 0 insert the file.
108 \return \c false if file is not completely read.
110 bool readDocument(LyXLex &);
113 void insertStringAsLines(ParagraphList & plist,
114 pit_type &, pos_type &,
115 LyXFont const &, docstring const &, bool);
117 ParIterator getParFromID(int id) const;
118 /// do we have a paragraph with this id?
119 bool hasParWithID(int id) const;
121 /// This signal is emitted when the buffer is changed.
122 boost::signal<void(bool)> changed;
123 /// This signal is emitted when some parsing error shows up.
124 boost::signal<void(std::string)> errors;
125 /// This signal is emitted when some message shows up.
126 boost::signal<void(docstring)> message;
127 /// This signal is emitted when the buffer busy status change.
128 boost::signal<void(bool)> busy;
129 /// This signal is emitted when the buffer readonly status change.
130 boost::signal<void(bool)> readonly;
131 /// Update window titles of all users.
132 boost::signal<void()> updateTitles;
133 /// Reset autosave timers for all users.
134 boost::signal<void()> resetAutosaveTimers;
135 /// This signal is emitting if the buffer is being closed.
136 boost::signal<void()> closing;
140 Takes care of auto-save files and backup file if requested.
141 Returns \c true if the save is successful, \c false otherwise.
145 /// Write file. Returns \c false if unsuccesful.
146 bool writeFile(std::string const &) const;
148 /// Just a wrapper for the method below, first creating the ofstream.
149 bool makeLaTeXFile(std::string const & filename,
150 std::string const & original_path,
151 OutputParams const &,
152 bool output_preamble = true,
153 bool output_body = true);
155 void writeLaTeXSource(odocstream & os,
156 std::string const & original_path,
157 OutputParams const &,
158 bool output_preamble = true,
159 bool output_body = true);
161 void makeDocBookFile(std::string const & filename,
162 OutputParams const & runparams_in,
163 bool only_body = false);
165 void writeDocBookSource(odocstream & os, std::string const & filename,
166 OutputParams const & runparams_in,
167 bool only_body = false);
168 /// returns the main language for the buffer (document)
169 Language const * getLanguage() const;
170 /// get l10n translated to the buffers language
171 docstring const B_(std::string const & l10n) const;
175 /// return true if the main lyx file does not need saving
176 bool isClean() const;
178 bool isBakClean() const;
180 bool isDepClean(std::string const & name) const;
182 /// mark the main lyx file as not needing saving
183 void markClean() const;
189 void markDepClean(std::string const & name);
192 void setUnnamed(bool flag = true);
195 bool isUnnamed() const;
197 /// Mark this buffer as dirty.
200 /// Returns the buffer's filename. It is always an absolute path.
201 std::string const & fileName() const;
203 /// Returns the the path where the buffer lives.
204 /// It is always an absolute path.
205 std::string const & filePath() const;
207 /** A transformed version of the file name, adequate for LaTeX.
208 \param no_path optional if \c true then the path is stripped.
210 std::string const getLatexName(bool no_path = true) const;
212 /// Get thee name and type of the log.
213 std::pair<LogType, std::string> const getLogName() const;
215 /// Change name of buffer. Updates "read-only" flag.
216 void setFileName(std::string const & newfile);
218 /// Name of the document's parent
219 void setParentName(std::string const &);
221 /** Get the document's master (or \c this if this is not a
224 Buffer const * getMasterBuffer() const;
225 /** Get the document's master (or \c this if this is not a
228 Buffer * getMasterBuffer();
230 /// Is buffer read-only?
231 bool isReadonly() const;
233 /// Set buffer read-only flag
234 void setReadonly(bool flag = true);
236 /// returns \c true if the buffer contains a LaTeX document
237 bool isLatex() const;
238 /// returns \c true if the buffer contains a DocBook document
239 bool isDocBook() const;
240 /// returns \c true if the buffer contains a Wed document
241 bool isLiterate() const;
243 /** Validate a buffer for LaTeX.
244 This validates the buffer, and returns a struct for use by
245 #makeLaTeX# and others. Its main use is to figure out what
246 commands and packages need to be included in the LaTeX file.
247 It (should) also check that the needed constructs are there
248 (i.e. that the \refs points to coresponding \labels). It
249 should perhaps inset "error" insets to help the user correct
252 void validate(LaTeXFeatures &) const;
254 /// return all bibkeys from buffer and its childs
255 void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
256 /// Update the cache with all bibfiles in use (including bibfiles
257 /// of loaded child documents).
258 void updateBibfilesCache();
259 /// Return the cache with all bibfiles in use (including bibfiles
260 /// of loaded child documents).
261 std::vector<std::string> const & getBibfilesCache() const;
263 void getLabelList(std::vector<docstring> &) const;
266 void changeLanguage(Language const * from, Language const * to);
269 void updateDocLang(Language const * nlang);
272 bool isMultiLingual() const;
274 /// Does this mean that this is buffer local?
275 limited_stack<Undo> & undostack();
276 limited_stack<Undo> const & undostack() const;
278 /// Does this mean that this is buffer local?
279 limited_stack<Undo> & redostack();
280 limited_stack<Undo> const & redostack() const;
283 BufferParams & params();
284 BufferParams const & params() const;
286 /** The list of paragraphs.
287 This is a linked list of paragraph, this list holds the
288 whole contents of the document.
290 ParagraphList & paragraphs();
291 ParagraphList const & paragraphs() const;
293 /// LyX version control object.
295 LyXVC const & lyxvc() const;
297 /// Where to put temporary files.
298 std::string const & temppath() const;
300 /// Used when typesetting to place errorboxes.
302 TexRow const & texrow() const;
305 ParIterator par_iterator_begin();
307 ParConstIterator par_iterator_begin() const;
309 ParIterator par_iterator_end();
311 ParConstIterator par_iterator_end() const;
313 /** \returns true only when the file is fully loaded.
314 * Used to prevent the premature generation of previews
315 * and by the citation inset.
317 bool fully_loaded() const;
318 /// Set by buffer_funcs' newFile.
319 void fully_loaded(bool);
321 /// Our main text (inside the top InsetText)
322 LyXText & text() const;
324 /// Our top InsetText!
325 InsetBase & inset() const;
333 bool hasMacro(docstring const & name) const;
335 MacroData const & getMacro(docstring const & name) const;
337 void insertMacro(docstring const & name, MacroData const & data);
340 void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
342 StableDocIterator getCursor() const { return cursor_; }
344 StableDocIterator getAnchor() const { return anchor_; }
346 void changeRefsIfUnique(docstring const & from, docstring const & to,
347 InsetBase::Code code);
348 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
349 /// including preamble
350 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
352 /// errorLists_ accessors.
354 ErrorList const & errorList(std::string const & type) const;
355 ErrorList & errorList(std::string const & type);
359 /** Inserts a file into a document
360 \return \c false if method fails.
362 bool readFile(LyXLex &, std::string const & filename);
364 bool do_writeFile(std::ostream & ofs) const;
366 /// Use the Pimpl idiom to hide the internals.
368 /// The pointer never changes although *pimpl_'s contents may.
369 boost::scoped_ptr<Impl> const pimpl_;
371 /// Save the cursor Position on Buffer switch
372 /// this would not be needed if every Buffer would have
373 /// it's BufferView, this should be FIXED in future.
374 StableDocIterator cursor_;
375 StableDocIterator anchor_;
376 /// A cache for the bibfiles (including bibfiles of loaded child
377 /// documents), needed for appropriate update of natbib labels.
378 std::vector<std::string> bibfilesCache_;
380 /// Container for all sort of Buffer dependant errors.
381 std::map<std::string, ErrorList> errorLists_;