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"
76 // TODO: replace if in Text::readParToken() with compile time switch
79 #include "support/metahash.h"
81 typedef boost::mpl::string<'\\end','_lay','out'> end_layout;
82 typedef boost::mpl::string<'\\end','in','set'> end_inset;
86 std::string token = "\\end_layout";
88 switch (boost::hash_value(token)) {
89 case lyx::support::hash_string<end_layout>::value:
91 case lyx::support::hash_string<end_inset>::value:
101 using namespace lyx::support;
105 using cap::cutSelection;
106 using cap::pasteParagraphList;
108 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
109 Paragraph & toPar, pos_type toPos, BufferParams const & params)
111 // Note: moveItem() does not honour change tracking!
112 // Therefore, it should only be used for breaking and merging paragraphs
114 // We need a copy here because the character at fromPos is going to be erased.
115 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
116 Change const tmpChange = fromPar.lookupChange(fromPos);
118 if (Inset * tmpInset = fromPar.getInset(fromPos)) {
119 fromPar.releaseInset(fromPos);
120 // The inset is not in fromPar any more.
121 if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
128 char_type const tmpChar = fromPar.getChar(fromPos);
129 fromPar.eraseChar(fromPos, false);
130 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
135 void breakParagraphConservative(BufferParams const & bparams,
136 ParagraphList & pars, pit_type par_offset, pos_type pos)
138 // create a new paragraph
139 Paragraph & tmp = *pars.insert(lyx::next(pars.begin(), par_offset + 1),
141 Paragraph & par = pars[par_offset];
143 tmp.setInsetOwner(&par.inInset());
144 tmp.makeSameLayout(par);
146 LASSERT(pos <= par.size(), return);
148 if (pos < par.size()) {
149 // move everything behind the break position to the new paragraph
150 pos_type pos_end = par.size() - 1;
152 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
153 if (moveItem(par, pos, tmp, j, bparams)) {
157 // Move over the end-of-par change information
158 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
159 par.setChange(par.size(), Change(bparams.track_changes ?
160 Change::INSERTED : Change::UNCHANGED));
165 void mergeParagraph(BufferParams const & bparams,
166 ParagraphList & pars, pit_type par_offset)
168 Paragraph & next = pars[par_offset + 1];
169 Paragraph & par = pars[par_offset];
171 pos_type pos_end = next.size() - 1;
172 pos_type pos_insert = par.size();
174 // the imaginary end-of-paragraph character (at par.size()) has to be
175 // marked as unmodified. Otherwise, its change is adopted by the first
176 // character of the next paragraph.
177 if (par.isChanged(par.size())) {
178 LYXERR(Debug::CHANGES,
179 "merging par with inserted/deleted end-of-par character");
180 par.setChange(par.size(), Change(Change::UNCHANGED));
183 Change change = next.lookupChange(next.size());
185 // move the content of the second paragraph to the end of the first one
186 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
187 if (moveItem(next, 0, par, j, bparams)) {
192 // move the change of the end-of-paragraph character
193 par.setChange(par.size(), change);
195 pars.erase(lyx::next(pars.begin(), par_offset + 1));
199 Text::Text(InsetText * owner, bool use_default_layout)
202 pars_.push_back(Paragraph());
203 Paragraph & par = pars_.back();
204 par.setInsetOwner(owner);
205 DocumentClass const & dc = owner->buffer().params().documentClass();
206 if (use_default_layout)
207 par.setDefaultLayout(dc);
209 par.setPlainLayout(dc);
213 Text::Text(InsetText * owner, Text const & text)
217 ParagraphList::iterator const end = pars_.end();
218 ParagraphList::iterator it = pars_.begin();
219 for (; it != end; ++it)
220 it->setInsetOwner(owner);
224 pit_type Text::depthHook(pit_type pit, depth_type depth) const
226 pit_type newpit = pit;
231 while (newpit != 0 && pars_[newpit].getDepth() > depth)
234 if (pars_[newpit].getDepth() > depth)
241 pit_type Text::outerHook(pit_type par_offset) const
243 Paragraph const & par = pars_[par_offset];
245 if (par.getDepth() == 0)
247 return depthHook(par_offset, depth_type(par.getDepth() - 1));
251 bool Text::isFirstInSequence(pit_type par_offset) const
253 Paragraph const & par = pars_[par_offset];
255 pit_type dhook_offset = depthHook(par_offset, par.getDepth());
257 if (dhook_offset == par_offset)
260 Paragraph const & dhook = pars_[dhook_offset];
262 return dhook.layout() != par.layout()
263 || dhook.getDepth() != par.getDepth();
267 int Text::getTocLevel(pit_type par_offset) const
269 Paragraph const & par = pars_[par_offset];
271 if (par.layout().isEnvironment() && !isFirstInSequence(par_offset))
272 return Layout::NOT_IN_TOC;
274 return par.layout().toclevel;
278 Font const Text::outerFont(pit_type par_offset) const
280 depth_type par_depth = pars_[par_offset].getDepth();
281 FontInfo tmpfont = inherit_font;
282 depth_type prev_par_depth = 0;
283 // Resolve against environment font information
284 while (par_offset != pit_type(pars_.size())
285 && par_depth != prev_par_depth
287 && !tmpfont.resolved()) {
288 prev_par_depth = par_depth;
289 par_offset = outerHook(par_offset);
290 if (par_offset != pit_type(pars_.size())) {
291 tmpfont.realize(pars_[par_offset].layout().font);
292 par_depth = pars_[par_offset].getDepth();
296 return Font(tmpfont);
300 static void acceptOrRejectChanges(ParagraphList & pars,
301 BufferParams const & bparams, Text::ChangeOp op)
303 pit_type pars_size = static_cast<pit_type>(pars.size());
305 // first, accept or reject changes within each individual
306 // paragraph (do not consider end-of-par)
307 for (pit_type pit = 0; pit < pars_size; ++pit) {
308 // prevent assertion failure
309 if (!pars[pit].empty()) {
310 if (op == Text::ACCEPT)
311 pars[pit].acceptChanges(0, pars[pit].size());
313 pars[pit].rejectChanges(0, pars[pit].size());
317 // next, accept or reject imaginary end-of-par characters
318 for (pit_type pit = 0; pit < pars_size; ++pit) {
319 pos_type pos = pars[pit].size();
320 if (pars[pit].isChanged(pos)) {
321 // keep the end-of-par char if it is inserted and accepted
322 // or when it is deleted and rejected.
323 if (pars[pit].isInserted(pos) == (op == Text::ACCEPT)) {
324 pars[pit].setChange(pos, Change(Change::UNCHANGED));
326 if (pit == pars_size - 1) {
327 // we cannot remove a par break at the end of the last
328 // paragraph; instead, we mark it unchanged
329 pars[pit].setChange(pos, Change(Change::UNCHANGED));
331 mergeParagraph(bparams, pars, pit);
341 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
343 acceptOrRejectChanges(pars, bparams, Text::ACCEPT);
347 void rejectChanges(ParagraphList & pars, BufferParams const & bparams)
349 acceptOrRejectChanges(pars, bparams, Text::REJECT);
353 InsetText const & Text::inset() const
360 void Text::readParToken(Paragraph & par, Lexer & lex,
361 string const & token, Font & font, Change & change, ErrorList & errorList)
363 Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
364 BufferParams & bp = buf->params();
366 if (token[0] != '\\') {
367 docstring dstr = lex.getDocString();
368 par.appendString(dstr, font, change);
370 } else if (token == "\\begin_layout") {
372 docstring layoutname = lex.getDocString();
374 font = Font(inherit_font, bp.language);
375 change = Change(Change::UNCHANGED);
377 DocumentClass const & tclass = bp.documentClass();
379 if (layoutname.empty())
380 layoutname = tclass.defaultLayoutName();
382 if (owner_->forcePlainLayout()) {
383 // in this case only the empty layout is allowed
384 layoutname = tclass.plainLayoutName();
385 } else if (par.usePlainLayout()) {
386 // in this case, default layout maps to empty layout
387 if (layoutname == tclass.defaultLayoutName())
388 layoutname = tclass.plainLayoutName();
390 // otherwise, the empty layout maps to the default
391 if (layoutname == tclass.plainLayoutName())
392 layoutname = tclass.defaultLayoutName();
395 // When we apply an unknown layout to a document, we add this layout to the textclass
396 // of this document. For example, when you apply class article to a beamer document,
397 // all unknown layouts such as frame will be added to document class article so that
398 // these layouts can keep their original names.
399 bool const added_one = tclass.addLayoutIfNeeded(layoutname);
402 docstring const s = bformat(_("Layout `%1$s' was not found."), layoutname);
404 ErrorItem(_("Layout Not Found"), s, par.id(), 0, par.size()));
407 par.setLayout(bp.documentClass()[layoutname]);
409 // Test whether the layout is obsolete.
410 Layout const & layout = par.layout();
411 if (!layout.obsoleted_by().empty())
412 par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
414 par.params().read(lex);
416 } else if (token == "\\end_layout") {
417 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
418 << "Missing \\begin_layout ?");
419 } else if (token == "\\end_inset") {
420 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
421 << "Missing \\begin_inset ?");
422 } else if (token == "\\begin_inset") {
423 Inset * inset = readInset(lex, buf);
425 par.insertInset(par.size(), inset, font, change);
428 docstring line = lex.getDocString();
429 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
430 par.id(), 0, par.size()));
432 } else if (token == "\\family") {
434 setLyXFamily(lex.getString(), font.fontInfo());
435 } else if (token == "\\series") {
437 setLyXSeries(lex.getString(), font.fontInfo());
438 } else if (token == "\\shape") {
440 setLyXShape(lex.getString(), font.fontInfo());
441 } else if (token == "\\size") {
443 setLyXSize(lex.getString(), font.fontInfo());
444 } else if (token == "\\lang") {
446 string const tok = lex.getString();
447 Language const * lang = languages.getLanguage(tok);
449 font.setLanguage(lang);
451 font.setLanguage(bp.language);
452 lex.printError("Unknown language `$$Token'");
454 } else if (token == "\\numeric") {
456 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
457 } else if (token == "\\emph") {
459 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
460 } else if (token == "\\bar") {
462 string const tok = lex.getString();
465 font.fontInfo().setUnderbar(FONT_ON);
466 else if (tok == "no")
467 font.fontInfo().setUnderbar(FONT_OFF);
468 else if (tok == "default")
469 font.fontInfo().setUnderbar(FONT_INHERIT);
471 lex.printError("Unknown bar font flag "
473 } else if (token == "\\strikeout") {
475 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
476 } else if (token == "\\uuline") {
478 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
479 } else if (token == "\\uwave") {
481 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
482 } else if (token == "\\noun") {
484 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
485 } else if (token == "\\color") {
487 setLyXColor(lex.getString(), font.fontInfo());
488 } else if (token == "\\SpecialChar" ||
489 (token == "\\SpecialCharNoPassThru" &&
490 !par.layout().pass_thru && !inset().isPassThru())) {
491 auto_ptr<Inset> inset;
492 inset.reset(new InsetSpecialChar);
494 inset->setBuffer(*buf);
495 par.insertInset(par.size(), inset.release(), font, change);
496 } else if (token == "\\SpecialCharNoPassThru") {
498 docstring const s = ltrim(lex.getDocString(), "\\");
499 par.insert(par.size(), s, font, change);
500 } else if (token == "\\IPAChar") {
501 auto_ptr<Inset> inset;
502 inset.reset(new InsetIPAChar);
504 inset->setBuffer(*buf);
505 par.insertInset(par.size(), inset.release(), font, change);
506 } else if (token == "\\twohyphens" || token == "\\threehyphens") {
507 // Ideally, this should be done by lyx2lyx, but lyx2lyx does not know the
508 // running font and does not know anything about layouts (and CopyStyle).
509 Layout const & layout(par.layout());
510 FontInfo info = font.fontInfo();
511 info.realize(layout.resfont);
512 if (layout.pass_thru || inset().isPassThru() ||
513 info.family() == TYPEWRITER_FAMILY) {
514 if (token == "\\twohyphens")
515 par.insert(par.size(), from_ascii("--"), font, change);
517 par.insert(par.size(), from_ascii("---"), font, change);
519 if (token == "\\twohyphens")
520 par.insertChar(par.size(), 0x2013, font, change);
522 par.insertChar(par.size(), 0x2014, font, change);
524 } else if (token == "\\backslash") {
525 par.appendChar('\\', font, change);
526 } else if (token == "\\LyXTable") {
527 auto_ptr<Inset> inset(new InsetTabular(buf));
529 par.insertInset(par.size(), inset.release(), font, change);
530 } else if (token == "\\change_unchanged") {
531 change = Change(Change::UNCHANGED);
532 } else if (token == "\\change_inserted" || token == "\\change_deleted") {
534 istringstream is(lex.getString());
538 BufferParams::AuthorMap const & am = bp.author_map_;
539 if (am.find(aid) == am.end()) {
540 errorList.push_back(ErrorItem(
541 _("Change tracking author index missing"),
542 bformat(_("A change tracking author information for index "
543 "%1$d is missing. This can happen after a wrong "
544 "merge by a version control system. In this case, "
545 "either fix the merge, or have this information "
546 "missing until the corresponding tracked changes "
547 "are merged or this user edits the file again.\n"),
549 par.id(), par.size(), par.size() + 1
551 bp.addAuthor(Author(aid));
553 if (token == "\\change_inserted")
554 change = Change(Change::INSERTED, am.find(aid)->second, ct);
556 change = Change(Change::DELETED, am.find(aid)->second, ct);
559 errorList.push_back(ErrorItem(_("Unknown token"),
560 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
562 par.id(), 0, par.size()));
567 void Text::readParagraph(Paragraph & par, Lexer & lex,
568 ErrorList & errorList)
571 string token = lex.getString();
573 Change change(Change::UNCHANGED);
576 readParToken(par, lex, token, font, change, errorList);
579 token = lex.getString();
584 if (token == "\\end_layout") {
585 //Ok, paragraph finished
589 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
590 if (token == "\\begin_layout" || token == "\\end_document"
591 || token == "\\end_inset" || token == "\\begin_deeper"
592 || token == "\\end_deeper") {
593 lex.pushToken(token);
594 lyxerr << "Paragraph ended in line "
595 << lex.lineNumber() << "\n"
596 << "Missing \\end_layout.\n";
600 // Final change goes to paragraph break:
601 par.setChange(par.size(), change);
603 // Initialize begin_of_body_ on load; redoParagraph maintains
604 par.setBeginOfBody();
606 // mark paragraph for spell checking on load
607 // par.requestSpellCheck();
611 class TextCompletionList : public CompletionList
615 TextCompletionList(Cursor const & cur, WordList const * list)
616 : buffer_(cur.buffer()), list_(list)
619 virtual ~TextCompletionList() {}
622 virtual bool sorted() const { return true; }
624 virtual size_t size() const
626 return list_->size();
629 virtual docstring const & data(size_t idx) const
631 return list_->word(idx);
636 Buffer const * buffer_;
638 WordList const * list_;
642 bool Text::empty() const
644 return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
645 // FIXME: Should we consider the labeled type as empty too?
646 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
650 double Text::spacing(Paragraph const & par) const
652 if (par.params().spacing().isDefault())
653 return owner_->buffer().params().spacing().getValue();
654 return par.params().spacing().getValue();
659 * This breaks a paragraph at the specified position.
660 * The new paragraph will:
661 * - Decrease depth by one (or change layout to default layout) when
662 * keep_layout == false
663 * - keep current depth and layout when keep_layout == true
665 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
668 BufferParams const & bparams = text.inset().buffer().params();
669 ParagraphList & pars = text.paragraphs();
670 // create a new paragraph, and insert into the list
671 ParagraphList::iterator tmp =
672 pars.insert(lyx::next(pars.begin(), par_offset + 1),
675 Paragraph & par = pars[par_offset];
677 // remember to set the inset_owner
678 tmp->setInsetOwner(&par.inInset());
679 // without doing that we get a crash when typing <Return> at the
680 // end of a paragraph
681 tmp->setPlainOrDefaultLayout(bparams.documentClass());
684 tmp->setLayout(par.layout());
685 tmp->setLabelWidthString(par.params().labelWidthString());
686 tmp->params().depth(par.params().depth());
687 } else if (par.params().depth() > 0) {
688 Paragraph const & hook = pars[text.outerHook(par_offset)];
689 tmp->setLayout(hook.layout());
690 // not sure the line below is useful
691 tmp->setLabelWidthString(par.params().labelWidthString());
692 tmp->params().depth(hook.params().depth());
695 bool const isempty = (par.allowEmpty() && par.empty());
697 if (!isempty && (par.size() > pos || par.empty())) {
698 tmp->setLayout(par.layout());
699 tmp->params().align(par.params().align());
700 tmp->setLabelWidthString(par.params().labelWidthString());
702 tmp->params().depth(par.params().depth());
703 tmp->params().noindent(par.params().noindent());
705 // move everything behind the break position
706 // to the new paragraph
708 /* Note: if !keepempty, empty() == true, then we reach
709 * here with size() == 0. So pos_end becomes - 1. This
710 * doesn't cause problems because both loops below
711 * enforce pos <= pos_end and 0 <= pos
713 pos_type pos_end = par.size() - 1;
715 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
716 if (moveItem(par, pos, *tmp, j, bparams)) {
722 // Move over the end-of-par change information
723 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
724 par.setChange(par.size(), Change(bparams.track_changes ?
725 Change::INSERTED : Change::UNCHANGED));
728 // Make sure that we keep the language when
729 // breaking paragraph.
731 Font changed = tmp->getFirstFontSettings(bparams);
732 Font const & old = par.getFontSettings(bparams, par.size());
733 changed.setLanguage(old.language());
734 tmp->setFont(0, changed);
741 bool const soa = par.params().startOfAppendix();
742 par.params().clear();
743 // do not lose start of appendix marker (bug 4212)
744 par.params().startOfAppendix(soa);
745 par.setPlainOrDefaultLayout(bparams.documentClass());
749 par.setLayout(tmp->layout());
750 par.setLabelWidthString(tmp->params().labelWidthString());
751 par.params().depth(tmp->params().depth());
756 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
758 LBUFERR(this == cur.text());
760 Paragraph & cpar = cur.paragraph();
761 pit_type cpit = cur.pit();
763 DocumentClass const & tclass = cur.buffer()->params().documentClass();
764 Layout const & layout = cpar.layout();
766 if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
767 if (changeDepthAllowed(cur, DEC_DEPTH)) {
768 changeDepth(cur, DEC_DEPTH);
769 pit_type const prev = depthHook(cpit, cpar.getDepth());
770 docstring const & lay = pars_[prev].layout().name();
771 if (lay != layout.name())
774 docstring const & lay = cur.paragraph().usePlainLayout()
775 ? tclass.plainLayoutName() : tclass.defaultLayoutName();
776 if (lay != layout.name())
784 // Always break behind a space
785 // It is better to erase the space (Dekel)
786 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
787 cpar.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
789 // What should the layout for the new paragraph be?
790 bool keep_layout = layout.isEnvironment()
791 || (layout.isParagraph() && layout.parbreak_is_newline);
793 keep_layout = !keep_layout;
795 // We need to remember this before we break the paragraph, because
796 // that invalidates the layout variable
797 bool sensitive = layout.labeltype == LABEL_SENSITIVE;
799 // we need to set this before we insert the paragraph.
800 bool const isempty = cpar.allowEmpty() && cpar.empty();
802 lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
804 // After this, neither paragraph contains any rows!
807 pit_type next_par = cpit + 1;
809 // well this is the caption hack since one caption is really enough
812 // set to standard-layout
813 //FIXME Check if this should be plainLayout() in some cases
814 pars_[cpit].applyLayout(tclass.defaultLayout());
816 // set to standard-layout
817 //FIXME Check if this should be plainLayout() in some cases
818 pars_[next_par].applyLayout(tclass.defaultLayout());
821 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
822 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().track_changes))
823 break; // the character couldn't be deleted physically due to change tracking
826 // A singlePar update is not enough in this case.
827 cur.screenUpdateFlags(Update::Force);
828 cur.forceBufferUpdate();
830 // This check is necessary. Otherwise the new empty paragraph will
831 // be deleted automatically. And it is more friendly for the user!
832 if (cur.pos() != 0 || isempty)
833 setCursor(cur, cur.pit() + 1, 0);
835 setCursor(cur, cur.pit(), 0);
839 // needed to insert the selection
840 void Text::insertStringAsLines(Cursor & cur, docstring const & str,
843 BufferParams const & bparams = owner_->buffer().params();
844 pit_type pit = cur.pit();
845 pos_type pos = cur.pos();
847 // insert the string, don't insert doublespace
848 bool space_inserted = true;
849 for (docstring::const_iterator cit = str.begin();
850 cit != str.end(); ++cit) {
851 Paragraph & par = pars_[pit];
853 if (inset().allowMultiPar() && (!par.empty() || par.allowEmpty())) {
854 lyx::breakParagraph(*this, pit, pos,
855 par.layout().isEnvironment());
858 space_inserted = true;
862 // do not insert consecutive spaces if !free_spacing
863 } else if ((*cit == ' ' || *cit == '\t') &&
864 space_inserted && !par.isFreeSpacing()) {
866 } else if (*cit == '\t') {
867 if (!par.isFreeSpacing()) {
868 // tabs are like spaces here
869 par.insertChar(pos, ' ', font, bparams.track_changes);
871 space_inserted = true;
873 par.insertChar(pos, *cit, font, bparams.track_changes);
875 space_inserted = true;
877 } else if (!isPrintable(*cit)) {
878 // Ignore unprintables
881 // just insert the character
882 par.insertChar(pos, *cit, font, bparams.track_changes);
884 space_inserted = (*cit == ' ');
887 setCursor(cur, pit, pos);
891 // turn double CR to single CR, others are converted into one
892 // blank. Then insertStringAsLines is called
893 void Text::insertStringAsParagraphs(Cursor & cur, docstring const & str,
896 docstring linestr = str;
897 bool newline_inserted = false;
899 for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
900 if (linestr[i] == '\n') {
901 if (newline_inserted) {
902 // we know that \r will be ignored by
903 // insertStringAsLines. Of course, it is a dirty
904 // trick, but it works...
905 linestr[i - 1] = '\r';
909 newline_inserted = true;
911 } else if (isPrintable(linestr[i])) {
912 newline_inserted = false;
915 insertStringAsLines(cur, linestr, font);
919 // insert a character, moves all the following breaks in the
920 // same Paragraph one to the right and make a rebreak
921 void Text::insertChar(Cursor & cur, char_type c)
923 LBUFERR(this == cur.text());
925 cur.recordUndo(INSERT_UNDO);
927 TextMetrics const & tm = cur.bv().textMetrics(this);
928 Buffer const & buffer = *cur.buffer();
929 Paragraph & par = cur.paragraph();
930 // try to remove this
931 pit_type const pit = cur.pit();
933 bool const freeSpacing = par.layout().free_spacing ||
936 if (lyxrc.auto_number) {
937 static docstring const number_operators = from_ascii("+-/*");
938 static docstring const number_unary_operators = from_ascii("+-");
939 static docstring const number_seperators = from_ascii(".,:");
941 if (cur.current_font.fontInfo().number() == FONT_ON) {
942 if (!isDigitASCII(c) && !contains(number_operators, c) &&
943 !(contains(number_seperators, c) &&
945 cur.pos() != cur.lastpos() &&
946 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
947 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
949 number(cur); // Set current_font.number to OFF
950 } else if (isDigitASCII(c) &&
951 cur.real_current_font.isVisibleRightToLeft()) {
952 number(cur); // Set current_font.number to ON
954 if (cur.pos() != 0) {
955 char_type const c = par.getChar(cur.pos() - 1);
956 if (contains(number_unary_operators, c) &&
958 || par.isSeparator(cur.pos() - 2)
959 || par.isEnvSeparator(cur.pos() - 2)
960 || par.isNewline(cur.pos() - 2))
962 setCharFont(pit, cur.pos() - 1, cur.current_font,
964 } else if (contains(number_seperators, c)
966 && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
967 setCharFont(pit, cur.pos() - 1, cur.current_font,
974 // In Bidi text, we want spaces to be treated in a special way: spaces
975 // which are between words in different languages should get the
976 // paragraph's language; otherwise, spaces should keep the language
977 // they were originally typed in. This is only in effect while typing;
978 // after the text is already typed in, the user can always go back and
979 // explicitly set the language of a space as desired. But 99.9% of the
980 // time, what we're doing here is what the user actually meant.
982 // The following cases are the ones in which the language of the space
983 // should be changed to match that of the containing paragraph. In the
984 // depictions, lowercase is LTR, uppercase is RTL, underscore (_)
985 // represents a space, pipe (|) represents the cursor position (so the
986 // character before it is the one just typed in). The different cases
987 // are depicted logically (not visually), from left to right:
992 // Theoretically, there are other situations that we should, perhaps, deal
993 // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any
994 // point (to understand why, just try to create this situation...).
996 if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
997 // get font in front and behind the space in question. But do NOT
998 // use getFont(cur.pos()) because the character c is not inserted yet
999 Font const pre_space_font = tm.displayFont(cur.pit(), cur.pos() - 2);
1000 Font const & post_space_font = cur.real_current_font;
1001 bool pre_space_rtl = pre_space_font.isVisibleRightToLeft();
1002 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
1004 if (pre_space_rtl != post_space_rtl) {
1005 // Set the space's language to match the language of the
1006 // adjacent character whose direction is the paragraph's
1007 // direction; don't touch other properties of the font
1008 Language const * lang =
1009 (pre_space_rtl == par.isRTL(buffer.params())) ?
1010 pre_space_font.language() : post_space_font.language();
1012 Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
1013 space_font.setLanguage(lang);
1014 par.setFont(cur.pos() - 1, space_font);
1018 // Next check, if there will be two blanks together or a blank at
1019 // the beginning of a paragraph.
1020 // I decided to handle blanks like normal characters, the main
1021 // difference are the special checks when calculating the row.fill
1022 // (blank does not count at the end of a row) and the check here
1024 // When the free-spacing option is set for the current layout,
1025 // disable the double-space checking
1026 if (!freeSpacing && isLineSeparatorChar(c)) {
1027 if (cur.pos() == 0) {
1029 "You cannot insert a space at the "
1030 "beginning of a paragraph. Please read the Tutorial."));
1033 // LASSERT: Is it safe to continue here?
1034 LASSERT(cur.pos() > 0, /**/);
1035 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
1036 && !par.isDeleted(cur.pos() - 1)) {
1038 "You cannot type two spaces this way. "
1039 "Please read the Tutorial."));
1044 // Prevent to insert uncodable characters in verbatim and ERT
1045 // (workaround for bug 9012)
1046 // Don't do it for listings inset, since InsetListings::latex() tries
1047 // to switch to a usable encoding which works in many cases (bug 9102).
1048 if (cur.paragraph().isPassThru() && owner_->lyxCode() != LISTINGS_CODE &&
1049 cur.current_font.language()) {
1050 Encoding const * e = cur.current_font.language()->encoding();
1051 if (!e->encodable(c)) {
1052 cur.message(_("Character is uncodable in verbatim paragraphs."));
1057 pos_type pos = cur.pos();
1058 if (!cur.paragraph().isPassThru() && owner_->lyxCode() != IPA_CODE &&
1059 cur.real_current_font.fontInfo().family() != TYPEWRITER_FAMILY &&
1060 c == '-' && pos > 0) {
1061 if (par.getChar(pos - 1) == '-') {
1062 // convert "--" to endash
1063 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1066 } else if (par.getChar(pos - 1) == 0x2013) {
1067 // convert "---" to emdash
1068 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1071 } else if (par.getChar(pos - 1) == 0x2014) {
1072 // convert "----" to "-"
1073 par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
1079 par.insertChar(pos, c, cur.current_font,
1080 cur.buffer()->params().track_changes);
1081 cur.checkBufferStructure();
1083 // cur.screenUpdateFlags(Update::Force);
1084 bool boundary = cur.boundary()
1085 || tm.isRTLBoundary(cur.pit(), pos + 1);
1086 setCursor(cur, cur.pit(), pos + 1, false, boundary);
1091 void Text::charInserted(Cursor & cur)
1093 Paragraph & par = cur.paragraph();
1095 // register word if a non-letter was entered
1097 && !par.isWordSeparator(cur.pos() - 2)
1098 && par.isWordSeparator(cur.pos() - 1)) {
1099 // get the word in front of cursor
1100 LBUFERR(this == cur.text());
1101 cur.paragraph().updateWords();
1106 // the cursor set functions have a special mechanism. When they
1107 // realize, that you left an empty paragraph, they will delete it.
1109 bool Text::cursorForwardOneWord(Cursor & cur)
1111 LBUFERR(this == cur.text());
1113 pos_type const lastpos = cur.lastpos();
1114 pit_type pit = cur.pit();
1115 pos_type pos = cur.pos();
1116 Paragraph const & par = cur.paragraph();
1118 // Paragraph boundary is a word boundary
1119 if (pos == lastpos || (pos + 1 == lastpos && par.isEnvSeparator(pos))) {
1120 if (pit != cur.lastpit())
1121 return setCursor(cur, pit + 1, 0);
1126 if (lyxrc.mac_like_cursor_movement) {
1127 // Skip through trailing punctuation and spaces.
1128 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
1131 // Skip over either a non-char inset or a full word
1132 if (pos != lastpos && par.isWordSeparator(pos))
1134 else while (pos != lastpos && !par.isWordSeparator(pos))
1137 LASSERT(pos < lastpos, return false); // see above
1138 if (!par.isWordSeparator(pos))
1139 while (pos != lastpos && !par.isWordSeparator(pos))
1141 else if (par.isChar(pos))
1142 while (pos != lastpos && par.isChar(pos))
1144 else if (!par.isSpace(pos)) // non-char inset
1147 // Skip over white space
1148 while (pos != lastpos && par.isSpace(pos))
1152 // Don't skip a separator inset at the end of a paragraph
1153 if (pos == lastpos && pos && par.isEnvSeparator(pos - 1))
1156 return setCursor(cur, pit, pos);
1160 bool Text::cursorBackwardOneWord(Cursor & cur)
1162 LBUFERR(this == cur.text());
1164 pit_type pit = cur.pit();
1165 pos_type pos = cur.pos();
1166 Paragraph & par = cur.paragraph();
1168 // Paragraph boundary is a word boundary
1169 if (pos == 0 && pit != 0) {
1170 Paragraph & prevpar = getPar(pit - 1);
1171 pos = prevpar.size();
1172 // Don't stop after an environment separator
1173 if (pos && prevpar.isEnvSeparator(pos - 1))
1175 return setCursor(cur, pit - 1, pos);
1178 if (lyxrc.mac_like_cursor_movement) {
1179 // Skip through punctuation and spaces.
1180 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1183 // Skip over either a non-char inset or a full word
1184 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1186 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1189 // Skip over white space
1190 while (pos != 0 && par.isSpace(pos - 1))
1193 if (pos != 0 && !par.isWordSeparator(pos - 1))
1194 while (pos != 0 && !par.isWordSeparator(pos - 1))
1196 else if (pos != 0 && par.isChar(pos - 1))
1197 while (pos != 0 && par.isChar(pos - 1))
1199 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1203 return setCursor(cur, pit, pos);
1207 bool Text::cursorVisLeftOneWord(Cursor & cur)
1209 LBUFERR(this == cur.text());
1211 pos_type left_pos, right_pos;
1213 Cursor temp_cur = cur;
1215 // always try to move at least once...
1216 while (temp_cur.posVisLeft(true /* skip_inset */)) {
1218 // collect some information about current cursor position
1219 temp_cur.getSurroundingPos(left_pos, right_pos);
1220 bool left_is_letter =
1221 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1222 bool right_is_letter =
1223 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1225 // if we're not at a letter/non-letter boundary, continue moving
1226 if (left_is_letter == right_is_letter)
1229 // we should stop when we have an LTR word on our right or an RTL word
1231 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1232 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1233 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1234 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1238 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1239 true, temp_cur.boundary());
1243 bool Text::cursorVisRightOneWord(Cursor & cur)
1245 LBUFERR(this == cur.text());
1247 pos_type left_pos, right_pos;
1249 Cursor temp_cur = cur;
1251 // always try to move at least once...
1252 while (temp_cur.posVisRight(true /* skip_inset */)) {
1254 // collect some information about current cursor position
1255 temp_cur.getSurroundingPos(left_pos, right_pos);
1256 bool left_is_letter =
1257 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1258 bool right_is_letter =
1259 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1261 // if we're not at a letter/non-letter boundary, continue moving
1262 if (left_is_letter == right_is_letter)
1265 // we should stop when we have an LTR word on our right or an RTL word
1267 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1268 temp_cur.buffer()->params(),
1269 left_pos).isRightToLeft())
1270 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1271 temp_cur.buffer()->params(),
1272 right_pos).isRightToLeft()))
1276 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1277 true, temp_cur.boundary());
1281 void Text::selectWord(Cursor & cur, word_location loc)
1283 LBUFERR(this == cur.text());
1284 CursorSlice from = cur.top();
1286 getWord(from, to, loc);
1287 if (cur.top() != from)
1288 setCursor(cur, from.pit(), from.pos());
1291 if (!cur.selection())
1293 setCursor(cur, to.pit(), to.pos());
1295 cur.setWordSelection(true);
1299 void Text::selectAll(Cursor & cur)
1301 LBUFERR(this == cur.text());
1302 if (cur.lastpos() == 0 && cur.lastpit() == 0)
1304 // If the cursor is at the beginning, make sure the cursor ends there
1305 if (cur.pit() == 0 && cur.pos() == 0) {
1306 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1308 setCursor(cur, 0, 0);
1310 setCursor(cur, 0, 0);
1312 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1318 // Select the word currently under the cursor when no
1319 // selection is currently set
1320 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1322 LBUFERR(this == cur.text());
1323 if (cur.selection())
1325 selectWord(cur, loc);
1326 return cur.selection();
1330 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1332 LBUFERR(this == cur.text());
1334 if (!cur.selection()) {
1335 if (!selectChange(cur))
1339 cur.recordUndoSelection();
1341 pit_type begPit = cur.selectionBegin().pit();
1342 pit_type endPit = cur.selectionEnd().pit();
1344 pos_type begPos = cur.selectionBegin().pos();
1345 pos_type endPos = cur.selectionEnd().pos();
1347 // keep selection info, because endPos becomes invalid after the first loop
1348 bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1350 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1351 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1352 pos_type parSize = pars_[pit].size();
1354 // ignore empty paragraphs; otherwise, an assertion will fail for
1355 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1359 // do not consider first paragraph if the cursor starts at pos size()
1360 if (pit == begPit && begPos == parSize)
1363 // do not consider last paragraph if the cursor ends at pos 0
1364 if (pit == endPit && endPos == 0)
1365 break; // last iteration anyway
1367 pos_type left = (pit == begPit ? begPos : 0);
1368 pos_type right = (pit == endPit ? endPos : parSize);
1371 // there is no change here
1375 pars_[pit].acceptChanges(left, right);
1377 pars_[pit].rejectChanges(left, right);
1381 // next, accept/reject imaginary end-of-par characters
1383 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1384 pos_type pos = pars_[pit].size();
1386 // skip if the selection ends before the end-of-par
1387 if (pit == endPit && endsBeforeEndOfPar)
1388 break; // last iteration anyway
1390 // skip if this is not the last paragraph of the document
1391 // note: the user should be able to accept/reject the par break of the last par!
1392 if (pit == endPit && pit + 1 != int(pars_.size()))
1393 break; // last iteration anway
1396 if (pars_[pit].isInserted(pos)) {
1397 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1398 } else if (pars_[pit].isDeleted(pos)) {
1399 if (pit + 1 == int(pars_.size())) {
1400 // we cannot remove a par break at the end of the last paragraph;
1401 // instead, we mark it unchanged
1402 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1404 mergeParagraph(cur.buffer()->params(), pars_, pit);
1410 if (pars_[pit].isDeleted(pos)) {
1411 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1412 } else if (pars_[pit].isInserted(pos)) {
1413 if (pit + 1 == int(pars_.size())) {
1414 // we mark the par break at the end of the last paragraph unchanged
1415 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1417 mergeParagraph(cur.buffer()->params(), pars_, pit);
1425 // finally, invoke the DEPM
1426 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().track_changes);
1429 cur.clearSelection();
1430 setCursorIntern(cur, begPit, begPos);
1431 cur.screenUpdateFlags(Update::Force);
1432 cur.forceBufferUpdate();
1436 void Text::acceptChanges()
1438 BufferParams const & bparams = owner_->buffer().params();
1439 lyx::acceptChanges(pars_, bparams);
1440 deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.track_changes);
1444 void Text::rejectChanges()
1446 BufferParams const & bparams = owner_->buffer().params();
1447 pit_type pars_size = static_cast<pit_type>(pars_.size());
1449 // first, reject changes within each individual paragraph
1450 // (do not consider end-of-par)
1451 for (pit_type pit = 0; pit < pars_size; ++pit) {
1452 if (!pars_[pit].empty()) // prevent assertion failure
1453 pars_[pit].rejectChanges(0, pars_[pit].size());
1456 // next, reject imaginary end-of-par characters
1457 for (pit_type pit = 0; pit < pars_size; ++pit) {
1458 pos_type pos = pars_[pit].size();
1460 if (pars_[pit].isDeleted(pos)) {
1461 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1462 } else if (pars_[pit].isInserted(pos)) {
1463 if (pit == pars_size - 1) {
1464 // we mark the par break at the end of the last
1465 // paragraph unchanged
1466 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1468 mergeParagraph(bparams, pars_, pit);
1475 // finally, invoke the DEPM
1476 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.track_changes);
1480 void Text::deleteWordForward(Cursor & cur)
1482 LBUFERR(this == cur.text());
1483 if (cur.lastpos() == 0)
1487 cur.setSelection(true);
1488 cursorForwardOneWord(cur);
1490 cutSelection(cur, true, false);
1491 cur.checkBufferStructure();
1496 void Text::deleteWordBackward(Cursor & cur)
1498 LBUFERR(this == cur.text());
1499 if (cur.lastpos() == 0)
1500 cursorBackward(cur);
1503 cur.setSelection(true);
1504 cursorBackwardOneWord(cur);
1506 cutSelection(cur, true, false);
1507 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.text()->setCursor(cur.top(), 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 // Pasting is not allowed, if the paragraphs have different
1674 // layouts. I think it is a real bug of all other
1675 // word processors to allow it. It confuses the user.
1676 // Correction: Pasting is always allowed with standard-layout
1677 // or the empty layout.
1678 else if (par.layout() == prevpar.layout()
1679 || tclass.isDefaultLayout(par.layout())
1680 || tclass.isPlainLayout(par.layout())) {
1681 cur.recordUndo(prevcur.pit());
1682 mergeParagraph(bufparams, plist, prevcur.pit());
1687 cur.forceBufferUpdate();
1688 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1695 bool Text::backspace(Cursor & cur)
1697 LBUFERR(this == cur.text());
1698 bool needsUpdate = false;
1699 if (cur.pos() == 0) {
1701 return dissolveInset(cur);
1703 Cursor prev_cur = cur;
1706 if (!prev_cur.paragraph().isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) {
1707 cur.recordUndo(prev_cur.pit(), prev_cur.pit());
1708 prev_cur.paragraph().setChange(prev_cur.lastpos(), Change(Change::DELETED));
1709 setCursorIntern(cur, prev_cur.pit(), prev_cur.lastpos());
1712 // The cursor is at the beginning of a paragraph, so
1713 // the backspace will collapse two paragraphs into one.
1714 needsUpdate = backspacePos0(cur);
1717 // this is the code for a normal backspace, not pasting
1719 cur.recordUndo(DELETE_UNDO);
1720 // We used to do cursorBackwardIntern() here, but it is
1721 // not a good idea since it triggers the auto-delete
1722 // mechanism. So we do a cursorBackwardIntern()-lite,
1723 // without the dreaded mechanism. (JMarc)
1724 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1725 false, cur.boundary());
1726 bool const was_inset = cur.paragraph().isInset(cur.pos());
1727 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().track_changes);
1729 cur.forceBufferUpdate();
1731 cur.checkBufferStructure();
1734 if (cur.pos() == cur.lastpos())
1735 cur.setCurrentFont();
1737 needsUpdate |= handleBibitems(cur);
1739 // A singlePar update is not enough in this case.
1740 // cur.screenUpdateFlags(Update::Force);
1741 setCursor(cur.top(), cur.pit(), cur.pos());
1747 bool Text::dissolveInset(Cursor & cur)
1749 LASSERT(this == cur.text(), return false);
1751 if (isMainText() || cur.inset().nargs() != 1)
1754 cur.recordUndoInset();
1756 cur.selHandle(false);
1758 pos_type spos = cur.pos();
1759 pit_type spit = cur.pit();
1760 ParagraphList plist;
1761 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1762 plist = paragraphs();
1764 // store cursor offset
1768 Buffer & b = *cur.buffer();
1769 cur.paragraph().eraseChar(cur.pos(), b.params().track_changes);
1771 if (!plist.empty()) {
1773 // we clear the cache so that we won't get conflicts with labels
1774 // that get pasted into the buffer. we should update this before
1775 // its being empty matters. if not (i.e., if we encounter bugs),
1776 // then this should instead be:
1777 // cur.buffer().updateBuffer();
1778 // but we'll try the cheaper solution here.
1779 cur.buffer()->clearReferenceCache();
1781 // ERT paragraphs have the Language latex_language.
1782 // This is invalid outside of ERT, so we need to
1783 // change it to the buffer language.
1784 ParagraphList::iterator it = plist.begin();
1785 ParagraphList::iterator it_end = plist.end();
1786 for (; it != it_end; ++it)
1787 it->changeLanguage(b.params(), latex_language, b.language());
1789 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1790 b.errorList("Paste"));
1792 cur.pit() = min(cur.lastpit(), spit);
1793 cur.pos() = min(cur.lastpos(), spos);
1796 cur.forceBufferUpdate();
1798 // Ensure the current language is set correctly (bug 6292)
1799 cur.text()->setCursor(cur, cur.pit(), cur.pos());
1800 cur.clearSelection();
1806 void Text::getWord(CursorSlice & from, CursorSlice & to,
1807 word_location const loc) const
1810 pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1814 void Text::write(ostream & os) const
1816 Buffer const & buf = owner_->buffer();
1817 ParagraphList::const_iterator pit = paragraphs().begin();
1818 ParagraphList::const_iterator end = paragraphs().end();
1820 for (; pit != end; ++pit)
1821 pit->write(os, buf.params(), dth);
1823 // Close begin_deeper
1824 for(; dth > 0; --dth)
1825 os << "\n\\end_deeper";
1829 bool Text::read(Lexer & lex,
1830 ErrorList & errorList, InsetText * insetPtr)
1832 Buffer const & buf = owner_->buffer();
1833 depth_type depth = 0;
1836 while (lex.isOK()) {
1838 string const token = lex.getString();
1843 if (token == "\\end_inset")
1846 if (token == "\\end_body")
1849 if (token == "\\begin_body")
1852 if (token == "\\end_document") {
1857 if (token == "\\begin_layout") {
1858 lex.pushToken(token);
1861 par.setInsetOwner(insetPtr);
1862 par.params().depth(depth);
1863 par.setFont(0, Font(inherit_font, buf.params().language));
1864 pars_.push_back(par);
1865 readParagraph(pars_.back(), lex, errorList);
1867 // register the words in the global word list
1868 pars_.back().updateWords();
1869 } else if (token == "\\begin_deeper") {
1871 } else if (token == "\\end_deeper") {
1873 lex.printError("\\end_deeper: " "depth is already null");
1877 LYXERR0("Handling unknown body token: `" << token << '\'');
1881 // avoid a crash on weird documents (bug 4859)
1882 if (pars_.empty()) {
1884 par.setInsetOwner(insetPtr);
1885 par.params().depth(depth);
1886 par.setFont(0, Font(inherit_font,
1887 buf.params().language));
1888 par.setPlainOrDefaultLayout(buf.params().documentClass());
1889 pars_.push_back(par);
1896 // Returns the current font and depth as a message.
1897 docstring Text::currentState(Cursor const & cur) const
1899 LBUFERR(this == cur.text());
1900 Buffer & buf = *cur.buffer();
1901 Paragraph const & par = cur.paragraph();
1902 odocstringstream os;
1904 if (buf.params().track_changes)
1905 os << _("[Change Tracking] ");
1907 Change change = par.lookupChange(cur.pos());
1909 if (change.changed()) {
1910 docstring const author =
1911 buf.params().authors().get(change.author).nameAndEmail();
1912 docstring const date = formatted_datetime(change.changetime);
1913 os << bformat(_("Changed by %1$s[[author]] on %2$s[[date]]. "),
1917 // I think we should only show changes from the default
1919 // No, from the document font (MV)
1920 Font font = cur.real_current_font;
1921 font.fontInfo().reduce(buf.params().getFont().fontInfo());
1923 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1925 // The paragraph depth
1926 int depth = cur.paragraph().getDepth();
1928 os << bformat(_(", Depth: %1$d"), depth);
1930 // The paragraph spacing, but only if different from
1932 Spacing const & spacing = par.params().spacing();
1933 if (!spacing.isDefault()) {
1934 os << _(", Spacing: ");
1935 switch (spacing.getSpace()) {
1936 case Spacing::Single:
1939 case Spacing::Onehalf:
1942 case Spacing::Double:
1945 case Spacing::Other:
1946 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1948 case Spacing::Default:
1949 // should never happen, do nothing
1954 #ifdef DEVEL_VERSION
1955 os << _(", Inset: ") << &cur.inset();
1956 os << _(", Paragraph: ") << cur.pit();
1957 os << _(", Id: ") << par.id();
1958 os << _(", Position: ") << cur.pos();
1959 // FIXME: Why is the check for par.size() needed?
1960 // We are called with cur.pos() == par.size() quite often.
1961 if (!par.empty() && cur.pos() < par.size()) {
1962 // Force output of code point, not character
1963 size_t const c = par.getChar(cur.pos());
1964 os << _(", Char: 0x") << hex << c;
1966 os << _(", Boundary: ") << cur.boundary();
1967 // Row & row = cur.textRow();
1968 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1974 docstring Text::getPossibleLabel(Cursor const & cur) const
1976 pit_type pit = cur.pit();
1978 Layout const * layout = &(pars_[pit].layout());
1981 docstring par_text = pars_[pit].asString();
1983 // The return string of math matrices might contain linebreaks
1984 par_text = subst(par_text, '\n', '-');
1985 int const numwords = 3;
1986 for (int i = 0; i < numwords; ++i) {
1987 if (par_text.empty())
1990 par_text = split(par_text, head, ' ');
1991 // Is it legal to use spaces in labels ?
1997 // Make sure it isn't too long
1998 unsigned int const max_label_length = 32;
1999 if (text.size() > max_label_length)
2000 text.resize(max_label_length);
2002 // Will contain the label prefix.
2005 // For section, subsection, etc...
2006 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2007 Layout const * layout2 = &(pars_[pit - 1].layout());
2008 if (layout2->latextype != LATEX_PARAGRAPH) {
2013 if (layout->latextype != LATEX_PARAGRAPH)
2014 name = layout->refprefix;
2016 // For captions, we just take the caption type
2017 Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
2018 if (caption_inset) {
2019 string const & ftype = static_cast<InsetCaption *>(caption_inset)->floattype();
2020 FloatList const & fl = cur.buffer()->params().documentClass().floats();
2021 if (fl.typeExist(ftype)) {
2022 Floating const & flt = fl.getType(ftype);
2023 name = from_utf8(flt.refPrefix());
2026 name = from_utf8(ftype.substr(0,3));
2029 // If none of the above worked, see if the inset knows.
2031 InsetLayout const & il = cur.inset().getLayout();
2032 name = il.refprefix();
2036 text = name + ':' + text;
2038 // We need a unique label
2039 docstring label = text;
2041 while (cur.buffer()->insetLabel(label)) {
2042 label = text + '-' + convert<docstring>(i);
2050 docstring Text::asString(int options) const
2052 return asString(0, pars_.size(), options);
2056 docstring Text::asString(pit_type beg, pit_type end, int options) const
2058 size_t i = size_t(beg);
2059 docstring str = pars_[i].asString(options);
2060 for (++i; i != size_t(end); ++i) {
2062 str += pars_[i].asString(options);
2068 void Text::shortenForOutliner(docstring & str, size_t const maxlen)
2070 support::truncateWithEllipsis(str, maxlen);
2071 docstring::iterator it = str.begin();
2072 docstring::iterator end = str.end();
2073 for (; it != end; ++it)
2074 if ((*it) == L'\n' || (*it) == L'\t')
2079 void Text::forOutliner(docstring & os, size_t const maxlen,
2080 bool const shorten) const
2082 size_t tmplen = shorten ? maxlen + 1 : maxlen;
2083 for (size_t i = 0; i != pars_.size() && os.length() < tmplen; ++i)
2084 pars_[i].forOutliner(os, tmplen, false);
2086 shortenForOutliner(os, maxlen);
2090 void Text::charsTranspose(Cursor & cur)
2092 LBUFERR(this == cur.text());
2094 pos_type pos = cur.pos();
2096 // If cursor is at beginning or end of paragraph, do nothing.
2097 if (pos == cur.lastpos() || pos == 0)
2100 Paragraph & par = cur.paragraph();
2102 // Get the positions of the characters to be transposed.
2103 pos_type pos1 = pos - 1;
2104 pos_type pos2 = pos;
2106 // In change tracking mode, ignore deleted characters.
2107 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2109 if (pos2 == cur.lastpos())
2112 while (pos1 >= 0 && par.isDeleted(pos1))
2117 // Don't do anything if one of the "characters" is not regular text.
2118 if (par.isInset(pos1) || par.isInset(pos2))
2121 // Store the characters to be transposed (including font information).
2122 char_type const char1 = par.getChar(pos1);
2124 par.getFontSettings(cur.buffer()->params(), pos1);
2126 char_type const char2 = par.getChar(pos2);
2128 par.getFontSettings(cur.buffer()->params(), pos2);
2130 // And finally, we are ready to perform the transposition.
2131 // Track the changes if Change Tracking is enabled.
2132 bool const trackChanges = cur.buffer()->params().track_changes;
2136 par.eraseChar(pos2, trackChanges);
2137 par.eraseChar(pos1, trackChanges);
2138 par.insertChar(pos1, char2, font2, trackChanges);
2139 par.insertChar(pos2, char1, font1, trackChanges);
2141 cur.checkBufferStructure();
2143 // After the transposition, move cursor to after the transposition.
2144 setCursor(cur, cur.pit(), pos2);
2149 DocIterator Text::macrocontextPosition() const
2151 return macrocontext_position_;
2155 void Text::setMacrocontextPosition(DocIterator const & pos)
2157 macrocontext_position_ = pos;
2161 docstring Text::previousWord(CursorSlice const & sl) const
2163 CursorSlice from = sl;
2164 CursorSlice to = sl;
2165 getWord(from, to, PREVIOUS_WORD);
2166 if (sl == from || to == from)
2169 Paragraph const & par = sl.paragraph();
2170 return par.asString(from.pos(), to.pos());
2174 bool Text::completionSupported(Cursor const & cur) const
2176 Paragraph const & par = cur.paragraph();
2177 return cur.pos() > 0
2178 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2179 && !par.isWordSeparator(cur.pos() - 1);
2183 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2185 WordList const * list = theWordList(cur.getFont().language()->lang());
2186 return new TextCompletionList(cur, list);
2190 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2192 LBUFERR(cur.bv().cursor() == cur);
2194 cur.bv().cursor() = cur;
2195 if (!(cur.result().screenUpdate() & Update::Force))
2196 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2201 docstring Text::completionPrefix(Cursor const & cur) const
2203 return previousWord(cur.top());