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 "InsetList.h"
17 #include "dociterator.h"
18 #include "ParagraphList_fwd.h"
20 #include "support/limited_stack.h"
21 #include "support/types.h"
23 #include <boost/scoped_ptr.hpp>
24 #include <boost/signal.hpp>
47 class ParConstIterator;
52 class StableDocIterator;
55 /** The buffer object.
56 * This is the buffer object. It contains all the informations about
57 * a document loaded into LyX.
58 * The buffer object owns the LyXText (wrapped in an InsetText), which
59 * contains the individual paragraphs of the document.
62 * I am not sure if the class is complete or
63 * minimal, probably not.
64 * \author Lars Gullik Bjønnes
68 /// What type of log will \c getLogName() return?
70 latexlog, ///< LaTeX log
71 buildlog ///< Literate build log
76 \param b optional \c false by default
78 explicit Buffer(std::string const & file, bool b = false);
83 /** High-level interface to buffer functionality.
84 This function parses a command string and executes it
86 bool dispatch(std::string const & command, bool * result = 0);
88 /// Maybe we know the function already by number...
89 bool dispatch(FuncRequest const & func, bool * result = 0);
91 /// Load the autosaved file.
92 void loadAutoSaveFile();
95 bool readFile(std::string const & filename);
97 /// read the header, returns number of unknown tokens
98 int readHeader(LyXLex & lex);
100 /** Reads a file without header.
101 \param par if != 0 insert the file.
102 \return \c false if file is not completely read.
104 bool readDocument(LyXLex &);
107 void insertStringAsLines(ParagraphList & plist,
108 lyx::pit_type &, lyx::pos_type &,
109 LyXFont const &, std::string const &, bool);
111 ParIterator getParFromID(int id) const;
112 /// do we have a paragraph with this id?
113 bool hasParWithID(int id) const;
115 /// This signal is emitted when a parsing error shows up.
116 boost::signal<void(ErrorItem)> error;
117 /// This signal is emitted when some message shows up.
118 boost::signal<void(std::string)> message;
119 /// This signal is emitted when the buffer busy status change.
120 boost::signal<void(bool)> busy;
121 /// This signal is emitted when the buffer readonly status change.
122 boost::signal<void(bool)> readonly;
123 /// Update window titles of all users.
124 boost::signal<void()> updateTitles;
125 /// Reset autosave timers for all users.
126 boost::signal<void()> resetAutosaveTimers;
127 /// This signal is emitting if the buffer is being closed.
128 boost::signal<void()> closing;
132 Takes care of auto-save files and backup file if requested.
133 Returns \c true if the save is successful, \c false otherwise.
137 /// Write file. Returns \c false if unsuccesful.
138 bool writeFile(std::string const &) const;
141 void writeFileAscii(std::string const &, OutputParams const &);
143 void writeFileAscii(std::ostream &, OutputParams const &);
144 /// Just a wrapper for the method below, first creating the ofstream.
145 void makeLaTeXFile(std::string const & filename,
146 std::string const & original_path,
147 OutputParams const &,
148 bool output_preamble = true,
149 bool output_body = true);
151 void makeLaTeXFile(std::ostream & os,
152 std::string const & original_path,
153 OutputParams const &,
154 bool output_preamble = true,
155 bool output_body = true);
157 void makeLinuxDocFile(std::string const & filename,
158 OutputParams const & runparams_in,
159 bool only_body = false);
161 void makeDocBookFile(std::string const & filename,
162 OutputParams const & runparams_in,
163 bool only_body = false);
164 /// returns the main language for the buffer (document)
165 Language const * getLanguage() const;
166 /// get l10n translated to the buffers language
167 std::string const B_(std::string const & l10n) const;
171 /// return true if the main lyx file does not need saving
172 bool isClean() const;
174 bool isBakClean() const;
176 bool isDepClean(std::string const & name) const;
178 /// mark the main lyx file as not needing saving
179 void markClean() const;
185 void markDepClean(std::string const & name);
188 void setUnnamed(bool flag = true);
191 bool isUnnamed() const;
193 /// Mark this buffer as dirty.
196 /// Returns the buffer's filename. It is always an absolute path.
197 std::string const & fileName() const;
199 /// Returns the the path where the buffer lives.
200 /// It is always an absolute path.
201 std::string const & filePath() const;
203 /** A transformed version of the file name, adequate for LaTeX.
204 \param no_path optional if \c true then the path is stripped.
206 std::string const getLatexName(bool no_path = true) const;
208 /// Get the name and type of the log.
209 std::pair<LogType, std::string> const getLogName() const;
211 /// Change name of buffer. Updates "read-only" flag.
212 void setFileName(std::string const & newfile);
214 /// Name of the document's parent
215 void setParentName(std::string const &);
217 /** Get the document's master (or \c this if this is not a
220 Buffer const * getMasterBuffer() const;
221 /** Get the document's master (or \c this if this is not a
224 Buffer * getMasterBuffer();
226 /// Is buffer read-only?
227 bool isReadonly() const;
229 /// Set buffer read-only flag
230 void setReadonly(bool flag = true);
232 /// returns \c true if the buffer contains a LaTeX document
233 bool isLatex() const;
234 /// returns \c true if the buffer contains a LinuxDoc document
235 bool isLinuxDoc() const;
236 /// returns \c true if the buffer contains a DocBook document
237 bool isDocBook() const;
238 /** returns \c true if the buffer contains either a LinuxDoc
239 or DocBook document */
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<std::string> &) 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(std::string const & name) const;
336 MacroData const & getMacro(std::string const & name) const;
338 void insertMacro(std::string const & name, MacroData const & data);
340 void saveCursor(StableDocIterator cursor, StableDocIterator anchor);
342 StableDocIterator getCursor() const { return cursor_; }
344 StableDocIterator getAnchor() const { return anchor_; }
346 void changeRefsIfUnique(std::string const & from, std::string const & to);
347 /// get source code (latex/docbook/linuxdoc) for some paragraphs
348 void getSourceCode(std::ostream & os, lyx::pit_type par_begin, lyx::pit_type par_end);
351 /** Inserts a file into a document
352 \return \c false if method fails.
354 bool readFile(LyXLex &, std::string const & filename);
356 bool do_writeFile(std::ostream & ofs) const;
358 /// Use the Pimpl idiom to hide the internals.
360 /// The pointer never changes although *pimpl_'s contents may.
361 boost::scoped_ptr<Impl> const pimpl_;
363 /// Save the cursor Position on Buffer switch
364 /// this would not be needed if every Buffer would have
365 /// it's BufferView, this should be FIXED in future.
366 StableDocIterator cursor_;
367 StableDocIterator anchor_;
368 /// A cache for the bibfiles (including bibfiles of loaded child
369 /// documents), needed for appropriate update of natbib labels.
370 std::vector<std::string> bibfilesCache_;