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