]> git.lyx.org Git - lyx.git/blob - src/Text.h
Pass the encoding to the japanese pLaTeX processor (#4697).
[lyx.git] / src / Text.h
1 // -*- C++ -*-
2 /**
3  * \file Text.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author unknown
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef TEXT_H
15 #define TEXT_H
16
17 #include "DocIterator.h"
18 #include "ParagraphList.h"
19
20 namespace lyx {
21
22 class Buffer;
23 class BufferParams;
24 class BufferView;
25 class CompletionList;
26 class Cursor;
27 class CursorSlice;
28 class DocIterator;
29 class ErrorList;
30 class Font;
31 class FontInfo;
32 class FuncRequest;
33 class FuncStatus;
34 class Inset;
35 class Lexer;
36 class PainterInfo;
37 class Spacing;
38
39 /// This class encapsulates the main text data and operations in LyX.
40 /// This is more or less the private implementation of InsetText.
41 class Text {
42 private:
43         /// Default constructor.
44         Text(InsetText * owner, bool use_default_layout);
45
46         /// Copy constructor.
47         Text(InsetText * owner, Text const & text);
48
49 public:
50         /// \return true if there's no content at all.
51         /// \warning a non standard layout on an empty paragraph doesn't
52         // count as empty.
53         bool empty() const;
54         /// Access to owner InsetText.
55         InsetText const & inset() const;
56
57         ///
58         FontInfo layoutFont(pit_type pit) const;
59         ///
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).
64          */
65         void setCharFont(pit_type pit, pos_type pos,
66                 Font const & font, Font const & display_font);
67
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.
72          */
73         void setInsetFont(BufferView const & bv, pit_type pit, pos_type pos,
74                 Font const & font, bool toggleall = false);
75
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);
81
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.
86         /// FIXME: replace Cursor with DocIterator.
87         void setLayout(Cursor & cur, docstring const & layout);
88
89         /// what type of depth change to make
90         enum DEPTH_CHANGE {
91                 INC_DEPTH,
92                 DEC_DEPTH
93         };
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);
97
98         /// Returns whether something would be changed by changeDepth
99         /// FIXME: replace Cursor with DocIterator.
100         bool changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const;
101
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 &,
108                 bool toggleall = false);
109
110         ///
111         void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
112
113         /// ???
114         /// FIXME: replace Cursor with DocIterator.
115         docstring getStringToIndex(Cursor const & cur);
116
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;
124         ///
125         docstring asString(pit_type beg, pit_type end,
126                 int options = AS_STR_NONE) const;
127         /// Appends a possibly abbreviated representation of our text
128         /// to \param os, where \param maxlen defines the maximum size
129         /// of \param os. If \param shorten is true, then we will shorten
130         /// \param os to maxlen chars and replace the final three by "...,
131         /// if \param os is longer than maxlen chars.
132         void forToc(docstring & os, size_t maxlen, bool shorten = true) const;
133
134         /// insert a character at cursor position
135         /// FIXME: replace Cursor with DocIterator.
136         void insertChar(Cursor & cur, char_type c);
137         /// insert an inset at cursor position
138         /// FIXME: replace Cursor with DocIterator.
139         void insertInset(Cursor & cur, Inset * inset);
140
141         /// try to handle that request
142         /// FIXME: replace Cursor with DocIterator.
143         void dispatch(Cursor & cur, FuncRequest & cmd);
144         /// do we want to handle this event?
145         bool getStatus(Cursor & cur, FuncRequest const & cmd,
146                 FuncStatus & status) const;
147
148         /// read-only access to individual paragraph
149         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
150         /// read-write access to individual paragraph
151         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
152         // Returns the current font and depth as a message.
153         /// FIXME: replace Cursor with DocIterator.
154         docstring currentState(Cursor const & cur) const;
155
156         /** Find the word under \c from in the relative location
157          *  defined by \c word_location.
158          *  @param from return here the start of the word
159          *  @param to return here the end of the word
160          */
161         void getWord(CursorSlice & from, CursorSlice & to, word_location const) const;
162         /// just selects the word the cursor is in
163         void selectWord(Cursor & cur, word_location loc);
164         /// select all text
165         void selectAll(Cursor & cur);
166         /// convenience function get the previous word or an empty string
167         docstring previousWord(CursorSlice const & sl) const;
168         
169         /// what type of change operation to make
170         enum ChangeOp {
171                 ACCEPT,
172                 REJECT
173         };
174         /// accept or reject the selected change
175         void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
176         /// accept the changes within the complete Text
177         void acceptChanges();
178         /// reject the changes within the complete Text
179         void rejectChanges();
180
181         /// returns true if par was empty and was removed
182         bool setCursor(Cursor & cur, pit_type par, pos_type pos,
183                        bool setfont = true, bool boundary = false);
184         ///
185         void setCursor(CursorSlice &, pit_type par, pos_type pos);
186         ///
187         void setCursorIntern(Cursor & cur, pit_type par,
188                  pos_type pos, bool setfont = true, bool boundary = false);
189
190         ///
191         void recUndo(Cursor & cur, pit_type first, pit_type last) const;
192         ///
193         void recUndo(Cursor & cur, pit_type first) const;
194
195         /// Move cursor one position backwards
196         /**
197          * Returns true if an update is needed after the move.
198          */
199         bool cursorBackward(Cursor & cur);
200         /// Move cursor visually one position to the left
201         /**
202          * \param skip_inset if true, don't enter insets
203          * Returns true if an update is needed after the move.
204          */
205         bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
206         /// Move cursor one position forward
207         /**
208          * Returns true if an update is needed after the move.
209          */
210         bool cursorForward(Cursor & cur);
211         /// Move cursor visually one position to the right
212         /**
213          * \param skip_inset if true, don't enter insets
214          * Returns true if an update is needed after the move.
215          */
216         bool cursorVisRight(Cursor & cur, bool skip_inset = false);
217         ///
218         bool cursorBackwardOneWord(Cursor & cur);
219         ///
220         bool cursorForwardOneWord(Cursor & cur);
221         ///
222         bool cursorVisLeftOneWord(Cursor & cur);
223         ///
224         bool cursorVisRightOneWord(Cursor & cur);
225         /// Delete from cursor up to the end of the current or next word.
226         void deleteWordForward(Cursor & cur);
227         /// Delete from cursor to start of current or prior word.
228         void deleteWordBackward(Cursor & cur);
229         ///
230         bool cursorUpParagraph(Cursor & cur);
231         ///
232         bool cursorDownParagraph(Cursor & cur);
233         ///
234         bool cursorTop(Cursor & cur);
235         ///
236         bool cursorBottom(Cursor & cur);
237         /// Erase character at cursor. Honour change tracking
238         /// FIXME: replace Cursor with DocIterator.
239         bool erase(Cursor & cur);
240         /// Delete character before cursor. Honour CT
241         /// FIXME: replace Cursor with DocIterator.
242         bool backspace(Cursor & cur);
243         // Dissolve the inset under cursor
244         /// FIXME: replace Cursor with DocIterator.
245         bool dissolveInset(Cursor & cur);
246         ///
247         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
248         /// Change the case of the word at cursor position.
249         void changeCase(Cursor & cur, TextCase action);
250         /// Transposes the character at the cursor with the one before it
251         void charsTranspose(Cursor & cur);
252
253         /** the DTP switches for paragraphs. LyX will store the top settings
254          always in the first physical paragraph, the bottom settings in the
255          last. When a paragraph is broken, the top settings rest, the bottom
256          settings are given to the new one.
257          This function will handle a multi-paragraph selection.
258          */
259         void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
260         /// Sets parameters for current or selected paragraphs
261         void setParagraphs(Cursor & cur, ParagraphParameters const & p);
262
263         /* these things are for search and replace */
264
265         /// needed to insert the selection
266         void insertStringAsLines(Cursor & cur, docstring const & str,
267                 Font const & font);
268         /// needed to insert the selection
269         void insertStringAsParagraphs(Cursor & cur, docstring const & str,
270                 Font const & font);
271
272         /// access to our paragraphs
273         ParagraphList const & paragraphs() const { return pars_; }
274         ParagraphList & paragraphs() { return pars_; }
275         /// return true if this is the main text
276         bool isMainText() const;
277
278         ///
279         double spacing(Paragraph const & par) const;
280         /// make a suggestion for a label
281         /// FIXME: replace Cursor with DocIterator.
282         docstring getPossibleLabel(Cursor const & cur) const;
283         /// is this paragraph right-to-left?
284         bool isRTL(Paragraph const & par) const;
285
286         ///
287         bool checkAndActivateInset(Cursor & cur, bool front);
288         ///
289         bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
290
291         ///
292         void write(std::ostream & os) const;
293         /// returns true if \end_document has not been read
294         /// insetPtr is the containing Inset
295         bool read(Lexer & lex, ErrorList & errorList, 
296                   InsetText * insetPtr);
297
298         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
299         /// \retval true if a change has happened and we need a redraw.
300         /// FIXME: replace Cursor with DocIterator. This is not possible right
301         /// now because recordUndo() is called which needs a Cursor.
302         static bool deleteEmptyParagraphMechanism(Cursor & cur,
303                 Cursor & old, bool & need_anchor_change);
304
305         /// delete double spaces, leading spaces, and empty paragraphs
306         /// from \first to \last paragraph
307         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
308
309         /// To resolve macros properly the texts get their DocIterator.
310         /// Every macro definition is stored with its DocIterator
311         /// as well. Only those macros with a smaller iterator become 
312         /// visible in a paragraph.
313         DocIterator macrocontextPosition() const;
314         ///
315         void setMacrocontextPosition(DocIterator const & pos);
316
317         ///
318         bool completionSupported(Cursor const & cur) const;
319         ///
320         CompletionList const * createCompletionList(Cursor const & cur) const;
321         ///
322         bool insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/);
323         ///
324         docstring completionPrefix(Cursor const & cur) const;
325         /// for the environments
326         pit_type depthHook(pit_type par, depth_type depth) const;
327         ///
328         pit_type outerHook(pit_type par) const;
329         /// Is it the first par with same depth and layout?
330         bool isFirstInSequence(pit_type par) const;
331         /// Get the font of the "environment" of paragraph \p par_offset in \p pars.
332         /// All font changes of the paragraph are relative to this font.
333         Font const outerFont(pit_type par_offset) const;
334
335 private:
336         /// The InsetText owner shall have access to everything.
337         friend class InsetText;
338
339         /// return past-the-last paragraph influenced by a layout
340         /// change on pit
341         pit_type undoSpan(pit_type pit);
342
343         // fix the cursor `cur' after a characters has been deleted at `where'
344         // position. Called by deleteEmptyParagraphMechanism
345         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
346
347         // At cursor position 0, try to merge the paragraph with the one before it.
348         // Ignore change tracking, i.e., physically remove the end-of-par character
349         bool backspacePos0(Cursor & cur);
350         /// handle the case where bibitems were deleted
351         bool handleBibitems(Cursor & cur);
352         /// are we in a list item (description etc.)?
353         bool inDescriptionItem(Cursor & cur) const;
354         ///
355         void charInserted(Cursor & cur);
356         /// set 'number' font property
357         void number(Cursor & cur);
358
359         /// paste plain text at current cursor.
360         /// \param str string to paste
361         /// \param asParagraphs whether to paste as paragraphs or as lines
362         void pasteString(Cursor & cur, docstring const & str,
363                         bool asParagraphs);
364         ///
365         void readParToken(Paragraph & par, Lexer & lex, std::string const & token,
366                 Font & font, Change & change, ErrorList & errorList);
367         ///
368         void readParagraph(Paragraph & par, Lexer & lex, ErrorList & errorList);
369         /// Set Label Width string to all paragraphs of the same layout
370     /// and depth in a sequence.
371         void setLabelWidthStringToSequence(pit_type const par_offset, docstring const & s);
372
373         /// Owner Inset.
374         InsetText * owner_;
375         ///
376         ParagraphList pars_;
377         ///
378         bool autoBreakRows_;
379         /// position of the text in the buffer.
380         DocIterator macrocontext_position_;
381         ///
382         unsigned int undo_counter_;
383 };
384
385
386 ///
387 void breakParagraphConservative(BufferParams const & bparams,
388                                 ParagraphList & paragraphs,
389                                 pit_type par,
390                                 pos_type pos);
391
392 /**
393  * Append the next paragraph onto the tail of this one.
394  * Be careful, this doesent make any check at all.
395  */
396 void mergeParagraph(BufferParams const & bparams,
397         ParagraphList & paragraphs, pit_type par);
398
399 /// accept the changes within the complete ParagraphList
400 void acceptChanges(ParagraphList & pars, BufferParams const & bparams);
401
402 /// reject the changes within the complete ParagraphList
403 void rejectChanges(ParagraphList & pars, BufferParams const & bparams);
404
405 } // namespace lyx
406
407 #endif // TEXT_H