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 "insets/InsetCode.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"
46 class ParConstIterator;
55 class GuiBufferDelegate;
56 class WorkAreaManager;
59 /** The buffer object.
60 * This is the buffer object. It contains all the informations about
61 * a document loaded into LyX.
62 * The buffer object owns the Text (wrapped in an InsetText), which
63 * contains the individual paragraphs of the document.
66 * I am not sure if the class is complete or
67 * minimal, probably not.
68 * \author Lars Gullik Bjønnes
72 /// What type of log will \c getLogName() return?
74 latexlog, ///< LaTeX log
75 buildlog ///< Literate build log
78 /// Result of \c readFile()
80 failure, ///< The file could not be read
81 success, ///< The file could not be read
82 wrongversion ///< The version of the file does not match ours
85 /// Method to check if a file is externally modified, used by
86 /// isExternallyModified()
88 * timestamp is fast but inaccurate. For example, the granularity
89 * of timestamp on a FAT filesystem is 2 second. Also, various operations
90 * may touch the timestamp of a file even when its content is unchanged.
92 * checksum is accurate but slow, which can be a problem when it is
93 * frequently used, or used for a large file on a slow (network) file
97 checksum_method, ///< Use file check sum
98 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
103 \param b optional \c false by default
105 explicit Buffer(std::string const & file, bool b = false);
110 /** High-level interface to buffer functionality.
111 This function parses a command string and executes it
113 bool dispatch(std::string const & command, bool * result = 0);
115 /// Maybe we know the function already by number...
116 bool dispatch(FuncRequest const & func, bool * result = 0);
118 /// Load the autosaved file.
119 void loadAutoSaveFile();
121 /// read a new document from a string
122 bool readString(std::string const &);
124 bool readFile(support::FileName const & filename);
126 /// read the header, returns number of unknown tokens
127 int readHeader(Lexer & lex);
129 /** Reads a file without header.
130 \param par if != 0 insert the file.
131 \return \c false if file is not completely read.
133 bool readDocument(Lexer &);
136 void insertStringAsLines(ParagraphList & plist,
137 pit_type &, pos_type &,
138 Font const &, docstring const &, bool);
140 ParIterator getParFromID(int id) const;
141 /// do we have a paragraph with this id?
142 bool hasParWithID(int id) const;
145 frontend::WorkAreaManager & workAreaManager() const;
148 Takes care of auto-save files and backup file if requested.
149 Returns \c true if the save is successful, \c false otherwise.
153 /// Write document to stream. Returns \c false if unsuccesful.
154 bool write(std::ostream &) const;
155 /// Write file. Returns \c false if unsuccesful.
156 bool writeFile(support::FileName const &) const;
158 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
159 bool makeLaTeXFile(support::FileName const & filename,
160 std::string const & original_path,
161 OutputParams const &,
162 bool output_preamble = true,
163 bool output_body = true);
164 /** Export the buffer to LaTeX.
165 If \p os is a file stream, and params().inputenc is "auto" or
166 "default", and the buffer contains text in different languages
167 with more than one encoding, then this method will change the
168 encoding associated to \p os. Therefore you must not call this
169 method with a string stream if the output is supposed to go to a
172 ofs.open("test.tex");
173 writeLaTeXSource(ofs, ...);
175 \endcode is NOT equivalent to \code
176 odocstringstream oss;
177 writeLaTeXSource(oss, ...);
179 ofs.open("test.tex");
184 void writeLaTeXSource(odocstream & os,
185 std::string const & original_path,
186 OutputParams const &,
187 bool output_preamble = true,
188 bool output_body = true);
190 void makeDocBookFile(support::FileName const & filename,
191 OutputParams const & runparams_in,
192 bool only_body = false);
194 void writeDocBookSource(odocstream & os, std::string const & filename,
195 OutputParams const & runparams_in,
196 bool only_body = false);
197 /// returns the main language for the buffer (document)
198 Language const * getLanguage() const;
199 /// get l10n translated to the buffers language
200 docstring const B_(std::string const & l10n) const;
204 /// return true if the main lyx file does not need saving
205 bool isClean() const;
207 bool isBakClean() const;
209 bool isDepClean(std::string const & name) const;
211 /// whether or not disk file has been externally modified
212 bool isExternallyModified(CheckMethod method) const;
214 /// save timestamp and checksum of the given file.
215 void saveCheckSum(std::string const & file) const;
217 /// mark the main lyx file as not needing saving
218 void markClean() const;
221 void markBakClean() const;
224 void markDepClean(std::string const & name);
227 void setUnnamed(bool flag = true);
230 bool isUnnamed() const;
232 /// Mark this buffer as dirty.
235 /// Returns the buffer's filename. It is always an absolute path.
236 std::string const fileName() const;
238 /// Returns the the path where the buffer lives.
239 /// It is always an absolute path.
240 std::string const & filePath() const;
242 /** A transformed version of the file name, adequate for LaTeX.
243 \param no_path optional if \c true then the path is stripped.
245 std::string const getLatexName(bool no_path = true) const;
247 /// Get thee name and type of the log.
248 std::pair<LogType, std::string> const getLogName() const;
250 /// Change name of buffer. Updates "read-only" flag.
251 void setFileName(std::string const & newfile);
253 /// Name of the document's parent
254 void setParentName(std::string const &);
256 /** Get the document's master (or \c this if this is not a
259 Buffer const * getMasterBuffer() const;
260 /** Get the document's master (or \c this if this is not a
263 Buffer * getMasterBuffer();
265 /// Is buffer read-only?
266 bool isReadonly() const;
268 /// Set buffer read-only flag
269 void setReadonly(bool flag = true);
271 /// returns \c true if the buffer contains a LaTeX document
272 bool isLatex() const;
273 /// returns \c true if the buffer contains a DocBook document
274 bool isDocBook() const;
275 /// returns \c true if the buffer contains a Wed document
276 bool isLiterate() const;
278 /** Validate a buffer for LaTeX.
279 This validates the buffer, and returns a struct for use by
280 #makeLaTeX# and others. Its main use is to figure out what
281 commands and packages need to be included in the LaTeX file.
282 It (should) also check that the needed constructs are there
283 (i.e. that the \refs points to coresponding \labels). It
284 should perhaps inset "error" insets to help the user correct
287 void validate(LaTeXFeatures &) const;
289 /// Update the cache with all bibfiles in use (including bibfiles
290 /// of loaded child documents).
291 void updateBibfilesCache();
292 /// Return the cache with all bibfiles in use (including bibfiles
293 /// of loaded child documents).
294 std::vector<support::FileName> const & getBibfilesCache() const;
296 void getLabelList(std::vector<docstring> &) const;
299 void changeLanguage(Language const * from, Language const * to);
302 bool isMultiLingual() const;
304 /// Does this mean that this is buffer local?
305 limited_stack<Undo> & undostack();
306 limited_stack<Undo> const & undostack() const;
308 /// Does this mean that this is buffer local?
309 limited_stack<Undo> & redostack();
310 limited_stack<Undo> const & redostack() const;
313 BufferParams & params();
314 BufferParams const & params() const;
316 /** The list of paragraphs.
317 This is a linked list of paragraph, this list holds the
318 whole contents of the document.
320 ParagraphList & paragraphs();
321 ParagraphList const & paragraphs() const;
323 /// LyX version control object.
325 LyXVC const & lyxvc() const;
327 /// Where to put temporary files.
328 std::string const & temppath() const;
330 /// Used when typesetting to place errorboxes.
332 TexRow const & texrow() const;
335 ParIterator par_iterator_begin();
337 ParConstIterator par_iterator_begin() const;
339 ParIterator par_iterator_end();
341 ParConstIterator par_iterator_end() const;
343 /** \returns true only when the file is fully loaded.
344 * Used to prevent the premature generation of previews
345 * and by the citation inset.
347 bool fully_loaded() const;
348 /// Set by buffer_funcs' newFile.
349 void fully_loaded(bool);
351 /// Our main text (inside the top InsetText)
354 /// Our top InsetText!
355 Inset & inset() const;
363 bool hasMacro(docstring const & name) const;
365 MacroData const & getMacro(docstring const & name) const;
367 void insertMacro(docstring const & name, MacroData const & data);
369 /// Replace the inset contents for insets which InsetCode is equal
370 /// to the passed \p inset_code.
371 void changeRefsIfUnique(docstring const & from, docstring const & to,
374 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
375 /// including preamble
376 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
378 /// errorLists_ accessors.
380 ErrorList const & errorList(std::string const & type) const;
381 ErrorList & errorList(std::string const & type);
385 TocBackend & tocBackend();
386 TocBackend const & tocBackend() const;
390 EmbeddedFiles & embeddedFiles();
391 EmbeddedFiles const & embeddedFiles() const;
394 /// This function is called when the buffer is changed.
395 void changed() const;
396 /// This function is called when the buffer structure is changed.
397 void structureChanged() const;
398 /// This function is called when an embedded file is changed
399 void embeddingChanged() const;
400 /// This function is called when some parsing error shows up.
401 void errors(std::string const & err) const;
402 /// This function is called when the buffer busy status change.
403 void busy(bool on) const;
404 /// This function is called when the buffer readonly status change.
405 void readonly(bool on) const;
406 /// Update window titles of all users.
407 void updateTitles() const;
408 /// Reset autosave timers for all users.
409 void resetAutosaveTimers() const;
411 void message(docstring const & msg) const;
413 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
416 void autoSave() const;
418 bool writeAs(std::string const & newname = std::string());
423 /** Inserts a file into a document
424 \return \c false if method fails.
426 ReadStatus readFile(Lexer &, support::FileName const & filename,
427 bool fromString = false);
429 /// Use the Pimpl idiom to hide the internals.
431 /// The pointer never changes although *pimpl_'s contents may.
434 /// A cache for the bibfiles (including bibfiles of loaded child
435 /// documents), needed for appropriate update of natbib labels.
436 mutable std::vector<support::FileName> bibfilesCache_;
438 frontend::GuiBufferDelegate * gui_;