]> git.lyx.org Git - lyx.git/blob - src/buffer.h
spellchecker cleanup - fscked knows why pspell is broken, I am TOTALLY
[lyx.git] / src / buffer.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  *
5  *           LyX, The Document Processor
6  *           Copyright 1995 Matthias Ettrich
7  *
8  *           This file is Copyleft 1996
9  *           Lars Gullik Bjønnes
10  *
11  * ====================================================== */
12
13 #ifndef BUFFER_H
14 #define BUFFER_H
15
16 #ifdef __GNUG__
17 #pragma interface
18 #endif
19
20 #include "LString.h"
21 #include "undo.h"
22 #include "support/limited_stack.h"
23
24 #include "lyxvc.h"
25 #include "bufferparams.h"
26 #include "texrow.h"
27 #include "paragraph.h"
28
29 #include <boost/shared_ptr.hpp>
30
31 class BufferView;
32 class LyXRC;
33 class TeXErrors;
34 class LaTeXFeatures;
35 class Language;
36 class ParIterator;
37
38 #define NO_COMPABILITY 1
39
40 ///
41 struct DEPCLEAN {
42         ///
43         bool clean;
44         ///
45         string master;
46         ///
47         DEPCLEAN * next;
48 };
49
50 /** The buffer object.
51   This is the buffer object. It contains all the informations about
52   a document loaded into LyX. I am not sure if the class is complete or
53   minimal, probably not.
54   \author Lars Gullik Bjønnes
55   */
56 class Buffer {
57 public:
58         /// What type of log will \c getLogName() return?
59         enum LogType {
60                 latexlog, ///< LaTeX log
61                 buildlog  ///< Literate build log
62         };
63
64         /** Constructor
65             \param file
66             \param b  optional \c false by default
67         */
68         explicit Buffer(string const & file, bool b = false);
69
70         /// Destructor
71         ~Buffer();
72
73         /** High-level interface to buffer functionality.
74             This function parses a command string and executes it
75         */
76         bool dispatch(string const & command, bool * result = 0);
77
78         /// Maybe we know the function already by number...
79         bool dispatch(int ac, string const & argument, bool * result = 0);
80
81         ///
82         void resizeInsets(BufferView *);
83
84         /// Update window titles of all users.
85         void updateTitles() const;
86
87         /// Reset autosave timers for all users.
88         void resetAutosaveTimers() const;
89
90         /** Adds the BufferView to the users list.
91             Later this func will insert the \c BufferView into a real list,
92             not just setting a pointer.
93         */
94         void addUser(BufferView * u);
95
96         /** Removes the #BufferView# from the users list.
97             Since we only can have one at the moment, we just reset it.
98         */
99         void delUser(BufferView *);
100
101         ///
102         void redraw();
103
104         /// Load the autosaved file.
105         void loadAutoSaveFile();
106
107         /** Reads a file.
108             \param par if != 0 insert the file.
109             \return \c false if method fails.
110         */
111         bool readFile(LyXLex &, Paragraph * par = 0);
112
113         /** Reads a file without header.
114             \param par if != 0 insert the file.
115             \return \c false if file is not completely read.
116         */
117         bool readLyXformat2(LyXLex &, Paragraph * par = 0);
118
119         /// This parses a single LyXformat-Token.
120         bool parseSingleLyXformat2Token(LyXLex &, Paragraph *& par,
121                                         Paragraph *& return_par,
122                                         string const & token, int & pos,
123                                         Paragraph::depth_type & depth,
124                                         LyXFont &);
125         ///
126         void insertStringAsLines(Paragraph *&, lyx::pos_type &,
127                                  LyXFont const &, string const &) const;
128 #ifndef NO_COMPABILITY
129         ///
130         Inset * isErtInset(Paragraph * par, int pos) const;
131         ///
132         void insertErtContents(Paragraph * par, int & pos,
133                                bool set_inactive = true);
134 #endif
135         ///
136         Paragraph * getParFromID(int id) const;
137 private:
138         /// Parse a single inset.
139         void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
140 public:
141         /** Save file.
142             Takes care of auto-save files and backup file if requested.
143             Returns \c true if the save is successful, \c false otherwise.
144         */
145         bool save() const;
146
147         /// Write file. Returns \c false if unsuccesful.
148         bool writeFile(string const &) const;
149
150         ///
151         void writeFileAscii(string const & , int);
152         ///
153         void writeFileAscii(std::ostream &, int);
154         ///
155         string const asciiParagraph(Paragraph const *, unsigned int linelen,
156                                     bool noparbreak = false) const;
157         /// Just a wrapper for the method below, first creating the ofstream.
158         void makeLaTeXFile(string const & filename,
159                            string const & original_path,
160                            bool nice,
161                            bool only_body = false,
162                            bool only_preamble = false);
163         ///
164         void makeLaTeXFile(std::ostream & os,
165                            string const & original_path,
166                            bool nice,
167                            bool only_body = false,
168                            bool only_preamble = false);
169         /** LaTeX all paragraphs from par to endpar.
170             \param \a endpar if == 0 then to the end
171         */
172         void latexParagraphs(std::ostream & os, Paragraph * par,
173                              Paragraph * endpar, TexRow & texrow, bool moving_arg = false) const;
174         ///
175         void simpleDocBookOnePar(std::ostream &,
176                                  Paragraph * par, int & desc_on,
177                                  Paragraph::depth_type depth) const ;
178         ///
179         void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par,
180                                   Paragraph::depth_type depth);
181         ///
182         void makeLinuxDocFile(string const & filename,
183                               bool nice, bool only_body = false);
184         ///
185         void makeDocBookFile(string const & filename,
186                              bool nice, bool only_body = false);
187         /// Open SGML/XML tag.
188         int sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
189                 string const & latexname) const;
190         /// Closes SGML/XML tag.
191         int sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth, bool mixcont,
192                 string const & latexname) const;
193         ///
194         void sgmlError(Paragraph * par, int pos, string const & message) const;
195
196         /// returns the main language for the buffer (document)
197         Language const * getLanguage() const;
198         ///
199         int runChktex();
200         /// return true if the main lyx file does not need saving
201         bool isClean() const;
202         ///
203         bool isBakClean() const;
204         ///
205         bool isDepClean(string const & name) const;
206
207         /// mark the main lyx file as not needing saving
208         void markClean() const;
209
210         ///
211         void markBakClean();
212
213         ///
214         void markDepClean(string const & name);
215
216         ///
217         void setUnnamed(bool flag = true);
218
219         ///
220         bool isUnnamed();
221
222         /// Mark this buffer as dirty.
223         void markDirty();
224
225         /// Returns the buffer's filename. It is always an absolute path.
226         string const & fileName() const;
227
228         /// Returns the the path where the buffer lives.
229         /// It is always an absolute path.
230         string const & filePath() const;
231
232         /** A transformed version of the file name, adequate for LaTeX.
233             \param no_path optional if \c true then the path is stripped.
234         */
235         string const getLatexName(bool no_path = true) const;
236
237         /// Get the name and type of the log.
238         std::pair<LogType, string> const getLogName() const;
239
240         /// Change name of buffer. Updates "read-only" flag.
241         void setFileName(string const & newfile);
242
243         /// Name of the document's parent
244         void setParentName(string const &);
245
246         /// Is buffer read-only?
247         bool isReadonly() const;
248
249         /// Set buffer read-only flag
250         void setReadonly(bool flag = true);
251
252         /// returns \c true if the buffer contains a LaTeX document
253         bool isLatex() const;
254         /// returns \c true if the buffer contains a LinuxDoc document
255         bool isLinuxDoc() const;
256         /// returns \c true if the buffer contains a DocBook document
257         bool isDocBook() const;
258         /** returns \c true if the buffer contains either a LinuxDoc
259             or DocBook document */
260         bool isSGML() const;
261         /// returns \c true if the buffer contains a Wed document
262         bool isLiterate() const;
263
264         /** Validate a buffer for LaTeX.
265             This validates the buffer, and returns a struct for use by
266             #makeLaTeX# and others. Its main use is to figure out what
267             commands and packages need to be included in the LaTeX file.
268             It (should) also check that the needed constructs are there
269             (i.e. that the \refs points to coresponding \labels). It
270             should perhaps inset "error" insets to help the user correct
271             obvious mistakes.
272         */
273         void validate(LaTeXFeatures &) const;
274
275         ///
276         string const getIncludeonlyList(char delim = ',');
277         ///
278         std::vector<std::pair<string, string> > const getBibkeyList() const;
279         ///
280         std::vector<string> const getLabelList();
281
282         /** This will clearly have to change later. Later we can have more
283             than one user per buffer. */
284         BufferView * getUser() const;
285
286         ///
287         void changeLanguage(Language const * from, Language const * to);
288         ///
289         bool isMultiLingual();
290
291         /// Does this mean that this is buffer local?
292         limited_stack<boost::shared_ptr<Undo> > undostack;
293
294         /// Does this mean that this is buffer local?
295         limited_stack<boost::shared_ptr<Undo> > redostack;
296
297         ///
298         BufferParams params;
299
300         /** The list of paragraphs.
301             This is a linked list of paragraph, this list holds the
302             whole contents of the document.
303          */
304         Paragraph * paragraph;
305
306         /// LyX version control object.
307         LyXVC lyxvc;
308
309         /// Where to put temporary files.
310         string tmppath;
311
312         /** If we are writing a nice LaTeX file or not.
313             While writing as LaTeX, tells whether we are
314             doing a 'nice' LaTeX file */
315         bool niceFile;
316
317         /// Used when typesetting to place errorboxes.
318         TexRow texrow;
319 private:
320         /// is save needed
321         mutable bool lyx_clean;
322
323         /// is autosave needed
324         mutable bool bak_clean;
325
326         /// is this a unnamed file (New...)
327         bool unnamed;
328
329         /// is regenerating #.tex# necessary
330         DEPCLEAN * dep_clean;
331
332         /// buffer is r/o
333         bool read_only;
334
335         /// name of the file the buffer is associated with.
336         string filename_;
337
338         /// The path to the document file.
339         string filepath_;
340
341         /// Format number of buffer
342         int file_format;
343         /** A list of views using this buffer.
344             Why not keep a list of the BufferViews that use this buffer?
345
346             At least then we don't have to do a lot of magic like:
347             #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
348             of the buffers in the list of users to do a #updateLayoutChoice#.
349         */
350         BufferView * users;
351
352 public:
353         ///
354         class inset_iterator {
355         public:
356                 typedef std::input_iterator_tag iterator_category;
357                 typedef Inset value_type;
358                 typedef ptrdiff_t difference_type;
359                 typedef Inset * pointer;
360                 typedef Inset & reference;
361
362
363                 ///
364                 inset_iterator() : par(0) /*, it(0)*/ {}
365                 //
366                 inset_iterator(Paragraph * paragraph) : par(paragraph) {
367                         setParagraph();
368                 }
369                 ///
370                 inset_iterator(Paragraph * paragraph, lyx::pos_type pos);
371                 ///
372                 inset_iterator & operator++() { // prefix ++
373                         if (par) {
374                                 ++it;
375                                 if (it == par->inset_iterator_end()) {
376                                         par = par->next();
377                                         setParagraph();
378                                 }
379                         }
380                         return *this;
381                 }
382                 ///
383                 inset_iterator operator++(int) { // postfix ++
384                         inset_iterator tmp(par, it.getPos());
385                         if (par) {
386                                 ++it;
387                                 if (it == par->inset_iterator_end()) {
388                                         par = par->next();
389                                         setParagraph();
390                                 }
391                         }
392                         return tmp;
393                 }
394
395                 ///
396                 Inset * operator*() { return *it; }
397
398                 ///
399                 Paragraph * getPar() { return par; }
400                 ///
401                 lyx::pos_type getPos() const { return it.getPos(); }
402                 ///
403                 friend
404                 bool operator==(inset_iterator const & iter1,
405                                 inset_iterator const & iter2);
406         private:
407                 ///
408                 void setParagraph();
409                 ///
410                 Paragraph * par;
411                 ///
412                 Paragraph::inset_iterator it;
413         };
414
415         ///
416         inset_iterator inset_iterator_begin() {
417                 return inset_iterator(paragraph);
418         }
419         ///
420         inset_iterator inset_iterator_end() {
421                 return inset_iterator();
422         }
423         ///
424         inset_iterator inset_const_iterator_begin() const {
425                 return inset_iterator(paragraph);
426         }
427         ///
428         inset_iterator inset_const_iterator_end() const {
429                 return inset_iterator();
430         }
431
432         ///
433         ParIterator par_iterator_begin();
434         ///
435         ParIterator par_iterator_end();
436
437         ///
438         Inset * getInsetFromID(int id_arg) const;
439 };
440
441
442 inline
443 void Buffer::addUser(BufferView * u)
444 {
445         users = u;
446 }
447
448
449 inline
450 void Buffer::delUser(BufferView *)
451 {
452         users = 0;
453 }
454
455
456 inline
457 Language const * Buffer::getLanguage() const
458 {
459         return params.language;
460 }
461
462
463 inline
464 bool Buffer::isClean() const
465 {
466         return lyx_clean;
467 }
468
469
470 inline
471 bool Buffer::isBakClean() const
472 {
473         return bak_clean;
474 }
475
476
477 inline
478 void Buffer::markClean() const
479 {
480         if (!lyx_clean) {
481                 lyx_clean = true;
482                 updateTitles();
483         }
484         // if the .lyx file has been saved, we don't need an
485         // autosave
486         bak_clean = true;
487 }
488
489
490 inline
491 void Buffer::markBakClean()
492 {
493         bak_clean = true;
494 }
495
496
497 inline
498 void Buffer::setUnnamed(bool flag)
499 {
500         unnamed = flag;
501 }
502
503
504 inline
505 bool Buffer::isUnnamed()
506 {
507         return unnamed;
508 }
509
510
511 inline
512 void Buffer::markDirty()
513 {
514         if (lyx_clean) {
515                 lyx_clean = false;
516                 updateTitles();
517         }
518         bak_clean = false;
519         DEPCLEAN * tmp = dep_clean;
520         while (tmp) {
521                 tmp->clean = false;
522                 tmp = tmp->next;
523         }
524 }
525
526
527 inline
528 string const & Buffer::fileName() const
529 {
530         return filename_;
531 }
532
533
534 inline
535 string const & Buffer::filePath() const
536 {
537         return filepath_;
538 }
539
540
541 inline
542 bool Buffer::isReadonly() const
543 {
544         return read_only;
545 }
546
547
548 inline
549 BufferView * Buffer::getUser() const
550 {
551         return users;
552 }
553
554
555 inline
556 void Buffer::setParentName(string const & name)
557 {
558         params.parentname = name;
559 }
560
561
562 ///
563 inline
564 bool operator==(Buffer::inset_iterator const & iter1,
565                 Buffer::inset_iterator const & iter2)
566 {
567         return iter1.par == iter2.par
568                 && (iter1.par == 0 || iter1.it == iter2.it);
569 }
570
571
572 ///
573 inline
574 bool operator!=(Buffer::inset_iterator const & iter1,
575                 Buffer::inset_iterator const & iter2)
576 {
577         return !(iter1 == iter2);
578 }
579
580 #endif