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 "errorlist.h"
16 #include "InsetList.h"
18 #include "dociterator.h"
19 #include "ParagraphList_fwd.h"
21 #include "support/filename.h"
22 #include "support/limited_stack.h"
23 #include "support/types.h"
24 #include "support/docstring.h"
25 #include "support/docstream.h"
27 #include <boost/scoped_ptr.hpp>
28 #include <boost/signal.hpp>
53 class ParConstIterator;
59 class StableDocIterator;
62 /** The buffer object.
63 * This is the buffer object. It contains all the informations about
64 * a document loaded into LyX.
65 * The buffer object owns the LyXText (wrapped in an InsetText), which
66 * contains the individual paragraphs of the document.
69 * I am not sure if the class is complete or
70 * minimal, probably not.
71 * \author Lars Gullik Bjønnes
75 /// What type of log will \c getLogName() return?
77 latexlog, ///< LaTeX log
78 buildlog ///< Literate build log
81 /// Result of \c readFile()
83 failure, ///< The file could not be read
84 success, ///< The file could not be read
85 wrongversion ///< The version of the file does not match ours
90 \param b optional \c false by default
92 explicit Buffer(std::string const & file, bool b = false);
97 /** High-level interface to buffer functionality.
98 This function parses a command string and executes it
100 bool dispatch(std::string const & command, bool * result = 0);
102 /// Maybe we know the function already by number...
103 bool dispatch(FuncRequest const & func, bool * result = 0);
105 /// Load the autosaved file.
106 void loadAutoSaveFile();
108 /// read a new document from a string
109 bool readString(std::string const &);
111 bool readFile(support::FileName const & filename);
113 /// read the header, returns number of unknown tokens
114 int readHeader(LyXLex & lex);
116 /** Reads a file without header.
117 \param par if != 0 insert the file.
118 \return \c false if file is not completely read.
120 bool readDocument(LyXLex &);
123 void insertStringAsLines(ParagraphList & plist,
124 pit_type &, pos_type &,
125 LyXFont const &, docstring const &, bool);
127 ParIterator getParFromID(int id) const;
128 /// do we have a paragraph with this id?
129 bool hasParWithID(int id) const;
131 /// This signal is emitted when the buffer is changed.
132 boost::signal<void()> changed;
133 /// This signal is emitted when some parsing error shows up.
134 boost::signal<void(std::string)> errors;
135 /// This signal is emitted when some message shows up.
136 boost::signal<void(docstring)> message;
137 /// This signal is emitted when the buffer busy status change.
138 boost::signal<void(bool)> busy;
139 /// This signal is emitted when the buffer readonly status change.
140 boost::signal<void(bool)> readonly;
141 /// Update window titles of all users.
142 boost::signal<void()> updateTitles;
143 /// Reset autosave timers for all users.
144 boost::signal<void()> resetAutosaveTimers;
145 /// This signal is emitting if the buffer is being closed.
146 boost::signal<void()> closing;
150 Takes care of auto-save files and backup file if requested.
151 Returns \c true if the save is successful, \c false otherwise.
155 /// Write document to stream. Returns \c false if unsuccesful.
156 bool write(std::ostream &) const;
157 /// Write file. Returns \c false if unsuccesful.
158 bool writeFile(support::FileName const &) const;
160 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
161 bool makeLaTeXFile(support::FileName const & filename,
162 std::string const & original_path,
163 OutputParams const &,
164 bool output_preamble = true,
165 bool output_body = true);
166 /** Export the buffer to LaTeX.
167 If \p os is a file stream, and params().inputenc is "auto" or
168 "default", and the buffer contains text in different languages
169 with more than one encoding, then this method will change the
170 encoding associated to \p os. Therefore you must not call this
171 method with a string stream if the output is supposed to go to a
174 ofs.open("test.tex");
175 writeLaTeXSource(ofs, ...);
177 \endcode is NOT equivalent to \code
178 odocstringstream oss;
179 writeLaTeXSource(oss, ...);
181 ofs.open("test.tex");
186 void writeLaTeXSource(odocstream & os,
187 std::string const & original_path,
188 OutputParams const &,
189 bool output_preamble = true,
190 bool output_body = true);
192 void makeDocBookFile(support::FileName const & filename,
193 OutputParams const & runparams_in,
194 bool only_body = false);
196 void writeDocBookSource(odocstream & os, std::string const & filename,
197 OutputParams const & runparams_in,
198 bool only_body = false);
199 /// returns the main language for the buffer (document)
200 Language const * getLanguage() const;
201 /// get l10n translated to the buffers language
202 docstring const B_(std::string const & l10n) const;
203 /// translate \p label to the buffer language if possible
204 docstring const translateLabel(docstring const & label) const;
208 /// return true if the main lyx file does not need saving
209 bool isClean() const;
211 bool isBakClean() const;
213 bool isDepClean(std::string const & name) const;
215 /// mark the main lyx file as not needing saving
216 void markClean() const;
222 void markDepClean(std::string const & name);
225 void setUnnamed(bool flag = true);
228 bool isUnnamed() const;
230 /// Mark this buffer as dirty.
233 /// Returns the buffer's filename. It is always an absolute path.
234 std::string const fileName() const;
236 /// Returns the the path where the buffer lives.
237 /// It is always an absolute path.
238 std::string const & filePath() const;
240 /** A transformed version of the file name, adequate for LaTeX.
241 \param no_path optional if \c true then the path is stripped.
243 std::string const getLatexName(bool no_path = true) const;
245 /// Get thee name and type of the log.
246 std::pair<LogType, std::string> const getLogName() const;
248 /// Change name of buffer. Updates "read-only" flag.
249 void setFileName(std::string const & newfile);
251 /// Name of the document's parent
252 void setParentName(std::string const &);
254 /** Get the document's master (or \c this if this is not a
257 Buffer const * getMasterBuffer() const;
258 /** Get the document's master (or \c this if this is not a
261 Buffer * getMasterBuffer();
263 /// Is buffer read-only?
264 bool isReadonly() const;
266 /// Set buffer read-only flag
267 void setReadonly(bool flag = true);
269 /// returns \c true if the buffer contains a LaTeX document
270 bool isLatex() const;
271 /// returns \c true if the buffer contains a DocBook document
272 bool isDocBook() const;
273 /// returns \c true if the buffer contains a Wed document
274 bool isLiterate() const;
276 /** Validate a buffer for LaTeX.
277 This validates the buffer, and returns a struct for use by
278 #makeLaTeX# and others. Its main use is to figure out what
279 commands and packages need to be included in the LaTeX file.
280 It (should) also check that the needed constructs are there
281 (i.e. that the \refs points to coresponding \labels). It
282 should perhaps inset "error" insets to help the user correct
285 void validate(LaTeXFeatures &) const;
287 /// return all bibkeys from buffer and its childs
288 void fillWithBibKeys(std::vector<std::pair<std::string, docstring> > & keys) const;
289 /// Update the cache with all bibfiles in use (including bibfiles
290 /// of loaded child documents).
291 void updateBibfilesCache();
292 /// Return the cache with all bibfiles in use (including bibfiles
293 /// of loaded child documents).
294 std::vector<support::FileName> const & getBibfilesCache() const;
296 void getLabelList(std::vector<docstring> &) const;
299 void changeLanguage(Language const * from, Language const * to);
302 void updateDocLang(Language const * nlang);
305 bool isMultiLingual() const;
307 /// Does this mean that this is buffer local?
308 limited_stack<Undo> & undostack();
309 limited_stack<Undo> const & undostack() const;
311 /// Does this mean that this is buffer local?
312 limited_stack<Undo> & redostack();
313 limited_stack<Undo> const & redostack() const;
316 BufferParams & params();
317 BufferParams const & params() const;
319 /** The list of paragraphs.
320 This is a linked list of paragraph, this list holds the
321 whole contents of the document.
323 ParagraphList & paragraphs();
324 ParagraphList const & paragraphs() const;
326 /// LyX version control object.
328 LyXVC const & lyxvc() const;
330 /// Where to put temporary files.
331 std::string const & temppath() const;
333 /// Used when typesetting to place errorboxes.
335 TexRow const & texrow() const;
338 ParIterator par_iterator_begin();
340 ParConstIterator par_iterator_begin() const;
342 ParIterator par_iterator_end();
344 ParConstIterator par_iterator_end() const;
346 /** \returns true only when the file is fully loaded.
347 * Used to prevent the premature generation of previews
348 * and by the citation inset.
350 bool fully_loaded() const;
351 /// Set by buffer_funcs' newFile.
352 void fully_loaded(bool);
354 /// Our main text (inside the top InsetText)
355 LyXText & text() const;
357 /// Our top InsetText!
358 InsetBase & inset() const;
366 bool hasMacro(docstring const & name) const;
368 MacroData const & getMacro(docstring const & name) const;
370 void insertMacro(docstring const & name, MacroData const & data);
373 void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
375 StableDocIterator getCursor() const { return cursor_; }
377 StableDocIterator getAnchor() const { return anchor_; }
379 void changeRefsIfUnique(docstring const & from, docstring const & to,
380 InsetBase::Code code);
381 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
382 /// including preamble
383 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
385 /// errorLists_ accessors.
387 ErrorList const & errorList(std::string const & type) const;
388 ErrorList & errorList(std::string const & type);
392 TocBackend & tocBackend();
393 TocBackend const & tocBackend() const;
397 /** Inserts a file into a document
398 \return \c false if method fails.
400 ReadStatus readFile(LyXLex &, support::FileName const & filename,
401 bool fromString = false);
403 /// Use the Pimpl idiom to hide the internals.
405 /// The pointer never changes although *pimpl_'s contents may.
406 boost::scoped_ptr<Impl> const pimpl_;
408 /// Save the cursor Position on Buffer switch
409 /// this would not be needed if every Buffer would have
410 /// it's BufferView, this should be FIXED in future.
411 StableDocIterator cursor_;
412 StableDocIterator anchor_;
413 /// A cache for the bibfiles (including bibfiles of loaded child
414 /// documents), needed for appropriate update of natbib labels.
415 mutable std::vector<support::FileName> bibfilesCache_;
417 /// Container for all sort of Buffer dependant errors.
418 std::map<std::string, ErrorList> errorLists_;