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