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