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/insetcaption.h"
60 #include "insets/insethfill.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 == "\\backslash") {
243 par.insertChar(par.size(), '\\', font, change);
244 } else if (token == "\\newline") {
245 auto_ptr<InsetBase> inset(new InsetNewline);
246 inset->read(buf, lex);
247 par.insertInset(par.size(), inset.release(), font, change);
248 } else if (token == "\\LyXTable") {
249 auto_ptr<InsetBase> inset(new InsetTabular(buf));
250 inset->read(buf, lex);
251 par.insertInset(par.size(), inset.release(), font, change);
252 } else if (token == "\\hfill") {
253 par.insertInset(par.size(), new InsetHFill, font, change);
254 } else if (token == "\\lyxline") {
255 par.insertInset(par.size(), new InsetLine, font, change);
256 } else if (token == "\\newpage") {
257 par.insertInset(par.size(), new InsetPagebreak, font, change);
258 } else if (token == "\\clearpage") {
259 par.insertInset(par.size(), new InsetClearPage, font, change);
260 } else if (token == "\\cleardoublepage") {
261 par.insertInset(par.size(), new InsetClearDoublePage, font, change);
262 } else if (token == "\\change_unchanged") {
263 change = Change(Change::UNCHANGED);
264 } else if (token == "\\change_inserted") {
266 std::istringstream is(lex.getString());
270 if (aid >= bp.author_map.size()) {
271 errorList.push_back(ErrorItem(_("Change tracking error"),
272 bformat(_("Unknown author index for insertion: %1$d\n"), aid),
273 par.id(), 0, par.size()));
274 change = Change(Change::UNCHANGED);
276 change = Change(Change::INSERTED, bp.author_map[aid], ct);
277 } else if (token == "\\change_deleted") {
279 std::istringstream is(lex.getString());
283 if (aid >= bp.author_map.size()) {
284 errorList.push_back(ErrorItem(_("Change tracking error"),
285 bformat(_("Unknown author index for deletion: %1$d\n"), aid),
286 par.id(), 0, par.size()));
287 change = Change(Change::UNCHANGED);
289 change = Change(Change::DELETED, bp.author_map[aid], ct);
292 errorList.push_back(ErrorItem(_("Unknown token"),
293 bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
295 par.id(), 0, par.size()));
300 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex,
301 ErrorList & errorList)
304 string token = lex.getString();
306 Change change(Change::UNCHANGED);
309 readParToken(buf, par, lex, token, font, change, errorList);
312 token = lex.getString();
317 if (token == "\\end_layout") {
318 //Ok, paragraph finished
322 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
323 << token << '\'' << endl;
324 if (token == "\\begin_layout" || token == "\\end_document"
325 || token == "\\end_inset" || token == "\\begin_deeper"
326 || token == "\\end_deeper") {
327 lex.pushToken(token);
328 lyxerr << "Paragraph ended in line "
329 << lex.getLineNo() << "\n"
330 << "Missing \\end_layout.\n";
334 // Final change goes to paragraph break:
335 par.setChange(par.size(), change);
337 // Initialize begin_of_body_ on load; redoParagraph maintains
338 par.setBeginOfBody();
346 double LyXText::spacing(Buffer const & buffer,
347 Paragraph const & par) const
349 if (par.params().spacing().isDefault())
350 return buffer.params().spacing().getValue();
351 return par.params().spacing().getValue();
355 int LyXText::singleWidth(Buffer const & buffer, Paragraph const & par,
358 return singleWidth(par, pos, par.getChar(pos),
359 getFont(buffer, par, pos));
363 int LyXText::singleWidth(Paragraph const & par,
364 pos_type pos, char_type c, LyXFont const & font) const
366 // The most common case is handled first (Asger)
367 if (isPrintable(c)) {
368 Language const * language = font.language();
369 if (language->rightToLeft()) {
370 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
371 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
372 && language->lang() == "arabic") {
373 if (Encodings::isComposeChar_arabic(c))
375 c = par.transformChar(c, pos);
376 } else if (language->lang() == "hebrew" &&
377 Encodings::isComposeChar_hebrew(c))
380 return theFontMetrics(font).width(c);
383 if (c == Paragraph::META_INSET)
384 return par.getInset(pos)->width();
386 return theFontMetrics(font).width(c);
390 int LyXText::leftMargin(Buffer const & buffer, int max_width, pit_type pit) const
392 BOOST_ASSERT(pit >= 0);
393 BOOST_ASSERT(pit < int(pars_.size()));
394 return leftMargin(buffer, max_width, pit, pars_[pit].size());
398 int LyXText::leftMargin(Buffer const & buffer, int max_width,
399 pit_type const pit, pos_type const pos) const
401 BOOST_ASSERT(pit >= 0);
402 BOOST_ASSERT(pit < int(pars_.size()));
403 Paragraph const & par = pars_[pit];
404 BOOST_ASSERT(pos >= 0);
405 BOOST_ASSERT(pos <= par.size());
406 //lyxerr << "LyXText::leftMargin: pit: " << pit << " pos: " << pos << endl;
407 LyXTextClass const & tclass = buffer.params().getLyXTextClass();
408 LyXLayout_ptr const & layout = par.layout();
410 string parindent = layout->parindent;
414 if (isMainText(buffer))
415 l_margin += changebarMargin();
418 docstring leftm = from_utf8(tclass.leftmargin());
419 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
421 if (par.getDepth() != 0) {
422 // find the next level paragraph
423 pit_type newpar = outerHook(pit, pars_);
424 if (newpar != pit_type(pars_.size())) {
425 if (pars_[newpar].layout()->isEnvironment()) {
426 l_margin = leftMargin(buffer, max_width, newpar);
428 if (par.layout() == tclass.defaultLayout()) {
429 if (pars_[newpar].params().noindent())
432 parindent = pars_[newpar].layout()->parindent;
437 // This happens after sections in standard classes. The 1.3.x
438 // code compared depths too, but it does not seem necessary
440 if (par.layout() == tclass.defaultLayout()
441 && pit > 0 && pars_[pit - 1].layout()->nextnoindent)
444 LyXFont const labelfont = getLabelFont(buffer, par);
445 FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
447 switch (layout->margintype) {
449 if (!layout->leftmargin.empty()) {
451 docstring leftm = from_utf8(layout->leftmargin);
452 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
454 if (!par.getLabelstring().empty()) {
456 docstring labin = from_utf8(layout->labelindent);
457 l_margin += labelfont_metrics.signedWidth(labin);
458 docstring labstr = par.getLabelstring();
459 l_margin += labelfont_metrics.width(labstr);
460 docstring labsep = from_utf8(layout->labelsep);
461 l_margin += labelfont_metrics.width(labsep);
465 case MARGIN_MANUAL: {
467 docstring labin = from_utf8(layout->labelindent);
468 l_margin += labelfont_metrics.signedWidth(labin);
469 // The width of an empty par, even with manual label, should be 0
470 if (!par.empty() && pos >= par.beginOfBody()) {
471 if (!par.getLabelWidthString().empty()) {
472 docstring labstr = par.getLabelWidthString();
473 l_margin += labelfont_metrics.width(labstr);
474 docstring labsep = from_utf8(layout->labelsep);
475 l_margin += labelfont_metrics.width(labsep);
481 case MARGIN_STATIC: {
483 docstring leftm = from_utf8(layout->leftmargin);
484 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm)
485 * 4 / (par.getDepth() + 4);
489 case MARGIN_FIRST_DYNAMIC:
490 if (layout->labeltype == LABEL_MANUAL) {
491 if (pos >= par.beginOfBody()) {
493 l_margin += labelfont_metrics.signedWidth(
494 from_utf8(layout->leftmargin));
497 l_margin += labelfont_metrics.signedWidth(
498 from_utf8(layout->labelindent));
501 // Special case to fix problems with
503 || (layout->labeltype == LABEL_STATIC
504 && layout->latextype == LATEX_ENVIRONMENT
505 && !isFirstInSequence(pit, pars_))) {
507 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->leftmargin));
508 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
509 && layout->labeltype != LABEL_BIBLIO
510 && layout->labeltype !=
511 LABEL_CENTERED_TOP_ENVIRONMENT) {
512 l_margin += labelfont_metrics.signedWidth(from_utf8(layout->labelindent));
513 l_margin += labelfont_metrics.width(from_utf8(layout->labelsep));
514 l_margin += labelfont_metrics.width(par.getLabelstring());
518 case MARGIN_RIGHT_ADDRESS_BOX: {
520 // ok, a terrible hack. The left margin depends on the widest
521 // row in this paragraph.
522 RowList::iterator rit = par.rows().begin();
523 RowList::iterator end = par.rows().end();
525 #warning This is wrong.
527 int minfill = max_width;
528 for ( ; rit != end; ++rit)
529 if (rit->fill() < minfill)
530 minfill = rit->fill();
531 l_margin += theFontMetrics(params.getFont()).signedWidth(layout->leftmargin);
534 // also wrong, but much shorter.
535 l_margin += max_width / 2;
540 if (!par.params().leftIndent().zero())
541 l_margin += par.params().leftIndent().inPixels(max_width);
545 if (par.params().align() == LYX_ALIGN_LAYOUT)
546 align = layout->align;
548 align = par.params().align();
550 // set the correct parindent
552 && (layout->labeltype == LABEL_NO_LABEL
553 || layout->labeltype == LABEL_TOP_ENVIRONMENT
554 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
555 || (layout->labeltype == LABEL_STATIC
556 && layout->latextype == LATEX_ENVIRONMENT
557 && !isFirstInSequence(pit, pars_)))
558 && align == LYX_ALIGN_BLOCK
559 && !par.params().noindent()
560 // in some insets, paragraphs are never indented
561 && !(par.inInset() && par.inInset()->neverIndent(buffer))
562 // display style insets are always centered, omit indentation
565 && par.getInset(pos)->display())
566 && (par.layout() != tclass.defaultLayout()
567 || buffer.params().paragraph_separation ==
568 BufferParams::PARSEP_INDENT))
570 docstring din = from_utf8(parindent);
571 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(din);
578 LColor_color LyXText::backgroundColor() const
580 return LColor_color(LColor::color(background_color_));
584 void LyXText::breakParagraph(LCursor & cur, bool keep_layout)
586 BOOST_ASSERT(this == cur.text());
588 Paragraph & cpar = cur.paragraph();
589 pit_type cpit = cur.pit();
591 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
592 LyXLayout_ptr const & layout = cpar.layout();
594 // this is only allowed, if the current paragraph is not empty
595 // or caption and if it has not the keepempty flag active
596 if (cur.lastpos() == 0 && !cpar.allowEmpty() &&
597 layout->labeltype != LABEL_SENSITIVE)
600 // a layout change may affect also the following paragraph
601 recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
603 // Always break behind a space
604 // It is better to erase the space (Dekel)
605 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
606 cpar.eraseChar(cur.pos(), cur.buffer().params().trackChanges);
608 // What should the layout for the new paragraph be?
609 int preserve_layout = 0;
613 preserve_layout = layout->isEnvironment();
615 // We need to remember this before we break the paragraph, because
616 // that invalidates the layout variable
617 bool sensitive = layout->labeltype == LABEL_SENSITIVE;
619 // we need to set this before we insert the paragraph.
620 bool const isempty = cpar.allowEmpty() && cpar.empty();
622 lyx::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
623 cur.pos(), preserve_layout);
625 // After this, neither paragraph contains any rows!
628 pit_type next_par = cpit + 1;
630 // well this is the caption hack since one caption is really enough
633 // set to standard-layout
634 pars_[cpit].applyLayout(tclass.defaultLayout());
636 // set to standard-layout
637 pars_[next_par].applyLayout(tclass.defaultLayout());
640 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
641 if (!pars_[next_par].eraseChar(0, cur.buffer().params().trackChanges))
642 break; // the character couldn't be deleted physically due to change tracking
645 ParIterator current_it(cur);
646 ParIterator last_it(cur);
650 updateLabels(cur.buffer(), current_it, last_it);
652 // A singlePar update is not enough in this case.
653 cur.updateFlags(Update::Force);
655 // This check is necessary. Otherwise the new empty paragraph will
656 // be deleted automatically. And it is more friendly for the user!
657 if (cur.pos() != 0 || isempty)
658 setCursor(cur.top(), cur.pit() + 1, 0);
660 setCursor(cur.top(), cur.pit(), 0);
664 // insert a character, moves all the following breaks in the
665 // same Paragraph one to the right and make a rebreak
666 void LyXText::insertChar(LCursor & cur, char_type c)
668 BOOST_ASSERT(this == cur.text());
669 BOOST_ASSERT(c != Paragraph::META_INSET);
671 recordUndo(cur, Undo::INSERT);
673 Buffer const & buffer = cur.buffer();
674 Paragraph & par = cur.paragraph();
675 // try to remove this
676 pit_type const pit = cur.pit();
678 bool const freeSpacing = par.layout()->free_spacing ||
681 if (lyxrc.auto_number) {
682 static docstring const number_operators = from_ascii("+-/*");
683 static docstring const number_unary_operators = from_ascii("+-");
684 static docstring const number_seperators = from_ascii(".,:");
686 if (current_font.number() == LyXFont::ON) {
687 if (!isDigit(c) && !contains(number_operators, c) &&
688 !(contains(number_seperators, c) &&
690 cur.pos() != cur.lastpos() &&
691 getFont(buffer, par, cur.pos()).number() == LyXFont::ON &&
692 getFont(buffer, par, cur.pos() - 1).number() == LyXFont::ON)
694 number(cur); // Set current_font.number to OFF
695 } else if (isDigit(c) &&
696 real_current_font.isVisibleRightToLeft()) {
697 number(cur); // Set current_font.number to ON
699 if (cur.pos() != 0) {
700 char_type const c = par.getChar(cur.pos() - 1);
701 if (contains(number_unary_operators, c) &&
703 || par.isSeparator(cur.pos() - 2)
704 || par.isNewline(cur.pos() - 2))
706 setCharFont(buffer, pit, cur.pos() - 1, current_font);
707 } else if (contains(number_seperators, c)
709 && getFont(buffer, par, cur.pos() - 2).number() == LyXFont::ON) {
710 setCharFont(buffer, pit, cur.pos() - 1, current_font);
716 // First check, if there will be two blanks together or a blank at
717 // the beginning of a paragraph.
718 // I decided to handle blanks like normal characters, the main
719 // difference are the special checks when calculating the row.fill
720 // (blank does not count at the end of a row) and the check here
722 // When the free-spacing option is set for the current layout,
723 // disable the double-space checking
724 if (!freeSpacing && isLineSeparatorChar(c)) {
725 if (cur.pos() == 0) {
726 static bool sent_space_message = false;
727 if (!sent_space_message) {
728 cur.message(_("You cannot insert a space at the "
729 "beginning of a paragraph. Please read the Tutorial."));
730 sent_space_message = true;
734 BOOST_ASSERT(cur.pos() > 0);
735 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
736 && !par.isDeleted(cur.pos() - 1)) {
737 static bool sent_space_message = false;
738 if (!sent_space_message) {
739 cur.message(_("You cannot type two spaces this way. "
740 "Please read the Tutorial."));
741 sent_space_message = true;
747 par.insertChar(cur.pos(), c, current_font, cur.buffer().params().trackChanges);
748 checkBufferStructure(cur.buffer(), cur);
750 // cur.updateFlags(Update::Force);
751 setCursor(cur.top(), cur.pit(), cur.pos() + 1);
756 void LyXText::charInserted()
758 // Here we call finishUndo for every 20 characters inserted.
759 // This is from my experience how emacs does it. (Lgb)
760 static unsigned int counter;
770 // the cursor set functions have a special mechanism. When they
771 // realize, that you left an empty paragraph, they will delete it.
773 bool LyXText::cursorRightOneWord(LCursor & cur)
775 BOOST_ASSERT(this == cur.text());
779 if (old.pos() == old.lastpos() && old.pit() != old.lastpit()) {
783 // Advance through word.
784 while (old.pos() != old.lastpos() && old.paragraph().isLetter(old.pos()))
786 // Skip through trailing nonword stuff.
787 while (old.pos() != old.lastpos() && !old.paragraph().isLetter(old.pos()))
790 return setCursor(cur, old.pit(), old.pos());
794 bool LyXText::cursorLeftOneWord(LCursor & cur)
796 BOOST_ASSERT(this == cur.text());
800 if (old.pos() == 0 && old.pit() != 0) {
802 old.pos() = old.lastpos();
804 // Skip through initial nonword stuff.
805 while (old.pos() != 0 && !old.paragraph().isLetter(old.pos() - 1))
807 // Advance through word.
808 while (old.pos() != 0 && old.paragraph().isLetter(old.pos() - 1))
811 return setCursor(cur, old.pit(), old.pos());
815 void LyXText::selectWord(LCursor & cur, word_location loc)
817 BOOST_ASSERT(this == cur.text());
818 CursorSlice from = cur.top();
819 CursorSlice to = cur.top();
820 getWord(from, to, loc);
821 if (cur.top() != from)
822 setCursor(cur, from.pit(), from.pos());
826 setCursor(cur, to.pit(), to.pos());
828 cap::saveSelection(cur);
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::acceptOrRejectChanges(LCursor & cur, ChangeOp op)
846 BOOST_ASSERT(this == cur.text());
848 if (!cur.selection())
851 recordUndoSelection(cur, Undo::ATOMIC);
853 pit_type begPit = cur.selectionBegin().pit();
854 pit_type endPit = cur.selectionEnd().pit();
856 pos_type begPos = cur.selectionBegin().pos();
857 pos_type endPos = cur.selectionEnd().pos();
859 // keep selection info, because endPos becomes invalid after the first loop
860 bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
862 // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
864 for (pit_type pit = begPit; pit <= endPit; ++pit) {
865 pos_type parSize = pars_[pit].size();
867 // ignore empty paragraphs; otherwise, an assertion will fail for
868 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
872 // do not consider first paragraph if the cursor starts at pos size()
873 if (pit == begPit && begPos == parSize)
876 // do not consider last paragraph if the cursor ends at pos 0
877 if (pit == endPit && endPos == 0)
878 break; // last iteration anyway
880 pos_type left = (pit == begPit ? begPos : 0);
881 pos_type right = (pit == endPit ? endPos : parSize);
884 pars_[pit].acceptChanges(cur.buffer().params(), left, right);
886 pars_[pit].rejectChanges(cur.buffer().params(), left, right);
890 // next, accept/reject imaginary end-of-par characters
892 for (pit_type pit = begPit; pit <= endPit; ++pit) {
893 pos_type pos = pars_[pit].size();
895 // skip if the selection ends before the end-of-par
896 if (pit == endPit && endsBeforeEndOfPar)
897 break; // last iteration anyway
899 // skip if this is not the last paragraph of the document
900 // note: the user should be able to accept/reject the par break of the last par!
901 if (pit == endPit && pit != pars_.size() - 1)
902 break; // last iteration anway
905 if (pars_[pit].isInserted(pos)) {
906 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
907 } else if (pars_[pit].isDeleted(pos)) {
908 if (pit == pars_.size() - 1) {
909 // we cannot remove a par break at the end of the last paragraph;
910 // instead, we mark it unchanged
911 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
913 mergeParagraph(cur.buffer().params(), pars_, pit);
919 if (pars_[pit].isDeleted(pos)) {
920 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
921 } else if (pars_[pit].isInserted(pos)) {
922 if (pit == pars_.size() - 1) {
923 // we mark the par break at the end of the last paragraph unchanged
924 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
926 mergeParagraph(cur.buffer().params(), pars_, pit);
934 // finally, invoke the DEPM
936 deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer().params().trackChanges);
941 cur.clearSelection();
942 setCursorIntern(cur, begPit, begPos);
943 cur.updateFlags(Update::Force);
944 updateLabels(cur.buffer());
948 void LyXText::acceptChanges(BufferParams const & bparams)
950 pit_type pars_size = static_cast<pit_type>(pars_.size());
952 // first, accept changes within each individual paragraph
953 // (do not consider end-of-par)
954 for (pit_type pit = 0; pit < pars_size; ++pit) {
955 if (!pars_[pit].empty()) // prevent assertion failure
956 pars_[pit].acceptChanges(bparams, 0, pars_[pit].size());
959 // next, accept imaginary end-of-par characters
960 for (pit_type pit = 0; pit < pars_size; ++pit) {
961 pos_type pos = pars_[pit].size();
963 if (pars_[pit].isInserted(pos)) {
964 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
965 } else if (pars_[pit].isDeleted(pos)) {
966 if (pit == pars_size - 1) {
967 // we cannot remove a par break at the end of the last
968 // paragraph; instead, we mark it unchanged
969 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
971 mergeParagraph(bparams, pars_, pit);
978 // finally, invoke the DEPM
979 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
983 void LyXText::rejectChanges(BufferParams const & bparams)
985 pit_type pars_size = static_cast<pit_type>(pars_.size());
987 // first, reject changes within each individual paragraph
988 // (do not consider end-of-par)
989 for (pit_type pit = 0; pit < pars_size; ++pit) {
990 if (!pars_[pit].empty()) // prevent assertion failure
991 pars_[pit].rejectChanges(bparams, 0, pars_[pit].size());
994 // next, reject imaginary end-of-par characters
995 for (pit_type pit = 0; pit < pars_size; ++pit) {
996 pos_type pos = pars_[pit].size();
998 if (pars_[pit].isDeleted(pos)) {
999 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1000 } else if (pars_[pit].isInserted(pos)) {
1001 if (pit == pars_size - 1) {
1002 // we mark the par break at the end of the last
1003 // paragraph unchanged
1004 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1006 mergeParagraph(bparams, pars_, pit);
1013 // finally, invoke the DEPM
1014 deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
1018 // Delete from cursor up to the end of the current or next word.
1019 void LyXText::deleteWordForward(LCursor & cur)
1021 BOOST_ASSERT(this == cur.text());
1022 if (cur.lastpos() == 0)
1026 cur.selection() = true;
1027 cursorRightOneWord(cur);
1029 cutSelection(cur, true, false);
1030 checkBufferStructure(cur.buffer(), cur);
1035 // Delete from cursor to start of current or prior word.
1036 void LyXText::deleteWordBackward(LCursor & cur)
1038 BOOST_ASSERT(this == cur.text());
1039 if (cur.lastpos() == 0)
1043 cur.selection() = true;
1044 cursorLeftOneWord(cur);
1046 cutSelection(cur, true, false);
1047 checkBufferStructure(cur.buffer(), cur);
1052 // Kill to end of line.
1053 void LyXText::deleteLineForward(LCursor & cur)
1055 BOOST_ASSERT(this == cur.text());
1056 if (cur.lastpos() == 0) {
1057 // Paragraph is empty, so we just go to the right
1061 cur.selection() = true; // to avoid deletion
1064 // What is this test for ??? (JMarc)
1065 if (!cur.selection())
1066 deleteWordForward(cur);
1068 cutSelection(cur, true, false);
1069 checkBufferStructure(cur.buffer(), cur);
1074 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1076 BOOST_ASSERT(this == cur.text());
1080 if (cur.selection()) {
1081 from = cur.selBegin();
1085 getWord(from, to, PARTIAL_WORD);
1086 cursorRightOneWord(cur);
1089 recordUndoSelection(cur);
1091 pos_type pos = from.pos();
1092 int par = from.pit();
1094 while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
1096 if (pos == pars_[pit].size()) {
1101 char_type c = pars_[pit].getChar(pos);
1102 if (c != Paragraph::META_INSET) {
1104 case text_lowercase:
1107 case text_capitalization:
1109 action = text_lowercase;
1111 case text_uppercase:
1117 // FIXME: change tracking (MG)
1118 // sorry but we are no longer allowed to set a single character directly
1119 // we have to rewrite this method in terms of erase&insert operations
1120 //pars_[pit].setChar(pos, c);
1123 checkBufferStructure(cur.buffer(), cur);
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();
1141 checkBufferStructure(cur.buffer(), cur);
1144 if (cur.pit() == cur.lastpit())
1145 return dissolveInset(cur);
1147 if (!par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1148 par.setChange(cur.pos(), Change(Change::DELETED));
1152 setCursorIntern(cur, cur.pit() + 1, 0);
1153 needsUpdate = backspacePos0(cur);
1158 // Make sure the cursor is correct. Is this really needed?
1159 // No, not really... at least not here!
1160 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1161 checkBufferStructure(cur.buffer(), cur);
1168 bool LyXText::backspacePos0(LCursor & cur)
1170 BOOST_ASSERT(this == cur.text());
1174 bool needsUpdate = false;
1176 BufferParams const & bufparams = cur.buffer().params();
1177 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1178 ParagraphList & plist = cur.text()->paragraphs();
1179 Paragraph const & par = cur.paragraph();
1180 LCursor prevcur = cur;
1182 prevcur.pos() = prevcur.lastpos();
1183 Paragraph const & prevpar = prevcur.paragraph();
1185 // is it an empty paragraph?
1186 if (cur.lastpos() == 0
1187 || (cur.lastpos() == 1 && par.isSeparator(0))) {
1188 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1189 plist.erase(boost::next(plist.begin(), cur.pit()));
1192 // is previous par empty?
1193 else if (prevcur.lastpos() == 0
1194 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1195 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1196 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1199 // Pasting is not allowed, if the paragraphs have different
1200 // layouts. I think it is a real bug of all other
1201 // word processors to allow it. It confuses the user.
1202 // Correction: Pasting is always allowed with standard-layout
1203 else if (par.layout() == prevpar.layout()
1204 || par.layout() == tclass.defaultLayout()) {
1205 recordUndo(cur, Undo::ATOMIC, prevcur.pit());
1206 mergeParagraph(bufparams, plist, prevcur.pit());
1211 updateLabels(cur.buffer());
1212 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1219 bool LyXText::backspace(LCursor & cur)
1221 BOOST_ASSERT(this == cur.text());
1222 bool needsUpdate = false;
1223 if (cur.pos() == 0) {
1225 return dissolveInset(cur);
1227 Paragraph & prev_par = pars_[cur.pit() - 1];
1229 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1230 prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1231 setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1234 // The cursor is at the beginning of a paragraph, so
1235 // the backspace will collapse two paragraphs into one.
1236 needsUpdate = backspacePos0(cur);
1239 // this is the code for a normal backspace, not pasting
1241 recordUndo(cur, Undo::DELETE);
1242 // We used to do cursorLeftIntern() here, but it is
1243 // not a good idea since it triggers the auto-delete
1244 // mechanism. So we do a cursorLeftIntern()-lite,
1245 // without the dreaded mechanism. (JMarc)
1246 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1247 false, cur.boundary());
1248 cur.paragraph().eraseChar(cur.pos(), cur.buffer().params().trackChanges);
1249 checkBufferStructure(cur.buffer(), cur);
1252 if (cur.pos() == cur.lastpos())
1253 setCurrentFont(cur);
1255 // A singlePar update is not enough in this case.
1256 // cur.updateFlags(Update::Force);
1257 setCursor(cur.top(), cur.pit(), cur.pos());
1263 bool LyXText::dissolveInset(LCursor & cur) {
1264 BOOST_ASSERT(this == cur.text());
1266 if (isMainText(*cur.bv().buffer()) || cur.inset().nargs() != 1)
1269 recordUndoInset(cur);
1270 cur.selHandle(false);
1272 pos_type spos = cur.pos();
1273 pit_type spit = cur.pit();
1274 ParagraphList plist;
1275 if (cur.lastpit() != 0 || cur.lastpos() != 0)
1276 plist = paragraphs();
1278 // store cursor offset
1282 Buffer & b = cur.buffer();
1283 cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1284 if (!plist.empty()) {
1285 // ERT paragraphs have the Language latex_language.
1286 // This is invalid outside of ERT, so we need to
1287 // change it to the buffer language.
1288 ParagraphList::iterator it = plist.begin();
1289 ParagraphList::iterator it_end = plist.end();
1290 for (; it != it_end; it++) {
1291 it->changeLanguage(b.params(), latex_language,
1295 pasteParagraphList(cur, plist, b.params().textclass,
1296 b.errorList("Paste"));
1298 cur.pit() = std::min(cur.lastpit(), spit);
1299 cur.pos() = std::min(cur.lastpos(), spos);
1301 cur.clearSelection();
1307 // only used for inset right now. should also be used for main text
1308 void LyXText::draw(PainterInfo & pi, int x, int y) const
1310 paintTextInset(*this, pi, x, y);
1314 // only used for inset right now. should also be used for main text
1315 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
1317 LCursor & cur = pi.base.bv->cursor();
1318 if (!cur.selection())
1320 if (!ptr_cmp(cur.text(), this))
1323 if (lyxerr.debugging(Debug::DEBUG)) {
1324 lyxerr[Debug::DEBUG]
1325 << BOOST_CURRENT_FUNCTION
1326 << "draw selection at " << x
1330 DocIterator beg = cur.selectionBegin();
1331 DocIterator end = cur.selectionEnd();
1333 BufferView & bv = *pi.base.bv;
1334 Buffer const & buffer = *bv.buffer();
1336 // the selection doesn't touch the visible screen
1337 if (bv_funcs::status(&bv, beg) == bv_funcs::CUR_BELOW
1338 || bv_funcs::status(&bv, end) == bv_funcs::CUR_ABOVE)
1341 Paragraph const & par1 = pars_[beg.pit()];
1342 Paragraph const & par2 = pars_[end.pit()];
1343 TextMetrics const & tm = bv.textMetrics(this);
1344 ParagraphMetrics const & pm1 = tm.parMetrics(beg.pit());
1345 ParagraphMetrics const & pm2 = tm.parMetrics(end.pit());
1347 bool const above = (bv_funcs::status(&bv, beg)
1348 == bv_funcs::CUR_ABOVE);
1349 bool const below = (bv_funcs::status(&bv, end)
1350 == bv_funcs::CUR_BELOW);
1358 Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
1359 y1 = bv_funcs::getPos(bv, beg, beg.boundary()).y_ - row1.ascent();
1360 y2 = y1 + row1.height();
1361 int const startx = cursorX(bv, beg.top(), beg.boundary());
1362 if (!isRTL(buffer, par1)) {
1364 x2 = 0 + tm.width();
1374 Y1 = bv.workHeight();
1375 Y2 = bv.workHeight();
1379 Row const & row2 = pm2.getRow(end.pos(), end.boundary());
1380 Y1 = bv_funcs::getPos(bv, end, end.boundary()).y_ - row2.ascent();
1381 Y2 = Y1 + row2.height();
1382 int const endx = cursorX(bv, end.top(), end.boundary());
1383 if (!isRTL(buffer, par2)) {
1389 X2 = 0 + tm.width();
1393 if (!above && !below && &pm1.getRow(beg.pos(), beg.boundary())
1394 == &pm2.getRow(end.pos(), end.boundary()))
1396 // paint only one rectangle
1397 int const b( !isRTL(*bv.buffer(), par1) ? x + x1 : x + X1 );
1398 int const w( !isRTL(*bv.buffer(), par1) ? X2 - x1 : x2 - X1 );
1399 pi.pain.fillRectangle(b, y1, w, y2 - y1, LColor::selection);
1403 if (lyxerr.debugging(Debug::DEBUG)) {
1404 lyxerr[Debug::DEBUG] << " y1: " << y1 << " y2: " << y2
1405 << "X1:" << X1 << " x2: " << X2 << " wid: " << tm.width()
1409 // paint upper rectangle
1410 pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
1412 // paint bottom rectangle
1413 pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
1415 // paint center rectangle
1416 pi.pain.fillRectangle(x, y2, tm.width(),
1417 Y1 - y2, LColor::selection);
1421 bool LyXText::isLastRow(pit_type pit, Row const & row) const
1423 return row.endpos() >= pars_[pit].size()
1424 && pit + 1 == pit_type(paragraphs().size());
1428 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
1430 return row.pos() == 0 && pit == 0;
1434 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1435 word_location const loc)
1437 Paragraph const & from_par = pars_[from.pit()];
1439 case WHOLE_WORD_STRICT:
1440 if (from.pos() == 0 || from.pos() == from_par.size()
1441 || !from_par.isLetter(from.pos())
1442 || !from_par.isLetter(from.pos() - 1)) {
1446 // no break here, we go to the next
1449 // If we are already at the beginning of a word, do nothing
1450 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
1452 // no break here, we go to the next
1455 // always move the cursor to the beginning of previous word
1456 while (from.pos() && from_par.isLetter(from.pos() - 1))
1460 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1464 // no need to move the 'from' cursor
1468 Paragraph & to_par = pars_[to.pit()];
1469 while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
1474 void LyXText::write(Buffer const & buf, std::ostream & os) const
1476 ParagraphList::const_iterator pit = paragraphs().begin();
1477 ParagraphList::const_iterator end = paragraphs().end();
1479 for (; pit != end; ++pit)
1480 pit->write(buf, os, buf.params(), dth);
1484 bool LyXText::read(Buffer const & buf, LyXLex & lex, ErrorList & errorList)
1486 depth_type depth = 0;
1488 while (lex.isOK()) {
1490 string const token = lex.getString();
1495 if (token == "\\end_inset")
1498 if (token == "\\end_body")
1501 if (token == "\\begin_body")
1504 if (token == "\\end_document")
1507 if (token == "\\begin_layout") {
1508 lex.pushToken(token);
1511 par.params().depth(depth);
1512 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1513 pars_.push_back(par);
1515 // FIXME: goddamn InsetTabular makes us pass a Buffer
1517 lyx::readParagraph(buf, pars_.back(), lex, errorList);
1519 } else if (token == "\\begin_deeper") {
1521 } else if (token == "\\end_deeper") {
1523 lex.printError("\\end_deeper: " "depth is already null");
1528 lyxerr << "Handling unknown body token: `"
1529 << token << '\'' << endl;
1535 int LyXText::cursorX(BufferView const & bv, CursorSlice const & sl,
1536 bool boundary) const
1538 TextMetrics const & tm = bv.textMetrics(sl.text());
1539 pit_type const pit = sl.pit();
1540 Paragraph const & par = pars_[pit];
1541 ParagraphMetrics const & pm = tm.parMetrics(pit);
1542 if (pm.rows().empty())
1545 pos_type ppos = sl.pos();
1546 // Correct position in front of big insets
1547 bool const boundary_correction = ppos != 0 && boundary;
1548 if (boundary_correction)
1551 Row const & row = pm.getRow(sl.pos(), boundary);
1553 pos_type cursor_vpos = 0;
1555 Buffer const & buffer = *bv.buffer();
1556 RowMetrics const m = tm.computeRowMetrics(pit, row);
1559 pos_type const row_pos = row.pos();
1560 pos_type const end = row.endpos();
1563 cursor_vpos = row_pos;
1564 else if (ppos >= end)
1565 cursor_vpos = isRTL(buffer, par) ? row_pos : end;
1566 else if (ppos > row_pos && ppos >= end)
1567 // Place cursor after char at (logical) position pos - 1
1568 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
1569 ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
1571 // Place cursor before char at (logical) position ppos
1572 cursor_vpos = (bidi.level(ppos) % 2 == 0)
1573 ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
1575 pos_type body_pos = par.beginOfBody();
1577 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1580 // Use font span to speed things up, see below
1583 FontMetrics const & labelfm = theFontMetrics(
1584 getLabelFont(buffer, par));
1586 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1587 pos_type pos = bidi.vis2log(vpos);
1588 if (body_pos > 0 && pos == body_pos - 1) {
1590 docstring const lsep = from_utf8(par.layout()->labelsep);
1591 x += m.label_hfill + labelfm.width(lsep);
1592 if (par.isLineSeparator(body_pos - 1))
1593 x -= singleWidth(buffer, par, body_pos - 1);
1596 // Use font span to speed things up, see above
1597 if (pos < font_span.first || pos > font_span.last) {
1598 font_span = par.fontSpan(pos);
1599 font = getFont(buffer, par, pos);
1602 x += singleWidth(par, pos, par.getChar(pos), font);
1604 if (par.hfillExpansion(row, pos))
1605 x += (pos >= body_pos) ? m.hfill : m.label_hfill;
1606 else if (par.isSeparator(pos) && pos >= body_pos)
1610 // see correction above
1611 if (boundary_correction)
1612 if (getFont(buffer, par, ppos).isVisibleRightToLeft())
1613 x -= singleWidth(buffer, par, ppos);
1615 x += singleWidth(buffer, par, ppos);
1617 // Make sure inside an inset we always count from the left
1618 // edge (bidi!) -- MV
1619 if (sl.pos() < par.size()) {
1620 font = getFont(buffer, par, sl.pos());
1621 if (!boundary && font.isVisibleRightToLeft()
1622 && par.isInset(sl.pos()))
1623 x -= par.getInset(sl.pos())->width();
1629 int LyXText::cursorY(BufferView const & bv, CursorSlice const & sl, bool boundary) const
1631 //lyxerr << "LyXText::cursorY: boundary: " << boundary << std::endl;
1632 ParagraphMetrics const & pm = bv.parMetrics(this, sl.pit());
1633 if (pm.rows().empty())
1637 h -= bv.parMetrics(this, 0).rows()[0].ascent();
1638 for (pit_type pit = 0; pit < sl.pit(); ++pit) {
1639 h += bv.parMetrics(this, pit).height();
1642 if (pos && boundary)
1644 size_t const rend = pm.pos2row(pos);
1645 for (size_t rit = 0; rit != rend; ++rit)
1646 h += pm.rows()[rit].height();
1647 h += pm.rows()[rend].ascent();
1652 // Returns the current font and depth as a message.
1653 docstring LyXText::currentState(LCursor & cur)
1655 BOOST_ASSERT(this == cur.text());
1656 Buffer & buf = cur.buffer();
1657 Paragraph const & par = cur.paragraph();
1658 odocstringstream os;
1660 if (buf.params().trackChanges)
1661 os << _("[Change Tracking] ");
1663 Change change = par.lookupChange(cur.pos());
1665 if (change.type != Change::UNCHANGED) {
1666 Author const & a = buf.params().authors().get(change.author);
1667 os << _("Change: ") << a.name();
1668 if (!a.email().empty())
1669 os << " (" << a.email() << ")";
1670 // FIXME ctime is english, we should translate that
1671 os << _(" at ") << ctime(&change.changetime);
1675 // I think we should only show changes from the default
1677 // No, from the document font (MV)
1678 LyXFont font = real_current_font;
1679 font.reduce(buf.params().getFont());
1681 os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1683 // The paragraph depth
1684 int depth = cur.paragraph().getDepth();
1686 os << bformat(_(", Depth: %1$d"), depth);
1688 // The paragraph spacing, but only if different from
1690 Spacing const & spacing = par.params().spacing();
1691 if (!spacing.isDefault()) {
1692 os << _(", Spacing: ");
1693 switch (spacing.getSpace()) {
1694 case Spacing::Single:
1697 case Spacing::Onehalf:
1700 case Spacing::Double:
1703 case Spacing::Other:
1704 os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1706 case Spacing::Default:
1707 // should never happen, do nothing
1712 #ifdef DEVEL_VERSION
1713 os << _(", Inset: ") << &cur.inset();
1714 os << _(", Paragraph: ") << cur.pit();
1715 os << _(", Id: ") << par.id();
1716 os << _(", Position: ") << cur.pos();
1717 // FIXME: Why is the check for par.size() needed?
1718 // We are called with cur.pos() == par.size() quite often.
1719 if (!par.empty() && cur.pos() < par.size()) {
1720 // Force output of code point, not character
1721 size_t const c = par.getChar(cur.pos());
1722 os << _(", Char: 0x") << std::hex << c;
1724 os << _(", Boundary: ") << cur.boundary();
1725 // Row & row = cur.textRow();
1726 // os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1732 docstring LyXText::getPossibleLabel(LCursor & cur) const
1734 pit_type pit = cur.pit();
1736 LyXLayout_ptr layout = pars_[pit].layout();
1739 docstring par_text = pars_[pit].asString(cur.buffer(), false);
1740 for (int i = 0; i < lyxrc.label_init_length; ++i) {
1741 if (par_text.empty())
1744 par_text = split(par_text, head, ' ');
1745 // Is it legal to use spaces in labels ?
1751 // No need for a prefix if the user said so.
1752 if (lyxrc.label_init_length <= 0)
1755 // Will contain the label type.
1758 // For section, subsection, etc...
1759 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1760 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
1761 if (layout2->latextype != LATEX_PARAGRAPH) {
1766 if (layout->latextype != LATEX_PARAGRAPH)
1767 name = from_ascii(layout->latexname());
1769 // for captions, we just take the caption type
1770 InsetBase * caption_inset = cur.innerInsetOfType(InsetBase::CAPTION_CODE);
1772 name = from_ascii(static_cast<InsetCaption *>(caption_inset)->type());
1774 // Inside floats or wraps, if none of the above worked
1775 // we want by default the abbreviation of the float type.
1777 InsetBase * float_inset = cur.innerInsetOfType(InsetBase::FLOAT_CODE);
1779 float_inset = cur.innerInsetOfType(InsetBase::WRAP_CODE);
1781 name = float_inset->getInsetName();
1784 // Create a correct prefix for prettyref
1785 if (name == "theorem")
1786 name = from_ascii("thm");
1789 text = name.substr(0, 3) + ':' + text;
1795 void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
1797 BOOST_ASSERT(this == cur.text());
1798 pit_type pit = getPitNearY(cur.bv(), y);
1800 TextMetrics const & tm = cur.bv().textMetrics(this);
1801 ParagraphMetrics const & pm = tm.parMetrics(pit);
1803 int yy = cur.bv().coordCache().get(this, pit).y_ - pm.ascent();
1804 lyxerr[Debug::DEBUG]
1805 << BOOST_CURRENT_FUNCTION
1809 << " yy: " << yy << endl;
1812 BOOST_ASSERT(pm.rows().size());
1813 for (; r < int(pm.rows().size()) - 1; ++r) {
1814 Row const & row = pm.rows()[r];
1815 if (int(yy + row.height()) > y)
1820 Row const & row = pm.rows()[r];
1822 lyxerr[Debug::DEBUG]
1823 << BOOST_CURRENT_FUNCTION
1825 << " from pos: " << row.pos()
1830 pos_type const pos = row.pos()
1831 + tm.getColumnNearX(pit, row, xx, bound);
1833 lyxerr[Debug::DEBUG]
1834 << BOOST_CURRENT_FUNCTION
1835 << ": setting cursor pit: " << pit
1839 setCursor(cur, pit, pos, true, bound);
1840 // remember new position.
1845 void LyXText::charsTranspose(LCursor & cur)
1847 BOOST_ASSERT(this == cur.text());
1849 pos_type pos = cur.pos();
1851 // If cursor is at beginning or end of paragraph, do nothing.
1852 if (pos == cur.lastpos() || pos == 0)
1855 Paragraph & par = cur.paragraph();
1857 // Get the positions of the characters to be transposed.
1858 pos_type pos1 = pos - 1;
1859 pos_type pos2 = pos;
1861 // In change tracking mode, ignore deleted characters.
1862 while (pos2 < cur.lastpos() && par.isDeleted(pos2))
1864 if (pos2 == cur.lastpos())
1867 while (pos1 >= 0 && par.isDeleted(pos1))
1872 // Don't do anything if one of the "characters" is not regular text.
1873 if (par.isInset(pos1) || par.isInset(pos2))
1876 // Store the characters to be transposed (including font information).
1877 char_type char1 = par.getChar(pos1);
1878 LyXFont const font1 =
1879 par.getFontSettings(cur.buffer().params(), pos1);
1881 char_type char2 = par.getChar(pos2);
1882 LyXFont const font2 =
1883 par.getFontSettings(cur.buffer().params(), pos2);
1885 // And finally, we are ready to perform the transposition.
1886 // Track the changes if Change Tracking is enabled.
1887 bool const trackChanges = cur.buffer().params().trackChanges;
1891 par.eraseChar(pos2, trackChanges);
1892 par.eraseChar(pos1, trackChanges);
1893 par.insertChar(pos1, char2, font2, trackChanges);
1894 par.insertChar(pos2, char1, font1, trackChanges);
1896 checkBufferStructure(cur.buffer(), cur);
1898 // After the transposition, move cursor to after the transposition.
1899 setCursor(cur, cur.pit(), pos2);