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