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