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"
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 & 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);
114 /// FIXME: replace Cursor with DocIterator.
115 docstring getStringToIndex(Cursor const & cur);
117 /// Convert the paragraphs to a string.
118 /// \param AsStringParameter options. This can contain any combination of
119 /// asStringParameter values. Valid examples:
120 /// asString(AS_STR_LABEL)
121 /// asString(AS_STR_LABEL | AS_STR_INSETS)
122 /// asString(AS_STR_INSETS)
123 docstring asString(int options = AS_STR_NONE) const;
125 docstring asString(pit_type beg, pit_type end,
126 int options = AS_STR_NONE) const;
128 /// truncates str to maxlenwith an ellipsis and replaces the characters '\n'
129 /// and '\t' with spaces
130 static void shortenForOutliner(docstring & str, size_t const maxlen);
132 /// Appends a possibly abbreviated representation of our text to \param os,
133 /// where \param maxlen defines the maximum size of \param os. If \param
134 /// shorten is true, then os is shortened as above.
135 void forOutliner(docstring & os, size_t maxlen, bool shorten = true) const;
136 /// Appends a possibly abbreviated representation of our text, from
137 /// start to end, to \param os, where \param maxlen defines the
138 /// maximum size of \param os. Omits the label of the first paragraph.
139 void forOutliner(docstring & os, size_t maxlen, pit_type start, pit_type end,
140 bool shorten = true) const;
142 /// insert a character at cursor position
143 /// FIXME: replace Cursor with DocIterator.
144 void insertChar(Cursor & cur, char_type c);
145 /// insert an inset at cursor position
146 /// FIXME: replace Cursor with DocIterator.
147 void insertInset(Cursor & cur, Inset * inset);
149 /// try to handle that request
150 /// FIXME: replace Cursor with DocIterator.
151 void dispatch(Cursor & cur, FuncRequest & cmd);
152 /// do we want to handle this event?
153 bool getStatus(Cursor & cur, FuncRequest const & cmd,
154 FuncStatus & status) const;
156 /// read-only access to individual paragraph
157 Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
158 /// read-write access to individual paragraph
159 Paragraph & getPar(pit_type pit) { return pars_[pit]; }
160 // Returns the current font and depth as a message.
161 // When \param devel_mode is true, add more precise information
162 docstring currentState(CursorData const & cur, bool devel_mode) const;
164 /** Find the word under \c from in the relative location
165 * defined by \c word_location.
166 * @param from return here the start of the word
167 * @param to return here the end of the word
169 void getWord(CursorSlice & from, CursorSlice & to, word_location const) const;
170 /// just selects the word the cursor is in
171 void selectWord(Cursor & cur, word_location loc);
173 void selectAll(Cursor & cur);
174 /// convenience function get the previous word or an empty string
175 docstring previousWord(CursorSlice const & sl) const;
177 /// what type of change operation to make
182 /// accept or reject the selected change
183 void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
184 /// accept the changes within the complete Text
185 void acceptChanges();
186 /// reject the changes within the complete Text
187 void rejectChanges();
189 /// returns true if par was empty and was removed
190 bool setCursor(Cursor & cur, pit_type pit, pos_type pos,
191 bool setfont = true, bool boundary = false);
193 void setCursorIntern(Cursor & cur, pit_type pit,
194 pos_type pos, bool setfont = true, bool boundary = false);
196 /// Move cursor one position backwards
198 * Returns true if an update is needed after the move.
200 bool cursorBackward(Cursor & cur);
201 /// Move cursor visually one position to the left
203 * \param skip_inset if true, don't enter insets
204 * Returns true if an update is needed after the move.
206 bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
207 /// Move cursor one position forward
209 * Returns true if an update is needed after the move.
211 bool cursorForward(Cursor & cur);
212 /// Move cursor visually one position to the right
214 * \param skip_inset if true, don't enter insets
215 * Returns true if an update is needed after the move.
217 bool cursorVisRight(Cursor & cur, bool skip_inset = false);
219 bool cursorBackwardOneWord(Cursor & cur);
221 bool cursorForwardOneWord(Cursor & cur);
223 bool cursorVisLeftOneWord(Cursor & cur);
225 bool cursorVisRightOneWord(Cursor & cur);
226 /// Delete from cursor up to the end of the current or next word.
227 /// Use force to skip the confirmDeletion check.
228 void deleteWordForward(Cursor & cur, bool force = false);
229 /// Delete from cursor to start of current or prior word.
230 void deleteWordBackward(Cursor & cur, bool force = false);
232 bool cursorUpParagraph(Cursor & cur);
234 bool cursorDownParagraph(Cursor & cur);
236 bool cursorTop(Cursor & cur);
238 bool cursorBottom(Cursor & cur);
239 /// Erase character at cursor. Honour change tracking
240 /// FIXME: replace Cursor with DocIterator.
241 bool erase(Cursor & cur);
242 /// Delete character before cursor. Honour CT
243 /// FIXME: replace Cursor with DocIterator.
244 bool backspace(Cursor & cur);
245 // Dissolve the inset under cursor
246 /// FIXME: replace Cursor with DocIterator.
247 bool dissolveInset(Cursor & cur);
249 bool selectWordWhenUnderCursor(Cursor & cur, word_location);
250 /// Change the case of the word at cursor position.
251 void changeCase(Cursor & cur, TextCase action, bool partial);
252 /// Transposes the character at the cursor with the one before it
253 void charsTranspose(Cursor & cur);
255 /** the DTP switches for paragraphs. LyX will store the top settings
256 always in the first physical paragraph, the bottom settings in the
257 last. When a paragraph is broken, the top settings rest, the bottom
258 settings are given to the new one.
259 This function will handle a multi-paragraph selection.
261 void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
262 /// Sets parameters for current or selected paragraphs
263 void setParagraphs(Cursor & cur, ParagraphParameters const & p);
265 /* these things are for search and replace */
267 /// needed to insert the selection
268 void insertStringAsLines(Cursor & cur, docstring const & str,
270 /// needed to insert the selection
271 void insertStringAsParagraphs(Cursor & cur, docstring const & str,
274 /// access to our paragraphs
275 ParagraphList const & paragraphs() const { return pars_; }
276 ParagraphList & paragraphs() { return pars_; }
277 /// return true if this is the main text
278 bool isMainText() const;
281 double spacing(Paragraph const & par) const;
282 /// make a suggestion for a label
283 /// FIXME: replace Cursor with DocIterator.
284 docstring getPossibleLabel(DocIterator const & cur) const;
285 /// is this paragraph right-to-left?
286 bool isRTL(Paragraph const & par) const;
289 bool checkAndActivateInset(Cursor & cur, bool front);
291 bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
294 void write(std::ostream & os) const;
295 /// returns true if \end_document has not been read
296 /// insetPtr is the containing Inset
297 bool read(Lexer & lex, ErrorList & errorList,
298 InsetText * insetPtr);
300 /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
301 /// \retval true if a change has happened and we need a redraw.
302 /// FIXME: replace Cursor with DocIterator. This is not possible right
303 /// now because recordUndo() is called which needs a Cursor.
304 static bool deleteEmptyParagraphMechanism(Cursor & cur,
305 Cursor & old, bool & need_anchor_change);
307 /// delete double spaces, leading spaces, and empty paragraphs
308 /// from \first to \last paragraph
309 void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
311 /// To resolve macros properly the texts get their DocIterator.
312 /// Every macro definition is stored with its DocIterator
313 /// as well. Only those macros with a smaller iterator become
314 /// visible in a paragraph.
315 DocIterator macrocontextPosition() const;
317 void setMacrocontextPosition(DocIterator const & pos);
320 bool completionSupported(Cursor const & cur) const;
322 CompletionList const * createCompletionList(Cursor const & cur) const;
324 bool insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/);
326 docstring completionPrefix(Cursor const & cur) const;
327 /// find a paragraph before \p par with the given \p depth, if such
328 /// a paragraph cannot be found, \p par is returned
329 pit_type depthHook(pit_type pit, depth_type depth) const;
330 /// find a paragraph before \p par with depth less than the
331 /// depth of \p par. If such paragraph cannot be found because
332 /// \p par already has depth 0, lastpar + 1 is returned. If
333 /// such paragraph cannot be found because there isn't a par
334 /// with less depth before this one, \p par is returned.
335 pit_type outerHook(pit_type pit) const;
336 /// Is it the first par with same depth and layout?
337 bool isFirstInSequence(pit_type pit) const;
338 /// Return the last paragraph with same depth and layout, or a strictly
340 pit_type lastInSequence(pit_type pit) const;
341 /// Is this paragraph in the table of contents?
342 int getTocLevel(pit_type pit) const;
343 /// Get the font of the "environment" of paragraph \p par_offset in \p pars.
344 /// All font changes of the paragraph are relative to this font.
345 Font const outerFont(pit_type pit_offset) const;
348 /// The InsetText owner shall have access to everything.
349 friend class InsetText;
351 // fix the cursor `cur' after a characters has been deleted at `where'
352 // position. Called by deleteEmptyParagraphMechanism
353 static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
355 // At cursor position 0, try to merge the paragraph with the one before it.
356 // Ignore change tracking, i.e., physically remove the end-of-par character
357 bool backspacePos0(Cursor & cur);
358 /// handle the case where bibitems were deleted
359 bool handleBibitems(Cursor & cur);
360 /// are we in a list item (description etc.)?
361 bool inDescriptionItem(Cursor & cur) const;
363 void charInserted(Cursor & cur);
364 /// set 'number' font property
365 void number(Cursor & cur);
367 /// paste plain text at current cursor.
368 /// \param str string to paste
369 /// \param asParagraphs whether to paste as paragraphs or as lines
370 void pasteString(Cursor & cur, docstring const & str,
373 void readParToken(Paragraph & par, Lexer & lex, std::string const & token,
374 Font & font, Change & change, ErrorList & errorList);
376 void readParagraph(Paragraph & par, Lexer & lex, ErrorList & errorList);
377 /// Set Label Width string to all paragraphs of the same layout
378 /// and depth in a sequence.
379 void setLabelWidthStringToSequence(Cursor const & cur, docstring const & s);
385 /// position of the text in the buffer.
386 DocIterator macrocontext_position_;
391 void breakParagraphConservative(BufferParams const & bparams,
392 ParagraphList & paragraphs,
397 * Append the next paragraph onto the tail of this one.
398 * Be careful, this doesent make any check at all.
400 void mergeParagraph(BufferParams const & bparams,
401 ParagraphList & paragraphs, pit_type pit);
403 /// accept the changes within the complete ParagraphList
404 void acceptChanges(ParagraphList & pars, BufferParams const & bparams);
406 /// reject the changes within the complete ParagraphList
407 void rejectChanges(ParagraphList & pars, BufferParams const & bparams);