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 "DocIterator.h"
17 #include "support/FileName.h"
18 #include "support/limited_stack.h"
19 #include "support/types.h"
20 #include "support/docstring.h"
21 #include "support/docstream.h"
23 #include <boost/scoped_ptr.hpp>
24 #include <boost/signal.hpp>
49 class ParConstIterator;
58 /** The buffer object.
59 * This is the buffer object. It contains all the informations about
60 * a document loaded into LyX.
61 * The buffer object owns the Text (wrapped in an InsetText), which
62 * contains the individual paragraphs of the document.
65 * I am not sure if the class is complete or
66 * minimal, probably not.
67 * \author Lars Gullik Bjønnes
71 /// What type of log will \c getLogName() return?
73 latexlog, ///< LaTeX log
74 buildlog ///< Literate build log
77 /// Result of \c readFile()
79 failure, ///< The file could not be read
80 success, ///< The file could not be read
81 wrongversion ///< The version of the file does not match ours
84 /// Method to check if a file is externally modified, used by
85 /// isExternallyModified()
87 * timestamp is fast but inaccurate. For example, the granularity
88 * of timestamp on a FAT filesystem is 2 second. Also, various operations
89 * may touch the timestamp of a file even when its content is unchanged.
91 * checksum is accurate but slow, which can be a problem when it is
92 * frequently used, or used for a large file on a slow (network) file
96 checksum_method, ///< Use file check sum
97 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
102 \param b optional \c false by default
104 explicit Buffer(std::string const & file, bool b = false);
109 /** High-level interface to buffer functionality.
110 This function parses a command string and executes it
112 bool dispatch(std::string const & command, bool * result = 0);
114 /// Maybe we know the function already by number...
115 bool dispatch(FuncRequest const & func, bool * result = 0);
117 /// Load the autosaved file.
118 void loadAutoSaveFile();
120 /// read a new document from a string
121 bool readString(std::string const &);
123 bool readFile(support::FileName const & filename);
125 /// read the header, returns number of unknown tokens
126 int readHeader(Lexer & lex);
128 /** Reads a file without header.
129 \param par if != 0 insert the file.
130 \return \c false if file is not completely read.
132 bool readDocument(Lexer &);
135 void insertStringAsLines(ParagraphList & plist,
136 pit_type &, pos_type &,
137 Font const &, docstring const &, bool);
139 ParIterator getParFromID(int id) const;
140 /// do we have a paragraph with this id?
141 bool hasParWithID(int id) const;
143 /// This signal is emitted when the buffer is changed.
144 boost::signal<void()> changed;
145 /// This signal is emitted when the buffer structure is changed.
146 boost::signal<void()> structureChanged;
147 /// This signal is emitted when an embedded file is changed
148 boost::signal<void()> embeddingChanged;
149 /// This signal is emitted when some parsing error shows up.
150 boost::signal<void(std::string)> errors;
151 /// This signal is emitted when some message shows up.
152 boost::signal<void(docstring)> message;
153 /// This signal is emitted when the buffer busy status change.
154 boost::signal<void(bool)> busy;
155 /// This signal is emitted when the buffer readonly status change.
156 boost::signal<void(bool)> readonly;
157 /// Update window titles of all users.
158 boost::signal<void()> updateTitles;
159 /// Reset autosave timers for all users.
160 boost::signal<void()> resetAutosaveTimers;
161 /// This signal is emitting if the buffer is being closed.
162 boost::signal<void(Buffer *)> closing;
166 Takes care of auto-save files and backup file if requested.
167 Returns \c true if the save is successful, \c false otherwise.
171 /// Write document to stream. Returns \c false if unsuccesful.
172 bool write(std::ostream &) const;
173 /// Write file. Returns \c false if unsuccesful.
174 bool writeFile(support::FileName const &) const;
176 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
177 bool makeLaTeXFile(support::FileName const & filename,
178 std::string const & original_path,
179 OutputParams const &,
180 bool output_preamble = true,
181 bool output_body = true);
182 /** Export the buffer to LaTeX.
183 If \p os is a file stream, and params().inputenc is "auto" or
184 "default", and the buffer contains text in different languages
185 with more than one encoding, then this method will change the
186 encoding associated to \p os. Therefore you must not call this
187 method with a string stream if the output is supposed to go to a
190 ofs.open("test.tex");
191 writeLaTeXSource(ofs, ...);
193 \endcode is NOT equivalent to \code
194 odocstringstream oss;
195 writeLaTeXSource(oss, ...);
197 ofs.open("test.tex");
202 void writeLaTeXSource(odocstream & os,
203 std::string const & original_path,
204 OutputParams const &,
205 bool output_preamble = true,
206 bool output_body = true);
208 void makeDocBookFile(support::FileName const & filename,
209 OutputParams const & runparams_in,
210 bool only_body = false);
212 void writeDocBookSource(odocstream & os, std::string const & filename,
213 OutputParams const & runparams_in,
214 bool only_body = false);
215 /// returns the main language for the buffer (document)
216 Language const * getLanguage() const;
217 /// get l10n translated to the buffers language
218 docstring const B_(std::string const & l10n) const;
222 /// return true if the main lyx file does not need saving
223 bool isClean() const;
225 bool isBakClean() const;
227 bool isDepClean(std::string const & name) const;
229 /// whether or not disk file has been externally modified
230 bool isExternallyModified(CheckMethod method) const;
232 /// save timestamp and checksum of the given file.
233 void saveCheckSum(std::string const & file) const;
235 /// mark the main lyx file as not needing saving
236 void markClean() const;
242 void markDepClean(std::string const & name);
245 void setUnnamed(bool flag = true);
248 bool isUnnamed() const;
250 /// Mark this buffer as dirty.
253 /// Returns the buffer's filename. It is always an absolute path.
254 std::string const fileName() const;
256 /// Returns the the path where the buffer lives.
257 /// It is always an absolute path.
258 std::string const & filePath() const;
260 /** A transformed version of the file name, adequate for LaTeX.
261 \param no_path optional if \c true then the path is stripped.
263 std::string const getLatexName(bool no_path = true) const;
265 /// Get thee name and type of the log.
266 std::pair<LogType, std::string> const getLogName() const;
268 /// Change name of buffer. Updates "read-only" flag.
269 void setFileName(std::string const & newfile);
271 /// Name of the document's parent
272 void setParentName(std::string const &);
274 /** Get the document's master (or \c this if this is not a
277 Buffer const * getMasterBuffer() const;
278 /** Get the document's master (or \c this if this is not a
281 Buffer * getMasterBuffer();
283 /// Is buffer read-only?
284 bool isReadonly() const;
286 /// Set buffer read-only flag
287 void setReadonly(bool flag = true);
289 /// returns \c true if the buffer contains a LaTeX document
290 bool isLatex() const;
291 /// returns \c true if the buffer contains a DocBook document
292 bool isDocBook() const;
293 /// returns \c true if the buffer contains a Wed document
294 bool isLiterate() const;
296 /** Validate a buffer for LaTeX.
297 This validates the buffer, and returns a struct for use by
298 #makeLaTeX# and others. Its main use is to figure out what
299 commands and packages need to be included in the LaTeX file.
300 It (should) also check that the needed constructs are there
301 (i.e. that the \refs points to coresponding \labels). It
302 should perhaps inset "error" insets to help the user correct
305 void validate(LaTeXFeatures &) const;
307 /// Update the cache with all bibfiles in use (including bibfiles
308 /// of loaded child documents).
309 void updateBibfilesCache();
310 /// Return the cache with all bibfiles in use (including bibfiles
311 /// of loaded child documents).
312 std::vector<support::FileName> const & getBibfilesCache() const;
314 void getLabelList(std::vector<docstring> &) const;
317 void changeLanguage(Language const * from, Language const * to);
320 bool isMultiLingual() const;
322 /// Does this mean that this is buffer local?
323 limited_stack<Undo> & undostack();
324 limited_stack<Undo> const & undostack() const;
326 /// Does this mean that this is buffer local?
327 limited_stack<Undo> & redostack();
328 limited_stack<Undo> const & redostack() const;
331 BufferParams & params();
332 BufferParams const & params() const;
334 /** The list of paragraphs.
335 This is a linked list of paragraph, this list holds the
336 whole contents of the document.
338 ParagraphList & paragraphs();
339 ParagraphList const & paragraphs() const;
341 /// LyX version control object.
343 LyXVC const & lyxvc() const;
345 /// Where to put temporary files.
346 std::string const & temppath() const;
348 /// Used when typesetting to place errorboxes.
350 TexRow const & texrow() const;
353 ParIterator par_iterator_begin();
355 ParConstIterator par_iterator_begin() const;
357 ParIterator par_iterator_end();
359 ParConstIterator par_iterator_end() const;
361 /** \returns true only when the file is fully loaded.
362 * Used to prevent the premature generation of previews
363 * and by the citation inset.
365 bool fully_loaded() const;
366 /// Set by buffer_funcs' newFile.
367 void fully_loaded(bool);
369 /// Our main text (inside the top InsetText)
372 /// Our top InsetText!
373 Inset & inset() const;
381 bool hasMacro(docstring const & name) const;
383 MacroData const & getMacro(docstring const & name) const;
385 void insertMacro(docstring const & name, MacroData const & data);
388 void changeRefsIfUnique(docstring const & from, docstring const & to,
390 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
391 /// including preamble
392 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
394 /// errorLists_ accessors.
396 ErrorList const & errorList(std::string const & type) const;
397 ErrorList & errorList(std::string const & type);
401 TocBackend & tocBackend();
402 TocBackend const & tocBackend() const;
406 EmbeddedFiles & embeddedFiles();
407 EmbeddedFiles const & embeddedFiles() const;
411 /** Inserts a file into a document
412 \return \c false if method fails.
414 ReadStatus readFile(Lexer &, support::FileName const & filename,
415 bool fromString = false);
417 /// Use the Pimpl idiom to hide the internals.
419 /// The pointer never changes although *pimpl_'s contents may.
420 boost::scoped_ptr<Impl> const pimpl_;
422 /// A cache for the bibfiles (including bibfiles of loaded child
423 /// documents), needed for appropriate update of natbib labels.
424 mutable std::vector<support::FileName> bibfilesCache_;