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"
19 #include "support/SignalSlot.h"
50 class ParConstIterator;
59 class GuiBufferDelegate;
60 class WorkAreaManager;
68 /** The buffer object.
69 * This is the buffer object. It contains all the informations about
70 * a document loaded into LyX.
71 * The buffer object owns the Text (wrapped in an InsetText), which
72 * contains the individual paragraphs of the document.
75 * I am not sure if the class is complete or
76 * minimal, probably not.
77 * \author Lars Gullik Bjønnes
81 /// What type of log will \c getLogName() return?
83 latexlog, ///< LaTeX log
84 buildlog ///< Literate build log
87 /// Result of \c readFile()
89 failure, ///< The file could not be read
90 success, ///< The file could not be read
91 wrongversion ///< The version of the file does not match ours
95 /// Method to check if a file is externally modified, used by
96 /// isExternallyModified()
98 * timestamp is fast but inaccurate. For example, the granularity
99 * of timestamp on a FAT filesystem is 2 second. Also, various operations
100 * may touch the timestamp of a file even when its content is unchanged.
102 * checksum is accurate but slow, which can be a problem when it is
103 * frequently used, or used for a large file on a slow (network) file
106 * FIXME: replace this method with support/FileMonitor.
109 checksum_method, ///< Use file checksum
110 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
120 explicit Buffer(std::string const & file, bool b = false);
125 /** High-level interface to buffer functionality.
126 This function parses a command string and executes it
128 bool dispatch(std::string const & command, bool * result = 0);
130 /// Maybe we know the function already by number...
131 bool dispatch(FuncRequest const & func, bool * result = 0);
133 /// Load the autosaved file.
134 void loadAutoSaveFile();
136 /// read a new document from a string
137 bool readString(std::string const &);
139 bool readFile(support::FileName const & filename);
141 /// read the header, returns number of unknown tokens
142 int readHeader(Lexer & lex);
144 /** Reads a file without header.
145 \param par if != 0 insert the file.
146 \return \c true if file is not completely read.
148 bool readDocument(Lexer &);
151 void insertStringAsLines(ParagraphList & plist,
152 pit_type &, pos_type &,
153 Font const &, docstring const &, bool);
155 DocIterator getParFromID(int id) const;
156 /// do we have a paragraph with this id?
157 bool hasParWithID(int id) const;
160 frontend::WorkAreaManager & workAreaManager() const;
163 Takes care of auto-save files and backup file if requested.
164 Returns \c true if the save is successful, \c false otherwise.
168 /// Write document to stream. Returns \c false if unsuccesful.
169 bool write(std::ostream &) const;
170 /// Write file. Returns \c false if unsuccesful.
171 bool writeFile(support::FileName const &) const;
173 /// Loads LyX file \c filename into buffer, * and \return success
174 bool loadLyXFile(support::FileName const & s);
176 /// Fill in the ErrorList with the TeXErrors
177 void bufferErrors(TeXErrors const &, ErrorList &) const;
179 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
180 bool makeLaTeXFile(support::FileName const & filename,
181 std::string const & original_path,
182 OutputParams const &,
183 bool output_preamble = true,
184 bool output_body = true) const;
185 /** Export the buffer to LaTeX.
186 If \p os is a file stream, and params().inputenc is "auto" or
187 "default", and the buffer contains text in different languages
188 with more than one encoding, then this method will change the
189 encoding associated to \p os. Therefore you must not call this
190 method with a string stream if the output is supposed to go to a
193 ofs.open("test.tex");
194 writeLaTeXSource(ofs, ...);
196 \endcode is NOT equivalent to \code
197 odocstringstream oss;
198 writeLaTeXSource(oss, ...);
200 ofs.open("test.tex");
205 void writeLaTeXSource(odocstream & os,
206 std::string const & original_path,
207 OutputParams const &,
208 bool output_preamble = true,
209 bool output_body = true) const;
211 void makeDocBookFile(support::FileName const & filename,
212 OutputParams const & runparams_in,
213 bool only_body = false) const;
215 void writeDocBookSource(odocstream & os, std::string const & filename,
216 OutputParams const & runparams_in,
217 bool only_body = false) const;
218 /// returns the main language for the buffer (document)
219 Language const * language() const;
220 /// get l10n translated to the buffers language
221 docstring const B_(std::string const & l10n) const;
225 /// return true if the main lyx file does not need saving
226 bool isClean() const;
228 bool isBakClean() const;
230 bool isDepClean(std::string const & name) const;
232 /// whether or not disk file has been externally modified
233 bool isExternallyModified(CheckMethod method) const;
235 /// save timestamp and checksum of the given file.
236 void saveCheckSum(support::FileName const & file) const;
238 /// mark the main lyx file as not needing saving
239 void markClean() const;
242 void markBakClean() const;
245 void markDepClean(std::string const & name);
248 void setUnnamed(bool flag = true);
251 bool isUnnamed() const;
253 /// Mark this buffer as dirty.
256 /// Returns the buffer's filename. It is always an absolute path.
257 support::FileName fileName() const;
259 /// Returns the buffer's filename. It is always an absolute path.
260 std::string absFileName() const;
262 /// Returns the the path where the buffer lives.
263 /// It is always an absolute path.
264 std::string filePath() const;
266 /** A transformed version of the file name, adequate for LaTeX.
267 \param no_path optional if \c true then the path is stripped.
269 std::string latexName(bool no_path = true) const;
271 /// Get thee name and type of the log.
272 std::string logName(LogType * type = 0) const;
274 /// Change name of buffer. Updates "read-only" flag.
275 void setFileName(std::string const & newfile);
277 /// Set document's parent Buffer.
278 void setParent(Buffer const *);
279 Buffer const * parent() const;
281 // Collect all relative buffer
282 std::vector<Buffer const *> allRelatives() const;
284 /** Get the document's master (or \c this if this is not a
287 Buffer const * masterBuffer() const;
289 /// \return true if \p child is a child of this \c Buffer.
290 bool isChild(Buffer * child) const;
292 /// return a vector with all children and grandchildren
293 std::vector<Buffer *> getChildren() const;
295 /// Is buffer read-only?
296 bool isReadonly() const;
298 /// Set buffer read-only flag
299 void setReadonly(bool flag = true);
301 /// returns \c true if the buffer contains a LaTeX document
302 bool isLatex() const;
303 /// returns \c true if the buffer contains a DocBook document
304 bool isDocBook() const;
305 /// returns \c true if the buffer contains a Wed document
306 bool isLiterate() const;
308 /** Validate a buffer for LaTeX.
309 This validates the buffer, and returns a struct for use by
310 #makeLaTeX# and others. Its main use is to figure out what
311 commands and packages need to be included in the LaTeX file.
312 It (should) also check that the needed constructs are there
313 (i.e. that the \refs points to coresponding \labels). It
314 should perhaps inset "error" insets to help the user correct
317 void validate(LaTeXFeatures &) const;
319 /// Update the cache with all bibfiles in use (including bibfiles
320 /// of loaded child documents).
321 void updateBibfilesCache(UpdateScope scope = UpdateMaster) const;
323 void invalidateBibinfoCache();
324 /// Return the cache with all bibfiles in use (including bibfiles
325 /// of loaded child documents).
326 support::FileNameList const &
327 getBibfilesCache(UpdateScope scope = UpdateMaster) const;
328 /// \return the bibliography information for this buffer's master,
329 /// or just for it, if it isn't a child.
330 BiblioInfo const & masterBibInfo() const;
331 /// \return the bibliography information for this buffer ONLY.
332 BiblioInfo const & localBibInfo() const;
334 void getLabelList(std::vector<docstring> &) const;
337 void changeLanguage(Language const * from, Language const * to);
340 bool isMultiLingual() const;
343 BufferParams & params();
344 BufferParams const & params() const;
346 /** The list of paragraphs.
347 This is a linked list of paragraph, this list holds the
348 whole contents of the document.
350 ParagraphList & paragraphs();
351 ParagraphList const & paragraphs() const;
353 /// LyX version control object.
355 LyXVC const & lyxvc() const;
357 /// Where to put temporary files.
358 std::string const temppath() const;
360 /// Used when typesetting to place errorboxes.
361 TexRow const & texrow() const;
365 ParIterator par_iterator_begin();
367 ParConstIterator par_iterator_begin() const;
369 ParIterator par_iterator_end();
371 ParConstIterator par_iterator_end() const;
373 // Position of the child buffer where it appears first in the master.
374 DocIterator firstChildPosition(Buffer const * child);
376 /** \returns true only when the file is fully loaded.
377 * Used to prevent the premature generation of previews
378 * and by the citation inset.
380 bool isFullyLoaded() const;
381 /// Set by buffer_funcs' newFile.
382 void setFullyLoaded(bool);
384 /// Our main text (inside the top InsetText)
387 /// Our top InsetText
388 Inset & inset() const;
393 /// Collect macro definitions in paragraphs
394 void updateMacros() const;
395 /// Iterate through the whole buffer and try to resolve macros
396 void updateMacroInstances() const;
398 /// List macro names of this buffer, the parent and the children
399 void listMacroNames(MacroNameSet & macros) const;
400 /// Collect macros of the parent and its children in front of this buffer.
401 void listParentMacros(MacroSet & macros, LaTeXFeatures & features) const;
403 /// Return macro defined before pos (or in the master buffer)
404 MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
405 /// Return macro defined anywhere in the buffer (or in the master buffer)
406 MacroData const * getMacro(docstring const & name, bool global = true) const;
407 /// Return macro defined before the inclusion of the child
408 MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
410 /// Replace the inset contents for insets which InsetCode is equal
411 /// to the passed \p inset_code.
412 void changeRefsIfUnique(docstring const & from, docstring const & to,
415 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
416 /// including preamble
417 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
418 bool full_source) const;
420 /// Access to error list.
421 /// This method is used only for GUI visualisation of Buffer related
422 /// errors (like parsing or LateX compilation). This method is const
423 /// because modifying the returned ErrorList does not touch the document
425 ErrorList & errorList(std::string const & type) const;
428 /// This is useful only for screen visualisation of the Buffer. This
429 /// method is const because modifying this backend does not touch
430 /// the document contents.
431 TocBackend & tocBackend() const;
436 /// This function is called when the buffer is changed.
437 void changed() const;
439 void updateTocItem(std::string const &, DocIterator const &) const;
440 /// This function is called when the buffer structure is changed.
441 void structureChanged() const;
442 /// This function is called when some parsing error shows up.
443 void errors(std::string const & err) const;
444 /// This function is called when the buffer busy status change.
445 void setBusy(bool on) const;
446 /// This function is called when the buffer readonly status change.
447 void setReadOnly(bool on) const;
448 /// Update window titles of all users.
449 void updateTitles() const;
450 /// Reset autosave timers for all users.
451 void resetAutosaveTimers() const;
453 void message(docstring const & msg) const;
456 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
458 bool hasGuiDelegate() const;
461 void autoSave() const;
463 /// return the format of the buffer on a string
464 std::string bufferFormat() const;
467 bool doExport(std::string const & format, bool put_in_tempdir,
468 std::string & result_file) const;
470 bool doExport(std::string const & format, bool put_in_tempdir) const;
472 bool preview(std::string const & format) const;
474 bool isExportable(std::string const & format) const;
476 std::vector<Format const *> exportableFormats(bool only_viewable) const;
479 typedef std::vector<std::pair<InsetRef *, ParIterator> > References;
480 References & references(docstring const & label);
481 References const & references(docstring const & label) const;
482 void clearReferenceCache() const;
483 void setInsetLabel(docstring const & label, InsetLabel const * il);
484 InsetLabel const * insetLabel(docstring const & label) const;
486 /// sets the buffer_ member for every inset in this buffer.
487 // FIXME This really shouldn't be needed, but at the moment it's not
488 // clear how to do it just for the individual pieces we need.
489 void setBuffersForInsets() const;
491 void updateLabels(UpdateScope = UpdateMaster) const;
493 void updateLabels(ParIterator & parit) const;
496 /// search for macro in local (buffer) table or in children
497 MacroData const * getBufferMacro(docstring const & name,
498 DocIterator const & pos) const;
499 /** Update macro table starting with position of it
500 \param it in some text inset
502 void updateMacros(DocIterator & it,
503 DocIterator & scope) const;
506 void collectRelatives(BufferSet & bufs) const;
509 bool readFileHelper(support::FileName const & s);
511 std::vector<std::string> backends() const;
512 /** Inserts a file into a document
513 \return \c false if method fails.
515 ReadStatus readFile(Lexer &, support::FileName const & filename,
516 bool fromString = false);
518 /// Use the Pimpl idiom to hide the internals.
520 /// The pointer never changes although *pimpl_'s contents may.
523 frontend::GuiBufferDelegate * gui_;
525 /// This function is called when the buffer structure is changed.
526 Signal structureChanged_;
527 /// This function is called when some parsing error shows up.
528 //Signal errors(std::string const &) = 0;
529 /// This function is called when some message shows up.
530 //Signal message(docstring const &) = 0;
531 /// This function is called when the buffer busy status change.
532 //Signal setBusy(bool) = 0;
533 /// Reset autosave timers for all users.
534 Signal resetAutosaveTimers_;