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
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "paragraph.h"
21 #include "paragraph_pimpl.h"
24 #include "bufferparams.h"
29 #include "latexrunparams.h"
36 #include "insets/insetbibitem.h"
37 #include "insets/insetoptarg.h"
39 #include "support/lstrings.h"
40 #include "support/textutils.h"
41 #include "support/std_sstream.h"
45 using lyx::support::contains;
46 using lyx::support::subst;
51 using std::ostringstream;
54 Paragraph::Paragraph()
55 : y(0), height(0), pimpl_(new Paragraph::Pimpl(this))
62 Paragraph::Paragraph(Paragraph const & lp)
63 : y(0), height(0), text_(lp.text_), pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this))
66 // this is because of the dummy layout of the paragraphs that
68 layout_ = lp.layout();
70 // copy everything behind the break-position to the new paragraph
71 insetlist = lp.insetlist;
72 InsetList::iterator it = insetlist.begin();
73 InsetList::iterator end = insetlist.end();
74 for (; it != end; ++it) {
75 // currently we hold Inset*, not InsetBase*
76 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
81 void Paragraph::operator=(Paragraph const & lp)
83 // needed as we will destroy the pimpl_ before copying it
87 lyxerr << "Paragraph::operator=()" << endl;
92 pimpl_ = new Pimpl(*lp.pimpl_, this);
94 itemdepth = lp.itemdepth;
95 // this is because of the dummy layout of the paragraphs that
97 layout_ = lp.layout();
99 // copy everything behind the break-position to the new paragraph
100 insetlist = lp.insetlist;
101 InsetList::iterator it = insetlist.begin();
102 InsetList::iterator end = insetlist.end();
103 for (; it != end; ++it) {
104 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
108 // the destructor removes the new paragraph from the list
109 Paragraph::~Paragraph()
113 //lyxerr << "Paragraph::paragraph_id = "
114 // << Paragraph::paragraph_id << endl;
118 void Paragraph::write(Buffer const & buf, ostream & os,
119 BufferParams const & bparams,
120 depth_type & dth) const
122 // The beginning or end of a deeper (i.e. nested) area?
123 if (dth != params().depth()) {
124 if (params().depth() > dth) {
125 while (params().depth() > dth) {
126 os << "\n\\begin_deeper ";
130 while (params().depth() < dth) {
131 os << "\n\\end_deeper ";
137 // First write the layout
138 os << "\n\\begin_layout " << layout()->name() << '\n';
142 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
144 Change running_change = Change(Change::UNCHANGED);
145 lyx::time_type const curtime(lyx::current_time());
148 for (pos_type i = 0; i < size(); ++i) {
154 Change change = pimpl_->lookupChangeFull(i);
155 Changes::lyxMarkChange(os, column, curtime, running_change, change);
156 running_change = change;
158 // Write font changes
159 LyXFont font2 = getFontSettings(bparams, i);
160 if (font2 != font1) {
161 font2.lyxWriteChanges(font1, os);
166 value_type const c = getChar(i);
170 InsetOld const * inset = getInset(i);
172 if (inset->directWrite()) {
173 // international char, let it write
174 // code directly so it's shorter in
176 inset->write(buf, os);
178 os << "\n\\begin_inset ";
179 inset->write(buf, os);
180 os << "\n\\end_inset \n\n";
186 os << "\n\\backslash \n";
190 if (i + 1 < size() && getChar(i + 1) == ' ') {
197 if ((column > 70 && c == ' ')
202 // this check is to amend a bug. LyX sometimes
203 // inserts '\0' this could cause problems.
207 lyxerr << "ERROR (Paragraph::writeFile):"
208 " NULL char in structure." << endl;
214 // to make reading work properly
216 running_change = pimpl_->lookupChange(0);
217 Changes::lyxMarkChange(os, column, curtime,
218 Change(Change::UNCHANGED), running_change);
220 Changes::lyxMarkChange(os, column, curtime,
221 running_change, Change(Change::UNCHANGED));
223 os << "\n\\end_layout\n";
227 void Paragraph::validate(LaTeXFeatures & features) const
229 pimpl_->validate(features, *layout());
233 void Paragraph::eraseIntern(lyx::pos_type pos)
235 pimpl_->eraseIntern(pos);
239 bool Paragraph::erase(pos_type pos)
241 return pimpl_->erase(pos);
245 int Paragraph::erase(pos_type start, pos_type end)
247 return pimpl_->erase(start, end);
251 bool Paragraph::checkInsertChar(LyXFont & font)
253 if (pimpl_->inset_owner)
254 return pimpl_->inset_owner->checkInsertChar(font);
259 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
261 insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
265 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
266 LyXFont const & font, Change change)
268 pimpl_->insertChar(pos, c, font, change);
272 void Paragraph::insertInset(pos_type pos, InsetOld * inset)
274 insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
278 void Paragraph::insertInset(pos_type pos, InsetOld * inset,
279 LyXFont const & font, Change change)
281 pimpl_->insertInset(pos, inset, font, change);
285 bool Paragraph::insetAllowed(InsetOld_code code)
287 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
288 if (pimpl_->inset_owner)
289 return pimpl_->inset_owner->insetAllowed(code);
294 InsetOld * Paragraph::getInset(pos_type pos)
296 BOOST_ASSERT(pos < size());
297 return insetlist.get(pos);
301 InsetOld const * Paragraph::getInset(pos_type pos) const
303 BOOST_ASSERT(pos < size());
304 return insetlist.get(pos);
308 // Gets uninstantiated font setting at position.
309 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
312 BOOST_ASSERT(pos <= size());
314 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
315 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
316 for (; cit != end; ++cit)
317 if (cit->pos() >= pos)
323 if (pos == size() && !empty())
324 return getFontSettings(bparams, pos - 1);
326 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
330 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
332 BOOST_ASSERT(pos <= size());
334 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
335 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
336 for (; cit != end; ++cit)
337 if (cit->pos() >= pos)
340 // This should not happen, but if so, we take no chances.
341 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
347 // Gets uninstantiated font setting at position 0
348 LyXFont const Paragraph::getFirstFontSettings() const
350 if (!empty() && !pimpl_->fontlist.empty())
351 return pimpl_->fontlist[0].font();
353 return LyXFont(LyXFont::ALL_INHERIT);
357 // Gets the fully instantiated font at a given position in a paragraph
358 // This is basically the same function as LyXText::GetFont() in text2.C.
359 // The difference is that this one is used for generating the LaTeX file,
360 // and thus cosmetic "improvements" are disallowed: This has to deliver
361 // the true picture of the buffer. (Asger)
362 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
363 LyXFont const & outerfont) const
365 BOOST_ASSERT(pos >= 0);
367 LyXLayout_ptr const & lout = layout();
369 pos_type const body_pos = beginningOfBody();
373 layoutfont = lout->labelfont;
375 layoutfont = lout->font;
377 LyXFont font = getFontSettings(bparams, pos);
378 font.realize(layoutfont);
379 font.realize(outerfont);
380 font.realize(bparams.getLyXTextClass().defaultfont());
386 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
387 LyXFont const & outerfont) const
389 LyXFont tmpfont = layout()->labelfont;
390 tmpfont.setLanguage(getParLanguage(bparams));
391 tmpfont.realize(outerfont);
392 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
397 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
398 LyXFont const & outerfont) const
400 LyXFont tmpfont = layout()->font;
401 tmpfont.setLanguage(getParLanguage(bparams));
402 tmpfont.realize(outerfont);
403 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
408 /// Returns the height of the highest font in range
410 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
411 LyXFont_size def_size) const
413 if (pimpl_->fontlist.empty())
416 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
417 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
418 for (; end_it != end; ++end_it) {
419 if (end_it->pos() >= endpos)
426 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
427 for (; cit != end; ++cit) {
428 if (cit->pos() >= startpos)
432 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
433 for (; cit != end_it; ++cit) {
434 LyXFont::FONT_SIZE size = cit->font().size();
435 if (size == LyXFont::INHERIT_SIZE)
437 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
444 Paragraph::value_type
445 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
447 value_type c = getChar(pos);
448 if (!lyxrc.rtl_support)
478 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
485 void Paragraph::setFont(pos_type pos, LyXFont const & font)
487 BOOST_ASSERT(pos <= size());
489 // First, reduce font against layout/label font
490 // Update: The setCharFont() routine in text2.C already
491 // reduces font, so we don't need to do that here. (Asger)
492 // No need to simplify this because it will disappear
493 // in a new kernel. (Asger)
494 // Next search font table
496 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
497 Pimpl::FontList::iterator it = beg;
498 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
499 for (; it != endit; ++it) {
500 if (it->pos() >= pos)
503 unsigned int i = std::distance(beg, it);
504 bool notfound = (it == endit);
506 if (!notfound && pimpl_->fontlist[i].font() == font)
509 bool begin = pos == 0 || notfound ||
510 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
511 // Is position pos is a beginning of a font block?
512 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
513 // Is position pos is the end of a font block?
514 if (begin && end) { // A single char block
515 if (i + 1 < pimpl_->fontlist.size() &&
516 pimpl_->fontlist[i + 1].font() == font) {
517 // Merge the singleton block with the next block
518 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
519 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
520 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
521 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
522 // Merge the singleton block with the previous block
523 pimpl_->fontlist[i - 1].pos(pos);
524 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
526 pimpl_->fontlist[i].font(font);
528 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
529 pimpl_->fontlist[i - 1].pos(pos);
531 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
532 Pimpl::FontTable(pos, font));
534 pimpl_->fontlist[i].pos(pos - 1);
535 if (!(i + 1 < pimpl_->fontlist.size() &&
536 pimpl_->fontlist[i + 1].font() == font))
537 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
538 Pimpl::FontTable(pos, font));
539 } else { // The general case. The block is splitted into 3 blocks
540 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
541 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
542 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
543 Pimpl::FontTable(pos, font));
548 void Paragraph::makeSameLayout(Paragraph const & par)
550 layout(par.layout());
552 params() = par.params();
556 int Paragraph::stripLeadingSpaces()
562 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
563 pimpl_->eraseIntern(0);
571 bool Paragraph::hasSameLayout(Paragraph const & par) const
574 par.layout() == layout() &&
575 params().sameLayout(par.params());
579 Paragraph::depth_type Paragraph::getDepth() const
581 return params().depth();
585 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
587 if (layout()->isEnvironment())
588 return params().depth() + 1;
590 return params().depth();
594 char Paragraph::getAlign() const
596 return params().align();
600 string const & Paragraph::getLabelstring() const
602 return params().labelString();
606 // the next two functions are for the manual labels
607 string const Paragraph::getLabelWidthString() const
609 if (!params().labelWidthString().empty())
610 return params().labelWidthString();
612 return _("Senseless with this layout!");
616 void Paragraph::setLabelWidthString(string const & s)
618 params().labelWidthString(s);
622 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
625 params().labelWidthString(string());
626 params().align(LYX_ALIGN_LAYOUT);
627 params().spaceTop(VSpace(VSpace::NONE));
628 params().spaceBottom(VSpace(VSpace::NONE));
629 params().spacing(Spacing(Spacing::Default));
633 int Paragraph::beginningOfBody() const
635 if (layout()->labeltype != LABEL_MANUAL)
638 // Unroll the first two cycles of the loop
639 // and remember the previous character to
640 // remove unnecessary getChar() calls
642 if (i < size() && !isNewline(i)) {
644 char previous_char = 0;
647 previous_char = getChar(i);
650 while (i < size() && previous_char != ' ') {
656 previous_char = temp;
666 // returns -1 if inset not found
667 int Paragraph::getPositionOfInset(InsetOld const * inset) const
670 InsetList::const_iterator it = insetlist.begin();
671 InsetList::const_iterator end = insetlist.end();
672 for (; it != end; ++it)
673 if (it->inset == inset)
679 InsetBibitem * Paragraph::bibitem() const
681 InsetList::const_iterator it = insetlist.begin();
682 if (it != insetlist.end() && it->inset->lyxCode() == InsetOld::BIBTEX_CODE)
683 return static_cast<InsetBibitem *>(it->inset);
690 /* paragraphs inside floats need different alignment tags to avoid
693 bool noTrivlistCentering(UpdatableInset const * inset)
695 if (inset && inset->owner()) {
696 InsetOld::Code const code = inset->owner()->lyxCode();
697 return code == InsetOld::FLOAT_CODE ||
698 code == InsetOld::WRAP_CODE;
704 string correction(string const & orig)
706 if (orig == "flushleft")
707 return "raggedright";
708 if (orig == "flushright")
710 if (orig == "center")
716 string const corrected_env(string const & suffix, string const & env,
717 UpdatableInset const * inset)
719 string output = suffix + "{";
720 if (noTrivlistCentering(inset))
721 output += correction(env);
730 // This could go to ParagraphParameters if we want to
731 int Paragraph::startTeXParParams(BufferParams const & bparams,
732 ostream & os, bool moving_arg) const
736 if (params().noindent()) {
741 switch (params().align()) {
743 case LYX_ALIGN_BLOCK:
744 case LYX_ALIGN_LAYOUT:
745 case LYX_ALIGN_SPECIAL:
748 case LYX_ALIGN_RIGHT:
749 case LYX_ALIGN_CENTER:
757 switch (params().align()) {
759 case LYX_ALIGN_BLOCK:
760 case LYX_ALIGN_LAYOUT:
761 case LYX_ALIGN_SPECIAL:
763 case LYX_ALIGN_LEFT: {
765 UpdatableInset const * const inset = pimpl_->inset_owner;
766 if (getParLanguage(bparams)->babel() != "hebrew")
767 output = corrected_env("\\begin", "flushleft", inset);
769 output = corrected_env("\\begin", "flushright", inset);
771 column += output.size();
773 } case LYX_ALIGN_RIGHT: {
775 UpdatableInset const * const inset = pimpl_->inset_owner;
776 if (getParLanguage(bparams)->babel() != "hebrew")
777 output = corrected_env("\\begin", "flushright", inset);
779 output = corrected_env("\\begin", "flushleft", inset);
781 column += output.size();
783 } case LYX_ALIGN_CENTER: {
785 output = corrected_env("\\begin", "center", pimpl_->inset_owner);
787 column += output.size();
796 // This could go to ParagraphParameters if we want to
797 int Paragraph::endTeXParParams(BufferParams const & bparams,
798 ostream & os, bool moving_arg) const
802 switch (params().align()) {
804 case LYX_ALIGN_BLOCK:
805 case LYX_ALIGN_LAYOUT:
806 case LYX_ALIGN_SPECIAL:
809 case LYX_ALIGN_RIGHT:
810 case LYX_ALIGN_CENTER:
818 switch (params().align()) {
820 case LYX_ALIGN_BLOCK:
821 case LYX_ALIGN_LAYOUT:
822 case LYX_ALIGN_SPECIAL:
824 case LYX_ALIGN_LEFT: {
826 UpdatableInset const * const inset = pimpl_->inset_owner;
827 if (getParLanguage(bparams)->babel() != "hebrew")
828 output = corrected_env("\\par\\end", "flushleft", inset);
830 output = corrected_env("\\par\\end", "flushright", inset);
832 column += output.size();
834 } case LYX_ALIGN_RIGHT: {
836 UpdatableInset const * const inset = pimpl_->inset_owner;
837 if (getParLanguage(bparams)->babel() != "hebrew")
838 output = corrected_env("\\par\\end", "flushright", inset);
840 output = corrected_env("\\par\\end", "flushleft", inset);
842 column += output.size();
844 } case LYX_ALIGN_CENTER: {
846 output = corrected_env("\\par\\end", "center", pimpl_->inset_owner);
848 column += output.size();
857 // This one spits out the text of the paragraph
858 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
859 BufferParams const & bparams,
860 LyXFont const & outerfont,
861 ostream & os, TexRow & texrow,
862 LatexRunParams const & runparams)
864 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
866 bool return_value = false;
870 // well we have to check if we are in an inset with unlimited
871 // length (all in one row) if that is true then we don't allow
872 // any special options in the paragraph and also we don't allow
873 // any environment other then "Standard" to be valid!
875 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
878 style = bparams.getLyXTextClass().defaultLayout();
885 // Maybe we have to create a optional argument.
888 // FIXME: can we actually skip this check and just call
889 // beginningOfBody() ??
890 if (style->labeltype != LABEL_MANUAL) {
893 body_pos = beginningOfBody();
896 unsigned int column = 0;
901 basefont = getLabelFont(bparams, outerfont);
903 basefont = getLayoutFont(bparams, outerfont);
906 bool moving_arg = runparams.moving_arg;
907 moving_arg |= style->needprotect;
909 // Which font is currently active?
910 LyXFont running_font(basefont);
911 // Do we have an open font change?
912 bool open_font = false;
914 Change::Type running_change = Change::UNCHANGED;
916 texrow.start(id(), 0);
918 // if the paragraph is empty, the loop will not be entered at all
920 if (style->isCommand()) {
925 column += startTeXParParams(bparams, os, moving_arg);
929 for (pos_type i = 0; i < size(); ++i) {
931 // First char in paragraph or after label?
935 column += running_font.latexWriteEndChanges(os, basefont, basefont);
938 basefont = getLayoutFont(bparams, outerfont);
939 running_font = basefont;
943 if (style->isCommand()) {
949 column += startTeXParParams(bparams, os,
953 value_type c = getChar(i);
955 // Fully instantiated font
956 LyXFont font = getFont(bparams, i, outerfont);
958 LyXFont const last_font = running_font;
960 // Spaces at end of font change are simulated to be
961 // outside font change, i.e. we write "\textXX{text} "
962 // rather than "\textXX{text }". (Asger)
963 if (open_font && c == ' ' && i <= size() - 2) {
964 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
965 if (next_font != running_font
966 && next_font != font) {
971 // We end font definition before blanks
973 (font != running_font ||
974 font.language() != running_font.language()))
976 column += running_font.latexWriteEndChanges(os,
978 (i == body_pos-1) ? basefont : font);
979 running_font = basefont;
983 // Blanks are printed before start of fontswitch
985 // Do not print the separation of the optional argument
986 if (i != body_pos - 1) {
987 pimpl_->simpleTeXBlanks(os, texrow, i,
988 column, font, *style);
992 // Do we need to change font?
993 if ((font != running_font ||
994 font.language() != running_font.language()) &&
997 column += font.latexWriteStartChanges(os, basefont,
1003 Change::Type change = pimpl_->lookupChange(i);
1005 column += Changes::latexMarkChange(os, running_change, change);
1006 running_change = change;
1008 LatexRunParams rp = runparams;
1009 rp.moving_arg = moving_arg;
1010 rp.free_spacing = style->free_spacing;
1011 pimpl_->simpleTeXSpecialChars(buf, bparams,
1012 os, texrow, runparams,
1014 basefont, outerfont, open_font,
1016 *style, i, column, c);
1019 column += Changes::latexMarkChange(os,
1020 running_change, Change::UNCHANGED);
1022 // If we have an open font definition, we have to close it
1024 #ifdef FIXED_LANGUAGE_END_DETECTION
1027 .latexWriteEndChanges(os, basefont,
1028 next_->getFont(bparams,
1031 running_font.latexWriteEndChanges(os, basefont,
1035 #ifdef WITH_WARNINGS
1036 //#warning For now we ALWAYS have to close the foreign font settings if they are
1037 //#warning there as we start another \selectlanguage with the next paragraph if
1038 //#warning we are in need of this. This should be fixed sometime (Jug)
1040 running_font.latexWriteEndChanges(os, basefont, basefont);
1044 // Needed if there is an optional argument but no contents.
1045 if (body_pos > 0 && body_pos == size()) {
1047 return_value = false;
1051 column += endTeXParParams(bparams, os, moving_arg);
1054 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1055 return return_value;
1061 /// return true if the char is a meta-character for an inset
1063 bool IsInsetChar(char c)
1065 return (c == Paragraph::META_INSET);
1072 bool Paragraph::isHfill(pos_type pos) const
1074 return IsInsetChar(getChar(pos))
1075 && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
1079 bool Paragraph::isInset(pos_type pos) const
1081 return IsInsetChar(getChar(pos));
1085 bool Paragraph::isNewline(pos_type pos) const
1087 return IsInsetChar(getChar(pos))
1088 && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
1092 bool Paragraph::isSeparator(pos_type pos) const
1094 return IsSeparatorChar(getChar(pos));
1098 bool Paragraph::isLineSeparator(pos_type pos) const
1100 value_type const c = getChar(pos);
1101 return IsLineSeparatorChar(c)
1102 || (IsInsetChar(c) && getInset(pos) &&
1103 getInset(pos)->isLineSeparator());
1107 bool Paragraph::isKomma(pos_type pos) const
1109 return IsKommaChar(getChar(pos));
1113 /// Used by the spellchecker
1114 bool Paragraph::isLetter(pos_type pos) const
1116 value_type const c = getChar(pos);
1117 if (IsLetterChar(c))
1120 return getInset(pos)->isLetter();
1121 // We want to pass the ' and escape chars to ispell
1122 string const extra = lyxrc.isp_esc_chars + '\'';
1123 return contains(extra, c);
1127 bool Paragraph::isWord(pos_type pos) const
1129 unsigned char const c = getChar(pos);
1130 return !(IsSeparatorChar(c)
1137 Paragraph::getParLanguage(BufferParams const & bparams) const
1140 return getFirstFontSettings().language();
1141 #warning FIXME we should check the prev par as well (Lgb)
1142 return bparams.language;
1146 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1148 return lyxrc.rtl_support
1149 && getParLanguage(bparams)->RightToLeft()
1150 && !(inInset() && inInset()->owner() &&
1151 inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
1155 void Paragraph::changeLanguage(BufferParams const & bparams,
1156 Language const * from, Language const * to)
1158 for (pos_type i = 0; i < size(); ++i) {
1159 LyXFont font = getFontSettings(bparams, i);
1160 if (font.language() == from) {
1161 font.setLanguage(to);
1168 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1170 Language const * doc_language = bparams.language;
1171 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1172 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1174 for (; cit != end; ++cit)
1175 if (cit->font().language() != ignore_language &&
1176 cit->font().language() != latex_language &&
1177 cit->font().language() != doc_language)
1183 // Convert the paragraph to a string.
1184 // Used for building the table of contents
1185 string const Paragraph::asString(Buffer const & buffer, bool label) const
1189 if (label && !params().labelString().empty())
1190 s += params().labelString() + ' ';
1192 for (pos_type i = 0; i < size(); ++i) {
1193 value_type c = getChar(i);
1196 else if (c == META_INSET &&
1197 getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
1199 getInset(i)->ascii(buffer, os);
1200 s += subst(STRCONV(os.str()),'\n',' ');
1206 // This should really be done by the caller and not here.
1207 string ret = asString(buffer, 0, size(), label);
1208 return subst(ret, '\n', ' ');
1213 string const Paragraph::asString(Buffer const & buffer,
1214 pos_type beg, pos_type end, bool label) const
1218 if (beg == 0 && label && !params().labelString().empty())
1219 os << params().labelString() << ' ';
1221 for (pos_type i = beg; i < end; ++i) {
1222 value_type const c = getUChar(buffer.params(), i);
1225 else if (c == META_INSET)
1226 getInset(i)->ascii(buffer, os);
1233 void Paragraph::setInsetOwner(UpdatableInset * inset)
1235 pimpl_->inset_owner = inset;
1236 InsetList::iterator it = insetlist.begin();
1237 InsetList::iterator end = insetlist.end();
1238 for (; it != end; ++it)
1240 it->inset->setOwner(inset);
1244 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1246 insetlist.deleteInsetsLyXText(bv);
1250 void Paragraph::setContentsFromPar(Paragraph const & par)
1252 pimpl_->setContentsFromPar(par);
1256 void Paragraph::trackChanges(Change::Type type)
1258 pimpl_->trackChanges(type);
1262 void Paragraph::untrackChanges()
1264 pimpl_->untrackChanges();
1268 void Paragraph::cleanChanges()
1270 pimpl_->cleanChanges();
1274 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1276 BOOST_ASSERT(!size() || pos < size());
1277 return pimpl_->lookupChange(pos);
1281 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1283 BOOST_ASSERT(!size() || pos < size());
1284 return pimpl_->lookupChangeFull(pos);
1288 bool Paragraph::isChanged(pos_type start, pos_type end) const
1290 return pimpl_->isChanged(start, end);
1294 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1296 return pimpl_->isChangeEdited(start, end);
1300 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1302 pimpl_->setChange(pos, type);
1307 void Paragraph::markErased()
1309 pimpl_->markErased();
1313 void Paragraph::acceptChange(pos_type start, pos_type end)
1315 return pimpl_->acceptChange(start, end);
1319 void Paragraph::rejectChange(pos_type start, pos_type end)
1321 return pimpl_->rejectChange(start, end);
1325 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1327 // This is in the critical path!
1328 pos_type const siz = text_.size();
1330 BOOST_ASSERT(0 <= pos);
1331 BOOST_ASSERT(pos <= siz);
1334 lyxerr << "getChar() on pos " << pos << " in par id "
1335 << id() << " of size " << siz
1336 << " is a bit silly !" << endl;
1337 BOOST_ASSERT(false);
1345 int Paragraph::id() const
1351 LyXLayout_ptr const & Paragraph::layout() const
1354 InsetOld * inset = inInset();
1355 if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
1356 return static_cast<InsetEnvironment*>(inset)->layout();
1362 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1364 layout_ = new_layout;
1368 UpdatableInset * Paragraph::inInset() const
1370 return pimpl_->inset_owner;
1374 void Paragraph::clearContents()
1380 void Paragraph::setChar(pos_type pos, value_type c)
1386 ParagraphParameters & Paragraph::params()
1388 return pimpl_->params;
1392 ParagraphParameters const & Paragraph::params() const
1394 return pimpl_->params;
1398 bool Paragraph::isFreeSpacing() const
1400 if (layout()->free_spacing)
1403 // for now we just need this, later should we need this in some
1404 // other way we can always add a function to InsetOld too.
1405 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1406 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1411 bool Paragraph::allowEmpty() const
1413 if (layout()->keepempty)
1415 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1416 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1421 RowList::iterator Paragraph::getRow(pos_type pos)
1423 RowList::iterator rit = rows.end();
1424 RowList::iterator const begin = rows.begin();
1426 for (--rit; rit != begin && rit->pos() > pos; --rit)
1433 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1435 if (!Encodings::is_arabic(c))
1436 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1437 return c + (0xb0 - '0');
1441 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1442 unsigned char next_char = ' ';
1444 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1445 unsigned char const par_char = getChar(i);
1446 if (!Encodings::IsComposeChar_arabic(par_char)) {
1447 next_char = par_char;
1452 if (Encodings::is_arabic(next_char)) {
1453 if (Encodings::is_arabic(prev_char) &&
1454 !Encodings::is_arabic_special(prev_char))
1455 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1457 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1459 if (Encodings::is_arabic(prev_char) &&
1460 !Encodings::is_arabic_special(prev_char))
1461 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1463 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);