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