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