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 ParagraphList::iterator const end = pars_.end();
194 ParagraphList::iterator it = pars_.begin();
195 for (; it != end; ++it)
196 it->setInsetOwner(owner);
200 pit_type Text::depthHook(pit_type pit, depth_type depth) const
202 pit_type newpit = pit;
207 while (newpit != 0 && pars_[newpit].getDepth() > depth)
210 if (pars_[newpit].getDepth() > depth)
217 pit_type Text::outerHook(pit_type par_offset) const
219 Paragraph const & par = pars_[par_offset];
221 if (par.getDepth() == 0)
223 return depthHook(par_offset, depth_type(par.getDepth() - 1));
227 bool Text::isFirstInSequence(pit_type par_offset) const
229 Paragraph const & par = pars_[par_offset];
231 pit_type dhook_offset = depthHook(par_offset, par.getDepth());
233 if (dhook_offset == par_offset)
236 Paragraph const & dhook = pars_[dhook_offset];
238 return dhook.layout() != par.layout()
239 || dhook.getDepth() != par.getDepth();
243 pit_type Text::lastInSequence(pit_type pit) const
245 depth_type const depth = pars_[pit].getDepth();
246 pit_type newpit = pit;
248 while (size_t(newpit + 1) < pars_.size() &&
249 (pars_[newpit + 1].getDepth() > depth ||
250 (pars_[newpit + 1].getDepth() == depth &&
251 pars_[newpit + 1].layout() == pars_[pit].layout())))
258 int Text::getTocLevel(pit_type par_offset) const
260 Paragraph const & par = pars_[par_offset];
262 if (par.layout().isEnvironment() && !isFirstInSequence(par_offset))
263 return Layout::NOT_IN_TOC;
265 return par.layout().toclevel;
269 Font const Text::outerFont(pit_type par_offset) const
271 depth_type par_depth = pars_[par_offset].getDepth();
272 FontInfo tmpfont = inherit_font;
273 depth_type prev_par_depth = 0;
274 // Resolve against environment font information
275 while (par_offset != pit_type(pars_.size())
276 && par_depth != prev_par_depth
278 && !tmpfont.resolved()) {
279 prev_par_depth = par_depth;
280 par_offset = outerHook(par_offset);
281 if (par_offset != pit_type(pars_.size())) {
282 tmpfont.realize(pars_[par_offset].layout().font);
283 par_depth = pars_[par_offset].getDepth();
287 return Font(tmpfont);
291 static void acceptOrRejectChanges(ParagraphList & pars,
292 BufferParams const & bparams, Text::ChangeOp op)
294 pit_type pars_size = static_cast<pit_type>(pars.size());
296 // first, accept or reject changes within each individual
297 // paragraph (do not consider end-of-par)
298 for (pit_type pit = 0; pit < pars_size; ++pit) {
299 // prevent assertion failure
300 if (!pars[pit].empty()) {
301 if (op == Text::ACCEPT)
302 pars[pit].acceptChanges(0, pars[pit].size());
304 pars[pit].rejectChanges(0, pars[pit].size());
308 // next, accept or reject imaginary end-of-par characters
309 for (pit_type pit = 0; pit < pars_size; ++pit) {
310 pos_type pos = pars[pit].size();
311 if (pars[pit].isChanged(pos)) {
312 // keep the end-of-par char if it is inserted and accepted
313 // or when it is deleted and rejected.
314 if (pars[pit].isInserted(pos) == (op == Text::ACCEPT)) {
315 pars[pit].setChange(pos, Change(Change::UNCHANGED));
317 if (pit == pars_size - 1) {
318 // we cannot remove a par break at the end of the last
319 // paragraph; instead, we mark it unchanged
320 pars[pit].setChange(pos, Change(Change::UNCHANGED));
322 mergeParagraph(bparams, pars, pit);
332 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
334 acceptOrRejectChanges(pars, bparams, Text::ACCEPT);
338 void rejectChanges(ParagraphList & pars, BufferParams const & bparams)
340 acceptOrRejectChanges(pars, bparams, Text::REJECT);
344 InsetText const & Text::inset() const
351 void Text::readParToken(Paragraph & par, Lexer & lex,
352 string const & token, Font & font, Change & change, ErrorList & errorList)
354 Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
355 BufferParams & bp = buf->params();
357 if (token[0] != '\\') {
358 docstring dstr = lex.getDocString();
359 par.appendString(dstr, font, change);
361 } else if (token == "\\begin_layout") {
363 docstring layoutname = lex.getDocString();
365 font = Font(inherit_font, bp.language);
366 change = Change(Change::UNCHANGED);
368 DocumentClass const & tclass = bp.documentClass();
370 if (layoutname.empty())
371 layoutname = tclass.defaultLayoutName();
373 if (owner_->forcePlainLayout()) {
374 // in this case only the empty layout is allowed
375 layoutname = tclass.plainLayoutName();
376 } else if (par.usePlainLayout()) {
377 // in this case, default layout maps to empty layout
378 if (layoutname == tclass.defaultLayoutName())
379 layoutname = tclass.plainLayoutName();
381 // otherwise, the empty layout maps to the default
382 if (layoutname == tclass.plainLayoutName())
383 layoutname = tclass.defaultLayoutName();
386 // When we apply an unknown layout to a document, we add this layout to the textclass
387 // of this document. For example, when you apply class article to a beamer document,
388 // all unknown layouts such as frame will be added to document class article so that
389 // these layouts can keep their original names.
390 bool const added_one = tclass.addLayoutIfNeeded(layoutname);
393 docstring const s = bformat(_("Layout `%1$s' was not found."), layoutname);
394 errorList.push_back(ErrorItem(_("Layout Not Found"), s,
395 {par.id(), 0}, {par.id(), -1}));
398 par.setLayout(bp.documentClass()[layoutname]);
400 // Test whether the layout is obsolete.
401 Layout const & layout = par.layout();
402 if (!layout.obsoleted_by().empty())
403 par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
405 par.params().read(lex);
407 } else if (token == "\\end_layout") {
408 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
409 << "Missing \\begin_layout ?");
410 } else if (token == "\\end_inset") {
411 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
412 << "Missing \\begin_inset ?");
413 } else if (token == "\\begin_inset") {
414 Inset * inset = readInset(lex, buf);
416 par.insertInset(par.size(), inset, font, change);
419 docstring line = lex.getDocString();
420 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
421 {par.id(), 0}, {par.id(), -1}));
423 } else if (token == "\\family") {
425 setLyXFamily(lex.getString(), font.fontInfo());
426 } else if (token == "\\series") {
428 setLyXSeries(lex.getString(), font.fontInfo());
429 } else if (token == "\\shape") {
431 setLyXShape(lex.getString(), font.fontInfo());
432 } else if (token == "\\size") {
434 setLyXSize(lex.getString(), font.fontInfo());
435 } else if (token == "\\lang") {
437 string const tok = lex.getString();
438 Language const * lang = languages.getLanguage(tok);
440 font.setLanguage(lang);
442 font.setLanguage(bp.language);
443 lex.printError("Unknown language `$$Token'");
445 } else if (token == "\\numeric") {
447 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
448 } else if (token == "\\nospellcheck") {
450 font.fontInfo().setNoSpellcheck(setLyXMisc(lex.getString()));
451 } else if (token == "\\emph") {
453 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
454 } else if (token == "\\bar") {
456 string const tok = lex.getString();
459 font.fontInfo().setUnderbar(FONT_ON);
460 else if (tok == "no")
461 font.fontInfo().setUnderbar(FONT_OFF);
462 else if (tok == "default")
463 font.fontInfo().setUnderbar(FONT_INHERIT);
465 lex.printError("Unknown bar font flag "
467 } else if (token == "\\strikeout") {
469 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
470 } else if (token == "\\xout") {
472 font.fontInfo().setXout(setLyXMisc(lex.getString()));
473 } else if (token == "\\uuline") {
475 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
476 } else if (token == "\\uwave") {
478 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
479 } else if (token == "\\noun") {
481 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
482 } else if (token == "\\color") {
484 setLyXColor(lex.getString(), font.fontInfo());
485 } else if (token == "\\SpecialChar" ||
486 (token == "\\SpecialCharNoPassThru" &&
487 !par.layout().pass_thru && !inset().isPassThru())) {
488 auto inset = make_unique<InsetSpecialChar>();
490 inset->setBuffer(*buf);
491 par.insertInset(par.size(), inset.release(), font, change);
492 } else if (token == "\\SpecialCharNoPassThru") {
494 docstring const s = ltrim(lex.getDocString(), "\\");
495 par.insert(par.size(), s, font, change);
496 } else if (token == "\\IPAChar") {
497 auto inset = make_unique<InsetIPAChar>();
499 inset->setBuffer(*buf);
500 par.insertInset(par.size(), inset.release(), font, change);
501 } else if (token == "\\twohyphens" || token == "\\threehyphens") {
502 // Ideally, this should be done by lyx2lyx, but lyx2lyx does not know the
503 // running font and does not know anything about layouts (and CopyStyle).
504 Layout const & layout(par.layout());
505 FontInfo info = font.fontInfo();
506 info.realize(layout.resfont);
507 if (layout.pass_thru || inset().isPassThru() ||
508 info.family() == TYPEWRITER_FAMILY) {
509 if (token == "\\twohyphens")
510 par.insert(par.size(), from_ascii("--"), font, change);
512 par.insert(par.size(), from_ascii("---"), font, change);
514 if (token == "\\twohyphens")
515 par.insertChar(par.size(), 0x2013, font, change);
517 par.insertChar(par.size(), 0x2014, font, change);
519 } else if (token == "\\backslash") {
520 par.appendChar('\\', font, change);
521 } else if (token == "\\LyXTable") {
522 auto inset = make_unique<InsetTabular>(buf);
524 par.insertInset(par.size(), inset.release(), font, change);
525 } else if (token == "\\change_unchanged") {
526 change = Change(Change::UNCHANGED);
527 } else if (token == "\\change_inserted" || token == "\\change_deleted") {
529 istringstream is(lex.getString());
533 BufferParams::AuthorMap const & am = bp.author_map_;
534 if (am.find(aid) == am.end()) {
535 errorList.push_back(ErrorItem(
536 _("Change tracking author index missing"),
537 bformat(_("A change tracking author information for index "
538 "%1$d is missing. This can happen after a wrong "
539 "merge by a version control system. In this case, "
540 "either fix the merge, or have this information "
541 "missing until the corresponding tracked changes "
542 "are merged or this user edits the file again.\n"),
544 {par.id(), par.size()}, {par.id(), par.size() + 1}));
545 bp.addAuthor(Author(aid));
547 if (token == "\\change_inserted")
548 change = Change(Change::INSERTED, am.find(aid)->second, ct);
550 change = Change(Change::DELETED, am.find(aid)->second, ct);
553 errorList.push_back(ErrorItem(_("Unknown token"),
554 bformat(_("Unknown token: %1$s %2$s\n"),
557 {par.id(), 0}, {par.id(), -1}));
562 void Text::readParagraph(Paragraph & par, Lexer & lex,
563 ErrorList & errorList)
566 string token = lex.getString();
568 Change change(Change::UNCHANGED);
571 readParToken(par, lex, token, font, change, errorList);
574 token = lex.getString();
579 if (token == "\\end_layout") {
580 //Ok, paragraph finished
584 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
585 if (token == "\\begin_layout" || token == "\\end_document"
586 || token == "\\end_inset" || token == "\\begin_deeper"
587 || token == "\\end_deeper") {
588 lex.pushToken(token);
589 lyxerr << "Paragraph ended in line "
590 << lex.lineNumber() << "\n"
591 << "Missing \\end_layout.\n";
595 // Final change goes to paragraph break:
596 par.setChange(par.size(), change);
598 // Initialize begin_of_body_ on load; redoParagraph maintains
599 par.setBeginOfBody();
601 // mark paragraph for spell checking on load
602 // par.requestSpellCheck();
606 class TextCompletionList : public CompletionList
610 TextCompletionList(Cursor const & cur, WordList const & list)
611 : buffer_(cur.buffer()), list_(list)
614 virtual ~TextCompletionList() {}
617 virtual bool sorted() const { return true; }
619 virtual size_t size() const
624 virtual docstring const & data(size_t idx) const
626 return list_.word(idx);
631 Buffer const * buffer_;
633 WordList const & list_;
637 bool Text::empty() const
639 return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
640 // FIXME: Should we consider the labeled type as empty too?
641 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
645 double Text::spacing(Paragraph const & par) const
647 if (par.params().spacing().isDefault())
648 return owner_->buffer().params().spacing().getValue();
649 return par.params().spacing().getValue();
654 * This breaks a paragraph at the specified position.
655 * The new paragraph will:
656 * - Decrease depth by one (or change layout to default layout) when
657 * keep_layout == false
658 * - keep current depth and layout when keep_layout == true
660 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
663 BufferParams const & bparams = text.inset().buffer().params();
664 ParagraphList & pars = text.paragraphs();
665 // create a new paragraph, and insert into the list
666 ParagraphList::iterator tmp =
667 pars.insert(lyx::next(pars.begin(), par_offset + 1),
670 Paragraph & par = pars[par_offset];
672 // remember to set the inset_owner
673 tmp->setInsetOwner(&par.inInset());
674 // without doing that we get a crash when typing <Return> at the
675 // end of a paragraph
676 tmp->setPlainOrDefaultLayout(bparams.documentClass());
679 tmp->setLayout(par.layout());
680 tmp->setLabelWidthString(par.params().labelWidthString());
681 tmp->params().depth(par.params().depth());
682 } else if (par.params().depth() > 0) {
683 Paragraph const & hook = pars[text.outerHook(par_offset)];
684 tmp->setLayout(hook.layout());
685 // not sure the line below is useful
686 tmp->setLabelWidthString(par.params().labelWidthString());
687 tmp->params().depth(hook.params().depth());
690 bool const isempty = (par.allowEmpty() && par.empty());
692 if (!isempty && (par.size() > pos || par.empty())) {
693 tmp->setLayout(par.layout());
694 tmp->params().align(par.params().align());
695 tmp->setLabelWidthString(par.params().labelWidthString());
697 tmp->params().depth(par.params().depth());
698 tmp->params().noindent(par.params().noindent());
700 // move everything behind the break position
701 // to the new paragraph
703 /* Note: if !keepempty, empty() == true, then we reach
704 * here with size() == 0. So pos_end becomes - 1. This
705 * doesn't cause problems because both loops below
706 * enforce pos <= pos_end and 0 <= pos
708 pos_type pos_end = par.size() - 1;
710 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
711 if (moveItem(par, pos, *tmp, j, bparams)) {
717 // Move over the end-of-par change information
718 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
719 par.setChange(par.size(), Change(bparams.track_changes ?
720 Change::INSERTED : Change::UNCHANGED));
723 // Make sure that we keep the language when
724 // breaking paragraph.
726 Font changed = tmp->getFirstFontSettings(bparams);
727 Font const & old = par.getFontSettings(bparams, par.size());
728 changed.setLanguage(old.language());
729 tmp->setFont(0, changed);
736 bool const soa = par.params().startOfAppendix();
737 par.params().clear();
738 // do not lose start of appendix marker (bug 4212)
739 par.params().startOfAppendix(soa);
740 par.setPlainOrDefaultLayout(bparams.documentClass());
744 par.setLayout(tmp->layout());
745 par.setLabelWidthString(tmp->params().labelWidthString());
746 par.params().depth(tmp->params().depth());
751 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
753 LBUFERR(this == cur.text());
755 Paragraph & cpar = cur.paragraph();
756 pit_type cpit = cur.pit();
758 DocumentClass const & tclass = cur.buffer()->params().documentClass();
759 Layout const & layout = cpar.layout();
761 if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
762 if (changeDepthAllowed(cur, DEC_DEPTH)) {
763 changeDepth(cur, DEC_DEPTH);
764 pit_type const prev = depthHook(cpit, cpar.getDepth());
765 docstring const & lay = pars_[prev].layout().name();
766 if (lay != layout.name())
769 docstring const & lay = cur.paragraph().usePlainLayout()
770 ? tclass.plainLayoutName() : tclass.defaultLayoutName();
771 if (lay != layout.name())
779 // Always break behind a space
780 // It is better to erase the space (Dekel)
781 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
782 cpar.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
784 // What should the layout for the new paragraph be?
785 bool keep_layout = layout.isEnvironment()
786 || (layout.isParagraph() && layout.parbreak_is_newline);
788 keep_layout = !keep_layout;
790 // We need to remember this before we break the paragraph, because
791 // that invalidates the layout variable
792 bool sensitive = layout.labeltype == LABEL_SENSITIVE;
794 // we need to set this before we insert the paragraph.
795 bool const isempty = cpar.allowEmpty() && cpar.empty();
797 lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
799 // After this, neither paragraph contains any rows!
802 pit_type next_par = cpit + 1;
804 // well this is the caption hack since one caption is really enough
807 // set to standard-layout
808 //FIXME Check if this should be plainLayout() in some cases
809 pars_[cpit].applyLayout(tclass.defaultLayout());
811 // set to standard-layout
812 //FIXME Check if this should be plainLayout() in some cases
813 pars_[next_par].applyLayout(tclass.defaultLayout());
816 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
817 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().track_changes))
818 break; // the character couldn't be deleted physically due to change tracking
821 // A singlePar update is not enough in this case.
822 cur.screenUpdateFlags(Update::Force);
823 cur.forceBufferUpdate();
825 // This check is necessary. Otherwise the new empty paragraph will
826 // be deleted automatically. And it is more friendly for the user!
827 if (cur.pos() != 0 || isempty)
828 setCursor(cur, cur.pit() + 1, 0);
830 setCursor(cur, cur.pit(), 0);
834 // needed to insert the selection
835 void Text::insertStringAsLines(Cursor & cur, docstring const & str,
838 BufferParams const & bparams = owner_->buffer().params();
839 pit_type pit = cur.pit();
840 pos_type pos = cur.pos();
842 // insert the string, don't insert doublespace
843 bool space_inserted = true;
844 for (docstring::const_iterator cit = str.begin();
845 cit != str.end(); ++cit) {
846 Paragraph & par = pars_[pit];
848 if (inset().allowMultiPar() && (!par.empty() || par.allowEmpty())) {
849 lyx::breakParagraph(*this, pit, pos,
850 par.layout().isEnvironment());
853 space_inserted = true;
857 // do not insert consecutive spaces if !free_spacing
858 } else if ((*cit == ' ' || *cit == '\t') &&
859 space_inserted && !par.isFreeSpacing()) {
861 } else if (*cit == '\t') {
862 if (!par.isFreeSpacing()) {
863 // tabs are like spaces here
864 par.insertChar(pos, ' ', font, bparams.track_changes);
866 space_inserted = true;
868 par.insertChar(pos, *cit, font, bparams.track_changes);
870 space_inserted = true;
872 } else if (!isPrintable(*cit)) {
873 // Ignore unprintables
876 // just insert the character
877 par.insertChar(pos, *cit, font, bparams.track_changes);
879 space_inserted = (*cit == ' ');
882 setCursor(cur, pit, pos);
886 // turn double CR to single CR, others are converted into one
887 // blank. Then insertStringAsLines is called
888 void Text::insertStringAsParagraphs(Cursor & cur, docstring const & str,
891 docstring linestr = str;
892 bool newline_inserted = false;
894 for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
895 if (linestr[i] == '\n') {
896 if (newline_inserted) {
897 // we know that \r will be ignored by
898 // insertStringAsLines. Of course, it is a dirty
899 // trick, but it works...
900 linestr[i - 1] = '\r';
904 newline_inserted = true;
906 } else if (isPrintable(linestr[i])) {
907 newline_inserted = false;
910 insertStringAsLines(cur, linestr, font);
916 bool canInsertChar(Cursor const & cur, char_type c)
918 Paragraph const & par = cur.paragraph();
919 // If not in free spacing mode, check if there will be two blanks together or a blank at
920 // the beginning of a paragraph.
921 if (!par.isFreeSpacing() && isLineSeparatorChar(c)) {
922 if (cur.pos() == 0) {
924 "You cannot insert a space at the "
925 "beginning of a paragraph. Please read the Tutorial."));
928 // If something is wrong, ignore this character.
929 LASSERT(cur.pos() > 0, return false);
930 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
931 && !par.isDeleted(cur.pos() - 1)) {
933 "You cannot type two spaces this way. "
934 "Please read the Tutorial."));
939 // Prevent to insert uncodable characters in verbatim and ERT.
940 // The encoding is inherited from the context here.
941 if (par.isPassThru() && cur.getEncoding()) {
942 Encoding const * e = cur.getEncoding();
943 if (!e->encodable(c)) {
944 cur.message(_("Character is uncodable in this verbatim context."));
954 // insert a character, moves all the following breaks in the
955 // same Paragraph one to the right and make a rebreak
956 void Text::insertChar(Cursor & cur, char_type c)
958 LBUFERR(this == cur.text());
960 if (!canInsertChar(cur,c))
963 cur.recordUndo(INSERT_UNDO);
965 TextMetrics const & tm = cur.bv().textMetrics(this);
966 Buffer const & buffer = *cur.buffer();
967 Paragraph & par = cur.paragraph();
968 // try to remove this
969 pit_type const pit = cur.pit();
971 if (lyxrc.auto_number) {
972 static docstring const number_operators = from_ascii("+-/*");
973 static docstring const number_unary_operators = from_ascii("+-");
975 // European Number Separators: comma, dot etc.
976 // European Number Terminators: percent, permille, degree, euro etc.
977 if (cur.current_font.fontInfo().number() == FONT_ON) {
978 if (!isDigitASCII(c) && !contains(number_operators, c) &&
979 !(isEuropeanNumberSeparator(c) &&
981 cur.pos() != cur.lastpos() &&
982 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
983 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON) &&
984 !(isEuropeanNumberTerminator(c) &&
986 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
987 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
989 number(cur); // Set current_font.number to OFF
990 } else if (isDigitASCII(c) &&
991 cur.real_current_font.isVisibleRightToLeft()) {
992 number(cur); // Set current_font.number to ON
994 if (cur.pos() != 0) {
995 char_type const ch = par.getChar(cur.pos() - 1);
996 if (contains(number_unary_operators, ch) &&
998 || par.isSeparator(cur.pos() - 2)
999 || par.isEnvSeparator(cur.pos() - 2)
1000 || par.isNewline(cur.pos() - 2))
1002 setCharFont(pit, cur.pos() - 1, cur.current_font,
1004 } else if (isEuropeanNumberSeparator(ch)
1006 && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
1007 setCharFont(pit, cur.pos() - 1, cur.current_font,
1014 // In Bidi text, we want spaces to be treated in a special way: spaces
1015 // which are between words in different languages should get the
1016 // paragraph's language; otherwise, spaces should keep the language
1017 // they were originally typed in. This is only in effect while typing;
1018 // after the text is already typed in, the user can always go back and
1019 // explicitly set the language of a space as desired. But 99.9% of the
1020 // time, what we're doing here is what the user actually meant.
1022 // The following cases are the ones in which the language of the space
1023 // should be changed to match that of the containing paragraph. In the
1024 // depictions, lowercase is LTR, uppercase is RTL, underscore (_)
1025 // represents a space, pipe (|) represents the cursor position (so the
1026 // character before it is the one just typed in). The different cases
1027 // are depicted logically (not visually), from left to right:
1032 // Theoretically, there are other situations that we should, perhaps, deal
1033 // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any
1034 // point (to understand why, just try to create this situation...).
1036 if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
1037 // get font in front and behind the space in question. But do NOT
1038 // use getFont(cur.pos()) because the character c is not inserted yet
1039 Font const pre_space_font = tm.displayFont(cur.pit(), cur.pos() - 2);
1040 Font const & post_space_font = cur.real_current_font;
1041 bool pre_space_rtl = pre_space_font.isVisibleRightToLeft();
1042 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
1044 if (pre_space_rtl != post_space_rtl) {
1045 // Set the space's language to match the language of the
1046 // adjacent character whose direction is the paragraph's
1047 // direction; don't touch other properties of the font
1048 Language const * lang =
1049 (pre_space_rtl == par.isRTL(buffer.params())) ?
1050 pre_space_font.language() : post_space_font.language();
1052 Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
1053 space_font.setLanguage(lang);
1054 par.setFont(cur.pos() - 1, space_font);
1058 pos_type pos = cur.pos();
1059 if (!cur.paragraph().isPassThru() && owner_->lyxCode() != IPA_CODE &&
1060 cur.real_current_font.fontInfo().family() != TYPEWRITER_FAMILY &&
1061 c == '-' && pos > 0) {
1062 if (par.getChar(pos - 1) == '-') {
1063 // convert "--" to endash
1064 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1067 } else if (par.getChar(pos - 1) == 0x2013) {
1068 // convert "---" to emdash
1069 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1075 par.insertChar(pos, c, cur.current_font,
1076 cur.buffer()->params().track_changes);
1077 cur.checkBufferStructure();
1079 // cur.screenUpdateFlags(Update::Force);
1080 bool boundary = cur.boundary()
1081 || tm.isRTLBoundary(cur.pit(), pos + 1);
1082 setCursor(cur, cur.pit(), pos + 1, false, boundary);
1087 void Text::charInserted(Cursor & cur)
1089 Paragraph & par = cur.paragraph();
1091 // register word if a non-letter was entered
1093 && !par.isWordSeparator(cur.pos() - 2)
1094 && par.isWordSeparator(cur.pos() - 1)) {
1095 // get the word in front of cursor
1096 LBUFERR(this == cur.text());
1097 cur.paragraph().updateWords();
1102 // the cursor set functions have a special mechanism. When they
1103 // realize, that you left an empty paragraph, they will delete it.
1105 bool Text::cursorForwardOneWord(Cursor & cur)
1107 LBUFERR(this == cur.text());
1109 pos_type const lastpos = cur.lastpos();
1110 pit_type pit = cur.pit();
1111 pos_type pos = cur.pos();
1112 Paragraph const & par = cur.paragraph();
1114 // Paragraph boundary is a word boundary
1115 if (pos == lastpos || (pos + 1 == lastpos && par.isEnvSeparator(pos))) {
1116 if (pit != cur.lastpit())
1117 return setCursor(cur, pit + 1, 0);
1122 if (lyxrc.mac_like_cursor_movement) {
1123 // Skip through trailing punctuation and spaces.
1124 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
1127 // Skip over either a non-char inset or a full word
1128 if (pos != lastpos && par.isWordSeparator(pos))
1130 else while (pos != lastpos && !par.isWordSeparator(pos))
1133 LASSERT(pos < lastpos, return false); // see above
1134 if (!par.isWordSeparator(pos))
1135 while (pos != lastpos && !par.isWordSeparator(pos))
1137 else if (par.isChar(pos))
1138 while (pos != lastpos && par.isChar(pos))
1140 else if (!par.isSpace(pos)) // non-char inset
1143 // Skip over white space
1144 while (pos != lastpos && par.isSpace(pos))
1148 // Don't skip a separator inset at the end of a paragraph
1149 if (pos == lastpos && pos && par.isEnvSeparator(pos - 1))
1152 return setCursor(cur, pit, pos);
1156 bool Text::cursorBackwardOneWord(Cursor & cur)
1158 LBUFERR(this == cur.text());
1160 pit_type pit = cur.pit();
1161 pos_type pos = cur.pos();
1162 Paragraph & par = cur.paragraph();
1164 // Paragraph boundary is a word boundary
1165 if (pos == 0 && pit != 0) {
1166 Paragraph & prevpar = getPar(pit - 1);
1167 pos = prevpar.size();
1168 // Don't stop after an environment separator
1169 if (pos && prevpar.isEnvSeparator(pos - 1))
1171 return setCursor(cur, pit - 1, pos);
1174 if (lyxrc.mac_like_cursor_movement) {
1175 // Skip through punctuation and spaces.
1176 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1179 // Skip over either a non-char inset or a full word
1180 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1182 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1185 // Skip over white space
1186 while (pos != 0 && par.isSpace(pos - 1))
1189 if (pos != 0 && !par.isWordSeparator(pos - 1))
1190 while (pos != 0 && !par.isWordSeparator(pos - 1))
1192 else if (pos != 0 && par.isChar(pos - 1))
1193 while (pos != 0 && par.isChar(pos - 1))
1195 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1199 return setCursor(cur, pit, pos);
1203 bool Text::cursorVisLeftOneWord(Cursor & cur)
1205 LBUFERR(this == cur.text());
1207 pos_type left_pos, right_pos;
1209 Cursor temp_cur = cur;
1211 // always try to move at least once...
1212 while (temp_cur.posVisLeft(true /* skip_inset */)) {
1214 // collect some information about current cursor position
1215 temp_cur.getSurroundingPos(left_pos, right_pos);
1216 bool left_is_letter =
1217 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1218 bool right_is_letter =
1219 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1221 // if we're not at a letter/non-letter boundary, continue moving
1222 if (left_is_letter == right_is_letter)
1225 // we should stop when we have an LTR word on our right or an RTL word
1227 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1228 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1229 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1230 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1234 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1235 true, temp_cur.boundary());
1239 bool Text::cursorVisRightOneWord(Cursor & cur)
1241 LBUFERR(this == cur.text());
1243 pos_type left_pos, right_pos;
1245 Cursor temp_cur = cur;
1247 // always try to move at least once...
1248 while (temp_cur.posVisRight(true /* skip_inset */)) {
1250 // collect some information about current cursor position
1251 temp_cur.getSurroundingPos(left_pos, right_pos);
1252 bool left_is_letter =
1253 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1254 bool right_is_letter =
1255 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1257 // if we're not at a letter/non-letter boundary, continue moving
1258 if (left_is_letter == right_is_letter)
1261 // we should stop when we have an LTR word on our right or an RTL word
1263 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1264 temp_cur.buffer()->params(),
1265 left_pos).isRightToLeft())
1266 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1267 temp_cur.buffer()->params(),
1268 right_pos).isRightToLeft()))
1272 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1273 true, temp_cur.boundary());
1277 void Text::selectWord(Cursor & cur, word_location loc)
1279 LBUFERR(this == cur.text());
1280 CursorSlice from = cur.top();
1282 getWord(from, to, loc);
1283 if (cur.top() != from)
1284 setCursor(cur, from.pit(), from.pos());
1287 if (!cur.selection())
1289 setCursor(cur, to.pit(), to.pos());
1291 cur.setWordSelection(true);
1295 void Text::selectAll(Cursor & cur)
1297 LBUFERR(this == cur.text());
1298 if (cur.lastpos() == 0 && cur.lastpit() == 0)
1300 // If the cursor is at the beginning, make sure the cursor ends there
1301 if (cur.pit() == 0 && cur.pos() == 0) {
1302 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1304 setCursor(cur, 0, 0);
1306 setCursor(cur, 0, 0);
1308 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1314 // Select the word currently under the cursor when no
1315 // selection is currently set
1316 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1318 LBUFERR(this == cur.text());
1319 if (cur.selection())
1321 selectWord(cur, loc);
1322 return cur.selection();
1326 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1328 LBUFERR(this == cur.text());
1330 if (!cur.selection()) {
1331 if (!selectChange(cur))
1335 cur.recordUndoSelection();
1337 pit_type begPit = cur.selectionBegin().pit();
1338 pit_type endPit = cur.selectionEnd().pit();
1340 pos_type begPos = cur.selectionBegin().pos();
1341 pos_type endPos = cur.selectionEnd().pos();
1343 // keep selection info, because endPos becomes invalid after the first loop
1344 bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1346 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1347 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1348 pos_type parSize = pars_[pit].size();
1350 // ignore empty paragraphs; otherwise, an assertion will fail for
1351 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1355 // do not consider first paragraph if the cursor starts at pos size()
1356 if (pit == begPit && begPos == parSize)
1359 // do not consider last paragraph if the cursor ends at pos 0
1360 if (pit == endPit && endPos == 0)
1361 break; // last iteration anyway
1363 pos_type left = (pit == begPit ? begPos : 0);
1364 pos_type right = (pit == endPit ? endPos : parSize);
1367 // there is no change here
1371 pars_[pit].acceptChanges(left, right);
1373 pars_[pit].rejectChanges(left, right);
1377 // next, accept/reject imaginary end-of-par characters
1379 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1380 pos_type pos = pars_[pit].size();
1382 // skip if the selection ends before the end-of-par
1383 if (pit == endPit && endsBeforeEndOfPar)
1384 break; // last iteration anyway
1386 // skip if this is not the last paragraph of the document
1387 // note: the user should be able to accept/reject the par break of the last par!
1388 if (pit == endPit && pit + 1 != int(pars_.size()))
1389 break; // last iteration anway
1392 if (pars_[pit].isInserted(pos)) {
1393 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1394 } else if (pars_[pit].isDeleted(pos)) {
1395 if (pit + 1 == int(pars_.size())) {
1396 // we cannot remove a par break at the end of the last paragraph;
1397 // instead, we mark it unchanged
1398 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1400 mergeParagraph(cur.buffer()->params(), pars_, pit);
1406 if (pars_[pit].isDeleted(pos)) {
1407 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1408 } else if (pars_[pit].isInserted(pos)) {
1409 if (pit + 1 == int(pars_.size())) {
1410 // we mark the par break at the end of the last paragraph unchanged
1411 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1413 mergeParagraph(cur.buffer()->params(), pars_, pit);
1421 // finally, invoke the DEPM
1422 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().track_changes);
1425 cur.clearSelection();
1426 setCursorIntern(cur, begPit, begPos);
1427 cur.screenUpdateFlags(Update::Force);
1428 cur.forceBufferUpdate();
1432 void Text::acceptChanges()
1434 BufferParams const & bparams = owner_->buffer().params();
1435 lyx::acceptChanges(pars_, bparams);
1436 deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.track_changes);
1440 void Text::rejectChanges()
1442 BufferParams const & bparams = owner_->buffer().params();
1443 pit_type pars_size = static_cast<pit_type>(pars_.size());
1445 // first, reject changes within each individual paragraph
1446 // (do not consider end-of-par)
1447 for (pit_type pit = 0; pit < pars_size; ++pit) {
1448 if (!pars_[pit].empty()) // prevent assertion failure
1449 pars_[pit].rejectChanges(0, pars_[pit].size());
1452 // next, reject imaginary end-of-par characters
1453 for (pit_type pit = 0; pit < pars_size; ++pit) {
1454 pos_type pos = pars_[pit].size();
1456 if (pars_[pit].isDeleted(pos)) {
1457 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1458 } else if (pars_[pit].isInserted(pos)) {
1459 if (pit == pars_size - 1) {
1460 // we mark the par break at the end of the last
1461 // paragraph unchanged
1462 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1464 mergeParagraph(bparams, pars_, pit);
1471 // finally, invoke the DEPM
1472 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.track_changes);
1476 void Text::deleteWordForward(Cursor & cur, bool const force)
1478 LBUFERR(this == cur.text());
1479 if (cur.lastpos() == 0)
1483 cur.selection(true);
1484 cursorForwardOneWord(cur);
1486 if (force || !cur.confirmDeletion()) {
1487 cutSelection(cur, false);
1488 cur.checkBufferStructure();
1494 void Text::deleteWordBackward(Cursor & cur, bool const force)
1496 LBUFERR(this == cur.text());
1497 if (cur.lastpos() == 0)
1498 cursorBackward(cur);
1501 cur.selection(true);
1502 cursorBackwardOneWord(cur);
1504 if (force || !cur.confirmDeletion()) {
1505 cutSelection(cur, false);
1506 cur.checkBufferStructure();
1512 // Kill to end of line.
1513 void Text::changeCase(Cursor & cur, TextCase action, bool partial)
1515 LBUFERR(this == cur.text());
1519 bool const gotsel = cur.selection();
1521 from = cur.selBegin();
1525 getWord(from, to, partial ? PARTIAL_WORD : WHOLE_WORD);
1526 cursorForwardOneWord(cur);
1529 cur.recordUndoSelection();
1531 pit_type begPit = from.pit();
1532 pit_type endPit = to.pit();
1534 pos_type begPos = from.pos();
1535 pos_type endPos = to.pos();
1537 pos_type right = 0; // needed after the for loop
1539 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1540 Paragraph & par = pars_[pit];
1541 pos_type const pos = (pit == begPit ? begPos : 0);
1542 right = (pit == endPit ? endPos : par.size());
1543 par.changeCase(cur.buffer()->params(), pos, right, action);
1546 // the selection may have changed due to logically-only deleted chars
1548 setCursor(cur, begPit, begPos);
1550 setCursor(cur, endPit, right);
1553 setCursor(cur, endPit, right);
1555 cur.checkBufferStructure();
1559 bool Text::handleBibitems(Cursor & cur)
1561 if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1567 BufferParams const & bufparams = cur.buffer()->params();
1568 Paragraph const & par = cur.paragraph();
1569 Cursor prevcur = cur;
1570 if (cur.pit() > 0) {
1572 prevcur.pos() = prevcur.lastpos();
1574 Paragraph const & prevpar = prevcur.paragraph();
1576 // if a bibitem is deleted, merge with previous paragraph
1577 // if this is a bibliography item as well
1578 if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1579 cur.recordUndo(prevcur.pit());
1580 mergeParagraph(bufparams, cur.text()->paragraphs(),
1582 cur.forceBufferUpdate();
1583 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1584 cur.screenUpdateFlags(Update::Force);
1588 // otherwise reset to default
1589 cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1594 bool Text::erase(Cursor & cur)
1596 LASSERT(this == cur.text(), return false);
1597 bool needsUpdate = false;
1598 Paragraph & par = cur.paragraph();
1600 if (cur.pos() != cur.lastpos()) {
1601 // this is the code for a normal delete, not pasting
1603 cur.recordUndo(DELETE_UNDO);
1604 bool const was_inset = cur.paragraph().isInset(cur.pos());
1605 if(!par.eraseChar(cur.pos(), cur.buffer()->params().track_changes))
1606 // the character has been logically deleted only => skip it
1607 cur.top().forwardPos();
1610 cur.forceBufferUpdate();
1612 cur.checkBufferStructure();
1615 if (cur.pit() == cur.lastpit())
1616 return dissolveInset(cur);
1618 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1619 cur.recordUndo(DELETE_UNDO);
1620 par.setChange(cur.pos(), Change(Change::DELETED));
1624 setCursorIntern(cur, cur.pit() + 1, 0);
1625 needsUpdate = backspacePos0(cur);
1629 needsUpdate |= handleBibitems(cur);
1632 // Make sure the cursor is correct. Is this really needed?
1633 // No, not really... at least not here!
1634 cur.top().setPitPos(cur.pit(), cur.pos());
1635 cur.checkBufferStructure();
1642 bool Text::backspacePos0(Cursor & cur)
1644 LBUFERR(this == cur.text());
1648 bool needsUpdate = false;
1650 BufferParams const & bufparams = cur.buffer()->params();
1651 DocumentClass const & tclass = bufparams.documentClass();
1652 ParagraphList & plist = cur.text()->paragraphs();
1653 Paragraph const & par = cur.paragraph();
1654 Cursor prevcur = cur;
1656 prevcur.pos() = prevcur.lastpos();
1657 Paragraph const & prevpar = prevcur.paragraph();
1659 // is it an empty paragraph?
1660 if (cur.lastpos() == 0
1661 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1662 cur.recordUndo(prevcur.pit());
1663 plist.erase(lyx::next(plist.begin(), cur.pit()));
1666 // is previous par empty?
1667 else if (prevcur.lastpos() == 0
1668 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1669 cur.recordUndo(prevcur.pit());
1670 plist.erase(lyx::next(plist.begin(), prevcur.pit()));
1673 // FIXME: Do we really not want to allow this???
1674 // Pasting is not allowed, if the paragraphs have different
1675 // layouts. I think it is a real bug of all other
1676 // word processors to allow it. It confuses the user.
1677 // Correction: Pasting is always allowed with standard-layout
1678 // or the empty layout.
1679 else if (par.layout() == prevpar.layout()
1680 || tclass.isDefaultLayout(par.layout())
1681 || tclass.isPlainLayout(par.layout())) {
1682 cur.recordUndo(prevcur.pit());
1683 mergeParagraph(bufparams, plist, prevcur.pit());
1688 cur.forceBufferUpdate();
1689 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1696 bool Text::backspace(Cursor & cur)
1698 LBUFERR(this == cur.text());
1699 bool needsUpdate = false;
1700 if (cur.pos() == 0) {
1702 return dissolveInset(cur);
1704 Cursor prev_cur = cur;
1707 if (!prev_cur.paragraph().isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1708 cur.recordUndo(prev_cur.pit(), prev_cur.pit());
1709 prev_cur.paragraph().setChange(prev_cur.lastpos(), Change(Change::DELETED));
1710 setCursorIntern(cur, prev_cur.pit(), prev_cur.lastpos());
1713 // The cursor is at the beginning of a paragraph, so
1714 // the backspace will collapse two paragraphs into one.
1715 needsUpdate = backspacePos0(cur);
1718 // this is the code for a normal backspace, not pasting
1720 cur.recordUndo(DELETE_UNDO);
1721 // We used to do cursorBackwardIntern() here, but it is
1722 // not a good idea since it triggers the auto-delete
1723 // mechanism. So we do a cursorBackwardIntern()-lite,
1724 // without the dreaded mechanism. (JMarc)
1725 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1726 false, cur.boundary());
1727 bool const was_inset = cur.paragraph().isInset(cur.pos());
1728 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().track_changes);
1730 cur.forceBufferUpdate();
1732 cur.checkBufferStructure();
1735 if (cur.pos() == cur.lastpos())
1736 cur.setCurrentFont();
1738 needsUpdate |= handleBibitems(cur);
1740 // A singlePar update is not enough in this case.
1741 // cur.screenUpdateFlags(Update::Force);
1742 cur.top().setPitPos(cur.pit(), cur.pos());
1748 bool Text::dissolveInset(Cursor & cur)
1750 LASSERT(this == cur.text(), return false);
1752 if (isMainText() || cur.inset().nargs() != 1)
1755 cur.recordUndoInset();
1757 cur.selHandle(false);
1758 // save position inside inset
1759 pos_type spos = cur.pos();
1760 pit_type spit = cur.pit();
1761 ParagraphList plist;
1762 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1763 plist = paragraphs();
1765 // update cursor offset
1769 // remember position outside inset to delete inset later
1770 // we do not do it now to avoid memory reuse issues (see #10667).
1771 DocIterator inset_it = cur;
1775 Buffer & b = *cur.buffer();
1776 if (!plist.empty()) {
1778 // we clear the cache so that we won't get conflicts with labels
1779 // that get pasted into the buffer. we should update this before
1780 // its being empty matters. if not (i.e., if we encounter bugs),
1781 // then this should instead be:
1782 // cur.buffer().updateBuffer();
1783 // but we'll try the cheaper solution here.
1784 cur.buffer()->clearReferenceCache();
1786 // ERT paragraphs have the Language latex_language.
1787 // This is invalid outside of ERT, so we need to
1788 // change it to the buffer language.
1789 ParagraphList::iterator it = plist.begin();
1790 ParagraphList::iterator it_end = plist.end();
1791 for (; it != it_end; ++it)
1792 it->changeLanguage(b.params(), latex_language, b.language());
1794 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1795 b.errorList("Paste"));
1798 // delete the inset now
1799 inset_it.paragraph().eraseChar(inset_it.pos(), b.params().track_changes);
1802 cur.pit() = min(cur.lastpit(), spit);
1803 cur.pos() = min(cur.lastpos(), spos);
1804 // Ensure the current language is set correctly (bug 6292)
1805 cur.text()->setCursor(cur, cur.pit(), cur.pos());
1806 cur.clearSelection();
1808 cur.forceBufferUpdate();
1814 void Text::getWord(CursorSlice & from, CursorSlice & to,
1815 word_location const loc) const
1818 pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1822 void Text::write(ostream & os) const
1824 Buffer const & buf = owner_->buffer();
1825 ParagraphList::const_iterator pit = paragraphs().begin();
1826 ParagraphList::const_iterator end = paragraphs().end();
1828 for (; pit != end; ++pit)
1829 pit->write(os, buf.params(), dth);
1831 // Close begin_deeper
1832 for(; dth > 0; --dth)
1833 os << "\n\\end_deeper";
1837 bool Text::read(Lexer & lex,
1838 ErrorList & errorList, InsetText * insetPtr)
1840 Buffer const & buf = owner_->buffer();
1841 depth_type depth = 0;
1844 while (lex.isOK()) {
1846 string const token = lex.getString();
1851 if (token == "\\end_inset")
1854 if (token == "\\end_body")
1857 if (token == "\\begin_body")
1860 if (token == "\\end_document") {
1865 if (token == "\\begin_layout") {
1866 lex.pushToken(token);
1869 par.setInsetOwner(insetPtr);
1870 par.params().depth(depth);
1871 par.setFont(0, Font(inherit_font, buf.params().language));
1872 pars_.push_back(par);
1873 readParagraph(pars_.back(), lex, errorList);
1875 // register the words in the global word list
1876 pars_.back().updateWords();
1877 } else if (token == "\\begin_deeper") {
1879 } else if (token == "\\end_deeper") {
1881 lex.printError("\\end_deeper: " "depth is already null");
1885 LYXERR0("Handling unknown body token: `" << token << '\'');
1889 // avoid a crash on weird documents (bug 4859)
1890 if (pars_.empty()) {
1892 par.setInsetOwner(insetPtr);
1893 par.params().depth(depth);
1894 par.setFont(0, Font(inherit_font,
1895 buf.params().language));
1896 par.setPlainOrDefaultLayout(buf.params().documentClass());
1897 pars_.push_back(par);
1904 // Returns the current font and depth as a message.
1905 docstring Text::currentState(CursorData const & cur, bool devel_mode) const
1907 LBUFERR(this == cur.text());
1908 Buffer & buf = *cur.buffer();
1909 Paragraph const & par = cur.paragraph();
1910 odocstringstream os;
1912 if (buf.params().track_changes)
1913 os << _("[Change Tracking] ");
1915 Change change = par.lookupChange(cur.pos());
1917 if (change.changed()) {
1918 docstring const author =
1919 buf.params().authors().get(change.author).nameAndEmail();
1920 docstring const date = formatted_datetime(change.changetime);
1921 os << bformat(_("Changed by %1$s[[author]] on %2$s[[date]]. "),
1925 // I think we should only show changes from the default
1927 // No, from the document font (MV)
1928 Font font = cur.real_current_font;
1929 font.fontInfo().reduce(buf.params().getFont().fontInfo());
1931 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1933 // The paragraph depth
1934 int depth = cur.paragraph().getDepth();
1936 os << bformat(_(", Depth: %1$d"), depth);
1938 // The paragraph spacing, but only if different from
1940 Spacing const & spacing = par.params().spacing();
1941 if (!spacing.isDefault()) {
1942 os << _(", Spacing: ");
1943 switch (spacing.getSpace()) {
1944 case Spacing::Single:
1947 case Spacing::Onehalf:
1950 case Spacing::Double:
1953 case Spacing::Other:
1954 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1956 case Spacing::Default:
1957 // should never happen, do nothing
1963 os << _(", Inset: ") << &cur.inset();
1964 if (cur.lastidx() > 0)
1965 os << _(", Cell: ") << cur.idx();
1966 os << _(", Paragraph: ") << cur.pit();
1967 os << _(", Id: ") << par.id();
1968 os << _(", Position: ") << cur.pos();
1969 // FIXME: Why is the check for par.size() needed?
1970 // We are called with cur.pos() == par.size() quite often.
1971 if (!par.empty() && cur.pos() < par.size()) {
1972 // Force output of code point, not character
1973 size_t const c = par.getChar(cur.pos());
1974 os << _(", Char: 0x") << hex << c;
1976 os << _(", Boundary: ") << cur.boundary();
1977 // Row & row = cur.textRow();
1978 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1984 docstring Text::getPossibleLabel(DocIterator const & cur) const
1986 pit_type textpit = cur.pit();
1987 Layout const * layout = &(pars_[textpit].layout());
1989 // Will contain the label prefix.
1992 // For captions, we just take the caption type
1993 Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
1994 if (caption_inset) {
1995 string const & ftype = static_cast<InsetCaption *>(caption_inset)->floattype();
1996 FloatList const & fl = cur.buffer()->params().documentClass().floats();
1997 if (fl.typeExist(ftype)) {
1998 Floating const & flt = fl.getType(ftype);
1999 name = from_utf8(flt.refPrefix());
2002 name = from_utf8(ftype.substr(0,3));
2004 // For section, subsection, etc...
2005 if (layout->latextype == LATEX_PARAGRAPH && textpit != 0) {
2006 Layout const * layout2 = &(pars_[textpit - 1].layout());
2007 if (layout2->latextype != LATEX_PARAGRAPH) {
2012 if (layout->latextype != LATEX_PARAGRAPH)
2013 name = layout->refprefix;
2015 // If none of the above worked, see if the inset knows.
2017 InsetLayout const & il = cur.inset().getLayout();
2018 name = il.refprefix();
2023 docstring par_text = pars_[textpit].asString(AS_STR_SKIPDELETE);
2025 // The return string of math matrices might contain linebreaks
2026 par_text = subst(par_text, '\n', '-');
2027 int const numwords = 3;
2028 for (int i = 0; i < numwords; ++i) {
2029 if (par_text.empty())
2032 par_text = split(par_text, head, ' ');
2033 // Is it legal to use spaces in labels ?
2039 // Make sure it isn't too long
2040 unsigned int const max_label_length = 32;
2041 if (text.size() > max_label_length)
2042 text.resize(max_label_length);
2045 text = name + ':' + text;
2047 // We need a unique label
2048 docstring label = text;
2050 while (cur.buffer()->insetLabel(label)) {
2051 label = text + '-' + convert<docstring>(i);
2059 docstring Text::asString(int options) const
2061 return asString(0, pars_.size(), options);
2065 docstring Text::asString(pit_type beg, pit_type end, int options) const
2067 size_t i = size_t(beg);
2068 docstring str = pars_[i].asString(options);
2069 for (++i; i != size_t(end); ++i) {
2071 str += pars_[i].asString(options);
2077 void Text::shortenForOutliner(docstring & str, size_t const maxlen)
2079 support::truncateWithEllipsis(str, maxlen);
2080 for (char_type & c : str)
2081 if (c == L'\n' || c == L'\t')
2086 void Text::forOutliner(docstring & os, size_t const maxlen,
2087 bool const shorten) const
2089 pit_type end = pars_.size() - 1;
2090 if (0 <= end && !pars_[0].labelString().empty())
2091 os += pars_[0].labelString() + ' ';
2092 forOutliner(os, maxlen, 0, end, shorten);
2096 void Text::forOutliner(docstring & os, size_t const maxlen,
2097 pit_type pit_start, pit_type pit_end,
2098 bool const shorten) const
2100 size_t tmplen = shorten ? maxlen + 1 : maxlen;
2101 pit_type end = min(size_t(pit_end), pars_.size() - 1);
2103 for (pit_type i = pit_start; i <= end && os.length() < tmplen; ++i) {
2106 // This function lets the first label be treated separately
2107 pars_[i].forOutliner(os, tmplen, false, !first);
2111 shortenForOutliner(os, maxlen);
2115 void Text::charsTranspose(Cursor & cur)
2117 LBUFERR(this == cur.text());
2119 pos_type pos = cur.pos();
2121 // If cursor is at beginning or end of paragraph, do nothing.
2122 if (pos == cur.lastpos() || pos == 0)
2125 Paragraph & par = cur.paragraph();
2127 // Get the positions of the characters to be transposed.
2128 pos_type pos1 = pos - 1;
2129 pos_type pos2 = pos;
2131 // In change tracking mode, ignore deleted characters.
2132 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2134 if (pos2 == cur.lastpos())
2137 while (pos1 >= 0 && par.isDeleted(pos1))
2142 // Don't do anything if one of the "characters" is not regular text.
2143 if (par.isInset(pos1) || par.isInset(pos2))
2146 // Store the characters to be transposed (including font information).
2147 char_type const char1 = par.getChar(pos1);
2149 par.getFontSettings(cur.buffer()->params(), pos1);
2151 char_type const char2 = par.getChar(pos2);
2153 par.getFontSettings(cur.buffer()->params(), pos2);
2155 // And finally, we are ready to perform the transposition.
2156 // Track the changes if Change Tracking is enabled.
2157 bool const trackChanges = cur.buffer()->params().track_changes;
2161 par.eraseChar(pos2, trackChanges);
2162 par.eraseChar(pos1, trackChanges);
2163 par.insertChar(pos1, char2, font2, trackChanges);
2164 par.insertChar(pos2, char1, font1, trackChanges);
2166 cur.checkBufferStructure();
2168 // After the transposition, move cursor to after the transposition.
2169 setCursor(cur, cur.pit(), pos2);
2174 DocIterator Text::macrocontextPosition() const
2176 return macrocontext_position_;
2180 void Text::setMacrocontextPosition(DocIterator const & pos)
2182 macrocontext_position_ = pos;
2186 docstring Text::previousWord(CursorSlice const & sl) const
2188 CursorSlice from = sl;
2189 CursorSlice to = sl;
2190 getWord(from, to, PREVIOUS_WORD);
2191 if (sl == from || to == from)
2194 Paragraph const & par = sl.paragraph();
2195 return par.asString(from.pos(), to.pos());
2199 bool Text::completionSupported(Cursor const & cur) const
2201 Paragraph const & par = cur.paragraph();
2202 return !cur.selection()
2204 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2205 && !par.isWordSeparator(cur.pos() - 1);
2209 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2211 WordList const & list = theWordList(cur.getFont().language()->lang());
2212 return new TextCompletionList(cur, list);
2216 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2218 LBUFERR(cur.bv().cursor() == cur);
2220 cur.bv().cursor() = cur;
2221 if (!(cur.result().screenUpdate() & Update::Force))
2222 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2227 docstring Text::completionPrefix(Cursor const & cur) const
2229 return previousWord(cur.top());