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