]> git.lyx.org Git - lyx.git/blob - src/Paragraph.h
Merge branch 'master' of git.lyx.org:lyx
[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 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 contains(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 (contains(f.first))
94                         result.first = f.first;
95                 else if (f.contains(first))
96                         result.first = first;
97                 else
98                         return result;
99                 if (contains(f.last))
100                         result.last = f.last;
101                 else if (f.contains(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 ///< Don't export 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         /// 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) 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 true if both positions are inside the same
487         /// spell range - i.e. the same word.
488         /// use it for positions inside misspelled range only.
489         bool isSameSpellRange(pos_type pos1, pos_type pos2) const;
490
491         /// spell check of whole paragraph
492         /// remember results until call of requestSpellCheck()
493         void spellCheck() const;
494
495         /// query state of spell checker results
496         bool needsSpellCheck() const;
497         /// mark position of text manipulation to inform the spell checker
498         /// default value -1 marks the whole paragraph to be checked (again)
499         void requestSpellCheck(pos_type pos = -1);
500
501         /// an automatically generated identifying label for this paragraph.
502         /// presently used only in the XHTML output routines.
503         std::string magicLabel() const;
504
505 private:
506         /// Expand the counters for the labelstring of \c layout
507         docstring expandParagraphLabel(Layout const &, BufferParams const &,
508                 bool process_appendix) const;
509         ///
510         void deregisterWords();
511         ///
512         void collectWords();
513         ///
514         void registerWords();
515
516         /// Pimpl away stuff
517         class Private;
518         ///
519         friend class Paragraph::Private;
520         ///
521         Private * d;
522 };
523
524 } // namespace lyx
525
526 #endif // PARAGRAPH_H