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"
30 #include "LaTeXFeatures.h"
34 #include "outputparams.h"
35 #include "paragraph_funcs.h"
36 #include "ParagraphList_fwd.h"
41 #include "insets/insetbibitem.h"
42 #include "insets/insetoptarg.h"
44 #include "support/lstrings.h"
45 #include "support/textutils.h"
46 #include "support/convert.h"
47 #include "support/unicode.h"
49 #include <boost/tuple/tuple.hpp>
50 #include <boost/bind.hpp>
60 using lyx::support::subst;
68 using std::ostringstream;
71 Paragraph::Paragraph()
72 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
79 Paragraph::Paragraph(Paragraph const & par)
80 : itemdepth(par.itemdepth), insetlist(par.insetlist),
82 rows_(par.rows_), rowSignature_(par.rowSignature_),
84 text_(par.text_), begin_of_body_(par.begin_of_body_),
85 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
87 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
88 InsetList::iterator it = insetlist.begin();
89 InsetList::iterator end = insetlist.end();
90 for (; it != end; ++it)
91 it->inset = it->inset->clone().release();
95 Paragraph & Paragraph::operator=(Paragraph const & par)
97 // needed as we will destroy the pimpl_ before copying it
99 itemdepth = par.itemdepth;
101 insetlist = par.insetlist;
102 InsetList::iterator it = insetlist.begin();
103 InsetList::iterator end = insetlist.end();
104 for (; it != end; ++it)
105 it->inset = it->inset->clone().release();
109 rowSignature_ = par.rowSignature_;
110 layout_ = par.layout();
112 begin_of_body_ = par.begin_of_body_;
115 pimpl_ = new Pimpl(*par.pimpl_, this);
121 Paragraph::~Paragraph()
125 //lyxerr << "Paragraph::paragraph_id = "
126 // << Paragraph::paragraph_id << endl;
130 void Paragraph::write(Buffer const & buf, ostream & os,
131 BufferParams const & bparams,
132 depth_type & dth) const
134 // The beginning or end of a deeper (i.e. nested) area?
135 if (dth != params().depth()) {
136 if (params().depth() > dth) {
137 while (params().depth() > dth) {
138 os << "\n\\begin_deeper";
142 while (params().depth() < dth) {
143 os << "\n\\end_deeper";
149 // First write the layout
150 os << "\n\\begin_layout " << layout()->name() << '\n';
154 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
156 Change running_change = Change(Change::UNCHANGED);
157 lyx::time_type const curtime(lyx::current_time());
160 for (pos_type i = 0; i <= size(); ++i) {
162 Change change = pimpl_->lookupChange(i);
163 Changes::lyxMarkChange(os, column, curtime, running_change, change);
164 running_change = change;
169 // Write font changes
170 LyXFont font2 = getFontSettings(bparams, i);
171 if (font2 != font1) {
172 font2.lyxWriteChanges(font1, os);
177 value_type const c = getChar(i);
181 InsetBase const * inset = getInset(i);
183 if (inset->directWrite()) {
184 // international char, let it write
185 // code directly so it's shorter in
187 inset->write(buf, os);
191 os << "\\begin_inset ";
192 inset->write(buf, os);
193 os << "\n\\end_inset\n\n";
199 os << "\n\\backslash\n";
203 if (i + 1 < size() && getChar(i + 1) == ' ') {
210 if ((column > 70 && c == ' ')
215 // this check is to amend a bug. LyX sometimes
216 // inserts '\0' this could cause problems.
218 std::vector<char> tmp = ucs4_to_utf8(c);
222 lyxerr << "ERROR (Paragraph::writeFile):"
223 " NULL char in structure." << endl;
229 os << "\n\\end_layout\n";
233 void Paragraph::validate(LaTeXFeatures & features) const
235 pimpl_->validate(features, *layout());
239 void Paragraph::eraseIntern(lyx::pos_type pos)
241 pimpl_->eraseIntern(pos);
245 bool Paragraph::erase(pos_type pos)
247 return pimpl_->erase(pos);
251 int Paragraph::erase(pos_type start, pos_type end)
253 return pimpl_->erase(start, end);
257 void Paragraph::insert(pos_type start, string const & str,
258 LyXFont const & font)
260 for (size_t i = 0, n = str.size(); i != n ; ++i)
261 insertChar(start + i, str[i], font);
265 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
268 pimpl_->insertChar(pos, c, change);
272 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
273 LyXFont const & font, Change change)
275 pimpl_->insertChar(pos, c, change);
280 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
282 pimpl_->insertInset(pos, inset, change);
286 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
287 LyXFont const & font, Change change)
289 pimpl_->insertInset(pos, inset, change);
294 bool Paragraph::insetAllowed(InsetBase_code code)
296 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
300 // Gets uninstantiated font setting at position.
301 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
305 lyxerr << " pos: " << pos << " size: " << size() << endl;
306 BOOST_ASSERT(pos <= size());
309 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
310 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
311 for (; cit != end; ++cit)
312 if (cit->pos() >= pos)
318 if (pos == size() && !empty())
319 return getFontSettings(bparams, pos - 1);
321 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
325 FontSpan Paragraph::fontSpan(lyx::pos_type pos) const
327 BOOST_ASSERT(pos <= size());
328 lyx::pos_type start = 0;
330 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
331 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
332 for (; cit != end; ++cit) {
333 if (cit->pos() >= pos) {
334 if (pos >= beginOfBody())
335 return FontSpan(std::max(start, beginOfBody()),
338 return FontSpan(start,
339 std::min(beginOfBody() - 1,
342 start = cit->pos() + 1;
345 // This should not happen, but if so, we take no chances.
346 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
348 return FontSpan(pos, pos);
352 // Gets uninstantiated font setting at position 0
353 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
355 if (!empty() && !pimpl_->fontlist.empty())
356 return pimpl_->fontlist[0].font();
358 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
362 // Gets the fully instantiated font at a given position in a paragraph
363 // This is basically the same function as LyXText::GetFont() in text2.C.
364 // The difference is that this one is used for generating the LaTeX file,
365 // and thus cosmetic "improvements" are disallowed: This has to deliver
366 // the true picture of the buffer. (Asger)
367 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
368 LyXFont const & outerfont) const
370 BOOST_ASSERT(pos >= 0);
372 LyXLayout_ptr const & lout = layout();
374 pos_type const body_pos = beginOfBody();
378 layoutfont = lout->labelfont;
380 layoutfont = lout->font;
382 LyXFont font = getFontSettings(bparams, pos);
383 font.realize(layoutfont);
384 font.realize(outerfont);
385 font.realize(bparams.getFont());
391 LyXFont const Paragraph::getLabelFont
392 (BufferParams const & bparams, LyXFont const & outerfont) const
394 LyXFont tmpfont = layout()->labelfont;
395 tmpfont.setLanguage(getParLanguage(bparams));
396 tmpfont.realize(outerfont);
397 tmpfont.realize(bparams.getFont());
402 LyXFont const Paragraph::getLayoutFont
403 (BufferParams const & bparams, LyXFont const & outerfont) const
405 LyXFont tmpfont = layout()->font;
406 tmpfont.setLanguage(getParLanguage(bparams));
407 tmpfont.realize(outerfont);
408 tmpfont.realize(bparams.getFont());
413 /// Returns the height of the highest font in range
414 LyXFont_size Paragraph::highestFontInRange
415 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
417 if (pimpl_->fontlist.empty())
420 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
421 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
422 for (; end_it != end; ++end_it) {
423 if (end_it->pos() >= endpos)
430 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
431 for (; cit != end; ++cit) {
432 if (cit->pos() >= startpos)
436 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
437 for (; cit != end_it; ++cit) {
438 LyXFont::FONT_SIZE size = cit->font().size();
439 if (size == LyXFont::INHERIT_SIZE)
441 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
448 Paragraph::value_type
449 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
451 value_type c = getChar(pos);
452 if (!lyxrc.rtl_support)
482 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
489 void Paragraph::setFont(pos_type pos, LyXFont const & font)
491 BOOST_ASSERT(pos <= size());
493 // First, reduce font against layout/label font
494 // Update: The setCharFont() routine in text2.C already
495 // reduces font, so we don't need to do that here. (Asger)
496 // No need to simplify this because it will disappear
497 // in a new kernel. (Asger)
498 // Next search font table
500 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
501 Pimpl::FontList::iterator it = beg;
502 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
503 for (; it != endit; ++it) {
504 if (it->pos() >= pos)
507 size_t const i = distance(beg, it);
508 bool notfound = (it == endit);
510 if (!notfound && pimpl_->fontlist[i].font() == font)
513 bool begin = pos == 0 || notfound ||
514 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
515 // Is position pos is a beginning of a font block?
516 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
517 // Is position pos is the end of a font block?
518 if (begin && end) { // A single char block
519 if (i + 1 < pimpl_->fontlist.size() &&
520 pimpl_->fontlist[i + 1].font() == font) {
521 // Merge the singleton block with the next block
522 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
523 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
524 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
525 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
526 // Merge the singleton block with the previous block
527 pimpl_->fontlist[i - 1].pos(pos);
528 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
530 pimpl_->fontlist[i].font(font);
532 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
533 pimpl_->fontlist[i - 1].pos(pos);
535 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
536 Pimpl::FontTable(pos, font));
538 pimpl_->fontlist[i].pos(pos - 1);
539 if (!(i + 1 < pimpl_->fontlist.size() &&
540 pimpl_->fontlist[i + 1].font() == font))
541 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
542 Pimpl::FontTable(pos, font));
543 } else { // The general case. The block is splitted into 3 blocks
544 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
545 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
546 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
547 Pimpl::FontTable(pos, font));
552 void Paragraph::makeSameLayout(Paragraph const & par)
554 layout(par.layout());
556 params() = par.params();
560 int Paragraph::stripLeadingSpaces()
566 while (!empty() && (isNewline(0) || isLineSeparator(0))
567 && (lookupChange(0).type != Change::DELETED)) {
576 bool Paragraph::hasSameLayout(Paragraph const & par) const
578 return par.layout() == layout() && params().sameLayout(par.params());
582 Paragraph::depth_type Paragraph::getDepth() const
584 return params().depth();
588 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
590 if (layout()->isEnvironment())
591 return params().depth() + 1;
593 return params().depth();
597 char Paragraph::getAlign() const
599 return params().align();
603 string const & Paragraph::getLabelstring() const
605 return params().labelString();
609 // the next two functions are for the manual labels
610 string const Paragraph::getLabelWidthString() const
612 if (!params().labelWidthString().empty())
613 return params().labelWidthString();
615 return lyx::to_utf8(_("Senseless with this layout!"));
619 void Paragraph::setLabelWidthString(string const & s)
621 params().labelWidthString(s);
625 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
628 params().labelWidthString(string());
629 params().align(LYX_ALIGN_LAYOUT);
630 params().spacing(Spacing(Spacing::Default));
634 pos_type Paragraph::beginOfBody() const
636 return begin_of_body_;
640 void Paragraph::setBeginOfBody()
642 if (layout()->labeltype != LABEL_MANUAL) {
647 // Unroll the first two cycles of the loop
648 // and remember the previous character to
649 // remove unnecessary getChar() calls
651 pos_type end = size();
652 if (i < end && !isNewline(i)) {
654 char previous_char = 0;
657 previous_char = text_[i];
660 while (i < end && previous_char != ' ') {
665 previous_char = temp;
675 // returns -1 if inset not found
676 int Paragraph::getPositionOfInset(InsetBase const * inset) const
679 InsetList::const_iterator it = insetlist.begin();
680 InsetList::const_iterator end = insetlist.end();
681 for (; it != end; ++it)
682 if (it->inset == inset)
688 InsetBibitem * Paragraph::bibitem() const
690 if (!insetlist.empty()) {
691 InsetBase * inset = insetlist.begin()->inset;
692 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
693 return static_cast<InsetBibitem *>(inset);
699 bool Paragraph::forceDefaultParagraphs() const
701 return inInset() && inInset()->forceDefaultParagraphs(0);
707 // paragraphs inside floats need different alignment tags to avoid
710 bool noTrivlistCentering(InsetBase::Code code)
712 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
716 string correction(string const & orig)
718 if (orig == "flushleft")
719 return "raggedright";
720 if (orig == "flushright")
722 if (orig == "center")
728 string const corrected_env(string const & suffix, string const & env,
729 InsetBase::Code code)
731 string output = suffix + "{";
732 if (noTrivlistCentering(code))
733 output += correction(env);
742 // This could go to ParagraphParameters if we want to
743 int Paragraph::startTeXParParams(BufferParams const & bparams,
744 ostream & os, bool moving_arg) const
748 if (params().noindent()) {
753 switch (params().align()) {
755 case LYX_ALIGN_BLOCK:
756 case LYX_ALIGN_LAYOUT:
757 case LYX_ALIGN_SPECIAL:
760 case LYX_ALIGN_RIGHT:
761 case LYX_ALIGN_CENTER:
769 switch (params().align()) {
771 case LYX_ALIGN_BLOCK:
772 case LYX_ALIGN_LAYOUT:
773 case LYX_ALIGN_SPECIAL:
775 case LYX_ALIGN_LEFT: {
777 if (getParLanguage(bparams)->babel() != "hebrew")
778 output = corrected_env("\\begin", "flushleft", ownerCode());
780 output = corrected_env("\\begin", "flushright", ownerCode());
782 column += output.size();
784 } case LYX_ALIGN_RIGHT: {
786 if (getParLanguage(bparams)->babel() != "hebrew")
787 output = corrected_env("\\begin", "flushright", ownerCode());
789 output = corrected_env("\\begin", "flushleft", ownerCode());
791 column += output.size();
793 } case LYX_ALIGN_CENTER: {
795 output = corrected_env("\\begin", "center", ownerCode());
797 column += output.size();
806 // This could go to ParagraphParameters if we want to
807 int Paragraph::endTeXParParams(BufferParams const & bparams,
808 ostream & os, bool moving_arg) const
812 switch (params().align()) {
814 case LYX_ALIGN_BLOCK:
815 case LYX_ALIGN_LAYOUT:
816 case LYX_ALIGN_SPECIAL:
819 case LYX_ALIGN_RIGHT:
820 case LYX_ALIGN_CENTER:
828 switch (params().align()) {
830 case LYX_ALIGN_BLOCK:
831 case LYX_ALIGN_LAYOUT:
832 case LYX_ALIGN_SPECIAL:
834 case LYX_ALIGN_LEFT: {
836 if (getParLanguage(bparams)->babel() != "hebrew")
837 output = corrected_env("\\par\\end", "flushleft", ownerCode());
839 output = corrected_env("\\par\\end", "flushright", ownerCode());
841 column += output.size();
843 } case LYX_ALIGN_RIGHT: {
845 if (getParLanguage(bparams)->babel() != "hebrew")
846 output = corrected_env("\\par\\end", "flushright", ownerCode());
848 output = corrected_env("\\par\\end", "flushleft", ownerCode());
850 column += output.size();
852 } case LYX_ALIGN_CENTER: {
854 output = corrected_env("\\par\\end", "center", ownerCode());
856 column += output.size();
865 // This one spits out the text of the paragraph
866 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
867 BufferParams const & bparams,
868 LyXFont const & outerfont,
869 ostream & os, TexRow & texrow,
870 OutputParams const & runparams) const
872 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
874 bool return_value = false;
878 // well we have to check if we are in an inset with unlimited
879 // length (all in one row) if that is true then we don't allow
880 // any special options in the paragraph and also we don't allow
881 // any environment other then "Standard" to be valid!
882 bool asdefault = forceDefaultParagraphs();
885 style = bparams.getLyXTextClass().defaultLayout();
892 LaTeXFeatures features(buf, bparams, runparams);
894 // output change tracking marks only if desired,
895 // if dvipost is installed,
896 // and with dvi/ps (other formats don't work)
897 bool const output = bparams.outputChanges
898 && runparams.flavor == OutputParams::LATEX
899 && features.isAvailable("dvipost");
901 // Maybe we have to create a optional argument.
902 pos_type body_pos = beginOfBody();
903 unsigned int column = 0;
906 // the optional argument is kept in curly brackets in
907 // case it contains a ']'
910 basefont = getLabelFont(bparams, outerfont);
912 basefont = getLayoutFont(bparams, outerfont);
915 // Which font is currently active?
916 LyXFont running_font(basefont);
917 // Do we have an open font change?
918 bool open_font = false;
920 Change::Type running_change = Change::UNCHANGED;
922 texrow.start(id(), 0);
924 // if the paragraph is empty, the loop will not be entered at all
926 if (style->isCommand()) {
931 column += startTeXParParams(bparams, os,
932 runparams.moving_arg);
935 for (pos_type i = 0; i < size(); ++i) {
937 // First char in paragraph or after label?
941 column += running_font.latexWriteEndChanges(os, basefont, basefont);
944 basefont = getLayoutFont(bparams, outerfont);
945 running_font = basefont;
949 if (style->isCommand()) {
955 column += startTeXParParams(bparams, os,
956 runparams.moving_arg);
959 value_type c = getChar(i);
961 // Fully instantiated font
962 LyXFont font = getFont(bparams, i, outerfont);
964 LyXFont const last_font = running_font;
966 // Spaces at end of font change are simulated to be
967 // outside font change, i.e. we write "\textXX{text} "
968 // rather than "\textXX{text }". (Asger)
969 if (open_font && c == ' ' && i <= size() - 2) {
970 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
971 if (next_font != running_font && next_font != font) {
976 // We end font definition before blanks
978 (font != running_font ||
979 font.language() != running_font.language()))
981 column += running_font.latexWriteEndChanges(os,
983 (i == body_pos-1) ? basefont : font);
984 running_font = basefont;
988 // Blanks are printed before start of fontswitch
990 // Do not print the separation of the optional argument
991 if (i != body_pos - 1) {
992 pimpl_->simpleTeXBlanks(os, texrow, i,
993 column, font, *style);
997 // Do we need to change font?
998 if ((font != running_font ||
999 font.language() != running_font.language()) &&
1002 column += font.latexWriteStartChanges(os, basefont,
1004 running_font = font;
1008 Change::Type change = pimpl_->lookupChange(i).type;
1010 column += Changes::latexMarkChange(os, running_change,
1012 running_change = change;
1014 // do not output text which is marked deleted
1015 // if change tracking output is not desired
1016 if (output || running_change != Change::DELETED) {
1017 OutputParams rp = runparams;
1018 rp.free_spacing = style->free_spacing;
1019 rp.local_font = &font;
1020 rp.intitle = style->intitle;
1021 pimpl_->simpleTeXSpecialChars(buf, bparams,
1024 basefont, outerfont, open_font,
1026 *style, i, column, c);
1030 column += Changes::latexMarkChange(os,
1031 running_change, Change::UNCHANGED, output);
1033 // If we have an open font definition, we have to close it
1035 #ifdef FIXED_LANGUAGE_END_DETECTION
1038 .latexWriteEndChanges(os, basefont,
1039 next_->getFont(bparams,
1042 running_font.latexWriteEndChanges(os, basefont,
1046 #ifdef WITH_WARNINGS
1047 //#warning For now we ALWAYS have to close the foreign font settings if they are
1048 //#warning there as we start another \selectlanguage with the next paragraph if
1049 //#warning we are in need of this. This should be fixed sometime (Jug)
1051 running_font.latexWriteEndChanges(os, basefont, basefont);
1055 // Needed if there is an optional argument but no contents.
1056 if (body_pos > 0 && body_pos == size()) {
1058 return_value = false;
1062 column += endTeXParParams(bparams, os, runparams.moving_arg);
1065 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1066 return return_value;
1072 // checks, if newcol chars should be put into this line
1073 // writes newline, if necessary.
1074 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1075 string::size_type newcol)
1078 if (colcount > lyxrc.ascii_linelen) {
1080 colcount = newcol; // assume write after this call
1095 string tag_name(PAR_TAG const & pt) {
1097 case PAR_NONE: return "!-- --";
1098 case TT: return "tt";
1099 case SF: return "sf";
1100 case BF: return "bf";
1101 case IT: return "it";
1102 case SL: return "sl";
1103 case EM: return "em";
1110 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1112 p1 = static_cast<PAR_TAG>(p1 | p2);
1117 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1119 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1125 bool Paragraph::emptyTag() const
1127 for (pos_type i = 0; i < size(); ++i) {
1129 InsetBase const * inset = getInset(i);
1130 InsetBase::Code lyx_code = inset->lyxCode();
1131 if (lyx_code != InsetBase::TOC_CODE &&
1132 lyx_code != InsetBase::INCLUDE_CODE &&
1133 lyx_code != InsetBase::GRAPHICS_CODE &&
1134 lyx_code != InsetBase::ERT_CODE &&
1135 lyx_code != InsetBase::FLOAT_CODE &&
1136 lyx_code != InsetBase::TABULAR_CODE) {
1140 value_type c = getChar(i);
1141 if (c != ' ' && c != '\t')
1149 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1151 for (pos_type i = 0; i < size(); ++i) {
1153 InsetBase const * inset = getInset(i);
1154 InsetBase::Code lyx_code = inset->lyxCode();
1155 if (lyx_code == InsetBase::LABEL_CODE) {
1156 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1157 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1166 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1169 for (i = 0; i < size(); ++i) {
1171 InsetBase const * inset = getInset(i);
1172 inset->docbook(buf, os, runparams);
1174 value_type c = getChar(i);
1179 boost::tie(ws, str) = sgml::escapeChar(c);
1188 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1192 for (pos_type i = initial; i < size(); ++i) {
1193 LyXFont font = getFont(buf.params(), i, outerfont);
1196 if (i != initial && font != font_old)
1205 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1207 OutputParams const & runparams,
1208 LyXFont const & outerfont,
1209 pos_type initial) const
1211 bool emph_flag = false;
1213 LyXLayout_ptr const & style = layout();
1215 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1217 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1220 // parsing main loop
1221 for (pos_type i = initial; i < size(); ++i) {
1222 LyXFont font = getFont(buf.params(), i, outerfont);
1224 // handle <emphasis> tag
1225 if (font_old.emph() != font.emph()) {
1226 if (font.emph() == LyXFont::ON) {
1229 } else if (i != initial) {
1230 os << "</emphasis>";
1236 InsetBase const * inset = getInset(i);
1237 inset->docbook(buf, os, runparams);
1239 value_type c = getChar(i);
1242 boost::tie(ws, str) = sgml::escapeChar(c);
1244 if (style->pass_thru)
1253 os << "</emphasis>";
1256 if (style->free_spacing)
1258 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1263 bool Paragraph::isNewline(pos_type pos) const
1266 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1270 bool Paragraph::isLineSeparator(pos_type pos) const
1272 value_type const c = getChar(pos);
1273 return isLineSeparatorChar(c)
1274 || (c == Paragraph::META_INSET && getInset(pos) &&
1275 getInset(pos)->isLineSeparator());
1279 /// Used by the spellchecker
1280 bool Paragraph::isLetter(pos_type pos) const
1283 return getInset(pos)->isLetter();
1285 value_type const c = getChar(pos);
1286 return isLetterChar(c) || isDigit(c);
1292 Paragraph::getParLanguage(BufferParams const & bparams) const
1295 return getFirstFontSettings(bparams).language();
1296 #ifdef WITH_WARNINGS
1297 #warning FIXME we should check the prev par as well (Lgb)
1299 return bparams.language;
1303 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1305 return lyxrc.rtl_support
1306 && getParLanguage(bparams)->rightToLeft()
1307 && ownerCode() != InsetBase::ERT_CODE;
1311 void Paragraph::changeLanguage(BufferParams const & bparams,
1312 Language const * from, Language const * to)
1314 for (pos_type i = 0; i < size(); ++i) {
1315 LyXFont font = getFontSettings(bparams, i);
1316 if (font.language() == from) {
1317 font.setLanguage(to);
1324 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1326 Language const * doc_language = bparams.language;
1327 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1328 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1330 for (; cit != end; ++cit)
1331 if (cit->font().language() != ignore_language &&
1332 cit->font().language() != latex_language &&
1333 cit->font().language() != doc_language)
1339 // Convert the paragraph to a string.
1340 // Used for building the table of contents
1341 string const Paragraph::asString(Buffer const & buffer, bool label) const
1343 OutputParams runparams;
1344 return asString(buffer, runparams, label);
1348 string const Paragraph::asString(Buffer const & buffer,
1349 OutputParams const & runparams,
1354 if (label && !params().labelString().empty())
1355 s += params().labelString() + ' ';
1357 for (pos_type i = 0; i < size(); ++i) {
1358 value_type c = getChar(i);
1361 else if (c == META_INSET &&
1362 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1364 getInset(i)->plaintext(buffer, os, runparams);
1365 s += subst(STRCONV(os.str()),'\n',' ');
1371 // This should really be done by the caller and not here.
1372 string ret = asString(buffer, runparams, 0, size(), label);
1373 return subst(ret, '\n', ' ');
1378 string const Paragraph::asString(Buffer const & buffer,
1379 pos_type beg, pos_type end, bool label) const
1382 OutputParams const runparams;
1383 return asString(buffer, runparams, beg, end, label);
1387 string const Paragraph::asString(Buffer const & buffer,
1388 OutputParams const & runparams,
1389 pos_type beg, pos_type end, bool label) const
1393 if (beg == 0 && label && !params().labelString().empty())
1394 os << params().labelString() << ' ';
1396 for (pos_type i = beg; i < end; ++i) {
1397 value_type const c = getUChar(buffer.params(), i);
1400 else if (c == META_INSET)
1401 getInset(i)->textString(buffer, os, runparams);
1408 void Paragraph::setInsetOwner(InsetBase * inset)
1410 pimpl_->inset_owner = inset;
1414 void Paragraph::setContentsFromPar(Paragraph const & par)
1416 pimpl_->setContentsFromPar(par);
1420 Change const Paragraph::lookupChange(lyx::pos_type pos) const
1422 BOOST_ASSERT(pos <= size());
1423 return pimpl_->lookupChange(pos);
1427 bool Paragraph::isChanged(pos_type start, pos_type end) const
1429 return pimpl_->isChanged(start, end);
1433 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1435 return pimpl_->isChangeEdited(start, end);
1439 void Paragraph::setChangeType(lyx::pos_type pos, Change::Type type)
1441 pimpl_->setChangeType(pos, type);
1445 void Paragraph::setChange(lyx::pos_type pos, Change change)
1447 pimpl_->setChange(pos, change);
1451 void Paragraph::markErased(bool erased)
1453 pimpl_->markErased(erased);
1457 void Paragraph::acceptChange(pos_type start, pos_type end)
1459 return pimpl_->acceptChange(start, end);
1463 void Paragraph::rejectChange(pos_type start, pos_type end)
1465 return pimpl_->rejectChange(start, end);
1469 int Paragraph::id() const
1475 LyXLayout_ptr const & Paragraph::layout() const
1481 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1483 layout_ = new_layout;
1487 InsetBase * Paragraph::inInset() const
1489 return pimpl_->inset_owner;
1493 InsetBase::Code Paragraph::ownerCode() const
1495 return pimpl_->inset_owner
1496 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1500 void Paragraph::clearContents()
1506 void Paragraph::setChar(pos_type pos, value_type c)
1512 ParagraphParameters & Paragraph::params()
1514 return pimpl_->params;
1518 ParagraphParameters const & Paragraph::params() const
1520 return pimpl_->params;
1524 bool Paragraph::isFreeSpacing() const
1526 if (layout()->free_spacing)
1529 // for now we just need this, later should we need this in some
1530 // other way we can always add a function to InsetBase too.
1531 return ownerCode() == InsetBase::ERT_CODE;
1535 bool Paragraph::allowEmpty() const
1537 if (layout()->keepempty)
1539 return ownerCode() == InsetBase::ERT_CODE;
1543 Row & Paragraph::getRow(pos_type pos, bool boundary)
1545 BOOST_ASSERT(!rows().empty());
1547 // If boundary is set we should return the row on which
1548 // the character before is inside.
1549 if (pos > 0 && boundary)
1552 RowList::iterator rit = rows_.end();
1553 RowList::iterator const begin = rows_.begin();
1555 for (--rit; rit != begin && rit->pos() > pos; --rit)
1562 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1564 BOOST_ASSERT(!rows().empty());
1566 // If boundary is set we should return the row on which
1567 // the character before is inside.
1568 if (pos > 0 && boundary)
1571 RowList::const_iterator rit = rows_.end();
1572 RowList::const_iterator const begin = rows_.begin();
1574 for (--rit; rit != begin && rit->pos() > pos; --rit)
1581 size_t Paragraph::pos2row(pos_type pos) const
1583 BOOST_ASSERT(!rows().empty());
1585 RowList::const_iterator rit = rows_.end();
1586 RowList::const_iterator const begin = rows_.begin();
1588 for (--rit; rit != begin && rit->pos() > pos; --rit)
1595 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1597 if (!Encodings::is_arabic(c))
1598 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1599 return c + (0xb0 - '0');
1603 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1604 value_type next_char = ' ';
1606 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1607 value_type const par_char = getChar(i);
1608 if (!Encodings::isComposeChar_arabic(par_char)) {
1609 next_char = par_char;
1614 if (Encodings::is_arabic(next_char)) {
1615 if (Encodings::is_arabic(prev_char) &&
1616 !Encodings::is_arabic_special(prev_char))
1617 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1619 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1621 if (Encodings::is_arabic(prev_char) &&
1622 !Encodings::is_arabic_special(prev_char))
1623 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1625 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1630 void Paragraph::dump() const
1632 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1633 for (size_t i = 0; i != rows_.size(); ++i) {
1634 lyxerr << " row " << i << ": ";