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