]> git.lyx.org Git - lyx.git/blob - src/buffer.h
Make buffer's member variables private; use accessor functions.
[lyx.git] / src / buffer.h
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.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef BUFFER_H
13 #define BUFFER_H
14
15 #include "bufferparams.h"
16 #include "InsetList.h"
17 #include "lyxvc.h"
18 #include "ParagraphList_fwd.h"
19 #include "texrow.h"
20
21 #include "support/limited_stack.h"
22 #include "support/types.h"
23
24 #include <boost/scoped_ptr.hpp>
25 #include <boost/signals/signal0.hpp>
26 #include <boost/signals/signal1.hpp>
27
28 #include "support/std_string.h"
29 #include <iosfwd>
30 #include <map>
31 #include <utility>
32 #include <vector>
33
34
35 class AuthorList;
36 class ErrorItem;
37 class LyXRC;
38 class LaTeXFeatures;
39 class LatexRunParams;
40 class Language;
41 class Messages;
42 class ParIterator;
43 class ParConstIterator;
44 class TeXErrors;
45 class Undo;
46
47
48 /** The buffer object.
49   This is the buffer object. It contains all the informations about
50   a document loaded into LyX. I am not sure if the class is complete or
51   minimal, probably not.
52   \author Lars Gullik Bjønnes
53   */
54 class Buffer {
55 public:
56         /// What type of log will \c getLogName() return?
57         enum LogType {
58                 latexlog, ///< LaTeX log
59                 buildlog  ///< Literate build log
60         };
61
62         /** Constructor
63             \param file
64             \param b  optional \c false by default
65         */
66         explicit Buffer(string const & file, bool b = false);
67
68         /// Destructor
69         ~Buffer();
70
71         /** High-level interface to buffer functionality.
72             This function parses a command string and executes it
73         */
74         bool dispatch(string const & command, bool * result = 0);
75
76         /// Maybe we know the function already by number...
77         bool dispatch(int ac, string const & argument, bool * result = 0);
78
79         /// Load the autosaved file.
80         void loadAutoSaveFile();
81
82         /// load a new file
83         bool readFile(string const & filename);
84
85         bool readFile(string const & filename, ParagraphList::iterator pit);
86
87         /// read the header, returns number of unknown tokens
88         int readHeader(LyXLex & lex);
89
90         /** Reads a file without header.
91             \param par if != 0 insert the file.
92             \return \c false if file is not completely read.
93         */
94         bool readBody(LyXLex &, ParagraphList::iterator pit);
95
96         /// This parses a single token
97         int readParagraph(LyXLex &, string const & token,
98                           ParagraphList & pars,
99                           ParagraphList::iterator & pit,
100                           lyx::depth_type & depth);
101
102         ///
103         void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &,
104                                  LyXFont const &, string const &);
105         ///
106         ParIterator getParFromID(int id) const;
107         /// do we have a paragraph with this id?
108         bool hasParWithID(int id) const;
109
110         /// This signal is emitted when a parsing error shows up.
111         boost::signal1<void, ErrorItem> error;
112         /// This signal is emitted when some message shows up.
113         boost::signal1<void, string> message;
114         /// This signal is emitted when the buffer busy status change.
115         boost::signal1<void, bool> busy;
116         /// This signal is emitted when the buffer readonly status change.
117         boost::signal1<void, bool> readonly;
118         /// Update window titles of all users.
119         boost::signal0<void> updateTitles;
120         /// Reset autosave timers for all users.
121         boost::signal0<void> resetAutosaveTimers;
122         /// This signal is emitting if the buffer is being closed.
123         boost::signal0<void> closing;
124
125
126         /** Save file.
127             Takes care of auto-save files and backup file if requested.
128             Returns \c true if the save is successful, \c false otherwise.
129         */
130         bool save() const;
131
132         /// Write file. Returns \c false if unsuccesful.
133         bool writeFile(string const &) const;
134
135         ///
136         void writeFileAscii(string const &, int);
137         ///
138         void writeFileAscii(std::ostream &, int);
139         ///
140         string const asciiParagraph(Paragraph const &, unsigned int linelen,
141                                     bool noparbreak = false) const;
142         /// Just a wrapper for the method below, first creating the ofstream.
143         void makeLaTeXFile(string const & filename,
144                            string const & original_path,
145                            LatexRunParams const &,
146                            bool output_preamble = true,
147                            bool output_body = true);
148         ///
149         void makeLaTeXFile(std::ostream & os,
150                            string const & original_path,
151                            LatexRunParams const &,
152                            bool output_preamble = true,
153                            bool output_body = true);
154         ///
155         void simpleDocBookOnePar(std::ostream &,
156                                  ParagraphList::iterator par,
157                                  int & desc_on,
158                                  lyx::depth_type depth) const;
159         ///
160         void simpleLinuxDocOnePar(std::ostream & os,
161                                   ParagraphList::iterator par,
162                                   lyx::depth_type depth) const;
163         ///
164         void makeLinuxDocFile(string const & filename,
165                               bool nice, bool only_body = false);
166         ///
167         void makeDocBookFile(string const & filename,
168                              bool nice, 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         string const B_(string const & l10n) const;
173
174         ///
175         int runChktex();
176         /// return true if the main lyx file does not need saving
177         bool isClean() const;
178         ///
179         bool isBakClean() const;
180         ///
181         bool isDepClean(string const & name) const;
182
183         /// mark the main lyx file as not needing saving
184         void markClean() const;
185
186         ///
187         void markBakClean();
188
189         ///
190         void markDepClean(string const & name);
191
192         ///
193         void setUnnamed(bool flag = true);
194
195         ///
196         bool isUnnamed();
197
198         /// Mark this buffer as dirty.
199         void markDirty();
200
201         /// Returns the buffer's filename. It is always an absolute path.
202         string const & fileName() const;
203
204         /// Returns the the path where the buffer lives.
205         /// It is always an absolute path.
206         string const & filePath() const;
207
208         /** A transformed version of the file name, adequate for LaTeX.
209             \param no_path optional if \c true then the path is stripped.
210         */
211         string const getLatexName(bool no_path = true) const;
212
213         /// Get the name and type of the log.
214         std::pair<LogType, string> const getLogName() const;
215
216         /// Change name of buffer. Updates "read-only" flag.
217         void setFileName(string const & newfile);
218
219         /// Name of the document's parent
220         void setParentName(string const &);
221
222         /// Is buffer read-only?
223         bool isReadonly() const;
224
225         /// Set buffer read-only flag
226         void setReadonly(bool flag = true);
227
228         /// returns \c true if the buffer contains a LaTeX document
229         bool isLatex() const;
230         /// returns \c true if the buffer contains a LinuxDoc document
231         bool isLinuxDoc() const;
232         /// returns \c true if the buffer contains a DocBook document
233         bool isDocBook() const;
234         /** returns \c true if the buffer contains either a LinuxDoc
235             or DocBook document */
236         bool isSGML() const;
237         /// returns \c true if the buffer contains a Wed document
238         bool isLiterate() const;
239
240         /** Validate a buffer for LaTeX.
241             This validates the buffer, and returns a struct for use by
242             #makeLaTeX# and others. Its main use is to figure out what
243             commands and packages need to be included in the LaTeX file.
244             It (should) also check that the needed constructs are there
245             (i.e. that the \refs points to coresponding \labels). It
246             should perhaps inset "error" insets to help the user correct
247             obvious mistakes.
248         */
249         void validate(LaTeXFeatures &) const;
250
251         /// return all bibkeys from buffer and its childs
252         void fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const;
253         ///
254         void getLabelList(std::vector<string> &) const;
255
256         ///
257         void changeLanguage(Language const * from, Language const * to);
258
259         ///
260         void updateDocLang(Language const * nlang);
261
262         ///
263         bool isMultiLingual();
264
265         /// Does this mean that this is buffer local?
266         limited_stack<Undo> & undostack();
267         limited_stack<Undo> const & undostack() const;
268
269         /// Does this mean that this is buffer local?
270         limited_stack<Undo> & redostack();
271         limited_stack<Undo> const & redostack() const;
272
273         ///
274         BufferParams & params();
275         BufferParams const & params() const;
276
277         /** The list of paragraphs.
278             This is a linked list of paragraph, this list holds the
279             whole contents of the document.
280          */
281         ParagraphList & paragraphs();
282         ParagraphList const & paragraphs() const;
283
284         /// LyX version control object.
285         LyXVC & lyxvc();
286         LyXVC const & lyxvc() const;
287
288         /// Where to put temporary files.
289         string const & temppath() const;
290
291         /** If we are writing a nice LaTeX file or not.
292             While writing as LaTeX, tells whether we are
293             doing a 'nice' LaTeX file */
294         bool & niceFile();
295         bool niceFile() const;
296
297         /// Used when typesetting to place errorboxes.
298         TexRow & texrow();
299         TexRow const & texrow() const;
300
301         /// the author list for the document
302         AuthorList & authors();
303
304 private:
305         /** Inserts a file into a document
306             \param par if != 0 insert the file.
307             \return \c false if method fails.
308         */
309         bool readFile(LyXLex &, string const & filename,
310                       ParagraphList::iterator pit);
311
312         bool do_writeFile(std::ostream & ofs) const;
313
314         limited_stack<Undo> undostack_;
315         limited_stack<Undo> redostack_;
316         BufferParams params_;
317         ParagraphList paragraphs_;
318         LyXVC lyxvc_;
319         string temppath_;
320         bool nicefile_;
321         TexRow texrow_;
322
323         typedef std::map<string, bool> DepClean;
324
325         /// need to regenerate .tex ?
326         DepClean dep_clean_;
327
328         /// is save needed
329         mutable bool lyx_clean;
330
331         /// is autosave needed
332         mutable bool bak_clean;
333
334         /// is this a unnamed file (New...)
335         bool unnamed;
336
337         /// buffer is r/o
338         bool read_only;
339
340         /// name of the file the buffer is associated with.
341         string filename_;
342
343         /// The path to the document file.
344         string filepath_;
345
346         ///
347         boost::scoped_ptr<Messages> messages_;
348 public:
349         ///
350         class inset_iterator {
351         public:
352                 typedef std::input_iterator_tag iterator_category;
353                 typedef InsetOld value_type;
354                 typedef ptrdiff_t difference_type;
355                 typedef InsetOld * pointer;
356                 typedef InsetOld & reference;
357                 typedef ParagraphList::iterator base_type;
358
359                 ///
360                 inset_iterator();
361                 ///
362                 inset_iterator(base_type p, base_type e);
363                 ///
364                 inset_iterator(base_type p, lyx::pos_type pos, base_type e);
365
366                 /// prefix ++
367                 inset_iterator & operator++();
368                 /// postfix ++
369                 inset_iterator operator++(int);
370                 ///
371                 reference operator*();
372                 ///
373                 pointer operator->();
374
375                 ///
376                 ParagraphList::iterator getPar() const;
377                 ///
378                 lyx::pos_type getPos() const;
379                 ///
380                 friend
381                 bool operator==(inset_iterator const & iter1,
382                                 inset_iterator const & iter2);
383         private:
384                 ///
385                 void setParagraph();
386                 ///
387                 ParagraphList::iterator pit;
388                 ///
389                 ParagraphList::iterator pend;
390                 ///
391                 InsetList::iterator it;
392         };
393
394         /// return an iterator to all *top-level* insets in the buffer
395         inset_iterator inset_iterator_begin();
396
397         /// return the end of all *top-level* insets in the buffer
398         inset_iterator inset_iterator_end();
399
400         /// return a const iterator to all *top-level* insets in the buffer
401         inset_iterator inset_const_iterator_begin() const;
402
403         /// return the const end of all *top-level* insets in the buffer
404         inset_iterator inset_const_iterator_end() const;
405
406         ///
407         ParIterator par_iterator_begin();
408         ///
409         ParConstIterator par_iterator_begin() const;
410         ///
411         ParIterator par_iterator_end();
412         ///
413         ParConstIterator par_iterator_end() const;
414
415         ///
416         InsetOld * getInsetFromID(int id_arg) const;
417 };
418
419 bool operator==(Buffer::inset_iterator const & iter1,
420                 Buffer::inset_iterator const & iter2);
421
422 bool operator!=(Buffer::inset_iterator const & iter1,
423                 Buffer::inset_iterator const & iter2);
424
425 #endif