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