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/docstring.h"
19 #include "support/docstream.h"
20 #include "support/types.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 * language() 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(support::FileName 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 absFileName() 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 latexName(bool no_path = true) const;
247 /// Get thee name and type of the log.
248 std::pair<LogType, std::string> logName() 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 * masterBuffer() const;
260 /** Get the document's master (or \c this if this is not a
263 Buffer * masterBuffer();
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;
305 BufferParams & params();
306 BufferParams const & params() const;
308 /** The list of paragraphs.
309 This is a linked list of paragraph, this list holds the
310 whole contents of the document.
312 ParagraphList & paragraphs();
313 ParagraphList const & paragraphs() const;
315 /// LyX version control object.
317 LyXVC const & lyxvc() const;
319 /// Where to put temporary files.
320 std::string const & temppath() const;
322 /// Used when typesetting to place errorboxes.
324 TexRow const & texrow() const;
327 ParIterator par_iterator_begin();
329 ParConstIterator par_iterator_begin() const;
331 ParIterator par_iterator_end();
333 ParConstIterator par_iterator_end() const;
335 /** \returns true only when the file is fully loaded.
336 * Used to prevent the premature generation of previews
337 * and by the citation inset.
339 bool isFullyLoaded() const;
340 /// Set by buffer_funcs' newFile.
341 void setFullyLoaded(bool);
343 /// Our main text (inside the top InsetText)
346 /// Our top InsetText
347 Inset & inset() const;
355 bool hasMacro(docstring const & name) const;
357 MacroData const & getMacro(docstring const & name) const;
359 void insertMacro(docstring const & name, MacroData const & data);
361 /// Replace the inset contents for insets which InsetCode is equal
362 /// to the passed \p inset_code.
363 void changeRefsIfUnique(docstring const & from, docstring const & to,
366 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
367 /// including preamble
368 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
371 /// Access to error list
372 ErrorList const & errorList(std::string const & type) const;
373 ErrorList & errorList(std::string const & type);
376 TocBackend & tocBackend();
377 TocBackend const & tocBackend() const;
381 EmbeddedFiles & embeddedFiles();
382 EmbeddedFiles const & embeddedFiles() const;
387 /// This function is called when the buffer is changed.
388 void changed() const;
389 /// This function is called when the buffer structure is changed.
390 void structureChanged() const;
391 /// This function is called when an embedded file is changed
392 void embeddingChanged() const;
393 /// This function is called when some parsing error shows up.
394 void errors(std::string const & err) const;
395 /// This function is called when the buffer busy status change.
396 void busy(bool on) const;
397 /// This function is called when the buffer readonly status change.
398 void readonly(bool on) const;
399 /// Update window titles of all users.
400 void updateTitles() const;
401 /// Reset autosave timers for all users.
402 void resetAutosaveTimers() const;
404 void message(docstring const & msg) const;
406 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
409 void autoSave() const;
411 bool writeAs(std::string const & newname = std::string());
415 void loadChildDocuments() const;
416 /// return the format of the buffer on a string
417 std::string bufferFormat() const;
420 bool doExport(std::string const & format, bool put_in_tempdir,
421 std::string & result_file);
423 bool doExport(std::string const & format, bool put_in_tempdir);
425 bool preview(std::string const & format);
427 bool isExportable(std::string const & format) const;
429 std::vector<Format const *> exportableFormats(bool only_viewable) const;
434 std::vector<std::string> backends() const;
435 /** Inserts a file into a document
436 \return \c false if method fails.
438 ReadStatus readFile(Lexer &, support::FileName const & filename,
439 bool fromString = false);
441 /// Use the Pimpl idiom to hide the internals.
443 /// The pointer never changes although *pimpl_'s contents may.
446 /// A cache for the bibfiles (including bibfiles of loaded child
447 /// documents), needed for appropriate update of natbib labels.
448 mutable std::vector<support::FileName> bibfilesCache_;
450 frontend::GuiBufferDelegate * gui_;