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 Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Jürgen Vigna
14 * Full author contact details are available in file CREDITS.
23 #include "buffer_funcs.h"
24 #include "bufferparams.h"
25 #include "BufferView.h"
27 #include "pariterator.h"
28 #include "coordcache.h"
29 #include "CutAndPaste.h"
31 #include "dispatchresult.h"
33 #include "errorlist.h"
34 #include "funcrequest.h"
36 #include "FontIterator.h"
40 #include "lyxlength.h"
44 #include "metricsinfo.h"
45 #include "paragraph.h"
46 #include "paragraph_funcs.h"
47 #include "ParagraphParameters.h"
48 #include "rowpainter.h"
51 #include "WordLangTuple.h"
53 #include "frontends/FontMetrics.h"
54 #include "frontends/Painter.h"
56 #include "insets/insettext.h"
57 #include "insets/insetbibitem.h"
58 #include "insets/insethfill.h"
59 #include "insets/insetlatexaccent.h"
60 #include "insets/insetline.h"
61 #include "insets/insetnewline.h"
62 #include "insets/insetpagebreak.h"
63 #include "insets/insetoptarg.h"
64 #include "insets/insetspace.h"
65 #include "insets/insetspecialchar.h"
66 #include "insets/insettabular.h"
68 #include "support/lstrings.h"
69 #include "support/textutils.h"
70 #include "support/convert.h"
72 #include <boost/current_function.hpp>
79 using support::bformat;
80 using support::contains;
81 using support::lowercase;
83 using support::uppercase;
85 using cap::cutSelection;
86 using cap::pasteParagraphList;
88 using frontend::FontMetrics;
101 int numberOfSeparators(Paragraph const & par, Row const & row)
103 pos_type const first = max(row.pos(), par.beginOfBody());
104 pos_type const last = row.endpos() - 1;
106 for (pos_type p = first; p < last; ++p) {
107 if (par.isSeparator(p))
114 int numberOfLabelHfills(Paragraph const & par, Row const & row)
116 pos_type last = row.endpos() - 1;
117 pos_type first = row.pos();
119 // hfill *DO* count at the beginning of paragraphs!
121 while (first < last && par.isHfill(first))
125 last = min(last, par.beginOfBody());
127 for (pos_type p = first; p < last; ++p) {
135 int numberOfHfills(Paragraph const & par, Row const & row)
137 pos_type const last = row.endpos();
138 pos_type first = row.pos();
140 // hfill *DO* count at the beginning of paragraphs!
142 while (first < last && par.isHfill(first))
146 first = max(first, par.beginOfBody());
149 for (pos_type p = first; p < last; ++p) {
157 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
158 string const & token, LyXFont & font, Change & change, ErrorList & errorList)
160 BufferParams const & bp = buf.params();
162 if (token[0] != '\\') {
164 string::const_iterator cit = token.begin();
165 for (; cit != token.end(); ++cit)
166 par.insertChar(par.size(), (*cit), font, change);
168 docstring dstr = lex.getDocString();
169 docstring::const_iterator cit = dstr.begin();
170 docstring::const_iterator cend = dstr.end();
171 for (; cit != cend; ++cit)
172 par.insertChar(par.size(), *cit, font, change);
174 } else if (token == "\\begin_layout") {
176 string layoutname = lex.getString();
178 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
179 change = Change(Change::UNCHANGED);
181 LyXTextClass const & tclass = bp.getLyXTextClass();
183 if (layoutname.empty()) {
184 layoutname = tclass.defaultLayoutName();
187 bool hasLayout = tclass.hasLayout(layoutname);
190 errorList.push_back(ErrorItem(_("Unknown layout"),
191 bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
192 from_utf8(layoutname), from_utf8(tclass.name())), par.id(), 0, par.size()));
193 layoutname = tclass.defaultLayoutName();
196 par.layout(bp.getLyXTextClass()[layoutname]);
198 // Test whether the layout is obsolete.
199 LyXLayout_ptr const & layout = par.layout();
200 if (!layout->obsoleted_by().empty())
201 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
203 par.params().read(lex);
205 } else if (token == "\\end_layout") {
206 lyxerr << BOOST_CURRENT_FUNCTION
207 << ": Solitary \\end_layout in line "
208 << lex.getLineNo() << "\n"
209 << "Missing \\begin_layout?.\n";
210 } else if (token == "\\end_inset") {
211 lyxerr << BOOST_CURRENT_FUNCTION
212 << ": Solitary \\end_inset in line "
213 << lex.getLineNo() << "\n"
214 << "Missing \\begin_inset?.\n";
215 } else if (token == "\\begin_inset") {
216 InsetBase * inset = readInset(lex, buf);
218 par.insertInset(par.size(), inset, font, change);
221 docstring line = lex.getDocString();
222 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
223 par.id(), 0, par.size()));
225 } else if (token == "\\family") {
227 font.setLyXFamily(lex.getString());
228 } else if (token == "\\series") {
230 font.setLyXSeries(lex.getString());
231 } else if (token == "\\shape") {
233 font.setLyXShape(lex.getString());
234 } else if (token == "\\size") {
236 font.setLyXSize(lex.getString());
237 } else if (token == "\\lang") {
239 string const tok = lex.getString();
240 Language const * lang = languages.getLanguage(tok);
242 font.setLanguage(lang);
244 font.setLanguage(bp.language);
245 lex.printError("Unknown language `$$Token'");
247 } else if (token == "\\numeric") {
249 font.setNumber(font.setLyXMisc(lex.getString()));
250 } else if (token == "\\emph") {
252 font.setEmph(font.setLyXMisc(lex.getString()));
253 } else if (token == "\\bar") {
255 string const tok = lex.getString();
258 font.setUnderbar(LyXFont::ON);
259 else if (tok == "no")
260 font.setUnderbar(LyXFont::OFF);
261 else if (tok == "default")
262 font.setUnderbar(LyXFont::INHERIT);
264 lex.printError("Unknown bar font flag "
266 } else if (token == "\\noun") {
268 font.setNoun(font.setLyXMisc(lex.getString()));
269 } else if (token == "\\color") {
271 font.setLyXColor(lex.getString());
272 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
274 // Insets don't make sense in a free-spacing context! ---Kayvan
275 if (par.isFreeSpacing()) {
276 if (token == "\\InsetSpace")
277 par.insertChar(par.size(), ' ', font, change);
278 else if (lex.isOK()) {
280 string const next_token = lex.getString();
281 if (next_token == "\\-")
282 par.insertChar(par.size(), '-', font, change);
284 lex.printError("Token `$$Token' "
286 "paragraph layout!");
290 auto_ptr<InsetBase> inset;
291 if (token == "\\SpecialChar" )
292 inset.reset(new InsetSpecialChar);
294 inset.reset(new InsetSpace);
295 inset->read(buf, lex);
296 par.insertInset(par.size(), inset.release(),
299 } else if (token == "\\i") {
300 auto_ptr<InsetBase> inset(new InsetLatexAccent);
301 inset->read(buf, lex);
302 par.insertInset(par.size(), inset.release(), font, change);
303 } else if (token == "\\backslash") {
304 par.insertChar(par.size(), '\\', font, change);
305 } else if (token == "\\newline") {
306 auto_ptr<InsetBase> inset(new InsetNewline);
307 inset->read(buf, lex);
308 par.insertInset(par.size(), inset.release(), font, change);
309 } else if (token == "\\LyXTable") {
310 auto_ptr<InsetBase> inset(new InsetTabular(buf));
311 inset->read(buf, lex);
312 par.insertInset(par.size(), inset.release(), font, change);
313 } else if (token == "\\hfill") {
314 par.insertInset(par.size(), new InsetHFill, font, change);
315 } else if (token == "\\lyxline") {
316 par.insertInset(par.size(), new InsetLine, font, change);
317 } else if (token == "\\newpage") {
318 par.insertInset(par.size(), new InsetPagebreak, font, change);
319 } else if (token == "\\clearpage") {
320 par.insertInset(par.size(), new InsetClearPage, font, change);
321 } else if (token == "\\cleardoublepage") {
322 par.insertInset(par.size(), new InsetClearDoublePage, font, change);
323 } else if (token == "\\change_unchanged") {
324 change = Change(Change::UNCHANGED);
325 } else if (token == "\\change_inserted") {
327 std::istringstream is(lex.getString());
331 if (aid >= bp.author_map.size()) {
332 errorList.push_back(ErrorItem(_("Change tracking error"),
333 bformat(_("Unknown author index for insertion: %1$d\n"), aid),
334 par.id(), 0, par.size()));
335 change = Change(Change::UNCHANGED);
337 change = Change(Change::INSERTED, bp.author_map[aid], ct);
338 } else if (token == "\\change_deleted") {
340 std::istringstream is(lex.getString());
344 if (aid >= bp.author_map.size()) {
345 errorList.push_back(ErrorItem(_("Change tracking error"),
346 bformat(_("Unknown author index for deletion: %1$d\n"), aid),
347 par.id(), 0, par.size()));
348 change = Change(Change::UNCHANGED);
350 change = Change(Change::DELETED, bp.author_map[aid], ct);
353 errorList.push_back(ErrorItem(_("Unknown token"),
354 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
356 par.id(), 0, par.size()));
361 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex,
362 ErrorList & errorList)
365 string token = lex.getString();
367 Change change(Change::UNCHANGED);
370 readParToken(buf, par, lex, token, font, change, errorList);
373 token = lex.getString();
378 if (token == "\\end_layout") {
379 //Ok, paragraph finished
383 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
384 << token << '\'' << endl;
385 if (token == "\\begin_layout" || token == "\\end_document"
386 || token == "\\end_inset" || token == "\\begin_deeper"
387 || token == "\\end_deeper") {
388 lex.pushToken(token);
389 lyxerr << "Paragraph ended in line "
390 << lex.getLineNo() << "\n"
391 << "Missing \\end_layout.\n";
395 // Final change goes to paragraph break:
396 par.setChange(par.size(), change);
398 // Initialize begin_of_body_ on load; redoParagraph maintains
399 par.setBeginOfBody();
407 double LyXText::spacing(Buffer const & buffer,
408 Paragraph const & par) const
410 if (par.params().spacing().isDefault())
411 return buffer.params().spacing().getValue();
412 return par.params().spacing().getValue();
416 int LyXText::width() const
422 int LyXText::height() const
424 return dim_.height();
428 int LyXText::singleWidth(Buffer const & buffer, Paragraph const & par,
431 return singleWidth(par, pos, par.getChar(pos),
432 getFont(buffer, par, pos));
436 int LyXText::singleWidth(Paragraph const & par,
437 pos_type pos, char_type c, LyXFont const & font) const
439 // The most common case is handled first (Asger)
440 if (isPrintable(c)) {
441 Language const * language = font.language();
442 if (language->rightToLeft()) {
443 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
444 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
445 && language->lang() == "arabic") {
446 if (Encodings::isComposeChar_arabic(c))
448 c = par.transformChar(c, pos);
449 } else if (language->lang() == "hebrew" &&
450 Encodings::isComposeChar_hebrew(c))
453 return theFontMetrics(font).width(c);
456 if (c == Paragraph::META_INSET)
457 return par.getInset(pos)->width();
459 return theFontMetrics(font).width(c);
463 int LyXText::leftMargin(Buffer const & buffer, pit_type pit) const
465 BOOST_ASSERT(pit >= 0);
466 BOOST_ASSERT(pit < int(pars_.size()));
467 return leftMargin(buffer, pit, pars_[pit].size());
471 int LyXText::leftMargin(Buffer const & buffer,
472 pit_type const pit, pos_type const pos) const
474 BOOST_ASSERT(pit >= 0);
475 BOOST_ASSERT(pit < int(pars_.size()));
476 Paragraph const & par = pars_[pit];
477 BOOST_ASSERT(pos >= 0);
478 BOOST_ASSERT(pos <= par.size());
479 //lyxerr << "LyXText::leftMargin: pit: " << pit << " pos: " << pos << endl;
480 LyXTextClass const & tclass = buffer.params().getLyXTextClass();
481 LyXLayout_ptr const & layout = par.layout();
483 string parindent = layout->parindent;
487 if (isMainText(buffer))
488 l_margin += changebarMargin();
491 docstring leftm = from_utf8(tclass.leftmargin());
492 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
494 if (par.getDepth() != 0) {
495 // find the next level paragraph
496 pit_type newpar = outerHook(pit, pars_);
497 if (newpar != pit_type(pars_.size())) {
498 if (pars_[newpar].layout()->isEnvironment()) {
499 l_margin = leftMargin(buffer, newpar);
501 if (par.layout() == tclass.defaultLayout()) {
502 if (pars_[newpar].params().noindent())
505 parindent = pars_[newpar].layout()->parindent;
510 // This happens after sections in standard classes. The 1.3.x
511 // code compared depths too, but it does not seem necessary
513 if (par.layout() == tclass.defaultLayout()
514 && pit > 0 && pars_[pit - 1].layout()->nextnoindent)
517 LyXFont const labelfont = getLabelFont(buffer, par);
518 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
520 switch (layout->margintype) {
522 if (!layout->leftmargin.empty()) {
524 docstring leftm = from_utf8(layout->leftmargin);
525 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
527 if (!par.getLabelstring().empty()) {
529 docstring labin = from_utf8(layout->labelindent);
530 l_margin += labelfont_metrics.signedWidth(labin);
531 docstring labstr = par.getLabelstring();
532 l_margin += labelfont_metrics.width(labstr);
533 docstring labsep = from_utf8(layout->labelsep);
534 l_margin += labelfont_metrics.width(labsep);
538 case MARGIN_MANUAL: {
540 docstring labin = from_utf8(layout->labelindent);
541 l_margin += labelfont_metrics.signedWidth(labin);
542 // The width of an empty par, even with manual label, should be 0
543 if (!par.empty() && pos >= par.beginOfBody()) {
544 if (!par.getLabelWidthString().empty()) {
545 docstring labstr = par.getLabelWidthString();
546 l_margin += labelfont_metrics.width(labstr);
547 docstring labsep = from_utf8(layout->labelsep);
548 l_margin += labelfont_metrics.width(labsep);
554 case MARGIN_STATIC: {
556 docstring leftm = from_utf8(layout->leftmargin);
557 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm)
558 * 4 / (par.getDepth() + 4);
562 case MARGIN_FIRST_DYNAMIC:
563 if (layout->labeltype == LABEL_MANUAL) {
564 if (pos >= par.beginOfBody()) {
566 l_margin += labelfont_metrics.signedWidth(
567 from_utf8(layout->leftmargin));
570 l_margin += labelfont_metrics.signedWidth(
571 from_utf8(layout->labelindent));
574 // Special case to fix problems with
576 || (layout->labeltype == LABEL_STATIC
577 && layout->latextype == LATEX_ENVIRONMENT
578 && !isFirstInSequence(pit, pars_))) {
580 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->leftmargin));
581 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
582 && layout->labeltype != LABEL_BIBLIO
583 && layout->labeltype !=
584 LABEL_CENTERED_TOP_ENVIRONMENT) {
585 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->labelindent));
586 l_margin += labelfont_metrics.width(from_utf8(layout->labelsep));
587 l_margin += labelfont_metrics.width(par.getLabelstring());
591 case MARGIN_RIGHT_ADDRESS_BOX: {
593 // ok, a terrible hack. The left margin depends on the widest
594 // row in this paragraph.
595 RowList::iterator rit = par.rows().begin();
596 RowList::iterator end = par.rows().end();
598 #warning This is wrong.
600 int minfill = maxwidth_;
601 for ( ; rit != end; ++rit)
602 if (rit->fill() < minfill)
603 minfill = rit->fill();
604 l_margin += theFontMetrics(params.getFont()).signedWidth(layout->leftmargin);
607 // also wrong, but much shorter.
608 l_margin += maxwidth_ / 2;
613 if (!par.params().leftIndent().zero())
614 l_margin += par.params().leftIndent().inPixels(maxwidth_);
618 if (par.params().align() == LYX_ALIGN_LAYOUT)
619 align = layout->align;
621 align = par.params().align();
623 // set the correct parindent
625 && (layout->labeltype == LABEL_NO_LABEL
626 || layout->labeltype == LABEL_TOP_ENVIRONMENT
627 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
628 || (layout->labeltype == LABEL_STATIC
629 && layout->latextype == LATEX_ENVIRONMENT
630 && !isFirstInSequence(pit, pars_)))
631 && align == LYX_ALIGN_BLOCK
632 && !par.params().noindent()
633 // in some insets, paragraphs are never indented
634 && !(par.inInset() && par.inInset()->neverIndent(buffer))
635 // display style insets are always centered, omit indentation
638 && par.getInset(pos)->display())
639 && (par.layout() != tclass.defaultLayout()
640 || buffer.params().paragraph_separation ==
641 BufferParams::PARSEP_INDENT))
643 docstring din = from_utf8(parindent);
644 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(din);
651 int LyXText::rightMargin(Buffer const & buffer, Paragraph const & par) const
653 // FIXME: the correct way is to only call rightMargin() only
654 // within the main LyXText. The following test is thus bogus.
655 LyXText const & text = buffer.text();
656 // We do not want rightmargins on inner texts.
660 BufferParams const & params = buffer.params();
661 LyXTextClass const & tclass = params.getLyXTextClass();
662 docstring trmarg = from_utf8(tclass.rightmargin());
663 docstring lrmarg = from_utf8(par.layout()->rightmargin);
664 FontMetrics const & fm = theFontMetrics(params.getFont());
667 + fm.signedWidth(trmarg)
668 + fm.signedWidth(lrmarg)
669 * 4 / (par.getDepth() + 4);
675 int LyXText::labelEnd(Buffer const & buffer, pit_type const pit) const
677 // labelEnd is only needed if the layout fills a flushleft label.
678 if (pars_[pit].layout()->margintype != MARGIN_MANUAL)
680 // return the beginning of the body
681 return leftMargin(buffer, pit);
687 // this needs special handling - only newlines count as a break point
688 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
690 pos_type const end = par.size();
693 if (par.isNewline(i))
702 void LyXText::rowBreakPoint(Buffer const & buffer, int right_margin,
703 pit_type const pit, Row & row) const
705 Paragraph const & par = pars_[pit];
706 pos_type const end = par.size();
707 pos_type const pos = row.pos();
713 // maximum pixel width of a row
714 int width = maxwidth_ - right_margin; // - leftMargin(buffer, pit, row);
720 LyXLayout_ptr const & layout = par.layout();
722 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
723 row.endpos(addressBreakPoint(pos, par));
727 pos_type const body_pos = par.beginOfBody();
730 // Now we iterate through until we reach the right margin
731 // or the end of the par, then choose the possible break
734 int const left = leftMargin(buffer, pit, pos);
737 // pixel width since last breakpoint
740 FontIterator fi = FontIterator(buffer, *this, par, pos);
741 pos_type point = end;
743 FontMetrics const & fm = theFontMetrics(getLabelFont(buffer, par));
744 for ( ; i < end; ++i, ++fi) {
745 char_type const c = par.getChar(i);
746 int thiswidth = singleWidth(par, i, c, *fi);
748 // add the auto-hfill from label end to the body
749 if (body_pos && i == body_pos) {
750 docstring lsep = from_utf8(layout->labelsep);
751 int add = fm.width(lsep);
752 if (par.isLineSeparator(i - 1))
753 add -= singleWidth(buffer, par, i - 1);
755 add = std::max(add, labelEnd(buffer, pit) - x);
760 chunkwidth += thiswidth;
762 // break before a character that will fall off
763 // the right of the row
765 // if no break before, break here
766 if (point == end || chunkwidth >= width - left) {
773 // exit on last registered breakpoint:
777 if (par.isNewline(i)) {
783 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
788 if (par.isInset(i) && par.getInset(i)->display()) {
794 if (!par.isInset(i) || par.getInset(i)->isChar()) {
795 // some insets are line separators too
796 if (par.isLineSeparator(i)) {
797 // register breakpoint:
804 // maybe found one, but the par is short enough.
805 if (i == end && x < width)
808 // manual labels cannot be broken in LaTeX. But we
809 // want to make our on-screen rendering of footnotes
811 if (body_pos && point < body_pos)
818 void LyXText::setRowWidth(Buffer const & buffer, pit_type const pit, Row & row) const
820 // get the pure distance
821 pos_type const end = row.endpos();
823 Paragraph const & par = pars_[pit];
824 docstring const labelsep = from_utf8(par.layout()->labelsep);
825 int w = leftMargin(buffer, pit, row.pos());
827 pos_type const body_pos = par.beginOfBody();
828 pos_type i = row.pos();
830 FontMetrics const & fm = theFontMetrics(getLabelFont(buffer, par));
833 FontIterator fi = FontIterator(buffer, *this, par, i);
834 for ( ; i < end; ++i, ++fi) {
835 if (body_pos > 0 && i == body_pos) {
836 w += fm.width(labelsep);
837 if (par.isLineSeparator(i - 1))
838 w -= singleWidth(buffer, par, i - 1);
839 w = max(w, labelEnd(buffer, pit));
841 char_type const c = par.getChar(i);
842 w += singleWidth(par, i, c, *fi);
846 if (body_pos > 0 && body_pos >= end) {
847 w += fm.width(labelsep);
848 if (end > 0 && par.isLineSeparator(end - 1))
849 w -= singleWidth(buffer, par, end - 1);
850 w = max(w, labelEnd(buffer, pit));
853 row.width(w + rightMargin(buffer, par));
857 // returns the minimum space a manual label needs on the screen in pixel
858 int LyXText::labelFill(Buffer const & buffer, Paragraph const & par,
859 Row const & row) const
861 pos_type last = par.beginOfBody();
863 BOOST_ASSERT(last > 0);
865 // -1 because a label ends with a space that is in the label
868 // a separator at this end does not count
869 if (par.isLineSeparator(last))
873 for (pos_type i = row.pos(); i <= last; ++i)
874 w += singleWidth(buffer, par, i);
876 docstring const & label = par.params().labelWidthString();
880 FontMetrics const & fm = theFontMetrics(getLabelFont(buffer, par));
882 return max(0, fm.width(label) - w);
886 LColor_color LyXText::backgroundColor() const
888 return LColor_color(LColor::color(background_color_));
892 void LyXText::setHeightOfRow(BufferView const & bv, pit_type const pit,
895 Paragraph const & par = pars_[pit];
896 // get the maximum ascent and the maximum descent
897 double layoutasc = 0;
898 double layoutdesc = 0;
899 double const dh = defaultRowHeight();
901 // ok, let us initialize the maxasc and maxdesc value.
902 // Only the fontsize count. The other properties
903 // are taken from the layoutfont. Nicer on the screen :)
904 LyXLayout_ptr const & layout = par.layout();
906 // as max get the first character of this row then it can
907 // increase but not decrease the height. Just some point to
908 // start with so we don't have to do the assignment below too
910 Buffer const & buffer = *bv.buffer();
911 LyXFont font = getFont(buffer, par, row.pos());
912 LyXFont::FONT_SIZE const tmpsize = font.size();
913 font = getLayoutFont(buffer, pit);
914 LyXFont::FONT_SIZE const size = font.size();
915 font.setSize(tmpsize);
917 LyXFont labelfont = getLabelFont(buffer, par);
919 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
920 FontMetrics const & fontmetrics = theFontMetrics(font);
922 // these are minimum values
923 double const spacing_val = layout->spacing.getValue()
924 * spacing(*bv.buffer(), par);
925 //lyxerr << "spacing_val = " << spacing_val << endl;
926 int maxasc = int(fontmetrics.maxAscent() * spacing_val);
927 int maxdesc = int(fontmetrics.maxDescent() * spacing_val);
929 // insets may be taller
930 InsetList::const_iterator ii = par.insetlist.begin();
931 InsetList::const_iterator iend = par.insetlist.end();
932 for ( ; ii != iend; ++ii) {
933 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
934 maxasc = max(maxasc, ii->inset->ascent());
935 maxdesc = max(maxdesc, ii->inset->descent());
939 // Check if any custom fonts are larger (Asger)
940 // This is not completely correct, but we can live with the small,
941 // cosmetic error for now.
943 pos_type const pos_end = row.endpos();
945 LyXFont::FONT_SIZE maxsize =
946 par.highestFontInRange(row.pos(), pos_end, size);
947 if (maxsize > font.size()) {
948 font.setSize(maxsize);
949 maxasc = max(maxasc, fontmetrics.maxAscent());
950 maxdesc = max(maxdesc, fontmetrics.maxDescent());
953 // This is nicer with box insets:
960 if (row.pos() == 0) {
961 Buffer const & buffer = *bv.buffer();
962 BufferParams const & bufparams = buffer.params();
963 // some parksips VERY EASY IMPLEMENTATION
964 if (bufparams.paragraph_separation
965 == BufferParams::PARSEP_SKIP
967 && ((layout->isParagraph() && par.getDepth() == 0)
968 || (pars_[pit - 1].layout()->isParagraph()
969 && pars_[pit - 1].getDepth() == 0)))
971 maxasc += bufparams.getDefSkip().inPixels(bv);
974 if (par.params().startOfAppendix())
975 maxasc += int(3 * dh);
977 // This is special code for the chapter, since the label of this
978 // layout is printed in an extra row
979 if (layout->counter == "chapter"
980 && !par.params().labelString().empty()) {
981 labeladdon = int(labelfont_metrics.maxHeight()
982 * layout->spacing.getValue()
983 * spacing(buffer, par));
986 // special code for the top label
987 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
988 || layout->labeltype == LABEL_BIBLIO
989 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
990 && isFirstInSequence(pit, paragraphs())
991 && !par.getLabelstring().empty())
994 labelfont_metrics.maxHeight()
995 * layout->spacing.getValue()
996 * spacing(*bv.buffer(), par)
997 + (layout->topsep + layout->labelbottomsep) * dh);
1000 // Add the layout spaces, for example before and after
1001 // a section, or between the items of a itemize or enumerate
1004 pit_type prev = depthHook(pit, pars_, par.getDepth());
1006 && pars_[prev].layout() == layout
1007 && pars_[prev].getDepth() == par.getDepth()
1008 && pars_[prev].getLabelWidthString() == par.getLabelWidthString())
1010 layoutasc = layout->itemsep * dh;
1011 } else if (pit != 0 || row.pos() != 0) {
1012 if (layout->topsep > 0)
1013 layoutasc = layout->topsep * dh;
1016 prev = outerHook(pit, pars_);
1017 if (prev != pit_type(pars_.size())) {
1018 maxasc += int(pars_[prev].layout()->parsep * dh);
1019 } else if (pit != 0) {
1020 if (pars_[pit - 1].getDepth() != 0 ||
1021 pars_[pit - 1].layout() == layout) {
1022 maxasc += int(layout->parsep * dh);
1027 // is it a bottom line?
1028 if (row.endpos() >= par.size()) {
1029 // add the layout spaces, for example before and after
1030 // a section, or between the items of a itemize or enumerate
1032 pit_type nextpit = pit + 1;
1033 if (nextpit != pit_type(pars_.size())) {
1034 pit_type cpit = pit;
1038 if (pars_[cpit].getDepth() > pars_[nextpit].getDepth()) {
1039 usual = pars_[cpit].layout()->bottomsep * dh;
1040 cpit = depthHook(cpit, paragraphs(), pars_[nextpit].getDepth());
1041 if (pars_[cpit].layout() != pars_[nextpit].layout()
1042 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1044 unusual = pars_[cpit].layout()->bottomsep * dh;
1046 layoutdesc = max(unusual, usual);
1047 } else if (pars_[cpit].getDepth() == pars_[nextpit].getDepth()) {
1048 if (pars_[cpit].layout() != pars_[nextpit].layout()
1049 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1050 layoutdesc = int(pars_[cpit].layout()->bottomsep * dh);
1055 // incalculate the layout spaces
1056 maxasc += int(layoutasc * 2 / (2 + pars_[pit].getDepth()));
1057 maxdesc += int(layoutdesc * 2 / (2 + pars_[pit].getDepth()));
1059 // FIXME: the correct way is to do the following is to move the
1060 // following code in another method specially tailored for the
1061 // main LyXText. The following test is thus bogus.
1062 LyXText const & text = buffer.text();
1063 // Top and bottom margin of the document (only at top-level)
1064 if (&text == this) {
1065 if (pit == 0 && row.pos() == 0)
1067 if (pit + 1 == pit_type(pars_.size()) &&
1068 row.endpos() == par.size())
1072 row.ascent(maxasc + labeladdon);
1073 row.descent(maxdesc);
1077 void LyXText::breakParagraph(LCursor & cur, bool keep_layout)
1079 BOOST_ASSERT(this == cur.text());
1081 Paragraph & cpar = cur.paragraph();
1082 pit_type cpit = cur.pit();
1084 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1085 LyXLayout_ptr const & layout = cpar.layout();
1087 // this is only allowed, if the current paragraph is not empty
1088 // or caption and if it has not the keepempty flag active
1089 if (cur.lastpos() == 0 && !cpar.allowEmpty() &&
1090 layout->labeltype != LABEL_SENSITIVE)
1093 // a layout change may affect also the following paragraph
1094 recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
1096 // Always break behind a space
1097 // It is better to erase the space (Dekel)
1098 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1099 cpar.eraseChar(cur.pos(), cur.buffer().params().trackChanges);
1101 // What should the layout for the new paragraph be?
1102 int preserve_layout = 0;
1104 preserve_layout = 2;
1106 preserve_layout = layout->isEnvironment();
1108 // We need to remember this before we break the paragraph, because
1109 // that invalidates the layout variable
1110 bool sensitive = layout->labeltype == LABEL_SENSITIVE;
1112 // we need to set this before we insert the paragraph.
1113 bool const isempty = cpar.allowEmpty() && cpar.empty();
1115 lyx::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1116 cur.pos(), preserve_layout);
1118 // After this, neither paragraph contains any rows!
1121 pit_type next_par = cpit + 1;
1123 // well this is the caption hack since one caption is really enough
1126 // set to standard-layout
1127 pars_[cpit].applyLayout(tclass.defaultLayout());
1129 // set to standard-layout
1130 pars_[next_par].applyLayout(tclass.defaultLayout());
1133 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1134 pars_[next_par].eraseChar(0, cur.buffer().params().trackChanges);
1136 ParIterator current_it(cur);
1137 ParIterator last_it(cur);
1141 updateLabels(cur.buffer(), current_it, last_it);
1143 // FIXME: Breaking a paragraph has nothing to do with setting a cursor.
1144 // Because of the mix between the model (the paragraph contents) and the
1145 // view (the paragraph breaking in rows, we have to do this here before
1146 // the setCursor() call below.
1147 bool changed_height = redoParagraph(cur.bv(), cpit);
1148 changed_height |= redoParagraph(cur.bv(), cpit + 1);
1150 // A singlePar update is not enough in this case.
1151 cur.updateFlags(Update::Force);
1153 // This check is necessary. Otherwise the new empty paragraph will
1154 // be deleted automatically. And it is more friendly for the user!
1155 if (cur.pos() != 0 || isempty)
1156 setCursor(cur, cur.pit() + 1, 0);
1158 setCursor(cur, cur.pit(), 0);
1162 // insert a character, moves all the following breaks in the
1163 // same Paragraph one to the right and make a rebreak
1164 void LyXText::insertChar(LCursor & cur, char_type c)
1166 BOOST_ASSERT(this == cur.text());
1167 BOOST_ASSERT(c != Paragraph::META_INSET);
1169 recordUndo(cur, Undo::INSERT);
1171 Buffer const & buffer = cur.buffer();
1172 Paragraph & par = cur.paragraph();
1173 // try to remove this
1174 pit_type const pit = cur.pit();
1176 bool const freeSpacing = par.layout()->free_spacing ||
1177 par.isFreeSpacing();
1179 if (lyxrc.auto_number) {
1180 static docstring const number_operators = from_ascii("+-/*");
1181 static docstring const number_unary_operators = from_ascii("+-");
1182 static docstring const number_seperators = from_ascii(".,:");
1184 if (current_font.number() == LyXFont::ON) {
1185 if (!isDigit(c) && !contains(number_operators, c) &&
1186 !(contains(number_seperators, c) &&
1188 cur.pos() != cur.lastpos() &&
1189 getFont(buffer, par, cur.pos()).number() == LyXFont::ON &&
1190 getFont(buffer, par, cur.pos() - 1).number() == LyXFont::ON)
1192 number(cur); // Set current_font.number to OFF
1193 } else if (isDigit(c) &&
1194 real_current_font.isVisibleRightToLeft()) {
1195 number(cur); // Set current_font.number to ON
1197 if (cur.pos() != 0) {
1198 char_type const c = par.getChar(cur.pos() - 1);
1199 if (contains(number_unary_operators, c) &&
1201 || par.isSeparator(cur.pos() - 2)
1202 || par.isNewline(cur.pos() - 2))
1204 setCharFont(buffer, pit, cur.pos() - 1, current_font);
1205 } else if (contains(number_seperators, c)
1207 && getFont(buffer, par, cur.pos() - 2).number() == LyXFont::ON) {
1208 setCharFont(buffer, pit, cur.pos() - 1, current_font);
1214 // First check, if there will be two blanks together or a blank at
1215 // the beginning of a paragraph.
1216 // I decided to handle blanks like normal characters, the main
1217 // difference are the special checks when calculating the row.fill
1218 // (blank does not count at the end of a row) and the check here
1220 // When the free-spacing option is set for the current layout,
1221 // disable the double-space checking
1222 if (!freeSpacing && isLineSeparatorChar(c)) {
1223 if (cur.pos() == 0) {
1224 static bool sent_space_message = false;
1225 if (!sent_space_message) {
1226 cur.message(_("You cannot insert a space at the "
1227 "beginning of a paragraph. Please read the Tutorial."));
1228 sent_space_message = true;
1232 BOOST_ASSERT(cur.pos() > 0);
1233 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
1234 && !par.isDeleted(cur.pos() - 1)) {
1235 static bool sent_space_message = false;
1236 if (!sent_space_message) {
1237 cur.message(_("You cannot type two spaces this way. "
1238 "Please read the Tutorial."));
1239 sent_space_message = true;
1245 par.insertChar(cur.pos(), c, current_font, cur.buffer().params().trackChanges);
1247 // FIXME: Inserting a character has nothing to do with setting a cursor.
1248 // Because of the mix between the model (the paragraph contents) and the
1249 // view (the paragraph breaking in rows, we have to do this here.
1250 if (redoParagraph(cur.bv(), cur.pit()))
1251 // A singlePar update is not enough in this case.
1252 cur.updateFlags(Update::Force);
1253 setCursor(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1258 void LyXText::charInserted()
1260 // Here we call finishUndo for every 20 characters inserted.
1261 // This is from my experience how emacs does it. (Lgb)
1262 static unsigned int counter;
1272 RowMetrics LyXText::computeRowMetrics(Buffer const & buffer,
1273 pit_type const pit, Row const & row) const
1276 Paragraph const & par = pars_[pit];
1278 double w = dim_.wid - row.width();
1280 bool const is_rtl = isRTL(buffer, par);
1282 result.x = rightMargin(buffer, par);
1284 result.x = leftMargin(buffer, pit, row.pos());
1286 // is there a manual margin with a manual label
1287 LyXLayout_ptr const & layout = par.layout();
1289 if (layout->margintype == MARGIN_MANUAL
1290 && layout->labeltype == LABEL_MANUAL) {
1291 /// We might have real hfills in the label part
1292 int nlh = numberOfLabelHfills(par, row);
1294 // A manual label par (e.g. List) has an auto-hfill
1295 // between the label text and the body of the
1297 // But we don't want to do this auto hfill if the par
1302 if (nlh && !par.getLabelWidthString().empty())
1303 result.label_hfill = labelFill(buffer, par, row) / double(nlh);
1306 // are there any hfills in the row?
1307 int const nh = numberOfHfills(par, row);
1311 result.hfill = w / nh;
1312 // we don't have to look at the alignment if it is ALIGN_LEFT and
1313 // if the row is already larger then the permitted width as then
1314 // we force the LEFT_ALIGN'edness!
1315 } else if (int(row.width()) < maxwidth_) {
1316 // is it block, flushleft or flushright?
1317 // set x how you need it
1319 if (par.params().align() == LYX_ALIGN_LAYOUT)
1320 align = layout->align;
1322 align = par.params().align();
1324 // Display-style insets should always be on a centred row
1325 // The test on par.size() is to catch zero-size pars, which
1326 // would trigger the assert in Paragraph::getInset().
1327 //inset = par.size() ? par.getInset(row.pos()) : 0;
1329 && par.isInset(row.pos())
1330 && par.getInset(row.pos())->display())
1332 align = LYX_ALIGN_CENTER;
1336 case LYX_ALIGN_BLOCK: {
1337 int const ns = numberOfSeparators(par, row);
1338 bool disp_inset = false;
1339 if (row.endpos() < par.size()) {
1340 InsetBase const * in = par.getInset(row.endpos());
1342 disp_inset = in->display();
1344 // If we have separators, this is not the last row of a
1345 // par, does not end in newline, and is not row above a
1346 // display inset... then stretch it
1348 && row.endpos() < par.size()
1349 && !par.isNewline(row.endpos() - 1)
1352 result.separator = w / ns;
1353 } else if (is_rtl) {
1358 case LYX_ALIGN_RIGHT:
1361 case LYX_ALIGN_CENTER:
1367 bidi.computeTables(par, buffer, row);
1369 pos_type body_pos = par.beginOfBody();
1370 pos_type end = row.endpos();
1373 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1375 docstring const lsep = from_utf8(layout->labelsep);
1376 result.x += theFontMetrics(getLabelFont(buffer, par)).width(lsep);
1377 if (body_pos <= end)
1378 result.x += result.label_hfill;
1386 // the cursor set functions have a special mechanism. When they
1387 // realize, that you left an empty paragraph, they will delete it.
1389 bool LyXText::cursorRightOneWord(LCursor & cur)
1391 BOOST_ASSERT(this == cur.text());
1395 if (old.pos() == old.lastpos() && old.pit() != old.lastpit()) {
1399 // Advance through word.
1400 while (old.pos() != old.lastpos() && old.paragraph().isLetter(old.pos()))
1402 // Skip through trailing nonword stuff.
1403 while (old.pos() != old.lastpos() && !old.paragraph().isLetter(old.pos()))
1406 return setCursor(cur, old.pit(), old.pos());
1410 bool LyXText::cursorLeftOneWord(LCursor & cur)
1412 BOOST_ASSERT(this == cur.text());
1416 if (old.pos() == 0 && old.pit() != 0) {
1418 old.pos() = old.lastpos();
1420 // Skip through initial nonword stuff.
1421 while (old.pos() != 0 && !old.paragraph().isLetter(old.pos() - 1))
1423 // Advance through word.
1424 while (old.pos() != 0 && old.paragraph().isLetter(old.pos() - 1))
1427 return setCursor(cur, old.pit(), old.pos());
1431 void LyXText::selectWord(LCursor & cur, word_location loc)
1433 BOOST_ASSERT(this == cur.text());
1434 CursorSlice from = cur.top();
1435 CursorSlice to = cur.top();
1436 getWord(from, to, loc);
1437 if (cur.top() != from)
1438 setCursor(cur, from.pit(), from.pos());
1442 setCursor(cur, to.pit(), to.pos());
1447 // Select the word currently under the cursor when no
1448 // selection is currently set
1449 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1451 BOOST_ASSERT(this == cur.text());
1452 if (cur.selection())
1454 selectWord(cur, loc);
1455 return cur.selection();
1459 void LyXText::acceptChange(LCursor & cur)
1461 // FIXME: change tracking (MG)
1463 BOOST_ASSERT(this == cur.text());
1465 if (!cur.selection() && cur.lastpos() != 0)
1468 // FIXME: we must handle start = end = 0
1470 recordUndoSelection(cur, Undo::INSERT);
1472 DocIterator it = cur.selectionBegin();
1473 DocIterator et = cur.selectionEnd();
1474 pit_type pit = it.pit();
1475 for (; pit <= et.pit(); ++pit) {
1476 pos_type left = (pit == it.pit() ? it.pos() : 0);
1477 pos_type right = (pit == et.pit() ? et.pos() : pars_[pit].size());
1478 pars_[pit].acceptChanges(left, right);
1480 // merge paragraph if appropriate:
1481 // if (right >= pars_[pit].size() && pit + 1 < et.pit() &&
1482 // pars_[pit].isDeleted(pars_[pit].size())) {
1483 // setCursorIntern(cur, pit + 1, 0);
1484 // backspacePos0(cur);
1488 cur.clearSelection();
1489 setCursorIntern(cur, it.pit(), 0);
1493 void LyXText::rejectChange(LCursor & cur)
1495 // FIXME: change tracking (MG)
1497 BOOST_ASSERT(this == cur.text());
1499 if (!cur.selection() && cur.lastpos() != 0)
1502 // FIXME: we must handle start = end = 0
1504 recordUndoSelection(cur, Undo::INSERT);
1506 DocIterator it = cur.selectionBegin();
1507 DocIterator et = cur.selectionEnd();
1508 pit_type pit = it.pit();
1509 for (; pit <= et.pit(); ++pit) {
1510 pos_type left = (pit == it.pit() ? it.pos() : 0);
1511 pos_type right = (pit == et.pit() ? et.pos() : pars_[pit].size());
1512 pars_[pit].rejectChanges(left, right);
1514 // merge paragraph if appropriate:
1515 // if (right >= pars_[pit].size() && pit + 1 < et.pit() &&
1516 // pars_[pit].isInserted(pars_[pit].size())) {
1517 // setCursorIntern(cur, pit + 1, 0);
1518 // backspacePos0(cur);
1522 cur.clearSelection();
1523 setCursorIntern(cur, it.pit(), 0);
1527 // Delete from cursor up to the end of the current or next word.
1528 void LyXText::deleteWordForward(LCursor & cur)
1530 BOOST_ASSERT(this == cur.text());
1531 if (cur.lastpos() == 0)
1535 cur.selection() = true;
1536 cursorRightOneWord(cur);
1538 cutSelection(cur, true, false);
1543 // Delete from cursor to start of current or prior word.
1544 void LyXText::deleteWordBackward(LCursor & cur)
1546 BOOST_ASSERT(this == cur.text());
1547 if (cur.lastpos() == 0)
1551 cur.selection() = true;
1552 cursorLeftOneWord(cur);
1554 cutSelection(cur, true, false);
1559 // Kill to end of line.
1560 void LyXText::deleteLineForward(LCursor & cur)
1562 BOOST_ASSERT(this == cur.text());
1563 if (cur.lastpos() == 0) {
1564 // Paragraph is empty, so we just go to the right
1568 cur.selection() = true; // to avoid deletion
1571 // What is this test for ??? (JMarc)
1572 if (!cur.selection())
1573 deleteWordForward(cur);
1575 cutSelection(cur, true, false);
1580 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1582 BOOST_ASSERT(this == cur.text());
1586 if (cur.selection()) {
1587 from = cur.selBegin();
1591 getWord(from, to, PARTIAL_WORD);
1592 cursorRightOneWord(cur);
1595 recordUndoSelection(cur);
1597 pos_type pos = from.pos();
1598 int par = from.pit();
1600 while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
1602 if (pos == pars_[pit].size()) {
1607 char_type c = pars_[pit].getChar(pos);
1608 if (c != Paragraph::META_INSET) {
1610 case text_lowercase:
1613 case text_capitalization:
1615 action = text_lowercase;
1617 case text_uppercase:
1623 // FIXME: change tracking (MG)
1624 // sorry but we are no longer allowed to set a single character directly
1625 // we have to rewrite this method in terms of erase&insert operations
1626 //pars_[pit].setChar(pos, c);
1632 bool LyXText::erase(LCursor & cur)
1634 BOOST_ASSERT(this == cur.text());
1635 bool needsUpdate = false;
1636 Paragraph & par = cur.paragraph();
1638 if (cur.pos() != cur.lastpos()) {
1639 // this is the code for a normal delete, not pasting
1641 recordUndo(cur, Undo::DELETE);
1642 par.eraseChar(cur.pos(), cur.buffer().params().trackChanges);
1643 if (par.isDeleted(cur.pos()))
1644 cur.forwardPosNoDescend();
1647 if (cur.pit() == cur.lastpit())
1648 return dissolveInset(cur);
1650 if (!par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1651 par.setChange(cur.pos(), Change(Change::DELETED));
1655 setCursorIntern(cur, cur.pit() + 1, 0);
1656 needsUpdate = backspacePos0(cur);
1660 // FIXME: Inserting characters has nothing to do with setting a cursor.
1661 // Because of the mix between the model (the paragraph contents)
1662 // and the view (the paragraph breaking in rows, we have to do this
1663 // here before the setCursorIntern() call.
1665 if (redoParagraph(cur.bv(), cur.pit()))
1666 // A singlePar update is not enough in this case.
1667 cur.updateFlags(Update::Force);
1668 // Make sure the cursor is correct. Is this really needed?
1669 // No, not really... at least not here!
1670 cur.text()->setCursorIntern(cur, cur.pit(), cur.pos());
1677 bool LyXText::backspacePos0(LCursor & cur)
1679 BOOST_ASSERT(this == cur.text());
1683 bool needsUpdate = false;
1685 BufferParams const & bufparams = cur.buffer().params();
1686 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1687 ParagraphList & plist = cur.text()->paragraphs();
1688 Paragraph const & par = cur.paragraph();
1689 LCursor prevcur = cur;
1691 prevcur.pos() = prevcur.lastpos();
1692 Paragraph const & prevpar = prevcur.paragraph();
1694 // is it an empty paragraph?
1695 if (cur.lastpos() == 0
1696 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1697 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1698 plist.erase(boost::next(plist.begin(), cur.pit()));
1701 // is previous par empty?
1702 else if (prevcur.lastpos() == 0
1703 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1704 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1705 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1708 // Pasting is not allowed, if the paragraphs have different
1709 // layouts. I think it is a real bug of all other
1710 // word processors to allow it. It confuses the user.
1711 // Correction: Pasting is always allowed with standard-layout
1712 else if (par.layout() == prevpar.layout()
1713 || par.layout() == tclass.defaultLayout()) {
1714 recordUndo(cur, Undo::ATOMIC, prevcur.pit());
1715 mergeParagraph(bufparams, plist, prevcur.pit());
1720 updateLabels(cur.buffer());
1721 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1728 bool LyXText::backspace(LCursor & cur)
1730 BOOST_ASSERT(this == cur.text());
1731 bool needsUpdate = false;
1732 if (cur.pos() == 0) {
1734 return dissolveInset(cur);
1736 Paragraph & prev_par = pars_[cur.pit() - 1];
1738 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1739 prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1740 setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1743 // The cursor is at the beginning of a paragraph, so
1744 // the backspace will collapse two paragraphs into one.
1745 needsUpdate = backspacePos0(cur);
1748 // this is the code for a normal backspace, not pasting
1750 recordUndo(cur, Undo::DELETE);
1751 // We used to do cursorLeftIntern() here, but it is
1752 // not a good idea since it triggers the auto-delete
1753 // mechanism. So we do a cursorLeftIntern()-lite,
1754 // without the dreaded mechanism. (JMarc)
1755 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1756 false, cur.boundary());
1757 cur.paragraph().eraseChar(cur.pos(), cur.buffer().params().trackChanges);
1760 if (cur.pos() == cur.lastpos())
1761 setCurrentFont(cur);
1763 // FIXME: Backspacing has nothing to do with setting a cursor.
1764 // Because of the mix between the model (the paragraph contents)
1765 // and the view (the paragraph breaking in rows, we have to do this
1766 // here before the setCursor() call.
1767 if (redoParagraph(cur.bv(), cur.pit()))
1768 // A singlePar update is not enough in this case.
1769 cur.updateFlags(Update::Force);
1770 setCursor(cur, cur.pit(), cur.pos(), false, cur.boundary());
1776 bool LyXText::dissolveInset(LCursor & cur) {
1777 BOOST_ASSERT(this == cur.text());
1779 if (isMainText(*cur.bv().buffer()) || cur.inset().nargs() != 1)
1782 bool const in_ert = cur.inset().lyxCode() == InsetBase::ERT_CODE;
1783 recordUndoInset(cur);
1784 cur.selHandle(false);
1786 pos_type spos = cur.pos();
1787 pit_type spit = cur.pit();
1788 ParagraphList plist;
1789 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1790 plist = paragraphs();
1792 // store cursor offset
1796 Buffer & b = cur.buffer();
1797 cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1798 if (!plist.empty()) {
1800 // ERT paragraphs have the Language latex_language.
1801 // This is invalid outside of ERT, so we need to
1802 // change it to the buffer language.
1803 ParagraphList::iterator it = plist.begin();
1804 ParagraphList::iterator it_end = plist.end();
1805 for (; it != it_end; it++) {
1806 it->changeLanguage(b.params(),
1812 pasteParagraphList(cur, plist, b.params().textclass,
1813 b.errorList("Paste"));
1815 cur.pit() = std::min(cur.lastpit(), spit);
1816 cur.pos() = std::min(cur.lastpos(), spos);
1818 cur.clearSelection();
1824 Row const & LyXText::firstRow() const
1826 return *paragraphs().front().rows().begin();
1830 bool LyXText::redoParagraph(BufferView & bv, pit_type const pit)
1832 // remove rows of paragraph, keep track of height changes
1833 Paragraph & par = pars_[pit];
1834 Buffer const & buffer = *bv.buffer();
1836 bool changed = false;
1838 // Add bibitem insets if necessary
1839 if (par.layout()->labeltype == LABEL_BIBLIO) {
1840 bool hasbibitem(false);
1841 if (!par.insetlist.empty()
1842 // Insist on it being in pos 0
1843 && par.getChar(0) == Paragraph::META_INSET) {
1844 InsetBase * inset = par.insetlist.begin()->inset;
1845 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
1849 InsetBibitem * inset(new
1850 InsetBibitem(InsetCommandParams("bibitem")));
1851 par.insertInset(0, static_cast<InsetBase *>(inset),
1852 Change(buffer.params().trackChanges ?
1853 Change::INSERTED : Change::UNCHANGED));
1854 bv.cursor().posRight();
1858 // Optimisation: this is used in the next two loops
1859 // so better to calculate that once here.
1860 int right_margin = rightMargin(buffer, par);
1863 // FIXME: We should always use getFont(), see documentation of
1864 // noFontChange() in insetbase.h.
1865 LyXFont const bufferfont = buffer.params().getFont();
1866 InsetList::const_iterator ii = par.insetlist.begin();
1867 InsetList::const_iterator iend = par.insetlist.end();
1868 for (; ii != iend; ++ii) {
1870 int const w = maxwidth_ - leftMargin(buffer, pit, ii->pos)
1872 LyXFont const & font = ii->inset->noFontChange() ?
1874 getFont(buffer, par, ii->pos);
1875 MetricsInfo mi(&bv, font, w);
1876 changed |= ii->inset->metrics(mi, dim);
1879 // rebreak the paragraph
1883 par.setBeginOfBody();
1887 rowBreakPoint(buffer, right_margin, pit, row);
1888 setRowWidth(buffer, pit, row);
1889 setHeightOfRow(bv, pit, row);
1890 par.rows().push_back(row);
1891 dim.wid = std::max(dim.wid, row.width());
1892 dim.des += row.height();
1894 } while (z < par.size());
1896 // Make sure that if a par ends in newline, there is one more row
1898 // FIXME this is a dirty trick. Now the _same_ position in the
1899 // paragraph occurs in _two_ different rows, and has two different
1900 // display positions, leading to weird behaviour when moving up/down.
1901 if (z > 0 && par.isNewline(z - 1)) {
1904 setRowWidth(buffer, pit, row);
1905 setHeightOfRow(bv, pit, row);
1906 par.rows().push_back(row);
1907 dim.des += row.height();
1910 dim.asc += par.rows()[0].ascent();
1911 dim.des -= par.rows()[0].ascent();
1913 changed |= dim.height() != par.dim().height();
1916 //lyxerr << "redoParagraph: " << par.rows().size() << " rows\n";
1922 bool LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1924 //BOOST_ASSERT(mi.base.textwidth);
1925 if (mi.base.textwidth)
1926 maxwidth_ = mi.base.textwidth;
1927 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1928 // << " maxWidth: " << maxwidth_ << "\nfont: " << mi.base.font << endl;
1929 // save the caller's font locally:
1930 font_ = mi.base.font;
1932 bool changed = false;
1936 for (pit_type pit = 0, n = paragraphs().size(); pit != n; ++pit) {
1937 changed |= redoParagraph(*mi.base.bv, pit);
1938 Paragraph & par = paragraphs()[pit];
1940 if (w < par.width())
1945 dim.asc = pars_[0].ascent();
1946 dim.des = h - dim.asc;
1948 changed |= dim_ != dim;
1954 // only used for inset right now. should also be used for main text
1955 void LyXText::draw(PainterInfo & pi, int x, int y) const
1957 paintTextInset(*this, pi, x, y);
1961 // only used for inset right now. should also be used for main text
1962 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
1964 LCursor & cur = pi.base.bv->cursor();
1965 if (!cur.selection())
1967 if (!ptr_cmp(cur.text(), this))
1970 if (lyxerr.debugging(Debug::DEBUG)) {
1971 lyxerr[Debug::DEBUG]
1972 << BOOST_CURRENT_FUNCTION
1973 << "draw selection at " << x
1977 DocIterator beg = cur.selectionBegin();
1978 DocIterator end = cur.selectionEnd();
1980 BufferView & bv = *pi.base.bv;
1981 Buffer const & buffer = *bv.buffer();
1983 // the selection doesn't touch the visible screen
1984 if (bv_funcs::status(&bv, beg) == bv_funcs::CUR_BELOW
1985 || bv_funcs::status(&bv, end) == bv_funcs::CUR_ABOVE)
1988 Paragraph const & par1 = pars_[beg.pit()];
1989 Paragraph const & par2 = pars_[end.pit()];
1991 bool const above = (bv_funcs::status(&bv, beg)
1992 == bv_funcs::CUR_ABOVE);
1993 bool const below = (bv_funcs::status(&bv, end)
1994 == bv_funcs::CUR_BELOW);
2002 Row const & row1 = par1.getRow(beg.pos(), beg.boundary());
2003 y1 = bv_funcs::getPos(bv, beg, beg.boundary()).y_ - row1.ascent();
2004 y2 = y1 + row1.height();
2005 int const startx = cursorX(buffer, beg.top(), beg.boundary());
2006 if (!isRTL(buffer, par1)) {
2018 Y1 = bv.workHeight();
2019 Y2 = bv.workHeight();
2023 Row const & row2 = par2.getRow(end.pos(), end.boundary());
2024 Y1 = bv_funcs::getPos(bv, end, end.boundary()).y_ - row2.ascent();
2025 Y2 = Y1 + row2.height();
2026 int const endx = cursorX(buffer, end.top(), end.boundary());
2027 if (!isRTL(buffer, par2)) {
2037 if (!above && !below && &par1.getRow(beg.pos(), beg.boundary())
2038 == &par2.getRow(end.pos(), end.boundary()))
2040 // paint only one rectangle
2041 int const b( !isRTL(*bv.buffer(), par1) ? x + x1 : x + X1 );
2042 int const w( !isRTL(*bv.buffer(), par1) ? X2 - x1 : x2 - X1 );
2043 pi.pain.fillRectangle(b, y1, w, y2 - y1, LColor::selection);
2047 if (lyxerr.debugging(Debug::DEBUG)) {
2048 lyxerr[Debug::DEBUG] << " y1: " << y1 << " y2: " << y2
2049 << "X1:" << X1 << " x2: " << X2 << " wid: " << dim_.wid
2053 // paint upper rectangle
2054 pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
2056 // paint bottom rectangle
2057 pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
2059 // paint center rectangle
2060 pi.pain.fillRectangle(x, y2, dim_.wid,
2061 Y1 - y2, LColor::selection);
2064 bool LyXText::isLastRow(pit_type pit, Row const & row) const
2066 return row.endpos() >= pars_[pit].size()
2067 && pit + 1 == pit_type(paragraphs().size());
2071 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
2073 return row.pos() == 0 && pit == 0;
2077 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
2078 word_location const loc)
2080 Paragraph const & from_par = pars_[from.pit()];
2082 case WHOLE_WORD_STRICT:
2083 if (from.pos() == 0 || from.pos() == from_par.size()
2084 || !from_par.isLetter(from.pos())
2085 || !from_par.isLetter(from.pos() - 1)) {
2089 // no break here, we go to the next
2092 // If we are already at the beginning of a word, do nothing
2093 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
2095 // no break here, we go to the next
2098 // always move the cursor to the beginning of previous word
2099 while (from.pos() && from_par.isLetter(from.pos() - 1))
2103 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
2107 // no need to move the 'from' cursor
2111 Paragraph & to_par = pars_[to.pit()];
2112 while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
2117 void LyXText::write(Buffer const & buf, std::ostream & os) const
2119 ParagraphList::const_iterator pit = paragraphs().begin();
2120 ParagraphList::const_iterator end = paragraphs().end();
2122 for (; pit != end; ++pit)
2123 pit->write(buf, os, buf.params(), dth);
2127 bool LyXText::read(Buffer const & buf, LyXLex & lex, ErrorList & errorList)
2129 depth_type depth = 0;
2131 while (lex.isOK()) {
2133 string const token = lex.getString();
2138 if (token == "\\end_inset")
2141 if (token == "\\end_body")
2144 if (token == "\\begin_body")
2147 if (token == "\\end_document")
2150 if (token == "\\begin_layout") {
2151 lex.pushToken(token);
2154 par.params().depth(depth);
2155 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
2156 pars_.push_back(par);
2158 // FIXME: goddamn InsetTabular makes us pass a Buffer
2160 lyx::readParagraph(buf, pars_.back(), lex, errorList);
2162 } else if (token == "\\begin_deeper") {
2164 } else if (token == "\\end_deeper") {
2166 lex.printError("\\end_deeper: " "depth is already null");
2171 lyxerr << "Handling unknown body token: `"
2172 << token << '\'' << endl;
2179 int LyXText::ascent() const
2185 int LyXText::descent() const
2191 int LyXText::cursorX(Buffer const & buffer, CursorSlice const & sl,
2192 bool boundary) const
2194 pit_type const pit = sl.pit();
2195 Paragraph const & par = pars_[pit];
2196 if (par.rows().empty())
2199 pos_type ppos = sl.pos();
2200 // Correct position in front of big insets
2201 bool const boundary_correction = ppos != 0 && boundary;
2202 if (boundary_correction)
2205 Row const & row = par.getRow(sl.pos(), boundary);
2207 pos_type cursor_vpos = 0;
2209 RowMetrics const m = computeRowMetrics(buffer, pit, row);
2212 pos_type const row_pos = row.pos();
2213 pos_type const end = row.endpos();
2216 cursor_vpos = row_pos;
2217 else if (ppos >= end)
2218 cursor_vpos = isRTL(buffer, par) ? row_pos : end;
2219 else if (ppos > row_pos && ppos >= end)
2220 // Place cursor after char at (logical) position pos - 1
2221 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
2222 ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
2224 // Place cursor before char at (logical) position ppos
2225 cursor_vpos = (bidi.level(ppos) % 2 == 0)
2226 ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
2228 pos_type body_pos = par.beginOfBody();
2230 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
2233 // Use font span to speed things up, see below
2236 FontMetrics const & labelfm = theFontMetrics(
2237 getLabelFont(buffer, par));
2239 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
2240 pos_type pos = bidi.vis2log(vpos);
2241 if (body_pos > 0 && pos == body_pos - 1) {
2243 docstring const lsep = from_utf8(par.layout()->labelsep);
2244 x += m.label_hfill + labelfm.width(lsep);
2245 if (par.isLineSeparator(body_pos - 1))
2246 x -= singleWidth(buffer, par, body_pos - 1);
2249 // Use font span to speed things up, see above
2250 if (pos < font_span.first || pos > font_span.last) {
2251 font_span = par.fontSpan(pos);
2252 font = getFont(buffer, par, pos);
2255 x += singleWidth(par, pos, par.getChar(pos), font);
2257 if (par.hfillExpansion(row, pos))
2258 x += (pos >= body_pos) ? m.hfill : m.label_hfill;
2259 else if (par.isSeparator(pos) && pos >= body_pos)
2263 // see correction above
2264 if (boundary_correction)
2265 if (getFont(buffer, par, ppos).isVisibleRightToLeft())
2266 x -= singleWidth(buffer, par, ppos);
2268 x += singleWidth(buffer, par, ppos);
2270 // Make sure inside an inset we always count from the left
2271 // edge (bidi!) -- MV
2272 if (sl.pos() < par.size()) {
2273 font = getFont(buffer, par, sl.pos());
2274 if (!boundary && font.isVisibleRightToLeft()
2275 && par.isInset(sl.pos()))
2276 x -= par.getInset(sl.pos())->width();
2282 int LyXText::cursorY(CursorSlice const & sl, bool boundary) const
2284 //lyxerr << "LyXText::cursorY: boundary: " << boundary << std::endl;
2285 Paragraph const & par = getPar(sl.pit());
2286 if (par.rows().empty())
2290 h -= pars_[0].rows()[0].ascent();
2291 for (pit_type pit = 0; pit < sl.pit(); ++pit)
2292 h += pars_[pit].height();
2294 if (pos && boundary)
2296 size_t const rend = par.pos2row(pos);
2297 for (size_t rit = 0; rit != rend; ++rit)
2298 h += par.rows()[rit].height();
2299 h += par.rows()[rend].ascent();
2304 // Returns the current font and depth as a message.
2305 string LyXText::currentState(LCursor & cur)
2307 BOOST_ASSERT(this == cur.text());
2308 Buffer & buf = cur.buffer();
2309 Paragraph const & par = cur.paragraph();
2310 std::ostringstream os;
2312 if (buf.params().trackChanges)
2315 Change change = par.lookupChange(cur.pos());
2317 if (change.type != Change::UNCHANGED) {
2318 Author const & a = buf.params().authors().get(change.author);
2319 os << to_utf8(_("Change: ")) << a.name();
2320 if (!a.email().empty())
2321 os << " (" << a.email() << ")";
2322 os << to_utf8(_(" at ")) << ctime(&change.changetime);
2326 // I think we should only show changes from the default
2328 // No, from the document font (MV)
2329 LyXFont font = real_current_font;
2330 font.reduce(buf.params().getFont());
2332 // avoid to_utf8(_(...)) re-entrance problem
2333 string const s = font.stateText(&buf.params());
2334 os << to_utf8(bformat(_("Font: %1$s"), from_utf8(s)));
2336 // os << to_utf8(bformat(_("Font: %1$s"), font.stateText(&buf.params)));
2338 // The paragraph depth
2339 int depth = cur.paragraph().getDepth();
2341 os << to_utf8(bformat(_(", Depth: %1$d"), depth));
2343 // The paragraph spacing, but only if different from
2345 Spacing const & spacing = par.params().spacing();
2346 if (!spacing.isDefault()) {
2347 os << to_utf8(_(", Spacing: "));
2348 switch (spacing.getSpace()) {
2349 case Spacing::Single:
2350 os << to_utf8(_("Single"));
2352 case Spacing::Onehalf:
2353 os << to_utf8(_("OneHalf"));
2355 case Spacing::Double:
2356 os << to_utf8(_("Double"));
2358 case Spacing::Other:
2359 os << to_utf8(_("Other (")) << spacing.getValueAsString() << ')';
2361 case Spacing::Default:
2362 // should never happen, do nothing
2367 #ifdef DEVEL_VERSION
2368 os << to_utf8(_(", Inset: ")) << &cur.inset();
2369 os << to_utf8(_(", Paragraph: ")) << cur.pit();
2370 os << to_utf8(_(", Id: ")) << par.id();
2371 os << to_utf8(_(", Position: ")) << cur.pos();
2372 os << to_utf8(_(", Boundary: ")) << cur.boundary();
2373 // Row & row = cur.textRow();
2374 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2380 docstring LyXText::getPossibleLabel(LCursor & cur) const
2382 pit_type pit = cur.pit();
2384 LyXLayout_ptr layout = pars_[pit].layout();
2386 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2387 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2388 if (layout2->latextype != LATEX_PARAGRAPH) {
2394 docstring name = from_ascii(layout->latexname());
2396 // for captions, we want the abbreviation of the float type
2397 if (layout->labeltype == LABEL_SENSITIVE) {
2398 // Search for the first float or wrap inset in the iterator
2399 for (int i = cur.depth(); --i >= 0; ) {
2400 InsetBase * const in = &cur[i].inset();
2401 if (in->lyxCode() == InsetBase::FLOAT_CODE
2402 || in->lyxCode() == InsetBase::WRAP_CODE) {
2403 name = in->getInsetName();
2409 docstring text = name.substr(0, 3);
2410 if (name == "theorem")
2411 text = from_ascii("thm"); // Create a correct prefix for prettyref
2414 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2417 docstring par_text = pars_[pit].asString(cur.buffer(), false);
2418 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2419 if (par_text.empty())
2422 par_text = split(par_text, head, ' ');
2423 // Is it legal to use spaces in labels ?
2433 //pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2437 // Paragraph const & par = pars_[pit];
2438 // Row const & r = par.rows()[row];
2439 // int pos = r.pos();
2440 // for (; currx < x && pos < r.endpos(); ++pos) {
2442 // currx += singleWidth(buffer, par, pos);
2444 // if (abs(lastx - x) < abs(currx - x) && pos != r.pos())
2450 pos_type LyXText::x2pos(BufferView const & bv, pit_type pit, int row,
2453 BOOST_ASSERT(row < int(pars_[pit].rows().size()));
2455 Row const & r = pars_[pit].rows()[row];
2456 return r.pos() + getColumnNearX(bv, pit, r, x, bound);
2460 //int LyXText::pos2x(pit_type pit, pos_type pos) const
2462 // Paragraph const & par = pars_[pit];
2463 // Row const & r = par.rows()[row];
2469 // x,y are screen coordinates
2470 // sets cursor only within this LyXText
2471 bool LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
2473 BOOST_ASSERT(this == cur.text());
2474 pit_type pit = getPitNearY(cur.bv(), y);
2475 int yy = cur.bv().coordCache().get(this, pit).y_ - pars_[pit].ascent();
2476 lyxerr[Debug::DEBUG]
2477 << BOOST_CURRENT_FUNCTION
2481 << " yy: " << yy << endl;
2483 Paragraph const & par = pars_[pit];
2485 BOOST_ASSERT(par.rows().size());
2486 for (; r < int(par.rows().size()) - 1; ++r) {
2487 Row const & row = par.rows()[r];
2488 if (int(yy + row.height()) > y)
2493 Row const & row = par.rows()[r];
2495 lyxerr[Debug::DEBUG]
2496 << BOOST_CURRENT_FUNCTION
2498 << " from pos: " << row.pos()
2503 pos_type const pos = row.pos() + getColumnNearX(cur.bv(), pit, row,
2506 lyxerr[Debug::DEBUG]
2507 << BOOST_CURRENT_FUNCTION
2508 << ": setting cursor pit: " << pit
2512 return setCursor(cur, pit, pos, true, bound);
2516 void LyXText::charsTranspose(LCursor & cur)
2518 BOOST_ASSERT(this == cur.text());
2520 pos_type pos = cur.pos();
2522 // If cursor is at beginning or end of paragraph, do nothing.
2523 if (pos == cur.lastpos() || pos == 0)
2526 Paragraph & par = cur.paragraph();
2528 // Get the positions of the characters to be transposed.
2529 pos_type pos1 = pos - 1;
2530 pos_type pos2 = pos;
2532 // In change tracking mode, ignore deleted characters.
2533 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
2535 if (pos2 == cur.lastpos())
2538 while (pos1 >= 0 && par.isDeleted(pos1))
2543 // Don't do anything if one of the "characters" is not regular text.
2544 if (par.isInset(pos1) || par.isInset(pos2))
2547 // Store the characters to be transposed (including font information).
2548 char_type char1 = par.getChar(pos1);
2549 LyXFont const font1 =
2550 par.getFontSettings(cur.buffer().params(), pos1);
2552 char_type char2 = par.getChar(pos2);
2553 LyXFont const font2 =
2554 par.getFontSettings(cur.buffer().params(), pos2);
2556 // And finally, we are ready to perform the transposition.
2557 // Track the changes if Change Tracking is enabled.
2558 bool const trackChanges = cur.buffer().params().trackChanges;
2562 par.eraseChar(pos2, trackChanges);
2563 par.eraseChar(pos1, trackChanges);
2564 par.insertChar(pos1, char2, font2, trackChanges);
2565 par.insertChar(pos2, char1, font1, trackChanges);
2567 // After the transposition, move cursor to after the transposition.
2568 setCursor(cur, cur.pit(), pos2);