Consider the case where there is not any layout name.
[lyx.git] / src / buffer.h
blob26ae13f928b9d2914202f56bb5a1d8f99331867e
1 // -*- C++ -*-
2 /**
3 * \file buffer.h
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.
12 #ifndef BUFFER_H
13 #define BUFFER_H
15 #include "InsetList.h"
17 #include "support/limited_stack.h"
18 #include "support/types.h"
20 #include <boost/scoped_ptr.hpp>
21 #include <boost/signal.hpp>
23 #include <iosfwd>
24 #include <map>
25 #include <utility>
26 #include <vector>
29 class BufferParams;
30 class ErrorItem;
31 class FuncRequest;
32 class InsetBase;
33 class InsetText;
34 class LyXFont;
35 class LyXLex;
36 class LyXRC;
37 class LyXText;
38 class LyXVC;
39 class LaTeXFeatures;
40 class Language;
41 class MacroData;
42 class OutputParams;
43 class ParagraphList;
44 class ParConstIterator;
45 class ParIterator;
46 class TeXErrors;
47 class TexRow;
48 class Undo;
51 /** The buffer object.
52 * This is the buffer object. It contains all the informations about
53 * a document loaded into LyX.
54 * The buffer object owns the LyXText (wrapped in an InsetText), which
55 * contains the individual paragraphs of the document.
58 * I am not sure if the class is complete or
59 * minimal, probably not.
60 * \author Lars Gullik Bjønnes
62 class Buffer {
63 public:
64 /// What type of log will \c getLogName() return?
65 enum LogType {
66 latexlog, ///< LaTeX log
67 buildlog ///< Literate build log
70 /** Constructor
71 \param file
72 \param b optional \c false by default
74 explicit Buffer(std::string const & file, bool b = false);
76 /// Destructor
77 ~Buffer();
79 /** High-level interface to buffer functionality.
80 This function parses a command string and executes it
82 bool dispatch(std::string const & command, bool * result = 0);
84 /// Maybe we know the function already by number...
85 bool dispatch(FuncRequest const & func, bool * result = 0);
87 /// Load the autosaved file.
88 void loadAutoSaveFile();
90 /// load a new file
91 bool readFile(std::string const & filename);
93 bool readFile(std::string const & filename, lyx::pit_type pit);
95 /// read the header, returns number of unknown tokens
96 int readHeader(LyXLex & lex);
98 /** Reads a file without header.
99 \param par if != 0 insert the file.
100 \return \c false if file is not completely read.
102 bool readDocument(LyXLex &);
105 void insertStringAsLines(ParagraphList & plist,
106 lyx::pit_type &, lyx::pos_type &,
107 LyXFont const &, std::string const &, bool);
109 ParIterator getParFromID(int id) const;
110 /// do we have a paragraph with this id?
111 bool hasParWithID(int id) const;
113 /// This signal is emitted when a parsing error shows up.
114 boost::signal<void(ErrorItem)> error;
115 /// This signal is emitted when some message shows up.
116 boost::signal<void(std::string)> message;
117 /// This signal is emitted when the buffer busy status change.
118 boost::signal<void(bool)> busy;
119 /// This signal is emitted when the buffer readonly status change.
120 boost::signal<void(bool)> readonly;
121 /// Update window titles of all users.
122 boost::signal<void()> updateTitles;
123 /// Reset autosave timers for all users.
124 boost::signal<void()> resetAutosaveTimers;
125 /// This signal is emitting if the buffer is being closed.
126 boost::signal<void()> closing;
129 /** Save file.
130 Takes care of auto-save files and backup file if requested.
131 Returns \c true if the save is successful, \c false otherwise.
133 bool save() const;
135 /// Write file. Returns \c false if unsuccesful.
136 bool writeFile(std::string const &) const;
139 void writeFileAscii(std::string const &, OutputParams const &);
141 void writeFileAscii(std::ostream &, OutputParams const &);
142 /// Just a wrapper for the method below, first creating the ofstream.
143 void makeLaTeXFile(std::string const & filename,
144 std::string const & original_path,
145 OutputParams const &,
146 bool output_preamble = true,
147 bool output_body = true);
149 void makeLaTeXFile(std::ostream & os,
150 std::string const & original_path,
151 OutputParams const &,
152 bool output_preamble = true,
153 bool output_body = true);
155 void makeLinuxDocFile(std::string const & filename,
156 OutputParams const & runparams_in,
157 bool only_body = false);
159 void makeDocBookFile(std::string const & filename,
160 OutputParams const & runparams_in,
161 bool only_body = false);
162 /// returns the main language for the buffer (document)
163 Language const * getLanguage() const;
164 /// get l10n translated to the buffers language
165 std::string const B_(std::string const & l10n) const;
168 int runChktex();
169 /// return true if the main lyx file does not need saving
170 bool isClean() const;
172 bool isBakClean() const;
174 bool isDepClean(std::string const & name) const;
176 /// mark the main lyx file as not needing saving
177 void markClean() const;
180 void markBakClean();
183 void markDepClean(std::string const & name);
186 void setUnnamed(bool flag = true);
189 bool isUnnamed() const;
191 /// Mark this buffer as dirty.
192 void markDirty();
194 /// Returns the buffer's filename. It is always an absolute path.
195 std::string const & fileName() const;
197 /// Returns the the path where the buffer lives.
198 /// It is always an absolute path.
199 std::string const & filePath() const;
201 /** A transformed version of the file name, adequate for LaTeX.
202 \param no_path optional if \c true then the path is stripped.
204 std::string const getLatexName(bool no_path = true) const;
206 /// Get the name and type of the log.
207 std::pair<LogType, std::string> const getLogName() const;
209 /// Change name of buffer. Updates "read-only" flag.
210 void setFileName(std::string const & newfile);
212 /// Name of the document's parent
213 void setParentName(std::string const &);
215 /** Get the document's master (or \c this if this is not a
216 child document)
218 Buffer const * getMasterBuffer() const;
220 /// Is buffer read-only?
221 bool isReadonly() const;
223 /// Set buffer read-only flag
224 void setReadonly(bool flag = true);
226 /// returns \c true if the buffer contains a LaTeX document
227 bool isLatex() const;
228 /// returns \c true if the buffer contains a LinuxDoc document
229 bool isLinuxDoc() const;
230 /// returns \c true if the buffer contains a DocBook document
231 bool isDocBook() const;
232 /** returns \c true if the buffer contains either a LinuxDoc
233 or DocBook document */
234 bool isSGML() const;
235 /// returns \c true if the buffer contains a Wed document
236 bool isLiterate() const;
238 /** Validate a buffer for LaTeX.
239 This validates the buffer, and returns a struct for use by
240 #makeLaTeX# and others. Its main use is to figure out what
241 commands and packages need to be included in the LaTeX file.
242 It (should) also check that the needed constructs are there
243 (i.e. that the \refs points to coresponding \labels). It
244 should perhaps inset "error" insets to help the user correct
245 obvious mistakes.
247 void validate(LaTeXFeatures &) const;
249 /// return all bibkeys from buffer and its childs
250 void fillWithBibKeys(std::vector<std::pair<std::string, std::string> > & keys) const;
252 void getLabelList(std::vector<std::string> &) const;
255 void changeLanguage(Language const * from, Language const * to);
258 void updateDocLang(Language const * nlang);
261 bool isMultiLingual() const;
263 /// Does this mean that this is buffer local?
264 limited_stack<Undo> & undostack();
265 limited_stack<Undo> const & undostack() const;
267 /// Does this mean that this is buffer local?
268 limited_stack<Undo> & redostack();
269 limited_stack<Undo> const & redostack() const;
272 BufferParams & params();
273 BufferParams const & params() const;
275 /** The list of paragraphs.
276 This is a linked list of paragraph, this list holds the
277 whole contents of the document.
279 ParagraphList & paragraphs();
280 ParagraphList const & paragraphs() const;
282 /// LyX version control object.
283 LyXVC & lyxvc();
284 LyXVC const & lyxvc() const;
286 /// Where to put temporary files.
287 std::string const & temppath() const;
289 /// Used when typesetting to place errorboxes.
290 TexRow & texrow();
291 TexRow const & texrow() const;
294 ParIterator par_iterator_begin();
296 ParConstIterator par_iterator_begin() const;
298 ParIterator par_iterator_end();
300 ParConstIterator par_iterator_end() const;
302 /** \returns true only when the file is fully loaded.
303 * Used to prevent the premature generation of previews
304 * and by the citation inset.
306 bool fully_loaded() const;
307 /// Set by buffer_funcs' newFile.
308 void fully_loaded(bool);
311 LyXText & text() const;
313 InsetBase & inset() const;
316 // Macro handling
319 void buildMacros();
321 bool hasMacro(std::string const & name) const;
323 MacroData const & getMacro(std::string const & name) const;
325 void insertMacro(std::string const & name, MacroData const & data);
327 private:
328 /** Inserts a file into a document
329 \param par if != 0 insert the file.
330 \return \c false if method fails.
332 bool readFile(LyXLex &, std::string const & filename,
333 lyx::pit_type pit);
335 bool do_writeFile(std::ostream & ofs) const;
337 /// Use the Pimpl idiom to hide the internals.
338 class Impl;
339 /// The pointer never changes although *pimpl_'s contents may.
340 boost::scoped_ptr<Impl> const pimpl_;
343 #endif