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