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