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>
74 // TODO: replace if in Text::readParToken() with compile time switch
77 #include "support/metahash.h"
79 typedef boost::mpl::string<'\\end','_lay','out'> end_layout;
80 typedef boost::mpl::string<'\\end','in','set'> end_inset;
84 std::string token = "\\end_layout";
86 switch (boost::hash_value(token)) {
87 case lyx::support::hash_string<end_layout>::value:
89 case lyx::support::hash_string<end_inset>::value:
99 using namespace lyx::support;
103 using cap::cutSelection;
104 using cap::pasteParagraphList;
106 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
107 Paragraph & toPar, pos_type toPos, BufferParams const & params)
109 // Note: moveItem() does not honour change tracking!
110 // Therefore, it should only be used for breaking and merging paragraphs
112 // We need a copy here because the character at fromPos is going to be erased.
113 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
114 Change const tmpChange = fromPar.lookupChange(fromPos);
116 if (Inset * tmpInset = fromPar.getInset(fromPos)) {
117 fromPar.releaseInset(fromPos);
118 // The inset is not in fromPar any more.
119 if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
126 char_type const tmpChar = fromPar.getChar(fromPos);
127 fromPar.eraseChar(fromPos, false);
128 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
133 void breakParagraphConservative(BufferParams const & bparams,
134 ParagraphList & pars, pit_type par_offset, pos_type pos)
136 // create a new paragraph
137 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
139 Paragraph & par = pars[par_offset];
141 tmp.setInsetOwner(&par.inInset());
142 tmp.makeSameLayout(par);
144 LASSERT(pos <= par.size(), /**/);
146 if (pos < par.size()) {
147 // move everything behind the break position to the new paragraph
148 pos_type pos_end = par.size() - 1;
150 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
151 if (moveItem(par, pos, tmp, j, bparams)) {
155 // Move over the end-of-par change information
156 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
157 par.setChange(par.size(), Change(bparams.trackChanges ?
158 Change::INSERTED : Change::UNCHANGED));
163 void mergeParagraph(BufferParams const & bparams,
164 ParagraphList & pars, pit_type par_offset)
166 Paragraph & next = pars[par_offset + 1];
167 Paragraph & par = pars[par_offset];
169 pos_type pos_end = next.size() - 1;
170 pos_type pos_insert = par.size();
172 // the imaginary end-of-paragraph character (at par.size()) has to be
173 // marked as unmodified. Otherwise, its change is adopted by the first
174 // character of the next paragraph.
175 if (par.isChanged(par.size())) {
176 LYXERR(Debug::CHANGES,
177 "merging par with inserted/deleted end-of-par character");
178 par.setChange(par.size(), Change(Change::UNCHANGED));
181 Change change = next.lookupChange(next.size());
183 // move the content of the second paragraph to the end of the first one
184 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
185 if (moveItem(next, 0, par, j, bparams)) {
190 // move the change of the end-of-paragraph character
191 par.setChange(par.size(), change);
193 pars.erase(boost::next(pars.begin(), par_offset + 1));
197 Text::Text(InsetText * owner, bool use_default_layout)
198 : owner_(owner), autoBreakRows_(false), undo_counter_(0)
200 pars_.push_back(Paragraph());
201 Paragraph & par = pars_.back();
202 par.setInsetOwner(owner);
203 DocumentClass const & dc = owner->buffer().params().documentClass();
204 if (use_default_layout)
205 par.setDefaultLayout(dc);
207 par.setPlainLayout(dc);
211 Text::Text(InsetText * owner, Text const & text)
212 : owner_(owner), autoBreakRows_(text.autoBreakRows_), undo_counter_(0)
215 ParagraphList::iterator const end = pars_.end();
216 ParagraphList::iterator it = pars_.begin();
217 for (; it != end; ++it)
218 it->setInsetOwner(owner);
222 pit_type Text::depthHook(pit_type pit, depth_type depth) const
224 pit_type newpit = pit;
229 while (newpit != 0 && pars_[newpit].getDepth() > depth)
232 if (pars_[newpit].getDepth() > depth)
239 pit_type Text::outerHook(pit_type par_offset) const
241 Paragraph const & par = pars_[par_offset];
243 if (par.getDepth() == 0)
245 return depthHook(par_offset, depth_type(par.getDepth() - 1));
249 bool Text::isFirstInSequence(pit_type par_offset) const
251 Paragraph const & par = pars_[par_offset];
253 pit_type dhook_offset = depthHook(par_offset, par.getDepth());
255 if (dhook_offset == par_offset)
258 Paragraph const & dhook = pars_[dhook_offset];
260 return dhook.layout() != par.layout()
261 || dhook.getDepth() != par.getDepth();
265 Font const Text::outerFont(pit_type par_offset) const
267 depth_type par_depth = pars_[par_offset].getDepth();
268 FontInfo tmpfont = inherit_font;
270 // Resolve against environment font information
271 while (par_offset != pit_type(pars_.size())
273 && !tmpfont.resolved()) {
274 par_offset = outerHook(par_offset);
275 if (par_offset != pit_type(pars_.size())) {
276 tmpfont.realize(pars_[par_offset].layout().font);
277 par_depth = pars_[par_offset].getDepth();
281 return Font(tmpfont);
285 static void acceptOrRejectChanges(ParagraphList & pars,
286 BufferParams const & bparams, Text::ChangeOp op)
288 pit_type pars_size = static_cast<pit_type>(pars.size());
290 // first, accept or reject changes within each individual
291 // paragraph (do not consider end-of-par)
292 for (pit_type pit = 0; pit < pars_size; ++pit) {
293 // prevent assertion failure
294 if (!pars[pit].empty()) {
295 if (op == Text::ACCEPT)
296 pars[pit].acceptChanges(0, pars[pit].size());
298 pars[pit].rejectChanges(0, pars[pit].size());
302 // next, accept or reject imaginary end-of-par characters
303 for (pit_type pit = 0; pit < pars_size; ++pit) {
304 pos_type pos = pars[pit].size();
305 if (pars[pit].isChanged(pos)) {
306 // keep the end-of-par char if it is inserted and accepted
307 // or when it is deleted and rejected.
308 if (pars[pit].isInserted(pos) == (op == Text::ACCEPT)) {
309 pars[pit].setChange(pos, Change(Change::UNCHANGED));
311 if (pit == pars_size - 1) {
312 // we cannot remove a par break at the end of the last
313 // paragraph; instead, we mark it unchanged
314 pars[pit].setChange(pos, Change(Change::UNCHANGED));
316 mergeParagraph(bparams, pars, pit);
326 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
328 acceptOrRejectChanges(pars, bparams, Text::ACCEPT);
332 void rejectChanges(ParagraphList & pars, BufferParams const & bparams)
334 acceptOrRejectChanges(pars, bparams, Text::REJECT);
338 InsetText const & Text::inset() const
345 void Text::readParToken(Paragraph & par, Lexer & lex,
346 string const & token, Font & font, Change & change, ErrorList & errorList)
348 Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
349 BufferParams const & bp = buf->params();
351 if (token[0] != '\\') {
352 docstring dstr = lex.getDocString();
353 par.appendString(dstr, font, change);
355 } else if (token == "\\begin_layout") {
357 docstring layoutname = lex.getDocString();
359 font = Font(inherit_font, bp.language);
360 change = Change(Change::UNCHANGED);
362 DocumentClass const & tclass = bp.documentClass();
364 if (layoutname.empty())
365 layoutname = tclass.defaultLayoutName();
367 if (owner_->forcePlainLayout()) {
368 // in this case only the empty layout is allowed
369 layoutname = tclass.plainLayoutName();
370 } else if (par.usePlainLayout()) {
371 // in this case, default layout maps to empty layout
372 if (layoutname == tclass.defaultLayoutName())
373 layoutname = tclass.plainLayoutName();
375 // otherwise, the empty layout maps to the default
376 if (layoutname == tclass.plainLayoutName())
377 layoutname = tclass.defaultLayoutName();
380 // When we apply an unknown layout to a document, we add this layout to the textclass
381 // of this document. For example, when you apply class article to a beamer document,
382 // all unknown layouts such as frame will be added to document class article so that
383 // these layouts can keep their original names.
384 bool const added_one = tclass.addLayoutIfNeeded(layoutname);
387 docstring const s = bformat(_("Layout `%1$s' was not found."), layoutname);
389 ErrorItem(_("Layout Not Found"), s, par.id(), 0, par.size()));
392 par.setLayout(bp.documentClass()[layoutname]);
394 // Test whether the layout is obsolete.
395 Layout const & layout = par.layout();
396 if (!layout.obsoleted_by().empty())
397 par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
399 par.params().read(lex);
401 } else if (token == "\\end_layout") {
402 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
403 << "Missing \\begin_layout ?");
404 } else if (token == "\\end_inset") {
405 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
406 << "Missing \\begin_inset ?");
407 } else if (token == "\\begin_inset") {
408 Inset * inset = readInset(lex, buf);
410 par.insertInset(par.size(), inset, font, change);
413 docstring line = lex.getDocString();
414 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
415 par.id(), 0, par.size()));
417 } else if (token == "\\family") {
419 setLyXFamily(lex.getString(), font.fontInfo());
420 } else if (token == "\\series") {
422 setLyXSeries(lex.getString(), font.fontInfo());
423 } else if (token == "\\shape") {
425 setLyXShape(lex.getString(), font.fontInfo());
426 } else if (token == "\\size") {
428 setLyXSize(lex.getString(), font.fontInfo());
429 } else if (token == "\\lang") {
431 string const tok = lex.getString();
432 Language const * lang = languages.getLanguage(tok);
434 font.setLanguage(lang);
436 font.setLanguage(bp.language);
437 lex.printError("Unknown language `$$Token'");
439 } else if (token == "\\numeric") {
441 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
442 } else if (token == "\\emph") {
444 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
445 } else if (token == "\\bar") {
447 string const tok = lex.getString();
450 font.fontInfo().setUnderbar(FONT_ON);
451 else if (tok == "no")
452 font.fontInfo().setUnderbar(FONT_OFF);
453 else if (tok == "default")
454 font.fontInfo().setUnderbar(FONT_INHERIT);
456 lex.printError("Unknown bar font flag "
458 } else if (token == "\\strikeout") {
460 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
461 } else if (token == "\\uuline") {
463 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
464 } else if (token == "\\uwave") {
466 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
467 } else if (token == "\\noun") {
469 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
470 } else if (token == "\\color") {
472 setLyXColor(lex.getString(), font.fontInfo());
473 } else if (token == "\\SpecialChar") {
474 auto_ptr<Inset> inset;
475 inset.reset(new InsetSpecialChar);
477 inset->setBuffer(*buf);
478 par.insertInset(par.size(), inset.release(), font, change);
479 } else if (token == "\\backslash") {
480 par.appendChar('\\', font, change);
481 } else if (token == "\\LyXTable") {
482 auto_ptr<Inset> inset(new InsetTabular(buf));
484 par.insertInset(par.size(), inset.release(), font, change);
485 } else if (token == "\\change_unchanged") {
486 change = Change(Change::UNCHANGED);
487 } else if (token == "\\change_inserted" || token == "\\change_deleted") {
489 istringstream is(lex.getString());
493 BufferParams::AuthorMap const & am = bp.author_map;
494 if (am.find(aid) == am.end()) {
495 errorList.push_back(ErrorItem(_("Change tracking error"),
496 bformat(_("Unknown author index for change: %1$d\n"), aid),
497 par.id(), 0, par.size()));
498 change = Change(Change::UNCHANGED);
500 if (token == "\\change_inserted")
501 change = Change(Change::INSERTED, am.find(aid)->second, ct);
503 change = Change(Change::DELETED, am.find(aid)->second, ct);
507 errorList.push_back(ErrorItem(_("Unknown token"),
508 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
510 par.id(), 0, par.size()));
515 void Text::readParagraph(Paragraph & par, Lexer & lex,
516 ErrorList & errorList)
519 string token = lex.getString();
521 Change change(Change::UNCHANGED);
524 readParToken(par, lex, token, font, change, errorList);
527 token = lex.getString();
532 if (token == "\\end_layout") {
533 //Ok, paragraph finished
537 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
538 if (token == "\\begin_layout" || token == "\\end_document"
539 || token == "\\end_inset" || token == "\\begin_deeper"
540 || token == "\\end_deeper") {
541 lex.pushToken(token);
542 lyxerr << "Paragraph ended in line "
543 << lex.lineNumber() << "\n"
544 << "Missing \\end_layout.\n";
548 // Final change goes to paragraph break:
549 par.setChange(par.size(), change);
551 // Initialize begin_of_body_ on load; redoParagraph maintains
552 par.setBeginOfBody();
554 // mark paragraph for spell checking on load
555 // par.requestSpellCheck();
559 class TextCompletionList : public CompletionList
563 TextCompletionList(Cursor const & cur, WordList const * list)
564 : buffer_(cur.buffer()), pos_(0), list_(list)
567 virtual ~TextCompletionList() {}
570 virtual bool sorted() const { return true; }
572 virtual size_t size() const
574 return list_->size();
577 virtual docstring const & data(size_t idx) const
579 return list_->word(idx);
584 Buffer const * buffer_;
588 WordList const * list_;
592 bool Text::empty() const
594 return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
595 // FIXME: Should we consider the labeled type as empty too?
596 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
600 double Text::spacing(Paragraph const & par) const
602 if (par.params().spacing().isDefault())
603 return owner_->buffer().params().spacing().getValue();
604 return par.params().spacing().getValue();
609 * This breaks a paragraph at the specified position.
610 * The new paragraph will:
611 * - Decrease depth by one (or change layout to default layout) when
612 * keep_layout == false
613 * - keep current depth and layout when keep_layout == true
615 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
618 BufferParams const & bparams = text.inset().buffer().params();
619 ParagraphList & pars = text.paragraphs();
620 // create a new paragraph, and insert into the list
621 ParagraphList::iterator tmp =
622 pars.insert(boost::next(pars.begin(), par_offset + 1),
625 Paragraph & par = pars[par_offset];
627 // remember to set the inset_owner
628 tmp->setInsetOwner(&par.inInset());
629 // without doing that we get a crash when typing <Return> at the
630 // end of a paragraph
631 tmp->setPlainOrDefaultLayout(bparams.documentClass());
634 tmp->setLayout(par.layout());
635 tmp->setLabelWidthString(par.params().labelWidthString());
636 tmp->params().depth(par.params().depth());
637 } else if (par.params().depth() > 0) {
638 Paragraph const & hook = pars[text.outerHook(par_offset)];
639 tmp->setLayout(hook.layout());
640 // not sure the line below is useful
641 tmp->setLabelWidthString(par.params().labelWidthString());
642 tmp->params().depth(hook.params().depth());
645 bool const isempty = (par.allowEmpty() && par.empty());
647 if (!isempty && (par.size() > pos || par.empty())) {
648 tmp->setLayout(par.layout());
649 tmp->params().align(par.params().align());
650 tmp->setLabelWidthString(par.params().labelWidthString());
652 tmp->params().depth(par.params().depth());
653 tmp->params().noindent(par.params().noindent());
655 // move everything behind the break position
656 // to the new paragraph
658 /* Note: if !keepempty, empty() == true, then we reach
659 * here with size() == 0. So pos_end becomes - 1. This
660 * doesn't cause problems because both loops below
661 * enforce pos <= pos_end and 0 <= pos
663 pos_type pos_end = par.size() - 1;
665 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
666 if (moveItem(par, pos, *tmp, j, bparams)) {
672 // Move over the end-of-par change information
673 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
674 par.setChange(par.size(), Change(bparams.trackChanges ?
675 Change::INSERTED : Change::UNCHANGED));
678 // Make sure that we keep the language when
679 // breaking paragraph.
681 Font changed = tmp->getFirstFontSettings(bparams);
682 Font const & old = par.getFontSettings(bparams, par.size());
683 changed.setLanguage(old.language());
684 tmp->setFont(0, changed);
691 bool const soa = par.params().startOfAppendix();
692 par.params().clear();
693 // do not lose start of appendix marker (bug 4212)
694 par.params().startOfAppendix(soa);
695 par.setPlainOrDefaultLayout(bparams.documentClass());
699 par.setLayout(tmp->layout());
700 par.setLabelWidthString(tmp->params().labelWidthString());
701 par.params().depth(tmp->params().depth());
706 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
708 LASSERT(this == cur.text(), /**/);
710 Paragraph & cpar = cur.paragraph();
711 pit_type cpit = cur.pit();
713 DocumentClass const & tclass = cur.buffer()->params().documentClass();
714 Layout const & layout = cpar.layout();
716 if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
717 if (changeDepthAllowed(cur, DEC_DEPTH))
718 changeDepth(cur, DEC_DEPTH);
720 setLayout(cur, tclass.defaultLayoutName());
724 // a layout change may affect also the following paragraph
725 recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
727 // Always break behind a space
728 // It is better to erase the space (Dekel)
729 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
730 cpar.eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
732 // What should the layout for the new paragraph be?
733 bool keep_layout = layout.isEnvironment()
734 || (layout.isParagraph() && layout.parbreak_is_newline);
736 keep_layout = !keep_layout;
738 // We need to remember this before we break the paragraph, because
739 // that invalidates the layout variable
740 bool sensitive = layout.labeltype == LABEL_SENSITIVE;
742 // we need to set this before we insert the paragraph.
743 bool const isempty = cpar.allowEmpty() && cpar.empty();
745 lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
747 // After this, neither paragraph contains any rows!
750 pit_type next_par = cpit + 1;
752 // well this is the caption hack since one caption is really enough
755 // set to standard-layout
756 //FIXME Check if this should be plainLayout() in some cases
757 pars_[cpit].applyLayout(tclass.defaultLayout());
759 // set to standard-layout
760 //FIXME Check if this should be plainLayout() in some cases
761 pars_[next_par].applyLayout(tclass.defaultLayout());
764 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
765 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().trackChanges))
766 break; // the character couldn't be deleted physically due to change tracking
769 // A singlePar update is not enough in this case.
770 cur.screenUpdateFlags(Update::Force);
771 cur.forceBufferUpdate();
773 // This check is necessary. Otherwise the new empty paragraph will
774 // be deleted automatically. And it is more friendly for the user!
775 if (cur.pos() != 0 || isempty)
776 setCursor(cur, cur.pit() + 1, 0);
778 setCursor(cur, cur.pit(), 0);
782 // needed to insert the selection
783 void Text::insertStringAsLines(Cursor & cur, docstring const & str,
786 BufferParams const & bparams = owner_->buffer().params();
787 pit_type pit = cur.pit();
788 pos_type pos = cur.pos();
790 // insert the string, don't insert doublespace
791 bool space_inserted = true;
792 for (docstring::const_iterator cit = str.begin();
793 cit != str.end(); ++cit) {
794 Paragraph & par = pars_[pit];
796 if (autoBreakRows_ && (!par.empty() || par.allowEmpty())) {
797 lyx::breakParagraph(*this, pit, pos,
798 par.layout().isEnvironment());
801 space_inserted = true;
805 // do not insert consecutive spaces if !free_spacing
806 } else if ((*cit == ' ' || *cit == '\t') &&
807 space_inserted && !par.isFreeSpacing()) {
809 } else if (*cit == '\t') {
810 if (!par.isFreeSpacing()) {
811 // tabs are like spaces here
812 par.insertChar(pos, ' ', font, bparams.trackChanges);
814 space_inserted = true;
816 par.insertChar(pos, *cit, font, bparams.trackChanges);
818 space_inserted = true;
820 } else if (!isPrintable(*cit)) {
821 // Ignore unprintables
824 // just insert the character
825 par.insertChar(pos, *cit, font, bparams.trackChanges);
827 space_inserted = (*cit == ' ');
830 setCursor(cur, pit, pos);
834 // turn double CR to single CR, others are converted into one
835 // blank. Then insertStringAsLines is called
836 void Text::insertStringAsParagraphs(Cursor & cur, docstring const & str,
839 docstring linestr = str;
840 bool newline_inserted = false;
842 for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
843 if (linestr[i] == '\n') {
844 if (newline_inserted) {
845 // we know that \r will be ignored by
846 // insertStringAsLines. Of course, it is a dirty
847 // trick, but it works...
848 linestr[i - 1] = '\r';
852 newline_inserted = true;
854 } else if (isPrintable(linestr[i])) {
855 newline_inserted = false;
858 insertStringAsLines(cur, linestr, font);
862 // insert a character, moves all the following breaks in the
863 // same Paragraph one to the right and make a rebreak
864 void Text::insertChar(Cursor & cur, char_type c)
866 LASSERT(this == cur.text(), /**/);
868 cur.recordUndo(INSERT_UNDO);
870 TextMetrics const & tm = cur.bv().textMetrics(this);
871 Buffer const & buffer = *cur.buffer();
872 Paragraph & par = cur.paragraph();
873 // try to remove this
874 pit_type const pit = cur.pit();
876 bool const freeSpacing = par.layout().free_spacing ||
879 if (lyxrc.auto_number) {
880 static docstring const number_operators = from_ascii("+-/*");
881 static docstring const number_unary_operators = from_ascii("+-");
882 static docstring const number_seperators = from_ascii(".,:");
884 if (cur.current_font.fontInfo().number() == FONT_ON) {
885 if (!isDigitASCII(c) && !contains(number_operators, c) &&
886 !(contains(number_seperators, c) &&
888 cur.pos() != cur.lastpos() &&
889 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
890 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
892 number(cur); // Set current_font.number to OFF
893 } else if (isDigitASCII(c) &&
894 cur.real_current_font.isVisibleRightToLeft()) {
895 number(cur); // Set current_font.number to ON
897 if (cur.pos() != 0) {
898 char_type const c = par.getChar(cur.pos() - 1);
899 if (contains(number_unary_operators, c) &&
901 || par.isSeparator(cur.pos() - 2)
902 || par.isNewline(cur.pos() - 2))
904 setCharFont(pit, cur.pos() - 1, cur.current_font,
906 } else if (contains(number_seperators, c)
908 && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
909 setCharFont(pit, cur.pos() - 1, cur.current_font,
916 // In Bidi text, we want spaces to be treated in a special way: spaces
917 // which are between words in different languages should get the
918 // paragraph's language; otherwise, spaces should keep the language
919 // they were originally typed in. This is only in effect while typing;
920 // after the text is already typed in, the user can always go back and
921 // explicitly set the language of a space as desired. But 99.9% of the
922 // time, what we're doing here is what the user actually meant.
924 // The following cases are the ones in which the language of the space
925 // should be changed to match that of the containing paragraph. In the
926 // depictions, lowercase is LTR, uppercase is RTL, underscore (_)
927 // represents a space, pipe (|) represents the cursor position (so the
928 // character before it is the one just typed in). The different cases
929 // are depicted logically (not visually), from left to right:
934 // Theoretically, there are other situations that we should, perhaps, deal
935 // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any
936 // point (to understand why, just try to create this situation...).
938 if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
939 // get font in front and behind the space in question. But do NOT
940 // use getFont(cur.pos()) because the character c is not inserted yet
941 Font const pre_space_font = tm.displayFont(cur.pit(), cur.pos() - 2);
942 Font const & post_space_font = cur.real_current_font;
943 bool pre_space_rtl = pre_space_font.isVisibleRightToLeft();
944 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
946 if (pre_space_rtl != post_space_rtl) {
947 // Set the space's language to match the language of the
948 // adjacent character whose direction is the paragraph's
949 // direction; don't touch other properties of the font
950 Language const * lang =
951 (pre_space_rtl == par.isRTL(buffer.params())) ?
952 pre_space_font.language() : post_space_font.language();
954 Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
955 space_font.setLanguage(lang);
956 par.setFont(cur.pos() - 1, space_font);
960 // Next check, if there will be two blanks together or a blank at
961 // the beginning of a paragraph.
962 // I decided to handle blanks like normal characters, the main
963 // difference are the special checks when calculating the row.fill
964 // (blank does not count at the end of a row) and the check here
966 // When the free-spacing option is set for the current layout,
967 // disable the double-space checking
968 if (!freeSpacing && isLineSeparatorChar(c)) {
969 if (cur.pos() == 0) {
971 "You cannot insert a space at the "
972 "beginning of a paragraph. Please read the Tutorial."));
975 LASSERT(cur.pos() > 0, /**/);
976 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
977 && !par.isDeleted(cur.pos() - 1)) {
979 "You cannot type two spaces this way. "
980 "Please read the Tutorial."));
985 par.insertChar(cur.pos(), c, cur.current_font,
986 cur.buffer()->params().trackChanges);
987 cur.checkBufferStructure();
989 // cur.screenUpdateFlags(Update::Force);
990 bool boundary = cur.boundary()
991 || tm.isRTLBoundary(cur.pit(), cur.pos() + 1);
992 setCursor(cur, cur.pit(), cur.pos() + 1, false, boundary);
997 void Text::charInserted(Cursor & cur)
999 Paragraph & par = cur.paragraph();
1001 // Here we call finishUndo for every 20 characters inserted.
1002 // This is from my experience how emacs does it. (Lgb)
1003 if (undo_counter_ < 20) {
1010 // register word if a non-letter was entered
1012 && !par.isWordSeparator(cur.pos() - 2)
1013 && par.isWordSeparator(cur.pos() - 1)) {
1014 // get the word in front of cursor
1015 LASSERT(this == cur.text(), /**/);
1016 cur.paragraph().updateWords();
1021 // the cursor set functions have a special mechanism. When they
1022 // realize, that you left an empty paragraph, they will delete it.
1024 bool Text::cursorForwardOneWord(Cursor & cur)
1026 LASSERT(this == cur.text(), /**/);
1028 pos_type const lastpos = cur.lastpos();
1029 pit_type pit = cur.pit();
1030 pos_type pos = cur.pos();
1031 Paragraph const & par = cur.paragraph();
1033 // Paragraph boundary is a word boundary
1034 if (pos == lastpos) {
1035 if (pit != cur.lastpit())
1036 return setCursor(cur, pit + 1, 0);
1041 if (lyxrc.mac_like_word_movement) {
1042 // Skip through trailing punctuation and spaces.
1043 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
1046 // Skip over either a non-char inset or a full word
1047 if (pos != lastpos && par.isWordSeparator(pos))
1049 else while (pos != lastpos && !par.isWordSeparator(pos))
1052 LASSERT(pos < lastpos, /**/); // see above
1053 if (!par.isWordSeparator(pos))
1054 while (pos != lastpos && !par.isWordSeparator(pos))
1056 else if (par.isChar(pos))
1057 while (pos != lastpos && par.isChar(pos))
1059 else if (!par.isSpace(pos)) // non-char inset
1062 // Skip over white space
1063 while (pos != lastpos && par.isSpace(pos))
1067 return setCursor(cur, pit, pos);
1071 bool Text::cursorBackwardOneWord(Cursor & cur)
1073 LASSERT(this == cur.text(), /**/);
1075 pit_type pit = cur.pit();
1076 pos_type pos = cur.pos();
1077 Paragraph & par = cur.paragraph();
1079 // Paragraph boundary is a word boundary
1080 if (pos == 0 && pit != 0)
1081 return setCursor(cur, pit - 1, getPar(pit - 1).size());
1083 if (lyxrc.mac_like_word_movement) {
1084 // Skip through punctuation and spaces.
1085 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1088 // Skip over either a non-char inset or a full word
1089 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1091 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1094 // Skip over white space
1095 while (pos != 0 && par.isSpace(pos - 1))
1098 if (pos != 0 && !par.isWordSeparator(pos - 1))
1099 while (pos != 0 && !par.isWordSeparator(pos - 1))
1101 else if (pos != 0 && par.isChar(pos - 1))
1102 while (pos != 0 && par.isChar(pos - 1))
1104 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1108 return setCursor(cur, pit, pos);
1112 bool Text::cursorVisLeftOneWord(Cursor & cur)
1114 LASSERT(this == cur.text(), /**/);
1116 pos_type left_pos, right_pos;
1117 bool left_is_letter, right_is_letter;
1119 Cursor temp_cur = cur;
1121 // always try to move at least once...
1122 while (temp_cur.posVisLeft(true /* skip_inset */)) {
1124 // collect some information about current cursor position
1125 temp_cur.getSurroundingPos(left_pos, right_pos);
1127 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1129 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1131 // if we're not at a letter/non-letter boundary, continue moving
1132 if (left_is_letter == right_is_letter)
1135 // we should stop when we have an LTR word on our right or an RTL word
1137 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1138 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1139 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1140 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1144 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1145 true, temp_cur.boundary());
1149 bool Text::cursorVisRightOneWord(Cursor & cur)
1151 LASSERT(this == cur.text(), /**/);
1153 pos_type left_pos, right_pos;
1154 bool left_is_letter, right_is_letter;
1156 Cursor temp_cur = cur;
1158 // always try to move at least once...
1159 while (temp_cur.posVisRight(true /* skip_inset */)) {
1161 // collect some information about current cursor position
1162 temp_cur.getSurroundingPos(left_pos, right_pos);
1164 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1166 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1168 // if we're not at a letter/non-letter boundary, continue moving
1169 if (left_is_letter == right_is_letter)
1172 // we should stop when we have an LTR word on our right or an RTL word
1174 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1175 temp_cur.buffer()->params(),
1176 left_pos).isRightToLeft())
1177 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1178 temp_cur.buffer()->params(),
1179 right_pos).isRightToLeft()))
1183 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1184 true, temp_cur.boundary());
1188 void Text::selectWord(Cursor & cur, word_location loc)
1190 LASSERT(this == cur.text(), /**/);
1191 CursorSlice from = cur.top();
1192 CursorSlice to = cur.top();
1193 getWord(from, to, loc);
1194 if (cur.top() != from)
1195 setCursor(cur, from.pit(), from.pos());
1198 if (!cur.selection())
1200 setCursor(cur, to.pit(), to.pos());
1202 cur.setWordSelection(true);
1206 void Text::selectAll(Cursor & cur)
1208 LASSERT(this == cur.text(), /**/);
1209 if (cur.lastpos() == 0 && cur.lastpit() == 0)
1211 // If the cursor is at the beginning, make sure the cursor ends there
1212 if (cur.pit() == 0 && cur.pos() == 0) {
1213 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1215 setCursor(cur, 0, 0);
1217 setCursor(cur, 0, 0);
1219 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1225 // Select the word currently under the cursor when no
1226 // selection is currently set
1227 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1229 LASSERT(this == cur.text(), /**/);
1230 if (cur.selection())
1232 selectWord(cur, loc);
1233 return cur.selection();
1237 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1239 LASSERT(this == cur.text(), /**/);
1241 if (!cur.selection()) {
1242 bool const changed = cur.paragraph().isChanged(cur.pos());
1243 if (!(changed && findNextChange(&cur.bv())))
1247 cur.recordUndoSelection();
1249 pit_type begPit = cur.selectionBegin().pit();
1250 pit_type endPit = cur.selectionEnd().pit();
1252 pos_type begPos = cur.selectionBegin().pos();
1253 pos_type endPos = cur.selectionEnd().pos();
1255 // keep selection info, because endPos becomes invalid after the first loop
1256 bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1258 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1260 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1261 pos_type parSize = pars_[pit].size();
1263 // ignore empty paragraphs; otherwise, an assertion will fail for
1264 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1268 // do not consider first paragraph if the cursor starts at pos size()
1269 if (pit == begPit && begPos == parSize)
1272 // do not consider last paragraph if the cursor ends at pos 0
1273 if (pit == endPit && endPos == 0)
1274 break; // last iteration anyway
1276 pos_type left = (pit == begPit ? begPos : 0);
1277 pos_type right = (pit == endPit ? endPos : parSize);
1280 // there is no change here
1284 pars_[pit].acceptChanges(left, right);
1286 pars_[pit].rejectChanges(left, right);
1290 // next, accept/reject imaginary end-of-par characters
1292 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1293 pos_type pos = pars_[pit].size();
1295 // skip if the selection ends before the end-of-par
1296 if (pit == endPit && endsBeforeEndOfPar)
1297 break; // last iteration anyway
1299 // skip if this is not the last paragraph of the document
1300 // note: the user should be able to accept/reject the par break of the last par!
1301 if (pit == endPit && pit + 1 != int(pars_.size()))
1302 break; // last iteration anway
1305 if (pars_[pit].isInserted(pos)) {
1306 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1307 } else if (pars_[pit].isDeleted(pos)) {
1308 if (pit + 1 == int(pars_.size())) {
1309 // we cannot remove a par break at the end of the last paragraph;
1310 // instead, we mark it unchanged
1311 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1313 mergeParagraph(cur.buffer()->params(), pars_, pit);
1319 if (pars_[pit].isDeleted(pos)) {
1320 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1321 } else if (pars_[pit].isInserted(pos)) {
1322 if (pit + 1 == int(pars_.size())) {
1323 // we mark the par break at the end of the last paragraph unchanged
1324 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1326 mergeParagraph(cur.buffer()->params(), pars_, pit);
1334 // finally, invoke the DEPM
1336 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().trackChanges);
1341 cur.clearSelection();
1342 setCursorIntern(cur, begPit, begPos);
1343 cur.screenUpdateFlags(Update::Force);
1344 cur.forceBufferUpdate();
1348 void Text::acceptChanges()
1350 BufferParams const & bparams = owner_->buffer().params();
1351 lyx::acceptChanges(pars_, bparams);
1352 deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.trackChanges);
1356 void Text::rejectChanges()
1358 BufferParams const & bparams = owner_->buffer().params();
1359 pit_type pars_size = static_cast<pit_type>(pars_.size());
1361 // first, reject changes within each individual paragraph
1362 // (do not consider end-of-par)
1363 for (pit_type pit = 0; pit < pars_size; ++pit) {
1364 if (!pars_[pit].empty()) // prevent assertion failure
1365 pars_[pit].rejectChanges(0, pars_[pit].size());
1368 // next, reject imaginary end-of-par characters
1369 for (pit_type pit = 0; pit < pars_size; ++pit) {
1370 pos_type pos = pars_[pit].size();
1372 if (pars_[pit].isDeleted(pos)) {
1373 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1374 } else if (pars_[pit].isInserted(pos)) {
1375 if (pit == pars_size - 1) {
1376 // we mark the par break at the end of the last
1377 // paragraph unchanged
1378 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1380 mergeParagraph(bparams, pars_, pit);
1387 // finally, invoke the DEPM
1388 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
1392 void Text::deleteWordForward(Cursor & cur)
1394 LASSERT(this == cur.text(), /**/);
1395 if (cur.lastpos() == 0)
1399 cur.setSelection(true);
1400 cursorForwardOneWord(cur);
1402 cutSelection(cur, true, false);
1403 cur.checkBufferStructure();
1408 void Text::deleteWordBackward(Cursor & cur)
1410 LASSERT(this == cur.text(), /**/);
1411 if (cur.lastpos() == 0)
1412 cursorBackward(cur);
1415 cur.setSelection(true);
1416 cursorBackwardOneWord(cur);
1418 cutSelection(cur, true, false);
1419 cur.checkBufferStructure();
1424 // Kill to end of line.
1425 void Text::changeCase(Cursor & cur, TextCase action)
1427 LASSERT(this == cur.text(), /**/);
1431 bool gotsel = false;
1432 if (cur.selection()) {
1433 from = cur.selBegin();
1438 getWord(from, to, PARTIAL_WORD);
1439 cursorForwardOneWord(cur);
1442 cur.recordUndoSelection();
1444 pit_type begPit = from.pit();
1445 pit_type endPit = to.pit();
1447 pos_type begPos = from.pos();
1448 pos_type endPos = to.pos();
1450 pos_type right = 0; // needed after the for loop
1452 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1453 Paragraph & par = pars_[pit];
1454 pos_type const pos = (pit == begPit ? begPos : 0);
1455 right = (pit == endPit ? endPos : par.size());
1456 par.changeCase(cur.buffer()->params(), pos, right, action);
1459 // the selection may have changed due to logically-only deleted chars
1461 setCursor(cur, begPit, begPos);
1463 setCursor(cur, endPit, right);
1466 setCursor(cur, endPit, right);
1468 cur.checkBufferStructure();
1472 bool Text::handleBibitems(Cursor & cur)
1474 if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1480 BufferParams const & bufparams = cur.buffer()->params();
1481 Paragraph const & par = cur.paragraph();
1482 Cursor prevcur = cur;
1483 if (cur.pit() > 0) {
1485 prevcur.pos() = prevcur.lastpos();
1487 Paragraph const & prevpar = prevcur.paragraph();
1489 // if a bibitem is deleted, merge with previous paragraph
1490 // if this is a bibliography item as well
1491 if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1492 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1493 mergeParagraph(bufparams, cur.text()->paragraphs(),
1495 cur.forceBufferUpdate();
1496 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1497 cur.screenUpdateFlags(Update::Force);
1501 // otherwise reset to default
1502 cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1507 bool Text::erase(Cursor & cur)
1509 LASSERT(this == cur.text(), return false);
1510 bool needsUpdate = false;
1511 Paragraph & par = cur.paragraph();
1513 if (cur.pos() != cur.lastpos()) {
1514 // this is the code for a normal delete, not pasting
1516 cur.recordUndo(DELETE_UNDO);
1517 bool const was_inset = cur.paragraph().isInset(cur.pos());
1518 if(!par.eraseChar(cur.pos(), cur.buffer()->params().trackChanges))
1519 // the character has been logically deleted only => skip it
1520 cur.top().forwardPos();
1523 cur.forceBufferUpdate();
1525 cur.checkBufferStructure();
1528 if (cur.pit() == cur.lastpit())
1529 return dissolveInset(cur);
1531 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1532 par.setChange(cur.pos(), Change(Change::DELETED));
1536 setCursorIntern(cur, cur.pit() + 1, 0);
1537 needsUpdate = backspacePos0(cur);
1541 needsUpdate |= handleBibitems(cur);
1544 // Make sure the cursor is correct. Is this really needed?
1545 // No, not really... at least not here!
1546 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1547 cur.checkBufferStructure();
1554 bool Text::backspacePos0(Cursor & cur)
1556 LASSERT(this == cur.text(), /**/);
1560 bool needsUpdate = false;
1562 BufferParams const & bufparams = cur.buffer()->params();
1563 DocumentClass const & tclass = bufparams.documentClass();
1564 ParagraphList & plist = cur.text()->paragraphs();
1565 Paragraph const & par = cur.paragraph();
1566 Cursor prevcur = cur;
1568 prevcur.pos() = prevcur.lastpos();
1569 Paragraph const & prevpar = prevcur.paragraph();
1571 // is it an empty paragraph?
1572 if (cur.lastpos() == 0
1573 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1574 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1575 plist.erase(boost::next(plist.begin(), cur.pit()));
1578 // is previous par empty?
1579 else if (prevcur.lastpos() == 0
1580 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1581 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1582 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1585 // Pasting is not allowed, if the paragraphs have different
1586 // layouts. I think it is a real bug of all other
1587 // word processors to allow it. It confuses the user.
1588 // Correction: Pasting is always allowed with standard-layout
1589 // or the empty layout.
1590 else if (par.layout() == prevpar.layout()
1591 || tclass.isDefaultLayout(par.layout())
1592 || tclass.isPlainLayout(par.layout())) {
1593 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1594 mergeParagraph(bufparams, plist, prevcur.pit());
1599 cur.forceBufferUpdate();
1600 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1607 bool Text::backspace(Cursor & cur)
1609 LASSERT(this == cur.text(), /**/);
1610 bool needsUpdate = false;
1611 if (cur.pos() == 0) {
1613 return dissolveInset(cur);
1615 Paragraph & prev_par = pars_[cur.pit() - 1];
1617 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1618 prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1619 setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1622 // The cursor is at the beginning of a paragraph, so
1623 // the backspace will collapse two paragraphs into one.
1624 needsUpdate = backspacePos0(cur);
1627 // this is the code for a normal backspace, not pasting
1629 cur.recordUndo(DELETE_UNDO);
1630 // We used to do cursorBackwardIntern() here, but it is
1631 // not a good idea since it triggers the auto-delete
1632 // mechanism. So we do a cursorBackwardIntern()-lite,
1633 // without the dreaded mechanism. (JMarc)
1634 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1635 false, cur.boundary());
1636 bool const was_inset = cur.paragraph().isInset(cur.pos());
1637 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
1639 cur.forceBufferUpdate();
1641 cur.checkBufferStructure();
1644 if (cur.pos() == cur.lastpos())
1645 cur.setCurrentFont();
1647 needsUpdate |= handleBibitems(cur);
1649 // A singlePar update is not enough in this case.
1650 // cur.screenUpdateFlags(Update::Force);
1651 setCursor(cur.top(), cur.pit(), cur.pos());
1657 bool Text::dissolveInset(Cursor & cur)
1659 LASSERT(this == cur.text(), return false);
1661 if (isMainText() || cur.inset().nargs() != 1)
1664 cur.recordUndoInset();
1666 cur.selHandle(false);
1668 pos_type spos = cur.pos();
1669 pit_type spit = cur.pit();
1670 ParagraphList plist;
1671 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1672 plist = paragraphs();
1674 // store cursor offset
1678 Buffer & b = *cur.buffer();
1679 cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1681 if (!plist.empty()) {
1683 // we clear the cache so that we won't get conflicts with labels
1684 // that get pasted into the buffer. we should update this before
1685 // its being empty matters. if not (i.e., if we encounter bugs),
1686 // then this should instead be:
1687 // cur.buffer().updateBuffer();
1688 // but we'll try the cheaper solution here.
1689 cur.buffer()->clearReferenceCache();
1691 // ERT paragraphs have the Language latex_language.
1692 // This is invalid outside of ERT, so we need to
1693 // change it to the buffer language.
1694 ParagraphList::iterator it = plist.begin();
1695 ParagraphList::iterator it_end = plist.end();
1696 for (; it != it_end; it++)
1697 it->changeLanguage(b.params(), latex_language, b.language());
1699 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1700 b.errorList("Paste"));
1702 cur.pit() = min(cur.lastpit(), spit);
1703 cur.pos() = min(cur.lastpos(), spos);
1706 cur.forceBufferUpdate();
1708 // Ensure the current language is set correctly (bug 6292)
1709 cur.text()->setCursor(cur, cur.pit(), cur.pos());
1710 cur.clearSelection();
1716 void Text::getWord(CursorSlice & from, CursorSlice & to,
1717 word_location const loc) const
1720 pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1724 void Text::write(ostream & os) const
1726 Buffer const & buf = owner_->buffer();
1727 ParagraphList::const_iterator pit = paragraphs().begin();
1728 ParagraphList::const_iterator end = paragraphs().end();
1730 for (; pit != end; ++pit)
1731 pit->write(os, buf.params(), dth);
1733 // Close begin_deeper
1734 for(; dth > 0; --dth)
1735 os << "\n\\end_deeper";
1739 bool Text::read(Lexer & lex,
1740 ErrorList & errorList, InsetText * insetPtr)
1742 Buffer const & buf = owner_->buffer();
1743 depth_type depth = 0;
1746 while (lex.isOK()) {
1748 string const token = lex.getString();
1753 if (token == "\\end_inset")
1756 if (token == "\\end_body")
1759 if (token == "\\begin_body")
1762 if (token == "\\end_document") {
1767 if (token == "\\begin_layout") {
1768 lex.pushToken(token);
1771 par.setInsetOwner(insetPtr);
1772 par.params().depth(depth);
1773 par.setFont(0, Font(inherit_font, buf.params().language));
1774 pars_.push_back(par);
1775 readParagraph(pars_.back(), lex, errorList);
1777 // register the words in the global word list
1778 pars_.back().updateWords();
1779 } else if (token == "\\begin_deeper") {
1781 } else if (token == "\\end_deeper") {
1783 lex.printError("\\end_deeper: " "depth is already null");
1787 LYXERR0("Handling unknown body token: `" << token << '\'');
1791 // avoid a crash on weird documents (bug 4859)
1792 if (pars_.empty()) {
1794 par.setInsetOwner(insetPtr);
1795 par.params().depth(depth);
1796 par.setFont(0, Font(inherit_font,
1797 buf.params().language));
1798 par.setPlainOrDefaultLayout(buf.params().documentClass());
1799 pars_.push_back(par);
1805 // Returns the current font and depth as a message.
1806 docstring Text::currentState(Cursor const & cur) const
1808 LASSERT(this == cur.text(), /**/);
1809 Buffer & buf = *cur.buffer();
1810 Paragraph const & par = cur.paragraph();
1811 odocstringstream os;
1813 if (buf.params().trackChanges)
1814 os << _("[Change Tracking] ");
1816 Change change = par.lookupChange(cur.pos());
1818 if (change.changed()) {
1819 Author const & a = buf.params().authors().get(change.author);
1820 os << _("Change: ") << a.name();
1821 if (!a.email().empty())
1822 os << " (" << a.email() << ")";
1823 // FIXME ctime is english, we should translate that
1824 os << _(" at ") << ctime(&change.changetime);
1828 // I think we should only show changes from the default
1830 // No, from the document font (MV)
1831 Font font = cur.real_current_font;
1832 font.fontInfo().reduce(buf.params().getFont().fontInfo());
1834 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1836 // The paragraph depth
1837 int depth = cur.paragraph().getDepth();
1839 os << bformat(_(", Depth: %1$d"), depth);
1841 // The paragraph spacing, but only if different from
1843 Spacing const & spacing = par.params().spacing();
1844 if (!spacing.isDefault()) {
1845 os << _(", Spacing: ");
1846 switch (spacing.getSpace()) {
1847 case Spacing::Single:
1850 case Spacing::Onehalf:
1853 case Spacing::Double:
1856 case Spacing::Other:
1857 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1859 case Spacing::Default:
1860 // should never happen, do nothing
1865 #ifdef DEVEL_VERSION
1866 os << _(", Inset: ") << &cur.inset();
1867 os << _(", Paragraph: ") << cur.pit();
1868 os << _(", Id: ") << par.id();
1869 os << _(", Position: ") << cur.pos();
1870 // FIXME: Why is the check for par.size() needed?
1871 // We are called with cur.pos() == par.size() quite often.
1872 if (!par.empty() && cur.pos() < par.size()) {
1873 // Force output of code point, not character
1874 size_t const c = par.getChar(cur.pos());
1875 os << _(", Char: 0x") << hex << c;
1877 os << _(", Boundary: ") << cur.boundary();
1878 // Row & row = cur.textRow();
1879 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1885 docstring Text::getPossibleLabel(Cursor const & cur) const
1887 pit_type pit = cur.pit();
1889 Layout const * layout = &(pars_[pit].layout());
1892 docstring par_text = pars_[pit].asString();
1894 // The return string of math matrices might contain linebreaks
1895 par_text = subst(par_text, '\n', '-');
1896 int const numwords = 3;
1897 for (int i = 0; i < numwords; ++i) {
1898 if (par_text.empty())
1901 par_text = split(par_text, head, ' ');
1902 // Is it legal to use spaces in labels ?
1908 // Make sure it isn't too long
1909 unsigned int const max_label_length = 32;
1910 if (text.size() > max_label_length)
1911 text.resize(max_label_length);
1913 // Will contain the label prefix.
1916 // For section, subsection, etc...
1917 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1918 Layout const * layout2 = &(pars_[pit - 1].layout());
1919 if (layout2->latextype != LATEX_PARAGRAPH) {
1924 if (layout->latextype != LATEX_PARAGRAPH)
1925 name = layout->refprefix;
1927 // For captions, we just take the caption type
1928 Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
1929 if (caption_inset) {
1930 string const & ftype = static_cast<InsetCaption *>(caption_inset)->type();
1931 FloatList const & fl = cur.buffer()->params().documentClass().floats();
1932 if (fl.typeExist(ftype)) {
1933 Floating const & flt = fl.getType(ftype);
1934 name = from_utf8(flt.refPrefix());
1937 name = from_utf8(ftype.substr(0,3));
1940 // If none of the above worked, see if the inset knows.
1942 InsetLayout const & il = cur.inset().getLayout();
1943 name = il.refprefix();
1947 text = name + ':' + text;
1953 docstring Text::asString(int options) const
1955 return asString(0, pars_.size(), options);
1959 docstring Text::asString(pit_type beg, pit_type end, int options) const
1961 size_t i = size_t(beg);
1962 docstring str = pars_[i].asString(options);
1963 for (++i; i != size_t(end); ++i) {
1965 str += pars_[i].asString(options);
1971 void Text::forToc(docstring & os, size_t maxlen, bool shorten) const
1973 LASSERT(maxlen > 10, maxlen = 30);
1974 for (size_t i = 0; i != pars_.size() && os.length() < maxlen; ++i)
1975 pars_[i].forToc(os, maxlen);
1976 if (shorten && os.length() >= maxlen)
1977 os = os.substr(0, maxlen - 3) + from_ascii("...");
1981 void Text::charsTranspose(Cursor & cur)
1983 LASSERT(this == cur.text(), /**/);
1985 pos_type pos = cur.pos();
1987 // If cursor is at beginning or end of paragraph, do nothing.
1988 if (pos == cur.lastpos() || pos == 0)
1991 Paragraph & par = cur.paragraph();
1993 // Get the positions of the characters to be transposed.
1994 pos_type pos1 = pos - 1;
1995 pos_type pos2 = pos;
1997 // In change tracking mode, ignore deleted characters.
1998 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2000 if (pos2 == cur.lastpos())
2003 while (pos1 >= 0 && par.isDeleted(pos1))
2008 // Don't do anything if one of the "characters" is not regular text.
2009 if (par.isInset(pos1) || par.isInset(pos2))
2012 // Store the characters to be transposed (including font information).
2013 char_type const char1 = par.getChar(pos1);
2015 par.getFontSettings(cur.buffer()->params(), pos1);
2017 char_type const char2 = par.getChar(pos2);
2019 par.getFontSettings(cur.buffer()->params(), pos2);
2021 // And finally, we are ready to perform the transposition.
2022 // Track the changes if Change Tracking is enabled.
2023 bool const trackChanges = cur.buffer()->params().trackChanges;
2027 par.eraseChar(pos2, trackChanges);
2028 par.eraseChar(pos1, trackChanges);
2029 par.insertChar(pos1, char2, font2, trackChanges);
2030 par.insertChar(pos2, char1, font1, trackChanges);
2032 cur.checkBufferStructure();
2034 // After the transposition, move cursor to after the transposition.
2035 setCursor(cur, cur.pit(), pos2);
2040 DocIterator Text::macrocontextPosition() const
2042 return macrocontext_position_;
2046 void Text::setMacrocontextPosition(DocIterator const & pos)
2048 macrocontext_position_ = pos;
2052 docstring Text::previousWord(CursorSlice const & sl) const
2054 CursorSlice from = sl;
2055 CursorSlice to = sl;
2056 getWord(from, to, PREVIOUS_WORD);
2057 if (sl == from || to == from)
2060 Paragraph const & par = sl.paragraph();
2061 return par.asString(from.pos(), to.pos());
2065 bool Text::completionSupported(Cursor const & cur) const
2067 Paragraph const & par = cur.paragraph();
2068 return cur.pos() > 0
2069 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2070 && !par.isWordSeparator(cur.pos() - 1);
2074 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2076 WordList const * list = theWordList(*cur.getFont().language());
2077 return new TextCompletionList(cur, list);
2081 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2083 LASSERT(cur.bv().cursor() == cur, /**/);
2085 cur.bv().cursor() = cur;
2086 if (!(cur.result().screenUpdate() & Update::Force))
2087 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2092 docstring Text::completionPrefix(Cursor const & cur) const
2094 return previousWord(cur.top());