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 "LayoutEnums.h"
20 #include "SpellChecker.h"
22 #include "support/strfwd.h"
23 #include "support/types.h"
25 #include "insets/InsetCode.h"
26 #include "insets/InsetLayout.h"
48 class ParagraphParameters;
54 /// Inset identifier (above 0x10ffff, for ucs-4)
55 char_type const META_INSET = 0x200001;
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) {}
65 /// Range including first and last.
68 inline bool operator<(FontSpan const & s) const
70 return first < s.first;
73 inline bool operator==(FontSpan const & s) const
75 return first == s.first && last == s.last;
78 inline bool contains(pos_type p) const
80 return first <= p && p <= last;
83 inline size_t size() const
85 return empty() ? 0 : last - first;
89 inline FontSpan intersect(FontSpan const & f) const
91 FontSpan result = FontSpan();
92 if (contains(f.first))
93 result.first = f.first;
94 else if (f.contains(first))
100 else if (f.contains(last))
105 inline bool empty() const
116 text_capitalization = 1,
123 enum AsStringParameter
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 AS_STR_MATHED = 32 ///< Use a format suitable for mathed (eg. for InsetRef).
135 /// A Paragraph holds all text, attributes and insets in a text paragraph
141 /// Copy constructor.
142 Paragraph(Paragraph const &);
143 /// Partial copy constructor.
144 /// Copy the Paragraph contents from \p beg to \p end (without end).
145 Paragraph(Paragraph const & par, pos_type beg, pos_type end);
147 Paragraph & operator=(Paragraph const &);
156 void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
157 bool output_active, TocBackend & backend) const;
159 Language const * getParLanguage(BufferParams const &) const;
161 bool isRTL(BufferParams const &) const;
163 void changeLanguage(BufferParams const & bparams,
164 Language const * from, Language const * to);
166 bool isMultiLingual(BufferParams const &) const;
168 void getLanguages(std::set<Language const *> &) const;
170 /// Convert the paragraph to a string.
171 /// \param AsStringParameter options. This can contain any combination of
172 /// asStringParameter values. Valid examples:
173 /// asString(AS_STR_LABEL)
174 /// asString(AS_STR_LABEL | AS_STR_INSETS)
175 /// asString(AS_STR_INSETS)
176 docstring asString(int options = AS_STR_NONE) const;
178 /// Convert the paragraph to a string.
179 /// \note If options includes AS_STR_PLAINTEXT, then runparams must be != 0
180 docstring asString(pos_type beg, pos_type end,
181 int options = AS_STR_NONE,
182 const OutputParams *runparams = 0) const;
184 void forOutliner(docstring &, size_t maxlen, bool shorten = true,
185 bool label = true) const;
188 void write(std::ostream &, BufferParams const &,
189 depth_type & depth) const;
191 void validate(LaTeXFeatures &) const;
193 /// \param force means: output even if layout.inpreamble is true.
194 void latex(BufferParams const &, Font const & outerfont, otexstream &,
195 OutputParams const &, int start_pos = 0, int end_pos = -1,
196 bool force = false) const;
198 /// Can we drop the standard paragraph wrapper?
199 bool emptyTag() const;
201 /// Get the id of the paragraph, useful for DocBook
202 std::string getID(Buffer const & buf, OutputParams const & runparams) const;
204 /// Output the first word of a paragraph, return the position where it left.
205 pos_type firstWordDocBook(XMLStream & xs, OutputParams const & runparams) const;
207 /// Output the first word of a paragraph, return the position where it left.
208 pos_type firstWordLyXHTML(XMLStream & xs, OutputParams const & runparams) const;
210 /// Outputs to stream the DocBook representation, one element per paragraph.
211 std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring>>
212 simpleDocBookOnePar(Buffer const & buf,
213 OutputParams const & runparams,
214 Font const & outerfont,
215 pos_type initial = 0,
216 bool is_last_par = false,
217 bool ignore_fonts = false) const;
219 /// \return any material that has had to be deferred until after the
220 /// paragraph has closed.
221 docstring simpleLyXHTMLOnePar(Buffer const & buf,
223 OutputParams const & runparams,
224 Font const & outerfont,
225 bool start_paragraph = true,
226 bool close_paragraph = true,
227 pos_type initial = 0) const;
230 bool hasSameLayout(Paragraph const & par) const;
233 void makeSameLayout(Paragraph const & par);
236 void setInsetOwner(Inset const * inset);
238 Inset const & inInset() const;
240 bool allowParagraphCustomization() const;
242 bool usePlainLayout() const;
244 bool isPassThru() const;
246 bool parbreakIsNewline() const;
248 bool allowedInContext(Cursor const & cur, InsetLayout const & il) const;
250 bool isPartOfTextSequence() const;
252 pos_type size() const;
257 Layout const & layout() const;
258 /// Do not pass a temporary to this!
259 void setLayout(Layout const & layout);
261 void setPlainOrDefaultLayout(DocumentClass const & tc);
263 void setDefaultLayout(DocumentClass const & tc);
265 void setPlainLayout(DocumentClass const & tc);
267 /// This is the item depth, only used by enumerate and itemize
268 signed char itemdepth;
270 /// look up change at given pos
271 Change const & lookupChange(pos_type pos) const;
273 /// is there a change within the given range (does not
274 /// check contained paragraphs)
275 bool isChanged(pos_type start, pos_type end) const;
276 /// Are there insets containing changes in the range?
277 bool hasChangedInsets(pos_type start, pos_type end) const;
278 /// is there an unchanged char at the given pos ?
279 bool isChanged(pos_type pos) const;
280 /// is there a change in the paragraph ?
281 bool isChanged() const;
283 /// is there an insertion at the given pos ?
284 bool isInserted(pos_type pos) const;
285 /// is there a deletion at the given pos ?
286 bool isDeleted(pos_type pos) const;
287 /// is the whole paragraph deleted ?
288 bool isDeleted(pos_type start, pos_type end) const;
290 /// will the paragraph be physically merged with the next
291 /// one if the imaginary end-of-par character is logically deleted?
292 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
293 /// Return Change form of paragraph break
294 Change parEndChange() const;
296 /// set change for the entire par
297 void setChange(Change const & change);
299 /// set change at given pos
300 void setChange(pos_type pos, Change const & change);
302 /// accept changes within the given range
303 void acceptChanges(pos_type start, pos_type end);
305 /// reject changes within the given range
306 void rejectChanges(pos_type start, pos_type end);
308 /// Paragraphs can contain "manual labels", for example, Description
309 /// environment. The text for this user-editable label is stored in
310 /// the paragraph alongside the text of the rest of the paragraph
311 /// (the body). This function returns the starting position of the
312 /// body of the text in the paragraph.
313 pos_type beginOfBody() const;
314 /// recompute this value
315 void setBeginOfBody();
318 docstring expandLabel(Layout const &, BufferParams const &) const;
320 docstring const & labelString() const;
321 /// the next two functions are for the manual labels
322 docstring const getLabelWidthString() const;
323 /// Set label width string.
324 void setLabelWidthString(docstring const & s);
325 /// Actual paragraph alignment used
326 LyXAlignment getAlign(BufferParams const &) const;
327 /// Default paragraph alignment as determined by layout
328 LyXAlignment getDefaultAlign(BufferParams const &) const;
329 /// The nesting depth of a paragraph
330 depth_type getDepth() const;
331 /// The maximal possible depth of a paragraph after this one
332 depth_type getMaxDepthAfter() const;
334 void applyLayout(Layout const & new_layout);
336 /// (logically) erase the char at pos; return true if it was actually erased
337 bool eraseChar(pos_type pos, bool trackChanges);
338 /// (logically) erase the given range; return the number of chars actually erased
339 int eraseChars(pos_type start, pos_type end, bool trackChanges);
342 void resetFonts(Font const & font);
344 /** Get uninstantiated font setting. Returns the difference
345 between the characters font and the layoutfont.
346 This is what is stored in the fonttable
349 getFontSettings(BufferParams const &, pos_type pos) const;
351 Font const & getFirstFontSettings(BufferParams const &) const;
353 /** Get fully instantiated font, i.e., one that does not have any
354 attributes with values FONT_INHERIT, FONT_IGNORE or FONT_TOGGLE.
356 Font const getFont(BufferParams const &, pos_type pos,
357 Font const & outerfont) const;
358 Font const getLayoutFont(BufferParams const &,
359 Font const & outerfont) const;
360 Font const getLabelFont(BufferParams const &,
361 Font const & outerfont) const;
363 * The font returned by the above functions is the same in a
364 * span of characters. This method will return the first and
365 * the last positions in the paragraph for which that font is
366 * the same. This can be used to avoid unnecessary calls to getFont.
368 FontSpan fontSpan(pos_type pos) const;
370 char_type getChar(pos_type pos) const;
371 /// Get the char, but mirror all bracket characters if it is right-to-left
372 char_type getUChar(BufferParams const &, OutputParams const &,
374 /// pos <= size() (there is a dummy font change at the end of each par)
375 void setFont(pos_type pos, Font const & font);
377 void insert(pos_type pos, docstring const & str,
378 Font const & font, Change const & change);
381 void appendString(docstring const & s, Font const & font,
382 Change const & change);
384 void appendChar(char_type c, Font const & font, Change const & change);
386 void insertChar(pos_type pos, char_type c, bool trackChanges);
388 void insertChar(pos_type pos, char_type c,
389 Font const &, bool trackChanges);
391 void insertChar(pos_type pos, char_type c,
392 Font const &, Change const & change);
393 /// Insert \p inset at position \p pos with \p change traking status and
395 /// \return true if successful.
396 bool insertInset(pos_type pos, Inset * inset,
397 Font const & font, Change const & change);
399 Inset * getInset(pos_type pos);
401 Inset const * getInset(pos_type pos) const;
403 /// Release inset at given position.
404 /// \warning does not honour change tracking!
405 /// Therefore, it should only be used for breaking and merging
407 Inset * releaseInset(pos_type pos);
410 InsetList const & insetList() const;
412 void setInsetBuffers(Buffer &);
417 bool isHfill(pos_type pos) const;
419 /// hinted by profiler
420 bool isInset(pos_type pos) const;
422 bool isNewline(pos_type pos) const;
424 bool isEnvSeparator(pos_type pos) const;
425 /// return true if the char is a word separator
426 bool isSeparator(pos_type pos) const;
428 bool isLineSeparator(pos_type pos) const;
429 /// True if the character/inset at this point is a word separator.
430 /// Note that digits in particular are not considered as word separator.
431 bool isWordSeparator(pos_type pos, bool const ignore_deleted = false) const;
432 /// True if the element at this point is a character that is not a letter.
433 bool isChar(pos_type pos) const;
434 /// True if the element at this point is a space
435 bool isSpace(pos_type pos) const;
436 /// True if the element at this point is a hard hyphen or a apostrophe
437 /// If it is enclosed by spaces return false
438 bool isHardHyphenOrApostrophe(pos_type pos) const;
439 /// Return true if this paragraph has verbatim content that needs to be
440 /// protected by \cprotect
441 bool needsCProtection(bool const fragile = false) const;
443 /// returns true if at least one line break or line separator has been deleted
444 /// at the beginning of the paragraph (either physically or logically)
445 bool stripLeadingSpaces(bool trackChanges);
447 /// return true if we allow multiple spaces
448 bool isFreeSpacing() const;
450 /// return true if we allow this par to stay empty
451 bool allowEmpty() const;
453 ParagraphParameters & params();
455 ParagraphParameters const & params() const;
457 /// Check whether a call to fixBiblio is needed.
458 bool brokenBiblio() const;
459 /// Check if we are in a Biblio environment and insert or
460 /// delete InsetBibitems as necessary.
461 /// \retval int 1, if we had to add an inset, in which case
462 /// the cursor will need to move cursor forward; -pos, if we deleted
463 /// an inset, in which case pos is the position from which the inset
464 /// was deleted, and the cursor will need to be moved back one if it
465 /// was previously past that position. Return 0 otherwise.
466 int fixBiblio(Buffer const & buffer);
468 /// For each author, set 'used' to true if there is a change
469 /// by this author in the paragraph.
470 void checkAuthors(AuthorList const & authorList);
473 void changeCase(BufferParams const & bparams, pos_type pos,
474 pos_type & right, TextCase action);
476 /// find \param str string inside Paragraph.
477 /// \return non-zero if the specified string is at the specified
478 /// position; returned value is the actual match length in positions
479 /// \param del specifies whether deleted strings in ct mode will be considered
481 docstring const & str, ///< string to search
484 pos_type pos, ///< start from here.
485 bool del = true) const;
487 void locateWord(pos_type & from, pos_type & to,
488 word_location const loc, bool const ignore_deleted = false) const;
492 /// Spellcheck word at position \p from and fill in found misspelled word
493 /// and \p suggestions if \p do_suggestion is true.
494 /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
495 SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
496 docstring_list & suggestions, bool do_suggestion = true,
497 bool check_learned = false) const;
499 /// Spell checker status at position \p pos.
500 /// If \p check_boundary is true the status of position immediately
501 /// before \p pos is tested too if it is at word boundary.
502 /// \return true if one of the tested positions is misspelled.
503 bool isMisspelled(pos_type pos, bool check_boundary = false) const;
505 /// \return the spell range (misspelled area) around position.
506 /// Range is empty if word at position is correctly spelled.
507 FontSpan const & getSpellRange(pos_type pos) const;
509 /// spell check of whole paragraph
510 /// remember results until call of requestSpellCheck()
511 void spellCheck() const;
513 /// query state of spell checker results
514 bool needsSpellCheck() const;
515 /// mark position of text manipulation to inform the spell checker
516 /// default value -1 marks the whole paragraph to be checked (again)
517 void requestSpellCheck(pos_type pos = -1);
519 /// an automatically generated identifying label for this paragraph.
520 /// presently used only in the XHTML output routines.
521 std::string magicLabel() const;
523 /// anonymizes the paragraph contents (but not the paragraphs
524 /// contained inside it. Does not handle undo.
528 /// Expand the counters for the labelstring of \c layout
529 docstring expandParagraphLabel(Layout const &, BufferParams const &,
530 bool process_appendix) const;
532 void deregisterWords();
536 void registerWords();
538 int getInsetPos(InsetCode const code, int startpos,
539 bool ignore_deleted=false) const;
544 friend class Paragraph::Private;
551 #endif // PARAGRAPH_H