4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Asger Alstrup
8 * \author Lars Gullik Bjønnes
10 * \author André Pönitz
11 * \author Jürgen Vigna
13 * Full author contact details are available in file CREDITS.
19 #include "FontEnums.h"
20 #include "LayoutEnums.h"
21 #include "SpellChecker.h"
23 #include "support/strfwd.h"
24 #include "support/types.h"
50 class ParagraphParameters;
56 /// Inset identifier (above 0x10ffff, for ucs-4)
57 char_type const META_INSET = 0x200001;
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) {}
67 /// Range including first and last.
70 inline bool operator<(FontSpan const & s) const
72 return first < s.first;
75 inline bool operator==(FontSpan const & s) const
77 return first == s.first && last == s.last;
80 inline bool contains(pos_type p) const
82 return first <= p && p <= last;
85 inline size_t size() const
87 return empty() ? 0 : last - first;
91 inline FontSpan intersect(FontSpan const & f) const
93 FontSpan result = FontSpan();
94 if (contains(f.first))
95 result.first = f.first;
96 else if (f.contains(first))
100 if (contains(f.last))
101 result.last = f.last;
102 else if (f.contains(last))
107 inline bool empty() const
118 text_capitalization = 1,
125 enum AsStringParameter
127 AS_STR_NONE = 0, ///< No option, only printable characters.
128 AS_STR_LABEL = 1, ///< Prefix with paragraph label.
129 AS_STR_INSETS = 2, ///< Go into insets.
130 AS_STR_NEWLINES = 4, ///< Get also newline characters.
131 AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
132 AS_STR_PLAINTEXT = 16 ///< Don't export formatting when descending into insets.
136 /// A Paragraph holds all text, attributes and insets in a text paragraph
142 /// Copy constructor.
143 Paragraph(Paragraph const &);
144 /// Partial copy constructor.
145 /// Copy the Paragraph contents from \p beg to \p end (without end).
146 Paragraph(Paragraph const & par, pos_type beg, pos_type end);
148 Paragraph & operator=(Paragraph const &);
157 void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
158 bool output_active, TocBackend & backend) const;
160 Language const * getParLanguage(BufferParams const &) const;
162 bool isRTL(BufferParams const &) const;
164 void changeLanguage(BufferParams const & bparams,
165 Language const * from, Language const * to);
167 bool isMultiLingual(BufferParams const &) const;
169 void getLanguages(std::set<Language const *> &) const;
171 /// Convert the paragraph to a string.
172 /// \param AsStringParameter options. This can contain any combination of
173 /// asStringParameter values. Valid examples:
174 /// asString(AS_STR_LABEL)
175 /// asString(AS_STR_LABEL | AS_STR_INSETS)
176 /// asString(AS_STR_INSETS)
177 docstring asString(int options = AS_STR_NONE) const;
179 /// Convert the paragraph to a string.
180 /// \note If options includes AS_STR_PLAINTEXT, then runparams must be != 0
181 docstring asString(pos_type beg, pos_type end,
182 int options = AS_STR_NONE,
183 const OutputParams *runparams = 0) const;
185 void forOutliner(docstring &, size_t maxlen, bool shorten = true,
186 bool label = true) const;
189 void write(std::ostream &, BufferParams const &,
190 depth_type & depth) const;
192 void validate(LaTeXFeatures &) const;
194 /// \param force means: output even if layout.inpreamble is true.
195 void latex(BufferParams const &, Font const & outerfont, otexstream &,
196 OutputParams const &, int start_pos = 0, int end_pos = -1,
197 bool force = false) const;
199 /// Can we drop the standard paragraph wrapper?
200 bool emptyTag() const;
202 /// Get the id of the paragraph, usefull for docbook
203 std::string getID(Buffer const & buf, OutputParams const & runparams) const;
205 /// Output the first word of a paragraph, return the position where it left.
206 pos_type firstWordDocBook(XMLStream & xs, OutputParams const & runparams) const;
208 /// Output the first word of a paragraph, return the position where it left.
209 pos_type firstWordLyXHTML(XMLStream & xs, OutputParams const & runparams) const;
211 /// Writes to stream the DocBook representation
212 void simpleDocBookOnePar(Buffer const & buf,
214 OutputParams const & runparams,
215 Font const & outerfont,
216 bool start_paragraph = true,
217 bool close_paragraph = true,
218 pos_type initial = 0) const;
220 /// \return any material that has had to be deferred until after the
221 /// paragraph has closed.
222 docstring simpleLyXHTMLOnePar(Buffer const & buf,
224 OutputParams const & runparams,
225 Font const & outerfont,
226 bool start_paragraph = true,
227 bool close_paragraph = true,
228 pos_type initial = 0) const;
231 bool hasSameLayout(Paragraph const & par) const;
234 void makeSameLayout(Paragraph const & par);
237 void setInsetOwner(Inset const * inset);
239 Inset const & inInset() const;
241 bool allowParagraphCustomization() const;
243 bool usePlainLayout() const;
245 bool isPassThru() const;
247 pos_type size() const;
252 Layout const & layout() const;
253 /// Do not pass a temporary to this!
254 void setLayout(Layout const & layout);
256 void setPlainOrDefaultLayout(DocumentClass const & tc);
258 void setDefaultLayout(DocumentClass const & tc);
260 void setPlainLayout(DocumentClass const & tc);
262 /// This is the item depth, only used by enumerate and itemize
263 signed char itemdepth;
265 /// look up change at given pos
266 Change const & lookupChange(pos_type pos) const;
268 /// is there a change within the given range (does not
269 /// check contained paragraphs)
270 bool isChanged(pos_type start, pos_type end) const;
271 /// Are there insets containing changes in the range?
272 bool hasChangedInsets(pos_type start, pos_type end) const;
273 /// is there an unchanged char at the given pos ?
274 bool isChanged(pos_type pos) const;
275 /// is there a change in the paragraph ?
276 bool isChanged() const;
278 /// is there an insertion at the given pos ?
279 bool isInserted(pos_type pos) const;
280 /// is there a deletion at the given pos ?
281 bool isDeleted(pos_type pos) const;
282 /// is the whole paragraph deleted ?
283 bool isDeleted(pos_type start, pos_type end) const;
285 /// will the paragraph be physically merged with the next
286 /// one if the imaginary end-of-par character is logically deleted?
287 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
288 /// Return Change form of paragraph break
289 Change parEndChange() const;
291 /// set change for the entire par
292 void setChange(Change const & change);
294 /// set change at given pos
295 void setChange(pos_type pos, Change const & change);
297 /// accept changes within the given range
298 void acceptChanges(pos_type start, pos_type end);
300 /// reject changes within the given range
301 void rejectChanges(pos_type start, pos_type end);
303 /// Paragraphs can contain "manual labels", for example, Description
304 /// environment. The text for this user-editable label is stored in
305 /// the paragraph alongside the text of the rest of the paragraph
306 /// (the body). This function returns the starting position of the
307 /// body of the text in the paragraph.
308 pos_type beginOfBody() const;
309 /// recompute this value
310 void setBeginOfBody();
313 docstring expandLabel(Layout const &, BufferParams const &) const;
315 docstring const & labelString() const;
316 /// the next two functions are for the manual labels
317 docstring const getLabelWidthString() const;
318 /// Set label width string.
319 void setLabelWidthString(docstring const & s);
320 /// Actual paragraph alignment used
321 LyXAlignment getAlign(BufferParams const &) const;
322 /// Default paragraph alignment as determined by layout
323 LyXAlignment getDefaultAlign(BufferParams const &) const;
324 /// The nesting depth of a paragraph
325 depth_type getDepth() const;
326 /// The maximal possible depth of a paragraph after this one
327 depth_type getMaxDepthAfter() const;
329 void applyLayout(Layout const & new_layout);
331 /// (logically) erase the char at pos; return true if it was actually erased
332 bool eraseChar(pos_type pos, bool trackChanges);
333 /// (logically) erase the given range; return the number of chars actually erased
334 int eraseChars(pos_type start, pos_type end, bool trackChanges);
337 void resetFonts(Font const & font);
339 /** Get uninstantiated font setting. Returns the difference
340 between the characters font and the layoutfont.
341 This is what is stored in the fonttable
344 getFontSettings(BufferParams const &, pos_type pos) const;
346 Font const & getFirstFontSettings(BufferParams const &) const;
348 /** Get fully instantiated font, i.e., one that does not have any
349 attributes with values FONT_INHERIT, FONT_IGNORE or FONT_TOGGLE.
351 Font const getFont(BufferParams const &, pos_type pos,
352 Font const & outerfont) const;
353 Font const getLayoutFont(BufferParams const &,
354 Font const & outerfont) const;
355 Font const getLabelFont(BufferParams const &,
356 Font const & outerfont) const;
358 * The font returned by the above functions is the same in a
359 * span of characters. This method will return the first and
360 * the last positions in the paragraph for which that font is
361 * the same. This can be used to avoid unnecessary calls to getFont.
363 FontSpan fontSpan(pos_type pos) const;
365 char_type getChar(pos_type pos) const;
366 /// Get the char, but mirror all bracket characters if it is right-to-left
367 char_type getUChar(BufferParams const &, OutputParams const &,
369 /// pos <= size() (there is a dummy font change at the end of each par)
370 void setFont(pos_type pos, Font const & font);
372 void insert(pos_type pos, docstring const & str,
373 Font const & font, Change const & change);
376 void appendString(docstring const & s, Font const & font,
377 Change const & change);
379 void appendChar(char_type c, Font const & font, Change const & change);
381 void insertChar(pos_type pos, char_type c, bool trackChanges);
383 void insertChar(pos_type pos, char_type c,
384 Font const &, bool trackChanges);
386 void insertChar(pos_type pos, char_type c,
387 Font const &, Change const & change);
388 /// Insert \p inset at position \p pos with \p change traking status and
390 /// \return true if successful.
391 bool insertInset(pos_type pos, Inset * inset,
392 Font const & font, Change const & change);
394 Inset * getInset(pos_type pos);
396 Inset const * getInset(pos_type pos) const;
398 /// Release inset at given position.
399 /// \warning does not honour change tracking!
400 /// Therefore, it should only be used for breaking and merging
402 Inset * releaseInset(pos_type pos);
405 InsetList const & insetList() const;
407 void setInsetBuffers(Buffer &);
412 bool isHfill(pos_type pos) const;
414 /// hinted by profiler
415 bool isInset(pos_type pos) const;
417 bool isNewline(pos_type pos) const;
419 bool isEnvSeparator(pos_type pos) const;
420 /// return true if the char is a word separator
421 bool isSeparator(pos_type pos) const;
423 bool isLineSeparator(pos_type pos) const;
424 /// True if the character/inset at this point is a word separator.
425 /// Note that digits in particular are not considered as word separator.
426 bool isWordSeparator(pos_type pos, bool const ignore_deleted = false) const;
427 /// True if the element at this point is a character that is not a letter.
428 bool isChar(pos_type pos) const;
429 /// True if the element at this point is a space
430 bool isSpace(pos_type pos) const;
431 /// True if the element at this point is a hard hyphen or a apostrophe
432 /// If it is enclosed by spaces return false
433 bool isHardHyphenOrApostrophe(pos_type pos) const;
434 /// Return true if this paragraph has verbatim content that needs to be
435 /// protected by \cprotect
436 bool needsCProtection(bool const fragile = false) const;
438 /// returns true if at least one line break or line separator has been deleted
439 /// at the beginning of the paragraph (either physically or logically)
440 bool stripLeadingSpaces(bool trackChanges);
442 /// return true if we allow multiple spaces
443 bool isFreeSpacing() const;
445 /// return true if we allow this par to stay empty
446 bool allowEmpty() const;
448 ParagraphParameters & params();
450 ParagraphParameters const & params() const;
452 /// Check whether a call to fixBiblio is needed.
453 bool brokenBiblio() const;
454 /// Check if we are in a Biblio environment and insert or
455 /// delete InsetBibitems as necessary.
456 /// \retval int 1, if we had to add an inset, in which case
457 /// the cursor will need to move cursor forward; -pos, if we deleted
458 /// an inset, in which case pos is the position from which the inset
459 /// was deleted, and the cursor will need to be moved back one if it
460 /// was previously past that position. Return 0 otherwise.
461 int fixBiblio(Buffer const & buffer);
463 /// For each author, set 'used' to true if there is a change
464 /// by this author in the paragraph.
465 void checkAuthors(AuthorList const & authorList);
468 void changeCase(BufferParams const & bparams, pos_type pos,
469 pos_type & right, TextCase action);
471 /// find \param str string inside Paragraph.
472 /// \return non-zero if the specified string is at the specified
473 /// position; returned value is the actual match length in positions
474 /// \param del specifies whether deleted strings in ct mode will be considered
476 docstring const & str, ///< string to search
479 pos_type pos, ///< start from here.
480 bool del = true) const;
482 void locateWord(pos_type & from, pos_type & to,
483 word_location const loc, bool const ignore_deleted = false) const;
487 /// Spellcheck word at position \p from and fill in found misspelled word
488 /// and \p suggestions if \p do_suggestion is true.
489 /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
490 SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
491 docstring_list & suggestions, bool do_suggestion = true,
492 bool check_learned = false) const;
494 /// Spell checker status at position \p pos.
495 /// If \p check_boundary is true the status of position immediately
496 /// before \p pos is tested too if it is at word boundary.
497 /// \return true if one of the tested positions is misspelled.
498 bool isMisspelled(pos_type pos, bool check_boundary = false) const;
500 /// \return the spell range (misspelled area) around position.
501 /// Range is empty if word at position is correctly spelled.
502 FontSpan const & getSpellRange(pos_type pos) const;
504 /// spell check of whole paragraph
505 /// remember results until call of requestSpellCheck()
506 void spellCheck() const;
508 /// query state of spell checker results
509 bool needsSpellCheck() const;
510 /// mark position of text manipulation to inform the spell checker
511 /// default value -1 marks the whole paragraph to be checked (again)
512 void requestSpellCheck(pos_type pos = -1);
514 /// an automatically generated identifying label for this paragraph.
515 /// presently used only in the XHTML output routines.
516 std::string magicLabel() const;
518 /// anonymizes the paragraph contents (but not the paragraphs
519 /// contained inside it. Does not handle undo.
523 /// Expand the counters for the labelstring of \c layout
524 docstring expandParagraphLabel(Layout const &, BufferParams const &,
525 bool process_appendix) const;
527 void deregisterWords();
531 void registerWords();
536 friend class Paragraph::Private;
543 #endif // PARAGRAPH_H