]> git.lyx.org Git - lyx.git/blob - src/buffer.h
32f669e59acb8021c94f9a5c34719d2d8eb3bcfc
[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         ///
158         int runLaTeX();
159
160         ///
161         int runLiterate();
162
163         ///
164         int buildProgram();
165
166         ///
167         int runChktex();
168
169         ///
170         void makeLinuxDocFile(string const & filename, int column);
171         ///
172         void makeDocBookFile(string const & filename, int column);
173
174         /// returns the main language for the buffer (document)
175         string GetLanguage() const {
176                 return params.language;
177         }
178         
179         ///
180         bool isLyxClean() const { return lyx_clean; }
181         
182         /// changed Heinrich Bauer, 23/03/98
183         bool isDviClean() const;
184         
185         ///
186         bool isNwClean() const { return nw_clean; }
187        
188         ///
189         bool isBakClean() const { return bak_clean; }
190         
191         ///
192         bool isDepClean(string const & name) const;
193         
194         ///
195         void markLyxClean() const { 
196                 if (!lyx_clean) {
197                         lyx_clean = true; 
198                         updateTitles();
199                 }
200                 // if the .lyx file has been saved, we don't need an
201                 // autosave 
202                 bak_clean = true;
203         }
204
205         /// changed Heinrich Bauer, 23/03/98
206         void markDviClean();
207         
208         ///
209         void markNwClean() { nw_clean = true; }
210        
211         ///
212         void markBakClean() { bak_clean = true; }
213         
214         ///
215         void markDepClean(string const & name);
216         
217         ///
218         void markDviDirty();
219         
220         ///
221         void markNwDirty() { nw_clean = false; }
222        
223         ///
224         void markDirty() {
225                 if (lyx_clean) {
226                         lyx_clean = false;
227                         updateTitles();
228                 }
229                 dvi_clean_tmpd = false;
230                 dvi_clean_orgd = false;
231                 nw_clean = false;
232                 bak_clean = false;
233                 DEPCLEAN * tmp = dep_clean;
234                 while (tmp) {
235                         tmp->clean = false;
236                         tmp = tmp->next;
237                 }
238         }
239
240         ///
241         string const & fileName() const { return filename; }
242
243         /** A transformed version of the file name, adequate for LaTeX  
244             The path is stripped if no_path is true (default) */
245         string getLatexName(bool no_path = true) const;
246
247         /// Change name of buffer. Updates "read-only" flag.
248         void fileName(string const & newfile);
249
250         /// Name of the document's parent
251         void setParentName(string const &);
252
253         /// Is buffer read-only?
254         bool isReadonly() const { return read_only; }
255
256         /// Set buffer read-only flag
257         void setReadonly(bool flag = true);
258
259         /// returns true if the buffer contains a LaTeX document
260         bool isLatex() const;
261         /// returns true if the buffer contains a LinuxDoc document
262         bool isLinuxDoc() const;
263         /// returns true if the buffer contains a DocBook document
264         bool isDocBook() const;
265         /** returns true if the buffer contains either a LinuxDoc
266             or DocBook document */
267         bool isSGML() const;
268         /// returns true if the buffer contains a Wed document
269         bool isLiterate() const;
270
271         ///
272         void setPaperStuff();
273
274         /** Validate a buffer for LaTeX.
275             This validates the buffer, and returns a struct for use by
276             makeLaTeX and others. Its main use is to figure out what
277             commands and packages need to be included in the LaTeX file.
278             It (should) also check that the needed constructs are there
279             (i.e. that the \refs points to coresponding \labels). It
280             should perhaps inset "error" insets to help the user correct
281             obvious mistakes.
282         */
283         void validate(LaTeXFeatures &) const;
284
285         ///
286         string getIncludeonlyList(char delim = ',');
287         ///
288         string getReferenceList(char delim = '|');
289         ///
290         string getBibkeyList(char delim = '|');
291
292         /** This will clearly have to change later. Later we can have more
293             than one user per buffer. */
294         BufferView * getUser() const { return users; }
295
296         ///
297         void ChangeLanguage(Language const * from, Language const * to);
298         ///
299         bool isMultiLingual();
300
301         //@}
302
303         /// Does this mean that this is buffer local?
304         UndoStack undostack;
305         
306         /// Does this mean that this is buffer local? 
307         UndoStack redostack;
308         
309         ///
310         BufferParams params;
311         
312         /** is a list of paragraphs.
313          */
314         LyXParagraph * paragraph;
315
316         /// RCS object
317         LyXVC lyxvc;
318
319         /// where the temporaries go if we want them
320         string tmppath;
321
322         ///
323         string filepath;
324
325         /** While writing as LaTeX, tells whether we are
326             doing a 'nice' LaTeX file */
327         bool niceFile;
328
329         /// Used when typesetting to place errorboxes.
330         TexRow texrow;
331 private:
332         ///
333         void linuxDocHandleFootnote(std::ostream & os,
334                                     LyXParagraph * & par, int const depth);
335         ///
336         void DocBookHandleCaption(std::ostream & os, string & inner_tag,
337                                   int const depth, int desc_on,
338                                   LyXParagraph * & par);
339         ///
340         void DocBookHandleFootnote(std::ostream & os,
341                                    LyXParagraph * & par, int const depth);
342         ///
343         void sgmlOpenTag(std::ostream & os, int depth,
344                          string const & latexname) const;
345         ///
346         void sgmlCloseTag(std::ostream & os, int depth,
347                           string const & latexname) const;
348         ///
349         void LinuxDocError(LyXParagraph * par, int pos, char const * message);
350         ///
351         void SimpleLinuxDocOnePar(std::ostream & os, LyXParagraph * par,
352                                   int desc_on, int const depth);
353         ///
354         void SimpleDocBookOnePar(std::ostream &, string & extra,
355                                  LyXParagraph * par, int & desc_on,
356                                  int const depth);
357
358         /// LinuxDoc.
359         void push_tag(std::ostream & os, char const * tag,
360                       int & pos, char stack[5][3]);
361         
362         /// LinuxDoc.
363         void pop_tag(std::ostream & os, char const * tag,
364                      int & pos, char stack[5][3]);
365
366 #if 0
367         ///
368         void RoffAsciiTable(std::ostream &, LyXParagraph * par);
369 #endif
370         
371         /// is save needed
372         mutable bool lyx_clean;
373         
374         /// is autosave needed
375         mutable bool bak_clean;
376         
377         /** do we need to run LaTeX, changed 23/03/98, Heinrich Bauer
378             We have to distinguish between TeX-runs executed in the original
379             directory (in which the original LyX-file resides) and TeX-runs
380             executed in a temporary directory. The first situation is valid
381             for a dvi-export, the latter one for printing or previewing. */
382         bool dvi_clean_orgd;
383         bool dvi_clean_tmpd;
384
385         /// do we need to run weave/tangle
386         bool nw_clean;
387
388         /// is regenerating .tex necessary
389         DEPCLEAN * dep_clean;
390
391         /// buffer is r/o
392         bool read_only;
393
394         /// name of the file the buffer is associated with.
395         string filename;
396
397         /// Format number of buffer
398         float format;
399         
400         /** A list of views using this buffer.
401             Why not keep a list of the BufferViews that use this buffer?
402
403             At least then we don't have to do a lot of magic like:
404             buffer->lyx_gui->bufferview->updateLayoutChoice. Just ask each
405             of the buffers in the list of users to do a updateLayoutChoice.
406         */
407         BufferView * users;
408 };
409
410
411 inline  
412 void Buffer::setParentName(string const & name)
413 {
414         params.parentname = name;    
415 }
416
417 #endif