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"
44 class ParagraphParameters;
50 /// Inset identifier (above 0x10ffff, for ucs-4)
51 char_type const META_INSET = 0x200001;
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) {}
61 /// Range including first and last.
64 inline bool operator<(FontSpan const & s) const
66 return first < s.first;
69 inline bool operator==(FontSpan const & s) const
71 return first == s.first && last == s.last;
74 inline bool contains(pos_type p) const
76 return first <= p && p <= last;
79 inline size_t size() const
81 return empty() ? 0 : last - first;
85 inline FontSpan intersect(FontSpan const & f) const
87 FontSpan result = FontSpan();
88 if (contains(f.first))
89 result.first = f.first;
90 else if (f.contains(first))
96 else if (f.contains(last))
101 inline bool empty() const
112 text_capitalization = 1,
119 enum AsStringParameter
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).
131 /// A Paragraph holds all text, attributes and insets in a text 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);
143 Paragraph & operator=(Paragraph const &);
152 void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
153 bool output_active, TocBackend & backend) const;
155 Language const * getParLanguage(BufferParams const &) const;
157 bool isRTL(BufferParams const &) const;
159 void changeLanguage(BufferParams const & bparams,
160 Language const * from, Language const * to);
162 bool isMultiLingual(BufferParams const &) const;
164 void getLanguages(std::set<Language const *> &) const;
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;
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;
180 void forOutliner(docstring &, size_t maxlen, bool shorten = true,
181 bool label = true) const;
184 void write(std::ostream &, BufferParams const &,
185 depth_type & depth) const;
187 void validate(LaTeXFeatures &) const;
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;
194 /// Can we drop the standard paragraph wrapper?
195 bool emptyTag() const;
197 /// Get the id of the paragraph, usefull for docbook
198 std::string getID(Buffer const & buf, OutputParams const & runparams) const;
200 /// Output the first word of a paragraph, return the position where it left.
201 pos_type firstWordDocBook(XMLStream & xs, OutputParams const & runparams) const;
203 /// Output the first word of a paragraph, return the position where it left.
204 pos_type firstWordLyXHTML(XMLStream & xs, OutputParams const & runparams) const;
206 /// Outputs to stream the DocBook representation, one element per paragraph.
207 std::tuple<std::vector<docstring>, std::vector<docstring>, std::vector<docstring>>
208 simpleDocBookOnePar(Buffer const & buf,
209 OutputParams const & runparams,
210 Font const & outerfont,
211 pos_type initial = 0,
212 bool is_last_par = false,
213 bool ignore_fonts = false) const;
215 /// \return any material that has had to be deferred until after the
216 /// paragraph has closed.
217 docstring simpleLyXHTMLOnePar(Buffer const & buf,
219 OutputParams const & runparams,
220 Font const & outerfont,
221 bool start_paragraph = true,
222 bool close_paragraph = true,
223 pos_type initial = 0) const;
226 bool hasSameLayout(Paragraph const & par) const;
229 void makeSameLayout(Paragraph const & par);
232 void setInsetOwner(Inset const * inset);
234 Inset const & inInset() const;
236 bool allowParagraphCustomization() const;
238 bool usePlainLayout() const;
240 bool isPassThru() const;
242 pos_type size() const;
247 Layout const & layout() const;
248 /// Do not pass a temporary to this!
249 void setLayout(Layout const & layout);
251 void setPlainOrDefaultLayout(DocumentClass const & tc);
253 void setDefaultLayout(DocumentClass const & tc);
255 void setPlainLayout(DocumentClass const & tc);
257 /// This is the item depth, only used by enumerate and itemize
258 signed char itemdepth;
260 /// look up change at given pos
261 Change const & lookupChange(pos_type pos) const;
263 /// is there a change within the given range (does not
264 /// check contained paragraphs)
265 bool isChanged(pos_type start, pos_type end) const;
266 /// Are there insets containing changes in the range?
267 bool hasChangedInsets(pos_type start, pos_type end) const;
268 /// is there an unchanged char at the given pos ?
269 bool isChanged(pos_type pos) const;
270 /// is there a change in the paragraph ?
271 bool isChanged() const;
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;
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 /// Return Change form of paragraph break
284 Change parEndChange() const;
286 /// set change for the entire par
287 void setChange(Change const & change);
289 /// set change at given pos
290 void setChange(pos_type pos, Change const & change);
292 /// accept changes within the given range
293 void acceptChanges(pos_type start, pos_type end);
295 /// reject changes within the given range
296 void rejectChanges(pos_type start, pos_type end);
298 /// Paragraphs can contain "manual labels", for example, Description
299 /// environment. The text for this user-editable label is stored in
300 /// the paragraph alongside the text of the rest of the paragraph
301 /// (the body). This function returns the starting position of the
302 /// body of the text in the paragraph.
303 pos_type beginOfBody() const;
304 /// recompute this value
305 void setBeginOfBody();
308 docstring expandLabel(Layout const &, BufferParams const &) const;
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;
324 void applyLayout(Layout const & new_layout);
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);
332 void resetFonts(Font const & font);
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
339 getFontSettings(BufferParams const &, pos_type pos) const;
341 Font const & getFirstFontSettings(BufferParams const &) const;
343 /** Get fully instantiated font, i.e., one that does not have any
344 attributes with values FONT_INHERIT, FONT_IGNORE or FONT_TOGGLE.
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;
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.
358 FontSpan fontSpan(pos_type pos) const;
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 &, OutputParams const &,
364 /// pos <= size() (there is a dummy font change at the end of each par)
365 void setFont(pos_type pos, Font const & font);
367 void insert(pos_type pos, docstring const & str,
368 Font const & font, Change const & change);
371 void appendString(docstring const & s, Font const & font,
372 Change const & change);
374 void appendChar(char_type c, Font const & font, Change const & change);
376 void insertChar(pos_type pos, char_type c, bool trackChanges);
378 void insertChar(pos_type pos, char_type c,
379 Font const &, bool trackChanges);
381 void insertChar(pos_type pos, char_type c,
382 Font const &, Change const & change);
383 /// Insert \p inset at position \p pos with \p change traking status and
385 /// \return true if successful.
386 bool insertInset(pos_type pos, Inset * inset,
387 Font const & font, Change const & change);
389 Inset * getInset(pos_type pos);
391 Inset const * getInset(pos_type pos) const;
393 /// Release inset at given position.
394 /// \warning does not honour change tracking!
395 /// Therefore, it should only be used for breaking and merging
397 Inset * releaseInset(pos_type pos);
400 InsetList const & insetList() const;
402 void setInsetBuffers(Buffer &);
407 bool isHfill(pos_type pos) const;
409 /// hinted by profiler
410 bool isInset(pos_type pos) const;
412 bool isNewline(pos_type pos) const;
414 bool isEnvSeparator(pos_type pos) const;
415 /// return true if the char is a word separator
416 bool isSeparator(pos_type pos) const;
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, bool const ignore_deleted = false) 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 /// Return true if this paragraph has verbatim content that needs to be
430 /// protected by \cprotect
431 bool needsCProtection(bool const fragile = false) const;
433 /// returns true if at least one line break or line separator has been deleted
434 /// at the beginning of the paragraph (either physically or logically)
435 bool stripLeadingSpaces(bool trackChanges);
437 /// return true if we allow multiple spaces
438 bool isFreeSpacing() const;
440 /// return true if we allow this par to stay empty
441 bool allowEmpty() const;
443 ParagraphParameters & params();
445 ParagraphParameters const & params() const;
447 /// Check whether a call to fixBiblio is needed.
448 bool brokenBiblio() const;
449 /// Check if we are in a Biblio environment and insert or
450 /// delete InsetBibitems as necessary.
451 /// \retval int 1, if we had to add an inset, in which case
452 /// the cursor will need to move cursor forward; -pos, if we deleted
453 /// an inset, in which case pos is the position from which the inset
454 /// was deleted, and the cursor will need to be moved back one if it
455 /// was previously past that position. Return 0 otherwise.
456 int fixBiblio(Buffer const & buffer);
458 /// For each author, set 'used' to true if there is a change
459 /// by this author in the paragraph.
460 void checkAuthors(AuthorList const & authorList);
463 void changeCase(BufferParams const & bparams, pos_type pos,
464 pos_type & right, TextCase action);
466 /// find \param str string inside Paragraph.
467 /// \return non-zero if the specified string is at the specified
468 /// position; returned value is the actual match length in positions
469 /// \param del specifies whether deleted strings in ct mode will be considered
471 docstring const & str, ///< string to search
474 pos_type pos, ///< start from here.
475 bool del = true) const;
477 void locateWord(pos_type & from, pos_type & to,
478 word_location const loc, bool const ignore_deleted = false) const;
482 /// Spellcheck word at position \p from and fill in found misspelled word
483 /// and \p suggestions if \p do_suggestion is true.
484 /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
485 SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
486 docstring_list & suggestions, bool do_suggestion = true,
487 bool check_learned = false) const;
489 /// Spell checker status at position \p pos.
490 /// If \p check_boundary is true the status of position immediately
491 /// before \p pos is tested too if it is at word boundary.
492 /// \return true if one of the tested positions is misspelled.
493 bool isMisspelled(pos_type pos, bool check_boundary = false) const;
495 /// \return the spell range (misspelled area) around position.
496 /// Range is empty if word at position is correctly spelled.
497 FontSpan const & getSpellRange(pos_type pos) const;
499 /// spell check of whole paragraph
500 /// remember results until call of requestSpellCheck()
501 void spellCheck() const;
503 /// query state of spell checker results
504 bool needsSpellCheck() const;
505 /// mark position of text manipulation to inform the spell checker
506 /// default value -1 marks the whole paragraph to be checked (again)
507 void requestSpellCheck(pos_type pos = -1);
509 /// an automatically generated identifying label for this paragraph.
510 /// presently used only in the XHTML output routines.
511 std::string magicLabel() const;
513 /// anonymizes the paragraph contents (but not the paragraphs
514 /// contained inside it. Does not handle undo.
518 /// Expand the counters for the labelstring of \c layout
519 docstring expandParagraphLabel(Layout const &, BufferParams const &,
520 bool process_appendix) const;
522 void deregisterWords();
526 void registerWords();
531 friend class Paragraph::Private;
538 #endif // PARAGRAPH_H