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.
20 #include "InsetList.h"
21 #include "lyxlayout_ptr_fwd.h"
22 #include "RowList_fwd.h"
24 #include "insets/insetbase.h" // only for InsetBase::Code
26 #include "support/types.h"
28 #include <boost/assert.hpp>
44 class ParagraphParameters;
48 /// A Paragraph holds all text, attributes and insets in a text paragraph
53 /// Note that this is 1 right now to avoid
54 /// crashes where getChar() is called wrongly
55 /// (returning 0) - if this was 0, then we'd
56 /// try getInset() and crash. We should fix
61 typedef char value_type;
63 typedef lyx::depth_type depth_type;
65 typedef std::vector<value_type> TextContainer;
70 Paragraph(Paragraph const &);
72 Paragraph & operator=(Paragraph const &);
80 Language const * getParLanguage(BufferParams const &) const;
82 bool isRightToLeftPar(BufferParams const &) const;
84 void changeLanguage(BufferParams const & bparams,
85 Language const * from, Language const * to);
87 bool isMultiLingual(BufferParams const &) const;
90 std::string const asString(Buffer const &,
91 OutputParams const & runparams,
94 std::string const asString(Buffer const &, bool label) const;
96 std::string const Paragraph::asString(Buffer const & buffer,
101 std::string const asString(Buffer const &,
102 OutputParams const & runparams,
108 void write(Buffer const &, std::ostream &, BufferParams const &,
109 depth_type & depth) const;
111 void validate(LaTeXFeatures &) const;
114 int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
117 int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
121 bool simpleTeXOnePar(Buffer const &, BufferParams const &,
122 LyXFont const & outerfont, std::ostream &,
123 TexRow & texrow, OutputParams const &) const;
125 /// Writes to stream the content of the paragraph for linuxdoc
126 void simpleLinuxDocOnePar(Buffer const & buf,
128 LyXFont const & outerfont,
129 OutputParams const & runparams,
130 lyx::depth_type depth) const;
132 /// Can we drop the standard paragraph wrapper?
133 bool Paragraph::emptyTag() const;
135 /// Get the id of the paragraph, usefull for docbook and linuxdoc
136 std::string getID(Buffer const & buf,
137 OutputParams const & runparams) const;
139 // Get the first word of a paragraph, return the position where it left
140 lyx::pos_type getFirstWord(Buffer const & buf,
142 OutputParams const & runparams) const;
144 /// Checks if the paragraph contains only text and no inset or font change.
145 bool onlyText(Buffer const & buf, LyXFont const & outerfont,
146 lyx::pos_type initial) const;
148 /// Writes to stream the docbook representation
149 void simpleDocBookOnePar(Buffer const & buf,
151 OutputParams const & runparams,
152 LyXFont const & outerfont,
153 lyx::pos_type initial = 0) const;
156 bool hasSameLayout(Paragraph const & par) const;
159 void makeSameLayout(Paragraph const & par);
162 void setInsetOwner(UpdatableInset * inset);
164 UpdatableInset * inInset() const;
166 InsetBase::Code ownerCode() const;
168 bool autoBreakRows() const;
170 bool forceDefaultParagraphs() const;
173 lyx::pos_type size() const { return text_.size(); }
175 bool empty() const { return text_.empty(); }
177 void setContentsFromPar(Paragraph const & par);
179 void clearContents();
182 LyXLayout_ptr const & layout() const;
184 void layout(LyXLayout_ptr const & new_layout);
186 /// This is the item depth, only used by enumerate and itemize
187 signed char itemdepth;
190 InsetBibitem * bibitem() const; // ale970302
192 /// initialise tracking for this par
193 void trackChanges(Change::Type = Change::UNCHANGED);
196 void untrackChanges();
198 /// set entire paragraph to new text for change tracking
201 /// look up change type at given pos
202 Change::Type lookupChange(lyx::pos_type pos) const;
204 /// look up change at given pos
205 Change const lookupChangeFull(lyx::pos_type pos) const;
207 /// is there a change within the given range ?
208 bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
210 /// is there a non-addition in this range ?
211 bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
213 /// set change at pos
214 void setChange(lyx::pos_type pos, Change::Type type);
217 void acceptChange(lyx::pos_type start, lyx::pos_type end);
220 void rejectChange(lyx::pos_type start, lyx::pos_type end);
222 /// mark whole par as erased
225 /// Paragraphs can contain "manual labels", for example, Description
226 /// environment. The text for this user-editable label is stored in
227 /// the paragraph alongside the text of the rest of the paragraph
228 /// (the body). This function returns the starting position of the
229 /// body of the text in the paragraph.
230 lyx::pos_type beginOfBody() const;
231 /// recompute this value
232 void setBeginOfBody();
235 std::string const & getLabelstring() const;
237 /// the next two functions are for the manual labels
238 std::string const getLabelWidthString() const;
240 void setLabelWidthString(std::string const & s);
242 char getAlign() const;
243 /// The nesting depth of a paragraph
244 depth_type getDepth() const;
245 /// The maximal possible depth of a paragraph after this one
246 depth_type getMaxDepthAfter() const;
248 void applyLayout(LyXLayout_ptr const & new_layout);
251 void eraseIntern(lyx::pos_type pos);
252 /// erase the char at the given position
253 bool erase(lyx::pos_type pos);
254 /// erase the given range. Returns the number of chars actually erased
255 int erase(lyx::pos_type start, lyx::pos_type end);
257 /** Get uninstantiated font setting. Returns the difference
258 between the characters font and the layoutfont.
259 This is what is stored in the fonttable
262 getFontSettings(BufferParams const &, lyx::pos_type pos) const;
264 LyXFont const getFirstFontSettings() const;
266 /** Get fully instantiated font. If pos == -1, use the layout
267 font attached to this paragraph.
268 If pos == -2, use the label font of the layout attached here.
269 In all cases, the font is instantiated, i.e. does not have any
270 attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
273 LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
274 LyXFont const & outerfont) const;
275 LyXFont const getLayoutFont(BufferParams const &,
276 LyXFont const & outerfont) const;
277 LyXFont const getLabelFont(BufferParams const &,
278 LyXFont const & outerfont) const;
280 * The font returned by the above functions is the same in a
281 * span of characters. This method will return the last position
282 * in the paragraph for which that font is the same.
283 * This can be used to avoid unnecessary calls to getFont.
285 lyx::pos_type getEndPosOfFontSpan(lyx::pos_type pos) const;
287 /// this is a bottleneck.
288 value_type getChar(lyx::pos_type pos) const
290 BOOST_ASSERT(pos >= 0);
291 BOOST_ASSERT(pos < int(text_.size()));
295 value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
296 /// The position must already exist.
297 void setChar(lyx::pos_type pos, value_type c);
298 /// pos <= size() (there is a dummy font change at the end of each par)
299 void setFont(lyx::pos_type pos, LyXFont const & font);
300 /// Returns the height of the highest font in range
301 LyXFont_size highestFontInRange(lyx::pos_type startpos,
302 lyx::pos_type endpos,
303 LyXFont_size def_size) const;
305 void insert(lyx::pos_type pos, std::string const & str,
306 LyXFont const & font);
308 void insertChar(lyx::pos_type pos, value_type c,
309 Change change = Change(Change::INSERTED));
311 void insertChar(lyx::pos_type pos, value_type c,
312 LyXFont const &, Change change = Change(Change::INSERTED));
314 bool checkInsertChar(LyXFont &);
316 void insertInset(lyx::pos_type pos, InsetBase * inset,
317 Change change = Change(Change::INSERTED));
319 void insertInset(lyx::pos_type pos, InsetBase * inset,
320 LyXFont const &, Change change = Change(Change::INSERTED));
322 bool insetAllowed(InsetOld_code code);
324 InsetBase * getInset(lyx::pos_type pos);
326 InsetBase const * getInset(lyx::pos_type pos) const;
329 bool isHfill(lyx::pos_type pos) const;
330 /// hinted by profiler
331 bool isInset(lyx::pos_type pos) const { return getChar(pos) == static_cast<value_type>(META_INSET); }
333 bool isNewline(lyx::pos_type pos) const;
335 bool isSeparator(lyx::pos_type pos) const;
337 bool isLineSeparator(lyx::pos_type pos) const;
339 bool isKomma(lyx::pos_type pos) const;
340 /// Used by the spellchecker
341 bool isLetter(lyx::pos_type pos) const;
343 bool isWord(lyx::pos_type pos) const;
345 /// returns -1 if inset not found
346 int getPositionOfInset(InsetBase const * inset) const;
349 int stripLeadingSpaces();
351 /// return true if we allow multiple spaces
352 bool isFreeSpacing() const;
354 /// return true if we allow this par to stay empty
355 bool allowEmpty() const;
357 unsigned char transformChar(unsigned char c, lyx::pos_type pos) const;
359 ParagraphParameters & params();
361 ParagraphParameters const & params() const;
364 RowList::iterator getRow(lyx::pos_type pos);
366 RowList::const_iterator getRow(lyx::pos_type pos) const;
368 size_t row(lyx::pos_type pos) const;
374 mutable RowList rows;
375 /// last draw y position (baseline of top row)
377 /// total height of paragraph
379 /// total width of paragraph, may differ from workwidth
384 LyXLayout_ptr layout_;
385 /// keeping this here instead of in the pimpl makes LyX >10% faster
386 // for average tasks as buffer loading/switching etc.
389 lyx::pos_type begin_of_body_;
393 friend struct Paragraph::Pimpl;
400 inline bool isInsertedText(Paragraph const & par, lyx::pos_type pos)
402 return par.lookupChange(pos) == Change::INSERTED;
406 inline bool isDeletedText(Paragraph const & par, lyx::pos_type pos)
408 return par.lookupChange(pos) == Change::DELETED;
411 #endif // PARAGRAPH_H