]> git.lyx.org Git - lyx.git/blob - src/Text.cpp
Add lookup for standard location of hunspell dictionaries of Debian packages
[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/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         // Don't do it for listings inset, since InsetListings::latex() tries
1012         // to switch to a usable encoding which works in many cases (bug 9102).
1013         if (cur.paragraph().isPassThru() && owner_->lyxCode() != LISTINGS_CODE &&
1014             cur.current_font.language()) {
1015                 Encoding const * e = cur.current_font.language()->encoding();
1016                 if (!e->encodable(c)) {
1017                         cur.message(_("Character is uncodable in verbatim paragraphs."));
1018                         return;
1019                 }
1020         }
1021
1022         par.insertChar(cur.pos(), c, cur.current_font,
1023                 cur.buffer()->params().track_changes);
1024         cur.checkBufferStructure();
1025
1026 //              cur.screenUpdateFlags(Update::Force);
1027         bool boundary = cur.boundary()
1028                 || tm.isRTLBoundary(cur.pit(), cur.pos() + 1);
1029         setCursor(cur, cur.pit(), cur.pos() + 1, false, boundary);
1030         charInserted(cur);
1031 }
1032
1033
1034 void Text::charInserted(Cursor & cur)
1035 {
1036         Paragraph & par = cur.paragraph();
1037
1038         // Here we call finishUndo for every 20 characters inserted.
1039         // This is from my experience how emacs does it. (Lgb)
1040         if (undo_counter_ < 20) {
1041                 ++undo_counter_;
1042         } else {
1043                 cur.finishUndo();
1044                 undo_counter_ = 0;
1045         }
1046
1047         // register word if a non-letter was entered
1048         if (cur.pos() > 1
1049             && !par.isWordSeparator(cur.pos() - 2)
1050             && par.isWordSeparator(cur.pos() - 1)) {
1051                 // get the word in front of cursor
1052                 LBUFERR(this == cur.text());
1053                 cur.paragraph().updateWords();
1054         }
1055 }
1056
1057
1058 // the cursor set functions have a special mechanism. When they
1059 // realize, that you left an empty paragraph, they will delete it.
1060
1061 bool Text::cursorForwardOneWord(Cursor & cur)
1062 {
1063         LBUFERR(this == cur.text());
1064
1065         pos_type const lastpos = cur.lastpos();
1066         pit_type pit = cur.pit();
1067         pos_type pos = cur.pos();
1068         Paragraph const & par = cur.paragraph();
1069
1070         // Paragraph boundary is a word boundary
1071         if (pos == lastpos) {
1072                 if (pit != cur.lastpit())
1073                         return setCursor(cur, pit + 1, 0);
1074                 else
1075                         return false;
1076         }
1077
1078         if (lyxrc.mac_like_cursor_movement) {
1079                 // Skip through trailing punctuation and spaces.
1080                 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
1081                         ++pos;
1082
1083                 // Skip over either a non-char inset or a full word
1084                 if (pos != lastpos && par.isWordSeparator(pos))
1085                         ++pos;
1086                 else while (pos != lastpos && !par.isWordSeparator(pos))
1087                              ++pos;
1088         } else {
1089                 LASSERT(pos < lastpos, return false); // see above
1090                 if (!par.isWordSeparator(pos))
1091                         while (pos != lastpos && !par.isWordSeparator(pos))
1092                                 ++pos;
1093                 else if (par.isChar(pos))
1094                         while (pos != lastpos && par.isChar(pos))
1095                                 ++pos;
1096                 else if (!par.isSpace(pos)) // non-char inset
1097                         ++pos;
1098
1099                 // Skip over white space
1100                 while (pos != lastpos && par.isSpace(pos))
1101                              ++pos;
1102         }
1103
1104         return setCursor(cur, pit, pos);
1105 }
1106
1107
1108 bool Text::cursorBackwardOneWord(Cursor & cur)
1109 {
1110         LBUFERR(this == cur.text());
1111
1112         pit_type pit = cur.pit();
1113         pos_type pos = cur.pos();
1114         Paragraph & par = cur.paragraph();
1115
1116         // Paragraph boundary is a word boundary
1117         if (pos == 0 && pit != 0)
1118                 return setCursor(cur, pit - 1, getPar(pit - 1).size());
1119
1120         if (lyxrc.mac_like_cursor_movement) {
1121                 // Skip through punctuation and spaces.
1122                 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1123                         --pos;
1124
1125                 // Skip over either a non-char inset or a full word
1126                 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1127                         --pos;
1128                 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1129                              --pos;
1130         } else {
1131                 // Skip over white space
1132                 while (pos != 0 && par.isSpace(pos - 1))
1133                              --pos;
1134
1135                 if (pos != 0 && !par.isWordSeparator(pos - 1))
1136                         while (pos != 0 && !par.isWordSeparator(pos - 1))
1137                                 --pos;
1138                 else if (pos != 0 && par.isChar(pos - 1))
1139                         while (pos != 0 && par.isChar(pos - 1))
1140                                 --pos;
1141                 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1142                         --pos;
1143         }
1144
1145         return setCursor(cur, pit, pos);
1146 }
1147
1148
1149 bool Text::cursorVisLeftOneWord(Cursor & cur)
1150 {
1151         LBUFERR(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.posVisLeft(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(), left_pos).isRightToLeft())
1176                         || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1177                                 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1178                         break;
1179         }
1180
1181         return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1182                                          true, temp_cur.boundary());
1183 }
1184
1185
1186 bool Text::cursorVisRightOneWord(Cursor & cur)
1187 {
1188         LBUFERR(this == cur.text());
1189
1190         pos_type left_pos, right_pos;
1191         bool left_is_letter, right_is_letter;
1192
1193         Cursor temp_cur = cur;
1194
1195         // always try to move at least once...
1196         while (temp_cur.posVisRight(true /* skip_inset */)) {
1197
1198                 // collect some information about current cursor position
1199                 temp_cur.getSurroundingPos(left_pos, right_pos);
1200                 left_is_letter =
1201                         (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1202                 right_is_letter =
1203                         (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1204
1205                 // if we're not at a letter/non-letter boundary, continue moving
1206                 if (left_is_letter == right_is_letter)
1207                         continue;
1208
1209                 // we should stop when we have an LTR word on our right or an RTL word
1210                 // on our left
1211                 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1212                                 temp_cur.buffer()->params(),
1213                                 left_pos).isRightToLeft())
1214                         || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1215                                 temp_cur.buffer()->params(),
1216                                 right_pos).isRightToLeft()))
1217                         break;
1218         }
1219
1220         return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1221                                          true, temp_cur.boundary());
1222 }
1223
1224
1225 void Text::selectWord(Cursor & cur, word_location loc)
1226 {
1227         LBUFERR(this == cur.text());
1228         CursorSlice from = cur.top();
1229         CursorSlice to = cur.top();
1230         getWord(from, to, loc);
1231         if (cur.top() != from)
1232                 setCursor(cur, from.pit(), from.pos());
1233         if (to == from)
1234                 return;
1235         if (!cur.selection())
1236                 cur.resetAnchor();
1237         setCursor(cur, to.pit(), to.pos());
1238         cur.setSelection();
1239         cur.setWordSelection(true);
1240 }
1241
1242
1243 void Text::selectAll(Cursor & cur)
1244 {
1245         LBUFERR(this == cur.text());
1246         if (cur.lastpos() == 0 && cur.lastpit() == 0)
1247                 return;
1248         // If the cursor is at the beginning, make sure the cursor ends there
1249         if (cur.pit() == 0 && cur.pos() == 0) {
1250                 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1251                 cur.resetAnchor();
1252                 setCursor(cur, 0, 0);
1253         } else {
1254                 setCursor(cur, 0, 0);
1255                 cur.resetAnchor();
1256                 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1257         }
1258         cur.setSelection();
1259 }
1260
1261
1262 // Select the word currently under the cursor when no
1263 // selection is currently set
1264 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1265 {
1266         LBUFERR(this == cur.text());
1267         if (cur.selection())
1268                 return false;
1269         selectWord(cur, loc);
1270         return cur.selection();
1271 }
1272
1273
1274 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1275 {
1276         LBUFERR(this == cur.text());
1277
1278         if (!cur.selection()) {
1279                 if (!selectChange(cur))
1280                         return;
1281         }
1282
1283         cur.recordUndoSelection();
1284
1285         pit_type begPit = cur.selectionBegin().pit();
1286         pit_type endPit = cur.selectionEnd().pit();
1287
1288         pos_type begPos = cur.selectionBegin().pos();
1289         pos_type endPos = cur.selectionEnd().pos();
1290
1291         // keep selection info, because endPos becomes invalid after the first loop
1292         bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1293
1294         // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1295         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1296                 pos_type parSize = pars_[pit].size();
1297
1298                 // ignore empty paragraphs; otherwise, an assertion will fail for
1299                 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1300                 if (parSize == 0)
1301                         continue;
1302
1303                 // do not consider first paragraph if the cursor starts at pos size()
1304                 if (pit == begPit && begPos == parSize)
1305                         continue;
1306
1307                 // do not consider last paragraph if the cursor ends at pos 0
1308                 if (pit == endPit && endPos == 0)
1309                         break; // last iteration anyway
1310
1311                 pos_type left  = (pit == begPit ? begPos : 0);
1312                 pos_type right = (pit == endPit ? endPos : parSize);
1313
1314                 if (left == right)
1315                         // there is no change here
1316                         continue;
1317
1318                 if (op == ACCEPT) {
1319                         pars_[pit].acceptChanges(left, right);
1320                 } else {
1321                         pars_[pit].rejectChanges(left, right);
1322                 }
1323         }
1324
1325         // next, accept/reject imaginary end-of-par characters
1326
1327         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1328                 pos_type pos = pars_[pit].size();
1329
1330                 // skip if the selection ends before the end-of-par
1331                 if (pit == endPit && endsBeforeEndOfPar)
1332                         break; // last iteration anyway
1333
1334                 // skip if this is not the last paragraph of the document
1335                 // note: the user should be able to accept/reject the par break of the last par!
1336                 if (pit == endPit && pit + 1 != int(pars_.size()))
1337                         break; // last iteration anway
1338
1339                 if (op == ACCEPT) {
1340                         if (pars_[pit].isInserted(pos)) {
1341                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1342                         } else if (pars_[pit].isDeleted(pos)) {
1343                                 if (pit + 1 == int(pars_.size())) {
1344                                         // we cannot remove a par break at the end of the last paragraph;
1345                                         // instead, we mark it unchanged
1346                                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1347                                 } else {
1348                                         mergeParagraph(cur.buffer()->params(), pars_, pit);
1349                                         --endPit;
1350                                         --pit;
1351                                 }
1352                         }
1353                 } else {
1354                         if (pars_[pit].isDeleted(pos)) {
1355                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1356                         } else if (pars_[pit].isInserted(pos)) {
1357                                 if (pit + 1 == int(pars_.size())) {
1358                                         // we mark the par break at the end of the last paragraph unchanged
1359                                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1360                                 } else {
1361                                         mergeParagraph(cur.buffer()->params(), pars_, pit);
1362                                         --endPit;
1363                                         --pit;
1364                                 }
1365                         }
1366                 }
1367         }
1368
1369         // finally, invoke the DEPM
1370         deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().track_changes);
1371
1372         cur.finishUndo();
1373         cur.clearSelection();
1374         setCursorIntern(cur, begPit, begPos);
1375         cur.screenUpdateFlags(Update::Force);
1376         cur.forceBufferUpdate();
1377 }
1378
1379
1380 void Text::acceptChanges()
1381 {
1382         BufferParams const & bparams = owner_->buffer().params();
1383         lyx::acceptChanges(pars_, bparams);
1384         deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.track_changes);
1385 }
1386
1387
1388 void Text::rejectChanges()
1389 {
1390         BufferParams const & bparams = owner_->buffer().params();
1391         pit_type pars_size = static_cast<pit_type>(pars_.size());
1392
1393         // first, reject changes within each individual paragraph
1394         // (do not consider end-of-par)
1395         for (pit_type pit = 0; pit < pars_size; ++pit) {
1396                 if (!pars_[pit].empty())   // prevent assertion failure
1397                         pars_[pit].rejectChanges(0, pars_[pit].size());
1398         }
1399
1400         // next, reject imaginary end-of-par characters
1401         for (pit_type pit = 0; pit < pars_size; ++pit) {
1402                 pos_type pos = pars_[pit].size();
1403
1404                 if (pars_[pit].isDeleted(pos)) {
1405                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1406                 } else if (pars_[pit].isInserted(pos)) {
1407                         if (pit == pars_size - 1) {
1408                                 // we mark the par break at the end of the last
1409                                 // paragraph unchanged
1410                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1411                         } else {
1412                                 mergeParagraph(bparams, pars_, pit);
1413                                 --pit;
1414                                 --pars_size;
1415                         }
1416                 }
1417         }
1418
1419         // finally, invoke the DEPM
1420         deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.track_changes);
1421 }
1422
1423
1424 void Text::deleteWordForward(Cursor & cur)
1425 {
1426         LBUFERR(this == cur.text());
1427         if (cur.lastpos() == 0)
1428                 cursorForward(cur);
1429         else {
1430                 cur.resetAnchor();
1431                 cur.setSelection(true);
1432                 cursorForwardOneWord(cur);
1433                 cur.setSelection();
1434                 cutSelection(cur, true, false);
1435                 cur.checkBufferStructure();
1436         }
1437 }
1438
1439
1440 void Text::deleteWordBackward(Cursor & cur)
1441 {
1442         LBUFERR(this == cur.text());
1443         if (cur.lastpos() == 0)
1444                 cursorBackward(cur);
1445         else {
1446                 cur.resetAnchor();
1447                 cur.setSelection(true);
1448                 cursorBackwardOneWord(cur);
1449                 cur.setSelection();
1450                 cutSelection(cur, true, false);
1451                 cur.checkBufferStructure();
1452         }
1453 }
1454
1455
1456 // Kill to end of line.
1457 void Text::changeCase(Cursor & cur, TextCase action)
1458 {
1459         LBUFERR(this == cur.text());
1460         CursorSlice from;
1461         CursorSlice to;
1462
1463         bool gotsel = false;
1464         if (cur.selection()) {
1465                 from = cur.selBegin();
1466                 to = cur.selEnd();
1467                 gotsel = true;
1468         } else {
1469                 from = cur.top();
1470                 getWord(from, to, PARTIAL_WORD);
1471                 cursorForwardOneWord(cur);
1472         }
1473
1474         cur.recordUndoSelection();
1475
1476         pit_type begPit = from.pit();
1477         pit_type endPit = to.pit();
1478
1479         pos_type begPos = from.pos();
1480         pos_type endPos = to.pos();
1481
1482         pos_type right = 0; // needed after the for loop
1483
1484         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1485                 Paragraph & par = pars_[pit];
1486                 pos_type const pos = (pit == begPit ? begPos : 0);
1487                 right = (pit == endPit ? endPos : par.size());
1488                 par.changeCase(cur.buffer()->params(), pos, right, action);
1489         }
1490
1491         // the selection may have changed due to logically-only deleted chars
1492         if (gotsel) {
1493                 setCursor(cur, begPit, begPos);
1494                 cur.resetAnchor();
1495                 setCursor(cur, endPit, right);
1496                 cur.setSelection();
1497         } else
1498                 setCursor(cur, endPit, right);
1499
1500         cur.checkBufferStructure();
1501 }
1502
1503
1504 bool Text::handleBibitems(Cursor & cur)
1505 {
1506         if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1507                 return false;
1508
1509         if (cur.pos() != 0)
1510                 return false;
1511
1512         BufferParams const & bufparams = cur.buffer()->params();
1513         Paragraph const & par = cur.paragraph();
1514         Cursor prevcur = cur;
1515         if (cur.pit() > 0) {
1516                 --prevcur.pit();
1517                 prevcur.pos() = prevcur.lastpos();
1518         }
1519         Paragraph const & prevpar = prevcur.paragraph();
1520
1521         // if a bibitem is deleted, merge with previous paragraph
1522         // if this is a bibliography item as well
1523         if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1524                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1525                 mergeParagraph(bufparams, cur.text()->paragraphs(),
1526                                                         prevcur.pit());
1527                 cur.forceBufferUpdate();
1528                 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1529                 cur.screenUpdateFlags(Update::Force);
1530                 return true;
1531         }
1532
1533         // otherwise reset to default
1534         cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1535         return true;
1536 }
1537
1538
1539 bool Text::erase(Cursor & cur)
1540 {
1541         LASSERT(this == cur.text(), return false);
1542         bool needsUpdate = false;
1543         Paragraph & par = cur.paragraph();
1544
1545         if (cur.pos() != cur.lastpos()) {
1546                 // this is the code for a normal delete, not pasting
1547                 // any paragraphs
1548                 cur.recordUndo(DELETE_UNDO);
1549                 bool const was_inset = cur.paragraph().isInset(cur.pos());
1550                 if(!par.eraseChar(cur.pos(), cur.buffer()->params().track_changes))
1551                         // the character has been logically deleted only => skip it
1552                         cur.top().forwardPos();
1553
1554                 if (was_inset)
1555                         cur.forceBufferUpdate();
1556                 else
1557                         cur.checkBufferStructure();
1558                 needsUpdate = true;
1559         } else {
1560                 if (cur.pit() == cur.lastpit())
1561                         return dissolveInset(cur);
1562
1563                 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1564                         par.setChange(cur.pos(), Change(Change::DELETED));
1565                         cur.forwardPos();
1566                         needsUpdate = true;
1567                 } else {
1568                         setCursorIntern(cur, cur.pit() + 1, 0);
1569                         needsUpdate = backspacePos0(cur);
1570                 }
1571         }
1572
1573         needsUpdate |= handleBibitems(cur);
1574
1575         if (needsUpdate) {
1576                 // Make sure the cursor is correct. Is this really needed?
1577                 // No, not really... at least not here!
1578                 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1579                 cur.checkBufferStructure();
1580         }
1581
1582         return needsUpdate;
1583 }
1584
1585
1586 bool Text::backspacePos0(Cursor & cur)
1587 {
1588         LBUFERR(this == cur.text());
1589         if (cur.pit() == 0)
1590                 return false;
1591
1592         bool needsUpdate = false;
1593
1594         BufferParams const & bufparams = cur.buffer()->params();
1595         DocumentClass const & tclass = bufparams.documentClass();
1596         ParagraphList & plist = cur.text()->paragraphs();
1597         Paragraph const & par = cur.paragraph();
1598         Cursor prevcur = cur;
1599         --prevcur.pit();
1600         prevcur.pos() = prevcur.lastpos();
1601         Paragraph const & prevpar = prevcur.paragraph();
1602
1603         // is it an empty paragraph?
1604         if (cur.lastpos() == 0
1605             || (cur.lastpos() == 1 && par.isSeparator(0))) {
1606                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1607                 plist.erase(boost::next(plist.begin(), cur.pit()));
1608                 needsUpdate = true;
1609         }
1610         // is previous par empty?
1611         else if (prevcur.lastpos() == 0
1612                  || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1613                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1614                 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1615                 needsUpdate = true;
1616         }
1617         // Pasting is not allowed, if the paragraphs have different
1618         // layouts. I think it is a real bug of all other
1619         // word processors to allow it. It confuses the user.
1620         // Correction: Pasting is always allowed with standard-layout
1621         // or the empty layout.
1622         else if (par.layout() == prevpar.layout()
1623                  || tclass.isDefaultLayout(par.layout())
1624                  || tclass.isPlainLayout(par.layout())) {
1625                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1626                 mergeParagraph(bufparams, plist, prevcur.pit());
1627                 needsUpdate = true;
1628         }
1629
1630         if (needsUpdate) {
1631                 cur.forceBufferUpdate();
1632                 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1633         }
1634
1635         return needsUpdate;
1636 }
1637
1638
1639 bool Text::backspace(Cursor & cur)
1640 {
1641         LBUFERR(this == cur.text());
1642         bool needsUpdate = false;
1643         if (cur.pos() == 0) {
1644                 if (cur.pit() == 0)
1645                         return dissolveInset(cur);
1646
1647                 Cursor prev_cur = cur;
1648                 --prev_cur.pit();
1649
1650                 if (!prev_cur.paragraph().isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1651                         cur.recordUndo(ATOMIC_UNDO, prev_cur.pit(), prev_cur.pit());
1652                         prev_cur.paragraph().setChange(prev_cur.lastpos(), Change(Change::DELETED));
1653                         setCursorIntern(cur, prev_cur.pit(), prev_cur.lastpos());
1654                         return true;
1655                 }
1656                 // The cursor is at the beginning of a paragraph, so
1657                 // the backspace will collapse two paragraphs into one.
1658                 needsUpdate = backspacePos0(cur);
1659
1660         } else {
1661                 // this is the code for a normal backspace, not pasting
1662                 // any paragraphs
1663                 cur.recordUndo(DELETE_UNDO);
1664                 // We used to do cursorBackwardIntern() here, but it is
1665                 // not a good idea since it triggers the auto-delete
1666                 // mechanism. So we do a cursorBackwardIntern()-lite,
1667                 // without the dreaded mechanism. (JMarc)
1668                 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1669                                 false, cur.boundary());
1670                 bool const was_inset = cur.paragraph().isInset(cur.pos());
1671                 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().track_changes);
1672                 if (was_inset)
1673                         cur.forceBufferUpdate();
1674                 else
1675                         cur.checkBufferStructure();
1676         }
1677
1678         if (cur.pos() == cur.lastpos())
1679                 cur.setCurrentFont();
1680
1681         needsUpdate |= handleBibitems(cur);
1682
1683         // A singlePar update is not enough in this case.
1684 //              cur.screenUpdateFlags(Update::Force);
1685         setCursor(cur.top(), cur.pit(), cur.pos());
1686
1687         return needsUpdate;
1688 }
1689
1690
1691 bool Text::dissolveInset(Cursor & cur)
1692 {
1693         LASSERT(this == cur.text(), return false);
1694
1695         if (isMainText() || cur.inset().nargs() != 1)
1696                 return false;
1697
1698         cur.recordUndoInset();
1699         cur.setMark(false);
1700         cur.selHandle(false);
1701         // save position
1702         pos_type spos = cur.pos();
1703         pit_type spit = cur.pit();
1704         ParagraphList plist;
1705         if (cur.lastpit() != 0 || cur.lastpos() != 0)
1706                 plist = paragraphs();
1707         cur.popBackward();
1708         // store cursor offset
1709         if (spit == 0)
1710                 spos += cur.pos();
1711         spit += cur.pit();
1712         Buffer & b = *cur.buffer();
1713         cur.paragraph().eraseChar(cur.pos(), b.params().track_changes);
1714
1715         if (!plist.empty()) {
1716                 // see bug 7319
1717                 // we clear the cache so that we won't get conflicts with labels
1718                 // that get pasted into the buffer. we should update this before
1719                 // its being empty matters. if not (i.e., if we encounter bugs),
1720                 // then this should instead be:
1721                 //        cur.buffer().updateBuffer();
1722                 // but we'll try the cheaper solution here.
1723                 cur.buffer()->clearReferenceCache();
1724
1725                 // ERT paragraphs have the Language latex_language.
1726                 // This is invalid outside of ERT, so we need to
1727                 // change it to the buffer language.
1728                 ParagraphList::iterator it = plist.begin();
1729                 ParagraphList::iterator it_end = plist.end();
1730                 for (; it != it_end; ++it)
1731                         it->changeLanguage(b.params(), latex_language, b.language());
1732
1733                 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1734                                    b.errorList("Paste"));
1735                 // restore position
1736                 cur.pit() = min(cur.lastpit(), spit);
1737                 cur.pos() = min(cur.lastpos(), spos);
1738         }
1739
1740         cur.forceBufferUpdate();
1741
1742         // Ensure the current language is set correctly (bug 6292)
1743         cur.text()->setCursor(cur, cur.pit(), cur.pos());
1744         cur.clearSelection();
1745         cur.resetAnchor();
1746         return true;
1747 }
1748
1749
1750 void Text::getWord(CursorSlice & from, CursorSlice & to,
1751         word_location const loc) const
1752 {
1753         to = from;
1754         pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1755 }
1756
1757
1758 void Text::write(ostream & os) const
1759 {
1760         Buffer const & buf = owner_->buffer();
1761         ParagraphList::const_iterator pit = paragraphs().begin();
1762         ParagraphList::const_iterator end = paragraphs().end();
1763         depth_type dth = 0;
1764         for (; pit != end; ++pit)
1765                 pit->write(os, buf.params(), dth);
1766
1767         // Close begin_deeper
1768         for(; dth > 0; --dth)
1769                 os << "\n\\end_deeper";
1770 }
1771
1772
1773 bool Text::read(Lexer & lex,
1774                 ErrorList & errorList, InsetText * insetPtr)
1775 {
1776         Buffer const & buf = owner_->buffer();
1777         depth_type depth = 0;
1778         bool res = true;
1779
1780         while (lex.isOK()) {
1781                 lex.nextToken();
1782                 string const token = lex.getString();
1783
1784                 if (token.empty())
1785                         continue;
1786
1787                 if (token == "\\end_inset")
1788                         break;
1789
1790                 if (token == "\\end_body")
1791                         continue;
1792
1793                 if (token == "\\begin_body")
1794                         continue;
1795
1796                 if (token == "\\end_document") {
1797                         res = false;
1798                         break;
1799                 }
1800
1801                 if (token == "\\begin_layout") {
1802                         lex.pushToken(token);
1803
1804                         Paragraph par;
1805                         par.setInsetOwner(insetPtr);
1806                         par.params().depth(depth);
1807                         par.setFont(0, Font(inherit_font, buf.params().language));
1808                         pars_.push_back(par);
1809                         readParagraph(pars_.back(), lex, errorList);
1810
1811                         // register the words in the global word list
1812                         pars_.back().updateWords();
1813                 } else if (token == "\\begin_deeper") {
1814                         ++depth;
1815                 } else if (token == "\\end_deeper") {
1816                         if (!depth)
1817                                 lex.printError("\\end_deeper: " "depth is already null");
1818                         else
1819                                 --depth;
1820                 } else {
1821                         LYXERR0("Handling unknown body token: `" << token << '\'');
1822                 }
1823         }
1824
1825         // avoid a crash on weird documents (bug 4859)
1826         if (pars_.empty()) {
1827                 Paragraph par;
1828                 par.setInsetOwner(insetPtr);
1829                 par.params().depth(depth);
1830                 par.setFont(0, Font(inherit_font,
1831                                     buf.params().language));
1832                 par.setPlainOrDefaultLayout(buf.params().documentClass());
1833                 pars_.push_back(par);
1834         }
1835
1836         return res;
1837 }
1838
1839
1840 // Returns the current font and depth as a message.
1841 docstring Text::currentState(Cursor const & cur) const
1842 {
1843         LBUFERR(this == cur.text());
1844         Buffer & buf = *cur.buffer();
1845         Paragraph const & par = cur.paragraph();
1846         odocstringstream os;
1847
1848         if (buf.params().track_changes)
1849                 os << _("[Change Tracking] ");
1850
1851         Change change = par.lookupChange(cur.pos());
1852
1853         if (change.changed()) {
1854                 Author const & a = buf.params().authors().get(change.author);
1855                 os << _("Change: ") << a.name();
1856                 if (!a.email().empty())
1857                         os << " (" << a.email() << ")";
1858                 // FIXME ctime is english, we should translate that
1859                 os << _(" at ") << ctime(&change.changetime);
1860                 os << " : ";
1861         }
1862
1863         // I think we should only show changes from the default
1864         // font. (Asger)
1865         // No, from the document font (MV)
1866         Font font = cur.real_current_font;
1867         font.fontInfo().reduce(buf.params().getFont().fontInfo());
1868
1869         os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1870
1871         // The paragraph depth
1872         int depth = cur.paragraph().getDepth();
1873         if (depth > 0)
1874                 os << bformat(_(", Depth: %1$d"), depth);
1875
1876         // The paragraph spacing, but only if different from
1877         // buffer spacing.
1878         Spacing const & spacing = par.params().spacing();
1879         if (!spacing.isDefault()) {
1880                 os << _(", Spacing: ");
1881                 switch (spacing.getSpace()) {
1882                 case Spacing::Single:
1883                         os << _("Single");
1884                         break;
1885                 case Spacing::Onehalf:
1886                         os << _("OneHalf");
1887                         break;
1888                 case Spacing::Double:
1889                         os << _("Double");
1890                         break;
1891                 case Spacing::Other:
1892                         os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1893                         break;
1894                 case Spacing::Default:
1895                         // should never happen, do nothing
1896                         break;
1897                 }
1898         }
1899
1900 #ifdef DEVEL_VERSION
1901         os << _(", Inset: ") << &cur.inset();
1902         os << _(", Paragraph: ") << cur.pit();
1903         os << _(", Id: ") << par.id();
1904         os << _(", Position: ") << cur.pos();
1905         // FIXME: Why is the check for par.size() needed?
1906         // We are called with cur.pos() == par.size() quite often.
1907         if (!par.empty() && cur.pos() < par.size()) {
1908                 // Force output of code point, not character
1909                 size_t const c = par.getChar(cur.pos());
1910                 os << _(", Char: 0x") << hex << c;
1911         }
1912         os << _(", Boundary: ") << cur.boundary();
1913 //      Row & row = cur.textRow();
1914 //      os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1915 #endif
1916         return os.str();
1917 }
1918
1919
1920 docstring Text::getPossibleLabel(Cursor const & cur) const
1921 {
1922         pit_type pit = cur.pit();
1923
1924         Layout const * layout = &(pars_[pit].layout());
1925
1926         docstring text;
1927         docstring par_text = pars_[pit].asString();
1928
1929         // The return string of math matrices might contain linebreaks
1930         par_text = subst(par_text, '\n', '-');
1931         int const numwords = 3;
1932         for (int i = 0; i < numwords; ++i) {
1933                 if (par_text.empty())
1934                         break;
1935                 docstring head;
1936                 par_text = split(par_text, head, ' ');
1937                 // Is it legal to use spaces in labels ?
1938                 if (i > 0)
1939                         text += '-';
1940                 text += head;
1941         }
1942
1943         // Make sure it isn't too long
1944         unsigned int const max_label_length = 32;
1945         if (text.size() > max_label_length)
1946                 text.resize(max_label_length);
1947
1948         // Will contain the label prefix.
1949         docstring name;
1950
1951         // For section, subsection, etc...
1952         if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1953                 Layout const * layout2 = &(pars_[pit - 1].layout());
1954                 if (layout2->latextype != LATEX_PARAGRAPH) {
1955                         --pit;
1956                         layout = layout2;
1957                 }
1958         }
1959         if (layout->latextype != LATEX_PARAGRAPH)
1960                 name = layout->refprefix;
1961
1962         // For captions, we just take the caption type
1963         Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
1964         if (caption_inset) {
1965                 string const & ftype = static_cast<InsetCaption *>(caption_inset)->floattype();
1966                 FloatList const & fl = cur.buffer()->params().documentClass().floats();
1967                 if (fl.typeExist(ftype)) {
1968                         Floating const & flt = fl.getType(ftype);
1969                         name = from_utf8(flt.refPrefix());
1970                 }
1971                 if (name.empty())
1972                         name = from_utf8(ftype.substr(0,3));
1973         }
1974
1975         // If none of the above worked, see if the inset knows.
1976         if (name.empty()) {
1977                 InsetLayout const & il = cur.inset().getLayout();
1978                 name = il.refprefix();
1979         }
1980
1981         if (!name.empty())
1982                 text = name + ':' + text;
1983
1984         // We need a unique label
1985         docstring label = text;
1986         int i = 1;
1987         while (cur.buffer()->insetLabel(label)) {
1988                         label = text + '-' + convert<docstring>(i);
1989                         ++i;
1990                 }
1991
1992         return label;
1993 }
1994
1995
1996 docstring Text::asString(int options) const
1997 {
1998         return asString(0, pars_.size(), options);
1999 }
2000
2001
2002 docstring Text::asString(pit_type beg, pit_type end, int options) const
2003 {
2004         size_t i = size_t(beg);
2005         docstring str = pars_[i].asString(options);
2006         for (++i; i != size_t(end); ++i) {
2007                 str += '\n';
2008                 str += pars_[i].asString(options);
2009         }
2010         return str;
2011 }
2012
2013
2014 void Text::forOutliner(docstring & os, size_t maxlen, bool shorten) const
2015 {
2016         if (maxlen == 0)
2017                 maxlen = std::numeric_limits<std::size_t>::max();
2018         else
2019                 LASSERT(maxlen >= 8, maxlen = TOC_ENTRY_LENGTH);
2020         for (size_t i = 0; i != pars_.size() && os.length() < maxlen; ++i)
2021                 pars_[i].forOutliner(os, maxlen);
2022         if (shorten && os.length() >= maxlen)
2023                 os = os.substr(0, maxlen - 3) + from_ascii("...");
2024 }
2025
2026
2027 void Text::charsTranspose(Cursor & cur)
2028 {
2029         LBUFERR(this == cur.text());
2030
2031         pos_type pos = cur.pos();
2032
2033         // If cursor is at beginning or end of paragraph, do nothing.
2034         if (pos == cur.lastpos() || pos == 0)
2035                 return;
2036
2037         Paragraph & par = cur.paragraph();
2038
2039         // Get the positions of the characters to be transposed.
2040         pos_type pos1 = pos - 1;
2041         pos_type pos2 = pos;
2042
2043         // In change tracking mode, ignore deleted characters.
2044         while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2045                 ++pos2;
2046         if (pos2 == cur.lastpos())
2047                 return;
2048
2049         while (pos1 >= 0 && par.isDeleted(pos1))
2050                 --pos1;
2051         if (pos1 < 0)
2052                 return;
2053
2054         // Don't do anything if one of the "characters" is not regular text.
2055         if (par.isInset(pos1) || par.isInset(pos2))
2056                 return;
2057
2058         // Store the characters to be transposed (including font information).
2059         char_type const char1 = par.getChar(pos1);
2060         Font const font1 =
2061                 par.getFontSettings(cur.buffer()->params(), pos1);
2062
2063         char_type const char2 = par.getChar(pos2);
2064         Font const font2 =
2065                 par.getFontSettings(cur.buffer()->params(), pos2);
2066
2067         // And finally, we are ready to perform the transposition.
2068         // Track the changes if Change Tracking is enabled.
2069         bool const trackChanges = cur.buffer()->params().track_changes;
2070
2071         cur.recordUndo();
2072
2073         par.eraseChar(pos2, trackChanges);
2074         par.eraseChar(pos1, trackChanges);
2075         par.insertChar(pos1, char2, font2, trackChanges);
2076         par.insertChar(pos2, char1, font1, trackChanges);
2077
2078         cur.checkBufferStructure();
2079
2080         // After the transposition, move cursor to after the transposition.
2081         setCursor(cur, cur.pit(), pos2);
2082         cur.forwardPos();
2083 }
2084
2085
2086 DocIterator Text::macrocontextPosition() const
2087 {
2088         return macrocontext_position_;
2089 }
2090
2091
2092 void Text::setMacrocontextPosition(DocIterator const & pos)
2093 {
2094         macrocontext_position_ = pos;
2095 }
2096
2097
2098 docstring Text::previousWord(CursorSlice const & sl) const
2099 {
2100         CursorSlice from = sl;
2101         CursorSlice to = sl;
2102         getWord(from, to, PREVIOUS_WORD);
2103         if (sl == from || to == from)
2104                 return docstring();
2105
2106         Paragraph const & par = sl.paragraph();
2107         return par.asString(from.pos(), to.pos());
2108 }
2109
2110
2111 bool Text::completionSupported(Cursor const & cur) const
2112 {
2113         Paragraph const & par = cur.paragraph();
2114         return cur.pos() > 0
2115                 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2116                 && !par.isWordSeparator(cur.pos() - 1);
2117 }
2118
2119
2120 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2121 {
2122         WordList const * list = theWordList(cur.getFont().language()->lang());
2123         return new TextCompletionList(cur, list);
2124 }
2125
2126
2127 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2128 {
2129         LBUFERR(cur.bv().cursor() == cur);
2130         cur.insert(s);
2131         cur.bv().cursor() = cur;
2132         if (!(cur.result().screenUpdate() & Update::Force))
2133                 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2134         return true;
2135 }
2136
2137
2138 docstring Text::completionPrefix(Cursor const & cur) const
2139 {
2140         return previousWord(cur.top());
2141 }
2142
2143 } // namespace lyx