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"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
48 #include "WordLangTuple.h"
51 #include "insets/InsetText.h"
52 #include "insets/InsetBibitem.h"
53 #include "insets/InsetCaption.h"
54 #include "insets/InsetNewline.h"
55 #include "insets/InsetNewpage.h"
56 #include "insets/InsetArgument.h"
57 #include "insets/InsetIPAMacro.h"
58 #include "insets/InsetSpace.h"
59 #include "insets/InsetSpecialChar.h"
60 #include "insets/InsetTabular.h"
62 #include "support/convert.h"
63 #include "support/debug.h"
64 #include "support/docstream.h"
65 #include "support/gettext.h"
66 #include "support/lassert.h"
67 #include "support/Length.h"
68 #include "support/lstrings.h"
69 #include "support/lyxalgo.h"
70 #include "support/lyxtime.h"
71 #include "support/textutils.h"
72 #include "support/unique_ptr.h"
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(pars.iterator_at(pit + 1), Paragraph());
117 Paragraph & par = pars[pit];
119 tmp.setInsetOwner(&par.inInset());
120 tmp.makeSameLayout(par);
122 LASSERT(pos <= par.size(), return);
124 if (pos < par.size()) {
125 // move everything behind the break position to the new paragraph
126 pos_type pos_end = par.size() - 1;
128 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
129 if (moveItem(par, pos, tmp, j, bparams)) {
133 // Move over the end-of-par change information
134 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
135 par.setChange(par.size(), Change(bparams.track_changes ?
136 Change::INSERTED : Change::UNCHANGED));
141 void mergeParagraph(BufferParams const & bparams,
142 ParagraphList & pars, pit_type par_offset)
144 Paragraph & next = pars[par_offset + 1];
145 Paragraph & par = pars[par_offset];
147 pos_type pos_end = next.size() - 1;
148 pos_type pos_insert = par.size();
150 // the imaginary end-of-paragraph character (at par.size()) has to be
151 // marked as unmodified. Otherwise, its change is adopted by the first
152 // character of the next paragraph.
153 if (par.isChanged(par.size())) {
154 LYXERR(Debug::CHANGES,
155 "merging par with inserted/deleted end-of-par character");
156 par.setChange(par.size(), Change(Change::UNCHANGED));
159 Change change = next.lookupChange(next.size());
161 // move the content of the second paragraph to the end of the first one
162 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
163 if (moveItem(next, 0, par, j, bparams)) {
168 // move the change of the end-of-paragraph character
169 par.setChange(par.size(), change);
171 pars.erase(pars.iterator_at(par_offset + 1));
175 Text::Text(InsetText * owner, bool use_default_layout)
178 pars_.push_back(Paragraph());
179 Paragraph & par = pars_.back();
180 par.setInsetOwner(owner);
181 DocumentClass const & dc = owner->buffer().params().documentClass();
182 if (use_default_layout)
183 par.setDefaultLayout(dc);
185 par.setPlainLayout(dc);
189 Text::Text(InsetText * owner, Text const & text)
190 : owner_(owner), pars_(text.pars_)
192 for (auto & p : pars_)
193 p.setInsetOwner(owner);
197 pit_type Text::depthHook(pit_type pit, depth_type depth) const
199 pit_type newpit = pit;
204 while (newpit != 0 && pars_[newpit].getDepth() > depth)
207 if (pars_[newpit].getDepth() > depth)
214 pit_type Text::outerHook(pit_type par_offset) const
216 Paragraph const & par = pars_[par_offset];
218 if (par.getDepth() == 0)
220 return depthHook(par_offset, depth_type(par.getDepth() - 1));
224 bool Text::isFirstInSequence(pit_type par_offset) const
226 Paragraph const & par = pars_[par_offset];
228 pit_type dhook_offset = depthHook(par_offset, par.getDepth());
230 if (dhook_offset == par_offset)
233 Paragraph const & dhook = pars_[dhook_offset];
235 return dhook.layout() != par.layout()
236 || dhook.getDepth() != par.getDepth();
240 pit_type Text::lastInSequence(pit_type pit) const
242 depth_type const depth = pars_[pit].getDepth();
243 pit_type newpit = pit;
245 while (size_t(newpit + 1) < pars_.size() &&
246 (pars_[newpit + 1].getDepth() > depth ||
247 (pars_[newpit + 1].getDepth() == depth &&
248 pars_[newpit + 1].layout() == pars_[pit].layout())))
255 int Text::getTocLevel(pit_type par_offset) const
257 Paragraph const & par = pars_[par_offset];
259 if (par.layout().isEnvironment() && !isFirstInSequence(par_offset))
260 return Layout::NOT_IN_TOC;
262 return par.layout().toclevel;
266 Font const Text::outerFont(pit_type par_offset) const
268 depth_type par_depth = pars_[par_offset].getDepth();
269 FontInfo tmpfont = inherit_font;
270 depth_type prev_par_depth = 0;
271 // Resolve against environment font information
272 while (par_offset != pit_type(pars_.size())
273 && par_depth != prev_par_depth
275 && !tmpfont.resolved()) {
276 prev_par_depth = par_depth;
277 par_offset = outerHook(par_offset);
278 if (par_offset != pit_type(pars_.size())) {
279 tmpfont.realize(pars_[par_offset].layout().font);
280 par_depth = pars_[par_offset].getDepth();
284 return Font(tmpfont);
288 int Text::getEndLabel(pit_type p) const
291 depth_type par_depth = pars_[p].getDepth();
292 while (pit != pit_type(pars_.size())) {
293 Layout const & layout = pars_[pit].layout();
294 int const endlabeltype = layout.endlabeltype;
296 if (endlabeltype != END_LABEL_NO_LABEL) {
297 if (p + 1 == pit_type(pars_.size()))
300 depth_type const next_depth =
301 pars_[p + 1].getDepth();
302 if (par_depth > next_depth ||
303 (par_depth == next_depth && layout != pars_[p + 1].layout()))
309 pit = outerHook(pit);
310 if (pit != pit_type(pars_.size()))
311 par_depth = pars_[pit].getDepth();
313 return END_LABEL_NO_LABEL;
317 static void acceptOrRejectChanges(ParagraphList & pars,
318 BufferParams const & bparams, Text::ChangeOp op)
320 pit_type pars_size = static_cast<pit_type>(pars.size());
322 // first, accept or reject changes within each individual
323 // paragraph (do not consider end-of-par)
324 for (pit_type pit = 0; pit < pars_size; ++pit) {
325 // prevent assertion failure
326 if (!pars[pit].empty()) {
327 if (op == Text::ACCEPT)
328 pars[pit].acceptChanges(0, pars[pit].size());
330 pars[pit].rejectChanges(0, pars[pit].size());
334 // next, accept or reject imaginary end-of-par characters
335 for (pit_type pit = 0; pit < pars_size; ++pit) {
336 pos_type pos = pars[pit].size();
337 if (pars[pit].isChanged(pos)) {
338 // keep the end-of-par char if it is inserted and accepted
339 // or when it is deleted and rejected.
340 if (pars[pit].isInserted(pos) == (op == Text::ACCEPT)) {
341 pars[pit].setChange(pos, Change(Change::UNCHANGED));
343 if (pit == pars_size - 1) {
344 // we cannot remove a par break at the end of the last
345 // paragraph; instead, we mark it unchanged
346 pars[pit].setChange(pos, Change(Change::UNCHANGED));
348 mergeParagraph(bparams, pars, pit);
358 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
360 acceptOrRejectChanges(pars, bparams, Text::ACCEPT);
364 void rejectChanges(ParagraphList & pars, BufferParams const & bparams)
366 acceptOrRejectChanges(pars, bparams, Text::REJECT);
370 InsetText const & Text::inset() const
377 void Text::readParToken(Paragraph & par, Lexer & lex,
378 string const & token, Font & font, Change & change, ErrorList & errorList)
380 Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
381 BufferParams & bp = buf->params();
383 if (token[0] != '\\') {
384 docstring dstr = lex.getDocString();
385 par.appendString(dstr, font, change);
387 } else if (token == "\\begin_layout") {
389 docstring layoutname = lex.getDocString();
391 font = Font(inherit_font, bp.language);
392 change = Change(Change::UNCHANGED);
394 DocumentClass const & tclass = bp.documentClass();
396 if (layoutname.empty())
397 layoutname = tclass.defaultLayoutName();
399 if (owner_->forcePlainLayout()) {
400 // in this case only the empty layout is allowed
401 layoutname = tclass.plainLayoutName();
402 } else if (par.usePlainLayout()) {
403 // in this case, default layout maps to empty layout
404 if (layoutname == tclass.defaultLayoutName())
405 layoutname = tclass.plainLayoutName();
407 // otherwise, the empty layout maps to the default
408 if (layoutname == tclass.plainLayoutName())
409 layoutname = tclass.defaultLayoutName();
412 // When we apply an unknown layout to a document, we add this layout to the textclass
413 // of this document. For example, when you apply class article to a beamer document,
414 // all unknown layouts such as frame will be added to document class article so that
415 // these layouts can keep their original names.
416 bool const added_one = tclass.addLayoutIfNeeded(layoutname);
419 docstring const s = bformat(_("Layout `%1$s' was not found."), layoutname);
420 errorList.push_back(ErrorItem(_("Layout Not Found"), s,
421 {par.id(), 0}, {par.id(), -1}));
424 par.setLayout(bp.documentClass()[layoutname]);
426 // Test whether the layout is obsolete.
427 Layout const & layout = par.layout();
428 if (!layout.obsoleted_by().empty())
429 par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
431 par.params().read(lex);
433 } else if (token == "\\end_layout") {
434 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
435 << "Missing \\begin_layout ?");
436 } else if (token == "\\end_inset") {
437 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
438 << "Missing \\begin_inset ?");
439 } else if (token == "\\begin_inset") {
440 Inset * inset = readInset(lex, buf);
442 par.insertInset(par.size(), inset, font, change);
445 docstring line = lex.getDocString();
446 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
447 {par.id(), 0}, {par.id(), -1}));
449 } else if (token == "\\family") {
451 setLyXFamily(lex.getString(), font.fontInfo());
452 } else if (token == "\\series") {
454 setLyXSeries(lex.getString(), font.fontInfo());
455 } else if (token == "\\shape") {
457 setLyXShape(lex.getString(), font.fontInfo());
458 } else if (token == "\\size") {
460 setLyXSize(lex.getString(), font.fontInfo());
461 } else if (token == "\\lang") {
463 string const tok = lex.getString();
464 Language const * lang = languages.getLanguage(tok);
466 font.setLanguage(lang);
468 font.setLanguage(bp.language);
469 lex.printError("Unknown language `$$Token'");
471 } else if (token == "\\numeric") {
473 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
474 } else if (token == "\\nospellcheck") {
476 font.fontInfo().setNoSpellcheck(setLyXMisc(lex.getString()));
477 } else if (token == "\\emph") {
479 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
480 } else if (token == "\\bar") {
482 string const tok = lex.getString();
485 font.fontInfo().setUnderbar(FONT_ON);
486 else if (tok == "no")
487 font.fontInfo().setUnderbar(FONT_OFF);
488 else if (tok == "default")
489 font.fontInfo().setUnderbar(FONT_INHERIT);
491 lex.printError("Unknown bar font flag "
493 } else if (token == "\\strikeout") {
495 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
496 } else if (token == "\\xout") {
498 font.fontInfo().setXout(setLyXMisc(lex.getString()));
499 } else if (token == "\\uuline") {
501 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
502 } else if (token == "\\uwave") {
504 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
505 } else if (token == "\\noun") {
507 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
508 } else if (token == "\\color") {
510 setLyXColor(lex.getString(), font.fontInfo());
511 } else if (token == "\\SpecialChar" ||
512 (token == "\\SpecialCharNoPassThru" &&
513 !par.layout().pass_thru && !inset().isPassThru())) {
514 auto inset = make_unique<InsetSpecialChar>();
516 inset->setBuffer(*buf);
517 par.insertInset(par.size(), inset.release(), font, change);
518 } else if (token == "\\SpecialCharNoPassThru") {
520 docstring const s = ltrim(lex.getDocString(), "\\");
521 par.insert(par.size(), s, font, change);
522 } else if (token == "\\IPAChar") {
523 auto inset = make_unique<InsetIPAChar>();
525 inset->setBuffer(*buf);
526 par.insertInset(par.size(), inset.release(), font, change);
527 } else if (token == "\\twohyphens" || token == "\\threehyphens") {
528 // Ideally, this should be done by lyx2lyx, but lyx2lyx does not know the
529 // running font and does not know anything about layouts (and CopyStyle).
530 Layout const & layout(par.layout());
531 FontInfo info = font.fontInfo();
532 info.realize(layout.resfont);
533 if (layout.pass_thru || inset().isPassThru() ||
534 info.family() == TYPEWRITER_FAMILY) {
535 if (token == "\\twohyphens")
536 par.insert(par.size(), from_ascii("--"), font, change);
538 par.insert(par.size(), from_ascii("---"), font, change);
540 if (token == "\\twohyphens")
541 par.insertChar(par.size(), 0x2013, font, change);
543 par.insertChar(par.size(), 0x2014, font, change);
545 } else if (token == "\\backslash") {
546 par.appendChar('\\', font, change);
547 } else if (token == "\\LyXTable") {
548 auto inset = make_unique<InsetTabular>(buf);
550 par.insertInset(par.size(), inset.release(), font, change);
551 } else if (token == "\\change_unchanged") {
552 change = Change(Change::UNCHANGED);
553 } else if (token == "\\change_inserted" || token == "\\change_deleted") {
555 istringstream is(lex.getString());
559 BufferParams::AuthorMap const & am = bp.author_map_;
560 if (am.find(aid) == am.end()) {
561 errorList.push_back(ErrorItem(
562 _("Change tracking author index missing"),
563 bformat(_("A change tracking author information for index "
564 "%1$d is missing. This can happen after a wrong "
565 "merge by a version control system. In this case, "
566 "either fix the merge, or have this information "
567 "missing until the corresponding tracked changes "
568 "are merged or this user edits the file again.\n"),
570 {par.id(), par.size()}, {par.id(), par.size() + 1}));
571 bp.addAuthor(Author(aid));
573 if (token == "\\change_inserted")
574 change = Change(Change::INSERTED, am.find(aid)->second, ct);
576 change = Change(Change::DELETED, am.find(aid)->second, ct);
579 errorList.push_back(ErrorItem(_("Unknown token"),
580 bformat(_("Unknown token: %1$s %2$s\n"),
583 {par.id(), 0}, {par.id(), -1}));
588 void Text::readParagraph(Paragraph & par, Lexer & lex,
589 ErrorList & errorList)
592 string token = lex.getString();
594 Change change(Change::UNCHANGED);
597 readParToken(par, lex, token, font, change, errorList);
600 token = lex.getString();
605 if (token == "\\end_layout") {
606 //Ok, paragraph finished
610 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
611 if (token == "\\begin_layout" || token == "\\end_document"
612 || token == "\\end_inset" || token == "\\begin_deeper"
613 || token == "\\end_deeper") {
614 lex.pushToken(token);
615 lyxerr << "Paragraph ended in line "
616 << lex.lineNumber() << "\n"
617 << "Missing \\end_layout.\n";
621 // Final change goes to paragraph break:
622 if (inset().allowMultiPar())
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(pars.iterator_at(par_offset + 1), Paragraph());
696 Paragraph & par = pars[par_offset];
698 // remember to set the inset_owner
699 tmp->setInsetOwner(&par.inInset());
700 // without doing that we get a crash when typing <Return> at the
701 // end of a paragraph
702 tmp->setPlainOrDefaultLayout(bparams.documentClass());
705 tmp->setLayout(par.layout());
706 tmp->setLabelWidthString(par.params().labelWidthString());
707 tmp->params().depth(par.params().depth());
708 } else if (par.params().depth() > 0) {
709 Paragraph const & hook = pars[text.outerHook(par_offset)];
710 tmp->setLayout(hook.layout());
711 // not sure the line below is useful
712 tmp->setLabelWidthString(par.params().labelWidthString());
713 tmp->params().depth(hook.params().depth());
716 bool const isempty = (par.allowEmpty() && par.empty());
718 if (!isempty && (par.size() > pos || par.empty())) {
719 tmp->setLayout(par.layout());
720 tmp->params().align(par.params().align());
721 tmp->setLabelWidthString(par.params().labelWidthString());
723 tmp->params().depth(par.params().depth());
724 tmp->params().noindent(par.params().noindent());
725 tmp->params().spacing(par.params().spacing());
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 // The special chars we handle
870 map<wchar_t, InsetSpecialChar::Kind> specialchars;
871 specialchars[0x200c] = InsetSpecialChar::LIGATURE_BREAK;
872 specialchars[0x200b] = InsetSpecialChar::ALLOWBREAK;
873 specialchars[0x2026] = InsetSpecialChar::LDOTS;
874 specialchars[0x2011] = InsetSpecialChar::NOBREAKDASH;
876 // insert the string, don't insert doublespace
877 bool space_inserted = true;
878 for (auto const & ch : str) {
879 Paragraph & par = pars_[pit];
881 if (inset().allowMultiPar() && (!par.empty() || par.allowEmpty())) {
882 lyx::breakParagraph(*this, pit, pos,
883 par.layout().isEnvironment());
886 space_inserted = true;
890 // do not insert consecutive spaces if !free_spacing
891 } else if ((ch == ' ' || ch == '\t') &&
892 space_inserted && !par.isFreeSpacing()) {
894 } else if (ch == '\t') {
895 if (!par.isFreeSpacing()) {
896 // tabs are like spaces here
897 par.insertChar(pos, ' ', font, bparams.track_changes);
899 space_inserted = true;
901 par.insertChar(pos, ch, font, bparams.track_changes);
903 space_inserted = true;
905 } else if (specialchars.find(ch) != specialchars.end()) {
906 par.insertInset(pos, new InsetSpecialChar(specialchars.find(ch)->second),
907 font, bparams.track_changes ?
908 Change(Change::INSERTED)
909 : Change(Change::UNCHANGED));
911 space_inserted = false;
912 } else if (!isPrintable(ch)) {
913 // Ignore (other) unprintables
916 // just insert the character
917 par.insertChar(pos, ch, font, bparams.track_changes);
919 space_inserted = (ch == ' ');
922 setCursor(cur, pit, pos);
926 // turn double CR to single CR, others are converted into one
927 // blank. Then insertStringAsLines is called
928 void Text::insertStringAsParagraphs(Cursor & cur, docstring const & str,
931 docstring linestr = str;
932 bool newline_inserted = false;
934 for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
935 if (linestr[i] == '\n') {
936 if (newline_inserted) {
937 // we know that \r will be ignored by
938 // insertStringAsLines. Of course, it is a dirty
939 // trick, but it works...
940 linestr[i - 1] = '\r';
944 newline_inserted = true;
946 } else if (isPrintable(linestr[i])) {
947 newline_inserted = false;
950 insertStringAsLines(cur, linestr, font);
956 bool canInsertChar(Cursor const & cur, char_type c)
958 Paragraph const & par = cur.paragraph();
959 // If not in free spacing mode, check if there will be two blanks together or a blank at
960 // the beginning of a paragraph.
961 if (!par.isFreeSpacing() && isLineSeparatorChar(c)) {
962 if (cur.pos() == 0) {
964 "You cannot insert a space at the "
965 "beginning of a paragraph. Please read the Tutorial."));
968 // If something is wrong, ignore this character.
969 LASSERT(cur.pos() > 0, return false);
970 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
971 && !par.isDeleted(cur.pos() - 1)) {
973 "You cannot type two spaces this way. "
974 "Please read the Tutorial."));
979 // Prevent to insert uncodable characters in verbatim and ERT.
980 // The encoding is inherited from the context here.
981 if (par.isPassThru() && cur.getEncoding()) {
982 Encoding const * e = cur.getEncoding();
983 if (!e->encodable(c)) {
984 cur.message(_("Character is uncodable in this verbatim context."));
994 // insert a character, moves all the following breaks in the
995 // same Paragraph one to the right and make a rebreak
996 void Text::insertChar(Cursor & cur, char_type c)
998 LBUFERR(this == cur.text());
1000 if (!canInsertChar(cur,c))
1003 cur.recordUndo(INSERT_UNDO);
1005 TextMetrics const & tm = cur.bv().textMetrics(this);
1006 Buffer const & buffer = *cur.buffer();
1007 Paragraph & par = cur.paragraph();
1008 // try to remove this
1009 pit_type const pit = cur.pit();
1011 if (lyxrc.auto_number) {
1012 static docstring const number_operators = from_ascii("+-/*");
1013 static docstring const number_unary_operators = from_ascii("+-");
1015 // Common Number Separators: comma, dot etc.
1016 // European Number Terminators: percent, permille, degree, euro etc.
1017 if (cur.current_font.fontInfo().number() == FONT_ON) {
1018 if (!isDigitASCII(c) && !contains(number_operators, c) &&
1019 !(isCommonNumberSeparator(c) &&
1021 cur.pos() != cur.lastpos() &&
1022 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
1023 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON) &&
1024 !(isEuropeanNumberTerminator(c) &&
1026 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
1027 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
1029 number(cur); // Set current_font.number to OFF
1030 } else if (isDigitASCII(c) &&
1031 cur.real_current_font.isVisibleRightToLeft()) {
1032 number(cur); // Set current_font.number to ON
1034 if (cur.pos() != 0) {
1035 char_type const ch = par.getChar(cur.pos() - 1);
1036 if (contains(number_unary_operators, ch) &&
1038 || par.isSeparator(cur.pos() - 2)
1039 || par.isEnvSeparator(cur.pos() - 2)
1040 || par.isNewline(cur.pos() - 2))
1042 setCharFont(pit, cur.pos() - 1, cur.current_font,
1044 } else if (isCommonNumberSeparator(ch)
1046 && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
1047 setCharFont(pit, cur.pos() - 1, cur.current_font,
1054 // In Bidi text, we want spaces to be treated in a special way: spaces
1055 // which are between words in different languages should get the
1056 // paragraph's language; otherwise, spaces should keep the language
1057 // they were originally typed in. This is only in effect while typing;
1058 // after the text is already typed in, the user can always go back and
1059 // explicitly set the language of a space as desired. But 99.9% of the
1060 // time, what we're doing here is what the user actually meant.
1062 // The following cases are the ones in which the language of the space
1063 // should be changed to match that of the containing paragraph. In the
1064 // depictions, lowercase is LTR, uppercase is RTL, underscore (_)
1065 // represents a space, pipe (|) represents the cursor position (so the
1066 // character before it is the one just typed in). The different cases
1067 // are depicted logically (not visually), from left to right:
1072 // Theoretically, there are other situations that we should, perhaps, deal
1073 // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any
1074 // point (to understand why, just try to create this situation...).
1076 if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
1077 // get font in front and behind the space in question. But do NOT
1078 // use getFont(cur.pos()) because the character c is not inserted yet
1079 Font const pre_space_font = tm.displayFont(cur.pit(), cur.pos() - 2);
1080 Font const & post_space_font = cur.real_current_font;
1081 bool pre_space_rtl = pre_space_font.isVisibleRightToLeft();
1082 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
1084 if (pre_space_rtl != post_space_rtl) {
1085 // Set the space's language to match the language of the
1086 // adjacent character whose direction is the paragraph's
1087 // direction; don't touch other properties of the font
1088 Language const * lang =
1089 (pre_space_rtl == par.isRTL(buffer.params())) ?
1090 pre_space_font.language() : post_space_font.language();
1092 Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
1093 space_font.setLanguage(lang);
1094 par.setFont(cur.pos() - 1, space_font);
1098 pos_type pos = cur.pos();
1099 if (!cur.paragraph().isPassThru() && owner_->lyxCode() != IPA_CODE &&
1100 cur.real_current_font.fontInfo().family() != TYPEWRITER_FAMILY &&
1101 c == '-' && pos > 0) {
1102 if (par.getChar(pos - 1) == '-') {
1103 // convert "--" to endash
1104 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1107 } else if (par.getChar(pos - 1) == 0x2013) {
1108 // convert "---" to emdash
1109 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1115 par.insertChar(pos, c, cur.current_font,
1116 cur.buffer()->params().track_changes);
1117 cur.checkBufferStructure();
1119 // cur.screenUpdateFlags(Update::Force);
1120 bool boundary = cur.boundary()
1121 || tm.isRTLBoundary(cur.pit(), pos + 1);
1122 setCursor(cur, cur.pit(), pos + 1, false, boundary);
1127 void Text::charInserted(Cursor & cur)
1129 Paragraph & par = cur.paragraph();
1131 // register word if a non-letter was entered
1133 && !par.isWordSeparator(cur.pos() - 2)
1134 && par.isWordSeparator(cur.pos() - 1)) {
1135 // get the word in front of cursor
1136 LBUFERR(this == cur.text());
1137 cur.paragraph().updateWords();
1142 // the cursor set functions have a special mechanism. When they
1143 // realize, that you left an empty paragraph, they will delete it.
1145 bool Text::cursorForwardOneWord(Cursor & cur)
1147 LBUFERR(this == cur.text());
1149 if (lyxrc.mac_like_cursor_movement) {
1150 DocIterator dit(cur);
1151 DocIterator prv(cur);
1152 bool inword = false;
1153 bool intext = dit.inTexted();
1154 while (!dit.atEnd()) {
1155 if (dit.inTexted()) { // no paragraphs in mathed
1156 Paragraph const & par = dit.paragraph();
1157 pos_type const pos = dit.pos();
1159 if (!par.isDeleted(pos)) {
1160 bool wordsep = par.isWordSeparator(pos);
1161 if (inword && wordsep)
1162 break; // stop at word end
1163 else if (!inword && !wordsep)
1167 } else if (intext) {
1168 // move to end of math
1169 while (!dit.inTexted() && !dit.atEnd()) dit.forwardPos();
1173 dit.forwardPosIgnoreCollapsed();
1175 if (dit.atEnd()) dit = prv;
1176 if (dit == cur) return false; // we didn't move
1179 // see comment above
1180 cur.bv().checkDepm(cur, orig);
1183 pos_type const lastpos = cur.lastpos();
1184 pit_type pit = cur.pit();
1185 pos_type pos = cur.pos();
1186 Paragraph const & par = cur.paragraph();
1188 // Paragraph boundary is a word boundary
1189 if (pos == lastpos || (pos + 1 == lastpos && par.isEnvSeparator(pos))) {
1190 if (pit != cur.lastpit())
1191 return setCursor(cur, pit + 1, 0);
1196 LASSERT(pos < lastpos, return false); // see above
1197 if (!par.isWordSeparator(pos))
1198 while (pos != lastpos && !par.isWordSeparator(pos))
1200 else if (par.isChar(pos))
1201 while (pos != lastpos && par.isChar(pos))
1203 else if (!par.isSpace(pos)) // non-char inset
1206 // Skip over white space
1207 while (pos != lastpos && par.isSpace(pos))
1210 // Don't skip a separator inset at the end of a paragraph
1211 if (pos == lastpos && pos && par.isEnvSeparator(pos - 1))
1214 return setCursor(cur, pit, pos);
1219 bool Text::cursorBackwardOneWord(Cursor & cur)
1221 LBUFERR(this == cur.text());
1223 if (lyxrc.mac_like_cursor_movement) {
1224 DocIterator dit(cur);
1225 bool inword = false;
1226 bool intext = dit.inTexted();
1227 while (!dit.atBegin()) {
1228 DocIterator prv(dit);
1229 dit.backwardPosIgnoreCollapsed();
1230 if (dit.inTexted()) { // no paragraphs in mathed
1231 Paragraph const & par = dit.paragraph();
1232 pos_type pos = dit.pos();
1234 if (!par.isDeleted(pos)) {
1235 bool wordsep = par.isWordSeparator(pos);
1236 if (inword && wordsep) {
1238 break; // stop at word begin
1239 } else if (!inword && !wordsep)
1243 } else if (intext) {
1244 // move to begin of math
1245 while (!dit.inTexted() && !dit.atBegin()) dit.backwardPos();
1249 if (dit == cur) return false; // we didn't move
1252 // see comment above cursorForwardOneWord
1253 cur.bv().checkDepm(cur, orig);
1256 Paragraph const & par = cur.paragraph();
1257 pit_type const pit = cur.pit();
1258 pos_type pos = cur.pos();
1260 // Paragraph boundary is a word boundary
1261 if (pos == 0 && pit != 0) {
1262 Paragraph & prevpar = getPar(pit - 1);
1263 pos = prevpar.size();
1264 // Don't stop after an environment separator
1265 if (pos && prevpar.isEnvSeparator(pos - 1))
1267 return setCursor(cur, pit - 1, pos);
1269 // Skip over white space
1270 while (pos != 0 && par.isSpace(pos - 1))
1273 if (pos != 0 && !par.isWordSeparator(pos - 1))
1274 while (pos != 0 && !par.isWordSeparator(pos - 1))
1276 else if (pos != 0 && par.isChar(pos - 1))
1277 while (pos != 0 && par.isChar(pos - 1))
1279 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1282 return setCursor(cur, pit, pos);
1287 bool Text::cursorVisLeftOneWord(Cursor & cur)
1289 LBUFERR(this == cur.text());
1291 pos_type left_pos, right_pos;
1293 Cursor temp_cur = cur;
1295 // always try to move at least once...
1296 while (temp_cur.posVisLeft(true /* skip_inset */)) {
1298 // collect some information about current cursor position
1299 temp_cur.getSurroundingPos(left_pos, right_pos);
1300 bool left_is_letter =
1301 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1302 bool right_is_letter =
1303 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1305 // if we're not at a letter/non-letter boundary, continue moving
1306 if (left_is_letter == right_is_letter)
1309 // we should stop when we have an LTR word on our right or an RTL word
1311 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1312 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1313 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1314 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1318 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1319 true, temp_cur.boundary());
1323 bool Text::cursorVisRightOneWord(Cursor & cur)
1325 LBUFERR(this == cur.text());
1327 pos_type left_pos, right_pos;
1329 Cursor temp_cur = cur;
1331 // always try to move at least once...
1332 while (temp_cur.posVisRight(true /* skip_inset */)) {
1334 // collect some information about current cursor position
1335 temp_cur.getSurroundingPos(left_pos, right_pos);
1336 bool left_is_letter =
1337 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1338 bool right_is_letter =
1339 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1341 // if we're not at a letter/non-letter boundary, continue moving
1342 if (left_is_letter == right_is_letter)
1345 // we should stop when we have an LTR word on our right or an RTL word
1347 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1348 temp_cur.buffer()->params(),
1349 left_pos).isRightToLeft())
1350 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1351 temp_cur.buffer()->params(),
1352 right_pos).isRightToLeft()))
1356 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1357 true, temp_cur.boundary());
1361 void Text::selectWord(Cursor & cur, word_location loc)
1363 LBUFERR(this == cur.text());
1364 CursorSlice from = cur.top();
1366 getWord(from, to, loc);
1367 if (cur.top() != from)
1368 setCursor(cur, from.pit(), from.pos());
1371 if (!cur.selection())
1373 setCursor(cur, to.pit(), to.pos());
1375 cur.setWordSelection(true);
1379 void Text::selectAll(Cursor & cur)
1381 LBUFERR(this == cur.text());
1382 if (cur.lastpos() == 0 && cur.lastpit() == 0)
1384 // If the cursor is at the beginning, make sure the cursor ends there
1385 if (cur.pit() == 0 && cur.pos() == 0) {
1386 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1388 setCursor(cur, 0, 0);
1390 setCursor(cur, 0, 0);
1392 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1398 // Select the word currently under the cursor when no
1399 // selection is currently set
1400 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1402 LBUFERR(this == cur.text());
1403 if (cur.selection())
1405 selectWord(cur, loc);
1406 return cur.selection();
1410 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1412 LBUFERR(this == cur.text());
1414 if (!cur.selection()) {
1415 if (!selectChange(cur))
1419 cur.recordUndoSelection();
1421 pit_type begPit = cur.selectionBegin().pit();
1422 pit_type endPit = cur.selectionEnd().pit();
1424 pos_type begPos = cur.selectionBegin().pos();
1425 pos_type endPos = cur.selectionEnd().pos();
1427 // keep selection info, because endPos becomes invalid after the first loop
1428 bool const endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1430 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1431 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1432 pos_type parSize = pars_[pit].size();
1434 // ignore empty paragraphs; otherwise, an assertion will fail for
1435 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1439 // do not consider first paragraph if the cursor starts at pos size()
1440 if (pit == begPit && begPos == parSize)
1443 // do not consider last paragraph if the cursor ends at pos 0
1444 if (pit == endPit && endPos == 0)
1445 break; // last iteration anyway
1447 pos_type const left = (pit == begPit ? begPos : 0);
1448 pos_type const right = (pit == endPit ? endPos : parSize);
1451 // there is no change here
1455 pars_[pit].acceptChanges(left, right);
1457 pars_[pit].rejectChanges(left, right);
1461 // next, accept/reject imaginary end-of-par characters
1463 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1464 pos_type pos = pars_[pit].size();
1466 // skip if the selection ends before the end-of-par
1467 if (pit == endPit && endsBeforeEndOfPar)
1468 break; // last iteration anyway
1470 // skip if this is not the last paragraph of the document
1471 // note: the user should be able to accept/reject the par break of the last par!
1472 if (pit == endPit && pit + 1 != int(pars_.size()))
1473 break; // last iteration anway
1476 if (pars_[pit].isInserted(pos)) {
1477 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1478 } else if (pars_[pit].isDeleted(pos)) {
1479 if (pit + 1 == int(pars_.size())) {
1480 // we cannot remove a par break at the end of the last paragraph;
1481 // instead, we mark it unchanged
1482 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1484 mergeParagraph(cur.buffer()->params(), pars_, pit);
1490 if (pars_[pit].isDeleted(pos)) {
1491 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1492 } else if (pars_[pit].isInserted(pos)) {
1493 if (pit + 1 == int(pars_.size())) {
1494 // we mark the par break at the end of the last paragraph unchanged
1495 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1497 mergeParagraph(cur.buffer()->params(), pars_, pit);
1505 // finally, invoke the DEPM
1506 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().track_changes);
1509 cur.clearSelection();
1510 setCursorIntern(cur, begPit, begPos);
1511 cur.screenUpdateFlags(Update::Force);
1512 cur.forceBufferUpdate();
1516 void Text::acceptChanges()
1518 BufferParams const & bparams = owner_->buffer().params();
1519 lyx::acceptChanges(pars_, bparams);
1520 deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.track_changes);
1524 void Text::rejectChanges()
1526 BufferParams const & bparams = owner_->buffer().params();
1527 pit_type pars_size = static_cast<pit_type>(pars_.size());
1529 // first, reject changes within each individual paragraph
1530 // (do not consider end-of-par)
1531 for (pit_type pit = 0; pit < pars_size; ++pit) {
1532 if (!pars_[pit].empty()) // prevent assertion failure
1533 pars_[pit].rejectChanges(0, pars_[pit].size());
1536 // next, reject imaginary end-of-par characters
1537 for (pit_type pit = 0; pit < pars_size; ++pit) {
1538 pos_type pos = pars_[pit].size();
1540 if (pars_[pit].isDeleted(pos)) {
1541 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1542 } else if (pars_[pit].isInserted(pos)) {
1543 if (pit == pars_size - 1) {
1544 // we mark the par break at the end of the last
1545 // paragraph unchanged
1546 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1548 mergeParagraph(bparams, pars_, pit);
1555 // finally, invoke the DEPM
1556 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.track_changes);
1560 void Text::deleteWordForward(Cursor & cur, bool const force)
1562 LBUFERR(this == cur.text());
1563 if (cur.lastpos() == 0)
1567 cur.selection(true);
1568 cursorForwardOneWord(cur);
1570 if (force || !cur.confirmDeletion()) {
1571 cutSelection(cur, false);
1572 cur.checkBufferStructure();
1578 void Text::deleteWordBackward(Cursor & cur, bool const force)
1580 LBUFERR(this == cur.text());
1581 if (cur.lastpos() == 0)
1582 cursorBackward(cur);
1585 cur.selection(true);
1586 cursorBackwardOneWord(cur);
1588 if (force || !cur.confirmDeletion()) {
1589 cutSelection(cur, false);
1590 cur.checkBufferStructure();
1596 // Kill to end of line.
1597 void Text::changeCase(Cursor & cur, TextCase action, bool partial)
1599 LBUFERR(this == cur.text());
1603 bool const gotsel = cur.selection();
1605 from = cur.selBegin();
1609 getWord(from, to, partial ? PARTIAL_WORD : WHOLE_WORD);
1610 cursorForwardOneWord(cur);
1613 cur.recordUndoSelection();
1615 pit_type begPit = from.pit();
1616 pit_type endPit = to.pit();
1618 pos_type begPos = from.pos();
1619 pos_type endPos = to.pos();
1621 pos_type right = 0; // needed after the for loop
1623 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1624 Paragraph & par = pars_[pit];
1625 pos_type const pos = (pit == begPit ? begPos : 0);
1626 right = (pit == endPit ? endPos : par.size());
1627 par.changeCase(cur.buffer()->params(), pos, right, action);
1630 // the selection may have changed due to logically-only deleted chars
1632 setCursor(cur, begPit, begPos);
1634 setCursor(cur, endPit, right);
1637 setCursor(cur, endPit, right);
1639 cur.checkBufferStructure();
1643 bool Text::handleBibitems(Cursor & cur)
1645 if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1651 BufferParams const & bufparams = cur.buffer()->params();
1652 Paragraph const & par = cur.paragraph();
1653 Cursor prevcur = cur;
1654 if (cur.pit() > 0) {
1656 prevcur.pos() = prevcur.lastpos();
1658 Paragraph const & prevpar = prevcur.paragraph();
1660 // if a bibitem is deleted, merge with previous paragraph
1661 // if this is a bibliography item as well
1662 if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1663 cur.recordUndo(prevcur.pit());
1664 mergeParagraph(bufparams, cur.text()->paragraphs(),
1666 cur.forceBufferUpdate();
1667 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1668 cur.screenUpdateFlags(Update::Force);
1672 // otherwise reset to default
1673 cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1678 bool Text::erase(Cursor & cur)
1680 LASSERT(this == cur.text(), return false);
1681 bool needsUpdate = false;
1682 Paragraph & par = cur.paragraph();
1684 if (cur.pos() != cur.lastpos()) {
1685 // this is the code for a normal delete, not pasting
1687 cur.recordUndo(DELETE_UNDO);
1688 bool const was_inset = cur.paragraph().isInset(cur.pos());
1689 if(!par.eraseChar(cur.pos(), cur.buffer()->params().track_changes))
1690 // the character has been logically deleted only => skip it
1691 cur.top().forwardPos();
1694 cur.forceBufferUpdate();
1696 cur.checkBufferStructure();
1699 if (cur.pit() == cur.lastpit())
1700 return dissolveInset(cur);
1702 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1703 cur.recordUndo(DELETE_UNDO);
1704 par.setChange(cur.pos(), Change(Change::DELETED));
1708 setCursorIntern(cur, cur.pit() + 1, 0);
1709 needsUpdate = backspacePos0(cur);
1713 needsUpdate |= handleBibitems(cur);
1716 // Make sure the cursor is correct. Is this really needed?
1717 // No, not really... at least not here!
1718 cur.top().setPitPos(cur.pit(), cur.pos());
1719 cur.checkBufferStructure();
1726 bool Text::backspacePos0(Cursor & cur)
1728 LBUFERR(this == cur.text());
1732 BufferParams const & bufparams = cur.buffer()->params();
1733 ParagraphList & plist = cur.text()->paragraphs();
1734 Paragraph const & par = cur.paragraph();
1735 Cursor prevcur = cur;
1737 prevcur.pos() = prevcur.lastpos();
1738 Paragraph const & prevpar = prevcur.paragraph();
1740 // is it an empty paragraph?
1741 if (cur.lastpos() == 0
1742 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1743 cur.recordUndo(prevcur.pit());
1744 plist.erase(plist.iterator_at(cur.pit()));
1746 // is previous par empty?
1747 else if (prevcur.lastpos() == 0
1748 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1749 cur.recordUndo(prevcur.pit());
1750 plist.erase(plist.iterator_at(prevcur.pit()));
1752 // FIXME: Do we really not want to allow this???
1753 // Pasting is not allowed, if the paragraphs have different
1754 // layouts. I think it is a real bug of all other
1755 // word processors to allow it. It confuses the user.
1756 // Correction: Pasting is always allowed with standard-layout
1757 // or the empty layout.
1759 cur.recordUndo(prevcur.pit());
1760 mergeParagraph(bufparams, plist, prevcur.pit());
1763 cur.forceBufferUpdate();
1764 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1770 bool Text::backspace(Cursor & cur)
1772 LBUFERR(this == cur.text());
1773 bool needsUpdate = false;
1774 if (cur.pos() == 0) {
1776 return dissolveInset(cur);
1778 Cursor prev_cur = cur;
1781 if (cur.paragraph().size() > 0
1782 && !prev_cur.paragraph().isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1783 cur.recordUndo(prev_cur.pit(), prev_cur.pit());
1784 prev_cur.paragraph().setChange(prev_cur.lastpos(), Change(Change::DELETED));
1785 setCursorIntern(cur, prev_cur.pit(), prev_cur.lastpos());
1788 // The cursor is at the beginning of a paragraph, so
1789 // the backspace will collapse two paragraphs into one.
1790 needsUpdate = backspacePos0(cur);
1793 // this is the code for a normal backspace, not pasting
1795 cur.recordUndo(DELETE_UNDO);
1796 // We used to do cursorBackwardIntern() here, but it is
1797 // not a good idea since it triggers the auto-delete
1798 // mechanism. So we do a cursorBackwardIntern()-lite,
1799 // without the dreaded mechanism. (JMarc)
1800 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1801 false, cur.boundary());
1802 bool const was_inset = cur.paragraph().isInset(cur.pos());
1803 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().track_changes);
1805 cur.forceBufferUpdate();
1807 cur.checkBufferStructure();
1810 if (cur.pos() == cur.lastpos())
1811 cur.setCurrentFont();
1813 needsUpdate |= handleBibitems(cur);
1815 // A singlePar update is not enough in this case.
1816 // cur.screenUpdateFlags(Update::Force);
1817 cur.top().setPitPos(cur.pit(), cur.pos());
1823 bool Text::dissolveInset(Cursor & cur)
1825 LASSERT(this == cur.text(), return false);
1827 if (isMainText() || cur.inset().nargs() != 1)
1830 cur.recordUndoInset();
1832 cur.selHandle(false);
1833 // save position inside inset
1834 pos_type spos = cur.pos();
1835 pit_type spit = cur.pit();
1836 bool const inset_non_empty = cur.lastpit() != 0 || cur.lastpos() != 0;
1838 // update cursor offset
1842 // remember position outside inset to delete inset later
1843 // we do not do it now to avoid memory reuse issues (see #10667).
1844 DocIterator inset_it = cur;
1848 Buffer & b = *cur.buffer();
1849 // Is there anything in this text?
1850 if (inset_non_empty) {
1852 // we clear the cache so that we won't get conflicts with labels
1853 // that get pasted into the buffer. we should update this before
1854 // its being empty matters. if not (i.e., if we encounter bugs),
1855 // then this should instead be:
1856 // cur.buffer().updateBuffer();
1857 // but we'll try the cheaper solution here.
1858 cur.buffer()->clearReferenceCache();
1860 ParagraphList & plist = paragraphs();
1861 if (!lyxrc.ct_markup_copied)
1862 // Do not revive deleted text
1863 lyx::acceptChanges(plist, b.params());
1865 // ERT paragraphs have the Language latex_language.
1866 // This is invalid outside of ERT, so we need to
1867 // change it to the buffer language.
1868 for (auto & p : plist)
1869 p.changeLanguage(b.params(), latex_language, b.language());
1871 /* If the inset is the only thing in paragraph and the layout
1872 * is not plain, then the layout of the first paragraph of
1873 * inset should be remembered.
1874 * FIXME: this does not work as expected when change tracking
1875 * is on However, we do not really know what to do in this
1878 DocumentClass const & tclass = cur.buffer()->params().documentClass();
1879 if (inset_it.lastpos() == 1
1880 && !tclass.isPlainLayout(plist[0].layout())
1881 && !tclass.isDefaultLayout(plist[0].layout()))
1882 cur.paragraph().makeSameLayout(plist[0]);
1884 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1885 b.errorList("Paste"));
1888 // delete the inset now
1889 inset_it.paragraph().eraseChar(inset_it.pos(), b.params().track_changes);
1892 cur.pit() = min(cur.lastpit(), spit);
1893 cur.pos() = min(cur.lastpos(), spos);
1894 // Ensure the current language is set correctly (bug 6292)
1895 cur.text()->setCursor(cur, cur.pit(), cur.pos());
1896 cur.clearSelection();
1898 cur.forceBufferUpdate();
1904 void Text::getWord(CursorSlice & from, CursorSlice & to,
1905 word_location const loc) const
1908 pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1912 void Text::write(ostream & os) const
1914 Buffer const & buf = owner_->buffer();
1915 ParagraphList::const_iterator pit = paragraphs().begin();
1916 ParagraphList::const_iterator end = paragraphs().end();
1918 for (; pit != end; ++pit)
1919 pit->write(os, buf.params(), dth);
1921 // Close begin_deeper
1922 for(; dth > 0; --dth)
1923 os << "\n\\end_deeper";
1927 bool Text::read(Lexer & lex,
1928 ErrorList & errorList, InsetText * insetPtr)
1930 Buffer const & buf = owner_->buffer();
1931 depth_type depth = 0;
1934 while (lex.isOK()) {
1936 string const token = lex.getString();
1941 if (token == "\\end_inset")
1944 if (token == "\\end_body")
1947 if (token == "\\begin_body")
1950 if (token == "\\end_document") {
1955 if (token == "\\begin_layout") {
1956 lex.pushToken(token);
1959 par.setInsetOwner(insetPtr);
1960 par.params().depth(depth);
1961 par.setFont(0, Font(inherit_font, buf.params().language));
1962 pars_.push_back(par);
1963 readParagraph(pars_.back(), lex, errorList);
1965 // register the words in the global word list
1966 pars_.back().updateWords();
1967 } else if (token == "\\begin_deeper") {
1969 } else if (token == "\\end_deeper") {
1971 lex.printError("\\end_deeper: " "depth is already null");
1975 LYXERR0("Handling unknown body token: `" << token << '\'');
1979 // avoid a crash on weird documents (bug 4859)
1980 if (pars_.empty()) {
1982 par.setInsetOwner(insetPtr);
1983 par.params().depth(depth);
1984 par.setFont(0, Font(inherit_font,
1985 buf.params().language));
1986 par.setPlainOrDefaultLayout(buf.params().documentClass());
1987 pars_.push_back(par);
1994 // Returns the current state (font, depth etc.) as a message for status bar.
1995 docstring Text::currentState(CursorData const & cur, bool devel_mode) const
1997 LBUFERR(this == cur.text());
1998 Buffer & buf = *cur.buffer();
1999 Paragraph const & par = cur.paragraph();
2000 odocstringstream os;
2002 if (buf.params().track_changes)
2003 os << _("[Change Tracking] ");
2005 Change change = par.lookupChange(cur.pos());
2007 if (change.changed()) {
2008 docstring const author =
2009 buf.params().authors().get(change.author).nameAndEmail();
2010 docstring const date = formatted_datetime(change.changetime);
2011 os << bformat(_("Changed by %1$s[[author]] on %2$s[[date]]. "),
2015 // I think we should only show changes from the default
2017 // No, from the document font (MV)
2018 Font font = cur.real_current_font;
2019 font.fontInfo().reduce(buf.params().getFont().fontInfo());
2021 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
2023 // The paragraph depth
2024 int depth = par.getDepth();
2026 os << bformat(_(", Depth: %1$d"), depth);
2028 // The paragraph spacing, but only if different from
2030 Spacing const & spacing = par.params().spacing();
2031 if (!spacing.isDefault()) {
2032 os << _(", Spacing: ");
2033 switch (spacing.getSpace()) {
2034 case Spacing::Single:
2037 case Spacing::Onehalf:
2040 case Spacing::Double:
2043 case Spacing::Other:
2044 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
2046 case Spacing::Default:
2047 // should never happen, do nothing
2052 // Custom text style
2053 InsetLayout const & layout = cur.inset().getLayout();
2054 if (layout.lyxtype() == InsetLayout::CHARSTYLE)
2055 os << _(", Style: ") << translateIfPossible(layout.labelstring());
2058 os << _(", Inset: ") << &cur.inset();
2059 if (cur.lastidx() > 0)
2060 os << _(", Cell: ") << cur.idx();
2061 os << _(", Paragraph: ") << cur.pit();
2062 os << _(", Id: ") << par.id();
2063 os << _(", Position: ") << cur.pos();
2064 // FIXME: Why is the check for par.size() needed?
2065 // We are called with cur.pos() == par.size() quite often.
2066 if (!par.empty() && cur.pos() < par.size()) {
2067 // Force output of code point, not character
2068 size_t const c = par.getChar(cur.pos());
2069 os << _(", Char: 0x") << hex << c;
2071 os << _(", Boundary: ") << cur.boundary();
2072 // Row & row = cur.textRow();
2073 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2079 docstring Text::getPossibleLabel(DocIterator const & cur) const
2081 pit_type textpit = cur.pit();
2082 Layout const * layout = &(pars_[textpit].layout());
2084 // Will contain the label prefix.
2087 // For captions, we just take the caption type
2088 Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
2089 if (caption_inset) {
2090 string const & ftype = static_cast<InsetCaption *>(caption_inset)->floattype();
2091 FloatList const & fl = cur.buffer()->params().documentClass().floats();
2092 if (fl.typeExist(ftype)) {
2093 Floating const & flt = fl.getType(ftype);
2094 name = from_utf8(flt.refPrefix());
2097 name = from_utf8(ftype.substr(0,3));
2099 // For section, subsection, etc...
2100 if (layout->latextype == LATEX_PARAGRAPH && textpit != 0) {
2101 Layout const * layout2 = &(pars_[textpit - 1].layout());
2102 if (layout2->latextype != LATEX_PARAGRAPH) {
2107 if (layout->latextype != LATEX_PARAGRAPH)
2108 name = layout->refprefix;
2110 // If none of the above worked, see if the inset knows.
2112 InsetLayout const & il = cur.inset().getLayout();
2113 name = il.refprefix();
2118 docstring par_text = pars_[textpit].asString(AS_STR_SKIPDELETE);
2120 // The return string of math matrices might contain linebreaks
2121 par_text = subst(par_text, '\n', '-');
2122 int const numwords = 3;
2123 for (int i = 0; i < numwords; ++i) {
2124 if (par_text.empty())
2127 par_text = split(par_text, head, ' ');
2128 // Is it legal to use spaces in labels ?
2134 // Make sure it isn't too long
2135 unsigned int const max_label_length = 32;
2136 if (text.size() > max_label_length)
2137 text.resize(max_label_length);
2140 text = name + ':' + text;
2142 // We need a unique label
2143 docstring label = text;
2145 while (cur.buffer()->activeLabel(label)) {
2146 label = text + '-' + convert<docstring>(i);
2154 docstring Text::asString(int options) const
2156 return asString(0, pars_.size(), options);
2160 docstring Text::asString(pit_type beg, pit_type end, int options) const
2162 size_t i = size_t(beg);
2163 docstring str = pars_[i].asString(options);
2164 for (++i; i != size_t(end); ++i) {
2166 str += pars_[i].asString(options);
2172 void Text::shortenForOutliner(docstring & str, size_t const maxlen)
2174 support::truncateWithEllipsis(str, maxlen);
2175 for (char_type & c : str)
2176 if (c == L'\n' || c == L'\t')
2181 void Text::forOutliner(docstring & os, size_t const maxlen,
2182 bool const shorten) const
2184 pit_type end = pars_.size() - 1;
2185 if (0 <= end && !pars_[0].labelString().empty())
2186 os += pars_[0].labelString() + ' ';
2187 forOutliner(os, maxlen, 0, end, shorten);
2191 void Text::forOutliner(docstring & os, size_t const maxlen,
2192 pit_type pit_start, pit_type pit_end,
2193 bool const shorten) const
2195 size_t tmplen = shorten ? maxlen + 1 : maxlen;
2196 pit_type end = min(size_t(pit_end), pars_.size() - 1);
2198 for (pit_type i = pit_start; i <= end && os.length() < tmplen; ++i) {
2201 // This function lets the first label be treated separately
2202 pars_[i].forOutliner(os, tmplen, false, !first);
2206 shortenForOutliner(os, maxlen);
2210 void Text::charsTranspose(Cursor & cur)
2212 LBUFERR(this == cur.text());
2214 pos_type pos = cur.pos();
2216 // If cursor is at beginning or end of paragraph, do nothing.
2217 if (pos == cur.lastpos() || pos == 0)
2220 Paragraph & par = cur.paragraph();
2222 // Get the positions of the characters to be transposed.
2223 pos_type pos1 = pos - 1;
2224 pos_type pos2 = pos;
2226 // In change tracking mode, ignore deleted characters.
2227 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2229 if (pos2 == cur.lastpos())
2232 while (pos1 >= 0 && par.isDeleted(pos1))
2237 // Don't do anything if one of the "characters" is not regular text.
2238 if (par.isInset(pos1) || par.isInset(pos2))
2241 // Store the characters to be transposed (including font information).
2242 char_type const char1 = par.getChar(pos1);
2244 par.getFontSettings(cur.buffer()->params(), pos1);
2246 char_type const char2 = par.getChar(pos2);
2248 par.getFontSettings(cur.buffer()->params(), pos2);
2250 // And finally, we are ready to perform the transposition.
2251 // Track the changes if Change Tracking is enabled.
2252 bool const trackChanges = cur.buffer()->params().track_changes;
2256 par.eraseChar(pos2, trackChanges);
2257 par.eraseChar(pos1, trackChanges);
2258 par.insertChar(pos1, char2, font2, trackChanges);
2259 par.insertChar(pos2, char1, font1, trackChanges);
2261 cur.checkBufferStructure();
2263 // After the transposition, move cursor to after the transposition.
2264 setCursor(cur, cur.pit(), pos2);
2269 DocIterator Text::macrocontextPosition() const
2271 return macrocontext_position_;
2275 void Text::setMacrocontextPosition(DocIterator const & pos)
2277 macrocontext_position_ = pos;
2281 docstring Text::previousWord(CursorSlice const & sl) const
2283 CursorSlice from = sl;
2284 CursorSlice to = sl;
2285 getWord(from, to, PREVIOUS_WORD);
2286 if (sl == from || to == from)
2289 Paragraph const & par = sl.paragraph();
2290 return par.asString(from.pos(), to.pos());
2294 bool Text::completionSupported(Cursor const & cur) const
2296 Paragraph const & par = cur.paragraph();
2297 return !cur.selection()
2299 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2300 && !par.isWordSeparator(cur.pos() - 1);
2304 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2306 WordList const & list = theWordList(cur.getFont().language()->lang());
2307 return new TextCompletionList(cur, list);
2311 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2313 LBUFERR(cur.bv().cursor() == cur);
2315 cur.bv().cursor() = cur;
2316 if (!(cur.result().screenUpdate() & Update::Force))
2317 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2322 docstring Text::completionPrefix(Cursor const & cur) const
2324 return previousWord(cur.top());