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