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