]> git.lyx.org Git - lyx.git/blob - src/buffer.h
Removed Options->LaTeX and all related code. New Variables.[Ch] files (not
[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                                         LyXParagraph::footnote_flag &,
137                                         LyXParagraph::footnote_kind &);
138
139         /** Save file
140             Takes care of auto-save files and backup file if requested.
141             Returns true if the save is successful, false otherwise.
142         */
143         bool save() const;
144         
145         /// Write file. Returns false if unsuccesful.
146         bool writeFile(string const &, bool) const;
147         
148         ///
149         void writeFileAscii(string const & , int);
150         
151         ///
152         void makeLaTeXFile(string const & filename,
153                            string const & original_path,
154                            bool nice, bool only_body = false);
155         //
156         // LaTeX all paragraphs from par to endpar,
157         // if endpar == 0 then to the end
158         //
159         void latexParagraphs(std::ostream & os, LyXParagraph *par,
160                              LyXParagraph *endpar, TexRow & texrow) const;
161
162         ///
163         int runLaTeX();
164
165         ///
166         int runLiterate();
167
168         ///
169         int buildProgram();
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         string GetLanguage() const {
183                 return params.language;
184         }
185         
186         ///
187         bool isLyxClean() const { return lyx_clean; }
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         ///
210         void markNwClean() { nw_clean = true; }
211        
212         ///
213         void markBakClean() { bak_clean = true; }
214         
215         ///
216         void markDepClean(string const & name);
217         
218         ///
219         void markNwDirty() { nw_clean = false; }
220        
221         ///
222         void markDirty() {
223                 if (lyx_clean) {
224                         lyx_clean = false;
225                         updateTitles();
226                 }
227                 nw_clean = false;
228                 bak_clean = false;
229                 DEPCLEAN * tmp = dep_clean;
230                 while (tmp) {
231                         tmp->clean = false;
232                         tmp = tmp->next;
233                 }
234         }
235
236         ///
237         string const & fileName() const { return filename; }
238
239         /** A transformed version of the file name, adequate for LaTeX  
240             The path is stripped if no_path is true (default) */
241         string getLatexName(bool no_path = true) const;
242
243         /// Change name of buffer. Updates "read-only" flag.
244         void fileName(string const & newfile);
245
246         /// Name of the document's parent
247         void setParentName(string const &);
248
249         /// Is buffer read-only?
250         bool isReadonly() const { return read_only; }
251
252         /// Set buffer read-only flag
253         void setReadonly(bool flag = true);
254
255         /// returns true if the buffer contains a LaTeX document
256         bool isLatex() const;
257         /// returns true if the buffer contains a LinuxDoc document
258         bool isLinuxDoc() const;
259         /// returns true if the buffer contains a DocBook document
260         bool isDocBook() const;
261         /** returns true if the buffer contains either a LinuxDoc
262             or DocBook document */
263         bool isSGML() const;
264         /// returns true if the buffer contains a Wed document
265         bool isLiterate() const;
266
267         ///
268         void setPaperStuff();
269
270         /** Validate a buffer for LaTeX.
271             This validates the buffer, and returns a struct for use by
272             makeLaTeX and others. Its main use is to figure out what
273             commands and packages need to be included in the LaTeX file.
274             It (should) also check that the needed constructs are there
275             (i.e. that the \refs points to coresponding \labels). It
276             should perhaps inset "error" insets to help the user correct
277             obvious mistakes.
278         */
279         void validate(LaTeXFeatures &) const;
280
281         ///
282         string getIncludeonlyList(char delim = ',');
283         ///
284         std::vector<std::pair<string,string> > getBibkeyList();
285         ///
286         struct TocItem {
287                 LyXParagraph * par;
288                 int depth;
289                 string str;
290         };
291         ///
292         enum TocType {
293                 ///
294                 TOC_TOC = 0,
295                 ///
296                 TOC_LOF,
297                 ///
298                 TOC_LOT,
299                 ///
300                 TOC_LOA
301         };
302         ///
303         std::vector<std::vector<TocItem> > getTocList();
304         ///
305         std::vector<string> getLabelList();
306
307         /** This will clearly have to change later. Later we can have more
308             than one user per buffer. */
309         BufferView * getUser() const { return users; }
310
311         ///
312         void ChangeLanguage(Language const * from, Language const * to);
313         ///
314         bool isMultiLingual();
315
316         //@}
317
318         /// Does this mean that this is buffer local?
319         UndoStack undostack;
320         
321         /// Does this mean that this is buffer local? 
322         UndoStack redostack;
323         
324         ///
325         BufferParams params;
326         
327         /** is a list of paragraphs.
328          */
329         LyXParagraph * paragraph;
330
331         /// RCS object
332         LyXVC lyxvc;
333
334         /// where the temporaries go if we want them
335         string tmppath;
336
337         ///
338         string filepath;
339
340         /** While writing as LaTeX, tells whether we are
341             doing a 'nice' LaTeX file */
342         bool niceFile;
343
344         /// Used when typesetting to place errorboxes.
345         TexRow texrow;
346 private:
347         ///
348         void linuxDocHandleFootnote(std::ostream & os,
349                                     LyXParagraph * & par, int const depth);
350         ///
351         void DocBookHandleCaption(std::ostream & os, string & inner_tag,
352                                   int const depth, int desc_on,
353                                   LyXParagraph * & par);
354         ///
355         void DocBookHandleFootnote(std::ostream & os,
356                                    LyXParagraph * & par, int const depth);
357         ///
358         void sgmlOpenTag(std::ostream & os, int depth,
359                          string const & latexname) const;
360         ///
361         void sgmlCloseTag(std::ostream & os, int depth,
362                           string const & latexname) const;
363         ///
364         void LinuxDocError(LyXParagraph * par, int pos, char const * message);
365         ///
366         void SimpleLinuxDocOnePar(std::ostream & os, LyXParagraph * par,
367                                   int desc_on, int const depth);
368         ///
369         void SimpleDocBookOnePar(std::ostream &, string & extra,
370                                  LyXParagraph * par, int & desc_on,
371                                  int const depth);
372
373         /// LinuxDoc.
374         void push_tag(std::ostream & os, char const * tag,
375                       int & pos, char stack[5][3]);
376         
377         /// LinuxDoc.
378         void pop_tag(std::ostream & os, char const * tag,
379                      int & pos, char stack[5][3]);
380
381         /// is save needed
382         mutable bool lyx_clean;
383         
384         /// is autosave needed
385         mutable bool bak_clean;
386         
387         /// do we need to run weave/tangle
388         bool nw_clean;
389
390         /// is regenerating .tex necessary
391         DEPCLEAN * dep_clean;
392
393         /// buffer is r/o
394         bool read_only;
395
396         /// name of the file the buffer is associated with.
397         string filename;
398
399         /// Format number of buffer
400         float format;
401         
402         /** A list of views using this buffer.
403             Why not keep a list of the BufferViews that use this buffer?
404
405             At least then we don't have to do a lot of magic like:
406             buffer->lyx_gui->bufferview->updateLayoutChoice. Just ask each
407             of the buffers in the list of users to do a updateLayoutChoice.
408         */
409         BufferView * users;
410
411 public:
412         class inset_iterator {
413         public:
414                 inset_iterator() : par(0) /*, it(0)*/ {}
415                 inset_iterator(LyXParagraph * paragraph) : par(paragraph) {
416                         SetParagraph();
417                 }
418                 inset_iterator(LyXParagraph * paragraph, LyXParagraph::size_type pos);
419                 inset_iterator & operator++() {
420                         if (par) {
421                                 ++it;
422                                 if (it == par->inset_iterator_end()) {
423                                         par = par->next;
424                                         SetParagraph();
425                                 }
426                         }
427                         return *this;
428                 }
429                 Inset * operator*() {return *it; }
430                 LyXParagraph * getPar() { return par; }
431                 LyXParagraph::size_type getPos() {return it.getPos(); }
432                 friend
433                 bool operator==(inset_iterator const & iter1,
434                                 inset_iterator const & iter2) {
435                         return iter1.par == iter2.par
436                                 && (iter1.par == 0 || iter1.it == iter2.it);
437                 }
438                 friend
439                 bool operator!=(inset_iterator const & iter1,
440                                 inset_iterator const & iter2) {
441                         return !(iter1 == iter2);
442                 }
443         private:
444                 void SetParagraph();
445                 LyXParagraph * par;
446                 LyXParagraph::inset_iterator it;
447         };
448
449         ///
450         inset_iterator inset_iterator_begin() {
451                 return inset_iterator(paragraph);
452         }
453         ///
454         inset_iterator inset_iterator_end() {
455                 return inset_iterator();
456         }
457 };
458
459
460 inline  
461 void Buffer::setParentName(string const & name)
462 {
463         params.parentname = name;    
464 }
465
466 inline
467 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
468         return a.par == b.par && a.str == b.str;
469         // No need to compare depth.
470 }
471
472
473 inline
474 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
475         return !(a == b);
476         // No need to compare depth.
477 }
478
479 #endif