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