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 class WorkAreaManager;
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 Text (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
80 /// Result of \c readFile()
82 failure, ///< The file could not be read
83 success, ///< The file could not be read
84 wrongversion ///< The version of the file does not match ours
87 /// Method to check if a file is externally modified, used by
88 /// isExternallyModified()
90 * timestamp is fast but inaccurate. For example, the granularity
91 * of timestamp on a FAT filesystem is 2 second. Also, various operations
92 * may touch the timestamp of a file even when its content is unchanged.
94 * checksum is accurate but slow, which can be a problem when it is
95 * frequently used, or used for a large file on a slow (network) file
99 checksum_method, ///< Use file check sum
100 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
105 \param b optional \c false by default
107 explicit Buffer(std::string const & file, bool b = false);
112 /** High-level interface to buffer functionality.
113 This function parses a command string and executes it
115 bool dispatch(std::string const & command, bool * result = 0);
117 /// Maybe we know the function already by number...
118 bool dispatch(FuncRequest const & func, bool * result = 0);
120 /// Load the autosaved file.
121 void loadAutoSaveFile();
123 /// read a new document from a string
124 bool readString(std::string const &);
126 bool readFile(support::FileName const & filename);
128 /// read the header, returns number of unknown tokens
129 int readHeader(Lexer & lex);
131 /** Reads a file without header.
132 \param par if != 0 insert the file.
133 \return \c false if file is not completely read.
135 bool readDocument(Lexer &);
138 void insertStringAsLines(ParagraphList & plist,
139 pit_type &, pos_type &,
140 Font const &, docstring const &, bool);
142 ParIterator getParFromID(int id) const;
143 /// do we have a paragraph with this id?
144 bool hasParWithID(int id) const;
146 /// This signal is emitted when the buffer is changed.
150 frontend::WorkAreaManager * workAreaManager() const;
152 /// This signal is emitted when the buffer structure is changed.
153 boost::signal<void()> structureChanged;
154 /// This signal is emitted when an embedded file is changed
155 boost::signal<void()> embeddingChanged;
156 /// This signal is emitted when some parsing error shows up.
157 boost::signal<void(std::string)> errors;
158 /// This signal is emitted when some message shows up.
159 boost::signal<void(docstring)> message;
160 /// This signal is emitted when the buffer busy status change.
161 boost::signal<void(bool)> busy;
162 /// This signal is emitted when the buffer readonly status change.
163 boost::signal<void(bool)> readonly;
164 /// Update window titles of all users.
165 boost::signal<void()> updateTitles;
166 /// Reset autosave timers for all users.
167 boost::signal<void()> resetAutosaveTimers;
171 Takes care of auto-save files and backup file if requested.
172 Returns \c true if the save is successful, \c false otherwise.
176 /// Write document to stream. Returns \c false if unsuccesful.
177 bool write(std::ostream &) const;
178 /// Write file. Returns \c false if unsuccesful.
179 bool writeFile(support::FileName const &) const;
181 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
182 bool makeLaTeXFile(support::FileName const & filename,
183 std::string const & original_path,
184 OutputParams const &,
185 bool output_preamble = true,
186 bool output_body = true);
187 /** Export the buffer to LaTeX.
188 If \p os is a file stream, and params().inputenc is "auto" or
189 "default", and the buffer contains text in different languages
190 with more than one encoding, then this method will change the
191 encoding associated to \p os. Therefore you must not call this
192 method with a string stream if the output is supposed to go to a
195 ofs.open("test.tex");
196 writeLaTeXSource(ofs, ...);
198 \endcode is NOT equivalent to \code
199 odocstringstream oss;
200 writeLaTeXSource(oss, ...);
202 ofs.open("test.tex");
207 void writeLaTeXSource(odocstream & os,
208 std::string const & original_path,
209 OutputParams const &,
210 bool output_preamble = true,
211 bool output_body = true);
213 void makeDocBookFile(support::FileName const & filename,
214 OutputParams const & runparams_in,
215 bool only_body = false);
217 void writeDocBookSource(odocstream & os, std::string const & filename,
218 OutputParams const & runparams_in,
219 bool only_body = false);
220 /// returns the main language for the buffer (document)
221 Language const * getLanguage() const;
222 /// get l10n translated to the buffers language
223 docstring const B_(std::string const & l10n) const;
227 /// return true if the main lyx file does not need saving
228 bool isClean() const;
230 bool isBakClean() const;
232 bool isDepClean(std::string const & name) const;
234 /// whether or not disk file has been externally modified
235 bool isExternallyModified(CheckMethod method) const;
237 /// save timestamp and checksum of the given file.
238 void saveCheckSum(std::string const & file) const;
240 /// mark the main lyx file as not needing saving
241 void markClean() const;
247 void markDepClean(std::string const & name);
250 void setUnnamed(bool flag = true);
253 bool isUnnamed() const;
255 /// Mark this buffer as dirty.
258 /// Returns the buffer's filename. It is always an absolute path.
259 std::string const fileName() const;
261 /// Returns the the path where the buffer lives.
262 /// It is always an absolute path.
263 std::string const & filePath() const;
265 /** A transformed version of the file name, adequate for LaTeX.
266 \param no_path optional if \c true then the path is stripped.
268 std::string const getLatexName(bool no_path = true) const;
270 /// Get thee name and type of the log.
271 std::pair<LogType, std::string> const getLogName() const;
273 /// Change name of buffer. Updates "read-only" flag.
274 void setFileName(std::string const & newfile);
276 /// Name of the document's parent
277 void setParentName(std::string const &);
279 /** Get the document's master (or \c this if this is not a
282 Buffer const * getMasterBuffer() const;
283 /** Get the document's master (or \c this if this is not a
286 Buffer * getMasterBuffer();
288 /// Is buffer read-only?
289 bool isReadonly() const;
291 /// Set buffer read-only flag
292 void setReadonly(bool flag = true);
294 /// returns \c true if the buffer contains a LaTeX document
295 bool isLatex() const;
296 /// returns \c true if the buffer contains a DocBook document
297 bool isDocBook() const;
298 /// returns \c true if the buffer contains a Wed document
299 bool isLiterate() const;
301 /** Validate a buffer for LaTeX.
302 This validates the buffer, and returns a struct for use by
303 #makeLaTeX# and others. Its main use is to figure out what
304 commands and packages need to be included in the LaTeX file.
305 It (should) also check that the needed constructs are there
306 (i.e. that the \refs points to coresponding \labels). It
307 should perhaps inset "error" insets to help the user correct
310 void validate(LaTeXFeatures &) const;
312 /// Update the cache with all bibfiles in use (including bibfiles
313 /// of loaded child documents).
314 void updateBibfilesCache();
315 /// Return the cache with all bibfiles in use (including bibfiles
316 /// of loaded child documents).
317 std::vector<support::FileName> const & getBibfilesCache() const;
319 void getLabelList(std::vector<docstring> &) const;
322 void changeLanguage(Language const * from, Language const * to);
325 bool isMultiLingual() const;
327 /// Does this mean that this is buffer local?
328 limited_stack<Undo> & undostack();
329 limited_stack<Undo> const & undostack() const;
331 /// Does this mean that this is buffer local?
332 limited_stack<Undo> & redostack();
333 limited_stack<Undo> const & redostack() const;
336 BufferParams & params();
337 BufferParams const & params() const;
339 /** The list of paragraphs.
340 This is a linked list of paragraph, this list holds the
341 whole contents of the document.
343 ParagraphList & paragraphs();
344 ParagraphList const & paragraphs() const;
346 /// LyX version control object.
348 LyXVC const & lyxvc() const;
350 /// Where to put temporary files.
351 std::string const & temppath() const;
353 /// Used when typesetting to place errorboxes.
355 TexRow const & texrow() const;
358 ParIterator par_iterator_begin();
360 ParConstIterator par_iterator_begin() const;
362 ParIterator par_iterator_end();
364 ParConstIterator par_iterator_end() const;
366 /** \returns true only when the file is fully loaded.
367 * Used to prevent the premature generation of previews
368 * and by the citation inset.
370 bool fully_loaded() const;
371 /// Set by buffer_funcs' newFile.
372 void fully_loaded(bool);
374 /// Our main text (inside the top InsetText)
377 /// Our top InsetText!
378 Inset & inset() const;
386 bool hasMacro(docstring const & name) const;
388 MacroData const & getMacro(docstring const & name) const;
390 void insertMacro(docstring const & name, MacroData const & data);
393 void changeRefsIfUnique(docstring const & from, docstring const & to,
395 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
396 /// including preamble
397 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
399 /// errorLists_ accessors.
401 ErrorList const & errorList(std::string const & type) const;
402 ErrorList & errorList(std::string const & type);
406 TocBackend & tocBackend();
407 TocBackend const & tocBackend() const;
411 EmbeddedFiles & embeddedFiles();
412 EmbeddedFiles const & embeddedFiles() const;
416 /** Inserts a file into a document
417 \return \c false if method fails.
419 ReadStatus readFile(Lexer &, support::FileName const & filename,
420 bool fromString = false);
422 /// Use the Pimpl idiom to hide the internals.
424 /// The pointer never changes although *pimpl_'s contents may.
425 boost::scoped_ptr<Impl> const pimpl_;
427 /// A cache for the bibfiles (including bibfiles of loaded child
428 /// documents), needed for appropriate update of natbib labels.
429 mutable std::vector<support::FileName> bibfilesCache_;