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"
56 class ParConstIterator;
66 class GuiBufferDelegate;
67 class WorkAreaManager;
75 /** The buffer object.
76 * This is the buffer object. It contains all the informations about
77 * a document loaded into LyX.
78 * The buffer object owns the Text (wrapped in an InsetText), which
79 * contains the individual paragraphs of the document.
82 * I am not sure if the class is complete or
83 * minimal, probably not.
84 * \author Lars Gullik Bjønnes
88 /// What type of log will \c getLogName() return?
90 latexlog, ///< LaTeX log
91 buildlog ///< Literate build log
94 /// Result of \c readFile()
96 failure, ///< The file could not be read
97 success, ///< The file could not be read
98 wrongversion ///< The version of the file does not match ours
102 /// Method to check if a file is externally modified, used by
103 /// isExternallyModified()
105 * timestamp is fast but inaccurate. For example, the granularity
106 * of timestamp on a FAT filesystem is 2 second. Also, various operations
107 * may touch the timestamp of a file even when its content is unchanged.
109 * checksum is accurate but slow, which can be a problem when it is
110 * frequently used, or used for a large file on a slow (network) file
113 * FIXME: replace this method with support/FileMonitor.
116 checksum_method, ///< Use file checksum
117 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
127 explicit Buffer(std::string const & file, bool b = false);
132 /** High-level interface to buffer functionality.
133 This function parses a command string and executes it.
135 void dispatch(std::string const & command, DispatchResult & result);
137 /// Maybe we know the function already by number...
138 void dispatch(FuncRequest const & func, DispatchResult & result);
140 /// Can this function be exectued?
141 /// \return true if we made a decision
142 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
144 /// read a new document from a string
145 bool readString(std::string const &);
147 bool readFile(support::FileName const & filename);
149 /// read the header, returns number of unknown tokens
150 int readHeader(Lexer & lex);
152 /** Reads a file without header.
153 \param par if != 0 insert the file.
154 \return \c true if file is not completely read.
156 bool readDocument(Lexer &);
159 DocIterator getParFromID(int id) const;
160 /// do we have a paragraph with this id?
161 bool hasParWithID(int id) const;
164 frontend::WorkAreaManager & workAreaManager() const;
167 Takes care of auto-save files and backup file if requested.
168 Returns \c true if the save is successful, \c false otherwise.
172 /// Write document to stream. Returns \c false if unsuccesful.
173 bool write(std::ostream &) const;
174 /// Write file. Returns \c false if unsuccesful.
175 bool writeFile(support::FileName const &) const;
177 /// Loads LyX file \c filename into buffer, * and \return success
178 bool loadLyXFile(support::FileName const & s);
180 /// Fill in the ErrorList with the TeXErrors
181 void bufferErrors(TeXErrors const &, ErrorList &) const;
183 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
184 bool makeLaTeXFile(support::FileName const & filename,
185 std::string const & original_path,
186 OutputParams const &,
187 bool output_preamble = true,
188 bool output_body = true) const;
189 /** Export the buffer to LaTeX.
190 If \p os is a file stream, and params().inputenc is "auto" or
191 "default", and the buffer contains text in different languages
192 with more than one encoding, then this method will change the
193 encoding associated to \p os. Therefore you must not call this
194 method with a string stream if the output is supposed to go to a
197 ofs.open("test.tex");
198 writeLaTeXSource(ofs, ...);
200 \endcode is NOT equivalent to \code
201 odocstringstream oss;
202 writeLaTeXSource(oss, ...);
204 ofs.open("test.tex");
209 void writeLaTeXSource(odocstream & os,
210 std::string const & original_path,
211 OutputParams const &,
212 bool output_preamble = true,
213 bool output_body = true) const;
215 void makeDocBookFile(support::FileName const & filename,
216 OutputParams const & runparams_in,
217 bool only_body = false) const;
219 void writeDocBookSource(odocstream & os, std::string const & filename,
220 OutputParams const & runparams_in,
221 bool only_body = false) const;
223 void makeLyXHTMLFile(support::FileName const & filename,
224 OutputParams const & runparams_in,
225 bool only_body = false) const;
227 void writeLyXHTMLSource(odocstream & os,
228 OutputParams const & runparams_in,
229 bool only_body = false) const;
230 /// returns the main language for the buffer (document)
231 Language const * language() const;
232 /// get l10n translated to the buffers language
233 docstring const B_(std::string const & l10n) const;
237 /// return true if the main lyx file does not need saving
238 bool isClean() const;
240 bool isBakClean() const;
242 bool isDepClean(std::string const & name) const;
244 /// whether or not disk file has been externally modified
245 bool isExternallyModified(CheckMethod method) const;
247 /// save timestamp and checksum of the given file.
248 void saveCheckSum(support::FileName const & file) const;
250 /// mark the main lyx file as not needing saving
251 void markClean() const;
254 void markBakClean() const;
257 void markDepClean(std::string const & name);
260 void setUnnamed(bool flag = true);
263 bool isUnnamed() const;
265 /// Mark this buffer as dirty.
268 /// Returns the buffer's filename. It is always an absolute path.
269 support::FileName fileName() const;
271 /// Returns the buffer's filename. It is always an absolute path.
272 std::string absFileName() const;
274 /// Returns the the path where the buffer lives.
275 /// It is always an absolute path.
276 std::string filePath() const;
278 /** A transformed version of the file name, adequate for LaTeX.
279 \param no_path optional if \c true then the path is stripped.
281 std::string latexName(bool no_path = true) const;
283 /// Get the name and type of the log.
284 std::string logName(LogType * type = 0) const;
286 /// Change name of buffer. Updates "read-only" flag.
287 void setFileName(std::string const & newfile);
289 /// Set document's parent Buffer.
290 void setParent(Buffer const *);
291 Buffer const * parent() const;
293 // Collect all relative buffer
294 std::vector<Buffer const *> allRelatives() const;
296 /** Get the document's master (or \c this if this is not a
299 Buffer const * masterBuffer() const;
301 /// \return true if \p child is a child of this \c Buffer.
302 bool isChild(Buffer * child) const;
304 /// return a vector with all children and grandchildren
305 std::vector<Buffer *> getChildren() const;
307 /// Is buffer read-only?
308 bool isReadonly() const;
310 /// Set buffer read-only flag
311 void setReadonly(bool flag = true);
313 /// returns \c true if the buffer contains a LaTeX document
314 bool isLatex() const;
315 /// returns \c true if the buffer contains a DocBook document
316 bool isDocBook() const;
317 /// returns \c true if the buffer contains a Wed document
318 bool isLiterate() const;
320 /** Validate a buffer for LaTeX.
321 This validates the buffer, and returns a struct for use by
322 #makeLaTeX# and others. Its main use is to figure out what
323 commands and packages need to be included in the LaTeX file.
324 It (should) also check that the needed constructs are there
325 (i.e. that the \refs points to coresponding \labels). It
326 should perhaps inset "error" insets to help the user correct
329 void validate(LaTeXFeatures &) const;
331 /// Update the cache with all bibfiles in use (including bibfiles
332 /// of loaded child documents).
333 void updateBibfilesCache(UpdateScope scope = UpdateMaster) const;
335 void invalidateBibinfoCache();
336 /// Return the cache with all bibfiles in use (including bibfiles
337 /// of loaded child documents).
338 support::FileNameList const &
339 getBibfilesCache(UpdateScope scope = UpdateMaster) const;
340 /// \return the bibliography information for this buffer's master,
341 /// or just for it, if it isn't a child.
342 BiblioInfo const & masterBibInfo() const;
343 /// \return the bibliography information for this buffer ONLY.
344 BiblioInfo const & localBibInfo() const;
346 void getLabelList(std::vector<docstring> &) const;
349 void changeLanguage(Language const * from, Language const * to);
352 bool isMultiLingual() const;
355 BufferParams & params();
356 BufferParams const & params() const;
358 /** The list of paragraphs.
359 This is a linked list of paragraph, this list holds the
360 whole contents of the document.
362 ParagraphList & paragraphs();
363 ParagraphList const & paragraphs() const;
365 /// LyX version control object.
367 LyXVC const & lyxvc() const;
369 /// Where to put temporary files.
370 std::string const temppath() const;
372 /// Used when typesetting to place errorboxes.
373 TexRow const & texrow() const;
377 ParIterator par_iterator_begin();
379 ParConstIterator par_iterator_begin() const;
381 ParIterator par_iterator_end();
383 ParConstIterator par_iterator_end() const;
385 // Position of the child buffer where it appears first in the master.
386 DocIterator firstChildPosition(Buffer const * child);
388 /** \returns true only when the file is fully loaded.
389 * Used to prevent the premature generation of previews
390 * and by the citation inset.
392 bool isFullyLoaded() const;
393 /// Set by buffer_funcs' newFile.
394 void setFullyLoaded(bool);
396 /// Our main text (inside the top InsetText)
399 /// Our top InsetText
400 Inset & inset() const;
405 /// Collect macro definitions in paragraphs
406 void updateMacros() const;
407 /// Iterate through the whole buffer and try to resolve macros
408 void updateMacroInstances() const;
410 /// List macro names of this buffer, the parent and the children
411 void listMacroNames(MacroNameSet & macros) const;
412 /// Collect macros of the parent and its children in front of this buffer.
413 void listParentMacros(MacroSet & macros, LaTeXFeatures & features) const;
415 /// Return macro defined before pos (or in the master buffer)
416 MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
417 /// Return macro defined anywhere in the buffer (or in the master buffer)
418 MacroData const * getMacro(docstring const & name, bool global = true) const;
419 /// Return macro defined before the inclusion of the child
420 MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
422 /// Replace the inset contents for insets which InsetCode is equal
423 /// to the passed \p inset_code.
424 void changeRefsIfUnique(docstring const & from, docstring const & to,
427 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
428 /// including preamble
429 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
430 bool full_source) const;
432 /// Access to error list.
433 /// This method is used only for GUI visualisation of Buffer related
434 /// errors (like parsing or LateX compilation). This method is const
435 /// because modifying the returned ErrorList does not touch the document
437 ErrorList & errorList(std::string const & type) const;
440 /// This is useful only for screen visualisation of the Buffer. This
441 /// method is const because modifying this backend does not touch
442 /// the document contents.
443 TocBackend & tocBackend() const;
448 /// This function is called when the buffer is changed.
449 void changed() const;
451 void updateTocItem(std::string const &, DocIterator const &) const;
452 /// This function is called when the buffer structure is changed.
453 void structureChanged() const;
454 /// This function is called when some parsing error shows up.
455 void errors(std::string const & err, bool from_master = false) const;
456 /// This function is called when the buffer busy status change.
457 void setBusy(bool on) const;
458 /// This function is called when the buffer readonly status change.
459 void setReadOnly(bool on) const;
460 /// Update window titles of all users.
461 void updateTitles() const;
462 /// Reset autosave timers for all users.
463 void resetAutosaveTimers() const;
465 void message(docstring const & msg) const;
468 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
470 bool hasGuiDelegate() const;
473 void autoSave() const;
475 void removeAutosaveFile() const;
477 void moveAutosaveFile(support::FileName const & old) const;
479 support::FileName getAutosaveFilename() const;
481 /// return the format of the buffer on a string
482 std::string bufferFormat() const;
483 /// return the default output format of the current backend
484 std::string getDefaultOutputFormat() const;
487 bool doExport(std::string const & format, bool put_in_tempdir,
488 std::string & result_file) const;
490 bool doExport(std::string const & format, bool put_in_tempdir) const;
492 bool preview(std::string const & format) const;
494 bool isExportable(std::string const & format) const;
496 std::vector<Format const *> exportableFormats(bool only_viewable) const;
499 typedef std::vector<std::pair<InsetRef *, ParIterator> > References;
500 References & references(docstring const & label);
501 References const & references(docstring const & label) const;
502 void clearReferenceCache() const;
503 void setInsetLabel(docstring const & label, InsetLabel const * il);
504 InsetLabel const * insetLabel(docstring const & label) const;
506 /// return a list of all used branches (also in children)
507 void getUsedBranches(std::list<docstring> &, bool const from_master = false) const;
509 /// sets the buffer_ member for every inset in this buffer.
510 // FIXME This really shouldn't be needed, but at the moment it's not
511 // clear how to do it just for the individual pieces we need.
512 void setBuffersForInsets() const;
514 void updateLabels(UpdateScope = UpdateMaster) const;
516 void updateLabels(ParIterator & parit) const;
518 /// Spellcheck starting from \p from.
519 /// \p from initial position, will then points to the next misspelled
521 /// \p to will points to the end of the next misspelled word.
522 /// \p word_lang will contain the found misspelled word.
523 /// \return progress if a new word was found.
524 int spellCheck(DocIterator & from, DocIterator & to,
525 WordLangTuple & word_lang, docstring_list & suggestions) const;
528 /// search for macro in local (buffer) table or in children
529 MacroData const * getBufferMacro(docstring const & name,
530 DocIterator const & pos) const;
531 /** Update macro table starting with position of it
532 \param it in some text inset
534 void updateMacros(DocIterator & it,
535 DocIterator & scope) const;
538 void collectRelatives(BufferSet & bufs) const;
541 bool readFileHelper(support::FileName const & s);
543 std::vector<std::string> backends() const;
544 /** Inserts a file into a document
545 \return \c false if method fails.
547 ReadStatus readFile(Lexer &, support::FileName const & filename,
548 bool fromString = false);
550 /** If we have branches that use the file suffix
551 feature, return the file name with suffix appended.
553 support::FileName exportFileName() const;
555 /// Use the Pimpl idiom to hide the internals.
557 /// The pointer never changes although *pimpl_'s contents may.
560 frontend::GuiBufferDelegate * gui_;
562 /// This function is called when the buffer structure is changed.
563 Signal structureChanged_;
564 /// This function is called when some parsing error shows up.
565 //Signal errors(std::string const &) = 0;
566 /// This function is called when some message shows up.
567 //Signal message(docstring const &) = 0;
568 /// This function is called when the buffer busy status change.
569 //Signal setBusy(bool) = 0;
570 /// Reset autosave timers for all users.
571 Signal resetAutosaveTimers_;