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