]> git.lyx.org Git - lyx.git/blob - src/buffer.h
remove !NEW_INSETS cruft
[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
36
37 class LyXRC;
38 class TeXErrors;
39 class LaTeXFeatures;
40 class Language;
41
42 ///
43 struct DEPCLEAN {
44         ///
45         bool clean;
46         ///
47         string master;
48         ///
49         DEPCLEAN * next;
50 };
51
52 /** The buffer object.
53   The is is the buffer object. It contains all the informations about
54   a document loaded into LyX. I am not sure if the class is complete or
55   minimal, probably not.
56   \author Lars Gullik Bjønnes
57   */
58 class Buffer {
59 public:
60         /// What type of log will \c getLogName() return?
61         enum LogType {
62                 latexlog, ///< LaTeX log
63                 buildlog  ///< Literate build log
64         };
65
66         /** Constructor
67             \param file
68             \param b  optional \c false by default
69         */
70         explicit Buffer(string const & file, bool b = false);
71         
72         /// Destrucotr
73         ~Buffer();
74
75         /** Save the buffer's parameters as user default.
76             This function saves a file \c 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 \c 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         bool Dispatch(string const & command);
87
88         /// Maybe we know the function already by number...
89         bool Dispatch(int ac, string const & argument);
90
91         /// Should be changed to work for a list.
92         void resize();
93         /// 
94         void resizeInsets(BufferView *);
95
96         /// Update window titles of all users.
97         void updateTitles() const;
98
99         /// Reset autosave timers for all users.
100         void resetAutosaveTimers() const;
101
102         /** Adds the BufferView to the users list.
103             Later this func will insert the \c BufferView into a real list,
104             not just setting a pointer.
105         */
106         void addUser(BufferView * u);
107
108         /** Removes the #BufferView# from the users list.
109             Since we only can have one at the moment, we just reset it.
110         */
111         void delUser(BufferView *);
112         
113         ///
114         void redraw();
115
116         /// Load the autosaved file.
117         void loadAutoSaveFile();
118         
119         /** Reads a file. 
120             \param par if != 0 insert the file.
121             \return \c false if method fails.
122         */
123         bool readFile(LyXLex &, LyXParagraph * par = 0);
124         
125         /** Reads a file without header.
126             \param par if != 0 insert the file.
127             \return \c false if file is not completely read.
128         */
129         bool readLyXformat2(LyXLex &, LyXParagraph * par = 0);
130
131         /// This parses a single LyXformat-Token.
132         bool parseSingleLyXformat2Token(LyXLex &, LyXParagraph *& par,
133                                         LyXParagraph *& return_par,
134                                         string const & token, int & pos,
135                                         char & depth, LyXFont &);
136 private:
137         /// Parse a single inset.
138         void readInset(LyXLex &, LyXParagraph *& par, int & pos, LyXFont &);
139 public:
140         /** Save file.
141             Takes care of auto-save files and backup file if requested.
142             Returns \c true if the save is successful, \c false otherwise.
143         */
144         bool save() const;
145         
146         /// Write file. Returns \c false if unsuccesful.
147         bool writeFile(string const &, bool) const;
148         
149         ///
150         void writeFileAscii(string const & , int);
151         ///
152         void writeFileAscii(std::ostream &, int);
153         ///
154         string const asciiParagraph(LyXParagraph const *,
155                                     unsigned int linelen) const;
156         ///
157         void makeLaTeXFile(string const & filename,
158                            string const & original_path,
159                            bool nice, bool only_body = false);
160         /** LaTeX all paragraphs from par to endpar.
161             \param \a endpar if == 0 then to the end
162         */
163         void latexParagraphs(std::ostream & os, LyXParagraph * par,
164                              LyXParagraph * endpar, TexRow & texrow) const;
165
166         ///
167         void SimpleDocBookOnePar(std::ostream &, string & extra,
168                                  LyXParagraph * par, int & desc_on,
169                                  int depth) const ;
170
171         ///
172         int runChktex();
173
174         ///
175         void makeLinuxDocFile(string const & filename,
176                               bool nice, bool only_body = false);
177         ///
178         void makeDocBookFile(string const & filename,
179                              bool nice, bool only_body = false);
180
181         /// returns the main language for the buffer (document)
182         Language const * GetLanguage() const;
183         ///
184         bool isLyxClean() const;
185         ///
186         bool isBakClean() const;
187         ///
188         bool isDepClean(string const & name) const;
189         
190         ///
191         void markLyxClean() const;
192
193         ///
194         void markBakClean();
195         
196         ///
197         void markDepClean(string const & name);
198         
199         ///
200         void setUnnamed(bool flag=true);
201
202         ///
203         bool isUnnamed();
204
205         /// Mark this buffer as dirty.
206         void markDirty();
207
208         /// Returns the buffers filename.
209         string const & fileName() const;
210
211         /** A transformed version of the file name, adequate for LaTeX.
212             \param no_path optional if \c true then the path is stripped.
213         */
214         string const getLatexName(bool no_path = true) const;
215
216         /// Get the name and type of the log.
217         std::pair<LogType, string> const getLogName() const;
218  
219         /// Change name of buffer. Updates "read-only" flag.
220         void setFileName(string const & newfile);
221
222         /// Name of the document's parent
223         void setParentName(string const &);
224
225         /// Is buffer read-only?
226         bool isReadonly() const;
227
228         /// Set buffer read-only flag
229         void setReadonly(bool flag = true);
230
231         /// returns \c true if the buffer contains a LaTeX document
232         bool isLatex() const;
233         /// returns \c true if the buffer contains a LinuxDoc document
234         bool isLinuxDoc() const;
235         /// returns \c true if the buffer contains a DocBook document
236         bool isDocBook() const;
237         /** returns \c true if the buffer contains either a LinuxDoc
238             or DocBook document */
239         bool isSGML() const;
240         /// returns \c true if the buffer contains a Wed document
241         bool isLiterate() const;
242
243         ///
244         void setPaperStuff();
245
246         /** Validate a buffer for LaTeX.
247             This validates the buffer, and returns a struct for use by
248             #makeLaTeX# and others. Its main use is to figure out what
249             commands and packages need to be included in the LaTeX file.
250             It (should) also check that the needed constructs are there
251             (i.e. that the \refs points to coresponding \labels). It
252             should perhaps inset "error" insets to help the user correct
253             obvious mistakes.
254         */
255         void validate(LaTeXFeatures &) const;
256
257         ///
258         string const getIncludeonlyList(char delim = ',');
259         ///
260         std::vector<std::pair<string, string> > const getBibkeyList();
261         ///
262         struct TocItem {
263                 TocItem(LyXParagraph * p, int d, string const & s)
264                         : par(p), depth(d), str(s) {}
265                 ///
266                 LyXParagraph * par;
267                 ///
268                 int depth;
269                 ///
270                 string str;
271         };
272         ///
273         typedef std::vector<TocItem> SingleList;
274         ///
275         typedef std::map<string, SingleList> Lists;
276         ///
277         Lists const getLists() const;
278         ///
279         std::vector<string> const getLabelList();
280
281         /** This will clearly have to change later. Later we can have more
282             than one user per buffer. */
283         BufferView * getUser() const;
284
285         ///
286         void ChangeLanguage(Language const * from, Language const * to);
287         ///
288         bool isMultiLingual();
289
290         /// Does this mean that this is buffer local?
291         UndoStack undostack;
292         
293         /// Does this mean that this is buffer local? 
294         UndoStack redostack;
295         
296         ///
297         BufferParams params;
298         
299         /** The list of paragraphs.
300             This is a linked list of paragraph, this list holds the
301             whole contents of the document.
302          */
303         LyXParagraph * paragraph;
304
305         /// LyX version control object.
306         LyXVC lyxvc;
307
308         /// Where to put temporary files.
309         string tmppath;
310
311         /// The path to the document file.
312         string filepath;
313
314         /** If we are writing a nice LaTeX file or not.
315             While writing as LaTeX, tells whether we are
316             doing a 'nice' LaTeX file */
317         bool niceFile;
318
319         /// Used when typesetting to place errorboxes.
320         TexRow texrow;
321 private:
322         ///
323         void DocBookHandleCaption(std::ostream & os, string & inner_tag,
324                                   int depth, int desc_on,
325                                   LyXParagraph * & par);
326         /// Open SGML/XML tag.
327         void sgmlOpenTag(std::ostream & os, int depth,
328                          string const & latexname) const;
329         /// Closes SGML/XML tag.
330         void sgmlCloseTag(std::ostream & os, int depth,
331                           string const & latexname) const;
332         ///
333         void LinuxDocError(LyXParagraph * par, int pos,
334                            string const & message);
335         ///
336         void SimpleLinuxDocOnePar(std::ostream & os, LyXParagraph * par, int depth);
337
338         /// is save needed
339         mutable bool lyx_clean;
340         
341         /// is autosave needed
342         mutable bool bak_clean;
343         
344         /// is this a unnamed file (New...)
345         bool unnamed;
346
347         /// is regenerating #.tex# necessary
348         DEPCLEAN * dep_clean;
349
350         /// buffer is r/o
351         bool read_only;
352
353         /// name of the file the buffer is associated with.
354         string filename;
355
356         /// Format number of buffer
357         int file_format;
358         /** A list of views using this buffer.
359             Why not keep a list of the BufferViews that use this buffer?
360
361             At least then we don't have to do a lot of magic like:
362             #buffer->lyx_gui->bufferview->updateLayoutChoice#. Just ask each
363             of the buffers in the list of users to do a #updateLayoutChoice#.
364         */
365         BufferView * users;
366
367 public:
368         ///
369         class inset_iterator {
370         public:
371                 typedef std::input_iterator_tag iterator_category;
372                 typedef Inset value_type;
373                 typedef ptrdiff_t difference_type;
374                 typedef Inset * pointer;
375                 typedef Inset & reference;
376                 
377                 
378                 ///
379                 inset_iterator() : par(0) /*, it(0)*/ {}
380                 //
381                 inset_iterator(LyXParagraph * paragraph) : par(paragraph) {
382                         SetParagraph();
383                 }
384                 ///
385                 inset_iterator(LyXParagraph * paragraph,
386                                LyXParagraph::size_type pos);
387                 ///
388                 inset_iterator & operator++() { // prefix ++
389                         if (par) {
390                                 ++it;
391                                 if (it == par->inset_iterator_end()) {
392                                         par = par->next();
393                                         SetParagraph();
394                                 }
395                         }
396                         return *this;
397                 }
398                 ///
399                 inset_iterator operator++(int) { // postfix ++
400                         inset_iterator tmp(par, it.getPos());
401                         if (par) {
402                                 ++it;
403                                 if (it == par->inset_iterator_end()) {
404                                         par = par->next();
405                                         SetParagraph();
406                                 }
407                         }
408                         return tmp;
409                 }
410                 ///
411                 Inset * operator*() { return *it; }
412                 
413                 ///
414                 LyXParagraph * getPar() { return par; }
415                 ///
416                 LyXParagraph::size_type getPos() const { return it.getPos(); }
417                 ///
418                 friend
419                 bool operator==(inset_iterator const & iter1,
420                                 inset_iterator const & iter2);
421         private:
422                 ///
423                 void SetParagraph();
424                 ///
425                 LyXParagraph * par;
426                 ///
427                 LyXParagraph::inset_iterator it;
428         };
429
430         ///
431         inset_iterator inset_iterator_begin() {
432                 return inset_iterator(paragraph);
433         }
434         ///
435         inset_iterator inset_iterator_end() {
436                 return inset_iterator();
437         }
438 };
439
440
441 inline
442 void Buffer::addUser(BufferView * u)
443 {
444         users = u;
445 }
446
447
448 inline
449 void Buffer::delUser(BufferView *)
450 {
451         users = 0;
452 }
453         
454
455 inline
456 void Buffer::redraw()
457 {
458         users->redraw(); 
459         users->fitCursor(users->text); 
460 }
461
462
463 inline
464 Language const * Buffer::GetLanguage() const
465 {
466         return params.language;
467 }
468         
469
470 inline
471 bool Buffer::isLyxClean() const
472 {
473         return lyx_clean;
474 }
475         
476
477 inline
478 bool Buffer::isBakClean() const
479 {
480         return bak_clean;
481 }
482
483
484 inline
485 void Buffer::markLyxClean() const
486
487         if (!lyx_clean) {
488                 lyx_clean = true; 
489                 updateTitles();
490         }
491         // if the .lyx file has been saved, we don't need an
492         // autosave 
493         bak_clean = true;
494 }
495
496
497 inline
498 void Buffer::markBakClean()
499 {
500         bak_clean = true;
501 }
502
503
504 inline
505 void Buffer::setUnnamed(bool flag)
506 {
507         unnamed = flag;
508 }
509
510
511 inline
512 bool Buffer::isUnnamed()
513 {
514         return unnamed;
515 }
516
517
518 inline
519 void Buffer::markDirty()
520 {
521         if (lyx_clean) {
522                 lyx_clean = false;
523                 updateTitles();
524         }
525         bak_clean = false;
526         DEPCLEAN * tmp = dep_clean;
527         while (tmp) {
528                 tmp->clean = false;
529                 tmp = tmp->next;
530         }
531 }
532
533
534 inline
535 string const & Buffer::fileName() const
536 {
537         return filename;
538 }
539
540
541 inline
542 bool Buffer::isReadonly() const
543 {
544         return read_only;
545 }
546
547
548 inline
549 BufferView * Buffer::getUser() const
550 {
551         return users;
552 }
553
554
555 inline  
556 void Buffer::setParentName(string const & name)
557 {
558         params.parentname = name;    
559 }
560
561
562 ///
563 inline
564 bool operator==(Buffer::TocItem const & a, Buffer::TocItem const & b) {
565         return a.par == b.par && a.str == b.str;
566         // No need to compare depth.
567 }
568
569
570 ///
571 inline
572 bool operator!=(Buffer::TocItem const & a, Buffer::TocItem const & b) {
573         return !(a == b);
574         // No need to compare depth.
575 }
576
577 ///
578 inline
579 bool operator==(Buffer::inset_iterator const & iter1,
580                 Buffer::inset_iterator const & iter2) {
581         return iter1.par == iter2.par
582                 && (iter1.par == 0 || iter1.it == iter2.it);
583 }
584
585 ///
586 inline
587 bool operator!=(Buffer::inset_iterator const & iter1,
588                 Buffer::inset_iterator const & iter2) {
589         return !(iter1 == iter2);
590 }
591 #endif
592