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 "support/unique_ptr.h"
18 #include "support/strfwd.h"
19 #include "support/types.h"
20 #include "support/FileNameList.h"
56 class ParConstIterator;
66 class GuiBufferDelegate;
67 class WorkAreaManager;
68 } // namespace frontend
73 } // namespace support
78 } // namespace graphics
82 typedef std::list<Buffer *> ListOfBuffers;
83 /// a list of Buffers we cloned
84 typedef std::set<Buffer *> CloneList;
87 /** The buffer object.
88 * This is the buffer object. It contains all the information about
89 * a document loaded into LyX.
90 * The buffer object owns the Text (wrapped in an InsetText), which
91 * contains the individual paragraphs of the document.
94 * I am not sure if the class is complete or
95 * minimal, probably not.
96 * \author Lars Gullik Bjønnes
99 class MarkAsExporting;
103 /// What type of log will \c getLogName() return?
105 latexlog, ///< LaTeX log
106 buildlog ///< Literate build log
109 /// Result of \c readFile()
119 ReadEmergencyFailure,
137 ExportNoPathToFormat,
138 ExportTexPathHasSpaces,
139 ExportConverterError,
141 // Implies ExportSuccess.
143 // The exported file exists but there was an error when opening
155 explicit Buffer(std::string const & file, bool readonly = false,
156 Buffer const * cloned_buffer = 0);
161 /// Clones the entire structure of which this Buffer is part,
162 /// cloning all the children, too.
163 Buffer * cloneWithChildren() const;
164 /// Just clones this single Buffer. For autosave.
165 Buffer * cloneBufferOnly() const;
167 bool isClone() const;
169 /** High-level interface to buffer functionality.
170 This function parses a command string and executes it.
172 void dispatch(std::string const & command, DispatchResult & result);
174 /// Maybe we know the function already by number...
175 void dispatch(FuncRequest const & func, DispatchResult & result);
177 /// Can this function be exectued?
178 /// \return true if we made a decision
179 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
182 DocIterator getParFromID(int id) const;
183 /// do we have a paragraph with this id?
184 bool hasParWithID(int id) const;
187 frontend::WorkAreaManager & workAreaManager() const;
190 Takes care of auto-save files and backup file if requested.
191 Returns \c true if the save is successful, \c false otherwise.
194 /// Renames and saves the buffer
195 bool saveAs(support::FileName const & fn);
197 /// Write document to stream. Returns \c false if unsuccessful.
198 bool write(std::ostream &) const;
199 /// Write file. Returns \c false if unsuccessful.
200 bool writeFile(support::FileName const &) const;
202 /// \name Functions involved in reading files/strings.
204 /// Loads the LyX file into the buffer. This function
205 /// tries to extract the file from version control if it
206 /// cannot be found. If it can be found, it will try to
207 /// read an emergency save file or an autosave file.
208 /// \sa loadThisLyXFile
209 ReadStatus loadLyXFile();
210 /// Loads the LyX file \c fn into the buffer. If you want
211 /// to check for files in a version control container,
212 /// emergency or autosave files, one should use \c loadLyXFile.
214 ReadStatus loadThisLyXFile(support::FileName const & fn);
215 /// import a new document from a string
216 bool importString(std::string const &, docstring const &, ErrorList &);
217 /// import a new file
218 bool importFile(std::string const &, support::FileName const &, ErrorList &);
219 /// read a new document from a string
220 bool readString(std::string const &);
221 /// Reloads the LyX file
223 //FIXME: The following function should be private
225 /// read the header, returns number of unknown tokens
226 int readHeader(Lexer & lex);
228 double fontScalingFactor() const;
232 typedef std::map<Buffer const *, Buffer *> BufferMap;
234 void cloneWithChildren(BufferMap &, CloneList *) const;
235 /// save checksum of the given file.
236 void saveCheckSum() const;
238 ReadStatus readFile(support::FileName const & fn);
239 /// Reads a file without header.
240 /// \param par if != 0 insert the file.
241 /// \return \c true if file is not completely read.
242 bool readDocument(Lexer &);
243 /// Try to extract the file from a version control container
244 /// before reading if the file cannot be found.
245 /// \sa LyXVC::file_not_found_hook
246 ReadStatus extractFromVC();
247 /// Reads the first tag of a LyX File and
248 /// returns the file format number.
249 ReadStatus parseLyXFormat(Lexer & lex, support::FileName const & fn,
250 int & file_format) const;
251 /// Convert the LyX file to the LYX_FORMAT using
252 /// the lyx2lyx script and returns the filename
253 /// of the temporary file to be read
254 ReadStatus convertLyXFormat(support::FileName const & fn,
255 support::FileName & tmpfile, int from_format);
256 /// get appropriate name for backing up files from older versions
257 support::FileName getBackupName() const;
261 /// \name Functions involved in autosave and emergency files.
263 /// Save an autosave file to #filename.lyx#
264 bool autoSave() const;
265 /// save emergency file
266 /// \return a status message towards the user.
267 docstring emergencyWrite();
269 //FIXME:The following function should be private
272 void removeAutosaveFile() const;
275 /// Try to load an autosave file associated to \c fn.
276 ReadStatus loadAutosave();
277 /// Try to load an emergency file associated to \c fn.
278 ReadStatus loadEmergency();
279 /// Get the filename of the emergency file associated with the Buffer
280 support::FileName getEmergencyFileName() const;
281 /// Get the filename of the autosave file associated with the Buffer
282 support::FileName getAutosaveFileName() const;
284 void moveAutosaveFile(support::FileName const & old) const;
288 /// Fill in the ErrorList with the TeXErrors
289 void bufferErrors(TeXErrors const &, ErrorList &) const;
290 /// Fill in the Citation/Reference ErrorList from the TeXErrors
291 void bufferRefs(TeXErrors const &, ErrorList &) const;
301 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
302 ExportStatus makeLaTeXFile(support::FileName const & filename,
303 std::string const & original_path,
304 OutputParams const &,
305 OutputWhat output = FullSource) const;
306 /** Export the buffer to LaTeX.
307 If \p os is a file stream, and params().inputenc is "auto-legacy" or
308 "auto-legacy-plain", and the buffer contains text in different languages
309 with more than one encoding, then this method will change the
310 encoding associated to \p os. Therefore you must not call this
311 method with a string stream if the output is supposed to go to a
315 ofs.open("test.tex");
316 writeLaTeXSource(os, ...);
318 \endcode is NOT equivalent to \code
319 odocstringstream oss;
321 writeLaTeXSource(os, ...);
323 ofs.open("test.tex");
328 ExportStatus writeLaTeXSource(otexstream & os,
329 std::string const & original_path,
330 OutputParams const &,
331 OutputWhat output = FullSource) const;
333 ExportStatus makeDocBookFile(support::FileName const & filename,
334 OutputParams const & runparams_in,
335 OutputWhat output = FullSource) const;
337 ExportStatus writeDocBookSource(odocstream & os, std::string const & filename,
338 OutputParams const & runparams_in,
339 OutputWhat output = FullSource) const;
341 ExportStatus makeLyXHTMLFile(support::FileName const & filename,
342 OutputParams const & runparams_in) const;
344 ExportStatus writeLyXHTMLSource(odocstream & os,
345 OutputParams const & runparams_in,
346 OutputWhat output = FullSource) const;
347 /// returns the main language for the buffer (document)
348 Language const * language() const;
349 /// get l10n translated to the buffers language
350 docstring const B_(std::string const & l10n) const;
354 /// return true if the main lyx file does not need saving
355 bool isClean() const;
357 bool isDepClean(std::string const & name) const;
359 /// Whether or not disk file has been externally modified. Uses a checksum
360 /// which is accurate but slow, which can be a problem when it is frequently
361 /// used, or used for a large file on a slow (network) file system.
362 bool isChecksumModified() const;
364 /// Flag set by the FileSystemWatcher.
365 /// Fast but (not so) inaccurate, can be cleared by the user.
366 bool notifiesExternalModification() const;
367 void clearExternalModification() const;
369 /// mark the main lyx file as not needing saving
370 void markClean() const;
373 void markDepClean(std::string const & name);
376 void setUnnamed(bool flag = true);
378 /// Whether or not a filename has been assigned to this buffer
379 bool isUnnamed() const;
381 /// Whether or not this buffer is internal.
383 /// An internal buffer does not contain a real document, but some auxiliary text segment.
384 /// It is not associated with a filename, it is never saved, thus it does not need to be
385 /// automatically saved, nor it needs to trigger any "do you want to save ?" question.
386 bool isInternal() const;
388 void setInternal(bool flag);
390 /// Mark this buffer as dirty.
393 /// Returns the buffer's filename. It is always an absolute path.
394 support::FileName fileName() const;
396 /// Returns the buffer's filename. It is always an absolute path.
397 std::string absFileName() const;
399 /// Returns the path where the buffer lives.
400 /// It is always an absolute path.
401 std::string filePath() const;
403 /** Contructs a file name of a referenced file (child doc, included graphics etc).
404 * Absolute names are returned as is. If the name is relative, it is
405 * interpreted relative to filePath() if the file exists, otherwise
406 * relative to the original path where the document was last saved.
407 * The original path may be different from filePath() if the document was
408 * later manually moved to a different location.
410 support::DocFileName getReferencedFileName(std::string const & fn) const;
412 /// Format a file name for LaTeX output (absolute or relative or filename only,
413 /// depending on file and context)
414 std::string const prepareFileNameForLaTeX(std::string const &,
415 std::string const &, bool nice) const;
417 /** Returns a vector of bibliography (*.bib) file paths suitable for the
418 * output in the respective BibTeX/Biblatex macro and potential individual encoding
420 std::vector<std::pair<docstring, std::string>> const prepareBibFilePaths(OutputParams const &,
421 const docstring_list & bibfilelist,
422 bool const extension = true) const;
424 /** Returns the path where a local layout file lives.
425 * An empty string is returned for standard system and user layouts.
426 * If possible, it is always relative to the buffer path.
428 std::string layoutPos() const;
430 /** Set the path to a local layout file.
431 * This must be an absolute path but, if possible, it is always
432 * stored as relative to the buffer path.
434 void setLayoutPos(std::string const & path);
436 /** A transformed version of the file name, adequate for LaTeX.
437 \param no_path optional if \c true then the path is stripped.
439 std::string latexName(bool no_path = true) const;
441 /// Get the name and type of the log.
442 std::string logName(LogType * type = 0) const;
444 /// Set document's parent Buffer.
445 void setParent(Buffer const *);
446 Buffer const * parent() const;
448 /** Get the document's master (or \c this if this is not a
451 Buffer const * masterBuffer() const;
453 /// \return true if \p child is a child of this \c Buffer.
454 bool isChild(Buffer * child) const;
456 /// \return true if this \c Buffer has children
457 bool hasChildren() const;
459 /// \return a list of the direct children of this Buffer.
460 /// this list has no duplicates and is in the order in which
461 /// the children appear.
462 ListOfBuffers getChildren() const;
464 /// \return a list of all descendents of this Buffer (children,
465 /// grandchildren, etc). this list has no duplicates and is in
466 /// the order in which the children appear.
467 ListOfBuffers getDescendents() const;
469 /// Collect all relative buffers, in the order in which they appear.
470 /// I.e., the "root" Buffer is first, then its first child, then any
471 /// of its children, etc. However, there are no duplicates in this
473 /// This is "stable", too, in the sense that it returns the same
474 /// thing from whichever Buffer it is called.
475 ListOfBuffers allRelatives() const;
477 /// Is buffer read-only? True if it has either the read-only flag or the
478 /// externally modified flag.
479 bool isReadonly() const;
481 /// Does the buffer have the read-only flag?
482 bool hasReadonlyFlag() const;
484 /// Set buffer read-only flag
485 void setReadonly(bool flag = true);
487 /** Validate a buffer for LaTeX.
488 This validates the buffer, and returns a struct for use by
489 #makeLaTeX# and others. Its main use is to figure out what
490 commands and packages need to be included in the LaTeX file.
491 It (should) also check that the needed constructs are there
492 (i.e. that the \refs points to coresponding \labels). It
493 should perhaps inset "error" insets to help the user correct
496 void validate(LaTeXFeatures &) const;
498 /// Bibliography information is cached in the Buffer, so we do not
499 /// have to check or read things over and over.
500 /// The cache exists only in the master buffer. When it is updated,
501 /// the children add their information to the master's cache.
502 /// Calling this method invalidates the cache and so requires a
504 void invalidateBibinfoCache() const;
505 /// Clear the bibfiles cache
506 void clearBibFileCache() const;
507 /// Updates the cached bibliography information, checking first to see
508 /// whether the cache is valid. If so, we do nothing. If not, then we
509 /// reload all the BibTeX info.
510 /// Note that this operates on the master document.
511 void reloadBibInfoCache(bool const force = false) const;
512 /// \return the bibliography information for this buffer's master,
513 /// or just for it, if it isn't a child.
514 BiblioInfo const & masterBibInfo() const;
515 /// \return this buffer's bibliography information
516 BiblioInfo const & bibInfo() const;
517 /// collect bibliography info from the various insets in this buffer.
518 void collectBibKeys(support::FileNameList &) const;
519 /// add some BiblioInfo to our cache
520 void addBiblioInfo(BiblioInfo const & bi) const;
521 /// add a single piece of bibliography info to our cache
522 void addBibTeXInfo(docstring const & key, BibTeXInfo const & bi) const;
524 void makeCitationLabels() const;
526 void invalidateCiteLabels() const;
528 bool citeLabelsValid() const;
530 void getLabelList(std::vector<docstring> &) const;
532 /// This removes the .aux and .bbl files from the temp dir.
533 void removeBiblioTempFiles() const;
536 void changeLanguage(Language const * from, Language const * to);
539 bool isMultiLingual() const;
541 std::set<Language const *> getLanguages() const;
544 BufferParams & params();
545 BufferParams const & params() const;
547 BufferParams const & masterParams() const;
549 /** The list of paragraphs.
550 This is a linked list of paragraph, this list holds the
551 whole contents of the document.
553 ParagraphList & paragraphs();
554 ParagraphList const & paragraphs() const;
556 /// LyX version control object.
558 LyXVC const & lyxvc() const;
560 /// Where to put temporary files.
561 std::string const temppath() const;
563 /// Used when typesetting to place errorboxes.
564 TexRow const & texrow() const;
568 ParIterator par_iterator_begin();
570 ParConstIterator par_iterator_begin() const;
572 ParIterator par_iterator_end();
574 ParConstIterator par_iterator_end() const;
576 // Position of the child buffer where it appears first in the master.
577 DocIterator firstChildPosition(Buffer const * child);
579 /** \returns true only when the file is fully loaded.
580 * Used to prevent the premature generation of previews
581 * and by the citation inset.
583 bool isFullyLoaded() const;
584 /// Set by buffer_funcs' newFile.
585 void setFullyLoaded(bool);
587 /// FIXME: Needed by RenderPreview.
588 graphics::PreviewLoader * loader() const;
589 /// Update the LaTeX preview snippets associated with this buffer
590 void updatePreviews() const;
591 /// Remove any previewed LaTeX snippets associated with this buffer
592 void removePreviews() const;
594 graphics::Cache & graphicsCache() const;
596 /// Our main text (inside the top InsetText)
599 /// Our top InsetText
600 Inset & inset() const;
605 /// Collect macro definitions in paragraphs
606 void updateMacros() const;
607 /// Iterate through the whole buffer and try to resolve macros
608 void updateMacroInstances(UpdateType) const;
610 /// List macro names of this buffer, the parent and the children
611 void listMacroNames(MacroNameSet & macros) const;
612 /// Collect macros of the parent and its children in front of this buffer.
613 void listParentMacros(MacroSet & macros, LaTeXFeatures & features) const;
615 /// Return macro defined before pos (or in the master buffer)
616 MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
617 /// Return macro defined anywhere in the buffer (or in the master buffer)
618 MacroData const * getMacro(docstring const & name, bool global = true) const;
619 /// Return macro defined before the inclusion of the child
620 MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
622 /// Collect user macro names at loading time
623 typedef std::set<docstring> UserMacroSet;
624 mutable UserMacroSet usermacros;
626 /// Replace the inset contents for insets which InsetCode is equal
627 /// to the passed \p inset_code.
628 void changeRefsIfUnique(docstring const & from, docstring const & to);
630 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
631 /// including preamble
632 /// returns nullptr if Id to Row conversion is unsupported
633 unique_ptr<TexRow> getSourceCode(odocstream & os,
634 std::string const & format, pit_type par_begin,
635 pit_type par_end, OutputWhat output, bool master) const;
637 /// Access to error list.
638 /// This method is used only for GUI visualisation of Buffer related
639 /// errors (like parsing or LateX compilation). This method is const
640 /// because modifying the returned ErrorList does not touch the document
642 ErrorList & errorList(std::string const & type) const;
645 /// This is useful only for screen visualisation of the Buffer. This
646 /// method is const because modifying this backend does not touch
647 /// the document contents.
648 TocBackend & tocBackend() const;
653 /// This function is called when the buffer is changed.
654 void changed(bool update_metrics) const;
656 void setChild(DocIterator const & dit, Buffer * child);
658 void updateTocItem(std::string const &, DocIterator const &) const;
659 /// This function is called when the buffer structure is changed.
660 void structureChanged() const;
661 /// This function is called when some parsing error shows up.
662 void errors(std::string const & err, bool from_master = false) const;
663 /// This function is called when the buffer busy status change.
664 void setBusy(bool on) const;
665 /// Update window titles of all users.
666 void updateTitles() const;
667 /// Reset autosave timers for all users.
668 void resetAutosaveTimers() const;
670 void message(docstring const & msg) const;
673 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
675 bool hasGuiDelegate() const;
678 ExportStatus doExport(std::string const & target, bool put_in_tempdir) const;
679 /// Export buffer to format \p format and open the result in a suitable viewer.
680 /// Note: This has nothing to do with preview of graphics or math formulas.
681 ExportStatus preview(std::string const & format) const;
682 /// true if there was a previous preview this session of this buffer and
683 /// there was an error on the previous preview of this buffer.
684 bool lastPreviewError() const;
688 ExportStatus doExport(std::string const & target, bool put_in_tempdir,
689 std::string & result_file) const;
690 /// target is a format name optionally followed by a space
691 /// and a destination file-name
692 ExportStatus doExport(std::string const & target, bool put_in_tempdir,
693 bool includeall, std::string & result_file) const;
695 ExportStatus preview(std::string const & format, bool includeall) const;
697 void setMathFlavor(OutputParams & op) const;
701 bool isExporting() const;
703 /// A collection of InsetRef insets and their position in the buffer
704 typedef std::vector<std::pair<Inset *, ParIterator> > References;
705 /// Get all InsetRef insets and their positions associated with
706 /// the InsetLabel with the label string \p label
707 References const & references(docstring const & label) const;
708 /// Add an InsetRef at position \p it to the Insetlabel
709 /// with the label string \p label
710 void addReference(docstring const & label, Inset * inset, ParIterator it);
711 /// Clear the whole reference cache
712 void clearReferenceCache() const;
713 /// Set the InsetLabel for a given \p label string. \p active
714 /// determines whether this is an active label (see @ref activeLabel)
715 void setInsetLabel(docstring const & label, InsetLabel const * il,
717 /// \return the InsetLabel associated with this \p label string
718 /// If \p active is true we only return active labels
719 /// (see @ref activeLabel)
720 InsetLabel const * insetLabel(docstring const & label,
721 bool const active = false) const;
722 /// \return true if this \param label is an active label.
723 /// Inactive labels are currently deleted labels (in ct mode)
724 bool activeLabel(docstring const & label) const;
726 /// return a list of all used branches (also in children)
727 void getUsedBranches(std::list<docstring> &, bool const from_master = false) const;
729 /// Updates screen labels and some other information associated with
730 /// insets and paragraphs. Actually, it's more like a general "recurse
731 /// through the Buffer" routine, that visits all the insets and paragraphs.
732 void updateBuffer() const { updateBuffer(UpdateMaster, InternalUpdate); }
733 /// \param scope: whether to start with the master document or just
735 /// \param output: whether we are preparing for output.
736 void updateBuffer(UpdateScope scope, UpdateType utype) const;
738 void updateBuffer(ParIterator & parit, UpdateType utype) const;
740 /// Spellcheck starting from \p from.
741 /// \p from initial position, will then points to the next misspelled
743 /// \p to will points to the end of the next misspelled word.
744 /// \p word_lang will contain the found misspelled word.
745 /// \return progress if a new word was found.
746 int spellCheck(DocIterator & from, DocIterator & to,
747 WordLangTuple & word_lang, docstring_list & suggestions) const;
749 void checkChildBuffers();
751 void checkMasterBuffer();
753 /// If the document is being saved to a new location and the named file
754 /// exists at the old location, return its updated path relative to the
755 /// new buffer path if possible, otherwise return its absolute path.
756 /// In all other cases, this is a no-op and name is returned unchanged.
757 /// If a non-empty ext is given, the existence of name.ext is checked
758 /// but the returned path will not contain this extension.
759 /// Similarly, when loading a document that was moved from the location
760 /// where it was saved, return the correct path relative to the new
762 std::string includedFilePath(std::string const & name,
763 std::string const & ext = empty_string()) const;
765 /// compute statistics between \p from and \p to
766 /// \p from initial position
767 /// \p to points to the end position
768 /// \p skipNoOutput if notes etc. should be ignored
769 void updateStatistics(DocIterator & from, DocIterator & to,
770 bool skipNoOutput = true) const;
771 /// statistics accessor functions
772 int wordCount() const;
773 int charCount(bool with_blanks) const;
775 // this is const because it does not modify the buffer's real contents,
776 // only the mutable flag.
777 void setChangesPresent(bool) const;
778 bool areChangesPresent() const;
779 void updateChangesPresent() const;
781 void registerBibfiles(docstring_list const & bf) const;
783 support::FileName getBibfilePath(docstring const & bibid) const;
786 friend class MarkAsExporting;
787 /// mark the buffer as busy exporting something, or not
788 void setExportStatus(bool e) const;
791 References & getReferenceCache(docstring const & label);
792 /// Change name of buffer. Updates "read-only" flag.
793 void setFileName(support::FileName const & fname);
795 void getLanguages(std::set<Language const *> &) const;
796 /// Checks whether any of the referenced bibfiles have changed since the
797 /// last time we loaded the cache. Note that this does NOT update the
798 /// cached information.
799 void checkIfBibInfoCacheIsValid() const;
800 /// Return the list with all bibfiles in use (including bibfiles
801 /// of loaded child documents).
802 docstring_list const &
803 getBibfiles(UpdateScope scope = UpdateMaster) const;
805 void collectChildren(ListOfBuffers & children, bool grand_children) const;
808 Buffer(Buffer const &);
809 void operator=(Buffer const &);
811 /// Use the Pimpl idiom to hide the internals.
813 /// The pointer never changes although *pimpl_'s contents may.
818 /// Helper class, to guarantee that the export status
819 /// gets reset properly. To use, simply create a local variable:
820 /// MarkAsExporting mex(bufptr);
821 /// and leave the rest to us.
822 class MarkAsExporting {
824 MarkAsExporting(Buffer const * buf) : buf_(buf)
826 buf_->setExportStatus(true);
830 buf_->setExportStatus(false);
833 Buffer const * const buf_;