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/FileNameList.h"
18 #include "support/strfwd.h"
19 #include "support/types.h"
20 #include "support/SignalSlot.h"
49 class ParConstIterator;
58 class GuiBufferDelegate;
59 class WorkAreaManager;
67 /** The buffer object.
68 * This is the buffer object. It contains all the informations about
69 * a document loaded into LyX.
70 * The buffer object owns the Text (wrapped in an InsetText), which
71 * contains the individual paragraphs of the document.
74 * I am not sure if the class is complete or
75 * minimal, probably not.
76 * \author Lars Gullik Bjønnes
80 /// What type of log will \c getLogName() return?
82 latexlog, ///< LaTeX log
83 buildlog ///< Literate build log
86 /// Result of \c readFile()
88 failure, ///< The file could not be read
89 success, ///< The file could not be read
90 wrongversion ///< The version of the file does not match ours
94 /// Method to check if a file is externally modified, used by
95 /// isExternallyModified()
97 * timestamp is fast but inaccurate. For example, the granularity
98 * of timestamp on a FAT filesystem is 2 second. Also, various operations
99 * may touch the timestamp of a file even when its content is unchanged.
101 * checksum is accurate but slow, which can be a problem when it is
102 * frequently used, or used for a large file on a slow (network) file
105 * FIXME: replace this method with support/FileMonitor.
108 checksum_method, ///< Use file checksum
109 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
114 \param b optional \c false by default
116 explicit Buffer(std::string const & file, bool b = false);
121 /** High-level interface to buffer functionality.
122 This function parses a command string and executes it
124 bool dispatch(std::string const & command, bool * result = 0);
126 /// Maybe we know the function already by number...
127 bool dispatch(FuncRequest const & func, bool * result = 0);
129 /// Load the autosaved file.
130 void loadAutoSaveFile();
132 /// read a new document from a string
133 bool readString(std::string const &);
135 bool readFile(support::FileName const & filename);
137 /// read the header, returns number of unknown tokens
138 int readHeader(Lexer & lex);
140 /** Reads a file without header.
141 \param par if != 0 insert the file.
142 \return \c false if file is not completely read.
144 bool readDocument(Lexer &);
147 void insertStringAsLines(ParagraphList & plist,
148 pit_type &, pos_type &,
149 Font const &, docstring const &, bool);
151 DocIterator getParFromID(int id) const;
152 /// do we have a paragraph with this id?
153 bool hasParWithID(int id) const;
156 frontend::WorkAreaManager & workAreaManager() const;
159 Takes care of auto-save files and backup file if requested.
160 Returns \c true if the save is successful, \c false otherwise.
164 /// Write document to stream. Returns \c false if unsuccesful.
165 bool write(std::ostream &) const;
166 /// Write file. Returns \c false if unsuccesful.
167 bool writeFile(support::FileName const &) const;
169 /// Loads LyX file \c filename into buffer, * and \return success
170 bool loadLyXFile(support::FileName const & s);
172 /// Fill in the ErrorList with the TeXErrors
173 void bufferErrors(TeXErrors const &, ErrorList &) const;
175 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
176 bool makeLaTeXFile(support::FileName const & filename,
177 std::string const & original_path,
178 OutputParams const &,
179 bool output_preamble = true,
180 bool output_body = true) const;
181 /** Export the buffer to LaTeX.
182 If \p os is a file stream, and params().inputenc is "auto" or
183 "default", and the buffer contains text in different languages
184 with more than one encoding, then this method will change the
185 encoding associated to \p os. Therefore you must not call this
186 method with a string stream if the output is supposed to go to a
189 ofs.open("test.tex");
190 writeLaTeXSource(ofs, ...);
192 \endcode is NOT equivalent to \code
193 odocstringstream oss;
194 writeLaTeXSource(oss, ...);
196 ofs.open("test.tex");
201 void writeLaTeXSource(odocstream & os,
202 std::string const & original_path,
203 OutputParams const &,
204 bool output_preamble = true,
205 bool output_body = true) const;
207 void makeDocBookFile(support::FileName const & filename,
208 OutputParams const & runparams_in,
209 bool only_body = false) const;
211 void writeDocBookSource(odocstream & os, std::string const & filename,
212 OutputParams const & runparams_in,
213 bool only_body = false) const;
214 /// returns the main language for the buffer (document)
215 Language const * language() const;
216 /// get l10n translated to the buffers language
217 docstring const B_(std::string const & l10n) const;
221 /// return true if the main lyx file does not need saving
222 bool isClean() const;
224 bool isBakClean() const;
226 bool isDepClean(std::string const & name) const;
228 /// whether or not disk file has been externally modified
229 bool isExternallyModified(CheckMethod method) const;
231 /// save timestamp and checksum of the given file.
232 void saveCheckSum(support::FileName const & file) const;
234 /// mark the main lyx file as not needing saving
235 void markClean() const;
238 void markBakClean() const;
241 void markDepClean(std::string const & name);
244 void setUnnamed(bool flag = true);
247 bool isUnnamed() const;
249 /// Mark this buffer as dirty.
252 /// Returns the buffer's filename. It is always an absolute path.
253 support::FileName fileName() const;
255 /// Returns the buffer's filename. It is always an absolute path.
256 std::string absFileName() const;
258 /// Returns the the path where the buffer lives.
259 /// It is always an absolute path.
260 std::string filePath() const;
262 /** A transformed version of the file name, adequate for LaTeX.
263 \param no_path optional if \c true then the path is stripped.
265 std::string latexName(bool no_path = true) const;
267 /// Get thee name and type of the log.
268 std::string logName(LogType * type = 0) const;
270 /// Change name of buffer. Updates "read-only" flag.
271 void setFileName(std::string const & newfile);
273 /// Set document's parent Buffer.
274 void setParent(Buffer const *);
275 Buffer const * parent();
277 /** Get the document's master (or \c this if this is not a
280 Buffer const * masterBuffer() const;
282 /// Is buffer read-only?
283 bool isReadonly() const;
285 /// Set buffer read-only flag
286 void setReadonly(bool flag = true);
288 /// returns \c true if the buffer contains a LaTeX document
289 bool isLatex() const;
290 /// returns \c true if the buffer contains a DocBook document
291 bool isDocBook() const;
292 /// returns \c true if the buffer contains a Wed document
293 bool isLiterate() const;
295 /** Validate a buffer for LaTeX.
296 This validates the buffer, and returns a struct for use by
297 #makeLaTeX# and others. Its main use is to figure out what
298 commands and packages need to be included in the LaTeX file.
299 It (should) also check that the needed constructs are there
300 (i.e. that the \refs points to coresponding \labels). It
301 should perhaps inset "error" insets to help the user correct
304 void validate(LaTeXFeatures &) const;
306 /// Update the cache with all bibfiles in use (including bibfiles
307 /// of loaded child documents).
308 void updateBibfilesCache() const;
309 /// Return the cache with all bibfiles in use (including bibfiles
310 /// of loaded child documents).
311 support::FileNameList const & getBibfilesCache() const;
313 void getLabelList(std::vector<docstring> &) const;
316 void changeLanguage(Language const * from, Language const * to);
319 bool isMultiLingual() const;
322 BufferParams & params();
323 BufferParams const & params() const;
325 /** The list of paragraphs.
326 This is a linked list of paragraph, this list holds the
327 whole contents of the document.
329 ParagraphList & paragraphs();
330 ParagraphList const & paragraphs() const;
332 /// LyX version control object.
334 LyXVC const & lyxvc() const;
336 /// Where to put temporary files.
337 std::string const temppath() const;
339 /// Used when typesetting to place errorboxes.
340 TexRow const & texrow() const;
343 ParIterator par_iterator_begin();
345 ParConstIterator par_iterator_begin() const;
347 ParIterator par_iterator_end();
349 ParConstIterator par_iterator_end() const;
351 /** \returns true only when the file is fully loaded.
352 * Used to prevent the premature generation of previews
353 * and by the citation inset.
355 bool isFullyLoaded() const;
356 /// Set by buffer_funcs' newFile.
357 void setFullyLoaded(bool);
359 /// Our main text (inside the top InsetText)
362 /// Our top InsetText
363 Inset & inset() const;
368 /// Collect macro definitions in paragraphs
369 void updateMacros() const;
370 /// Iterate through the whole buffer and try to resolve macros
371 void updateMacroInstances() const;
373 /// List macro names of this buffer, the parent and the children
374 void listMacroNames(MacroNameSet & macros) const;
375 /// Collect macros of the parent and its children in front of this buffer.
376 void listParentMacros(MacroSet & macros, LaTeXFeatures & features) const;
378 /// Return macro defined before pos (or in the master buffer)
379 MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
380 /// Return macro defined anywhere in the buffer (or in the master buffer)
381 MacroData const * getMacro(docstring const & name, bool global = true) const;
382 /// Return macro defined before the inclusion of the child
383 MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
385 /// Replace the inset contents for insets which InsetCode is equal
386 /// to the passed \p inset_code.
387 void changeRefsIfUnique(docstring const & from, docstring const & to,
390 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
391 /// including preamble
392 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
395 /// Access to error list.
396 /// This method is used only for GUI visualisation of Buffer related
397 /// errors (like parsing or LateX compilation). This method is const
398 /// because modifying the returned ErrorList does not touch the document
400 ErrorList & errorList(std::string const & type) const;
403 /// This is useful only for screen visualisation of the Buffer. This
404 /// method is const because modifying this backend does not touch
405 /// the document contents.
406 TocBackend & tocBackend() const;
411 /// This function is called when the buffer is changed.
412 void changed() const;
413 /// This function is called when the buffer structure is changed.
414 void structureChanged() const;
415 /// This function is called when some parsing error shows up.
416 void errors(std::string const & err) const;
417 /// This function is called when the buffer busy status change.
418 void setBusy(bool on) const;
419 /// This function is called when the buffer readonly status change.
420 void setReadOnly(bool on) const;
421 /// Update window titles of all users.
422 void updateTitles() const;
423 /// Reset autosave timers for all users.
424 void resetAutosaveTimers() const;
426 void message(docstring const & msg) const;
428 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
431 void autoSave() const;
433 /// return the format of the buffer on a string
434 std::string bufferFormat() const;
437 bool doExport(std::string const & format, bool put_in_tempdir,
438 std::string & result_file) const;
440 bool doExport(std::string const & format, bool put_in_tempdir) const;
442 bool preview(std::string const & format) const;
444 bool isExportable(std::string const & format) const;
446 std::vector<Format const *> exportableFormats(bool only_viewable) const;
449 typedef std::vector<std::pair<InsetRef *, ParIterator> > References;
450 References & references(docstring const & label);
451 References const & references(docstring const & label) const;
452 void clearReferenceCache() const;
453 void setInsetLabel(docstring const & label, InsetLabel const * il);
454 InsetLabel const * insetLabel(docstring const & label) const;
457 /// search for macro in local (buffer) table or in children
458 MacroData const * getBufferMacro(docstring const & name,
459 DocIterator const & pos) const;
460 /** Update macro table starting with position of it
461 \param it in some text inset
463 void updateMacros(DocIterator & it,
464 DocIterator & scope) const;
467 bool readFileHelper(support::FileName const & s);
469 std::vector<std::string> backends() const;
470 /** Inserts a file into a document
471 \return \c false if method fails.
473 ReadStatus readFile(Lexer &, support::FileName const & filename,
474 bool fromString = false);
476 /// Use the Pimpl idiom to hide the internals.
478 /// The pointer never changes although *pimpl_'s contents may.
481 frontend::GuiBufferDelegate * gui_;
483 /// This function is called when the buffer structure is changed.
484 Signal structureChanged_;
485 /// This function is called when some parsing error shows up.
486 //Signal errors(std::string const &) = 0;
487 /// This function is called when some message shows up.
488 //Signal message(docstring const &) = 0;
489 /// This function is called when the buffer busy status change.
490 //Signal setBusy(bool) = 0;
491 /// Reset autosave timers for all users.
492 Signal resetAutosaveTimers_;