]> git.lyx.org Git - lyx.git/blob - src/Paragraph.h
Provide proper fallback if a bibliography processor is not found
[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 "LayoutEnums.h"
21 #include "SpellChecker.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 InsetList;
44 class Language;
45 class Layout;
46 class Font;
47 class MetricsInfo;
48 class OutputParams;
49 class PainterInfo;
50 class ParagraphParameters;
51 class TocBackend;
52 class WordLangTuple;
53 class XHTMLStream;
54 class otexstream;
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         inline bool operator<(FontSpan const & s) const
68         {
69                 return first < s.first;
70         }
71
72         inline bool operator==(FontSpan const & s) const
73         {
74                 return first == s.first && last == s.last;
75         }
76
77         inline bool contains(pos_type p) const
78         {
79                 return first <= p && p <= last;
80         }
81
82         inline size_t size() const
83         {
84                 return empty() ? 0 : last - first;
85         }
86
87
88         inline FontSpan intersect(FontSpan const & f) const
89         {
90                 FontSpan result = FontSpan();
91                 if (contains(f.first))
92                         result.first = f.first;
93                 else if (f.contains(first))
94                         result.first = first;
95                 else
96                         return result;
97                 if (contains(f.last))
98                         result.last = f.last;
99                 else if (f.contains(last))
100                         result.last = last;
101                 return result;
102         }
103
104         inline bool empty() const
105         {
106                 return first > last;
107         }
108 };
109
110 ///
111 enum TextCase {
112         ///
113         text_lowercase = 0,
114         ///
115         text_capitalization = 1,
116         ///
117         text_uppercase = 2
118 };
119
120
121 ///
122 enum AsStringParameter
123 {
124         AS_STR_NONE = 0, ///< No option, only printable characters.
125         AS_STR_LABEL = 1, ///< Prefix with paragraph label.
126         AS_STR_INSETS = 2, ///< Go into insets.
127         AS_STR_NEWLINES = 4, ///< Get also newline characters.
128         AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
129         AS_STR_PLAINTEXT = 16, ///< Don't export formatting when descending into insets.
130         AS_STR_MATHED = 32 ///< Use a format suitable for mathed (eg. for InsetRef).
131 };
132
133
134 /// A Paragraph holds all text, attributes and insets in a text paragraph
135 class Paragraph
136 {
137 public:
138         ///
139         Paragraph();
140         /// Copy constructor.
141         Paragraph(Paragraph const &);
142         /// Partial copy constructor.
143         /// Copy the Paragraph contents from \p beg to \p end (without end).
144         Paragraph(Paragraph const & par, pos_type beg, pos_type end);
145         ///
146         Paragraph & operator=(Paragraph const &);
147         ///
148         ~Paragraph();
149         ///
150         int id() const;
151         ///
152         void setId(int id);
153
154         ///
155         void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
156                              bool output_active, TocBackend & backend) const;
157         /// set the buffer flag if there are changes in the paragraph
158         void addChangesToBuffer(Buffer const & buf) const;
159         ///
160         bool isChangeUpdateRequired() const;
161         ///
162         Language const * getParLanguage(BufferParams const &) const;
163         ///
164         bool isRTL(BufferParams const &) const;
165         ///
166         void changeLanguage(BufferParams const & bparams,
167                             Language const * from, Language const * to);
168         ///
169         bool isMultiLingual(BufferParams const &) const;
170         ///
171         void getLanguages(std::set<Language const *> &) const;
172
173         /// Convert the paragraph to a string.
174         /// \param AsStringParameter options. This can contain any combination of
175         /// asStringParameter values. Valid examples:
176         ///             asString(AS_STR_LABEL)
177         ///             asString(AS_STR_LABEL | AS_STR_INSETS)
178         ///             asString(AS_STR_INSETS)
179         docstring asString(int options = AS_STR_NONE) const;
180
181         /// Convert the paragraph to a string.
182         /// \note If options includes AS_STR_PLAINTEXT, then runparams must be != 0
183         docstring asString(pos_type beg, pos_type end,
184                            int options = AS_STR_NONE,
185                            const OutputParams *runparams = 0) const;
186         ///
187         void forOutliner(docstring &, size_t maxlen, bool shorten = true,
188                          bool label = true) const;
189
190         ///
191         void write(std::ostream &, BufferParams const &,
192                 depth_type & depth) const;
193         ///
194         void validate(LaTeXFeatures &) const;
195
196         /// \param force means: output even if layout.inpreamble is true.
197         void latex(BufferParams const &, Font const & outerfont, otexstream &,
198                 OutputParams const &, int start_pos = 0, int end_pos = -1,
199                 bool force = false) const;
200
201         /// Can we drop the standard paragraph wrapper?
202         bool emptyTag() const;
203
204         /// Get the id of the paragraph, usefull for docbook
205         std::string getID(Buffer const & buf, OutputParams const & runparams) const;
206
207         /// Output the first word of a paragraph, return the position where it left.
208         pos_type firstWordDocBook(odocstream & os, OutputParams const & runparams) const;
209
210         /// Output the first word of a paragraph, return the position where it left.
211         pos_type firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams) const;
212
213         /// Writes to stream the docbook representation
214         void simpleDocBookOnePar(Buffer const & buf,
215                                  odocstream &,
216                                  OutputParams const & runparams,
217                                  Font const & outerfont,
218                                  pos_type initial = 0) const;
219         /// \return any material that has had to be deferred until after the
220         /// paragraph has closed.
221         docstring simpleLyXHTMLOnePar(Buffer const & buf,
222                                  XHTMLStream & xs,
223                                  OutputParams const & runparams,
224                                  Font const & outerfont,
225                                  bool start_paragraph = true,
226                                  bool close_paragraph = true,
227                                  pos_type initial = 0) const;
228
229         ///
230         bool hasSameLayout(Paragraph const & par) const;
231
232         ///
233         void makeSameLayout(Paragraph const & par);
234
235         ///
236         void setInsetOwner(Inset const * inset);
237         ///
238         Inset const & inInset() const;
239         ///
240         bool allowParagraphCustomization() const;
241         ///
242         bool usePlainLayout() const;
243         ///
244         bool isPassThru() const;
245         ///
246         pos_type size() const;
247         ///
248         bool empty() const;
249
250         ///
251         Layout const & layout() const;
252         /// Do not pass a temporary to this!
253         void setLayout(Layout const & layout);
254         ///
255         void setPlainOrDefaultLayout(DocumentClass const & tc);
256         ///
257         void setDefaultLayout(DocumentClass const & tc);
258         ///
259         void setPlainLayout(DocumentClass const & tc);
260
261         /// This is the item depth, only used by enumerate and itemize
262         signed char itemdepth;
263
264         /// look up change at given pos
265         Change const & lookupChange(pos_type pos) const;
266
267         /// is there a change within the given range (does not
268         /// check contained paragraphs)
269         bool isChanged(pos_type start, pos_type end) const;
270         /// is there an unchanged char at the given pos ?
271         bool isChanged(pos_type pos) const;
272
273         /// is there an insertion at the given pos ?
274         bool isInserted(pos_type pos) const;
275         /// is there a deletion at the given pos ?
276         bool isDeleted(pos_type pos) const;
277         /// is the whole paragraph deleted ?
278         bool isDeleted(pos_type start, pos_type end) const;
279
280         /// will the paragraph be physically merged with the next
281         /// one if the imaginary end-of-par character is logically deleted?
282         bool isMergedOnEndOfParDeletion(bool trackChanges) const;
283
284         /// set change for the entire par
285         void setChange(Change const & change);
286
287         /// set change at given pos
288         void setChange(pos_type pos, Change const & change);
289
290         /// accept changes within the given range
291         void acceptChanges(pos_type start, pos_type end);
292
293         /// reject changes within the given range
294         void rejectChanges(pos_type start, pos_type end);
295
296         /// Paragraphs can contain "manual labels", for example, Description
297         /// environment. The text for this user-editable label is stored in
298         /// the paragraph alongside the text of the rest of the paragraph
299         /// (the body). This function returns the starting position of the
300         /// body of the text in the paragraph.
301         pos_type beginOfBody() const;
302         /// recompute this value
303         void setBeginOfBody();
304
305         ///
306         docstring expandLabel(Layout const &, BufferParams const &) const;
307         ///
308         docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
309         ///
310         docstring const & labelString() const;
311         /// the next two functions are for the manual labels
312         docstring const getLabelWidthString() const;
313         /// Set label width string.
314         void setLabelWidthString(docstring const & s);
315         /// Actual paragraph alignment used
316         LyXAlignment getAlign(BufferParams const &) const;
317         /// Default paragraph alignment as determined by layout
318         LyXAlignment getDefaultAlign(BufferParams const &) const;
319         /// The nesting depth of a paragraph
320         depth_type getDepth() const;
321         /// The maximal possible depth of a paragraph after this one
322         depth_type getMaxDepthAfter() const;
323         ///
324         void applyLayout(Layout const & new_layout);
325
326         /// (logically) erase the char at pos; return true if it was actually erased
327         bool eraseChar(pos_type pos, bool trackChanges);
328         /// (logically) erase the given range; return the number of chars actually erased
329         int eraseChars(pos_type start, pos_type end, bool trackChanges);
330
331         ///
332         void resetFonts(Font const & font);
333
334         /** Get uninstantiated font setting. Returns the difference
335             between the characters font and the layoutfont.
336             This is what is stored in the fonttable
337         */
338         Font const &
339         getFontSettings(BufferParams const &, pos_type pos) const;
340         ///
341         Font const & getFirstFontSettings(BufferParams const &) const;
342
343         /** Get fully instantiated font. If pos == -1, use the layout
344             font attached to this paragraph.
345             If pos == -2, use the label font of the layout attached here.
346             In all cases, the font is instantiated, i.e. does not have any
347             attributes with values FONT_INHERIT, FONT_IGNORE or
348             FONT_TOGGLE.
349         */
350         Font const getFont(BufferParams const &, pos_type pos,
351                               Font const & outerfont) const;
352         Font const getLayoutFont(BufferParams const &,
353                                     Font const & outerfont) const;
354         Font const getLabelFont(BufferParams const &,
355                                    Font const & outerfont) const;
356         /**
357          * The font returned by the above functions is the same in a
358          * span of characters. This method will return the first and
359          * the last positions in the paragraph for which that font is
360          * the same. This can be used to avoid unnecessary calls to getFont.
361          */
362         FontSpan fontSpan(pos_type pos) const;
363         ///
364         char_type getChar(pos_type pos) const;
365         /// Get the char, but mirror all bracket characters if it is right-to-left
366         char_type getUChar(BufferParams const &, OutputParams const &,
367                            pos_type pos) const;
368         /// pos <= size() (there is a dummy font change at the end of each par)
369         void setFont(pos_type pos, Font const & font);
370         ///
371         void insert(pos_type pos, docstring const & str,
372                     Font const & font, Change const & change);
373
374         ///
375         void appendString(docstring const & s, Font const & font,
376                 Change const & change);
377         ///
378         void appendChar(char_type c, Font const & font, Change const & change);
379         ///
380         void insertChar(pos_type pos, char_type c, bool trackChanges);
381         ///
382         void insertChar(pos_type pos, char_type c,
383                         Font const &, bool trackChanges);
384         ///
385         void insertChar(pos_type pos, char_type c,
386                         Font const &, Change const & change);
387         /// Insert \p inset at position \p pos with \p change traking status and
388         /// \p font.
389         /// \return true if successful.
390         bool insertInset(pos_type pos, Inset * inset,
391                          Font const & font, Change const & change);
392         ///
393         Inset * getInset(pos_type pos);
394         ///
395         Inset const * getInset(pos_type pos) const;
396
397         /// Release inset at given position.
398         /// \warning does not honour change tracking!
399         /// Therefore, it should only be used for breaking and merging
400         /// paragraphs
401         Inset * releaseInset(pos_type pos);
402
403         ///
404         InsetList const & insetList() const;
405         ///
406         void setBuffer(Buffer &);
407         ///
408         void resetBuffer();
409
410         ///
411         bool isHfill(pos_type pos) const;
412
413         /// hinted by profiler
414         bool isInset(pos_type pos) const;
415         ///
416         bool isNewline(pos_type pos) const;
417         ///
418         bool isEnvSeparator(pos_type pos) const;
419         /// return true if the char is a word separator
420         bool isSeparator(pos_type pos) const;
421         ///
422         bool isLineSeparator(pos_type pos) const;
423         /// True if the character/inset at this point is a word separator.
424         /// Note that digits in particular are not considered as word separator.
425         bool isWordSeparator(pos_type pos) const;
426         /// True if the element at this point is a character that is not a letter.
427         bool isChar(pos_type pos) const;
428         /// True if the element at this point is a space
429         bool isSpace(pos_type pos) const;
430         /// True if the element at this point is a hard hyphen or a apostrophe
431         /// If it is enclosed by spaces return false
432         bool isHardHyphenOrApostrophe(pos_type pos) const;
433
434         /// returns true if at least one line break or line separator has been deleted
435         /// at the beginning of the paragraph (either physically or logically)
436         bool stripLeadingSpaces(bool trackChanges);
437
438         /// return true if we allow multiple spaces
439         bool isFreeSpacing() const;
440
441         /// return true if we allow this par to stay empty
442         bool allowEmpty() const;
443         ///
444         ParagraphParameters & params();
445         ///
446         ParagraphParameters const & params() const;
447
448         /// Check whether a call to fixBiblio is needed.
449         bool brokenBiblio() const;
450         /// Check if we are in a Biblio environment and insert or
451         /// delete InsetBibitems as necessary.
452         /// \retval int 1, if we had to add an inset, in which case
453         /// the cursor will need to move cursor forward; -pos, if we deleted
454         /// an inset, in which case pos is the position from which the inset
455         /// was deleted, and the cursor will need to be moved back one if it
456         /// was previously past that position. Return 0 otherwise.
457         int fixBiblio(Buffer const & buffer);
458
459         /// For each author, set 'used' to true if there is a change
460         /// by this author in the paragraph.
461         void checkAuthors(AuthorList const & authorList);
462
463         ///
464         void changeCase(BufferParams const & bparams, pos_type pos,
465                 pos_type & right, TextCase action);
466
467         /// find \param str string inside Paragraph.
468         /// \return non-zero if the specified string is at the specified
469         ///     position; returned value is the actual match length in positions
470         /// \param del specifies whether deleted strings in ct mode will be considered
471         int find(
472                 docstring const & str, ///< string to search
473                 bool cs, ///<
474                 bool mw, ///<
475                 pos_type pos, ///< start from here.
476                 bool del = true) const;
477
478         void locateWord(pos_type & from, pos_type & to,
479                 word_location const loc) const;
480         ///
481         void updateWords();
482
483         /// Spellcheck word at position \p from and fill in found misspelled word
484         /// and \p suggestions if \p do_suggestion is true.
485         /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
486         SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
487                 docstring_list & suggestions, bool do_suggestion =  true,
488                 bool check_learned = false) const;
489
490         /// Spell checker status at position \p pos.
491         /// If \p check_boundary is true the status of position immediately
492         /// before \p pos is tested too if it is at word boundary.
493         /// \return true if one of the tested positions is misspelled.
494         bool isMisspelled(pos_type pos, bool check_boundary = false) const;
495
496         /// \return the spell range (misspelled area) around position.
497         /// Range is empty if word at position is correctly spelled.
498         FontSpan const & getSpellRange(pos_type pos) const;
499
500         /// spell check of whole paragraph
501         /// remember results until call of requestSpellCheck()
502         void spellCheck() const;
503
504         /// query state of spell checker results
505         bool needsSpellCheck() const;
506         /// mark position of text manipulation to inform the spell checker
507         /// default value -1 marks the whole paragraph to be checked (again)
508         void requestSpellCheck(pos_type pos = -1);
509
510         /// an automatically generated identifying label for this paragraph.
511         /// presently used only in the XHTML output routines.
512         std::string magicLabel() const;
513
514         /// anonymizes the paragraph contents (but not the paragraphs
515         /// contained inside it. Does not handle undo.
516         void anonymize();
517
518 private:
519         /// Expand the counters for the labelstring of \c layout
520         docstring expandParagraphLabel(Layout const &, BufferParams const &,
521                 bool process_appendix) const;
522         ///
523         void deregisterWords();
524         ///
525         void collectWords();
526         ///
527         void registerWords();
528
529         /// Pimpl away stuff
530         class Private;
531         ///
532         friend class Paragraph::Private;
533         ///
534         Private * d;
535 };
536
537 } // namespace lyx
538
539 #endif // PARAGRAPH_H