* lyx_1_6.py:
[lyx.git] / src / Buffer.h
blobdd1a2b4c84501335a376cd99074322e5ac81146f
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 "update_flags.h"
17 #include "insets/InsetCode.h"
19 #include "support/strfwd.h"
20 #include "support/types.h"
21 #include "support/SignalSlot.h"
23 #include <string>
24 #include <vector>
27 namespace lyx {
29 class BiblioInfo;
30 class BufferParams;
31 class BufferSet;
32 class DispatchResult;
33 class DocIterator;
34 class docstring_list;
35 class ErrorItem;
36 class ErrorList;
37 class FuncRequest;
38 class FuncStatus;
39 class Inset;
40 class InsetRef;
41 class InsetLabel;
42 class Font;
43 class Format;
44 class Lexer;
45 class LyXRC;
46 class Text;
47 class LyXVC;
48 class LaTeXFeatures;
49 class Language;
50 class MacroData;
51 class MacroNameSet;
52 class MacroSet;
53 class OutputParams;
54 class Paragraph;
55 class ParConstIterator;
56 class ParIterator;
57 class ParagraphList;
58 class TeXErrors;
59 class TexRow;
60 class TocBackend;
61 class Undo;
62 class WordLangTuple;
64 namespace frontend {
65 class GuiBufferDelegate;
66 class WorkAreaManager;
69 namespace support {
70 class FileName;
71 class FileNameList;
74 /** The buffer object.
75 * This is the buffer object. It contains all the informations about
76 * a document loaded into LyX.
77 * The buffer object owns the Text (wrapped in an InsetText), which
78 * contains the individual paragraphs of the document.
81 * I am not sure if the class is complete or
82 * minimal, probably not.
83 * \author Lars Gullik Bjønnes
85 class Buffer {
86 public:
87 /// What type of log will \c getLogName() return?
88 enum LogType {
89 latexlog, ///< LaTeX log
90 buildlog ///< Literate build log
93 /// Result of \c readFile()
94 enum ReadStatus {
95 failure, ///< The file could not be read
96 success, ///< The file could not be read
97 wrongversion ///< The version of the file does not match ours
101 /// Method to check if a file is externally modified, used by
102 /// isExternallyModified()
104 * timestamp is fast but inaccurate. For example, the granularity
105 * of timestamp on a FAT filesystem is 2 second. Also, various operations
106 * may touch the timestamp of a file even when its content is unchanged.
108 * checksum is accurate but slow, which can be a problem when it is
109 * frequently used, or used for a large file on a slow (network) file
110 * system.
112 * FIXME: replace this method with support/FileMonitor.
114 enum CheckMethod {
115 checksum_method, ///< Use file checksum
116 timestamp_method, ///< Use timestamp, and checksum if timestamp has changed
120 enum UpdateScope {
121 UpdateMaster,
122 UpdateChildOnly
125 /// Constructor
126 explicit Buffer(std::string const & file, bool b = false);
128 /// Destructor
129 ~Buffer();
131 /** High-level interface to buffer functionality.
132 This function parses a command string and executes it.
134 void dispatch(std::string const & command, DispatchResult & result);
136 /// Maybe we know the function already by number...
137 void dispatch(FuncRequest const & func, DispatchResult & result);
139 /// Can this function be exectued?
140 /// \return true if we made a decision
141 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
143 /// read a new document from a string
144 bool readString(std::string const &);
145 /// load a new file
146 bool readFile(support::FileName const & filename);
148 /// read the header, returns number of unknown tokens
149 int readHeader(Lexer & lex);
151 /** Reads a file without header.
152 \param par if != 0 insert the file.
153 \return \c true if file is not completely read.
155 bool readDocument(Lexer &);
158 void insertStringAsLines(ParagraphList & plist,
159 pit_type &, pos_type &,
160 Font const &, docstring const &, bool);
162 DocIterator getParFromID(int id) const;
163 /// do we have a paragraph with this id?
164 bool hasParWithID(int id) const;
167 frontend::WorkAreaManager & workAreaManager() const;
169 /** Save file.
170 Takes care of auto-save files and backup file if requested.
171 Returns \c true if the save is successful, \c false otherwise.
173 bool save() const;
175 /// Write document to stream. Returns \c false if unsuccesful.
176 bool write(std::ostream &) const;
177 /// Write file. Returns \c false if unsuccesful.
178 bool writeFile(support::FileName const &) const;
180 /// Loads LyX file \c filename into buffer, * and \return success
181 bool loadLyXFile(support::FileName const & s);
183 /// Fill in the ErrorList with the TeXErrors
184 void bufferErrors(TeXErrors const &, ErrorList &) const;
186 /// Just a wrapper for writeLaTeXSource, first creating the ofstream.
187 bool makeLaTeXFile(support::FileName const & filename,
188 std::string const & original_path,
189 OutputParams const &,
190 bool output_preamble = true,
191 bool output_body = true) const;
192 /** Export the buffer to LaTeX.
193 If \p os is a file stream, and params().inputenc is "auto" or
194 "default", and the buffer contains text in different languages
195 with more than one encoding, then this method will change the
196 encoding associated to \p os. Therefore you must not call this
197 method with a string stream if the output is supposed to go to a
198 file. \code
199 ofdocstream ofs;
200 ofs.open("test.tex");
201 writeLaTeXSource(ofs, ...);
202 ofs.close();
203 \endcode is NOT equivalent to \code
204 odocstringstream oss;
205 writeLaTeXSource(oss, ...);
206 ofdocstream ofs;
207 ofs.open("test.tex");
208 ofs << oss.str();
209 ofs.close();
210 \endcode
212 void writeLaTeXSource(odocstream & os,
213 std::string const & original_path,
214 OutputParams const &,
215 bool output_preamble = true,
216 bool output_body = true) const;
218 void makeDocBookFile(support::FileName const & filename,
219 OutputParams const & runparams_in,
220 bool only_body = false) const;
222 void writeDocBookSource(odocstream & os, std::string const & filename,
223 OutputParams const & runparams_in,
224 bool only_body = false) const;
225 /// returns the main language for the buffer (document)
226 Language const * language() const;
227 /// get l10n translated to the buffers language
228 docstring const B_(std::string const & l10n) const;
231 int runChktex();
232 /// return true if the main lyx file does not need saving
233 bool isClean() const;
235 bool isBakClean() const;
237 bool isDepClean(std::string const & name) const;
239 /// whether or not disk file has been externally modified
240 bool isExternallyModified(CheckMethod method) const;
242 /// save timestamp and checksum of the given file.
243 void saveCheckSum(support::FileName const & file) const;
245 /// mark the main lyx file as not needing saving
246 void markClean() const;
249 void markBakClean() const;
252 void markDepClean(std::string const & name);
255 void setUnnamed(bool flag = true);
258 bool isUnnamed() const;
260 /// Mark this buffer as dirty.
261 void markDirty();
263 /// Returns the buffer's filename. It is always an absolute path.
264 support::FileName fileName() const;
266 /// Returns the buffer's filename. It is always an absolute path.
267 std::string absFileName() const;
269 /// Returns the the path where the buffer lives.
270 /// It is always an absolute path.
271 std::string filePath() const;
273 /** A transformed version of the file name, adequate for LaTeX.
274 \param no_path optional if \c true then the path is stripped.
276 std::string latexName(bool no_path = true) const;
278 /// Get thee name and type of the log.
279 std::string logName(LogType * type = 0) const;
281 /// Change name of buffer. Updates "read-only" flag.
282 void setFileName(std::string const & newfile);
284 /// Set document's parent Buffer.
285 void setParent(Buffer const *);
286 Buffer const * parent() const;
288 // Collect all relative buffer
289 std::vector<Buffer const *> allRelatives() const;
291 /** Get the document's master (or \c this if this is not a
292 child document)
294 Buffer const * masterBuffer() const;
296 /// \return true if \p child is a child of this \c Buffer.
297 bool isChild(Buffer * child) const;
299 /// return a vector with all children and grandchildren
300 std::vector<Buffer *> getChildren() const;
302 /// Is buffer read-only?
303 bool isReadonly() const;
305 /// Set buffer read-only flag
306 void setReadonly(bool flag = true);
308 /// returns \c true if the buffer contains a LaTeX document
309 bool isLatex() const;
310 /// returns \c true if the buffer contains a DocBook document
311 bool isDocBook() const;
312 /// returns \c true if the buffer contains a Wed document
313 bool isLiterate() const;
315 /** Validate a buffer for LaTeX.
316 This validates the buffer, and returns a struct for use by
317 #makeLaTeX# and others. Its main use is to figure out what
318 commands and packages need to be included in the LaTeX file.
319 It (should) also check that the needed constructs are there
320 (i.e. that the \refs points to coresponding \labels). It
321 should perhaps inset "error" insets to help the user correct
322 obvious mistakes.
324 void validate(LaTeXFeatures &) const;
326 /// Update the cache with all bibfiles in use (including bibfiles
327 /// of loaded child documents).
328 void updateBibfilesCache(UpdateScope scope = UpdateMaster) const;
330 void invalidateBibinfoCache();
331 /// Return the cache with all bibfiles in use (including bibfiles
332 /// of loaded child documents).
333 support::FileNameList const &
334 getBibfilesCache(UpdateScope scope = UpdateMaster) const;
335 /// \return the bibliography information for this buffer's master,
336 /// or just for it, if it isn't a child.
337 BiblioInfo const & masterBibInfo() const;
338 /// \return the bibliography information for this buffer ONLY.
339 BiblioInfo const & localBibInfo() const;
341 void getLabelList(std::vector<docstring> &) const;
344 void changeLanguage(Language const * from, Language const * to);
347 bool isMultiLingual() const;
350 BufferParams & params();
351 BufferParams const & params() const;
353 /** The list of paragraphs.
354 This is a linked list of paragraph, this list holds the
355 whole contents of the document.
357 ParagraphList & paragraphs();
358 ParagraphList const & paragraphs() const;
360 /// LyX version control object.
361 LyXVC & lyxvc();
362 LyXVC const & lyxvc() const;
364 /// Where to put temporary files.
365 std::string const temppath() const;
367 /// Used when typesetting to place errorboxes.
368 TexRow const & texrow() const;
369 TexRow & texrow();
372 ParIterator par_iterator_begin();
374 ParConstIterator par_iterator_begin() const;
376 ParIterator par_iterator_end();
378 ParConstIterator par_iterator_end() const;
380 // Position of the child buffer where it appears first in the master.
381 DocIterator firstChildPosition(Buffer const * child);
383 /** \returns true only when the file is fully loaded.
384 * Used to prevent the premature generation of previews
385 * and by the citation inset.
387 bool isFullyLoaded() const;
388 /// Set by buffer_funcs' newFile.
389 void setFullyLoaded(bool);
391 /// Our main text (inside the top InsetText)
392 Text & text() const;
394 /// Our top InsetText
395 Inset & inset() const;
398 // Macro handling
400 /// Collect macro definitions in paragraphs
401 void updateMacros() const;
402 /// Iterate through the whole buffer and try to resolve macros
403 void updateMacroInstances() const;
405 /// List macro names of this buffer, the parent and the children
406 void listMacroNames(MacroNameSet & macros) const;
407 /// Collect macros of the parent and its children in front of this buffer.
408 void listParentMacros(MacroSet & macros, LaTeXFeatures & features) const;
410 /// Return macro defined before pos (or in the master buffer)
411 MacroData const * getMacro(docstring const & name, DocIterator const & pos, bool global = true) const;
412 /// Return macro defined anywhere in the buffer (or in the master buffer)
413 MacroData const * getMacro(docstring const & name, bool global = true) const;
414 /// Return macro defined before the inclusion of the child
415 MacroData const * getMacro(docstring const & name, Buffer const & child, bool global = true) const;
417 /// Replace the inset contents for insets which InsetCode is equal
418 /// to the passed \p inset_code.
419 void changeRefsIfUnique(docstring const & from, docstring const & to,
420 InsetCode code);
422 /// get source code (latex/docbook) for some paragraphs, or all paragraphs
423 /// including preamble
424 void getSourceCode(odocstream & os, pit_type par_begin, pit_type par_end,
425 bool full_source) const;
427 /// Access to error list.
428 /// This method is used only for GUI visualisation of Buffer related
429 /// errors (like parsing or LateX compilation). This method is const
430 /// because modifying the returned ErrorList does not touch the document
431 /// contents.
432 ErrorList & errorList(std::string const & type) const;
434 /// The Toc backend.
435 /// This is useful only for screen visualisation of the Buffer. This
436 /// method is const because modifying this backend does not touch
437 /// the document contents.
438 TocBackend & tocBackend() const;
441 Undo & undo();
443 /// This function is called when the buffer is changed.
444 void changed() const;
446 void updateTocItem(std::string const &, DocIterator const &) const;
447 /// This function is called when the buffer structure is changed.
448 void structureChanged() const;
449 /// This function is called when some parsing error shows up.
450 void errors(std::string const & err) const;
451 /// This function is called when the buffer busy status change.
452 void setBusy(bool on) const;
453 /// This function is called when the buffer readonly status change.
454 void setReadOnly(bool on) const;
455 /// Update window titles of all users.
456 void updateTitles() const;
457 /// Reset autosave timers for all users.
458 void resetAutosaveTimers() const;
460 void message(docstring const & msg) const;
463 void setGuiDelegate(frontend::GuiBufferDelegate * gui);
465 bool hasGuiDelegate() const;
468 void autoSave() const;
470 void removeAutosaveFile() const;
472 /// return the format of the buffer on a string
473 std::string bufferFormat() const;
474 /// return the default output format of the current backend
475 std::string getDefaultOutputFormat() const;
478 bool doExport(std::string const & format, bool put_in_tempdir,
479 std::string & result_file) const;
481 bool doExport(std::string const & format, bool put_in_tempdir) const;
483 bool preview(std::string const & format) const;
485 bool isExportable(std::string const & format) const;
487 std::vector<Format const *> exportableFormats(bool only_viewable) const;
490 typedef std::vector<std::pair<InsetRef *, ParIterator> > References;
491 References & references(docstring const & label);
492 References const & references(docstring const & label) const;
493 void clearReferenceCache() const;
494 void setInsetLabel(docstring const & label, InsetLabel const * il);
495 InsetLabel const * insetLabel(docstring const & label) const;
497 /// sets the buffer_ member for every inset in this buffer.
498 // FIXME This really shouldn't be needed, but at the moment it's not
499 // clear how to do it just for the individual pieces we need.
500 void setBuffersForInsets() const;
502 void updateLabels(UpdateScope = UpdateMaster) const;
504 void updateLabels(ParIterator & parit) const;
506 /// Find next word starting from \p from.
507 /// \p from initial position to search, will then points to the next
508 /// word.
509 /// \p to will points to the end of the next word.
510 /// \p word will contain the found word if any.
511 /// \return true if a new word was found.
512 bool nextWord(DocIterator & from, DocIterator & to,
513 docstring & word) const;
515 int spellCheck(DocIterator & from, DocIterator & to,
516 WordLangTuple & word_lang, docstring_list & suggestions) const;
518 private:
519 /// search for macro in local (buffer) table or in children
520 MacroData const * getBufferMacro(docstring const & name,
521 DocIterator const & pos) const;
522 /** Update macro table starting with position of it
523 \param it in some text inset
525 void updateMacros(DocIterator & it,
526 DocIterator & scope) const;
529 void collectRelatives(BufferSet & bufs) const;
532 bool readFileHelper(support::FileName const & s);
534 std::vector<std::string> backends() const;
535 /** Inserts a file into a document
536 \return \c false if method fails.
538 ReadStatus readFile(Lexer &, support::FileName const & filename,
539 bool fromString = false);
541 support::FileName getAutosaveFilename() const;
543 /// Use the Pimpl idiom to hide the internals.
544 class Impl;
545 /// The pointer never changes although *pimpl_'s contents may.
546 Impl * const d;
548 frontend::GuiBufferDelegate * gui_;
550 /// This function is called when the buffer structure is changed.
551 Signal structureChanged_;
552 /// This function is called when some parsing error shows up.
553 //Signal errors(std::string const &) = 0;
554 /// This function is called when some message shows up.
555 //Signal message(docstring const &) = 0;
556 /// This function is called when the buffer busy status change.
557 //Signal setBusy(bool) = 0;
558 /// Reset autosave timers for all users.
559 Signal resetAutosaveTimers_;
563 } // namespace lyx
565 #endif