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