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