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