]> git.lyx.org Git - lyx.git/blob - src/buffer.h
bug 183
[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 "undostack.h"
23 #include "lyxvc.h"
24 #include "bufferparams.h"
25 #include "texrow.h"
26 #include "paragraph.h"
27
28 class BufferView;
29 class LyXRC;
30 class TeXErrors;
31 class LaTeXFeatures;
32 class Language;
33 class ParIterator;
34
35 // When lyx 1.3.x starts we should enable this
36 // btw. we should also test this with 1.2 so that we
37 // do not get any surprises. (Lgb)
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);
77
78         /// Maybe we know the function already by number...
79         bool dispatch(int ac, string const & argument);
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         void insertErtContents(Paragraph * par, int & pos,
131                                bool set_inactive = true);
132 #endif
133         ///
134         Paragraph * getParFromID(int id) const;
135 private:
136         /// Parse a single inset.
137         void readInset(LyXLex &, Paragraph *& par, int & pos, LyXFont &);
138 public:
139         /** Save file.
140             Takes care of auto-save files and backup file if requested.
141             Returns \c true if the save is successful, \c false otherwise.
142         */
143         bool save() const;
144         
145         /// Write file. Returns \c false if unsuccesful.
146         bool writeFile(string const &, bool) const;
147         
148         ///
149         void writeFileAscii(string const & , int);
150         ///
151         void writeFileAscii(std::ostream &, int);
152         ///
153         string const asciiParagraph(Paragraph const *, unsigned int linelen,
154                                     bool noparbreak = false) const;
155         ///
156         void makeLaTeXFile(string const & filename,
157                            string const & original_path,
158                            bool nice, bool only_body = false);
159         /** LaTeX all paragraphs from par to endpar.
160             \param \a endpar if == 0 then to the end
161         */
162         void latexParagraphs(std::ostream & os, Paragraph * par,
163                              Paragraph * endpar, TexRow & texrow) const;
164         ///
165         void simpleDocBookOnePar(std::ostream &,
166                                  Paragraph * par, int & desc_on,
167                                  Paragraph::depth_type depth) const ;
168         ///
169         void simpleLinuxDocOnePar(std::ostream & os, Paragraph * par, 
170                                   Paragraph::depth_type depth);
171         ///
172         void makeLinuxDocFile(string const & filename,
173                               bool nice, bool only_body = false);
174         ///
175         void makeDocBookFile(string const & filename,
176                              bool nice, bool only_body = false);
177         /// Open SGML/XML tag.
178         void sgmlOpenTag(std::ostream & os, Paragraph::depth_type depth,
179                 string const & latexname) const;
180         /// Closes SGML/XML tag.
181         void sgmlCloseTag(std::ostream & os, Paragraph::depth_type depth,
182                 string const & latexname) const;
183         ///
184         void sgmlError(Paragraph * par, int pos, string const & message) const;
185
186         /// returns the main language for the buffer (document)
187         Language const * getLanguage() const;
188         ///
189         int runChktex();
190         ///
191         bool isLyxClean() const;
192         ///
193         bool isBakClean() const;
194         ///
195         bool isDepClean(string const & name) const;
196         
197         ///
198         void markLyxClean() const;
199
200         ///
201         void markBakClean();
202         
203         ///
204         void markDepClean(string const & name);
205         
206         ///
207         void setUnnamed(bool flag = true);
208
209         ///
210         bool isUnnamed();
211
212         /// Mark this buffer as dirty.
213         void markDirty();
214
215         /// Returns the buffers filename.
216         string const & fileName() const;
217
218         /** A transformed version of the file name, adequate for LaTeX.
219             \param no_path optional if \c true then the path is stripped.
220         */
221         string const getLatexName(bool no_path = true) const;
222
223         /// Get the name and type of the log.
224         std::pair<LogType, string> const getLogName() const;
225  
226         /// Change name of buffer. Updates "read-only" flag.
227         void setFileName(string const & newfile);
228
229         /// Name of the document's parent
230         void setParentName(string const &);
231
232         /// Is buffer read-only?
233         bool isReadonly() const;
234
235         /// Set buffer read-only flag
236         void setReadonly(bool flag = true);
237
238         /// returns \c true if the buffer contains a LaTeX document
239         bool isLatex() const;
240         /// returns \c true if the buffer contains a LinuxDoc document
241         bool isLinuxDoc() const;
242         /// returns \c true if the buffer contains a DocBook document
243         bool isDocBook() const;
244         /** returns \c true if the buffer contains either a LinuxDoc
245             or DocBook document */
246         bool isSGML() const;
247         /// returns \c true if the buffer contains a Wed document
248         bool isLiterate() const;
249
250         /** Validate a buffer for LaTeX.
251             This validates the buffer, and returns a struct for use by
252             #makeLaTeX# and others. Its main use is to figure out what
253             commands and packages need to be included in the LaTeX file.
254             It (should) also check that the needed constructs are there
255             (i.e. that the \refs points to coresponding \labels). It
256             should perhaps inset "error" insets to help the user correct
257             obvious mistakes.
258         */
259         void validate(LaTeXFeatures &) const;
260
261         ///
262         string const getIncludeonlyList(char delim = ',');
263         ///
264         std::vector<std::pair<string, string> > const getBibkeyList();
265         ///
266         struct TocItem {
267                 TocItem(Paragraph * p, int d, string const & s)
268                         : par(p), depth(d), str(s) {}
269                 ///
270                 Paragraph * par;
271                 ///
272                 int depth;
273                 ///
274                 string str;
275         };
276         ///
277         typedef std::vector<TocItem> SingleList;
278         ///
279         typedef std::map<string, SingleList> Lists;
280         ///
281         Lists const getLists() const;
282         ///
283         std::vector<string> const getLabelList();
284
285         /** This will clearly have to change later. Later we can have more
286             than one user per buffer. */
287         BufferView * getUser() const;
288
289         ///
290         void changeLanguage(Language const * from, Language const * to);
291         ///
292         bool isMultiLingual();
293
294         /// Does this mean that this is buffer local?
295         UndoStack undostack;
296         
297         /// Does this mean that this is buffer local? 
298         UndoStack redostack;
299         
300         ///
301         BufferParams params;
302         
303         /** The list of paragraphs.
304             This is a linked list of paragraph, this list holds the
305             whole contents of the document.
306          */
307         Paragraph * paragraph;
308
309         /// LyX version control object.
310         LyXVC lyxvc;
311
312         /// Where to put temporary files.
313         string tmppath;
314
315         /// The path to the document file.
316         string filepath;
317
318         /** If we are writing a nice LaTeX file or not.
319             While writing as LaTeX, tells whether we are
320             doing a 'nice' LaTeX file */
321         bool niceFile;
322
323         /// Used when typesetting to place errorboxes.
324         TexRow texrow;
325 private:
326         /// is save needed
327         mutable bool lyx_clean;
328         
329         /// is autosave needed
330         mutable bool bak_clean;
331         
332         /// is this a unnamed file (New...)
333         bool unnamed;
334
335         /// is regenerating #.tex# necessary
336         DEPCLEAN * dep_clean;
337
338         /// buffer is r/o
339         bool read_only;
340
341         /// name of the file the buffer is associated with.
342         string filename;
343
344         /// Format number of buffer
345         int file_format;
346         /** A list of views using this buffer.
347             Why not keep a list of the BufferViews that use this buffer?
348
349             At least then we don't have to do a lot of magic like:
350             #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
351             of the buffers in the list of users to do a #updateLayoutChoice#.
352         */
353         BufferView * users;
354
355 public:
356         ///
357         class inset_iterator {
358         public:
359                 typedef std::input_iterator_tag iterator_category;
360                 typedef Inset value_type;
361                 typedef ptrdiff_t difference_type;
362                 typedef Inset * pointer;
363                 typedef Inset & reference;
364                 
365                 
366                 ///
367                 inset_iterator() : par(0) /*, it(0)*/ {}
368                 //
369                 inset_iterator(Paragraph * paragraph) : par(paragraph) {
370                         setParagraph();
371                 }
372                 ///
373                 inset_iterator(Paragraph * paragraph, lyx::pos_type pos);
374                 ///
375                 inset_iterator & operator++() { // prefix ++
376                         if (par) {
377                                 ++it;
378                                 if (it == par->inset_iterator_end()) {
379                                         par = par->next();
380                                         setParagraph();
381                                 }
382                         }
383                         return *this;
384                 }
385                 ///
386                 inset_iterator operator++(int) { // postfix ++
387                         inset_iterator tmp(par, it.getPos());
388                         if (par) {
389                                 ++it;
390                                 if (it == par->inset_iterator_end()) {
391                                         par = par->next();
392                                         setParagraph();
393                                 }
394                         }
395                         return tmp;
396                 }
397                 ///
398                 Inset * operator*() { return *it; }
399                 
400                 ///
401                 Paragraph * getPar() { return par; }
402                 ///
403                 lyx::pos_type getPos() const { return it.getPos(); }
404                 ///
405                 friend
406                 bool operator==(inset_iterator const & iter1,
407                                 inset_iterator const & iter2);
408         private:
409                 ///
410                 void setParagraph();
411                 ///
412                 Paragraph * par;
413                 ///
414                 Paragraph::inset_iterator it;
415         };
416
417         ///
418         inset_iterator inset_iterator_begin() {
419                 return inset_iterator(paragraph);
420         }
421         ///
422         inset_iterator inset_iterator_end() {
423                 return inset_iterator();
424         }
425         ///
426         inset_iterator inset_const_iterator_begin() const {
427                 return inset_iterator(paragraph);
428         }
429         ///
430         inset_iterator inset_const_iterator_end() const {
431                 return inset_iterator();
432         }
433
434         ///
435         ParIterator par_iterator_begin();
436         ///
437         ParIterator par_iterator_end();
438
439         ///
440         Inset * getInsetFromID(int id_arg) const;
441 };
442
443
444 inline
445 void Buffer::addUser(BufferView * u)
446 {
447         users = u;
448 }
449
450
451 inline
452 void Buffer::delUser(BufferView *)
453 {
454         users = 0;
455 }
456         
457
458 inline
459 Language const * Buffer::getLanguage() const
460 {
461         return params.language;
462 }
463         
464
465 inline
466 bool Buffer::isLyxClean() const
467 {
468         return lyx_clean;
469 }
470         
471
472 inline
473 bool Buffer::isBakClean() const
474 {
475         return bak_clean;
476 }
477
478
479 inline
480 void Buffer::markLyxClean() const
481
482         if (!lyx_clean) {
483                 lyx_clean = true; 
484                 updateTitles();
485         }
486         // if the .lyx file has been saved, we don't need an
487         // autosave 
488         bak_clean = true;
489 }
490
491
492 inline
493 void Buffer::markBakClean()
494 {
495         bak_clean = true;
496 }
497
498
499 inline
500 void Buffer::setUnnamed(bool flag)
501 {
502         unnamed = flag;
503 }
504
505
506 inline
507 bool Buffer::isUnnamed()
508 {
509         return unnamed;
510 }
511
512
513 inline
514 void Buffer::markDirty()
515 {
516         if (lyx_clean) {
517                 lyx_clean = false;
518                 updateTitles();
519         }
520         bak_clean = false;
521         DEPCLEAN * tmp = dep_clean;
522         while (tmp) {
523                 tmp->clean = false;
524                 tmp = tmp->next;
525         }
526 }
527
528
529 inline
530 string const & Buffer::fileName() const
531 {
532         return filename;
533 }
534
535
536 inline
537 bool Buffer::isReadonly() const
538 {
539         return read_only;
540 }
541
542
543 inline
544 BufferView * Buffer::getUser() const
545 {
546         return users;
547 }
548
549
550 inline  
551 void Buffer::setParentName(string const & name)
552 {
553         params.parentname = name;    
554 }
555
556
557 ///
558 inline
559 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b)
560 {
561         return a.par == b.par && a.str == b.str;
562         // No need to compare depth.
563 }
564
565
566 ///
567 inline
568 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b)
569 {
570         return !(a == b);
571         // No need to compare depth.
572 }
573
574
575 ///
576 inline
577 bool operator==(Buffer::inset_iterator const & iter1,
578                 Buffer::inset_iterator const & iter2)
579 {
580         return iter1.par == iter2.par
581                 && (iter1.par == 0 || iter1.it == iter2.it);
582 }
583
584
585 ///
586 inline
587 bool operator!=(Buffer::inset_iterator const & iter1,
588                 Buffer::inset_iterator const & iter2)
589 {
590         return !(iter1 == iter2);
591 }
592
593 #endif