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