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