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/InsetSpace.h"
59 #include "insets/InsetSpecialChar.h"
60 #include "insets/InsetTabular.h"
62 #include "support/debug.h"
63 #include "support/docstream.h"
64 #include "support/gettext.h"
65 #include "support/lassert.h"
66 #include "support/lstrings.h"
67 #include "support/textutils.h"
69 #include <boost/next_prior.hpp>
75 // TODO: replace if in Text::readParToken() with compile time switch
78 #include "support/metahash.h"
80 typedef boost::mpl::string<'\\end','_lay','out'> end_layout;
81 typedef boost::mpl::string<'\\end','in','set'> end_inset;
85 std::string token = "\\end_layout";
87 switch (boost::hash_value(token)) {
88 case lyx::support::hash_string<end_layout>::value:
90 case lyx::support::hash_string<end_inset>::value:
100 using namespace lyx::support;
104 using cap::cutSelection;
105 using cap::pasteParagraphList;
107 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
108 Paragraph & toPar, pos_type toPos, BufferParams const & params)
110 // Note: moveItem() does not honour change tracking!
111 // Therefore, it should only be used for breaking and merging paragraphs
113 // We need a copy here because the character at fromPos is going to be erased.
114 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
115 Change const tmpChange = fromPar.lookupChange(fromPos);
117 if (Inset * tmpInset = fromPar.getInset(fromPos)) {
118 fromPar.releaseInset(fromPos);
119 // The inset is not in fromPar any more.
120 if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
127 char_type const tmpChar = fromPar.getChar(fromPos);
128 fromPar.eraseChar(fromPos, false);
129 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
134 void breakParagraphConservative(BufferParams const & bparams,
135 ParagraphList & pars, pit_type par_offset, pos_type pos)
137 // create a new paragraph
138 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
140 Paragraph & par = pars[par_offset];
142 tmp.setInsetOwner(&par.inInset());
143 tmp.makeSameLayout(par);
145 LASSERT(pos <= par.size(), /**/);
147 if (pos < par.size()) {
148 // move everything behind the break position to the new paragraph
149 pos_type pos_end = par.size() - 1;
151 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
152 if (moveItem(par, pos, tmp, j, bparams)) {
156 // Move over the end-of-par change information
157 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
158 par.setChange(par.size(), Change(bparams.trackChanges ?
159 Change::INSERTED : Change::UNCHANGED));
164 void mergeParagraph(BufferParams const & bparams,
165 ParagraphList & pars, pit_type par_offset)
167 Paragraph & next = pars[par_offset + 1];
168 Paragraph & par = pars[par_offset];
170 pos_type pos_end = next.size() - 1;
171 pos_type pos_insert = par.size();
173 // the imaginary end-of-paragraph character (at par.size()) has to be
174 // marked as unmodified. Otherwise, its change is adopted by the first
175 // character of the next paragraph.
176 if (par.isChanged(par.size())) {
177 LYXERR(Debug::CHANGES,
178 "merging par with inserted/deleted end-of-par character");
179 par.setChange(par.size(), Change(Change::UNCHANGED));
182 Change change = next.lookupChange(next.size());
184 // move the content of the second paragraph to the end of the first one
185 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
186 if (moveItem(next, 0, par, j, bparams)) {
191 // move the change of the end-of-paragraph character
192 par.setChange(par.size(), change);
194 pars.erase(boost::next(pars.begin(), par_offset + 1));
198 Text::Text(InsetText * owner, bool use_default_layout)
199 : owner_(owner), autoBreakRows_(false), undo_counter_(0)
201 pars_.push_back(Paragraph());
202 Paragraph & par = pars_.back();
203 par.setInsetOwner(owner);
204 DocumentClass const & dc = owner->buffer().params().documentClass();
205 if (use_default_layout)
206 par.setDefaultLayout(dc);
208 par.setPlainLayout(dc);
212 Text::Text(InsetText * owner, Text const & text)
213 : owner_(owner), autoBreakRows_(text.autoBreakRows_), undo_counter_(0)
216 ParagraphList::iterator const end = pars_.end();
217 ParagraphList::iterator it = pars_.begin();
218 for (; it != end; ++it)
219 it->setInsetOwner(owner);
223 pit_type Text::depthHook(pit_type pit, depth_type depth) const
225 pit_type newpit = pit;
230 while (newpit != 0 && pars_[newpit].getDepth() > depth)
233 if (pars_[newpit].getDepth() > depth)
240 pit_type Text::outerHook(pit_type par_offset) const
242 Paragraph const & par = pars_[par_offset];
244 if (par.getDepth() == 0)
246 return depthHook(par_offset, depth_type(par.getDepth() - 1));
250 bool Text::isFirstInSequence(pit_type par_offset) const
252 Paragraph const & par = pars_[par_offset];
254 pit_type dhook_offset = depthHook(par_offset, par.getDepth());
256 if (dhook_offset == par_offset)
259 Paragraph const & dhook = pars_[dhook_offset];
261 return dhook.layout() != par.layout()
262 || dhook.getDepth() != par.getDepth();
266 Font const Text::outerFont(pit_type par_offset) const
268 depth_type par_depth = pars_[par_offset].getDepth();
269 FontInfo tmpfont = inherit_font;
270 depth_type prev_par_depth = 0;
271 // Resolve against environment font information
272 while (par_offset != pit_type(pars_.size())
273 && par_depth != prev_par_depth
275 && !tmpfont.resolved()) {
276 prev_par_depth = par_depth;
277 par_offset = outerHook(par_offset);
278 if (par_offset != pit_type(pars_.size())) {
279 tmpfont.realize(pars_[par_offset].layout().font);
280 par_depth = pars_[par_offset].getDepth();
284 return Font(tmpfont);
288 static void acceptOrRejectChanges(ParagraphList & pars,
289 BufferParams const & bparams, Text::ChangeOp op)
291 pit_type pars_size = static_cast<pit_type>(pars.size());
293 // first, accept or reject changes within each individual
294 // paragraph (do not consider end-of-par)
295 for (pit_type pit = 0; pit < pars_size; ++pit) {
296 // prevent assertion failure
297 if (!pars[pit].empty()) {
298 if (op == Text::ACCEPT)
299 pars[pit].acceptChanges(0, pars[pit].size());
301 pars[pit].rejectChanges(0, pars[pit].size());
305 // next, accept or reject imaginary end-of-par characters
306 for (pit_type pit = 0; pit < pars_size; ++pit) {
307 pos_type pos = pars[pit].size();
308 if (pars[pit].isChanged(pos)) {
309 // keep the end-of-par char if it is inserted and accepted
310 // or when it is deleted and rejected.
311 if (pars[pit].isInserted(pos) == (op == Text::ACCEPT)) {
312 pars[pit].setChange(pos, Change(Change::UNCHANGED));
314 if (pit == pars_size - 1) {
315 // we cannot remove a par break at the end of the last
316 // paragraph; instead, we mark it unchanged
317 pars[pit].setChange(pos, Change(Change::UNCHANGED));
319 mergeParagraph(bparams, pars, pit);
329 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
331 acceptOrRejectChanges(pars, bparams, Text::ACCEPT);
335 void rejectChanges(ParagraphList & pars, BufferParams const & bparams)
337 acceptOrRejectChanges(pars, bparams, Text::REJECT);
341 InsetText const & Text::inset() const
348 void Text::readParToken(Paragraph & par, Lexer & lex,
349 string const & token, Font & font, Change & change, ErrorList & errorList)
351 Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
352 BufferParams const & bp = buf->params();
354 if (token[0] != '\\') {
355 docstring dstr = lex.getDocString();
356 par.appendString(dstr, font, change);
358 } else if (token == "\\begin_layout") {
360 docstring layoutname = lex.getDocString();
362 font = Font(inherit_font, bp.language);
363 change = Change(Change::UNCHANGED);
365 DocumentClass const & tclass = bp.documentClass();
367 if (layoutname.empty())
368 layoutname = tclass.defaultLayoutName();
370 if (owner_->forcePlainLayout()) {
371 // in this case only the empty layout is allowed
372 layoutname = tclass.plainLayoutName();
373 } else if (par.usePlainLayout()) {
374 // in this case, default layout maps to empty layout
375 if (layoutname == tclass.defaultLayoutName())
376 layoutname = tclass.plainLayoutName();
378 // otherwise, the empty layout maps to the default
379 if (layoutname == tclass.plainLayoutName())
380 layoutname = tclass.defaultLayoutName();
383 // When we apply an unknown layout to a document, we add this layout to the textclass
384 // of this document. For example, when you apply class article to a beamer document,
385 // all unknown layouts such as frame will be added to document class article so that
386 // these layouts can keep their original names.
387 bool const added_one = tclass.addLayoutIfNeeded(layoutname);
390 docstring const s = bformat(_("Layout `%1$s' was not found."), layoutname);
392 ErrorItem(_("Layout Not Found"), s, par.id(), 0, par.size()));
395 par.setLayout(bp.documentClass()[layoutname]);
397 // Test whether the layout is obsolete.
398 Layout const & layout = par.layout();
399 if (!layout.obsoleted_by().empty())
400 par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
402 par.params().read(lex);
404 } else if (token == "\\end_layout") {
405 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
406 << "Missing \\begin_layout ?");
407 } else if (token == "\\end_inset") {
408 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
409 << "Missing \\begin_inset ?");
410 } else if (token == "\\begin_inset") {
411 Inset * inset = readInset(lex, buf);
413 par.insertInset(par.size(), inset, font, change);
416 docstring line = lex.getDocString();
417 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
418 par.id(), 0, par.size()));
420 } else if (token == "\\family") {
422 setLyXFamily(lex.getString(), font.fontInfo());
423 } else if (token == "\\series") {
425 setLyXSeries(lex.getString(), font.fontInfo());
426 } else if (token == "\\shape") {
428 setLyXShape(lex.getString(), font.fontInfo());
429 } else if (token == "\\size") {
431 setLyXSize(lex.getString(), font.fontInfo());
432 } else if (token == "\\lang") {
434 string const tok = lex.getString();
435 Language const * lang = languages.getLanguage(tok);
437 font.setLanguage(lang);
439 font.setLanguage(bp.language);
440 lex.printError("Unknown language `$$Token'");
442 } else if (token == "\\numeric") {
444 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
445 } else if (token == "\\emph") {
447 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
448 } else if (token == "\\bar") {
450 string const tok = lex.getString();
453 font.fontInfo().setUnderbar(FONT_ON);
454 else if (tok == "no")
455 font.fontInfo().setUnderbar(FONT_OFF);
456 else if (tok == "default")
457 font.fontInfo().setUnderbar(FONT_INHERIT);
459 lex.printError("Unknown bar font flag "
461 } else if (token == "\\strikeout") {
463 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
464 } else if (token == "\\uuline") {
466 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
467 } else if (token == "\\uwave") {
469 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
470 } else if (token == "\\noun") {
472 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
473 } else if (token == "\\color") {
475 setLyXColor(lex.getString(), font.fontInfo());
476 } else if (token == "\\SpecialChar") {
477 auto_ptr<Inset> inset;
478 inset.reset(new InsetSpecialChar);
480 inset->setBuffer(*buf);
481 par.insertInset(par.size(), inset.release(), font, change);
482 } else if (token == "\\backslash") {
483 par.appendChar('\\', font, change);
484 } else if (token == "\\LyXTable") {
485 auto_ptr<Inset> inset(new InsetTabular(buf));
487 par.insertInset(par.size(), inset.release(), font, change);
488 } else if (token == "\\change_unchanged") {
489 change = Change(Change::UNCHANGED);
490 } else if (token == "\\change_inserted" || token == "\\change_deleted") {
492 istringstream is(lex.getString());
496 BufferParams::AuthorMap const & am = bp.author_map;
497 if (am.find(aid) == am.end()) {
498 errorList.push_back(ErrorItem(_("Change tracking error"),
499 bformat(_("Unknown author index for change: %1$d\n"), aid),
500 par.id(), 0, par.size()));
501 change = Change(Change::UNCHANGED);
503 if (token == "\\change_inserted")
504 change = Change(Change::INSERTED, am.find(aid)->second, ct);
506 change = Change(Change::DELETED, am.find(aid)->second, ct);
510 errorList.push_back(ErrorItem(_("Unknown token"),
511 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
513 par.id(), 0, par.size()));
518 void Text::readParagraph(Paragraph & par, Lexer & lex,
519 ErrorList & errorList)
522 string token = lex.getString();
524 Change change(Change::UNCHANGED);
527 readParToken(par, lex, token, font, change, errorList);
530 token = lex.getString();
535 if (token == "\\end_layout") {
536 //Ok, paragraph finished
540 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
541 if (token == "\\begin_layout" || token == "\\end_document"
542 || token == "\\end_inset" || token == "\\begin_deeper"
543 || token == "\\end_deeper") {
544 lex.pushToken(token);
545 lyxerr << "Paragraph ended in line "
546 << lex.lineNumber() << "\n"
547 << "Missing \\end_layout.\n";
551 // Final change goes to paragraph break:
552 par.setChange(par.size(), change);
554 // Initialize begin_of_body_ on load; redoParagraph maintains
555 par.setBeginOfBody();
557 // mark paragraph for spell checking on load
558 // par.requestSpellCheck();
562 class TextCompletionList : public CompletionList
566 TextCompletionList(Cursor const & cur, WordList const * list)
567 : buffer_(cur.buffer()), pos_(0), list_(list)
570 virtual ~TextCompletionList() {}
573 virtual bool sorted() const { return true; }
575 virtual size_t size() const
577 return list_->size();
580 virtual docstring const & data(size_t idx) const
582 return list_->word(idx);
587 Buffer const * buffer_;
591 WordList const * list_;
595 bool Text::empty() const
597 return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
598 // FIXME: Should we consider the labeled type as empty too?
599 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
603 double Text::spacing(Paragraph const & par) const
605 if (par.params().spacing().isDefault())
606 return owner_->buffer().params().spacing().getValue();
607 return par.params().spacing().getValue();
612 * This breaks a paragraph at the specified position.
613 * The new paragraph will:
614 * - Decrease depth by one (or change layout to default layout) when
615 * keep_layout == false
616 * - keep current depth and layout when keep_layout == true
618 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
621 BufferParams const & bparams = text.inset().buffer().params();
622 ParagraphList & pars = text.paragraphs();
623 // create a new paragraph, and insert into the list
624 ParagraphList::iterator tmp =
625 pars.insert(boost::next(pars.begin(), par_offset + 1),
628 Paragraph & par = pars[par_offset];
630 // remember to set the inset_owner
631 tmp->setInsetOwner(&par.inInset());
632 // without doing that we get a crash when typing <Return> at the
633 // end of a paragraph
634 tmp->setPlainOrDefaultLayout(bparams.documentClass());
637 tmp->setLayout(par.layout());
638 tmp->setLabelWidthString(par.params().labelWidthString());
639 tmp->params().depth(par.params().depth());
640 } else if (par.params().depth() > 0) {
641 Paragraph const & hook = pars[text.outerHook(par_offset)];
642 tmp->setLayout(hook.layout());
643 // not sure the line below is useful
644 tmp->setLabelWidthString(par.params().labelWidthString());
645 tmp->params().depth(hook.params().depth());
648 bool const isempty = (par.allowEmpty() && par.empty());
650 if (!isempty && (par.size() > pos || par.empty())) {
651 tmp->setLayout(par.layout());
652 tmp->params().align(par.params().align());
653 tmp->setLabelWidthString(par.params().labelWidthString());
655 tmp->params().depth(par.params().depth());
656 tmp->params().noindent(par.params().noindent());
658 // move everything behind the break position
659 // to the new paragraph
661 /* Note: if !keepempty, empty() == true, then we reach
662 * here with size() == 0. So pos_end becomes - 1. This
663 * doesn't cause problems because both loops below
664 * enforce pos <= pos_end and 0 <= pos
666 pos_type pos_end = par.size() - 1;
668 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
669 if (moveItem(par, pos, *tmp, j, bparams)) {
675 // Move over the end-of-par change information
676 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
677 par.setChange(par.size(), Change(bparams.trackChanges ?
678 Change::INSERTED : Change::UNCHANGED));
681 // Make sure that we keep the language when
682 // breaking paragraph.
684 Font changed = tmp->getFirstFontSettings(bparams);
685 Font const & old = par.getFontSettings(bparams, par.size());
686 changed.setLanguage(old.language());
687 tmp->setFont(0, changed);
694 bool const soa = par.params().startOfAppendix();
695 par.params().clear();
696 // do not lose start of appendix marker (bug 4212)
697 par.params().startOfAppendix(soa);
698 par.setPlainOrDefaultLayout(bparams.documentClass());
702 par.setLayout(tmp->layout());
703 par.setLabelWidthString(tmp->params().labelWidthString());
704 par.params().depth(tmp->params().depth());
709 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
711 LASSERT(this == cur.text(), /**/);
713 Paragraph & cpar = cur.paragraph();
714 pit_type cpit = cur.pit();
716 DocumentClass const & tclass = cur.buffer()->params().documentClass();
717 Layout const & layout = cpar.layout();
719 if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
720 if (changeDepthAllowed(cur, DEC_DEPTH))
721 changeDepth(cur, DEC_DEPTH);
723 setLayout(cur, tclass.defaultLayoutName());
727 // a layout change may affect also the following paragraph
728 recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
730 // Always break behind a space
731 // It is better to erase the space (Dekel)
732 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
733 cpar.eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
735 // What should the layout for the new paragraph be?
736 bool keep_layout = layout.isEnvironment()
737 || (layout.isParagraph() && layout.parbreak_is_newline);
739 keep_layout = !keep_layout;
741 // We need to remember this before we break the paragraph, because
742 // that invalidates the layout variable
743 bool sensitive = layout.labeltype == LABEL_SENSITIVE;
745 // we need to set this before we insert the paragraph.
746 bool const isempty = cpar.allowEmpty() && cpar.empty();
748 lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
750 // After this, neither paragraph contains any rows!
753 pit_type next_par = cpit + 1;
755 // well this is the caption hack since one caption is really enough
758 // set to standard-layout
759 //FIXME Check if this should be plainLayout() in some cases
760 pars_[cpit].applyLayout(tclass.defaultLayout());
762 // set to standard-layout
763 //FIXME Check if this should be plainLayout() in some cases
764 pars_[next_par].applyLayout(tclass.defaultLayout());
767 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
768 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().trackChanges))
769 break; // the character couldn't be deleted physically due to change tracking
772 // A singlePar update is not enough in this case.
773 cur.screenUpdateFlags(Update::Force);
774 cur.forceBufferUpdate();
776 // This check is necessary. Otherwise the new empty paragraph will
777 // be deleted automatically. And it is more friendly for the user!
778 if (cur.pos() != 0 || isempty)
779 setCursor(cur, cur.pit() + 1, 0);
781 setCursor(cur, cur.pit(), 0);
785 // needed to insert the selection
786 void Text::insertStringAsLines(Cursor & cur, docstring const & str,
789 BufferParams const & bparams = owner_->buffer().params();
790 pit_type pit = cur.pit();
791 pos_type pos = cur.pos();
793 // insert the string, don't insert doublespace
794 bool space_inserted = true;
795 for (docstring::const_iterator cit = str.begin();
796 cit != str.end(); ++cit) {
797 Paragraph & par = pars_[pit];
799 if (autoBreakRows_ && (!par.empty() || par.allowEmpty())) {
800 lyx::breakParagraph(*this, pit, pos,
801 par.layout().isEnvironment());
804 space_inserted = true;
808 // do not insert consecutive spaces if !free_spacing
809 } else if ((*cit == ' ' || *cit == '\t') &&
810 space_inserted && !par.isFreeSpacing()) {
812 } else if (*cit == '\t') {
813 if (!par.isFreeSpacing()) {
814 // tabs are like spaces here
815 par.insertChar(pos, ' ', font, bparams.trackChanges);
817 space_inserted = true;
819 par.insertChar(pos, *cit, font, bparams.trackChanges);
821 space_inserted = true;
823 } else if (!isPrintable(*cit)) {
824 // Ignore unprintables
827 // just insert the character
828 par.insertChar(pos, *cit, font, bparams.trackChanges);
830 space_inserted = (*cit == ' ');
833 setCursor(cur, pit, pos);
837 // turn double CR to single CR, others are converted into one
838 // blank. Then insertStringAsLines is called
839 void Text::insertStringAsParagraphs(Cursor & cur, docstring const & str,
842 docstring linestr = str;
843 bool newline_inserted = false;
845 for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
846 if (linestr[i] == '\n') {
847 if (newline_inserted) {
848 // we know that \r will be ignored by
849 // insertStringAsLines. Of course, it is a dirty
850 // trick, but it works...
851 linestr[i - 1] = '\r';
855 newline_inserted = true;
857 } else if (isPrintable(linestr[i])) {
858 newline_inserted = false;
861 insertStringAsLines(cur, linestr, font);
865 // insert a character, moves all the following breaks in the
866 // same Paragraph one to the right and make a rebreak
867 void Text::insertChar(Cursor & cur, char_type c)
869 LASSERT(this == cur.text(), /**/);
871 cur.recordUndo(INSERT_UNDO);
873 TextMetrics const & tm = cur.bv().textMetrics(this);
874 Buffer const & buffer = *cur.buffer();
875 Paragraph & par = cur.paragraph();
876 // try to remove this
877 pit_type const pit = cur.pit();
879 bool const freeSpacing = par.layout().free_spacing ||
882 if (lyxrc.auto_number) {
883 static docstring const number_operators = from_ascii("+-/*");
884 static docstring const number_unary_operators = from_ascii("+-");
885 static docstring const number_seperators = from_ascii(".,:");
887 if (cur.current_font.fontInfo().number() == FONT_ON) {
888 if (!isDigitASCII(c) && !contains(number_operators, c) &&
889 !(contains(number_seperators, c) &&
891 cur.pos() != cur.lastpos() &&
892 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
893 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
895 number(cur); // Set current_font.number to OFF
896 } else if (isDigitASCII(c) &&
897 cur.real_current_font.isVisibleRightToLeft()) {
898 number(cur); // Set current_font.number to ON
900 if (cur.pos() != 0) {
901 char_type const c = par.getChar(cur.pos() - 1);
902 if (contains(number_unary_operators, c) &&
904 || par.isSeparator(cur.pos() - 2)
905 || par.isNewline(cur.pos() - 2))
907 setCharFont(pit, cur.pos() - 1, cur.current_font,
909 } else if (contains(number_seperators, c)
911 && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
912 setCharFont(pit, cur.pos() - 1, cur.current_font,
919 // In Bidi text, we want spaces to be treated in a special way: spaces
920 // which are between words in different languages should get the
921 // paragraph's language; otherwise, spaces should keep the language
922 // they were originally typed in. This is only in effect while typing;
923 // after the text is already typed in, the user can always go back and
924 // explicitly set the language of a space as desired. But 99.9% of the
925 // time, what we're doing here is what the user actually meant.
927 // The following cases are the ones in which the language of the space
928 // should be changed to match that of the containing paragraph. In the
929 // depictions, lowercase is LTR, uppercase is RTL, underscore (_)
930 // represents a space, pipe (|) represents the cursor position (so the
931 // character before it is the one just typed in). The different cases
932 // are depicted logically (not visually), from left to right:
937 // Theoretically, there are other situations that we should, perhaps, deal
938 // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any
939 // point (to understand why, just try to create this situation...).
941 if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
942 // get font in front and behind the space in question. But do NOT
943 // use getFont(cur.pos()) because the character c is not inserted yet
944 Font const pre_space_font = tm.displayFont(cur.pit(), cur.pos() - 2);
945 Font const & post_space_font = cur.real_current_font;
946 bool pre_space_rtl = pre_space_font.isVisibleRightToLeft();
947 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
949 if (pre_space_rtl != post_space_rtl) {
950 // Set the space's language to match the language of the
951 // adjacent character whose direction is the paragraph's
952 // direction; don't touch other properties of the font
953 Language const * lang =
954 (pre_space_rtl == par.isRTL(buffer.params())) ?
955 pre_space_font.language() : post_space_font.language();
957 Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
958 space_font.setLanguage(lang);
959 par.setFont(cur.pos() - 1, space_font);
963 // Next check, if there will be two blanks together or a blank at
964 // the beginning of a paragraph.
965 // I decided to handle blanks like normal characters, the main
966 // difference are the special checks when calculating the row.fill
967 // (blank does not count at the end of a row) and the check here
969 // When the free-spacing option is set for the current layout,
970 // disable the double-space checking
971 if (!freeSpacing && isLineSeparatorChar(c)) {
972 if (cur.pos() == 0) {
974 "You cannot insert a space at the "
975 "beginning of a paragraph. Please read the Tutorial."));
978 LASSERT(cur.pos() > 0, /**/);
979 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
980 && !par.isDeleted(cur.pos() - 1)) {
982 "You cannot type two spaces this way. "
983 "Please read the Tutorial."));
988 par.insertChar(cur.pos(), c, cur.current_font,
989 cur.buffer()->params().trackChanges);
990 cur.checkBufferStructure();
992 // cur.screenUpdateFlags(Update::Force);
993 bool boundary = cur.boundary()
994 || tm.isRTLBoundary(cur.pit(), cur.pos() + 1);
995 setCursor(cur, cur.pit(), cur.pos() + 1, false, boundary);
1000 void Text::charInserted(Cursor & cur)
1002 Paragraph & par = cur.paragraph();
1004 // Here we call finishUndo for every 20 characters inserted.
1005 // This is from my experience how emacs does it. (Lgb)
1006 if (undo_counter_ < 20) {
1013 // register word if a non-letter was entered
1015 && !par.isWordSeparator(cur.pos() - 2)
1016 && par.isWordSeparator(cur.pos() - 1)) {
1017 // get the word in front of cursor
1018 LASSERT(this == cur.text(), /**/);
1019 cur.paragraph().updateWords();
1024 // the cursor set functions have a special mechanism. When they
1025 // realize, that you left an empty paragraph, they will delete it.
1027 bool Text::cursorForwardOneWord(Cursor & cur)
1029 LASSERT(this == cur.text(), /**/);
1031 pos_type const lastpos = cur.lastpos();
1032 pit_type pit = cur.pit();
1033 pos_type pos = cur.pos();
1034 Paragraph const & par = cur.paragraph();
1036 // Paragraph boundary is a word boundary
1037 if (pos == lastpos) {
1038 if (pit != cur.lastpit())
1039 return setCursor(cur, pit + 1, 0);
1044 if (lyxrc.mac_like_word_movement) {
1045 // Skip through trailing punctuation and spaces.
1046 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
1049 // Skip over either a non-char inset or a full word
1050 if (pos != lastpos && par.isWordSeparator(pos))
1052 else while (pos != lastpos && !par.isWordSeparator(pos))
1055 LASSERT(pos < lastpos, /**/); // see above
1056 if (!par.isWordSeparator(pos))
1057 while (pos != lastpos && !par.isWordSeparator(pos))
1059 else if (par.isChar(pos))
1060 while (pos != lastpos && par.isChar(pos))
1062 else if (!par.isSpace(pos)) // non-char inset
1065 // Skip over white space
1066 while (pos != lastpos && par.isSpace(pos))
1070 return setCursor(cur, pit, pos);
1074 bool Text::cursorBackwardOneWord(Cursor & cur)
1076 LASSERT(this == cur.text(), /**/);
1078 pit_type pit = cur.pit();
1079 pos_type pos = cur.pos();
1080 Paragraph & par = cur.paragraph();
1082 // Paragraph boundary is a word boundary
1083 if (pos == 0 && pit != 0)
1084 return setCursor(cur, pit - 1, getPar(pit - 1).size());
1086 if (lyxrc.mac_like_word_movement) {
1087 // Skip through punctuation and spaces.
1088 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1091 // Skip over either a non-char inset or a full word
1092 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1094 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1097 // Skip over white space
1098 while (pos != 0 && par.isSpace(pos - 1))
1101 if (pos != 0 && !par.isWordSeparator(pos - 1))
1102 while (pos != 0 && !par.isWordSeparator(pos - 1))
1104 else if (pos != 0 && par.isChar(pos - 1))
1105 while (pos != 0 && par.isChar(pos - 1))
1107 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1111 return setCursor(cur, pit, pos);
1115 bool Text::cursorVisLeftOneWord(Cursor & cur)
1117 LASSERT(this == cur.text(), /**/);
1119 pos_type left_pos, right_pos;
1120 bool left_is_letter, right_is_letter;
1122 Cursor temp_cur = cur;
1124 // always try to move at least once...
1125 while (temp_cur.posVisLeft(true /* skip_inset */)) {
1127 // collect some information about current cursor position
1128 temp_cur.getSurroundingPos(left_pos, right_pos);
1130 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1132 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1134 // if we're not at a letter/non-letter boundary, continue moving
1135 if (left_is_letter == right_is_letter)
1138 // we should stop when we have an LTR word on our right or an RTL word
1140 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1141 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1142 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1143 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1147 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1148 true, temp_cur.boundary());
1152 bool Text::cursorVisRightOneWord(Cursor & cur)
1154 LASSERT(this == cur.text(), /**/);
1156 pos_type left_pos, right_pos;
1157 bool left_is_letter, right_is_letter;
1159 Cursor temp_cur = cur;
1161 // always try to move at least once...
1162 while (temp_cur.posVisRight(true /* skip_inset */)) {
1164 // collect some information about current cursor position
1165 temp_cur.getSurroundingPos(left_pos, right_pos);
1167 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1169 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1171 // if we're not at a letter/non-letter boundary, continue moving
1172 if (left_is_letter == right_is_letter)
1175 // we should stop when we have an LTR word on our right or an RTL word
1177 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1178 temp_cur.buffer()->params(),
1179 left_pos).isRightToLeft())
1180 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1181 temp_cur.buffer()->params(),
1182 right_pos).isRightToLeft()))
1186 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1187 true, temp_cur.boundary());
1191 void Text::selectWord(Cursor & cur, word_location loc)
1193 LASSERT(this == cur.text(), /**/);
1194 CursorSlice from = cur.top();
1195 CursorSlice to = cur.top();
1196 getWord(from, to, loc);
1197 if (cur.top() != from)
1198 setCursor(cur, from.pit(), from.pos());
1201 if (!cur.selection())
1203 setCursor(cur, to.pit(), to.pos());
1205 cur.setWordSelection(true);
1209 void Text::selectAll(Cursor & cur)
1211 LASSERT(this == cur.text(), /**/);
1212 if (cur.lastpos() == 0 && cur.lastpit() == 0)
1214 // If the cursor is at the beginning, make sure the cursor ends there
1215 if (cur.pit() == 0 && cur.pos() == 0) {
1216 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1218 setCursor(cur, 0, 0);
1220 setCursor(cur, 0, 0);
1222 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1228 // Select the word currently under the cursor when no
1229 // selection is currently set
1230 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1232 LASSERT(this == cur.text(), /**/);
1233 if (cur.selection())
1235 selectWord(cur, loc);
1236 return cur.selection();
1240 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1242 LASSERT(this == cur.text(), /**/);
1244 if (!cur.selection()) {
1245 bool const changed = cur.paragraph().isChanged(cur.pos());
1246 if (!(changed && findNextChange(&cur.bv())))
1250 cur.recordUndoSelection();
1252 pit_type begPit = cur.selectionBegin().pit();
1253 pit_type endPit = cur.selectionEnd().pit();
1255 pos_type begPos = cur.selectionBegin().pos();
1256 pos_type endPos = cur.selectionEnd().pos();
1258 // keep selection info, because endPos becomes invalid after the first loop
1259 bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1261 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1263 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1264 pos_type parSize = pars_[pit].size();
1266 // ignore empty paragraphs; otherwise, an assertion will fail for
1267 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1271 // do not consider first paragraph if the cursor starts at pos size()
1272 if (pit == begPit && begPos == parSize)
1275 // do not consider last paragraph if the cursor ends at pos 0
1276 if (pit == endPit && endPos == 0)
1277 break; // last iteration anyway
1279 pos_type left = (pit == begPit ? begPos : 0);
1280 pos_type right = (pit == endPit ? endPos : parSize);
1283 // there is no change here
1287 pars_[pit].acceptChanges(left, right);
1289 pars_[pit].rejectChanges(left, right);
1293 // next, accept/reject imaginary end-of-par characters
1295 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1296 pos_type pos = pars_[pit].size();
1298 // skip if the selection ends before the end-of-par
1299 if (pit == endPit && endsBeforeEndOfPar)
1300 break; // last iteration anyway
1302 // skip if this is not the last paragraph of the document
1303 // note: the user should be able to accept/reject the par break of the last par!
1304 if (pit == endPit && pit + 1 != int(pars_.size()))
1305 break; // last iteration anway
1308 if (pars_[pit].isInserted(pos)) {
1309 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1310 } else if (pars_[pit].isDeleted(pos)) {
1311 if (pit + 1 == int(pars_.size())) {
1312 // we cannot remove a par break at the end of the last paragraph;
1313 // instead, we mark it unchanged
1314 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1316 mergeParagraph(cur.buffer()->params(), pars_, pit);
1322 if (pars_[pit].isDeleted(pos)) {
1323 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1324 } else if (pars_[pit].isInserted(pos)) {
1325 if (pit + 1 == int(pars_.size())) {
1326 // we mark the par break at the end of the last paragraph unchanged
1327 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1329 mergeParagraph(cur.buffer()->params(), pars_, pit);
1337 // finally, invoke the DEPM
1339 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().trackChanges);
1344 cur.clearSelection();
1345 setCursorIntern(cur, begPit, begPos);
1346 cur.screenUpdateFlags(Update::Force);
1347 cur.forceBufferUpdate();
1351 void Text::acceptChanges()
1353 BufferParams const & bparams = owner_->buffer().params();
1354 lyx::acceptChanges(pars_, bparams);
1355 deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.trackChanges);
1359 void Text::rejectChanges()
1361 BufferParams const & bparams = owner_->buffer().params();
1362 pit_type pars_size = static_cast<pit_type>(pars_.size());
1364 // first, reject changes within each individual paragraph
1365 // (do not consider end-of-par)
1366 for (pit_type pit = 0; pit < pars_size; ++pit) {
1367 if (!pars_[pit].empty()) // prevent assertion failure
1368 pars_[pit].rejectChanges(0, pars_[pit].size());
1371 // next, reject imaginary end-of-par characters
1372 for (pit_type pit = 0; pit < pars_size; ++pit) {
1373 pos_type pos = pars_[pit].size();
1375 if (pars_[pit].isDeleted(pos)) {
1376 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1377 } else if (pars_[pit].isInserted(pos)) {
1378 if (pit == pars_size - 1) {
1379 // we mark the par break at the end of the last
1380 // paragraph unchanged
1381 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1383 mergeParagraph(bparams, pars_, pit);
1390 // finally, invoke the DEPM
1391 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
1395 void Text::deleteWordForward(Cursor & cur)
1397 LASSERT(this == cur.text(), /**/);
1398 if (cur.lastpos() == 0)
1402 cur.setSelection(true);
1403 cursorForwardOneWord(cur);
1405 cutSelection(cur, true, false);
1406 cur.checkBufferStructure();
1411 void Text::deleteWordBackward(Cursor & cur)
1413 LASSERT(this == cur.text(), /**/);
1414 if (cur.lastpos() == 0)
1415 cursorBackward(cur);
1418 cur.setSelection(true);
1419 cursorBackwardOneWord(cur);
1421 cutSelection(cur, true, false);
1422 cur.checkBufferStructure();
1427 // Kill to end of line.
1428 void Text::changeCase(Cursor & cur, TextCase action)
1430 LASSERT(this == cur.text(), /**/);
1434 bool gotsel = false;
1435 if (cur.selection()) {
1436 from = cur.selBegin();
1441 getWord(from, to, PARTIAL_WORD);
1442 cursorForwardOneWord(cur);
1445 cur.recordUndoSelection();
1447 pit_type begPit = from.pit();
1448 pit_type endPit = to.pit();
1450 pos_type begPos = from.pos();
1451 pos_type endPos = to.pos();
1453 pos_type right = 0; // needed after the for loop
1455 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1456 Paragraph & par = pars_[pit];
1457 pos_type const pos = (pit == begPit ? begPos : 0);
1458 right = (pit == endPit ? endPos : par.size());
1459 par.changeCase(cur.buffer()->params(), pos, right, action);
1462 // the selection may have changed due to logically-only deleted chars
1464 setCursor(cur, begPit, begPos);
1466 setCursor(cur, endPit, right);
1469 setCursor(cur, endPit, right);
1471 cur.checkBufferStructure();
1475 bool Text::handleBibitems(Cursor & cur)
1477 if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1483 BufferParams const & bufparams = cur.buffer()->params();
1484 Paragraph const & par = cur.paragraph();
1485 Cursor prevcur = cur;
1486 if (cur.pit() > 0) {
1488 prevcur.pos() = prevcur.lastpos();
1490 Paragraph const & prevpar = prevcur.paragraph();
1492 // if a bibitem is deleted, merge with previous paragraph
1493 // if this is a bibliography item as well
1494 if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1495 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1496 mergeParagraph(bufparams, cur.text()->paragraphs(),
1498 cur.forceBufferUpdate();
1499 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1500 cur.screenUpdateFlags(Update::Force);
1504 // otherwise reset to default
1505 cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1510 bool Text::erase(Cursor & cur)
1512 LASSERT(this == cur.text(), return false);
1513 bool needsUpdate = false;
1514 Paragraph & par = cur.paragraph();
1516 if (cur.pos() != cur.lastpos()) {
1517 // this is the code for a normal delete, not pasting
1519 cur.recordUndo(DELETE_UNDO);
1520 bool const was_inset = cur.paragraph().isInset(cur.pos());
1521 if(!par.eraseChar(cur.pos(), cur.buffer()->params().trackChanges))
1522 // the character has been logically deleted only => skip it
1523 cur.top().forwardPos();
1526 cur.forceBufferUpdate();
1528 cur.checkBufferStructure();
1531 if (cur.pit() == cur.lastpit())
1532 return dissolveInset(cur);
1534 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1535 par.setChange(cur.pos(), Change(Change::DELETED));
1539 setCursorIntern(cur, cur.pit() + 1, 0);
1540 needsUpdate = backspacePos0(cur);
1544 needsUpdate |= handleBibitems(cur);
1547 // Make sure the cursor is correct. Is this really needed?
1548 // No, not really... at least not here!
1549 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1550 cur.checkBufferStructure();
1557 bool Text::backspacePos0(Cursor & cur)
1559 LASSERT(this == cur.text(), /**/);
1563 bool needsUpdate = false;
1565 BufferParams const & bufparams = cur.buffer()->params();
1566 DocumentClass const & tclass = bufparams.documentClass();
1567 ParagraphList & plist = cur.text()->paragraphs();
1568 Paragraph const & par = cur.paragraph();
1569 Cursor prevcur = cur;
1571 prevcur.pos() = prevcur.lastpos();
1572 Paragraph const & prevpar = prevcur.paragraph();
1574 // is it an empty paragraph?
1575 if (cur.lastpos() == 0
1576 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1577 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1578 plist.erase(boost::next(plist.begin(), cur.pit()));
1581 // is previous par empty?
1582 else if (prevcur.lastpos() == 0
1583 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1584 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1585 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1588 // Pasting is not allowed, if the paragraphs have different
1589 // layouts. I think it is a real bug of all other
1590 // word processors to allow it. It confuses the user.
1591 // Correction: Pasting is always allowed with standard-layout
1592 // or the empty layout.
1593 else if (par.layout() == prevpar.layout()
1594 || tclass.isDefaultLayout(par.layout())
1595 || tclass.isPlainLayout(par.layout())) {
1596 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1597 mergeParagraph(bufparams, plist, prevcur.pit());
1602 cur.forceBufferUpdate();
1603 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1610 bool Text::backspace(Cursor & cur)
1612 LASSERT(this == cur.text(), /**/);
1613 bool needsUpdate = false;
1614 if (cur.pos() == 0) {
1616 return dissolveInset(cur);
1618 Paragraph & prev_par = pars_[cur.pit() - 1];
1620 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1621 prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1622 setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1625 // The cursor is at the beginning of a paragraph, so
1626 // the backspace will collapse two paragraphs into one.
1627 needsUpdate = backspacePos0(cur);
1630 // this is the code for a normal backspace, not pasting
1632 cur.recordUndo(DELETE_UNDO);
1633 // We used to do cursorBackwardIntern() here, but it is
1634 // not a good idea since it triggers the auto-delete
1635 // mechanism. So we do a cursorBackwardIntern()-lite,
1636 // without the dreaded mechanism. (JMarc)
1637 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1638 false, cur.boundary());
1639 bool const was_inset = cur.paragraph().isInset(cur.pos());
1640 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
1642 cur.forceBufferUpdate();
1644 cur.checkBufferStructure();
1647 if (cur.pos() == cur.lastpos())
1648 cur.setCurrentFont();
1650 needsUpdate |= handleBibitems(cur);
1652 // A singlePar update is not enough in this case.
1653 // cur.screenUpdateFlags(Update::Force);
1654 setCursor(cur.top(), cur.pit(), cur.pos());
1660 bool Text::dissolveInset(Cursor & cur)
1662 LASSERT(this == cur.text(), return false);
1664 if (isMainText() || cur.inset().nargs() != 1)
1667 cur.recordUndoInset();
1669 cur.selHandle(false);
1671 pos_type spos = cur.pos();
1672 pit_type spit = cur.pit();
1673 ParagraphList plist;
1674 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1675 plist = paragraphs();
1677 // store cursor offset
1681 Buffer & b = *cur.buffer();
1682 cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1684 if (!plist.empty()) {
1686 // we clear the cache so that we won't get conflicts with labels
1687 // that get pasted into the buffer. we should update this before
1688 // its being empty matters. if not (i.e., if we encounter bugs),
1689 // then this should instead be:
1690 // cur.buffer().updateBuffer();
1691 // but we'll try the cheaper solution here.
1692 cur.buffer()->clearReferenceCache();
1694 // ERT paragraphs have the Language latex_language.
1695 // This is invalid outside of ERT, so we need to
1696 // change it to the buffer language.
1697 ParagraphList::iterator it = plist.begin();
1698 ParagraphList::iterator it_end = plist.end();
1699 for (; it != it_end; it++)
1700 it->changeLanguage(b.params(), latex_language, b.language());
1702 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1703 b.errorList("Paste"));
1705 cur.pit() = min(cur.lastpit(), spit);
1706 cur.pos() = min(cur.lastpos(), spos);
1709 cur.forceBufferUpdate();
1711 // Ensure the current language is set correctly (bug 6292)
1712 cur.text()->setCursor(cur, cur.pit(), cur.pos());
1713 cur.clearSelection();
1719 void Text::getWord(CursorSlice & from, CursorSlice & to,
1720 word_location const loc) const
1723 pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1727 void Text::write(ostream & os) const
1729 Buffer const & buf = owner_->buffer();
1730 ParagraphList::const_iterator pit = paragraphs().begin();
1731 ParagraphList::const_iterator end = paragraphs().end();
1733 for (; pit != end; ++pit)
1734 pit->write(os, buf.params(), dth);
1736 // Close begin_deeper
1737 for(; dth > 0; --dth)
1738 os << "\n\\end_deeper";
1742 bool Text::read(Lexer & lex,
1743 ErrorList & errorList, InsetText * insetPtr)
1745 Buffer const & buf = owner_->buffer();
1746 depth_type depth = 0;
1749 while (lex.isOK()) {
1751 string const token = lex.getString();
1756 if (token == "\\end_inset")
1759 if (token == "\\end_body")
1762 if (token == "\\begin_body")
1765 if (token == "\\end_document") {
1770 if (token == "\\begin_layout") {
1771 lex.pushToken(token);
1774 par.setInsetOwner(insetPtr);
1775 par.params().depth(depth);
1776 par.setFont(0, Font(inherit_font, buf.params().language));
1777 pars_.push_back(par);
1778 readParagraph(pars_.back(), lex, errorList);
1780 // register the words in the global word list
1781 pars_.back().updateWords();
1782 } else if (token == "\\begin_deeper") {
1784 } else if (token == "\\end_deeper") {
1786 lex.printError("\\end_deeper: " "depth is already null");
1790 LYXERR0("Handling unknown body token: `" << token << '\'');
1794 // avoid a crash on weird documents (bug 4859)
1795 if (pars_.empty()) {
1797 par.setInsetOwner(insetPtr);
1798 par.params().depth(depth);
1799 par.setFont(0, Font(inherit_font,
1800 buf.params().language));
1801 par.setPlainOrDefaultLayout(buf.params().documentClass());
1802 pars_.push_back(par);
1808 // Returns the current font and depth as a message.
1809 docstring Text::currentState(Cursor const & cur) const
1811 LASSERT(this == cur.text(), /**/);
1812 Buffer & buf = *cur.buffer();
1813 Paragraph const & par = cur.paragraph();
1814 odocstringstream os;
1816 if (buf.params().trackChanges)
1817 os << _("[Change Tracking] ");
1819 Change change = par.lookupChange(cur.pos());
1821 if (change.changed()) {
1822 Author const & a = buf.params().authors().get(change.author);
1823 os << _("Change: ") << a.name();
1824 if (!a.email().empty())
1825 os << " (" << a.email() << ")";
1826 // FIXME ctime is english, we should translate that
1827 os << _(" at ") << ctime(&change.changetime);
1831 // I think we should only show changes from the default
1833 // No, from the document font (MV)
1834 Font font = cur.real_current_font;
1835 font.fontInfo().reduce(buf.params().getFont().fontInfo());
1837 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1839 // The paragraph depth
1840 int depth = cur.paragraph().getDepth();
1842 os << bformat(_(", Depth: %1$d"), depth);
1844 // The paragraph spacing, but only if different from
1846 Spacing const & spacing = par.params().spacing();
1847 if (!spacing.isDefault()) {
1848 os << _(", Spacing: ");
1849 switch (spacing.getSpace()) {
1850 case Spacing::Single:
1853 case Spacing::Onehalf:
1856 case Spacing::Double:
1859 case Spacing::Other:
1860 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1862 case Spacing::Default:
1863 // should never happen, do nothing
1868 #ifdef DEVEL_VERSION
1869 os << _(", Inset: ") << &cur.inset();
1870 os << _(", Paragraph: ") << cur.pit();
1871 os << _(", Id: ") << par.id();
1872 os << _(", Position: ") << cur.pos();
1873 // FIXME: Why is the check for par.size() needed?
1874 // We are called with cur.pos() == par.size() quite often.
1875 if (!par.empty() && cur.pos() < par.size()) {
1876 // Force output of code point, not character
1877 size_t const c = par.getChar(cur.pos());
1878 os << _(", Char: 0x") << hex << c;
1880 os << _(", Boundary: ") << cur.boundary();
1881 // Row & row = cur.textRow();
1882 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1888 docstring Text::getPossibleLabel(Cursor const & cur) const
1890 pit_type pit = cur.pit();
1892 Layout const * layout = &(pars_[pit].layout());
1895 docstring par_text = pars_[pit].asString();
1897 // The return string of math matrices might contain linebreaks
1898 par_text = subst(par_text, '\n', '-');
1899 int const numwords = 3;
1900 for (int i = 0; i < numwords; ++i) {
1901 if (par_text.empty())
1904 par_text = split(par_text, head, ' ');
1905 // Is it legal to use spaces in labels ?
1911 // Make sure it isn't too long
1912 unsigned int const max_label_length = 32;
1913 if (text.size() > max_label_length)
1914 text.resize(max_label_length);
1916 // Will contain the label prefix.
1919 // For section, subsection, etc...
1920 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1921 Layout const * layout2 = &(pars_[pit - 1].layout());
1922 if (layout2->latextype != LATEX_PARAGRAPH) {
1927 if (layout->latextype != LATEX_PARAGRAPH)
1928 name = layout->refprefix;
1930 // For captions, we just take the caption type
1931 Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
1932 if (caption_inset) {
1933 string const & ftype = static_cast<InsetCaption *>(caption_inset)->type();
1934 FloatList const & fl = cur.buffer()->params().documentClass().floats();
1935 if (fl.typeExist(ftype)) {
1936 Floating const & flt = fl.getType(ftype);
1937 name = from_utf8(flt.refPrefix());
1940 name = from_utf8(ftype.substr(0,3));
1943 // If none of the above worked, see if the inset knows.
1945 InsetLayout const & il = cur.inset().getLayout();
1946 name = il.refprefix();
1950 text = name + ':' + text;
1956 docstring Text::asString(int options) const
1958 return asString(0, pars_.size(), options);
1962 docstring Text::asString(pit_type beg, pit_type end, int options) const
1964 size_t i = size_t(beg);
1965 docstring str = pars_[i].asString(options);
1966 for (++i; i != size_t(end); ++i) {
1968 str += pars_[i].asString(options);
1974 void Text::forToc(docstring & os, size_t maxlen, bool shorten) const
1977 maxlen = std::numeric_limits<std::size_t>::max();
1979 LASSERT(maxlen >= 8, maxlen = TOC_ENTRY_LENGTH);
1980 for (size_t i = 0; i != pars_.size() && os.length() < maxlen; ++i)
1981 pars_[i].forToc(os, maxlen);
1982 if (shorten && os.length() >= maxlen)
1983 os = os.substr(0, maxlen - 3) + from_ascii("...");
1987 void Text::charsTranspose(Cursor & cur)
1989 LASSERT(this == cur.text(), /**/);
1991 pos_type pos = cur.pos();
1993 // If cursor is at beginning or end of paragraph, do nothing.
1994 if (pos == cur.lastpos() || pos == 0)
1997 Paragraph & par = cur.paragraph();
1999 // Get the positions of the characters to be transposed.
2000 pos_type pos1 = pos - 1;
2001 pos_type pos2 = pos;
2003 // In change tracking mode, ignore deleted characters.
2004 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2006 if (pos2 == cur.lastpos())
2009 while (pos1 >= 0 && par.isDeleted(pos1))
2014 // Don't do anything if one of the "characters" is not regular text.
2015 if (par.isInset(pos1) || par.isInset(pos2))
2018 // Store the characters to be transposed (including font information).
2019 char_type const char1 = par.getChar(pos1);
2021 par.getFontSettings(cur.buffer()->params(), pos1);
2023 char_type const char2 = par.getChar(pos2);
2025 par.getFontSettings(cur.buffer()->params(), pos2);
2027 // And finally, we are ready to perform the transposition.
2028 // Track the changes if Change Tracking is enabled.
2029 bool const trackChanges = cur.buffer()->params().trackChanges;
2033 par.eraseChar(pos2, trackChanges);
2034 par.eraseChar(pos1, trackChanges);
2035 par.insertChar(pos1, char2, font2, trackChanges);
2036 par.insertChar(pos2, char1, font1, trackChanges);
2038 cur.checkBufferStructure();
2040 // After the transposition, move cursor to after the transposition.
2041 setCursor(cur, cur.pit(), pos2);
2046 DocIterator Text::macrocontextPosition() const
2048 return macrocontext_position_;
2052 void Text::setMacrocontextPosition(DocIterator const & pos)
2054 macrocontext_position_ = pos;
2058 docstring Text::previousWord(CursorSlice const & sl) const
2060 CursorSlice from = sl;
2061 CursorSlice to = sl;
2062 getWord(from, to, PREVIOUS_WORD);
2063 if (sl == from || to == from)
2066 Paragraph const & par = sl.paragraph();
2067 return par.asString(from.pos(), to.pos());
2071 bool Text::completionSupported(Cursor const & cur) const
2073 Paragraph const & par = cur.paragraph();
2074 return cur.pos() > 0
2075 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2076 && !par.isWordSeparator(cur.pos() - 1);
2080 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2082 WordList const * list = theWordList(*cur.getFont().language());
2083 return new TextCompletionList(cur, list);
2087 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2089 LASSERT(cur.bv().cursor() == cur, /**/);
2091 cur.bv().cursor() = cur;
2092 if (!(cur.result().screenUpdate() & Update::Force))
2093 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2098 docstring Text::completionPrefix(Cursor const & cur) const
2100 return previousWord(cur.top());