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