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