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 "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
29 #include "CompletionList.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
34 #include "ErrorList.h"
35 #include "FuncRequest.h"
37 #include "InsetList.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"
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"
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"
78 using namespace lyx::support;
82 using cap::cutSelection;
83 using cap::pasteParagraphList;
85 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
86 Paragraph & toPar, pos_type toPos, BufferParams const & params)
88 // Note: moveItem() does not honour change tracking!
89 // Therefore, it should only be used for breaking and merging paragraphs
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);
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)) {
105 char_type const tmpChar = fromPar.getChar(fromPos);
106 fromPar.eraseChar(fromPos, false);
107 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
112 void breakParagraphConservative(BufferParams const & bparams,
113 ParagraphList & pars, pit_type pit, pos_type pos)
115 // create a new paragraph
116 Paragraph & tmp = *pars.insert(lyx::next(pars.begin(), pit + 1),
118 Paragraph & par = pars[pit];
120 tmp.setInsetOwner(&par.inInset());
121 tmp.makeSameLayout(par);
123 LASSERT(pos <= par.size(), return);
125 if (pos < par.size()) {
126 // move everything behind the break position to the new paragraph
127 pos_type pos_end = par.size() - 1;
129 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
130 if (moveItem(par, pos, tmp, j, bparams)) {
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));
142 void mergeParagraph(BufferParams const & bparams,
143 ParagraphList & pars, pit_type par_offset)
145 Paragraph & next = pars[par_offset + 1];
146 Paragraph & par = pars[par_offset];
148 pos_type pos_end = next.size() - 1;
149 pos_type pos_insert = par.size();
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));
160 Change change = next.lookupChange(next.size());
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)) {
169 // move the change of the end-of-paragraph character
170 par.setChange(par.size(), change);
172 pars.erase(lyx::next(pars.begin(), par_offset + 1));
176 Text::Text(InsetText * owner, bool use_default_layout)
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);
186 par.setPlainLayout(dc);
190 Text::Text(InsetText * owner, Text const & text)
191 : owner_(owner), pars_(text.pars_)
193 for (auto & p : pars_)
194 p.setInsetOwner(owner);
198 pit_type Text::depthHook(pit_type pit, depth_type depth) const
200 pit_type newpit = pit;
205 while (newpit != 0 && pars_[newpit].getDepth() > depth)
208 if (pars_[newpit].getDepth() > depth)
215 pit_type Text::outerHook(pit_type par_offset) const
217 Paragraph const & par = pars_[par_offset];
219 if (par.getDepth() == 0)
221 return depthHook(par_offset, depth_type(par.getDepth() - 1));
225 bool Text::isFirstInSequence(pit_type par_offset) const
227 Paragraph const & par = pars_[par_offset];
229 pit_type dhook_offset = depthHook(par_offset, par.getDepth());
231 if (dhook_offset == par_offset)
234 Paragraph const & dhook = pars_[dhook_offset];
236 return dhook.layout() != par.layout()
237 || dhook.getDepth() != par.getDepth();
241 pit_type Text::lastInSequence(pit_type pit) const
243 depth_type const depth = pars_[pit].getDepth();
244 pit_type newpit = pit;
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())))
256 int Text::getTocLevel(pit_type par_offset) const
258 Paragraph const & par = pars_[par_offset];
260 if (par.layout().isEnvironment() && !isFirstInSequence(par_offset))
261 return Layout::NOT_IN_TOC;
263 return par.layout().toclevel;
267 Font const Text::outerFont(pit_type par_offset) const
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
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();
285 return Font(tmpfont);
289 int Text::getEndLabel(pit_type p) const
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;
297 if (endlabeltype != END_LABEL_NO_LABEL) {
298 if (p + 1 == pit_type(pars_.size()))
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()))
310 pit = outerHook(pit);
311 if (pit != pit_type(pars_.size()))
312 par_depth = pars_[pit].getDepth();
314 return END_LABEL_NO_LABEL;
318 static void acceptOrRejectChanges(ParagraphList & pars,
319 BufferParams const & bparams, Text::ChangeOp op)
321 pit_type pars_size = static_cast<pit_type>(pars.size());
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());
331 pars[pit].rejectChanges(0, pars[pit].size());
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));
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));
349 mergeParagraph(bparams, pars, pit);
359 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
361 acceptOrRejectChanges(pars, bparams, Text::ACCEPT);
365 void rejectChanges(ParagraphList & pars, BufferParams const & bparams)
367 acceptOrRejectChanges(pars, bparams, Text::REJECT);
371 InsetText const & Text::inset() const
378 void Text::readParToken(Paragraph & par, Lexer & lex,
379 string const & token, Font & font, Change & change, ErrorList & errorList)
381 Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
382 BufferParams & bp = buf->params();
384 if (token[0] != '\\') {
385 docstring dstr = lex.getDocString();
386 par.appendString(dstr, font, change);
388 } else if (token == "\\begin_layout") {
390 docstring layoutname = lex.getDocString();
392 font = Font(inherit_font, bp.language);
393 change = Change(Change::UNCHANGED);
395 DocumentClass const & tclass = bp.documentClass();
397 if (layoutname.empty())
398 layoutname = tclass.defaultLayoutName();
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();
408 // otherwise, the empty layout maps to the default
409 if (layoutname == tclass.plainLayoutName())
410 layoutname = tclass.defaultLayoutName();
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);
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}));
425 par.setLayout(bp.documentClass()[layoutname]);
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()]);
432 par.params().read(lex);
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);
443 par.insertInset(par.size(), inset, font, change);
446 docstring line = lex.getDocString();
447 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
448 {par.id(), 0}, {par.id(), -1}));
450 } else if (token == "\\family") {
452 setLyXFamily(lex.getString(), font.fontInfo());
453 } else if (token == "\\series") {
455 setLyXSeries(lex.getString(), font.fontInfo());
456 } else if (token == "\\shape") {
458 setLyXShape(lex.getString(), font.fontInfo());
459 } else if (token == "\\size") {
461 setLyXSize(lex.getString(), font.fontInfo());
462 } else if (token == "\\lang") {
464 string const tok = lex.getString();
465 Language const * lang = languages.getLanguage(tok);
467 font.setLanguage(lang);
469 font.setLanguage(bp.language);
470 lex.printError("Unknown language `$$Token'");
472 } else if (token == "\\numeric") {
474 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
475 } else if (token == "\\nospellcheck") {
477 font.fontInfo().setNoSpellcheck(setLyXMisc(lex.getString()));
478 } else if (token == "\\emph") {
480 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
481 } else if (token == "\\bar") {
483 string const tok = lex.getString();
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);
492 lex.printError("Unknown bar font flag "
494 } else if (token == "\\strikeout") {
496 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
497 } else if (token == "\\xout") {
499 font.fontInfo().setXout(setLyXMisc(lex.getString()));
500 } else if (token == "\\uuline") {
502 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
503 } else if (token == "\\uwave") {
505 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
506 } else if (token == "\\noun") {
508 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
509 } else if (token == "\\color") {
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>();
517 inset->setBuffer(*buf);
518 par.insertInset(par.size(), inset.release(), font, change);
519 } else if (token == "\\SpecialCharNoPassThru") {
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>();
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);
539 par.insert(par.size(), from_ascii("---"), font, change);
541 if (token == "\\twohyphens")
542 par.insertChar(par.size(), 0x2013, font, change);
544 par.insertChar(par.size(), 0x2014, font, change);
546 } else if (token == "\\backslash") {
547 par.appendChar('\\', font, change);
548 } else if (token == "\\LyXTable") {
549 auto inset = make_unique<InsetTabular>(buf);
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") {
556 istringstream is(lex.getString());
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"),
571 {par.id(), par.size()}, {par.id(), par.size() + 1}));
572 bp.addAuthor(Author(aid));
574 if (token == "\\change_inserted")
575 change = Change(Change::INSERTED, am.find(aid)->second, ct);
577 change = Change(Change::DELETED, am.find(aid)->second, ct);
580 errorList.push_back(ErrorItem(_("Unknown token"),
581 bformat(_("Unknown token: %1$s %2$s\n"),
584 {par.id(), 0}, {par.id(), -1}));
589 void Text::readParagraph(Paragraph & par, Lexer & lex,
590 ErrorList & errorList)
593 string token = lex.getString();
595 Change change(Change::UNCHANGED);
598 readParToken(par, lex, token, font, change, errorList);
601 token = lex.getString();
606 if (token == "\\end_layout") {
607 //Ok, paragraph finished
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";
622 // Final change goes to paragraph break:
623 par.setChange(par.size(), change);
625 // Initialize begin_of_body_ on load; redoParagraph maintains
626 par.setBeginOfBody();
628 // mark paragraph for spell checking on load
629 // par.requestSpellCheck();
633 class TextCompletionList : public CompletionList
637 TextCompletionList(Cursor const & cur, WordList const & list)
638 : buffer_(cur.buffer()), list_(list)
641 virtual ~TextCompletionList() {}
644 virtual bool sorted() const { return true; }
646 virtual size_t size() const
651 virtual docstring const & data(size_t idx) const
653 return list_.word(idx);
658 Buffer const * buffer_;
660 WordList const & list_;
664 bool Text::empty() const
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);
672 double Text::spacing(Paragraph const & par) const
674 if (par.params().spacing().isDefault())
675 return owner_->buffer().params().spacing().getValue();
676 return par.params().spacing().getValue();
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
687 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
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),
697 Paragraph & par = pars[par_offset];
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());
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());
717 bool const isempty = (par.allowEmpty() && par.empty());
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());
724 tmp->params().depth(par.params().depth());
725 tmp->params().noindent(par.params().noindent());
727 // move everything behind the break position
728 // to the new paragraph
730 /* Note: if !keepempty, empty() == true, then we reach
731 * here with size() == 0. So pos_end becomes - 1. This
732 * doesn't cause problems because both loops below
733 * enforce pos <= pos_end and 0 <= pos
735 pos_type pos_end = par.size() - 1;
737 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
738 if (moveItem(par, pos, *tmp, j, bparams)) {
744 // Move over the end-of-par change information
745 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
746 par.setChange(par.size(), Change(bparams.track_changes ?
747 Change::INSERTED : Change::UNCHANGED));
750 // Make sure that we keep the language when
751 // breaking paragraph.
753 Font changed = tmp->getFirstFontSettings(bparams);
754 Font const & old = par.getFontSettings(bparams, par.size());
755 changed.setLanguage(old.language());
756 tmp->setFont(0, changed);
763 bool const soa = par.params().startOfAppendix();
764 par.params().clear();
765 // do not lose start of appendix marker (bug 4212)
766 par.params().startOfAppendix(soa);
767 par.setPlainOrDefaultLayout(bparams.documentClass());
771 par.setLayout(tmp->layout());
772 par.setLabelWidthString(tmp->params().labelWidthString());
773 par.params().depth(tmp->params().depth());
778 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
780 LBUFERR(this == cur.text());
782 Paragraph & cpar = cur.paragraph();
783 pit_type cpit = cur.pit();
785 DocumentClass const & tclass = cur.buffer()->params().documentClass();
786 Layout const & layout = cpar.layout();
788 if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
789 if (changeDepthAllowed(cur, DEC_DEPTH)) {
790 changeDepth(cur, DEC_DEPTH);
791 pit_type const prev = depthHook(cpit, cpar.getDepth());
792 docstring const & lay = pars_[prev].layout().name();
793 if (lay != layout.name())
796 docstring const & lay = cur.paragraph().usePlainLayout()
797 ? tclass.plainLayoutName() : tclass.defaultLayoutName();
798 if (lay != layout.name())
806 // Always break behind a space
807 // It is better to erase the space (Dekel)
808 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
809 cpar.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
811 // What should the layout for the new paragraph be?
812 bool keep_layout = layout.isEnvironment()
813 || (layout.isParagraph() && layout.parbreak_is_newline);
815 keep_layout = !keep_layout;
817 // We need to remember this before we break the paragraph, because
818 // that invalidates the layout variable
819 bool sensitive = layout.labeltype == LABEL_SENSITIVE;
821 // we need to set this before we insert the paragraph.
822 bool const isempty = cpar.allowEmpty() && cpar.empty();
824 lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
826 // After this, neither paragraph contains any rows!
829 pit_type next_par = cpit + 1;
831 // well this is the caption hack since one caption is really enough
834 // set to standard-layout
835 //FIXME Check if this should be plainLayout() in some cases
836 pars_[cpit].applyLayout(tclass.defaultLayout());
838 // set to standard-layout
839 //FIXME Check if this should be plainLayout() in some cases
840 pars_[next_par].applyLayout(tclass.defaultLayout());
843 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
844 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().track_changes))
845 break; // the character couldn't be deleted physically due to change tracking
848 // A singlePar update is not enough in this case.
849 cur.screenUpdateFlags(Update::Force);
850 cur.forceBufferUpdate();
852 // This check is necessary. Otherwise the new empty paragraph will
853 // be deleted automatically. And it is more friendly for the user!
854 if (cur.pos() != 0 || isempty)
855 setCursor(cur, cur.pit() + 1, 0);
857 setCursor(cur, cur.pit(), 0);
861 // needed to insert the selection
862 void Text::insertStringAsLines(Cursor & cur, docstring const & str,
865 BufferParams const & bparams = owner_->buffer().params();
866 pit_type pit = cur.pit();
867 pos_type pos = cur.pos();
869 // insert the string, don't insert doublespace
870 bool space_inserted = true;
871 for (auto const & ch : str) {
872 Paragraph & par = pars_[pit];
874 if (inset().allowMultiPar() && (!par.empty() || par.allowEmpty())) {
875 lyx::breakParagraph(*this, pit, pos,
876 par.layout().isEnvironment());
879 space_inserted = true;
883 // do not insert consecutive spaces if !free_spacing
884 } else if ((ch == ' ' || ch == '\t') &&
885 space_inserted && !par.isFreeSpacing()) {
887 } else if (ch == '\t') {
888 if (!par.isFreeSpacing()) {
889 // tabs are like spaces here
890 par.insertChar(pos, ' ', font, bparams.track_changes);
892 space_inserted = true;
894 par.insertChar(pos, ch, font, bparams.track_changes);
896 space_inserted = true;
898 } else if (!isPrintable(ch)) {
899 // Ignore unprintables
902 // just insert the character
903 par.insertChar(pos, ch, font, bparams.track_changes);
905 space_inserted = (ch == ' ');
908 setCursor(cur, pit, pos);
912 // turn double CR to single CR, others are converted into one
913 // blank. Then insertStringAsLines is called
914 void Text::insertStringAsParagraphs(Cursor & cur, docstring const & str,
917 docstring linestr = str;
918 bool newline_inserted = false;
920 for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
921 if (linestr[i] == '\n') {
922 if (newline_inserted) {
923 // we know that \r will be ignored by
924 // insertStringAsLines. Of course, it is a dirty
925 // trick, but it works...
926 linestr[i - 1] = '\r';
930 newline_inserted = true;
932 } else if (isPrintable(linestr[i])) {
933 newline_inserted = false;
936 insertStringAsLines(cur, linestr, font);
942 bool canInsertChar(Cursor const & cur, char_type c)
944 Paragraph const & par = cur.paragraph();
945 // If not in free spacing mode, check if there will be two blanks together or a blank at
946 // the beginning of a paragraph.
947 if (!par.isFreeSpacing() && isLineSeparatorChar(c)) {
948 if (cur.pos() == 0) {
950 "You cannot insert a space at the "
951 "beginning of a paragraph. Please read the Tutorial."));
954 // If something is wrong, ignore this character.
955 LASSERT(cur.pos() > 0, return false);
956 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
957 && !par.isDeleted(cur.pos() - 1)) {
959 "You cannot type two spaces this way. "
960 "Please read the Tutorial."));
965 // Prevent to insert uncodable characters in verbatim and ERT.
966 // The encoding is inherited from the context here.
967 if (par.isPassThru() && cur.getEncoding()) {
968 Encoding const * e = cur.getEncoding();
969 if (!e->encodable(c)) {
970 cur.message(_("Character is uncodable in this verbatim context."));
980 // insert a character, moves all the following breaks in the
981 // same Paragraph one to the right and make a rebreak
982 void Text::insertChar(Cursor & cur, char_type c)
984 LBUFERR(this == cur.text());
986 if (!canInsertChar(cur,c))
989 cur.recordUndo(INSERT_UNDO);
991 TextMetrics const & tm = cur.bv().textMetrics(this);
992 Buffer const & buffer = *cur.buffer();
993 Paragraph & par = cur.paragraph();
994 // try to remove this
995 pit_type const pit = cur.pit();
997 if (lyxrc.auto_number) {
998 static docstring const number_operators = from_ascii("+-/*");
999 static docstring const number_unary_operators = from_ascii("+-");
1001 // European Number Separators: comma, dot etc.
1002 // European Number Terminators: percent, permille, degree, euro etc.
1003 if (cur.current_font.fontInfo().number() == FONT_ON) {
1004 if (!isDigitASCII(c) && !contains(number_operators, c) &&
1005 !(isEuropeanNumberSeparator(c) &&
1007 cur.pos() != cur.lastpos() &&
1008 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
1009 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON) &&
1010 !(isEuropeanNumberTerminator(c) &&
1012 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
1013 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
1015 number(cur); // Set current_font.number to OFF
1016 } else if (isDigitASCII(c) &&
1017 cur.real_current_font.isVisibleRightToLeft()) {
1018 number(cur); // Set current_font.number to ON
1020 if (cur.pos() != 0) {
1021 char_type const ch = par.getChar(cur.pos() - 1);
1022 if (contains(number_unary_operators, ch) &&
1024 || par.isSeparator(cur.pos() - 2)
1025 || par.isEnvSeparator(cur.pos() - 2)
1026 || par.isNewline(cur.pos() - 2))
1028 setCharFont(pit, cur.pos() - 1, cur.current_font,
1030 } else if (isEuropeanNumberSeparator(ch)
1032 && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
1033 setCharFont(pit, cur.pos() - 1, cur.current_font,
1040 // In Bidi text, we want spaces to be treated in a special way: spaces
1041 // which are between words in different languages should get the
1042 // paragraph's language; otherwise, spaces should keep the language
1043 // they were originally typed in. This is only in effect while typing;
1044 // after the text is already typed in, the user can always go back and
1045 // explicitly set the language of a space as desired. But 99.9% of the
1046 // time, what we're doing here is what the user actually meant.
1048 // The following cases are the ones in which the language of the space
1049 // should be changed to match that of the containing paragraph. In the
1050 // depictions, lowercase is LTR, uppercase is RTL, underscore (_)
1051 // represents a space, pipe (|) represents the cursor position (so the
1052 // character before it is the one just typed in). The different cases
1053 // are depicted logically (not visually), from left to right:
1058 // Theoretically, there are other situations that we should, perhaps, deal
1059 // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any
1060 // point (to understand why, just try to create this situation...).
1062 if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
1063 // get font in front and behind the space in question. But do NOT
1064 // use getFont(cur.pos()) because the character c is not inserted yet
1065 Font const pre_space_font = tm.displayFont(cur.pit(), cur.pos() - 2);
1066 Font const & post_space_font = cur.real_current_font;
1067 bool pre_space_rtl = pre_space_font.isVisibleRightToLeft();
1068 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
1070 if (pre_space_rtl != post_space_rtl) {
1071 // Set the space's language to match the language of the
1072 // adjacent character whose direction is the paragraph's
1073 // direction; don't touch other properties of the font
1074 Language const * lang =
1075 (pre_space_rtl == par.isRTL(buffer.params())) ?
1076 pre_space_font.language() : post_space_font.language();
1078 Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
1079 space_font.setLanguage(lang);
1080 par.setFont(cur.pos() - 1, space_font);
1084 pos_type pos = cur.pos();
1085 if (!cur.paragraph().isPassThru() && owner_->lyxCode() != IPA_CODE &&
1086 cur.real_current_font.fontInfo().family() != TYPEWRITER_FAMILY &&
1087 c == '-' && pos > 0) {
1088 if (par.getChar(pos - 1) == '-') {
1089 // convert "--" to endash
1090 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1093 } else if (par.getChar(pos - 1) == 0x2013) {
1094 // convert "---" to emdash
1095 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1101 par.insertChar(pos, c, cur.current_font,
1102 cur.buffer()->params().track_changes);
1103 cur.checkBufferStructure();
1105 // cur.screenUpdateFlags(Update::Force);
1106 bool boundary = cur.boundary()
1107 || tm.isRTLBoundary(cur.pit(), pos + 1);
1108 setCursor(cur, cur.pit(), pos + 1, false, boundary);
1113 void Text::charInserted(Cursor & cur)
1115 Paragraph & par = cur.paragraph();
1117 // register word if a non-letter was entered
1119 && !par.isWordSeparator(cur.pos() - 2)
1120 && par.isWordSeparator(cur.pos() - 1)) {
1121 // get the word in front of cursor
1122 LBUFERR(this == cur.text());
1123 cur.paragraph().updateWords();
1128 // the cursor set functions have a special mechanism. When they
1129 // realize, that you left an empty paragraph, they will delete it.
1131 bool Text::cursorForwardOneWord(Cursor & cur)
1133 LBUFERR(this == cur.text());
1135 pos_type const lastpos = cur.lastpos();
1136 pit_type pit = cur.pit();
1137 pos_type pos = cur.pos();
1138 Paragraph const & par = cur.paragraph();
1140 // Paragraph boundary is a word boundary
1141 if (pos == lastpos || (pos + 1 == lastpos && par.isEnvSeparator(pos))) {
1142 if (pit != cur.lastpit())
1143 return setCursor(cur, pit + 1, 0);
1148 if (lyxrc.mac_like_cursor_movement) {
1149 // Skip through trailing punctuation and spaces.
1150 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
1153 // Skip over either a non-char inset or a full word
1154 if (pos != lastpos && par.isWordSeparator(pos))
1156 else while (pos != lastpos && !par.isWordSeparator(pos))
1159 LASSERT(pos < lastpos, return false); // see above
1160 if (!par.isWordSeparator(pos))
1161 while (pos != lastpos && !par.isWordSeparator(pos))
1163 else if (par.isChar(pos))
1164 while (pos != lastpos && par.isChar(pos))
1166 else if (!par.isSpace(pos)) // non-char inset
1169 // Skip over white space
1170 while (pos != lastpos && par.isSpace(pos))
1174 // Don't skip a separator inset at the end of a paragraph
1175 if (pos == lastpos && pos && par.isEnvSeparator(pos - 1))
1178 return setCursor(cur, pit, pos);
1182 bool Text::cursorBackwardOneWord(Cursor & cur)
1184 LBUFERR(this == cur.text());
1186 pit_type pit = cur.pit();
1187 pos_type pos = cur.pos();
1188 Paragraph & par = cur.paragraph();
1190 // Paragraph boundary is a word boundary
1191 if (pos == 0 && pit != 0) {
1192 Paragraph & prevpar = getPar(pit - 1);
1193 pos = prevpar.size();
1194 // Don't stop after an environment separator
1195 if (pos && prevpar.isEnvSeparator(pos - 1))
1197 return setCursor(cur, pit - 1, pos);
1200 if (lyxrc.mac_like_cursor_movement) {
1201 // Skip through punctuation and spaces.
1202 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1205 // Skip over either a non-char inset or a full word
1206 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1208 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1211 // Skip over white space
1212 while (pos != 0 && par.isSpace(pos - 1))
1215 if (pos != 0 && !par.isWordSeparator(pos - 1))
1216 while (pos != 0 && !par.isWordSeparator(pos - 1))
1218 else if (pos != 0 && par.isChar(pos - 1))
1219 while (pos != 0 && par.isChar(pos - 1))
1221 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1225 return setCursor(cur, pit, pos);
1229 bool Text::cursorVisLeftOneWord(Cursor & cur)
1231 LBUFERR(this == cur.text());
1233 pos_type left_pos, right_pos;
1235 Cursor temp_cur = cur;
1237 // always try to move at least once...
1238 while (temp_cur.posVisLeft(true /* skip_inset */)) {
1240 // collect some information about current cursor position
1241 temp_cur.getSurroundingPos(left_pos, right_pos);
1242 bool left_is_letter =
1243 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1244 bool right_is_letter =
1245 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1247 // if we're not at a letter/non-letter boundary, continue moving
1248 if (left_is_letter == right_is_letter)
1251 // we should stop when we have an LTR word on our right or an RTL word
1253 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1254 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1255 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1256 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1260 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1261 true, temp_cur.boundary());
1265 bool Text::cursorVisRightOneWord(Cursor & cur)
1267 LBUFERR(this == cur.text());
1269 pos_type left_pos, right_pos;
1271 Cursor temp_cur = cur;
1273 // always try to move at least once...
1274 while (temp_cur.posVisRight(true /* skip_inset */)) {
1276 // collect some information about current cursor position
1277 temp_cur.getSurroundingPos(left_pos, right_pos);
1278 bool left_is_letter =
1279 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1280 bool right_is_letter =
1281 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1283 // if we're not at a letter/non-letter boundary, continue moving
1284 if (left_is_letter == right_is_letter)
1287 // we should stop when we have an LTR word on our right or an RTL word
1289 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1290 temp_cur.buffer()->params(),
1291 left_pos).isRightToLeft())
1292 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1293 temp_cur.buffer()->params(),
1294 right_pos).isRightToLeft()))
1298 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1299 true, temp_cur.boundary());
1303 void Text::selectWord(Cursor & cur, word_location loc)
1305 LBUFERR(this == cur.text());
1306 CursorSlice from = cur.top();
1308 getWord(from, to, loc);
1309 if (cur.top() != from)
1310 setCursor(cur, from.pit(), from.pos());
1313 if (!cur.selection())
1315 setCursor(cur, to.pit(), to.pos());
1317 cur.setWordSelection(true);
1321 void Text::selectAll(Cursor & cur)
1323 LBUFERR(this == cur.text());
1324 if (cur.lastpos() == 0 && cur.lastpit() == 0)
1326 // If the cursor is at the beginning, make sure the cursor ends there
1327 if (cur.pit() == 0 && cur.pos() == 0) {
1328 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1330 setCursor(cur, 0, 0);
1332 setCursor(cur, 0, 0);
1334 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1340 // Select the word currently under the cursor when no
1341 // selection is currently set
1342 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1344 LBUFERR(this == cur.text());
1345 if (cur.selection())
1347 selectWord(cur, loc);
1348 return cur.selection();
1352 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1354 LBUFERR(this == cur.text());
1356 if (!cur.selection()) {
1357 if (!selectChange(cur))
1361 cur.recordUndoSelection();
1363 pit_type begPit = cur.selectionBegin().pit();
1364 pit_type endPit = cur.selectionEnd().pit();
1366 pos_type begPos = cur.selectionBegin().pos();
1367 pos_type endPos = cur.selectionEnd().pos();
1369 // keep selection info, because endPos becomes invalid after the first loop
1370 bool const endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1372 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1373 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1374 pos_type parSize = pars_[pit].size();
1376 // ignore empty paragraphs; otherwise, an assertion will fail for
1377 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1381 // do not consider first paragraph if the cursor starts at pos size()
1382 if (pit == begPit && begPos == parSize)
1385 // do not consider last paragraph if the cursor ends at pos 0
1386 if (pit == endPit && endPos == 0)
1387 break; // last iteration anyway
1389 pos_type const left = (pit == begPit ? begPos : 0);
1390 pos_type const right = (pit == endPit ? endPos : parSize);
1393 // there is no change here
1397 pars_[pit].acceptChanges(left, right);
1399 pars_[pit].rejectChanges(left, right);
1403 // next, accept/reject imaginary end-of-par characters
1405 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1406 pos_type pos = pars_[pit].size();
1408 // skip if the selection ends before the end-of-par
1409 if (pit == endPit && endsBeforeEndOfPar)
1410 break; // last iteration anyway
1412 // skip if this is not the last paragraph of the document
1413 // note: the user should be able to accept/reject the par break of the last par!
1414 if (pit == endPit && pit + 1 != int(pars_.size()))
1415 break; // last iteration anway
1418 if (pars_[pit].isInserted(pos)) {
1419 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1420 } else if (pars_[pit].isDeleted(pos)) {
1421 if (pit + 1 == int(pars_.size())) {
1422 // we cannot remove a par break at the end of the last paragraph;
1423 // instead, we mark it unchanged
1424 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1426 mergeParagraph(cur.buffer()->params(), pars_, pit);
1432 if (pars_[pit].isDeleted(pos)) {
1433 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1434 } else if (pars_[pit].isInserted(pos)) {
1435 if (pit + 1 == int(pars_.size())) {
1436 // we mark the par break at the end of the last paragraph unchanged
1437 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1439 mergeParagraph(cur.buffer()->params(), pars_, pit);
1447 // finally, invoke the DEPM
1448 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().track_changes);
1451 cur.clearSelection();
1452 setCursorIntern(cur, begPit, begPos);
1453 cur.screenUpdateFlags(Update::Force);
1454 cur.forceBufferUpdate();
1458 void Text::acceptChanges()
1460 BufferParams const & bparams = owner_->buffer().params();
1461 lyx::acceptChanges(pars_, bparams);
1462 deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.track_changes);
1466 void Text::rejectChanges()
1468 BufferParams const & bparams = owner_->buffer().params();
1469 pit_type pars_size = static_cast<pit_type>(pars_.size());
1471 // first, reject changes within each individual paragraph
1472 // (do not consider end-of-par)
1473 for (pit_type pit = 0; pit < pars_size; ++pit) {
1474 if (!pars_[pit].empty()) // prevent assertion failure
1475 pars_[pit].rejectChanges(0, pars_[pit].size());
1478 // next, reject imaginary end-of-par characters
1479 for (pit_type pit = 0; pit < pars_size; ++pit) {
1480 pos_type pos = pars_[pit].size();
1482 if (pars_[pit].isDeleted(pos)) {
1483 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1484 } else if (pars_[pit].isInserted(pos)) {
1485 if (pit == pars_size - 1) {
1486 // we mark the par break at the end of the last
1487 // paragraph unchanged
1488 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1490 mergeParagraph(bparams, pars_, pit);
1497 // finally, invoke the DEPM
1498 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.track_changes);
1502 void Text::deleteWordForward(Cursor & cur, bool const force)
1504 LBUFERR(this == cur.text());
1505 if (cur.lastpos() == 0)
1509 cur.selection(true);
1510 cursorForwardOneWord(cur);
1512 if (force || !cur.confirmDeletion()) {
1513 cutSelection(cur, false);
1514 cur.checkBufferStructure();
1520 void Text::deleteWordBackward(Cursor & cur, bool const force)
1522 LBUFERR(this == cur.text());
1523 if (cur.lastpos() == 0)
1524 cursorBackward(cur);
1527 cur.selection(true);
1528 cursorBackwardOneWord(cur);
1530 if (force || !cur.confirmDeletion()) {
1531 cutSelection(cur, false);
1532 cur.checkBufferStructure();
1538 // Kill to end of line.
1539 void Text::changeCase(Cursor & cur, TextCase action, bool partial)
1541 LBUFERR(this == cur.text());
1545 bool const gotsel = cur.selection();
1547 from = cur.selBegin();
1551 getWord(from, to, partial ? PARTIAL_WORD : WHOLE_WORD);
1552 cursorForwardOneWord(cur);
1555 cur.recordUndoSelection();
1557 pit_type begPit = from.pit();
1558 pit_type endPit = to.pit();
1560 pos_type begPos = from.pos();
1561 pos_type endPos = to.pos();
1563 pos_type right = 0; // needed after the for loop
1565 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1566 Paragraph & par = pars_[pit];
1567 pos_type const pos = (pit == begPit ? begPos : 0);
1568 right = (pit == endPit ? endPos : par.size());
1569 par.changeCase(cur.buffer()->params(), pos, right, action);
1572 // the selection may have changed due to logically-only deleted chars
1574 setCursor(cur, begPit, begPos);
1576 setCursor(cur, endPit, right);
1579 setCursor(cur, endPit, right);
1581 cur.checkBufferStructure();
1585 bool Text::handleBibitems(Cursor & cur)
1587 if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1593 BufferParams const & bufparams = cur.buffer()->params();
1594 Paragraph const & par = cur.paragraph();
1595 Cursor prevcur = cur;
1596 if (cur.pit() > 0) {
1598 prevcur.pos() = prevcur.lastpos();
1600 Paragraph const & prevpar = prevcur.paragraph();
1602 // if a bibitem is deleted, merge with previous paragraph
1603 // if this is a bibliography item as well
1604 if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1605 cur.recordUndo(prevcur.pit());
1606 mergeParagraph(bufparams, cur.text()->paragraphs(),
1608 cur.forceBufferUpdate();
1609 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1610 cur.screenUpdateFlags(Update::Force);
1614 // otherwise reset to default
1615 cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1620 bool Text::erase(Cursor & cur)
1622 LASSERT(this == cur.text(), return false);
1623 bool needsUpdate = false;
1624 Paragraph & par = cur.paragraph();
1626 if (cur.pos() != cur.lastpos()) {
1627 // this is the code for a normal delete, not pasting
1629 cur.recordUndo(DELETE_UNDO);
1630 bool const was_inset = cur.paragraph().isInset(cur.pos());
1631 if(!par.eraseChar(cur.pos(), cur.buffer()->params().track_changes))
1632 // the character has been logically deleted only => skip it
1633 cur.top().forwardPos();
1636 cur.forceBufferUpdate();
1638 cur.checkBufferStructure();
1641 if (cur.pit() == cur.lastpit())
1642 return dissolveInset(cur);
1644 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1645 cur.recordUndo(DELETE_UNDO);
1646 par.setChange(cur.pos(), Change(Change::DELETED));
1650 setCursorIntern(cur, cur.pit() + 1, 0);
1651 needsUpdate = backspacePos0(cur);
1655 needsUpdate |= handleBibitems(cur);
1658 // Make sure the cursor is correct. Is this really needed?
1659 // No, not really... at least not here!
1660 cur.top().setPitPos(cur.pit(), cur.pos());
1661 cur.checkBufferStructure();
1668 bool Text::backspacePos0(Cursor & cur)
1670 LBUFERR(this == cur.text());
1674 bool needsUpdate = false;
1676 BufferParams const & bufparams = cur.buffer()->params();
1677 DocumentClass const & tclass = bufparams.documentClass();
1678 ParagraphList & plist = cur.text()->paragraphs();
1679 Paragraph const & par = cur.paragraph();
1680 Cursor prevcur = cur;
1682 prevcur.pos() = prevcur.lastpos();
1683 Paragraph const & prevpar = prevcur.paragraph();
1685 // is it an empty paragraph?
1686 if (cur.lastpos() == 0
1687 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1688 cur.recordUndo(prevcur.pit());
1689 plist.erase(lyx::next(plist.begin(), cur.pit()));
1692 // is previous par empty?
1693 else if (prevcur.lastpos() == 0
1694 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1695 cur.recordUndo(prevcur.pit());
1696 plist.erase(lyx::next(plist.begin(), prevcur.pit()));
1699 // FIXME: Do we really not want to allow this???
1700 // Pasting is not allowed, if the paragraphs have different
1701 // layouts. I think it is a real bug of all other
1702 // word processors to allow it. It confuses the user.
1703 // Correction: Pasting is always allowed with standard-layout
1704 // or the empty layout.
1705 else if (par.layout() == prevpar.layout()
1706 || tclass.isDefaultLayout(par.layout())
1707 || tclass.isPlainLayout(par.layout())) {
1708 cur.recordUndo(prevcur.pit());
1709 mergeParagraph(bufparams, plist, prevcur.pit());
1714 cur.forceBufferUpdate();
1715 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1722 bool Text::backspace(Cursor & cur)
1724 LBUFERR(this == cur.text());
1725 bool needsUpdate = false;
1726 if (cur.pos() == 0) {
1728 return dissolveInset(cur);
1730 Cursor prev_cur = cur;
1733 if (cur.paragraph().size() > 0
1734 && !prev_cur.paragraph().isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1735 cur.recordUndo(prev_cur.pit(), prev_cur.pit());
1736 prev_cur.paragraph().setChange(prev_cur.lastpos(), Change(Change::DELETED));
1737 setCursorIntern(cur, prev_cur.pit(), prev_cur.lastpos());
1740 // The cursor is at the beginning of a paragraph, so
1741 // the backspace will collapse two paragraphs into one.
1742 needsUpdate = backspacePos0(cur);
1745 // this is the code for a normal backspace, not pasting
1747 cur.recordUndo(DELETE_UNDO);
1748 // We used to do cursorBackwardIntern() here, but it is
1749 // not a good idea since it triggers the auto-delete
1750 // mechanism. So we do a cursorBackwardIntern()-lite,
1751 // without the dreaded mechanism. (JMarc)
1752 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1753 false, cur.boundary());
1754 bool const was_inset = cur.paragraph().isInset(cur.pos());
1755 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().track_changes);
1757 cur.forceBufferUpdate();
1759 cur.checkBufferStructure();
1762 if (cur.pos() == cur.lastpos())
1763 cur.setCurrentFont();
1765 needsUpdate |= handleBibitems(cur);
1767 // A singlePar update is not enough in this case.
1768 // cur.screenUpdateFlags(Update::Force);
1769 cur.top().setPitPos(cur.pit(), cur.pos());
1775 bool Text::dissolveInset(Cursor & cur)
1777 LASSERT(this == cur.text(), return false);
1779 if (isMainText() || cur.inset().nargs() != 1)
1782 cur.recordUndoInset();
1784 cur.selHandle(false);
1785 // save position inside inset
1786 pos_type spos = cur.pos();
1787 pit_type spit = cur.pit();
1788 ParagraphList plist;
1789 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1790 plist = paragraphs();
1792 // update cursor offset
1796 // remember position outside inset to delete inset later
1797 // we do not do it now to avoid memory reuse issues (see #10667).
1798 DocIterator inset_it = cur;
1802 Buffer & b = *cur.buffer();
1803 if (!plist.empty()) {
1805 // we clear the cache so that we won't get conflicts with labels
1806 // that get pasted into the buffer. we should update this before
1807 // its being empty matters. if not (i.e., if we encounter bugs),
1808 // then this should instead be:
1809 // cur.buffer().updateBuffer();
1810 // but we'll try the cheaper solution here.
1811 cur.buffer()->clearReferenceCache();
1813 if (!lyxrc.ct_markup_copied)
1814 // Do not revive deleted text
1815 lyx::acceptChanges(plist, b.params());
1817 // ERT paragraphs have the Language latex_language.
1818 // This is invalid outside of ERT, so we need to
1819 // change it to the buffer language.
1820 for (auto & p : plist)
1821 p.changeLanguage(b.params(), latex_language, b.language());
1823 /* If the inset is the only thing in paragraph and the layout
1824 * is not plain, then the layout of the first paragraph of
1825 * inset should be remembered.
1826 * FIXME: this does not work as expected when change tracking
1827 * is on However, we do not really know what to do in this
1830 DocumentClass const & tclass = cur.buffer()->params().documentClass();
1831 if (inset_it.lastpos() == 1
1832 && plist[0].layout().name() != tclass.plainLayoutName())
1833 cur.paragraph().makeSameLayout(plist[0]);
1835 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1836 b.errorList("Paste"));
1839 // delete the inset now
1840 inset_it.paragraph().eraseChar(inset_it.pos(), b.params().track_changes);
1843 cur.pit() = min(cur.lastpit(), spit);
1844 cur.pos() = min(cur.lastpos(), spos);
1845 // Ensure the current language is set correctly (bug 6292)
1846 cur.text()->setCursor(cur, cur.pit(), cur.pos());
1847 cur.clearSelection();
1849 cur.forceBufferUpdate();
1855 void Text::getWord(CursorSlice & from, CursorSlice & to,
1856 word_location const loc) const
1859 pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1863 void Text::write(ostream & os) const
1865 Buffer const & buf = owner_->buffer();
1866 ParagraphList::const_iterator pit = paragraphs().begin();
1867 ParagraphList::const_iterator end = paragraphs().end();
1869 for (; pit != end; ++pit)
1870 pit->write(os, buf.params(), dth);
1872 // Close begin_deeper
1873 for(; dth > 0; --dth)
1874 os << "\n\\end_deeper";
1878 bool Text::read(Lexer & lex,
1879 ErrorList & errorList, InsetText * insetPtr)
1881 Buffer const & buf = owner_->buffer();
1882 depth_type depth = 0;
1885 while (lex.isOK()) {
1887 string const token = lex.getString();
1892 if (token == "\\end_inset")
1895 if (token == "\\end_body")
1898 if (token == "\\begin_body")
1901 if (token == "\\end_document") {
1906 if (token == "\\begin_layout") {
1907 lex.pushToken(token);
1910 par.setInsetOwner(insetPtr);
1911 par.params().depth(depth);
1912 par.setFont(0, Font(inherit_font, buf.params().language));
1913 pars_.push_back(par);
1914 readParagraph(pars_.back(), lex, errorList);
1916 // register the words in the global word list
1917 pars_.back().updateWords();
1918 } else if (token == "\\begin_deeper") {
1920 } else if (token == "\\end_deeper") {
1922 lex.printError("\\end_deeper: " "depth is already null");
1926 LYXERR0("Handling unknown body token: `" << token << '\'');
1930 // avoid a crash on weird documents (bug 4859)
1931 if (pars_.empty()) {
1933 par.setInsetOwner(insetPtr);
1934 par.params().depth(depth);
1935 par.setFont(0, Font(inherit_font,
1936 buf.params().language));
1937 par.setPlainOrDefaultLayout(buf.params().documentClass());
1938 pars_.push_back(par);
1945 // Returns the current font and depth as a message.
1946 docstring Text::currentState(CursorData const & cur, bool devel_mode) const
1948 LBUFERR(this == cur.text());
1949 Buffer & buf = *cur.buffer();
1950 Paragraph const & par = cur.paragraph();
1951 odocstringstream os;
1953 if (buf.params().track_changes)
1954 os << _("[Change Tracking] ");
1956 Change change = par.lookupChange(cur.pos());
1958 if (change.changed()) {
1959 docstring const author =
1960 buf.params().authors().get(change.author).nameAndEmail();
1961 docstring const date = formatted_datetime(change.changetime);
1962 os << bformat(_("Changed by %1$s[[author]] on %2$s[[date]]. "),
1966 // I think we should only show changes from the default
1968 // No, from the document font (MV)
1969 Font font = cur.real_current_font;
1970 font.fontInfo().reduce(buf.params().getFont().fontInfo());
1972 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1974 // The paragraph depth
1975 int depth = cur.paragraph().getDepth();
1977 os << bformat(_(", Depth: %1$d"), depth);
1979 // The paragraph spacing, but only if different from
1981 Spacing const & spacing = par.params().spacing();
1982 if (!spacing.isDefault()) {
1983 os << _(", Spacing: ");
1984 switch (spacing.getSpace()) {
1985 case Spacing::Single:
1988 case Spacing::Onehalf:
1991 case Spacing::Double:
1994 case Spacing::Other:
1995 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1997 case Spacing::Default:
1998 // should never happen, do nothing
2004 os << _(", Inset: ") << &cur.inset();
2005 if (cur.lastidx() > 0)
2006 os << _(", Cell: ") << cur.idx();
2007 os << _(", Paragraph: ") << cur.pit();
2008 os << _(", Id: ") << par.id();
2009 os << _(", Position: ") << cur.pos();
2010 // FIXME: Why is the check for par.size() needed?
2011 // We are called with cur.pos() == par.size() quite often.
2012 if (!par.empty() && cur.pos() < par.size()) {
2013 // Force output of code point, not character
2014 size_t const c = par.getChar(cur.pos());
2015 os << _(", Char: 0x") << hex << c;
2017 os << _(", Boundary: ") << cur.boundary();
2018 // Row & row = cur.textRow();
2019 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2025 docstring Text::getPossibleLabel(DocIterator const & cur) const
2027 pit_type textpit = cur.pit();
2028 Layout const * layout = &(pars_[textpit].layout());
2030 // Will contain the label prefix.
2033 // For captions, we just take the caption type
2034 Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
2035 if (caption_inset) {
2036 string const & ftype = static_cast<InsetCaption *>(caption_inset)->floattype();
2037 FloatList const & fl = cur.buffer()->params().documentClass().floats();
2038 if (fl.typeExist(ftype)) {
2039 Floating const & flt = fl.getType(ftype);
2040 name = from_utf8(flt.refPrefix());
2043 name = from_utf8(ftype.substr(0,3));
2045 // For section, subsection, etc...
2046 if (layout->latextype == LATEX_PARAGRAPH && textpit != 0) {
2047 Layout const * layout2 = &(pars_[textpit - 1].layout());
2048 if (layout2->latextype != LATEX_PARAGRAPH) {
2053 if (layout->latextype != LATEX_PARAGRAPH)
2054 name = layout->refprefix;
2056 // If none of the above worked, see if the inset knows.
2058 InsetLayout const & il = cur.inset().getLayout();
2059 name = il.refprefix();
2064 docstring par_text = pars_[textpit].asString(AS_STR_SKIPDELETE);
2066 // The return string of math matrices might contain linebreaks
2067 par_text = subst(par_text, '\n', '-');
2068 int const numwords = 3;
2069 for (int i = 0; i < numwords; ++i) {
2070 if (par_text.empty())
2073 par_text = split(par_text, head, ' ');
2074 // Is it legal to use spaces in labels ?
2080 // Make sure it isn't too long
2081 unsigned int const max_label_length = 32;
2082 if (text.size() > max_label_length)
2083 text.resize(max_label_length);
2086 text = name + ':' + text;
2088 // We need a unique label
2089 docstring label = text;
2091 while (cur.buffer()->activeLabel(label)) {
2092 label = text + '-' + convert<docstring>(i);
2100 docstring Text::asString(int options) const
2102 return asString(0, pars_.size(), options);
2106 docstring Text::asString(pit_type beg, pit_type end, int options) const
2108 size_t i = size_t(beg);
2109 docstring str = pars_[i].asString(options);
2110 for (++i; i != size_t(end); ++i) {
2112 str += pars_[i].asString(options);
2118 void Text::shortenForOutliner(docstring & str, size_t const maxlen)
2120 support::truncateWithEllipsis(str, maxlen);
2121 for (char_type & c : str)
2122 if (c == L'\n' || c == L'\t')
2127 void Text::forOutliner(docstring & os, size_t const maxlen,
2128 bool const shorten) const
2130 pit_type end = pars_.size() - 1;
2131 if (0 <= end && !pars_[0].labelString().empty())
2132 os += pars_[0].labelString() + ' ';
2133 forOutliner(os, maxlen, 0, end, shorten);
2137 void Text::forOutliner(docstring & os, size_t const maxlen,
2138 pit_type pit_start, pit_type pit_end,
2139 bool const shorten) const
2141 size_t tmplen = shorten ? maxlen + 1 : maxlen;
2142 pit_type end = min(size_t(pit_end), pars_.size() - 1);
2144 for (pit_type i = pit_start; i <= end && os.length() < tmplen; ++i) {
2147 // This function lets the first label be treated separately
2148 pars_[i].forOutliner(os, tmplen, false, !first);
2152 shortenForOutliner(os, maxlen);
2156 void Text::charsTranspose(Cursor & cur)
2158 LBUFERR(this == cur.text());
2160 pos_type pos = cur.pos();
2162 // If cursor is at beginning or end of paragraph, do nothing.
2163 if (pos == cur.lastpos() || pos == 0)
2166 Paragraph & par = cur.paragraph();
2168 // Get the positions of the characters to be transposed.
2169 pos_type pos1 = pos - 1;
2170 pos_type pos2 = pos;
2172 // In change tracking mode, ignore deleted characters.
2173 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2175 if (pos2 == cur.lastpos())
2178 while (pos1 >= 0 && par.isDeleted(pos1))
2183 // Don't do anything if one of the "characters" is not regular text.
2184 if (par.isInset(pos1) || par.isInset(pos2))
2187 // Store the characters to be transposed (including font information).
2188 char_type const char1 = par.getChar(pos1);
2190 par.getFontSettings(cur.buffer()->params(), pos1);
2192 char_type const char2 = par.getChar(pos2);
2194 par.getFontSettings(cur.buffer()->params(), pos2);
2196 // And finally, we are ready to perform the transposition.
2197 // Track the changes if Change Tracking is enabled.
2198 bool const trackChanges = cur.buffer()->params().track_changes;
2202 par.eraseChar(pos2, trackChanges);
2203 par.eraseChar(pos1, trackChanges);
2204 par.insertChar(pos1, char2, font2, trackChanges);
2205 par.insertChar(pos2, char1, font1, trackChanges);
2207 cur.checkBufferStructure();
2209 // After the transposition, move cursor to after the transposition.
2210 setCursor(cur, cur.pit(), pos2);
2215 DocIterator Text::macrocontextPosition() const
2217 return macrocontext_position_;
2221 void Text::setMacrocontextPosition(DocIterator const & pos)
2223 macrocontext_position_ = pos;
2227 docstring Text::previousWord(CursorSlice const & sl) const
2229 CursorSlice from = sl;
2230 CursorSlice to = sl;
2231 getWord(from, to, PREVIOUS_WORD);
2232 if (sl == from || to == from)
2235 Paragraph const & par = sl.paragraph();
2236 return par.asString(from.pos(), to.pos());
2240 bool Text::completionSupported(Cursor const & cur) const
2242 Paragraph const & par = cur.paragraph();
2243 return !cur.selection()
2245 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2246 && !par.isWordSeparator(cur.pos() - 1);
2250 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2252 WordList const & list = theWordList(cur.getFont().language()->lang());
2253 return new TextCompletionList(cur, list);
2257 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2259 LBUFERR(cur.bv().cursor() == cur);
2261 cur.bv().cursor() = cur;
2262 if (!(cur.result().screenUpdate() & Update::Force))
2263 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2268 docstring Text::completionPrefix(Cursor const & cur) const
2270 return previousWord(cur.top());