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"
56 #include "frontends/Selection.h"
58 #include "insets/insettext.h"
59 #include "insets/insetbibitem.h"
60 #include "insets/insethfill.h"
61 #include "insets/insetlatexaccent.h"
62 #include "insets/insetline.h"
63 #include "insets/insetnewline.h"
64 #include "insets/insetpagebreak.h"
65 #include "insets/insetoptarg.h"
66 #include "insets/insetspace.h"
67 #include "insets/insetspecialchar.h"
68 #include "insets/insettabular.h"
70 #include "support/lstrings.h"
71 #include "support/textutils.h"
72 #include "support/convert.h"
74 #include <boost/current_function.hpp>
88 using support::bformat;
89 using support::contains;
90 using support::lowercase;
92 using support::uppercase;
94 using cap::cutSelection;
95 using cap::pasteParagraphList;
97 using frontend::FontMetrics;
101 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
102 string const & token, LyXFont & font, Change & change, ErrorList & errorList)
104 BufferParams const & bp = buf.params();
106 if (token[0] != '\\') {
108 string::const_iterator cit = token.begin();
109 for (; cit != token.end(); ++cit)
110 par.insertChar(par.size(), (*cit), font, change);
112 docstring dstr = lex.getDocString();
113 docstring::const_iterator cit = dstr.begin();
114 docstring::const_iterator cend = dstr.end();
115 for (; cit != cend; ++cit)
116 par.insertChar(par.size(), *cit, font, change);
118 } else if (token == "\\begin_layout") {
120 string layoutname = lex.getString();
122 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
123 change = Change(Change::UNCHANGED);
125 LyXTextClass const & tclass = bp.getLyXTextClass();
127 if (layoutname.empty()) {
128 layoutname = tclass.defaultLayoutName();
131 bool hasLayout = tclass.hasLayout(layoutname);
134 errorList.push_back(ErrorItem(_("Unknown layout"),
135 bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
136 from_utf8(layoutname), from_utf8(tclass.name())), par.id(), 0, par.size()));
137 layoutname = tclass.defaultLayoutName();
140 par.layout(bp.getLyXTextClass()[layoutname]);
142 // Test whether the layout is obsolete.
143 LyXLayout_ptr const & layout = par.layout();
144 if (!layout->obsoleted_by().empty())
145 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
147 par.params().read(lex);
149 } else if (token == "\\end_layout") {
150 lyxerr << BOOST_CURRENT_FUNCTION
151 << ": Solitary \\end_layout in line "
152 << lex.getLineNo() << "\n"
153 << "Missing \\begin_layout?.\n";
154 } else if (token == "\\end_inset") {
155 lyxerr << BOOST_CURRENT_FUNCTION
156 << ": Solitary \\end_inset in line "
157 << lex.getLineNo() << "\n"
158 << "Missing \\begin_inset?.\n";
159 } else if (token == "\\begin_inset") {
160 InsetBase * inset = readInset(lex, buf);
162 par.insertInset(par.size(), inset, font, change);
165 docstring line = lex.getDocString();
166 errorList.push_back(ErrorItem(_("Unknown Inset"), line,
167 par.id(), 0, par.size()));
169 } else if (token == "\\family") {
171 font.setLyXFamily(lex.getString());
172 } else if (token == "\\series") {
174 font.setLyXSeries(lex.getString());
175 } else if (token == "\\shape") {
177 font.setLyXShape(lex.getString());
178 } else if (token == "\\size") {
180 font.setLyXSize(lex.getString());
181 } else if (token == "\\lang") {
183 string const tok = lex.getString();
184 Language const * lang = languages.getLanguage(tok);
186 font.setLanguage(lang);
188 font.setLanguage(bp.language);
189 lex.printError("Unknown language `$$Token'");
191 } else if (token == "\\numeric") {
193 font.setNumber(font.setLyXMisc(lex.getString()));
194 } else if (token == "\\emph") {
196 font.setEmph(font.setLyXMisc(lex.getString()));
197 } else if (token == "\\bar") {
199 string const tok = lex.getString();
202 font.setUnderbar(LyXFont::ON);
203 else if (tok == "no")
204 font.setUnderbar(LyXFont::OFF);
205 else if (tok == "default")
206 font.setUnderbar(LyXFont::INHERIT);
208 lex.printError("Unknown bar font flag "
210 } else if (token == "\\noun") {
212 font.setNoun(font.setLyXMisc(lex.getString()));
213 } else if (token == "\\color") {
215 font.setLyXColor(lex.getString());
216 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
218 // Insets don't make sense in a free-spacing context! ---Kayvan
219 if (par.isFreeSpacing()) {
220 if (token == "\\InsetSpace")
221 par.insertChar(par.size(), ' ', font, change);
222 else if (lex.isOK()) {
224 string const next_token = lex.getString();
225 if (next_token == "\\-")
226 par.insertChar(par.size(), '-', font, change);
228 lex.printError("Token `$$Token' "
230 "paragraph layout!");
234 auto_ptr<InsetBase> inset;
235 if (token == "\\SpecialChar" )
236 inset.reset(new InsetSpecialChar);
238 inset.reset(new InsetSpace);
239 inset->read(buf, lex);
240 par.insertInset(par.size(), inset.release(),
243 } else if (token == "\\i") {
244 auto_ptr<InsetBase> inset(new InsetLatexAccent);
245 inset->read(buf, lex);
246 par.insertInset(par.size(), inset.release(), font, change);
247 } else if (token == "\\backslash") {
248 par.insertChar(par.size(), '\\', font, change);
249 } else if (token == "\\newline") {
250 auto_ptr<InsetBase> inset(new InsetNewline);
251 inset->read(buf, lex);
252 par.insertInset(par.size(), inset.release(), font, change);
253 } else if (token == "\\LyXTable") {
254 auto_ptr<InsetBase> inset(new InsetTabular(buf));
255 inset->read(buf, lex);
256 par.insertInset(par.size(), inset.release(), font, change);
257 } else if (token == "\\hfill") {
258 par.insertInset(par.size(), new InsetHFill, font, change);
259 } else if (token == "\\lyxline") {
260 par.insertInset(par.size(), new InsetLine, font, change);
261 } else if (token == "\\newpage") {
262 par.insertInset(par.size(), new InsetPagebreak, font, change);
263 } else if (token == "\\clearpage") {
264 par.insertInset(par.size(), new InsetClearPage, font, change);
265 } else if (token == "\\cleardoublepage") {
266 par.insertInset(par.size(), new InsetClearDoublePage, font, change);
267 } else if (token == "\\change_unchanged") {
268 change = Change(Change::UNCHANGED);
269 } else if (token == "\\change_inserted") {
271 std::istringstream is(lex.getString());
275 if (aid >= bp.author_map.size()) {
276 errorList.push_back(ErrorItem(_("Change tracking error"),
277 bformat(_("Unknown author index for insertion: %1$d\n"), aid),
278 par.id(), 0, par.size()));
279 change = Change(Change::UNCHANGED);
281 change = Change(Change::INSERTED, bp.author_map[aid], ct);
282 } else if (token == "\\change_deleted") {
284 std::istringstream is(lex.getString());
288 if (aid >= bp.author_map.size()) {
289 errorList.push_back(ErrorItem(_("Change tracking error"),
290 bformat(_("Unknown author index for deletion: %1$d\n"), aid),
291 par.id(), 0, par.size()));
292 change = Change(Change::UNCHANGED);
294 change = Change(Change::DELETED, bp.author_map[aid], ct);
297 errorList.push_back(ErrorItem(_("Unknown token"),
298 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
300 par.id(), 0, par.size()));
305 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex,
306 ErrorList & errorList)
309 string token = lex.getString();
311 Change change(Change::UNCHANGED);
314 readParToken(buf, par, lex, token, font, change, errorList);
317 token = lex.getString();
322 if (token == "\\end_layout") {
323 //Ok, paragraph finished
327 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
328 << token << '\'' << endl;
329 if (token == "\\begin_layout" || token == "\\end_document"
330 || token == "\\end_inset" || token == "\\begin_deeper"
331 || token == "\\end_deeper") {
332 lex.pushToken(token);
333 lyxerr << "Paragraph ended in line "
334 << lex.getLineNo() << "\n"
335 << "Missing \\end_layout.\n";
339 // Final change goes to paragraph break:
340 par.setChange(par.size(), change);
342 // Initialize begin_of_body_ on load; redoParagraph maintains
343 par.setBeginOfBody();
351 double LyXText::spacing(Buffer const & buffer,
352 Paragraph const & par) const
354 if (par.params().spacing().isDefault())
355 return buffer.params().spacing().getValue();
356 return par.params().spacing().getValue();
360 int LyXText::singleWidth(Buffer const & buffer, Paragraph const & par,
363 return singleWidth(par, pos, par.getChar(pos),
364 getFont(buffer, par, pos));
368 int LyXText::singleWidth(Paragraph const & par,
369 pos_type pos, char_type c, LyXFont const & font) const
371 // The most common case is handled first (Asger)
372 if (isPrintable(c)) {
373 Language const * language = font.language();
374 if (language->rightToLeft()) {
375 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
376 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
377 && language->lang() == "arabic") {
378 if (Encodings::isComposeChar_arabic(c))
380 c = par.transformChar(c, pos);
381 } else if (language->lang() == "hebrew" &&
382 Encodings::isComposeChar_hebrew(c))
385 return theFontMetrics(font).width(c);
388 if (c == Paragraph::META_INSET)
389 return par.getInset(pos)->width();
391 return theFontMetrics(font).width(c);
395 int LyXText::leftMargin(Buffer const & buffer, int max_width, pit_type pit) const
397 BOOST_ASSERT(pit >= 0);
398 BOOST_ASSERT(pit < int(pars_.size()));
399 return leftMargin(buffer, max_width, pit, pars_[pit].size());
403 int LyXText::leftMargin(Buffer const & buffer, int max_width,
404 pit_type const pit, pos_type const pos) const
406 BOOST_ASSERT(pit >= 0);
407 BOOST_ASSERT(pit < int(pars_.size()));
408 Paragraph const & par = pars_[pit];
409 BOOST_ASSERT(pos >= 0);
410 BOOST_ASSERT(pos <= par.size());
411 //lyxerr << "LyXText::leftMargin: pit: " << pit << " pos: " << pos << endl;
412 LyXTextClass const & tclass = buffer.params().getLyXTextClass();
413 LyXLayout_ptr const & layout = par.layout();
415 string parindent = layout->parindent;
419 if (isMainText(buffer))
420 l_margin += changebarMargin();
423 docstring leftm = from_utf8(tclass.leftmargin());
424 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
426 if (par.getDepth() != 0) {
427 // find the next level paragraph
428 pit_type newpar = outerHook(pit, pars_);
429 if (newpar != pit_type(pars_.size())) {
430 if (pars_[newpar].layout()->isEnvironment()) {
431 l_margin = leftMargin(buffer, max_width, newpar);
433 if (par.layout() == tclass.defaultLayout()) {
434 if (pars_[newpar].params().noindent())
437 parindent = pars_[newpar].layout()->parindent;
442 // This happens after sections in standard classes. The 1.3.x
443 // code compared depths too, but it does not seem necessary
445 if (par.layout() == tclass.defaultLayout()
446 && pit > 0 && pars_[pit - 1].layout()->nextnoindent)
449 LyXFont const labelfont = getLabelFont(buffer, par);
450 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
452 switch (layout->margintype) {
454 if (!layout->leftmargin.empty()) {
456 docstring leftm = from_utf8(layout->leftmargin);
457 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
459 if (!par.getLabelstring().empty()) {
461 docstring labin = from_utf8(layout->labelindent);
462 l_margin += labelfont_metrics.signedWidth(labin);
463 docstring labstr = par.getLabelstring();
464 l_margin += labelfont_metrics.width(labstr);
465 docstring labsep = from_utf8(layout->labelsep);
466 l_margin += labelfont_metrics.width(labsep);
470 case MARGIN_MANUAL: {
472 docstring labin = from_utf8(layout->labelindent);
473 l_margin += labelfont_metrics.signedWidth(labin);
474 // The width of an empty par, even with manual label, should be 0
475 if (!par.empty() && pos >= par.beginOfBody()) {
476 if (!par.getLabelWidthString().empty()) {
477 docstring labstr = par.getLabelWidthString();
478 l_margin += labelfont_metrics.width(labstr);
479 docstring labsep = from_utf8(layout->labelsep);
480 l_margin += labelfont_metrics.width(labsep);
486 case MARGIN_STATIC: {
488 docstring leftm = from_utf8(layout->leftmargin);
489 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm)
490 * 4 / (par.getDepth() + 4);
494 case MARGIN_FIRST_DYNAMIC:
495 if (layout->labeltype == LABEL_MANUAL) {
496 if (pos >= par.beginOfBody()) {
498 l_margin += labelfont_metrics.signedWidth(
499 from_utf8(layout->leftmargin));
502 l_margin += labelfont_metrics.signedWidth(
503 from_utf8(layout->labelindent));
506 // Special case to fix problems with
508 || (layout->labeltype == LABEL_STATIC
509 && layout->latextype == LATEX_ENVIRONMENT
510 && !isFirstInSequence(pit, pars_))) {
512 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->leftmargin));
513 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
514 && layout->labeltype != LABEL_BIBLIO
515 && layout->labeltype !=
516 LABEL_CENTERED_TOP_ENVIRONMENT) {
517 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->labelindent));
518 l_margin += labelfont_metrics.width(from_utf8(layout->labelsep));
519 l_margin += labelfont_metrics.width(par.getLabelstring());
523 case MARGIN_RIGHT_ADDRESS_BOX: {
525 // ok, a terrible hack. The left margin depends on the widest
526 // row in this paragraph.
527 RowList::iterator rit = par.rows().begin();
528 RowList::iterator end = par.rows().end();
530 #warning This is wrong.
532 int minfill = max_width;
533 for ( ; rit != end; ++rit)
534 if (rit->fill() < minfill)
535 minfill = rit->fill();
536 l_margin += theFontMetrics(params.getFont()).signedWidth(layout->leftmargin);
539 // also wrong, but much shorter.
540 l_margin += max_width / 2;
545 if (!par.params().leftIndent().zero())
546 l_margin += par.params().leftIndent().inPixels(max_width);
550 if (par.params().align() == LYX_ALIGN_LAYOUT)
551 align = layout->align;
553 align = par.params().align();
555 // set the correct parindent
557 && (layout->labeltype == LABEL_NO_LABEL
558 || layout->labeltype == LABEL_TOP_ENVIRONMENT
559 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
560 || (layout->labeltype == LABEL_STATIC
561 && layout->latextype == LATEX_ENVIRONMENT
562 && !isFirstInSequence(pit, pars_)))
563 && align == LYX_ALIGN_BLOCK
564 && !par.params().noindent()
565 // in some insets, paragraphs are never indented
566 && !(par.inInset() && par.inInset()->neverIndent(buffer))
567 // display style insets are always centered, omit indentation
570 && par.getInset(pos)->display())
571 && (par.layout() != tclass.defaultLayout()
572 || buffer.params().paragraph_separation ==
573 BufferParams::PARSEP_INDENT))
575 docstring din = from_utf8(parindent);
576 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(din);
583 LColor_color LyXText::backgroundColor() const
585 return LColor_color(LColor::color(background_color_));
589 void LyXText::breakParagraph(LCursor & cur, bool keep_layout)
591 BOOST_ASSERT(this == cur.text());
593 Paragraph & cpar = cur.paragraph();
594 pit_type cpit = cur.pit();
596 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
597 LyXLayout_ptr const & layout = cpar.layout();
599 // this is only allowed, if the current paragraph is not empty
600 // or caption and if it has not the keepempty flag active
601 if (cur.lastpos() == 0 && !cpar.allowEmpty() &&
602 layout->labeltype != LABEL_SENSITIVE)
605 // a layout change may affect also the following paragraph
606 recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
608 // Always break behind a space
609 // It is better to erase the space (Dekel)
610 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
611 cpar.eraseChar(cur.pos(), cur.buffer().params().trackChanges);
613 // What should the layout for the new paragraph be?
614 int preserve_layout = 0;
618 preserve_layout = layout->isEnvironment();
620 // We need to remember this before we break the paragraph, because
621 // that invalidates the layout variable
622 bool sensitive = layout->labeltype == LABEL_SENSITIVE;
624 // we need to set this before we insert the paragraph.
625 bool const isempty = cpar.allowEmpty() && cpar.empty();
627 lyx::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
628 cur.pos(), preserve_layout);
630 // After this, neither paragraph contains any rows!
633 pit_type next_par = cpit + 1;
635 // well this is the caption hack since one caption is really enough
638 // set to standard-layout
639 pars_[cpit].applyLayout(tclass.defaultLayout());
641 // set to standard-layout
642 pars_[next_par].applyLayout(tclass.defaultLayout());
645 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
646 if (!pars_[next_par].eraseChar(0, cur.buffer().params().trackChanges))
647 break; // the character couldn't be deleted physically due to change tracking
650 ParIterator current_it(cur);
651 ParIterator last_it(cur);
655 updateLabels(cur.buffer(), current_it, last_it);
657 // A singlePar update is not enough in this case.
658 cur.updateFlags(Update::Force);
660 // This check is necessary. Otherwise the new empty paragraph will
661 // be deleted automatically. And it is more friendly for the user!
662 if (cur.pos() != 0 || isempty)
663 setCursor(cur.top(), cur.pit() + 1, 0);
665 setCursor(cur.top(), cur.pit(), 0);
669 // insert a character, moves all the following breaks in the
670 // same Paragraph one to the right and make a rebreak
671 void LyXText::insertChar(LCursor & cur, char_type c)
673 BOOST_ASSERT(this == cur.text());
674 BOOST_ASSERT(c != Paragraph::META_INSET);
676 recordUndo(cur, Undo::INSERT);
678 Buffer const & buffer = cur.buffer();
679 Paragraph & par = cur.paragraph();
680 // try to remove this
681 pit_type const pit = cur.pit();
683 bool const freeSpacing = par.layout()->free_spacing ||
686 if (lyxrc.auto_number) {
687 static docstring const number_operators = from_ascii("+-/*");
688 static docstring const number_unary_operators = from_ascii("+-");
689 static docstring const number_seperators = from_ascii(".,:");
691 if (current_font.number() == LyXFont::ON) {
692 if (!isDigit(c) && !contains(number_operators, c) &&
693 !(contains(number_seperators, c) &&
695 cur.pos() != cur.lastpos() &&
696 getFont(buffer, par, cur.pos()).number() == LyXFont::ON &&
697 getFont(buffer, par, cur.pos() - 1).number() == LyXFont::ON)
699 number(cur); // Set current_font.number to OFF
700 } else if (isDigit(c) &&
701 real_current_font.isVisibleRightToLeft()) {
702 number(cur); // Set current_font.number to ON
704 if (cur.pos() != 0) {
705 char_type const c = par.getChar(cur.pos() - 1);
706 if (contains(number_unary_operators, c) &&
708 || par.isSeparator(cur.pos() - 2)
709 || par.isNewline(cur.pos() - 2))
711 setCharFont(buffer, pit, cur.pos() - 1, current_font);
712 } else if (contains(number_seperators, c)
714 && getFont(buffer, par, cur.pos() - 2).number() == LyXFont::ON) {
715 setCharFont(buffer, pit, cur.pos() - 1, current_font);
721 // First check, if there will be two blanks together or a blank at
722 // the beginning of a paragraph.
723 // I decided to handle blanks like normal characters, the main
724 // difference are the special checks when calculating the row.fill
725 // (blank does not count at the end of a row) and the check here
727 // When the free-spacing option is set for the current layout,
728 // disable the double-space checking
729 if (!freeSpacing && isLineSeparatorChar(c)) {
730 if (cur.pos() == 0) {
731 static bool sent_space_message = false;
732 if (!sent_space_message) {
733 cur.message(_("You cannot insert a space at the "
734 "beginning of a paragraph. Please read the Tutorial."));
735 sent_space_message = true;
739 BOOST_ASSERT(cur.pos() > 0);
740 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
741 && !par.isDeleted(cur.pos() - 1)) {
742 static bool sent_space_message = false;
743 if (!sent_space_message) {
744 cur.message(_("You cannot type two spaces this way. "
745 "Please read the Tutorial."));
746 sent_space_message = true;
752 par.insertChar(cur.pos(), c, current_font, cur.buffer().params().trackChanges);
754 // cur.updateFlags(Update::Force);
755 setCursor(cur.top(), cur.pit(), cur.pos() + 1);
760 void LyXText::charInserted()
762 // Here we call finishUndo for every 20 characters inserted.
763 // This is from my experience how emacs does it. (Lgb)
764 static unsigned int counter;
774 // the cursor set functions have a special mechanism. When they
775 // realize, that you left an empty paragraph, they will delete it.
777 bool LyXText::cursorRightOneWord(LCursor & cur)
779 BOOST_ASSERT(this == cur.text());
783 if (old.pos() == old.lastpos() && old.pit() != old.lastpit()) {
787 // Advance through word.
788 while (old.pos() != old.lastpos() && old.paragraph().isLetter(old.pos()))
790 // Skip through trailing nonword stuff.
791 while (old.pos() != old.lastpos() && !old.paragraph().isLetter(old.pos()))
794 return setCursor(cur, old.pit(), old.pos());
798 bool LyXText::cursorLeftOneWord(LCursor & cur)
800 BOOST_ASSERT(this == cur.text());
804 if (old.pos() == 0 && old.pit() != 0) {
806 old.pos() = old.lastpos();
808 // Skip through initial nonword stuff.
809 while (old.pos() != 0 && !old.paragraph().isLetter(old.pos() - 1))
811 // Advance through word.
812 while (old.pos() != 0 && old.paragraph().isLetter(old.pos() - 1))
815 return setCursor(cur, old.pit(), old.pos());
819 void LyXText::selectWord(LCursor & cur, word_location loc)
821 BOOST_ASSERT(this == cur.text());
822 CursorSlice from = cur.top();
823 CursorSlice to = cur.top();
824 getWord(from, to, loc);
825 if (cur.top() != from)
826 setCursor(cur, from.pit(), from.pos());
830 setCursor(cur, to.pit(), to.pos());
832 theSelection().haveSelection(cur.selection());
836 // Select the word currently under the cursor when no
837 // selection is currently set
838 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
840 BOOST_ASSERT(this == cur.text());
843 selectWord(cur, loc);
844 return cur.selection();
848 void LyXText::acceptOrRejectChanges(LCursor & cur, ChangeOp op)
850 BOOST_ASSERT(this == cur.text());
852 if (!cur.selection())
855 recordUndoSelection(cur, Undo::ATOMIC);
857 pit_type begPit = cur.selectionBegin().pit();
858 pit_type endPit = cur.selectionEnd().pit();
860 pos_type begPos = cur.selectionBegin().pos();
861 pos_type endPos = cur.selectionEnd().pos();
863 // keep selection info, because endPos becomes invalid after the first loop
864 bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
866 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
868 for (pit_type pit = begPit; pit <= endPit; ++pit) {
869 pos_type parSize = pars_[pit].size();
871 // ignore empty paragraphs; otherwise, an assertion will fail for
872 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
876 // do not consider first paragraph if the cursor starts at pos size()
877 if (pit == begPit && begPos == parSize)
880 // do not consider last paragraph if the cursor ends at pos 0
881 if (pit == endPit && endPos == 0)
882 break; // last iteration anyway
884 pos_type left = (pit == begPit ? begPos : 0);
885 pos_type right = (pit == endPit ? endPos : parSize);
888 pars_[pit].acceptChanges(cur.buffer().params(), left, right);
890 pars_[pit].rejectChanges(cur.buffer().params(), left, right);
894 // next, accept/reject imaginary end-of-par characters
896 for (pit_type pit = begPit; pit <= endPit; ++pit) {
897 pos_type pos = pars_[pit].size();
899 // skip if the selection ends before the end-of-par
900 if (pit == endPit && endsBeforeEndOfPar)
901 break; // last iteration anyway
903 // skip if this is not the last paragraph of the document
904 // note: the user should be able to accept/reject the par break of the last par!
905 if (pit == endPit && pit != pars_.size() - 1)
906 break; // last iteration anway
909 if (pars_[pit].isInserted(pos)) {
910 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
911 } else if (pars_[pit].isDeleted(pos)) {
912 if (pit == pars_.size() - 1) {
913 // we cannot remove a par break at the end of the last paragraph;
914 // instead, we mark it unchanged
915 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
917 mergeParagraph(cur.buffer().params(), pars_, pit);
923 if (pars_[pit].isDeleted(pos)) {
924 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
925 } else if (pars_[pit].isInserted(pos)) {
926 if (pit == pars_.size() - 1) {
927 // we mark the par break at the end of the last paragraph unchanged
928 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
930 mergeParagraph(cur.buffer().params(), pars_, pit);
938 // finally, invoke the DEPM
940 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer().params().trackChanges);
945 cur.clearSelection();
946 setCursorIntern(cur, begPit, begPos);
947 cur.updateFlags(Update::Force);
948 updateLabels(cur.buffer());
952 void LyXText::acceptChanges(BufferParams const & bparams)
954 pit_type pars_size = (pit_type) pars_.size();
956 // first, accept changes within each individual paragraph
957 // (do not consider end-of-par)
958 for (pit_type pit = 0; pit < pars_size; ++pit) {
959 if (!pars_[pit].empty()) // prevent assertion failure
960 pars_[pit].acceptChanges(bparams, 0, pars_[pit].size());
963 // next, accept imaginary end-of-par characters
964 for (pit_type pit = 0; pit < pars_size; ++pit) {
965 pos_type pos = pars_[pit].size();
967 if (pars_[pit].isInserted(pos)) {
968 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
969 } else if (pars_[pit].isDeleted(pos)) {
970 if (pit == pars_size - 1) {
971 // we cannot remove a par break at the end of the last
972 // paragraph; instead, we mark it unchanged
973 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
975 mergeParagraph(bparams, pars_, pit);
982 // finally, invoke the DEPM
983 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
987 void LyXText::rejectChanges(BufferParams const & bparams)
989 pit_type pars_size = (pit_type) pars_.size();
991 // first, reject changes within each individual paragraph
992 // (do not consider end-of-par)
993 for (pit_type pit = 0; pit < pars_size; ++pit) {
994 if (!pars_[pit].empty()) // prevent assertion failure
995 pars_[pit].rejectChanges(bparams, 0, pars_[pit].size());
998 // next, reject imaginary end-of-par characters
999 for (pit_type pit = 0; pit < pars_size; ++pit) {
1000 pos_type pos = pars_[pit].size();
1002 if (pars_[pit].isDeleted(pos)) {
1003 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1004 } else if (pars_[pit].isInserted(pos)) {
1005 if (pit == pars_size - 1) {
1006 // we mark the par break at the end of the last
1007 // paragraph unchanged
1008 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1010 mergeParagraph(bparams, pars_, pit);
1017 // finally, invoke the DEPM
1018 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
1022 // Delete from cursor up to the end of the current or next word.
1023 void LyXText::deleteWordForward(LCursor & cur)
1025 BOOST_ASSERT(this == cur.text());
1026 if (cur.lastpos() == 0)
1030 cur.selection() = true;
1031 cursorRightOneWord(cur);
1033 cutSelection(cur, true, false);
1038 // Delete from cursor to start of current or prior word.
1039 void LyXText::deleteWordBackward(LCursor & cur)
1041 BOOST_ASSERT(this == cur.text());
1042 if (cur.lastpos() == 0)
1046 cur.selection() = true;
1047 cursorLeftOneWord(cur);
1049 cutSelection(cur, true, false);
1054 // Kill to end of line.
1055 void LyXText::deleteLineForward(LCursor & cur)
1057 BOOST_ASSERT(this == cur.text());
1058 if (cur.lastpos() == 0) {
1059 // Paragraph is empty, so we just go to the right
1063 cur.selection() = true; // to avoid deletion
1066 // What is this test for ??? (JMarc)
1067 if (!cur.selection())
1068 deleteWordForward(cur);
1070 cutSelection(cur, true, false);
1075 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1077 BOOST_ASSERT(this == cur.text());
1081 if (cur.selection()) {
1082 from = cur.selBegin();
1086 getWord(from, to, PARTIAL_WORD);
1087 cursorRightOneWord(cur);
1090 recordUndoSelection(cur);
1092 pos_type pos = from.pos();
1093 int par = from.pit();
1095 while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
1097 if (pos == pars_[pit].size()) {
1102 char_type c = pars_[pit].getChar(pos);
1103 if (c != Paragraph::META_INSET) {
1105 case text_lowercase:
1108 case text_capitalization:
1110 action = text_lowercase;
1112 case text_uppercase:
1118 // FIXME: change tracking (MG)
1119 // sorry but we are no longer allowed to set a single character directly
1120 // we have to rewrite this method in terms of erase&insert operations
1121 //pars_[pit].setChar(pos, c);
1127 bool LyXText::erase(LCursor & cur)
1129 BOOST_ASSERT(this == cur.text());
1130 bool needsUpdate = false;
1131 Paragraph & par = cur.paragraph();
1133 if (cur.pos() != cur.lastpos()) {
1134 // this is the code for a normal delete, not pasting
1136 recordUndo(cur, Undo::DELETE);
1137 if(!par.eraseChar(cur.pos(), cur.buffer().params().trackChanges)) {
1138 // the character has been logically deleted only => skip it
1139 cur.forwardPosNoDescend();
1143 if (cur.pit() == cur.lastpit())
1144 return dissolveInset(cur);
1146 if (!par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1147 par.setChange(cur.pos(), Change(Change::DELETED));
1151 setCursorIntern(cur, cur.pit() + 1, 0);
1152 needsUpdate = backspacePos0(cur);
1157 // Make sure the cursor is correct. Is this really needed?
1158 // No, not really... at least not here!
1159 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1166 bool LyXText::backspacePos0(LCursor & cur)
1168 BOOST_ASSERT(this == cur.text());
1172 bool needsUpdate = false;
1174 BufferParams const & bufparams = cur.buffer().params();
1175 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1176 ParagraphList & plist = cur.text()->paragraphs();
1177 Paragraph const & par = cur.paragraph();
1178 LCursor prevcur = cur;
1180 prevcur.pos() = prevcur.lastpos();
1181 Paragraph const & prevpar = prevcur.paragraph();
1183 // is it an empty paragraph?
1184 if (cur.lastpos() == 0
1185 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1186 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1187 plist.erase(boost::next(plist.begin(), cur.pit()));
1190 // is previous par empty?
1191 else if (prevcur.lastpos() == 0
1192 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1193 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1194 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1197 // Pasting is not allowed, if the paragraphs have different
1198 // layouts. I think it is a real bug of all other
1199 // word processors to allow it. It confuses the user.
1200 // Correction: Pasting is always allowed with standard-layout
1201 else if (par.layout() == prevpar.layout()
1202 || par.layout() == tclass.defaultLayout()) {
1203 recordUndo(cur, Undo::ATOMIC, prevcur.pit());
1204 mergeParagraph(bufparams, plist, prevcur.pit());
1209 updateLabels(cur.buffer());
1210 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1217 bool LyXText::backspace(LCursor & cur)
1219 BOOST_ASSERT(this == cur.text());
1220 bool needsUpdate = false;
1221 if (cur.pos() == 0) {
1223 return dissolveInset(cur);
1225 Paragraph & prev_par = pars_[cur.pit() - 1];
1227 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1228 prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1229 setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1232 // The cursor is at the beginning of a paragraph, so
1233 // the backspace will collapse two paragraphs into one.
1234 needsUpdate = backspacePos0(cur);
1237 // this is the code for a normal backspace, not pasting
1239 recordUndo(cur, Undo::DELETE);
1240 // We used to do cursorLeftIntern() here, but it is
1241 // not a good idea since it triggers the auto-delete
1242 // mechanism. So we do a cursorLeftIntern()-lite,
1243 // without the dreaded mechanism. (JMarc)
1244 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1245 false, cur.boundary());
1246 cur.paragraph().eraseChar(cur.pos(), cur.buffer().params().trackChanges);
1249 if (cur.pos() == cur.lastpos())
1250 setCurrentFont(cur);
1252 // A singlePar update is not enough in this case.
1253 // cur.updateFlags(Update::Force);
1254 setCursor(cur.top(), cur.pit(), cur.pos());
1260 bool LyXText::dissolveInset(LCursor & cur) {
1261 BOOST_ASSERT(this == cur.text());
1263 if (isMainText(*cur.bv().buffer()) || cur.inset().nargs() != 1)
1266 recordUndoInset(cur);
1267 cur.selHandle(false);
1269 pos_type spos = cur.pos();
1270 pit_type spit = cur.pit();
1271 ParagraphList plist;
1272 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1273 plist = paragraphs();
1275 // store cursor offset
1279 Buffer & b = cur.buffer();
1280 cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1281 if (!plist.empty()) {
1282 // ERT paragraphs have the Language latex_language.
1283 // This is invalid outside of ERT, so we need to
1284 // change it to the buffer language.
1285 ParagraphList::iterator it = plist.begin();
1286 ParagraphList::iterator it_end = plist.end();
1287 for (; it != it_end; it++) {
1288 it->changeLanguage(b.params(), latex_language,
1292 pasteParagraphList(cur, plist, b.params().textclass,
1293 b.errorList("Paste"));
1295 cur.pit() = std::min(cur.lastpit(), spit);
1296 cur.pos() = std::min(cur.lastpos(), spos);
1298 cur.clearSelection();
1304 // only used for inset right now. should also be used for main text
1305 void LyXText::draw(PainterInfo & pi, int x, int y) const
1307 paintTextInset(*this, pi, x, y);
1311 // only used for inset right now. should also be used for main text
1312 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
1314 LCursor & cur = pi.base.bv->cursor();
1315 if (!cur.selection())
1317 if (!ptr_cmp(cur.text(), this))
1320 if (lyxerr.debugging(Debug::DEBUG)) {
1321 lyxerr[Debug::DEBUG]
1322 << BOOST_CURRENT_FUNCTION
1323 << "draw selection at " << x
1327 DocIterator beg = cur.selectionBegin();
1328 DocIterator end = cur.selectionEnd();
1330 BufferView & bv = *pi.base.bv;
1331 Buffer const & buffer = *bv.buffer();
1333 // the selection doesn't touch the visible screen
1334 if (bv_funcs::status(&bv, beg) == bv_funcs::CUR_BELOW
1335 || bv_funcs::status(&bv, end) == bv_funcs::CUR_ABOVE)
1338 Paragraph const & par1 = pars_[beg.pit()];
1339 Paragraph const & par2 = pars_[end.pit()];
1340 TextMetrics const & tm = bv.textMetrics(this);
1341 ParagraphMetrics const & pm1 = tm.parMetrics(beg.pit());
1342 ParagraphMetrics const & pm2 = tm.parMetrics(end.pit());
1344 bool const above = (bv_funcs::status(&bv, beg)
1345 == bv_funcs::CUR_ABOVE);
1346 bool const below = (bv_funcs::status(&bv, end)
1347 == bv_funcs::CUR_BELOW);
1355 Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
1356 y1 = bv_funcs::getPos(bv, beg, beg.boundary()).y_ - row1.ascent();
1357 y2 = y1 + row1.height();
1358 int const startx = cursorX(bv, beg.top(), beg.boundary());
1359 if (!isRTL(buffer, par1)) {
1361 x2 = 0 + tm.width();
1371 Y1 = bv.workHeight();
1372 Y2 = bv.workHeight();
1376 Row const & row2 = pm2.getRow(end.pos(), end.boundary());
1377 Y1 = bv_funcs::getPos(bv, end, end.boundary()).y_ - row2.ascent();
1378 Y2 = Y1 + row2.height();
1379 int const endx = cursorX(bv, end.top(), end.boundary());
1380 if (!isRTL(buffer, par2)) {
1386 X2 = 0 + tm.width();
1390 if (!above && !below && &pm1.getRow(beg.pos(), beg.boundary())
1391 == &pm2.getRow(end.pos(), end.boundary()))
1393 // paint only one rectangle
1394 int const b( !isRTL(*bv.buffer(), par1) ? x + x1 : x + X1 );
1395 int const w( !isRTL(*bv.buffer(), par1) ? X2 - x1 : x2 - X1 );
1396 pi.pain.fillRectangle(b, y1, w, y2 - y1, LColor::selection);
1400 if (lyxerr.debugging(Debug::DEBUG)) {
1401 lyxerr[Debug::DEBUG] << " y1: " << y1 << " y2: " << y2
1402 << "X1:" << X1 << " x2: " << X2 << " wid: " << tm.width()
1406 // paint upper rectangle
1407 pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
1409 // paint bottom rectangle
1410 pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
1412 // paint center rectangle
1413 pi.pain.fillRectangle(x, y2, tm.width(),
1414 Y1 - y2, LColor::selection);
1418 bool LyXText::isLastRow(pit_type pit, Row const & row) const
1420 return row.endpos() >= pars_[pit].size()
1421 && pit + 1 == pit_type(paragraphs().size());
1425 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
1427 return row.pos() == 0 && pit == 0;
1431 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1432 word_location const loc)
1434 Paragraph const & from_par = pars_[from.pit()];
1436 case WHOLE_WORD_STRICT:
1437 if (from.pos() == 0 || from.pos() == from_par.size()
1438 || !from_par.isLetter(from.pos())
1439 || !from_par.isLetter(from.pos() - 1)) {
1443 // no break here, we go to the next
1446 // If we are already at the beginning of a word, do nothing
1447 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
1449 // no break here, we go to the next
1452 // always move the cursor to the beginning of previous word
1453 while (from.pos() && from_par.isLetter(from.pos() - 1))
1457 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1461 // no need to move the 'from' cursor
1465 Paragraph & to_par = pars_[to.pit()];
1466 while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
1471 void LyXText::write(Buffer const & buf, std::ostream & os) const
1473 ParagraphList::const_iterator pit = paragraphs().begin();
1474 ParagraphList::const_iterator end = paragraphs().end();
1476 for (; pit != end; ++pit)
1477 pit->write(buf, os, buf.params(), dth);
1481 bool LyXText::read(Buffer const & buf, LyXLex & lex, ErrorList & errorList)
1483 depth_type depth = 0;
1485 while (lex.isOK()) {
1487 string const token = lex.getString();
1492 if (token == "\\end_inset")
1495 if (token == "\\end_body")
1498 if (token == "\\begin_body")
1501 if (token == "\\end_document")
1504 if (token == "\\begin_layout") {
1505 lex.pushToken(token);
1508 par.params().depth(depth);
1509 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1510 pars_.push_back(par);
1512 // FIXME: goddamn InsetTabular makes us pass a Buffer
1514 lyx::readParagraph(buf, pars_.back(), lex, errorList);
1516 } else if (token == "\\begin_deeper") {
1518 } else if (token == "\\end_deeper") {
1520 lex.printError("\\end_deeper: " "depth is already null");
1525 lyxerr << "Handling unknown body token: `"
1526 << token << '\'' << endl;
1532 int LyXText::cursorX(BufferView const & bv, CursorSlice const & sl,
1533 bool boundary) const
1535 TextMetrics const & tm = bv.textMetrics(sl.text());
1536 pit_type const pit = sl.pit();
1537 Paragraph const & par = pars_[pit];
1538 ParagraphMetrics const & pm = tm.parMetrics(pit);
1539 if (pm.rows().empty())
1542 pos_type ppos = sl.pos();
1543 // Correct position in front of big insets
1544 bool const boundary_correction = ppos != 0 && boundary;
1545 if (boundary_correction)
1548 Row const & row = pm.getRow(sl.pos(), boundary);
1550 pos_type cursor_vpos = 0;
1552 Buffer const & buffer = *bv.buffer();
1553 RowMetrics const m = tm.computeRowMetrics(pit, row);
1556 pos_type const row_pos = row.pos();
1557 pos_type const end = row.endpos();
1560 cursor_vpos = row_pos;
1561 else if (ppos >= end)
1562 cursor_vpos = isRTL(buffer, par) ? row_pos : end;
1563 else if (ppos > row_pos && ppos >= end)
1564 // Place cursor after char at (logical) position pos - 1
1565 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
1566 ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
1568 // Place cursor before char at (logical) position ppos
1569 cursor_vpos = (bidi.level(ppos) % 2 == 0)
1570 ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
1572 pos_type body_pos = par.beginOfBody();
1574 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1577 // Use font span to speed things up, see below
1580 FontMetrics const & labelfm = theFontMetrics(
1581 getLabelFont(buffer, par));
1583 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1584 pos_type pos = bidi.vis2log(vpos);
1585 if (body_pos > 0 && pos == body_pos - 1) {
1587 docstring const lsep = from_utf8(par.layout()->labelsep);
1588 x += m.label_hfill + labelfm.width(lsep);
1589 if (par.isLineSeparator(body_pos - 1))
1590 x -= singleWidth(buffer, par, body_pos - 1);
1593 // Use font span to speed things up, see above
1594 if (pos < font_span.first || pos > font_span.last) {
1595 font_span = par.fontSpan(pos);
1596 font = getFont(buffer, par, pos);
1599 x += singleWidth(par, pos, par.getChar(pos), font);
1601 if (par.hfillExpansion(row, pos))
1602 x += (pos >= body_pos) ? m.hfill : m.label_hfill;
1603 else if (par.isSeparator(pos) && pos >= body_pos)
1607 // see correction above
1608 if (boundary_correction)
1609 if (getFont(buffer, par, ppos).isVisibleRightToLeft())
1610 x -= singleWidth(buffer, par, ppos);
1612 x += singleWidth(buffer, par, ppos);
1614 // Make sure inside an inset we always count from the left
1615 // edge (bidi!) -- MV
1616 if (sl.pos() < par.size()) {
1617 font = getFont(buffer, par, sl.pos());
1618 if (!boundary && font.isVisibleRightToLeft()
1619 && par.isInset(sl.pos()))
1620 x -= par.getInset(sl.pos())->width();
1626 int LyXText::cursorY(BufferView const & bv, CursorSlice const & sl, bool boundary) const
1628 //lyxerr << "LyXText::cursorY: boundary: " << boundary << std::endl;
1629 ParagraphMetrics const & pm = bv.parMetrics(this, sl.pit());
1630 if (pm.rows().empty())
1634 h -= bv.parMetrics(this, 0).rows()[0].ascent();
1635 for (pit_type pit = 0; pit < sl.pit(); ++pit) {
1636 h += bv.parMetrics(this, pit).height();
1639 if (pos && boundary)
1641 size_t const rend = pm.pos2row(pos);
1642 for (size_t rit = 0; rit != rend; ++rit)
1643 h += pm.rows()[rit].height();
1644 h += pm.rows()[rend].ascent();
1649 // Returns the current font and depth as a message.
1650 docstring LyXText::currentState(LCursor & cur)
1652 BOOST_ASSERT(this == cur.text());
1653 Buffer & buf = cur.buffer();
1654 Paragraph const & par = cur.paragraph();
1655 odocstringstream os;
1657 if (buf.params().trackChanges)
1658 os << _("[Change Tracking] ");
1660 Change change = par.lookupChange(cur.pos());
1662 if (change.type != Change::UNCHANGED) {
1663 Author const & a = buf.params().authors().get(change.author);
1664 os << _("Change: ") << a.name();
1665 if (!a.email().empty())
1666 os << " (" << a.email() << ")";
1667 // FIXME ctime is english, we should translate that
1668 os << _(" at ") << ctime(&change.changetime);
1672 // I think we should only show changes from the default
1674 // No, from the document font (MV)
1675 LyXFont font = real_current_font;
1676 font.reduce(buf.params().getFont());
1678 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1680 // The paragraph depth
1681 int depth = cur.paragraph().getDepth();
1683 os << bformat(_(", Depth: %1$d"), depth);
1685 // The paragraph spacing, but only if different from
1687 Spacing const & spacing = par.params().spacing();
1688 if (!spacing.isDefault()) {
1689 os << _(", Spacing: ");
1690 switch (spacing.getSpace()) {
1691 case Spacing::Single:
1694 case Spacing::Onehalf:
1697 case Spacing::Double:
1700 case Spacing::Other:
1701 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1703 case Spacing::Default:
1704 // should never happen, do nothing
1709 #ifdef DEVEL_VERSION
1710 os << _(", Inset: ") << &cur.inset();
1711 os << _(", Paragraph: ") << cur.pit();
1712 os << _(", Id: ") << par.id();
1713 os << _(", Position: ") << cur.pos();
1714 // FIXME: Why is the check for par.size() needed?
1715 // We are called with cur.pos() == par.size() quite often.
1716 if (!par.empty() && cur.pos() < par.size()) {
1717 // Force output of code point, not character
1718 size_t const c = par.getChar(cur.pos());
1719 os << _(", Char: 0x") << std::hex << c;
1721 os << _(", Boundary: ") << cur.boundary();
1722 // Row & row = cur.textRow();
1723 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1729 docstring LyXText::getPossibleLabel(LCursor & cur) const
1731 pit_type pit = cur.pit();
1733 LyXLayout_ptr layout = pars_[pit].layout();
1735 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1736 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
1737 if (layout2->latextype != LATEX_PARAGRAPH) {
1743 docstring name = from_ascii(layout->latexname());
1745 // for captions, we want the abbreviation of the float type
1746 if (layout->labeltype == LABEL_SENSITIVE) {
1747 // Search for the first float or wrap inset in the iterator
1748 for (int i = cur.depth(); --i >= 0; ) {
1749 InsetBase * const in = &cur[i].inset();
1750 if (in->lyxCode() == InsetBase::FLOAT_CODE
1751 || in->lyxCode() == InsetBase::WRAP_CODE) {
1752 name = in->getInsetName();
1758 docstring text = name.substr(0, 3);
1759 if (name == "theorem")
1760 text = from_ascii("thm"); // Create a correct prefix for prettyref
1763 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
1766 docstring par_text = pars_[pit].asString(cur.buffer(), false);
1767 for (int i = 0; i < lyxrc.label_init_length; ++i) {
1768 if (par_text.empty())
1771 par_text = split(par_text, head, ' ');
1772 // Is it legal to use spaces in labels ?
1782 void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
1784 BOOST_ASSERT(this == cur.text());
1785 pit_type pit = getPitNearY(cur.bv(), y);
1787 TextMetrics const & tm = cur.bv().textMetrics(this);
1788 ParagraphMetrics const & pm = tm.parMetrics(pit);
1790 int yy = cur.bv().coordCache().get(this, pit).y_ - pm.ascent();
1791 lyxerr[Debug::DEBUG]
1792 << BOOST_CURRENT_FUNCTION
1796 << " yy: " << yy << endl;
1799 BOOST_ASSERT(pm.rows().size());
1800 for (; r < int(pm.rows().size()) - 1; ++r) {
1801 Row const & row = pm.rows()[r];
1802 if (int(yy + row.height()) > y)
1807 Row const & row = pm.rows()[r];
1809 lyxerr[Debug::DEBUG]
1810 << BOOST_CURRENT_FUNCTION
1812 << " from pos: " << row.pos()
1817 pos_type const pos = row.pos()
1818 + tm.getColumnNearX(pit, row, xx, bound);
1820 lyxerr[Debug::DEBUG]
1821 << BOOST_CURRENT_FUNCTION
1822 << ": setting cursor pit: " << pit
1826 setCursor(cur, pit, pos, true, bound);
1827 // remember new position.
1832 void LyXText::charsTranspose(LCursor & cur)
1834 BOOST_ASSERT(this == cur.text());
1836 pos_type pos = cur.pos();
1838 // If cursor is at beginning or end of paragraph, do nothing.
1839 if (pos == cur.lastpos() || pos == 0)
1842 Paragraph & par = cur.paragraph();
1844 // Get the positions of the characters to be transposed.
1845 pos_type pos1 = pos - 1;
1846 pos_type pos2 = pos;
1848 // In change tracking mode, ignore deleted characters.
1849 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
1851 if (pos2 == cur.lastpos())
1854 while (pos1 >= 0 && par.isDeleted(pos1))
1859 // Don't do anything if one of the "characters" is not regular text.
1860 if (par.isInset(pos1) || par.isInset(pos2))
1863 // Store the characters to be transposed (including font information).
1864 char_type char1 = par.getChar(pos1);
1865 LyXFont const font1 =
1866 par.getFontSettings(cur.buffer().params(), pos1);
1868 char_type char2 = par.getChar(pos2);
1869 LyXFont const font2 =
1870 par.getFontSettings(cur.buffer().params(), pos2);
1872 // And finally, we are ready to perform the transposition.
1873 // Track the changes if Change Tracking is enabled.
1874 bool const trackChanges = cur.buffer().params().trackChanges;
1878 par.eraseChar(pos2, trackChanges);
1879 par.eraseChar(pos1, trackChanges);
1880 par.insertChar(pos1, char2, font2, trackChanges);
1881 par.insertChar(pos2, char1, font1, trackChanges);
1883 // After the transposition, move cursor to after the transposition.
1884 setCursor(cur, cur.pit(), pos2);