]> git.lyx.org Git - lyx.git/blob - src/Text.cpp
Remerge po files
[lyx.git] / src / Text.cpp
1 /**
2  * \file src/Text.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Dov Feldstern
9  * \author Jean-Marc Lasgouttes
10  * \author John Levon
11  * \author André Pönitz
12  * \author Stefan Schimanski
13  * \author Dekel Tsur
14  * \author Jürgen Vigna
15  *
16  * Full author contact details are available in file CREDITS.
17  */
18
19 #include <config.h>
20
21 #include "Text.h"
22
23 #include "Author.h"
24 #include "Buffer.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "Changes.h"
29 #include "CompletionList.h"
30 #include "Cursor.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "Encoding.h"
34 #include "ErrorList.h"
35 #include "FuncRequest.h"
36 #include "factory.h"
37 #include "InsetList.h"
38 #include "Language.h"
39 #include "Layout.h"
40 #include "Length.h"
41 #include "Lexer.h"
42 #include "lyxfind.h"
43 #include "LyXRC.h"
44 #include "Paragraph.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
47 #include "TextClass.h"
48 #include "TextMetrics.h"
49 #include "VSpace.h"
50 #include "WordLangTuple.h"
51 #include "WordList.h"
52
53 #include "insets/InsetText.h"
54 #include "insets/InsetBibitem.h"
55 #include "insets/InsetCaption.h"
56 #include "insets/InsetNewline.h"
57 #include "insets/InsetNewpage.h"
58 #include "insets/InsetArgument.h"
59 #include "insets/InsetSpace.h"
60 #include "insets/InsetSpecialChar.h"
61 #include "insets/InsetTabular.h"
62
63 #include "support/debug.h"
64 #include "support/docstream.h"
65 #include "support/gettext.h"
66 #include "support/lassert.h"
67 #include "support/lstrings.h"
68 #include "support/textutils.h"
69
70 #include <boost/next_prior.hpp>
71
72 #include <sstream>
73
74 using namespace std;
75 using namespace lyx::support;
76
77 namespace lyx {
78
79 using cap::cutSelection;
80 using cap::pasteParagraphList;
81
82 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
83         Paragraph & toPar, pos_type toPos, BufferParams const & params)
84 {
85         // Note: moveItem() does not honour change tracking!
86         // Therefore, it should only be used for breaking and merging paragraphs
87
88         // We need a copy here because the character at fromPos is going to be erased.
89         Font const tmpFont = fromPar.getFontSettings(params, fromPos);
90         Change const tmpChange = fromPar.lookupChange(fromPos);
91
92         if (Inset * tmpInset = fromPar.getInset(fromPos)) {
93                 fromPar.releaseInset(fromPos);
94                 // The inset is not in fromPar any more.
95                 if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
96                         delete tmpInset;
97                         return false;
98                 }
99                 return true;
100         }
101
102         char_type const tmpChar = fromPar.getChar(fromPos);
103         fromPar.eraseChar(fromPos, false);
104         toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
105         return true;
106 }
107
108
109 void breakParagraphConservative(BufferParams const & bparams,
110         ParagraphList & pars, pit_type par_offset, pos_type pos)
111 {
112         // create a new paragraph
113         Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
114                                        Paragraph());
115         Paragraph & par = pars[par_offset];
116
117         tmp.setInsetOwner(&par.inInset());
118         tmp.makeSameLayout(par);
119
120         LASSERT(pos <= par.size(), /**/);
121
122         if (pos < par.size()) {
123                 // move everything behind the break position to the new paragraph
124                 pos_type pos_end = par.size() - 1;
125
126                 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
127                         if (moveItem(par, pos, tmp, j, bparams)) {
128                                 ++j;
129                         }
130                 }
131                 // Move over the end-of-par change information
132                 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
133                 par.setChange(par.size(), Change(bparams.trackChanges ?
134                                            Change::INSERTED : Change::UNCHANGED));
135         }
136 }
137
138
139 void mergeParagraph(BufferParams const & bparams,
140         ParagraphList & pars, pit_type par_offset)
141 {
142         Paragraph & next = pars[par_offset + 1];
143         Paragraph & par = pars[par_offset];
144
145         pos_type pos_end = next.size() - 1;
146         pos_type pos_insert = par.size();
147
148         // the imaginary end-of-paragraph character (at par.size()) has to be
149         // marked as unmodified. Otherwise, its change is adopted by the first
150         // character of the next paragraph.
151         if (par.isChanged(par.size())) {
152                 LYXERR(Debug::CHANGES,
153                    "merging par with inserted/deleted end-of-par character");
154                 par.setChange(par.size(), Change(Change::UNCHANGED));
155         }
156
157         Change change = next.lookupChange(next.size());
158
159         // move the content of the second paragraph to the end of the first one
160         for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
161                 if (moveItem(next, 0, par, j, bparams)) {
162                         ++j;
163                 }
164         }
165
166         // move the change of the end-of-paragraph character
167         par.setChange(par.size(), change);
168
169         pars.erase(boost::next(pars.begin(), par_offset + 1));
170 }
171
172
173 Text::Text(InsetText * owner, bool use_default_layout)
174         : owner_(owner), autoBreakRows_(false), undo_counter_(0)
175 {
176         pars_.push_back(Paragraph());
177         Paragraph & par = pars_.back();
178         par.setInsetOwner(owner);
179         DocumentClass const & dc = owner->buffer().params().documentClass();
180         if (use_default_layout)
181                 par.setDefaultLayout(dc);
182         else
183                 par.setPlainLayout(dc);
184 }
185
186
187 Text::Text(InsetText * owner, Text const & text)
188         : owner_(owner), autoBreakRows_(text.autoBreakRows_), undo_counter_(0)
189 {
190         pars_ = text.pars_;
191         ParagraphList::iterator const end = pars_.end();
192         ParagraphList::iterator it = pars_.begin();
193         for (; it != end; ++it)
194                 it->setInsetOwner(owner);
195 }
196
197
198 pit_type Text::depthHook(pit_type pit, depth_type depth) const
199 {
200         pit_type newpit = pit;
201
202         if (newpit != 0)
203                 --newpit;
204
205         while (newpit != 0 && pars_[newpit].getDepth() > depth)
206                 --newpit;
207
208         if (pars_[newpit].getDepth() > depth)
209                 return pit;
210
211         return newpit;
212 }
213
214
215 pit_type Text::outerHook(pit_type par_offset) const
216 {
217         Paragraph const & par = pars_[par_offset];
218
219         if (par.getDepth() == 0)
220                 return pars_.size();
221         return depthHook(par_offset, depth_type(par.getDepth() - 1));
222 }
223
224
225 bool Text::isFirstInSequence(pit_type par_offset) const
226 {
227         Paragraph const & par = pars_[par_offset];
228
229         pit_type dhook_offset = depthHook(par_offset, par.getDepth());
230
231         if (dhook_offset == par_offset)
232                 return true;
233
234         Paragraph const & dhook = pars_[dhook_offset];
235
236         return dhook.layout() != par.layout()
237                 || dhook.getDepth() != par.getDepth();
238 }
239
240
241 Font const Text::outerFont(pit_type par_offset) const
242 {
243         depth_type par_depth = pars_[par_offset].getDepth();
244         FontInfo tmpfont = inherit_font;
245
246         // Resolve against environment font information
247         while (par_offset != pit_type(pars_.size())
248                && par_depth
249                && !tmpfont.resolved()) {
250                 par_offset = outerHook(par_offset);
251                 if (par_offset != pit_type(pars_.size())) {
252                         tmpfont.realize(pars_[par_offset].layout().font);
253                         par_depth = pars_[par_offset].getDepth();
254                 }
255         }
256
257         return Font(tmpfont);
258 }
259
260
261 static void acceptOrRejectChanges(ParagraphList & pars,
262         BufferParams const & bparams, Text::ChangeOp op)
263 {
264         pit_type pars_size = static_cast<pit_type>(pars.size());
265
266         // first, accept or reject changes within each individual
267         // paragraph (do not consider end-of-par)
268         for (pit_type pit = 0; pit < pars_size; ++pit) {
269                 // prevent assertion failure
270                 if (!pars[pit].empty()) {
271                         if (op == Text::ACCEPT)
272                                 pars[pit].acceptChanges(0, pars[pit].size());
273                         else
274                                 pars[pit].rejectChanges(0, pars[pit].size());
275                 }
276         }
277
278         // next, accept or reject imaginary end-of-par characters
279         for (pit_type pit = 0; pit < pars_size; ++pit) {
280                 pos_type pos = pars[pit].size();
281                 if (pars[pit].isChanged(pos)) {
282                         // keep the end-of-par char if it is inserted and accepted
283                         // or when it is deleted and rejected.
284                         if (pars[pit].isInserted(pos) == (op == Text::ACCEPT)) {
285                                 pars[pit].setChange(pos, Change(Change::UNCHANGED));
286                         } else {
287                                 if (pit == pars_size - 1) {
288                                         // we cannot remove a par break at the end of the last
289                                         // paragraph; instead, we mark it unchanged
290                                         pars[pit].setChange(pos, Change(Change::UNCHANGED));
291                                 } else {
292                                         mergeParagraph(bparams, pars, pit);
293                                         --pit;
294                                         --pars_size;
295                                 }
296                         }
297                 }
298         }
299 }
300
301
302 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
303 {
304         acceptOrRejectChanges(pars, bparams, Text::ACCEPT);
305 }
306
307
308 void rejectChanges(ParagraphList & pars, BufferParams const & bparams)
309 {
310         acceptOrRejectChanges(pars, bparams, Text::REJECT);
311 }
312
313
314 InsetText const & Text::inset() const
315 {
316         return *owner_;
317 }
318
319
320 void Text::readParToken(Paragraph & par, Lexer & lex,
321         string const & token, Font & font, Change & change, ErrorList & errorList)
322 {
323         Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
324         BufferParams const & bp = buf->params();
325
326         if (token[0] != '\\') {
327                 docstring dstr = lex.getDocString();
328                 par.appendString(dstr, font, change);
329
330         } else if (token == "\\begin_layout") {
331                 lex.eatLine();
332                 docstring layoutname = lex.getDocString();
333
334                 font = Font(inherit_font, bp.language);
335                 change = Change(Change::UNCHANGED);
336
337                 DocumentClass const & tclass = bp.documentClass();
338
339                 if (layoutname.empty())
340                         layoutname = tclass.defaultLayoutName();
341
342                 if (owner_->forcePlainLayout()) {
343                         // in this case only the empty layout is allowed
344                         layoutname = tclass.plainLayoutName();
345                 } else if (par.usePlainLayout()) {
346                         // in this case, default layout maps to empty layout 
347                         if (layoutname == tclass.defaultLayoutName())
348                                 layoutname = tclass.plainLayoutName();
349                 } else { 
350                         // otherwise, the empty layout maps to the default
351                         if (layoutname == tclass.plainLayoutName())
352                                 layoutname = tclass.defaultLayoutName();
353                 }
354
355                 // When we apply an unknown layout to a document, we add this layout to the textclass
356                 // of this document. For example, when you apply class article to a beamer document,
357                 // all unknown layouts such as frame will be added to document class article so that
358                 // these layouts can keep their original names.
359                 tclass.addLayoutIfNeeded(layoutname);
360
361                 par.setLayout(bp.documentClass()[layoutname]);
362
363                 // Test whether the layout is obsolete.
364                 Layout const & layout = par.layout();
365                 if (!layout.obsoleted_by().empty())
366                         par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
367
368                 par.params().read(lex);
369
370         } else if (token == "\\end_layout") {
371                 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
372                        << "Missing \\begin_layout ?");
373         } else if (token == "\\end_inset") {
374                 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
375                        << "Missing \\begin_inset ?");
376         } else if (token == "\\begin_inset") {
377                 Inset * inset = readInset(lex, buf);
378                 if (inset)
379                         par.insertInset(par.size(), inset, font, change);
380                 else {
381                         lex.eatLine();
382                         docstring line = lex.getDocString();
383                         errorList.push_back(ErrorItem(_("Unknown Inset"), line,
384                                             par.id(), 0, par.size()));
385                 }
386         } else if (token == "\\family") {
387                 lex.next();
388                 setLyXFamily(lex.getString(), font.fontInfo());
389         } else if (token == "\\series") {
390                 lex.next();
391                 setLyXSeries(lex.getString(), font.fontInfo());
392         } else if (token == "\\shape") {
393                 lex.next();
394                 setLyXShape(lex.getString(), font.fontInfo());
395         } else if (token == "\\size") {
396                 lex.next();
397                 setLyXSize(lex.getString(), font.fontInfo());
398         } else if (token == "\\lang") {
399                 lex.next();
400                 string const tok = lex.getString();
401                 Language const * lang = languages.getLanguage(tok);
402                 if (lang) {
403                         font.setLanguage(lang);
404                 } else {
405                         font.setLanguage(bp.language);
406                         lex.printError("Unknown language `$$Token'");
407                 }
408         } else if (token == "\\numeric") {
409                 lex.next();
410                 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
411         } else if (token == "\\emph") {
412                 lex.next();
413                 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
414         } else if (token == "\\bar") {
415                 lex.next();
416                 string const tok = lex.getString();
417
418                 if (tok == "under")
419                         font.fontInfo().setUnderbar(FONT_ON);
420                 else if (tok == "no")
421                         font.fontInfo().setUnderbar(FONT_OFF);
422                 else if (tok == "default")
423                         font.fontInfo().setUnderbar(FONT_INHERIT);
424                 else
425                         lex.printError("Unknown bar font flag "
426                                        "`$$Token'");
427         } else if (token == "\\strikeout") {
428                 lex.next();
429                 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
430         } else if (token == "\\uuline") {
431                 lex.next();
432                 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
433         } else if (token == "\\uwave") {
434                 lex.next();
435                 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
436         } else if (token == "\\noun") {
437                 lex.next();
438                 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
439         } else if (token == "\\color") {
440                 lex.next();
441                 setLyXColor(lex.getString(), font.fontInfo());
442         } else if (token == "\\SpecialChar") {
443                 auto_ptr<Inset> inset;
444                 inset.reset(new InsetSpecialChar);
445                 inset->read(lex);
446                 inset->setBuffer(*buf);
447                 par.insertInset(par.size(), inset.release(), font, change);
448         } else if (token == "\\backslash") {
449                 par.appendChar('\\', font, change);
450         } else if (token == "\\LyXTable") {
451                 auto_ptr<Inset> inset(new InsetTabular(buf));
452                 inset->read(lex);
453                 par.insertInset(par.size(), inset.release(), font, change);
454         } else if (token == "\\change_unchanged") {
455                 change = Change(Change::UNCHANGED);
456         } else if (token == "\\change_inserted" || token == "\\change_deleted") {
457                 lex.eatLine();
458                 istringstream is(lex.getString());
459                 unsigned int aid;
460                 time_t ct;
461                 is >> aid >> ct;
462                 map<unsigned int, int> const & am = bp.author_map;
463                 if (am.find(aid) == am.end()) {
464                         errorList.push_back(ErrorItem(_("Change tracking error"),
465                                             bformat(_("Unknown author index for change: %1$d\n"), aid),
466                                             par.id(), 0, par.size()));
467                         change = Change(Change::UNCHANGED);
468                 } else {
469                         if (token == "\\change_inserted")
470                                 change = Change(Change::INSERTED, am.find(aid)->second, ct);
471                         else
472                                 change = Change(Change::DELETED, am.find(aid)->second, ct);
473                 }
474         } else {
475                 lex.eatLine();
476                 errorList.push_back(ErrorItem(_("Unknown token"),
477                         bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
478                         lex.getDocString()),
479                         par.id(), 0, par.size()));
480         }
481 }
482
483
484 void Text::readParagraph(Paragraph & par, Lexer & lex,
485         ErrorList & errorList)
486 {
487         lex.nextToken();
488         string token = lex.getString();
489         Font font;
490         Change change(Change::UNCHANGED);
491
492         while (lex.isOK()) {
493                 readParToken(par, lex, token, font, change, errorList);
494
495                 lex.nextToken();
496                 token = lex.getString();
497
498                 if (token.empty())
499                         continue;
500
501                 if (token == "\\end_layout") {
502                         //Ok, paragraph finished
503                         break;
504                 }
505
506                 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
507                 if (token == "\\begin_layout" || token == "\\end_document"
508                     || token == "\\end_inset" || token == "\\begin_deeper"
509                     || token == "\\end_deeper") {
510                         lex.pushToken(token);
511                         lyxerr << "Paragraph ended in line "
512                                << lex.lineNumber() << "\n"
513                                << "Missing \\end_layout.\n";
514                         break;
515                 }
516         }
517         // Final change goes to paragraph break:
518         par.setChange(par.size(), change);
519
520         // Initialize begin_of_body_ on load; redoParagraph maintains
521         par.setBeginOfBody();
522 }
523
524
525 class TextCompletionList : public CompletionList
526 {
527 public:
528         ///
529         TextCompletionList(Cursor const & cur, WordList const * list)
530                 : buffer_(cur.buffer()), pos_(0), list_(list)
531         {}
532         ///
533         virtual ~TextCompletionList() {}
534         
535         ///
536         virtual bool sorted() const { return true; }
537         ///
538         virtual size_t size() const
539         {
540                 return list_->size();
541         }
542         ///
543         virtual docstring const & data(size_t idx) const
544         {
545                 return list_->word(idx);
546         }
547         
548 private:
549         ///
550         Buffer const * buffer_;
551         ///
552         size_t pos_;
553         ///
554         WordList const * list_;
555 };
556
557
558 bool Text::empty() const
559 {
560         return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
561                 // FIXME: Should we consider the labeled type as empty too? 
562                 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
563 }
564
565
566 double Text::spacing(Paragraph const & par) const
567 {
568         if (par.params().spacing().isDefault())
569                 return owner_->buffer().params().spacing().getValue();
570         return par.params().spacing().getValue();
571 }
572
573
574 /**
575  * This breaks a paragraph at the specified position.
576  * The new paragraph will:
577  * - Decrease depth by one (or change layout to default layout) when
578  *    keep_layout == false  
579  * - keep current depth and layout when keep_layout == true
580  */
581 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos, 
582                     bool keep_layout)
583 {
584         BufferParams const & bparams = text.inset().buffer().params();
585         ParagraphList & pars = text.paragraphs();
586         // create a new paragraph, and insert into the list
587         ParagraphList::iterator tmp =
588                 pars.insert(boost::next(pars.begin(), par_offset + 1),
589                             Paragraph());
590
591         Paragraph & par = pars[par_offset];
592
593         // remember to set the inset_owner
594         tmp->setInsetOwner(&par.inInset());
595         // without doing that we get a crash when typing <Return> at the
596         // end of a paragraph
597         tmp->setPlainOrDefaultLayout(bparams.documentClass());
598
599         // layout stays the same with latex-environments
600         if (keep_layout) {
601                 tmp->setLayout(par.layout());
602                 tmp->setLabelWidthString(par.params().labelWidthString());
603                 tmp->params().depth(par.params().depth());
604         } else if (par.params().depth() > 0) {
605                 Paragraph const & hook = pars[text.outerHook(par_offset)];
606                 tmp->setLayout(hook.layout());
607                 // not sure the line below is useful
608                 tmp->setLabelWidthString(par.params().labelWidthString());
609                 tmp->params().depth(hook.params().depth());
610         }
611
612         bool const isempty = (par.allowEmpty() && par.empty());
613
614         if (!isempty && (par.size() > pos || par.empty())) {
615                 tmp->setLayout(par.layout());
616                 tmp->params().align(par.params().align());
617                 tmp->setLabelWidthString(par.params().labelWidthString());
618
619                 tmp->params().depth(par.params().depth());
620                 tmp->params().noindent(par.params().noindent());
621
622                 // move everything behind the break position
623                 // to the new paragraph
624
625                 /* Note: if !keepempty, empty() == true, then we reach
626                  * here with size() == 0. So pos_end becomes - 1. This
627                  * doesn't cause problems because both loops below
628                  * enforce pos <= pos_end and 0 <= pos
629                  */
630                 pos_type pos_end = par.size() - 1;
631
632                 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
633                         if (moveItem(par, pos, *tmp, j, bparams)) {
634                                 ++j;
635                         }
636                 }
637         }
638
639         // Move over the end-of-par change information
640         tmp->setChange(tmp->size(), par.lookupChange(par.size()));
641         par.setChange(par.size(), Change(bparams.trackChanges ?
642                                            Change::INSERTED : Change::UNCHANGED));
643
644         if (pos) {
645                 // Make sure that we keep the language when
646                 // breaking paragraph.
647                 if (tmp->empty()) {
648                         Font changed = tmp->getFirstFontSettings(bparams);
649                         Font const & old = par.getFontSettings(bparams, par.size());
650                         changed.setLanguage(old.language());
651                         tmp->setFont(0, changed);
652                 }
653
654                 return;
655         }
656
657         if (!isempty) {
658                 bool const soa = par.params().startOfAppendix();
659                 par.params().clear();
660                 // do not lose start of appendix marker (bug 4212)
661                 par.params().startOfAppendix(soa);
662                 par.setPlainOrDefaultLayout(bparams.documentClass());
663         }
664
665         // layout stays the same with latex-environments
666         if (keep_layout) {
667                 par.setLayout(tmp->layout());
668                 par.setLabelWidthString(tmp->params().labelWidthString());
669                 par.params().depth(tmp->params().depth());
670         }
671 }
672
673
674 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
675 {
676         LASSERT(this == cur.text(), /**/);
677
678         Paragraph & cpar = cur.paragraph();
679         pit_type cpit = cur.pit();
680
681         DocumentClass const & tclass = cur.buffer()->params().documentClass();
682         Layout const & layout = cpar.layout();
683
684         if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
685                 if (changeDepthAllowed(cur, DEC_DEPTH))
686                         changeDepth(cur, DEC_DEPTH);
687                 else 
688                         setLayout(cur, tclass.defaultLayoutName());
689                 return;
690         }
691
692         // a layout change may affect also the following paragraph
693         recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
694
695         // Always break behind a space
696         // It is better to erase the space (Dekel)
697         if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
698                 cpar.eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
699
700         // What should the layout for the new paragraph be?
701         bool keep_layout = inverse_logic ? 
702                 !layout.isEnvironment() 
703                 : layout.isEnvironment();
704
705         // We need to remember this before we break the paragraph, because
706         // that invalidates the layout variable
707         bool sensitive = layout.labeltype == LABEL_SENSITIVE;
708
709         // we need to set this before we insert the paragraph.
710         bool const isempty = cpar.allowEmpty() && cpar.empty();
711
712         lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
713
714         // After this, neither paragraph contains any rows!
715
716         cpit = cur.pit();
717         pit_type next_par = cpit + 1;
718
719         // well this is the caption hack since one caption is really enough
720         if (sensitive) {
721                 if (cur.pos() == 0)
722                         // set to standard-layout
723                 //FIXME Check if this should be plainLayout() in some cases
724                         pars_[cpit].applyLayout(tclass.defaultLayout());
725                 else
726                         // set to standard-layout
727                         //FIXME Check if this should be plainLayout() in some cases
728                         pars_[next_par].applyLayout(tclass.defaultLayout());
729         }
730
731         while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
732                 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().trackChanges))
733                         break; // the character couldn't be deleted physically due to change tracking
734         }
735
736         // A singlePar update is not enough in this case.
737         cur.screenUpdateFlags(Update::Force);
738         cur.forceBufferUpdate();
739
740         // This check is necessary. Otherwise the new empty paragraph will
741         // be deleted automatically. And it is more friendly for the user!
742         if (cur.pos() != 0 || isempty)
743                 setCursor(cur, cur.pit() + 1, 0);
744         else
745                 setCursor(cur, cur.pit(), 0);
746 }
747
748
749 // needed to insert the selection
750 void Text::insertStringAsLines(DocIterator const & dit, docstring const & str,
751                 Font const & font)
752 {
753         BufferParams const & bparams = owner_->buffer().params();
754         pit_type pit = dit.pit();
755         pos_type pos = dit.pos();
756
757         // insert the string, don't insert doublespace
758         bool space_inserted = true;
759         for (docstring::const_iterator cit = str.begin();
760             cit != str.end(); ++cit) {
761                 Paragraph & par = pars_[pit];
762                 if (*cit == '\n') {
763                         if (autoBreakRows_ && (!par.empty() || par.allowEmpty())) {
764                                 lyx::breakParagraph(*this, pit, pos,
765                                         par.layout().isEnvironment());
766                                 ++pit;
767                                 pos = 0;
768                                 space_inserted = true;
769                         } else {
770                                 continue;
771                         }
772                         // do not insert consecutive spaces if !free_spacing
773                 } else if ((*cit == ' ' || *cit == '\t') &&
774                            space_inserted && !par.isFreeSpacing()) {
775                         continue;
776                 } else if (*cit == '\t') {
777                         if (!par.isFreeSpacing()) {
778                                 // tabs are like spaces here
779                                 par.insertChar(pos, ' ', font, bparams.trackChanges);
780                                 ++pos;
781                                 space_inserted = true;
782                         } else {
783                                 par.insertChar(pos, *cit, font, bparams.trackChanges);
784                                 ++pos;
785                                 space_inserted = true;
786                         }
787                 } else if (!isPrintable(*cit)) {
788                         // Ignore unprintables
789                         continue;
790                 } else {
791                         // just insert the character
792                         par.insertChar(pos, *cit, font, bparams.trackChanges);
793                         ++pos;
794                         space_inserted = (*cit == ' ');
795                 }
796         }
797 }
798
799
800 // turn double CR to single CR, others are converted into one
801 // blank. Then insertStringAsLines is called
802 void Text::insertStringAsParagraphs(DocIterator const & dit, docstring const & str,
803                 Font const & font)
804 {
805         docstring linestr = str;
806         bool newline_inserted = false;
807
808         for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
809                 if (linestr[i] == '\n') {
810                         if (newline_inserted) {
811                                 // we know that \r will be ignored by
812                                 // insertStringAsLines. Of course, it is a dirty
813                                 // trick, but it works...
814                                 linestr[i - 1] = '\r';
815                                 linestr[i] = '\n';
816                         } else {
817                                 linestr[i] = ' ';
818                                 newline_inserted = true;
819                         }
820                 } else if (isPrintable(linestr[i])) {
821                         newline_inserted = false;
822                 }
823         }
824         insertStringAsLines(dit, linestr, font);
825 }
826
827
828 // insert a character, moves all the following breaks in the
829 // same Paragraph one to the right and make a rebreak
830 void Text::insertChar(Cursor & cur, char_type c)
831 {
832         LASSERT(this == cur.text(), /**/);
833
834         cur.recordUndo(INSERT_UNDO);
835
836         TextMetrics const & tm = cur.bv().textMetrics(this);
837         Buffer const & buffer = *cur.buffer();
838         Paragraph & par = cur.paragraph();
839         // try to remove this
840         pit_type const pit = cur.pit();
841
842         bool const freeSpacing = par.layout().free_spacing ||
843                 par.isFreeSpacing();
844
845         if (lyxrc.auto_number) {
846                 static docstring const number_operators = from_ascii("+-/*");
847                 static docstring const number_unary_operators = from_ascii("+-");
848                 static docstring const number_seperators = from_ascii(".,:");
849
850                 if (cur.current_font.fontInfo().number() == FONT_ON) {
851                         if (!isDigit(c) && !contains(number_operators, c) &&
852                             !(contains(number_seperators, c) &&
853                               cur.pos() != 0 &&
854                               cur.pos() != cur.lastpos() &&
855                               tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
856                               tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
857                            )
858                                 number(cur); // Set current_font.number to OFF
859                 } else if (isDigit(c) &&
860                            cur.real_current_font.isVisibleRightToLeft()) {
861                         number(cur); // Set current_font.number to ON
862
863                         if (cur.pos() != 0) {
864                                 char_type const c = par.getChar(cur.pos() - 1);
865                                 if (contains(number_unary_operators, c) &&
866                                     (cur.pos() == 1
867                                      || par.isSeparator(cur.pos() - 2)
868                                      || par.isNewline(cur.pos() - 2))
869                                   ) {
870                                         setCharFont(pit, cur.pos() - 1, cur.current_font,
871                                                 tm.font_);
872                                 } else if (contains(number_seperators, c)
873                                      && cur.pos() >= 2
874                                      && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
875                                         setCharFont(pit, cur.pos() - 1, cur.current_font,
876                                                 tm.font_);
877                                 }
878                         }
879                 }
880         }
881
882         // In Bidi text, we want spaces to be treated in a special way: spaces
883         // which are between words in different languages should get the 
884         // paragraph's language; otherwise, spaces should keep the language 
885         // they were originally typed in. This is only in effect while typing;
886         // after the text is already typed in, the user can always go back and
887         // explicitly set the language of a space as desired. But 99.9% of the
888         // time, what we're doing here is what the user actually meant.
889         // 
890         // The following cases are the ones in which the language of the space
891         // should be changed to match that of the containing paragraph. In the
892         // depictions, lowercase is LTR, uppercase is RTL, underscore (_) 
893         // represents a space, pipe (|) represents the cursor position (so the
894         // character before it is the one just typed in). The different cases
895         // are depicted logically (not visually), from left to right:
896         // 
897         // 1. A_a|
898         // 2. a_A|
899         //
900         // Theoretically, there are other situations that we should, perhaps, deal
901         // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any 
902         // point (to understand why, just try to create this situation...).
903
904         if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
905                 // get font in front and behind the space in question. But do NOT 
906                 // use getFont(cur.pos()) because the character c is not inserted yet
907                 Font const pre_space_font  = tm.displayFont(cur.pit(), cur.pos() - 2);
908                 Font const & post_space_font = cur.real_current_font;
909                 bool pre_space_rtl  = pre_space_font.isVisibleRightToLeft();
910                 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
911                 
912                 if (pre_space_rtl != post_space_rtl) {
913                         // Set the space's language to match the language of the 
914                         // adjacent character whose direction is the paragraph's
915                         // direction; don't touch other properties of the font
916                         Language const * lang = 
917                                 (pre_space_rtl == par.isRTL(buffer.params())) ?
918                                 pre_space_font.language() : post_space_font.language();
919
920                         Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
921                         space_font.setLanguage(lang);
922                         par.setFont(cur.pos() - 1, space_font);
923                 }
924         }
925         
926         // Next check, if there will be two blanks together or a blank at
927         // the beginning of a paragraph.
928         // I decided to handle blanks like normal characters, the main
929         // difference are the special checks when calculating the row.fill
930         // (blank does not count at the end of a row) and the check here
931
932         // When the free-spacing option is set for the current layout,
933         // disable the double-space checking
934         if (!freeSpacing && isLineSeparatorChar(c)) {
935                 if (cur.pos() == 0) {
936                         static bool sent_space_message = false;
937                         if (!sent_space_message) {
938                                 cur.message(_("You cannot insert a space at the "
939                                                            "beginning of a paragraph. Please read the Tutorial."));
940                                 sent_space_message = true;
941                         }
942                         return;
943                 }
944                 LASSERT(cur.pos() > 0, /**/);
945                 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
946                     && !par.isDeleted(cur.pos() - 1)) {
947                         static bool sent_space_message = false;
948                         if (!sent_space_message) {
949                                 cur.message(_("You cannot type two spaces this way. "
950                                                            "Please read the Tutorial."));
951                                 sent_space_message = true;
952                         }
953                         return;
954                 }
955         }
956
957         par.insertChar(cur.pos(), c, cur.current_font,
958                 cur.buffer()->params().trackChanges);
959         cur.checkBufferStructure();
960
961 //              cur.screenUpdateFlags(Update::Force);
962         bool boundary = cur.boundary()
963                 || tm.isRTLBoundary(cur.pit(), cur.pos() + 1);
964         setCursor(cur, cur.pit(), cur.pos() + 1, false, boundary);
965         charInserted(cur);
966 }
967
968
969 void Text::charInserted(Cursor & cur)
970 {
971         Paragraph & par = cur.paragraph();
972
973         // Here we call finishUndo for every 20 characters inserted.
974         // This is from my experience how emacs does it. (Lgb)
975         if (undo_counter_ < 20) {
976                 ++undo_counter_;
977         } else {
978                 cur.finishUndo();
979                 undo_counter_ = 0;
980         }
981
982         // register word if a non-letter was entered
983         if (cur.pos() > 1
984             && !par.isWordSeparator(cur.pos() - 2)
985             && par.isWordSeparator(cur.pos() - 1)) {
986                 // get the word in front of cursor
987                 LASSERT(this == cur.text(), /**/);
988                 cur.paragraph().updateWords();
989         }
990 }
991
992
993 // the cursor set functions have a special mechanism. When they
994 // realize, that you left an empty paragraph, they will delete it.
995
996 bool Text::cursorForwardOneWord(Cursor & cur)
997 {
998         LASSERT(this == cur.text(), /**/);
999
1000         pos_type const lastpos = cur.lastpos();
1001         pit_type pit = cur.pit();
1002         pos_type pos = cur.pos();
1003         Paragraph const & par = cur.paragraph();
1004
1005         // Paragraph boundary is a word boundary
1006         if (pos == lastpos) {
1007                 if (pit != cur.lastpit())
1008                         return setCursor(cur, pit + 1, 0);
1009                 else
1010                         return false;
1011         }
1012
1013         if (lyxrc.mac_like_word_movement) {
1014                 // Skip through trailing punctuation and spaces.
1015                 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
1016                         ++pos;
1017
1018                 // Skip over either a non-char inset or a full word
1019                 if (pos != lastpos && par.isWordSeparator(pos))
1020                         ++pos;
1021                 else while (pos != lastpos && !par.isWordSeparator(pos))
1022                              ++pos;
1023         } else {
1024                 LASSERT(pos < lastpos, /**/); // see above
1025                 if (!par.isWordSeparator(pos))
1026                         while (pos != lastpos && !par.isWordSeparator(pos))
1027                                 ++pos;
1028                 else if (par.isChar(pos))
1029                         while (pos != lastpos && par.isChar(pos))
1030                                 ++pos;
1031                 else if (!par.isSpace(pos)) // non-char inset
1032                         ++pos;
1033
1034                 // Skip over white space
1035                 while (pos != lastpos && par.isSpace(pos))
1036                              ++pos;             
1037         }
1038
1039         return setCursor(cur, pit, pos);
1040 }
1041
1042
1043 bool Text::cursorBackwardOneWord(Cursor & cur)
1044 {
1045         LASSERT(this == cur.text(), /**/);
1046
1047         pit_type pit = cur.pit();
1048         pos_type pos = cur.pos();
1049         Paragraph & par = cur.paragraph();
1050
1051         // Paragraph boundary is a word boundary
1052         if (pos == 0 && pit != 0)
1053                 return setCursor(cur, pit - 1, getPar(pit - 1).size());
1054
1055         if (lyxrc.mac_like_word_movement) {
1056                 // Skip through punctuation and spaces.
1057                 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1058                         --pos;
1059
1060                 // Skip over either a non-char inset or a full word
1061                 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1062                         --pos;
1063                 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1064                              --pos;
1065         } else {
1066                 // Skip over white space
1067                 while (pos != 0 && par.isSpace(pos - 1))
1068                              --pos;
1069
1070                 if (pos != 0 && !par.isWordSeparator(pos - 1))
1071                         while (pos != 0 && !par.isWordSeparator(pos - 1))
1072                                 --pos;
1073                 else if (pos != 0 && par.isChar(pos - 1))
1074                         while (pos != 0 && par.isChar(pos - 1))
1075                                 --pos;
1076                 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1077                         --pos;
1078         }
1079
1080         return setCursor(cur, pit, pos);
1081 }
1082
1083
1084 bool Text::cursorVisLeftOneWord(Cursor & cur)
1085 {
1086         LASSERT(this == cur.text(), /**/);
1087
1088         pos_type left_pos, right_pos;
1089         bool left_is_letter, right_is_letter;
1090
1091         Cursor temp_cur = cur;
1092
1093         // always try to move at least once...
1094         while (temp_cur.posVisLeft(true /* skip_inset */)) {
1095
1096                 // collect some information about current cursor position
1097                 temp_cur.getSurroundingPos(left_pos, right_pos);
1098                 left_is_letter = 
1099                         (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1100                 right_is_letter = 
1101                         (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1102
1103                 // if we're not at a letter/non-letter boundary, continue moving
1104                 if (left_is_letter == right_is_letter)
1105                         continue;
1106
1107                 // we should stop when we have an LTR word on our right or an RTL word
1108                 // on our left
1109                 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1110                                 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1111                         || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1112                                 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1113                         break;
1114         }
1115
1116         return setCursor(cur, temp_cur.pit(), temp_cur.pos(), 
1117                                          true, temp_cur.boundary());
1118 }
1119
1120
1121 bool Text::cursorVisRightOneWord(Cursor & cur)
1122 {
1123         LASSERT(this == cur.text(), /**/);
1124
1125         pos_type left_pos, right_pos;
1126         bool left_is_letter, right_is_letter;
1127
1128         Cursor temp_cur = cur;
1129
1130         // always try to move at least once...
1131         while (temp_cur.posVisRight(true /* skip_inset */)) {
1132
1133                 // collect some information about current cursor position
1134                 temp_cur.getSurroundingPos(left_pos, right_pos);
1135                 left_is_letter = 
1136                         (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1137                 right_is_letter = 
1138                         (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1139
1140                 // if we're not at a letter/non-letter boundary, continue moving
1141                 if (left_is_letter == right_is_letter)
1142                         continue;
1143
1144                 // we should stop when we have an LTR word on our right or an RTL word
1145                 // on our left
1146                 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1147                                 temp_cur.buffer()->params(), 
1148                                 left_pos).isRightToLeft())
1149                         || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1150                                 temp_cur.buffer()->params(), 
1151                                 right_pos).isRightToLeft()))
1152                         break;
1153         }
1154
1155         return setCursor(cur, temp_cur.pit(), temp_cur.pos(), 
1156                                          true, temp_cur.boundary());
1157 }
1158
1159
1160 void Text::selectWord(Cursor & cur, word_location loc)
1161 {
1162         LASSERT(this == cur.text(), /**/);
1163         CursorSlice from = cur.top();
1164         CursorSlice to = cur.top();
1165         getWord(from, to, loc);
1166         if (cur.top() != from)
1167                 setCursor(cur, from.pit(), from.pos());
1168         if (to == from)
1169                 return;
1170         if (!cur.selection())
1171                 cur.resetAnchor();
1172         setCursor(cur, to.pit(), to.pos());
1173         cur.setSelection();
1174         cur.setWordSelection(true);
1175 }
1176
1177
1178 void Text::selectAll(Cursor & cur)
1179 {
1180         LASSERT(this == cur.text(), /**/);
1181         if (cur.lastpos() == 0 && cur.lastpit() == 0)
1182                 return;
1183         // If the cursor is at the beginning, make sure the cursor ends there
1184         if (cur.pit() == 0 && cur.pos() == 0) {
1185                 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1186                 cur.resetAnchor();
1187                 setCursor(cur, 0, 0);           
1188         } else {
1189                 setCursor(cur, 0, 0);
1190                 cur.resetAnchor();
1191                 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1192         }
1193         cur.setSelection();
1194 }
1195
1196
1197 // Select the word currently under the cursor when no
1198 // selection is currently set
1199 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1200 {
1201         LASSERT(this == cur.text(), /**/);
1202         if (cur.selection())
1203                 return false;
1204         selectWord(cur, loc);
1205         return cur.selection();
1206 }
1207
1208
1209 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1210 {
1211         LASSERT(this == cur.text(), /**/);
1212
1213         if (!cur.selection()) {
1214                 bool const changed = cur.paragraph().isChanged(cur.pos());
1215                 if (!(changed && findNextChange(&cur.bv())))
1216                         return;
1217         }
1218
1219         cur.recordUndoSelection();
1220
1221         pit_type begPit = cur.selectionBegin().pit();
1222         pit_type endPit = cur.selectionEnd().pit();
1223
1224         pos_type begPos = cur.selectionBegin().pos();
1225         pos_type endPos = cur.selectionEnd().pos();
1226
1227         // keep selection info, because endPos becomes invalid after the first loop
1228         bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1229
1230         // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1231
1232         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1233                 pos_type parSize = pars_[pit].size();
1234
1235                 // ignore empty paragraphs; otherwise, an assertion will fail for
1236                 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1237                 if (parSize == 0)
1238                         continue;
1239
1240                 // do not consider first paragraph if the cursor starts at pos size()
1241                 if (pit == begPit && begPos == parSize)
1242                         continue;
1243
1244                 // do not consider last paragraph if the cursor ends at pos 0
1245                 if (pit == endPit && endPos == 0)
1246                         break; // last iteration anyway
1247
1248                 pos_type left  = (pit == begPit ? begPos : 0);
1249                 pos_type right = (pit == endPit ? endPos : parSize);
1250
1251                 if (op == ACCEPT) {
1252                         pars_[pit].acceptChanges(left, right);
1253                 } else {
1254                         pars_[pit].rejectChanges(left, right);
1255                 }
1256         }
1257
1258         // next, accept/reject imaginary end-of-par characters
1259
1260         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1261                 pos_type pos = pars_[pit].size();
1262
1263                 // skip if the selection ends before the end-of-par
1264                 if (pit == endPit && endsBeforeEndOfPar)
1265                         break; // last iteration anyway
1266
1267                 // skip if this is not the last paragraph of the document
1268                 // note: the user should be able to accept/reject the par break of the last par!
1269                 if (pit == endPit && pit + 1 != int(pars_.size()))
1270                         break; // last iteration anway
1271
1272                 if (op == ACCEPT) {
1273                         if (pars_[pit].isInserted(pos)) {
1274                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1275                         } else if (pars_[pit].isDeleted(pos)) {
1276                                 if (pit + 1 == int(pars_.size())) {
1277                                         // we cannot remove a par break at the end of the last paragraph;
1278                                         // instead, we mark it unchanged
1279                                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1280                                 } else {
1281                                         mergeParagraph(cur.buffer()->params(), pars_, pit);
1282                                         --endPit;
1283                                         --pit;
1284                                 }
1285                         }
1286                 } else {
1287                         if (pars_[pit].isDeleted(pos)) {
1288                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1289                         } else if (pars_[pit].isInserted(pos)) {
1290                                 if (pit + 1 == int(pars_.size())) {
1291                                         // we mark the par break at the end of the last paragraph unchanged
1292                                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1293                                 } else {
1294                                         mergeParagraph(cur.buffer()->params(), pars_, pit);
1295                                         --endPit;
1296                                         --pit;
1297                                 }
1298                         }
1299                 }
1300         }
1301
1302         // finally, invoke the DEPM
1303
1304         deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().trackChanges);
1305
1306         //
1307
1308         cur.finishUndo();
1309         cur.clearSelection();
1310         setCursorIntern(cur, begPit, begPos);
1311         cur.screenUpdateFlags(Update::Force);
1312         cur.forceBufferUpdate();
1313 }
1314
1315
1316 void Text::acceptChanges()
1317 {
1318         BufferParams const & bparams = owner_->buffer().params();
1319         lyx::acceptChanges(pars_, bparams);
1320         deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.trackChanges);
1321 }
1322
1323
1324 void Text::rejectChanges()
1325 {
1326         BufferParams const & bparams = owner_->buffer().params();
1327         pit_type pars_size = static_cast<pit_type>(pars_.size());
1328
1329         // first, reject changes within each individual paragraph
1330         // (do not consider end-of-par)
1331         for (pit_type pit = 0; pit < pars_size; ++pit) {
1332                 if (!pars_[pit].empty())   // prevent assertion failure
1333                         pars_[pit].rejectChanges(0, pars_[pit].size());
1334         }
1335
1336         // next, reject imaginary end-of-par characters
1337         for (pit_type pit = 0; pit < pars_size; ++pit) {
1338                 pos_type pos = pars_[pit].size();
1339
1340                 if (pars_[pit].isDeleted(pos)) {
1341                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1342                 } else if (pars_[pit].isInserted(pos)) {
1343                         if (pit == pars_size - 1) {
1344                                 // we mark the par break at the end of the last
1345                                 // paragraph unchanged
1346                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1347                         } else {
1348                                 mergeParagraph(bparams, pars_, pit);
1349                                 --pit;
1350                                 --pars_size;
1351                         }
1352                 }
1353         }
1354
1355         // finally, invoke the DEPM
1356         deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
1357 }
1358
1359
1360 void Text::deleteWordForward(Cursor & cur)
1361 {
1362         LASSERT(this == cur.text(), /**/);
1363         if (cur.lastpos() == 0)
1364                 cursorForward(cur);
1365         else {
1366                 cur.resetAnchor();
1367                 cur.setSelection(true);
1368                 cursorForwardOneWord(cur);
1369                 cur.setSelection();
1370                 cutSelection(cur, true, false);
1371                 cur.checkBufferStructure();
1372         }
1373 }
1374
1375
1376 void Text::deleteWordBackward(Cursor & cur)
1377 {
1378         LASSERT(this == cur.text(), /**/);
1379         if (cur.lastpos() == 0)
1380                 cursorBackward(cur);
1381         else {
1382                 cur.resetAnchor();
1383                 cur.setSelection(true);
1384                 cursorBackwardOneWord(cur);
1385                 cur.setSelection();
1386                 cutSelection(cur, true, false);
1387                 cur.checkBufferStructure();
1388         }
1389 }
1390
1391
1392 // Kill to end of line.
1393 void Text::changeCase(Cursor & cur, TextCase action)
1394 {
1395         LASSERT(this == cur.text(), /**/);
1396         CursorSlice from;
1397         CursorSlice to;
1398
1399         bool gotsel = false;
1400         if (cur.selection()) {
1401                 from = cur.selBegin();
1402                 to = cur.selEnd();
1403                 gotsel = true;
1404         } else {
1405                 from = cur.top();
1406                 getWord(from, to, PARTIAL_WORD);
1407                 cursorForwardOneWord(cur);
1408         }
1409
1410         cur.recordUndoSelection();
1411
1412         pit_type begPit = from.pit();
1413         pit_type endPit = to.pit();
1414
1415         pos_type begPos = from.pos();
1416         pos_type endPos = to.pos();
1417
1418         pos_type right = 0; // needed after the for loop
1419
1420         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1421                 Paragraph & par = pars_[pit];
1422                 pos_type const pos = (pit == begPit ? begPos : 0);
1423                 right = (pit == endPit ? endPos : par.size());
1424                 par.changeCase(cur.buffer()->params(), pos, right, action);
1425         }
1426
1427         // the selection may have changed due to logically-only deleted chars
1428         if (gotsel) {
1429                 setCursor(cur, begPit, begPos);
1430                 cur.resetAnchor();
1431                 setCursor(cur, endPit, right);
1432                 cur.setSelection();
1433         } else
1434                 setCursor(cur, endPit, right);
1435
1436         cur.checkBufferStructure();
1437 }
1438
1439
1440 bool Text::handleBibitems(Cursor & cur)
1441 {
1442         if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1443                 return false;
1444
1445         if (cur.pos() != 0)
1446                 return false;
1447
1448         BufferParams const & bufparams = cur.buffer()->params();
1449         Paragraph const & par = cur.paragraph();
1450         Cursor prevcur = cur;
1451         if (cur.pit() > 0) {
1452                 --prevcur.pit();
1453                 prevcur.pos() = prevcur.lastpos();
1454         }
1455         Paragraph const & prevpar = prevcur.paragraph();
1456
1457         // if a bibitem is deleted, merge with previous paragraph
1458         // if this is a bibliography item as well
1459         if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1460                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1461                 mergeParagraph(bufparams, cur.text()->paragraphs(),
1462                                                         prevcur.pit());
1463                 cur.forceBufferUpdate();
1464                 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1465                 cur.screenUpdateFlags(Update::Force);
1466                 return true;
1467         } 
1468
1469         // otherwise reset to default
1470         cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1471         return true;
1472 }
1473
1474
1475 bool Text::erase(Cursor & cur)
1476 {
1477         LASSERT(this == cur.text(), return false);
1478         bool needsUpdate = false;
1479         Paragraph & par = cur.paragraph();
1480
1481         if (cur.pos() != cur.lastpos()) {
1482                 // this is the code for a normal delete, not pasting
1483                 // any paragraphs
1484                 cur.recordUndo(DELETE_UNDO);
1485                 bool const was_inset = cur.paragraph().isInset(cur.pos());
1486                 if(!par.eraseChar(cur.pos(), cur.buffer()->params().trackChanges))
1487                         // the character has been logically deleted only => skip it
1488                         cur.top().forwardPos();
1489
1490                 if (was_inset)
1491                         cur.forceBufferUpdate();
1492                 else
1493                         cur.checkBufferStructure();
1494                 needsUpdate = true;
1495         } else {
1496                 if (cur.pit() == cur.lastpit())
1497                         return dissolveInset(cur);
1498
1499                 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1500                         par.setChange(cur.pos(), Change(Change::DELETED));
1501                         cur.forwardPos();
1502                         needsUpdate = true;
1503                 } else {
1504                         setCursorIntern(cur, cur.pit() + 1, 0);
1505                         needsUpdate = backspacePos0(cur);
1506                 }
1507         }
1508
1509         needsUpdate |= handleBibitems(cur);
1510
1511         if (needsUpdate) {
1512                 // Make sure the cursor is correct. Is this really needed?
1513                 // No, not really... at least not here!
1514                 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1515                 cur.checkBufferStructure();
1516         }
1517
1518         return needsUpdate;
1519 }
1520
1521
1522 bool Text::backspacePos0(Cursor & cur)
1523 {
1524         LASSERT(this == cur.text(), /**/);
1525         if (cur.pit() == 0)
1526                 return false;
1527
1528         bool needsUpdate = false;
1529
1530         BufferParams const & bufparams = cur.buffer()->params();
1531         DocumentClass const & tclass = bufparams.documentClass();
1532         ParagraphList & plist = cur.text()->paragraphs();
1533         Paragraph const & par = cur.paragraph();
1534         Cursor prevcur = cur;
1535         --prevcur.pit();
1536         prevcur.pos() = prevcur.lastpos();
1537         Paragraph const & prevpar = prevcur.paragraph();
1538
1539         // is it an empty paragraph?
1540         if (cur.lastpos() == 0
1541             || (cur.lastpos() == 1 && par.isSeparator(0))) {
1542                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1543                 plist.erase(boost::next(plist.begin(), cur.pit()));
1544                 needsUpdate = true;
1545         }
1546         // is previous par empty?
1547         else if (prevcur.lastpos() == 0
1548                  || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1549                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1550                 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1551                 needsUpdate = true;
1552         }
1553         // Pasting is not allowed, if the paragraphs have different
1554         // layouts. I think it is a real bug of all other
1555         // word processors to allow it. It confuses the user.
1556         // Correction: Pasting is always allowed with standard-layout
1557         // or the empty layout.
1558         else if (par.layout() == prevpar.layout()
1559                  || tclass.isDefaultLayout(par.layout())
1560                  || tclass.isPlainLayout(par.layout())) {
1561                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1562                 mergeParagraph(bufparams, plist, prevcur.pit());
1563                 needsUpdate = true;
1564         }
1565
1566         if (needsUpdate) {
1567                 cur.forceBufferUpdate();
1568                 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1569         }
1570
1571         return needsUpdate;
1572 }
1573
1574
1575 bool Text::backspace(Cursor & cur)
1576 {
1577         LASSERT(this == cur.text(), /**/);
1578         bool needsUpdate = false;
1579         if (cur.pos() == 0) {
1580                 if (cur.pit() == 0)
1581                         return dissolveInset(cur);
1582
1583                 Paragraph & prev_par = pars_[cur.pit() - 1];
1584
1585                 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1586                         prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1587                         setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1588                         return true;
1589                 }
1590                 // The cursor is at the beginning of a paragraph, so
1591                 // the backspace will collapse two paragraphs into one.
1592                 needsUpdate = backspacePos0(cur);
1593
1594         } else {
1595                 // this is the code for a normal backspace, not pasting
1596                 // any paragraphs
1597                 cur.recordUndo(DELETE_UNDO);
1598                 // We used to do cursorBackwardIntern() here, but it is
1599                 // not a good idea since it triggers the auto-delete
1600                 // mechanism. So we do a cursorBackwardIntern()-lite,
1601                 // without the dreaded mechanism. (JMarc)
1602                 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1603                                 false, cur.boundary());
1604                 bool const was_inset = cur.paragraph().isInset(cur.pos());
1605                 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
1606                 if (was_inset)
1607                         cur.forceBufferUpdate();
1608                 else
1609                         cur.checkBufferStructure();
1610         }
1611
1612         if (cur.pos() == cur.lastpos())
1613                 cur.setCurrentFont();
1614
1615         needsUpdate |= handleBibitems(cur);
1616
1617         // A singlePar update is not enough in this case.
1618 //              cur.screenUpdateFlags(Update::Force);
1619         setCursor(cur.top(), cur.pit(), cur.pos());
1620
1621         return needsUpdate;
1622 }
1623
1624
1625 bool Text::dissolveInset(Cursor & cur)
1626 {
1627         LASSERT(this == cur.text(), return false);
1628
1629         if (isMainText() || cur.inset().nargs() != 1)
1630                 return false;
1631
1632         cur.recordUndoInset();
1633         cur.setMark(false);
1634         cur.selHandle(false);
1635         // save position
1636         pos_type spos = cur.pos();
1637         pit_type spit = cur.pit();
1638         ParagraphList plist;
1639         if (cur.lastpit() != 0 || cur.lastpos() != 0)
1640                 plist = paragraphs();
1641         cur.popBackward();
1642         // store cursor offset
1643         if (spit == 0)
1644                 spos += cur.pos();
1645         spit += cur.pit();
1646         Buffer & b = *cur.buffer();
1647         cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1648         if (!plist.empty()) {
1649                 // ERT paragraphs have the Language latex_language.
1650                 // This is invalid outside of ERT, so we need to
1651                 // change it to the buffer language.
1652                 ParagraphList::iterator it = plist.begin();
1653                 ParagraphList::iterator it_end = plist.end();
1654                 for (; it != it_end; it++)
1655                         it->changeLanguage(b.params(), latex_language, b.language());
1656
1657                 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1658                                    b.errorList("Paste"));
1659                 // restore position
1660                 cur.pit() = min(cur.lastpit(), spit);
1661                 cur.pos() = min(cur.lastpos(), spos);
1662         } else
1663                 cur.forceBufferUpdate();
1664
1665         // Ensure the current language is set correctly (bug 6292)
1666         cur.text()->setCursor(cur, cur.pit(), cur.pos());
1667         cur.clearSelection();
1668         cur.resetAnchor();
1669         return true;
1670 }
1671
1672
1673 void Text::getWord(CursorSlice & from, CursorSlice & to,
1674         word_location const loc) const
1675 {
1676         to = from;
1677         pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1678 }
1679
1680
1681 void Text::write(ostream & os) const
1682 {
1683         Buffer const & buf = owner_->buffer();
1684         ParagraphList::const_iterator pit = paragraphs().begin();
1685         ParagraphList::const_iterator end = paragraphs().end();
1686         depth_type dth = 0;
1687         for (; pit != end; ++pit)
1688                 pit->write(os, buf.params(), dth);
1689
1690         // Close begin_deeper
1691         for(; dth > 0; --dth)
1692                 os << "\n\\end_deeper";
1693 }
1694
1695
1696 bool Text::read(Lexer & lex, 
1697                 ErrorList & errorList, InsetText * insetPtr)
1698 {
1699         Buffer const & buf = owner_->buffer();
1700         depth_type depth = 0;
1701         bool res = true;
1702
1703         while (lex.isOK()) {
1704                 lex.nextToken();
1705                 string const token = lex.getString();
1706
1707                 if (token.empty())
1708                         continue;
1709
1710                 if (token == "\\end_inset")
1711                         break;
1712
1713                 if (token == "\\end_body")
1714                         continue;
1715
1716                 if (token == "\\begin_body")
1717                         continue;
1718
1719                 if (token == "\\end_document") {
1720                         res = false;
1721                         break;
1722                 }
1723
1724                 if (token == "\\begin_layout") {
1725                         lex.pushToken(token);
1726
1727                         Paragraph par;
1728                         par.setInsetOwner(insetPtr);
1729                         par.params().depth(depth);
1730                         par.setFont(0, Font(inherit_font, buf.params().language));
1731                         pars_.push_back(par);
1732                         readParagraph(pars_.back(), lex, errorList);
1733
1734                         // register the words in the global word list
1735                         pars_.back().updateWords();
1736                 } else if (token == "\\begin_deeper") {
1737                         ++depth;
1738                 } else if (token == "\\end_deeper") {
1739                         if (!depth)
1740                                 lex.printError("\\end_deeper: " "depth is already null");
1741                         else
1742                                 --depth;
1743                 } else {
1744                         LYXERR0("Handling unknown body token: `" << token << '\'');
1745                 }
1746         }
1747
1748         // avoid a crash on weird documents (bug 4859)
1749         if (pars_.empty()) {
1750                 Paragraph par;
1751                 par.setInsetOwner(insetPtr);
1752                 par.params().depth(depth);
1753                 par.setFont(0, Font(inherit_font, 
1754                                     buf.params().language));
1755                 par.setPlainOrDefaultLayout(buf.params().documentClass());
1756                 pars_.push_back(par);
1757         }
1758         
1759         return res;
1760 }
1761
1762 // Returns the current font and depth as a message.
1763 docstring Text::currentState(Cursor const & cur) const
1764 {
1765         LASSERT(this == cur.text(), /**/);
1766         Buffer & buf = *cur.buffer();
1767         Paragraph const & par = cur.paragraph();
1768         odocstringstream os;
1769
1770         if (buf.params().trackChanges)
1771                 os << _("[Change Tracking] ");
1772
1773         Change change = par.lookupChange(cur.pos());
1774
1775         if (change.changed()) {
1776                 Author const & a = buf.params().authors().get(change.author);
1777                 os << _("Change: ") << a.name();
1778                 if (!a.email().empty())
1779                         os << " (" << a.email() << ")";
1780                 // FIXME ctime is english, we should translate that
1781                 os << _(" at ") << ctime(&change.changetime);
1782                 os << " : ";
1783         }
1784
1785         // I think we should only show changes from the default
1786         // font. (Asger)
1787         // No, from the document font (MV)
1788         Font font = cur.real_current_font;
1789         font.fontInfo().reduce(buf.params().getFont().fontInfo());
1790
1791         os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1792
1793         // The paragraph depth
1794         int depth = cur.paragraph().getDepth();
1795         if (depth > 0)
1796                 os << bformat(_(", Depth: %1$d"), depth);
1797
1798         // The paragraph spacing, but only if different from
1799         // buffer spacing.
1800         Spacing const & spacing = par.params().spacing();
1801         if (!spacing.isDefault()) {
1802                 os << _(", Spacing: ");
1803                 switch (spacing.getSpace()) {
1804                 case Spacing::Single:
1805                         os << _("Single");
1806                         break;
1807                 case Spacing::Onehalf:
1808                         os << _("OneHalf");
1809                         break;
1810                 case Spacing::Double:
1811                         os << _("Double");
1812                         break;
1813                 case Spacing::Other:
1814                         os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1815                         break;
1816                 case Spacing::Default:
1817                         // should never happen, do nothing
1818                         break;
1819                 }
1820         }
1821
1822 #ifdef DEVEL_VERSION
1823         os << _(", Inset: ") << &cur.inset();
1824         os << _(", Paragraph: ") << cur.pit();
1825         os << _(", Id: ") << par.id();
1826         os << _(", Position: ") << cur.pos();
1827         // FIXME: Why is the check for par.size() needed?
1828         // We are called with cur.pos() == par.size() quite often.
1829         if (!par.empty() && cur.pos() < par.size()) {
1830                 // Force output of code point, not character
1831                 size_t const c = par.getChar(cur.pos());
1832                 os << _(", Char: 0x") << hex << c;
1833         }
1834         os << _(", Boundary: ") << cur.boundary();
1835 //      Row & row = cur.textRow();
1836 //      os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1837 #endif
1838         return os.str();
1839 }
1840
1841
1842 docstring Text::getPossibleLabel(Cursor const & cur) const
1843 {
1844         pit_type pit = cur.pit();
1845
1846         Layout const * layout = &(pars_[pit].layout());
1847
1848         docstring text;
1849         docstring par_text = pars_[pit].asString();
1850
1851         // The return string of math matrices might contain linebreaks
1852         par_text = subst(par_text, '\n', '-');
1853         int const numwords = 3;
1854         for (int i = 0; i < numwords; ++i) {
1855                 if (par_text.empty())
1856                         break;
1857                 docstring head;
1858                 par_text = split(par_text, head, ' ');
1859                 // Is it legal to use spaces in labels ?
1860                 if (i > 0)
1861                         text += '-';
1862                 text += head;
1863         }
1864         
1865         // Make sure it isn't too long
1866         unsigned int const max_label_length = 32;
1867         if (text.size() > max_label_length)
1868                 text.resize(max_label_length);
1869
1870         // Will contain the label prefix.
1871         docstring name;
1872
1873         // For section, subsection, etc...
1874         if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1875                 Layout const * layout2 = &(pars_[pit - 1].layout());
1876                 if (layout2->latextype != LATEX_PARAGRAPH) {
1877                         --pit;
1878                         layout = layout2;
1879                 }
1880         }
1881         if (layout->latextype != LATEX_PARAGRAPH)
1882                 name = layout->refprefix;
1883
1884         // For captions, we just take the caption type
1885         Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
1886         if (caption_inset) {
1887                 string const & ftype = static_cast<InsetCaption *>(caption_inset)->type();
1888                 FloatList const & fl = cur.buffer()->params().documentClass().floats();
1889                 if (fl.typeExist(ftype)) {
1890                         Floating const & flt = fl.getType(ftype);
1891                         name = from_utf8(flt.refPrefix());
1892                 }
1893                 if (name.empty())
1894                         name = from_utf8(ftype.substr(0,3));
1895         }
1896
1897         // If none of the above worked, see if the inset knows.
1898         if (name.empty()) {
1899                 InsetLayout const & il = cur.inset().getLayout();
1900                 name = il.refprefix();
1901         }
1902
1903         if (!name.empty())
1904                 // FIXME refstyle
1905                 // We should allow customization of the separator or else change it
1906                 text = name + ':' + text;
1907
1908         return text;
1909 }
1910
1911
1912 docstring Text::asString(int options) const
1913 {
1914         return asString(0, pars_.size(), options);
1915 }
1916
1917
1918 docstring Text::asString(pit_type beg, pit_type end, int options) const
1919 {
1920         size_t i = size_t(beg);
1921         docstring str = pars_[i].asString(options);
1922         for (++i; i != size_t(end); ++i) {
1923                 str += '\n';
1924                 str += pars_[i].asString(options);
1925         }
1926         return str;
1927 }
1928
1929
1930
1931 void Text::charsTranspose(Cursor & cur)
1932 {
1933         LASSERT(this == cur.text(), /**/);
1934
1935         pos_type pos = cur.pos();
1936
1937         // If cursor is at beginning or end of paragraph, do nothing.
1938         if (pos == cur.lastpos() || pos == 0)
1939                 return;
1940
1941         Paragraph & par = cur.paragraph();
1942
1943         // Get the positions of the characters to be transposed.
1944         pos_type pos1 = pos - 1;
1945         pos_type pos2 = pos;
1946
1947         // In change tracking mode, ignore deleted characters.
1948         while (pos2 < cur.lastpos() && par.isDeleted(pos2))
1949                 ++pos2;
1950         if (pos2 == cur.lastpos())
1951                 return;
1952
1953         while (pos1 >= 0 && par.isDeleted(pos1))
1954                 --pos1;
1955         if (pos1 < 0)
1956                 return;
1957
1958         // Don't do anything if one of the "characters" is not regular text.
1959         if (par.isInset(pos1) || par.isInset(pos2))
1960                 return;
1961
1962         // Store the characters to be transposed (including font information).
1963         char_type const char1 = par.getChar(pos1);
1964         Font const font1 =
1965                 par.getFontSettings(cur.buffer()->params(), pos1);
1966
1967         char_type const char2 = par.getChar(pos2);
1968         Font const font2 =
1969                 par.getFontSettings(cur.buffer()->params(), pos2);
1970
1971         // And finally, we are ready to perform the transposition.
1972         // Track the changes if Change Tracking is enabled.
1973         bool const trackChanges = cur.buffer()->params().trackChanges;
1974
1975         cur.recordUndo();
1976
1977         par.eraseChar(pos2, trackChanges);
1978         par.eraseChar(pos1, trackChanges);
1979         par.insertChar(pos1, char2, font2, trackChanges);
1980         par.insertChar(pos2, char1, font1, trackChanges);
1981
1982         cur.checkBufferStructure();
1983
1984         // After the transposition, move cursor to after the transposition.
1985         setCursor(cur, cur.pit(), pos2);
1986         cur.forwardPos();
1987 }
1988
1989
1990 DocIterator Text::macrocontextPosition() const
1991 {
1992         return macrocontext_position_;
1993 }
1994
1995
1996 void Text::setMacrocontextPosition(DocIterator const & pos)
1997 {
1998         macrocontext_position_ = pos;
1999 }
2000
2001
2002 docstring Text::previousWord(CursorSlice const & sl) const
2003 {
2004         CursorSlice from = sl;
2005         CursorSlice to = sl;
2006         getWord(from, to, PREVIOUS_WORD);
2007         if (sl == from || to == from)
2008                 return docstring();
2009         
2010         Paragraph const & par = sl.paragraph();
2011         return par.asString(from.pos(), to.pos());
2012 }
2013
2014
2015 bool Text::completionSupported(Cursor const & cur) const
2016 {
2017         Paragraph const & par = cur.paragraph();
2018         return cur.pos() > 0
2019                 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2020                 && !par.isWordSeparator(cur.pos() - 1);
2021 }
2022
2023
2024 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2025 {
2026         WordList const * list = theWordList(*cur.getFont().language());
2027         return new TextCompletionList(cur, list);
2028 }
2029
2030
2031 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2032 {       
2033         LASSERT(cur.bv().cursor() == cur, /**/);
2034         cur.insert(s);
2035         cur.bv().cursor() = cur;
2036         if (!(cur.result().update() & Update::Force))
2037                 cur.screenUpdateFlags(cur.result().update() | Update::SinglePar);
2038         return true;
2039 }
2040         
2041         
2042 docstring Text::completionPrefix(Cursor const & cur) const
2043 {
2044         return previousWord(cur.top());
2045 }
2046
2047 } // namespace lyx