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 "bufferparams.h"
24 #include "BufferView.h"
26 #include "CutAndPaste.h"
28 #include "dispatchresult.h"
30 #include "errorlist.h"
31 #include "funcrequest.h"
33 #include "FontIterator.h"
37 #include "lyxlength.h"
41 #include "lyxrow_funcs.h"
42 #include "metricsinfo.h"
43 #include "paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "rowpainter.h"
49 #include "WordLangTuple.h"
51 #include "frontends/font_metrics.h"
52 #include "frontends/LyXView.h"
54 #include "insets/insettext.h"
55 #include "insets/insetbibitem.h"
56 #include "insets/insethfill.h"
57 #include "insets/insetlatexaccent.h"
58 #include "insets/insetline.h"
59 #include "insets/insetnewline.h"
60 #include "insets/insetpagebreak.h"
61 #include "insets/insetoptarg.h"
62 #include "insets/insetspace.h"
63 #include "insets/insetspecialchar.h"
64 #include "insets/insettabular.h"
66 #include "support/lstrings.h"
67 #include "support/textutils.h"
68 #include "support/tostr.h"
74 using lyx::word_location;
76 using lyx::support::bformat;
77 using lyx::support::contains;
78 using lyx::support::lowercase;
79 using lyx::support::split;
80 using lyx::support::uppercase;
82 using lyx::cap::cutSelection;
95 int numberOfSeparators(Paragraph const & par, Row const & row)
97 pos_type const first = max(row.pos(), par.beginOfBody());
98 pos_type const last = row.endpos() - 1;
100 for (pos_type p = first; p < last; ++p) {
101 if (par.isSeparator(p))
108 unsigned int maxParagraphWidth(ParagraphList const & plist)
110 unsigned int width = 0;
111 ParagraphList::const_iterator pit = plist.begin();
112 ParagraphList::const_iterator end = plist.end();
113 for (; pit != end; ++pit)
114 width = std::max(width, pit->width);
119 int numberOfLabelHfills(Paragraph const & par, Row const & row)
121 pos_type last = row.endpos() - 1;
122 pos_type first = row.pos();
124 // hfill *DO* count at the beginning of paragraphs!
126 while (first < last && par.isHfill(first))
130 last = min(last, par.beginOfBody());
132 for (pos_type p = first; p < last; ++p) {
140 int numberOfHfills(Paragraph const & par, Row const & row)
142 pos_type const last = row.endpos() - 1;
143 pos_type first = row.pos();
145 // hfill *DO* count at the beginning of paragraphs!
147 while (first < last && par.isHfill(first))
151 first = max(first, par.beginOfBody());
154 for (pos_type p = first; p < last; ++p) {
162 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
163 string const & token, LyXFont & font)
165 static Change change;
167 BufferParams const & bp = buf.params();
169 if (token[0] != '\\') {
170 string::const_iterator cit = token.begin();
171 for (; cit != token.end(); ++cit) {
172 par.insertChar(par.size(), (*cit), font, change);
174 } else if (token == "\\begin_layout") {
176 string layoutname = lex.getString();
178 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
181 LyXTextClass const & tclass = bp.getLyXTextClass();
183 if (layoutname.empty()) {
184 layoutname = tclass.defaultLayoutName();
187 bool hasLayout = tclass.hasLayout(layoutname);
190 buf.error(ErrorItem(_("Unknown layout"),
191 bformat(_("Layout '%1$s' does not exists in textclass '%2$s'\nTrying to use the default instead.\n"),
192 layoutname, tclass.name()), par.id(), 0, par.size()));
193 layoutname = tclass.defaultLayoutName();
196 par.layout(bp.getLyXTextClass()[layoutname]);
198 // Test whether the layout is obsolete.
199 LyXLayout_ptr const & layout = par.layout();
200 if (!layout->obsoleted_by().empty())
201 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
203 par.params().read(lex);
205 } else if (token == "\\end_layout") {
206 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
207 << "Missing \\begin_layout?.\n";
208 } else if (token == "\\end_inset") {
209 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
210 << "Missing \\begin_inset?.\n";
211 } else if (token == "\\begin_inset") {
212 InsetBase * inset = readInset(lex, buf);
214 par.insertInset(par.size(), inset, font, change);
217 string line = lex.getString();
218 buf.error(ErrorItem(_("Unknown Inset"), line,
219 par.id(), 0, par.size()));
221 } else if (token == "\\family") {
223 font.setLyXFamily(lex.getString());
224 } else if (token == "\\series") {
226 font.setLyXSeries(lex.getString());
227 } else if (token == "\\shape") {
229 font.setLyXShape(lex.getString());
230 } else if (token == "\\size") {
232 font.setLyXSize(lex.getString());
233 } else if (token == "\\lang") {
235 string const tok = lex.getString();
236 Language const * lang = languages.getLanguage(tok);
238 font.setLanguage(lang);
240 font.setLanguage(bp.language);
241 lex.printError("Unknown language `$$Token'");
243 } else if (token == "\\numeric") {
245 font.setNumber(font.setLyXMisc(lex.getString()));
246 } else if (token == "\\emph") {
248 font.setEmph(font.setLyXMisc(lex.getString()));
249 } else if (token == "\\bar") {
251 string const tok = lex.getString();
254 font.setUnderbar(LyXFont::ON);
255 else if (tok == "no")
256 font.setUnderbar(LyXFont::OFF);
257 else if (tok == "default")
258 font.setUnderbar(LyXFont::INHERIT);
260 lex.printError("Unknown bar font flag "
262 } else if (token == "\\noun") {
264 font.setNoun(font.setLyXMisc(lex.getString()));
265 } else if (token == "\\color") {
267 font.setLyXColor(lex.getString());
268 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
270 // Insets don't make sense in a free-spacing context! ---Kayvan
271 if (par.isFreeSpacing()) {
272 if (token == "\\InsetSpace")
273 par.insertChar(par.size(), ' ', font, change);
274 else if (lex.isOK()) {
276 string const next_token = lex.getString();
277 if (next_token == "\\-")
278 par.insertChar(par.size(), '-', font, change);
280 lex.printError("Token `$$Token' "
282 "paragraph layout!");
286 auto_ptr<InsetBase> inset;
287 if (token == "\\SpecialChar" )
288 inset.reset(new InsetSpecialChar);
290 inset.reset(new InsetSpace);
291 inset->read(buf, lex);
292 par.insertInset(par.size(), inset.release(),
295 } else if (token == "\\i") {
296 auto_ptr<InsetBase> inset(new InsetLatexAccent);
297 inset->read(buf, lex);
298 par.insertInset(par.size(), inset.release(), font, change);
299 } else if (token == "\\backslash") {
300 par.insertChar(par.size(), '\\', font, change);
301 } else if (token == "\\newline") {
302 auto_ptr<InsetBase> inset(new InsetNewline);
303 inset->read(buf, lex);
304 par.insertInset(par.size(), inset.release(), font, change);
305 } else if (token == "\\LyXTable") {
306 auto_ptr<InsetBase> inset(new InsetTabular(buf));
307 inset->read(buf, lex);
308 par.insertInset(par.size(), inset.release(), font, change);
309 } else if (token == "\\bibitem") {
310 InsetCommandParams p("bibitem", "dummy");
311 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
312 inset->read(buf, lex);
313 par.insertInset(par.size(), inset.release(), font, change);
314 } else if (token == "\\hfill") {
315 par.insertInset(par.size(), new InsetHFill, font, change);
316 } else if (token == "\\lyxline") {
317 par.insertInset(par.size(), new InsetLine, font, change);
318 } else if (token == "\\newpage") {
319 par.insertInset(par.size(), new InsetPagebreak, font, change);
320 } else if (token == "\\change_unchanged") {
321 // Hack ! Needed for empty paragraphs :/
322 // FIXME: is it still ??
325 change = Change(Change::UNCHANGED);
326 } else if (token == "\\change_inserted") {
328 std::istringstream is(lex.getString());
332 change = Change(Change::INSERTED, bp.author_map[aid], ct);
333 } else if (token == "\\change_deleted") {
335 std::istringstream is(lex.getString());
339 change = Change(Change::DELETED, bp.author_map[aid], ct);
342 buf.error(ErrorItem(_("Unknown token"),
343 bformat(_("Unknown token: %1$s %2$s\n"), token, lex.getString()),
344 par.id(), 0, par.size()));
349 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex)
352 string token = lex.getString();
357 readParToken(buf, par, lex, token, font);
360 token = lex.getString();
365 if (token == "\\end_layout") {
366 //Ok, paragraph finished
370 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
371 << token << '\'' << endl;
372 if (token == "\\begin_layout" || token == "\\end_document"
373 || token == "\\end_inset" || token == "\\begin_deeper"
374 || token == "\\end_deeper") {
375 lex.pushToken(token);
376 lyxerr << "Paragraph ended in line "
377 << lex.getLineNo() << "\n"
378 << "Missing \\end_layout.\n";
388 BufferView * LyXText::bv()
390 BOOST_ASSERT(bv_owner != 0);
395 BufferView * LyXText::bv() const
397 BOOST_ASSERT(bv_owner != 0);
402 double LyXText::spacing(Paragraph const & par) const
404 if (par.params().spacing().isDefault())
405 return bv()->buffer()->params().spacing().getValue();
406 return par.params().spacing().getValue();
410 void LyXText::updateParPositions()
413 par_type end = pars_.size();
414 for (height_ = 0; pit != end; ++pit) {
415 pars_[pit].y = height_;
416 height_ += pars_[pit].height;
421 int LyXText::width() const
427 int LyXText::height() const
433 int LyXText::singleWidth(Paragraph const & par, pos_type pos) const
435 return singleWidth(par, pos, par.getChar(pos), getFont(par, pos));
439 int LyXText::singleWidth(Paragraph const & par,
440 pos_type pos, char c, LyXFont const & font) const
442 BOOST_ASSERT(pos < par.size());
444 // The most common case is handled first (Asger)
445 if (IsPrintable(c)) {
446 if (font.language()->RightToLeft()) {
447 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
448 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
449 && font.language()->lang() == "arabic") {
450 if (Encodings::IsComposeChar_arabic(c))
453 c = par.transformChar(c, pos);
454 } else if (font.language()->lang() == "hebrew" &&
455 Encodings::IsComposeChar_hebrew(c))
458 return font_metrics::width(c, font);
461 if (c == Paragraph::META_INSET)
462 return par.getInset(pos)->width();
464 if (IsSeparatorChar(c))
466 return font_metrics::width(c, font);
470 int LyXText::leftMargin(par_type pit) const
472 return leftMargin(pit, pars_[pit].size());
476 int LyXText::leftMargin(par_type const pit, pos_type const pos) const
478 Paragraph const & par = pars_[pit];
479 LyXTextClass const & tclass =
480 bv()->buffer()->params().getLyXTextClass();
481 LyXLayout_ptr const & layout = par.layout();
483 string parindent = layout->parindent;
488 l_margin += changebarMargin();
490 l_margin += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
492 if (par.getDepth() != 0) {
493 // find the next level paragraph
494 par_type newpar = outerHook(pit, pars_);
495 if (newpar != par_type(pars_.size())) {
496 if (pars_[newpar].layout()->isEnvironment()) {
497 l_margin = leftMargin(newpar);
499 if (par.layout() == tclass.defaultLayout()) {
500 if (pars_[newpar].params().noindent())
503 parindent = pars_[newpar].layout()->parindent;
508 LyXFont const labelfont = getLabelFont(par);
509 switch (layout->margintype) {
511 if (!layout->leftmargin.empty())
512 l_margin += font_metrics::signedWidth(layout->leftmargin,
513 tclass.defaultfont());
514 if (!par.getLabelstring().empty()) {
515 l_margin += font_metrics::signedWidth(layout->labelindent,
517 l_margin += font_metrics::width(par.getLabelstring(),
519 l_margin += font_metrics::width(layout->labelsep, labelfont);
524 l_margin += font_metrics::signedWidth(layout->labelindent, labelfont);
525 // The width of an empty par, even with manual label, should be 0
526 if (!par.empty() && pos >= par.beginOfBody()) {
527 if (!par.getLabelWidthString().empty()) {
528 l_margin += font_metrics::width(par.getLabelWidthString(),
530 l_margin += font_metrics::width(layout->labelsep, labelfont);
536 l_margin += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
537 / (par.getDepth() + 4);
540 case MARGIN_FIRST_DYNAMIC:
541 if (layout->labeltype == LABEL_MANUAL) {
542 if (pos >= par.beginOfBody()) {
543 l_margin += font_metrics::signedWidth(layout->leftmargin,
546 l_margin += font_metrics::signedWidth(layout->labelindent,
550 // Special case to fix problems with
552 || (layout->labeltype == LABEL_STATIC
553 && layout->latextype == LATEX_ENVIRONMENT
554 && !isFirstInSequence(pit, pars_))) {
555 l_margin += font_metrics::signedWidth(layout->leftmargin,
557 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
558 && layout->labeltype != LABEL_BIBLIO
559 && layout->labeltype !=
560 LABEL_CENTERED_TOP_ENVIRONMENT) {
561 l_margin += font_metrics::signedWidth(layout->labelindent,
563 l_margin += font_metrics::width(layout->labelsep, labelfont);
564 l_margin += font_metrics::width(par.getLabelstring(),
569 case MARGIN_RIGHT_ADDRESS_BOX: {
571 // ok, a terrible hack. The left margin depends on the widest
572 // row in this paragraph.
573 RowList::iterator rit = par.rows.begin();
574 RowList::iterator end = par.rows.end();
576 #warning This is wrong.
578 int minfill = maxwidth_;
579 for ( ; rit != end; ++rit)
580 if (rit->fill() < minfill)
581 minfill = rit->fill();
582 l_margin += font_metrics::signedWidth(layout->leftmargin,
583 tclass.defaultfont());
586 // also wrong, but much shorter.
587 l_margin += maxwidth_ / 2;
592 if (!par.params().leftIndent().zero())
593 l_margin += par.params().leftIndent().inPixels(maxwidth_);
597 if (par.params().align() == LYX_ALIGN_LAYOUT)
598 align = layout->align;
600 align = par.params().align();
602 // set the correct parindent
604 && (layout->labeltype == LABEL_NO_LABEL
605 || layout->labeltype == LABEL_TOP_ENVIRONMENT
606 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
607 || (layout->labeltype == LABEL_STATIC
608 && layout->latextype == LATEX_ENVIRONMENT
609 && !isFirstInSequence(pit, pars_)))
610 && align == LYX_ALIGN_BLOCK
611 && !par.params().noindent()
612 // in charstyles, tabulars and ert paragraphs are never indented!
613 && (par.ownerCode() != InsetBase::TEXT_CODE
614 && par.ownerCode() != InsetBase::ERT_CODE
615 && par.ownerCode() != InsetBase::CHARSTYLE_CODE)
616 && (par.layout() != tclass.defaultLayout()
617 || bv()->buffer()->params().paragraph_separation ==
618 BufferParams::PARSEP_INDENT))
620 l_margin += font_metrics::signedWidth(parindent, tclass.defaultfont());
627 int LyXText::rightMargin(Paragraph const & par) const
629 // We do not want rightmargins on inner texts.
630 if (bv()->text() != this)
633 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
636 + font_metrics::signedWidth(tclass.rightmargin(),
637 tclass.defaultfont())
638 + font_metrics::signedWidth(par.layout()->rightmargin,
639 tclass.defaultfont())
640 * 4 / (par.getDepth() + 4);
646 int LyXText::labelEnd(par_type const pit) const
648 // labelEnd is only needed if the layout fills a flushleft label.
649 if (pars_[pit].layout()->margintype != MARGIN_MANUAL)
651 // return the beginning of the body
652 return leftMargin(pit);
658 // this needs special handling - only newlines count as a break point
659 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
661 pos_type const end = par.size();
664 if (par.isNewline(i))
673 void LyXText::rowBreakPoint(par_type const pit, Row & row) const
675 Paragraph const & par = pars_[pit];
676 pos_type const end = par.size();
677 pos_type const pos = row.pos();
683 // maximum pixel width of a row
684 int width = maxwidth_ - rightMargin(par); // - leftMargin(pit, row);
690 LyXLayout_ptr const & layout = par.layout();
692 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
693 row.endpos(addressBreakPoint(pos, par));
697 pos_type const body_pos = par.beginOfBody();
700 // Now we iterate through until we reach the right margin
701 // or the end of the par, then choose the possible break
704 int const left = leftMargin(pit, pos);
707 // pixel width since last breakpoint
710 FontIterator fi = FontIterator(*this, pit, pos);
711 pos_type point = end;
713 for ( ; i < end; ++i, ++fi) {
714 char const c = par.getChar(i);
717 int thiswidth = singleWidth(par, i, c, *fi);
719 // add the auto-hfill from label end to the body
720 if (body_pos && i == body_pos) {
721 int add = font_metrics::width(layout->labelsep, getLabelFont(par));
722 if (par.isLineSeparator(i - 1))
723 add -= singleWidth(par, i - 1);
725 add = std::max(add, labelEnd(pit) - x);
730 chunkwidth += thiswidth;
733 // break before a character that will fall off
734 // the right of the row
736 // if no break before, break here
737 if (point == end || chunkwidth >= width - left) {
744 // exit on last registered breakpoint:
748 if (par.isNewline(i)) {
754 if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
759 if (par.isInset(i) && par.getInset(i)->display()) {
765 if (!par.isInset(i) || par.getInset(i)->isChar()) {
766 // some insets are line separators too
767 if (par.isLineSeparator(i)) {
768 // register breakpoint:
775 // maybe found one, but the par is short enough.
776 if (i == end && x < width)
779 // manual labels cannot be broken in LaTeX. But we
780 // want to make our on-screen rendering of footnotes
782 if (body_pos && point < body_pos)
789 void LyXText::setRowWidth(par_type const pit, Row & row) const
791 // get the pure distance
792 pos_type const end = row.endpos();
794 Paragraph const & par = pars_[pit];
795 string labelsep = par.layout()->labelsep;
796 int w = leftMargin(pit, row.pos());
798 pos_type const body_pos = par.beginOfBody();
799 pos_type i = row.pos();
802 FontIterator fi = FontIterator(*this, pit, i);
803 for ( ; i < end; ++i, ++fi) {
804 if (body_pos > 0 && i == body_pos) {
805 w += font_metrics::width(labelsep, getLabelFont(par));
806 if (par.isLineSeparator(i - 1))
807 w -= singleWidth(par, i - 1);
808 w = max(w, labelEnd(pit));
810 char const c = par.getChar(i);
811 w += singleWidth(par, i, c, *fi);
815 if (body_pos > 0 && body_pos >= end) {
816 w += font_metrics::width(labelsep, getLabelFont(par));
817 if (end > 0 && par.isLineSeparator(end - 1))
818 w -= singleWidth(par, end - 1);
819 w = max(w, labelEnd(pit));
822 row.width(w + rightMargin(par));
826 // returns the minimum space a manual label needs on the screen in pixel
827 int LyXText::labelFill(Paragraph const & par, Row const & row) const
829 pos_type last = par.beginOfBody();
831 BOOST_ASSERT(last > 0);
833 // -1 because a label ends with a space that is in the label
836 // a separator at this end does not count
837 if (par.isLineSeparator(last))
841 for (pos_type i = row.pos(); i <= last; ++i)
842 w += singleWidth(par, i);
844 string const & label = par.params().labelWidthString();
848 return max(0, font_metrics::width(label, getLabelFont(par)) - w);
852 LColor_color LyXText::backgroundColor() const
854 return LColor_color(LColor::color(background_color_));
858 void LyXText::setHeightOfRow(par_type const pit, Row & row)
860 Paragraph const & par = pars_[pit];
861 // get the maximum ascent and the maximum descent
862 double layoutasc = 0;
863 double layoutdesc = 0;
864 double const dh = defaultRowHeight();
866 // ok, let us initialize the maxasc and maxdesc value.
867 // Only the fontsize count. The other properties
868 // are taken from the layoutfont. Nicer on the screen :)
869 LyXLayout_ptr const & layout = par.layout();
871 // as max get the first character of this row then it can
872 // increase but not decrease the height. Just some point to
873 // start with so we don't have to do the assignment below too
875 LyXFont font = getFont(par, row.pos());
876 LyXFont::FONT_SIZE const tmpsize = font.size();
877 font = getLayoutFont(pit);
878 LyXFont::FONT_SIZE const size = font.size();
879 font.setSize(tmpsize);
881 LyXFont labelfont = getLabelFont(par);
883 // these are minimum values
884 double const spacing_val = layout->spacing.getValue() * spacing(par);
885 //lyxerr << "spacing_val = " << spacing_val << endl;
886 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
887 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
889 // insets may be taller
890 InsetList::const_iterator ii = par.insetlist.begin();
891 InsetList::const_iterator iend = par.insetlist.end();
892 for ( ; ii != iend; ++ii) {
893 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
894 maxasc = max(maxasc, ii->inset->ascent());
895 maxdesc = max(maxdesc, ii->inset->descent());
899 // Check if any custom fonts are larger (Asger)
900 // This is not completely correct, but we can live with the small,
901 // cosmetic error for now.
903 pos_type const pos_end = row.endpos();
905 LyXFont::FONT_SIZE maxsize =
906 par.highestFontInRange(row.pos(), pos_end, size);
907 if (maxsize > font.size()) {
908 font.setSize(maxsize);
909 maxasc = max(maxasc, font_metrics::maxAscent(font));
910 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
913 // This is nicer with box insets:
917 row.ascent_of_text(maxasc);
920 if (row.pos() == 0) {
921 BufferParams const & bufparams = bv()->buffer()->params();
922 // some parksips VERY EASY IMPLEMENTATION
923 if (bv()->buffer()->params().paragraph_separation
924 == BufferParams::PARSEP_SKIP
926 && ((layout->isParagraph() && par.getDepth() == 0)
927 || (pars_[pit - 1].layout()->isParagraph()
928 && pars_[pit - 1].getDepth() == 0)))
930 maxasc += bufparams.getDefSkip().inPixels(*bv());
933 if (pars_[pit].params().startOfAppendix())
934 maxasc += int(3 * dh);
936 // This is special code for the chapter, since the label of this
937 // layout is printed in an extra row
938 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
939 labeladdon = int(font_metrics::maxHeight(labelfont)
940 * layout->spacing.getValue()
944 // special code for the top label
945 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
946 || layout->labeltype == LABEL_BIBLIO
947 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
948 && isFirstInSequence(pit, paragraphs())
949 && !par.getLabelstring().empty())
952 font_metrics::maxHeight(labelfont)
953 * layout->spacing.getValue()
955 + (layout->topsep + layout->labelbottomsep) * dh);
958 // Add the layout spaces, for example before and after
959 // a section, or between the items of a itemize or enumerate
962 par_type prev = depthHook(pit, pars_, par.getDepth());
964 && pars_[prev].layout() == layout
965 && pars_[prev].getDepth() == par.getDepth()
966 && pars_[prev].getLabelWidthString() == par.getLabelWidthString())
968 layoutasc = layout->itemsep * dh;
969 } else if (pit != 0 || row.pos() != 0) {
970 if (layout->topsep > 0)
971 layoutasc = layout->topsep * dh;
974 prev = outerHook(pit, pars_);
975 if (prev != par_type(pars_.size())) {
976 maxasc += int(pars_[prev].layout()->parsep * dh);
977 } else if (pit != 0) {
978 if (pars_[pit - 1].getDepth() != 0 ||
979 pars_[pit - 1].layout() == layout) {
980 maxasc += int(layout->parsep * dh);
985 // is it a bottom line?
986 if (row.endpos() >= par.size()) {
987 // add the layout spaces, for example before and after
988 // a section, or between the items of a itemize or enumerate
990 par_type nextpit = pit + 1;
991 if (nextpit != par_type(pars_.size())) {
996 if (pars_[cpit].getDepth() > pars_[nextpit].getDepth()) {
997 usual = pars_[cpit].layout()->bottomsep * dh;
998 cpit = depthHook(cpit, paragraphs(), pars_[nextpit].getDepth());
999 if (pars_[cpit].layout() != pars_[nextpit].layout()
1000 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1002 unusual = pars_[cpit].layout()->bottomsep * dh;
1004 layoutdesc = max(unusual, usual);
1005 } else if (pars_[cpit].getDepth() == pars_[nextpit].getDepth()) {
1006 if (pars_[cpit].layout() != pars_[nextpit].layout()
1007 || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
1008 layoutdesc = int(pars_[cpit].layout()->bottomsep * dh);
1013 // incalculate the layout spaces
1014 maxasc += int(layoutasc * 2 / (2 + pars_[pit].getDepth()));
1015 maxdesc += int(layoutdesc * 2 / (2 + pars_[pit].getDepth()));
1017 row.height(maxasc + maxdesc + labeladdon);
1018 row.baseline(maxasc + labeladdon);
1019 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
1027 void LyXText::breakParagraph(LCursor & cur, char keep_layout)
1029 BOOST_ASSERT(this == cur.text());
1030 // allow only if at start or end, or all previous is new text
1031 Paragraph & cpar = cur.paragraph();
1032 par_type cpit = cur.par();
1034 if (cur.pos() != 0 && cur.pos() != cur.lastpos()
1035 && cpar.isChangeEdited(0, cur.pos()))
1038 LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1039 LyXLayout_ptr const & layout = cpar.layout();
1041 // this is only allowed, if the current paragraph is not empty
1042 // or caption and if it has not the keepempty flag active
1043 if (cur.lastpos() == 0 && !cpar.allowEmpty()
1044 && layout->labeltype != LABEL_SENSITIVE)
1047 // a layout change may affect also the following paragraph
1048 recUndo(cur.par(), undoSpan(cur.par()) - 1);
1050 // Always break behind a space
1051 // It is better to erase the space (Dekel)
1052 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1053 cpar.erase(cur.pos());
1055 // break the paragraph
1059 keep_layout = layout->isEnvironment();
1061 // we need to set this before we insert the paragraph. IMO the
1062 // breakParagraph call should return a bool if it inserts the
1063 // paragraph before or behind and we should react on that one
1064 // but we can fix this in 1.3.0 (Jug 20020509)
1065 bool const isempty = cpar.allowEmpty() && cpar.empty();
1066 ::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1067 cur.pos(), keep_layout);
1070 par_type next_par = cpit + 1;
1072 // well this is the caption hack since one caption is really enough
1073 if (layout->labeltype == LABEL_SENSITIVE) {
1075 // set to standard-layout
1076 pars_[cpit].applyLayout(tclass.defaultLayout());
1078 // set to standard-layout
1079 pars_[next_par].applyLayout(tclass.defaultLayout());
1082 // if the cursor is at the beginning of a row without prior newline,
1084 // This touches only the screen-update. Otherwise we would may have
1085 // an empty row on the screen
1086 if (cur.pos() != 0 && cur.textRow().pos() == cur.pos()
1087 && !pars_[cpit].isNewline(cur.pos() - 1))
1092 while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1093 pars_[next_par].erase(0);
1096 redoParagraph(cpit);
1097 redoParagraph(next_par);
1099 // This check is necessary. Otherwise the new empty paragraph will
1100 // be deleted automatically. And it is more friendly for the user!
1101 if (cur.pos() != 0 || isempty)
1102 setCursor(cur, cur.par() + 1, 0);
1104 setCursor(cur, cur.par(), 0);
1108 // convenience function
1109 void LyXText::redoParagraph(LCursor & cur)
1111 BOOST_ASSERT(this == cur.text());
1112 cur.clearSelection();
1113 redoParagraph(cur.par());
1114 setCursorIntern(cur, cur.par(), cur.pos());
1118 // insert a character, moves all the following breaks in the
1119 // same Paragraph one to the right and make a rebreak
1120 void LyXText::insertChar(LCursor & cur, char c)
1122 BOOST_ASSERT(this == cur.text());
1123 BOOST_ASSERT(c != Paragraph::META_INSET);
1125 recordUndo(cur, Undo::INSERT);
1127 Paragraph & par = cur.paragraph();
1128 // try to remove this
1129 par_type const pit = cur.par();
1131 bool const freeSpacing = par.layout()->free_spacing ||
1132 par.isFreeSpacing();
1134 if (lyxrc.auto_number) {
1135 static string const number_operators = "+-/*";
1136 static string const number_unary_operators = "+-";
1137 static string const number_seperators = ".,:";
1139 if (current_font.number() == LyXFont::ON) {
1140 if (!IsDigit(c) && !contains(number_operators, c) &&
1141 !(contains(number_seperators, c) &&
1143 cur.pos() != cur.lastpos() &&
1144 getFont(par, cur.pos()).number() == LyXFont::ON &&
1145 getFont(par, cur.pos() - 1).number() == LyXFont::ON)
1147 number(cur); // Set current_font.number to OFF
1148 } else if (IsDigit(c) &&
1149 real_current_font.isVisibleRightToLeft()) {
1150 number(cur); // Set current_font.number to ON
1152 if (cur.pos() != 0) {
1153 char const c = par.getChar(cur.pos() - 1);
1154 if (contains(number_unary_operators, c) &&
1156 || par.isSeparator(cur.pos() - 2)
1157 || par.isNewline(cur.pos() - 2))
1159 setCharFont(pit, cur.pos() - 1, current_font);
1160 } else if (contains(number_seperators, c)
1162 && getFont(par, cur.pos() - 2).number() == LyXFont::ON) {
1163 setCharFont(pit, cur.pos() - 1, current_font);
1169 // First check, if there will be two blanks together or a blank at
1170 // the beginning of a paragraph.
1171 // I decided to handle blanks like normal characters, the main
1172 // difference are the special checks when calculating the row.fill
1173 // (blank does not count at the end of a row) and the check here
1175 // The bug is triggered when we type in a description environment:
1176 // The current_font is not changed when we go from label to main text
1177 // and it should (along with realtmpfont) when we type the space.
1178 // CHECK There is a bug here! (Asger)
1180 // store the current font. This is because of the use of cursor
1181 // movements. The moving cursor would refresh the current font
1182 LyXFont realtmpfont = real_current_font;
1183 LyXFont rawtmpfont = current_font;
1185 // When the free-spacing option is set for the current layout,
1186 // disable the double-space checking
1187 if (!freeSpacing && IsLineSeparatorChar(c)) {
1188 if (cur.pos() == 0) {
1189 static bool sent_space_message = false;
1190 if (!sent_space_message) {
1191 cur.message(_("You cannot insert a space at the "
1192 "beginning of a paragraph. Please read the Tutorial."));
1193 sent_space_message = true;
1197 BOOST_ASSERT(cur.pos() > 0);
1198 if (par.isLineSeparator(cur.pos() - 1)
1199 || par.isNewline(cur.pos() - 1)) {
1200 static bool sent_space_message = false;
1201 if (!sent_space_message) {
1202 cur.message(_("You cannot type two spaces this way. "
1203 "Please read the Tutorial."));
1204 sent_space_message = true;
1210 par.insertChar(cur.pos(), c, rawtmpfont);
1212 current_font = rawtmpfont;
1213 real_current_font = realtmpfont;
1215 setCursor(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
1220 void LyXText::charInserted()
1222 // Here we call finishUndo for every 20 characters inserted.
1223 // This is from my experience how emacs does it. (Lgb)
1224 static unsigned int counter;
1234 RowMetrics LyXText::computeRowMetrics(par_type const pit, Row const & row) const
1237 Paragraph const & par = pars_[pit];
1239 double w = width_ - row.width();
1241 bool const is_rtl = isRTL(par);
1243 result.x = rightMargin(par);
1245 result.x = leftMargin(pit, row.pos());
1247 // is there a manual margin with a manual label
1248 LyXLayout_ptr const & layout = par.layout();
1250 if (layout->margintype == MARGIN_MANUAL
1251 && layout->labeltype == LABEL_MANUAL) {
1252 /// We might have real hfills in the label part
1253 int nlh = numberOfLabelHfills(par, row);
1255 // A manual label par (e.g. List) has an auto-hfill
1256 // between the label text and the body of the
1258 // But we don't want to do this auto hfill if the par
1263 if (nlh && !par.getLabelWidthString().empty())
1264 result.label_hfill = labelFill(par, row) / double(nlh);
1267 // are there any hfills in the row?
1268 int const nh = numberOfHfills(par, row);
1272 result.hfill = w / nh;
1273 // we don't have to look at the alignment if it is ALIGN_LEFT and
1274 // if the row is already larger then the permitted width as then
1275 // we force the LEFT_ALIGN'edness!
1276 } else if (int(row.width()) < maxwidth_) {
1277 // is it block, flushleft or flushright?
1278 // set x how you need it
1280 if (par.params().align() == LYX_ALIGN_LAYOUT)
1281 align = layout->align;
1283 align = par.params().align();
1285 // Display-style insets should always be on a centred row
1286 // The test on par.size() is to catch zero-size pars, which
1287 // would trigger the assert in Paragraph::getInset().
1288 //inset = par.size() ? par.getInset(row.pos()) : 0;
1290 && par.isInset(row.pos())
1291 && par.getInset(row.pos())->display())
1293 align = LYX_ALIGN_CENTER;
1297 case LYX_ALIGN_BLOCK: {
1298 int const ns = numberOfSeparators(par, row);
1299 bool disp_inset = false;
1300 if (row.endpos() < par.size()) {
1301 InsetBase const * in = par.getInset(row.endpos());
1303 disp_inset = in->display();
1305 // If we have separators, this is not the last row of a
1306 // par, does not end in newline, and is not row above a
1307 // display inset... then stretch it
1309 && row.endpos() < par.size()
1310 && !par.isNewline(row.endpos() - 1)
1313 result.separator = w / ns;
1314 } else if (is_rtl) {
1319 case LYX_ALIGN_RIGHT:
1322 case LYX_ALIGN_CENTER:
1328 bidi.computeTables(par, *bv()->buffer(), row);
1330 pos_type body_pos = par.beginOfBody();
1331 pos_type end = row.endpos();
1334 && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1336 result.x += font_metrics::width(layout->labelsep, getLabelFont(par));
1337 if (body_pos <= end)
1338 result.x += result.label_hfill;
1346 // the cursor set functions have a special mechanism. When they
1347 // realize, that you left an empty paragraph, they will delete it.
1349 void LyXText::cursorRightOneWord(LCursor & cur)
1351 BOOST_ASSERT(this == cur.text());
1352 if (cur.pos() == cur.lastpos() && cur.par() != cur.lastpar()) {
1356 // Skip through initial nonword stuff.
1357 // Treat floats and insets as words.
1358 while (cur.pos() != cur.lastpos() && !cur.paragraph().isLetter(cur.pos()))
1360 // Advance through word.
1361 while (cur.pos() != cur.lastpos() && cur.paragraph().isLetter(cur.pos()))
1364 setCursor(cur, cur.par(), cur.pos());
1368 void LyXText::cursorLeftOneWord(LCursor & cur)
1370 BOOST_ASSERT(this == cur.text());
1371 if (cur.pos() == 0 && cur.par() != 0) {
1373 cur.pos() = cur.lastpos();
1375 // Skip through initial nonword stuff.
1376 // Treat floats and insets as words.
1377 while (cur.pos() != 0 && !cur.paragraph().isLetter(cur.pos() - 1))
1379 // Advance through word.
1380 while (cur.pos() != 0 && cur.paragraph().isLetter(cur.pos() - 1))
1383 setCursor(cur, cur.par(), cur.pos());
1387 void LyXText::selectWord(LCursor & cur, word_location loc)
1389 BOOST_ASSERT(this == cur.text());
1390 CursorSlice from = cur.top();
1391 CursorSlice to = cur.top();
1392 getWord(from, to, loc);
1393 if (cur.top() != from)
1394 setCursor(cur, from.par(), from.pos());
1398 setCursor(cur, to.par(), to.pos());
1403 // Select the word currently under the cursor when no
1404 // selection is currently set
1405 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1407 BOOST_ASSERT(this == cur.text());
1408 if (cur.selection())
1410 selectWord(cur, loc);
1411 return cur.selection();
1415 void LyXText::acceptChange(LCursor & cur)
1417 BOOST_ASSERT(this == cur.text());
1418 if (!cur.selection() && cur.lastpos() != 0)
1421 CursorSlice const & startc = cur.selBegin();
1422 CursorSlice const & endc = cur.selEnd();
1423 if (startc.par() == endc.par()) {
1424 recordUndoSelection(cur, Undo::INSERT);
1425 pars_[startc.par()].acceptChange(startc.pos(), endc.pos());
1427 cur.clearSelection();
1428 redoParagraph(startc.par());
1429 setCursorIntern(cur, startc.par(), 0);
1431 #ifdef WITH_WARNINGS
1432 #warning handle multi par selection
1437 void LyXText::rejectChange(LCursor & cur)
1439 BOOST_ASSERT(this == cur.text());
1440 if (!cur.selection() && cur.lastpos() != 0)
1443 CursorSlice const & startc = cur.selBegin();
1444 CursorSlice const & endc = cur.selEnd();
1445 if (startc.par() == endc.par()) {
1446 recordUndoSelection(cur, Undo::INSERT);
1447 pars_[startc.par()].rejectChange(startc.pos(), endc.pos());
1449 cur.clearSelection();
1450 redoParagraph(startc.par());
1451 setCursorIntern(cur, startc.par(), 0);
1453 #ifdef WITH_WARNINGS
1454 #warning handle multi par selection
1459 // Delete from cursor up to the end of the current or next word.
1460 void LyXText::deleteWordForward(LCursor & cur)
1462 BOOST_ASSERT(this == cur.text());
1463 if (cur.lastpos() == 0)
1467 cur.selection() = true;
1468 cursorRightOneWord(cur);
1470 cutSelection(cur, true, false);
1475 // Delete from cursor to start of current or prior word.
1476 void LyXText::deleteWordBackward(LCursor & cur)
1478 BOOST_ASSERT(this == cur.text());
1479 if (cur.lastpos() == 0)
1483 cur.selection() = true;
1484 cursorLeftOneWord(cur);
1486 cutSelection(cur, true, false);
1491 // Kill to end of line.
1492 void LyXText::deleteLineForward(LCursor & cur)
1494 BOOST_ASSERT(this == cur.text());
1495 if (cur.lastpos() == 0) {
1496 // Paragraph is empty, so we just go to the right
1500 cur.selection() = true; // to avoid deletion
1503 // What is this test for ??? (JMarc)
1504 if (!cur.selection())
1505 deleteWordForward(cur);
1507 cutSelection(cur, true, false);
1512 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1514 BOOST_ASSERT(this == cur.text());
1518 if (cur.selection()) {
1519 from = cur.selBegin();
1523 getWord(from, to, lyx::PARTIAL_WORD);
1524 setCursor(cur, to.par(), to.pos() + 1);
1527 recordUndoSelection(cur);
1529 pos_type pos = from.pos();
1530 int par = from.par();
1532 while (par != int(pars_.size()) && (pos != to.pos() || par != to.par())) {
1534 if (pos == pars_[pit].size()) {
1539 unsigned char c = pars_[pit].getChar(pos);
1540 if (c != Paragraph::META_INSET) {
1542 case text_lowercase:
1545 case text_capitalization:
1547 action = text_lowercase;
1549 case text_uppercase:
1554 #ifdef WITH_WARNINGS
1557 pars_[pit].setChar(pos, c);
1563 void LyXText::Delete(LCursor & cur)
1565 BOOST_ASSERT(this == cur.text());
1566 if (cur.pos() != cur.lastpos()) {
1567 recordUndo(cur, Undo::DELETE, cur.par());
1568 setCursorIntern(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
1571 // should we do anything in an else branch?
1575 void LyXText::backspace(LCursor & cur)
1577 BOOST_ASSERT(this == cur.text());
1578 if (cur.pos() == 0) {
1579 // The cursor is at the beginning of a paragraph, so
1580 // the the backspace will collapse two paragraphs into
1583 // but it's not allowed unless it's new
1584 Paragraph & par = cur.paragraph();
1585 if (par.isChangeEdited(0, par.size()))
1588 // we may paste some paragraphs
1590 // is it an empty paragraph?
1591 pos_type lastpos = cur.lastpos();
1592 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1593 // This is an empty paragraph and we delete it just
1594 // by moving the cursor one step
1595 // left and let the DeleteEmptyParagraphMechanism
1596 // handle the actual deletion of the paragraph.
1598 if (cur.par() != 0) {
1600 // the layout things can change the height of a row !
1607 recordUndo(cur, Undo::DELETE, cur.par() - 1);
1609 par_type tmppit = cur.par();
1610 // We used to do cursorLeftIntern() here, but it is
1611 // not a good idea since it triggers the auto-delete
1612 // mechanism. So we do a cursorLeftIntern()-lite,
1613 // without the dreaded mechanism. (JMarc)
1614 if (cur.par() != 0) {
1615 // steps into the above paragraph.
1616 setCursorIntern(cur, cur.par() - 1,
1617 pars_[cur.par() - 1].size(),
1621 // Pasting is not allowed, if the paragraphs have different
1622 // layout. I think it is a real bug of all other
1623 // word processors to allow it. It confuses the user.
1624 // Correction: Pasting is always allowed with standard-layout
1625 Buffer & buf = cur.buffer();
1626 BufferParams const & bufparams = buf.params();
1627 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1628 par_type const cpit = cur.par();
1631 && (pars_[cpit].layout() == pars_[tmppit].layout()
1632 || pars_[tmppit].layout() == tclass.defaultLayout())
1633 && pars_[cpit].getAlign() == pars_[tmppit].getAlign()) {
1634 mergeParagraph(bufparams, pars_, cpit);
1636 if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
1639 // the counters may have changed
1641 setCursor(cur, cur.par(), cur.pos(), false);
1644 // this is the code for a normal backspace, not pasting
1646 recordUndo(cur, Undo::DELETE);
1647 // We used to do cursorLeftIntern() here, but it is
1648 // not a good idea since it triggers the auto-delete
1649 // mechanism. So we do a cursorLeftIntern()-lite,
1650 // without the dreaded mechanism. (JMarc)
1651 setCursorIntern(cur, cur.par(), cur.pos() - 1,
1652 false, cur.boundary());
1653 cur.paragraph().erase(cur.pos());
1656 if (cur.pos() == cur.lastpos())
1657 setCurrentFont(cur);
1660 setCursor(cur, cur.par(), cur.pos(), false, cur.boundary());
1664 Paragraph & LyXText::getPar(par_type par) const
1666 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1667 BOOST_ASSERT(par >= 0);
1668 BOOST_ASSERT(par < int(paragraphs().size()));
1669 return paragraphs()[par];
1673 Row const & LyXText::firstRow() const
1675 return *paragraphs().front().rows.begin();
1679 void LyXText::redoParagraphInternal(par_type const pit)
1681 // remove rows of paragraph, keep track of height changes
1682 Paragraph & par = pars_[pit];
1683 height_ -= par.height;
1691 InsetList::iterator ii = par.insetlist.begin();
1692 InsetList::iterator iend = par.insetlist.end();
1693 for (; ii != iend; ++ii) {
1695 int const w = maxwidth_ - leftMargin(pit) - rightMargin(par);
1696 MetricsInfo mi(bv(), getFont(par, ii->pos), w);
1697 ii->inset->metrics(mi, dim);
1700 // rebreak the paragraph
1701 par.setBeginOfBody();
1705 rowBreakPoint(pit, row);
1706 setRowWidth(pit, row);
1707 setHeightOfRow(pit, row);
1708 row.y_offset(par.height);
1709 par.rows.push_back(row);
1710 par.width = std::max(par.width, row.width());
1711 par.height += row.height();
1713 } while (z < par.size());
1715 height_ += par.height;
1716 //lyxerr << "redoParagraph: " << par.rows.size() << " rows\n";
1720 void LyXText::redoParagraphs(par_type pit, par_type end)
1722 for (; pit != end; ++pit)
1723 redoParagraphInternal(pit);
1724 updateParPositions();
1729 void LyXText::redoParagraph(par_type pit)
1731 redoParagraphInternal(pit);
1732 updateParPositions();
1736 void LyXText::fullRebreak()
1738 redoParagraphs(0, paragraphs().size());
1739 bv()->cursor().resetAnchor();
1743 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1745 //BOOST_ASSERT(mi.base.textwidth);
1746 if (mi.base.textwidth)
1747 maxwidth_ = mi.base.textwidth;
1748 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1749 //<< " maxWidth: " << maxwidth << "\nfont: " << mi.base.font
1752 // Rebuild row cache. This recomputes height as well.
1753 redoParagraphs(0, paragraphs().size());
1755 width_ = maxParagraphWidth(paragraphs());
1758 dim.asc = firstRow().ascent_of_text();
1759 dim.des = height_ - dim.asc;
1764 // only used for inset right now. should also be used for main text
1765 void LyXText::draw(PainterInfo & pi, int x, int y) const
1769 paintTextInset(*this, pi);
1773 // only used for inset right now. should also be used for main text
1774 void LyXText::drawSelection(PainterInfo &, int, int) const
1776 //lyxerr << "LyXText::drawSelection at " << x << " " << y << endl;
1780 bool LyXText::isLastRow(par_type pit, Row const & row) const
1782 return row.endpos() >= pars_[pit].size()
1783 && pit + 1 == par_type(paragraphs().size());
1787 bool LyXText::isFirstRow(par_type pit, Row const & row) const
1789 return row.pos() == 0 && pit == 0;
1793 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1794 word_location const loc)
1796 Paragraph const & from_par = pars_[from.par()];
1798 case lyx::WHOLE_WORD_STRICT:
1799 if (from.pos() == 0 || from.pos() == from_par.size()
1800 || !from_par.isLetter(from.pos())
1801 || !from_par.isLetter(from.pos() - 1)) {
1805 // no break here, we go to the next
1807 case lyx::WHOLE_WORD:
1808 // If we are already at the beginning of a word, do nothing
1809 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
1811 // no break here, we go to the next
1813 case lyx::PREVIOUS_WORD:
1814 // always move the cursor to the beginning of previous word
1815 while (from.pos() && from_par.isLetter(from.pos() - 1))
1818 case lyx::NEXT_WORD:
1819 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1822 case lyx::PARTIAL_WORD:
1823 // no need to move the 'from' cursor
1827 Paragraph & to_par = pars_[to.par()];
1828 while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
1833 void LyXText::write(Buffer const & buf, std::ostream & os) const
1835 ParagraphList::const_iterator pit = paragraphs().begin();
1836 ParagraphList::const_iterator end = paragraphs().end();
1837 Paragraph::depth_type dth = 0;
1838 for (; pit != end; ++pit)
1839 pit->write(buf, os, buf.params(), dth);
1843 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1845 static Change current_change;
1847 Paragraph::depth_type depth = 0;
1849 while (lex.isOK()) {
1851 string const token = lex.getString();
1856 if (token == "\\end_inset") {
1860 if (token == "\\end_body") {
1864 if (token == "\\begin_body") {
1868 if (token == "\\end_document") {
1872 if (token == "\\begin_layout") {
1873 lex.pushToken(token);
1876 par.params().depth(depth);
1877 if (buf.params().tracking_changes)
1879 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1880 pars_.push_back(par);
1882 // FIXME: goddamn InsetTabular makes us pass a Buffer
1884 ::readParagraph(buf, pars_.back(), lex);
1886 } else if (token == "\\begin_deeper") {
1888 } else if (token == "\\end_deeper") {
1890 lex.printError("\\end_deeper: " "depth is already null");
1895 lyxerr << "Handling unknown body token: `"
1896 << token << '\'' << endl;
1903 int LyXText::ascent() const
1905 return firstRow().ascent_of_text();
1909 int LyXText::descent() const
1911 return height_ - firstRow().ascent_of_text();
1915 int LyXText::cursorX(CursorSlice const & cur) const
1917 par_type const pit = cur.par();
1918 Paragraph const & par = pars_[pit];
1919 if (par.rows.empty())
1922 Row const & row = *par.getRow(cur.pos());
1924 pos_type pos = cur.pos();
1925 pos_type cursor_vpos = 0;
1927 RowMetrics const m = computeRowMetrics(pit, row);
1930 pos_type const row_pos = row.pos();
1931 pos_type const end = row.endpos();
1934 cursor_vpos = row_pos;
1935 else if (pos >= end)
1936 cursor_vpos = isRTL(par) ? row_pos : end;
1937 else if (pos > row_pos && pos >= end)
1938 // Place cursor after char at (logical) position pos - 1
1939 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
1940 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
1942 // Place cursor before char at (logical) position pos
1943 cursor_vpos = (bidi.level(pos) % 2 == 0)
1944 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
1946 pos_type body_pos = par.beginOfBody();
1948 (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1951 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1952 pos_type pos = bidi.vis2log(vpos);
1953 if (body_pos > 0 && pos == body_pos - 1) {
1955 + font_metrics::width(par.layout()->labelsep,
1957 if (par.isLineSeparator(body_pos - 1))
1958 x -= singleWidth(par, body_pos - 1);
1961 if (hfillExpansion(par, row, pos)) {
1962 x += singleWidth(par, pos);
1963 if (pos >= body_pos)
1967 } else if (par.isSeparator(pos)) {
1968 x += singleWidth(par, pos);
1969 if (pos >= body_pos)
1972 x += singleWidth(par, pos);
1974 return xo_ + int(x);
1978 int LyXText::cursorY(CursorSlice const & cur) const
1980 Paragraph const & par = getPar(cur.par());
1981 Row const & row = *par.getRow(cur.pos());
1982 return yo_ + par.y + row.y_offset() + row.baseline();
1986 // Returns the current font and depth as a message.
1987 string LyXText::currentState(LCursor & cur)
1989 BOOST_ASSERT(this == cur.text());
1990 Buffer & buf = cur.buffer();
1991 Paragraph const & par = cur.paragraph();
1992 std::ostringstream os;
1994 bool const show_change = buf.params().tracking_changes
1995 && cur.pos() != cur.lastpos()
1996 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
1999 Change change = par.lookupChangeFull(cur.pos());
2000 Author const & a = buf.params().authors().get(change.author);
2001 os << _("Change: ") << a.name();
2002 if (!a.email().empty())
2003 os << " (" << a.email() << ")";
2004 if (change.changetime)
2005 os << _(" at ") << ctime(&change.changetime);
2009 // I think we should only show changes from the default
2011 LyXFont font = real_current_font;
2012 font.reduce(buf.params().getLyXTextClass().defaultfont());
2014 // avoid _(...) re-entrance problem
2015 string const s = font.stateText(&buf.params());
2016 os << bformat(_("Font: %1$s"), s);
2018 // os << bformat(_("Font: %1$s"), font.stateText(&buf.params));
2020 // The paragraph depth
2021 int depth = cur.paragraph().getDepth();
2023 os << bformat(_(", Depth: %1$s"), tostr(depth));
2025 // The paragraph spacing, but only if different from
2027 Spacing const & spacing = par.params().spacing();
2028 if (!spacing.isDefault()) {
2029 os << _(", Spacing: ");
2030 switch (spacing.getSpace()) {
2031 case Spacing::Single:
2034 case Spacing::Onehalf:
2037 case Spacing::Double:
2040 case Spacing::Other:
2041 os << _("Other (") << spacing.getValue() << ')';
2043 case Spacing::Default:
2044 // should never happen, do nothing
2049 #ifdef DEVEL_VERSION
2050 os << _(", Inset: ") << &cur.inset();
2051 os << _(", Paragraph: ") << cur.par();
2052 os << _(", Id: ") << par.id();
2053 os << _(", Position: ") << cur.pos();
2054 Row & row = cur.textRow();
2055 os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2061 string LyXText::getPossibleLabel(LCursor & cur) const
2063 par_type pit = cur.par();
2065 LyXLayout_ptr layout = pars_[pit].layout();
2067 if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2068 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2069 if (layout2->latextype != LATEX_PARAGRAPH) {
2075 string text = layout->latexname().substr(0, 3);
2076 if (layout->latexname() == "theorem")
2077 text = "thm"; // Create a correct prefix for prettyref
2080 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2083 string par_text = pars_[pit].asString(cur.buffer(), false);
2084 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2085 if (par_text.empty())
2088 par_text = split(par_text, head, ' ');
2089 // Is it legal to use spaces in labels ?
2099 // Manhattan distance to nearest corner
2100 int LyXText::dist(int x, int y) const
2107 else if (x > xo_ + int(width_))
2108 xx = x - xo_ - width_;
2110 if (y < yo_ - ascent())
2111 yy = yo_ - ascent() - y;
2112 else if (y > yo_ + descent())
2113 yy = y - yo_ - descent();
2115 lyxerr << " xo_=" << xo_ << " yo_=" << yo_
2116 << " width_=" << width_ << " ascent=" << ascent()
2117 << " descent=" << descent()
2118 << " dist=" << xx+yy <<endl;