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