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"
38 class ParagraphParameters;
40 /// This class encapsulates the main text data and operations in LyX.
41 /// This is more or less the private implementation of InsetText.
44 /// Default constructor.
45 Text(InsetText * owner, bool use_default_layout);
48 Text(InsetText * owner, Text const & text);
51 /// \return true if there's no content at all.
52 /// \warning a non standard layout on an empty paragraph doesn't
55 /// Access to owner InsetText.
56 InsetText const & inset() const;
59 FontInfo layoutFont(pit_type pit) const;
61 FontInfo labelFont(Paragraph const & par) const;
62 /** Set font of character at position \p pos in paragraph \p pit.
63 * Must not be called if \p pos denotes an inset with text contents,
64 * and the inset is not allowed inside a font change (see below).
66 void setCharFont(pit_type pit, pos_type pos,
67 Font const & font, Font const & display_font);
69 /** Needed to propagate font changes to all text cells of insets
70 * that are not allowed inside a font change (bugs 1973, 6919).
71 * Must not be called if \p pos denotes an ordinary character or an
72 * inset that is alowed inside a font change.
74 void setInsetFont(BufferView const & bv, pit_type pit, pos_type pos,
77 /// what you expect when pressing \<enter\> at cursor position
78 /// \param inverse_logic if false, the same layout is set for the
79 /// new paragraph if the layout is an environment; if true, the
80 /// same layout is set if it is not an environment
81 void breakParagraph(Cursor & cur, bool inverse_logic = false);
83 /// set layout over selection
84 void setLayout(pit_type start, pit_type end,
85 docstring const & layout);
86 /// Set given layout to current cursor position or selection.
88 void setLayout(Cursor & cur, docstring const & layout);
90 /// what type of depth change to make
95 /// Increase or decrease the nesting depth of the selected paragraph(s)
96 /// FIXME: replace Cursor with DocIterator.
97 void changeDepth(Cursor & cur, DEPTH_CHANGE type);
99 /// Returns whether something would be changed by changeDepth
100 /// FIXME: replace Cursor with DocIterator.
101 bool changeDepthAllowed(Cursor const & cur, DEPTH_CHANGE type) const;
103 /// Set font over selection paragraphs and rebreak.
104 /// FIXME: replace Cursor with DocIterator.
105 void setFont(Cursor & cur, Font const &, bool toggleall = false);
106 /// Set font from \p begin to \p end and rebreak.
107 void setFont(BufferView const & bv, CursorSlice const & begin,
108 CursorSlice const & end, Font const &);
111 void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
112 /// Stack to save recent text propterty applications
113 std::vector<docstring> getFreeFonts() const;
116 /// FIXME: replace Cursor with DocIterator.
117 docstring getStringForDialog(Cursor & cur);
119 /// Convert the paragraphs to a string.
120 /// \param AsStringParameter options. This can contain any combination of
121 /// asStringParameter values. Valid examples:
122 /// asString(AS_STR_LABEL)
123 /// asString(AS_STR_LABEL | AS_STR_INSETS)
124 /// asString(AS_STR_INSETS)
125 docstring asString(int options = AS_STR_NONE) const;
127 docstring asString(pit_type beg, pit_type end,
128 int options = AS_STR_NONE) const;
130 /// truncates str to maxlenwith an ellipsis and replaces the characters '\n'
131 /// and '\t' with spaces
132 static void shortenForOutliner(docstring & str, size_t const maxlen);
134 /// Appends a possibly abbreviated representation of our text to \param os,
135 /// where \param maxlen defines the maximum size of \param os. If \param
136 /// shorten is true, then os is shortened as above.
137 void forOutliner(docstring & os, size_t maxlen, bool shorten = true) const;
138 /// Appends a possibly abbreviated representation of our text, from
139 /// start to end, to \param os, where \param maxlen defines the
140 /// maximum size of \param os. Omits the label of the first paragraph.
141 void forOutliner(docstring & os, size_t maxlen, pit_type start, pit_type end,
142 bool shorten = true) const;
144 /// FIXME: investigate why those two function behave differently wrt cursor.
145 /// insert a character at cursor position and move cursor forward
146 /// FIXME: replace Cursor with DocIterator.
147 void insertChar(Cursor & cur, char_type c);
148 /// insert an inset at cursor position; do not move cursor
149 /// FIXME: replace Cursor with DocIterator.
150 bool insertInset(Cursor & cur, Inset * inset);
152 /// try to handle that request
153 /// FIXME: replace Cursor with DocIterator.
154 void dispatch(Cursor & cur, FuncRequest & cmd);
155 /// do we want to handle this event?
156 bool getStatus(Cursor & cur, FuncRequest const & cmd,
157 FuncStatus & status) const;
159 /// read-only access to individual paragraph
160 Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
161 /// read-write access to individual paragraph
162 Paragraph & getPar(pit_type pit) { return pars_[pit]; }
163 // Returns the current font and depth as a message.
164 // When \param devel_mode is true, add more precise information
165 docstring currentState(CursorData const & cur, bool devel_mode) const;
167 /** Find the word under \c from in the relative location
168 * defined by \c word_location.
169 * @param from return here the start of the word
170 * @param to return here the end of the word
172 void getWord(CursorSlice & from, CursorSlice & to, word_location const) const;
173 /// just selects the word the cursor is in
174 void selectWord(Cursor & cur, word_location loc);
175 /// expands the selection to the word the cursor is in
176 void expandWordSel(Cursor & cur);
178 void selectAll(Cursor & cur);
180 /// what type of change operation to make
185 /// accept or reject the selected change
186 void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
187 /// accept the changes within the complete Text
188 void acceptChanges();
189 /// reject the changes within the complete Text
190 void rejectChanges();
192 /// returns true if par was empty and was removed
193 bool setCursor(Cursor & cur, pit_type pit, pos_type pos,
194 bool setfont = true, bool boundary = false);
196 void setCursorIntern(Cursor & cur, pit_type pit,
197 pos_type pos, bool setfont = true, bool boundary = false);
199 /// Move cursor one position backwards
201 * Returns true if an update is needed after the move.
203 bool cursorBackward(Cursor & cur);
204 /// Move cursor visually one position to the left
206 * \param skip_inset if true, don't enter insets
207 * Returns true if an update is needed after the move.
209 bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
210 /// Move cursor one position forward
212 * Returns true if an update is needed after the move.
214 bool cursorForward(Cursor & cur);
215 /// Move cursor visually one position to the right
217 * \param skip_inset if true, don't enter insets
218 * Returns true if an update is needed after the move.
220 bool cursorVisRight(Cursor & cur, bool skip_inset = false);
222 bool cursorBackwardOneWord(Cursor & cur);
224 bool cursorForwardOneWord(Cursor & cur);
226 bool cursorVisLeftOneWord(Cursor & cur);
228 bool cursorVisRightOneWord(Cursor & cur);
229 /// Delete from cursor up to the end of the current or next word.
230 /// Use force to skip the confirmDeletion check.
231 void deleteWordForward(Cursor & cur, bool force = false);
232 /// Delete from cursor to start of current or prior word.
233 void deleteWordBackward(Cursor & cur, bool force = false);
235 bool cursorUpParagraph(Cursor & cur);
237 bool cursorDownParagraph(Cursor & cur);
239 bool cursorTop(Cursor & cur);
241 bool cursorBottom(Cursor & cur);
242 /// Erase character at cursor. Honour change tracking
243 /// FIXME: replace Cursor with DocIterator.
244 bool erase(Cursor & cur);
245 /// Delete character before cursor. Honour CT
246 /// FIXME: replace Cursor with DocIterator.
247 bool backspace(Cursor & cur);
248 // Dissolve the inset under cursor
249 /// FIXME: replace Cursor with DocIterator.
250 bool dissolveInset(Cursor & cur);
251 /// FIXME: replace Cursor with DocIterator.
252 bool splitInset(Cursor & cur);
254 bool selectWordWhenUnderCursor(Cursor & cur, word_location);
255 /// Change the case of the word at cursor position.
256 void changeCase(Cursor & cur, TextCase action, bool partial);
257 /// Transposes the character at the cursor with the one before it
258 void charsTranspose(Cursor & cur);
260 /** the DTP switches for paragraphs. LyX will store the top settings
261 always in the first physical paragraph, the bottom settings in the
262 last. When a paragraph is broken, the top settings rest, the bottom
263 settings are given to the new one.
264 This function will handle a multi-paragraph selection.
266 void setParagraphs(Cursor const & cur, docstring const & arg, bool merge = false);
267 /// Sets parameters for current or selected paragraphs
268 void setParagraphs(Cursor const & cur, ParagraphParameters const & p);
270 /* these things are for search and replace */
272 /// needed to insert the selection
273 void insertStringAsLines(Cursor & cur, docstring const & str,
275 /// needed to insert the selection
276 void insertStringAsParagraphs(Cursor & cur, docstring const & str,
279 /// access to our paragraphs
280 ParagraphList const & paragraphs() const { return pars_; }
281 ParagraphList & paragraphs() { return pars_; }
282 /// return true if this is the main text
283 bool isMainText() const;
286 double spacing(Paragraph const & par) const;
287 /// make a suggestion for a label
288 /// FIXME: replace Cursor with DocIterator.
289 docstring getPossibleLabel(DocIterator const & cur) const;
290 /// is this paragraph right-to-left?
291 bool isRTL(pit_type pit) const;
294 bool checkAndActivateInset(Cursor & cur, bool front);
296 bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
299 void write(std::ostream & os) const;
300 /// returns true if \end_document has not been read
301 /// insetPtr is the containing Inset
302 bool read(Lexer & lex, ErrorList & errorList,
303 InsetText * insetPtr);
305 /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
306 /// \retval true if a change has happened and we need a redraw.
308 static bool deleteEmptyParagraphMechanism(Cursor & cur,
309 Cursor & old, bool & need_anchor_change);
311 /// delete double spaces, leading spaces, and empty paragraphs
312 /// from \first to \last paragraph
313 /// Does NOT handle undo (responsibility of the caller)
314 void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
316 /// delete double spaces, leading spaces, and empty paragraphs
317 /// from \first to \last paragraph and \first_pos to \last_pos
318 /// Does NOT handle undo (responsibility of the caller)
319 void deleteEmptyParagraphMechanism(pit_type first, pit_type last,
320 pos_type first_pos, pos_type last_pos,
323 /// To resolve macros properly the texts get their DocIterator.
324 /// Every macro definition is stored with its DocIterator
325 /// as well. Only those macros with a smaller iterator become
326 /// visible in a paragraph.
327 DocIterator macrocontextPosition() const;
329 void setMacrocontextPosition(DocIterator const & pos);
332 bool completionSupported(Cursor const & cur) const;
334 CompletionList const * createCompletionList(Cursor const & cur) const;
335 /// Do a completion at the cursor position. Return true on success.
336 /// The completion does not contain the prefix. Handles undo.
337 bool insertCompletion(Cursor & cur, docstring const & s);
339 docstring completionPrefix(Cursor const & cur) const;
340 /// find a paragraph before \p par with the given \p depth, if such
341 /// a paragraph cannot be found, \p par is returned
342 pit_type depthHook(pit_type pit, depth_type depth) const;
343 /// find a paragraph before \p par with depth less than the
344 /// depth of \p par. If such paragraph cannot be found because
345 /// \p par already has depth 0, lastpar + 1 is returned. If
346 /// such paragraph cannot be found because there isn't a par
347 /// with less depth before this one, \p par is returned.
348 pit_type outerHook(pit_type pit) const;
349 /// Is it the first par with same depth and layout?
350 bool isFirstInSequence(pit_type pit) const;
351 /// Return the last paragraph with same depth and layout, or a strictly
353 pit_type lastInSequence(pit_type pit) const;
354 /// Is this paragraph in the table of contents?
355 int getTocLevel(pit_type pit) const;
356 /// Get the font of the "environment" of paragraph \p par_offset in \p pars.
357 /// All font changes of the paragraph are relative to this font.
358 Font const outerFont(pit_type pit_offset) const;
359 /// Return the label type at the end of paragraph \c pit.
360 int getEndLabel(pit_type pit) const;
363 /// The InsetText owner shall have access to everything.
364 friend class InsetText;
366 // At cursor position 0, try to merge the paragraph with the one before it.
367 // Ignore change tracking, i.e., physically remove the end-of-par character
368 bool backspacePos0(Cursor & cur);
369 /// handle the case where bibitems were deleted
370 bool handleBibitems(Cursor & cur);
371 /// are we in a list item (description etc.)?
372 bool inDescriptionItem(Cursor const & cur) const;
374 void charInserted(Cursor & cur);
375 /// set 'number' font property
376 void number(Cursor & cur);
378 /// paste plain text at current cursor.
379 /// \param str string to paste
380 /// \param asParagraphs whether to paste as paragraphs or as lines
381 void pasteString(Cursor & cur, docstring const & str,
384 void readParToken(Paragraph & par, Lexer & lex, std::string const & token,
385 Font & font, Change & change, ErrorList & errorList);
387 void readParagraph(Paragraph & par, Lexer & lex, ErrorList & errorList);
388 /// Set Label Width string to all paragraphs of the same layout
389 /// and depth in a sequence.
390 void setLabelWidthStringToSequence(Cursor const & cur, docstring const & s);
396 /// position of the text in the buffer.
397 DocIterator macrocontext_position_;
402 void breakParagraphConservative(BufferParams const & bparams,
403 ParagraphList & paragraphs,
408 * Append the next paragraph onto the tail of this one.
409 * Be careful, this doesent make any check at all.
411 void mergeParagraph(BufferParams const & bparams,
412 ParagraphList & paragraphs, pit_type pit);
414 /// accept the changes within the complete ParagraphList
415 void acceptChanges(ParagraphList & pars, BufferParams const & bparams);
417 /// reject the changes within the complete ParagraphList
418 void rejectChanges(ParagraphList & pars, BufferParams const & bparams);