]> git.lyx.org Git - lyx.git/blob - src/Paragraph.h
Audit all the LASSERT calls, and try to do something sensible at
[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 size_t size() const
87         {
88                 return empty() ? 0 : last - first;
89         }
90         
91
92         inline FontSpan intersect(FontSpan const & f) const
93         {
94                 FontSpan result = FontSpan();
95                 if (inside(f.first))
96                         result.first = f.first;
97                 else if (f.inside(first))
98                         result.first = first;
99                 else
100                         return result;
101                 if (inside(f.last))
102                         result.last = f.last;
103                 else if (f.inside(last))
104                         result.last = last;
105                 return result;
106         }
107         
108         inline bool empty() const
109         {
110                 return first > last;
111         }
112 };
113
114 ///
115 enum TextCase {
116         ///
117         text_lowercase = 0,
118         ///
119         text_capitalization = 1,
120         ///
121         text_uppercase = 2
122 };
123
124
125 ///
126 enum AsStringParameter
127 {
128         AS_STR_NONE = 0, ///< No option, only printable characters.
129         AS_STR_LABEL = 1, ///< Prefix with paragraph label.
130         AS_STR_INSETS = 2, ///< Go into insets.
131         AS_STR_NEWLINES = 4, ///< Get also newline characters.
132         AS_STR_SKIPDELETE = 8 ///< Skip deleted text in change tracking.
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) 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         docstring asString(pos_type beg, pos_type end,
180                 int options = AS_STR_NONE) const;
181         ///
182         void forToc(docstring &, size_t maxlen) const;
183
184         /// Extract only the explicitly visible text (without any formatting),
185         /// descending into insets
186         docstring stringify(pos_type beg, pos_type end, int options,
187                 OutputParams const & runparams) const;
188
189         ///
190         void write(std::ostream &, BufferParams const &,
191                 depth_type & depth) const;
192         ///
193         void validate(LaTeXFeatures &) const;
194
195         /// \param force means: output even if layout.inpreamble is true.
196         void latex(BufferParams const &, Font const & outerfont, otexstream &,
197                 OutputParams const &, int start_pos = 0, int end_pos = -1,
198                 bool force = false) const;
199
200         /// Can we drop the standard paragraph wrapper?
201         bool emptyTag() const;
202
203         /// Get the id of the paragraph, usefull for docbook
204         std::string getID(Buffer const & buf, OutputParams const & runparams) const;
205
206         /// Output the first word of a paragraph, return the position where it left.
207         pos_type firstWordDocBook(odocstream & os, OutputParams const & runparams) const;
208
209         /// Output the first word of a paragraph, return the position where it left.
210         pos_type firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams) const;
211
212         /// Writes to stream the docbook representation
213         void simpleDocBookOnePar(Buffer const & buf,
214                                  odocstream &,
215                                  OutputParams const & runparams,
216                                  Font const & outerfont,
217                                  pos_type initial = 0) const;
218         /// \return any material that has had to be deferred until after the
219         /// paragraph has closed.
220         docstring simpleLyXHTMLOnePar(Buffer const & buf,
221                                  XHTMLStream & xs,
222                                  OutputParams const & runparams,
223                                  Font const & outerfont,
224                                  pos_type initial = 0) const;
225
226         ///
227         bool hasSameLayout(Paragraph const & par) const;
228
229         ///
230         void makeSameLayout(Paragraph const & par);
231
232         ///
233         void setInsetOwner(Inset const * inset);
234         ///
235         Inset const & inInset() const;
236         ///
237         bool allowParagraphCustomization() const;
238         ///
239         bool usePlainLayout() const;
240         ///
241         bool isPassThru() const;
242         ///
243         pos_type size() const;
244         ///
245         bool empty() const;
246
247         ///
248         Layout const & layout() const;
249         /// Do not pass a temporary to this!
250         void setLayout(Layout const & layout);
251         ///
252         void setPlainOrDefaultLayout(DocumentClass const & tc);
253         ///
254         void setDefaultLayout(DocumentClass const & tc);
255         ///
256         void setPlainLayout(DocumentClass const & tc);
257
258         /// This is the item depth, only used by enumerate and itemize
259         signed char itemdepth;
260
261         /// look up change at given pos
262         Change const & lookupChange(pos_type pos) const;
263
264         /// is there a change within the given range ?
265         bool isChanged(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 an insertion at the given pos ?
269         bool isInserted(pos_type pos) const;
270         /// is there a deletion at the given pos ?
271         bool isDeleted(pos_type pos) const;
272         /// is the whole paragraph deleted ?
273         bool isDeleted(pos_type start, pos_type end) const;
274
275         /// will the paragraph be physically merged with the next
276         /// one if the imaginary end-of-par character is logically deleted?
277         bool isMergedOnEndOfParDeletion(bool trackChanges) const;
278
279         /// set change for the entire par
280         void setChange(Change const & change);
281
282         /// set change at given pos
283         void setChange(pos_type pos, Change const & change);
284
285         /// accept changes within the given range
286         void acceptChanges(pos_type start, pos_type end);
287
288         /// reject changes within the given range
289         void rejectChanges(pos_type start, pos_type end);
290
291         /// Paragraphs can contain "manual labels", for example, Description
292         /// environment. The text for this user-editable label is stored in
293         /// the paragraph alongside the text of the rest of the paragraph
294         /// (the body). This function returns the starting position of the
295         /// body of the text in the paragraph.
296         pos_type beginOfBody() const;
297         /// recompute this value
298         void setBeginOfBody();
299
300         ///
301         docstring expandLabel(Layout const &, BufferParams const &) const;
302         ///
303         docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
304         ///
305         docstring const & labelString() const;
306         /// the next two functions are for the manual labels
307         docstring const getLabelWidthString() const;
308         /// Set label width string.
309         void setLabelWidthString(docstring const & s);
310         /// Actual paragraph alignment used
311         char getAlign() const;
312         /// The nesting depth of a paragraph
313         depth_type getDepth() const;
314         /// The maximal possible depth of a paragraph after this one
315         depth_type getMaxDepthAfter() const;
316         ///
317         void applyLayout(Layout const & new_layout);
318
319         /// (logically) erase the char at pos; return true if it was actually erased
320         bool eraseChar(pos_type pos, bool trackChanges);
321         /// (logically) erase the given range; return the number of chars actually erased
322         int eraseChars(pos_type start, pos_type end, bool trackChanges);
323
324         ///
325         void resetFonts(Font const & font);
326
327         /** Get uninstantiated font setting. Returns the difference
328             between the characters font and the layoutfont.
329             This is what is stored in the fonttable
330         */
331         Font const &
332         getFontSettings(BufferParams const &, pos_type pos) const;
333         ///
334         Font const & getFirstFontSettings(BufferParams const &) const;
335
336         /** Get fully instantiated font. If pos == -1, use the layout
337             font attached to this paragraph.
338             If pos == -2, use the label font of the layout attached here.
339             In all cases, the font is instantiated, i.e. does not have any
340             attributes with values FONT_INHERIT, FONT_IGNORE or
341             FONT_TOGGLE.
342         */
343         Font const getFont(BufferParams const &, pos_type pos,
344                               Font const & outerfont) const;
345         Font const getLayoutFont(BufferParams const &,
346                                     Font const & outerfont) const;
347         Font const getLabelFont(BufferParams const &,
348                                    Font const & outerfont) const;
349         /**
350          * The font returned by the above functions is the same in a
351          * span of characters. This method will return the first and
352          * the last positions in the paragraph for which that font is
353          * the same. This can be used to avoid unnecessary calls to getFont.
354          */
355         FontSpan fontSpan(pos_type pos) const;
356         ///
357         char_type getChar(pos_type pos) const;
358         /// Get the char, but mirror all bracket characters if it is right-to-left
359         char_type getUChar(BufferParams const &, pos_type pos) const;
360         /// pos <= size() (there is a dummy font change at the end of each par)
361         void setFont(pos_type pos, Font const & font);
362         /// Returns the height of the highest font in range
363         FontSize highestFontInRange(pos_type startpos,
364                                         pos_type endpos, FontSize def_size) const;
365         ///
366         void insert(pos_type pos, docstring const & str,
367                     Font const & font, Change const & change);
368
369         ///
370         void appendString(docstring const & s, Font const & font,
371                 Change const & change);
372         ///
373         void appendChar(char_type c, Font const & font, Change const & change);
374         ///
375         void insertChar(pos_type pos, char_type c, bool trackChanges);
376         ///
377         void insertChar(pos_type pos, char_type c,
378                         Font const &, bool trackChanges);
379         ///
380         void insertChar(pos_type pos, char_type c,
381                         Font const &, Change const & change);
382         /// Insert \p inset at position \p pos with \p change traking status.
383         /// \return true if successful.
384         bool insertInset(pos_type pos, Inset * inset,
385                          Change const & change);
386         /// Insert \p inset at position \p pos with \p change traking status and
387         /// \p font.
388         /// \return true if successful.
389         bool insertInset(pos_type pos, Inset * inset,
390                          Font const & font, Change const & change);
391         ///
392         Inset * getInset(pos_type pos);
393         ///
394         Inset const * getInset(pos_type pos) const;
395
396         /// Release inset at given position.
397         /// \warning does not honour change tracking!
398         /// Therefore, it should only be used for breaking and merging
399         /// paragraphs
400         Inset * releaseInset(pos_type pos);
401
402         ///
403         InsetList const & insetList() const;
404         ///
405         void setBuffer(Buffer &);
406
407         ///
408         bool isHfill(pos_type pos) const;
409
410         /// hinted by profiler
411         bool isInset(pos_type pos) const;
412         ///
413         bool isNewline(pos_type pos) const;
414         /// return true if the char is a word separator
415         bool isSeparator(pos_type pos) const;
416         ///
417         bool isLineSeparator(pos_type pos) const;
418         /// True if the character/inset at this point is a word separator.
419         /// Note that digits in particular are not considered as word separator.
420         bool isWordSeparator(pos_type pos) const;
421         /// True if the element at this point is a character that is not a letter.
422         bool isChar(pos_type pos) const;
423         /// True if the element at this point is a space
424         bool isSpace(pos_type pos) const;
425         /// True if the element at this point is a hard hyphen or a apostrophe
426         /// If it is enclosed by spaces return false
427         bool isHardHyphenOrApostrophe(pos_type pos) const;
428
429         /// returns true if at least one line break or line separator has been deleted
430         /// at the beginning of the paragraph (either physically or logically)
431         bool stripLeadingSpaces(bool trackChanges);
432
433         /// return true if we allow multiple spaces
434         bool isFreeSpacing() const;
435
436         /// return true if we allow this par to stay empty
437         bool allowEmpty() const;
438         ///
439         char_type transformChar(char_type c, pos_type pos) 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) 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 true if both positions are inside the same
494         /// spell range - i.e. the same word.
495         /// use it for positions inside misspelled range only.
496         bool isSameSpellRange(pos_type pos1, pos_type pos2) const;
497
498         /// spell check of whole paragraph
499         /// remember results until call of requestSpellCheck()
500         void spellCheck() const;
501
502         /// query state of spell checker results
503         bool needsSpellCheck() const;
504         /// mark position of text manipulation to inform the spell checker
505         /// default value -1 marks the whole paragraph to be checked (again)
506         void requestSpellCheck(pos_type pos = -1);
507
508         /// an automatically generated identifying label for this paragraph.
509         /// presently used only in the XHTML output routines.
510         std::string magicLabel() const;
511
512 private:
513         /// Expand the counters for the labelstring of \c layout
514         docstring expandParagraphLabel(Layout const &, BufferParams const &,
515                 bool process_appendix) const;
516         ///
517         void deregisterWords();
518         ///
519         void collectWords();
520         ///
521         void registerWords();
522
523         /// Pimpl away stuff
524         class Private;
525         ///
526         friend class Paragraph::Private;
527         ///
528         Private * d;
529 };
530
531 } // namespace lyx
532
533 #endif // PARAGRAPH_H