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 "DocIterator.h"
17 #include "support/FileName.h"
18 #include "support/limited_stack.h"
19 #include "support/types.h"
20 #include "support/docstring.h"
21 #include "support/docstream.h"
23 #include <boost/signal.hpp>
48 class ParConstIterator;
57 class WorkAreaManager;
60 /** The buffer object.
61 * This is the buffer object. It contains all the informations about
62 * a document loaded into LyX.
63 * The buffer object owns the Text (wrapped in an InsetText), which
64 * contains the individual paragraphs of the document.
67 * I am not sure if the class is complete or
68 * minimal, probably not.
69 * \author Lars Gullik Bjønnes
73 /// What type of log will \c getLogName() return?
75 latexlog, ///< LaTeX log
76 buildlog ///< Literate build log
79 /// Result of \c readFile()
81 failure, ///< The file could not be read
82 success, ///< The file could not be read
83 wrongversion ///< The version of the file does not match ours
86 /// Method to check if a file is externally modified, used by
87 /// isExternallyModified()
89 * timestamp is fast but inaccurate. For example, the granularity
90 * of timestamp on a FAT filesystem is 2 second. Also, various operations
91 * may touch the timestamp of a file even when its content is unchanged.
93 * checksum is accurate but slow, which can be a problem when it is
94 * frequently used, or used for a large file on a slow (network) file
98 checksum_method, ///< Use file check sum
99 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
104 \param b optional \c false by default
106 explicit Buffer(std::string const & file, bool b = false);
111 /** High-level interface to buffer functionality.
112 This function parses a command string and executes it
114 bool dispatch(std::string const & command, bool * result = 0);
116 /// Maybe we know the function already by number...
117 bool dispatch(FuncRequest const & func, bool * result = 0);
119 /// Load the autosaved file.
120 void loadAutoSaveFile();
122 /// read a new document from a string
123 bool readString(std::string const &);
125 bool readFile(support::FileName const & filename);
127 /// read the header, returns number of unknown tokens
128 int readHeader(Lexer & lex);
130 /** Reads a file without header.
131 \param par if != 0 insert the file.
132 \return \c false if file is not completely read.
134 bool readDocument(Lexer &);
137 void insertStringAsLines(ParagraphList & plist,
138 pit_type &, pos_type &,
139 Font const &, docstring const &, bool);
141 ParIterator getParFromID(int id) const;
142 /// do we have a paragraph with this id?
143 bool hasParWithID(int id) const;
145 /// This signal is emitted when the buffer is changed.
149 frontend::WorkAreaManager * workAreaManager() const;
151 /// This signal is emitted when the buffer structure is changed.
152 boost::signal<void()> structureChanged;
153 /// This signal is emitted when an embedded file is changed
154 boost::signal<void()> embeddingChanged;
155 /// This signal is emitted when some parsing error shows up.
156 boost::signal<void(std::string)> errors;
157 /// This signal is emitted when some message shows up.
158 boost::signal<void(docstring)> message;
159 /// This signal is emitted when the buffer busy status change.
160 boost::signal<void(bool)> busy;
161 /// This signal is emitted when the buffer readonly status change.
162 boost::signal<void(bool)> readonly;
163 /// Update window titles of all users.
164 boost::signal<void()> updateTitles;
165 /// Reset autosave timers for all users.
166 boost::signal<void()> resetAutosaveTimers;
170 Takes care of auto-save files and backup file if requested.
171 Returns \c true if the save is successful, \c false otherwise.
175 /// Write document to stream. Returns \c false if unsuccesful.
176 bool write(std::ostream &) const;
177 /// Write file. Returns \c false if unsuccesful.
178 bool writeFile(support::FileName const &) const;
180 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
181 bool makeLaTeXFile(support::FileName const & filename,
182 std::string const & original_path,
183 OutputParams const &,
184 bool output_preamble = true,
185 bool output_body = true);
186 /** Export the buffer to LaTeX.
187 If \p os is a file stream, and params().inputenc is "auto" or
188 "default", and the buffer contains text in different languages
189 with more than one encoding, then this method will change the
190 encoding associated to \p os. Therefore you must not call this
191 method with a string stream if the output is supposed to go to a
194 ofs.open("test.tex");
195 writeLaTeXSource(ofs, ...);
197 \endcode is NOT equivalent to \code
198 odocstringstream oss;
199 writeLaTeXSource(oss, ...);
201 ofs.open("test.tex");
206 void writeLaTeXSource(odocstream & os,
207 std::string const & original_path,
208 OutputParams const &,
209 bool output_preamble = true,
210 bool output_body = true);
212 void makeDocBookFile(support::FileName const & filename,
213 OutputParams const & runparams_in,
214 bool only_body = false);
216 void writeDocBookSource(odocstream & os, std::string const & filename,
217 OutputParams const & runparams_in,
218 bool only_body = false);
219 /// returns the main language for the buffer (document)
220 Language const * getLanguage() const;
221 /// get l10n translated to the buffers language
222 docstring const B_(std::string const & l10n) const;
226 /// return true if the main lyx file does not need saving
227 bool isClean() const;
229 bool isBakClean() const;
231 bool isDepClean(std::string const & name) const;
233 /// whether or not disk file has been externally modified
234 bool isExternallyModified(CheckMethod method) const;
236 /// save timestamp and checksum of the given file.
237 void saveCheckSum(std::string const & file) const;
239 /// mark the main lyx file as not needing saving
240 void markClean() const;
246 void markDepClean(std::string const & name);
249 void setUnnamed(bool flag = true);
252 bool isUnnamed() const;
254 /// Mark this buffer as dirty.
257 /// Returns the buffer's filename. It is always an absolute path.
258 std::string const fileName() const;
260 /// Returns the the path where the buffer lives.
261 /// It is always an absolute path.
262 std::string const & filePath() const;
264 /** A transformed version of the file name, adequate for LaTeX.
265 \param no_path optional if \c true then the path is stripped.
267 std::string const getLatexName(bool no_path = true) const;
269 /// Get thee name and type of the log.
270 std::pair<LogType, std::string> const getLogName() const;
272 /// Change name of buffer. Updates "read-only" flag.
273 void setFileName(std::string const & newfile);
275 /// Name of the document's parent
276 void setParentName(std::string const &);
278 /** Get the document's master (or \c this if this is not a
281 Buffer const * getMasterBuffer() const;
282 /** Get the document's master (or \c this if this is not a
285 Buffer * getMasterBuffer();
287 /// Is buffer read-only?
288 bool isReadonly() const;
290 /// Set buffer read-only flag
291 void setReadonly(bool flag = true);
293 /// returns \c true if the buffer contains a LaTeX document
294 bool isLatex() const;
295 /// returns \c true if the buffer contains a DocBook document
296 bool isDocBook() const;
297 /// returns \c true if the buffer contains a Wed document
298 bool isLiterate() const;
300 /** Validate a buffer for LaTeX.
301 This validates the buffer, and returns a struct for use by
302 #makeLaTeX# and others. Its main use is to figure out what
303 commands and packages need to be included in the LaTeX file.
304 It (should) also check that the needed constructs are there
305 (i.e. that the \refs points to coresponding \labels). It
306 should perhaps inset "error" insets to help the user correct
309 void validate(LaTeXFeatures &) const;
311 /// Update the cache with all bibfiles in use (including bibfiles
312 /// of loaded child documents).
313 void updateBibfilesCache();
314 /// Return the cache with all bibfiles in use (including bibfiles
315 /// of loaded child documents).
316 std::vector<support::FileName> const & getBibfilesCache() const;
318 void getLabelList(std::vector<docstring> &) const;
321 void changeLanguage(Language const * from, Language const * to);
324 bool isMultiLingual() const;
326 /// Does this mean that this is buffer local?
327 limited_stack<Undo> & undostack();
328 limited_stack<Undo> const & undostack() const;
330 /// Does this mean that this is buffer local?
331 limited_stack<Undo> & redostack();
332 limited_stack<Undo> const & redostack() const;
335 BufferParams & params();
336 BufferParams const & params() const;
338 /** The list of paragraphs.
339 This is a linked list of paragraph, this list holds the
340 whole contents of the document.
342 ParagraphList & paragraphs();
343 ParagraphList const & paragraphs() const;
345 /// LyX version control object.
347 LyXVC const & lyxvc() const;
349 /// Where to put temporary files.
350 std::string const & temppath() const;
352 /// Used when typesetting to place errorboxes.
354 TexRow const & texrow() const;
357 ParIterator par_iterator_begin();
359 ParConstIterator par_iterator_begin() const;
361 ParIterator par_iterator_end();
363 ParConstIterator par_iterator_end() const;
365 /** \returns true only when the file is fully loaded.
366 * Used to prevent the premature generation of previews
367 * and by the citation inset.
369 bool fully_loaded() const;
370 /// Set by buffer_funcs' newFile.
371 void fully_loaded(bool);
373 /// Our main text (inside the top InsetText)
376 /// Our top InsetText!
377 Inset & inset() const;
385 bool hasMacro(docstring const & name) const;
387 MacroData const & getMacro(docstring const & name) const;
389 void insertMacro(docstring const & name, MacroData const & data);
392 void changeRefsIfUnique(docstring const & from, docstring const & to,
394 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
395 /// including preamble
396 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
398 /// errorLists_ accessors.
400 ErrorList const & errorList(std::string const & type) const;
401 ErrorList & errorList(std::string const & type);
405 TocBackend & tocBackend();
406 TocBackend const & tocBackend() const;
410 EmbeddedFiles & embeddedFiles();
411 EmbeddedFiles const & embeddedFiles() const;
415 /** Inserts a file into a document
416 \return \c false if method fails.
418 ReadStatus readFile(Lexer &, support::FileName const & filename,
419 bool fromString = false);
421 /// Use the Pimpl idiom to hide the internals.
423 /// The pointer never changes although *pimpl_'s contents may.
426 /// A cache for the bibfiles (including bibfiles of loaded child
427 /// documents), needed for appropriate update of natbib labels.
428 mutable std::vector<support::FileName> bibfilesCache_;