]> git.lyx.org Git - lyx.git/blob - src/buffer.h
53df1feade9359992004e5af90d50d4f32d10a0d
[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         ///
60         explicit Buffer(string const & file, bool b = false);
61         
62         ///
63         ~Buffer();
64
65         /** save the buffer's parameters as user default
66             This function saves a file #user_lyxdir/templates/defaults.lyx# 
67             which parameters are those of the current buffer. This file
68             is used as a default template when creating a new
69             file. Returns #true# on success.
70         */
71         bool saveParamsAsDefaults();
72
73         /** High-level interface to buffer functionality.
74             This function parses a command string and executes it
75         */
76         bool Dispatch(string const & command);
77
78         /// Maybe we know the function already by number...
79         bool Dispatch(int ac, string const & argument);
80
81         /// and have an xtl buffer to work with.
82         bool Dispatch(int, auto_mem_buffer &);
83
84         /// should be changed to work for a list.
85         void resize();
86         ///
87         void resizeInsets(BufferView *);
88
89         /// Update window titles of all users
90         void updateTitles() const;
91
92         /// Reset autosave timers for all users
93         void resetAutosaveTimers() const;
94
95         /** Adds the BufferView to the users list.
96             Later this func will insert the #BufferView# into a real list,
97             not just setting a pointer.
98         */
99         void addUser(BufferView * u) { users = u; }
100
101         /** Removes the #BufferView# from the users list.
102             Since we only can have one at the moment, we just reset it.
103         */
104         void delUser(BufferView *) { users = 0; }
105         
106         ///
107         void redraw() {
108                 users->redraw(); 
109                 users->fitCursor(); 
110                 //users->updateScrollbar();
111         }
112
113         ///
114         void loadAutoSaveFile();
115         
116         /** Reads a file. 
117             @param par if != 0 insert the file.
118             @return #false# if method fails.
119         */
120         bool readFile(LyXLex &, LyXParagraph * par = 0);
121         
122         /** Reads a file without header.
123             @param par if != 0 insert the file.
124             @return false if file is not completely read.
125         */
126         bool readLyXformat2(LyXLex &, LyXParagraph * par = 0);
127
128         /// This parses a single LyXformat-Token.
129         bool parseSingleLyXformat2Token(LyXLex &, LyXParagraph *& par,
130                                         LyXParagraph *& return_par,
131                                         string const & token, int & pos,
132                                         char & depth, LyXFont &
133 #ifndef NEW_INSETS
134                                         ,LyXParagraph::footnote_flag &,
135                                         LyXParagraph::footnote_kind &
136 #endif
137                 );
138 private:
139         /// Parse a single inset.
140         void readInset(LyXLex &, LyXParagraph *& par, int & pos, LyXFont &);
141 public:
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         void writeFileAscii(std::ostream &, int);
154         string const asciiParagraph(LyXParagraph const *, int linelen) const;
155         ///
156         void makeLaTeXFile(string const & filename,
157                            string const & original_path,
158                            bool nice, bool only_body = false);
159         /** LaTeX all paragraphs from par to endpar.
160             @param endpar if == 0 then to the end
161         */
162         void latexParagraphs(std::ostream & os, LyXParagraph * par,
163                              LyXParagraph * endpar, TexRow & texrow) const;
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 const 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 setUnnamed(bool flag=true) { unnamed = flag; }
223
224         ///
225         bool isUnnamed() { return unnamed; }
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         */
248         string const getLatexName(bool no_path = true) const;
249
250         /// Change name of buffer. Updates "read-only" flag.
251         void fileName(string const & newfile);
252
253         /// Name of the document's parent
254         void setParentName(string const &);
255
256         /// Is buffer read-only?
257         bool isReadonly() const { return read_only; }
258
259         /// Set buffer read-only flag
260         void setReadonly(bool flag = true);
261
262         /// returns #true# if the buffer contains a LaTeX document
263         bool isLatex() const;
264         /// returns #true# if the buffer contains a LinuxDoc document
265         bool isLinuxDoc() const;
266         /// returns #true# if the buffer contains a DocBook document
267         bool isDocBook() const;
268         /** returns #true# if the buffer contains either a LinuxDoc
269             or DocBook document */
270         bool isSGML() const;
271         /// returns #true# if the buffer contains a Wed document
272         bool isLiterate() const;
273
274         ///
275         void setPaperStuff();
276
277         /** Validate a buffer for LaTeX.
278             This validates the buffer, and returns a struct for use by
279             #makeLaTeX# and others. Its main use is to figure out what
280             commands and packages need to be included in the LaTeX file.
281             It (should) also check that the needed constructs are there
282             (i.e. that the \refs points to coresponding \labels). It
283             should perhaps inset "error" insets to help the user correct
284             obvious mistakes.
285         */
286         void validate(LaTeXFeatures &) const;
287
288         ///
289         string const getIncludeonlyList(char delim = ',');
290         ///
291         std::vector<std::pair<string,string> > const getBibkeyList();
292         ///
293         struct TocItem {
294                 ///
295                 LyXParagraph * par;
296                 ///
297                 int depth;
298                 ///
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> > const getTocList();
314         ///
315         std::vector<string> const 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         /// 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 #ifndef NEW_INSETS
356         ///
357         void linuxDocHandleFootnote(std::ostream & os,
358                                     LyXParagraph * & par, int const depth);
359 #endif
360         ///
361         void DocBookHandleCaption(std::ostream & os, string & inner_tag,
362                                   int const depth, int desc_on,
363                                   LyXParagraph * & par);
364 #ifndef NEW_INSETS
365         ///
366         void DocBookHandleFootnote(std::ostream & os,
367                                    LyXParagraph * & par, int const depth);
368 #endif
369         ///
370         void sgmlOpenTag(std::ostream & os, int depth,
371                          string const & latexname) const;
372         ///
373         void sgmlCloseTag(std::ostream & os, int depth,
374                           string const & latexname) const;
375         ///
376         void LinuxDocError(LyXParagraph * par, int pos,
377                            string const & message);
378         ///
379         void SimpleLinuxDocOnePar(std::ostream & os, LyXParagraph * par,
380                                   int desc_on, int const depth);
381         ///
382         void SimpleDocBookOnePar(std::ostream &, string & extra,
383                                  LyXParagraph * par, int & desc_on,
384                                  int const depth);
385
386         /// LinuxDoc.
387         void push_tag(std::ostream & os, string const & tag,
388                       int & pos, char stack[5][3]);
389         
390         /// LinuxDoc.
391         void pop_tag(std::ostream & os, string const & tag,
392                      int & pos, char stack[5][3]);
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 weave/tangle
401         bool nw_clean;
402
403         /// is this a unnamed file (New...)
404         bool unnamed;
405
406         /// is regenerating #.tex# necessary
407         DEPCLEAN * dep_clean;
408
409         /// buffer is r/o
410         bool read_only;
411
412         /// name of the file the buffer is associated with.
413         string filename;
414
415         /// Format number of buffer
416         float format;
417         
418         /** A list of views using this buffer.
419             Why not keep a list of the BufferViews that use this buffer?
420
421             At least then we don't have to do a lot of magic like:
422             #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
423             of the buffers in the list of users to do a #updateLayoutChoice#.
424         */
425         BufferView * users;
426
427 public:
428         ///
429         class inset_iterator {
430         public:
431                 ///
432                 inset_iterator() : par(0) /*, it(0)*/ {}
433                 //
434                 inset_iterator(LyXParagraph * paragraph) : par(paragraph) {
435                         SetParagraph();
436                 }
437                 ///
438                 inset_iterator(LyXParagraph * paragraph,
439                                LyXParagraph::size_type pos);
440                 ///
441                 inset_iterator & operator++() {
442                         if (par) {
443                                 ++it;
444                                 if (it == par->inset_iterator_end()) {
445                                         par = par->next;
446                                         SetParagraph();
447                                 }
448                         }
449                         return *this;
450                 }
451                 ///
452                 Inset * operator*() {return *it; }
453                 ///
454                 LyXParagraph * getPar() { return par; }
455                 ///
456                 LyXParagraph::size_type getPos() {return it.getPos(); }
457                 ///
458                 friend
459                 bool operator==(inset_iterator const & iter1,
460                                 inset_iterator const & iter2);
461                 //
462                 //friend
463                 //bool operator!=(inset_iterator const & iter1,
464                 //              inset_iterator const & iter2);
465         private:
466                 ///
467                 void SetParagraph();
468                 ///
469                 LyXParagraph * par;
470                 ///
471                 LyXParagraph::inset_iterator it;
472         };
473
474         ///
475         inset_iterator inset_iterator_begin() {
476                 return inset_iterator(paragraph);
477         }
478         ///
479         inset_iterator inset_iterator_end() {
480                 return inset_iterator();
481         }
482 };
483
484
485 inline  
486 void Buffer::setParentName(string const & name)
487 {
488         params.parentname = name;    
489 }
490
491 ///
492 inline
493 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
494         return a.par == b.par && a.str == b.str;
495         // No need to compare depth.
496 }
497
498
499 ///
500 inline
501 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
502         return !(a == b);
503         // No need to compare depth.
504 }
505
506 ///
507 inline
508 bool operator==(Buffer::inset_iterator const & iter1,
509                 Buffer::inset_iterator const & iter2) {
510         return iter1.par == iter2.par
511                 && (iter1.par == 0 || iter1.it == iter2.it);
512 }
513
514 ///
515 inline
516 bool operator!=(Buffer::inset_iterator const & iter1,
517                 Buffer::inset_iterator const & iter2) {
518         return !(iter1 == iter2);
519 }
520 #endif
521