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