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/strfwd.h"
18 #include "support/types.h"
28 class EmbeddedFileList;
45 class ParConstIterator;
54 class GuiBufferDelegate;
55 class WorkAreaManager;
62 /** The buffer object.
63 * This is the buffer object. It contains all the informations about
64 * a document loaded into LyX.
65 * The buffer object owns the Text (wrapped in an InsetText), which
66 * contains the individual paragraphs of the document.
69 * I am not sure if the class is complete or
70 * minimal, probably not.
71 * \author Lars Gullik Bjønnes
75 /// What type of log will \c getLogName() return?
77 latexlog, ///< LaTeX log
78 buildlog ///< Literate build log
81 /// Result of \c readFile()
83 failure, ///< The file could not be read
84 success, ///< The file could not be read
85 wrongversion ///< The version of the file does not match ours
89 /// Method to check if a file is externally modified, used by
90 /// isExternallyModified()
92 * timestamp is fast but inaccurate. For example, the granularity
93 * of timestamp on a FAT filesystem is 2 second. Also, various operations
94 * may touch the timestamp of a file even when its content is unchanged.
96 * checksum is accurate but slow, which can be a problem when it is
97 * frequently used, or used for a large file on a slow (network) file
100 * FIXME: replace this method with support/FileMonitor.
103 checksum_method, ///< Use file checksum
104 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
109 \param b optional \c false by default
111 explicit Buffer(std::string const & file, bool b = false);
116 /** High-level interface to buffer functionality.
117 This function parses a command string and executes it
119 bool dispatch(std::string const & command, bool * result = 0);
121 /// Maybe we know the function already by number...
122 bool dispatch(FuncRequest const & func, bool * result = 0);
124 /// Load the autosaved file.
125 void loadAutoSaveFile();
127 /// read a new document from a string
128 bool readString(std::string const &);
130 bool readFile(support::FileName const & filename);
132 /// read the header, returns number of unknown tokens
133 int readHeader(Lexer & lex);
135 /** Reads a file without header.
136 \param par if != 0 insert the file.
137 \return \c false if file is not completely read.
139 bool readDocument(Lexer &);
142 void insertStringAsLines(ParagraphList & plist,
143 pit_type &, pos_type &,
144 Font const &, docstring const &, bool);
146 ParIterator getParFromID(int id) const;
147 /// do we have a paragraph with this id?
148 bool hasParWithID(int id) const;
151 frontend::WorkAreaManager & workAreaManager() const;
154 Takes care of auto-save files and backup file if requested.
155 Returns \c true if the save is successful, \c false otherwise.
159 /// Write document to stream. Returns \c false if unsuccesful.
160 bool write(std::ostream &) const;
161 /// Write file. Returns \c false if unsuccesful.
162 bool writeFile(support::FileName const &) const;
164 /// Loads LyX file \c filename into buffer, * and \return success
165 bool loadLyXFile(support::FileName const & s);
167 /// Fill in the ErrorList with the TeXErrors
168 void bufferErrors(TeXErrors const &, ErrorList &) const;
170 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
171 bool makeLaTeXFile(support::FileName const & filename,
172 std::string const & original_path,
173 OutputParams const &,
174 bool output_preamble = true,
175 bool output_body = true) const;
176 /** Export the buffer to LaTeX.
177 If \p os is a file stream, and params().inputenc is "auto" or
178 "default", and the buffer contains text in different languages
179 with more than one encoding, then this method will change the
180 encoding associated to \p os. Therefore you must not call this
181 method with a string stream if the output is supposed to go to a
184 ofs.open("test.tex");
185 writeLaTeXSource(ofs, ...);
187 \endcode is NOT equivalent to \code
188 odocstringstream oss;
189 writeLaTeXSource(oss, ...);
191 ofs.open("test.tex");
196 void writeLaTeXSource(odocstream & os,
197 std::string const & original_path,
198 OutputParams const &,
199 bool output_preamble = true,
200 bool output_body = true) const;
202 void makeDocBookFile(support::FileName const & filename,
203 OutputParams const & runparams_in,
204 bool only_body = false) const;
206 void writeDocBookSource(odocstream & os, std::string const & filename,
207 OutputParams const & runparams_in,
208 bool only_body = false) const;
209 /// returns the main language for the buffer (document)
210 Language const * language() const;
211 /// get l10n translated to the buffers language
212 docstring const B_(std::string const & l10n) const;
216 /// return true if the main lyx file does not need saving
217 bool isClean() const;
219 bool isBakClean() const;
221 bool isDepClean(std::string const & name) const;
223 /// whether or not disk file has been externally modified
224 bool isExternallyModified(CheckMethod method) const;
226 /// save timestamp and checksum of the given file.
227 void saveCheckSum(support::FileName const & file) const;
229 /// mark the main lyx file as not needing saving
230 void markClean() const;
233 void markBakClean() const;
236 void markDepClean(std::string const & name);
239 void setUnnamed(bool flag = true);
242 bool isUnnamed() const;
244 /// Mark this buffer as dirty.
247 /// Returns the buffer's filename. It is always an absolute path.
248 support::FileName fileName() const;
250 /// Returns the buffer's filename. It is always an absolute path.
251 std::string absFileName() const;
253 /// Returns the the path where the buffer lives.
254 /// It is always an absolute path.
255 std::string filePath() const;
257 /** A transformed version of the file name, adequate for LaTeX.
258 \param no_path optional if \c true then the path is stripped.
260 std::string latexName(bool no_path = true) const;
262 /// Get thee name and type of the log.
263 std::string logName(LogType * type = 0) const;
265 /// Change name of buffer. Updates "read-only" flag.
266 void setFileName(std::string const & newfile);
268 /// Set document's parent Buffer.
269 void setParent(Buffer const *);
270 Buffer const * parent();
272 /** Get the document's master (or \c this if this is not a
275 Buffer const * masterBuffer() const;
277 /// Is buffer read-only?
278 bool isReadonly() const;
280 /// Set buffer read-only flag
281 void setReadonly(bool flag = true);
283 /// returns \c true if the buffer contains a LaTeX document
284 bool isLatex() const;
285 /// returns \c true if the buffer contains a DocBook document
286 bool isDocBook() const;
287 /// returns \c true if the buffer contains a Wed document
288 bool isLiterate() const;
290 /** Validate a buffer for LaTeX.
291 This validates the buffer, and returns a struct for use by
292 #makeLaTeX# and others. Its main use is to figure out what
293 commands and packages need to be included in the LaTeX file.
294 It (should) also check that the needed constructs are there
295 (i.e. that the \refs points to coresponding \labels). It
296 should perhaps inset "error" insets to help the user correct
299 void validate(LaTeXFeatures &) const;
301 /// Update the cache with all bibfiles in use (including bibfiles
302 /// of loaded child documents).
303 void updateBibfilesCache() const;
304 /// Return the cache with all bibfiles in use (including bibfiles
305 /// of loaded child documents).
306 EmbeddedFileList const & getBibfilesCache() const;
308 void getLabelList(std::vector<docstring> &) const;
311 void changeLanguage(Language const * from, Language const * to);
314 bool isMultiLingual() const;
317 BufferParams & params();
318 BufferParams const & params() const;
320 /** The list of paragraphs.
321 This is a linked list of paragraph, this list holds the
322 whole contents of the document.
324 ParagraphList & paragraphs();
325 ParagraphList const & paragraphs() const;
327 /// LyX version control object.
329 LyXVC const & lyxvc() const;
331 /// Where to put temporary files.
332 std::string const temppath() const;
334 /// Used when typesetting to place errorboxes.
335 TexRow const & texrow() const;
338 ParIterator par_iterator_begin();
340 ParConstIterator par_iterator_begin() const;
342 ParIterator par_iterator_end();
344 ParConstIterator par_iterator_end() const;
346 /** \returns true only when the file is fully loaded.
347 * Used to prevent the premature generation of previews
348 * and by the citation inset.
350 bool isFullyLoaded() const;
351 /// Set by buffer_funcs' newFile.
352 void setFullyLoaded(bool);
354 /// Our main text (inside the top InsetText)
357 /// Our top InsetText
358 Inset & inset() const;
363 /// Collect macro definitions in paragraphs
364 void updateMacros() const;
365 /// Iterate through the whole buffer and try to resolve macros
366 void updateMacroInstances() const;
368 typedef std::set<docstring> MacroNameSet;
369 /// List macro names of this buffer. the parent and the children
370 void listMacroNames(MacroNameSet & macros) const;
371 /// Write out all macros somewhere defined in the parent,
372 /// its parents and its children, which are visible at the beginning
374 void writeParentMacros(odocstream & os) const;
376 /// Return macro defined before pos (or in the master buffer)
377 MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
378 /// Return macro defined anywhere in the buffer (or in the master buffer)
379 MacroData const * getMacro(docstring const & name, bool global = true) const;
380 /// Return macro defined before the inclusion of the child
381 MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
383 /// Replace the inset contents for insets which InsetCode is equal
384 /// to the passed \p inset_code.
385 void changeRefsIfUnique(docstring const & from, docstring const & to,
388 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
389 /// including preamble
390 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
393 /// Access to error list.
394 /// This method is used only for GUI visualisation of Buffer related
395 /// errors (like parsing or LateX compilation). This method is const
396 /// because modifying the returned ErrorList does not touch the document
398 ErrorList & errorList(std::string const & type) const;
401 /// This is useful only for screen visualisation of the Buffer. This
402 /// method is const because modifying this backend does not touch
403 /// the document contents.
404 TocBackend & tocBackend() const;
407 EmbeddedFileList & embeddedFiles();
408 EmbeddedFileList const & embeddedFiles() const;
409 bool embedded() const;
414 /// This function is called when the buffer is changed.
415 void changed() const;
416 /// This function is called when the buffer structure is changed.
417 void structureChanged() const;
418 /// This function is called when some parsing error shows up.
419 void errors(std::string const & err) const;
420 /// This function is called when the buffer busy status change.
421 void setBusy(bool on) const;
422 /// This function is called when the buffer readonly status change.
423 void setReadOnly(bool on) const;
424 /// Update window titles of all users.
425 void updateTitles() const;
426 /// Reset autosave timers for all users.
427 void resetAutosaveTimers() const;
429 void message(docstring const & msg) const;
431 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
434 void autoSave() const;
436 void loadChildDocuments() const;
438 void resetChildDocuments(bool close_them) const;
440 /// return the format of the buffer on a string
441 std::string bufferFormat() const;
444 bool doExport(std::string const & format, bool put_in_tempdir,
445 std::string & result_file) const;
447 bool doExport(std::string const & format, bool put_in_tempdir) const;
449 bool preview(std::string const & format) const;
451 bool isExportable(std::string const & format) const;
453 std::vector<Format const *> exportableFormats(bool only_viewable) const;
456 /// search for macro in local (buffer) table or in children
457 MacroData const * getBufferMacro(docstring const & name,
458 DocIterator const & pos) const;
459 /** Update macro table in the whole text inset
460 \param it at the start of the text inset)
462 void updateInsetMacros(DocIterator & it,
463 DocIterator & scope) const;
464 /** Update macro table for paragraphs until \c lastpit
465 \param it in some text inset
466 \param lastpit last processed paragraph
468 void updateEnvironmentMacros(DocIterator & it,
470 DocIterator & scope) const;
471 /** Update macro table for one paragraph block with
472 same layout and depth, until \c lastpit
473 \param it in some text inset
474 \param lastpit last processed paragraph
476 void updateBlockMacros(DocIterator & it,
477 DocIterator & scope) const;
480 bool readFileHelper(support::FileName const & s);
482 std::vector<std::string> backends() const;
483 /** Inserts a file into a document
484 \return \c false if method fails.
486 ReadStatus readFile(Lexer &, support::FileName const & filename,
487 bool fromString = false);
489 /// Use the Pimpl idiom to hide the internals.
491 /// The pointer never changes although *pimpl_'s contents may.
494 frontend::GuiBufferDelegate * gui_;