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>
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
83 \param b optional \c false by default
85 explicit Buffer(std::string const & file, bool b = false);
90 /** High-level interface to buffer functionality.
91 This function parses a command string and executes it
93 bool dispatch(std::string const & command, bool * result = 0);
95 /// Maybe we know the function already by number...
96 bool dispatch(FuncRequest const & func, bool * result = 0);
98 /// Load the autosaved file.
99 void loadAutoSaveFile();
102 bool readFile(std::string const & filename);
104 /// read the header, returns number of unknown tokens
105 int readHeader(LyXLex & lex);
107 /** Reads a file without header.
108 \param par if != 0 insert the file.
109 \return \c false if file is not completely read.
111 bool readDocument(LyXLex &);
114 void insertStringAsLines(ParagraphList & plist,
115 pit_type &, pos_type &,
116 LyXFont const &, docstring const &, bool);
118 ParIterator getParFromID(int id) const;
119 /// do we have a paragraph with this id?
120 bool hasParWithID(int id) const;
122 /// This signal is emitted when the buffer is changed.
123 boost::signal<void(bool)> changed;
124 /// This signal is emitted when some parsing error shows up.
125 boost::signal<void(std::string)> errors;
126 /// This signal is emitted when some message shows up.
127 boost::signal<void(docstring)> message;
128 /// This signal is emitted when the buffer busy status change.
129 boost::signal<void(bool)> busy;
130 /// This signal is emitted when the buffer readonly status change.
131 boost::signal<void(bool)> readonly;
132 /// Update window titles of all users.
133 boost::signal<void()> updateTitles;
134 /// Reset autosave timers for all users.
135 boost::signal<void()> resetAutosaveTimers;
136 /// This signal is emitting if the buffer is being closed.
137 boost::signal<void()> closing;
141 Takes care of auto-save files and backup file if requested.
142 Returns \c true if the save is successful, \c false otherwise.
146 /// Write file. Returns \c false if unsuccesful.
147 bool writeFile(std::string const &) const;
149 /// Just a wrapper for the method below, first creating the ofstream.
150 bool makeLaTeXFile(std::string const & filename,
151 std::string const & original_path,
152 OutputParams const &,
153 bool output_preamble = true,
154 bool output_body = true);
156 void writeLaTeXSource(odocstream & os,
157 std::string const & original_path,
158 OutputParams const &,
159 bool output_preamble = true,
160 bool output_body = true);
162 void makeDocBookFile(std::string const & filename,
163 OutputParams const & runparams_in,
164 bool only_body = false);
166 void writeDocBookSource(odocstream & os, std::string const & filename,
167 OutputParams const & runparams_in,
168 bool only_body = false);
169 /// returns the main language for the buffer (document)
170 Language const * getLanguage() const;
171 /// get l10n translated to the buffers language
172 docstring const B_(std::string const & l10n) const;
176 /// return true if the main lyx file does not need saving
177 bool isClean() const;
179 bool isBakClean() const;
181 bool isDepClean(std::string const & name) const;
183 /// mark the main lyx file as not needing saving
184 void markClean() const;
190 void markDepClean(std::string const & name);
193 void setUnnamed(bool flag = true);
196 bool isUnnamed() const;
198 /// Mark this buffer as dirty.
201 /// Returns the buffer's filename. It is always an absolute path.
202 std::string const & fileName() const;
204 /// Returns the the path where the buffer lives.
205 /// It is always an absolute path.
206 std::string const & filePath() const;
208 /** A transformed version of the file name, adequate for LaTeX.
209 \param no_path optional if \c true then the path is stripped.
211 std::string const getLatexName(bool no_path = true) const;
213 /// Get thee name and type of the log.
214 std::pair<LogType, std::string> const getLogName() const;
216 /// Change name of buffer. Updates "read-only" flag.
217 void setFileName(std::string const & newfile);
219 /// Name of the document's parent
220 void setParentName(std::string const &);
222 /** Get the document's master (or \c this if this is not a
225 Buffer const * getMasterBuffer() const;
226 /** Get the document's master (or \c this if this is not a
229 Buffer * getMasterBuffer();
231 /// Is buffer read-only?
232 bool isReadonly() const;
234 /// Set buffer read-only flag
235 void setReadonly(bool flag = true);
237 /// returns \c true if the buffer contains a LaTeX document
238 bool isLatex() const;
239 /// returns \c true if the buffer contains a DocBook document
240 bool isDocBook() const;
241 /// returns \c true if the buffer contains a Wed document
242 bool isLiterate() const;
244 /** Validate a buffer for LaTeX.
245 This validates the buffer, and returns a struct for use by
246 #makeLaTeX# and others. Its main use is to figure out what
247 commands and packages need to be included in the LaTeX file.
248 It (should) also check that the needed constructs are there
249 (i.e. that the \refs points to coresponding \labels). It
250 should perhaps inset "error" insets to help the user correct
253 void validate(LaTeXFeatures &) const;
255 /// return all bibkeys from buffer and its childs
256 void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
257 /// Update the cache with all bibfiles in use (including bibfiles
258 /// of loaded child documents).
259 void updateBibfilesCache();
260 /// Return the cache with all bibfiles in use (including bibfiles
261 /// of loaded child documents).
262 std::vector<std::string> const & getBibfilesCache() const;
264 void getLabelList(std::vector<docstring> &) const;
267 void changeLanguage(Language const * from, Language const * to);
270 void updateDocLang(Language const * nlang);
273 bool isMultiLingual() const;
275 /// Does this mean that this is buffer local?
276 limited_stack<Undo> & undostack();
277 limited_stack<Undo> const & undostack() const;
279 /// Does this mean that this is buffer local?
280 limited_stack<Undo> & redostack();
281 limited_stack<Undo> const & redostack() const;
284 BufferParams & params();
285 BufferParams const & params() const;
287 /** The list of paragraphs.
288 This is a linked list of paragraph, this list holds the
289 whole contents of the document.
291 ParagraphList & paragraphs();
292 ParagraphList const & paragraphs() const;
294 /// LyX version control object.
296 LyXVC const & lyxvc() const;
298 /// Where to put temporary files.
299 std::string const & temppath() const;
301 /// Used when typesetting to place errorboxes.
303 TexRow const & texrow() const;
306 ParIterator par_iterator_begin();
308 ParConstIterator par_iterator_begin() const;
310 ParIterator par_iterator_end();
312 ParConstIterator par_iterator_end() const;
314 /** \returns true only when the file is fully loaded.
315 * Used to prevent the premature generation of previews
316 * and by the citation inset.
318 bool fully_loaded() const;
319 /// Set by buffer_funcs' newFile.
320 void fully_loaded(bool);
322 /// Our main text (inside the top InsetText)
323 LyXText & text() const;
325 /// Our top InsetText!
326 InsetBase & inset() const;
334 bool hasMacro(docstring const & name) const;
336 MacroData const & getMacro(docstring const & name) const;
338 void insertMacro(docstring const & name, MacroData const & data);
341 void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
343 StableDocIterator getCursor() const { return cursor_; }
345 StableDocIterator getAnchor() const { return anchor_; }
347 void changeRefsIfUnique(docstring const & from, docstring const & to,
348 InsetBase::Code code);
349 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
350 /// including preamble
351 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end, bool full_source);
353 /// errorLists_ accessors.
355 ErrorList const & errorList(std::string const & type) const;
356 ErrorList & errorList(std::string const & type);
360 TocBackend & tocBackend();
361 TocBackend const & tocBackend() const;
365 /** Inserts a file into a document
366 \return \c false if method fails.
368 bool readFile(LyXLex &, std::string const & filename);
370 bool do_writeFile(std::ostream & ofs) const;
372 /// Use the Pimpl idiom to hide the internals.
374 /// The pointer never changes although *pimpl_'s contents may.
375 boost::scoped_ptr<Impl> const pimpl_;
377 /// Save the cursor Position on Buffer switch
378 /// this would not be needed if every Buffer would have
379 /// it's BufferView, this should be FIXED in future.
380 StableDocIterator cursor_;
381 StableDocIterator anchor_;
382 /// A cache for the bibfiles (including bibfiles of loaded child
383 /// documents), needed for appropriate update of natbib labels.
384 std::vector<std::string> bibfilesCache_;
386 /// Container for all sort of Buffer dependant errors.
387 std::map<std::string, ErrorList> errorLists_;