]> git.lyx.org Git - lyx.git/blob - src/Buffer.h
Revert "Attempt to fix bug 9158 using updateBuffer."
[lyx.git] / src / Buffer.h
1 // -*- C++ -*-
2 /**
3  * \file Buffer.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef BUFFER_H
13 #define BUFFER_H
14
15 #include "OutputEnums.h"
16
17 #include "support/unique_ptr.h"
18 #include "support/strfwd.h"
19 #include "support/types.h"
20 #include "support/FileNameList.h"
21
22 #include <map>
23 #include <list>
24 #include <set>
25 #include <string>
26 #include <vector>
27
28
29 namespace lyx {
30
31 class BiblioInfo;
32 class BibTeXInfo;
33 class BufferParams;
34 class DispatchResult;
35 class DocIterator;
36 class docstring_list;
37 class ErrorList;
38 class FuncRequest;
39 class FuncStatus;
40 class Inset;
41 class InsetLabel;
42 class InsetRef;
43 class Font;
44 class Format;
45 class Lexer;
46 class Text;
47 class LyXVC;
48 class LaTeXFeatures;
49 class Language;
50 class MacroData;
51 class MacroNameSet;
52 class MacroSet;
53 class OutputParams;
54 class otexstream;
55 class Paragraph;
56 class ParConstIterator;
57 class ParIterator;
58 class ParagraphList;
59 class TeXErrors;
60 class TexRow;
61 class TocBackend;
62 class Undo;
63 class WordLangTuple;
64
65 namespace frontend {
66 class GuiBufferDelegate;
67 class WorkAreaManager;
68 } // namespace frontend
69
70 namespace support {
71 class DocFileName;
72 class FileName;
73 class FileNamePairList;
74 } // namespace support
75
76 namespace graphics {
77 class PreviewLoader;
78 class Cache;
79 } // namespace graphics
80
81
82 class Buffer;
83 typedef std::list<Buffer *> ListOfBuffers;
84 /// a list of Buffers we cloned
85 typedef std::set<Buffer *> CloneList;
86
87
88 /** The buffer object.
89  * This is the buffer object. It contains all the information about
90  * a document loaded into LyX.
91  * The buffer object owns the Text (wrapped in an InsetText), which
92  * contains the individual paragraphs of the document.
93  *
94  *
95  * I am not sure if the class is complete or
96  * minimal, probably not.
97  * \author Lars Gullik Bjønnes
98  */
99
100 class MarkAsExporting;
101
102 class Buffer {
103 public:
104         /// What type of log will \c getLogName() return?
105         enum LogType {
106                 latexlog, ///< LaTeX log
107                 buildlog  ///< Literate build log
108         };
109
110         /// Result of \c readFile()
111         enum ReadStatus {
112                 ReadSuccess,
113                 ReadCancel,
114                 // failures
115                 ReadFailure,
116                 ReadWrongVersion,
117                 ReadFileNotFound,
118                 ReadVCError,
119                 ReadAutosaveFailure,
120                 ReadEmergencyFailure,
121                 ReadNoLyXFormat,
122                 ReadDocumentFailure,
123                 // lyx2lyx
124                 LyX2LyXNoTempFile,
125                 LyX2LyXNotFound,
126                 LyX2LyXOlderFormat,
127                 LyX2LyXNewerFormat,
128                 // other
129                 ReadOriginal
130         };
131
132         enum ExportStatus {
133                 // export
134                 ExportSuccess,
135                 ExportCancel,
136                 ExportError,
137                 ExportNoPathToFormat,
138                 ExportTexPathHasSpaces,
139                 ExportConverterError,
140                 // preview
141                 // Implies ExportSuccess.
142                 PreviewSuccess,
143                 // The exported file exists but there was an error when opening
144                 // it in a viewer.
145                 PreviewError
146         };
147
148         ///
149         enum UpdateScope {
150                 UpdateMaster,
151                 UpdateChildOnly
152         };
153
154         /// Constructor
155         explicit Buffer(std::string const & file, bool readonly = false,
156                 Buffer const * cloned_buffer = 0);
157
158         /// Destructor
159         ~Buffer();
160
161         /// Clones the entire structure of which this Buffer is part, starting
162         /// with the master and cloning all the children, too.
163         Buffer * cloneFromMaster() const;
164         /// Just clones this single Buffer. For autosave.
165         Buffer * cloneBufferOnly() const;
166         ///
167         bool isClone() const;
168
169         /** High-level interface to buffer functionality.
170             This function parses a command string and executes it.
171         */
172         void dispatch(std::string const & command, DispatchResult & result);
173
174         /// Maybe we know the function already by number...
175         void dispatch(FuncRequest const & func, DispatchResult & result);
176
177         /// Can this function be exectued?
178         /// \return true if we made a decision
179         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
180
181         ///
182         DocIterator getParFromID(int id) const;
183         /// do we have a paragraph with this id?
184         bool hasParWithID(int id) const;
185
186         ///
187         frontend::WorkAreaManager & workAreaManager() const;
188
189         /** Save file.
190             Takes care of auto-save files and backup file if requested.
191             Returns \c true if the save is successful, \c false otherwise.
192         */
193         bool save() const;
194         /// Renames and saves the buffer
195         bool saveAs(support::FileName const & fn);
196
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;
201
202         /// \name Functions involved in reading files/strings.
203         //@{
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.
213         /// /sa 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
222         ReadStatus reload();
223 //FIXME: The following function should be private
224 //private:
225         /// read the header, returns number of unknown tokens
226         int readHeader(Lexer & lex);
227
228         double fontScalingFactor() const;
229
230 private:
231         ///
232         typedef std::map<Buffer const *, Buffer *> BufferMap;
233         ///
234         void cloneWithChildren(BufferMap &, CloneList *) const;
235         /// save checksum of the given file.
236         void saveCheckSum() const;
237         /// read a new file
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;
258         //@}
259
260 public:
261         /// \name Functions involved in autosave and emergency files.
262         //@{
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();
268
269 //FIXME:The following function should be private
270 //private:
271         ///
272         void removeAutosaveFile() const;
273
274 private:
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;
283         ///
284         void moveAutosaveFile(support::FileName const & old) const;
285         //@}
286
287 public:
288         /// Fill in the ErrorList with the TeXErrors
289         void bufferErrors(TeXErrors const &, ErrorList &) const;
290
291         enum OutputWhat {
292                 FullSource,
293                 OnlyBody,
294                 IncludedFile,
295                 OnlyPreamble,
296                 CurrentParagraph
297         };
298
299         /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
300         bool makeLaTeXFile(support::FileName const & filename,
301                            std::string const & original_path,
302                            OutputParams const &,
303                            OutputWhat output = FullSource) const;
304         /** Export the buffer to LaTeX.
305             If \p os is a file stream, and params().inputenc is "auto" or
306             "default", and the buffer contains text in different languages
307             with more than one encoding, then this method will change the
308             encoding associated to \p os. Therefore you must not call this
309             method with a string stream if the output is supposed to go to a
310             file. \code
311             ofdocstream ofs;
312             otexstream os(ofs);
313             ofs.open("test.tex");
314             writeLaTeXSource(os, ...);
315             ofs.close();
316             \endcode is NOT equivalent to \code
317             odocstringstream oss;
318             otexstream os(oss);
319             writeLaTeXSource(os, ...);
320             ofdocstream ofs;
321             ofs.open("test.tex");
322             ofs << oss.str();
323             ofs.close();
324             \endcode
325          */
326         void writeLaTeXSource(otexstream & os,
327                            std::string const & original_path,
328                            OutputParams const &,
329                            OutputWhat output = FullSource) const;
330         ///
331         void makeDocBookFile(support::FileName const & filename,
332                              OutputParams const & runparams_in,
333                              OutputWhat output = FullSource) const;
334         ///
335         void writeDocBookSource(odocstream & os, std::string const & filename,
336                              OutputParams const & runparams_in,
337                              OutputWhat output = FullSource) const;
338         ///
339         void makeLyXHTMLFile(support::FileName const & filename,
340                              OutputParams const & runparams_in) const;
341         ///
342         void writeLyXHTMLSource(odocstream & os,
343                              OutputParams const & runparams_in,
344                              OutputWhat output = FullSource) const;
345         /// returns the main language for the buffer (document)
346         Language const * language() const;
347         /// get l10n translated to the buffers language
348         docstring const B_(std::string const & l10n) const;
349
350         ///
351         int runChktex();
352         /// return true if the main lyx file does not need saving
353         bool isClean() const;
354         ///
355         bool isDepClean(std::string const & name) const;
356
357         /// Whether or not disk file has been externally modified. Uses a checksum
358         /// which is accurate but slow, which can be a problem when it is frequently
359         /// used, or used for a large file on a slow (network) file system.
360         bool isChecksumModified() const;
361
362         /// Flag set by the FileSystemWatcher.
363         /// Fast but (not so) inaccurate, can be cleared by the user.
364         bool notifiesExternalModification() const;
365         void clearExternalModification() const;
366
367         /// mark the main lyx file as not needing saving
368         void markClean() const;
369
370         ///
371         void markDepClean(std::string const & name);
372
373         ///
374         void setUnnamed(bool flag = true);
375
376         /// Whether or not a filename has been assigned to this buffer
377         bool isUnnamed() const;
378
379         /// Whether or not this buffer is internal.
380         ///
381         /// An internal buffer does not contain a real document, but some auxiliary text segment.
382         /// It is not associated with a filename, it is never saved, thus it does not need to be
383         /// automatically saved, nor it needs to trigger any "do you want to save ?" question.
384         bool isInternal() const;
385
386         void setInternal(bool flag);
387
388         /// Mark this buffer as dirty.
389         void markDirty();
390
391         /// Returns the buffer's filename. It is always an absolute path.
392         support::FileName fileName() const;
393
394         /// Returns the buffer's filename. It is always an absolute path.
395         std::string absFileName() const;
396
397         /// Returns the path where the buffer lives.
398         /// It is always an absolute path.
399         std::string filePath() const;
400
401         /** Contructs a file name of a referenced file (child doc, included graphics etc).
402          *  Absolute names are returned as is. If the name is relative, it is
403          *  interpreted relative to filePath() if the file exists, otherwise
404          *  relative to the original path where the document was last saved.
405          *  The original path may be different from filePath() if the document was
406          *  later manually moved to a different location.
407          */
408         support::DocFileName getReferencedFileName(std::string const & fn) const;
409
410         /// Format a file name for LaTeX output (absolute or relative or filename only,
411         /// depending on file and context)
412         std::string const prepareFileNameForLaTeX(std::string const &,
413                                         std::string const &, bool nice) const;
414
415         /** Returns a vector of bibliography (*.bib) file paths suitable for the
416          *  output in the respective BibTeX/Biblatex macro
417          */
418         std::vector<docstring> const prepareBibFilePaths(OutputParams const &,
419                                     support::FileNamePairList const bibfilelist,
420                                     bool const extension = true) const;
421
422         /** Returns the path where a local layout file lives.
423          *  An empty string is returned for standard system and user layouts.
424          *  If possible, it is always relative to the buffer path.
425          */
426         std::string layoutPos() const;
427
428         /** Set the path to a local layout file.
429          *  This must be an absolute path but, if possible, it is always
430          *  stored as relative to the buffer path.
431          */
432         void setLayoutPos(std::string const & path);
433
434         /** A transformed version of the file name, adequate for LaTeX.
435             \param no_path optional if \c true then the path is stripped.
436         */
437         std::string latexName(bool no_path = true) const;
438
439         /// Get the name and type of the log.
440         std::string logName(LogType * type = 0) const;
441
442         /// Set document's parent Buffer.
443         void setParent(Buffer const *);
444         Buffer const * parent() const;
445
446         /** Get the document's master (or \c this if this is not a
447             child document)
448          */
449         Buffer const * masterBuffer() const;
450
451         /// \return true if \p child is a child of this \c Buffer.
452         bool isChild(Buffer * child) const;
453
454         /// \return true if this \c Buffer has children
455         bool hasChildren() const;
456
457         /// \return a list of the direct children of this Buffer.
458         /// this list has no duplicates and is in the order in which
459         /// the children appear.
460         ListOfBuffers getChildren() const;
461
462         /// \return a list of all descendents of this Buffer (children,
463         /// grandchildren, etc). this list has no duplicates and is in
464         /// the order in which the children appear.
465         ListOfBuffers getDescendents() const;
466
467         /// Collect all relative buffers, in the order in which they appear.
468         /// I.e., the "root" Buffer is first, then its first child, then any
469         /// of its children, etc. However, there are no duplicates in this
470         /// list.
471         /// This is "stable", too, in the sense that it returns the same
472         /// thing from whichever Buffer it is called.
473         ListOfBuffers allRelatives() const;
474
475         /// Is buffer read-only? True if it has either the read-only flag or the
476         /// externally modified flag.
477         bool isReadonly() const;
478
479         /// Does the buffer have the read-only flag?
480         bool hasReadonlyFlag() const;
481
482         /// Set buffer read-only flag
483         void setReadonly(bool flag = true);
484
485         /** Validate a buffer for LaTeX.
486             This validates the buffer, and returns a struct for use by
487             #makeLaTeX# and others. Its main use is to figure out what
488             commands and packages need to be included in the LaTeX file.
489             It (should) also check that the needed constructs are there
490             (i.e. that the \refs points to coresponding \labels). It
491             should perhaps inset "error" insets to help the user correct
492             obvious mistakes.
493         */
494         void validate(LaTeXFeatures &) const;
495
496         /// Reference information is cached in the Buffer, so we do not
497         /// have to check or read things over and over.
498         ///
499         /// There are two caches.
500         ///
501         /// One is a cache of the BibTeX files from which reference info is
502         /// being gathered. This cache is PER BUFFER, and the cache for the
503         /// master essentially includes the cache for its children. This gets
504         /// invalidated when an InsetBibtex is created, deleted, or modified.
505         ///
506         /// The other is a cache of the reference information itself. This
507         /// exists only in the master buffer, and when it needs to be updated,
508         /// the children add their information to the master's cache.
509
510         /// Calling this method invalidates the cache and so requires a
511         /// re-read.
512         void invalidateBibinfoCache() const;
513         /// This invalidates the cache of files we need to check.
514         void invalidateBibfileCache() const;
515         /// Updates the cached bibliography information, checking first to see
516         /// whether the cache is valid. If so, we do nothing. If not, then we
517         /// reload all the BibTeX info.
518         /// Note that this operates on the master document.
519         void reloadBibInfoCache() const;
520         /// \return the bibliography information for this buffer's master,
521         /// or just for it, if it isn't a child.
522         BiblioInfo const & masterBibInfo() const;
523         /// \return this buffer's bibliography information
524         BiblioInfo const & bibInfo() const;
525         /// collect bibliography info from the various insets in this buffer.
526         void collectBibKeys(support::FileNameList &) const;
527         /// add some BiblioInfo to our cache
528         void addBiblioInfo(BiblioInfo const & bi) const;
529         /// add a single piece of bibliography info to our cache
530         void addBibTeXInfo(docstring const & key, BibTeXInfo const & bi) const;
531         ///
532         void makeCitationLabels() const;
533         ///
534         void invalidateCiteLabels() const;
535         ///
536         bool citeLabelsValid() const;
537         ///
538         void getLabelList(std::vector<docstring> &) const;
539
540         /// This removes the .aux and .bbl files from the temp dir.
541         void removeBiblioTempFiles() const;
542
543         ///
544         void changeLanguage(Language const * from, Language const * to);
545
546         ///
547         bool isMultiLingual() const;
548         ///
549         std::set<Language const *> getLanguages() const;
550
551         ///
552         BufferParams & params();
553         BufferParams const & params() const;
554         ///
555         BufferParams const & masterParams() const;
556
557         /** The list of paragraphs.
558             This is a linked list of paragraph, this list holds the
559             whole contents of the document.
560          */
561         ParagraphList & paragraphs();
562         ParagraphList const & paragraphs() const;
563
564         /// LyX version control object.
565         LyXVC & lyxvc();
566         LyXVC const & lyxvc() const;
567
568         /// Where to put temporary files.
569         std::string const temppath() const;
570
571         /// Used when typesetting to place errorboxes.
572         TexRow const & texrow() const;
573         TexRow & texrow();
574
575         ///
576         ParIterator par_iterator_begin();
577         ///
578         ParConstIterator par_iterator_begin() const;
579         ///
580         ParIterator par_iterator_end();
581         ///
582         ParConstIterator par_iterator_end() const;
583
584         // Position of the child buffer where it appears first in the master.
585         DocIterator firstChildPosition(Buffer const * child);
586
587         /** \returns true only when the file is fully loaded.
588          *  Used to prevent the premature generation of previews
589          *  and by the citation inset.
590          */
591         bool isFullyLoaded() const;
592         /// Set by buffer_funcs' newFile.
593         void setFullyLoaded(bool);
594
595         /// FIXME: Needed by RenderPreview.
596         graphics::PreviewLoader * loader() const;
597         /// Update the LaTeX preview snippets associated with this buffer
598         void updatePreviews() const;
599         /// Remove any previewed LaTeX snippets associated with this buffer
600         void removePreviews() const;
601         ///
602         graphics::Cache & graphicsCache() const;
603
604         /// Our main text (inside the top InsetText)
605         Text & text() const;
606
607         /// Our top InsetText
608         Inset & inset() const;
609
610         //
611         // Macro handling
612         //
613         /// Collect macro definitions in paragraphs
614         void updateMacros() const;
615         /// Iterate through the whole buffer and try to resolve macros
616         void updateMacroInstances(UpdateType) const;
617
618         /// List macro names of this buffer, the parent and the children
619         void listMacroNames(MacroNameSet & macros) const;
620         /// Collect macros of the parent and its children in front of this buffer.
621         void listParentMacros(MacroSet & macros, LaTeXFeatures & features) const;
622
623         /// Return macro defined before pos (or in the master buffer)
624         MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
625         /// Return macro defined anywhere in the buffer (or in the master buffer)
626         MacroData const * getMacro(docstring const & name, bool global = true) const;
627         /// Return macro defined before the inclusion of the child
628         MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
629
630         /// Collect user macro names at loading time
631         typedef std::set<docstring> UserMacroSet;
632         mutable UserMacroSet usermacros;
633
634         /// Replace the inset contents for insets which InsetCode is equal
635         /// to the passed \p inset_code.
636         void changeRefsIfUnique(docstring const & from, docstring const & to);
637
638         /// get source code (latex/docbook) for some paragraphs, or all paragraphs
639         /// including preamble
640         /// returns nullptr if Id to Row conversion is unsupported
641         unique_ptr<TexRow> getSourceCode(odocstream & os,
642                         std::string const & format, pit_type par_begin,
643                         pit_type par_end, OutputWhat output, bool master) const;
644
645         /// Access to error list.
646         /// This method is used only for GUI visualisation of Buffer related
647         /// errors (like parsing or LateX compilation). This method is const
648         /// because modifying the returned ErrorList does not touch the document
649         /// contents.
650         ErrorList & errorList(std::string const & type) const;
651
652         /// The Toc backend.
653         /// This is useful only for screen visualisation of the Buffer. This
654         /// method is const because modifying this backend does not touch
655         /// the document contents.
656         TocBackend & tocBackend() const;
657
658         ///
659         Undo & undo();
660
661         /// This function is called when the buffer is changed.
662         void changed(bool update_metrics) const;
663         ///
664         void setChild(DocIterator const & dit, Buffer * child);
665         ///
666         void updateTocItem(std::string const &, DocIterator const &) const;
667         /// This function is called when the buffer structure is changed.
668         void structureChanged() const;
669         /// This function is called when some parsing error shows up.
670         void errors(std::string const & err, bool from_master = false) const;
671         /// This function is called when the buffer busy status change.
672         void setBusy(bool on) const;
673         /// Update window titles of all users.
674         void updateTitles() const;
675         /// Reset autosave timers for all users.
676         void resetAutosaveTimers() const;
677         ///
678         void message(docstring const & msg) const;
679
680         ///
681         void setGuiDelegate(frontend::GuiBufferDelegate * gui);
682         ///
683         bool hasGuiDelegate() const;
684
685         ///
686         ExportStatus doExport(std::string const & target, bool put_in_tempdir) const;
687         /// Export buffer to format \p format and open the result in a suitable viewer.
688         /// Note: This has nothing to do with preview of graphics or math formulas.
689         ExportStatus preview(std::string const & format) const;
690         /// true if there was a previous preview this session of this buffer and
691         /// there was an error on the previous preview of this buffer.
692         bool lastPreviewError() const;
693
694 private:
695         ///
696         ExportStatus doExport(std::string const & target, bool put_in_tempdir,
697                 std::string & result_file) const;
698         /// target is a format name optionally followed by a space
699         /// and a destination file-name
700         ExportStatus doExport(std::string const & target, bool put_in_tempdir,
701                 bool includeall, std::string & result_file) const;
702         ///
703         ExportStatus preview(std::string const & format, bool includeall) const;
704         ///
705         void setMathFlavor(OutputParams & op) const;
706
707 public:
708         ///
709         bool isExporting() const;
710
711         ///
712         typedef std::vector<std::pair<Inset *, ParIterator> > References;
713         ///
714         References const & references(docstring const & label) const;
715         ///
716         void addReference(docstring const & label, Inset * inset, ParIterator it);
717         ///
718         void clearReferenceCache() const;
719         ///
720         void setInsetLabel(docstring const & label, InsetLabel const * il);
721         ///
722         InsetLabel const * insetLabel(docstring const & label) const;
723
724         /// return a list of all used branches (also in children)
725         void getUsedBranches(std::list<docstring> &, bool const from_master = false) const;
726
727         /// sets the buffer_ member for every inset in this buffer.
728         // FIXME This really shouldn't be needed, but at the moment it's not
729         // clear how to do it just for the individual pieces we need.
730         void setBuffersForInsets() const;
731         /// Updates screen labels and some other information associated with
732         /// insets and paragraphs. Actually, it's more like a general "recurse
733         /// through the Buffer" routine, that visits all the insets and paragraphs.
734         void updateBuffer() const { updateBuffer(UpdateMaster, InternalUpdate); }
735         /// \param scope: whether to start with the master document or just
736         /// do this one.
737         /// \param output: whether we are preparing for output.
738         void updateBuffer(UpdateScope scope, UpdateType utype) const;
739         ///
740         void updateBuffer(ParIterator & parit, UpdateType utype) const;
741
742         /// Spellcheck starting from \p from.
743         /// \p from initial position, will then points to the next misspelled
744         ///    word.
745         /// \p to will points to the end of the next misspelled word.
746         /// \p word_lang will contain the found misspelled word.
747         /// \return progress if a new word was found.
748         int spellCheck(DocIterator & from, DocIterator & to,
749                 WordLangTuple & word_lang, docstring_list & suggestions) const;
750         ///
751         void checkChildBuffers();
752         ///
753         void checkMasterBuffer();
754
755         /// If the document is being saved to a new location and the named file
756         /// exists at the old location, return its updated path relative to the
757         /// new buffer path if possible, otherwise return its absolute path.
758         /// In all other cases, this is a no-op and name is returned unchanged.
759         /// If a non-empty ext is given, the existence of name.ext is checked
760         /// but the returned path will not contain this extension.
761         /// Similarly, when loading a document that was moved from the location
762         /// where it was saved, return the correct path relative to the new
763         /// location.
764         std::string includedFilePath(std::string const & name,
765                                 std::string const & ext = empty_string()) const;
766
767         /// compute statistics between \p from and \p to
768         /// \p from initial position
769         /// \p to points to the end position
770         /// \p skipNoOutput if notes etc. should be ignored
771         void updateStatistics(DocIterator & from, DocIterator & to,
772                                                   bool skipNoOutput = true) const;
773         /// statistics accessor functions
774         int wordCount() const;
775         int charCount(bool with_blanks) const;
776
777         // this is const because it does not modify the buffer's real contents,
778         // only the mutable flag.
779         void setChangesPresent(bool) const;
780         bool areChangesPresent() const;
781         void updateChangesPresent() const;
782
783 private:
784         friend class MarkAsExporting;
785         /// mark the buffer as busy exporting something, or not
786         void setExportStatus(bool e) const;
787
788         ///
789         References & getReferenceCache(docstring const & label);
790         /// Change name of buffer. Updates "read-only" flag.
791         void setFileName(support::FileName const & fname);
792         ///
793         void getLanguages(std::set<Language const *> &) const;
794         /// Checks whether any of the referenced bibfiles have changed since the
795         /// last time we loaded the cache. Note that this does NOT update the
796         /// cached information.
797         void checkIfBibInfoCacheIsValid() const;
798         /// Update the list of all bibfiles in use (including bibfiles
799         /// of loaded child documents).
800         void updateBibfilesCache(UpdateScope scope = UpdateMaster) const;
801         /// Return the list with all bibfiles in use (including bibfiles
802         /// of loaded child documents).
803         support::FileNamePairList const &
804                 getBibfilesCache(UpdateScope scope = UpdateMaster) const;
805         ///
806         void collectChildren(ListOfBuffers & children, bool grand_children) const;
807
808         /// noncopyable
809         Buffer(Buffer const &);
810         void operator=(Buffer const &);
811
812         /// Use the Pimpl idiom to hide the internals.
813         class Impl;
814         /// The pointer never changes although *pimpl_'s contents may.
815         Impl * const d;
816 };
817
818
819 /// Helper class, to guarantee that the export status
820 /// gets reset properly. To use, simply create a local variable:
821 ///    MarkAsExporting mex(bufptr);
822 /// and leave the rest to us.
823 class MarkAsExporting {
824 public:
825         MarkAsExporting(Buffer const * buf) : buf_(buf)
826         {
827                 buf_->setExportStatus(true);
828         }
829         ~MarkAsExporting()
830         {
831                 buf_->setExportStatus(false);
832         }
833 private:
834         Buffer const * const buf_;
835 };
836
837
838 } // namespace lyx
839
840 #endif