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"
24 #include <boost/scoped_ptr.hpp>
25 #include <boost/signal.hpp>
48 class ParConstIterator;
53 class StableDocIterator;
56 /** The buffer object.
57 * This is the buffer object. It contains all the informations about
58 * a document loaded into LyX.
59 * The buffer object owns the LyXText (wrapped in an InsetText), which
60 * contains the individual paragraphs of the document.
63 * I am not sure if the class is complete or
64 * minimal, probably not.
65 * \author Lars Gullik Bjønnes
69 /// What type of log will \c getLogName() return?
71 latexlog, ///< LaTeX log
72 buildlog ///< Literate build log
77 \param b optional \c false by default
79 explicit Buffer(std::string const & file, bool b = false);
84 /** High-level interface to buffer functionality.
85 This function parses a command string and executes it
87 bool dispatch(std::string const & command, bool * result = 0);
89 /// Maybe we know the function already by number...
90 bool dispatch(FuncRequest const & func, bool * result = 0);
92 /// Load the autosaved file.
93 void loadAutoSaveFile();
96 bool readFile(std::string const & filename);
98 /// read the header, returns number of unknown tokens
99 int readHeader(LyXLex & lex);
101 /** Reads a file without header.
102 \param par if != 0 insert the file.
103 \return \c false if file is not completely read.
105 bool readDocument(LyXLex &);
108 void insertStringAsLines(ParagraphList & plist,
109 lyx::pit_type &, lyx::pos_type &,
110 LyXFont const &, std::string const &, bool);
112 ParIterator getParFromID(int id) const;
113 /// do we have a paragraph with this id?
114 bool hasParWithID(int id) const;
116 /// This signal is emitted when some parsing error shows up.
117 boost::signal<void(std::string)> errors;
118 /// This signal is emitted when some message shows up.
119 boost::signal<void(std::string)> message;
120 /// This signal is emitted when the buffer busy status change.
121 boost::signal<void(bool)> busy;
122 /// This signal is emitted when the buffer readonly status change.
123 boost::signal<void(bool)> readonly;
124 /// Update window titles of all users.
125 boost::signal<void()> updateTitles;
126 /// Reset autosave timers for all users.
127 boost::signal<void()> resetAutosaveTimers;
128 /// This signal is emitting if the buffer is being closed.
129 boost::signal<void()> closing;
133 Takes care of auto-save files and backup file if requested.
134 Returns \c true if the save is successful, \c false otherwise.
138 /// Write file. Returns \c false if unsuccesful.
139 bool writeFile(std::string const &) const;
142 void writeFileAscii(std::string const &, OutputParams const &);
144 void writeFileAscii(std::ostream &, OutputParams const &);
145 /// Just a wrapper for the method below, first creating the ofstream.
146 void makeLaTeXFile(std::string const & filename,
147 std::string const & original_path,
148 OutputParams const &,
149 bool output_preamble = true,
150 bool output_body = true);
152 void makeLaTeXFile(std::ostream & os,
153 std::string const & original_path,
154 OutputParams const &,
155 bool output_preamble = true,
156 bool output_body = true);
158 void makeLinuxDocFile(std::string const & filename,
159 OutputParams const & runparams_in,
160 bool only_body = false);
162 void makeDocBookFile(std::string const & filename,
163 OutputParams const & runparams_in,
164 bool only_body = false);
165 /// returns the main language for the buffer (document)
166 Language const * getLanguage() const;
167 /// get l10n translated to the buffers language
168 std::string const B_(std::string const & l10n) const;
172 /// return true if the main lyx file does not need saving
173 bool isClean() const;
175 bool isBakClean() const;
177 bool isDepClean(std::string const & name) const;
179 /// mark the main lyx file as not needing saving
180 void markClean() const;
186 void markDepClean(std::string const & name);
189 void setUnnamed(bool flag = true);
192 bool isUnnamed() const;
194 /// Mark this buffer as dirty.
197 /// Returns the buffer's filename. It is always an absolute path.
198 std::string const & fileName() const;
200 /// Returns the the path where the buffer lives.
201 /// It is always an absolute path.
202 std::string const & filePath() const;
204 /** A transformed version of the file name, adequate for LaTeX.
205 \param no_path optional if \c true then the path is stripped.
207 std::string const getLatexName(bool no_path = true) const;
209 /// Get the name and type of the log.
210 std::pair<LogType, std::string> const getLogName() const;
212 /// Change name of buffer. Updates "read-only" flag.
213 void setFileName(std::string const & newfile);
215 /// Name of the document's parent
216 void setParentName(std::string const &);
218 /** Get the document's master (or \c this if this is not a
221 Buffer const * getMasterBuffer() const;
222 /** Get the document's master (or \c this if this is not a
225 Buffer * getMasterBuffer();
227 /// Is buffer read-only?
228 bool isReadonly() const;
230 /// Set buffer read-only flag
231 void setReadonly(bool flag = true);
233 /// returns \c true if the buffer contains a LaTeX document
234 bool isLatex() const;
235 /// returns \c true if the buffer contains a LinuxDoc document
236 bool isLinuxDoc() const;
237 /// returns \c true if the buffer contains a DocBook document
238 bool isDocBook() const;
239 /** returns \c true if the buffer contains either a LinuxDoc
240 or DocBook document */
242 /// returns \c true if the buffer contains a Wed document
243 bool isLiterate() const;
245 /** Validate a buffer for LaTeX.
246 This validates the buffer, and returns a struct for use by
247 #makeLaTeX# and others. Its main use is to figure out what
248 commands and packages need to be included in the LaTeX file.
249 It (should) also check that the needed constructs are there
250 (i.e. that the \refs points to coresponding \labels). It
251 should perhaps inset "error" insets to help the user correct
254 void validate(LaTeXFeatures &) const;
256 /// return all bibkeys from buffer and its childs
257 void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
258 /// Update the cache with all bibfiles in use (including bibfiles
259 /// of loaded child documents).
260 void updateBibfilesCache();
261 /// Return the cache with all bibfiles in use (including bibfiles
262 /// of loaded child documents).
263 std::vector<std::string> const & getBibfilesCache() const;
265 void getLabelList(std::vector<std::string> &) const;
268 void changeLanguage(Language const * from, Language const * to);
271 void updateDocLang(Language const * nlang);
274 bool isMultiLingual() const;
276 /// Does this mean that this is buffer local?
277 limited_stack<Undo> & undostack();
278 limited_stack<Undo> const & undostack() const;
280 /// Does this mean that this is buffer local?
281 limited_stack<Undo> & redostack();
282 limited_stack<Undo> const & redostack() const;
285 BufferParams & params();
286 BufferParams const & params() const;
288 /** The list of paragraphs.
289 This is a linked list of paragraph, this list holds the
290 whole contents of the document.
292 ParagraphList & paragraphs();
293 ParagraphList const & paragraphs() const;
295 /// LyX version control object.
297 LyXVC const & lyxvc() const;
299 /// Where to put temporary files.
300 std::string const & temppath() const;
302 /// Used when typesetting to place errorboxes.
304 TexRow const & texrow() const;
307 ParIterator par_iterator_begin();
309 ParConstIterator par_iterator_begin() const;
311 ParIterator par_iterator_end();
313 ParConstIterator par_iterator_end() const;
315 /** \returns true only when the file is fully loaded.
316 * Used to prevent the premature generation of previews
317 * and by the citation inset.
319 bool fully_loaded() const;
320 /// Set by buffer_funcs' newFile.
321 void fully_loaded(bool);
323 /// Our main text (inside the top InsetText)
324 LyXText & text() const;
326 /// Our top InsetText!
327 InsetBase & inset() const;
335 bool hasMacro(std::string const & name) const;
337 MacroData const & getMacro(std::string const & name) const;
339 void insertMacro(std::string const & name, MacroData const & data);
341 void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
343 StableDocIterator getCursor() const { return cursor_; }
345 StableDocIterator getAnchor() const { return anchor_; }
347 void changeRefsIfUnique(std::string const & from, std::string const & to);
348 /// get source code (latex/docbook/linuxdoc) for some paragraphs
349 void getSourceCode(std::ostream & os, lyx::pit_type par_begin, lyx::pit_type par_end);
351 /// errorList_ accessor.
352 ErrorList const & getErrorList() const;
353 /// replace the internal errorList_
354 /** FIXME: This method is const for now because the ErrorList GUI
355 * showing mechanism is used by other classes in order to show their
356 * own processing errors (ex: Converter.C).
358 void setErrorList(ErrorList const &) const;
359 /// add an error to the errorList_
360 /** FIXME: This method is const for now because the ErrorList GUI
361 * showing mechanism is used by other classes in order to show their
362 * own processing errors (ex: Converter.C).
364 void addError(ErrorItem const &) const;
367 /** Inserts a file into a document
368 \return \c false if method fails.
370 bool readFile(LyXLex &, std::string const & filename);
372 bool do_writeFile(std::ostream & ofs) const;
374 /// Use the Pimpl idiom to hide the internals.
376 /// The pointer never changes although *pimpl_'s contents may.
377 boost::scoped_ptr<Impl> const pimpl_;
379 /// Save the cursor Position on Buffer switch
380 /// this would not be needed if every Buffer would have
381 /// it's BufferView, this should be FIXED in future.
382 StableDocIterator cursor_;
383 StableDocIterator anchor_;
384 /// A cache for the bibfiles (including bibfiles of loaded child
385 /// documents), needed for appropriate update of natbib labels.
386 std::vector<std::string> bibfilesCache_;
388 /// An error list (replaces the error insets)
389 /** FIXME: This member is mutable for now because the ErrorList GUI
390 * showing mechanism is used by other classes in order to show their
391 * own processing errors (ex: Converter.C).
393 mutable ErrorList errorList_;