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;
58 /// Invalid font span containing no character
59 FontSpan() : first(0), last(-1) {}
60 /// Span including first and last
61 FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
64 /// Range including first and last.
67 inline bool operator<(FontSpan const & s) const
69 return first < s.first;
72 inline bool operator==(FontSpan const & s) const
74 return first == s.first && last == s.last;
77 inline bool contains(pos_type p) const
79 return first <= p && p <= last;
82 inline size_t size() const
84 return empty() ? 0 : last - first;
88 inline FontSpan intersect(FontSpan const & f) const
90 FontSpan result = FontSpan();
91 if (contains(f.first))
92 result.first = f.first;
93 else if (f.contains(first))
99 else if (f.contains(last))
104 inline bool empty() const
115 text_capitalization = 1,
122 enum AsStringParameter
124 AS_STR_NONE = 0, ///< No option, only printable characters.
125 AS_STR_LABEL = 1, ///< Prefix with paragraph label.
126 AS_STR_INSETS = 2, ///< Go into insets.
127 AS_STR_NEWLINES = 4, ///< Get also newline characters.
128 AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
129 AS_STR_PLAINTEXT = 16 ///< Don't export formatting when descending into insets.
133 /// A Paragraph holds all text, attributes and insets in a text paragraph
139 /// Copy constructor.
140 Paragraph(Paragraph const &);
141 /// Partial copy constructor.
142 /// Copy the Paragraph contents from \p beg to \p end (without end).
143 Paragraph(Paragraph const & par, pos_type beg, pos_type end);
145 Paragraph & operator=(Paragraph const &);
154 void addChangesToToc(DocIterator const & cdit, Buffer const & buf,
155 bool output_active, TocBackend & backend) const;
156 /// set the buffer flag if there are changes in the paragraph
157 void addChangesToBuffer(Buffer const & buf) const;
159 bool isChangeUpdateRequired() const;
161 Language const * getParLanguage(BufferParams const &) const;
163 bool isRTL(BufferParams const &) const;
165 void changeLanguage(BufferParams const & bparams,
166 Language const * from, Language const * to);
168 bool isMultiLingual(BufferParams const &) const;
170 void getLanguages(std::set<Language const *> &) const;
172 /// Convert the paragraph to a string.
173 /// \param AsStringParameter options. This can contain any combination of
174 /// asStringParameter values. Valid examples:
175 /// asString(AS_STR_LABEL)
176 /// asString(AS_STR_LABEL | AS_STR_INSETS)
177 /// asString(AS_STR_INSETS)
178 docstring asString(int options = AS_STR_NONE) const;
180 /// Convert the paragraph to a string.
181 /// \note If options includes AS_STR_PLAINTEXT, then runparams must be != 0
182 docstring asString(pos_type beg, pos_type end,
183 int options = AS_STR_NONE,
184 const OutputParams *runparams = 0) const;
186 void forOutliner(docstring &, size_t maxlen, bool shorten = true,
187 bool label = true) const;
190 void write(std::ostream &, BufferParams const &,
191 depth_type & depth) const;
193 void validate(LaTeXFeatures &) const;
195 /// \param force means: output even if layout.inpreamble is true.
196 void latex(BufferParams const &, Font const & outerfont, otexstream &,
197 OutputParams const &, int start_pos = 0, int end_pos = -1,
198 bool force = false) const;
200 /// Can we drop the standard paragraph wrapper?
201 bool emptyTag() const;
203 /// Get the id of the paragraph, usefull for docbook
204 std::string getID(Buffer const & buf, OutputParams const & runparams) const;
206 /// Output the first word of a paragraph, return the position where it left.
207 pos_type firstWordDocBook(odocstream & os, OutputParams const & runparams) const;
209 /// Output the first word of a paragraph, return the position where it left.
210 pos_type firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams) const;
212 /// Writes to stream the docbook representation
213 void simpleDocBookOnePar(Buffer const & buf,
215 OutputParams const & runparams,
216 Font const & outerfont,
217 pos_type initial = 0) const;
218 /// \return any material that has had to be deferred until after the
219 /// paragraph has closed.
220 docstring simpleLyXHTMLOnePar(Buffer const & buf,
222 OutputParams const & runparams,
223 Font const & outerfont,
224 bool start_paragraph = true,
225 bool close_paragraph = true,
226 pos_type initial = 0) const;
229 bool hasSameLayout(Paragraph const & par) const;
232 void makeSameLayout(Paragraph const & par);
235 void setInsetOwner(Inset const * inset);
237 Inset const & inInset() const;
239 bool allowParagraphCustomization() const;
241 bool usePlainLayout() const;
243 bool isPassThru() const;
245 pos_type size() const;
250 Layout const & layout() const;
251 /// Do not pass a temporary to this!
252 void setLayout(Layout const & layout);
254 void setPlainOrDefaultLayout(DocumentClass const & tc);
256 void setDefaultLayout(DocumentClass const & tc);
258 void setPlainLayout(DocumentClass const & tc);
260 /// This is the item depth, only used by enumerate and itemize
261 signed char itemdepth;
263 /// look up change at given pos
264 Change const & lookupChange(pos_type pos) const;
266 /// is there a change within the given range ?
267 bool isChanged(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 an insertion at the given pos ?
271 bool isInserted(pos_type pos) const;
272 /// is there a deletion at the given pos ?
273 bool isDeleted(pos_type pos) const;
274 /// is the whole paragraph deleted ?
275 bool isDeleted(pos_type start, pos_type end) const;
277 /// will the paragraph be physically merged with the next
278 /// one if the imaginary end-of-par character is logically deleted?
279 bool isMergedOnEndOfParDeletion(bool trackChanges) const;
281 /// set change for the entire par
282 void setChange(Change const & change);
284 /// set change at given pos
285 void setChange(pos_type pos, Change const & change);
287 /// accept changes within the given range
288 void acceptChanges(pos_type start, pos_type end);
290 /// reject changes within the given range
291 void rejectChanges(pos_type start, pos_type end);
293 /// Paragraphs can contain "manual labels", for example, Description
294 /// environment. The text for this user-editable label is stored in
295 /// the paragraph alongside the text of the rest of the paragraph
296 /// (the body). This function returns the starting position of the
297 /// body of the text in the paragraph.
298 pos_type beginOfBody() const;
299 /// recompute this value
300 void setBeginOfBody();
303 docstring expandLabel(Layout const &, BufferParams const &) const;
305 docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
307 docstring const & labelString() const;
308 /// the next two functions are for the manual labels
309 docstring const getLabelWidthString() const;
310 /// Set label width string.
311 void setLabelWidthString(docstring const & s);
312 /// Actual paragraph alignment used
313 LyXAlignment getAlign() const;
314 /// The nesting depth of a paragraph
315 depth_type getDepth() const;
316 /// The maximal possible depth of a paragraph after this one
317 depth_type getMaxDepthAfter() const;
319 void applyLayout(Layout const & new_layout);
321 /// (logically) erase the char at pos; return true if it was actually erased
322 bool eraseChar(pos_type pos, bool trackChanges);
323 /// (logically) erase the given range; return the number of chars actually erased
324 int eraseChars(pos_type start, pos_type end, bool trackChanges);
327 void resetFonts(Font const & font);
329 /** Get uninstantiated font setting. Returns the difference
330 between the characters font and the layoutfont.
331 This is what is stored in the fonttable
334 getFontSettings(BufferParams const &, pos_type pos) const;
336 Font const & getFirstFontSettings(BufferParams const &) const;
338 /** Get fully instantiated font. If pos == -1, use the layout
339 font attached to this paragraph.
340 If pos == -2, use the label font of the layout attached here.
341 In all cases, the font is instantiated, i.e. does not have any
342 attributes with values FONT_INHERIT, FONT_IGNORE or
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;
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.
357 FontSpan fontSpan(pos_type pos) const;
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 &, pos_type pos) const;
362 /// pos <= size() (there is a dummy font change at the end of each par)
363 void setFont(pos_type pos, Font const & font);
365 void insert(pos_type pos, docstring const & str,
366 Font const & font, Change const & change);
369 void appendString(docstring const & s, Font const & font,
370 Change const & change);
372 void appendChar(char_type c, Font const & font, Change const & change);
374 void insertChar(pos_type pos, char_type c, bool trackChanges);
376 void insertChar(pos_type pos, char_type c,
377 Font const &, bool trackChanges);
379 void insertChar(pos_type pos, char_type c,
380 Font const &, Change const & change);
381 /// Insert \p inset at position \p pos with \p change traking status and
383 /// \return true if successful.
384 bool insertInset(pos_type pos, Inset * inset,
385 Font const & font, Change const & change);
387 Inset * getInset(pos_type pos);
389 Inset const * getInset(pos_type pos) const;
391 /// Release inset at given position.
392 /// \warning does not honour change tracking!
393 /// Therefore, it should only be used for breaking and merging
395 Inset * releaseInset(pos_type pos);
398 InsetList const & insetList() const;
400 void setInsetBuffers(Buffer &);
405 bool isHfill(pos_type pos) const;
407 /// hinted by profiler
408 bool isInset(pos_type pos) const;
410 bool isNewline(pos_type pos) const;
412 bool isEnvSeparator(pos_type pos) const;
413 /// return true if the char is a word separator
414 bool isSeparator(pos_type pos) const;
416 bool isLineSeparator(pos_type pos) const;
417 /// True if the character/inset at this point is a word separator.
418 /// Note that digits in particular are not considered as word separator.
419 bool isWordSeparator(pos_type pos) const;
420 /// True if the element at this point is a character that is not a letter.
421 bool isChar(pos_type pos) const;
422 /// True if the element at this point is a space
423 bool isSpace(pos_type pos) const;
424 /// True if the element at this point is a hard hyphen or a apostrophe
425 /// If it is enclosed by spaces return false
426 bool isHardHyphenOrApostrophe(pos_type pos) const;
427 /// Return true if this paragraph has verbatim content that needs to be
428 /// protected by \cprotect
429 bool needsCProtection() const;
431 /// returns true if at least one line break or line separator has been deleted
432 /// at the beginning of the paragraph (either physically or logically)
433 bool stripLeadingSpaces(bool trackChanges);
435 /// return true if we allow multiple spaces
436 bool isFreeSpacing() const;
438 /// return true if we allow this par to stay empty
439 bool allowEmpty() const;
441 ParagraphParameters & params();
443 ParagraphParameters const & params() const;
445 /// Check whether a call to fixBiblio is needed.
446 bool brokenBiblio() const;
447 /// Check if we are in a Biblio environment and insert or
448 /// delete InsetBibitems as necessary.
449 /// \retval int 1, if we had to add an inset, in which case
450 /// the cursor will need to move cursor forward; -pos, if we deleted
451 /// an inset, in which case pos is the position from which the inset
452 /// was deleted, and the cursor will need to be moved back one if it
453 /// was previously past that position. Return 0 otherwise.
454 int fixBiblio(Buffer const & buffer);
456 /// For each author, set 'used' to true if there is a change
457 /// by this author in the paragraph.
458 void checkAuthors(AuthorList const & authorList);
461 void changeCase(BufferParams const & bparams, pos_type pos,
462 pos_type & right, TextCase action);
464 /// find \param str string inside Paragraph.
465 /// \return non-zero if the specified string is at the specified
466 /// position; returned value is the actual match length in positions
467 /// \param del specifies whether deleted strings in ct mode will be considered
469 docstring const & str, ///< string to search
472 pos_type pos, ///< start from here.
473 bool del = true) const;
475 void locateWord(pos_type & from, pos_type & to,
476 word_location const loc) const;
480 /// Spellcheck word at position \p from and fill in found misspelled word
481 /// and \p suggestions if \p do_suggestion is true.
482 /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
483 SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
484 docstring_list & suggestions, bool do_suggestion = true,
485 bool check_learned = false) const;
487 /// Spell checker status at position \p pos.
488 /// If \p check_boundary is true the status of position immediately
489 /// before \p pos is tested too if it is at word boundary.
490 /// \return true if one of the tested positions is misspelled.
491 bool isMisspelled(pos_type pos, bool check_boundary = false) const;
493 /// \return the spell range (misspelled area) around position.
494 /// Range is empty if word at position is correctly spelled.
495 FontSpan const & getSpellRange(pos_type pos) const;
497 /// spell check of whole paragraph
498 /// remember results until call of requestSpellCheck()
499 void spellCheck() const;
501 /// query state of spell checker results
502 bool needsSpellCheck() const;
503 /// mark position of text manipulation to inform the spell checker
504 /// default value -1 marks the whole paragraph to be checked (again)
505 void requestSpellCheck(pos_type pos = -1);
507 /// an automatically generated identifying label for this paragraph.
508 /// presently used only in the XHTML output routines.
509 std::string magicLabel() const;
511 /// anonymizes the paragraph contents (but not the paragraphs
512 /// contained inside it. Does not handle undo.
516 /// Expand the counters for the labelstring of \c layout
517 docstring expandParagraphLabel(Layout const &, BufferParams const &,
518 bool process_appendix) const;
520 void deregisterWords();
524 void registerWords();
529 friend class Paragraph::Private;
536 #endif // PARAGRAPH_H