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/FileName.h"
18 #include "support/docstring.h"
19 #include "support/docstream.h"
20 #include "support/types.h"
45 class ParConstIterator;
54 class GuiBufferDelegate;
55 class WorkAreaManager;
58 /** The buffer object.
59 * This is the buffer object. It contains all the informations about
60 * a document loaded into LyX.
61 * The buffer object owns the Text (wrapped in an InsetText), which
62 * contains the individual paragraphs of the document.
65 * I am not sure if the class is complete or
66 * minimal, probably not.
67 * \author Lars Gullik Bjønnes
71 /// What type of log will \c getLogName() return?
73 latexlog, ///< LaTeX log
74 buildlog ///< Literate build log
77 /// Result of \c readFile()
79 failure, ///< The file could not be read
80 success, ///< The file could not be read
81 wrongversion ///< The version of the file does not match ours
84 /// Method to check if a file is externally modified, used by
85 /// isExternallyModified()
87 * timestamp is fast but inaccurate. For example, the granularity
88 * of timestamp on a FAT filesystem is 2 second. Also, various operations
89 * may touch the timestamp of a file even when its content is unchanged.
91 * checksum is accurate but slow, which can be a problem when it is
92 * frequently used, or used for a large file on a slow (network) file
96 checksum_method, ///< Use file check sum
97 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
102 \param b optional \c false by default
104 explicit Buffer(std::string const & file, bool b = false);
109 /** High-level interface to buffer functionality.
110 This function parses a command string and executes it
112 bool dispatch(std::string const & command, bool * result = 0);
114 /// Maybe we know the function already by number...
115 bool dispatch(FuncRequest const & func, bool * result = 0);
117 /// Load the autosaved file.
118 void loadAutoSaveFile();
120 /// read a new document from a string
121 bool readString(std::string const &);
123 bool readFile(support::FileName const & filename);
125 /// read the header, returns number of unknown tokens
126 int readHeader(Lexer & lex);
128 /** Reads a file without header.
129 \param par if != 0 insert the file.
130 \return \c false if file is not completely read.
132 bool readDocument(Lexer &);
135 void insertStringAsLines(ParagraphList & plist,
136 pit_type &, pos_type &,
137 Font const &, docstring const &, bool);
139 ParIterator getParFromID(int id) const;
140 /// do we have a paragraph with this id?
141 bool hasParWithID(int id) const;
144 frontend::WorkAreaManager & workAreaManager() const;
147 Takes care of auto-save files and backup file if requested.
148 Returns \c true if the save is successful, \c false otherwise.
152 /// Write document to stream. Returns \c false if unsuccesful.
153 bool write(std::ostream &) const;
154 /// Write file. Returns \c false if unsuccesful.
155 bool writeFile(support::FileName const &) const;
157 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
158 bool makeLaTeXFile(support::FileName const & filename,
159 std::string const & original_path,
160 OutputParams const &,
161 bool output_preamble = true,
162 bool output_body = true);
163 /** Export the buffer to LaTeX.
164 If \p os is a file stream, and params().inputenc is "auto" or
165 "default", and the buffer contains text in different languages
166 with more than one encoding, then this method will change the
167 encoding associated to \p os. Therefore you must not call this
168 method with a string stream if the output is supposed to go to a
171 ofs.open("test.tex");
172 writeLaTeXSource(ofs, ...);
174 \endcode is NOT equivalent to \code
175 odocstringstream oss;
176 writeLaTeXSource(oss, ...);
178 ofs.open("test.tex");
183 void writeLaTeXSource(odocstream & os,
184 std::string const & original_path,
185 OutputParams const &,
186 bool output_preamble = true,
187 bool output_body = true);
189 void makeDocBookFile(support::FileName const & filename,
190 OutputParams const & runparams_in,
191 bool only_body = false);
193 void writeDocBookSource(odocstream & os, std::string const & filename,
194 OutputParams const & runparams_in,
195 bool only_body = false);
196 /// returns the main language for the buffer (document)
197 Language const * language() const;
198 /// get l10n translated to the buffers language
199 docstring const B_(std::string const & l10n) const;
203 /// return true if the main lyx file does not need saving
204 bool isClean() const;
206 bool isBakClean() const;
208 bool isDepClean(std::string const & name) const;
210 /// whether or not disk file has been externally modified
211 bool isExternallyModified(CheckMethod method) const;
213 /// save timestamp and checksum of the given file.
214 void saveCheckSum(support::FileName const & file) const;
216 /// mark the main lyx file as not needing saving
217 void markClean() const;
220 void markBakClean() const;
223 void markDepClean(std::string const & name);
226 void setUnnamed(bool flag = true);
229 bool isUnnamed() const;
231 /// Mark this buffer as dirty.
234 /// Returns the buffer's filename. It is always an absolute path.
235 std::string absFileName() const;
237 /// Returns the the path where the buffer lives.
238 /// It is always an absolute path.
239 std::string const & filePath() const;
241 /** A transformed version of the file name, adequate for LaTeX.
242 \param no_path optional if \c true then the path is stripped.
244 std::string latexName(bool no_path = true) const;
246 /// Get thee name and type of the log.
247 std::pair<LogType, std::string> logName() const;
249 /// Change name of buffer. Updates "read-only" flag.
250 void setFileName(std::string const & newfile);
252 /// Name of the document's parent
253 void setParentName(std::string const &);
255 /** Get the document's master (or \c this if this is not a
258 Buffer const * masterBuffer() const;
259 /** Get the document's master (or \c this if this is not a
262 Buffer * masterBuffer();
264 /// Is buffer read-only?
265 bool isReadonly() const;
267 /// Set buffer read-only flag
268 void setReadonly(bool flag = true);
270 /// returns \c true if the buffer contains a LaTeX document
271 bool isLatex() const;
272 /// returns \c true if the buffer contains a DocBook document
273 bool isDocBook() const;
274 /// returns \c true if the buffer contains a Wed document
275 bool isLiterate() const;
277 /** Validate a buffer for LaTeX.
278 This validates the buffer, and returns a struct for use by
279 #makeLaTeX# and others. Its main use is to figure out what
280 commands and packages need to be included in the LaTeX file.
281 It (should) also check that the needed constructs are there
282 (i.e. that the \refs points to coresponding \labels). It
283 should perhaps inset "error" insets to help the user correct
286 void validate(LaTeXFeatures &) const;
288 /// Update the cache with all bibfiles in use (including bibfiles
289 /// of loaded child documents).
290 void updateBibfilesCache();
291 /// Return the cache with all bibfiles in use (including bibfiles
292 /// of loaded child documents).
293 std::vector<support::FileName> const & getBibfilesCache() const;
295 void getLabelList(std::vector<docstring> &) const;
298 void changeLanguage(Language const * from, Language const * to);
301 bool isMultiLingual() const;
304 BufferParams & params();
305 BufferParams const & params() const;
307 /** The list of paragraphs.
308 This is a linked list of paragraph, this list holds the
309 whole contents of the document.
311 ParagraphList & paragraphs();
312 ParagraphList const & paragraphs() const;
314 /// LyX version control object.
316 LyXVC const & lyxvc() const;
318 /// Where to put temporary files.
319 std::string const & temppath() const;
321 /// Used when typesetting to place errorboxes.
323 TexRow const & texrow() const;
326 ParIterator par_iterator_begin();
328 ParConstIterator par_iterator_begin() const;
330 ParIterator par_iterator_end();
332 ParConstIterator par_iterator_end() const;
334 /** \returns true only when the file is fully loaded.
335 * Used to prevent the premature generation of previews
336 * and by the citation inset.
338 bool isFullyLoaded() const;
339 /// Set by buffer_funcs' newFile.
340 void setFullyLoaded(bool);
342 /// Our main text (inside the top InsetText)
345 /// Our top InsetText
346 Inset & inset() const;
354 bool hasMacro(docstring const & name) const;
356 MacroData const & getMacro(docstring const & name) const;
358 void insertMacro(docstring const & name, MacroData const & data);
360 /// Replace the inset contents for insets which InsetCode is equal
361 /// to the passed \p inset_code.
362 void changeRefsIfUnique(docstring const & from, docstring const & to,
365 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
366 /// including preamble
367 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
370 /// Access to error list
371 ErrorList const & errorList(std::string const & type) const;
372 ErrorList & errorList(std::string const & type);
375 TocBackend & tocBackend();
376 TocBackend const & tocBackend() const;
380 EmbeddedFiles & embeddedFiles();
381 EmbeddedFiles const & embeddedFiles() const;
386 /// This function is called when the buffer is changed.
387 void changed() const;
388 /// This function is called when the buffer structure is changed.
389 void structureChanged() const;
390 /// This function is called when an embedded file is changed
391 void embeddingChanged() const;
392 /// This function is called when some parsing error shows up.
393 void errors(std::string const & err) const;
394 /// This function is called when the buffer busy status change.
395 void busy(bool on) const;
396 /// This function is called when the buffer readonly status change.
397 void readonly(bool on) const;
398 /// Update window titles of all users.
399 void updateTitles() const;
400 /// Reset autosave timers for all users.
401 void resetAutosaveTimers() const;
403 void message(docstring const & msg) const;
405 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
408 void autoSave() const;
410 bool writeAs(std::string const & newname = std::string());
414 void loadChildDocuments() const;
415 /// return the format of the buffer on a string
416 std::string bufferFormat() const;
419 /** Inserts a file into a document
420 \return \c false if method fails.
422 ReadStatus readFile(Lexer &, support::FileName const & filename,
423 bool fromString = false);
425 /// Use the Pimpl idiom to hide the internals.
427 /// The pointer never changes although *pimpl_'s contents may.
430 /// A cache for the bibfiles (including bibfiles of loaded child
431 /// documents), needed for appropriate update of natbib labels.
432 mutable std::vector<support::FileName> bibfilesCache_;
434 frontend::GuiBufferDelegate * gui_;