3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Dov Feldstern
9 * \author Jean-Marc Lasgouttes
11 * \author André Pönitz
12 * \author Stefan Schimanski
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
25 #include "BufferParams.h"
26 #include "BufferView.h"
28 #include "CompletionList.h"
30 #include "CursorSlice.h"
31 #include "CutAndPaste.h"
33 #include "ErrorList.h"
36 #include "FuncRequest.h"
42 #include "Paragraph.h"
43 #include "ParagraphParameters.h"
44 #include "TextClass.h"
45 #include "TextMetrics.h"
48 #include "insets/Inset.h"
49 #include "insets/InsetText.h"
50 #include "insets/InsetCaption.h"
51 #include "insets/InsetIPAMacro.h"
52 #include "insets/InsetSpecialChar.h"
53 #include "insets/InsetTabular.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/docstring.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
62 #include "support/lyxtime.h"
63 #include "support/textutils.h"
64 #include "support/unique_ptr.h"
70 using namespace lyx::support;
74 using cap::cutSelection;
75 using cap::pasteParagraphList;
77 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
78 Paragraph & toPar, pos_type toPos, BufferParams const & params)
80 // Note: moveItem() does not honour change tracking!
81 // Therefore, it should only be used for breaking and merging paragraphs
83 // We need a copy here because the character at fromPos is going to be erased.
84 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
85 Change const tmpChange = fromPar.lookupChange(fromPos);
87 if (Inset * tmpInset = fromPar.getInset(fromPos)) {
88 fromPar.releaseInset(fromPos);
89 // The inset is not in fromPar any more.
90 if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
97 char_type const tmpChar = fromPar.getChar(fromPos);
98 fromPar.eraseChar(fromPos, false);
99 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
104 void breakParagraphConservative(BufferParams const & bparams,
105 ParagraphList & pars, pit_type pit, pos_type pos)
107 // create a new paragraph
108 Paragraph & tmp = *pars.insert(pars.iterator_at(pit + 1), Paragraph());
109 Paragraph & par = pars[pit];
111 tmp.setInsetOwner(&par.inInset());
112 tmp.makeSameLayout(par);
114 LASSERT(pos <= par.size(), return);
116 if (pos < par.size()) {
117 // move everything behind the break position to the new paragraph
118 pos_type pos_end = par.size() - 1;
120 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
121 if (moveItem(par, pos, tmp, j, bparams)) {
125 // Move over the end-of-par change information
126 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
127 par.setChange(par.size(), Change(bparams.track_changes ?
128 Change::INSERTED : Change::UNCHANGED));
133 void mergeParagraph(BufferParams const & bparams,
134 ParagraphList & pars, pit_type par_offset)
136 Paragraph & next = pars[par_offset + 1];
137 Paragraph & par = pars[par_offset];
139 pos_type pos_end = next.size() - 1;
140 pos_type pos_insert = par.size();
142 // the imaginary end-of-paragraph character (at par.size()) has to be
143 // marked as unmodified. Otherwise, its change is adopted by the first
144 // character of the next paragraph.
145 if (par.isChanged(par.size())) {
146 LYXERR(Debug::CHANGES,
147 "merging par with inserted/deleted end-of-par character");
148 par.setChange(par.size(), Change(Change::UNCHANGED));
151 Change change = next.lookupChange(next.size());
153 // move the content of the second paragraph to the end of the first one
154 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
155 if (moveItem(next, 0, par, j, bparams)) {
160 // move the change of the end-of-paragraph character
161 par.setChange(par.size(), change);
163 pars.erase(pars.iterator_at(par_offset + 1));
167 Text::Text(InsetText * owner, bool use_default_layout)
170 pars_.push_back(Paragraph());
171 Paragraph & par = pars_.back();
172 par.setInsetOwner(owner);
173 DocumentClass const & dc = owner->buffer().params().documentClass();
174 if (use_default_layout)
175 par.setDefaultLayout(dc);
177 par.setPlainLayout(dc);
181 Text::Text(InsetText * owner, Text const & text)
182 : owner_(owner), pars_(text.pars_)
184 for (auto & p : pars_)
185 p.setInsetOwner(owner);
189 pit_type Text::depthHook(pit_type pit, depth_type depth) const
191 pit_type newpit = pit;
196 while (newpit != 0 && pars_[newpit].getDepth() > depth)
199 if (pars_[newpit].getDepth() > depth)
206 pit_type Text::outerHook(pit_type par_offset) const
208 Paragraph const & par = pars_[par_offset];
210 if (par.getDepth() == 0)
212 return depthHook(par_offset, depth_type(par.getDepth() - 1));
216 bool Text::isFirstInSequence(pit_type par_offset) const
218 Paragraph const & par = pars_[par_offset];
220 pit_type dhook_offset = depthHook(par_offset, par.getDepth());
222 if (dhook_offset == par_offset)
225 Paragraph const & dhook = pars_[dhook_offset];
227 return dhook.layout() != par.layout()
228 || dhook.getDepth() != par.getDepth();
232 pit_type Text::lastInSequence(pit_type pit) const
234 depth_type const depth = pars_[pit].getDepth();
235 pit_type newpit = pit;
237 while (size_t(newpit + 1) < pars_.size() &&
238 (pars_[newpit + 1].getDepth() > depth ||
239 (pars_[newpit + 1].getDepth() == depth &&
240 pars_[newpit + 1].layout() == pars_[pit].layout())))
247 int Text::getTocLevel(pit_type par_offset) const
249 Paragraph const & par = pars_[par_offset];
251 if (par.layout().isEnvironment() && !isFirstInSequence(par_offset))
252 return Layout::NOT_IN_TOC;
254 return par.layout().toclevel;
258 Font const Text::outerFont(pit_type par_offset) const
260 depth_type par_depth = pars_[par_offset].getDepth();
261 FontInfo tmpfont = inherit_font;
262 depth_type prev_par_depth = 0;
263 // Resolve against environment font information
264 while (par_offset != pit_type(pars_.size())
265 && par_depth != prev_par_depth
267 && !tmpfont.resolved()) {
268 prev_par_depth = par_depth;
269 par_offset = outerHook(par_offset);
270 if (par_offset != pit_type(pars_.size())) {
271 tmpfont.realize(pars_[par_offset].layout().font);
272 par_depth = pars_[par_offset].getDepth();
276 return Font(tmpfont);
280 int Text::getEndLabel(pit_type p) const
283 depth_type par_depth = pars_[p].getDepth();
284 while (pit != pit_type(pars_.size())) {
285 Layout const & layout = pars_[pit].layout();
286 int const endlabeltype = layout.endlabeltype;
288 if (endlabeltype != END_LABEL_NO_LABEL) {
289 if (p + 1 == pit_type(pars_.size()))
292 depth_type const next_depth =
293 pars_[p + 1].getDepth();
294 if (par_depth > next_depth ||
295 (par_depth == next_depth && layout != pars_[p + 1].layout()))
301 pit = outerHook(pit);
302 if (pit != pit_type(pars_.size()))
303 par_depth = pars_[pit].getDepth();
305 return END_LABEL_NO_LABEL;
309 static void acceptOrRejectChanges(ParagraphList & pars,
310 BufferParams const & bparams, Text::ChangeOp op)
312 pit_type pars_size = static_cast<pit_type>(pars.size());
314 // first, accept or reject changes within each individual
315 // paragraph (do not consider end-of-par)
316 for (pit_type pit = 0; pit < pars_size; ++pit) {
317 // prevent assertion failure
318 if (!pars[pit].empty()) {
319 if (op == Text::ACCEPT)
320 pars[pit].acceptChanges(0, pars[pit].size());
322 pars[pit].rejectChanges(0, pars[pit].size());
326 // next, accept or reject imaginary end-of-par characters
327 for (pit_type pit = 0; pit < pars_size; ++pit) {
328 pos_type pos = pars[pit].size();
329 if (pars[pit].isChanged(pos)) {
330 // keep the end-of-par char if it is inserted and accepted
331 // or when it is deleted and rejected.
332 if (pars[pit].isInserted(pos) == (op == Text::ACCEPT)) {
333 pars[pit].setChange(pos, Change(Change::UNCHANGED));
335 if (pit == pars_size - 1) {
336 // we cannot remove a par break at the end of the last
337 // paragraph; instead, we mark it unchanged
338 pars[pit].setChange(pos, Change(Change::UNCHANGED));
340 mergeParagraph(bparams, pars, pit);
350 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
352 acceptOrRejectChanges(pars, bparams, Text::ACCEPT);
356 void rejectChanges(ParagraphList & pars, BufferParams const & bparams)
358 acceptOrRejectChanges(pars, bparams, Text::REJECT);
362 InsetText const & Text::inset() const
369 void Text::readParToken(Paragraph & par, Lexer & lex,
370 string const & token, Font & font, Change & change, ErrorList & errorList)
372 Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
373 BufferParams & bp = buf->params();
375 if (token[0] != '\\') {
376 docstring dstr = lex.getDocString();
377 par.appendString(dstr, font, change);
379 } else if (token == "\\begin_layout") {
381 docstring layoutname = lex.getDocString();
383 font = Font(inherit_font, bp.language);
384 change = Change(Change::UNCHANGED);
386 DocumentClass const & tclass = bp.documentClass();
388 if (layoutname.empty())
389 layoutname = tclass.defaultLayoutName();
391 if (owner_->forcePlainLayout()) {
392 // in this case only the empty layout is allowed
393 layoutname = tclass.plainLayoutName();
394 } else if (par.usePlainLayout()) {
395 // in this case, default layout maps to empty layout
396 if (layoutname == tclass.defaultLayoutName())
397 layoutname = tclass.plainLayoutName();
399 // otherwise, the empty layout maps to the default
400 if (layoutname == tclass.plainLayoutName())
401 layoutname = tclass.defaultLayoutName();
404 // When we apply an unknown layout to a document, we add this layout to the textclass
405 // of this document. For example, when you apply class article to a beamer document,
406 // all unknown layouts such as frame will be added to document class article so that
407 // these layouts can keep their original names.
408 bool const added_one = tclass.addLayoutIfNeeded(layoutname);
411 docstring const s = bformat(_("Layout `%1$s' was not found."), layoutname);
412 errorList.push_back(ErrorItem(_("Layout Not Found"), s,
413 {par.id(), 0}, {par.id(), -1}));
416 par.setLayout(bp.documentClass()[layoutname]);
418 // Test whether the layout is obsolete.
419 Layout const & layout = par.layout();
420 if (!layout.obsoleted_by().empty())
421 par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
423 par.params().read(lex);
425 } else if (token == "\\end_layout") {
426 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
427 << "Missing \\begin_layout ?");
428 } else if (token == "\\end_inset") {
429 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
430 << "Missing \\begin_inset ?");
431 } else if (token == "\\begin_inset") {
432 Inset * inset = readInset(lex, buf);
434 par.insertInset(par.size(), inset, font, change);
437 docstring line = lex.getDocString();
438 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
439 {par.id(), 0}, {par.id(), -1}));
441 } else if (token == "\\family") {
443 setLyXFamily(lex.getString(), font.fontInfo());
444 } else if (token == "\\series") {
446 setLyXSeries(lex.getString(), font.fontInfo());
447 } else if (token == "\\shape") {
449 setLyXShape(lex.getString(), font.fontInfo());
450 } else if (token == "\\size") {
452 setLyXSize(lex.getString(), font.fontInfo());
453 } else if (token == "\\lang") {
455 string const tok = lex.getString();
456 Language const * lang = languages.getLanguage(tok);
458 font.setLanguage(lang);
460 font.setLanguage(bp.language);
461 lex.printError("Unknown language `$$Token'");
463 } else if (token == "\\numeric") {
465 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
466 } else if (token == "\\nospellcheck") {
468 font.fontInfo().setNoSpellcheck(setLyXMisc(lex.getString()));
469 } else if (token == "\\emph") {
471 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
472 } else if (token == "\\bar") {
474 string const tok = lex.getString();
477 font.fontInfo().setUnderbar(FONT_ON);
478 else if (tok == "no")
479 font.fontInfo().setUnderbar(FONT_OFF);
480 else if (tok == "default")
481 font.fontInfo().setUnderbar(FONT_INHERIT);
483 lex.printError("Unknown bar font flag "
485 } else if (token == "\\strikeout") {
487 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
488 } else if (token == "\\xout") {
490 font.fontInfo().setXout(setLyXMisc(lex.getString()));
491 } else if (token == "\\uuline") {
493 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
494 } else if (token == "\\uwave") {
496 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
497 } else if (token == "\\noun") {
499 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
500 } else if (token == "\\color") {
502 setLyXColor(lex.getString(), font.fontInfo());
503 } else if (token == "\\SpecialChar" ||
504 (token == "\\SpecialCharNoPassThru" &&
505 !par.layout().pass_thru && !inset().isPassThru())) {
506 auto inset = make_unique<InsetSpecialChar>();
508 inset->setBuffer(*buf);
509 par.insertInset(par.size(), inset.release(), font, change);
510 } else if (token == "\\SpecialCharNoPassThru") {
512 docstring const s = ltrim(lex.getDocString(), "\\");
513 par.insert(par.size(), s, font, change);
514 } else if (token == "\\IPAChar") {
515 auto inset = make_unique<InsetIPAChar>();
517 inset->setBuffer(*buf);
518 par.insertInset(par.size(), inset.release(), font, change);
519 } else if (token == "\\twohyphens" || token == "\\threehyphens") {
520 // Ideally, this should be done by lyx2lyx, but lyx2lyx does not know the
521 // running font and does not know anything about layouts (and CopyStyle).
522 Layout const & layout(par.layout());
523 FontInfo info = font.fontInfo();
524 info.realize(layout.resfont);
525 if (layout.pass_thru || inset().isPassThru() ||
526 info.family() == TYPEWRITER_FAMILY) {
527 if (token == "\\twohyphens")
528 par.insert(par.size(), from_ascii("--"), font, change);
530 par.insert(par.size(), from_ascii("---"), font, change);
532 if (token == "\\twohyphens")
533 par.insertChar(par.size(), 0x2013, font, change);
535 par.insertChar(par.size(), 0x2014, font, change);
537 } else if (token == "\\backslash") {
538 par.appendChar('\\', font, change);
539 } else if (token == "\\LyXTable") {
540 auto inset = make_unique<InsetTabular>(buf);
542 par.insertInset(par.size(), inset.release(), font, change);
543 } else if (token == "\\change_unchanged") {
544 change = Change(Change::UNCHANGED);
545 } else if (token == "\\change_inserted" || token == "\\change_deleted") {
547 istringstream is(lex.getString());
551 BufferParams::AuthorMap const & am = bp.author_map_;
552 if (am.find(aid) == am.end()) {
553 errorList.push_back(ErrorItem(
554 _("Change tracking author index missing"),
555 bformat(_("A change tracking author information for index "
556 "%1$d is missing. This can happen after a wrong "
557 "merge by a version control system. In this case, "
558 "either fix the merge, or have this information "
559 "missing until the corresponding tracked changes "
560 "are merged or this user edits the file again.\n"),
562 {par.id(), par.size()}, {par.id(), par.size() + 1}));
563 bp.addAuthor(Author(aid));
565 if (token == "\\change_inserted")
566 change = Change(Change::INSERTED, am.find(aid)->second, ct);
568 change = Change(Change::DELETED, am.find(aid)->second, ct);
571 errorList.push_back(ErrorItem(_("Unknown token"),
572 bformat(_("Unknown token: %1$s %2$s\n"),
575 {par.id(), 0}, {par.id(), -1}));
580 void Text::readParagraph(Paragraph & par, Lexer & lex,
581 ErrorList & errorList)
584 string token = lex.getString();
586 Change change(Change::UNCHANGED);
589 readParToken(par, lex, token, font, change, errorList);
592 token = lex.getString();
597 if (token == "\\end_layout") {
598 //Ok, paragraph finished
602 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
603 if (token == "\\begin_layout" || token == "\\end_document"
604 || token == "\\end_inset" || token == "\\begin_deeper"
605 || token == "\\end_deeper") {
606 lex.pushToken(token);
607 lyxerr << "Paragraph ended in line "
608 << lex.lineNumber() << "\n"
609 << "Missing \\end_layout.\n";
613 // Final change goes to paragraph break:
614 if (inset().allowMultiPar())
615 par.setChange(par.size(), change);
617 // Initialize begin_of_body_ on load; redoParagraph maintains
618 par.setBeginOfBody();
620 // mark paragraph for spell checking on load
621 // par.requestSpellCheck();
625 class TextCompletionList : public CompletionList
629 TextCompletionList(Cursor const & cur, WordList const & list)
630 : buffer_(cur.buffer()), list_(list)
633 virtual ~TextCompletionList() {}
636 bool sorted() const override { return true; }
638 size_t size() const override
643 docstring const & data(size_t idx) const override
645 return list_.word(idx);
650 Buffer const * buffer_;
652 WordList const & list_;
656 bool Text::empty() const
658 return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
659 // FIXME: Should we consider the labeled type as empty too?
660 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
664 double Text::spacing(Paragraph const & par) const
666 if (par.params().spacing().isDefault())
667 return owner_->buffer().params().spacing().getValue();
668 return par.params().spacing().getValue();
673 * This breaks a paragraph at the specified position.
674 * The new paragraph will:
675 * - Decrease depth by one (or change layout to default layout) when
676 * keep_layout == false
677 * - keep current depth and layout when keep_layout == true
679 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
682 BufferParams const & bparams = text.inset().buffer().params();
683 ParagraphList & pars = text.paragraphs();
684 // create a new paragraph, and insert into the list
685 ParagraphList::iterator tmp =
686 pars.insert(pars.iterator_at(par_offset + 1), Paragraph());
688 Paragraph & par = pars[par_offset];
690 // remember to set the inset_owner
691 tmp->setInsetOwner(&par.inInset());
692 // without doing that we get a crash when typing <Return> at the
693 // end of a paragraph
694 tmp->setPlainOrDefaultLayout(bparams.documentClass());
697 tmp->setLayout(par.layout());
698 tmp->setLabelWidthString(par.params().labelWidthString());
699 tmp->params().depth(par.params().depth());
700 } else if (par.params().depth() > 0) {
701 Paragraph const & hook = pars[text.outerHook(par_offset)];
702 tmp->setLayout(hook.layout());
703 // not sure the line below is useful
704 tmp->setLabelWidthString(par.params().labelWidthString());
705 tmp->params().depth(hook.params().depth());
708 bool const isempty = (par.allowEmpty() && par.empty());
710 if (!isempty && (par.size() > pos || par.empty())) {
711 tmp->setLayout(par.layout());
712 tmp->params().align(par.params().align());
713 tmp->setLabelWidthString(par.params().labelWidthString());
715 tmp->params().depth(par.params().depth());
716 tmp->params().noindent(par.params().noindent());
717 tmp->params().spacing(par.params().spacing());
719 // move everything behind the break position
720 // to the new paragraph
722 /* Note: if !keepempty, empty() == true, then we reach
723 * here with size() == 0. So pos_end becomes - 1. This
724 * doesn't cause problems because both loops below
725 * enforce pos <= pos_end and 0 <= pos
727 pos_type pos_end = par.size() - 1;
729 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
730 if (moveItem(par, pos, *tmp, j, bparams)) {
736 // Move over the end-of-par change information
737 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
738 par.setChange(par.size(), Change(bparams.track_changes ?
739 Change::INSERTED : Change::UNCHANGED));
742 // Make sure that we keep the language when
743 // breaking paragraph.
745 Font changed = tmp->getFirstFontSettings(bparams);
746 Font const & old = par.getFontSettings(bparams, par.size());
747 changed.setLanguage(old.language());
748 tmp->setFont(0, changed);
755 bool const soa = par.params().startOfAppendix();
756 par.params().clear();
757 // do not lose start of appendix marker (bug 4212)
758 par.params().startOfAppendix(soa);
759 par.setPlainOrDefaultLayout(bparams.documentClass());
763 par.setLayout(tmp->layout());
764 par.setLabelWidthString(tmp->params().labelWidthString());
765 par.params().depth(tmp->params().depth());
770 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
772 LBUFERR(this == cur.text());
774 Paragraph & cpar = cur.paragraph();
775 pit_type cpit = cur.pit();
777 DocumentClass const & tclass = cur.buffer()->params().documentClass();
778 Layout const & layout = cpar.layout();
780 if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
781 if (changeDepthAllowed(cur, DEC_DEPTH)) {
782 changeDepth(cur, DEC_DEPTH);
783 pit_type const prev = depthHook(cpit, cpar.getDepth());
784 docstring const & lay = pars_[prev].layout().name();
785 if (lay != layout.name())
788 docstring const & lay = cur.paragraph().usePlainLayout()
789 ? tclass.plainLayoutName() : tclass.defaultLayoutName();
790 if (lay != layout.name())
798 // Always break behind a space
799 // It is better to erase the space (Dekel)
800 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
801 cpar.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
803 // What should the layout for the new paragraph be?
804 bool keep_layout = layout.isEnvironment()
805 || (layout.isParagraph() && layout.parbreak_is_newline);
807 keep_layout = !keep_layout;
809 // We need to remember this before we break the paragraph, because
810 // that invalidates the layout variable
811 bool sensitive = layout.labeltype == LABEL_SENSITIVE;
813 // we need to set this before we insert the paragraph.
814 bool const isempty = cpar.allowEmpty() && cpar.empty();
816 lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
818 // After this, neither paragraph contains any rows!
821 pit_type next_par = cpit + 1;
823 // well this is the caption hack since one caption is really enough
826 // set to standard-layout
827 //FIXME Check if this should be plainLayout() in some cases
828 pars_[cpit].applyLayout(tclass.defaultLayout());
830 // set to standard-layout
831 //FIXME Check if this should be plainLayout() in some cases
832 pars_[next_par].applyLayout(tclass.defaultLayout());
835 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
836 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().track_changes))
837 break; // the character couldn't be deleted physically due to change tracking
840 // A singlePar update is not enough in this case.
841 cur.screenUpdateFlags(Update::Force);
842 cur.forceBufferUpdate();
844 // This check is necessary. Otherwise the new empty paragraph will
845 // be deleted automatically. And it is more friendly for the user!
846 if (cur.pos() != 0 || isempty)
847 setCursor(cur, cur.pit() + 1, 0);
849 setCursor(cur, cur.pit(), 0);
853 // needed to insert the selection
854 void Text::insertStringAsLines(Cursor & cur, docstring const & str,
857 BufferParams const & bparams = owner_->buffer().params();
858 pit_type pit = cur.pit();
859 pos_type pos = cur.pos();
861 // The special chars we handle
862 map<wchar_t, InsetSpecialChar::Kind> specialchars;
863 specialchars[0x200c] = InsetSpecialChar::LIGATURE_BREAK;
864 specialchars[0x200b] = InsetSpecialChar::ALLOWBREAK;
865 specialchars[0x2026] = InsetSpecialChar::LDOTS;
866 specialchars[0x2011] = InsetSpecialChar::NOBREAKDASH;
868 // insert the string, don't insert doublespace
869 bool space_inserted = true;
870 for (auto const & ch : str) {
871 Paragraph & par = pars_[pit];
873 if (inset().allowMultiPar() && (!par.empty() || par.allowEmpty())) {
874 lyx::breakParagraph(*this, pit, pos,
875 par.layout().isEnvironment());
878 space_inserted = true;
882 // do not insert consecutive spaces if !free_spacing
883 } else if ((ch == ' ' || ch == '\t') &&
884 space_inserted && !par.isFreeSpacing()) {
886 } else if (ch == '\t') {
887 if (!par.isFreeSpacing()) {
888 // tabs are like spaces here
889 par.insertChar(pos, ' ', font, bparams.track_changes);
891 space_inserted = true;
893 par.insertChar(pos, ch, font, bparams.track_changes);
895 space_inserted = true;
897 } else if (specialchars.find(ch) != specialchars.end()) {
898 par.insertInset(pos, new InsetSpecialChar(specialchars.find(ch)->second),
899 font, bparams.track_changes ?
900 Change(Change::INSERTED)
901 : Change(Change::UNCHANGED));
903 space_inserted = false;
904 } else if (!isPrintable(ch)) {
905 // Ignore (other) unprintables
908 // just insert the character
909 par.insertChar(pos, ch, font, bparams.track_changes);
911 space_inserted = (ch == ' ');
914 setCursor(cur, pit, pos);
918 // turn double CR to single CR, others are converted into one
919 // blank. Then insertStringAsLines is called
920 void Text::insertStringAsParagraphs(Cursor & cur, docstring const & str,
923 docstring linestr = str;
924 bool newline_inserted = false;
926 for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
927 if (linestr[i] == '\n') {
928 if (newline_inserted) {
929 // we know that \r will be ignored by
930 // insertStringAsLines. Of course, it is a dirty
931 // trick, but it works...
932 linestr[i - 1] = '\r';
936 newline_inserted = true;
938 } else if (isPrintable(linestr[i])) {
939 newline_inserted = false;
942 insertStringAsLines(cur, linestr, font);
948 bool canInsertChar(Cursor const & cur, char_type c)
950 Paragraph const & par = cur.paragraph();
951 // If not in free spacing mode, check if there will be two blanks together or a blank at
952 // the beginning of a paragraph.
953 if (!par.isFreeSpacing() && isLineSeparatorChar(c)) {
954 if (cur.pos() == 0) {
956 "You cannot insert a space at the "
957 "beginning of a paragraph. Please read the Tutorial."));
960 // If something is wrong, ignore this character.
961 LASSERT(cur.pos() > 0, return false);
962 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
963 && !par.isDeleted(cur.pos() - 1)) {
965 "You cannot type two spaces this way. "
966 "Please read the Tutorial."));
971 // Prevent to insert uncodable characters in verbatim and ERT.
972 // The encoding is inherited from the context here.
973 if (par.isPassThru() && cur.getEncoding()) {
974 Encoding const * e = cur.getEncoding();
975 if (!e->encodable(c)) {
976 cur.message(_("Character is uncodable in this verbatim context."));
986 // insert a character, moves all the following breaks in the
987 // same Paragraph one to the right and make a rebreak
988 void Text::insertChar(Cursor & cur, char_type c)
990 LBUFERR(this == cur.text());
992 if (!canInsertChar(cur,c))
995 cur.recordUndo(INSERT_UNDO);
997 TextMetrics const & tm = cur.bv().textMetrics(this);
998 Buffer const & buffer = *cur.buffer();
999 Paragraph & par = cur.paragraph();
1000 // try to remove this
1001 pit_type const pit = cur.pit();
1003 if (lyxrc.auto_number) {
1004 static docstring const number_operators = from_ascii("+-/*");
1005 static docstring const number_unary_operators = from_ascii("+-");
1007 // Common Number Separators: comma, dot etc.
1008 // European Number Terminators: percent, permille, degree, euro etc.
1009 if (cur.current_font.fontInfo().number() == FONT_ON) {
1010 if (!isDigitASCII(c) && !contains(number_operators, c) &&
1011 !(isCommonNumberSeparator(c) &&
1013 cur.pos() != cur.lastpos() &&
1014 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
1015 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON) &&
1016 !(isEuropeanNumberTerminator(c) &&
1018 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
1019 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
1021 number(cur); // Set current_font.number to OFF
1022 } else if (isDigitASCII(c) &&
1023 cur.real_current_font.isVisibleRightToLeft()) {
1024 number(cur); // Set current_font.number to ON
1026 if (cur.pos() != 0) {
1027 char_type const ch = par.getChar(cur.pos() - 1);
1028 if (contains(number_unary_operators, ch) &&
1030 || par.isSeparator(cur.pos() - 2)
1031 || par.isEnvSeparator(cur.pos() - 2)
1032 || par.isNewline(cur.pos() - 2))
1034 setCharFont(pit, cur.pos() - 1, cur.current_font,
1036 } else if (isCommonNumberSeparator(ch)
1038 && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
1039 setCharFont(pit, cur.pos() - 1, cur.current_font,
1046 // In Bidi text, we want spaces to be treated in a special way: spaces
1047 // which are between words in different languages should get the
1048 // paragraph's language; otherwise, spaces should keep the language
1049 // they were originally typed in. This is only in effect while typing;
1050 // after the text is already typed in, the user can always go back and
1051 // explicitly set the language of a space as desired. But 99.9% of the
1052 // time, what we're doing here is what the user actually meant.
1054 // The following cases are the ones in which the language of the space
1055 // should be changed to match that of the containing paragraph. In the
1056 // depictions, lowercase is LTR, uppercase is RTL, underscore (_)
1057 // represents a space, pipe (|) represents the cursor position (so the
1058 // character before it is the one just typed in). The different cases
1059 // are depicted logically (not visually), from left to right:
1064 // Theoretically, there are other situations that we should, perhaps, deal
1065 // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any
1066 // point (to understand why, just try to create this situation...).
1068 if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
1069 // get font in front and behind the space in question. But do NOT
1070 // use getFont(cur.pos()) because the character c is not inserted yet
1071 Font const pre_space_font = tm.displayFont(cur.pit(), cur.pos() - 2);
1072 Font const & post_space_font = cur.real_current_font;
1073 bool pre_space_rtl = pre_space_font.isVisibleRightToLeft();
1074 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
1076 if (pre_space_rtl != post_space_rtl) {
1077 // Set the space's language to match the language of the
1078 // adjacent character whose direction is the paragraph's
1079 // direction; don't touch other properties of the font
1080 Language const * lang =
1081 (pre_space_rtl == par.isRTL(buffer.params())) ?
1082 pre_space_font.language() : post_space_font.language();
1084 Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
1085 space_font.setLanguage(lang);
1086 par.setFont(cur.pos() - 1, space_font);
1090 pos_type pos = cur.pos();
1091 if (!cur.paragraph().isPassThru() && owner_->lyxCode() != IPA_CODE &&
1092 cur.real_current_font.fontInfo().family() != TYPEWRITER_FAMILY &&
1093 c == '-' && pos > 0) {
1094 if (par.getChar(pos - 1) == '-') {
1095 // convert "--" to endash
1096 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1099 } else if (par.getChar(pos - 1) == 0x2013) {
1100 // convert "---" to emdash
1101 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1107 par.insertChar(pos, c, cur.current_font,
1108 cur.buffer()->params().track_changes);
1109 cur.checkBufferStructure();
1111 // cur.screenUpdateFlags(Update::Force);
1112 bool boundary = cur.boundary()
1113 || tm.isRTLBoundary(cur.pit(), pos + 1);
1114 setCursor(cur, cur.pit(), pos + 1, false, boundary);
1119 void Text::charInserted(Cursor & cur)
1121 Paragraph & par = cur.paragraph();
1123 // register word if a non-letter was entered
1125 && !par.isWordSeparator(cur.pos() - 2)
1126 && par.isWordSeparator(cur.pos() - 1)) {
1127 // get the word in front of cursor
1128 LBUFERR(this == cur.text());
1134 // the cursor set functions have a special mechanism. When they
1135 // realize, that you left an empty paragraph, they will delete it.
1137 bool Text::cursorForwardOneWord(Cursor & cur)
1139 LBUFERR(this == cur.text());
1141 if (lyxrc.mac_like_cursor_movement) {
1142 DocIterator dit(cur);
1143 DocIterator prv(cur);
1144 bool inword = false;
1145 bool intext = dit.inTexted();
1146 while (!dit.atEnd()) {
1147 if (dit.inTexted()) { // no paragraphs in mathed
1148 Paragraph const & par = dit.paragraph();
1149 pos_type const pos = dit.pos();
1151 if (!par.isDeleted(pos)) {
1152 bool wordsep = par.isWordSeparator(pos);
1153 if (inword && wordsep)
1154 break; // stop at word end
1155 else if (!inword && !wordsep)
1159 } else if (intext) {
1160 // move to end of math
1161 while (!dit.inTexted() && !dit.atEnd()) dit.forwardPos();
1165 dit.forwardPosIgnoreCollapsed();
1167 if (dit.atEnd()) dit = prv;
1168 if (dit == cur) return false; // we didn't move
1171 // see comment above
1172 cur.bv().checkDepm(cur, orig);
1175 pos_type const lastpos = cur.lastpos();
1176 pit_type pit = cur.pit();
1177 pos_type pos = cur.pos();
1178 Paragraph const & par = cur.paragraph();
1180 // Paragraph boundary is a word boundary
1181 if (pos == lastpos || (pos + 1 == lastpos && par.isEnvSeparator(pos))) {
1182 if (pit != cur.lastpit())
1183 return setCursor(cur, pit + 1, 0);
1188 LASSERT(pos < lastpos, return false); // see above
1189 if (!par.isWordSeparator(pos))
1190 while (pos != lastpos && !par.isWordSeparator(pos))
1192 else if (par.isChar(pos))
1193 while (pos != lastpos && par.isChar(pos))
1195 else if (!par.isSpace(pos)) // non-char inset
1198 // Skip over white space
1199 while (pos != lastpos && par.isSpace(pos))
1202 // Don't skip a separator inset at the end of a paragraph
1203 if (pos == lastpos && pos && par.isEnvSeparator(pos - 1))
1206 return setCursor(cur, pit, pos);
1211 bool Text::cursorBackwardOneWord(Cursor & cur)
1213 LBUFERR(this == cur.text());
1215 if (lyxrc.mac_like_cursor_movement) {
1216 DocIterator dit(cur);
1217 bool inword = false;
1218 bool intext = dit.inTexted();
1219 while (!dit.atBegin()) {
1220 DocIterator prv(dit);
1221 dit.backwardPosIgnoreCollapsed();
1222 if (dit.inTexted()) { // no paragraphs in mathed
1223 Paragraph const & par = dit.paragraph();
1224 pos_type pos = dit.pos();
1226 if (!par.isDeleted(pos)) {
1227 bool wordsep = par.isWordSeparator(pos);
1228 if (inword && wordsep) {
1230 break; // stop at word begin
1231 } else if (!inword && !wordsep)
1235 } else if (intext) {
1236 // move to begin of math
1237 while (!dit.inTexted() && !dit.atBegin()) dit.backwardPos();
1241 if (dit == cur) return false; // we didn't move
1244 // see comment above cursorForwardOneWord
1245 cur.bv().checkDepm(cur, orig);
1248 Paragraph const & par = cur.paragraph();
1249 pit_type const pit = cur.pit();
1250 pos_type pos = cur.pos();
1252 // Paragraph boundary is a word boundary
1253 if (pos == 0 && pit != 0) {
1254 Paragraph & prevpar = getPar(pit - 1);
1255 pos = prevpar.size();
1256 // Don't stop after an environment separator
1257 if (pos && prevpar.isEnvSeparator(pos - 1))
1259 return setCursor(cur, pit - 1, pos);
1261 // Skip over white space
1262 while (pos != 0 && par.isSpace(pos - 1))
1265 if (pos != 0 && !par.isWordSeparator(pos - 1))
1266 while (pos != 0 && !par.isWordSeparator(pos - 1))
1268 else if (pos != 0 && par.isChar(pos - 1))
1269 while (pos != 0 && par.isChar(pos - 1))
1271 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1274 return setCursor(cur, pit, pos);
1279 bool Text::cursorVisLeftOneWord(Cursor & cur)
1281 LBUFERR(this == cur.text());
1283 pos_type left_pos, right_pos;
1285 Cursor temp_cur = cur;
1287 // always try to move at least once...
1288 while (temp_cur.posVisLeft(true /* skip_inset */)) {
1290 // collect some information about current cursor position
1291 temp_cur.getSurroundingPos(left_pos, right_pos);
1292 bool left_is_letter =
1293 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1294 bool right_is_letter =
1295 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1297 // if we're not at a letter/non-letter boundary, continue moving
1298 if (left_is_letter == right_is_letter)
1301 // we should stop when we have an LTR word on our right or an RTL word
1303 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1304 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1305 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1306 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1310 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1311 true, temp_cur.boundary());
1315 bool Text::cursorVisRightOneWord(Cursor & cur)
1317 LBUFERR(this == cur.text());
1319 pos_type left_pos, right_pos;
1321 Cursor temp_cur = cur;
1323 // always try to move at least once...
1324 while (temp_cur.posVisRight(true /* skip_inset */)) {
1326 // collect some information about current cursor position
1327 temp_cur.getSurroundingPos(left_pos, right_pos);
1328 bool left_is_letter =
1329 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1330 bool right_is_letter =
1331 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1333 // if we're not at a letter/non-letter boundary, continue moving
1334 if (left_is_letter == right_is_letter)
1337 // we should stop when we have an LTR word on our right or an RTL word
1339 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1340 temp_cur.buffer()->params(),
1341 left_pos).isRightToLeft())
1342 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1343 temp_cur.buffer()->params(),
1344 right_pos).isRightToLeft()))
1348 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1349 true, temp_cur.boundary());
1353 void Text::selectWord(Cursor & cur, word_location loc)
1355 LBUFERR(this == cur.text());
1356 CursorSlice from = cur.top();
1358 getWord(from, to, loc);
1359 if (cur.top() != from)
1360 setCursor(cur, from.pit(), from.pos());
1363 if (!cur.selection())
1365 setCursor(cur, to.pit(), to.pos());
1367 cur.setWordSelection(true);
1371 void Text::selectAll(Cursor & cur)
1373 LBUFERR(this == cur.text());
1374 if (cur.lastpos() == 0 && cur.lastpit() == 0)
1376 // If the cursor is at the beginning, make sure the cursor ends there
1377 if (cur.pit() == 0 && cur.pos() == 0) {
1378 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1380 setCursor(cur, 0, 0);
1382 setCursor(cur, 0, 0);
1384 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1390 // Select the word currently under the cursor when no
1391 // selection is currently set
1392 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1394 LBUFERR(this == cur.text());
1395 if (cur.selection())
1397 selectWord(cur, loc);
1398 return cur.selection();
1402 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1404 LBUFERR(this == cur.text());
1406 if (!cur.selection()) {
1407 if (!selectChange(cur))
1411 cur.recordUndoSelection();
1413 pit_type begPit = cur.selectionBegin().pit();
1414 pit_type endPit = cur.selectionEnd().pit();
1416 pos_type begPos = cur.selectionBegin().pos();
1417 pos_type endPos = cur.selectionEnd().pos();
1419 // keep selection info, because endPos becomes invalid after the first loop
1420 bool const endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1422 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1423 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1424 pos_type parSize = pars_[pit].size();
1426 // ignore empty paragraphs; otherwise, an assertion will fail for
1427 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1431 // do not consider first paragraph if the cursor starts at pos size()
1432 if (pit == begPit && begPos == parSize)
1435 // do not consider last paragraph if the cursor ends at pos 0
1436 if (pit == endPit && endPos == 0)
1437 break; // last iteration anyway
1439 pos_type const left = (pit == begPit ? begPos : 0);
1440 pos_type const right = (pit == endPit ? endPos : parSize);
1443 // there is no change here
1447 pars_[pit].acceptChanges(left, right);
1449 pars_[pit].rejectChanges(left, right);
1453 // next, accept/reject imaginary end-of-par characters
1455 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1456 pos_type pos = pars_[pit].size();
1458 // skip if the selection ends before the end-of-par
1459 if (pit == endPit && endsBeforeEndOfPar)
1460 break; // last iteration anyway
1462 // skip if this is not the last paragraph of the document
1463 // note: the user should be able to accept/reject the par break of the last par!
1464 if (pit == endPit && pit + 1 != int(pars_.size()))
1465 break; // last iteration anway
1468 if (pars_[pit].isInserted(pos)) {
1469 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1470 } else if (pars_[pit].isDeleted(pos)) {
1471 if (pit + 1 == int(pars_.size())) {
1472 // we cannot remove a par break at the end of the last paragraph;
1473 // instead, we mark it unchanged
1474 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1476 mergeParagraph(cur.buffer()->params(), pars_, pit);
1482 if (pars_[pit].isDeleted(pos)) {
1483 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1484 } else if (pars_[pit].isInserted(pos)) {
1485 if (pit + 1 == int(pars_.size())) {
1486 // we mark the par break at the end of the last paragraph unchanged
1487 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1489 mergeParagraph(cur.buffer()->params(), pars_, pit);
1497 // finally, invoke the DEPM
1498 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().track_changes);
1501 cur.clearSelection();
1502 setCursorIntern(cur, begPit, begPos);
1503 cur.screenUpdateFlags(Update::Force);
1504 cur.forceBufferUpdate();
1508 void Text::acceptChanges()
1510 BufferParams const & bparams = owner_->buffer().params();
1511 lyx::acceptChanges(pars_, bparams);
1512 deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.track_changes);
1516 void Text::rejectChanges()
1518 BufferParams const & bparams = owner_->buffer().params();
1519 pit_type pars_size = static_cast<pit_type>(pars_.size());
1521 // first, reject changes within each individual paragraph
1522 // (do not consider end-of-par)
1523 for (pit_type pit = 0; pit < pars_size; ++pit) {
1524 if (!pars_[pit].empty()) // prevent assertion failure
1525 pars_[pit].rejectChanges(0, pars_[pit].size());
1528 // next, reject imaginary end-of-par characters
1529 for (pit_type pit = 0; pit < pars_size; ++pit) {
1530 pos_type pos = pars_[pit].size();
1532 if (pars_[pit].isDeleted(pos)) {
1533 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1534 } else if (pars_[pit].isInserted(pos)) {
1535 if (pit == pars_size - 1) {
1536 // we mark the par break at the end of the last
1537 // paragraph unchanged
1538 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1540 mergeParagraph(bparams, pars_, pit);
1547 // finally, invoke the DEPM
1548 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.track_changes);
1552 void Text::deleteWordForward(Cursor & cur, bool const force)
1554 LBUFERR(this == cur.text());
1555 if (cur.lastpos() == 0)
1559 cur.selection(true);
1560 cursorForwardOneWord(cur);
1562 if (force || !cur.confirmDeletion()) {
1563 cutSelection(cur, false);
1564 cur.checkBufferStructure();
1570 void Text::deleteWordBackward(Cursor & cur, bool const force)
1572 LBUFERR(this == cur.text());
1573 if (cur.lastpos() == 0)
1574 cursorBackward(cur);
1577 cur.selection(true);
1578 cursorBackwardOneWord(cur);
1580 if (force || !cur.confirmDeletion()) {
1581 cutSelection(cur, false);
1582 cur.checkBufferStructure();
1588 // Kill to end of line.
1589 void Text::changeCase(Cursor & cur, TextCase action, bool partial)
1591 LBUFERR(this == cur.text());
1595 bool const gotsel = cur.selection();
1597 from = cur.selBegin();
1601 getWord(from, to, partial ? PARTIAL_WORD : WHOLE_WORD);
1602 cursorForwardOneWord(cur);
1605 cur.recordUndoSelection();
1607 pit_type begPit = from.pit();
1608 pit_type endPit = to.pit();
1610 pos_type begPos = from.pos();
1611 pos_type endPos = to.pos();
1613 pos_type right = 0; // needed after the for loop
1615 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1616 Paragraph & par = pars_[pit];
1617 pos_type const pos = (pit == begPit ? begPos : 0);
1618 right = (pit == endPit ? endPos : par.size());
1619 par.changeCase(cur.buffer()->params(), pos, right, action);
1622 // the selection may have changed due to logically-only deleted chars
1624 setCursor(cur, begPit, begPos);
1626 setCursor(cur, endPit, right);
1629 setCursor(cur, endPit, right);
1631 cur.checkBufferStructure();
1635 bool Text::handleBibitems(Cursor & cur)
1637 if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1643 BufferParams const & bufparams = cur.buffer()->params();
1644 Paragraph const & par = cur.paragraph();
1645 Cursor prevcur = cur;
1646 if (cur.pit() > 0) {
1648 prevcur.pos() = prevcur.lastpos();
1650 Paragraph const & prevpar = prevcur.paragraph();
1652 // if a bibitem is deleted, merge with previous paragraph
1653 // if this is a bibliography item as well
1654 if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1655 cur.recordUndo(prevcur.pit());
1656 mergeParagraph(bufparams, cur.text()->paragraphs(),
1658 cur.forceBufferUpdate();
1659 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1660 cur.screenUpdateFlags(Update::Force);
1664 // otherwise reset to default
1665 cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1670 bool Text::erase(Cursor & cur)
1672 LASSERT(this == cur.text(), return false);
1673 bool needsUpdate = false;
1674 Paragraph & par = cur.paragraph();
1676 if (cur.pos() != cur.lastpos()) {
1677 // this is the code for a normal delete, not pasting
1679 cur.recordUndo(DELETE_UNDO);
1680 bool const was_inset = cur.paragraph().isInset(cur.pos());
1681 if(!par.eraseChar(cur.pos(), cur.buffer()->params().track_changes))
1682 // the character has been logically deleted only => skip it
1683 cur.top().forwardPos();
1686 cur.forceBufferUpdate();
1688 cur.checkBufferStructure();
1691 if (cur.pit() == cur.lastpit())
1692 return dissolveInset(cur);
1694 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1695 cur.recordUndo(DELETE_UNDO);
1696 par.setChange(cur.pos(), Change(Change::DELETED));
1700 setCursorIntern(cur, cur.pit() + 1, 0);
1701 needsUpdate = backspacePos0(cur);
1705 needsUpdate |= handleBibitems(cur);
1708 // Make sure the cursor is correct. Is this really needed?
1709 // No, not really... at least not here!
1710 cur.top().setPitPos(cur.pit(), cur.pos());
1711 cur.checkBufferStructure();
1718 bool Text::backspacePos0(Cursor & cur)
1720 LBUFERR(this == cur.text());
1724 BufferParams const & bufparams = cur.buffer()->params();
1725 ParagraphList & plist = cur.text()->paragraphs();
1726 Paragraph const & par = cur.paragraph();
1727 Cursor prevcur = cur;
1729 prevcur.pos() = prevcur.lastpos();
1730 Paragraph const & prevpar = prevcur.paragraph();
1732 // is it an empty paragraph?
1733 if (cur.lastpos() == 0
1734 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1735 cur.recordUndo(prevcur.pit());
1736 plist.erase(plist.iterator_at(cur.pit()));
1738 // is previous par empty?
1739 else if (prevcur.lastpos() == 0
1740 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1741 cur.recordUndo(prevcur.pit());
1742 plist.erase(plist.iterator_at(prevcur.pit()));
1744 // FIXME: Do we really not want to allow this???
1745 // Pasting is not allowed, if the paragraphs have different
1746 // layouts. I think it is a real bug of all other
1747 // word processors to allow it. It confuses the user.
1748 // Correction: Pasting is always allowed with standard-layout
1749 // or the empty layout.
1751 cur.recordUndo(prevcur.pit());
1752 mergeParagraph(bufparams, plist, prevcur.pit());
1755 cur.forceBufferUpdate();
1756 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1762 bool Text::backspace(Cursor & cur)
1764 LBUFERR(this == cur.text());
1765 bool needsUpdate = false;
1766 if (cur.pos() == 0) {
1768 return dissolveInset(cur);
1770 Cursor prev_cur = cur;
1773 if (!cur.paragraph().empty()
1774 && !prev_cur.paragraph().isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1775 cur.recordUndo(prev_cur.pit(), prev_cur.pit());
1776 prev_cur.paragraph().setChange(prev_cur.lastpos(), Change(Change::DELETED));
1777 setCursorIntern(cur, prev_cur.pit(), prev_cur.lastpos());
1780 // The cursor is at the beginning of a paragraph, so
1781 // the backspace will collapse two paragraphs into one.
1782 needsUpdate = backspacePos0(cur);
1785 // this is the code for a normal backspace, not pasting
1787 cur.recordUndo(DELETE_UNDO);
1788 // We used to do cursorBackwardIntern() here, but it is
1789 // not a good idea since it triggers the auto-delete
1790 // mechanism. So we do a cursorBackwardIntern()-lite,
1791 // without the dreaded mechanism. (JMarc)
1792 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1793 false, cur.boundary());
1794 bool const was_inset = cur.paragraph().isInset(cur.pos());
1795 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().track_changes);
1797 cur.forceBufferUpdate();
1799 cur.checkBufferStructure();
1802 if (cur.pos() == cur.lastpos())
1803 cur.setCurrentFont();
1805 needsUpdate |= handleBibitems(cur);
1807 // A singlePar update is not enough in this case.
1808 // cur.screenUpdateFlags(Update::Force);
1809 cur.top().setPitPos(cur.pit(), cur.pos());
1815 bool Text::dissolveInset(Cursor & cur)
1817 LASSERT(this == cur.text(), return false);
1819 if (isMainText() || cur.inset().nargs() != 1)
1822 cur.recordUndoInset();
1824 cur.selHandle(false);
1825 // save position inside inset
1826 pos_type spos = cur.pos();
1827 pit_type spit = cur.pit();
1828 bool const inset_non_empty = cur.lastpit() != 0 || cur.lastpos() != 0;
1830 // update cursor offset
1834 // remember position outside inset to delete inset later
1835 // we do not do it now to avoid memory reuse issues (see #10667).
1836 DocIterator inset_it = cur;
1840 Buffer & b = *cur.buffer();
1841 // Is there anything in this text?
1842 if (inset_non_empty) {
1844 // we clear the cache so that we won't get conflicts with labels
1845 // that get pasted into the buffer. we should update this before
1846 // its being empty matters. if not (i.e., if we encounter bugs),
1847 // then this should instead be:
1848 // cur.buffer().updateBuffer();
1849 // but we'll try the cheaper solution here.
1850 cur.buffer()->clearReferenceCache();
1852 ParagraphList & plist = paragraphs();
1853 if (!lyxrc.ct_markup_copied)
1854 // Do not revive deleted text
1855 lyx::acceptChanges(plist, b.params());
1857 // ERT paragraphs have the Language latex_language.
1858 // This is invalid outside of ERT, so we need to
1859 // change it to the buffer language.
1860 for (auto & p : plist)
1861 p.changeLanguage(b.params(), latex_language, b.language());
1863 /* If the inset is the only thing in paragraph and the layout
1864 * is not plain, then the layout of the first paragraph of
1865 * inset should be remembered.
1866 * FIXME: this does not work as expected when change tracking
1867 * is on However, we do not really know what to do in this
1870 DocumentClass const & tclass = cur.buffer()->params().documentClass();
1871 if (inset_it.lastpos() == 1
1872 && !tclass.isPlainLayout(plist[0].layout())
1873 && !tclass.isDefaultLayout(plist[0].layout())) {
1874 // Copy all parameters except depth.
1875 Paragraph & par = cur.paragraph();
1876 par.setLayout(plist[0].layout());
1877 depth_type const dpth = par.getDepth();
1878 par.params() = plist[0].params();
1879 par.params().depth(dpth);
1882 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1883 b.errorList("Paste"));
1886 // delete the inset now
1887 inset_it.paragraph().eraseChar(inset_it.pos(), b.params().track_changes);
1890 cur.pit() = min(cur.lastpit(), spit);
1891 cur.pos() = min(cur.lastpos(), spos);
1892 // Ensure the current language is set correctly (bug 6292)
1893 cur.text()->setCursor(cur, cur.pit(), cur.pos());
1894 cur.clearSelection();
1896 cur.forceBufferUpdate();
1902 bool Text::splitInset(Cursor & cur)
1904 LASSERT(this == cur.text(), return false);
1906 if (isMainText() || cur.inset().nargs() != 1)
1910 if (cur.selection()) {
1911 // start from selection begin
1912 setCursor(cur, cur.selBegin().pit(), cur.selBegin().pos());
1913 cur.clearSelection();
1915 // save split position inside inset
1916 // (we need to copy the whole inset first)
1917 pos_type spos = cur.pos();
1918 pit_type spit = cur.pit();
1919 // some things only need to be done if the inset has content
1920 bool const inset_non_empty = cur.lastpit() != 0 || cur.lastpos() != 0;
1922 // move right before the inset
1925 // remember position outside inset
1926 pos_type ipos = cur.pos();
1927 pit_type ipit = cur.pit();
1932 cap::copySelectionToTemp(cur);
1933 cur.clearSelection();
1935 // paste copied inset
1936 cap::pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
1937 cur.forceBufferUpdate();
1939 // if the inset has text, cut after split position
1940 // and paste to new inset
1941 if (inset_non_empty) {
1942 // go back to first inset
1943 cur.text()->setCursor(cur, ipit, ipos);
1945 setCursor(cur, spit, spos);
1947 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1949 cap::cutSelectionToTemp(cur);
1951 cur.selHandle(false);
1953 bool atlastpos = false;
1954 if (cur.pos() == 0 && cur.pit() > 0) {
1955 // if we are at par start, remove this par
1956 cur.text()->backspace(cur);
1957 cur.forceBufferUpdate();
1958 } else if (cur.pos() == cur.lastpos())
1960 // Move out of and jump over inset
1968 cur.text()->selectAll(cur);
1969 cutSelection(cur, false);
1970 cap::pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
1971 cur.text()->setCursor(cur, 0, 0);
1972 if (atlastpos && cur.paragraph().isFreeSpacing() && cur.paragraph().empty()) {
1973 // We started from par end, remove extra empty par in free spacing insets
1974 cur.text()->erase(cur);
1975 cur.forceBufferUpdate();
1984 void Text::getWord(CursorSlice & from, CursorSlice & to,
1985 word_location const loc) const
1988 pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1992 void Text::write(ostream & os) const
1994 Buffer const & buf = owner_->buffer();
1995 ParagraphList::const_iterator pit = paragraphs().begin();
1996 ParagraphList::const_iterator end = paragraphs().end();
1998 for (; pit != end; ++pit)
1999 pit->write(os, buf.params(), dth);
2001 // Close begin_deeper
2002 for(; dth > 0; --dth)
2003 os << "\n\\end_deeper";
2007 bool Text::read(Lexer & lex,
2008 ErrorList & errorList, InsetText * insetPtr)
2010 Buffer const & buf = owner_->buffer();
2011 depth_type depth = 0;
2014 while (lex.isOK()) {
2016 string const token = lex.getString();
2021 if (token == "\\end_inset")
2024 if (token == "\\end_body")
2027 if (token == "\\begin_body")
2030 if (token == "\\end_document") {
2035 if (token == "\\begin_layout") {
2036 lex.pushToken(token);
2039 par.setInsetOwner(insetPtr);
2040 par.params().depth(depth);
2041 par.setFont(0, Font(inherit_font, buf.params().language));
2042 pars_.push_back(par);
2043 readParagraph(pars_.back(), lex, errorList);
2045 // register the words in the global word list
2046 pars_.back().updateWords();
2047 } else if (token == "\\begin_deeper") {
2049 } else if (token == "\\end_deeper") {
2051 lex.printError("\\end_deeper: " "depth is already null");
2055 LYXERR0("Handling unknown body token: `" << token << '\'');
2059 // avoid a crash on weird documents (bug 4859)
2060 if (pars_.empty()) {
2062 par.setInsetOwner(insetPtr);
2063 par.params().depth(depth);
2064 par.setFont(0, Font(inherit_font,
2065 buf.params().language));
2066 par.setPlainOrDefaultLayout(buf.params().documentClass());
2067 pars_.push_back(par);
2074 // Returns the current state (font, depth etc.) as a message for status bar.
2075 docstring Text::currentState(CursorData const & cur, bool devel_mode) const
2077 LBUFERR(this == cur.text());
2078 Buffer & buf = *cur.buffer();
2079 Paragraph const & par = cur.paragraph();
2080 odocstringstream os;
2082 if (buf.params().track_changes)
2083 os << _("[Change Tracking] ");
2085 Change change = par.lookupChange(cur.pos());
2087 if (change.changed()) {
2088 docstring const author =
2089 buf.params().authors().get(change.author).nameAndEmail();
2090 docstring const date = formatted_datetime(change.changetime);
2091 os << bformat(_("Changed by %1$s[[author]] on %2$s[[date]]. "),
2095 // I think we should only show changes from the default
2097 // No, from the document font (MV)
2098 Font font = cur.real_current_font;
2099 font.fontInfo().reduce(buf.params().getFont().fontInfo());
2101 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
2103 // The paragraph depth
2104 int depth = par.getDepth();
2106 os << bformat(_(", Depth: %1$d"), depth);
2108 // The paragraph spacing, but only if different from
2110 Spacing const & spacing = par.params().spacing();
2111 if (!spacing.isDefault()) {
2112 os << _(", Spacing: ");
2113 switch (spacing.getSpace()) {
2114 case Spacing::Single:
2117 case Spacing::Onehalf:
2120 case Spacing::Double:
2123 case Spacing::Other:
2124 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
2126 case Spacing::Default:
2127 // should never happen, do nothing
2132 // Custom text style
2133 InsetLayout const & layout = cur.inset().getLayout();
2134 if (layout.lyxtype() == InsetLyXType::CHARSTYLE)
2135 os << _(", Style: ") << translateIfPossible(layout.labelstring());
2138 os << _(", Inset: ") << &cur.inset();
2139 if (cur.lastidx() > 0)
2140 os << _(", Cell: ") << cur.idx();
2141 os << _(", Paragraph: ") << cur.pit();
2142 os << _(", Id: ") << par.id();
2143 os << _(", Position: ") << cur.pos();
2144 // FIXME: Why is the check for par.size() needed?
2145 // We are called with cur.pos() == par.size() quite often.
2146 if (!par.empty() && cur.pos() < par.size()) {
2147 // Force output of code point, not character
2148 size_t const c = par.getChar(cur.pos());
2149 os << _(", Char: 0x") << hex << c;
2151 os << _(", Boundary: ") << cur.boundary();
2152 // Row & row = cur.textRow();
2153 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2159 docstring Text::getPossibleLabel(DocIterator const & cur) const
2161 pit_type textpit = cur.pit();
2162 Layout const * layout = &(pars_[textpit].layout());
2164 // Will contain the label prefix.
2167 // For captions, we just take the caption type
2168 Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
2169 if (caption_inset) {
2170 string const & ftype = static_cast<InsetCaption *>(caption_inset)->floattype();
2171 FloatList const & fl = cur.buffer()->params().documentClass().floats();
2172 if (fl.typeExist(ftype)) {
2173 Floating const & flt = fl.getType(ftype);
2174 name = from_utf8(flt.refPrefix());
2177 name = from_utf8(ftype.substr(0,3));
2179 // For section, subsection, etc...
2180 if (layout->latextype == LATEX_PARAGRAPH && textpit != 0) {
2181 Layout const * layout2 = &(pars_[textpit - 1].layout());
2182 if (layout2->latextype != LATEX_PARAGRAPH) {
2187 if (layout->latextype != LATEX_PARAGRAPH)
2188 name = layout->refprefix;
2190 // If none of the above worked, see if the inset knows.
2192 InsetLayout const & il = cur.inset().getLayout();
2193 name = il.refprefix();
2198 docstring par_text = pars_[textpit].asString(AS_STR_SKIPDELETE);
2200 // The return string of math matrices might contain linebreaks
2201 par_text = subst(par_text, '\n', '-');
2202 int const numwords = 3;
2203 for (int i = 0; i < numwords; ++i) {
2204 if (par_text.empty())
2207 par_text = split(par_text, head, ' ');
2208 // Is it legal to use spaces in labels ?
2214 // Make sure it isn't too long
2215 unsigned int const max_label_length = 32;
2216 if (text.size() > max_label_length)
2217 text.resize(max_label_length);
2220 text = name + ':' + text;
2222 // We need a unique label
2223 docstring label = text;
2225 while (cur.buffer()->activeLabel(label)) {
2226 label = text + '-' + convert<docstring>(i);
2234 docstring Text::asString(int options) const
2236 return asString(0, pars_.size(), options);
2240 docstring Text::asString(pit_type beg, pit_type end, int options) const
2242 size_t i = size_t(beg);
2243 docstring str = pars_[i].asString(options);
2244 for (++i; i != size_t(end); ++i) {
2246 str += pars_[i].asString(options);
2252 void Text::shortenForOutliner(docstring & str, size_t const maxlen)
2254 support::truncateWithEllipsis(str, maxlen);
2255 for (char_type & c : str)
2256 if (c == L'\n' || c == L'\t')
2261 void Text::forOutliner(docstring & os, size_t const maxlen,
2262 bool const shorten) const
2264 pit_type end = pars_.size() - 1;
2265 if (0 <= end && !pars_[0].labelString().empty())
2266 os += pars_[0].labelString() + ' ';
2267 forOutliner(os, maxlen, 0, end, shorten);
2271 void Text::forOutliner(docstring & os, size_t const maxlen,
2272 pit_type pit_start, pit_type pit_end,
2273 bool const shorten) const
2275 size_t tmplen = shorten ? maxlen + 1 : maxlen;
2276 pit_type end = min(size_t(pit_end), pars_.size() - 1);
2278 for (pit_type i = pit_start; i <= end && os.length() < tmplen; ++i) {
2281 // This function lets the first label be treated separately
2282 pars_[i].forOutliner(os, tmplen, false, !first);
2286 shortenForOutliner(os, maxlen);
2290 void Text::charsTranspose(Cursor & cur)
2292 LBUFERR(this == cur.text());
2294 pos_type pos = cur.pos();
2296 // If cursor is at beginning or end of paragraph, do nothing.
2297 if (pos == cur.lastpos() || pos == 0)
2300 Paragraph & par = cur.paragraph();
2302 // Get the positions of the characters to be transposed.
2303 pos_type pos1 = pos - 1;
2304 pos_type pos2 = pos;
2306 // In change tracking mode, ignore deleted characters.
2307 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2309 if (pos2 == cur.lastpos())
2312 while (pos1 >= 0 && par.isDeleted(pos1))
2317 // Don't do anything if one of the "characters" is not regular text.
2318 if (par.isInset(pos1) || par.isInset(pos2))
2321 // Store the characters to be transposed (including font information).
2322 char_type const char1 = par.getChar(pos1);
2324 par.getFontSettings(cur.buffer()->params(), pos1);
2326 char_type const char2 = par.getChar(pos2);
2328 par.getFontSettings(cur.buffer()->params(), pos2);
2330 // And finally, we are ready to perform the transposition.
2331 // Track the changes if Change Tracking is enabled.
2332 bool const trackChanges = cur.buffer()->params().track_changes;
2336 par.eraseChar(pos2, trackChanges);
2337 par.eraseChar(pos1, trackChanges);
2338 par.insertChar(pos1, char2, font2, trackChanges);
2339 par.insertChar(pos2, char1, font1, trackChanges);
2341 cur.checkBufferStructure();
2343 // After the transposition, move cursor to after the transposition.
2344 setCursor(cur, cur.pit(), pos2);
2349 DocIterator Text::macrocontextPosition() const
2351 return macrocontext_position_;
2355 void Text::setMacrocontextPosition(DocIterator const & pos)
2357 macrocontext_position_ = pos;
2361 bool Text::completionSupported(Cursor const & cur) const
2363 Paragraph const & par = cur.paragraph();
2364 return !cur.selection()
2366 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2367 && !par.isWordSeparator(cur.pos() - 1);
2371 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2373 WordList const & list = theWordList(cur.getFont().language()->lang());
2374 return new TextCompletionList(cur, list);
2378 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2380 LBUFERR(cur.bv().cursor() == cur);
2382 cur.bv().cursor() = cur;
2383 if (!(cur.result().screenUpdate() & Update::Force))
2384 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2389 docstring Text::completionPrefix(Cursor const & cur) const
2391 CursorSlice from = cur.top();
2392 CursorSlice to = from;
2393 getWord(from, to, PREVIOUS_WORD);
2395 return cur.paragraph().asString(from.pos(), to.pos());