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