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