]> git.lyx.org Git - lyx.git/blob - src/Paragraph.h
ar.po: updates from Hatim
[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 "support/strfwd.h"
23 #include "support/types.h"
24
25 #include <set>
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 Layout;
46 class Font;
47 class Font_size;
48 class MetricsInfo;
49 class OutputParams;
50 class PainterInfo;
51 class ParagraphParameters;
52 class Toc;
53 class WordLangTuple;
54 class XHTMLStream;
55 class otexstream;
56
57 class FontSpan {
58 public:
59         /// Invalid font span containing no character
60         FontSpan() : first(0), last(-1) {}
61         /// Span including first and last
62         FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
63
64 public:
65         /// Range including first and last.
66         pos_type first, last;
67
68         inline bool operator<(FontSpan const & s) const
69         {
70                 return first < s.first;
71         }
72         
73         inline bool operator==(FontSpan const & s) const
74         {
75                 return first == s.first && last == s.last;
76         }
77
78         inline bool contains(pos_type p) const
79         {
80                 return first <= p && p <= last;
81         }
82
83         inline size_t size() const
84         {
85                 return empty() ? 0 : last - first;
86         }
87         
88
89         inline FontSpan intersect(FontSpan const & f) const
90         {
91                 FontSpan result = FontSpan();
92                 if (contains(f.first))
93                         result.first = f.first;
94                 else if (f.contains(first))
95                         result.first = first;
96                 else
97                         return result;
98                 if (contains(f.last))
99                         result.last = f.last;
100                 else if (f.contains(last))
101                         result.last = last;
102                 return result;
103         }
104         
105         inline bool empty() const
106         {
107                 return first > last;
108         }
109 };
110
111 ///
112 enum TextCase {
113         ///
114         text_lowercase = 0,
115         ///
116         text_capitalization = 1,
117         ///
118         text_uppercase = 2
119 };
120
121
122 ///
123 enum AsStringParameter
124 {
125         AS_STR_NONE = 0, ///< No option, only printable characters.
126         AS_STR_LABEL = 1, ///< Prefix with paragraph label.
127         AS_STR_INSETS = 2, ///< Go into insets.
128         AS_STR_NEWLINES = 4, ///< Get also newline characters.
129         AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
130         AS_STR_PLAINTEXT = 16 ///< Don't export formatting when descending into insets.
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) 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 const maxlen,
188                                          bool const shorten = 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 ?
268         bool isChanged(pos_type start, pos_type end) const;
269         /// is there an unchanged char at the given pos ?
270         bool isChanged(pos_type pos) const;
271         /// is there an insertion at the given pos ?
272         bool isInserted(pos_type pos) const;
273         /// is there a deletion at the given pos ?
274         bool isDeleted(pos_type pos) const;
275         /// is the whole paragraph deleted ?
276         bool isDeleted(pos_type start, pos_type end) const;
277
278         /// will the paragraph be physically merged with the next
279         /// one if the imaginary end-of-par character is logically deleted?
280         bool isMergedOnEndOfParDeletion(bool trackChanges) const;
281
282         /// set change for the entire par
283         void setChange(Change const & change);
284
285         /// set change at given pos
286         void setChange(pos_type pos, Change const & change);
287
288         /// accept changes within the given range
289         void acceptChanges(pos_type start, pos_type end);
290
291         /// reject changes within the given range
292         void rejectChanges(pos_type start, pos_type end);
293
294         /// Paragraphs can contain "manual labels", for example, Description
295         /// environment. The text for this user-editable label is stored in
296         /// the paragraph alongside the text of the rest of the paragraph
297         /// (the body). This function returns the starting position of the
298         /// body of the text in the paragraph.
299         pos_type beginOfBody() const;
300         /// recompute this value
301         void setBeginOfBody();
302
303         ///
304         docstring expandLabel(Layout const &, BufferParams const &) const;
305         ///
306         docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
307         ///
308         docstring const & labelString() const;
309         /// the next two functions are for the manual labels
310         docstring const getLabelWidthString() const;
311         /// Set label width string.
312         void setLabelWidthString(docstring const & s);
313         /// Actual paragraph alignment used
314         char getAlign() const;
315         /// The nesting depth of a paragraph
316         depth_type getDepth() const;
317         /// The maximal possible depth of a paragraph after this one
318         depth_type getMaxDepthAfter() const;
319         ///
320         void applyLayout(Layout const & new_layout);
321
322         /// (logically) erase the char at pos; return true if it was actually erased
323         bool eraseChar(pos_type pos, bool trackChanges);
324         /// (logically) erase the given range; return the number of chars actually erased
325         int eraseChars(pos_type start, pos_type end, bool trackChanges);
326
327         ///
328         void resetFonts(Font const & font);
329
330         /** Get uninstantiated font setting. Returns the difference
331             between the characters font and the layoutfont.
332             This is what is stored in the fonttable
333         */
334         Font const &
335         getFontSettings(BufferParams const &, pos_type pos) const;
336         ///
337         Font const & getFirstFontSettings(BufferParams const &) const;
338
339         /** Get fully instantiated font. If pos == -1, use the layout
340             font attached to this paragraph.
341             If pos == -2, use the label font of the layout attached here.
342             In all cases, the font is instantiated, i.e. does not have any
343             attributes with values FONT_INHERIT, FONT_IGNORE or
344             FONT_TOGGLE.
345         */
346         Font const getFont(BufferParams const &, pos_type pos,
347                               Font const & outerfont) const;
348         Font const getLayoutFont(BufferParams const &,
349                                     Font const & outerfont) const;
350         Font const getLabelFont(BufferParams const &,
351                                    Font const & outerfont) const;
352         /**
353          * The font returned by the above functions is the same in a
354          * span of characters. This method will return the first and
355          * the last positions in the paragraph for which that font is
356          * the same. This can be used to avoid unnecessary calls to getFont.
357          */
358         FontSpan fontSpan(pos_type pos) const;
359         ///
360         char_type getChar(pos_type pos) const;
361         /// Get the char, but mirror all bracket characters if it is right-to-left
362         char_type getUChar(BufferParams const &, 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         /// Returns the height of the highest font in range
366         FontSize highestFontInRange(pos_type startpos,
367                                         pos_type endpos, FontSize def_size) const;
368         ///
369         void insert(pos_type pos, docstring const & str,
370                     Font const & font, Change const & change);
371
372         ///
373         void appendString(docstring const & s, Font const & font,
374                 Change const & change);
375         ///
376         void appendChar(char_type c, Font const & font, Change const & change);
377         ///
378         void insertChar(pos_type pos, char_type c, bool trackChanges);
379         ///
380         void insertChar(pos_type pos, char_type c,
381                         Font const &, bool trackChanges);
382         ///
383         void insertChar(pos_type pos, char_type c,
384                         Font const &, Change const & change);
385         /// Insert \p inset at position \p pos with \p change traking status and
386         /// \p font.
387         /// \return true if successful.
388         bool insertInset(pos_type pos, Inset * inset,
389                          Font const & font, Change const & change);
390         ///
391         Inset * getInset(pos_type pos);
392         ///
393         Inset const * getInset(pos_type pos) const;
394
395         /// Release inset at given position.
396         /// \warning does not honour change tracking!
397         /// Therefore, it should only be used for breaking and merging
398         /// paragraphs
399         Inset * releaseInset(pos_type pos);
400
401         ///
402         InsetList const & insetList() const;
403         ///
404         void setBuffer(Buffer &);
405
406         ///
407         bool isHfill(pos_type pos) const;
408
409         /// hinted by profiler
410         bool isInset(pos_type pos) const;
411         ///
412         bool isNewline(pos_type pos) const;
413         ///
414         bool isEnvSeparator(pos_type pos) const;
415         /// return true if the char is a word separator
416         bool isSeparator(pos_type pos) const;
417         ///
418         bool isLineSeparator(pos_type pos) const;
419         /// True if the character/inset at this point is a word separator.
420         /// Note that digits in particular are not considered as word separator.
421         bool isWordSeparator(pos_type pos) const;
422         /// True if the element at this point is a character that is not a letter.
423         bool isChar(pos_type pos) const;
424         /// True if the element at this point is a space
425         bool isSpace(pos_type pos) const;
426         /// True if the element at this point is a hard hyphen or a apostrophe
427         /// If it is enclosed by spaces return false
428         bool isHardHyphenOrApostrophe(pos_type pos) const;
429
430         /// returns true if at least one line break or line separator has been deleted
431         /// at the beginning of the paragraph (either physically or logically)
432         bool stripLeadingSpaces(bool trackChanges);
433
434         /// return true if we allow multiple spaces
435         bool isFreeSpacing() const;
436
437         /// return true if we allow this par to stay empty
438         bool allowEmpty() const;
439         ///
440         ParagraphParameters & params();
441         ///
442         ParagraphParameters const & params() const;
443
444         /// Check whether a call to fixBiblio is needed.
445         bool brokenBiblio() const;
446         /// Check if we are in a Biblio environment and insert or
447         /// delete InsetBibitems as necessary.
448         /// \retval int 1, if we had to add an inset, in which case
449         /// the cursor will need to move cursor forward; -pos, if we deleted
450         /// an inset, in which case pos is the position from which the inset
451         /// was deleted, and the cursor will need to be moved back one if it
452         /// was previously past that position. Return 0 otherwise.
453         int fixBiblio(Buffer const & buffer);
454
455         /// For each author, set 'used' to true if there is a change
456         /// by this author in the paragraph.
457         void checkAuthors(AuthorList const & authorList);
458
459         ///
460         void changeCase(BufferParams const & bparams, pos_type pos,
461                 pos_type & right, TextCase action);
462
463         /// find \param str string inside Paragraph.
464         /// \return non-zero if the specified string is at the specified
465         ///     position; returned value is the actual match length in positions
466         /// \param del specifies whether deleted strings in ct mode will be considered
467         int find(
468                 docstring const & str, ///< string to search
469                 bool cs, ///<
470                 bool mw, ///<
471                 pos_type pos, ///< start from here.
472                 bool del = true) const;
473         
474         void locateWord(pos_type & from, pos_type & to,
475                 word_location const loc) const;
476         ///
477         void updateWords();
478
479         /// Spellcheck word at position \p from and fill in found misspelled word
480         /// and \p suggestions if \p do_suggestion is true.
481         /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
482         SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
483                 docstring_list & suggestions, bool do_suggestion =  true,
484                 bool check_learned = false) const;
485
486         /// Spell checker status at position \p pos.
487         /// If \p check_boundary is true the status of position immediately
488         /// before \p pos is tested too if it is at word boundary.
489         /// \return true if one of the tested positions is misspelled.
490         bool isMisspelled(pos_type pos, bool check_boundary = false) const;
491
492         /// \return the spell range (misspelled area) around position.
493         /// Range is empty if word at position is correctly spelled.
494         FontSpan const & getSpellRange(pos_type pos) const;
495
496         /// spell check of whole paragraph
497         /// remember results until call of requestSpellCheck()
498         void spellCheck() const;
499
500         /// query state of spell checker results
501         bool needsSpellCheck() const;
502         /// mark position of text manipulation to inform the spell checker
503         /// default value -1 marks the whole paragraph to be checked (again)
504         void requestSpellCheck(pos_type pos = -1);
505
506         /// an automatically generated identifying label for this paragraph.
507         /// presently used only in the XHTML output routines.
508         std::string magicLabel() const;
509
510 private:
511         /// Expand the counters for the labelstring of \c layout
512         docstring expandParagraphLabel(Layout const &, BufferParams const &,
513                 bool process_appendix) const;
514         ///
515         void deregisterWords();
516         ///
517         void collectWords();
518         ///
519         void registerWords();
520
521         /// Pimpl away stuff
522         class Private;
523         ///
524         friend class Paragraph::Private;
525         ///
526         Private * d;
527 };
528
529 } // namespace lyx
530
531 #endif // PARAGRAPH_H