3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Dov Feldstern
9 * \author Jean-Marc Lasgouttes
11 * \author André Pönitz
12 * \author Stefan Schimanski
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
29 #include "CompletionList.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
34 #include "ErrorList.h"
35 #include "FuncRequest.h"
37 #include "InsetList.h"
44 #include "Paragraph.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
47 #include "TextClass.h"
48 #include "TextMetrics.h"
49 #include "WordLangTuple.h"
52 #include "insets/InsetText.h"
53 #include "insets/InsetBibitem.h"
54 #include "insets/InsetCaption.h"
55 #include "insets/InsetNewline.h"
56 #include "insets/InsetNewpage.h"
57 #include "insets/InsetArgument.h"
58 #include "insets/InsetIPAMacro.h"
59 #include "insets/InsetSpace.h"
60 #include "insets/InsetSpecialChar.h"
61 #include "insets/InsetTabular.h"
63 #include "support/debug.h"
64 #include "support/docstream.h"
65 #include "support/gettext.h"
66 #include "support/lassert.h"
67 #include "support/lstrings.h"
68 #include "support/textutils.h"
70 #include <boost/next_prior.hpp>
76 // TODO: replace if in Text::readParToken() with compile time switch
79 #include "support/metahash.h"
81 typedef boost::mpl::string<'\\end','_lay','out'> end_layout;
82 typedef boost::mpl::string<'\\end','in','set'> end_inset;
86 std::string token = "\\end_layout";
88 switch (boost::hash_value(token)) {
89 case lyx::support::hash_string<end_layout>::value:
91 case lyx::support::hash_string<end_inset>::value:
101 using namespace lyx::support;
105 using cap::cutSelection;
106 using cap::pasteParagraphList;
108 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
109 Paragraph & toPar, pos_type toPos, BufferParams const & params)
111 // Note: moveItem() does not honour change tracking!
112 // Therefore, it should only be used for breaking and merging paragraphs
114 // We need a copy here because the character at fromPos is going to be erased.
115 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
116 Change const tmpChange = fromPar.lookupChange(fromPos);
118 if (Inset * tmpInset = fromPar.getInset(fromPos)) {
119 fromPar.releaseInset(fromPos);
120 // The inset is not in fromPar any more.
121 if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
128 char_type const tmpChar = fromPar.getChar(fromPos);
129 fromPar.eraseChar(fromPos, false);
130 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
135 void breakParagraphConservative(BufferParams const & bparams,
136 ParagraphList & pars, pit_type par_offset, pos_type pos)
138 // create a new paragraph
139 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
141 Paragraph & par = pars[par_offset];
143 tmp.setInsetOwner(&par.inInset());
144 tmp.makeSameLayout(par);
146 LASSERT(pos <= par.size(), /**/);
148 if (pos < par.size()) {
149 // move everything behind the break position to the new paragraph
150 pos_type pos_end = par.size() - 1;
152 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
153 if (moveItem(par, pos, tmp, j, bparams)) {
157 // Move over the end-of-par change information
158 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
159 par.setChange(par.size(), Change(bparams.trackChanges ?
160 Change::INSERTED : Change::UNCHANGED));
165 void mergeParagraph(BufferParams const & bparams,
166 ParagraphList & pars, pit_type par_offset)
168 Paragraph & next = pars[par_offset + 1];
169 Paragraph & par = pars[par_offset];
171 pos_type pos_end = next.size() - 1;
172 pos_type pos_insert = par.size();
174 // the imaginary end-of-paragraph character (at par.size()) has to be
175 // marked as unmodified. Otherwise, its change is adopted by the first
176 // character of the next paragraph.
177 if (par.isChanged(par.size())) {
178 LYXERR(Debug::CHANGES,
179 "merging par with inserted/deleted end-of-par character");
180 par.setChange(par.size(), Change(Change::UNCHANGED));
183 Change change = next.lookupChange(next.size());
185 // move the content of the second paragraph to the end of the first one
186 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
187 if (moveItem(next, 0, par, j, bparams)) {
192 // move the change of the end-of-paragraph character
193 par.setChange(par.size(), change);
195 pars.erase(boost::next(pars.begin(), par_offset + 1));
199 Text::Text(InsetText * owner, bool use_default_layout)
200 : owner_(owner), autoBreakRows_(false), undo_counter_(0)
202 pars_.push_back(Paragraph());
203 Paragraph & par = pars_.back();
204 par.setInsetOwner(owner);
205 DocumentClass const & dc = owner->buffer().params().documentClass();
206 if (use_default_layout)
207 par.setDefaultLayout(dc);
209 par.setPlainLayout(dc);
213 Text::Text(InsetText * owner, Text const & text)
214 : owner_(owner), autoBreakRows_(text.autoBreakRows_), undo_counter_(0)
217 ParagraphList::iterator const end = pars_.end();
218 ParagraphList::iterator it = pars_.begin();
219 for (; it != end; ++it)
220 it->setInsetOwner(owner);
224 pit_type Text::depthHook(pit_type pit, depth_type depth) const
226 pit_type newpit = pit;
231 while (newpit != 0 && pars_[newpit].getDepth() > depth)
234 if (pars_[newpit].getDepth() > depth)
241 pit_type Text::outerHook(pit_type par_offset) const
243 Paragraph const & par = pars_[par_offset];
245 if (par.getDepth() == 0)
247 return depthHook(par_offset, depth_type(par.getDepth() - 1));
251 bool Text::isFirstInSequence(pit_type par_offset) const
253 Paragraph const & par = pars_[par_offset];
255 pit_type dhook_offset = depthHook(par_offset, par.getDepth());
257 if (dhook_offset == par_offset)
260 Paragraph const & dhook = pars_[dhook_offset];
262 return dhook.layout() != par.layout()
263 || dhook.getDepth() != par.getDepth();
267 Font const Text::outerFont(pit_type par_offset) const
269 depth_type par_depth = pars_[par_offset].getDepth();
270 FontInfo tmpfont = inherit_font;
271 depth_type prev_par_depth = 0;
272 // Resolve against environment font information
273 while (par_offset != pit_type(pars_.size())
274 && par_depth != prev_par_depth
276 && !tmpfont.resolved()) {
277 prev_par_depth = par_depth;
278 par_offset = outerHook(par_offset);
279 if (par_offset != pit_type(pars_.size())) {
280 tmpfont.realize(pars_[par_offset].layout().font);
281 par_depth = pars_[par_offset].getDepth();
285 return Font(tmpfont);
289 static void acceptOrRejectChanges(ParagraphList & pars,
290 BufferParams const & bparams, Text::ChangeOp op)
292 pit_type pars_size = static_cast<pit_type>(pars.size());
294 // first, accept or reject changes within each individual
295 // paragraph (do not consider end-of-par)
296 for (pit_type pit = 0; pit < pars_size; ++pit) {
297 // prevent assertion failure
298 if (!pars[pit].empty()) {
299 if (op == Text::ACCEPT)
300 pars[pit].acceptChanges(0, pars[pit].size());
302 pars[pit].rejectChanges(0, pars[pit].size());
306 // next, accept or reject imaginary end-of-par characters
307 for (pit_type pit = 0; pit < pars_size; ++pit) {
308 pos_type pos = pars[pit].size();
309 if (pars[pit].isChanged(pos)) {
310 // keep the end-of-par char if it is inserted and accepted
311 // or when it is deleted and rejected.
312 if (pars[pit].isInserted(pos) == (op == Text::ACCEPT)) {
313 pars[pit].setChange(pos, Change(Change::UNCHANGED));
315 if (pit == pars_size - 1) {
316 // we cannot remove a par break at the end of the last
317 // paragraph; instead, we mark it unchanged
318 pars[pit].setChange(pos, Change(Change::UNCHANGED));
320 mergeParagraph(bparams, pars, pit);
330 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
332 acceptOrRejectChanges(pars, bparams, Text::ACCEPT);
336 void rejectChanges(ParagraphList & pars, BufferParams const & bparams)
338 acceptOrRejectChanges(pars, bparams, Text::REJECT);
342 InsetText const & Text::inset() const
349 void Text::readParToken(Paragraph & par, Lexer & lex,
350 string const & token, Font & font, Change & change, ErrorList & errorList)
352 Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
353 BufferParams const & bp = buf->params();
355 if (token[0] != '\\') {
356 docstring dstr = lex.getDocString();
357 par.appendString(dstr, font, change);
359 } else if (token == "\\begin_layout") {
361 docstring layoutname = lex.getDocString();
363 font = Font(inherit_font, bp.language);
364 change = Change(Change::UNCHANGED);
366 DocumentClass const & tclass = bp.documentClass();
368 if (layoutname.empty())
369 layoutname = tclass.defaultLayoutName();
371 if (owner_->forcePlainLayout()) {
372 // in this case only the empty layout is allowed
373 layoutname = tclass.plainLayoutName();
374 } else if (par.usePlainLayout()) {
375 // in this case, default layout maps to empty layout
376 if (layoutname == tclass.defaultLayoutName())
377 layoutname = tclass.plainLayoutName();
379 // otherwise, the empty layout maps to the default
380 if (layoutname == tclass.plainLayoutName())
381 layoutname = tclass.defaultLayoutName();
384 // When we apply an unknown layout to a document, we add this layout to the textclass
385 // of this document. For example, when you apply class article to a beamer document,
386 // all unknown layouts such as frame will be added to document class article so that
387 // these layouts can keep their original names.
388 bool const added_one = tclass.addLayoutIfNeeded(layoutname);
391 docstring const s = bformat(_("Layout `%1$s' was not found."), layoutname);
393 ErrorItem(_("Layout Not Found"), s, par.id(), 0, par.size()));
396 par.setLayout(bp.documentClass()[layoutname]);
398 // Test whether the layout is obsolete.
399 Layout const & layout = par.layout();
400 if (!layout.obsoleted_by().empty())
401 par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
403 par.params().read(lex);
405 } else if (token == "\\end_layout") {
406 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
407 << "Missing \\begin_layout ?");
408 } else if (token == "\\end_inset") {
409 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
410 << "Missing \\begin_inset ?");
411 } else if (token == "\\begin_inset") {
412 Inset * inset = readInset(lex, buf);
414 par.insertInset(par.size(), inset, font, change);
417 docstring line = lex.getDocString();
418 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
419 par.id(), 0, par.size()));
421 } else if (token == "\\family") {
423 setLyXFamily(lex.getString(), font.fontInfo());
424 } else if (token == "\\series") {
426 setLyXSeries(lex.getString(), font.fontInfo());
427 } else if (token == "\\shape") {
429 setLyXShape(lex.getString(), font.fontInfo());
430 } else if (token == "\\size") {
432 setLyXSize(lex.getString(), font.fontInfo());
433 } else if (token == "\\lang") {
435 string const tok = lex.getString();
436 Language const * lang = languages.getLanguage(tok);
438 font.setLanguage(lang);
440 font.setLanguage(bp.language);
441 lex.printError("Unknown language `$$Token'");
443 } else if (token == "\\numeric") {
445 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
446 } else if (token == "\\emph") {
448 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
449 } else if (token == "\\bar") {
451 string const tok = lex.getString();
454 font.fontInfo().setUnderbar(FONT_ON);
455 else if (tok == "no")
456 font.fontInfo().setUnderbar(FONT_OFF);
457 else if (tok == "default")
458 font.fontInfo().setUnderbar(FONT_INHERIT);
460 lex.printError("Unknown bar font flag "
462 } else if (token == "\\strikeout") {
464 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
465 } else if (token == "\\uuline") {
467 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
468 } else if (token == "\\uwave") {
470 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
471 } else if (token == "\\noun") {
473 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
474 } else if (token == "\\color") {
476 setLyXColor(lex.getString(), font.fontInfo());
477 } else if (token == "\\SpecialChar") {
478 auto_ptr<Inset> inset;
479 inset.reset(new InsetSpecialChar);
481 inset->setBuffer(*buf);
482 par.insertInset(par.size(), inset.release(), font, change);
483 } else if (token == "\\IPAChar") {
484 auto_ptr<Inset> inset;
485 inset.reset(new InsetIPAChar);
487 inset->setBuffer(*buf);
488 par.insertInset(par.size(), inset.release(), font, change);
489 } else if (token == "\\backslash") {
490 par.appendChar('\\', font, change);
491 } else if (token == "\\LyXTable") {
492 auto_ptr<Inset> inset(new InsetTabular(buf));
494 par.insertInset(par.size(), inset.release(), font, change);
495 } else if (token == "\\change_unchanged") {
496 change = Change(Change::UNCHANGED);
497 } else if (token == "\\change_inserted" || token == "\\change_deleted") {
499 istringstream is(lex.getString());
503 BufferParams::AuthorMap const & am = bp.author_map;
504 if (am.find(aid) == am.end()) {
505 errorList.push_back(ErrorItem(_("Change tracking error"),
506 bformat(_("Unknown author index for change: %1$d\n"), aid),
507 par.id(), 0, par.size()));
508 change = Change(Change::UNCHANGED);
510 if (token == "\\change_inserted")
511 change = Change(Change::INSERTED, am.find(aid)->second, ct);
513 change = Change(Change::DELETED, am.find(aid)->second, ct);
517 errorList.push_back(ErrorItem(_("Unknown token"),
518 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
520 par.id(), 0, par.size()));
525 void Text::readParagraph(Paragraph & par, Lexer & lex,
526 ErrorList & errorList)
529 string token = lex.getString();
531 Change change(Change::UNCHANGED);
534 readParToken(par, lex, token, font, change, errorList);
537 token = lex.getString();
542 if (token == "\\end_layout") {
543 //Ok, paragraph finished
547 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
548 if (token == "\\begin_layout" || token == "\\end_document"
549 || token == "\\end_inset" || token == "\\begin_deeper"
550 || token == "\\end_deeper") {
551 lex.pushToken(token);
552 lyxerr << "Paragraph ended in line "
553 << lex.lineNumber() << "\n"
554 << "Missing \\end_layout.\n";
558 // Final change goes to paragraph break:
559 par.setChange(par.size(), change);
561 // Initialize begin_of_body_ on load; redoParagraph maintains
562 par.setBeginOfBody();
564 // mark paragraph for spell checking on load
565 // par.requestSpellCheck();
569 class TextCompletionList : public CompletionList
573 TextCompletionList(Cursor const & cur, WordList const * list)
574 : buffer_(cur.buffer()), pos_(0), list_(list)
577 virtual ~TextCompletionList() {}
580 virtual bool sorted() const { return true; }
582 virtual size_t size() const
584 return list_->size();
587 virtual docstring const & data(size_t idx) const
589 return list_->word(idx);
594 Buffer const * buffer_;
598 WordList const * list_;
602 bool Text::empty() const
604 return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
605 // FIXME: Should we consider the labeled type as empty too?
606 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
610 double Text::spacing(Paragraph const & par) const
612 if (par.params().spacing().isDefault())
613 return owner_->buffer().params().spacing().getValue();
614 return par.params().spacing().getValue();
619 * This breaks a paragraph at the specified position.
620 * The new paragraph will:
621 * - Decrease depth by one (or change layout to default layout) when
622 * keep_layout == false
623 * - keep current depth and layout when keep_layout == true
625 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
628 BufferParams const & bparams = text.inset().buffer().params();
629 ParagraphList & pars = text.paragraphs();
630 // create a new paragraph, and insert into the list
631 ParagraphList::iterator tmp =
632 pars.insert(boost::next(pars.begin(), par_offset + 1),
635 Paragraph & par = pars[par_offset];
637 // remember to set the inset_owner
638 tmp->setInsetOwner(&par.inInset());
639 // without doing that we get a crash when typing <Return> at the
640 // end of a paragraph
641 tmp->setPlainOrDefaultLayout(bparams.documentClass());
644 tmp->setLayout(par.layout());
645 tmp->setLabelWidthString(par.params().labelWidthString());
646 tmp->params().depth(par.params().depth());
647 } else if (par.params().depth() > 0) {
648 Paragraph const & hook = pars[text.outerHook(par_offset)];
649 tmp->setLayout(hook.layout());
650 // not sure the line below is useful
651 tmp->setLabelWidthString(par.params().labelWidthString());
652 tmp->params().depth(hook.params().depth());
655 bool const isempty = (par.allowEmpty() && par.empty());
657 if (!isempty && (par.size() > pos || par.empty())) {
658 tmp->setLayout(par.layout());
659 tmp->params().align(par.params().align());
660 tmp->setLabelWidthString(par.params().labelWidthString());
662 tmp->params().depth(par.params().depth());
663 tmp->params().noindent(par.params().noindent());
665 // move everything behind the break position
666 // to the new paragraph
668 /* Note: if !keepempty, empty() == true, then we reach
669 * here with size() == 0. So pos_end becomes - 1. This
670 * doesn't cause problems because both loops below
671 * enforce pos <= pos_end and 0 <= pos
673 pos_type pos_end = par.size() - 1;
675 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
676 if (moveItem(par, pos, *tmp, j, bparams)) {
682 // Move over the end-of-par change information
683 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
684 par.setChange(par.size(), Change(bparams.trackChanges ?
685 Change::INSERTED : Change::UNCHANGED));
688 // Make sure that we keep the language when
689 // breaking paragraph.
691 Font changed = tmp->getFirstFontSettings(bparams);
692 Font const & old = par.getFontSettings(bparams, par.size());
693 changed.setLanguage(old.language());
694 tmp->setFont(0, changed);
701 bool const soa = par.params().startOfAppendix();
702 par.params().clear();
703 // do not lose start of appendix marker (bug 4212)
704 par.params().startOfAppendix(soa);
705 par.setPlainOrDefaultLayout(bparams.documentClass());
709 par.setLayout(tmp->layout());
710 par.setLabelWidthString(tmp->params().labelWidthString());
711 par.params().depth(tmp->params().depth());
716 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
718 LASSERT(this == cur.text(), /**/);
720 Paragraph & cpar = cur.paragraph();
721 pit_type cpit = cur.pit();
723 DocumentClass const & tclass = cur.buffer()->params().documentClass();
724 Layout const & layout = cpar.layout();
726 if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
727 if (changeDepthAllowed(cur, DEC_DEPTH))
728 changeDepth(cur, DEC_DEPTH);
730 setLayout(cur, tclass.defaultLayoutName());
736 // Always break behind a space
737 // It is better to erase the space (Dekel)
738 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
739 cpar.eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
741 // What should the layout for the new paragraph be?
742 bool keep_layout = layout.isEnvironment()
743 || (layout.isParagraph() && layout.parbreak_is_newline);
745 keep_layout = !keep_layout;
747 // We need to remember this before we break the paragraph, because
748 // that invalidates the layout variable
749 bool sensitive = layout.labeltype == LABEL_SENSITIVE;
751 // we need to set this before we insert the paragraph.
752 bool const isempty = cpar.allowEmpty() && cpar.empty();
754 lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
756 // After this, neither paragraph contains any rows!
759 pit_type next_par = cpit + 1;
761 // well this is the caption hack since one caption is really enough
764 // set to standard-layout
765 //FIXME Check if this should be plainLayout() in some cases
766 pars_[cpit].applyLayout(tclass.defaultLayout());
768 // set to standard-layout
769 //FIXME Check if this should be plainLayout() in some cases
770 pars_[next_par].applyLayout(tclass.defaultLayout());
773 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
774 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().trackChanges))
775 break; // the character couldn't be deleted physically due to change tracking
778 // A singlePar update is not enough in this case.
779 cur.screenUpdateFlags(Update::Force);
780 cur.forceBufferUpdate();
782 // This check is necessary. Otherwise the new empty paragraph will
783 // be deleted automatically. And it is more friendly for the user!
784 if (cur.pos() != 0 || isempty)
785 setCursor(cur, cur.pit() + 1, 0);
787 setCursor(cur, cur.pit(), 0);
791 // needed to insert the selection
792 void Text::insertStringAsLines(Cursor & cur, docstring const & str,
795 BufferParams const & bparams = owner_->buffer().params();
796 pit_type pit = cur.pit();
797 pos_type pos = cur.pos();
799 // insert the string, don't insert doublespace
800 bool space_inserted = true;
801 for (docstring::const_iterator cit = str.begin();
802 cit != str.end(); ++cit) {
803 Paragraph & par = pars_[pit];
805 if (autoBreakRows_ && (!par.empty() || par.allowEmpty())) {
806 lyx::breakParagraph(*this, pit, pos,
807 par.layout().isEnvironment());
810 space_inserted = true;
814 // do not insert consecutive spaces if !free_spacing
815 } else if ((*cit == ' ' || *cit == '\t') &&
816 space_inserted && !par.isFreeSpacing()) {
818 } else if (*cit == '\t') {
819 if (!par.isFreeSpacing()) {
820 // tabs are like spaces here
821 par.insertChar(pos, ' ', font, bparams.trackChanges);
823 space_inserted = true;
825 par.insertChar(pos, *cit, font, bparams.trackChanges);
827 space_inserted = true;
829 } else if (!isPrintable(*cit)) {
830 // Ignore unprintables
833 // just insert the character
834 par.insertChar(pos, *cit, font, bparams.trackChanges);
836 space_inserted = (*cit == ' ');
839 setCursor(cur, pit, pos);
843 // turn double CR to single CR, others are converted into one
844 // blank. Then insertStringAsLines is called
845 void Text::insertStringAsParagraphs(Cursor & cur, docstring const & str,
848 docstring linestr = str;
849 bool newline_inserted = false;
851 for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
852 if (linestr[i] == '\n') {
853 if (newline_inserted) {
854 // we know that \r will be ignored by
855 // insertStringAsLines. Of course, it is a dirty
856 // trick, but it works...
857 linestr[i - 1] = '\r';
861 newline_inserted = true;
863 } else if (isPrintable(linestr[i])) {
864 newline_inserted = false;
867 insertStringAsLines(cur, linestr, font);
871 // insert a character, moves all the following breaks in the
872 // same Paragraph one to the right and make a rebreak
873 void Text::insertChar(Cursor & cur, char_type c)
875 LASSERT(this == cur.text(), /**/);
877 cur.recordUndo(INSERT_UNDO);
879 TextMetrics const & tm = cur.bv().textMetrics(this);
880 Buffer const & buffer = *cur.buffer();
881 Paragraph & par = cur.paragraph();
882 // try to remove this
883 pit_type const pit = cur.pit();
885 bool const freeSpacing = par.layout().free_spacing ||
888 if (lyxrc.auto_number) {
889 static docstring const number_operators = from_ascii("+-/*");
890 static docstring const number_unary_operators = from_ascii("+-");
891 static docstring const number_seperators = from_ascii(".,:");
893 if (cur.current_font.fontInfo().number() == FONT_ON) {
894 if (!isDigitASCII(c) && !contains(number_operators, c) &&
895 !(contains(number_seperators, c) &&
897 cur.pos() != cur.lastpos() &&
898 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
899 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
901 number(cur); // Set current_font.number to OFF
902 } else if (isDigitASCII(c) &&
903 cur.real_current_font.isVisibleRightToLeft()) {
904 number(cur); // Set current_font.number to ON
906 if (cur.pos() != 0) {
907 char_type const c = par.getChar(cur.pos() - 1);
908 if (contains(number_unary_operators, c) &&
910 || par.isSeparator(cur.pos() - 2)
911 || par.isNewline(cur.pos() - 2))
913 setCharFont(pit, cur.pos() - 1, cur.current_font,
915 } else if (contains(number_seperators, c)
917 && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
918 setCharFont(pit, cur.pos() - 1, cur.current_font,
925 // In Bidi text, we want spaces to be treated in a special way: spaces
926 // which are between words in different languages should get the
927 // paragraph's language; otherwise, spaces should keep the language
928 // they were originally typed in. This is only in effect while typing;
929 // after the text is already typed in, the user can always go back and
930 // explicitly set the language of a space as desired. But 99.9% of the
931 // time, what we're doing here is what the user actually meant.
933 // The following cases are the ones in which the language of the space
934 // should be changed to match that of the containing paragraph. In the
935 // depictions, lowercase is LTR, uppercase is RTL, underscore (_)
936 // represents a space, pipe (|) represents the cursor position (so the
937 // character before it is the one just typed in). The different cases
938 // are depicted logically (not visually), from left to right:
943 // Theoretically, there are other situations that we should, perhaps, deal
944 // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any
945 // point (to understand why, just try to create this situation...).
947 if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
948 // get font in front and behind the space in question. But do NOT
949 // use getFont(cur.pos()) because the character c is not inserted yet
950 Font const pre_space_font = tm.displayFont(cur.pit(), cur.pos() - 2);
951 Font const & post_space_font = cur.real_current_font;
952 bool pre_space_rtl = pre_space_font.isVisibleRightToLeft();
953 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
955 if (pre_space_rtl != post_space_rtl) {
956 // Set the space's language to match the language of the
957 // adjacent character whose direction is the paragraph's
958 // direction; don't touch other properties of the font
959 Language const * lang =
960 (pre_space_rtl == par.isRTL(buffer.params())) ?
961 pre_space_font.language() : post_space_font.language();
963 Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
964 space_font.setLanguage(lang);
965 par.setFont(cur.pos() - 1, space_font);
969 // Next check, if there will be two blanks together or a blank at
970 // the beginning of a paragraph.
971 // I decided to handle blanks like normal characters, the main
972 // difference are the special checks when calculating the row.fill
973 // (blank does not count at the end of a row) and the check here
975 // When the free-spacing option is set for the current layout,
976 // disable the double-space checking
977 if (!freeSpacing && isLineSeparatorChar(c)) {
978 if (cur.pos() == 0) {
980 "You cannot insert a space at the "
981 "beginning of a paragraph. Please read the Tutorial."));
984 LASSERT(cur.pos() > 0, /**/);
985 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
986 && !par.isDeleted(cur.pos() - 1)) {
988 "You cannot type two spaces this way. "
989 "Please read the Tutorial."));
994 par.insertChar(cur.pos(), c, cur.current_font,
995 cur.buffer()->params().trackChanges);
996 cur.checkBufferStructure();
998 // cur.screenUpdateFlags(Update::Force);
999 bool boundary = cur.boundary()
1000 || tm.isRTLBoundary(cur.pit(), cur.pos() + 1);
1001 setCursor(cur, cur.pit(), cur.pos() + 1, false, boundary);
1006 void Text::charInserted(Cursor & cur)
1008 Paragraph & par = cur.paragraph();
1010 // Here we call finishUndo for every 20 characters inserted.
1011 // This is from my experience how emacs does it. (Lgb)
1012 if (undo_counter_ < 20) {
1019 // register word if a non-letter was entered
1021 && !par.isWordSeparator(cur.pos() - 2)
1022 && par.isWordSeparator(cur.pos() - 1)) {
1023 // get the word in front of cursor
1024 LASSERT(this == cur.text(), /**/);
1025 cur.paragraph().updateWords();
1030 // the cursor set functions have a special mechanism. When they
1031 // realize, that you left an empty paragraph, they will delete it.
1033 bool Text::cursorForwardOneWord(Cursor & cur)
1035 LASSERT(this == cur.text(), /**/);
1037 pos_type const lastpos = cur.lastpos();
1038 pit_type pit = cur.pit();
1039 pos_type pos = cur.pos();
1040 Paragraph const & par = cur.paragraph();
1042 // Paragraph boundary is a word boundary
1043 if (pos == lastpos) {
1044 if (pit != cur.lastpit())
1045 return setCursor(cur, pit + 1, 0);
1050 if (lyxrc.mac_like_word_movement) {
1051 // Skip through trailing punctuation and spaces.
1052 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
1055 // Skip over either a non-char inset or a full word
1056 if (pos != lastpos && par.isWordSeparator(pos))
1058 else while (pos != lastpos && !par.isWordSeparator(pos))
1061 LASSERT(pos < lastpos, /**/); // see above
1062 if (!par.isWordSeparator(pos))
1063 while (pos != lastpos && !par.isWordSeparator(pos))
1065 else if (par.isChar(pos))
1066 while (pos != lastpos && par.isChar(pos))
1068 else if (!par.isSpace(pos)) // non-char inset
1071 // Skip over white space
1072 while (pos != lastpos && par.isSpace(pos))
1076 return setCursor(cur, pit, pos);
1080 bool Text::cursorBackwardOneWord(Cursor & cur)
1082 LASSERT(this == cur.text(), /**/);
1084 pit_type pit = cur.pit();
1085 pos_type pos = cur.pos();
1086 Paragraph & par = cur.paragraph();
1088 // Paragraph boundary is a word boundary
1089 if (pos == 0 && pit != 0)
1090 return setCursor(cur, pit - 1, getPar(pit - 1).size());
1092 if (lyxrc.mac_like_word_movement) {
1093 // Skip through punctuation and spaces.
1094 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1097 // Skip over either a non-char inset or a full word
1098 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1100 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1103 // Skip over white space
1104 while (pos != 0 && par.isSpace(pos - 1))
1107 if (pos != 0 && !par.isWordSeparator(pos - 1))
1108 while (pos != 0 && !par.isWordSeparator(pos - 1))
1110 else if (pos != 0 && par.isChar(pos - 1))
1111 while (pos != 0 && par.isChar(pos - 1))
1113 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1117 return setCursor(cur, pit, pos);
1121 bool Text::cursorVisLeftOneWord(Cursor & cur)
1123 LASSERT(this == cur.text(), /**/);
1125 pos_type left_pos, right_pos;
1126 bool left_is_letter, right_is_letter;
1128 Cursor temp_cur = cur;
1130 // always try to move at least once...
1131 while (temp_cur.posVisLeft(true /* skip_inset */)) {
1133 // collect some information about current cursor position
1134 temp_cur.getSurroundingPos(left_pos, right_pos);
1136 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1138 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1140 // if we're not at a letter/non-letter boundary, continue moving
1141 if (left_is_letter == right_is_letter)
1144 // we should stop when we have an LTR word on our right or an RTL word
1146 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1147 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1148 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1149 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1153 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1154 true, temp_cur.boundary());
1158 bool Text::cursorVisRightOneWord(Cursor & cur)
1160 LASSERT(this == cur.text(), /**/);
1162 pos_type left_pos, right_pos;
1163 bool left_is_letter, right_is_letter;
1165 Cursor temp_cur = cur;
1167 // always try to move at least once...
1168 while (temp_cur.posVisRight(true /* skip_inset */)) {
1170 // collect some information about current cursor position
1171 temp_cur.getSurroundingPos(left_pos, right_pos);
1173 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1175 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1177 // if we're not at a letter/non-letter boundary, continue moving
1178 if (left_is_letter == right_is_letter)
1181 // we should stop when we have an LTR word on our right or an RTL word
1183 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1184 temp_cur.buffer()->params(),
1185 left_pos).isRightToLeft())
1186 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1187 temp_cur.buffer()->params(),
1188 right_pos).isRightToLeft()))
1192 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1193 true, temp_cur.boundary());
1197 void Text::selectWord(Cursor & cur, word_location loc)
1199 LASSERT(this == cur.text(), /**/);
1200 CursorSlice from = cur.top();
1201 CursorSlice to = cur.top();
1202 getWord(from, to, loc);
1203 if (cur.top() != from)
1204 setCursor(cur, from.pit(), from.pos());
1207 if (!cur.selection())
1209 setCursor(cur, to.pit(), to.pos());
1211 cur.setWordSelection(true);
1215 void Text::selectAll(Cursor & cur)
1217 LASSERT(this == cur.text(), /**/);
1218 if (cur.lastpos() == 0 && cur.lastpit() == 0)
1220 // If the cursor is at the beginning, make sure the cursor ends there
1221 if (cur.pit() == 0 && cur.pos() == 0) {
1222 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1224 setCursor(cur, 0, 0);
1226 setCursor(cur, 0, 0);
1228 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1234 // Select the word currently under the cursor when no
1235 // selection is currently set
1236 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1238 LASSERT(this == cur.text(), /**/);
1239 if (cur.selection())
1241 selectWord(cur, loc);
1242 return cur.selection();
1246 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1248 LASSERT(this == cur.text(), /**/);
1250 if (!cur.selection()) {
1251 bool const changed = cur.paragraph().isChanged(cur.pos());
1252 if (!(changed && findNextChange(&cur.bv())))
1256 cur.recordUndoSelection();
1258 pit_type begPit = cur.selectionBegin().pit();
1259 pit_type endPit = cur.selectionEnd().pit();
1261 pos_type begPos = cur.selectionBegin().pos();
1262 pos_type endPos = cur.selectionEnd().pos();
1264 // keep selection info, because endPos becomes invalid after the first loop
1265 bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1267 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1269 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1270 pos_type parSize = pars_[pit].size();
1272 // ignore empty paragraphs; otherwise, an assertion will fail for
1273 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1277 // do not consider first paragraph if the cursor starts at pos size()
1278 if (pit == begPit && begPos == parSize)
1281 // do not consider last paragraph if the cursor ends at pos 0
1282 if (pit == endPit && endPos == 0)
1283 break; // last iteration anyway
1285 pos_type left = (pit == begPit ? begPos : 0);
1286 pos_type right = (pit == endPit ? endPos : parSize);
1289 // there is no change here
1293 pars_[pit].acceptChanges(left, right);
1295 pars_[pit].rejectChanges(left, right);
1299 // next, accept/reject imaginary end-of-par characters
1301 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1302 pos_type pos = pars_[pit].size();
1304 // skip if the selection ends before the end-of-par
1305 if (pit == endPit && endsBeforeEndOfPar)
1306 break; // last iteration anyway
1308 // skip if this is not the last paragraph of the document
1309 // note: the user should be able to accept/reject the par break of the last par!
1310 if (pit == endPit && pit + 1 != int(pars_.size()))
1311 break; // last iteration anway
1314 if (pars_[pit].isInserted(pos)) {
1315 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1316 } else if (pars_[pit].isDeleted(pos)) {
1317 if (pit + 1 == int(pars_.size())) {
1318 // we cannot remove a par break at the end of the last paragraph;
1319 // instead, we mark it unchanged
1320 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1322 mergeParagraph(cur.buffer()->params(), pars_, pit);
1328 if (pars_[pit].isDeleted(pos)) {
1329 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1330 } else if (pars_[pit].isInserted(pos)) {
1331 if (pit + 1 == int(pars_.size())) {
1332 // we mark the par break at the end of the last paragraph unchanged
1333 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1335 mergeParagraph(cur.buffer()->params(), pars_, pit);
1343 // finally, invoke the DEPM
1345 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().trackChanges);
1350 cur.clearSelection();
1351 setCursorIntern(cur, begPit, begPos);
1352 cur.screenUpdateFlags(Update::Force);
1353 cur.forceBufferUpdate();
1357 void Text::acceptChanges()
1359 BufferParams const & bparams = owner_->buffer().params();
1360 lyx::acceptChanges(pars_, bparams);
1361 deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.trackChanges);
1365 void Text::rejectChanges()
1367 BufferParams const & bparams = owner_->buffer().params();
1368 pit_type pars_size = static_cast<pit_type>(pars_.size());
1370 // first, reject changes within each individual paragraph
1371 // (do not consider end-of-par)
1372 for (pit_type pit = 0; pit < pars_size; ++pit) {
1373 if (!pars_[pit].empty()) // prevent assertion failure
1374 pars_[pit].rejectChanges(0, pars_[pit].size());
1377 // next, reject imaginary end-of-par characters
1378 for (pit_type pit = 0; pit < pars_size; ++pit) {
1379 pos_type pos = pars_[pit].size();
1381 if (pars_[pit].isDeleted(pos)) {
1382 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1383 } else if (pars_[pit].isInserted(pos)) {
1384 if (pit == pars_size - 1) {
1385 // we mark the par break at the end of the last
1386 // paragraph unchanged
1387 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1389 mergeParagraph(bparams, pars_, pit);
1396 // finally, invoke the DEPM
1397 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
1401 void Text::deleteWordForward(Cursor & cur)
1403 LASSERT(this == cur.text(), /**/);
1404 if (cur.lastpos() == 0)
1408 cur.setSelection(true);
1409 cursorForwardOneWord(cur);
1411 cutSelection(cur, true, false);
1412 cur.checkBufferStructure();
1417 void Text::deleteWordBackward(Cursor & cur)
1419 LASSERT(this == cur.text(), /**/);
1420 if (cur.lastpos() == 0)
1421 cursorBackward(cur);
1424 cur.setSelection(true);
1425 cursorBackwardOneWord(cur);
1427 cutSelection(cur, true, false);
1428 cur.checkBufferStructure();
1433 // Kill to end of line.
1434 void Text::changeCase(Cursor & cur, TextCase action)
1436 LASSERT(this == cur.text(), /**/);
1440 bool gotsel = false;
1441 if (cur.selection()) {
1442 from = cur.selBegin();
1447 getWord(from, to, PARTIAL_WORD);
1448 cursorForwardOneWord(cur);
1451 cur.recordUndoSelection();
1453 pit_type begPit = from.pit();
1454 pit_type endPit = to.pit();
1456 pos_type begPos = from.pos();
1457 pos_type endPos = to.pos();
1459 pos_type right = 0; // needed after the for loop
1461 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1462 Paragraph & par = pars_[pit];
1463 pos_type const pos = (pit == begPit ? begPos : 0);
1464 right = (pit == endPit ? endPos : par.size());
1465 par.changeCase(cur.buffer()->params(), pos, right, action);
1468 // the selection may have changed due to logically-only deleted chars
1470 setCursor(cur, begPit, begPos);
1472 setCursor(cur, endPit, right);
1475 setCursor(cur, endPit, right);
1477 cur.checkBufferStructure();
1481 bool Text::handleBibitems(Cursor & cur)
1483 if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1489 BufferParams const & bufparams = cur.buffer()->params();
1490 Paragraph const & par = cur.paragraph();
1491 Cursor prevcur = cur;
1492 if (cur.pit() > 0) {
1494 prevcur.pos() = prevcur.lastpos();
1496 Paragraph const & prevpar = prevcur.paragraph();
1498 // if a bibitem is deleted, merge with previous paragraph
1499 // if this is a bibliography item as well
1500 if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1501 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1502 mergeParagraph(bufparams, cur.text()->paragraphs(),
1504 cur.forceBufferUpdate();
1505 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1506 cur.screenUpdateFlags(Update::Force);
1510 // otherwise reset to default
1511 cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1516 bool Text::erase(Cursor & cur)
1518 LASSERT(this == cur.text(), return false);
1519 bool needsUpdate = false;
1520 Paragraph & par = cur.paragraph();
1522 if (cur.pos() != cur.lastpos()) {
1523 // this is the code for a normal delete, not pasting
1525 cur.recordUndo(DELETE_UNDO);
1526 bool const was_inset = cur.paragraph().isInset(cur.pos());
1527 if(!par.eraseChar(cur.pos(), cur.buffer()->params().trackChanges))
1528 // the character has been logically deleted only => skip it
1529 cur.top().forwardPos();
1532 cur.forceBufferUpdate();
1534 cur.checkBufferStructure();
1537 if (cur.pit() == cur.lastpit())
1538 return dissolveInset(cur);
1540 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1541 par.setChange(cur.pos(), Change(Change::DELETED));
1545 setCursorIntern(cur, cur.pit() + 1, 0);
1546 needsUpdate = backspacePos0(cur);
1550 needsUpdate |= handleBibitems(cur);
1553 // Make sure the cursor is correct. Is this really needed?
1554 // No, not really... at least not here!
1555 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1556 cur.checkBufferStructure();
1563 bool Text::backspacePos0(Cursor & cur)
1565 LASSERT(this == cur.text(), /**/);
1569 bool needsUpdate = false;
1571 BufferParams const & bufparams = cur.buffer()->params();
1572 DocumentClass const & tclass = bufparams.documentClass();
1573 ParagraphList & plist = cur.text()->paragraphs();
1574 Paragraph const & par = cur.paragraph();
1575 Cursor prevcur = cur;
1577 prevcur.pos() = prevcur.lastpos();
1578 Paragraph const & prevpar = prevcur.paragraph();
1580 // is it an empty paragraph?
1581 if (cur.lastpos() == 0
1582 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1583 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1584 plist.erase(boost::next(plist.begin(), cur.pit()));
1587 // is previous par empty?
1588 else if (prevcur.lastpos() == 0
1589 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1590 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1591 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1594 // Pasting is not allowed, if the paragraphs have different
1595 // layouts. I think it is a real bug of all other
1596 // word processors to allow it. It confuses the user.
1597 // Correction: Pasting is always allowed with standard-layout
1598 // or the empty layout.
1599 else if (par.layout() == prevpar.layout()
1600 || tclass.isDefaultLayout(par.layout())
1601 || tclass.isPlainLayout(par.layout())) {
1602 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1603 mergeParagraph(bufparams, plist, prevcur.pit());
1608 cur.forceBufferUpdate();
1609 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1616 bool Text::backspace(Cursor & cur)
1618 LASSERT(this == cur.text(), /**/);
1619 bool needsUpdate = false;
1620 if (cur.pos() == 0) {
1622 return dissolveInset(cur);
1624 Paragraph & prev_par = pars_[cur.pit() - 1];
1626 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1627 prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1628 setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1631 // The cursor is at the beginning of a paragraph, so
1632 // the backspace will collapse two paragraphs into one.
1633 needsUpdate = backspacePos0(cur);
1636 // this is the code for a normal backspace, not pasting
1638 cur.recordUndo(DELETE_UNDO);
1639 // We used to do cursorBackwardIntern() here, but it is
1640 // not a good idea since it triggers the auto-delete
1641 // mechanism. So we do a cursorBackwardIntern()-lite,
1642 // without the dreaded mechanism. (JMarc)
1643 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1644 false, cur.boundary());
1645 bool const was_inset = cur.paragraph().isInset(cur.pos());
1646 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
1648 cur.forceBufferUpdate();
1650 cur.checkBufferStructure();
1653 if (cur.pos() == cur.lastpos())
1654 cur.setCurrentFont();
1656 needsUpdate |= handleBibitems(cur);
1658 // A singlePar update is not enough in this case.
1659 // cur.screenUpdateFlags(Update::Force);
1660 setCursor(cur.top(), cur.pit(), cur.pos());
1666 bool Text::dissolveInset(Cursor & cur)
1668 LASSERT(this == cur.text(), return false);
1670 if (isMainText() || cur.inset().nargs() != 1)
1673 cur.recordUndoInset();
1675 cur.selHandle(false);
1677 pos_type spos = cur.pos();
1678 pit_type spit = cur.pit();
1679 ParagraphList plist;
1680 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1681 plist = paragraphs();
1683 // store cursor offset
1687 Buffer & b = *cur.buffer();
1688 cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1690 if (!plist.empty()) {
1692 // we clear the cache so that we won't get conflicts with labels
1693 // that get pasted into the buffer. we should update this before
1694 // its being empty matters. if not (i.e., if we encounter bugs),
1695 // then this should instead be:
1696 // cur.buffer().updateBuffer();
1697 // but we'll try the cheaper solution here.
1698 cur.buffer()->clearReferenceCache();
1700 // ERT paragraphs have the Language latex_language.
1701 // This is invalid outside of ERT, so we need to
1702 // change it to the buffer language.
1703 ParagraphList::iterator it = plist.begin();
1704 ParagraphList::iterator it_end = plist.end();
1705 for (; it != it_end; ++it)
1706 it->changeLanguage(b.params(), latex_language, b.language());
1708 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1709 b.errorList("Paste"));
1711 cur.pit() = min(cur.lastpit(), spit);
1712 cur.pos() = min(cur.lastpos(), spos);
1715 cur.forceBufferUpdate();
1717 // Ensure the current language is set correctly (bug 6292)
1718 cur.text()->setCursor(cur, cur.pit(), cur.pos());
1719 cur.clearSelection();
1725 void Text::getWord(CursorSlice & from, CursorSlice & to,
1726 word_location const loc) const
1729 pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1733 void Text::write(ostream & os) const
1735 Buffer const & buf = owner_->buffer();
1736 ParagraphList::const_iterator pit = paragraphs().begin();
1737 ParagraphList::const_iterator end = paragraphs().end();
1739 for (; pit != end; ++pit)
1740 pit->write(os, buf.params(), dth);
1742 // Close begin_deeper
1743 for(; dth > 0; --dth)
1744 os << "\n\\end_deeper";
1748 bool Text::read(Lexer & lex,
1749 ErrorList & errorList, InsetText * insetPtr)
1751 Buffer const & buf = owner_->buffer();
1752 depth_type depth = 0;
1755 while (lex.isOK()) {
1757 string const token = lex.getString();
1762 if (token == "\\end_inset")
1765 if (token == "\\end_body")
1768 if (token == "\\begin_body")
1771 if (token == "\\end_document") {
1776 if (token == "\\begin_layout") {
1777 lex.pushToken(token);
1780 par.setInsetOwner(insetPtr);
1781 par.params().depth(depth);
1782 par.setFont(0, Font(inherit_font, buf.params().language));
1783 pars_.push_back(par);
1784 readParagraph(pars_.back(), lex, errorList);
1786 // register the words in the global word list
1787 pars_.back().updateWords();
1788 } else if (token == "\\begin_deeper") {
1790 } else if (token == "\\end_deeper") {
1792 lex.printError("\\end_deeper: " "depth is already null");
1796 LYXERR0("Handling unknown body token: `" << token << '\'');
1800 // avoid a crash on weird documents (bug 4859)
1801 if (pars_.empty()) {
1803 par.setInsetOwner(insetPtr);
1804 par.params().depth(depth);
1805 par.setFont(0, Font(inherit_font,
1806 buf.params().language));
1807 par.setPlainOrDefaultLayout(buf.params().documentClass());
1808 pars_.push_back(par);
1814 // Returns the current font and depth as a message.
1815 docstring Text::currentState(Cursor const & cur) const
1817 LASSERT(this == cur.text(), /**/);
1818 Buffer & buf = *cur.buffer();
1819 Paragraph const & par = cur.paragraph();
1820 odocstringstream os;
1822 if (buf.params().trackChanges)
1823 os << _("[Change Tracking] ");
1825 Change change = par.lookupChange(cur.pos());
1827 if (change.changed()) {
1828 Author const & a = buf.params().authors().get(change.author);
1829 os << _("Change: ") << a.name();
1830 if (!a.email().empty())
1831 os << " (" << a.email() << ")";
1832 // FIXME ctime is english, we should translate that
1833 os << _(" at ") << ctime(&change.changetime);
1837 // I think we should only show changes from the default
1839 // No, from the document font (MV)
1840 Font font = cur.real_current_font;
1841 font.fontInfo().reduce(buf.params().getFont().fontInfo());
1843 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1845 // The paragraph depth
1846 int depth = cur.paragraph().getDepth();
1848 os << bformat(_(", Depth: %1$d"), depth);
1850 // The paragraph spacing, but only if different from
1852 Spacing const & spacing = par.params().spacing();
1853 if (!spacing.isDefault()) {
1854 os << _(", Spacing: ");
1855 switch (spacing.getSpace()) {
1856 case Spacing::Single:
1859 case Spacing::Onehalf:
1862 case Spacing::Double:
1865 case Spacing::Other:
1866 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1868 case Spacing::Default:
1869 // should never happen, do nothing
1874 #ifdef DEVEL_VERSION
1875 os << _(", Inset: ") << &cur.inset();
1876 os << _(", Paragraph: ") << cur.pit();
1877 os << _(", Id: ") << par.id();
1878 os << _(", Position: ") << cur.pos();
1879 // FIXME: Why is the check for par.size() needed?
1880 // We are called with cur.pos() == par.size() quite often.
1881 if (!par.empty() && cur.pos() < par.size()) {
1882 // Force output of code point, not character
1883 size_t const c = par.getChar(cur.pos());
1884 os << _(", Char: 0x") << hex << c;
1886 os << _(", Boundary: ") << cur.boundary();
1887 // Row & row = cur.textRow();
1888 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1894 docstring Text::getPossibleLabel(Cursor const & cur) const
1896 pit_type pit = cur.pit();
1898 Layout const * layout = &(pars_[pit].layout());
1901 docstring par_text = pars_[pit].asString();
1903 // The return string of math matrices might contain linebreaks
1904 par_text = subst(par_text, '\n', '-');
1905 int const numwords = 3;
1906 for (int i = 0; i < numwords; ++i) {
1907 if (par_text.empty())
1910 par_text = split(par_text, head, ' ');
1911 // Is it legal to use spaces in labels ?
1917 // Make sure it isn't too long
1918 unsigned int const max_label_length = 32;
1919 if (text.size() > max_label_length)
1920 text.resize(max_label_length);
1922 // Will contain the label prefix.
1925 // For section, subsection, etc...
1926 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1927 Layout const * layout2 = &(pars_[pit - 1].layout());
1928 if (layout2->latextype != LATEX_PARAGRAPH) {
1933 if (layout->latextype != LATEX_PARAGRAPH)
1934 name = layout->refprefix;
1936 // For captions, we just take the caption type
1937 Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
1938 if (caption_inset) {
1939 string const & ftype = static_cast<InsetCaption *>(caption_inset)->type();
1940 FloatList const & fl = cur.buffer()->params().documentClass().floats();
1941 if (fl.typeExist(ftype)) {
1942 Floating const & flt = fl.getType(ftype);
1943 name = from_utf8(flt.refPrefix());
1946 name = from_utf8(ftype.substr(0,3));
1949 // If none of the above worked, see if the inset knows.
1951 InsetLayout const & il = cur.inset().getLayout();
1952 name = il.refprefix();
1956 text = name + ':' + text;
1962 docstring Text::asString(int options) const
1964 return asString(0, pars_.size(), options);
1968 docstring Text::asString(pit_type beg, pit_type end, int options) const
1970 size_t i = size_t(beg);
1971 docstring str = pars_[i].asString(options);
1972 for (++i; i != size_t(end); ++i) {
1974 str += pars_[i].asString(options);
1980 void Text::forToc(docstring & os, size_t maxlen, bool shorten) const
1983 maxlen = std::numeric_limits<std::size_t>::max();
1985 LASSERT(maxlen >= 8, maxlen = TOC_ENTRY_LENGTH);
1986 for (size_t i = 0; i != pars_.size() && os.length() < maxlen; ++i)
1987 pars_[i].forToc(os, maxlen);
1988 if (shorten && os.length() >= maxlen)
1989 os = os.substr(0, maxlen - 3) + from_ascii("...");
1993 void Text::charsTranspose(Cursor & cur)
1995 LASSERT(this == cur.text(), /**/);
1997 pos_type pos = cur.pos();
1999 // If cursor is at beginning or end of paragraph, do nothing.
2000 if (pos == cur.lastpos() || pos == 0)
2003 Paragraph & par = cur.paragraph();
2005 // Get the positions of the characters to be transposed.
2006 pos_type pos1 = pos - 1;
2007 pos_type pos2 = pos;
2009 // In change tracking mode, ignore deleted characters.
2010 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2012 if (pos2 == cur.lastpos())
2015 while (pos1 >= 0 && par.isDeleted(pos1))
2020 // Don't do anything if one of the "characters" is not regular text.
2021 if (par.isInset(pos1) || par.isInset(pos2))
2024 // Store the characters to be transposed (including font information).
2025 char_type const char1 = par.getChar(pos1);
2027 par.getFontSettings(cur.buffer()->params(), pos1);
2029 char_type const char2 = par.getChar(pos2);
2031 par.getFontSettings(cur.buffer()->params(), pos2);
2033 // And finally, we are ready to perform the transposition.
2034 // Track the changes if Change Tracking is enabled.
2035 bool const trackChanges = cur.buffer()->params().trackChanges;
2039 par.eraseChar(pos2, trackChanges);
2040 par.eraseChar(pos1, trackChanges);
2041 par.insertChar(pos1, char2, font2, trackChanges);
2042 par.insertChar(pos2, char1, font1, trackChanges);
2044 cur.checkBufferStructure();
2046 // After the transposition, move cursor to after the transposition.
2047 setCursor(cur, cur.pit(), pos2);
2052 DocIterator Text::macrocontextPosition() const
2054 return macrocontext_position_;
2058 void Text::setMacrocontextPosition(DocIterator const & pos)
2060 macrocontext_position_ = pos;
2064 docstring Text::previousWord(CursorSlice const & sl) const
2066 CursorSlice from = sl;
2067 CursorSlice to = sl;
2068 getWord(from, to, PREVIOUS_WORD);
2069 if (sl == from || to == from)
2072 Paragraph const & par = sl.paragraph();
2073 return par.asString(from.pos(), to.pos());
2077 bool Text::completionSupported(Cursor const & cur) const
2079 Paragraph const & par = cur.paragraph();
2080 return cur.pos() > 0
2081 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2082 && !par.isWordSeparator(cur.pos() - 1);
2086 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2088 WordList const * list = theWordList(*cur.getFont().language());
2089 return new TextCompletionList(cur, list);
2093 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2095 LASSERT(cur.bv().cursor() == cur, /**/);
2097 cur.bv().cursor() = cur;
2098 if (!(cur.result().screenUpdate() & Update::Force))
2099 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
2104 docstring Text::completionPrefix(Cursor const & cur) const
2106 return previousWord(cur.top());