]> git.lyx.org Git - lyx.git/blob - src/buffer.h
03dea1ae29aead681ae2a7c7375773c6bfc37c87
[lyx.git] / src / buffer.h
1 // -*- C++ -*-
2 /* This file is part of
3 * ====================================================== 
4
5 *           LyX, The Document Processor          
6 *           Copyright (C) 1995 Matthias Ettrich
7 *
8 *           This file is Copyleft (C) 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 "undo.h"
30 #include "BufferView.h"
31 #include "lyxvc.h"
32 #include "bufferparams.h"
33 #include "texrow.h"
34 #include "lyxtext.h"
35 #include "support/filetools.h"
36
37 class LyXRC;
38 class TeXErrors;
39 class LaTeXFeatures;
40
41 extern void updateAllVisibleBufferRelatedPopups();
42 extern void WarnReadonly();
43
44 ///
45 struct DEPCLEAN {
46         ///
47         bool clean;
48         ///
49         string master;
50         ///
51         DEPCLEAN *next;
52 };
53
54 /** The buffer object.
55   The is is the buffer object. It contains all the informations about
56   a document loaded into LyX. I am not sure if the class is complete or
57   minimal, probably not.
58   */
59 class Buffer {
60 public:
61         /**@name Constructors and destructor */
62         //@{
63         ///
64         Buffer(string const & file, LyXRC * lyxrc = 0, bool b = false);
65         
66         ///
67         ~Buffer();
68         //@}
69
70         /**@name Methods */
71         //@{
72
73         /** save the buffer's parameters as user default
74           This function saves a file user_lyxdir/templates/defaults.lyx 
75           which parameters are those of the current buffer. This file
76           is used as a default template when creating a new
77           file. Returns true on success.
78           */
79         bool saveParamsAsDefaults();
80
81         /// should be changed to work for a list.
82         void resize()
83         {
84                 if (users) {
85                         users->resize();
86                 } else if (text) {
87                         delete text;
88                         text = 0;
89                 }
90         }
91
92         /// Update window titles of all users
93         void updateTitles();
94
95         /// Reset autosave timers for all users
96         void resetAutosaveTimers();
97
98         /** Adds the BufferView to the users list.
99           Later this func will insert the BufferView into a real list,
100           not just setting a pointer.
101           */
102         void addUser(BufferView * u) { users = u; }
103
104         /** Removes the BufferView from the users list.
105           Since we only can have one at the moment, we just reset it.
106           */
107         void delUser(BufferView *){ users = 0; }
108
109         ///
110         void update(signed char f);
111
112         ///
113         void redraw() {
114               users->redraw(); 
115               users->fitCursor(); 
116               users->updateScrollbar();
117         }
118
119         /// Open and lock an updatable inset
120         void open_new_inset(UpdatableInset *);
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         /// Inserts a lyx file at cursor position. Returns false if it fails.
136         bool insertLyXFile(string const & filename);
137
138         /// Write file. Returns false if unsuccesful.
139         bool writeFile(string const &, bool);
140         
141         ///
142         void writeFileAscii(string const & , int);
143         
144         ///
145         void makeLaTeXFile(string const & filename,
146                            string const & original_path,
147                            bool nice, bool only_body = false);
148
149         ///
150         int runLaTeX();
151
152         ///
153         int runLiterate();
154
155         ///
156         int buildProgram();
157
158         ///
159         int runChktex();
160
161         ///
162         void makeLinuxDocFile(string const & filename, int column);
163         ///
164         void makeDocBookFile(string const & filename, int column);
165
166         /// returns the main language for the buffer (document)
167         string GetLanguage() const {
168                 return params.language;
169         }
170         
171         ///
172         bool isLyxClean() const { return lyx_clean; }
173         
174         /// changed Heinrich Bauer, 23/03/98
175         bool isDviClean();
176         
177         ///
178         bool isNwClean() const { return nw_clean; }
179        
180         ///
181         bool isBakClean() const { return bak_clean; }
182         
183         ///
184         bool isDepClean(string const & name) const;
185         
186         ///
187         void markLyxClean() { 
188                 if (!lyx_clean) {
189                         lyx_clean = true; 
190                         updateTitles();
191                 }
192                 // if the .lyx file has been saved, we don't need an
193                 // autosave 
194                 bak_clean = true;
195         }
196
197         /// changed Heinrich Bauer, 23/03/98
198         void markDviClean();
199         
200         ///
201         void markNwClean() { nw_clean = true; }
202        
203         ///
204         void markBakClean() { bak_clean = true; }
205         
206         ///
207         void markDepClean(string const & name);
208         
209         ///
210         void markDviDirty();
211         
212         ///
213         void markNwDirty() { nw_clean = false; }
214        
215         ///
216         void markDirty() {
217                 if (lyx_clean) {
218                         lyx_clean = false;
219                         updateTitles();
220                 }
221                 dvi_clean_tmpd = false;
222                 dvi_clean_orgd = false;
223                 nw_clean = false;
224                 bak_clean = false;
225                 DEPCLEAN* tmp = dep_clean;
226                 while (tmp) {
227                         tmp->clean = false;
228                         tmp = tmp->next;
229                 }
230         }
231
232         ///
233         string getFileName() const { return filename; }
234
235         /// A transformed version of the file name, adequate for LaTeX  
236         string getLatexName() const {
237                 return ChangeExtension(MakeLatexName(filename), ".tex", true); 
238         }
239
240         /// Change name of buffer. Updates "read-only" flag.
241         void setFileName(string const & newfile);
242
243         /// Name of the document's parent
244         void setParentName(string const &);
245
246         /// Is buffer read-only?
247         bool isReadonly() { return read_only; }
248
249         /// Set buffer read-only flag
250         void setReadonly(bool flag = true) 
251         {
252                 if (read_only != flag) {
253                         read_only = flag; 
254                         updateTitles();
255                         updateAllVisibleBufferRelatedPopups();
256                 }
257                 if (read_only) {
258                         WarnReadonly();
259                 }
260         }
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 or DocBook document
269         bool isSGML() const;
270         /// returns true if the buffer contains a Wed document
271         bool isLiterate() const;
272
273         ///
274         void setPaperStuff();
275
276 #if 0
277         ///
278         void setOldPaperStuff();
279 #endif
280         
281         /** Validate a buffer for LaTeX.
282           This validates the buffer, and returns a struct for use by
283           makeLaTeX and others. Its main use is to figure out what commands
284           and packages need to be included in the LaTeX file. It (should)
285           also check that the needed constructs are there (i.e. that the \refs
286           points to coresponding \labels). It should perhaps inset "error"
287           insets to help the user correct obvious mistakes.
288          */
289         void validate(LaTeXFeatures &); //the correct parameters to be
290                                         //included later 
291
292         /** Insert an inset into the buffer
293           Insert inset into buffer, placing it in a layout of lout,
294           if no_table make sure that it doesn't end up in a table. */
295         void insertInset(Inset *, string const & lout = string(), 
296                          bool no_table = false);
297
298         ///
299         void setCursorFromRow (int);
300
301         ///
302         string getIncludeonlyList(char delim = ',');
303         ///
304         string getReferenceList(char delim = '|');
305         ///
306         string getBibkeyList(char delim = '|');
307         ///
308         bool gotoLabel(string const &);
309
310         /// removes all autodeletable insets
311         bool removeAutoInsets();
312
313         /** This will clearly have to change later. Later we can have more
314           than one user per buffer. */
315         BufferView * getUser() const { return users; }
316
317         //@}
318
319         /// Does this mean that this is buffer local?
320         UndoStack undostack;
321         
322         /// Does this mean that this is buffer local? 
323         UndoStack redostack;
324         
325         ///
326         BufferParams params;
327         
328         /** is a list of paragraphs.
329          */
330         LyXParagraph * paragraph;
331         
332         /** This holds the mapping between buffer paragraphs and screen rows.
333             Should be moved to BufferView. (Asger)
334          */
335         LyXText * text;
336
337         ///
338         UpdatableInset * the_locking_inset;
339
340         /// RCS object
341         LyXVC lyxvc;
342
343         /// where the temporaries go if we want them
344         string tmppath;
345
346         ///
347         string filepath;
348
349         /** While writing as LaTeX, tells whether we are
350             doing a 'nice' LaTeX file */
351         bool niceFile;
352 protected:
353         ///
354         void InsetUnlock();
355         
356         ///
357         inline void InsetSleep();
358         
359         ///
360         inline void InsetWakeup();
361         
362         ///
363         bool inset_slept;
364         
365         ///
366         int  slx;
367         ///
368         int sly;
369 private:
370         ///
371         void insertErrors(TeXErrors &);
372         
373         ///
374         void linuxDocHandleFootnote(FILE * file,
375                                     LyXParagraph * & par, int const depth);
376         ///
377         void DocBookHandleCaption(FILE * file, string & inner_tag,
378                                   int const depth, int desc_on,
379                                   LyXParagraph * &par);
380         ///
381         void DocBookHandleFootnote(FILE * file,
382                                    LyXParagraph * & par, int const depth);
383         ///
384         void sgmlOpenTag(FILE * file, int depth,
385                          string const & latexname) const;
386         ///
387         void sgmlCloseTag(FILE * file, int depth,
388                           string const & latexname) const;
389         ///
390         void LinuxDocError(LyXParagraph * par, int pos, char const * message);
391         ///
392         void SimpleLinuxDocOnePar(FILE * file, LyXParagraph * par,
393                                   int desc_on, int const depth);
394         ///
395         void SimpleDocBookOnePar(string & file, string & extra,
396                                  LyXParagraph * par, int & desc_on,
397                                  int const depth);
398
399         /// LinuxDoc.
400         void push_tag(FILE * file, char const * tag,
401                       int & pos, char stack[5][3]);
402         
403         /// LinuxDoc.
404         void pop_tag(FILE * file, char const * tag,
405                      int & pos, char stack[5][3]);
406
407         ///
408         void RoffAsciiTable(FILE * file, LyXParagraph * par);
409
410         /// is save needed
411         bool lyx_clean;
412         
413         /// is autosave needed
414         bool bak_clean;
415         
416         /** do we need to run LaTeX, changed 23/03/98, Heinrich Bauer
417             We have to distinguish between TeX-runs executed in the original
418             directory (in which the original LyX-file resides) and TeX-runs
419             executed in a temporary directory. The first situation is valid
420             for a dvi-export, the latter one for printing or previewing. */
421         bool dvi_clean_orgd;
422         bool dvi_clean_tmpd;
423
424         /// do we need to run weave/tangle
425         bool nw_clean;
426
427         /// is regenerating .tex necessary
428         DEPCLEAN * dep_clean;
429
430         /// buffer is r/o
431         bool read_only;
432
433         /// name of the file the buffer is associated with.
434         string filename;
435
436         /// Format number of buffer
437         float format;
438         
439         /** A list of views using this buffer.
440           Why not keep a list of the BufferViews that use this buffer?
441
442           At least then we don't have to do a lot of magic like:
443           buffer->lyx_gui->bufferview->updateLayoutChoice. Just ask each
444           of the buffers in the list of users to do a updateLayoutChoice.
445           */
446         BufferView * users;
447
448         ///
449         friend class BufferList;
450         ///
451         friend class BufferView;
452
453         /// Used when typesetting to place errorboxes.
454         TexRow texrow;
455 };
456
457
458 inline  
459 void Buffer::InsetSleep()
460 {
461     if (the_locking_inset && !inset_slept) {
462         the_locking_inset->GetCursorPos(slx, sly);
463         the_locking_inset->InsetUnlock();
464         inset_slept = true;
465     }
466 }
467
468
469 inline  
470 void Buffer::InsetWakeup()
471 {
472     if (the_locking_inset && inset_slept) {
473         the_locking_inset->Edit(slx, sly);
474         inset_slept = false;
475     }   
476 }
477
478
479 inline  
480 void Buffer::setParentName(string const & name)
481 {
482     params.parentname = name;    
483 }
484
485 #endif