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