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"
26 #include "bufferview_funcs.h"
28 #include "pariterator.h"
29 #include "coordcache.h"
30 #include "CutAndPaste.h"
32 #include "dispatchresult.h"
34 #include "errorlist.h"
35 #include "funcrequest.h"
37 #include "FontIterator.h"
41 #include "lyxlength.h"
45 #include "metricsinfo.h"
46 #include "paragraph.h"
47 #include "paragraph_funcs.h"
48 #include "ParagraphParameters.h"
49 #include "rowpainter.h"
52 #include "WordLangTuple.h"
54 #include "frontends/FontMetrics.h"
55 #include "frontends/Painter.h"
57 #include "insets/insettext.h"
58 #include "insets/insetbibitem.h"
59 #include "insets/insethfill.h"
60 #include "insets/insetlatexaccent.h"
61 #include "insets/insetline.h"
62 #include "insets/insetnewline.h"
63 #include "insets/insetpagebreak.h"
64 #include "insets/insetoptarg.h"
65 #include "insets/insetspace.h"
66 #include "insets/insetspecialchar.h"
67 #include "insets/insettabular.h"
69 #include "support/lstrings.h"
70 #include "support/textutils.h"
71 #include "support/convert.h"
73 #include <boost/current_function.hpp>
87 using support::bformat;
88 using support::contains;
89 using support::lowercase;
91 using support::uppercase;
93 using cap::cutSelection;
94 using cap::pasteParagraphList;
96 using frontend::FontMetrics;
100 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
101 string const & token, LyXFont & font, Change & change, ErrorList & errorList)
103 BufferParams const & bp = buf.params();
105 if (token[0] != '\\') {
107 string::const_iterator cit = token.begin();
108 for (; cit != token.end(); ++cit)
109 par.insertChar(par.size(), (*cit), font, change);
111 docstring dstr = lex.getDocString();
112 docstring::const_iterator cit = dstr.begin();
113 docstring::const_iterator cend = dstr.end();
114 for (; cit != cend; ++cit)
115 par.insertChar(par.size(), *cit, font, change);
117 } else if (token == "\\begin_layout") {
119 string layoutname = lex.getString();
121 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
122 change = Change(Change::UNCHANGED);
124 LyXTextClass const & tclass = bp.getLyXTextClass();
126 if (layoutname.empty()) {
127 layoutname = tclass.defaultLayoutName();
130 bool hasLayout = tclass.hasLayout(layoutname);
133 errorList.push_back(ErrorItem(_("Unknown layout"),
134 bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
135 from_utf8(layoutname), from_utf8(tclass.name())), par.id(), 0, par.size()));
136 layoutname = tclass.defaultLayoutName();
139 par.layout(bp.getLyXTextClass()[layoutname]);
141 // Test whether the layout is obsolete.
142 LyXLayout_ptr const & layout = par.layout();
143 if (!layout->obsoleted_by().empty())
144 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
146 par.params().read(lex);
148 } else if (token == "\\end_layout") {
149 lyxerr << BOOST_CURRENT_FUNCTION
150 << ": Solitary \\end_layout in line "
151 << lex.getLineNo() << "\n"
152 << "Missing \\begin_layout?.\n";
153 } else if (token == "\\end_inset") {
154 lyxerr << BOOST_CURRENT_FUNCTION
155 << ": Solitary \\end_inset in line "
156 << lex.getLineNo() << "\n"
157 << "Missing \\begin_inset?.\n";
158 } else if (token == "\\begin_inset") {
159 InsetBase * inset = readInset(lex, buf);
161 par.insertInset(par.size(), inset, font, change);
164 docstring line = lex.getDocString();
165 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
166 par.id(), 0, par.size()));
168 } else if (token == "\\family") {
170 font.setLyXFamily(lex.getString());
171 } else if (token == "\\series") {
173 font.setLyXSeries(lex.getString());
174 } else if (token == "\\shape") {
176 font.setLyXShape(lex.getString());
177 } else if (token == "\\size") {
179 font.setLyXSize(lex.getString());
180 } else if (token == "\\lang") {
182 string const tok = lex.getString();
183 Language const * lang = languages.getLanguage(tok);
185 font.setLanguage(lang);
187 font.setLanguage(bp.language);
188 lex.printError("Unknown language `$$Token'");
190 } else if (token == "\\numeric") {
192 font.setNumber(font.setLyXMisc(lex.getString()));
193 } else if (token == "\\emph") {
195 font.setEmph(font.setLyXMisc(lex.getString()));
196 } else if (token == "\\bar") {
198 string const tok = lex.getString();
201 font.setUnderbar(LyXFont::ON);
202 else if (tok == "no")
203 font.setUnderbar(LyXFont::OFF);
204 else if (tok == "default")
205 font.setUnderbar(LyXFont::INHERIT);
207 lex.printError("Unknown bar font flag "
209 } else if (token == "\\noun") {
211 font.setNoun(font.setLyXMisc(lex.getString()));
212 } else if (token == "\\color") {
214 font.setLyXColor(lex.getString());
215 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
217 // Insets don't make sense in a free-spacing context! ---Kayvan
218 if (par.isFreeSpacing()) {
219 if (token == "\\InsetSpace")
220 par.insertChar(par.size(), ' ', font, change);
221 else if (lex.isOK()) {
223 string const next_token = lex.getString();
224 if (next_token == "\\-")
225 par.insertChar(par.size(), '-', font, change);
227 lex.printError("Token `$$Token' "
229 "paragraph layout!");
233 auto_ptr<InsetBase> inset;
234 if (token == "\\SpecialChar" )
235 inset.reset(new InsetSpecialChar);
237 inset.reset(new InsetSpace);
238 inset->read(buf, lex);
239 par.insertInset(par.size(), inset.release(),
242 } else if (token == "\\i") {
243 auto_ptr<InsetBase> inset(new InsetLatexAccent);
244 inset->read(buf, lex);
245 par.insertInset(par.size(), inset.release(), font, change);
246 } else if (token == "\\backslash") {
247 par.insertChar(par.size(), '\\', font, change);
248 } else if (token == "\\newline") {
249 auto_ptr<InsetBase> inset(new InsetNewline);
250 inset->read(buf, lex);
251 par.insertInset(par.size(), inset.release(), font, change);
252 } else if (token == "\\LyXTable") {
253 auto_ptr<InsetBase> inset(new InsetTabular(buf));
254 inset->read(buf, lex);
255 par.insertInset(par.size(), inset.release(), font, change);
256 } else if (token == "\\hfill") {
257 par.insertInset(par.size(), new InsetHFill, font, change);
258 } else if (token == "\\lyxline") {
259 par.insertInset(par.size(), new InsetLine, font, change);
260 } else if (token == "\\newpage") {
261 par.insertInset(par.size(), new InsetPagebreak, font, change);
262 } else if (token == "\\clearpage") {
263 par.insertInset(par.size(), new InsetClearPage, font, change);
264 } else if (token == "\\cleardoublepage") {
265 par.insertInset(par.size(), new InsetClearDoublePage, font, change);
266 } else if (token == "\\change_unchanged") {
267 change = Change(Change::UNCHANGED);
268 } else if (token == "\\change_inserted") {
270 std::istringstream is(lex.getString());
274 if (aid >= bp.author_map.size()) {
275 errorList.push_back(ErrorItem(_("Change tracking error"),
276 bformat(_("Unknown author index for insertion: %1$d\n"), aid),
277 par.id(), 0, par.size()));
278 change = Change(Change::UNCHANGED);
280 change = Change(Change::INSERTED, bp.author_map[aid], ct);
281 } else if (token == "\\change_deleted") {
283 std::istringstream is(lex.getString());
287 if (aid >= bp.author_map.size()) {
288 errorList.push_back(ErrorItem(_("Change tracking error"),
289 bformat(_("Unknown author index for deletion: %1$d\n"), aid),
290 par.id(), 0, par.size()));
291 change = Change(Change::UNCHANGED);
293 change = Change(Change::DELETED, bp.author_map[aid], ct);
296 errorList.push_back(ErrorItem(_("Unknown token"),
297 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
299 par.id(), 0, par.size()));
304 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex,
305 ErrorList & errorList)
308 string token = lex.getString();
310 Change change(Change::UNCHANGED);
313 readParToken(buf, par, lex, token, font, change, errorList);
316 token = lex.getString();
321 if (token == "\\end_layout") {
322 //Ok, paragraph finished
326 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
327 << token << '\'' << endl;
328 if (token == "\\begin_layout" || token == "\\end_document"
329 || token == "\\end_inset" || token == "\\begin_deeper"
330 || token == "\\end_deeper") {
331 lex.pushToken(token);
332 lyxerr << "Paragraph ended in line "
333 << lex.getLineNo() << "\n"
334 << "Missing \\end_layout.\n";
338 // Final change goes to paragraph break:
339 par.setChange(par.size(), change);
341 // Initialize begin_of_body_ on load; redoParagraph maintains
342 par.setBeginOfBody();
350 double LyXText::spacing(Buffer const & buffer,
351 Paragraph const & par) const
353 if (par.params().spacing().isDefault())
354 return buffer.params().spacing().getValue();
355 return par.params().spacing().getValue();
359 int LyXText::singleWidth(Buffer const & buffer, Paragraph const & par,
362 return singleWidth(par, pos, par.getChar(pos),
363 getFont(buffer, par, pos));
367 int LyXText::singleWidth(Paragraph const & par,
368 pos_type pos, char_type c, LyXFont const & font) const
370 // The most common case is handled first (Asger)
371 if (isPrintable(c)) {
372 Language const * language = font.language();
373 if (language->rightToLeft()) {
374 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
375 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
376 && language->lang() == "arabic") {
377 if (Encodings::isComposeChar_arabic(c))
379 c = par.transformChar(c, pos);
380 } else if (language->lang() == "hebrew" &&
381 Encodings::isComposeChar_hebrew(c))
384 return theFontMetrics(font).width(c);
387 if (c == Paragraph::META_INSET)
388 return par.getInset(pos)->width();
390 return theFontMetrics(font).width(c);
394 int LyXText::leftMargin(Buffer const & buffer, int max_width, pit_type pit) const
396 BOOST_ASSERT(pit >= 0);
397 BOOST_ASSERT(pit < int(pars_.size()));
398 return leftMargin(buffer, max_width, pit, pars_[pit].size());
402 int LyXText::leftMargin(Buffer const & buffer, int max_width,
403 pit_type const pit, pos_type const pos) const
405 BOOST_ASSERT(pit >= 0);
406 BOOST_ASSERT(pit < int(pars_.size()));
407 Paragraph const & par = pars_[pit];
408 BOOST_ASSERT(pos >= 0);
409 BOOST_ASSERT(pos <= par.size());
410 //lyxerr << "LyXText::leftMargin: pit: " << pit << " pos: " << pos << endl;
411 LyXTextClass const & tclass = buffer.params().getLyXTextClass();
412 LyXLayout_ptr const & layout = par.layout();
414 string parindent = layout->parindent;
418 if (isMainText(buffer))
419 l_margin += changebarMargin();
422 docstring leftm = from_utf8(tclass.leftmargin());
423 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
425 if (par.getDepth() != 0) {
426 // find the next level paragraph
427 pit_type newpar = outerHook(pit, pars_);
428 if (newpar != pit_type(pars_.size())) {
429 if (pars_[newpar].layout()->isEnvironment()) {
430 l_margin = leftMargin(buffer, max_width, newpar);
432 if (par.layout() == tclass.defaultLayout()) {
433 if (pars_[newpar].params().noindent())
436 parindent = pars_[newpar].layout()->parindent;
441 // This happens after sections in standard classes. The 1.3.x
442 // code compared depths too, but it does not seem necessary
444 if (par.layout() == tclass.defaultLayout()
445 && pit > 0 && pars_[pit - 1].layout()->nextnoindent)
448 LyXFont const labelfont = getLabelFont(buffer, par);
449 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
451 switch (layout->margintype) {
453 if (!layout->leftmargin.empty()) {
455 docstring leftm = from_utf8(layout->leftmargin);
456 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
458 if (!par.getLabelstring().empty()) {
460 docstring labin = from_utf8(layout->labelindent);
461 l_margin += labelfont_metrics.signedWidth(labin);
462 docstring labstr = par.getLabelstring();
463 l_margin += labelfont_metrics.width(labstr);
464 docstring labsep = from_utf8(layout->labelsep);
465 l_margin += labelfont_metrics.width(labsep);
469 case MARGIN_MANUAL: {
471 docstring labin = from_utf8(layout->labelindent);
472 l_margin += labelfont_metrics.signedWidth(labin);
473 // The width of an empty par, even with manual label, should be 0
474 if (!par.empty() && pos >= par.beginOfBody()) {
475 if (!par.getLabelWidthString().empty()) {
476 docstring labstr = par.getLabelWidthString();
477 l_margin += labelfont_metrics.width(labstr);
478 docstring labsep = from_utf8(layout->labelsep);
479 l_margin += labelfont_metrics.width(labsep);
485 case MARGIN_STATIC: {
487 docstring leftm = from_utf8(layout->leftmargin);
488 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm)
489 * 4 / (par.getDepth() + 4);
493 case MARGIN_FIRST_DYNAMIC:
494 if (layout->labeltype == LABEL_MANUAL) {
495 if (pos >= par.beginOfBody()) {
497 l_margin += labelfont_metrics.signedWidth(
498 from_utf8(layout->leftmargin));
501 l_margin += labelfont_metrics.signedWidth(
502 from_utf8(layout->labelindent));
505 // Special case to fix problems with
507 || (layout->labeltype == LABEL_STATIC
508 && layout->latextype == LATEX_ENVIRONMENT
509 && !isFirstInSequence(pit, pars_))) {
511 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->leftmargin));
512 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
513 && layout->labeltype != LABEL_BIBLIO
514 && layout->labeltype !=
515 LABEL_CENTERED_TOP_ENVIRONMENT) {
516 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->labelindent));
517 l_margin += labelfont_metrics.width(from_utf8(layout->labelsep));
518 l_margin += labelfont_metrics.width(par.getLabelstring());
522 case MARGIN_RIGHT_ADDRESS_BOX: {
524 // ok, a terrible hack. The left margin depends on the widest
525 // row in this paragraph.
526 RowList::iterator rit = par.rows().begin();
527 RowList::iterator end = par.rows().end();
529 #warning This is wrong.
531 int minfill = max_width;
532 for ( ; rit != end; ++rit)
533 if (rit->fill() < minfill)
534 minfill = rit->fill();
535 l_margin += theFontMetrics(params.getFont()).signedWidth(layout->leftmargin);
538 // also wrong, but much shorter.
539 l_margin += max_width / 2;
544 if (!par.params().leftIndent().zero())
545 l_margin += par.params().leftIndent().inPixels(max_width);
549 if (par.params().align() == LYX_ALIGN_LAYOUT)
550 align = layout->align;
552 align = par.params().align();
554 // set the correct parindent
556 && (layout->labeltype == LABEL_NO_LABEL
557 || layout->labeltype == LABEL_TOP_ENVIRONMENT
558 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
559 || (layout->labeltype == LABEL_STATIC
560 && layout->latextype == LATEX_ENVIRONMENT
561 && !isFirstInSequence(pit, pars_)))
562 && align == LYX_ALIGN_BLOCK
563 && !par.params().noindent()
564 // in some insets, paragraphs are never indented
565 && !(par.inInset() && par.inInset()->neverIndent(buffer))
566 // display style insets are always centered, omit indentation
569 && par.getInset(pos)->display())
570 && (par.layout() != tclass.defaultLayout()
571 || buffer.params().paragraph_separation ==
572 BufferParams::PARSEP_INDENT))
574 docstring din = from_utf8(parindent);
575 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(din);
582 LColor_color LyXText::backgroundColor() const
584 return LColor_color(LColor::color(background_color_));
588 void LyXText::breakParagraph(LCursor & cur, bool keep_layout)
590 BOOST_ASSERT(this == cur.text());
592 Paragraph & cpar = cur.paragraph();
593 pit_type cpit = cur.pit();
595 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
596 LyXLayout_ptr const & layout = cpar.layout();
598 // this is only allowed, if the current paragraph is not empty
599 // or caption and if it has not the keepempty flag active
600 if (cur.lastpos() == 0 && !cpar.allowEmpty() &&
601 layout->labeltype != LABEL_SENSITIVE)
604 // a layout change may affect also the following paragraph
605 recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
607 // Always break behind a space
608 // It is better to erase the space (Dekel)
609 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
610 cpar.eraseChar(cur.pos(), cur.buffer().params().trackChanges);
612 // What should the layout for the new paragraph be?
613 int preserve_layout = 0;
617 preserve_layout = layout->isEnvironment();
619 // We need to remember this before we break the paragraph, because
620 // that invalidates the layout variable
621 bool sensitive = layout->labeltype == LABEL_SENSITIVE;
623 // we need to set this before we insert the paragraph.
624 bool const isempty = cpar.allowEmpty() && cpar.empty();
626 lyx::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
627 cur.pos(), preserve_layout);
629 // After this, neither paragraph contains any rows!
632 pit_type next_par = cpit + 1;
634 // well this is the caption hack since one caption is really enough
637 // set to standard-layout
638 pars_[cpit].applyLayout(tclass.defaultLayout());
640 // set to standard-layout
641 pars_[next_par].applyLayout(tclass.defaultLayout());
644 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
645 pars_[next_par].eraseChar(0, cur.buffer().params().trackChanges);
647 ParIterator current_it(cur);
648 ParIterator last_it(cur);
652 updateLabels(cur.buffer(), current_it, last_it);
654 // A singlePar update is not enough in this case.
655 cur.updateFlags(Update::Force);
657 // This check is necessary. Otherwise the new empty paragraph will
658 // be deleted automatically. And it is more friendly for the user!
659 if (cur.pos() != 0 || isempty)
660 setCursor(cur.top(), cur.pit() + 1, 0);
662 setCursor(cur.top(), cur.pit(), 0);
666 // insert a character, moves all the following breaks in the
667 // same Paragraph one to the right and make a rebreak
668 void LyXText::insertChar(LCursor & cur, char_type c)
670 BOOST_ASSERT(this == cur.text());
671 BOOST_ASSERT(c != Paragraph::META_INSET);
673 recordUndo(cur, Undo::INSERT);
675 Buffer const & buffer = cur.buffer();
676 Paragraph & par = cur.paragraph();
677 // try to remove this
678 pit_type const pit = cur.pit();
680 bool const freeSpacing = par.layout()->free_spacing ||
683 if (lyxrc.auto_number) {
684 static docstring const number_operators = from_ascii("+-/*");
685 static docstring const number_unary_operators = from_ascii("+-");
686 static docstring const number_seperators = from_ascii(".,:");
688 if (current_font.number() == LyXFont::ON) {
689 if (!isDigit(c) && !contains(number_operators, c) &&
690 !(contains(number_seperators, c) &&
692 cur.pos() != cur.lastpos() &&
693 getFont(buffer, par, cur.pos()).number() == LyXFont::ON &&
694 getFont(buffer, par, cur.pos() - 1).number() == LyXFont::ON)
696 number(cur); // Set current_font.number to OFF
697 } else if (isDigit(c) &&
698 real_current_font.isVisibleRightToLeft()) {
699 number(cur); // Set current_font.number to ON
701 if (cur.pos() != 0) {
702 char_type const c = par.getChar(cur.pos() - 1);
703 if (contains(number_unary_operators, c) &&
705 || par.isSeparator(cur.pos() - 2)
706 || par.isNewline(cur.pos() - 2))
708 setCharFont(buffer, pit, cur.pos() - 1, current_font);
709 } else if (contains(number_seperators, c)
711 && getFont(buffer, par, cur.pos() - 2).number() == LyXFont::ON) {
712 setCharFont(buffer, pit, cur.pos() - 1, current_font);
718 // First check, if there will be two blanks together or a blank at
719 // the beginning of a paragraph.
720 // I decided to handle blanks like normal characters, the main
721 // difference are the special checks when calculating the row.fill
722 // (blank does not count at the end of a row) and the check here
724 // When the free-spacing option is set for the current layout,
725 // disable the double-space checking
726 if (!freeSpacing && isLineSeparatorChar(c)) {
727 if (cur.pos() == 0) {
728 static bool sent_space_message = false;
729 if (!sent_space_message) {
730 cur.message(_("You cannot insert a space at the "
731 "beginning of a paragraph. Please read the Tutorial."));
732 sent_space_message = true;
736 BOOST_ASSERT(cur.pos() > 0);
737 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
738 && !par.isDeleted(cur.pos() - 1)) {
739 static bool sent_space_message = false;
740 if (!sent_space_message) {
741 cur.message(_("You cannot type two spaces this way. "
742 "Please read the Tutorial."));
743 sent_space_message = true;
749 par.insertChar(cur.pos(), c, current_font, cur.buffer().params().trackChanges);
751 // cur.updateFlags(Update::Force);
752 setCursor(cur.top(), cur.pit(), cur.pos() + 1);
757 void LyXText::charInserted()
759 // Here we call finishUndo for every 20 characters inserted.
760 // This is from my experience how emacs does it. (Lgb)
761 static unsigned int counter;
771 // the cursor set functions have a special mechanism. When they
772 // realize, that you left an empty paragraph, they will delete it.
774 bool LyXText::cursorRightOneWord(LCursor & cur)
776 BOOST_ASSERT(this == cur.text());
780 if (old.pos() == old.lastpos() && old.pit() != old.lastpit()) {
784 // Advance through word.
785 while (old.pos() != old.lastpos() && old.paragraph().isLetter(old.pos()))
787 // Skip through trailing nonword stuff.
788 while (old.pos() != old.lastpos() && !old.paragraph().isLetter(old.pos()))
791 return setCursor(cur, old.pit(), old.pos());
795 bool LyXText::cursorLeftOneWord(LCursor & cur)
797 BOOST_ASSERT(this == cur.text());
801 if (old.pos() == 0 && old.pit() != 0) {
803 old.pos() = old.lastpos();
805 // Skip through initial nonword stuff.
806 while (old.pos() != 0 && !old.paragraph().isLetter(old.pos() - 1))
808 // Advance through word.
809 while (old.pos() != 0 && old.paragraph().isLetter(old.pos() - 1))
812 return setCursor(cur, old.pit(), old.pos());
816 void LyXText::selectWord(LCursor & cur, word_location loc)
818 BOOST_ASSERT(this == cur.text());
819 CursorSlice from = cur.top();
820 CursorSlice to = cur.top();
821 getWord(from, to, loc);
822 if (cur.top() != from)
823 setCursor(cur, from.pit(), from.pos());
827 setCursor(cur, to.pit(), to.pos());
832 // Select the word currently under the cursor when no
833 // selection is currently set
834 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
836 BOOST_ASSERT(this == cur.text());
839 selectWord(cur, loc);
840 return cur.selection();
844 void LyXText::acceptChange(LCursor & cur)
846 // FIXME: change tracking (MG)
848 BOOST_ASSERT(this == cur.text());
850 if (!cur.selection() && cur.lastpos() != 0)
853 // FIXME: we must handle start = end = 0
855 recordUndoSelection(cur, Undo::INSERT);
857 DocIterator it = cur.selectionBegin();
858 DocIterator et = cur.selectionEnd();
859 pit_type pit = it.pit();
860 for (; pit <= et.pit(); ++pit) {
861 pos_type left = (pit == it.pit() ? it.pos() : 0);
862 pos_type right = (pit == et.pit() ? et.pos() : pars_[pit].size());
863 pars_[pit].acceptChanges(left, right);
865 // merge paragraph if appropriate:
866 // if (right >= pars_[pit].size() && pit + 1 < et.pit() &&
867 // pars_[pit].isDeleted(pars_[pit].size())) {
868 // setCursorIntern(cur, pit + 1, 0);
869 // backspacePos0(cur);
873 cur.clearSelection();
874 setCursorIntern(cur, it.pit(), 0);
878 void LyXText::rejectChange(LCursor & cur)
880 // FIXME: change tracking (MG)
882 BOOST_ASSERT(this == cur.text());
884 if (!cur.selection() && cur.lastpos() != 0)
887 // FIXME: we must handle start = end = 0
889 recordUndoSelection(cur, Undo::INSERT);
891 DocIterator it = cur.selectionBegin();
892 DocIterator et = cur.selectionEnd();
893 pit_type pit = it.pit();
894 for (; pit <= et.pit(); ++pit) {
895 pos_type left = (pit == it.pit() ? it.pos() : 0);
896 pos_type right = (pit == et.pit() ? et.pos() : pars_[pit].size());
897 pars_[pit].rejectChanges(left, right);
899 // merge paragraph if appropriate:
900 // if (right >= pars_[pit].size() && pit + 1 < et.pit() &&
901 // pars_[pit].isInserted(pars_[pit].size())) {
902 // setCursorIntern(cur, pit + 1, 0);
903 // backspacePos0(cur);
907 cur.clearSelection();
908 setCursorIntern(cur, it.pit(), 0);
912 // Delete from cursor up to the end of the current or next word.
913 void LyXText::deleteWordForward(LCursor & cur)
915 BOOST_ASSERT(this == cur.text());
916 if (cur.lastpos() == 0)
920 cur.selection() = true;
921 cursorRightOneWord(cur);
923 cutSelection(cur, true, false);
928 // Delete from cursor to start of current or prior word.
929 void LyXText::deleteWordBackward(LCursor & cur)
931 BOOST_ASSERT(this == cur.text());
932 if (cur.lastpos() == 0)
936 cur.selection() = true;
937 cursorLeftOneWord(cur);
939 cutSelection(cur, true, false);
944 // Kill to end of line.
945 void LyXText::deleteLineForward(LCursor & cur)
947 BOOST_ASSERT(this == cur.text());
948 if (cur.lastpos() == 0) {
949 // Paragraph is empty, so we just go to the right
953 cur.selection() = true; // to avoid deletion
956 // What is this test for ??? (JMarc)
957 if (!cur.selection())
958 deleteWordForward(cur);
960 cutSelection(cur, true, false);
965 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
967 BOOST_ASSERT(this == cur.text());
971 if (cur.selection()) {
972 from = cur.selBegin();
976 getWord(from, to, PARTIAL_WORD);
977 cursorRightOneWord(cur);
980 recordUndoSelection(cur);
982 pos_type pos = from.pos();
983 int par = from.pit();
985 while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
987 if (pos == pars_[pit].size()) {
992 char_type c = pars_[pit].getChar(pos);
993 if (c != Paragraph::META_INSET) {
998 case text_capitalization:
1000 action = text_lowercase;
1002 case text_uppercase:
1008 // FIXME: change tracking (MG)
1009 // sorry but we are no longer allowed to set a single character directly
1010 // we have to rewrite this method in terms of erase&insert operations
1011 //pars_[pit].setChar(pos, c);
1017 bool LyXText::erase(LCursor & cur)
1019 BOOST_ASSERT(this == cur.text());
1020 bool needsUpdate = false;
1021 Paragraph & par = cur.paragraph();
1023 if (cur.pos() != cur.lastpos()) {
1024 // this is the code for a normal delete, not pasting
1026 recordUndo(cur, Undo::DELETE);
1027 par.eraseChar(cur.pos(), cur.buffer().params().trackChanges);
1028 if (par.isDeleted(cur.pos()))
1029 cur.forwardPosNoDescend();
1032 if (cur.pit() == cur.lastpit())
1033 return dissolveInset(cur);
1035 if (!par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1036 par.setChange(cur.pos(), Change(Change::DELETED));
1040 setCursorIntern(cur, cur.pit() + 1, 0);
1041 needsUpdate = backspacePos0(cur);
1046 // Make sure the cursor is correct. Is this really needed?
1047 // No, not really... at least not here!
1048 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1055 bool LyXText::backspacePos0(LCursor & cur)
1057 BOOST_ASSERT(this == cur.text());
1061 bool needsUpdate = false;
1063 BufferParams const & bufparams = cur.buffer().params();
1064 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1065 ParagraphList & plist = cur.text()->paragraphs();
1066 Paragraph const & par = cur.paragraph();
1067 LCursor prevcur = cur;
1069 prevcur.pos() = prevcur.lastpos();
1070 Paragraph const & prevpar = prevcur.paragraph();
1072 // is it an empty paragraph?
1073 if (cur.lastpos() == 0
1074 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1075 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1076 plist.erase(boost::next(plist.begin(), cur.pit()));
1079 // is previous par empty?
1080 else if (prevcur.lastpos() == 0
1081 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1082 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1083 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1086 // Pasting is not allowed, if the paragraphs have different
1087 // layouts. I think it is a real bug of all other
1088 // word processors to allow it. It confuses the user.
1089 // Correction: Pasting is always allowed with standard-layout
1090 else if (par.layout() == prevpar.layout()
1091 || par.layout() == tclass.defaultLayout()) {
1092 recordUndo(cur, Undo::ATOMIC, prevcur.pit());
1093 mergeParagraph(bufparams, plist, prevcur.pit());
1098 updateLabels(cur.buffer());
1099 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1106 bool LyXText::backspace(LCursor & cur)
1108 BOOST_ASSERT(this == cur.text());
1109 bool needsUpdate = false;
1110 if (cur.pos() == 0) {
1112 return dissolveInset(cur);
1114 Paragraph & prev_par = pars_[cur.pit() - 1];
1116 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1117 prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1118 setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1121 // The cursor is at the beginning of a paragraph, so
1122 // the backspace will collapse two paragraphs into one.
1123 needsUpdate = backspacePos0(cur);
1126 // this is the code for a normal backspace, not pasting
1128 recordUndo(cur, Undo::DELETE);
1129 // We used to do cursorLeftIntern() here, but it is
1130 // not a good idea since it triggers the auto-delete
1131 // mechanism. So we do a cursorLeftIntern()-lite,
1132 // without the dreaded mechanism. (JMarc)
1133 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1134 false, cur.boundary());
1135 cur.paragraph().eraseChar(cur.pos(), cur.buffer().params().trackChanges);
1138 if (cur.pos() == cur.lastpos())
1139 setCurrentFont(cur);
1141 // A singlePar update is not enough in this case.
1142 // cur.updateFlags(Update::Force);
1143 setCursor(cur.top(), cur.pit(), cur.pos());
1149 bool LyXText::dissolveInset(LCursor & cur) {
1150 BOOST_ASSERT(this == cur.text());
1152 if (isMainText(*cur.bv().buffer()) || cur.inset().nargs() != 1)
1155 recordUndoInset(cur);
1156 cur.selHandle(false);
1158 pos_type spos = cur.pos();
1159 pit_type spit = cur.pit();
1160 ParagraphList plist;
1161 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1162 plist = paragraphs();
1164 // store cursor offset
1168 Buffer & b = cur.buffer();
1169 cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1170 if (!plist.empty()) {
1171 // ERT paragraphs have the Language latex_language.
1172 // This is invalid outside of ERT, so we need to
1173 // change it to the buffer language.
1174 ParagraphList::iterator it = plist.begin();
1175 ParagraphList::iterator it_end = plist.end();
1176 for (; it != it_end; it++) {
1177 it->changeLanguage(b.params(), latex_language,
1181 pasteParagraphList(cur, plist, b.params().textclass,
1182 b.errorList("Paste"));
1184 cur.pit() = std::min(cur.lastpit(), spit);
1185 cur.pos() = std::min(cur.lastpos(), spos);
1187 cur.clearSelection();
1193 // only used for inset right now. should also be used for main text
1194 void LyXText::draw(PainterInfo & pi, int x, int y) const
1196 paintTextInset(*this, pi, x, y);
1200 // only used for inset right now. should also be used for main text
1201 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
1203 LCursor & cur = pi.base.bv->cursor();
1204 if (!cur.selection())
1206 if (!ptr_cmp(cur.text(), this))
1209 if (lyxerr.debugging(Debug::DEBUG)) {
1210 lyxerr[Debug::DEBUG]
1211 << BOOST_CURRENT_FUNCTION
1212 << "draw selection at " << x
1216 DocIterator beg = cur.selectionBegin();
1217 DocIterator end = cur.selectionEnd();
1219 BufferView & bv = *pi.base.bv;
1220 Buffer const & buffer = *bv.buffer();
1222 // the selection doesn't touch the visible screen
1223 if (bv_funcs::status(&bv, beg) == bv_funcs::CUR_BELOW
1224 || bv_funcs::status(&bv, end) == bv_funcs::CUR_ABOVE)
1227 Paragraph const & par1 = pars_[beg.pit()];
1228 Paragraph const & par2 = pars_[end.pit()];
1229 TextMetrics const & tm = bv.textMetrics(this);
1230 ParagraphMetrics const & pm1 = tm.parMetrics(beg.pit());
1231 ParagraphMetrics const & pm2 = tm.parMetrics(end.pit());
1233 bool const above = (bv_funcs::status(&bv, beg)
1234 == bv_funcs::CUR_ABOVE);
1235 bool const below = (bv_funcs::status(&bv, end)
1236 == bv_funcs::CUR_BELOW);
1244 Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
1245 y1 = bv_funcs::getPos(bv, beg, beg.boundary()).y_ - row1.ascent();
1246 y2 = y1 + row1.height();
1247 int const startx = cursorX(bv, beg.top(), beg.boundary());
1248 if (!isRTL(buffer, par1)) {
1250 x2 = 0 + tm.width();
1260 Y1 = bv.workHeight();
1261 Y2 = bv.workHeight();
1265 Row const & row2 = pm2.getRow(end.pos(), end.boundary());
1266 Y1 = bv_funcs::getPos(bv, end, end.boundary()).y_ - row2.ascent();
1267 Y2 = Y1 + row2.height();
1268 int const endx = cursorX(bv, end.top(), end.boundary());
1269 if (!isRTL(buffer, par2)) {
1275 X2 = 0 + tm.width();
1279 if (!above && !below && &pm1.getRow(beg.pos(), beg.boundary())
1280 == &pm2.getRow(end.pos(), end.boundary()))
1282 // paint only one rectangle
1283 int const b( !isRTL(*bv.buffer(), par1) ? x + x1 : x + X1 );
1284 int const w( !isRTL(*bv.buffer(), par1) ? X2 - x1 : x2 - X1 );
1285 pi.pain.fillRectangle(b, y1, w, y2 - y1, LColor::selection);
1289 if (lyxerr.debugging(Debug::DEBUG)) {
1290 lyxerr[Debug::DEBUG] << " y1: " << y1 << " y2: " << y2
1291 << "X1:" << X1 << " x2: " << X2 << " wid: " << tm.width()
1295 // paint upper rectangle
1296 pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
1298 // paint bottom rectangle
1299 pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
1301 // paint center rectangle
1302 pi.pain.fillRectangle(x, y2, tm.width(),
1303 Y1 - y2, LColor::selection);
1307 bool LyXText::isLastRow(pit_type pit, Row const & row) const
1309 return row.endpos() >= pars_[pit].size()
1310 && pit + 1 == pit_type(paragraphs().size());
1314 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
1316 return row.pos() == 0 && pit == 0;
1320 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1321 word_location const loc)
1323 Paragraph const & from_par = pars_[from.pit()];
1325 case WHOLE_WORD_STRICT:
1326 if (from.pos() == 0 || from.pos() == from_par.size()
1327 || !from_par.isLetter(from.pos())
1328 || !from_par.isLetter(from.pos() - 1)) {
1332 // no break here, we go to the next
1335 // If we are already at the beginning of a word, do nothing
1336 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
1338 // no break here, we go to the next
1341 // always move the cursor to the beginning of previous word
1342 while (from.pos() && from_par.isLetter(from.pos() - 1))
1346 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1350 // no need to move the 'from' cursor
1354 Paragraph & to_par = pars_[to.pit()];
1355 while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
1360 void LyXText::write(Buffer const & buf, std::ostream & os) const
1362 ParagraphList::const_iterator pit = paragraphs().begin();
1363 ParagraphList::const_iterator end = paragraphs().end();
1365 for (; pit != end; ++pit)
1366 pit->write(buf, os, buf.params(), dth);
1370 bool LyXText::read(Buffer const & buf, LyXLex & lex, ErrorList & errorList)
1372 depth_type depth = 0;
1374 while (lex.isOK()) {
1376 string const token = lex.getString();
1381 if (token == "\\end_inset")
1384 if (token == "\\end_body")
1387 if (token == "\\begin_body")
1390 if (token == "\\end_document")
1393 if (token == "\\begin_layout") {
1394 lex.pushToken(token);
1397 par.params().depth(depth);
1398 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1399 pars_.push_back(par);
1401 // FIXME: goddamn InsetTabular makes us pass a Buffer
1403 lyx::readParagraph(buf, pars_.back(), lex, errorList);
1405 } else if (token == "\\begin_deeper") {
1407 } else if (token == "\\end_deeper") {
1409 lex.printError("\\end_deeper: " "depth is already null");
1414 lyxerr << "Handling unknown body token: `"
1415 << token << '\'' << endl;
1421 int LyXText::cursorX(BufferView const & bv, CursorSlice const & sl,
1422 bool boundary) const
1424 TextMetrics const & tm = bv.textMetrics(sl.text());
1425 pit_type const pit = sl.pit();
1426 Paragraph const & par = pars_[pit];
1427 ParagraphMetrics const & pm = tm.parMetrics(pit);
1428 if (pm.rows().empty())
1431 pos_type ppos = sl.pos();
1432 // Correct position in front of big insets
1433 bool const boundary_correction = ppos != 0 && boundary;
1434 if (boundary_correction)
1437 Row const & row = pm.getRow(sl.pos(), boundary);
1439 pos_type cursor_vpos = 0;
1441 Buffer const & buffer = *bv.buffer();
1442 RowMetrics const m = tm.computeRowMetrics(pit, row);
1445 pos_type const row_pos = row.pos();
1446 pos_type const end = row.endpos();
1449 cursor_vpos = row_pos;
1450 else if (ppos >= end)
1451 cursor_vpos = isRTL(buffer, par) ? row_pos : end;
1452 else if (ppos > row_pos && ppos >= end)
1453 // Place cursor after char at (logical) position pos - 1
1454 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
1455 ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
1457 // Place cursor before char at (logical) position ppos
1458 cursor_vpos = (bidi.level(ppos) % 2 == 0)
1459 ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
1461 pos_type body_pos = par.beginOfBody();
1463 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1466 // Use font span to speed things up, see below
1469 FontMetrics const & labelfm = theFontMetrics(
1470 getLabelFont(buffer, par));
1472 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1473 pos_type pos = bidi.vis2log(vpos);
1474 if (body_pos > 0 && pos == body_pos - 1) {
1476 docstring const lsep = from_utf8(par.layout()->labelsep);
1477 x += m.label_hfill + labelfm.width(lsep);
1478 if (par.isLineSeparator(body_pos - 1))
1479 x -= singleWidth(buffer, par, body_pos - 1);
1482 // Use font span to speed things up, see above
1483 if (pos < font_span.first || pos > font_span.last) {
1484 font_span = par.fontSpan(pos);
1485 font = getFont(buffer, par, pos);
1488 x += singleWidth(par, pos, par.getChar(pos), font);
1490 if (par.hfillExpansion(row, pos))
1491 x += (pos >= body_pos) ? m.hfill : m.label_hfill;
1492 else if (par.isSeparator(pos) && pos >= body_pos)
1496 // see correction above
1497 if (boundary_correction)
1498 if (getFont(buffer, par, ppos).isVisibleRightToLeft())
1499 x -= singleWidth(buffer, par, ppos);
1501 x += singleWidth(buffer, par, ppos);
1503 // Make sure inside an inset we always count from the left
1504 // edge (bidi!) -- MV
1505 if (sl.pos() < par.size()) {
1506 font = getFont(buffer, par, sl.pos());
1507 if (!boundary && font.isVisibleRightToLeft()
1508 && par.isInset(sl.pos()))
1509 x -= par.getInset(sl.pos())->width();
1515 int LyXText::cursorY(BufferView const & bv, CursorSlice const & sl, bool boundary) const
1517 //lyxerr << "LyXText::cursorY: boundary: " << boundary << std::endl;
1518 ParagraphMetrics const & pm = bv.parMetrics(this, sl.pit());
1519 if (pm.rows().empty())
1523 h -= bv.parMetrics(this, 0).rows()[0].ascent();
1524 for (pit_type pit = 0; pit < sl.pit(); ++pit) {
1525 h += bv.parMetrics(this, pit).height();
1528 if (pos && boundary)
1530 size_t const rend = pm.pos2row(pos);
1531 for (size_t rit = 0; rit != rend; ++rit)
1532 h += pm.rows()[rit].height();
1533 h += pm.rows()[rend].ascent();
1538 // Returns the current font and depth as a message.
1539 docstring LyXText::currentState(LCursor & cur)
1541 BOOST_ASSERT(this == cur.text());
1542 Buffer & buf = cur.buffer();
1543 Paragraph const & par = cur.paragraph();
1544 odocstringstream os;
1546 if (buf.params().trackChanges)
1549 Change change = par.lookupChange(cur.pos());
1551 if (change.type != Change::UNCHANGED) {
1552 Author const & a = buf.params().authors().get(change.author);
1553 os << _("Change: ") << a.name();
1554 if (!a.email().empty())
1555 os << " (" << a.email() << ")";
1556 // FIXME ctime is english, we should translate that
1557 os << _(" at ") << ctime(&change.changetime);
1561 // I think we should only show changes from the default
1563 // No, from the document font (MV)
1564 LyXFont font = real_current_font;
1565 font.reduce(buf.params().getFont());
1567 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1569 // The paragraph depth
1570 int depth = cur.paragraph().getDepth();
1572 os << bformat(_(", Depth: %1$d"), depth);
1574 // The paragraph spacing, but only if different from
1576 Spacing const & spacing = par.params().spacing();
1577 if (!spacing.isDefault()) {
1578 os << _(", Spacing: ");
1579 switch (spacing.getSpace()) {
1580 case Spacing::Single:
1583 case Spacing::Onehalf:
1586 case Spacing::Double:
1589 case Spacing::Other:
1590 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1592 case Spacing::Default:
1593 // should never happen, do nothing
1598 #ifdef DEVEL_VERSION
1599 os << _(", Inset: ") << &cur.inset();
1600 os << _(", Paragraph: ") << cur.pit();
1601 os << _(", Id: ") << par.id();
1602 os << _(", Position: ") << cur.pos();
1603 os << _(", Boundary: ") << cur.boundary();
1604 // Row & row = cur.textRow();
1605 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1611 docstring LyXText::getPossibleLabel(LCursor & cur) const
1613 pit_type pit = cur.pit();
1615 LyXLayout_ptr layout = pars_[pit].layout();
1617 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1618 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
1619 if (layout2->latextype != LATEX_PARAGRAPH) {
1625 docstring name = from_ascii(layout->latexname());
1627 // for captions, we want the abbreviation of the float type
1628 if (layout->labeltype == LABEL_SENSITIVE) {
1629 // Search for the first float or wrap inset in the iterator
1630 for (int i = cur.depth(); --i >= 0; ) {
1631 InsetBase * const in = &cur[i].inset();
1632 if (in->lyxCode() == InsetBase::FLOAT_CODE
1633 || in->lyxCode() == InsetBase::WRAP_CODE) {
1634 name = in->getInsetName();
1640 docstring text = name.substr(0, 3);
1641 if (name == "theorem")
1642 text = from_ascii("thm"); // Create a correct prefix for prettyref
1645 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
1648 docstring par_text = pars_[pit].asString(cur.buffer(), false);
1649 for (int i = 0; i < lyxrc.label_init_length; ++i) {
1650 if (par_text.empty())
1653 par_text = split(par_text, head, ' ');
1654 // Is it legal to use spaces in labels ?
1664 //pos_type LyXText::x2pos(pit_type pit, int row, int x) const
1668 // Paragraph const & par = pars_[pit];
1669 // Row const & r = par.rows()[row];
1670 // int pos = r.pos();
1671 // for (; currx < x && pos < r.endpos(); ++pos) {
1673 // currx += singleWidth(buffer, par, pos);
1675 // if (abs(lastx - x) < abs(currx - x) && pos != r.pos())
1681 pos_type LyXText::x2pos(BufferView const & bv, pit_type pit, int row,
1684 TextMetrics const & tm = bv.textMetrics(this);
1685 ParagraphMetrics const & pm = tm.parMetrics(pit);
1686 BOOST_ASSERT(!pm.rows().empty());
1687 BOOST_ASSERT(row < int(pm.rows().size()));
1689 Row const & r = pm.rows()[row];
1690 return r.pos() + tm.getColumnNearX(pit, r, x, bound);
1694 //int LyXText::pos2x(pit_type pit, pos_type pos) const
1696 // Paragraph const & par = pars_[pit];
1697 // Row const & r = par.rows()[row];
1703 void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
1705 BOOST_ASSERT(this == cur.text());
1706 pit_type pit = getPitNearY(cur.bv(), y);
1708 TextMetrics const & tm = cur.bv().textMetrics(this);
1709 ParagraphMetrics const & pm = tm.parMetrics(pit);
1711 int yy = cur.bv().coordCache().get(this, pit).y_ - pm.ascent();
1712 lyxerr[Debug::DEBUG]
1713 << BOOST_CURRENT_FUNCTION
1717 << " yy: " << yy << endl;
1720 BOOST_ASSERT(pm.rows().size());
1721 for (; r < int(pm.rows().size()) - 1; ++r) {
1722 Row const & row = pm.rows()[r];
1723 if (int(yy + row.height()) > y)
1728 Row const & row = pm.rows()[r];
1730 lyxerr[Debug::DEBUG]
1731 << BOOST_CURRENT_FUNCTION
1733 << " from pos: " << row.pos()
1738 int right_margin = tm.rightMargin(pm);
1739 pos_type const pos = row.pos()
1740 + tm.getColumnNearX(pit, row, xx, bound);
1742 lyxerr[Debug::DEBUG]
1743 << BOOST_CURRENT_FUNCTION
1744 << ": setting cursor pit: " << pit
1748 setCursor(cur, pit, pos, true, bound);
1753 void LyXText::charsTranspose(LCursor & cur)
1755 BOOST_ASSERT(this == cur.text());
1757 pos_type pos = cur.pos();
1759 // If cursor is at beginning or end of paragraph, do nothing.
1760 if (pos == cur.lastpos() || pos == 0)
1763 Paragraph & par = cur.paragraph();
1765 // Get the positions of the characters to be transposed.
1766 pos_type pos1 = pos - 1;
1767 pos_type pos2 = pos;
1769 // In change tracking mode, ignore deleted characters.
1770 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
1772 if (pos2 == cur.lastpos())
1775 while (pos1 >= 0 && par.isDeleted(pos1))
1780 // Don't do anything if one of the "characters" is not regular text.
1781 if (par.isInset(pos1) || par.isInset(pos2))
1784 // Store the characters to be transposed (including font information).
1785 char_type char1 = par.getChar(pos1);
1786 LyXFont const font1 =
1787 par.getFontSettings(cur.buffer().params(), pos1);
1789 char_type char2 = par.getChar(pos2);
1790 LyXFont const font2 =
1791 par.getFontSettings(cur.buffer().params(), pos2);
1793 // And finally, we are ready to perform the transposition.
1794 // Track the changes if Change Tracking is enabled.
1795 bool const trackChanges = cur.buffer().params().trackChanges;
1799 par.eraseChar(pos2, trackChanges);
1800 par.eraseChar(pos1, trackChanges);
1801 par.insertChar(pos1, char2, font2, trackChanges);
1802 par.insertChar(pos2, char1, font1, trackChanges);
1804 // After the transposition, move cursor to after the transposition.
1805 setCursor(cur, cur.pit(), pos2);