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
84 /// Method to check if a file is externally modified, used by
85 /// isExternallyModified()
87 * timestamp is fast but inaccurate. For example, the granularity
88 * of timestamp on a FAT filesystem is 2 second. Also, various operations
89 * may touch the timestamp of a file even when its content is unchanged.
91 * checksum is accurate but slow, which can be a problem when it is
92 * frequently used, or used for a large file on a slow (network) file
95 * FIXME: replace this method with support/FileMonitor.
98 checksum_method, ///< Use file checksum
99 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
104 \param b optional \c false by default
106 explicit Buffer(std::string const & file, bool b = false);
111 /** High-level interface to buffer functionality.
112 This function parses a command string and executes it
114 bool dispatch(std::string const & command, bool * result = 0);
116 /// Maybe we know the function already by number...
117 bool dispatch(FuncRequest const & func, bool * result = 0);
119 /// Load the autosaved file.
120 void loadAutoSaveFile();
122 /// read a new document from a string
123 bool readString(std::string const &);
125 bool readFile(support::FileName const & filename);
127 /// read the header, returns number of unknown tokens
128 int readHeader(Lexer & lex);
130 /** Reads a file without header.
131 \param par if != 0 insert the file.
132 \return \c false if file is not completely read.
134 bool readDocument(Lexer &);
137 void insertStringAsLines(ParagraphList & plist,
138 pit_type &, pos_type &,
139 Font const &, docstring const &, bool);
141 ParIterator getParFromID(int id) const;
142 /// do we have a paragraph with this id?
143 bool hasParWithID(int id) const;
146 frontend::WorkAreaManager & workAreaManager() const;
149 Takes care of auto-save files and backup file if requested.
150 Returns \c true if the save is successful, \c false otherwise.
154 /// Write document to stream. Returns \c false if unsuccesful.
155 bool write(std::ostream &) const;
156 /// Write file. Returns \c false if unsuccesful.
157 bool writeFile(support::FileName const &) const;
159 /// Loads LyX file \c filename into buffer, * and \return success
160 bool loadLyXFile(support::FileName const & s);
162 /// Fill in the ErrorList with the TeXErrors
163 void bufferErrors(TeXErrors const &, ErrorList &) const;
165 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
166 bool makeLaTeXFile(support::FileName const & filename,
167 std::string const & original_path,
168 OutputParams const &,
169 bool output_preamble = true,
170 bool output_body = true) const;
171 /** Export the buffer to LaTeX.
172 If \p os is a file stream, and params().inputenc is "auto" or
173 "default", and the buffer contains text in different languages
174 with more than one encoding, then this method will change the
175 encoding associated to \p os. Therefore you must not call this
176 method with a string stream if the output is supposed to go to a
179 ofs.open("test.tex");
180 writeLaTeXSource(ofs, ...);
182 \endcode is NOT equivalent to \code
183 odocstringstream oss;
184 writeLaTeXSource(oss, ...);
186 ofs.open("test.tex");
191 void writeLaTeXSource(odocstream & os,
192 std::string const & original_path,
193 OutputParams const &,
194 bool output_preamble = true,
195 bool output_body = true) const;
197 void makeDocBookFile(support::FileName const & filename,
198 OutputParams const & runparams_in,
199 bool only_body = false) const;
201 void writeDocBookSource(odocstream & os, std::string const & filename,
202 OutputParams const & runparams_in,
203 bool only_body = false) const;
204 /// returns the main language for the buffer (document)
205 Language const * language() const;
206 /// get l10n translated to the buffers language
207 docstring const B_(std::string const & l10n) const;
211 /// return true if the main lyx file does not need saving
212 bool isClean() const;
214 bool isBakClean() const;
216 bool isDepClean(std::string const & name) const;
218 /// whether or not disk file has been externally modified
219 bool isExternallyModified(CheckMethod method) const;
221 /// save timestamp and checksum of the given file.
222 void saveCheckSum(support::FileName const & file) const;
224 /// mark the main lyx file as not needing saving
225 void markClean() const;
228 void markBakClean() const;
231 void markDepClean(std::string const & name);
234 void setUnnamed(bool flag = true);
237 bool isUnnamed() const;
239 /// Mark this buffer as dirty.
242 /// Returns the buffer's filename. It is always an absolute path.
243 support::FileName fileName() const;
245 /// Returns the buffer's filename. It is always an absolute path.
246 std::string absFileName() const;
248 /// Returns the the path where the buffer lives.
249 /// It is always an absolute path.
250 std::string filePath() const;
252 /** A transformed version of the file name, adequate for LaTeX.
253 \param no_path optional if \c true then the path is stripped.
255 std::string latexName(bool no_path = true) const;
257 /// Get thee name and type of the log.
258 std::string logName(LogType * type = 0) const;
260 /// Change name of buffer. Updates "read-only" flag.
261 void setFileName(std::string const & newfile);
263 /// Set document's parent Buffer.
264 void setParent(Buffer const *);
265 Buffer const * parent();
267 /** Get the document's master (or \c this if this is not a
270 Buffer const * masterBuffer() const;
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() const;
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.
330 TexRow const & texrow() const;
333 ParIterator par_iterator_begin();
335 ParConstIterator par_iterator_begin() const;
337 ParIterator par_iterator_end();
339 ParConstIterator par_iterator_end() const;
341 /** \returns true only when the file is fully loaded.
342 * Used to prevent the premature generation of previews
343 * and by the citation inset.
345 bool isFullyLoaded() const;
346 /// Set by buffer_funcs' newFile.
347 void setFullyLoaded(bool);
349 /// Our main text (inside the top InsetText)
352 /// Our top InsetText
353 Inset & inset() const;
358 /// Collect macros in paragraphs
360 /// Look for macro defined before par (or in the master buffer)
361 bool hasMacro(docstring const & name, Paragraph const & par) const;
362 /// Look for macro defined anywhere in the buffer (or in the master buffer)
363 bool hasMacro(docstring const & name) const;
364 /// Return macro defined before par (or in the master buffer)
365 MacroData const & getMacro(docstring const & name, Paragraph const & par) const;
366 /// Return macro defined anywhere in the buffer (or in the master buffer)
367 MacroData const & getMacro(docstring const & name) const;
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,
379 /// Access to error list.
380 /// This method is used only for GUI visualisation of Buffer related
381 /// errors (like parsing or LateX compilation). This method is const
382 /// because modifying the returned ErrorList does not touch the document
384 ErrorList & errorList(std::string const & type) const;
387 /// This is useful only for screen visualisation of the Buffer. This
388 /// method is const because modifying this backend does not touch
389 /// the document contents.
390 TocBackend & tocBackend() const;
393 EmbeddedFiles & embeddedFiles();
394 EmbeddedFiles const & embeddedFiles() const;
399 /// This function is called when the buffer is changed.
400 void changed() const;
401 /// This function is called when the buffer structure is changed.
402 void structureChanged() const;
403 /// This function is called when some parsing error shows up.
404 void errors(std::string const & err) const;
405 /// This function is called when the buffer busy status change.
406 void setBusy(bool on) const;
407 /// This function is called when the buffer readonly status change.
408 void setReadOnly(bool on) const;
409 /// Update window titles of all users.
410 void updateTitles() const;
411 /// Reset autosave timers for all users.
412 void resetAutosaveTimers() const;
414 void message(docstring const & msg) const;
416 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
419 void autoSave() const;
421 bool writeAs(std::string const & newname = std::string());
425 void loadChildDocuments() const;
427 void resetChildDocuments(bool close_them) const;
429 /// return the format of the buffer on a string
430 std::string bufferFormat() const;
433 bool doExport(std::string const & format, bool put_in_tempdir,
434 std::string & result_file) const;
436 bool doExport(std::string const & format, bool put_in_tempdir) const;
438 bool preview(std::string const & format) const;
440 bool isExportable(std::string const & format) const;
442 std::vector<Format const *> exportableFormats(bool only_viewable) const;
446 bool readFileHelper(support::FileName const & s);
448 std::vector<std::string> backends() const;
449 /** Inserts a file into a document
450 \return \c false if method fails.
452 ReadStatus readFile(Lexer &, support::FileName const & filename,
453 bool fromString = false);
455 /// Use the Pimpl idiom to hide the internals.
457 /// The pointer never changes although *pimpl_'s contents may.
460 /// A cache for the bibfiles (including bibfiles of loaded child
461 /// documents), needed for appropriate update of natbib labels.
462 mutable std::vector<support::FileName> bibfilesCache_;
464 frontend::GuiBufferDelegate * gui_;