]> git.lyx.org Git - features.git/blob - src/Paragraph.h
97d36dc443b659667b28a076281ef0ba584ffe8f
[features.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 "FontEnums.h"
20 #include "SpellChecker.h"
21
22 #include "support/strfwd.h"
23 #include "support/types.h"
24
25 #include <set>
26
27 namespace lyx {
28
29 class AuthorList;
30 class Buffer;
31 class BufferParams;
32 class Change;
33 class Counters;
34 class Cursor;
35 class CursorSlice;
36 class DocIterator;
37 class docstring_list;
38 class DocumentClass;
39 class Inset;
40 class InsetBibitem;
41 class LaTeXFeatures;
42 class Inset_code;
43 class InsetList;
44 class Language;
45 class Layout;
46 class Font;
47 class Font_size;
48 class MetricsInfo;
49 class OutputParams;
50 class PainterInfo;
51 class ParagraphParameters;
52 class TexRow;
53 class Toc;
54 class WordLangTuple;
55 class XHTMLStream;
56 class otexstream;
57
58 class FontSpan {
59 public:
60         /// Invalid font span containing no character
61         FontSpan() : first(0), last(-1) {}
62         /// Span including first and last
63         FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
64
65 public:
66         /// Range including first and last.
67         pos_type first, last;
68
69         inline bool operator<(FontSpan const & s) const
70         {
71                 return first < s.first;
72         }
73         
74         inline bool operator==(FontSpan const & s) const
75         {
76                 return first == s.first && last == s.last;
77         }
78
79         inline bool inside(pos_type p) const
80         {
81                 return first <= p && p <= last;
82         }
83
84         inline size_t size() const
85         {
86                 return empty() ? 0 : last - first;
87         }
88         
89
90         inline FontSpan intersect(FontSpan const & f) const
91         {
92                 FontSpan result = FontSpan();
93                 if (inside(f.first))
94                         result.first = f.first;
95                 else if (f.inside(first))
96                         result.first = first;
97                 else
98                         return result;
99                 if (inside(f.last))
100                         result.last = f.last;
101                 else if (f.inside(last))
102                         result.last = last;
103                 return result;
104         }
105         
106         inline bool empty() const
107         {
108                 return first > last;
109         }
110 };
111
112 ///
113 enum TextCase {
114         ///
115         text_lowercase = 0,
116         ///
117         text_capitalization = 1,
118         ///
119         text_uppercase = 2
120 };
121
122
123 ///
124 enum AsStringParameter
125 {
126         AS_STR_NONE = 0, ///< No option, only printable characters.
127         AS_STR_LABEL = 1, ///< Prefix with paragraph label.
128         AS_STR_INSETS = 2, ///< Go into insets.
129         AS_STR_NEWLINES = 4, ///< Get also newline characters.
130         AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
131         AS_STR_PLAINTEXT = 16 ///< Extract only the explicitly visible text (without any formatting), when descending into insets
132 };
133
134
135 /// A Paragraph holds all text, attributes and insets in a text paragraph
136 class Paragraph
137 {
138 public:
139         ///
140         Paragraph();
141         /// Copy constructor.
142         Paragraph(Paragraph const &);
143         /// Partial copy constructor.
144         /// Copy the Paragraph contents from \p beg to \p end (without end).
145         Paragraph(Paragraph const & par, pos_type beg, pos_type end);
146         ///
147         Paragraph & operator=(Paragraph const &);
148         ///
149         ~Paragraph();
150         ///
151         int id() const;
152         ///
153         void setId(int id);
154
155         ///
156         void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
157                 bool output_active) const;
158         ///
159         Language const * getParLanguage(BufferParams const &) const;
160         ///
161         bool isRTL(BufferParams const &) const;
162         ///
163         void changeLanguage(BufferParams const & bparams,
164                             Language const * from, Language const * to);
165         ///
166         bool isMultiLingual(BufferParams const &) const;
167         ///
168         void getLanguages(std::set<Language const *> &) const;
169
170         /// Convert the paragraph to a string.
171         /// \param AsStringParameter options. This can contain any combination of
172         /// asStringParameter values. Valid examples:
173         ///             asString(AS_STR_LABEL)
174         ///             asString(AS_STR_LABEL | AS_STR_INSETS)
175         ///             asString(AS_STR_INSETS)
176         docstring asString(int options = AS_STR_NONE) const;
177         ///
178         docstring asString(pos_type beg, pos_type end,
179                            int options = AS_STR_NONE,
180                            const OutputParams *p_runparams = 0) const;
181         ///
182         void forToc(docstring &, size_t maxlen) const;
183
184         ///
185         void write(std::ostream &, BufferParams const &,
186                 depth_type & depth) const;
187         ///
188         void validate(LaTeXFeatures &) const;
189
190         /// \param force means: output even if layout.inpreamble is true.
191         void latex(BufferParams const &, Font const & outerfont, otexstream &,
192                 OutputParams const &, int start_pos = 0, int end_pos = -1,
193                 bool force = false) const;
194
195         /// Can we drop the standard paragraph wrapper?
196         bool emptyTag() const;
197
198         /// Get the id of the paragraph, usefull for docbook
199         std::string getID(Buffer const & buf, OutputParams const & runparams) const;
200
201         /// Output the first word of a paragraph, return the position where it left.
202         pos_type firstWordDocBook(odocstream & os, OutputParams const & runparams) const;
203
204         /// Output the first word of a paragraph, return the position where it left.
205         pos_type firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams) const;
206
207         /// Writes to stream the docbook representation
208         void simpleDocBookOnePar(Buffer const & buf,
209                                  odocstream &,
210                                  OutputParams const & runparams,
211                                  Font const & outerfont,
212                                  pos_type initial = 0) const;
213         /// \return any material that has had to be deferred until after the
214         /// paragraph has closed.
215         docstring simpleLyXHTMLOnePar(Buffer const & buf,
216                                  XHTMLStream & xs,
217                                  OutputParams const & runparams,
218                                  Font const & outerfont,
219                                  pos_type initial = 0) const;
220
221         ///
222         bool hasSameLayout(Paragraph const & par) const;
223
224         ///
225         void makeSameLayout(Paragraph const & par);
226
227         ///
228         void setInsetOwner(Inset const * inset);
229         ///
230         Inset const & inInset() const;
231         ///
232         bool allowParagraphCustomization() const;
233         ///
234         bool usePlainLayout() const;
235         ///
236         bool isPassThru() const;
237         ///
238         pos_type size() const;
239         ///
240         bool empty() const;
241
242         ///
243         Layout const & layout() const;
244         /// Do not pass a temporary to this!
245         void setLayout(Layout const & layout);
246         ///
247         void setPlainOrDefaultLayout(DocumentClass const & tc);
248         ///
249         void setDefaultLayout(DocumentClass const & tc);
250         ///
251         void setPlainLayout(DocumentClass const & tc);
252
253         /// This is the item depth, only used by enumerate and itemize
254         signed char itemdepth;
255
256         /// look up change at given pos
257         Change const & lookupChange(pos_type pos) const;
258
259         /// is there a change within the given range ?
260         bool isChanged(pos_type start, pos_type end) const;
261         /// is there an unchanged char at the given pos ?
262         bool isChanged(pos_type pos) const;
263         /// is there an insertion at the given pos ?
264         bool isInserted(pos_type pos) const;
265         /// is there a deletion at the given pos ?
266         bool isDeleted(pos_type pos) const;
267         /// is the whole paragraph deleted ?
268         bool isDeleted(pos_type start, pos_type end) const;
269
270         /// will the paragraph be physically merged with the next
271         /// one if the imaginary end-of-par character is logically deleted?
272         bool isMergedOnEndOfParDeletion(bool trackChanges) const;
273
274         /// set change for the entire par
275         void setChange(Change const & change);
276
277         /// set change at given pos
278         void setChange(pos_type pos, Change const & change);
279
280         /// accept changes within the given range
281         void acceptChanges(pos_type start, pos_type end);
282
283         /// reject changes within the given range
284         void rejectChanges(pos_type start, pos_type end);
285
286         /// Paragraphs can contain "manual labels", for example, Description
287         /// environment. The text for this user-editable label is stored in
288         /// the paragraph alongside the text of the rest of the paragraph
289         /// (the body). This function returns the starting position of the
290         /// body of the text in the paragraph.
291         pos_type beginOfBody() const;
292         /// recompute this value
293         void setBeginOfBody();
294
295         ///
296         docstring expandLabel(Layout const &, BufferParams const &) const;
297         ///
298         docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
299         ///
300         docstring const & labelString() const;
301         /// the next two functions are for the manual labels
302         docstring const getLabelWidthString() const;
303         /// Set label width string.
304         void setLabelWidthString(docstring const & s);
305         /// Actual paragraph alignment used
306         char getAlign() const;
307         /// The nesting depth of a paragraph
308         depth_type getDepth() const;
309         /// The maximal possible depth of a paragraph after this one
310         depth_type getMaxDepthAfter() const;
311         ///
312         void applyLayout(Layout const & new_layout);
313
314         /// (logically) erase the char at pos; return true if it was actually erased
315         bool eraseChar(pos_type pos, bool trackChanges);
316         /// (logically) erase the given range; return the number of chars actually erased
317         int eraseChars(pos_type start, pos_type end, bool trackChanges);
318
319         ///
320         void resetFonts(Font const & font);
321
322         /** Get uninstantiated font setting. Returns the difference
323             between the characters font and the layoutfont.
324             This is what is stored in the fonttable
325         */
326         Font const &
327         getFontSettings(BufferParams const &, pos_type pos) const;
328         ///
329         Font const & getFirstFontSettings(BufferParams const &) const;
330
331         /** Get fully instantiated font. If pos == -1, use the layout
332             font attached to this paragraph.
333             If pos == -2, use the label font of the layout attached here.
334             In all cases, the font is instantiated, i.e. does not have any
335             attributes with values FONT_INHERIT, FONT_IGNORE or
336             FONT_TOGGLE.
337         */
338         Font const getFont(BufferParams const &, pos_type pos,
339                               Font const & outerfont) const;
340         Font const getLayoutFont(BufferParams const &,
341                                     Font const & outerfont) const;
342         Font const getLabelFont(BufferParams const &,
343                                    Font const & outerfont) const;
344         /**
345          * The font returned by the above functions is the same in a
346          * span of characters. This method will return the first and
347          * the last positions in the paragraph for which that font is
348          * the same. This can be used to avoid unnecessary calls to getFont.
349          */
350         FontSpan fontSpan(pos_type pos) const;
351         ///
352         char_type getChar(pos_type pos) const;
353         /// Get the char, but mirror all bracket characters if it is right-to-left
354         char_type getUChar(BufferParams const &, pos_type pos) const;
355         /// pos <= size() (there is a dummy font change at the end of each par)
356         void setFont(pos_type pos, Font const & font);
357         /// Returns the height of the highest font in range
358         FontSize highestFontInRange(pos_type startpos,
359                                         pos_type endpos, FontSize def_size) const;
360         ///
361         void insert(pos_type pos, docstring const & str,
362                     Font const & font, Change const & change);
363
364         ///
365         void appendString(docstring const & s, Font const & font,
366                 Change const & change);
367         ///
368         void appendChar(char_type c, Font const & font, Change const & change);
369         ///
370         void insertChar(pos_type pos, char_type c, bool trackChanges);
371         ///
372         void insertChar(pos_type pos, char_type c,
373                         Font const &, bool trackChanges);
374         ///
375         void insertChar(pos_type pos, char_type c,
376                         Font const &, Change const & change);
377         /// Insert \p inset at position \p pos with \p change traking status and
378         /// \p font.
379         /// \return true if successful.
380         bool insertInset(pos_type pos, Inset * inset,
381                          Font const & font, Change const & change);
382         ///
383         Inset * getInset(pos_type pos);
384         ///
385         Inset const * getInset(pos_type pos) const;
386
387         /// Release inset at given position.
388         /// \warning does not honour change tracking!
389         /// Therefore, it should only be used for breaking and merging
390         /// paragraphs
391         Inset * releaseInset(pos_type pos);
392
393         ///
394         InsetList const & insetList() const;
395         ///
396         void setBuffer(Buffer &);
397
398         ///
399         bool isHfill(pos_type pos) const;
400
401         /// hinted by profiler
402         bool isInset(pos_type pos) const;
403         ///
404         bool isNewline(pos_type pos) const;
405         /// return true if the char is a word separator
406         bool isSeparator(pos_type pos) const;
407         ///
408         bool isLineSeparator(pos_type pos) const;
409         /// True if the character/inset at this point is a word separator.
410         /// Note that digits in particular are not considered as word separator.
411         bool isWordSeparator(pos_type pos) const;
412         /// True if the element at this point is a character that is not a letter.
413         bool isChar(pos_type pos) const;
414         /// True if the element at this point is a space
415         bool isSpace(pos_type pos) const;
416         /// True if the element at this point is a hard hyphen or a apostrophe
417         /// If it is enclosed by spaces return false
418         bool isHardHyphenOrApostrophe(pos_type pos) const;
419
420         /// returns true if at least one line break or line separator has been deleted
421         /// at the beginning of the paragraph (either physically or logically)
422         bool stripLeadingSpaces(bool trackChanges);
423
424         /// return true if we allow multiple spaces
425         bool isFreeSpacing() const;
426
427         /// return true if we allow this par to stay empty
428         bool allowEmpty() const;
429         ///
430         char_type transformChar(char_type c, pos_type pos) const;
431         ///
432         ParagraphParameters & params();
433         ///
434         ParagraphParameters const & params() const;
435
436         /// Check whether a call to fixBiblio is needed.
437         bool brokenBiblio() const;
438         /// Check if we are in a Biblio environment and insert or
439         /// delete InsetBibitems as necessary.
440         /// \retval int 1, if we had to add an inset, in which case
441         /// the cursor will need to move cursor forward; -pos, if we deleted
442         /// an inset, in which case pos is the position from which the inset
443         /// was deleted, and the cursor will need to be moved back one if it
444         /// was previously past that position. Return 0 otherwise.
445         int fixBiblio(Buffer const & buffer);
446
447         /// For each author, set 'used' to true if there is a change
448         /// by this author in the paragraph.
449         void checkAuthors(AuthorList const & authorList);
450
451         ///
452         void changeCase(BufferParams const & bparams, pos_type pos,
453                 pos_type & right, TextCase action);
454
455         /// find \param str string inside Paragraph.
456         /// \return non-zero if the specified string is at the specified
457         ///     position; returned value is the actual match length in positions
458         /// \param del specifies whether deleted strings in ct mode will be considered
459         int find(
460                 docstring const & str, ///< string to search
461                 bool cs, ///<
462                 bool mw, ///<
463                 pos_type pos, ///< start from here.
464                 bool del = true) const;
465         
466         void locateWord(pos_type & from, pos_type & to,
467                 word_location const loc) const;
468         ///
469         void updateWords();
470
471         /// Spellcheck word at position \p from and fill in found misspelled word
472         /// and \p suggestions if \p do_suggestion is true.
473         /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
474         SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
475                 docstring_list & suggestions, bool do_suggestion =  true,
476                 bool check_learned = false) const;
477
478         /// Spell checker status at position \p pos.
479         /// If \p check_boundary is true the status of position immediately
480         /// before \p pos is tested too if it is at word boundary.
481         /// \return true if one of the tested positions is misspelled.
482         bool isMisspelled(pos_type pos, bool check_boundary = false) const;
483
484         /// \return true if both positions are inside the same
485         /// spell range - i.e. the same word.
486         /// use it for positions inside misspelled range only.
487         bool isSameSpellRange(pos_type pos1, pos_type pos2) const;
488
489         /// spell check of whole paragraph
490         /// remember results until call of requestSpellCheck()
491         void spellCheck() const;
492
493         /// query state of spell checker results
494         bool needsSpellCheck() const;
495         /// mark position of text manipulation to inform the spell checker
496         /// default value -1 marks the whole paragraph to be checked (again)
497         void requestSpellCheck(pos_type pos = -1);
498
499         /// an automatically generated identifying label for this paragraph.
500         /// presently used only in the XHTML output routines.
501         std::string magicLabel() const;
502
503 private:
504         /// Expand the counters for the labelstring of \c layout
505         docstring expandParagraphLabel(Layout const &, BufferParams const &,
506                 bool process_appendix) const;
507         ///
508         void deregisterWords();
509         ///
510         void collectWords();
511         ///
512         void registerWords();
513
514         /// Pimpl away stuff
515         class Private;
516         ///
517         friend class Paragraph::Private;
518         ///
519         Private * d;
520 };
521
522 } // namespace lyx
523
524 #endif // PARAGRAPH_H