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