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