]> git.lyx.org Git - lyx.git/blob - src/paragraph.h
* src/buffer_funcs.[Ch]
[lyx.git] / src / paragraph.h
1 // -*- C++ -*-
2 /**
3  * \file paragraph.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Asger Alstrup
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Jürgen Vigna
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #ifndef PARAGRAPH_H
17 #define PARAGRAPH_H
18
19 #include "changes.h"
20 #include "dimension.h"
21 #include "InsetList.h"
22 #include "lyxlayout_ptr_fwd.h"
23 #include "RowList_fwd.h"
24
25 #include "insets/insetbase.h" // only for InsetBase::Code
26
27 #include "support/types.h"
28
29 #include <string>
30
31 class Buffer;
32 class BufferParams;
33 class BufferView;
34 class Counters;
35 class InsetBase;
36 class InsetBibitem;
37 class LaTeXFeatures;
38 class InsetBase_code;
39 class Language;
40 class LyXFont;
41 class LyXFont_size;
42 class MetricsInfo;
43 class OutputParams;
44 class PainterInfo;
45 class ParagraphParameters;
46 class TexRow;
47
48
49 class FontSpan {
50 public:
51         /// Invalid font span containing no character
52         FontSpan() : first(0), last(-1) {}
53         /// Span including first and last
54         FontSpan(lyx::pos_type f, lyx::pos_type l) : first(f), last(l) {}
55
56 public:
57         /// Range including first and last.
58         lyx::pos_type first, last;
59 };
60
61
62 /// A Paragraph holds all text, attributes and insets in a text paragraph
63 class Paragraph  {
64 public:
65         ///
66         enum {
67                 /// Note that this is 1 right now to avoid
68                 /// crashes where getChar() is called wrongly
69                 /// (returning 0) - if this was 0, then we'd
70                 /// try getInset() and crash. We should fix
71                 /// all these places.
72                 META_INSET = 1
73                 //META_INSET = 0x200001  // above 0x10ffff, for ucs-4
74         };
75         enum ChangeTracking
76         {
77                 /// Change tracking is "on" in this buffer
78                 trackingOn,
79                 /// Change tracking is "off" in this buffer
80                 trackingOff,
81                 /// Change tracking is "unknown" in this buffer
82                 trackingUnknown
83         };
84         ///
85         typedef lyx::char_type value_type;
86         ///
87         typedef lyx::depth_type depth_type;
88         ///
89         typedef std::vector<value_type> TextContainer;
90
91         ///
92         Paragraph();
93         ///
94         Paragraph(Paragraph const &);
95         ///
96         Paragraph & operator=(Paragraph const &);
97         ///
98         ~Paragraph();
99         ///
100         int id() const;
101
102
103         ///
104         Language const * getParLanguage(BufferParams const &) const;
105         ///
106         bool isRightToLeftPar(BufferParams const &) const;
107         ///
108         void changeLanguage(BufferParams const & bparams,
109                             Language const * from, Language const * to);
110         ///
111         bool isMultiLingual(BufferParams const &) const;
112
113         ///
114         std::string const asString(Buffer const &,
115                                    OutputParams const & runparams,
116                                    bool label) const;
117         ///
118         std::string const asString(Buffer const &, bool label) const;
119         ///
120         std::string const asString(Buffer const & buffer,
121                                    lyx::pos_type beg,
122                                    lyx::pos_type end,
123                                    bool label) const;
124         ///
125         std::string const asString(Buffer const &,
126                                    OutputParams const & runparams,
127                                    lyx::pos_type beg,
128                                    lyx::pos_type end,
129                                    bool label) const;
130
131         ///
132         void write(Buffer const &, std::ostream &, BufferParams const &,
133                    depth_type & depth) const;
134         ///
135         void validate(LaTeXFeatures &) const;
136
137         ///
138         int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
139
140         ///
141         int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
142
143
144         ///
145         bool simpleTeXOnePar(Buffer const &, BufferParams const &,
146                              LyXFont const & outerfont, std::ostream &,
147                              TexRow & texrow, OutputParams const &) const;
148
149         /// Writes to stream the content of the paragraph for linuxdoc
150         void simpleLinuxDocOnePar(Buffer const & buf,
151                                   std::ostream & os,
152                                   LyXFont const & outerfont,
153                                   OutputParams const & runparams,
154                                   lyx::depth_type depth) const;
155
156         /// Can we drop the standard paragraph wrapper?
157         bool emptyTag() const;
158
159         /// Get the id of the paragraph, usefull for docbook and linuxdoc
160         std::string getID(Buffer const & buf,
161                           OutputParams const & runparams) const;
162
163         // Get the first word of a paragraph, return the position where it left
164         lyx::pos_type getFirstWord(Buffer const & buf,
165                                    std::ostream & os,
166                                    OutputParams const & runparams) const;
167
168         /// Checks if the paragraph contains only text and no inset or font change.
169         bool onlyText(Buffer const & buf, LyXFont const & outerfont,
170                       lyx::pos_type initial) const;
171
172         /// Writes to stream the docbook representation
173         void simpleDocBookOnePar(Buffer const & buf,
174                                  std::ostream &,
175                                  OutputParams const & runparams,
176                                  LyXFont const & outerfont,
177                                  lyx::pos_type initial = 0) const;
178
179         ///
180         bool hasSameLayout(Paragraph const & par) const;
181
182         ///
183         void makeSameLayout(Paragraph const & par);
184
185         ///
186         void setInsetOwner(InsetBase * inset);
187         ///
188         InsetBase * inInset() const;
189         ///
190         InsetBase::Code ownerCode() const;
191         ///
192         bool forceDefaultParagraphs() const;
193
194         ///
195         lyx::pos_type size() const { return text_.size(); }
196         ///
197         bool empty() const { return text_.empty(); }
198         ///
199         void setContentsFromPar(Paragraph const & par);
200         ///
201         void clearContents();
202
203         ///
204         LyXLayout_ptr const & layout() const;
205         ///
206         void layout(LyXLayout_ptr const & new_layout);
207
208         /// This is the item depth, only used by enumerate and itemize
209         signed char itemdepth;
210
211         ///
212         InsetBibitem * bibitem() const;  // ale970302
213
214         /// initialise tracking for this par
215         void trackChanges(Change::Type = Change::UNCHANGED);
216
217         /// stop tracking
218         void untrackChanges();
219
220         /// set entire paragraph to new text for change tracking
221         void cleanChanges(ChangeTracking ct = trackingUnknown);
222
223         /// look up change type at given pos
224         Change::Type lookupChange(lyx::pos_type pos) const;
225
226         /// look up change at given pos
227         Change const lookupChangeFull(lyx::pos_type pos) const;
228
229         /// is there a change within the given range ?
230         bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
231
232         /// is there a non-addition in this range ?
233         bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
234
235         /// set change at pos
236         void setChange(lyx::pos_type pos, Change::Type type);
237
238         /// set full change at pos
239         void setChangeFull(lyx::pos_type pos, Change change);
240
241         /// accept change
242         void acceptChange(lyx::pos_type start, lyx::pos_type end);
243
244         /// reject change
245         void rejectChange(lyx::pos_type start, lyx::pos_type end);
246
247         /// mark whole par as erased or not
248         void markErased(bool erased);
249
250         /// Paragraphs can contain "manual labels", for example, Description
251         /// environment. The text for this user-editable label is stored in
252         /// the paragraph alongside the text of the rest of the paragraph
253         /// (the body). This function returns the starting position of the
254         /// body of the text in the paragraph.
255         lyx::pos_type beginOfBody() const;
256         /// recompute this value
257         void setBeginOfBody();
258
259         ///
260         std::string const & getLabelstring() const;
261
262         /// the next two functions are for the manual labels
263         std::string const getLabelWidthString() const;
264         ///
265         void setLabelWidthString(std::string const & s);
266         ///
267         char getAlign() const;
268         /// The nesting depth of a paragraph
269         depth_type getDepth() const;
270         /// The maximal possible depth of a paragraph after this one
271         depth_type getMaxDepthAfter() const;
272         ///
273         void applyLayout(LyXLayout_ptr const & new_layout);
274
275         /// definite erase
276         void eraseIntern(lyx::pos_type pos);
277         /// erase the char at the given position
278         bool erase(lyx::pos_type pos);
279         /// erase the given range. Returns the number of chars actually erased
280         int erase(lyx::pos_type start, lyx::pos_type end);
281
282         /** Get uninstantiated font setting. Returns the difference
283             between the characters font and the layoutfont.
284             This is what is stored in the fonttable
285         */
286         LyXFont const
287         getFontSettings(BufferParams const &, lyx::pos_type pos) const;
288         ///
289         LyXFont const getFirstFontSettings(BufferParams const &) const;
290
291         /** Get fully instantiated font. If pos == -1, use the layout
292             font attached to this paragraph.
293             If pos == -2, use the label font of the layout attached here.
294             In all cases, the font is instantiated, i.e. does not have any
295             attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
296             LyXFont::TOGGLE.
297         */
298         LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
299                               LyXFont const & outerfont) const;
300         LyXFont const getLayoutFont(BufferParams const &,
301                                     LyXFont const & outerfont) const;
302         LyXFont const getLabelFont(BufferParams const &,
303                                    LyXFont const & outerfont) const;
304         /**
305          * The font returned by the above functions is the same in a
306          * span of characters. This method will return the first and
307          * the last positions in the paragraph for which that font is
308          * the same. This can be used to avoid unnecessary calls to
309    * getFont.
310          */
311         FontSpan fontSpan(lyx::pos_type pos) const;
312         ///
313         /// this is a bottleneck.
314         value_type getChar(lyx::pos_type pos) const { return text_[pos]; }
315         /// Get the char, but mirror all bracket characters if it is right-to-left
316         value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
317         /// The position must already exist.
318         void setChar(lyx::pos_type pos, value_type c);
319         /// pos <= size() (there is a dummy font change at the end of each par)
320         void setFont(lyx::pos_type pos, LyXFont const & font);
321         /// Returns the height of the highest font in range
322         LyXFont_size highestFontInRange(lyx::pos_type startpos,
323                                         lyx::pos_type endpos,
324                                         LyXFont_size def_size) const;
325         ///
326         void insert(lyx::pos_type pos, std::string const & str,
327                     LyXFont const & font);
328         ///
329         void insertChar(lyx::pos_type pos, value_type c,
330                         Change change = Change::INSERTED);
331         ///
332         void insertChar(lyx::pos_type pos, value_type c,
333                 LyXFont const &, Change change = Change::INSERTED);
334         ///
335         void insertInset(lyx::pos_type pos, InsetBase * inset,
336                          Change change = Change::INSERTED);
337         ///
338         void insertInset(lyx::pos_type pos, InsetBase * inset,
339                 LyXFont const &, Change change = Change::INSERTED);
340         ///
341         bool insetAllowed(InsetBase_code code);
342         ///
343         InsetBase * getInset(lyx::pos_type pos) {
344                 return insetlist.get(pos);
345         }
346         ///
347         InsetBase const * getInset(lyx::pos_type pos) const {
348                 return insetlist.get(pos);
349         }
350
351         ///
352         bool isHfill(lyx::pos_type pos) const {
353         return isInset(pos)
354                 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
355         }
356         /// hinted by profiler
357         bool isInset(lyx::pos_type pos) const {
358                 return getChar(pos) == static_cast<value_type>(META_INSET);
359         }
360         ///
361         bool isNewline(lyx::pos_type pos) const;
362         /// return true if the char is a word separator
363         bool isSeparator(lyx::pos_type pos) const { return getChar(pos) == ' '; }
364         ///
365         bool isLineSeparator(lyx::pos_type pos) const;
366         /// True if the character/inset at this point can be part of a word
367         // Note that digits in particular are considered as letters
368         bool isLetter(lyx::pos_type pos) const;
369
370         /// returns -1 if inset not found
371         int getPositionOfInset(InsetBase const * inset) const;
372
373         /// Returns the number of line breaks and white-space stripped at the start
374         int stripLeadingSpaces();
375
376         /// return true if we allow multiple spaces
377         bool isFreeSpacing() const;
378
379         /// return true if we allow this par to stay empty
380         bool allowEmpty() const;
381         ///
382         unsigned char transformChar(unsigned char c, lyx::pos_type pos) const;
383         ///
384         ParagraphParameters & params();
385         ///
386         ParagraphParameters const & params() const;
387
388         ///
389         Row & getRow(lyx::pos_type pos, bool boundary);
390         ///
391         Row const & getRow(lyx::pos_type pos, bool boundary) const;
392         ///
393         size_t pos2row(lyx::pos_type pos) const;
394
395         /// total height of paragraph
396         unsigned int height() const { return dim_.height(); }
397         /// total width of paragraph, may differ from workwidth
398         unsigned int width() const { return dim_.width(); }
399         /// ascend of paragraph above baseline
400         unsigned int ascent() const { return dim_.ascent(); }
401         /// descend of paragraph below baseline
402         unsigned int descent() const { return dim_.descent(); }
403         /// LyXText updates the rows using this access point
404         RowList & rows() { return rows_; }
405         /// The painter and others use this
406         RowList const & rows() const { return rows_; }
407         ///
408         RowSignature & rowSignature() const { return rowSignature_; }
409
410         /// LyXText::redoParagraph updates this
411         Dimension & dim() { return dim_; }
412
413         /// dump some information to lyxerr
414         void dump() const;
415
416 public:
417         ///
418         InsetList insetlist;
419
420 private:
421         /// cached dimensions of paragraph
422         Dimension dim_;
423
424         ///
425         mutable RowList rows_;
426         ///
427         mutable RowSignature rowSignature_;
428
429         ///
430         LyXLayout_ptr layout_;
431         /**
432          * Keeping this here instead of in the pimpl makes LyX >10% faster
433          * for average tasks as buffer loading/switching etc.
434          */
435         TextContainer text_;
436         /// end of label
437         lyx::pos_type begin_of_body_;
438
439         /// Pimpl away stuff
440         class Pimpl;
441         ///
442         friend class Paragraph::Pimpl;
443         ///
444         Pimpl * pimpl_;
445 };
446
447
448 inline bool isInsertedText(Paragraph const & par, lyx::pos_type pos)
449 {
450         return par.lookupChange(pos) == Change::INSERTED;
451 }
452
453
454 inline bool isDeletedText(Paragraph const & par, lyx::pos_type pos)
455 {
456         return par.lookupChange(pos) == Change::DELETED;
457 }
458
459 #endif // PARAGRAPH_H