]> git.lyx.org Git - lyx.git/blob - src/buffer.h
Fix small bug in reading \set_color in lyxrc
[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 // Change Log:
14 // =========== 
15 // 23/03/98   Heinrich Bauer (heinrich.bauer@t-mobil.de)
16 // Spots marked "changed Heinrich Bauer, 23/03/98" modified due to the
17 // following bug: dvi file export did not work after printing (or previewing)
18 // and vice versa as long as the same file was concerned. This happened
19 // every time the LyX-file was left unchanged between the two actions mentioned
20 // above.
21
22 #ifndef BUFFER_H
23 #define BUFFER_H
24
25 #ifdef __GNUG__
26 #pragma interface
27 #endif
28
29 #include "LString.h"
30
31 #include "BufferView.h"
32 #include "lyxvc.h"
33 #include "bufferparams.h"
34 #include "texrow.h"
35
36
37 class LyXRC;
38 class TeXErrors;
39 class LaTeXFeatures;
40 class auto_mem_buffer;
41
42 ///
43 struct DEPCLEAN {
44         ///
45         bool clean;
46         ///
47         string master;
48         ///
49         DEPCLEAN * next;
50 };
51
52 /** The buffer object.
53   The is is the buffer object. It contains all the informations about
54   a document loaded into LyX. I am not sure if the class is complete or
55   minimal, probably not.
56   */
57 class Buffer {
58 public:
59         /**@name Constructors and destructor */
60         //@{
61         ///
62         explicit Buffer(string const & file, bool b = false);
63         
64         ///
65         ~Buffer();
66         //@}
67
68         /**@name Methods */
69         //@{
70
71         /** save the buffer's parameters as user default
72             This function saves a file user_lyxdir/templates/defaults.lyx 
73             which parameters are those of the current buffer. This file
74             is used as a default template when creating a new
75             file. Returns true on success.
76         */
77         bool saveParamsAsDefaults();
78
79         /** high-level interface to buffer functionality
80             This function parses a command string and executes it
81         */
82         bool Dispatch(string const & command);
83
84         /// Maybe we know the function already by number...
85         bool Dispatch(int ac, string const & argument);
86
87         /// and have an xtl buffer to work with.
88         bool Dispatch(int, auto_mem_buffer &);
89
90         /// should be changed to work for a list.
91         void resize();
92         void resizeInsets(BufferView *);
93
94         /// Update window titles of all users
95         void updateTitles() const;
96
97         /// Reset autosave timers for all users
98         void resetAutosaveTimers() const;
99
100         /** Adds the BufferView to the users list.
101             Later this func will insert the BufferView into a real list,
102             not just setting a pointer.
103         */
104         void addUser(BufferView * u) { users = u; }
105
106         /** Removes the BufferView from the users list.
107             Since we only can have one at the moment, we just reset it.
108         */
109         void delUser(BufferView *) { users = 0; }
110         
111         ///
112         void redraw() {
113                 users->redraw(); 
114                 users->fitCursor(); 
115                 //users->updateScrollbar();
116         }
117
118         ///
119         void loadAutoSaveFile();
120         
121         /** Reads a file. 
122             Returns false if it fails.
123             If par is given, the file is inserted. */
124         bool readFile(LyXLex &, LyXParagraph * par = 0);
125         
126         /** Reads a file without header.
127             Returns false, if file is not completely read.
128             If par is given, the file is inserted. */
129         bool readLyXformat2(LyXLex &, LyXParagraph * par = 0);
130
131         /* This parses a single LyXformat-Token */
132         bool parseSingleLyXformat2Token(LyXLex &, LyXParagraph *& par,
133                                         LyXParagraph *& return_par,
134                                         string const & token, int & pos,
135                                         char & depth, LyXFont &
136 #ifndef NEW_INSETS
137                                         ,LyXParagraph::footnote_flag &,
138                                         LyXParagraph::footnote_kind &
139 #endif
140                 );
141 private:
142         // Parse a single inset.
143         void readInset(LyXLex &, LyXParagraph *& par, int & pos, LyXFont &);
144 public:
145         /** Save file
146             Takes care of auto-save files and backup file if requested.
147             Returns true if the save is successful, false otherwise.
148         */
149         bool save() const;
150         
151         /// Write file. Returns false if unsuccesful.
152         bool writeFile(string const &, bool) const;
153         
154         ///
155         void writeFileAscii(string const & , int);
156         
157         ///
158         void makeLaTeXFile(string const & filename,
159                            string const & original_path,
160                            bool nice, bool only_body = false);
161         //
162         // LaTeX all paragraphs from par to endpar,
163         // if endpar == 0 then to the end
164         //
165         void latexParagraphs(std::ostream & os, LyXParagraph *par,
166                              LyXParagraph *endpar, TexRow & texrow) const;
167
168         ///
169         int runLaTeX();
170
171         ///
172         int runLiterate();
173
174         ///
175         int buildProgram();
176
177         ///
178         int runChktex();
179
180         ///
181         void makeLinuxDocFile(string const & filename,
182                               bool nice, bool only_body = false);
183         ///
184         void makeDocBookFile(string const & filename,
185                              bool nice, bool only_body = false);
186
187         /// returns the main language for the buffer (document)
188         string GetLanguage() const {
189                 return params.language;
190         }
191         
192         ///
193         bool isLyxClean() const { return lyx_clean; }
194         
195         ///
196         bool isNwClean() const { return nw_clean; }
197        
198         ///
199         bool isBakClean() const { return bak_clean; }
200         
201         ///
202         bool isDepClean(string const & name) const;
203         
204         ///
205         void markLyxClean() const { 
206                 if (!lyx_clean) {
207                         lyx_clean = true; 
208                         updateTitles();
209                 }
210                 // if the .lyx file has been saved, we don't need an
211                 // autosave 
212                 bak_clean = true;
213         }
214
215         ///
216         void markNwClean() { nw_clean = true; }
217        
218         ///
219         void markBakClean() { bak_clean = true; }
220         
221         ///
222         void markDepClean(string const & name);
223         
224         ///
225         void markNwDirty() { nw_clean = false; }
226        
227         ///
228         void markDirty() {
229                 if (lyx_clean) {
230                         lyx_clean = false;
231                         updateTitles();
232                 }
233                 nw_clean = false;
234                 bak_clean = false;
235                 DEPCLEAN * tmp = dep_clean;
236                 while (tmp) {
237                         tmp->clean = false;
238                         tmp = tmp->next;
239                 }
240         }
241
242         ///
243         string const & fileName() const { return filename; }
244
245         /** A transformed version of the file name, adequate for LaTeX  
246             The path is stripped if no_path is true (default) */
247         string getLatexName(bool no_path = true) const;
248
249         /// Change name of buffer. Updates "read-only" flag.
250         void fileName(string const & newfile);
251
252         /// Name of the document's parent
253         void setParentName(string const &);
254
255         /// Is buffer read-only?
256         bool isReadonly() const { return read_only; }
257
258         /// Set buffer read-only flag
259         void setReadonly(bool flag = true);
260
261         /// returns true if the buffer contains a LaTeX document
262         bool isLatex() const;
263         /// returns true if the buffer contains a LinuxDoc document
264         bool isLinuxDoc() const;
265         /// returns true if the buffer contains a DocBook document
266         bool isDocBook() const;
267         /** returns true if the buffer contains either a LinuxDoc
268             or DocBook document */
269         bool isSGML() const;
270         /// returns true if the buffer contains a Wed document
271         bool isLiterate() const;
272
273         ///
274         void setPaperStuff();
275
276         /** Validate a buffer for LaTeX.
277             This validates the buffer, and returns a struct for use by
278             makeLaTeX and others. Its main use is to figure out what
279             commands and packages need to be included in the LaTeX file.
280             It (should) also check that the needed constructs are there
281             (i.e. that the \refs points to coresponding \labels). It
282             should perhaps inset "error" insets to help the user correct
283             obvious mistakes.
284         */
285         void validate(LaTeXFeatures &) const;
286
287         ///
288         string getIncludeonlyList(char delim = ',');
289         ///
290         std::vector<std::pair<string,string> > getBibkeyList();
291         ///
292         struct TocItem {
293                 LyXParagraph * par;
294                 int depth;
295                 string str;
296         };
297         ///
298         enum TocType {
299                 ///
300                 TOC_TOC = 0,
301                 ///
302                 TOC_LOF,
303                 ///
304                 TOC_LOT,
305                 ///
306                 TOC_LOA
307         };
308         ///
309         std::vector<std::vector<TocItem> > getTocList();
310         ///
311         std::vector<string> getLabelList();
312
313         /** This will clearly have to change later. Later we can have more
314             than one user per buffer. */
315         BufferView * getUser() const { return users; }
316
317         ///
318         void ChangeLanguage(Language const * from, Language const * to);
319         ///
320         bool isMultiLingual();
321
322         //@}
323
324         /// Does this mean that this is buffer local?
325         UndoStack undostack;
326         
327         /// Does this mean that this is buffer local? 
328         UndoStack redostack;
329         
330         ///
331         BufferParams params;
332         
333         /** is a list of paragraphs.
334          */
335         LyXParagraph * paragraph;
336
337         /// RCS object
338         LyXVC lyxvc;
339
340         /// where the temporaries go if we want them
341         string tmppath;
342
343         ///
344         string filepath;
345
346         /** While writing as LaTeX, tells whether we are
347             doing a 'nice' LaTeX file */
348         bool niceFile;
349
350         /// Used when typesetting to place errorboxes.
351         TexRow texrow;
352 private:
353 #ifndef NEW_INSETS
354         ///
355         void linuxDocHandleFootnote(std::ostream & os,
356                                     LyXParagraph * & par, int const depth);
357 #endif
358         ///
359         void DocBookHandleCaption(std::ostream & os, string & inner_tag,
360                                   int const depth, int desc_on,
361                                   LyXParagraph * & par);
362 #ifndef NEW_INSETS
363         ///
364         void DocBookHandleFootnote(std::ostream & os,
365                                    LyXParagraph * & par, int const depth);
366 #endif
367         ///
368         void sgmlOpenTag(std::ostream & os, int depth,
369                          string const & latexname) const;
370         ///
371         void sgmlCloseTag(std::ostream & os, int depth,
372                           string const & latexname) const;
373         ///
374         void LinuxDocError(LyXParagraph * par, int pos, char const * message);
375         ///
376         void SimpleLinuxDocOnePar(std::ostream & os, LyXParagraph * par,
377                                   int desc_on, int const depth);
378         ///
379         void SimpleDocBookOnePar(std::ostream &, string & extra,
380                                  LyXParagraph * par, int & desc_on,
381                                  int const depth);
382
383         /// LinuxDoc.
384         void push_tag(std::ostream & os, char const * tag,
385                       int & pos, char stack[5][3]);
386         
387         /// LinuxDoc.
388         void pop_tag(std::ostream & os, char const * tag,
389                      int & pos, char stack[5][3]);
390
391         /// is save needed
392         mutable bool lyx_clean;
393         
394         /// is autosave needed
395         mutable bool bak_clean;
396         
397         /// do we need to run weave/tangle
398         bool nw_clean;
399
400         /// is regenerating .tex necessary
401         DEPCLEAN * dep_clean;
402
403         /// buffer is r/o
404         bool read_only;
405
406         /// name of the file the buffer is associated with.
407         string filename;
408
409         /// Format number of buffer
410         float format;
411         
412         /** A list of views using this buffer.
413             Why not keep a list of the BufferViews that use this buffer?
414
415             At least then we don't have to do a lot of magic like:
416             buffer->lyx_gui->bufferview->updateLayoutChoice. Just ask each
417             of the buffers in the list of users to do a updateLayoutChoice.
418         */
419         BufferView * users;
420
421 public:
422         class inset_iterator {
423         public:
424                 inset_iterator() : par(0) /*, it(0)*/ {}
425                 inset_iterator(LyXParagraph * paragraph) : par(paragraph) {
426                         SetParagraph();
427                 }
428                 inset_iterator(LyXParagraph * paragraph, LyXParagraph::size_type pos);
429                 inset_iterator & operator++() {
430                         if (par) {
431                                 ++it;
432                                 if (it == par->inset_iterator_end()) {
433                                         par = par->next;
434                                         SetParagraph();
435                                 }
436                         }
437                         return *this;
438                 }
439                 Inset * operator*() {return *it; }
440                 LyXParagraph * getPar() { return par; }
441                 LyXParagraph::size_type getPos() {return it.getPos(); }
442                 friend
443                 bool operator==(inset_iterator const & iter1,
444                                 inset_iterator const & iter2) {
445                         return iter1.par == iter2.par
446                                 && (iter1.par == 0 || iter1.it == iter2.it);
447                 }
448                 friend
449                 bool operator!=(inset_iterator const & iter1,
450                                 inset_iterator const & iter2) {
451                         return !(iter1 == iter2);
452                 }
453         private:
454                 void SetParagraph();
455                 LyXParagraph * par;
456                 LyXParagraph::inset_iterator it;
457         };
458
459         ///
460         inset_iterator inset_iterator_begin() {
461                 return inset_iterator(paragraph);
462         }
463         ///
464         inset_iterator inset_iterator_end() {
465                 return inset_iterator();
466         }
467 };
468
469
470 inline  
471 void Buffer::setParentName(string const & name)
472 {
473         params.parentname = name;    
474 }
475
476 inline
477 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
478         return a.par == b.par && a.str == b.str;
479         // No need to compare depth.
480 }
481
482
483 inline
484 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
485         return !(a == b);
486         // No need to compare depth.
487 }
488
489 #endif