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