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/bind.hpp>
59 using lyx::support::contains;
60 using lyx::support::rsplit;
69 using std::ostringstream;
72 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
74 BOOST_ASSERT(!rows().empty());
76 // If boundary is set we should return the row on which
77 // the character before is inside.
78 if (pos > 0 && boundary)
81 RowList::iterator rit = rows_.end();
82 RowList::iterator const begin = rows_.begin();
84 for (--rit; rit != begin && rit->pos() > pos; --rit)
91 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
93 BOOST_ASSERT(!rows().empty());
95 // If boundary is set we should return the row on which
96 // the character before is inside.
97 if (pos > 0 && boundary)
100 RowList::const_iterator rit = rows_.end();
101 RowList::const_iterator const begin = rows_.begin();
103 for (--rit; rit != begin && rit->pos() > pos; --rit)
110 size_t ParagraphMetrics::pos2row(pos_type pos) const
112 BOOST_ASSERT(!rows().empty());
114 RowList::const_iterator rit = rows_.end();
115 RowList::const_iterator const begin = rows_.begin();
117 for (--rit; rit != begin && rit->pos() > pos; --rit)
124 void ParagraphMetrics::dump() const
126 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
127 for (size_t i = 0; i != rows_.size(); ++i) {
128 lyxerr << " row " << i << ": ";
134 Paragraph::Paragraph()
135 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
142 Paragraph::Paragraph(Paragraph const & par)
143 : ParagraphMetrics(par),
144 itemdepth(par.itemdepth), insetlist(par.insetlist),
145 layout_(par.layout_),
146 text_(par.text_), begin_of_body_(par.begin_of_body_),
147 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
149 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
150 InsetList::iterator it = insetlist.begin();
151 InsetList::iterator end = insetlist.end();
152 for (; it != end; ++it)
153 it->inset = it->inset->clone().release();
157 Paragraph & Paragraph::operator=(Paragraph const & par)
159 // needed as we will destroy the pimpl_ before copying it
161 itemdepth = par.itemdepth;
163 insetlist = par.insetlist;
164 InsetList::iterator it = insetlist.begin();
165 InsetList::iterator end = insetlist.end();
166 for (; it != end; ++it)
167 it->inset = it->inset->clone().release();
169 layout_ = par.layout();
171 begin_of_body_ = par.begin_of_body_;
174 pimpl_ = new Pimpl(*par.pimpl_, this);
176 ParagraphMetrics::operator=(par);
182 Paragraph::~Paragraph()
186 //lyxerr << "Paragraph::paragraph_id = "
187 // << Paragraph::paragraph_id << endl;
191 void Paragraph::write(Buffer const & buf, ostream & os,
192 BufferParams const & bparams,
193 depth_type & dth) const
195 // The beginning or end of a deeper (i.e. nested) area?
196 if (dth != params().depth()) {
197 if (params().depth() > dth) {
198 while (params().depth() > dth) {
199 os << "\n\\begin_deeper";
203 while (params().depth() < dth) {
204 os << "\n\\end_deeper";
210 // First write the layout
211 os << "\n\\begin_layout " << layout()->name() << '\n';
215 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
217 Change running_change = Change(Change::UNCHANGED);
220 for (pos_type i = 0; i <= size(); ++i) {
222 Change change = pimpl_->lookupChange(i);
223 Changes::lyxMarkChange(os, column, running_change, change);
224 running_change = change;
229 // Write font changes
230 LyXFont font2 = getFontSettings(bparams, i);
231 if (font2 != font1) {
232 font2.lyxWriteChanges(font1, os);
237 value_type const c = getChar(i);
241 InsetBase const * inset = getInset(i);
243 if (inset->directWrite()) {
244 // international char, let it write
245 // code directly so it's shorter in
247 inset->write(buf, os);
251 os << "\\begin_inset ";
252 inset->write(buf, os);
253 os << "\n\\end_inset\n\n";
259 os << "\n\\backslash\n";
263 if (i + 1 < size() && getChar(i + 1) == ' ') {
270 if ((column > 70 && c == ' ')
275 // this check is to amend a bug. LyX sometimes
276 // inserts '\0' this could cause problems.
278 std::vector<char> tmp = ucs4_to_utf8(c);
282 lyxerr << "ERROR (Paragraph::writeFile):"
283 " NULL char in structure." << endl;
289 os << "\n\\end_layout\n";
293 void Paragraph::validate(LaTeXFeatures & features) const
295 pimpl_->validate(features, *layout());
299 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
301 return pimpl_->eraseChar(pos, trackChanges);
305 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
307 return pimpl_->eraseChars(start, end, trackChanges);
311 void Paragraph::insert(pos_type start, docstring const & str,
312 LyXFont const & font, Change const & change)
314 for (size_t i = 0, n = str.size(); i != n ; ++i)
315 insertChar(start + i, str[i], font, change);
319 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
322 pimpl_->insertChar(pos, c, Change(trackChanges ?
323 Change::INSERTED : Change::UNCHANGED));
327 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
328 LyXFont const & font, bool trackChanges)
330 pimpl_->insertChar(pos, c, Change(trackChanges ?
331 Change::INSERTED : Change::UNCHANGED));
336 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
337 LyXFont const & font, Change const & change)
339 pimpl_->insertChar(pos, c, change);
344 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
345 Change const & change)
347 pimpl_->insertInset(pos, inset, change);
351 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
352 LyXFont const & font, Change const & change)
354 pimpl_->insertInset(pos, inset, change);
359 bool Paragraph::insetAllowed(InsetBase_code code)
361 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
365 // Gets uninstantiated font setting at position.
366 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
370 lyxerr << " pos: " << pos << " size: " << size() << endl;
371 BOOST_ASSERT(pos <= size());
374 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
375 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
376 for (; cit != end; ++cit)
377 if (cit->pos() >= pos)
383 if (pos == size() && !empty())
384 return getFontSettings(bparams, pos - 1);
386 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
390 FontSpan Paragraph::fontSpan(pos_type pos) const
392 BOOST_ASSERT(pos <= size());
395 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
396 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
397 for (; cit != end; ++cit) {
398 if (cit->pos() >= pos) {
399 if (pos >= beginOfBody())
400 return FontSpan(std::max(start, beginOfBody()),
403 return FontSpan(start,
404 std::min(beginOfBody() - 1,
407 start = cit->pos() + 1;
410 // This should not happen, but if so, we take no chances.
411 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
413 return FontSpan(pos, pos);
417 // Gets uninstantiated font setting at position 0
418 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
420 if (!empty() && !pimpl_->fontlist.empty())
421 return pimpl_->fontlist[0].font();
423 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
427 // Gets the fully instantiated font at a given position in a paragraph
428 // This is basically the same function as LyXText::GetFont() in text2.C.
429 // The difference is that this one is used for generating the LaTeX file,
430 // and thus cosmetic "improvements" are disallowed: This has to deliver
431 // the true picture of the buffer. (Asger)
432 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
433 LyXFont const & outerfont) const
435 BOOST_ASSERT(pos >= 0);
437 LyXLayout_ptr const & lout = layout();
439 pos_type const body_pos = beginOfBody();
443 layoutfont = lout->labelfont;
445 layoutfont = lout->font;
447 LyXFont font = getFontSettings(bparams, pos);
448 font.realize(layoutfont);
449 font.realize(outerfont);
450 font.realize(bparams.getFont());
456 LyXFont const Paragraph::getLabelFont
457 (BufferParams const & bparams, LyXFont const & outerfont) const
459 LyXFont tmpfont = layout()->labelfont;
460 tmpfont.setLanguage(getParLanguage(bparams));
461 tmpfont.realize(outerfont);
462 tmpfont.realize(bparams.getFont());
467 LyXFont const Paragraph::getLayoutFont
468 (BufferParams const & bparams, LyXFont const & outerfont) const
470 LyXFont tmpfont = layout()->font;
471 tmpfont.setLanguage(getParLanguage(bparams));
472 tmpfont.realize(outerfont);
473 tmpfont.realize(bparams.getFont());
478 /// Returns the height of the highest font in range
479 LyXFont_size Paragraph::highestFontInRange
480 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
482 if (pimpl_->fontlist.empty())
485 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
486 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
487 for (; end_it != end; ++end_it) {
488 if (end_it->pos() >= endpos)
495 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
496 for (; cit != end; ++cit) {
497 if (cit->pos() >= startpos)
501 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
502 for (; cit != end_it; ++cit) {
503 LyXFont::FONT_SIZE size = cit->font().size();
504 if (size == LyXFont::INHERIT_SIZE)
506 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
513 Paragraph::value_type
514 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
516 value_type c = getChar(pos);
517 if (!lyxrc.rtl_support)
547 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
554 void Paragraph::setFont(pos_type pos, LyXFont const & font)
556 BOOST_ASSERT(pos <= size());
558 // First, reduce font against layout/label font
559 // Update: The setCharFont() routine in text2.C already
560 // reduces font, so we don't need to do that here. (Asger)
561 // No need to simplify this because it will disappear
562 // in a new kernel. (Asger)
563 // Next search font table
565 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
566 Pimpl::FontList::iterator it = beg;
567 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
568 for (; it != endit; ++it) {
569 if (it->pos() >= pos)
572 size_t const i = distance(beg, it);
573 bool notfound = (it == endit);
575 if (!notfound && pimpl_->fontlist[i].font() == font)
578 bool begin = pos == 0 || notfound ||
579 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
580 // Is position pos is a beginning of a font block?
581 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
582 // Is position pos is the end of a font block?
583 if (begin && end) { // A single char block
584 if (i + 1 < pimpl_->fontlist.size() &&
585 pimpl_->fontlist[i + 1].font() == font) {
586 // Merge the singleton block with the next block
587 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
588 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
589 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
590 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
591 // Merge the singleton block with the previous block
592 pimpl_->fontlist[i - 1].pos(pos);
593 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
595 pimpl_->fontlist[i].font(font);
597 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
598 pimpl_->fontlist[i - 1].pos(pos);
600 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
601 Pimpl::FontTable(pos, font));
603 pimpl_->fontlist[i].pos(pos - 1);
604 if (!(i + 1 < pimpl_->fontlist.size() &&
605 pimpl_->fontlist[i + 1].font() == font))
606 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
607 Pimpl::FontTable(pos, font));
608 } else { // The general case. The block is splitted into 3 blocks
609 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
610 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
611 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
612 Pimpl::FontTable(pos, font));
617 void Paragraph::makeSameLayout(Paragraph const & par)
619 layout(par.layout());
621 params() = par.params();
625 int Paragraph::stripLeadingSpaces()
631 while (!empty() && (isNewline(0) || isLineSeparator(0))
633 eraseChar(0, false); // no change tracking here
641 bool Paragraph::hasSameLayout(Paragraph const & par) const
643 return par.layout() == layout() && params().sameLayout(par.params());
647 depth_type Paragraph::getDepth() const
649 return params().depth();
653 depth_type Paragraph::getMaxDepthAfter() const
655 if (layout()->isEnvironment())
656 return params().depth() + 1;
658 return params().depth();
662 char Paragraph::getAlign() const
664 if (params().align() == LYX_ALIGN_LAYOUT)
665 return layout()->align;
667 return params().align();
671 docstring const & Paragraph::getLabelstring() const
673 return params().labelString();
677 // the next two functions are for the manual labels
678 docstring const Paragraph::getLabelWidthString() const
680 if (!params().labelWidthString().empty())
681 return params().labelWidthString();
683 return _("Senseless with this layout!");
687 void Paragraph::setLabelWidthString(docstring const & s)
689 params().labelWidthString(s);
693 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
696 params().labelWidthString(docstring());
697 params().align(LYX_ALIGN_LAYOUT);
698 params().spacing(Spacing(Spacing::Default));
702 pos_type Paragraph::beginOfBody() const
704 return begin_of_body_;
708 void Paragraph::setBeginOfBody()
710 if (layout()->labeltype != LABEL_MANUAL) {
715 // Unroll the first two cycles of the loop
716 // and remember the previous character to
717 // remove unnecessary getChar() calls
719 pos_type end = size();
720 if (i < end && !isNewline(i)) {
722 char_type previous_char = 0;
725 previous_char = text_[i];
728 while (i < end && previous_char != ' ') {
733 previous_char = temp;
743 // returns -1 if inset not found
744 int Paragraph::getPositionOfInset(InsetBase const * inset) const
747 InsetList::const_iterator it = insetlist.begin();
748 InsetList::const_iterator end = insetlist.end();
749 for (; it != end; ++it)
750 if (it->inset == inset)
756 InsetBibitem * Paragraph::bibitem() const
758 if (!insetlist.empty()) {
759 InsetBase * inset = insetlist.begin()->inset;
760 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
761 return static_cast<InsetBibitem *>(inset);
767 bool Paragraph::forceDefaultParagraphs() const
769 return inInset() && inInset()->forceDefaultParagraphs(0);
775 // paragraphs inside floats need different alignment tags to avoid
778 bool noTrivlistCentering(InsetBase::Code code)
780 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
784 string correction(string const & orig)
786 if (orig == "flushleft")
787 return "raggedright";
788 if (orig == "flushright")
790 if (orig == "center")
796 string const corrected_env(string const & suffix, string const & env,
797 InsetBase::Code code)
799 string output = suffix + "{";
800 if (noTrivlistCentering(code))
801 output += correction(env);
805 if (suffix == "\\begin")
811 int adjust_column_count(string const & str, int oldcol)
813 if (!contains(str, "\n"))
814 return oldcol + str.size();
817 return rsplit(str, tmp, '\n').size();
824 // This could go to ParagraphParameters if we want to
825 int Paragraph::startTeXParParams(BufferParams const & bparams,
826 odocstream & os, bool moving_arg) const
830 if (params().noindent()) {
835 switch (params().align()) {
837 case LYX_ALIGN_BLOCK:
838 case LYX_ALIGN_LAYOUT:
839 case LYX_ALIGN_SPECIAL:
842 case LYX_ALIGN_RIGHT:
843 case LYX_ALIGN_CENTER:
851 switch (params().align()) {
853 case LYX_ALIGN_BLOCK:
854 case LYX_ALIGN_LAYOUT:
855 case LYX_ALIGN_SPECIAL:
857 case LYX_ALIGN_LEFT: {
859 if (getParLanguage(bparams)->babel() != "hebrew")
860 output = corrected_env("\\begin", "flushleft", ownerCode());
862 output = corrected_env("\\begin", "flushright", ownerCode());
863 os << from_ascii(output);
864 column = adjust_column_count(output, column);
866 } case LYX_ALIGN_RIGHT: {
868 if (getParLanguage(bparams)->babel() != "hebrew")
869 output = corrected_env("\\begin", "flushright", ownerCode());
871 output = corrected_env("\\begin", "flushleft", ownerCode());
872 os << from_ascii(output);
873 column = adjust_column_count(output, column);
875 } case LYX_ALIGN_CENTER: {
877 output = corrected_env("\\begin", "center", ownerCode());
878 os << from_ascii(output);
879 column = adjust_column_count(output, column);
888 // This could go to ParagraphParameters if we want to
889 int Paragraph::endTeXParParams(BufferParams const & bparams,
890 odocstream & os, bool moving_arg) const
894 switch (params().align()) {
896 case LYX_ALIGN_BLOCK:
897 case LYX_ALIGN_LAYOUT:
898 case LYX_ALIGN_SPECIAL:
901 case LYX_ALIGN_RIGHT:
902 case LYX_ALIGN_CENTER:
910 switch (params().align()) {
912 case LYX_ALIGN_BLOCK:
913 case LYX_ALIGN_LAYOUT:
914 case LYX_ALIGN_SPECIAL:
916 case LYX_ALIGN_LEFT: {
918 if (getParLanguage(bparams)->babel() != "hebrew")
919 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
921 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
922 os << from_ascii(output);
923 column = adjust_column_count(output, column);
925 } case LYX_ALIGN_RIGHT: {
927 if (getParLanguage(bparams)->babel() != "hebrew")
928 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
930 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
931 os << from_ascii(output);
932 column = adjust_column_count(output, column);
934 } case LYX_ALIGN_CENTER: {
936 output = corrected_env("\n\\par\\end", "center", ownerCode());
937 os << from_ascii(output);
938 column = adjust_column_count(output, column);
947 // This one spits out the text of the paragraph
948 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
949 BufferParams const & bparams,
950 LyXFont const & outerfont,
951 odocstream & os, TexRow & texrow,
952 OutputParams const & runparams) const
954 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
956 bool return_value = false;
960 // well we have to check if we are in an inset with unlimited
961 // length (all in one row) if that is true then we don't allow
962 // any special options in the paragraph and also we don't allow
963 // any environment other then "Standard" to be valid!
964 bool asdefault = forceDefaultParagraphs();
967 style = bparams.getLyXTextClass().defaultLayout();
974 LaTeXFeatures features(buf, bparams, runparams);
976 // output change tracking marks only if desired,
977 // if dvipost is installed,
978 // and with dvi/ps (other formats don't work)
979 bool const output = bparams.outputChanges
980 && runparams.flavor == OutputParams::LATEX
981 && features.isAvailable("dvipost");
983 // Maybe we have to create a optional argument.
984 pos_type body_pos = beginOfBody();
985 unsigned int column = 0;
988 // the optional argument is kept in curly brackets in
989 // case it contains a ']'
992 basefont = getLabelFont(bparams, outerfont);
994 basefont = getLayoutFont(bparams, outerfont);
997 // Which font is currently active?
998 LyXFont running_font(basefont);
999 // Do we have an open font change?
1000 bool open_font = false;
1002 Change::Type runningChangeType = Change::UNCHANGED;
1004 texrow.start(id(), 0);
1006 // if the paragraph is empty, the loop will not be entered at all
1008 if (style->isCommand()) {
1013 column += startTeXParParams(bparams, os,
1014 runparams.moving_arg);
1017 for (pos_type i = 0; i < size(); ++i) {
1019 // First char in paragraph or after label?
1020 if (i == body_pos) {
1023 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1026 basefont = getLayoutFont(bparams, outerfont);
1027 running_font = basefont;
1031 if (style->isCommand()) {
1037 column += startTeXParParams(bparams, os,
1038 runparams.moving_arg);
1041 value_type c = getChar(i);
1043 // Fully instantiated font
1044 LyXFont font = getFont(bparams, i, outerfont);
1046 LyXFont const last_font = running_font;
1048 // Spaces at end of font change are simulated to be
1049 // outside font change, i.e. we write "\textXX{text} "
1050 // rather than "\textXX{text }". (Asger)
1051 if (open_font && c == ' ' && i <= size() - 2) {
1052 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
1053 if (next_font != running_font && next_font != font) {
1058 // We end font definition before blanks
1060 (font != running_font ||
1061 font.language() != running_font.language()))
1063 column += running_font.latexWriteEndChanges(os,
1065 (i == body_pos-1) ? basefont : font);
1066 running_font = basefont;
1070 // Blanks are printed before start of fontswitch
1072 // Do not print the separation of the optional argument
1073 if (i != body_pos - 1) {
1074 pimpl_->simpleTeXBlanks(os, texrow, i,
1075 column, font, *style);
1079 // Do we need to change font?
1080 if ((font != running_font ||
1081 font.language() != running_font.language()) &&
1084 column += font.latexWriteStartChanges(os, basefont,
1086 running_font = font;
1090 Change::Type changeType = pimpl_->lookupChange(i).type;
1092 column += Changes::latexMarkChange(os, runningChangeType,
1093 changeType, output);
1094 runningChangeType = changeType;
1096 // do not output text which is marked deleted
1097 // if change tracking output is not desired
1098 if (output || runningChangeType != Change::DELETED) {
1099 OutputParams rp = runparams;
1100 rp.free_spacing = style->free_spacing;
1101 rp.local_font = &font;
1102 rp.intitle = style->intitle;
1103 pimpl_->simpleTeXSpecialChars(buf, bparams,
1106 basefont, outerfont, open_font,
1108 *style, i, column, c);
1112 column += Changes::latexMarkChange(os,
1113 runningChangeType, Change::UNCHANGED, output);
1115 // If we have an open font definition, we have to close it
1117 #ifdef FIXED_LANGUAGE_END_DETECTION
1120 .latexWriteEndChanges(os, basefont,
1121 next_->getFont(bparams,
1124 running_font.latexWriteEndChanges(os, basefont,
1128 #ifdef WITH_WARNINGS
1129 //#warning For now we ALWAYS have to close the foreign font settings if they are
1130 //#warning there as we start another \selectlanguage with the next paragraph if
1131 //#warning we are in need of this. This should be fixed sometime (Jug)
1133 running_font.latexWriteEndChanges(os, basefont, basefont);
1137 // Needed if there is an optional argument but no contents.
1138 if (body_pos > 0 && body_pos == size()) {
1140 return_value = false;
1144 column += endTeXParParams(bparams, os, runparams.moving_arg);
1147 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1148 return return_value;
1154 // checks, if newcol chars should be put into this line
1155 // writes newline, if necessary.
1156 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1157 string::size_type newcol)
1160 if (colcount > lyxrc.ascii_linelen) {
1162 colcount = newcol; // assume write after this call
1177 string tag_name(PAR_TAG const & pt) {
1179 case PAR_NONE: return "!-- --";
1180 case TT: return "tt";
1181 case SF: return "sf";
1182 case BF: return "bf";
1183 case IT: return "it";
1184 case SL: return "sl";
1185 case EM: return "em";
1192 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1194 p1 = static_cast<PAR_TAG>(p1 | p2);
1199 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1201 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1207 bool Paragraph::emptyTag() const
1209 for (pos_type i = 0; i < size(); ++i) {
1211 InsetBase const * inset = getInset(i);
1212 InsetBase::Code lyx_code = inset->lyxCode();
1213 if (lyx_code != InsetBase::TOC_CODE &&
1214 lyx_code != InsetBase::INCLUDE_CODE &&
1215 lyx_code != InsetBase::GRAPHICS_CODE &&
1216 lyx_code != InsetBase::ERT_CODE &&
1217 lyx_code != InsetBase::FLOAT_CODE &&
1218 lyx_code != InsetBase::TABULAR_CODE) {
1222 value_type c = getChar(i);
1223 if (c != ' ' && c != '\t')
1231 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1233 for (pos_type i = 0; i < size(); ++i) {
1235 InsetBase const * inset = getInset(i);
1236 InsetBase::Code lyx_code = inset->lyxCode();
1237 if (lyx_code == InsetBase::LABEL_CODE) {
1238 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1239 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
1248 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1251 for (i = 0; i < size(); ++i) {
1253 InsetBase const * inset = getInset(i);
1254 inset->docbook(buf, os, runparams);
1256 value_type c = getChar(i);
1259 os << sgml::escapeChar(c);
1266 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1270 for (pos_type i = initial; i < size(); ++i) {
1271 LyXFont font = getFont(buf.params(), i, outerfont);
1274 if (i != initial && font != font_old)
1283 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1285 OutputParams const & runparams,
1286 LyXFont const & outerfont,
1287 pos_type initial) const
1289 bool emph_flag = false;
1291 LyXLayout_ptr const & style = layout();
1293 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1295 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1298 // parsing main loop
1299 for (pos_type i = initial; i < size(); ++i) {
1300 LyXFont font = getFont(buf.params(), i, outerfont);
1302 // handle <emphasis> tag
1303 if (font_old.emph() != font.emph()) {
1304 if (font.emph() == LyXFont::ON) {
1307 } else if (i != initial) {
1308 os << "</emphasis>";
1314 InsetBase const * inset = getInset(i);
1315 inset->docbook(buf, os, runparams);
1317 value_type c = getChar(i);
1319 if (style->pass_thru)
1322 os << sgml::escapeChar(c);
1328 os << "</emphasis>";
1331 if (style->free_spacing)
1333 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1338 bool Paragraph::isNewline(pos_type pos) const
1341 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1345 bool Paragraph::isLineSeparator(pos_type pos) const
1347 value_type const c = getChar(pos);
1348 return isLineSeparatorChar(c)
1349 || (c == Paragraph::META_INSET && getInset(pos) &&
1350 getInset(pos)->isLineSeparator());
1354 /// Used by the spellchecker
1355 bool Paragraph::isLetter(pos_type pos) const
1358 return getInset(pos)->isLetter();
1360 value_type const c = getChar(pos);
1361 return isLetterChar(c) || isDigit(c);
1367 Paragraph::getParLanguage(BufferParams const & bparams) const
1370 return getFirstFontSettings(bparams).language();
1371 #ifdef WITH_WARNINGS
1372 #warning FIXME we should check the prev par as well (Lgb)
1374 return bparams.language;
1378 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1380 return lyxrc.rtl_support
1381 && getParLanguage(bparams)->rightToLeft()
1382 && ownerCode() != InsetBase::ERT_CODE;
1386 void Paragraph::changeLanguage(BufferParams const & bparams,
1387 Language const * from, Language const * to)
1389 // change language including dummy font change at the end
1390 for (pos_type i = 0; i <= size(); ++i) {
1391 LyXFont font = getFontSettings(bparams, i);
1392 if (font.language() == from) {
1393 font.setLanguage(to);
1400 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1402 Language const * doc_language = bparams.language;
1403 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1404 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1406 for (; cit != end; ++cit)
1407 if (cit->font().language() != ignore_language &&
1408 cit->font().language() != latex_language &&
1409 cit->font().language() != doc_language)
1415 // Convert the paragraph to a string.
1416 // Used for building the table of contents
1417 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1419 OutputParams runparams;
1420 return asString(buffer, runparams, label);
1424 docstring const Paragraph::asString(Buffer const & buffer,
1425 OutputParams const & runparams,
1430 if (label && !params().labelString().empty())
1431 s += params().labelString() + ' ';
1433 for (pos_type i = 0; i < size(); ++i) {
1434 value_type c = getChar(i);
1437 else if (c == META_INSET &&
1438 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1440 getInset(i)->plaintext(buffer, os, runparams);
1441 s += subst(STRCONV(os.str()),'\n',' ');
1447 // This should really be done by the caller and not here.
1448 docstring ret = asString(buffer, runparams, 0, size(), label);
1449 return subst(ret, '\n', ' ');
1454 docstring const Paragraph::asString(Buffer const & buffer,
1455 pos_type beg, pos_type end, bool label) const
1458 OutputParams const runparams;
1459 return asString(buffer, runparams, beg, end, label);
1463 docstring const Paragraph::asString(Buffer const & buffer,
1464 OutputParams const & runparams,
1465 pos_type beg, pos_type end, bool label) const
1467 lyx::odocstringstream os;
1469 if (beg == 0 && label && !params().labelString().empty())
1470 os << params().labelString() << ' ';
1472 for (pos_type i = beg; i < end; ++i) {
1473 value_type const c = getUChar(buffer.params(), i);
1476 else if (c == META_INSET)
1477 getInset(i)->textString(buffer, os, runparams);
1484 void Paragraph::setInsetOwner(InsetBase * inset)
1486 pimpl_->inset_owner = inset;
1490 Change const Paragraph::lookupChange(pos_type pos) const
1492 BOOST_ASSERT(pos <= size());
1493 return pimpl_->lookupChange(pos);
1497 bool Paragraph::isChanged(pos_type start, pos_type end) const
1499 return pimpl_->isChanged(start, end);
1503 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
1505 return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
1509 void Paragraph::setChange(Change const & change)
1511 pimpl_->setChange(change);
1515 void Paragraph::setChange(pos_type pos, Change const & change)
1517 pimpl_->setChange(pos, change);
1521 void Paragraph::acceptChanges(pos_type start, pos_type end)
1523 return pimpl_->acceptChanges(start, end);
1527 void Paragraph::rejectChanges(pos_type start, pos_type end)
1529 return pimpl_->rejectChanges(start, end);
1533 int Paragraph::id() const
1539 LyXLayout_ptr const & Paragraph::layout() const
1545 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1547 layout_ = new_layout;
1551 InsetBase * Paragraph::inInset() const
1553 return pimpl_->inset_owner;
1557 InsetBase::Code Paragraph::ownerCode() const
1559 return pimpl_->inset_owner
1560 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1564 void Paragraph::clearContents()
1570 ParagraphParameters & Paragraph::params()
1572 return pimpl_->params;
1576 ParagraphParameters const & Paragraph::params() const
1578 return pimpl_->params;
1582 bool Paragraph::isFreeSpacing() const
1584 if (layout()->free_spacing)
1587 // for now we just need this, later should we need this in some
1588 // other way we can always add a function to InsetBase too.
1589 return ownerCode() == InsetBase::ERT_CODE;
1593 bool Paragraph::allowEmpty() const
1595 if (layout()->keepempty)
1597 return ownerCode() == InsetBase::ERT_CODE;
1601 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1603 if (!Encodings::is_arabic(c))
1604 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1605 // FIXME UNICODE What does this do?
1606 return c + (0xb0 - '0');
1610 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1611 value_type next_char = ' ';
1613 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1614 value_type const par_char = getChar(i);
1615 if (!Encodings::isComposeChar_arabic(par_char)) {
1616 next_char = par_char;
1621 if (Encodings::is_arabic(next_char)) {
1622 if (Encodings::is_arabic(prev_char) &&
1623 !Encodings::is_arabic_special(prev_char))
1624 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1626 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1628 if (Encodings::is_arabic(prev_char) &&
1629 !Encodings::is_arabic_special(prev_char))
1630 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1632 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1637 bool Paragraph::hfillExpansion(Row const & row, pos_type pos) const
1642 // at the end of a row it does not count
1643 // unless another hfill exists on the line
1644 if (pos >= row.endpos()) {
1645 for (pos_type i = row.pos(); i < pos && !isHfill(i); ++i)
1649 // at the beginning of a row it does not count, if it is not
1650 // the first row of a paragaph
1654 // in some labels it does not count
1655 if (layout()->margintype != MARGIN_MANUAL && pos < beginOfBody())
1658 // if there is anything between the first char of the row and
1659 // the specified position that is not a newline and not a hfill,
1660 // the hfill will count, otherwise not
1661 pos_type i = row.pos();
1662 while (i < pos && (isNewline(i) || isHfill(i)))