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"
41 /// This class encapsulates the main text data and operations in LyX.
42 /// This is more or less the private implementation of InsetText.
45 /// Default constructor.
46 Text(InsetText * owner, bool use_default_layout);
49 Text(InsetText * owner, Text const & text);
52 /// \return true if there's no content at all.
53 /// \warning a non standard layout on an empty paragraph doesn't
56 /// Access to owner InsetText.
57 InsetText const & inset() const;
60 FontInfo layoutFont(pit_type pit) const;
62 FontInfo labelFont(Paragraph const & par) const;
63 /** Set font of character at position \p pos in paragraph \p pit.
64 * Must not be called if \p pos denotes an inset with text contents,
65 * and the inset is not allowed inside a font change (see below).
67 void setCharFont(pit_type pit, pos_type pos,
68 Font const & font, Font const & display_font);
70 /** Needed to propagate font changes to all text cells of insets
71 * that are not allowed inside a font change (bugs 1973, 6919).
72 * Must not be called if \p pos denotes an ordinary character or an
73 * inset that is alowed inside a font change.
75 void setInsetFont(BufferView const & bv, pit_type pit, pos_type pos,
78 /// what you expect when pressing \<enter\> at cursor position
79 /// \param inverse_logic if false, the same layout is set for the
80 /// new paragraph if the layout is an environment; if true, the
81 /// same layout is set if it is not an environment
82 void breakParagraph(Cursor & cur, bool inverse_logic = false);
84 /// set layout over selection
85 void setLayout(pit_type start, pit_type end,
86 docstring const & layout);
87 /// Set given layout to current cursor position or selection.
89 void setLayout(Cursor & cur, docstring const & layout);
91 /// what type of depth change to make
96 /// Increase or decrease the nesting depth of the selected paragraph(s)
97 /// FIXME: replace Cursor with DocIterator.
98 void changeDepth(Cursor const & cur, DEPTH_CHANGE type);
100 /// Returns whether something would be changed by changeDepth
101 /// FIXME: replace Cursor with DocIterator.
102 bool changeDepthAllowed(Cursor const & cur, DEPTH_CHANGE type) const;
104 /// Set font over selection paragraphs and rebreak.
105 /// FIXME: replace Cursor with DocIterator.
106 void setFont(Cursor & cur, Font const &, bool toggleall = false);
107 /// Set font from \p begin to \p end and rebreak.
108 void setFont(BufferView const & bv, CursorSlice const & begin,
109 CursorSlice const & end, Font const &);
112 void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
113 /// Stack to save recent text propterty applications
114 std::vector<docstring> getFreeFonts() const;
117 /// FIXME: replace Cursor with DocIterator.
118 docstring getStringForDialog(Cursor & cur);
120 /// Convert the paragraphs to a string.
121 /// \param AsStringParameter options. This can contain any combination of
122 /// asStringParameter values. Valid examples:
123 /// asString(AS_STR_LABEL)
124 /// asString(AS_STR_LABEL | AS_STR_INSETS)
125 /// asString(AS_STR_INSETS)
126 docstring asString(int options = AS_STR_NONE) const;
128 docstring asString(pit_type beg, pit_type end,
129 int options = AS_STR_NONE) const;
131 /// truncates str to maxlenwith an ellipsis and replaces the characters '\n'
132 /// and '\t' with spaces
133 static void shortenForOutliner(docstring & str, size_t const maxlen);
135 /// Appends a possibly abbreviated representation of our text to \param os,
136 /// where \param maxlen defines the maximum size of \param os. If \param
137 /// shorten is true, then os is shortened as above.
138 void forOutliner(docstring & os, size_t maxlen, bool shorten = true) const;
139 /// Appends a possibly abbreviated representation of our text, from
140 /// start to end, to \param os, where \param maxlen defines the
141 /// maximum size of \param os. Omits the label of the first paragraph.
142 void forOutliner(docstring & os, size_t maxlen, pit_type start, pit_type end,
143 bool shorten = true) const;
145 /// insert a character at cursor position
146 /// FIXME: replace Cursor with DocIterator.
147 void insertChar(Cursor & cur, char_type c);
148 /// insert an inset at cursor position
149 /// FIXME: replace Cursor with DocIterator.
150 void 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);
176 void selectAll(Cursor & cur);
178 /// what type of change operation to make
183 /// accept or reject the selected change
184 void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
185 /// accept the changes within the complete Text
186 void acceptChanges();
187 /// reject the changes within the complete Text
188 void rejectChanges();
190 /// returns true if par was empty and was removed
191 bool setCursor(Cursor & cur, pit_type pit, pos_type pos,
192 bool setfont = true, bool boundary = false);
194 void setCursorIntern(Cursor & cur, pit_type pit,
195 pos_type pos, bool setfont = true, bool boundary = false);
197 /// Move cursor one position backwards
199 * Returns true if an update is needed after the move.
201 bool cursorBackward(Cursor & cur);
202 /// Move cursor visually one position to the left
204 * \param skip_inset if true, don't enter insets
205 * Returns true if an update is needed after the move.
207 bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
208 /// Move cursor one position forward
210 * Returns true if an update is needed after the move.
212 bool cursorForward(Cursor & cur);
213 /// Move cursor visually one position to the right
215 * \param skip_inset if true, don't enter insets
216 * Returns true if an update is needed after the move.
218 bool cursorVisRight(Cursor & cur, bool skip_inset = false);
220 bool cursorBackwardOneWord(Cursor & cur);
222 bool cursorForwardOneWord(Cursor & cur);
224 bool cursorVisLeftOneWord(Cursor & cur);
226 bool cursorVisRightOneWord(Cursor & cur);
227 /// Delete from cursor up to the end of the current or next word.
228 /// Use force to skip the confirmDeletion check.
229 void deleteWordForward(Cursor & cur, bool force = false);
230 /// Delete from cursor to start of current or prior word.
231 void deleteWordBackward(Cursor & cur, bool force = false);
233 bool cursorUpParagraph(Cursor & cur);
235 bool cursorDownParagraph(Cursor & cur);
237 bool cursorTop(Cursor & cur);
239 bool cursorBottom(Cursor & cur);
240 /// Erase character at cursor. Honour change tracking
241 /// FIXME: replace Cursor with DocIterator.
242 bool erase(Cursor & cur);
243 /// Delete character before cursor. Honour CT
244 /// FIXME: replace Cursor with DocIterator.
245 bool backspace(Cursor & cur);
246 // Dissolve the inset under cursor
247 /// FIXME: replace Cursor with DocIterator.
248 bool dissolveInset(Cursor & cur);
250 bool selectWordWhenUnderCursor(Cursor & cur, word_location);
251 /// Change the case of the word at cursor position.
252 void changeCase(Cursor & cur, TextCase action, bool partial);
253 /// Transposes the character at the cursor with the one before it
254 void charsTranspose(Cursor & cur);
256 /** the DTP switches for paragraphs. LyX will store the top settings
257 always in the first physical paragraph, the bottom settings in the
258 last. When a paragraph is broken, the top settings rest, the bottom
259 settings are given to the new one.
260 This function will handle a multi-paragraph selection.
262 void setParagraphs(Cursor const & cur, docstring const & arg, bool modify = false);
263 /// Sets parameters for current or selected paragraphs
264 void setParagraphs(Cursor const & cur, ParagraphParameters const & p);
266 /* these things are for search and replace */
268 /// needed to insert the selection
269 void insertStringAsLines(Cursor & cur, docstring const & str,
271 /// needed to insert the selection
272 void insertStringAsParagraphs(Cursor & cur, docstring const & str,
275 /// access to our paragraphs
276 ParagraphList const & paragraphs() const { return pars_; }
277 ParagraphList & paragraphs() { return pars_; }
278 /// return true if this is the main text
279 bool isMainText() const;
282 double spacing(Paragraph const & par) const;
283 /// make a suggestion for a label
284 /// FIXME: replace Cursor with DocIterator.
285 docstring getPossibleLabel(DocIterator const & cur) const;
286 /// is this paragraph right-to-left?
287 bool isRTL(pit_type pit) const;
290 bool checkAndActivateInset(Cursor & cur, bool front);
292 bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
295 void write(std::ostream & os) const;
296 /// returns true if \end_document has not been read
297 /// insetPtr is the containing Inset
298 bool read(Lexer & lex, ErrorList & errorList,
299 InsetText * insetPtr);
301 /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
302 /// \retval true if a change has happened and we need a redraw.
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 /// Does NOT handle undo (responsibility of the caller)
310 void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
312 /// To resolve macros properly the texts get their DocIterator.
313 /// Every macro definition is stored with its DocIterator
314 /// as well. Only those macros with a smaller iterator become
315 /// visible in a paragraph.
316 DocIterator macrocontextPosition() const;
318 void setMacrocontextPosition(DocIterator const & pos);
321 bool completionSupported(Cursor const & cur) const;
323 CompletionList const * createCompletionList(Cursor const & cur) const;
325 bool insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/);
327 docstring completionPrefix(Cursor const & cur) const;
328 /// find a paragraph before \p par with the given \p depth, if such
329 /// a paragraph cannot be found, \p par is returned
330 pit_type depthHook(pit_type pit, depth_type depth) const;
331 /// find a paragraph before \p par with depth less than the
332 /// depth of \p par. If such paragraph cannot be found because
333 /// \p par already has depth 0, lastpar + 1 is returned. If
334 /// such paragraph cannot be found because there isn't a par
335 /// with less depth before this one, \p par is returned.
336 pit_type outerHook(pit_type pit) const;
337 /// Is it the first par with same depth and layout?
338 bool isFirstInSequence(pit_type pit) const;
339 /// Return the last paragraph with same depth and layout, or a strictly
341 pit_type lastInSequence(pit_type pit) const;
342 /// Is this paragraph in the table of contents?
343 int getTocLevel(pit_type pit) const;
344 /// Get the font of the "environment" of paragraph \p par_offset in \p pars.
345 /// All font changes of the paragraph are relative to this font.
346 Font const outerFont(pit_type pit_offset) const;
347 /// Return the label type at the end of paragraph \c pit.
348 int getEndLabel(pit_type pit) const;
351 /// The InsetText owner shall have access to everything.
352 friend class InsetText;
354 // At cursor position 0, try to merge the paragraph with the one before it.
355 // Ignore change tracking, i.e., physically remove the end-of-par character
356 bool backspacePos0(Cursor & cur);
357 /// handle the case where bibitems were deleted
358 bool handleBibitems(Cursor & cur);
359 /// are we in a list item (description etc.)?
360 bool inDescriptionItem(Cursor const & cur) const;
362 void charInserted(Cursor & cur);
363 /// set 'number' font property
364 void number(Cursor & cur);
366 /// paste plain text at current cursor.
367 /// \param str string to paste
368 /// \param asParagraphs whether to paste as paragraphs or as lines
369 void pasteString(Cursor & cur, docstring const & str,
372 void readParToken(Paragraph & par, Lexer & lex, std::string const & token,
373 Font & font, Change & change, ErrorList & errorList);
375 void readParagraph(Paragraph & par, Lexer & lex, ErrorList & errorList);
376 /// Set Label Width string to all paragraphs of the same layout
377 /// and depth in a sequence.
378 void setLabelWidthStringToSequence(Cursor const & cur, docstring const & s);
384 /// position of the text in the buffer.
385 DocIterator macrocontext_position_;
390 void breakParagraphConservative(BufferParams const & bparams,
391 ParagraphList & paragraphs,
396 * Append the next paragraph onto the tail of this one.
397 * Be careful, this doesent make any check at all.
399 void mergeParagraph(BufferParams const & bparams,
400 ParagraphList & paragraphs, pit_type pit);
402 /// accept the changes within the complete ParagraphList
403 void acceptChanges(ParagraphList & pars, BufferParams const & bparams);
405 /// reject the changes within the complete ParagraphList
406 void rejectChanges(ParagraphList & pars, BufferParams const & bparams);