4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Lars Gullik Bjønnes
11 * Full author contact details are available in file CREDITS.
17 #include "DocIterator.h"
18 #include "ParagraphList.h"
39 /// This class encapsulates the main text data and operations in LyX.
40 /// This is more or less the private implementation of InsetText.
43 /// Default constructor.
44 Text(InsetText * owner, bool use_default_layout);
47 Text(InsetText * owner, Text const & text);
50 /// \return true if there's no content at all.
51 /// \warning a non standard layout on an empty paragraph doesn't
54 /// Access to owner InsetText.
55 InsetText const & inset() const;
58 FontInfo layoutFont(pit_type pit) const;
60 FontInfo labelFont(Paragraph const & par) const;
61 /** Set font of character at position \p pos in paragraph \p pit.
62 * Must not be called if \p pos denotes an inset with text contents,
63 * and the inset is not allowed inside a font change (see below).
65 void setCharFont(pit_type pit, pos_type pos,
66 Font const & font, Font const & display_font);
68 /** Needed to propagate font changes to all text cells of insets
69 * that are not allowed inside a font change (bugs 1973, 6919).
70 * Must not be called if \p pos denotes an ordinary character or an
71 * inset that is alowed inside a font change.
73 void setInsetFont(BufferView const & bv, pit_type pit, pos_type pos,
76 /// what you expect when pressing \<enter\> at cursor position
77 /// \param inverse_logic if false, the same layout is set for the
78 /// new paragraph if the layout is an environment; if true, the
79 /// same layout is set if it is not an environment
80 void breakParagraph(Cursor & cur, bool inverse_logic = false);
82 /// set layout over selection
83 void setLayout(pit_type start, pit_type end,
84 docstring const & layout);
85 /// Set given layout to current cursor position or selection.
87 void setLayout(Cursor & cur, docstring const & layout);
89 /// what type of depth change to make
94 /// Increase or decrease the nesting depth of the selected paragraph(s)
95 /// FIXME: replace Cursor with DocIterator.
96 void changeDepth(Cursor & cur, DEPTH_CHANGE type);
98 /// Returns whether something would be changed by changeDepth
99 /// FIXME: replace Cursor with DocIterator.
100 bool changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const;
102 /// Set font over selection paragraphs and rebreak.
103 /// FIXME: replace Cursor with DocIterator.
104 void setFont(Cursor & cur, Font const &, bool toggleall = false);
105 /// Set font from \p begin to \p end and rebreak.
106 void setFont(BufferView const & bv, CursorSlice const & begin,
107 CursorSlice const & end, Font const &);
110 void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
113 /// FIXME: replace Cursor with DocIterator.
114 docstring getStringToIndex(Cursor const & cur);
116 /// Convert the paragraphs to a string.
117 /// \param AsStringParameter options. This can contain any combination of
118 /// asStringParameter values. Valid examples:
119 /// asString(AS_STR_LABEL)
120 /// asString(AS_STR_LABEL | AS_STR_INSETS)
121 /// asString(AS_STR_INSETS)
122 docstring asString(int options = AS_STR_NONE) const;
124 docstring asString(pit_type beg, pit_type end,
125 int options = AS_STR_NONE) const;
127 /// truncates str to maxlenwith an ellipsis and replaces the characters '\n'
128 /// and '\t' with spaces
129 static void shortenForOutliner(docstring & str, size_t const maxlen);
131 /// Appends a possibly abbreviated representation of our text to \param os,
132 /// where \param maxlen defines the maximum size of \param os. If \param
133 /// shorten is true, then os is shortened as above
134 void forOutliner(docstring & os, size_t const maxlen,
135 bool const shorten = true) const;
137 /// insert a character at cursor position
138 /// FIXME: replace Cursor with DocIterator.
139 void insertChar(Cursor & cur, char_type c);
140 /// insert an inset at cursor position
141 /// FIXME: replace Cursor with DocIterator.
142 void insertInset(Cursor & cur, Inset * inset);
144 /// try to handle that request
145 /// FIXME: replace Cursor with DocIterator.
146 void dispatch(Cursor & cur, FuncRequest & cmd);
147 /// do we want to handle this event?
148 bool getStatus(Cursor & cur, FuncRequest const & cmd,
149 FuncStatus & status) const;
151 /// read-only access to individual paragraph
152 Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
153 /// read-write access to individual paragraph
154 Paragraph & getPar(pit_type pit) { return pars_[pit]; }
155 // Returns the current font and depth as a message.
156 /// FIXME: replace Cursor with DocIterator.
157 docstring currentState(Cursor const & cur) const;
159 /** Find the word under \c from in the relative location
160 * defined by \c word_location.
161 * @param from return here the start of the word
162 * @param to return here the end of the word
164 void getWord(CursorSlice & from, CursorSlice & to, word_location const) const;
165 /// just selects the word the cursor is in
166 void selectWord(Cursor & cur, word_location loc);
168 void selectAll(Cursor & cur);
169 /// convenience function get the previous word or an empty string
170 docstring previousWord(CursorSlice const & sl) const;
172 /// what type of change operation to make
177 /// accept or reject the selected change
178 void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
179 /// accept the changes within the complete Text
180 void acceptChanges();
181 /// reject the changes within the complete Text
182 void rejectChanges();
184 /// returns true if par was empty and was removed
185 bool setCursor(Cursor & cur, pit_type par, pos_type pos,
186 bool setfont = true, bool boundary = false);
188 void setCursor(CursorSlice &, pit_type par, pos_type pos);
190 void setCursorIntern(Cursor & cur, pit_type par,
191 pos_type pos, bool setfont = true, bool boundary = false);
193 /// Move cursor one position backwards
195 * Returns true if an update is needed after the move.
197 bool cursorBackward(Cursor & cur);
198 /// Move cursor visually one position to the left
200 * \param skip_inset if true, don't enter insets
201 * Returns true if an update is needed after the move.
203 bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
204 /// Move cursor one position forward
206 * Returns true if an update is needed after the move.
208 bool cursorForward(Cursor & cur);
209 /// Move cursor visually one position to the right
211 * \param skip_inset if true, don't enter insets
212 * Returns true if an update is needed after the move.
214 bool cursorVisRight(Cursor & cur, bool skip_inset = false);
216 bool cursorBackwardOneWord(Cursor & cur);
218 bool cursorForwardOneWord(Cursor & cur);
220 bool cursorVisLeftOneWord(Cursor & cur);
222 bool cursorVisRightOneWord(Cursor & cur);
223 /// Delete from cursor up to the end of the current or next word.
224 void deleteWordForward(Cursor & cur);
225 /// Delete from cursor to start of current or prior word.
226 void deleteWordBackward(Cursor & cur);
228 bool cursorUpParagraph(Cursor & cur);
230 bool cursorDownParagraph(Cursor & cur);
232 bool cursorTop(Cursor & cur);
234 bool cursorBottom(Cursor & cur);
235 /// Erase character at cursor. Honour change tracking
236 /// FIXME: replace Cursor with DocIterator.
237 bool erase(Cursor & cur);
238 /// Delete character before cursor. Honour CT
239 /// FIXME: replace Cursor with DocIterator.
240 bool backspace(Cursor & cur);
241 // Dissolve the inset under cursor
242 /// FIXME: replace Cursor with DocIterator.
243 bool dissolveInset(Cursor & cur);
245 bool selectWordWhenUnderCursor(Cursor & cur, word_location);
246 /// Change the case of the word at cursor position.
247 void changeCase(Cursor & cur, TextCase action, bool partial);
248 /// Transposes the character at the cursor with the one before it
249 void charsTranspose(Cursor & cur);
251 /** the DTP switches for paragraphs. LyX will store the top settings
252 always in the first physical paragraph, the bottom settings in the
253 last. When a paragraph is broken, the top settings rest, the bottom
254 settings are given to the new one.
255 This function will handle a multi-paragraph selection.
257 void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
258 /// Sets parameters for current or selected paragraphs
259 void setParagraphs(Cursor & cur, ParagraphParameters const & p);
261 /* these things are for search and replace */
263 /// needed to insert the selection
264 void insertStringAsLines(Cursor & cur, docstring const & str,
266 /// needed to insert the selection
267 void insertStringAsParagraphs(Cursor & cur, docstring const & str,
270 /// access to our paragraphs
271 ParagraphList const & paragraphs() const { return pars_; }
272 ParagraphList & paragraphs() { return pars_; }
273 /// return true if this is the main text
274 bool isMainText() const;
277 double spacing(Paragraph const & par) const;
278 /// make a suggestion for a label
279 /// FIXME: replace Cursor with DocIterator.
280 docstring getPossibleLabel(Cursor const & cur) const;
281 /// is this paragraph right-to-left?
282 bool isRTL(Paragraph const & par) const;
285 bool checkAndActivateInset(Cursor & cur, bool front);
287 bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
290 void write(std::ostream & os) const;
291 /// returns true if \end_document has not been read
292 /// insetPtr is the containing Inset
293 bool read(Lexer & lex, ErrorList & errorList,
294 InsetText * insetPtr);
296 /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
297 /// \retval true if a change has happened and we need a redraw.
298 /// FIXME: replace Cursor with DocIterator. This is not possible right
299 /// now because recordUndo() is called which needs a Cursor.
300 static bool deleteEmptyParagraphMechanism(Cursor & cur,
301 Cursor & old, bool & need_anchor_change);
303 /// delete double spaces, leading spaces, and empty paragraphs
304 /// from \first to \last paragraph
305 void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
307 /// To resolve macros properly the texts get their DocIterator.
308 /// Every macro definition is stored with its DocIterator
309 /// as well. Only those macros with a smaller iterator become
310 /// visible in a paragraph.
311 DocIterator macrocontextPosition() const;
313 void setMacrocontextPosition(DocIterator const & pos);
316 bool completionSupported(Cursor const & cur) const;
318 CompletionList const * createCompletionList(Cursor const & cur) const;
320 bool insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/);
322 docstring completionPrefix(Cursor const & cur) const;
323 /// find a paragraph before \p par with the given \p depth, if such
324 /// a paragraph cannot be found, \p par is returned
325 pit_type depthHook(pit_type par, depth_type depth) const;
326 /// find a paragraph before \p par with depth less than the
327 /// depth of \p par. If such paragraph cannot be found because
328 /// \p par already has depth 0, lastpar + 1 is returned. If
329 /// such paragraph cannot be found because there isn't a par
330 /// with less depth before this one, \p par is returned.
331 pit_type outerHook(pit_type par) const;
332 /// Is it the first par with same depth and layout?
333 bool isFirstInSequence(pit_type par) const;
334 /// Is this paragraph in the table of contents?
335 int getTocLevel(pit_type par) const;
336 /// Get the font of the "environment" of paragraph \p par_offset in \p pars.
337 /// All font changes of the paragraph are relative to this font.
338 Font const outerFont(pit_type par_offset) const;
341 /// The InsetText owner shall have access to everything.
342 friend class InsetText;
344 // fix the cursor `cur' after a characters has been deleted at `where'
345 // position. Called by deleteEmptyParagraphMechanism
346 static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
348 // At cursor position 0, try to merge the paragraph with the one before it.
349 // Ignore change tracking, i.e., physically remove the end-of-par character
350 bool backspacePos0(Cursor & cur);
351 /// handle the case where bibitems were deleted
352 bool handleBibitems(Cursor & cur);
353 /// are we in a list item (description etc.)?
354 bool inDescriptionItem(Cursor & cur) const;
356 void charInserted(Cursor & cur);
357 /// set 'number' font property
358 void number(Cursor & cur);
360 /// paste plain text at current cursor.
361 /// \param str string to paste
362 /// \param asParagraphs whether to paste as paragraphs or as lines
363 void pasteString(Cursor & cur, docstring const & str,
366 void readParToken(Paragraph & par, Lexer & lex, std::string const & token,
367 Font & font, Change & change, ErrorList & errorList);
369 void readParagraph(Paragraph & par, Lexer & lex, ErrorList & errorList);
370 /// Set Label Width string to all paragraphs of the same layout
371 /// and depth in a sequence.
372 void setLabelWidthStringToSequence(Cursor const & cur, docstring const & s);
378 /// position of the text in the buffer.
379 DocIterator macrocontext_position_;
384 void breakParagraphConservative(BufferParams const & bparams,
385 ParagraphList & paragraphs,
390 * Append the next paragraph onto the tail of this one.
391 * Be careful, this doesent make any check at all.
393 void mergeParagraph(BufferParams const & bparams,
394 ParagraphList & paragraphs, pit_type par);
396 /// accept the changes within the complete ParagraphList
397 void acceptChanges(ParagraphList & pars, BufferParams const & bparams);
399 /// reject the changes within the complete ParagraphList
400 void rejectChanges(ParagraphList & pars, BufferParams const & bparams);