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