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 /// read a new document from a string
134 bool readString(std::string const &);
136 bool readFile(support::FileName const & filename);
138 /// read the header, returns number of unknown tokens
139 int readHeader(Lexer & lex);
141 /** Reads a file without header.
142 \param par if != 0 insert the file.
143 \return \c true if file is not completely read.
145 bool readDocument(Lexer &);
148 void insertStringAsLines(ParagraphList & plist,
149 pit_type &, pos_type &,
150 Font const &, docstring const &, bool);
152 DocIterator getParFromID(int id) const;
153 /// do we have a paragraph with this id?
154 bool hasParWithID(int id) const;
157 frontend::WorkAreaManager & workAreaManager() const;
160 Takes care of auto-save files and backup file if requested.
161 Returns \c true if the save is successful, \c false otherwise.
165 /// Write document to stream. Returns \c false if unsuccesful.
166 bool write(std::ostream &) const;
167 /// Write file. Returns \c false if unsuccesful.
168 bool writeFile(support::FileName const &) const;
170 /// Loads LyX file \c filename into buffer, * and \return success
171 bool loadLyXFile(support::FileName const & s);
173 /// Fill in the ErrorList with the TeXErrors
174 void bufferErrors(TeXErrors const &, ErrorList &) const;
176 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
177 bool makeLaTeXFile(support::FileName const & filename,
178 std::string const & original_path,
179 OutputParams const &,
180 bool output_preamble = true,
181 bool output_body = true) const;
182 /** Export the buffer to LaTeX.
183 If \p os is a file stream, and params().inputenc is "auto" or
184 "default", and the buffer contains text in different languages
185 with more than one encoding, then this method will change the
186 encoding associated to \p os. Therefore you must not call this
187 method with a string stream if the output is supposed to go to a
190 ofs.open("test.tex");
191 writeLaTeXSource(ofs, ...);
193 \endcode is NOT equivalent to \code
194 odocstringstream oss;
195 writeLaTeXSource(oss, ...);
197 ofs.open("test.tex");
202 void writeLaTeXSource(odocstream & os,
203 std::string const & original_path,
204 OutputParams const &,
205 bool output_preamble = true,
206 bool output_body = true) const;
208 void makeDocBookFile(support::FileName const & filename,
209 OutputParams const & runparams_in,
210 bool only_body = false) const;
212 void writeDocBookSource(odocstream & os, std::string const & filename,
213 OutputParams const & runparams_in,
214 bool only_body = false) const;
215 /// returns the main language for the buffer (document)
216 Language const * language() const;
217 /// get l10n translated to the buffers language
218 docstring const B_(std::string const & l10n) const;
222 /// return true if the main lyx file does not need saving
223 bool isClean() const;
225 bool isBakClean() const;
227 bool isDepClean(std::string const & name) const;
229 /// whether or not disk file has been externally modified
230 bool isExternallyModified(CheckMethod method) const;
232 /// save timestamp and checksum of the given file.
233 void saveCheckSum(support::FileName const & file) const;
235 /// mark the main lyx file as not needing saving
236 void markClean() const;
239 void markBakClean() const;
242 void markDepClean(std::string const & name);
245 void setUnnamed(bool flag = true);
248 bool isUnnamed() const;
250 /// Mark this buffer as dirty.
253 /// Returns the buffer's filename. It is always an absolute path.
254 support::FileName fileName() const;
256 /// Returns the buffer's filename. It is always an absolute path.
257 std::string absFileName() const;
259 /// Returns the the path where the buffer lives.
260 /// It is always an absolute path.
261 std::string filePath() const;
263 /** A transformed version of the file name, adequate for LaTeX.
264 \param no_path optional if \c true then the path is stripped.
266 std::string latexName(bool no_path = true) const;
268 /// Get thee name and type of the log.
269 std::string logName(LogType * type = 0) const;
271 /// Change name of buffer. Updates "read-only" flag.
272 void setFileName(std::string const & newfile);
274 /// Set document's parent Buffer.
275 void setParent(Buffer const *);
276 Buffer const * parent() const;
278 // Collect all relative buffer
279 std::vector<Buffer const *> allRelatives() const;
281 /** Get the document's master (or \c this if this is not a
284 Buffer const * masterBuffer() const;
286 /// \return true if \p child is a child of this \c Buffer.
287 bool isChild(Buffer * child) const;
289 /// return a vector with all children and grandchildren
290 std::vector<Buffer *> getChildren() const;
292 /// Is buffer read-only?
293 bool isReadonly() const;
295 /// Set buffer read-only flag
296 void setReadonly(bool flag = true);
298 /// returns \c true if the buffer contains a LaTeX document
299 bool isLatex() const;
300 /// returns \c true if the buffer contains a DocBook document
301 bool isDocBook() const;
302 /// returns \c true if the buffer contains a Wed document
303 bool isLiterate() const;
305 /** Validate a buffer for LaTeX.
306 This validates the buffer, and returns a struct for use by
307 #makeLaTeX# and others. Its main use is to figure out what
308 commands and packages need to be included in the LaTeX file.
309 It (should) also check that the needed constructs are there
310 (i.e. that the \refs points to coresponding \labels). It
311 should perhaps inset "error" insets to help the user correct
314 void validate(LaTeXFeatures &) const;
316 /// Update the cache with all bibfiles in use (including bibfiles
317 /// of loaded child documents).
318 void updateBibfilesCache(UpdateScope scope = UpdateMaster) const;
320 void invalidateBibinfoCache();
321 /// Return the cache with all bibfiles in use (including bibfiles
322 /// of loaded child documents).
323 support::FileNameList const &
324 getBibfilesCache(UpdateScope scope = UpdateMaster) const;
325 /// \return the bibliography information for this buffer's master,
326 /// or just for it, if it isn't a child.
327 BiblioInfo const & masterBibInfo() const;
328 /// \return the bibliography information for this buffer ONLY.
329 BiblioInfo const & localBibInfo() const;
331 void getLabelList(std::vector<docstring> &) const;
334 void changeLanguage(Language const * from, Language const * to);
337 bool isMultiLingual() const;
340 BufferParams & params();
341 BufferParams const & params() const;
343 /** The list of paragraphs.
344 This is a linked list of paragraph, this list holds the
345 whole contents of the document.
347 ParagraphList & paragraphs();
348 ParagraphList const & paragraphs() const;
350 /// LyX version control object.
352 LyXVC const & lyxvc() const;
354 /// Where to put temporary files.
355 std::string const temppath() const;
357 /// Used when typesetting to place errorboxes.
358 TexRow const & texrow() const;
362 ParIterator par_iterator_begin();
364 ParConstIterator par_iterator_begin() const;
366 ParIterator par_iterator_end();
368 ParConstIterator par_iterator_end() const;
370 // Position of the child buffer where it appears first in the master.
371 DocIterator firstChildPosition(Buffer const * child);
373 /** \returns true only when the file is fully loaded.
374 * Used to prevent the premature generation of previews
375 * and by the citation inset.
377 bool isFullyLoaded() const;
378 /// Set by buffer_funcs' newFile.
379 void setFullyLoaded(bool);
381 /// Our main text (inside the top InsetText)
384 /// Our top InsetText
385 Inset & inset() const;
390 /// Collect macro definitions in paragraphs
391 void updateMacros() const;
392 /// Iterate through the whole buffer and try to resolve macros
393 void updateMacroInstances() const;
395 /// List macro names of this buffer, the parent and the children
396 void listMacroNames(MacroNameSet & macros) const;
397 /// Collect macros of the parent and its children in front of this buffer.
398 void listParentMacros(MacroSet & macros, LaTeXFeatures & features) const;
400 /// Return macro defined before pos (or in the master buffer)
401 MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
402 /// Return macro defined anywhere in the buffer (or in the master buffer)
403 MacroData const * getMacro(docstring const & name, bool global = true) const;
404 /// Return macro defined before the inclusion of the child
405 MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
407 /// Replace the inset contents for insets which InsetCode is equal
408 /// to the passed \p inset_code.
409 void changeRefsIfUnique(docstring const & from, docstring const & to,
412 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
413 /// including preamble
414 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
415 bool full_source) const;
417 /// Access to error list.
418 /// This method is used only for GUI visualisation of Buffer related
419 /// errors (like parsing or LateX compilation). This method is const
420 /// because modifying the returned ErrorList does not touch the document
422 ErrorList & errorList(std::string const & type) const;
425 /// This is useful only for screen visualisation of the Buffer. This
426 /// method is const because modifying this backend does not touch
427 /// the document contents.
428 TocBackend & tocBackend() const;
433 /// This function is called when the buffer is changed.
434 void changed() const;
436 void updateTocItem(std::string const &, DocIterator const &) const;
437 /// This function is called when the buffer structure is changed.
438 void structureChanged() const;
439 /// This function is called when some parsing error shows up.
440 void errors(std::string const & err) const;
441 /// This function is called when the buffer busy status change.
442 void setBusy(bool on) const;
443 /// This function is called when the buffer readonly status change.
444 void setReadOnly(bool on) const;
445 /// Update window titles of all users.
446 void updateTitles() const;
447 /// Reset autosave timers for all users.
448 void resetAutosaveTimers() const;
450 void message(docstring const & msg) const;
453 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
455 bool hasGuiDelegate() const;
458 void autoSave() const;
460 void removeAutosaveFile() const;
462 /// return the format of the buffer on a string
463 std::string bufferFormat() const;
466 bool doExport(std::string const & format, bool put_in_tempdir,
467 std::string & result_file) const;
469 bool doExport(std::string const & format, bool put_in_tempdir) const;
471 bool preview(std::string const & format) const;
473 bool isExportable(std::string const & format) const;
475 std::vector<Format const *> exportableFormats(bool only_viewable) const;
478 typedef std::vector<std::pair<InsetRef *, ParIterator> > References;
479 References & references(docstring const & label);
480 References const & references(docstring const & label) const;
481 void clearReferenceCache() const;
482 void setInsetLabel(docstring const & label, InsetLabel const * il);
483 InsetLabel const * insetLabel(docstring const & label) const;
485 /// sets the buffer_ member for every inset in this buffer.
486 // FIXME This really shouldn't be needed, but at the moment it's not
487 // clear how to do it just for the individual pieces we need.
488 void setBuffersForInsets() const;
490 void updateLabels(UpdateScope = UpdateMaster) const;
492 void updateLabels(ParIterator & parit) const;
494 /// Find next word starting from \p from.
495 /// \p from initial position to search, will then points to the next
497 /// \p to will points to the end of the next word.
498 /// \p word will contain the found word if any.
499 /// \return true if a new word was found.
500 bool nextWord(DocIterator & from, DocIterator & to,
501 docstring & word) const;
504 /// search for macro in local (buffer) table or in children
505 MacroData const * getBufferMacro(docstring const & name,
506 DocIterator const & pos) const;
507 /** Update macro table starting with position of it
508 \param it in some text inset
510 void updateMacros(DocIterator & it,
511 DocIterator & scope) const;
514 void collectRelatives(BufferSet & bufs) const;
517 bool readFileHelper(support::FileName const & s);
519 std::vector<std::string> backends() const;
520 /** Inserts a file into a document
521 \return \c false if method fails.
523 ReadStatus readFile(Lexer &, support::FileName const & filename,
524 bool fromString = false);
526 support::FileName getAutosaveFilename() const;
528 /// Use the Pimpl idiom to hide the internals.
530 /// The pointer never changes although *pimpl_'s contents may.
533 frontend::GuiBufferDelegate * gui_;
535 /// This function is called when the buffer structure is changed.
536 Signal structureChanged_;
537 /// This function is called when some parsing error shows up.
538 //Signal errors(std::string const &) = 0;
539 /// This function is called when some message shows up.
540 //Signal message(docstring const &) = 0;
541 /// This function is called when the buffer busy status change.
542 //Signal setBusy(bool) = 0;
543 /// Reset autosave timers for all users.
544 Signal resetAutosaveTimers_;