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;
157 Language const * getParLanguage(BufferParams const &) const;
159 bool isRTL(BufferParams const &) const;
161 void changeLanguage(BufferParams const & bparams,
162 Language const * from, Language const * to);
164 bool isMultiLingual(BufferParams const &) const;
166 void getLanguages(std::set<Language const *> &) const;
168 /// Convert the paragraph to a string.
169 /// \param AsStringParameter options. This can contain any combination of
170 /// asStringParameter values. Valid examples:
171 /// asString(AS_STR_LABEL)
172 /// asString(AS_STR_LABEL | AS_STR_INSETS)
173 /// asString(AS_STR_INSETS)
174 docstring asString(int options = AS_STR_NONE) const;
176 /// Convert the paragraph to a string.
177 /// \note If options includes AS_STR_PLAINTEXT, then runparams must be != 0
178 docstring asString(pos_type beg, pos_type end,
179 int options = AS_STR_NONE,
180 const OutputParams *runparams = 0) const;
182 void forOutliner(docstring &, size_t maxlen, bool shorten = true,
183 bool label = true) const;
186 void write(std::ostream &, BufferParams const &,
187 depth_type & depth) const;
189 void validate(LaTeXFeatures &) const;
191 /// \param force means: output even if layout.inpreamble is true.
192 void latex(BufferParams const &, Font const & outerfont, otexstream &,
193 OutputParams const &, int start_pos = 0, int end_pos = -1,
194 bool force = false) const;
196 /// Can we drop the standard paragraph wrapper?
197 bool emptyTag() const;
199 /// Get the id of the paragraph, usefull for docbook
200 std::string getID(Buffer const & buf, OutputParams const & runparams) const;
202 /// Output the first word of a paragraph, return the position where it left.
203 pos_type firstWordDocBook(odocstream & os, OutputParams const & runparams) const;
205 /// Output the first word of a paragraph, return the position where it left.
206 pos_type firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runparams) const;
208 /// Writes to stream the docbook representation
209 void simpleDocBookOnePar(Buffer const & buf,
211 OutputParams const & runparams,
212 Font const & outerfont,
213 pos_type initial = 0) const;
214 /// \return any material that has had to be deferred until after the
215 /// paragraph has closed.
216 docstring simpleLyXHTMLOnePar(Buffer const & buf,
218 OutputParams const & runparams,
219 Font const & outerfont,
220 bool start_paragraph = true,
221 bool close_paragraph = true,
222 pos_type initial = 0) const;
225 bool hasSameLayout(Paragraph const & par) const;
228 void makeSameLayout(Paragraph const & par);
231 void setInsetOwner(Inset const * inset);
233 Inset const & inInset() const;
235 bool allowParagraphCustomization() const;
237 bool usePlainLayout() const;
239 bool isPassThru() const;
241 pos_type size() const;
246 Layout const & layout() const;
247 /// Do not pass a temporary to this!
248 void setLayout(Layout const & layout);
250 void setPlainOrDefaultLayout(DocumentClass const & tc);
252 void setDefaultLayout(DocumentClass const & tc);
254 void setPlainLayout(DocumentClass const & tc);
256 /// This is the item depth, only used by enumerate and itemize
257 signed char itemdepth;
259 /// look up change at given pos
260 Change const & lookupChange(pos_type pos) const;
262 /// is there a change within the given range (does not
263 /// check contained paragraphs)
264 bool isChanged(pos_type start, pos_type end) const;
265 /// is there an unchanged char at the given pos ?
266 bool isChanged(pos_type pos) const;
267 /// is there a change in the paragraph ?
268 bool isChanged() 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;
280 /// Return Change form of paragraph break
281 Change parEndChange() const;
283 /// set change for the entire par
284 void setChange(Change const & change);
286 /// set change at given pos
287 void setChange(pos_type pos, Change const & change);
289 /// accept changes within the given range
290 void acceptChanges(pos_type start, pos_type end);
292 /// reject changes within the given range
293 void rejectChanges(pos_type start, pos_type end);
295 /// Paragraphs can contain "manual labels", for example, Description
296 /// environment. The text for this user-editable label is stored in
297 /// the paragraph alongside the text of the rest of the paragraph
298 /// (the body). This function returns the starting position of the
299 /// body of the text in the paragraph.
300 pos_type beginOfBody() const;
301 /// recompute this value
302 void setBeginOfBody();
305 docstring expandLabel(Layout const &, BufferParams const &) const;
307 docstring expandDocBookLabel(Layout const &, BufferParams const &) const;
309 docstring const & labelString() const;
310 /// the next two functions are for the manual labels
311 docstring const getLabelWidthString() const;
312 /// Set label width string.
313 void setLabelWidthString(docstring const & s);
314 /// Actual paragraph alignment used
315 LyXAlignment getAlign(BufferParams const &) const;
316 /// Default paragraph alignment as determined by layout
317 LyXAlignment getDefaultAlign(BufferParams const &) const;
318 /// The nesting depth of a paragraph
319 depth_type getDepth() const;
320 /// The maximal possible depth of a paragraph after this one
321 depth_type getMaxDepthAfter() const;
323 void applyLayout(Layout const & new_layout);
325 /// (logically) erase the char at pos; return true if it was actually erased
326 bool eraseChar(pos_type pos, bool trackChanges);
327 /// (logically) erase the given range; return the number of chars actually erased
328 int eraseChars(pos_type start, pos_type end, bool trackChanges);
331 void resetFonts(Font const & font);
333 /** Get uninstantiated font setting. Returns the difference
334 between the characters font and the layoutfont.
335 This is what is stored in the fonttable
338 getFontSettings(BufferParams const &, pos_type pos) const;
340 Font const & getFirstFontSettings(BufferParams const &) const;
342 /** Get fully instantiated font. If pos == -1, use the layout
343 font attached to this paragraph.
344 If pos == -2, use the label font of the layout attached here.
345 In all cases, the font is instantiated, i.e. does not have any
346 attributes with values FONT_INHERIT, FONT_IGNORE or
349 Font const getFont(BufferParams const &, pos_type pos,
350 Font const & outerfont) const;
351 Font const getLayoutFont(BufferParams const &,
352 Font const & outerfont) const;
353 Font const getLabelFont(BufferParams const &,
354 Font const & outerfont) const;
356 * The font returned by the above functions is the same in a
357 * span of characters. This method will return the first and
358 * the last positions in the paragraph for which that font is
359 * the same. This can be used to avoid unnecessary calls to getFont.
361 FontSpan fontSpan(pos_type pos) const;
363 char_type getChar(pos_type pos) const;
364 /// Get the char, but mirror all bracket characters if it is right-to-left
365 char_type getUChar(BufferParams const &, OutputParams const &,
367 /// pos <= size() (there is a dummy font change at the end of each par)
368 void setFont(pos_type pos, Font const & font);
370 void insert(pos_type pos, docstring const & str,
371 Font const & font, Change const & change);
374 void appendString(docstring const & s, Font const & font,
375 Change const & change);
377 void appendChar(char_type c, Font const & font, Change const & change);
379 void insertChar(pos_type pos, char_type c, bool trackChanges);
381 void insertChar(pos_type pos, char_type c,
382 Font const &, bool trackChanges);
384 void insertChar(pos_type pos, char_type c,
385 Font const &, Change const & change);
386 /// Insert \p inset at position \p pos with \p change traking status and
388 /// \return true if successful.
389 bool insertInset(pos_type pos, Inset * inset,
390 Font const & font, Change const & change);
392 Inset * getInset(pos_type pos);
394 Inset const * getInset(pos_type pos) const;
396 /// Release inset at given position.
397 /// \warning does not honour change tracking!
398 /// Therefore, it should only be used for breaking and merging
400 Inset * releaseInset(pos_type pos);
403 InsetList const & insetList() const;
405 void setInsetBuffers(Buffer &);
410 bool isHfill(pos_type pos) const;
412 /// hinted by profiler
413 bool isInset(pos_type pos) const;
415 bool isNewline(pos_type pos) const;
417 bool isEnvSeparator(pos_type pos) const;
418 /// return true if the char is a word separator
419 bool isSeparator(pos_type pos) const;
421 bool isLineSeparator(pos_type pos) const;
422 /// True if the character/inset at this point is a word separator.
423 /// Note that digits in particular are not considered as word separator.
424 bool isWordSeparator(pos_type pos, bool const ignore_deleted = false) const;
425 /// True if the element at this point is a character that is not a letter.
426 bool isChar(pos_type pos) const;
427 /// True if the element at this point is a space
428 bool isSpace(pos_type pos) const;
429 /// True if the element at this point is a hard hyphen or a apostrophe
430 /// If it is enclosed by spaces return false
431 bool isHardHyphenOrApostrophe(pos_type pos) const;
432 /// Return true if this paragraph has verbatim content that needs to be
433 /// protected by \cprotect
434 bool needsCProtection(bool const fragile = false) const;
436 /// returns true if at least one line break or line separator has been deleted
437 /// at the beginning of the paragraph (either physically or logically)
438 bool stripLeadingSpaces(bool trackChanges);
440 /// return true if we allow multiple spaces
441 bool isFreeSpacing() const;
443 /// return true if we allow this par to stay empty
444 bool allowEmpty() const;
446 ParagraphParameters & params();
448 ParagraphParameters const & params() const;
450 /// Check whether a call to fixBiblio is needed.
451 bool brokenBiblio() const;
452 /// Check if we are in a Biblio environment and insert or
453 /// delete InsetBibitems as necessary.
454 /// \retval int 1, if we had to add an inset, in which case
455 /// the cursor will need to move cursor forward; -pos, if we deleted
456 /// an inset, in which case pos is the position from which the inset
457 /// was deleted, and the cursor will need to be moved back one if it
458 /// was previously past that position. Return 0 otherwise.
459 int fixBiblio(Buffer const & buffer);
461 /// For each author, set 'used' to true if there is a change
462 /// by this author in the paragraph.
463 void checkAuthors(AuthorList const & authorList);
466 void changeCase(BufferParams const & bparams, pos_type pos,
467 pos_type & right, TextCase action);
469 /// find \param str string inside Paragraph.
470 /// \return non-zero if the specified string is at the specified
471 /// position; returned value is the actual match length in positions
472 /// \param del specifies whether deleted strings in ct mode will be considered
474 docstring const & str, ///< string to search
477 pos_type pos, ///< start from here.
478 bool del = true) const;
480 void locateWord(pos_type & from, pos_type & to,
481 word_location const loc, bool const ignore_deleted = false) const;
485 /// Spellcheck word at position \p from and fill in found misspelled word
486 /// and \p suggestions if \p do_suggestion is true.
487 /// \return result from spell checker, SpellChecker::UNKNOWN_WORD when misspelled.
488 SpellChecker::Result spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
489 docstring_list & suggestions, bool do_suggestion = true,
490 bool check_learned = false) const;
492 /// Spell checker status at position \p pos.
493 /// If \p check_boundary is true the status of position immediately
494 /// before \p pos is tested too if it is at word boundary.
495 /// \return true if one of the tested positions is misspelled.
496 bool isMisspelled(pos_type pos, bool check_boundary = false) const;
498 /// \return the spell range (misspelled area) around position.
499 /// Range is empty if word at position is correctly spelled.
500 FontSpan const & getSpellRange(pos_type pos) const;
502 /// spell check of whole paragraph
503 /// remember results until call of requestSpellCheck()
504 void spellCheck() const;
506 /// query state of spell checker results
507 bool needsSpellCheck() const;
508 /// mark position of text manipulation to inform the spell checker
509 /// default value -1 marks the whole paragraph to be checked (again)
510 void requestSpellCheck(pos_type pos = -1);
512 /// an automatically generated identifying label for this paragraph.
513 /// presently used only in the XHTML output routines.
514 std::string magicLabel() const;
516 /// anonymizes the paragraph contents (but not the paragraphs
517 /// contained inside it. Does not handle undo.
521 /// Expand the counters for the labelstring of \c layout
522 docstring expandParagraphLabel(Layout const &, BufferParams const &,
523 bool process_appendix) const;
525 void deregisterWords();
529 void registerWords();
534 friend class Paragraph::Private;
541 #endif // PARAGRAPH_H