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