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