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