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 "update_flags.h"
17 #include "insets/InsetCode.h"
19 #include "support/strfwd.h"
20 #include "support/types.h"
21 #include "support/SignalSlot.h"
57 class ParConstIterator;
67 class GuiBufferDelegate;
68 class WorkAreaManager;
76 /** The buffer object.
77 * This is the buffer object. It contains all the informations about
78 * a document loaded into LyX.
79 * The buffer object owns the Text (wrapped in an InsetText), which
80 * contains the individual paragraphs of the document.
83 * I am not sure if the class is complete or
84 * minimal, probably not.
85 * \author Lars Gullik Bjønnes
89 /// What type of log will \c getLogName() return?
91 latexlog, ///< LaTeX log
92 buildlog ///< Literate build log
95 /// Result of \c readFile()
97 failure, ///< The file could not be read
98 success, ///< The file could not be read
99 wrongversion ///< The version of the file does not match ours
103 /// Method to check if a file is externally modified, used by
104 /// isExternallyModified()
106 * timestamp is fast but inaccurate. For example, the granularity
107 * of timestamp on a FAT filesystem is 2 second. Also, various operations
108 * may touch the timestamp of a file even when its content is unchanged.
110 * checksum is accurate but slow, which can be a problem when it is
111 * frequently used, or used for a large file on a slow (network) file
114 * FIXME: replace this method with support/FileMonitor.
117 checksum_method, ///< Use file checksum
118 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
128 explicit Buffer(std::string const & file, bool readonly = false,
129 Buffer const * cloned_buffer = 0);
135 Buffer * clone() const;
137 bool isClone() const;
139 /** High-level interface to buffer functionality.
140 This function parses a command string and executes it.
142 void dispatch(std::string const & command, DispatchResult & result);
144 /// Maybe we know the function already by number...
145 void dispatch(FuncRequest const & func, DispatchResult & result);
147 /// Can this function be exectued?
148 /// \return true if we made a decision
149 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
151 /// read a new document from a string
152 bool readString(std::string const &);
154 bool readFile(support::FileName const & filename);
156 /// read the header, returns number of unknown tokens
157 int readHeader(Lexer & lex);
159 /** Reads a file without header.
160 \param par if != 0 insert the file.
161 \return \c true if file is not completely read.
163 bool readDocument(Lexer &);
166 DocIterator getParFromID(int id) const;
167 /// do we have a paragraph with this id?
168 bool hasParWithID(int id) const;
171 frontend::WorkAreaManager & workAreaManager() const;
174 Takes care of auto-save files and backup file if requested.
175 Returns \c true if the save is successful, \c false otherwise.
179 /// Write document to stream. Returns \c false if unsuccesful.
180 bool write(std::ostream &) const;
181 /// save emergency file
182 /// \return a status message towards the user.
183 docstring emergencyWrite();
184 /// Write file. Returns \c false if unsuccesful.
185 bool writeFile(support::FileName const &) const;
187 /// Loads LyX file \c filename into buffer, * and return success
188 bool loadLyXFile(support::FileName const & s);
189 /// Reloads the LyX file
192 /// Fill in the ErrorList with the TeXErrors
193 void bufferErrors(TeXErrors const &, ErrorList &) const;
195 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
196 bool makeLaTeXFile(support::FileName const & filename,
197 std::string const & original_path,
198 OutputParams const &,
199 bool output_preamble = true,
200 bool output_body = true) const;
201 /** Export the buffer to LaTeX.
202 If \p os is a file stream, and params().inputenc is "auto" or
203 "default", and the buffer contains text in different languages
204 with more than one encoding, then this method will change the
205 encoding associated to \p os. Therefore you must not call this
206 method with a string stream if the output is supposed to go to a
209 ofs.open("test.tex");
210 writeLaTeXSource(ofs, ...);
212 \endcode is NOT equivalent to \code
213 odocstringstream oss;
214 writeLaTeXSource(oss, ...);
216 ofs.open("test.tex");
221 void writeLaTeXSource(odocstream & os,
222 std::string const & original_path,
223 OutputParams const &,
224 bool output_preamble = true,
225 bool output_body = true) const;
227 void makeDocBookFile(support::FileName const & filename,
228 OutputParams const & runparams_in,
229 bool only_body = false) const;
231 void writeDocBookSource(odocstream & os, std::string const & filename,
232 OutputParams const & runparams_in,
233 bool only_body = false) const;
235 void makeLyXHTMLFile(support::FileName const & filename,
236 OutputParams const & runparams_in,
237 bool only_body = false) const;
239 void writeLyXHTMLSource(odocstream & os,
240 OutputParams const & runparams_in,
241 bool only_body = false) const;
242 /// returns the main language for the buffer (document)
243 Language const * language() const;
244 /// get l10n translated to the buffers language
245 docstring const B_(std::string const & l10n) const;
249 /// return true if the main lyx file does not need saving
250 bool isClean() const;
252 bool isBakClean() const;
254 bool isDepClean(std::string const & name) const;
256 /// whether or not disk file has been externally modified
257 bool isExternallyModified(CheckMethod method) const;
259 /// save timestamp and checksum of the given file.
260 void saveCheckSum(support::FileName const & file) const;
262 /// mark the main lyx file as not needing saving
263 void markClean() const;
266 void markBakClean() const;
269 void markDepClean(std::string const & name);
272 void setUnnamed(bool flag = true);
274 /// Whether or not a filename has been assigned to this buffer
275 bool isUnnamed() const;
277 /// Whether or not this buffer is internal.
279 /// An internal buffer does not contain a real document, but some auxiliary text segment.
280 /// It is not associated with a filename, it is never saved, thus it does not need to be
281 /// automatically saved, nor it needs to trigger any "do you want to save ?" question.
282 bool isInternal() const;
284 /// Mark this buffer as dirty.
287 /// Returns the buffer's filename. It is always an absolute path.
288 support::FileName fileName() const;
290 /// Returns the buffer's filename. It is always an absolute path.
291 std::string absFileName() const;
293 /// Returns the the path where the buffer lives.
294 /// It is always an absolute path.
295 std::string filePath() const;
297 /** A transformed version of the file name, adequate for LaTeX.
298 \param no_path optional if \c true then the path is stripped.
300 std::string latexName(bool no_path = true) const;
302 /// Get the name and type of the log.
303 std::string logName(LogType * type = 0) const;
305 /// Change name of buffer. Updates "read-only" flag.
306 void setFileName(std::string const & newfile);
308 /// Set document's parent Buffer.
309 void setParent(Buffer const *);
310 Buffer const * parent() const;
312 // Collect all relative buffer
313 std::vector<Buffer const *> allRelatives() const;
315 /** Get the document's master (or \c this if this is not a
318 Buffer const * masterBuffer() const;
320 /// \return true if \p child is a child of this \c Buffer.
321 bool isChild(Buffer * child) const;
323 /// return a vector with all children (and grandchildren)
324 std::vector<Buffer *> getChildren(bool grand_children = true) const;
326 /// Add all children (and grandchildren) to supplied vector
327 void getChildren(std::vector<Buffer *> & children, bool grand_children = true) const;
329 /// Is buffer read-only?
330 bool isReadonly() const;
332 /// Set buffer read-only flag
333 void setReadonly(bool flag = true);
335 /// returns \c true if the buffer contains a LaTeX document
336 bool isLatex() const;
337 /// returns \c true if the buffer contains a DocBook document
338 bool isDocBook() const;
339 /// returns \c true if the buffer contains a Wed document
340 bool isLiterate() const;
342 /** Validate a buffer for LaTeX.
343 This validates the buffer, and returns a struct for use by
344 #makeLaTeX# and others. Its main use is to figure out what
345 commands and packages need to be included in the LaTeX file.
346 It (should) also check that the needed constructs are there
347 (i.e. that the \refs points to coresponding \labels). It
348 should perhaps inset "error" insets to help the user correct
351 void validate(LaTeXFeatures &) const;
353 /// Update the cache with all bibfiles in use (including bibfiles
354 /// of loaded child documents).
355 void updateBibfilesCache(UpdateScope scope = UpdateMaster) const;
357 void invalidateBibinfoCache();
358 /// Return the cache with all bibfiles in use (including bibfiles
359 /// of loaded child documents).
360 support::FileNameList const &
361 getBibfilesCache(UpdateScope scope = UpdateMaster) 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;
377 BufferParams & params();
378 BufferParams const & params() const;
380 /** The list of paragraphs.
381 This is a linked list of paragraph, this list holds the
382 whole contents of the document.
384 ParagraphList & paragraphs();
385 ParagraphList const & paragraphs() const;
387 /// LyX version control object.
389 LyXVC const & lyxvc() const;
391 /// Where to put temporary files.
392 std::string const temppath() const;
394 /// Used when typesetting to place errorboxes.
395 TexRow const & texrow() const;
399 ParIterator par_iterator_begin();
401 ParConstIterator par_iterator_begin() const;
403 ParIterator par_iterator_end();
405 ParConstIterator par_iterator_end() const;
407 // Position of the child buffer where it appears first in the master.
408 DocIterator firstChildPosition(Buffer const * child);
410 /** \returns true only when the file is fully loaded.
411 * Used to prevent the premature generation of previews
412 * and by the citation inset.
414 bool isFullyLoaded() const;
415 /// Set by buffer_funcs' newFile.
416 void setFullyLoaded(bool);
418 /// Our main text (inside the top InsetText)
421 /// Our top InsetText
422 Inset & inset() const;
427 /// Collect macro definitions in paragraphs
428 void updateMacros() const;
429 /// Iterate through the whole buffer and try to resolve macros
430 void updateMacroInstances() const;
432 /// List macro names of this buffer, the parent and the children
433 void listMacroNames(MacroNameSet & macros) const;
434 /// Collect macros of the parent and its children in front of this buffer.
435 void listParentMacros(MacroSet & macros, LaTeXFeatures & features) const;
437 /// Return macro defined before pos (or in the master buffer)
438 MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
439 /// Return macro defined anywhere in the buffer (or in the master buffer)
440 MacroData const * getMacro(docstring const & name, bool global = true) const;
441 /// Return macro defined before the inclusion of the child
442 MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
444 /// Collect user macro names at loading time
445 typedef std::set<docstring> UserMacroSet;
446 UserMacroSet usermacros;
448 /// Replace the inset contents for insets which InsetCode is equal
449 /// to the passed \p inset_code.
450 void changeRefsIfUnique(docstring const & from, docstring const & to,
453 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
454 /// including preamble
455 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
456 bool full_source) const;
458 /// Access to error list.
459 /// This method is used only for GUI visualisation of Buffer related
460 /// errors (like parsing or LateX compilation). This method is const
461 /// because modifying the returned ErrorList does not touch the document
463 ErrorList & errorList(std::string const & type) const;
466 /// This is useful only for screen visualisation of the Buffer. This
467 /// method is const because modifying this backend does not touch
468 /// the document contents.
469 TocBackend & tocBackend() const;
474 /// This function is called when the buffer is changed.
475 void changed(bool update_metrics) const;
477 void updateTocItem(std::string const &, DocIterator const &) const;
478 /// This function is called when the buffer structure is changed.
479 void structureChanged() const;
480 /// This function is called when some parsing error shows up.
481 void errors(std::string const & err, bool from_master = false) const;
482 /// This function is called when the buffer busy status change.
483 void setBusy(bool on) const;
484 /// This function is called when the buffer readonly status change.
485 void setReadOnly(bool on) const;
486 /// Update window titles of all users.
487 void updateTitles() const;
488 /// Reset autosave timers for all users.
489 void resetAutosaveTimers() const;
491 void message(docstring const & msg) const;
494 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
496 bool hasGuiDelegate() const;
499 void autoSave() const;
501 void removeAutosaveFile() const;
503 void moveAutosaveFile(support::FileName const & old) const;
505 support::FileName getAutosaveFilename() const;
507 /// return the format of the buffer on a string
508 std::string bufferFormat() const;
509 /// return the default output format of the current backend
510 std::string getDefaultOutputFormat() const;
513 bool doExport(std::string const & format, bool put_in_tempdir,
514 std::string & result_file) const;
516 bool doExport(std::string const & format, bool put_in_tempdir) const;
518 bool preview(std::string const & format) const;
520 bool isExportable(std::string const & format) const;
522 std::vector<Format const *> exportableFormats(bool only_viewable) const;
524 bool isExportableFormat(std::string const & format) const;
527 typedef std::vector<std::pair<InsetRef *, ParIterator> > References;
528 References & references(docstring const & label);
529 References const & references(docstring const & label) const;
530 void clearReferenceCache() const;
531 void setInsetLabel(docstring const & label, InsetLabel const * il);
532 InsetLabel const * insetLabel(docstring const & label) const;
534 /// return a list of all used branches (also in children)
535 void getUsedBranches(std::list<docstring> &, bool const from_master = false) const;
537 /// sets the buffer_ member for every inset in this buffer.
538 // FIXME This really shouldn't be needed, but at the moment it's not
539 // clear how to do it just for the individual pieces we need.
540 void setBuffersForInsets() const;
541 /// Updates screen labels and some other information associated with
542 /// insets and paragraphs. Actually, it's more like a general "recurse
543 /// through the Buffer" routine, that visits all the insets and paragraphs.
544 void updateLabels() const { updateLabels(UpdateMaster, false); }
545 /// \param scope: whether to start with the master document or just
547 /// \param output: whether we are preparing for output.
548 void updateLabels(UpdateScope scope, bool output) const;
550 void updateLabels(ParIterator & parit, bool output) const;
552 /// Spellcheck starting from \p from.
553 /// \p from initial position, will then points to the next misspelled
555 /// \p to will points to the end of the next misspelled word.
556 /// \p word_lang will contain the found misspelled word.
557 /// \return progress if a new word was found.
558 int spellCheck(DocIterator & from, DocIterator & to,
559 WordLangTuple & word_lang, docstring_list & suggestions) const;
562 /// search for macro in local (buffer) table or in children
563 MacroData const * getBufferMacro(docstring const & name,
564 DocIterator const & pos) const;
565 /** Update macro table starting with position of it
566 \param it in some text inset
568 void updateMacros(DocIterator & it,
569 DocIterator & scope) const;
571 void checkBibInfoCache() const;
573 void setLabel(ParIterator & it) const;
575 void collectRelatives(BufferSet & bufs) const;
578 bool readFileHelper(support::FileName const & s);
580 std::vector<std::string> backends() const;
581 /** Inserts a file into a document
582 \return \c false if method fails.
584 ReadStatus readFile(Lexer &, support::FileName const & filename,
585 bool fromString = false);
587 /** If we have branches that use the file suffix
588 feature, return the file name with suffix appended.
590 support::FileName exportFileName() const;
592 /// Use the Pimpl idiom to hide the internals.
594 /// The pointer never changes although *pimpl_'s contents may.
597 frontend::GuiBufferDelegate * gui_;
599 /// This function is called when the buffer structure is changed.
600 Signal structureChanged_;
601 /// This function is called when some parsing error shows up.
602 //Signal errors(std::string const &) = 0;
603 /// This function is called when some message shows up.
604 //Signal message(docstring const &) = 0;
605 /// This function is called when the buffer busy status change.
606 //Signal setBusy(bool) = 0;
607 /// Reset autosave timers for all users.
608 Signal resetAutosaveTimers_;