]> git.lyx.org Git - lyx.git/blob - src/buffer.h
db9af3a238b87bf48ff7969ea67a54976529c19b
[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
38 class LyXRC;
39 class TeXErrors;
40 class LaTeXFeatures;
41
42 extern void updateAllVisibleBufferRelatedPopups();
43 extern void WarnReadonly();
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, LyXRC * lyxrc = 0, 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                 {
93                         if (users) {
94                                 users->resize();
95                         }
96                 }
97
98         /// Update window titles of all users
99         void updateTitles();
100
101         /// Reset autosave timers for all users
102         void resetAutosaveTimers();
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         /// Open and lock an updatable inset
123         void open_new_inset(UpdatableInset *);
124
125         ///
126         void loadAutoSaveFile();
127         
128         /** Reads a file. 
129             Returns false if it fails.
130             If par is given, the file is inserted. */
131         bool readFile(LyXLex &, LyXParagraph * par = 0);
132         
133         /** Reads a file without header.
134             Returns false, if file is not completely read.
135             If par is given, the file is inserted. */
136         bool readLyXformat2(LyXLex &, LyXParagraph * par = 0);
137
138         /// Inserts a lyx file at cursor position. Returns false if it fails.
139         bool insertLyXFile(string const & filename);
140
141         /// Write file. Returns false if unsuccesful.
142         bool writeFile(string const &, bool);
143         
144         ///
145         void writeFileAscii(string const & , int);
146         
147         ///
148         void makeLaTeXFile(string const & filename,
149                            string const & original_path,
150                            bool nice, bool only_body = false);
151
152         ///
153         int runLaTeX();
154
155         ///
156         int runLiterate();
157
158         ///
159         int buildProgram();
160
161         ///
162         int runChktex();
163
164         ///
165         void makeLinuxDocFile(string const & filename, int column);
166         ///
167         void makeDocBookFile(string const & filename, int column);
168
169         /// returns the main language for the buffer (document)
170         string GetLanguage() const {
171                 return params.language;
172         }
173         
174         ///
175         bool isLyxClean() const { return lyx_clean; }
176         
177         /// changed Heinrich Bauer, 23/03/98
178         bool isDviClean();
179         
180         ///
181         bool isNwClean() const { return nw_clean; }
182        
183         ///
184         bool isBakClean() const { return bak_clean; }
185         
186         ///
187         bool isDepClean(string const & name) const;
188         
189         ///
190         void markLyxClean() { 
191                 if (!lyx_clean) {
192                         lyx_clean = true; 
193                         updateTitles();
194                 }
195                 // if the .lyx file has been saved, we don't need an
196                 // autosave 
197                 bak_clean = true;
198         }
199
200         /// changed Heinrich Bauer, 23/03/98
201         void markDviClean();
202         
203         ///
204         void markNwClean() { nw_clean = true; }
205        
206         ///
207         void markBakClean() { bak_clean = true; }
208         
209         ///
210         void markDepClean(string const & name);
211         
212         ///
213         void markDviDirty();
214         
215         ///
216         void markNwDirty() { nw_clean = false; }
217        
218         ///
219         void markDirty() {
220                 if (lyx_clean) {
221                         lyx_clean = false;
222                         updateTitles();
223                 }
224                 dvi_clean_tmpd = false;
225                 dvi_clean_orgd = false;
226                 nw_clean = false;
227                 bak_clean = false;
228                 DEPCLEAN * tmp = dep_clean;
229                 while (tmp) {
230                         tmp->clean = false;
231                         tmp = tmp->next;
232                 }
233         }
234
235         ///
236         string const & fileName() const { return filename; }
237
238         /// A transformed version of the file name, adequate for LaTeX  
239         /* The path is stripped if no_path is true (default) */
240         string getLatexName(bool no_path = true) const {
241                 return ChangeExtension(MakeLatexName(filename), 
242                                        ".tex", no_path); 
243         }
244
245         /// Change name of buffer. Updates "read-only" flag.
246         void fileName(string const & newfile);
247
248         /// Name of the document's parent
249         void setParentName(string const &);
250
251         /// Is buffer read-only?
252         bool isReadonly() { return read_only; }
253
254         /// Set buffer read-only flag
255         void setReadonly(bool flag = true) 
256                 {
257                         if (read_only != flag) {
258                                 read_only = flag; 
259                                 updateTitles();
260                                 updateAllVisibleBufferRelatedPopups();
261                         }
262                         if (read_only) {
263                                 WarnReadonly();
264                         }
265                 }
266
267         /// returns true if the buffer contains a LaTeX document
268         bool isLatex() const;
269         /// returns true if the buffer contains a LinuxDoc document
270         bool isLinuxDoc() const;
271         /// returns true if the buffer contains a DocBook document
272         bool isDocBook() const;
273         /// returns true if the buffer contains either a LinuxDoc or DocBook document
274         bool isSGML() const;
275         /// returns true if the buffer contains a Wed document
276         bool isLiterate() const;
277
278         ///
279         void setPaperStuff();
280
281 #if 0
282         ///
283         void setOldPaperStuff();
284 #endif
285         
286         /** Validate a buffer for LaTeX.
287             This validates the buffer, and returns a struct for use by
288             makeLaTeX and others. Its main use is to figure out what commands
289             and packages need to be included in the LaTeX file. It (should)
290             also check that the needed constructs are there (i.e. that the \refs
291             points to coresponding \labels). It should perhaps inset "error"
292             insets to help the user correct obvious mistakes.
293         */
294         void validate(LaTeXFeatures &); //the correct parameters to be
295         //included later 
296
297         /** Insert an inset into the buffer
298             Insert inset into buffer, placing it in a layout of lout,
299             if no_table make sure that it doesn't end up in a table. */
300         void insertInset(Inset *, string const & lout = string(), 
301                          bool no_table = false);
302
303         ///
304         void setCursorFromRow (int);
305
306         ///
307         string getIncludeonlyList(char delim = ',');
308         ///
309         string getReferenceList(char delim = '|');
310         ///
311         string getBibkeyList(char delim = '|');
312         ///
313         bool gotoLabel(string const &);
314
315         /// removes all autodeletable insets
316         bool removeAutoInsets();
317
318         /** This will clearly have to change later. Later we can have more
319             than one user per buffer. */
320         BufferView * getUser() const { return users; }
321
322         //@}
323
324         /// Does this mean that this is buffer local?
325         UndoStack undostack;
326         
327         /// Does this mean that this is buffer local? 
328         UndoStack redostack;
329         
330         ///
331         BufferParams params;
332         
333         /** is a list of paragraphs.
334          */
335         LyXParagraph * paragraph;
336
337         /// per view not per buffer?
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
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(ostream & os,
375                                     LyXParagraph * & par, int const depth);
376         ///
377         void DocBookHandleCaption(ostream & os, string & inner_tag,
378                                   int const depth, int desc_on,
379                                   LyXParagraph * & par);
380         ///
381         void DocBookHandleFootnote(ostream & os,
382                                    LyXParagraph * & par, int const depth);
383         ///
384         void sgmlOpenTag(ostream & os, int depth,
385                          string const & latexname) const;
386         ///
387         void sgmlCloseTag(ostream & os, int depth,
388                           string const & latexname) const;
389         ///
390         void LinuxDocError(LyXParagraph * par, int pos, char const * message);
391         ///
392         void SimpleLinuxDocOnePar(ostream & os, 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(ostream & os, char const * tag,
401                       int & pos, char stack[5][3]);
402         
403         /// LinuxDoc.
404         void pop_tag(ostream & os, char const * tag,
405                      int & pos, char stack[5][3]);
406         
407         ///
408         void RoffAsciiTable(ostream &, 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         /// Used when typesetting to place errorboxes.
449         TexRow texrow;
450 };
451
452
453 inline  
454 void Buffer::InsetSleep()
455 {
456         if (the_locking_inset && !inset_slept) {
457                 the_locking_inset->GetCursorPos(slx, sly);
458                 the_locking_inset->InsetUnlock();
459                 inset_slept = true;
460         }
461 }
462
463
464 inline  
465 void Buffer::InsetWakeup()
466 {
467         if (the_locking_inset && inset_slept) {
468                 the_locking_inset->Edit(slx, sly);
469                 inset_slept = false;
470         }       
471 }
472
473
474 inline  
475 void Buffer::setParentName(string const & name)
476 {
477         params.parentname = name;    
478 }
479
480 #endif