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"
50 #include "WordLangTuple.h"
53 #include "insets/InsetText.h"
54 #include "insets/InsetBibitem.h"
55 #include "insets/InsetCaption.h"
56 #include "insets/InsetLine.h"
57 #include "insets/InsetNewline.h"
58 #include "insets/InsetNewpage.h"
59 #include "insets/InsetOptArg.h"
60 #include "insets/InsetSpace.h"
61 #include "insets/InsetSpecialChar.h"
62 #include "insets/InsetTabular.h"
64 #include "support/debug.h"
65 #include "support/docstream.h"
66 #include "support/gettext.h"
67 #include "support/lassert.h"
68 #include "support/lstrings.h"
69 #include "support/textutils.h"
71 #include <boost/next_prior.hpp>
76 using namespace lyx::support;
80 using cap::cutSelection;
81 using cap::pasteParagraphList;
83 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
84 Paragraph & toPar, pos_type toPos, BufferParams const & params)
86 // Note: moveItem() does not honour change tracking!
87 // Therefore, it should only be used for breaking and merging paragraphs
89 // We need a copy here because the character at fromPos is going to be erased.
90 Font const tmpFont = fromPar.getFontSettings(params, fromPos);
91 Change const tmpChange = fromPar.lookupChange(fromPos);
93 if (Inset * tmpInset = fromPar.getInset(fromPos)) {
94 fromPar.releaseInset(fromPos);
95 // The inset is not in fromPar any more.
96 if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
103 char_type const tmpChar = fromPar.getChar(fromPos);
104 fromPar.eraseChar(fromPos, false);
105 toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
110 void breakParagraphConservative(BufferParams const & bparams,
111 ParagraphList & pars, pit_type par_offset, pos_type pos)
113 // create a new paragraph
114 Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
116 Paragraph & par = pars[par_offset];
118 tmp.setInsetOwner(&par.inInset());
119 tmp.makeSameLayout(par);
121 LASSERT(pos <= par.size(), /**/);
123 if (pos < par.size()) {
124 // move everything behind the break position to the new paragraph
125 pos_type pos_end = par.size() - 1;
127 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
128 if (moveItem(par, pos, tmp, j, bparams)) {
132 // Move over the end-of-par change information
133 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
134 par.setChange(par.size(), Change(bparams.trackChanges ?
135 Change::INSERTED : Change::UNCHANGED));
140 void mergeParagraph(BufferParams const & bparams,
141 ParagraphList & pars, pit_type par_offset)
143 Paragraph & next = pars[par_offset + 1];
144 Paragraph & par = pars[par_offset];
146 pos_type pos_end = next.size() - 1;
147 pos_type pos_insert = par.size();
149 // the imaginary end-of-paragraph character (at par.size()) has to be
150 // marked as unmodified. Otherwise, its change is adopted by the first
151 // character of the next paragraph.
152 if (par.isChanged(par.size())) {
153 LYXERR(Debug::CHANGES,
154 "merging par with inserted/deleted end-of-par character");
155 par.setChange(par.size(), Change(Change::UNCHANGED));
158 Change change = next.lookupChange(next.size());
160 // move the content of the second paragraph to the end of the first one
161 for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
162 if (moveItem(next, 0, par, j, bparams)) {
167 // move the change of the end-of-paragraph character
168 par.setChange(par.size(), change);
170 pars.erase(boost::next(pars.begin(), par_offset + 1));
174 pit_type Text::depthHook(pit_type pit, depth_type depth) const
176 pit_type newpit = pit;
181 while (newpit != 0 && pars_[newpit].getDepth() > depth)
184 if (pars_[newpit].getDepth() > depth)
191 pit_type Text::outerHook(pit_type par_offset) const
193 Paragraph const & par = pars_[par_offset];
195 if (par.getDepth() == 0)
197 return depthHook(par_offset, depth_type(par.getDepth() - 1));
201 bool Text::isFirstInSequence(pit_type par_offset) const
203 Paragraph const & par = pars_[par_offset];
205 pit_type dhook_offset = depthHook(par_offset, par.getDepth());
207 if (dhook_offset == par_offset)
210 Paragraph const & dhook = pars_[dhook_offset];
212 return dhook.layout() != par.layout()
213 || dhook.getDepth() != par.getDepth();
217 Font const Text::outerFont(pit_type par_offset) const
219 depth_type par_depth = pars_[par_offset].getDepth();
220 FontInfo tmpfont = inherit_font;
222 // Resolve against environment font information
223 while (par_offset != pit_type(pars_.size())
225 && !tmpfont.resolved()) {
226 par_offset = outerHook(par_offset);
227 if (par_offset != pit_type(pars_.size())) {
228 tmpfont.realize(pars_[par_offset].layout().font);
229 par_depth = pars_[par_offset].getDepth();
233 return Font(tmpfont);
237 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
239 pit_type pars_size = static_cast<pit_type>(pars.size());
241 // first, accept changes within each individual paragraph
242 // (do not consider end-of-par)
243 for (pit_type pit = 0; pit < pars_size; ++pit) {
244 if (!pars[pit].empty()) // prevent assertion failure
245 pars[pit].acceptChanges(0, pars[pit].size());
248 // next, accept imaginary end-of-par characters
249 for (pit_type pit = 0; pit < pars_size; ++pit) {
250 pos_type pos = pars[pit].size();
252 if (pars[pit].isInserted(pos)) {
253 pars[pit].setChange(pos, Change(Change::UNCHANGED));
254 } else if (pars[pit].isDeleted(pos)) {
255 if (pit == pars_size - 1) {
256 // we cannot remove a par break at the end of the last
257 // paragraph; instead, we mark it unchanged
258 pars[pit].setChange(pos, Change(Change::UNCHANGED));
260 mergeParagraph(bparams, pars, pit);
268 InsetText const & Text::inset() const
274 void Text::readParToken(Paragraph & par, Lexer & lex,
275 string const & token, Font & font, Change & change, ErrorList & errorList)
277 Buffer const & buf = owner_->buffer();
278 BufferParams const & bp = buf.params();
280 if (token[0] != '\\') {
281 docstring dstr = lex.getDocString();
282 par.appendString(dstr, font, change);
284 } else if (token == "\\begin_layout") {
286 docstring layoutname = lex.getDocString();
288 font = Font(inherit_font, bp.language);
289 change = Change(Change::UNCHANGED);
291 DocumentClass const & tclass = bp.documentClass();
293 if (layoutname.empty())
294 layoutname = tclass.defaultLayoutName();
296 if (owner_->forcePlainLayout()) {
297 // in this case only the empty layout is allowed
298 layoutname = tclass.plainLayoutName();
299 } else if (par.usePlainLayout()) {
300 // in this case, default layout maps to empty layout
301 if (layoutname == tclass.defaultLayoutName())
302 layoutname = tclass.plainLayoutName();
304 // otherwise, the empty layout maps to the default
305 if (layoutname == tclass.plainLayoutName())
306 layoutname = tclass.defaultLayoutName();
309 // When we apply an unknown layout to a document, we add this layout to the textclass
310 // of this document. For example, when you apply class article to a beamer document,
311 // all unknown layouts such as frame will be added to document class article so that
312 // these layouts can keep their original names.
313 tclass.addLayoutIfNeeded(layoutname);
315 par.setLayout(bp.documentClass()[layoutname]);
317 // Test whether the layout is obsolete.
318 Layout const & layout = par.layout();
319 if (!layout.obsoleted_by().empty())
320 par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
322 par.params().read(lex);
324 } else if (token == "\\end_layout") {
325 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
326 << "Missing \\begin_layout ?");
327 } else if (token == "\\end_inset") {
328 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
329 << "Missing \\begin_inset ?");
330 } else if (token == "\\begin_inset") {
331 Inset * inset = readInset(lex, buf);
333 par.insertInset(par.size(), inset, font, change);
336 docstring line = lex.getDocString();
337 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
338 par.id(), 0, par.size()));
340 } else if (token == "\\family") {
342 setLyXFamily(lex.getString(), font.fontInfo());
343 } else if (token == "\\series") {
345 setLyXSeries(lex.getString(), font.fontInfo());
346 } else if (token == "\\shape") {
348 setLyXShape(lex.getString(), font.fontInfo());
349 } else if (token == "\\size") {
351 setLyXSize(lex.getString(), font.fontInfo());
352 } else if (token == "\\lang") {
354 string const tok = lex.getString();
355 Language const * lang = languages.getLanguage(tok);
357 font.setLanguage(lang);
359 font.setLanguage(bp.language);
360 lex.printError("Unknown language `$$Token'");
362 } else if (token == "\\numeric") {
364 font.fontInfo().setNumber(font.setLyXMisc(lex.getString()));
365 } else if (token == "\\emph") {
367 font.fontInfo().setEmph(font.setLyXMisc(lex.getString()));
368 } else if (token == "\\bar") {
370 string const tok = lex.getString();
373 font.fontInfo().setUnderbar(FONT_ON);
374 else if (tok == "no")
375 font.fontInfo().setUnderbar(FONT_OFF);
376 else if (tok == "default")
377 font.fontInfo().setUnderbar(FONT_INHERIT);
379 lex.printError("Unknown bar font flag "
381 } else if (token == "\\strikeout") {
383 font.fontInfo().setStrikeout(font.setLyXMisc(lex.getString()));
384 } else if (token == "\\uuline") {
386 font.fontInfo().setUuline(font.setLyXMisc(lex.getString()));
387 } else if (token == "\\uwave") {
389 font.fontInfo().setUwave(font.setLyXMisc(lex.getString()));
390 } else if (token == "\\noun") {
392 font.fontInfo().setNoun(font.setLyXMisc(lex.getString()));
393 } else if (token == "\\color") {
395 setLyXColor(lex.getString(), font.fontInfo());
396 } else if (token == "\\SpecialChar") {
397 auto_ptr<Inset> inset;
398 inset.reset(new InsetSpecialChar);
400 inset->setBuffer(const_cast<Buffer &>(buf));
401 par.insertInset(par.size(), inset.release(), font, change);
402 } else if (token == "\\backslash") {
403 par.appendChar('\\', font, change);
404 } else if (token == "\\LyXTable") {
405 auto_ptr<Inset> inset(new InsetTabular(const_cast<Buffer &>(buf)));
407 par.insertInset(par.size(), inset.release(), font, change);
408 } else if (token == "\\lyxline") {
409 auto_ptr<Inset> inset;
410 inset.reset(new InsetLine);
411 inset->setBuffer(const_cast<Buffer &>(buf));
412 par.insertInset(par.size(), inset.release(), font, change);
413 } else if (token == "\\change_unchanged") {
414 change = Change(Change::UNCHANGED);
415 } else if (token == "\\change_inserted" || token == "\\change_deleted") {
417 istringstream is(lex.getString());
421 map<unsigned int, int> const & am = bp.author_map;
422 if (am.find(aid) == am.end()) {
423 errorList.push_back(ErrorItem(_("Change tracking error"),
424 bformat(_("Unknown author index for change: %1$d\n"), aid),
425 par.id(), 0, par.size()));
426 change = Change(Change::UNCHANGED);
428 if (token == "\\change_inserted")
429 change = Change(Change::INSERTED, am.find(aid)->second, ct);
431 change = Change(Change::DELETED, am.find(aid)->second, ct);
435 errorList.push_back(ErrorItem(_("Unknown token"),
436 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
438 par.id(), 0, par.size()));
443 void Text::readParagraph(Paragraph & par, Lexer & lex,
444 ErrorList & errorList)
447 string token = lex.getString();
449 Change change(Change::UNCHANGED);
452 readParToken(par, lex, token, font, change, errorList);
455 token = lex.getString();
460 if (token == "\\end_layout") {
461 //Ok, paragraph finished
465 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
466 if (token == "\\begin_layout" || token == "\\end_document"
467 || token == "\\end_inset" || token == "\\begin_deeper"
468 || token == "\\end_deeper") {
469 lex.pushToken(token);
470 lyxerr << "Paragraph ended in line "
471 << lex.lineNumber() << "\n"
472 << "Missing \\end_layout.\n";
476 // Final change goes to paragraph break:
477 par.setChange(par.size(), change);
479 // Initialize begin_of_body_ on load; redoParagraph maintains
480 par.setBeginOfBody();
484 class TextCompletionList : public CompletionList
488 TextCompletionList(Cursor const & cur)
489 : buffer_(cur.buffer()), pos_(0)
492 virtual ~TextCompletionList() {}
495 virtual bool sorted() const { return true; }
497 virtual size_t size() const
499 return theWordList().size();
502 virtual docstring const & data(size_t idx) const
504 return theWordList().word(idx);
509 Buffer const * buffer_;
515 bool Text::empty() const
517 return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
518 // FIXME: Should we consider the labeled type as empty too?
519 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
523 double Text::spacing(Paragraph const & par) const
525 if (par.params().spacing().isDefault())
526 return owner_->buffer().params().spacing().getValue();
527 return par.params().spacing().getValue();
532 * This breaks a paragraph at the specified position.
533 * The new paragraph will:
534 * - Decrease depth by one (or change layout to default layout) when
535 * keep_layout == false
536 * - keep current depth and layout when keep_layout == true
538 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
541 BufferParams const & bparams = text.inset().buffer().params();
542 ParagraphList & pars = text.paragraphs();
543 // create a new paragraph, and insert into the list
544 ParagraphList::iterator tmp =
545 pars.insert(boost::next(pars.begin(), par_offset + 1),
548 Paragraph & par = pars[par_offset];
550 // remember to set the inset_owner
551 tmp->setInsetOwner(&par.inInset());
552 // without doing that we get a crash when typing <Return> at the
553 // end of a paragraph
554 tmp->setPlainOrDefaultLayout(bparams.documentClass());
556 // layout stays the same with latex-environments
558 tmp->setLayout(par.layout());
559 tmp->setLabelWidthString(par.params().labelWidthString());
560 tmp->params().depth(par.params().depth());
561 } else if (par.params().depth() > 0) {
562 Paragraph const & hook = pars[text.outerHook(par_offset)];
563 tmp->setLayout(hook.layout());
564 // not sure the line below is useful
565 tmp->setLabelWidthString(par.params().labelWidthString());
566 tmp->params().depth(hook.params().depth());
569 bool const isempty = (par.allowEmpty() && par.empty());
571 if (!isempty && (par.size() > pos || par.empty())) {
572 tmp->setLayout(par.layout());
573 tmp->params().align(par.params().align());
574 tmp->setLabelWidthString(par.params().labelWidthString());
576 tmp->params().depth(par.params().depth());
577 tmp->params().noindent(par.params().noindent());
579 // move everything behind the break position
580 // to the new paragraph
582 /* Note: if !keepempty, empty() == true, then we reach
583 * here with size() == 0. So pos_end becomes - 1. This
584 * doesn't cause problems because both loops below
585 * enforce pos <= pos_end and 0 <= pos
587 pos_type pos_end = par.size() - 1;
589 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
590 if (moveItem(par, pos, *tmp, j, bparams)) {
596 // Move over the end-of-par change information
597 tmp->setChange(tmp->size(), par.lookupChange(par.size()));
598 par.setChange(par.size(), Change(bparams.trackChanges ?
599 Change::INSERTED : Change::UNCHANGED));
602 // Make sure that we keep the language when
603 // breaking paragraph.
605 Font changed = tmp->getFirstFontSettings(bparams);
606 Font const & old = par.getFontSettings(bparams, par.size());
607 changed.setLanguage(old.language());
608 tmp->setFont(0, changed);
615 bool const soa = par.params().startOfAppendix();
616 par.params().clear();
617 // do not lose start of appendix marker (bug 4212)
618 par.params().startOfAppendix(soa);
619 par.setPlainOrDefaultLayout(bparams.documentClass());
622 // layout stays the same with latex-environments
624 par.setLayout(tmp->layout());
625 par.setLabelWidthString(tmp->params().labelWidthString());
626 par.params().depth(tmp->params().depth());
631 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
633 LASSERT(this == cur.text(), /**/);
635 Paragraph & cpar = cur.paragraph();
636 pit_type cpit = cur.pit();
638 DocumentClass const & tclass = cur.buffer()->params().documentClass();
639 Layout const & layout = cpar.layout();
641 // this is only allowed, if the current paragraph is not empty
642 // or caption and if it has not the keepempty flag active
643 if (cur.lastpos() == 0 && !cpar.allowEmpty() &&
644 layout.labeltype != LABEL_SENSITIVE)
647 // a layout change may affect also the following paragraph
648 recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
650 // Always break behind a space
651 // It is better to erase the space (Dekel)
652 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
653 cpar.eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
655 // What should the layout for the new paragraph be?
656 bool keep_layout = inverse_logic ?
657 !layout.isEnvironment()
658 : layout.isEnvironment();
660 // We need to remember this before we break the paragraph, because
661 // that invalidates the layout variable
662 bool sensitive = layout.labeltype == LABEL_SENSITIVE;
664 // we need to set this before we insert the paragraph.
665 bool const isempty = cpar.allowEmpty() && cpar.empty();
667 lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
669 // After this, neither paragraph contains any rows!
672 pit_type next_par = cpit + 1;
674 // well this is the caption hack since one caption is really enough
677 // set to standard-layout
678 //FIXME Check if this should be plainLayout() in some cases
679 pars_[cpit].applyLayout(tclass.defaultLayout());
681 // set to standard-layout
682 //FIXME Check if this should be plainLayout() in some cases
683 pars_[next_par].applyLayout(tclass.defaultLayout());
686 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
687 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().trackChanges))
688 break; // the character couldn't be deleted physically due to change tracking
691 cur.buffer()->updateLabels();
693 // A singlePar update is not enough in this case.
694 cur.updateFlags(Update::Force);
696 // This check is necessary. Otherwise the new empty paragraph will
697 // be deleted automatically. And it is more friendly for the user!
698 if (cur.pos() != 0 || isempty)
699 setCursor(cur, cur.pit() + 1, 0);
701 setCursor(cur, cur.pit(), 0);
705 // needed to insert the selection
706 void Text::insertStringAsLines(DocIterator const & dit, docstring const & str,
709 BufferParams const & bparams = owner_->buffer().params();
710 pit_type pit = dit.pit();
711 pos_type pos = dit.pos();
713 // insert the string, don't insert doublespace
714 bool space_inserted = true;
715 for (docstring::const_iterator cit = str.begin();
716 cit != str.end(); ++cit) {
717 Paragraph & par = pars_[pit];
719 if (autoBreakRows_ && (!par.empty() || par.allowEmpty())) {
720 lyx::breakParagraph(*this, pit, pos,
721 par.layout().isEnvironment());
724 space_inserted = true;
728 // do not insert consecutive spaces if !free_spacing
729 } else if ((*cit == ' ' || *cit == '\t') &&
730 space_inserted && !par.isFreeSpacing()) {
732 } else if (*cit == '\t') {
733 if (!par.isFreeSpacing()) {
734 // tabs are like spaces here
735 par.insertChar(pos, ' ', font, bparams.trackChanges);
737 space_inserted = true;
739 par.insertChar(pos, *cit, font, bparams.trackChanges);
741 space_inserted = true;
743 } else if (!isPrintable(*cit)) {
744 // Ignore unprintables
747 // just insert the character
748 par.insertChar(pos, *cit, font, bparams.trackChanges);
750 space_inserted = (*cit == ' ');
756 // turn double CR to single CR, others are converted into one
757 // blank. Then insertStringAsLines is called
758 void Text::insertStringAsParagraphs(DocIterator const & dit, docstring const & str,
761 docstring linestr = str;
762 bool newline_inserted = false;
764 for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
765 if (linestr[i] == '\n') {
766 if (newline_inserted) {
767 // we know that \r will be ignored by
768 // insertStringAsLines. Of course, it is a dirty
769 // trick, but it works...
770 linestr[i - 1] = '\r';
774 newline_inserted = true;
776 } else if (isPrintable(linestr[i])) {
777 newline_inserted = false;
780 insertStringAsLines(dit, linestr, font);
784 // insert a character, moves all the following breaks in the
785 // same Paragraph one to the right and make a rebreak
786 void Text::insertChar(Cursor & cur, char_type c)
788 LASSERT(this == cur.text(), /**/);
790 cur.recordUndo(INSERT_UNDO);
792 TextMetrics const & tm = cur.bv().textMetrics(this);
793 Buffer const & buffer = *cur.buffer();
794 Paragraph & par = cur.paragraph();
795 // try to remove this
796 pit_type const pit = cur.pit();
798 bool const freeSpacing = par.layout().free_spacing ||
801 if (lyxrc.auto_number) {
802 static docstring const number_operators = from_ascii("+-/*");
803 static docstring const number_unary_operators = from_ascii("+-");
804 static docstring const number_seperators = from_ascii(".,:");
806 if (cur.current_font.fontInfo().number() == FONT_ON) {
807 if (!isDigit(c) && !contains(number_operators, c) &&
808 !(contains(number_seperators, c) &&
810 cur.pos() != cur.lastpos() &&
811 tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
812 tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
814 number(cur); // Set current_font.number to OFF
815 } else if (isDigit(c) &&
816 cur.real_current_font.isVisibleRightToLeft()) {
817 number(cur); // Set current_font.number to ON
819 if (cur.pos() != 0) {
820 char_type const c = par.getChar(cur.pos() - 1);
821 if (contains(number_unary_operators, c) &&
823 || par.isSeparator(cur.pos() - 2)
824 || par.isNewline(cur.pos() - 2))
826 setCharFont(pit, cur.pos() - 1, cur.current_font,
828 } else if (contains(number_seperators, c)
830 && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
831 setCharFont(pit, cur.pos() - 1, cur.current_font,
838 // In Bidi text, we want spaces to be treated in a special way: spaces
839 // which are between words in different languages should get the
840 // paragraph's language; otherwise, spaces should keep the language
841 // they were originally typed in. This is only in effect while typing;
842 // after the text is already typed in, the user can always go back and
843 // explicitly set the language of a space as desired. But 99.9% of the
844 // time, what we're doing here is what the user actually meant.
846 // The following cases are the ones in which the language of the space
847 // should be changed to match that of the containing paragraph. In the
848 // depictions, lowercase is LTR, uppercase is RTL, underscore (_)
849 // represents a space, pipe (|) represents the cursor position (so the
850 // character before it is the one just typed in). The different cases
851 // are depicted logically (not visually), from left to right:
856 // Theoretically, there are other situations that we should, perhaps, deal
857 // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any
858 // point (to understand why, just try to create this situation...).
860 if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
861 // get font in front and behind the space in question. But do NOT
862 // use getFont(cur.pos()) because the character c is not inserted yet
863 Font const pre_space_font = tm.displayFont(cur.pit(), cur.pos() - 2);
864 Font const & post_space_font = cur.real_current_font;
865 bool pre_space_rtl = pre_space_font.isVisibleRightToLeft();
866 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
868 if (pre_space_rtl != post_space_rtl) {
869 // Set the space's language to match the language of the
870 // adjacent character whose direction is the paragraph's
871 // direction; don't touch other properties of the font
872 Language const * lang =
873 (pre_space_rtl == par.isRTL(buffer.params())) ?
874 pre_space_font.language() : post_space_font.language();
876 Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
877 space_font.setLanguage(lang);
878 par.setFont(cur.pos() - 1, space_font);
882 // Next check, if there will be two blanks together or a blank at
883 // the beginning of a paragraph.
884 // I decided to handle blanks like normal characters, the main
885 // difference are the special checks when calculating the row.fill
886 // (blank does not count at the end of a row) and the check here
888 // When the free-spacing option is set for the current layout,
889 // disable the double-space checking
890 if (!freeSpacing && isLineSeparatorChar(c)) {
891 if (cur.pos() == 0) {
892 static bool sent_space_message = false;
893 if (!sent_space_message) {
894 cur.message(_("You cannot insert a space at the "
895 "beginning of a paragraph. Please read the Tutorial."));
896 sent_space_message = true;
900 LASSERT(cur.pos() > 0, /**/);
901 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
902 && !par.isDeleted(cur.pos() - 1)) {
903 static bool sent_space_message = false;
904 if (!sent_space_message) {
905 cur.message(_("You cannot type two spaces this way. "
906 "Please read the Tutorial."));
907 sent_space_message = true;
913 par.insertChar(cur.pos(), c, cur.current_font,
914 cur.buffer()->params().trackChanges);
915 cur.checkBufferStructure();
917 // cur.updateFlags(Update::Force);
918 bool boundary = tm.isRTLBoundary(cur.pit(), cur.pos() + 1);
919 setCursor(cur, cur.pit(), cur.pos() + 1, false, boundary);
924 void Text::charInserted(Cursor & cur)
926 Paragraph & par = cur.paragraph();
928 // Here we call finishUndo for every 20 characters inserted.
929 // This is from my experience how emacs does it. (Lgb)
930 if (undo_counter_ < 20) {
937 // register word if a non-letter was entered
939 && !par.isWordSeparator(cur.pos() - 2)
940 && par.isWordSeparator(cur.pos() - 1)) {
941 // get the word in front of cursor
942 LASSERT(this == cur.text(), /**/);
943 cur.paragraph().updateWords();
948 // the cursor set functions have a special mechanism. When they
949 // realize, that you left an empty paragraph, they will delete it.
951 bool Text::cursorForwardOneWord(Cursor & cur)
953 LASSERT(this == cur.text(), /**/);
955 pos_type const lastpos = cur.lastpos();
956 pit_type pit = cur.pit();
957 pos_type pos = cur.pos();
958 Paragraph const & par = cur.paragraph();
960 // Paragraph boundary is a word boundary
961 if (pos == lastpos) {
962 if (pit != cur.lastpit())
963 return setCursor(cur, pit + 1, 0);
968 if (lyxrc.mac_like_word_movement) {
969 // Skip through trailing punctuation and spaces.
970 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
973 // Skip over either a non-char inset or a full word
974 if (pos != lastpos && par.isWordSeparator(pos))
976 else while (pos != lastpos && !par.isWordSeparator(pos))
979 LASSERT(pos < lastpos, /**/); // see above
980 if (!par.isWordSeparator(pos))
981 while (pos != lastpos && !par.isWordSeparator(pos))
983 else if (par.isChar(pos))
984 while (pos != lastpos && par.isChar(pos))
986 else if (!par.isSpace(pos)) // non-char inset
989 // Skip over white space
990 while (pos != lastpos && par.isSpace(pos))
994 return setCursor(cur, pit, pos);
998 bool Text::cursorBackwardOneWord(Cursor & cur)
1000 LASSERT(this == cur.text(), /**/);
1002 pit_type pit = cur.pit();
1003 pos_type pos = cur.pos();
1004 Paragraph & par = cur.paragraph();
1006 // Paragraph boundary is a word boundary
1007 if (pos == 0 && pit != 0)
1008 return setCursor(cur, pit - 1, getPar(pit - 1).size());
1010 if (lyxrc.mac_like_word_movement) {
1011 // Skip through punctuation and spaces.
1012 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1015 // Skip over either a non-char inset or a full word
1016 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1018 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1021 // Skip over white space
1022 while (pos != 0 && par.isSpace(pos - 1))
1025 if (pos != 0 && !par.isWordSeparator(pos - 1))
1026 while (pos != 0 && !par.isWordSeparator(pos - 1))
1028 else if (pos != 0 && par.isChar(pos - 1))
1029 while (pos != 0 && par.isChar(pos - 1))
1031 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1035 return setCursor(cur, pit, pos);
1039 bool Text::cursorVisLeftOneWord(Cursor & cur)
1041 LASSERT(this == cur.text(), /**/);
1043 pos_type left_pos, right_pos;
1044 bool left_is_letter, right_is_letter;
1046 Cursor temp_cur = cur;
1048 // always try to move at least once...
1049 while (temp_cur.posVisLeft(true /* skip_inset */)) {
1051 // collect some information about current cursor position
1052 temp_cur.getSurroundingPos(left_pos, right_pos);
1054 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1056 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1058 // if we're not at a letter/non-letter boundary, continue moving
1059 if (left_is_letter == right_is_letter)
1062 // we should stop when we have an LTR word on our right or an RTL word
1064 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1065 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1066 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1067 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1071 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1072 true, temp_cur.boundary());
1076 bool Text::cursorVisRightOneWord(Cursor & cur)
1078 LASSERT(this == cur.text(), /**/);
1080 pos_type left_pos, right_pos;
1081 bool left_is_letter, right_is_letter;
1083 Cursor temp_cur = cur;
1085 // always try to move at least once...
1086 while (temp_cur.posVisRight(true /* skip_inset */)) {
1088 // collect some information about current cursor position
1089 temp_cur.getSurroundingPos(left_pos, right_pos);
1091 (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1093 (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1095 // if we're not at a letter/non-letter boundary, continue moving
1096 if (left_is_letter == right_is_letter)
1099 // we should stop when we have an LTR word on our right or an RTL word
1101 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1102 temp_cur.buffer()->params(),
1103 left_pos).isRightToLeft())
1104 || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1105 temp_cur.buffer()->params(),
1106 right_pos).isRightToLeft()))
1110 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
1111 true, temp_cur.boundary());
1115 void Text::selectWord(Cursor & cur, word_location loc)
1117 LASSERT(this == cur.text(), /**/);
1118 CursorSlice from = cur.top();
1119 CursorSlice to = cur.top();
1120 getWord(from, to, loc);
1121 if (cur.top() != from)
1122 setCursor(cur, from.pit(), from.pos());
1126 setCursor(cur, to.pit(), to.pos());
1131 void Text::selectAll(Cursor & cur)
1133 LASSERT(this == cur.text(), /**/);
1134 if (cur.lastpos() == 0 && cur.lastpit() == 0)
1136 // If the cursor is at the beginning, make sure the cursor ends there
1137 if (cur.pit() == 0 && cur.pos() == 0) {
1138 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1140 setCursor(cur, 0, 0);
1142 setCursor(cur, 0, 0);
1144 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1150 // Select the word currently under the cursor when no
1151 // selection is currently set
1152 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1154 LASSERT(this == cur.text(), /**/);
1155 if (cur.selection())
1157 selectWord(cur, loc);
1158 return cur.selection();
1162 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1164 LASSERT(this == cur.text(), /**/);
1166 if (!cur.selection()) {
1167 bool const changed = cur.paragraph().isChanged(cur.pos());
1168 if (!(changed && findNextChange(&cur.bv())))
1172 cur.recordUndoSelection();
1174 pit_type begPit = cur.selectionBegin().pit();
1175 pit_type endPit = cur.selectionEnd().pit();
1177 pos_type begPos = cur.selectionBegin().pos();
1178 pos_type endPos = cur.selectionEnd().pos();
1180 // keep selection info, because endPos becomes invalid after the first loop
1181 bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1183 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1185 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1186 pos_type parSize = pars_[pit].size();
1188 // ignore empty paragraphs; otherwise, an assertion will fail for
1189 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1193 // do not consider first paragraph if the cursor starts at pos size()
1194 if (pit == begPit && begPos == parSize)
1197 // do not consider last paragraph if the cursor ends at pos 0
1198 if (pit == endPit && endPos == 0)
1199 break; // last iteration anyway
1201 pos_type left = (pit == begPit ? begPos : 0);
1202 pos_type right = (pit == endPit ? endPos : parSize);
1205 pars_[pit].acceptChanges(left, right);
1207 pars_[pit].rejectChanges(left, right);
1211 // next, accept/reject imaginary end-of-par characters
1213 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1214 pos_type pos = pars_[pit].size();
1216 // skip if the selection ends before the end-of-par
1217 if (pit == endPit && endsBeforeEndOfPar)
1218 break; // last iteration anyway
1220 // skip if this is not the last paragraph of the document
1221 // note: the user should be able to accept/reject the par break of the last par!
1222 if (pit == endPit && pit + 1 != int(pars_.size()))
1223 break; // last iteration anway
1226 if (pars_[pit].isInserted(pos)) {
1227 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1228 } else if (pars_[pit].isDeleted(pos)) {
1229 if (pit + 1 == int(pars_.size())) {
1230 // we cannot remove a par break at the end of the last paragraph;
1231 // instead, we mark it unchanged
1232 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1234 mergeParagraph(cur.buffer()->params(), pars_, pit);
1240 if (pars_[pit].isDeleted(pos)) {
1241 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1242 } else if (pars_[pit].isInserted(pos)) {
1243 if (pit + 1 == int(pars_.size())) {
1244 // we mark the par break at the end of the last paragraph unchanged
1245 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1247 mergeParagraph(cur.buffer()->params(), pars_, pit);
1255 // finally, invoke the DEPM
1257 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().trackChanges);
1262 cur.clearSelection();
1263 setCursorIntern(cur, begPit, begPos);
1264 cur.updateFlags(Update::Force);
1265 cur.buffer()->updateLabels();
1269 void Text::acceptChanges()
1271 BufferParams const & bparams = owner_->buffer().params();
1272 lyx::acceptChanges(pars_, bparams);
1273 deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.trackChanges);
1277 void Text::rejectChanges()
1279 BufferParams const & bparams = owner_->buffer().params();
1280 pit_type pars_size = static_cast<pit_type>(pars_.size());
1282 // first, reject changes within each individual paragraph
1283 // (do not consider end-of-par)
1284 for (pit_type pit = 0; pit < pars_size; ++pit) {
1285 if (!pars_[pit].empty()) // prevent assertion failure
1286 pars_[pit].rejectChanges(0, pars_[pit].size());
1289 // next, reject imaginary end-of-par characters
1290 for (pit_type pit = 0; pit < pars_size; ++pit) {
1291 pos_type pos = pars_[pit].size();
1293 if (pars_[pit].isDeleted(pos)) {
1294 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1295 } else if (pars_[pit].isInserted(pos)) {
1296 if (pit == pars_size - 1) {
1297 // we mark the par break at the end of the last
1298 // paragraph unchanged
1299 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1301 mergeParagraph(bparams, pars_, pit);
1308 // finally, invoke the DEPM
1309 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
1313 void Text::deleteWordForward(Cursor & cur)
1315 LASSERT(this == cur.text(), /**/);
1316 if (cur.lastpos() == 0)
1320 cur.setSelection(true);
1321 cursorForwardOneWord(cur);
1323 cutSelection(cur, true, false);
1324 cur.checkBufferStructure();
1329 void Text::deleteWordBackward(Cursor & cur)
1331 LASSERT(this == cur.text(), /**/);
1332 if (cur.lastpos() == 0)
1333 cursorBackward(cur);
1336 cur.setSelection(true);
1337 cursorBackwardOneWord(cur);
1339 cutSelection(cur, true, false);
1340 cur.checkBufferStructure();
1345 // Kill to end of line.
1346 void Text::changeCase(Cursor & cur, TextCase action)
1348 LASSERT(this == cur.text(), /**/);
1352 bool gotsel = false;
1353 if (cur.selection()) {
1354 from = cur.selBegin();
1359 getWord(from, to, PARTIAL_WORD);
1360 cursorForwardOneWord(cur);
1363 cur.recordUndoSelection();
1365 pit_type begPit = from.pit();
1366 pit_type endPit = to.pit();
1368 pos_type begPos = from.pos();
1369 pos_type endPos = to.pos();
1371 pos_type right = 0; // needed after the for loop
1373 for (pit_type pit = begPit; pit <= endPit; ++pit) {
1374 Paragraph & par = pars_[pit];
1375 pos_type const pos = (pit == begPit ? begPos : 0);
1376 right = (pit == endPit ? endPos : par.size());
1377 par.changeCase(cur.buffer()->params(), pos, right, action);
1380 // the selection may have changed due to logically-only deleted chars
1382 setCursor(cur, begPit, begPos);
1384 setCursor(cur, endPit, right);
1387 setCursor(cur, endPit, right);
1389 cur.checkBufferStructure();
1393 bool Text::handleBibitems(Cursor & cur)
1395 if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1401 BufferParams const & bufparams = cur.buffer()->params();
1402 Paragraph const & par = cur.paragraph();
1403 Cursor prevcur = cur;
1404 if (cur.pit() > 0) {
1406 prevcur.pos() = prevcur.lastpos();
1408 Paragraph const & prevpar = prevcur.paragraph();
1410 // if a bibitem is deleted, merge with previous paragraph
1411 // if this is a bibliography item as well
1412 if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1413 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1414 mergeParagraph(bufparams, cur.text()->paragraphs(),
1416 cur.buffer()->updateLabels();
1417 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1418 cur.updateFlags(Update::Force);
1422 // otherwise reset to default
1423 cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1428 bool Text::erase(Cursor & cur)
1430 LASSERT(this == cur.text(), return false);
1431 bool needsUpdate = false;
1432 Paragraph & par = cur.paragraph();
1434 if (cur.pos() != cur.lastpos()) {
1435 // this is the code for a normal delete, not pasting
1437 cur.recordUndo(DELETE_UNDO);
1438 bool const was_inset = cur.paragraph().isInset(cur.pos());
1439 if(!par.eraseChar(cur.pos(), cur.buffer()->params().trackChanges))
1440 // the character has been logically deleted only => skip it
1441 cur.top().forwardPos();
1444 cur.buffer()->updateLabels();
1446 cur.checkBufferStructure();
1449 if (cur.pit() == cur.lastpit())
1450 return dissolveInset(cur);
1452 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1453 par.setChange(cur.pos(), Change(Change::DELETED));
1457 setCursorIntern(cur, cur.pit() + 1, 0);
1458 needsUpdate = backspacePos0(cur);
1462 needsUpdate |= handleBibitems(cur);
1465 // Make sure the cursor is correct. Is this really needed?
1466 // No, not really... at least not here!
1467 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1468 cur.checkBufferStructure();
1475 bool Text::backspacePos0(Cursor & cur)
1477 LASSERT(this == cur.text(), /**/);
1481 bool needsUpdate = false;
1483 BufferParams const & bufparams = cur.buffer()->params();
1484 DocumentClass const & tclass = bufparams.documentClass();
1485 ParagraphList & plist = cur.text()->paragraphs();
1486 Paragraph const & par = cur.paragraph();
1487 Cursor prevcur = cur;
1489 prevcur.pos() = prevcur.lastpos();
1490 Paragraph const & prevpar = prevcur.paragraph();
1492 // is it an empty paragraph?
1493 if (cur.lastpos() == 0
1494 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1495 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1496 plist.erase(boost::next(plist.begin(), cur.pit()));
1499 // is previous par empty?
1500 else if (prevcur.lastpos() == 0
1501 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1502 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1503 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1506 // Pasting is not allowed, if the paragraphs have different
1507 // layouts. I think it is a real bug of all other
1508 // word processors to allow it. It confuses the user.
1509 // Correction: Pasting is always allowed with standard-layout
1510 // or the empty layout.
1511 else if (par.layout() == prevpar.layout()
1512 || tclass.isDefaultLayout(par.layout())
1513 || tclass.isPlainLayout(par.layout())) {
1514 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1515 mergeParagraph(bufparams, plist, prevcur.pit());
1520 cur.buffer()->updateLabels();
1521 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1528 bool Text::backspace(Cursor & cur)
1530 LASSERT(this == cur.text(), /**/);
1531 bool needsUpdate = false;
1532 if (cur.pos() == 0) {
1534 return dissolveInset(cur);
1536 Paragraph & prev_par = pars_[cur.pit() - 1];
1538 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1539 prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1540 setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1543 // The cursor is at the beginning of a paragraph, so
1544 // the backspace will collapse two paragraphs into one.
1545 needsUpdate = backspacePos0(cur);
1548 // this is the code for a normal backspace, not pasting
1550 cur.recordUndo(DELETE_UNDO);
1551 // We used to do cursorBackwardIntern() here, but it is
1552 // not a good idea since it triggers the auto-delete
1553 // mechanism. So we do a cursorBackwardIntern()-lite,
1554 // without the dreaded mechanism. (JMarc)
1555 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1556 false, cur.boundary());
1557 bool const was_inset = cur.paragraph().isInset(cur.pos());
1558 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
1560 cur.buffer()->updateLabels();
1562 cur.checkBufferStructure();
1565 if (cur.pos() == cur.lastpos())
1566 cur.setCurrentFont();
1568 needsUpdate |= handleBibitems(cur);
1570 // A singlePar update is not enough in this case.
1571 // cur.updateFlags(Update::Force);
1572 setCursor(cur.top(), cur.pit(), cur.pos());
1578 bool Text::dissolveInset(Cursor & cur)
1580 LASSERT(this == cur.text(), return false);
1582 if (isMainText() || cur.inset().nargs() != 1)
1585 cur.recordUndoInset();
1587 cur.selHandle(false);
1589 pos_type spos = cur.pos();
1590 pit_type spit = cur.pit();
1591 ParagraphList plist;
1592 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1593 plist = paragraphs();
1595 // store cursor offset
1599 Buffer & b = *cur.buffer();
1600 cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1601 if (!plist.empty()) {
1602 // ERT paragraphs have the Language latex_language.
1603 // This is invalid outside of ERT, so we need to
1604 // change it to the buffer language.
1605 ParagraphList::iterator it = plist.begin();
1606 ParagraphList::iterator it_end = plist.end();
1607 for (; it != it_end; it++)
1608 it->changeLanguage(b.params(), latex_language, b.language());
1610 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1611 b.errorList("Paste"));
1613 cur.pit() = min(cur.lastpit(), spit);
1614 cur.pos() = min(cur.lastpos(), spos);
1616 // this is the least that needs to be done (bug 6003)
1617 // in the above case, pasteParagraphList handles this
1618 cur.buffer()->updateLabels();
1619 cur.clearSelection();
1625 void Text::getWord(CursorSlice & from, CursorSlice & to,
1626 word_location const loc) const
1629 pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1633 void Text::write(ostream & os) const
1635 Buffer const & buf = owner_->buffer();
1636 ParagraphList::const_iterator pit = paragraphs().begin();
1637 ParagraphList::const_iterator end = paragraphs().end();
1639 for (; pit != end; ++pit)
1640 pit->write(os, buf.params(), dth);
1642 // Close begin_deeper
1643 for(; dth > 0; --dth)
1644 os << "\n\\end_deeper";
1648 bool Text::read(Lexer & lex,
1649 ErrorList & errorList, InsetText * insetPtr)
1651 Buffer const & buf = owner_->buffer();
1652 depth_type depth = 0;
1655 while (lex.isOK()) {
1657 string const token = lex.getString();
1662 if (token == "\\end_inset")
1665 if (token == "\\end_body")
1668 if (token == "\\begin_body")
1671 if (token == "\\end_document") {
1676 if (token == "\\begin_layout") {
1677 lex.pushToken(token);
1680 par.setInsetOwner(insetPtr);
1681 par.params().depth(depth);
1682 par.setFont(0, Font(inherit_font, buf.params().language));
1683 pars_.push_back(par);
1684 readParagraph(pars_.back(), lex, errorList);
1686 // register the words in the global word list
1687 pars_.back().updateWords();
1688 } else if (token == "\\begin_deeper") {
1690 } else if (token == "\\end_deeper") {
1692 lex.printError("\\end_deeper: " "depth is already null");
1696 LYXERR0("Handling unknown body token: `" << token << '\'');
1700 // avoid a crash on weird documents (bug 4859)
1701 if (pars_.empty()) {
1703 par.setInsetOwner(insetPtr);
1704 par.params().depth(depth);
1705 par.setFont(0, Font(inherit_font,
1706 buf.params().language));
1707 par.setPlainOrDefaultLayout(buf.params().documentClass());
1708 pars_.push_back(par);
1714 // Returns the current font and depth as a message.
1715 docstring Text::currentState(Cursor const & cur) const
1717 LASSERT(this == cur.text(), /**/);
1718 Buffer & buf = *cur.buffer();
1719 Paragraph const & par = cur.paragraph();
1720 odocstringstream os;
1722 if (buf.params().trackChanges)
1723 os << _("[Change Tracking] ");
1725 Change change = par.lookupChange(cur.pos());
1727 if (change.changed()) {
1728 Author const & a = buf.params().authors().get(change.author);
1729 os << _("Change: ") << a.name();
1730 if (!a.email().empty())
1731 os << " (" << a.email() << ")";
1732 // FIXME ctime is english, we should translate that
1733 os << _(" at ") << ctime(&change.changetime);
1737 // I think we should only show changes from the default
1739 // No, from the document font (MV)
1740 Font font = cur.real_current_font;
1741 font.fontInfo().reduce(buf.params().getFont().fontInfo());
1743 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1745 // The paragraph depth
1746 int depth = cur.paragraph().getDepth();
1748 os << bformat(_(", Depth: %1$d"), depth);
1750 // The paragraph spacing, but only if different from
1752 Spacing const & spacing = par.params().spacing();
1753 if (!spacing.isDefault()) {
1754 os << _(", Spacing: ");
1755 switch (spacing.getSpace()) {
1756 case Spacing::Single:
1759 case Spacing::Onehalf:
1762 case Spacing::Double:
1765 case Spacing::Other:
1766 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1768 case Spacing::Default:
1769 // should never happen, do nothing
1774 #ifdef DEVEL_VERSION
1775 os << _(", Inset: ") << &cur.inset();
1776 os << _(", Paragraph: ") << cur.pit();
1777 os << _(", Id: ") << par.id();
1778 os << _(", Position: ") << cur.pos();
1779 // FIXME: Why is the check for par.size() needed?
1780 // We are called with cur.pos() == par.size() quite often.
1781 if (!par.empty() && cur.pos() < par.size()) {
1782 // Force output of code point, not character
1783 size_t const c = par.getChar(cur.pos());
1784 os << _(", Char: 0x") << hex << c;
1786 os << _(", Boundary: ") << cur.boundary();
1787 // Row & row = cur.textRow();
1788 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1794 docstring Text::getPossibleLabel(Cursor const & cur) const
1796 pit_type pit = cur.pit();
1798 Layout const * layout = &(pars_[pit].layout());
1801 docstring par_text = pars_[pit].asString();
1803 // the return string of math matrices might contain linebreaks
1804 par_text = subst(par_text, '\n', '-');
1805 for (int i = 0; i < lyxrc.label_init_length; ++i) {
1806 if (par_text.empty())
1809 par_text = split(par_text, head, ' ');
1810 // Is it legal to use spaces in labels ?
1816 // No need for a prefix if the user said so.
1817 if (lyxrc.label_init_length <= 0)
1820 // Will contain the label type.
1823 // For section, subsection, etc...
1824 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1825 Layout const * layout2 = &(pars_[pit - 1].layout());
1826 if (layout2->latextype != LATEX_PARAGRAPH) {
1831 if (layout->latextype != LATEX_PARAGRAPH)
1832 name = from_ascii(layout->latexname());
1834 // for captions, we just take the caption type
1835 Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
1837 name = from_ascii(static_cast<InsetCaption *>(caption_inset)->type());
1839 // If none of the above worked, we'll see if we're inside various
1840 // types of insets and take our abbreviation from them.
1842 InsetCode const codes[] = {
1847 for (unsigned int i = 0; i < (sizeof codes / sizeof codes[0]); ++i) {
1848 Inset * float_inset = cur.innerInsetOfType(codes[i]);
1850 name = float_inset->name();
1856 // Create a correct prefix for prettyref
1857 if (name == "theorem")
1858 name = from_ascii("thm");
1859 else if (name == "Foot")
1860 name = from_ascii("fn");
1861 else if (name == "listing")
1862 name = from_ascii("lst");
1865 text = name.substr(0, 3) + ':' + text;
1871 docstring Text::asString(int options) const
1873 return asString(0, pars_.size(), options);
1877 docstring Text::asString(pit_type beg, pit_type end, int options) const
1879 size_t i = size_t(beg);
1880 docstring str = pars_[i].asString(options);
1881 for (++i; i != size_t(end); ++i) {
1883 str += pars_[i].asString(options);
1890 void Text::charsTranspose(Cursor & cur)
1892 LASSERT(this == cur.text(), /**/);
1894 pos_type pos = cur.pos();
1896 // If cursor is at beginning or end of paragraph, do nothing.
1897 if (pos == cur.lastpos() || pos == 0)
1900 Paragraph & par = cur.paragraph();
1902 // Get the positions of the characters to be transposed.
1903 pos_type pos1 = pos - 1;
1904 pos_type pos2 = pos;
1906 // In change tracking mode, ignore deleted characters.
1907 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
1909 if (pos2 == cur.lastpos())
1912 while (pos1 >= 0 && par.isDeleted(pos1))
1917 // Don't do anything if one of the "characters" is not regular text.
1918 if (par.isInset(pos1) || par.isInset(pos2))
1921 // Store the characters to be transposed (including font information).
1922 char_type const char1 = par.getChar(pos1);
1924 par.getFontSettings(cur.buffer()->params(), pos1);
1926 char_type const char2 = par.getChar(pos2);
1928 par.getFontSettings(cur.buffer()->params(), pos2);
1930 // And finally, we are ready to perform the transposition.
1931 // Track the changes if Change Tracking is enabled.
1932 bool const trackChanges = cur.buffer()->params().trackChanges;
1936 par.eraseChar(pos2, trackChanges);
1937 par.eraseChar(pos1, trackChanges);
1938 par.insertChar(pos1, char2, font2, trackChanges);
1939 par.insertChar(pos2, char1, font1, trackChanges);
1941 cur.checkBufferStructure();
1943 // After the transposition, move cursor to after the transposition.
1944 setCursor(cur, cur.pit(), pos2);
1949 DocIterator Text::macrocontextPosition() const
1951 return macrocontext_position_;
1955 void Text::setMacrocontextPosition(DocIterator const & pos)
1957 macrocontext_position_ = pos;
1961 docstring Text::previousWord(CursorSlice const & sl) const
1963 CursorSlice from = sl;
1964 CursorSlice to = sl;
1965 getWord(from, to, PREVIOUS_WORD);
1966 if (sl == from || to == from)
1969 Paragraph const & par = sl.paragraph();
1970 return par.asString(from.pos(), to.pos());
1974 bool Text::completionSupported(Cursor const & cur) const
1976 Paragraph const & par = cur.paragraph();
1977 return cur.pos() > 0
1978 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
1979 && !par.isWordSeparator(cur.pos() - 1);
1983 CompletionList const * Text::createCompletionList(Cursor const & cur) const
1985 return new TextCompletionList(cur);
1989 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
1991 LASSERT(cur.bv().cursor() == cur, /**/);
1993 cur.bv().cursor() = cur;
1994 if (!(cur.disp_.update() & Update::Force))
1995 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
2000 docstring Text::completionPrefix(Cursor const & cur) const
2002 return previousWord(cur.top());