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