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 "OutputEnums.h"
17 #include "insets/InsetCode.h"
19 #include "support/strfwd.h"
20 #include "support/types.h"
54 class ParConstIterator;
64 class GuiBufferDelegate;
65 class WorkAreaManager;
73 /** The buffer object.
74 * This is the buffer object. It contains all the informations about
75 * a document loaded into LyX.
76 * The buffer object owns the Text (wrapped in an InsetText), which
77 * contains the individual paragraphs of the document.
80 * I am not sure if the class is complete or
81 * minimal, probably not.
82 * \author Lars Gullik Bjønnes
86 /// What type of log will \c getLogName() return?
88 latexlog, ///< LaTeX log
89 buildlog ///< Literate build log
92 /// Result of \c readFile()
94 failure, ///< The file could not be read
95 success, ///< The file could not be read
96 wrongversion ///< The version of the file does not match ours
100 /// Method to check if a file is externally modified, used by
101 /// isExternallyModified()
103 * timestamp is fast but inaccurate. For example, the granularity
104 * of timestamp on a FAT filesystem is 2 second. Also, various operations
105 * may touch the timestamp of a file even when its content is unchanged.
107 * checksum is accurate but slow, which can be a problem when it is
108 * frequently used, or used for a large file on a slow (network) file
111 * FIXME: replace this method with support/FileMonitor.
114 checksum_method, ///< Use file checksum
115 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
125 explicit Buffer(std::string const & file, bool readonly = false,
126 Buffer const * cloned_buffer = 0);
132 Buffer * clone() const;
134 bool isClone() const;
136 /** High-level interface to buffer functionality.
137 This function parses a command string and executes it.
139 void dispatch(std::string const & command, DispatchResult & result);
141 /// Maybe we know the function already by number...
142 void dispatch(FuncRequest const & func, DispatchResult & result);
144 /// Can this function be exectued?
145 /// \return true if we made a decision
146 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
148 /// read a new document from a string
149 bool readString(std::string const &);
151 bool readFile(support::FileName const & filename);
153 /// read the header, returns number of unknown tokens
154 int readHeader(Lexer & lex);
156 /** Reads a file without header.
157 \param par if != 0 insert the file.
158 \return \c true if file is not completely read.
160 bool readDocument(Lexer &);
163 DocIterator getParFromID(int id) const;
164 /// do we have a paragraph with this id?
165 bool hasParWithID(int id) const;
168 frontend::WorkAreaManager & workAreaManager() const;
171 Takes care of auto-save files and backup file if requested.
172 Returns \c true if the save is successful, \c false otherwise.
176 /// Write document to stream. Returns \c false if unsuccesful.
177 bool write(std::ostream &) const;
178 /// save emergency file
179 /// \return a status message towards the user.
180 docstring emergencyWrite();
181 /// Write file. Returns \c false if unsuccesful.
182 bool writeFile(support::FileName const &) const;
184 /// Loads LyX file \c filename into buffer, * and return success
185 bool loadLyXFile(support::FileName const & s);
186 /// Reloads the LyX file
189 /// Fill in the ErrorList with the TeXErrors
190 void bufferErrors(TeXErrors const &, ErrorList &) const;
192 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
193 bool makeLaTeXFile(support::FileName const & filename,
194 std::string const & original_path,
195 OutputParams const &,
196 bool output_preamble = true,
197 bool output_body = true) const;
198 /** Export the buffer to LaTeX.
199 If \p os is a file stream, and params().inputenc is "auto" or
200 "default", and the buffer contains text in different languages
201 with more than one encoding, then this method will change the
202 encoding associated to \p os. Therefore you must not call this
203 method with a string stream if the output is supposed to go to a
206 ofs.open("test.tex");
207 writeLaTeXSource(ofs, ...);
209 \endcode is NOT equivalent to \code
210 odocstringstream oss;
211 writeLaTeXSource(oss, ...);
213 ofs.open("test.tex");
218 void writeLaTeXSource(odocstream & os,
219 std::string const & original_path,
220 OutputParams const &,
221 bool output_preamble = true,
222 bool output_body = true) const;
224 void makeDocBookFile(support::FileName const & filename,
225 OutputParams const & runparams_in,
226 bool only_body = false) const;
228 void writeDocBookSource(odocstream & os, std::string const & filename,
229 OutputParams const & runparams_in,
230 bool only_body = false) const;
232 void makeLyXHTMLFile(support::FileName const & filename,
233 OutputParams const & runparams_in,
234 bool only_body = false) const;
236 void writeLyXHTMLSource(odocstream & os,
237 OutputParams const & runparams_in,
238 bool only_body = false) const;
239 /// returns the main language for the buffer (document)
240 Language const * language() const;
241 /// get l10n translated to the buffers language
242 docstring const B_(std::string const & l10n) const;
246 /// return true if the main lyx file does not need saving
247 bool isClean() const;
249 bool isDepClean(std::string const & name) const;
251 /// whether or not disk file has been externally modified
252 bool isExternallyModified(CheckMethod method) const;
254 /// save timestamp and checksum of the given file.
255 void saveCheckSum(support::FileName const & file) const;
257 /// mark the main lyx file as not needing saving
258 void markClean() const;
261 void markDepClean(std::string const & name);
264 void setUnnamed(bool flag = true);
266 /// Whether or not a filename has been assigned to this buffer
267 bool isUnnamed() const;
269 /// Whether or not this buffer is internal.
271 /// An internal buffer does not contain a real document, but some auxiliary text segment.
272 /// It is not associated with a filename, it is never saved, thus it does not need to be
273 /// automatically saved, nor it needs to trigger any "do you want to save ?" question.
274 bool isInternal() const;
276 /// Mark this buffer as dirty.
279 /// Returns the buffer's filename. It is always an absolute path.
280 support::FileName fileName() const;
282 /// Returns the buffer's filename. It is always an absolute path.
283 std::string absFileName() const;
285 /// Returns the the path where the buffer lives.
286 /// It is always an absolute path.
287 std::string filePath() const;
289 /** A transformed version of the file name, adequate for LaTeX.
290 \param no_path optional if \c true then the path is stripped.
292 std::string latexName(bool no_path = true) const;
294 /// Get the name and type of the log.
295 std::string logName(LogType * type = 0) const;
297 /// Change name of buffer. Updates "read-only" flag.
298 void setFileName(std::string const & newfile);
300 /// Set document's parent Buffer.
301 void setParent(Buffer const *);
302 Buffer const * parent() const;
304 // Collect all relative buffer
305 std::vector<Buffer const *> allRelatives() const;
307 /** Get the document's master (or \c this if this is not a
310 Buffer const * masterBuffer() const;
312 /// \return true if \p child is a child of this \c Buffer.
313 bool isChild(Buffer * child) const;
315 /// return a vector with all children (and grandchildren)
316 std::vector<Buffer *> getChildren(bool grand_children = true) const;
318 /// Add all children (and grandchildren) to supplied vector
319 void getChildren(std::vector<Buffer *> & children, bool grand_children = true) const;
321 /// Is buffer read-only?
322 bool isReadonly() const;
324 /// Set buffer read-only flag
325 void setReadonly(bool flag = true);
327 /// returns \c true if the buffer contains a LaTeX document
328 bool isLatex() const;
329 /// returns \c true if the buffer contains a DocBook document
330 bool isDocBook() const;
331 /// returns \c true if the buffer contains a Wed document
332 bool isLiterate() const;
334 /** Validate a buffer for LaTeX.
335 This validates the buffer, and returns a struct for use by
336 #makeLaTeX# and others. Its main use is to figure out what
337 commands and packages need to be included in the LaTeX file.
338 It (should) also check that the needed constructs are there
339 (i.e. that the \refs points to coresponding \labels). It
340 should perhaps inset "error" insets to help the user correct
343 void validate(LaTeXFeatures &) const;
345 /// Update the list of all bibfiles in use (including bibfiles
346 /// of loaded child documents).
347 void updateBibfilesCache(UpdateScope scope = UpdateMaster) const;
348 /// Return the list with all bibfiles in use (including bibfiles
349 /// of loaded child documents).
350 support::FileNameList const &
351 getBibfilesCache(UpdateScope scope = UpdateMaster) const;
352 /// Information from BibTeX databases is cached in the Buffer, so
353 /// we do not have to read the file over and over.
354 /// Calling this method invalidates the cache and so requires a
356 void invalidateBibinfoCache();
357 /// Updates the cached bibliography information.
358 /// Note that you MUST call this method to update the cache. It will
359 /// not happen otherwise. (Currently, it is called at the start of
360 /// updateBuffer() and from GuiCitation.)
361 void checkBibInfoCache() const;
362 /// \return the bibliography information for this buffer's master,
363 /// or just for it, if it isn't a child.
364 BiblioInfo const & masterBibInfo() const;
365 /// \return the bibliography information for this buffer ONLY.
366 BiblioInfo const & localBibInfo() const;
368 void getLabelList(std::vector<docstring> &) const;
371 void changeLanguage(Language const * from, Language const * to);
374 bool isMultiLingual() const;
376 std::set<Language const *> getLanguages() const;
379 BufferParams & params();
380 BufferParams const & params() const;
382 /** The list of paragraphs.
383 This is a linked list of paragraph, this list holds the
384 whole contents of the document.
386 ParagraphList & paragraphs();
387 ParagraphList const & paragraphs() const;
389 /// LyX version control object.
391 LyXVC const & lyxvc() const;
393 /// Where to put temporary files.
394 std::string const temppath() const;
396 /// Used when typesetting to place errorboxes.
397 TexRow const & texrow() const;
401 ParIterator par_iterator_begin();
403 ParConstIterator par_iterator_begin() const;
405 ParIterator par_iterator_end();
407 ParConstIterator par_iterator_end() const;
409 // Position of the child buffer where it appears first in the master.
410 DocIterator firstChildPosition(Buffer const * child);
412 /** \returns true only when the file is fully loaded.
413 * Used to prevent the premature generation of previews
414 * and by the citation inset.
416 bool isFullyLoaded() const;
417 /// Set by buffer_funcs' newFile.
418 void setFullyLoaded(bool);
420 /// Our main text (inside the top InsetText)
423 /// Our top InsetText
424 Inset & inset() const;
429 /// Collect macro definitions in paragraphs
430 void updateMacros() const;
431 /// Iterate through the whole buffer and try to resolve macros
432 void updateMacroInstances() const;
434 /// List macro names of this buffer, the parent and the children
435 void listMacroNames(MacroNameSet & macros) const;
436 /// Collect macros of the parent and its children in front of this buffer.
437 void listParentMacros(MacroSet & macros, LaTeXFeatures & features) const;
439 /// Return macro defined before pos (or in the master buffer)
440 MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
441 /// Return macro defined anywhere in the buffer (or in the master buffer)
442 MacroData const * getMacro(docstring const & name, bool global = true) const;
443 /// Return macro defined before the inclusion of the child
444 MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
446 /// Collect user macro names at loading time
447 typedef std::set<docstring> UserMacroSet;
448 UserMacroSet usermacros;
450 /// Replace the inset contents for insets which InsetCode is equal
451 /// to the passed \p inset_code.
452 void changeRefsIfUnique(docstring const & from, docstring const & to,
455 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
456 /// including preamble
457 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
458 bool full_source) const;
460 /// Access to error list.
461 /// This method is used only for GUI visualisation of Buffer related
462 /// errors (like parsing or LateX compilation). This method is const
463 /// because modifying the returned ErrorList does not touch the document
465 ErrorList & errorList(std::string const & type) const;
468 /// This is useful only for screen visualisation of the Buffer. This
469 /// method is const because modifying this backend does not touch
470 /// the document contents.
471 TocBackend & tocBackend() const;
476 /// This function is called when the buffer is changed.
477 void changed(bool update_metrics) const;
479 void setChild(DocIterator const & dit, Buffer * child);
481 void updateTocItem(std::string const &, DocIterator const &) const;
482 /// This function is called when the buffer structure is changed.
483 void structureChanged() const;
484 /// This function is called when some parsing error shows up.
485 void errors(std::string const & err, bool from_master = false) const;
486 /// This function is called when the buffer busy status change.
487 void setBusy(bool on) const;
488 /// Update window titles of all users.
489 void updateTitles() const;
490 /// Reset autosave timers for all users.
491 void resetAutosaveTimers() const;
493 void message(docstring const & msg) const;
496 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
498 bool hasGuiDelegate() const;
501 void autoSave() const;
503 void removeAutosaveFile() const;
505 void moveAutosaveFile(support::FileName const & old) const;
507 support::FileName getAutosaveFileName() const;
509 /// return the format of the buffer on a string
510 std::string bufferFormat() const;
511 /// return the default output format of the current backend
512 std::string getDefaultOutputFormat() const;
515 bool doExport(std::string const & format, bool put_in_tempdir,
516 bool includeall, std::string & result_file) const;
518 bool doExport(std::string const & format, bool put_in_tempdir,
519 bool includeall = false) const;
521 bool preview(std::string const & format, bool includeall = false) const;
523 bool isExportable(std::string const & format) const;
525 std::vector<Format const *> exportableFormats(bool only_viewable) const;
527 bool isExportableFormat(std::string const & format) const;
530 typedef std::vector<std::pair<Inset *, ParIterator> > References;
531 References & references(docstring const & label);
532 References const & references(docstring const & label) const;
533 void clearReferenceCache() const;
534 void setInsetLabel(docstring const & label, InsetLabel const * il);
535 InsetLabel const * insetLabel(docstring const & label) const;
537 /// return a list of all used branches (also in children)
538 void getUsedBranches(std::list<docstring> &, bool const from_master = false) const;
540 /// sets the buffer_ member for every inset in this buffer.
541 // FIXME This really shouldn't be needed, but at the moment it's not
542 // clear how to do it just for the individual pieces we need.
543 void setBuffersForInsets() const;
544 /// Updates screen labels and some other information associated with
545 /// insets and paragraphs. Actually, it's more like a general "recurse
546 /// through the Buffer" routine, that visits all the insets and paragraphs.
547 void updateBuffer() const { updateBuffer(UpdateMaster, InternalUpdate); }
548 /// \param scope: whether to start with the master document or just
550 /// \param output: whether we are preparing for output.
551 void updateBuffer(UpdateScope scope, UpdateType utype) const;
553 void updateBuffer(ParIterator & parit, UpdateType utype) const;
555 /// Spellcheck starting from \p from.
556 /// \p from initial position, will then points to the next misspelled
558 /// \p to will points to the end of the next misspelled word.
559 /// \p word_lang will contain the found misspelled word.
560 /// \return progress if a new word was found.
561 int spellCheck(DocIterator & from, DocIterator & to,
562 WordLangTuple & word_lang, docstring_list & suggestions) const;
564 void checkChildBuffers();
568 bool readFileHelper(support::FileName const & s);
570 std::vector<std::string> backends() const;
571 /** Inserts a file into a document
572 \return \c false if method fails.
574 ReadStatus readFile(Lexer &, support::FileName const & filename,
575 bool fromString = false);
577 void getLanguages(std::set<Language const *> &) const;
579 /// Use the Pimpl idiom to hide the internals.
581 /// The pointer never changes although *pimpl_'s contents may.