]> git.lyx.org Git - lyx.git/blob - src/Text.cpp
* add PreBabelPreamble to Language definition (fixes #4786).
[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                 int aid;
460                 time_t ct;
461                 is >> aid >> ct;
462                 BufferParams::AuthorMap 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         // mark paragraph for spell checking on load
524         // par.requestSpellCheck();
525 }
526
527
528 class TextCompletionList : public CompletionList
529 {
530 public:
531         ///
532         TextCompletionList(Cursor const & cur, WordList const * list)
533                 : buffer_(cur.buffer()), pos_(0), list_(list)
534         {}
535         ///
536         virtual ~TextCompletionList() {}
537         
538         ///
539         virtual bool sorted() const { return true; }
540         ///
541         virtual size_t size() const
542         {
543                 return list_->size();
544         }
545         ///
546         virtual docstring const & data(size_t idx) const
547         {
548                 return list_->word(idx);
549         }
550         
551 private:
552         ///
553         Buffer const * buffer_;
554         ///
555         size_t pos_;
556         ///
557         WordList const * list_;
558 };
559
560
561 bool Text::empty() const
562 {
563         return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
564                 // FIXME: Should we consider the labeled type as empty too? 
565                 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
566 }
567
568
569 double Text::spacing(Paragraph const & par) const
570 {
571         if (par.params().spacing().isDefault())
572                 return owner_->buffer().params().spacing().getValue();
573         return par.params().spacing().getValue();
574 }
575
576
577 /**
578  * This breaks a paragraph at the specified position.
579  * The new paragraph will:
580  * - Decrease depth by one (or change layout to default layout) when
581  *    keep_layout == false  
582  * - keep current depth and layout when keep_layout == true
583  */
584 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos, 
585                     bool keep_layout)
586 {
587         BufferParams const & bparams = text.inset().buffer().params();
588         ParagraphList & pars = text.paragraphs();
589         // create a new paragraph, and insert into the list
590         ParagraphList::iterator tmp =
591                 pars.insert(boost::next(pars.begin(), par_offset + 1),
592                             Paragraph());
593
594         Paragraph & par = pars[par_offset];
595
596         // remember to set the inset_owner
597         tmp->setInsetOwner(&par.inInset());
598         // without doing that we get a crash when typing <Return> at the
599         // end of a paragraph
600         tmp->setPlainOrDefaultLayout(bparams.documentClass());
601
602         if (keep_layout) {
603                 tmp->setLayout(par.layout());
604                 tmp->setLabelWidthString(par.params().labelWidthString());
605                 tmp->params().depth(par.params().depth());
606         } else if (par.params().depth() > 0) {
607                 Paragraph const & hook = pars[text.outerHook(par_offset)];
608                 tmp->setLayout(hook.layout());
609                 // not sure the line below is useful
610                 tmp->setLabelWidthString(par.params().labelWidthString());
611                 tmp->params().depth(hook.params().depth());
612         }
613
614         bool const isempty = (par.allowEmpty() && par.empty());
615
616         if (!isempty && (par.size() > pos || par.empty())) {
617                 tmp->setLayout(par.layout());
618                 tmp->params().align(par.params().align());
619                 tmp->setLabelWidthString(par.params().labelWidthString());
620
621                 tmp->params().depth(par.params().depth());
622                 tmp->params().noindent(par.params().noindent());
623
624                 // move everything behind the break position
625                 // to the new paragraph
626
627                 /* Note: if !keepempty, empty() == true, then we reach
628                  * here with size() == 0. So pos_end becomes - 1. This
629                  * doesn't cause problems because both loops below
630                  * enforce pos <= pos_end and 0 <= pos
631                  */
632                 pos_type pos_end = par.size() - 1;
633
634                 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
635                         if (moveItem(par, pos, *tmp, j, bparams)) {
636                                 ++j;
637                         }
638                 }
639         }
640
641         // Move over the end-of-par change information
642         tmp->setChange(tmp->size(), par.lookupChange(par.size()));
643         par.setChange(par.size(), Change(bparams.trackChanges ?
644                                            Change::INSERTED : Change::UNCHANGED));
645
646         if (pos) {
647                 // Make sure that we keep the language when
648                 // breaking paragraph.
649                 if (tmp->empty()) {
650                         Font changed = tmp->getFirstFontSettings(bparams);
651                         Font const & old = par.getFontSettings(bparams, par.size());
652                         changed.setLanguage(old.language());
653                         tmp->setFont(0, changed);
654                 }
655
656                 return;
657         }
658
659         if (!isempty) {
660                 bool const soa = par.params().startOfAppendix();
661                 par.params().clear();
662                 // do not lose start of appendix marker (bug 4212)
663                 par.params().startOfAppendix(soa);
664                 par.setPlainOrDefaultLayout(bparams.documentClass());
665         }
666
667         if (keep_layout) {
668                 par.setLayout(tmp->layout());
669                 par.setLabelWidthString(tmp->params().labelWidthString());
670                 par.params().depth(tmp->params().depth());
671         }
672 }
673
674
675 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
676 {
677         LASSERT(this == cur.text(), /**/);
678
679         Paragraph & cpar = cur.paragraph();
680         pit_type cpit = cur.pit();
681
682         DocumentClass const & tclass = cur.buffer()->params().documentClass();
683         Layout const & layout = cpar.layout();
684
685         if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
686                 if (changeDepthAllowed(cur, DEC_DEPTH))
687                         changeDepth(cur, DEC_DEPTH);
688                 else 
689                         setLayout(cur, tclass.defaultLayoutName());
690                 return;
691         }
692
693         // a layout change may affect also the following paragraph
694         recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
695
696         // Always break behind a space
697         // It is better to erase the space (Dekel)
698         if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
699                 cpar.eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
700
701         // What should the layout for the new paragraph be?
702         bool keep_layout = layout.isEnvironment() 
703                 || (layout.isParagraph() && layout.parbreak_is_newline);
704         if (inverse_logic)
705                 keep_layout = !keep_layout;
706
707         // We need to remember this before we break the paragraph, because
708         // that invalidates the layout variable
709         bool sensitive = layout.labeltype == LABEL_SENSITIVE;
710
711         // we need to set this before we insert the paragraph.
712         bool const isempty = cpar.allowEmpty() && cpar.empty();
713
714         lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
715
716         // After this, neither paragraph contains any rows!
717
718         cpit = cur.pit();
719         pit_type next_par = cpit + 1;
720
721         // well this is the caption hack since one caption is really enough
722         if (sensitive) {
723                 if (cur.pos() == 0)
724                         // set to standard-layout
725                 //FIXME Check if this should be plainLayout() in some cases
726                         pars_[cpit].applyLayout(tclass.defaultLayout());
727                 else
728                         // set to standard-layout
729                         //FIXME Check if this should be plainLayout() in some cases
730                         pars_[next_par].applyLayout(tclass.defaultLayout());
731         }
732
733         while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
734                 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().trackChanges))
735                         break; // the character couldn't be deleted physically due to change tracking
736         }
737
738         // A singlePar update is not enough in this case.
739         cur.screenUpdateFlags(Update::Force);
740         cur.forceBufferUpdate();
741
742         // This check is necessary. Otherwise the new empty paragraph will
743         // be deleted automatically. And it is more friendly for the user!
744         if (cur.pos() != 0 || isempty)
745                 setCursor(cur, cur.pit() + 1, 0);
746         else
747                 setCursor(cur, cur.pit(), 0);
748 }
749
750
751 // needed to insert the selection
752 void Text::insertStringAsLines(DocIterator const & dit, docstring const & str,
753                 Font const & font)
754 {
755         BufferParams const & bparams = owner_->buffer().params();
756         pit_type pit = dit.pit();
757         pos_type pos = dit.pos();
758
759         // insert the string, don't insert doublespace
760         bool space_inserted = true;
761         for (docstring::const_iterator cit = str.begin();
762             cit != str.end(); ++cit) {
763                 Paragraph & par = pars_[pit];
764                 if (*cit == '\n') {
765                         if (autoBreakRows_ && (!par.empty() || par.allowEmpty())) {
766                                 lyx::breakParagraph(*this, pit, pos,
767                                         par.layout().isEnvironment());
768                                 ++pit;
769                                 pos = 0;
770                                 space_inserted = true;
771                         } else {
772                                 continue;
773                         }
774                         // do not insert consecutive spaces if !free_spacing
775                 } else if ((*cit == ' ' || *cit == '\t') &&
776                            space_inserted && !par.isFreeSpacing()) {
777                         continue;
778                 } else if (*cit == '\t') {
779                         if (!par.isFreeSpacing()) {
780                                 // tabs are like spaces here
781                                 par.insertChar(pos, ' ', font, bparams.trackChanges);
782                                 ++pos;
783                                 space_inserted = true;
784                         } else {
785                                 par.insertChar(pos, *cit, font, bparams.trackChanges);
786                                 ++pos;
787                                 space_inserted = true;
788                         }
789                 } else if (!isPrintable(*cit)) {
790                         // Ignore unprintables
791                         continue;
792                 } else {
793                         // just insert the character
794                         par.insertChar(pos, *cit, font, bparams.trackChanges);
795                         ++pos;
796                         space_inserted = (*cit == ' ');
797                 }
798         }
799 }
800
801
802 // turn double CR to single CR, others are converted into one
803 // blank. Then insertStringAsLines is called
804 void Text::insertStringAsParagraphs(DocIterator const & dit, docstring const & str,
805                 Font const & font)
806 {
807         docstring linestr = str;
808         bool newline_inserted = false;
809
810         for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
811                 if (linestr[i] == '\n') {
812                         if (newline_inserted) {
813                                 // we know that \r will be ignored by
814                                 // insertStringAsLines. Of course, it is a dirty
815                                 // trick, but it works...
816                                 linestr[i - 1] = '\r';
817                                 linestr[i] = '\n';
818                         } else {
819                                 linestr[i] = ' ';
820                                 newline_inserted = true;
821                         }
822                 } else if (isPrintable(linestr[i])) {
823                         newline_inserted = false;
824                 }
825         }
826         insertStringAsLines(dit, linestr, font);
827 }
828
829
830 // insert a character, moves all the following breaks in the
831 // same Paragraph one to the right and make a rebreak
832 void Text::insertChar(Cursor & cur, char_type c)
833 {
834         LASSERT(this == cur.text(), /**/);
835
836         cur.recordUndo(INSERT_UNDO);
837
838         TextMetrics const & tm = cur.bv().textMetrics(this);
839         Buffer const & buffer = *cur.buffer();
840         Paragraph & par = cur.paragraph();
841         // try to remove this
842         pit_type const pit = cur.pit();
843
844         bool const freeSpacing = par.layout().free_spacing ||
845                 par.isFreeSpacing();
846
847         if (lyxrc.auto_number) {
848                 static docstring const number_operators = from_ascii("+-/*");
849                 static docstring const number_unary_operators = from_ascii("+-");
850                 static docstring const number_seperators = from_ascii(".,:");
851
852                 if (cur.current_font.fontInfo().number() == FONT_ON) {
853                         if (!isDigit(c) && !contains(number_operators, c) &&
854                             !(contains(number_seperators, c) &&
855                               cur.pos() != 0 &&
856                               cur.pos() != cur.lastpos() &&
857                               tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
858                               tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
859                            )
860                                 number(cur); // Set current_font.number to OFF
861                 } else if (isDigit(c) &&
862                            cur.real_current_font.isVisibleRightToLeft()) {
863                         number(cur); // Set current_font.number to ON
864
865                         if (cur.pos() != 0) {
866                                 char_type const c = par.getChar(cur.pos() - 1);
867                                 if (contains(number_unary_operators, c) &&
868                                     (cur.pos() == 1
869                                      || par.isSeparator(cur.pos() - 2)
870                                      || par.isNewline(cur.pos() - 2))
871                                   ) {
872                                         setCharFont(pit, cur.pos() - 1, cur.current_font,
873                                                 tm.font_);
874                                 } else if (contains(number_seperators, c)
875                                      && cur.pos() >= 2
876                                      && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
877                                         setCharFont(pit, cur.pos() - 1, cur.current_font,
878                                                 tm.font_);
879                                 }
880                         }
881                 }
882         }
883
884         // In Bidi text, we want spaces to be treated in a special way: spaces
885         // which are between words in different languages should get the 
886         // paragraph's language; otherwise, spaces should keep the language 
887         // they were originally typed in. This is only in effect while typing;
888         // after the text is already typed in, the user can always go back and
889         // explicitly set the language of a space as desired. But 99.9% of the
890         // time, what we're doing here is what the user actually meant.
891         // 
892         // The following cases are the ones in which the language of the space
893         // should be changed to match that of the containing paragraph. In the
894         // depictions, lowercase is LTR, uppercase is RTL, underscore (_) 
895         // represents a space, pipe (|) represents the cursor position (so the
896         // character before it is the one just typed in). The different cases
897         // are depicted logically (not visually), from left to right:
898         // 
899         // 1. A_a|
900         // 2. a_A|
901         //
902         // Theoretically, there are other situations that we should, perhaps, deal
903         // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any 
904         // point (to understand why, just try to create this situation...).
905
906         if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
907                 // get font in front and behind the space in question. But do NOT 
908                 // use getFont(cur.pos()) because the character c is not inserted yet
909                 Font const pre_space_font  = tm.displayFont(cur.pit(), cur.pos() - 2);
910                 Font const & post_space_font = cur.real_current_font;
911                 bool pre_space_rtl  = pre_space_font.isVisibleRightToLeft();
912                 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
913                 
914                 if (pre_space_rtl != post_space_rtl) {
915                         // Set the space's language to match the language of the 
916                         // adjacent character whose direction is the paragraph's
917                         // direction; don't touch other properties of the font
918                         Language const * lang = 
919                                 (pre_space_rtl == par.isRTL(buffer.params())) ?
920                                 pre_space_font.language() : post_space_font.language();
921
922                         Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
923                         space_font.setLanguage(lang);
924                         par.setFont(cur.pos() - 1, space_font);
925                 }
926         }
927         
928         // Next check, if there will be two blanks together or a blank at
929         // the beginning of a paragraph.
930         // I decided to handle blanks like normal characters, the main
931         // difference are the special checks when calculating the row.fill
932         // (blank does not count at the end of a row) and the check here
933
934         // When the free-spacing option is set for the current layout,
935         // disable the double-space checking
936         if (!freeSpacing && isLineSeparatorChar(c)) {
937                 if (cur.pos() == 0) {
938                         cur.message(_(
939                                         "You cannot insert a space at the "
940                                         "beginning of a paragraph. Please read the Tutorial."));
941                         return;
942                 }
943                 LASSERT(cur.pos() > 0, /**/);
944                 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
945                                 && !par.isDeleted(cur.pos() - 1)) {
946                         cur.message(_(
947                                         "You cannot type two spaces this way. "
948                                         "Please read the Tutorial."));
949                         return;
950                 }
951         }
952
953         par.insertChar(cur.pos(), c, cur.current_font,
954                 cur.buffer()->params().trackChanges);
955         cur.checkBufferStructure();
956
957 //              cur.screenUpdateFlags(Update::Force);
958         bool boundary = cur.boundary()
959                 || tm.isRTLBoundary(cur.pit(), cur.pos() + 1);
960         setCursor(cur, cur.pit(), cur.pos() + 1, false, boundary);
961         charInserted(cur);
962 }
963
964
965 void Text::charInserted(Cursor & cur)
966 {
967         Paragraph & par = cur.paragraph();
968
969         // Here we call finishUndo for every 20 characters inserted.
970         // This is from my experience how emacs does it. (Lgb)
971         if (undo_counter_ < 20) {
972                 ++undo_counter_;
973         } else {
974                 cur.finishUndo();
975                 undo_counter_ = 0;
976         }
977
978         // register word if a non-letter was entered
979         if (cur.pos() > 1
980             && !par.isWordSeparator(cur.pos() - 2)
981             && par.isWordSeparator(cur.pos() - 1)) {
982                 // get the word in front of cursor
983                 LASSERT(this == cur.text(), /**/);
984                 cur.paragraph().updateWords();
985         }
986 }
987
988
989 // the cursor set functions have a special mechanism. When they
990 // realize, that you left an empty paragraph, they will delete it.
991
992 bool Text::cursorForwardOneWord(Cursor & cur)
993 {
994         LASSERT(this == cur.text(), /**/);
995
996         pos_type const lastpos = cur.lastpos();
997         pit_type pit = cur.pit();
998         pos_type pos = cur.pos();
999         Paragraph const & par = cur.paragraph();
1000
1001         // Paragraph boundary is a word boundary
1002         if (pos == lastpos) {
1003                 if (pit != cur.lastpit())
1004                         return setCursor(cur, pit + 1, 0);
1005                 else
1006                         return false;
1007         }
1008
1009         if (lyxrc.mac_like_word_movement) {
1010                 // Skip through trailing punctuation and spaces.
1011                 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
1012                         ++pos;
1013
1014                 // Skip over either a non-char inset or a full word
1015                 if (pos != lastpos && par.isWordSeparator(pos))
1016                         ++pos;
1017                 else while (pos != lastpos && !par.isWordSeparator(pos))
1018                              ++pos;
1019         } else {
1020                 LASSERT(pos < lastpos, /**/); // see above
1021                 if (!par.isWordSeparator(pos))
1022                         while (pos != lastpos && !par.isWordSeparator(pos))
1023                                 ++pos;
1024                 else if (par.isChar(pos))
1025                         while (pos != lastpos && par.isChar(pos))
1026                                 ++pos;
1027                 else if (!par.isSpace(pos)) // non-char inset
1028                         ++pos;
1029
1030                 // Skip over white space
1031                 while (pos != lastpos && par.isSpace(pos))
1032                              ++pos;             
1033         }
1034
1035         return setCursor(cur, pit, pos);
1036 }
1037
1038
1039 bool Text::cursorBackwardOneWord(Cursor & cur)
1040 {
1041         LASSERT(this == cur.text(), /**/);
1042
1043         pit_type pit = cur.pit();
1044         pos_type pos = cur.pos();
1045         Paragraph & par = cur.paragraph();
1046
1047         // Paragraph boundary is a word boundary
1048         if (pos == 0 && pit != 0)
1049                 return setCursor(cur, pit - 1, getPar(pit - 1).size());
1050
1051         if (lyxrc.mac_like_word_movement) {
1052                 // Skip through punctuation and spaces.
1053                 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1054                         --pos;
1055
1056                 // Skip over either a non-char inset or a full word
1057                 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1058                         --pos;
1059                 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1060                              --pos;
1061         } else {
1062                 // Skip over white space
1063                 while (pos != 0 && par.isSpace(pos - 1))
1064                              --pos;
1065
1066                 if (pos != 0 && !par.isWordSeparator(pos - 1))
1067                         while (pos != 0 && !par.isWordSeparator(pos - 1))
1068                                 --pos;
1069                 else if (pos != 0 && par.isChar(pos - 1))
1070                         while (pos != 0 && par.isChar(pos - 1))
1071                                 --pos;
1072                 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1073                         --pos;
1074         }
1075
1076         return setCursor(cur, pit, pos);
1077 }
1078
1079
1080 bool Text::cursorVisLeftOneWord(Cursor & cur)
1081 {
1082         LASSERT(this == cur.text(), /**/);
1083
1084         pos_type left_pos, right_pos;
1085         bool left_is_letter, right_is_letter;
1086
1087         Cursor temp_cur = cur;
1088
1089         // always try to move at least once...
1090         while (temp_cur.posVisLeft(true /* skip_inset */)) {
1091
1092                 // collect some information about current cursor position
1093                 temp_cur.getSurroundingPos(left_pos, right_pos);
1094                 left_is_letter = 
1095                         (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1096                 right_is_letter = 
1097                         (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1098
1099                 // if we're not at a letter/non-letter boundary, continue moving
1100                 if (left_is_letter == right_is_letter)
1101                         continue;
1102
1103                 // we should stop when we have an LTR word on our right or an RTL word
1104                 // on our left
1105                 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1106                                 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1107                         || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1108                                 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1109                         break;
1110         }
1111
1112         return setCursor(cur, temp_cur.pit(), temp_cur.pos(), 
1113                                          true, temp_cur.boundary());
1114 }
1115
1116
1117 bool Text::cursorVisRightOneWord(Cursor & cur)
1118 {
1119         LASSERT(this == cur.text(), /**/);
1120
1121         pos_type left_pos, right_pos;
1122         bool left_is_letter, right_is_letter;
1123
1124         Cursor temp_cur = cur;
1125
1126         // always try to move at least once...
1127         while (temp_cur.posVisRight(true /* skip_inset */)) {
1128
1129                 // collect some information about current cursor position
1130                 temp_cur.getSurroundingPos(left_pos, right_pos);
1131                 left_is_letter = 
1132                         (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1133                 right_is_letter = 
1134                         (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1135
1136                 // if we're not at a letter/non-letter boundary, continue moving
1137                 if (left_is_letter == right_is_letter)
1138                         continue;
1139
1140                 // we should stop when we have an LTR word on our right or an RTL word
1141                 // on our left
1142                 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1143                                 temp_cur.buffer()->params(), 
1144                                 left_pos).isRightToLeft())
1145                         || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1146                                 temp_cur.buffer()->params(), 
1147                                 right_pos).isRightToLeft()))
1148                         break;
1149         }
1150
1151         return setCursor(cur, temp_cur.pit(), temp_cur.pos(), 
1152                                          true, temp_cur.boundary());
1153 }
1154
1155
1156 void Text::selectWord(Cursor & cur, word_location loc)
1157 {
1158         LASSERT(this == cur.text(), /**/);
1159         CursorSlice from = cur.top();
1160         CursorSlice to = cur.top();
1161         getWord(from, to, loc);
1162         if (cur.top() != from)
1163                 setCursor(cur, from.pit(), from.pos());
1164         if (to == from)
1165                 return;
1166         if (!cur.selection())
1167                 cur.resetAnchor();
1168         setCursor(cur, to.pit(), to.pos());
1169         cur.setSelection();
1170         cur.setWordSelection(true);
1171 }
1172
1173
1174 void Text::selectAll(Cursor & cur)
1175 {
1176         LASSERT(this == cur.text(), /**/);
1177         if (cur.lastpos() == 0 && cur.lastpit() == 0)
1178                 return;
1179         // If the cursor is at the beginning, make sure the cursor ends there
1180         if (cur.pit() == 0 && cur.pos() == 0) {
1181                 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1182                 cur.resetAnchor();
1183                 setCursor(cur, 0, 0);           
1184         } else {
1185                 setCursor(cur, 0, 0);
1186                 cur.resetAnchor();
1187                 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1188         }
1189         cur.setSelection();
1190 }
1191
1192
1193 // Select the word currently under the cursor when no
1194 // selection is currently set
1195 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1196 {
1197         LASSERT(this == cur.text(), /**/);
1198         if (cur.selection())
1199                 return false;
1200         selectWord(cur, loc);
1201         return cur.selection();
1202 }
1203
1204
1205 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1206 {
1207         LASSERT(this == cur.text(), /**/);
1208
1209         if (!cur.selection()) {
1210                 bool const changed = cur.paragraph().isChanged(cur.pos());
1211                 if (!(changed && findNextChange(&cur.bv())))
1212                         return;
1213         }
1214
1215         cur.recordUndoSelection();
1216
1217         pit_type begPit = cur.selectionBegin().pit();
1218         pit_type endPit = cur.selectionEnd().pit();
1219
1220         pos_type begPos = cur.selectionBegin().pos();
1221         pos_type endPos = cur.selectionEnd().pos();
1222
1223         // keep selection info, because endPos becomes invalid after the first loop
1224         bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1225
1226         // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1227
1228         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1229                 pos_type parSize = pars_[pit].size();
1230
1231                 // ignore empty paragraphs; otherwise, an assertion will fail for
1232                 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1233                 if (parSize == 0)
1234                         continue;
1235
1236                 // do not consider first paragraph if the cursor starts at pos size()
1237                 if (pit == begPit && begPos == parSize)
1238                         continue;
1239
1240                 // do not consider last paragraph if the cursor ends at pos 0
1241                 if (pit == endPit && endPos == 0)
1242                         break; // last iteration anyway
1243
1244                 pos_type left  = (pit == begPit ? begPos : 0);
1245                 pos_type right = (pit == endPit ? endPos : parSize);
1246
1247                 if (op == ACCEPT) {
1248                         pars_[pit].acceptChanges(left, right);
1249                 } else {
1250                         pars_[pit].rejectChanges(left, right);
1251                 }
1252         }
1253
1254         // next, accept/reject imaginary end-of-par characters
1255
1256         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1257                 pos_type pos = pars_[pit].size();
1258
1259                 // skip if the selection ends before the end-of-par
1260                 if (pit == endPit && endsBeforeEndOfPar)
1261                         break; // last iteration anyway
1262
1263                 // skip if this is not the last paragraph of the document
1264                 // note: the user should be able to accept/reject the par break of the last par!
1265                 if (pit == endPit && pit + 1 != int(pars_.size()))
1266                         break; // last iteration anway
1267
1268                 if (op == ACCEPT) {
1269                         if (pars_[pit].isInserted(pos)) {
1270                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1271                         } else if (pars_[pit].isDeleted(pos)) {
1272                                 if (pit + 1 == int(pars_.size())) {
1273                                         // we cannot remove a par break at the end of the last paragraph;
1274                                         // instead, we mark it unchanged
1275                                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1276                                 } else {
1277                                         mergeParagraph(cur.buffer()->params(), pars_, pit);
1278                                         --endPit;
1279                                         --pit;
1280                                 }
1281                         }
1282                 } else {
1283                         if (pars_[pit].isDeleted(pos)) {
1284                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1285                         } else if (pars_[pit].isInserted(pos)) {
1286                                 if (pit + 1 == int(pars_.size())) {
1287                                         // we mark the par break at the end of the last paragraph unchanged
1288                                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1289                                 } else {
1290                                         mergeParagraph(cur.buffer()->params(), pars_, pit);
1291                                         --endPit;
1292                                         --pit;
1293                                 }
1294                         }
1295                 }
1296         }
1297
1298         // finally, invoke the DEPM
1299
1300         deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().trackChanges);
1301
1302         //
1303
1304         cur.finishUndo();
1305         cur.clearSelection();
1306         setCursorIntern(cur, begPit, begPos);
1307         cur.screenUpdateFlags(Update::Force);
1308         cur.forceBufferUpdate();
1309 }
1310
1311
1312 void Text::acceptChanges()
1313 {
1314         BufferParams const & bparams = owner_->buffer().params();
1315         lyx::acceptChanges(pars_, bparams);
1316         deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.trackChanges);
1317 }
1318
1319
1320 void Text::rejectChanges()
1321 {
1322         BufferParams const & bparams = owner_->buffer().params();
1323         pit_type pars_size = static_cast<pit_type>(pars_.size());
1324
1325         // first, reject changes within each individual paragraph
1326         // (do not consider end-of-par)
1327         for (pit_type pit = 0; pit < pars_size; ++pit) {
1328                 if (!pars_[pit].empty())   // prevent assertion failure
1329                         pars_[pit].rejectChanges(0, pars_[pit].size());
1330         }
1331
1332         // next, reject imaginary end-of-par characters
1333         for (pit_type pit = 0; pit < pars_size; ++pit) {
1334                 pos_type pos = pars_[pit].size();
1335
1336                 if (pars_[pit].isDeleted(pos)) {
1337                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1338                 } else if (pars_[pit].isInserted(pos)) {
1339                         if (pit == pars_size - 1) {
1340                                 // we mark the par break at the end of the last
1341                                 // paragraph unchanged
1342                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1343                         } else {
1344                                 mergeParagraph(bparams, pars_, pit);
1345                                 --pit;
1346                                 --pars_size;
1347                         }
1348                 }
1349         }
1350
1351         // finally, invoke the DEPM
1352         deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
1353 }
1354
1355
1356 void Text::deleteWordForward(Cursor & cur)
1357 {
1358         LASSERT(this == cur.text(), /**/);
1359         if (cur.lastpos() == 0)
1360                 cursorForward(cur);
1361         else {
1362                 cur.resetAnchor();
1363                 cur.setSelection(true);
1364                 cursorForwardOneWord(cur);
1365                 cur.setSelection();
1366                 cutSelection(cur, true, false);
1367                 cur.checkBufferStructure();
1368         }
1369 }
1370
1371
1372 void Text::deleteWordBackward(Cursor & cur)
1373 {
1374         LASSERT(this == cur.text(), /**/);
1375         if (cur.lastpos() == 0)
1376                 cursorBackward(cur);
1377         else {
1378                 cur.resetAnchor();
1379                 cur.setSelection(true);
1380                 cursorBackwardOneWord(cur);
1381                 cur.setSelection();
1382                 cutSelection(cur, true, false);
1383                 cur.checkBufferStructure();
1384         }
1385 }
1386
1387
1388 // Kill to end of line.
1389 void Text::changeCase(Cursor & cur, TextCase action)
1390 {
1391         LASSERT(this == cur.text(), /**/);
1392         CursorSlice from;
1393         CursorSlice to;
1394
1395         bool gotsel = false;
1396         if (cur.selection()) {
1397                 from = cur.selBegin();
1398                 to = cur.selEnd();
1399                 gotsel = true;
1400         } else {
1401                 from = cur.top();
1402                 getWord(from, to, PARTIAL_WORD);
1403                 cursorForwardOneWord(cur);
1404         }
1405
1406         cur.recordUndoSelection();
1407
1408         pit_type begPit = from.pit();
1409         pit_type endPit = to.pit();
1410
1411         pos_type begPos = from.pos();
1412         pos_type endPos = to.pos();
1413
1414         pos_type right = 0; // needed after the for loop
1415
1416         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1417                 Paragraph & par = pars_[pit];
1418                 pos_type const pos = (pit == begPit ? begPos : 0);
1419                 right = (pit == endPit ? endPos : par.size());
1420                 par.changeCase(cur.buffer()->params(), pos, right, action);
1421         }
1422
1423         // the selection may have changed due to logically-only deleted chars
1424         if (gotsel) {
1425                 setCursor(cur, begPit, begPos);
1426                 cur.resetAnchor();
1427                 setCursor(cur, endPit, right);
1428                 cur.setSelection();
1429         } else
1430                 setCursor(cur, endPit, right);
1431
1432         cur.checkBufferStructure();
1433 }
1434
1435
1436 bool Text::handleBibitems(Cursor & cur)
1437 {
1438         if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1439                 return false;
1440
1441         if (cur.pos() != 0)
1442                 return false;
1443
1444         BufferParams const & bufparams = cur.buffer()->params();
1445         Paragraph const & par = cur.paragraph();
1446         Cursor prevcur = cur;
1447         if (cur.pit() > 0) {
1448                 --prevcur.pit();
1449                 prevcur.pos() = prevcur.lastpos();
1450         }
1451         Paragraph const & prevpar = prevcur.paragraph();
1452
1453         // if a bibitem is deleted, merge with previous paragraph
1454         // if this is a bibliography item as well
1455         if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1456                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1457                 mergeParagraph(bufparams, cur.text()->paragraphs(),
1458                                                         prevcur.pit());
1459                 cur.forceBufferUpdate();
1460                 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1461                 cur.screenUpdateFlags(Update::Force);
1462                 return true;
1463         } 
1464
1465         // otherwise reset to default
1466         cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1467         return true;
1468 }
1469
1470
1471 bool Text::erase(Cursor & cur)
1472 {
1473         LASSERT(this == cur.text(), return false);
1474         bool needsUpdate = false;
1475         Paragraph & par = cur.paragraph();
1476
1477         if (cur.pos() != cur.lastpos()) {
1478                 // this is the code for a normal delete, not pasting
1479                 // any paragraphs
1480                 cur.recordUndo(DELETE_UNDO);
1481                 bool const was_inset = cur.paragraph().isInset(cur.pos());
1482                 if(!par.eraseChar(cur.pos(), cur.buffer()->params().trackChanges))
1483                         // the character has been logically deleted only => skip it
1484                         cur.top().forwardPos();
1485
1486                 if (was_inset)
1487                         cur.forceBufferUpdate();
1488                 else
1489                         cur.checkBufferStructure();
1490                 needsUpdate = true;
1491         } else {
1492                 if (cur.pit() == cur.lastpit())
1493                         return dissolveInset(cur);
1494
1495                 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1496                         par.setChange(cur.pos(), Change(Change::DELETED));
1497                         cur.forwardPos();
1498                         needsUpdate = true;
1499                 } else {
1500                         setCursorIntern(cur, cur.pit() + 1, 0);
1501                         needsUpdate = backspacePos0(cur);
1502                 }
1503         }
1504
1505         needsUpdate |= handleBibitems(cur);
1506
1507         if (needsUpdate) {
1508                 // Make sure the cursor is correct. Is this really needed?
1509                 // No, not really... at least not here!
1510                 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1511                 cur.checkBufferStructure();
1512         }
1513
1514         return needsUpdate;
1515 }
1516
1517
1518 bool Text::backspacePos0(Cursor & cur)
1519 {
1520         LASSERT(this == cur.text(), /**/);
1521         if (cur.pit() == 0)
1522                 return false;
1523
1524         bool needsUpdate = false;
1525
1526         BufferParams const & bufparams = cur.buffer()->params();
1527         DocumentClass const & tclass = bufparams.documentClass();
1528         ParagraphList & plist = cur.text()->paragraphs();
1529         Paragraph const & par = cur.paragraph();
1530         Cursor prevcur = cur;
1531         --prevcur.pit();
1532         prevcur.pos() = prevcur.lastpos();
1533         Paragraph const & prevpar = prevcur.paragraph();
1534
1535         // is it an empty paragraph?
1536         if (cur.lastpos() == 0
1537             || (cur.lastpos() == 1 && par.isSeparator(0))) {
1538                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1539                 plist.erase(boost::next(plist.begin(), cur.pit()));
1540                 needsUpdate = true;
1541         }
1542         // is previous par empty?
1543         else if (prevcur.lastpos() == 0
1544                  || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1545                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1546                 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1547                 needsUpdate = true;
1548         }
1549         // Pasting is not allowed, if the paragraphs have different
1550         // layouts. I think it is a real bug of all other
1551         // word processors to allow it. It confuses the user.
1552         // Correction: Pasting is always allowed with standard-layout
1553         // or the empty layout.
1554         else if (par.layout() == prevpar.layout()
1555                  || tclass.isDefaultLayout(par.layout())
1556                  || tclass.isPlainLayout(par.layout())) {
1557                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1558                 mergeParagraph(bufparams, plist, prevcur.pit());
1559                 needsUpdate = true;
1560         }
1561
1562         if (needsUpdate) {
1563                 cur.forceBufferUpdate();
1564                 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1565         }
1566
1567         return needsUpdate;
1568 }
1569
1570
1571 bool Text::backspace(Cursor & cur)
1572 {
1573         LASSERT(this == cur.text(), /**/);
1574         bool needsUpdate = false;
1575         if (cur.pos() == 0) {
1576                 if (cur.pit() == 0)
1577                         return dissolveInset(cur);
1578
1579                 Paragraph & prev_par = pars_[cur.pit() - 1];
1580
1581                 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1582                         prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1583                         setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1584                         return true;
1585                 }
1586                 // The cursor is at the beginning of a paragraph, so
1587                 // the backspace will collapse two paragraphs into one.
1588                 needsUpdate = backspacePos0(cur);
1589
1590         } else {
1591                 // this is the code for a normal backspace, not pasting
1592                 // any paragraphs
1593                 cur.recordUndo(DELETE_UNDO);
1594                 // We used to do cursorBackwardIntern() here, but it is
1595                 // not a good idea since it triggers the auto-delete
1596                 // mechanism. So we do a cursorBackwardIntern()-lite,
1597                 // without the dreaded mechanism. (JMarc)
1598                 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1599                                 false, cur.boundary());
1600                 bool const was_inset = cur.paragraph().isInset(cur.pos());
1601                 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
1602                 if (was_inset)
1603                         cur.forceBufferUpdate();
1604                 else
1605                         cur.checkBufferStructure();
1606         }
1607
1608         if (cur.pos() == cur.lastpos())
1609                 cur.setCurrentFont();
1610
1611         needsUpdate |= handleBibitems(cur);
1612
1613         // A singlePar update is not enough in this case.
1614 //              cur.screenUpdateFlags(Update::Force);
1615         setCursor(cur.top(), cur.pit(), cur.pos());
1616
1617         return needsUpdate;
1618 }
1619
1620
1621 bool Text::dissolveInset(Cursor & cur)
1622 {
1623         LASSERT(this == cur.text(), return false);
1624
1625         if (isMainText() || cur.inset().nargs() != 1)
1626                 return false;
1627
1628         cur.recordUndoInset();
1629         cur.setMark(false);
1630         cur.selHandle(false);
1631         // save position
1632         pos_type spos = cur.pos();
1633         pit_type spit = cur.pit();
1634         ParagraphList plist;
1635         if (cur.lastpit() != 0 || cur.lastpos() != 0)
1636                 plist = paragraphs();
1637         cur.popBackward();
1638         // store cursor offset
1639         if (spit == 0)
1640                 spos += cur.pos();
1641         spit += cur.pit();
1642         Buffer & b = *cur.buffer();
1643         cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1644         if (!plist.empty()) {
1645                 // ERT paragraphs have the Language latex_language.
1646                 // This is invalid outside of ERT, so we need to
1647                 // change it to the buffer language.
1648                 ParagraphList::iterator it = plist.begin();
1649                 ParagraphList::iterator it_end = plist.end();
1650                 for (; it != it_end; it++)
1651                         it->changeLanguage(b.params(), latex_language, b.language());
1652
1653                 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1654                                    b.errorList("Paste"));
1655                 // restore position
1656                 cur.pit() = min(cur.lastpit(), spit);
1657                 cur.pos() = min(cur.lastpos(), spos);
1658         } else
1659                 cur.forceBufferUpdate();
1660
1661         // Ensure the current language is set correctly (bug 6292)
1662         cur.text()->setCursor(cur, cur.pit(), cur.pos());
1663         cur.clearSelection();
1664         cur.resetAnchor();
1665         return true;
1666 }
1667
1668
1669 void Text::getWord(CursorSlice & from, CursorSlice & to,
1670         word_location const loc) const
1671 {
1672         to = from;
1673         pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1674 }
1675
1676
1677 void Text::write(ostream & os) const
1678 {
1679         Buffer const & buf = owner_->buffer();
1680         ParagraphList::const_iterator pit = paragraphs().begin();
1681         ParagraphList::const_iterator end = paragraphs().end();
1682         depth_type dth = 0;
1683         for (; pit != end; ++pit)
1684                 pit->write(os, buf.params(), dth);
1685
1686         // Close begin_deeper
1687         for(; dth > 0; --dth)
1688                 os << "\n\\end_deeper";
1689 }
1690
1691
1692 bool Text::read(Lexer & lex, 
1693                 ErrorList & errorList, InsetText * insetPtr)
1694 {
1695         Buffer const & buf = owner_->buffer();
1696         depth_type depth = 0;
1697         bool res = true;
1698
1699         while (lex.isOK()) {
1700                 lex.nextToken();
1701                 string const token = lex.getString();
1702
1703                 if (token.empty())
1704                         continue;
1705
1706                 if (token == "\\end_inset")
1707                         break;
1708
1709                 if (token == "\\end_body")
1710                         continue;
1711
1712                 if (token == "\\begin_body")
1713                         continue;
1714
1715                 if (token == "\\end_document") {
1716                         res = false;
1717                         break;
1718                 }
1719
1720                 if (token == "\\begin_layout") {
1721                         lex.pushToken(token);
1722
1723                         Paragraph par;
1724                         par.setInsetOwner(insetPtr);
1725                         par.params().depth(depth);
1726                         par.setFont(0, Font(inherit_font, buf.params().language));
1727                         pars_.push_back(par);
1728                         readParagraph(pars_.back(), lex, errorList);
1729
1730                         // register the words in the global word list
1731                         pars_.back().updateWords();
1732                 } else if (token == "\\begin_deeper") {
1733                         ++depth;
1734                 } else if (token == "\\end_deeper") {
1735                         if (!depth)
1736                                 lex.printError("\\end_deeper: " "depth is already null");
1737                         else
1738                                 --depth;
1739                 } else {
1740                         LYXERR0("Handling unknown body token: `" << token << '\'');
1741                 }
1742         }
1743
1744         // avoid a crash on weird documents (bug 4859)
1745         if (pars_.empty()) {
1746                 Paragraph par;
1747                 par.setInsetOwner(insetPtr);
1748                 par.params().depth(depth);
1749                 par.setFont(0, Font(inherit_font, 
1750                                     buf.params().language));
1751                 par.setPlainOrDefaultLayout(buf.params().documentClass());
1752                 pars_.push_back(par);
1753         }
1754         
1755         return res;
1756 }
1757
1758 // Returns the current font and depth as a message.
1759 docstring Text::currentState(Cursor const & cur) const
1760 {
1761         LASSERT(this == cur.text(), /**/);
1762         Buffer & buf = *cur.buffer();
1763         Paragraph const & par = cur.paragraph();
1764         odocstringstream os;
1765
1766         if (buf.params().trackChanges)
1767                 os << _("[Change Tracking] ");
1768
1769         Change change = par.lookupChange(cur.pos());
1770
1771         if (change.changed()) {
1772                 Author const & a = buf.params().authors().get(change.author);
1773                 os << _("Change: ") << a.name();
1774                 if (!a.email().empty())
1775                         os << " (" << a.email() << ")";
1776                 // FIXME ctime is english, we should translate that
1777                 os << _(" at ") << ctime(&change.changetime);
1778                 os << " : ";
1779         }
1780
1781         // I think we should only show changes from the default
1782         // font. (Asger)
1783         // No, from the document font (MV)
1784         Font font = cur.real_current_font;
1785         font.fontInfo().reduce(buf.params().getFont().fontInfo());
1786
1787         os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1788
1789         // The paragraph depth
1790         int depth = cur.paragraph().getDepth();
1791         if (depth > 0)
1792                 os << bformat(_(", Depth: %1$d"), depth);
1793
1794         // The paragraph spacing, but only if different from
1795         // buffer spacing.
1796         Spacing const & spacing = par.params().spacing();
1797         if (!spacing.isDefault()) {
1798                 os << _(", Spacing: ");
1799                 switch (spacing.getSpace()) {
1800                 case Spacing::Single:
1801                         os << _("Single");
1802                         break;
1803                 case Spacing::Onehalf:
1804                         os << _("OneHalf");
1805                         break;
1806                 case Spacing::Double:
1807                         os << _("Double");
1808                         break;
1809                 case Spacing::Other:
1810                         os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1811                         break;
1812                 case Spacing::Default:
1813                         // should never happen, do nothing
1814                         break;
1815                 }
1816         }
1817
1818 #ifdef DEVEL_VERSION
1819         os << _(", Inset: ") << &cur.inset();
1820         os << _(", Paragraph: ") << cur.pit();
1821         os << _(", Id: ") << par.id();
1822         os << _(", Position: ") << cur.pos();
1823         // FIXME: Why is the check for par.size() needed?
1824         // We are called with cur.pos() == par.size() quite often.
1825         if (!par.empty() && cur.pos() < par.size()) {
1826                 // Force output of code point, not character
1827                 size_t const c = par.getChar(cur.pos());
1828                 os << _(", Char: 0x") << hex << c;
1829         }
1830         os << _(", Boundary: ") << cur.boundary();
1831 //      Row & row = cur.textRow();
1832 //      os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1833 #endif
1834         return os.str();
1835 }
1836
1837
1838 docstring Text::getPossibleLabel(Cursor const & cur) const
1839 {
1840         pit_type pit = cur.pit();
1841
1842         Layout const * layout = &(pars_[pit].layout());
1843
1844         docstring text;
1845         docstring par_text = pars_[pit].asString();
1846
1847         // The return string of math matrices might contain linebreaks
1848         par_text = subst(par_text, '\n', '-');
1849         int const numwords = 3;
1850         for (int i = 0; i < numwords; ++i) {
1851                 if (par_text.empty())
1852                         break;
1853                 docstring head;
1854                 par_text = split(par_text, head, ' ');
1855                 // Is it legal to use spaces in labels ?
1856                 if (i > 0)
1857                         text += '-';
1858                 text += head;
1859         }
1860         
1861         // Make sure it isn't too long
1862         unsigned int const max_label_length = 32;
1863         if (text.size() > max_label_length)
1864                 text.resize(max_label_length);
1865
1866         // Will contain the label prefix.
1867         docstring name;
1868
1869         // For section, subsection, etc...
1870         if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1871                 Layout const * layout2 = &(pars_[pit - 1].layout());
1872                 if (layout2->latextype != LATEX_PARAGRAPH) {
1873                         --pit;
1874                         layout = layout2;
1875                 }
1876         }
1877         if (layout->latextype != LATEX_PARAGRAPH)
1878                 name = layout->refprefix;
1879
1880         // For captions, we just take the caption type
1881         Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
1882         if (caption_inset) {
1883                 string const & ftype = static_cast<InsetCaption *>(caption_inset)->type();
1884                 FloatList const & fl = cur.buffer()->params().documentClass().floats();
1885                 if (fl.typeExist(ftype)) {
1886                         Floating const & flt = fl.getType(ftype);
1887                         name = from_utf8(flt.refPrefix());
1888                 }
1889                 if (name.empty())
1890                         name = from_utf8(ftype.substr(0,3));
1891         }
1892
1893         // If none of the above worked, see if the inset knows.
1894         if (name.empty()) {
1895                 InsetLayout const & il = cur.inset().getLayout();
1896                 name = il.refprefix();
1897         }
1898
1899         if (!name.empty())
1900                 // FIXME refstyle
1901                 // We should allow customization of the separator or else change it
1902                 text = name + ':' + text;
1903
1904         return text;
1905 }
1906
1907
1908 docstring Text::asString(int options) const
1909 {
1910         return asString(0, pars_.size(), options);
1911 }
1912
1913
1914 docstring Text::asString(pit_type beg, pit_type end, int options) const
1915 {
1916         size_t i = size_t(beg);
1917         docstring str = pars_[i].asString(options);
1918         for (++i; i != size_t(end); ++i) {
1919                 str += '\n';
1920                 str += pars_[i].asString(options);
1921         }
1922         return str;
1923 }
1924
1925
1926
1927 void Text::charsTranspose(Cursor & cur)
1928 {
1929         LASSERT(this == cur.text(), /**/);
1930
1931         pos_type pos = cur.pos();
1932
1933         // If cursor is at beginning or end of paragraph, do nothing.
1934         if (pos == cur.lastpos() || pos == 0)
1935                 return;
1936
1937         Paragraph & par = cur.paragraph();
1938
1939         // Get the positions of the characters to be transposed.
1940         pos_type pos1 = pos - 1;
1941         pos_type pos2 = pos;
1942
1943         // In change tracking mode, ignore deleted characters.
1944         while (pos2 < cur.lastpos() && par.isDeleted(pos2))
1945                 ++pos2;
1946         if (pos2 == cur.lastpos())
1947                 return;
1948
1949         while (pos1 >= 0 && par.isDeleted(pos1))
1950                 --pos1;
1951         if (pos1 < 0)
1952                 return;
1953
1954         // Don't do anything if one of the "characters" is not regular text.
1955         if (par.isInset(pos1) || par.isInset(pos2))
1956                 return;
1957
1958         // Store the characters to be transposed (including font information).
1959         char_type const char1 = par.getChar(pos1);
1960         Font const font1 =
1961                 par.getFontSettings(cur.buffer()->params(), pos1);
1962
1963         char_type const char2 = par.getChar(pos2);
1964         Font const font2 =
1965                 par.getFontSettings(cur.buffer()->params(), pos2);
1966
1967         // And finally, we are ready to perform the transposition.
1968         // Track the changes if Change Tracking is enabled.
1969         bool const trackChanges = cur.buffer()->params().trackChanges;
1970
1971         cur.recordUndo();
1972
1973         par.eraseChar(pos2, trackChanges);
1974         par.eraseChar(pos1, trackChanges);
1975         par.insertChar(pos1, char2, font2, trackChanges);
1976         par.insertChar(pos2, char1, font1, trackChanges);
1977
1978         cur.checkBufferStructure();
1979
1980         // After the transposition, move cursor to after the transposition.
1981         setCursor(cur, cur.pit(), pos2);
1982         cur.forwardPos();
1983 }
1984
1985
1986 DocIterator Text::macrocontextPosition() const
1987 {
1988         return macrocontext_position_;
1989 }
1990
1991
1992 void Text::setMacrocontextPosition(DocIterator const & pos)
1993 {
1994         macrocontext_position_ = pos;
1995 }
1996
1997
1998 docstring Text::previousWord(CursorSlice const & sl) const
1999 {
2000         CursorSlice from = sl;
2001         CursorSlice to = sl;
2002         getWord(from, to, PREVIOUS_WORD);
2003         if (sl == from || to == from)
2004                 return docstring();
2005         
2006         Paragraph const & par = sl.paragraph();
2007         return par.asString(from.pos(), to.pos());
2008 }
2009
2010
2011 bool Text::completionSupported(Cursor const & cur) const
2012 {
2013         Paragraph const & par = cur.paragraph();
2014         return cur.pos() > 0
2015                 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2016                 && !par.isWordSeparator(cur.pos() - 1);
2017 }
2018
2019
2020 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2021 {
2022         WordList const * list = theWordList(*cur.getFont().language());
2023         return new TextCompletionList(cur, list);
2024 }
2025
2026
2027 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2028 {       
2029         LASSERT(cur.bv().cursor() == cur, /**/);
2030         cur.insert(s);
2031         cur.bv().cursor() = cur;
2032         if (!(cur.result().screenUpdate() & Update::Force))
2033                 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2034         return true;
2035 }
2036         
2037         
2038 docstring Text::completionPrefix(Cursor const & cur) const
2039 {
2040         return previousWord(cur.top());
2041 }
2042
2043 } // namespace lyx