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/limited_stack.h"
22 #include "support/types.h"
23 #include "support/docstring.h"
24 #include "support/docstream.h"
26 #include <boost/scoped_ptr.hpp>
27 #include <boost/signal.hpp>
38 namespace support { class FileName; }
54 class ParConstIterator;
60 class StableDocIterator;
63 /** The buffer object.
64 * This is the buffer object. It contains all the informations about
65 * a document loaded into LyX.
66 * The buffer object owns the LyXText (wrapped in an InsetText), which
67 * contains the individual paragraphs of the document.
70 * I am not sure if the class is complete or
71 * minimal, probably not.
72 * \author Lars Gullik Bjønnes
76 /// What type of log will \c getLogName() return?
78 latexlog, ///< LaTeX log
79 buildlog ///< Literate build log
84 \param b optional \c false by default
86 explicit Buffer(std::string const & file, bool b = false);
91 /** High-level interface to buffer functionality.
92 This function parses a command string and executes it
94 bool dispatch(std::string const & command, bool * result = 0);
96 /// Maybe we know the function already by number...
97 bool dispatch(FuncRequest const & func, bool * result = 0);
99 /// Load the autosaved file.
100 void loadAutoSaveFile();
103 bool readFile(std::string const & filename);
105 /// read the header, returns number of unknown tokens
106 int readHeader(LyXLex & lex);
108 /** Reads a file without header.
109 \param par if != 0 insert the file.
110 \return \c false if file is not completely read.
112 bool readDocument(LyXLex &);
115 void insertStringAsLines(ParagraphList & plist,
116 pit_type &, pos_type &,
117 LyXFont const &, docstring const &, bool);
119 ParIterator getParFromID(int id) const;
120 /// do we have a paragraph with this id?
121 bool hasParWithID(int id) const;
123 /// This signal is emitted when the buffer is changed.
124 boost::signal<void()> changed;
125 /// This signal is emitted when some parsing error shows up.
126 boost::signal<void(std::string)> errors;
127 /// This signal is emitted when some message shows up.
128 boost::signal<void(docstring)> message;
129 /// This signal is emitted when the buffer busy status change.
130 boost::signal<void(bool)> busy;
131 /// This signal is emitted when the buffer readonly status change.
132 boost::signal<void(bool)> readonly;
133 /// Update window titles of all users.
134 boost::signal<void()> updateTitles;
135 /// Reset autosave timers for all users.
136 boost::signal<void()> resetAutosaveTimers;
137 /// This signal is emitting if the buffer is being closed.
138 boost::signal<void()> closing;
142 Takes care of auto-save files and backup file if requested.
143 Returns \c true if the save is successful, \c false otherwise.
147 /// Write file. Returns \c false if unsuccesful.
148 bool writeFile(support::FileName const &) const;
150 /// Just a wrapper for the method below, first creating the ofstream.
151 bool makeLaTeXFile(std::string const & filename,
152 std::string const & original_path,
153 OutputParams const &,
154 bool output_preamble = true,
155 bool output_body = true);
157 void writeLaTeXSource(odocstream & os,
158 std::string const & original_path,
159 OutputParams const &,
160 bool output_preamble = true,
161 bool output_body = true);
163 void makeDocBookFile(std::string const & filename,
164 OutputParams const & runparams_in,
165 bool only_body = false);
167 void writeDocBookSource(odocstream & os, std::string const & filename,
168 OutputParams const & runparams_in,
169 bool only_body = false);
170 /// returns the main language for the buffer (document)
171 Language const * getLanguage() const;
172 /// get l10n translated to the buffers language
173 docstring const B_(std::string const & l10n) const;
174 /// translate \p label to the buffer language if possible
175 docstring const translateLabel(docstring const & label) const;
179 /// return true if the main lyx file does not need saving
180 bool isClean() const;
182 bool isBakClean() const;
184 bool isDepClean(std::string const & name) const;
186 /// mark the main lyx file as not needing saving
187 void markClean() const;
193 void markDepClean(std::string const & name);
196 void setUnnamed(bool flag = true);
199 bool isUnnamed() const;
201 /// Mark this buffer as dirty.
204 /// Returns the buffer's filename. It is always an absolute path.
205 std::string const fileName() const;
207 /// Returns the the path where the buffer lives.
208 /// It is always an absolute path.
209 std::string const & filePath() const;
211 /** A transformed version of the file name, adequate for LaTeX.
212 \param no_path optional if \c true then the path is stripped.
214 std::string const getLatexName(bool no_path = true) const;
216 /// Get thee name and type of the log.
217 std::pair<LogType, std::string> const getLogName() const;
219 /// Change name of buffer. Updates "read-only" flag.
220 void setFileName(std::string const & newfile);
222 /// Name of the document's parent
223 void setParentName(std::string const &);
225 /** Get the document's master (or \c this if this is not a
228 Buffer const * getMasterBuffer() const;
229 /** Get the document's master (or \c this if this is not a
232 Buffer * getMasterBuffer();
234 /// Is buffer read-only?
235 bool isReadonly() const;
237 /// Set buffer read-only flag
238 void setReadonly(bool flag = true);
240 /// returns \c true if the buffer contains a LaTeX document
241 bool isLatex() const;
242 /// returns \c true if the buffer contains a DocBook document
243 bool isDocBook() const;
244 /// returns \c true if the buffer contains a Wed document
245 bool isLiterate() const;
247 /** Validate a buffer for LaTeX.
248 This validates the buffer, and returns a struct for use by
249 #makeLaTeX# and others. Its main use is to figure out what
250 commands and packages need to be included in the LaTeX file.
251 It (should) also check that the needed constructs are there
252 (i.e. that the \refs points to coresponding \labels). It
253 should perhaps inset "error" insets to help the user correct
256 void validate(LaTeXFeatures &) const;
258 /// return all bibkeys from buffer and its childs
259 void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
260 /// Update the cache with all bibfiles in use (including bibfiles
261 /// of loaded child documents).
262 void updateBibfilesCache();
263 /// Return the cache with all bibfiles in use (including bibfiles
264 /// of loaded child documents).
265 std::vector<std::string> const & getBibfilesCache() const;
267 void getLabelList(std::vector<docstring> &) const;
270 void changeLanguage(Language const * from, Language const * to);
273 void updateDocLang(Language const * nlang);
276 bool isMultiLingual() const;
278 /// Does this mean that this is buffer local?
279 limited_stack<Undo> & undostack();
280 limited_stack<Undo> const & undostack() const;
282 /// Does this mean that this is buffer local?
283 limited_stack<Undo> & redostack();
284 limited_stack<Undo> const & redostack() const;
287 BufferParams & params();
288 BufferParams const & params() const;
290 /** The list of paragraphs.
291 This is a linked list of paragraph, this list holds the
292 whole contents of the document.
294 ParagraphList & paragraphs();
295 ParagraphList const & paragraphs() const;
297 /// LyX version control object.
299 LyXVC const & lyxvc() const;
301 /// Where to put temporary files.
302 std::string const & temppath() const;
304 /// Used when typesetting to place errorboxes.
306 TexRow const & texrow() const;
309 ParIterator par_iterator_begin();
311 ParConstIterator par_iterator_begin() const;
313 ParIterator par_iterator_end();
315 ParConstIterator par_iterator_end() const;
317 /** \returns true only when the file is fully loaded.
318 * Used to prevent the premature generation of previews
319 * and by the citation inset.
321 bool fully_loaded() const;
322 /// Set by buffer_funcs' newFile.
323 void fully_loaded(bool);
325 /// Our main text (inside the top InsetText)
326 LyXText & text() const;
328 /// Our top InsetText!
329 InsetBase & inset() const;
337 bool hasMacro(docstring const & name) const;
339 MacroData const & getMacro(docstring const & name) const;
341 void insertMacro(docstring const & name, MacroData const & data);
344 void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
346 StableDocIterator getCursor() const { return cursor_; }
348 StableDocIterator getAnchor() const { return anchor_; }
350 void changeRefsIfUnique(docstring const & from, docstring const & to,
351 InsetBase::Code code);
352 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
353 /// including preamble
354 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
356 /// errorLists_ accessors.
358 ErrorList const & errorList(std::string const & type) const;
359 ErrorList & errorList(std::string const & type);
363 TocBackend & tocBackend();
364 TocBackend const & tocBackend() const;
368 /** Inserts a file into a document
369 \return \c false if method fails.
371 bool readFile(LyXLex &, std::string const & filename);
373 bool do_writeFile(std::ostream & ofs) const;
375 /// Use the Pimpl idiom to hide the internals.
377 /// The pointer never changes although *pimpl_'s contents may.
378 boost::scoped_ptr<Impl> const pimpl_;
380 /// Save the cursor Position on Buffer switch
381 /// this would not be needed if every Buffer would have
382 /// it's BufferView, this should be FIXED in future.
383 StableDocIterator cursor_;
384 StableDocIterator anchor_;
385 /// A cache for the bibfiles (including bibfiles of loaded child
386 /// documents), needed for appropriate update of natbib labels.
387 std::vector<std::string> bibfilesCache_;
389 /// Container for all sort of Buffer dependant errors.
390 std::map<std::string, ErrorList> errorLists_;