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/strfwd.h"
19 #include "support/types.h"
44 class ParConstIterator;
53 class GuiBufferDelegate;
54 class WorkAreaManager;
57 /** The buffer object.
58 * This is the buffer object. It contains all the informations about
59 * a document loaded into LyX.
60 * The buffer object owns the Text (wrapped in an InsetText), which
61 * contains the individual paragraphs of the document.
64 * I am not sure if the class is complete or
65 * minimal, probably not.
66 * \author Lars Gullik Bjønnes
70 /// What type of log will \c getLogName() return?
72 latexlog, ///< LaTeX log
73 buildlog ///< Literate build log
76 /// Result of \c readFile()
78 failure, ///< The file could not be read
79 success, ///< The file could not be read
80 wrongversion ///< The version of the file does not match ours
83 /// Method to check if a file is externally modified, used by
84 /// isExternallyModified()
86 * timestamp is fast but inaccurate. For example, the granularity
87 * of timestamp on a FAT filesystem is 2 second. Also, various operations
88 * may touch the timestamp of a file even when its content is unchanged.
90 * checksum is accurate but slow, which can be a problem when it is
91 * frequently used, or used for a large file on a slow (network) file
95 checksum_method, ///< Use file check sum
96 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
101 \param b optional \c false by default
103 explicit Buffer(std::string const & file, bool b = false);
108 /** High-level interface to buffer functionality.
109 This function parses a command string and executes it
111 bool dispatch(std::string const & command, bool * result = 0);
113 /// Maybe we know the function already by number...
114 bool dispatch(FuncRequest const & func, bool * result = 0);
116 /// Load the autosaved file.
117 void loadAutoSaveFile();
119 /// read a new document from a string
120 bool readString(std::string const &);
122 bool readFile(support::FileName const & filename);
124 /// read the header, returns number of unknown tokens
125 int readHeader(Lexer & lex);
127 /** Reads a file without header.
128 \param par if != 0 insert the file.
129 \return \c false if file is not completely read.
131 bool readDocument(Lexer &);
134 void insertStringAsLines(ParagraphList & plist,
135 pit_type &, pos_type &,
136 Font const &, docstring const &, bool);
138 ParIterator getParFromID(int id) const;
139 /// do we have a paragraph with this id?
140 bool hasParWithID(int id) const;
143 frontend::WorkAreaManager & workAreaManager() const;
146 Takes care of auto-save files and backup file if requested.
147 Returns \c true if the save is successful, \c false otherwise.
151 /// Write document to stream. Returns \c false if unsuccesful.
152 bool write(std::ostream &) const;
153 /// Write file. Returns \c false if unsuccesful.
154 bool writeFile(support::FileName const &) const;
156 /// Loads LyX file \c filename into buffer, * and \return success
157 bool loadLyXFile(support::FileName const & s);
159 /// Fill in the ErrorList with the TeXErrors
160 void bufferErrors(TeXErrors const &, ErrorList &) const;
162 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
163 bool makeLaTeXFile(support::FileName const & filename,
164 std::string const & original_path,
165 OutputParams const &,
166 bool output_preamble = true,
167 bool output_body = true);
168 /** Export the buffer to LaTeX.
169 If \p os is a file stream, and params().inputenc is "auto" or
170 "default", and the buffer contains text in different languages
171 with more than one encoding, then this method will change the
172 encoding associated to \p os. Therefore you must not call this
173 method with a string stream if the output is supposed to go to a
176 ofs.open("test.tex");
177 writeLaTeXSource(ofs, ...);
179 \endcode is NOT equivalent to \code
180 odocstringstream oss;
181 writeLaTeXSource(oss, ...);
183 ofs.open("test.tex");
188 void writeLaTeXSource(odocstream & os,
189 std::string const & original_path,
190 OutputParams const &,
191 bool output_preamble = true,
192 bool output_body = true);
194 void makeDocBookFile(support::FileName const & filename,
195 OutputParams const & runparams_in,
196 bool only_body = false);
198 void writeDocBookSource(odocstream & os, std::string const & filename,
199 OutputParams const & runparams_in,
200 bool only_body = false);
201 /// returns the main language for the buffer (document)
202 Language const * language() const;
203 /// get l10n translated to the buffers language
204 docstring const B_(std::string const & l10n) const;
208 /// return true if the main lyx file does not need saving
209 bool isClean() const;
211 bool isBakClean() const;
213 bool isDepClean(std::string const & name) const;
215 /// whether or not disk file has been externally modified
216 bool isExternallyModified(CheckMethod method) const;
218 /// save timestamp and checksum of the given file.
219 void saveCheckSum(support::FileName const & file) const;
221 /// mark the main lyx file as not needing saving
222 void markClean() const;
225 void markBakClean() const;
228 void markDepClean(std::string const & name);
231 void setUnnamed(bool flag = true);
234 bool isUnnamed() const;
236 /// Mark this buffer as dirty.
239 /// Returns the buffer's filename. It is always an absolute path.
240 support::FileName fileName() const;
242 /// Returns the buffer's filename. It is always an absolute path.
243 std::string absFileName() const;
245 /// Returns the the path where the buffer lives.
246 /// It is always an absolute path.
247 std::string filePath() const;
249 /** A transformed version of the file name, adequate for LaTeX.
250 \param no_path optional if \c true then the path is stripped.
252 std::string latexName(bool no_path = true) const;
254 /// Get thee name and type of the log.
255 std::string logName(LogType * type = 0) const;
257 /// Change name of buffer. Updates "read-only" flag.
258 void setFileName(std::string const & newfile);
260 /// Name of the document's parent
261 void setParentName(std::string const &);
263 /** Get the document's master (or \c this if this is not a
266 Buffer const * masterBuffer() const;
267 /** Get the document's master (or \c this if this is not a
270 Buffer * masterBuffer();
272 /// Is buffer read-only?
273 bool isReadonly() const;
275 /// Set buffer read-only flag
276 void setReadonly(bool flag = true);
278 /// returns \c true if the buffer contains a LaTeX document
279 bool isLatex() const;
280 /// returns \c true if the buffer contains a DocBook document
281 bool isDocBook() const;
282 /// returns \c true if the buffer contains a Wed document
283 bool isLiterate() const;
285 /** Validate a buffer for LaTeX.
286 This validates the buffer, and returns a struct for use by
287 #makeLaTeX# and others. Its main use is to figure out what
288 commands and packages need to be included in the LaTeX file.
289 It (should) also check that the needed constructs are there
290 (i.e. that the \refs points to coresponding \labels). It
291 should perhaps inset "error" insets to help the user correct
294 void validate(LaTeXFeatures &) const;
296 /// Update the cache with all bibfiles in use (including bibfiles
297 /// of loaded child documents).
298 void updateBibfilesCache();
299 /// Return the cache with all bibfiles in use (including bibfiles
300 /// of loaded child documents).
301 std::vector<support::FileName> const & getBibfilesCache() const;
303 void getLabelList(std::vector<docstring> &) const;
306 void changeLanguage(Language const * from, Language const * to);
309 bool isMultiLingual() const;
312 BufferParams & params();
313 BufferParams const & params() const;
315 /** The list of paragraphs.
316 This is a linked list of paragraph, this list holds the
317 whole contents of the document.
319 ParagraphList & paragraphs();
320 ParagraphList const & paragraphs() const;
322 /// LyX version control object.
324 LyXVC const & lyxvc() const;
326 /// Where to put temporary files.
327 std::string const & temppath() const;
329 /// Used when typesetting to place errorboxes.
331 TexRow const & texrow() const;
334 ParIterator par_iterator_begin();
336 ParConstIterator par_iterator_begin() const;
338 ParIterator par_iterator_end();
340 ParConstIterator par_iterator_end() const;
342 /** \returns true only when the file is fully loaded.
343 * Used to prevent the premature generation of previews
344 * and by the citation inset.
346 bool isFullyLoaded() const;
347 /// Set by buffer_funcs' newFile.
348 void setFullyLoaded(bool);
350 /// Our main text (inside the top InsetText)
353 /// Our top InsetText
354 Inset & inset() const;
359 /// Collect macros in paragraphs
361 /// Look for macro defined before par (or in the master buffer)
362 bool hasMacro(docstring const & name, Paragraph const & par) const;
363 /// Look for macro defined anywhere in the buffer (or in the master buffer)
364 bool hasMacro(docstring const & name) const;
365 /// Return macro defined before par (or in the master buffer)
366 MacroData const & getMacro(docstring const & name, Paragraph const & par) const;
367 /// Return macro defined anywhere in the buffer (or in the master buffer)
368 MacroData const & getMacro(docstring const & name) const;
370 /// Replace the inset contents for insets which InsetCode is equal
371 /// to the passed \p inset_code.
372 void changeRefsIfUnique(docstring const & from, docstring const & to,
375 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
376 /// including preamble
377 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
380 /// Access to error list
381 ErrorList const & errorList(std::string const & type) const;
382 ErrorList & errorList(std::string const & type);
385 TocBackend & tocBackend();
386 TocBackend const & tocBackend() const;
390 EmbeddedFiles & embeddedFiles();
391 EmbeddedFiles const & embeddedFiles() const;
396 /// This function is called when the buffer is changed.
397 void changed() const;
398 /// This function is called when the buffer structure is changed.
399 void structureChanged() 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 setBusy(bool on) const;
404 /// This function is called when the buffer readonly status change.
405 void setReadOnly(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());
422 void loadChildDocuments() const;
423 /// return the format of the buffer on a string
424 std::string bufferFormat() const;
427 bool doExport(std::string const & format, bool put_in_tempdir,
428 std::string & result_file);
430 bool doExport(std::string const & format, bool put_in_tempdir);
432 bool preview(std::string const & format);
434 bool isExportable(std::string const & format) const;
436 std::vector<Format const *> exportableFormats(bool only_viewable) const;
440 bool readFileHelper(support::FileName const & s);
442 std::vector<std::string> backends() const;
443 /** Inserts a file into a document
444 \return \c false if method fails.
446 ReadStatus readFile(Lexer &, support::FileName const & filename,
447 bool fromString = false);
449 /// Use the Pimpl idiom to hide the internals.
451 /// The pointer never changes although *pimpl_'s contents may.
454 /// A cache for the bibfiles (including bibfiles of loaded child
455 /// documents), needed for appropriate update of natbib labels.
456 mutable std::vector<support::FileName> bibfilesCache_;
458 frontend::GuiBufferDelegate * gui_;