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