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