]> git.lyx.org Git - lyx.git/blob - src/buffer.h
e1866b7e7359ce7f51d2cb5d83b437a38670f3a7
[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
36 class LyXRC;
37 class TeXErrors;
38 class LaTeXFeatures;
39
40 extern void updateAllVisibleBufferRelatedPopups();
41 extern void WarnReadonly();
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         Buffer(string const & file, LyXRC * lyxrc = 0, 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         /// should be changed to work for a list.
81         void resize()
82         {
83                 if (users) {
84                         users->resize();
85                 } else if (text) {
86                         delete text;
87                         text = 0;
88                 }
89         }
90
91         /// Update window titles of all users
92         void updateTitles();
93
94         /// Reset autosave timers for all users
95         void resetAutosaveTimers();
96
97         /** Adds the BufferView to the users list.
98           Later this func will insert the BufferView into a real list,
99           not just setting a pointer.
100           */
101         void addUser(BufferView *u) { users = u; }
102
103         /** Removes the BufferView from the users list.
104           Since we only can have one at the moment, we just reset it.
105           */
106         void delUser(BufferView *){ users = 0; }
107
108         ///
109         void update(signed char f);
110
111         ///
112         void redraw(void){
113               users->redraw(); 
114               users->fitCursor(); 
115               users->updateScrollbar(); }
116
117         /// Open and lock an updatable inset
118         void open_new_inset(UpdatableInset*);
119
120         ///
121         void loadAutoSaveFile();
122         
123         /** Reads a file. 
124             Returns false if it fails.
125             If par is given, the file is inserted. */
126         bool readFile(LyXLex &, LyXParagraph *par = 0);
127         
128         /** Reads a file without header.
129             Returns false, if file is not completely read.
130             If par is given, the file is inserted. */
131         bool readLyXformat2(LyXLex &,LyXParagraph *par = 0);
132
133         /// Inserts a lyx file at cursor position. Returns false if it fails.
134         bool insertLyXFile(string const & filename);
135
136         /// Write file. Returns false if unsuccesful.
137         bool writeFile(string const &, bool);
138         
139         ///
140         void writeFileAscii(string const & , int);
141         
142         ///
143         void makeLaTeXFile(string const & filename,
144                            string const & original_path,
145                            bool nice, bool only_body = false);
146
147         ///
148         int runLaTeX();
149
150         ///
151         int runLiterate();
152
153         ///
154         int buildProgram();
155
156         ///
157         int runChktex();
158
159         ///
160         void makeLinuxDocFile(string const & filename, int column);
161         ///
162         void makeDocBookFile(string const & filename, int column);
163
164         /// returns the main language for the buffer (document)
165         string GetLanguage() const {
166                 return params.language;
167         }
168         
169         ///
170         bool isLyxClean() const { return lyx_clean; }
171         
172         /// changed Heinrich Bauer, 23/03/98
173         bool isDviClean();
174         
175         ///
176         bool isNwClean() const { return nw_clean; }
177        
178         ///
179         bool isBakClean() const { return bak_clean; }
180         
181         ///
182         bool isDepClean(string const & name) const;
183         
184         ///
185         void markLyxClean() { 
186                 if (!lyx_clean) {
187                         lyx_clean = true; 
188                         updateTitles();
189                 }
190                 // if the .lyx file has been saved, we don't need an
191                 // autosave 
192                 bak_clean = true;
193         }
194
195         /// changed Heinrich Bauer, 23/03/98
196         void markDviClean();
197         
198         ///
199         void markNwClean() { nw_clean = true; }
200        
201         ///
202         void markBakClean() { bak_clean = true; }
203         
204         ///
205         void markDepClean(string const & name);
206         
207         ///
208         void markDviDirty();
209         
210         ///
211         void markNwDirty() { nw_clean = false; }
212        
213         ///
214         void markDirty() {
215                 if (lyx_clean) {
216                         lyx_clean = false;
217                         updateTitles();
218                 }
219                 dvi_clean_tmpd = false;
220                 dvi_clean_orgd = false;
221                 nw_clean = false;
222                 bak_clean = false;
223                 DEPCLEAN* tmp = dep_clean;
224                 while (tmp) {
225                         tmp->clean = false;
226                         tmp = tmp->next;
227                 }
228         }
229
230         ///
231         string getFileName() const { return filename; }
232
233         /// Change name of buffer. Updates "read-only" flag.
234         void setFileName(string const & newfile);
235
236         /// Name of the document's parent
237         void setParentName(string const &);
238
239         /// Is buffer read-only?
240         bool isReadonly() { return read_only; }
241
242         /// Set buffer read-only flag
243         void setReadonly(bool flag = true) 
244         {
245                 if (read_only != flag) {
246                         read_only = flag; 
247                         updateTitles();
248                         updateAllVisibleBufferRelatedPopups();
249                 }
250                 if (read_only) {
251                         WarnReadonly();
252                 }
253         }
254
255         /// returns true if the buffer contains a LaTeX document
256         bool isLatex();
257         /// returns true if the buffer contains a LinuxDoc document
258         bool isLinuxDoc();
259         /// returns true if the buffer contains a DocBook document
260         bool isDocBook();
261         /// returns true if the buffer contains either a LinuxDoc or DocBook document
262         bool isSGML();
263         /// returns true if the buffer contains a Wed document
264         bool isLiterate();
265
266         ///
267         void setPaperStuff();
268
269         ///
270         void setOldPaperStuff();
271         
272         /** Validate a buffer for LaTeX.
273           This validates the buffer, and returns a struct for use by
274           makeLaTeX and others. Its main use is to figure out what commands
275           and packages need to be included in the LaTeX file. It (should)
276           also check that the needed constructs are there (i.e. that the \refs
277           points to coresponding \labels). It should perhaps inset "error"
278           insets to help the user correct obvious mistakes.
279          */
280         void validate(LaTeXFeatures&); //the correct parameters to be
281                                        //included later 
282
283         /** Insert an inset into the buffer
284           Insert inset into buffer, placing it in a layout of lout,
285           if no_table make sure that it doesn't end up in a table. */
286         void insertInset(Inset *, string const &lout=string(), 
287                          bool no_table = false);
288
289         ///
290         void setCursorFromRow (int);
291
292         ///
293         string getIncludeonlyList(char delim=',');
294         ///
295         string getReferenceList(char delim='|');
296         ///
297         string getBibkeyList(char delim='|');
298         ///
299         bool gotoLabel(const string&);
300
301         /// removes all autodeletable insets
302         bool removeAutoInsets();
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         /** This holds the mapping between buffer paragraphs and screen rows.
324             Should be moved to BufferView. (Asger)
325          */
326         LyXText *text;
327
328         ///
329         UpdatableInset* the_locking_inset;
330
331         /// RCS object
332         LyXVC lyxvc;
333
334         /// where the temporaries go if we want them
335         string tmppath;
336
337         ///
338         string filepath;
339
340         /** While writing as LaTeX, tells whether we are
341             doing a 'nice' LaTeX file */
342         bool niceFile;
343 protected:
344         ///
345         void InsetUnlock();
346         
347         ///
348         inline void InsetSleep();
349         
350         ///
351         inline void InsetWakeup();
352         
353         ///
354         bool inset_slept;
355         
356         ///
357         int  slx;
358         ///
359         int sly;
360 private:
361         ///
362         void insertErrors(TeXErrors &);
363         
364         ///
365         void linuxDocHandleFootnote(FILE *file,LyXParagraph* &par, int const depth);
366         ///
367         void DocBookHandleCaption(FILE *file, string &inner_tag,
368                                   int const depth, int desc_on,
369                                   LyXParagraph* &par);
370         ///
371         void DocBookHandleFootnote(FILE *file,LyXParagraph* &par, int const depth);
372         ///
373         void sgmlOpenTag(FILE * file,int depth,string & latexname);
374         ///
375         void sgmlCloseTag(FILE * file,int depth,string & latexname);
376         ///
377         void LinuxDocError(LyXParagraph *par, int pos, char const *message);
378         ///
379         void SimpleLinuxDocOnePar(FILE *file, LyXParagraph *par, int desc_on, int const depth);
380         ///
381         void SimpleDocBookOnePar(string &file, string &extra, LyXParagraph *par, int & desc_on, int const depth);
382
383         /// LinuxDoc.
384         void push_tag(FILE *file, char const *tag, int& pos, char stack[5][3]);
385         
386         /// LinuxDoc.
387         void pop_tag(FILE *file, char const * tag, int& pos, char stack[5][3]);
388
389         ///
390         void RoffAsciiTable(FILE *file, LyXParagraph *par);
391
392         /// is save needed
393         bool lyx_clean;
394         
395         /// is autosave needed
396         bool bak_clean;
397         
398         /// do we need to run LaTeX, changed 23/03/98, Heinrich Bauer
399         /// We have to distinguish between TeX-runs executed in the original
400         /// directory (in which the original LyX-file resides) and TeX-runs
401         /// executed in a temporary directory. The first situation is valid
402         /// for a dvi-export, the latter one for printing or previewing.
403         bool dvi_clean_orgd;
404         bool dvi_clean_tmpd;
405
406         /// do we need to run weave/tangle
407         bool nw_clean;
408
409         /// is regenerating .tex necessary
410         DEPCLEAN *dep_clean;
411
412         /// buffer is r/o
413         bool read_only;
414
415         /// name of the file the buffer is associated with.
416         string filename;
417
418         /// Format number of buffer
419         float format;
420         
421         /** A list of views using this buffer.
422           Why not keep a list of the BufferViews that use this buffer?
423
424           At least then we don't have to do a lot of magic like:
425           buffer->lyx_gui->bufferview->updateLayoutChoice. Just ask each
426           of the buffers in the list of users to do a updateLayoutChoice.
427           */
428         BufferView *users;
429
430         ///
431         friend class BufferList;
432         ///
433         friend class BufferView;
434
435         /// Used when typesetting to place errorboxes.
436         TexRow texrow;
437 };
438
439
440 inline  
441 void Buffer::InsetSleep()
442 {
443     if (the_locking_inset && !inset_slept) {
444         the_locking_inset->GetCursorPos(slx, sly);
445         the_locking_inset->InsetUnlock();
446         inset_slept = true;
447     }
448 }
449
450
451 inline  
452 void Buffer::InsetWakeup()
453 {
454     if (the_locking_inset && inset_slept) {
455         the_locking_inset->Edit(slx, sly);
456         inset_slept = false;
457     }   
458 }
459
460
461 inline  
462 void Buffer::setParentName(string const &name)
463 {
464     params.parentname = name;    
465 }
466
467
468 #endif