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