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 ParagraphMetrics::ParagraphMetrics()
77 ParagraphMetrics::ParagraphMetrics(ParagraphMetrics const & pm)
78 : dim_(pm.dim_), rows_(pm.rows_), rowSignature_(pm.rowSignature_)
83 ParagraphMetrics & ParagraphMetrics::operator=(ParagraphMetrics const & pm)
87 rowSignature_ = pm.rowSignature_;
92 Row & ParagraphMetrics::getRow(pos_type pos, bool boundary)
94 BOOST_ASSERT(!rows().empty());
96 // If boundary is set we should return the row on which
97 // the character before is inside.
98 if (pos > 0 && boundary)
101 RowList::iterator rit = rows_.end();
102 RowList::iterator const begin = rows_.begin();
104 for (--rit; rit != begin && rit->pos() > pos; --rit)
111 Row const & ParagraphMetrics::getRow(pos_type pos, bool boundary) const
113 BOOST_ASSERT(!rows().empty());
115 // If boundary is set we should return the row on which
116 // the character before is inside.
117 if (pos > 0 && boundary)
120 RowList::const_iterator rit = rows_.end();
121 RowList::const_iterator const begin = rows_.begin();
123 for (--rit; rit != begin && rit->pos() > pos; --rit)
130 size_t ParagraphMetrics::pos2row(pos_type pos) const
132 BOOST_ASSERT(!rows().empty());
134 RowList::const_iterator rit = rows_.end();
135 RowList::const_iterator const begin = rows_.begin();
137 for (--rit; rit != begin && rit->pos() > pos; --rit)
144 void ParagraphMetrics::dump() const
146 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
147 for (size_t i = 0; i != rows_.size(); ++i) {
148 lyxerr << " row " << i << ": ";
154 Paragraph::Paragraph()
155 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
162 Paragraph::Paragraph(Paragraph const & par)
163 : ParagraphMetrics(par),
164 itemdepth(par.itemdepth), insetlist(par.insetlist),
165 layout_(par.layout_),
166 text_(par.text_), begin_of_body_(par.begin_of_body_),
167 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
169 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
170 InsetList::iterator it = insetlist.begin();
171 InsetList::iterator end = insetlist.end();
172 for (; it != end; ++it)
173 it->inset = it->inset->clone().release();
177 Paragraph & Paragraph::operator=(Paragraph const & par)
179 // needed as we will destroy the pimpl_ before copying it
181 itemdepth = par.itemdepth;
183 insetlist = par.insetlist;
184 InsetList::iterator it = insetlist.begin();
185 InsetList::iterator end = insetlist.end();
186 for (; it != end; ++it)
187 it->inset = it->inset->clone().release();
189 layout_ = par.layout();
191 begin_of_body_ = par.begin_of_body_;
194 pimpl_ = new Pimpl(*par.pimpl_, this);
196 ParagraphMetrics::operator=(par);
202 Paragraph::~Paragraph()
206 //lyxerr << "Paragraph::paragraph_id = "
207 // << Paragraph::paragraph_id << endl;
211 void Paragraph::write(Buffer const & buf, ostream & os,
212 BufferParams const & bparams,
213 depth_type & dth) const
215 // The beginning or end of a deeper (i.e. nested) area?
216 if (dth != params().depth()) {
217 if (params().depth() > dth) {
218 while (params().depth() > dth) {
219 os << "\n\\begin_deeper";
223 while (params().depth() < dth) {
224 os << "\n\\end_deeper";
230 // First write the layout
231 os << "\n\\begin_layout " << layout()->name() << '\n';
235 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
237 Change running_change = Change(Change::UNCHANGED);
240 for (pos_type i = 0; i <= size(); ++i) {
242 Change change = pimpl_->lookupChange(i);
243 Changes::lyxMarkChange(os, column, running_change, change);
244 running_change = change;
249 // Write font changes
250 LyXFont font2 = getFontSettings(bparams, i);
251 if (font2 != font1) {
252 font2.lyxWriteChanges(font1, os);
257 value_type const c = getChar(i);
261 InsetBase const * inset = getInset(i);
263 if (inset->directWrite()) {
264 // international char, let it write
265 // code directly so it's shorter in
267 inset->write(buf, os);
271 os << "\\begin_inset ";
272 inset->write(buf, os);
273 os << "\n\\end_inset\n\n";
279 os << "\n\\backslash\n";
283 if (i + 1 < size() && getChar(i + 1) == ' ') {
290 if ((column > 70 && c == ' ')
295 // this check is to amend a bug. LyX sometimes
296 // inserts '\0' this could cause problems.
298 std::vector<char> tmp = ucs4_to_utf8(c);
302 lyxerr << "ERROR (Paragraph::writeFile):"
303 " NULL char in structure." << endl;
309 os << "\n\\end_layout\n";
313 void Paragraph::validate(LaTeXFeatures & features) const
315 pimpl_->validate(features, *layout());
319 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
321 return pimpl_->eraseChar(pos, trackChanges);
325 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
327 return pimpl_->eraseChars(start, end, trackChanges);
331 void Paragraph::insert(pos_type start, docstring const & str,
332 LyXFont const & font, Change const & change)
334 for (size_t i = 0, n = str.size(); i != n ; ++i)
335 insertChar(start + i, str[i], font, change);
339 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
342 pimpl_->insertChar(pos, c, Change(trackChanges ?
343 Change::INSERTED : Change::UNCHANGED));
347 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
348 LyXFont const & font, bool trackChanges)
350 pimpl_->insertChar(pos, c, Change(trackChanges ?
351 Change::INSERTED : Change::UNCHANGED));
356 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
357 LyXFont const & font, Change const & change)
359 pimpl_->insertChar(pos, c, change);
364 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
365 Change const & change)
367 pimpl_->insertInset(pos, inset, change);
371 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
372 LyXFont const & font, Change const & change)
374 pimpl_->insertInset(pos, inset, change);
379 bool Paragraph::insetAllowed(InsetBase_code code)
381 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
385 // Gets uninstantiated font setting at position.
386 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
390 lyxerr << " pos: " << pos << " size: " << size() << endl;
391 BOOST_ASSERT(pos <= size());
394 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
395 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
396 for (; cit != end; ++cit)
397 if (cit->pos() >= pos)
403 if (pos == size() && !empty())
404 return getFontSettings(bparams, pos - 1);
406 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
410 FontSpan Paragraph::fontSpan(pos_type pos) const
412 BOOST_ASSERT(pos <= size());
415 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
416 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
417 for (; cit != end; ++cit) {
418 if (cit->pos() >= pos) {
419 if (pos >= beginOfBody())
420 return FontSpan(std::max(start, beginOfBody()),
423 return FontSpan(start,
424 std::min(beginOfBody() - 1,
427 start = cit->pos() + 1;
430 // This should not happen, but if so, we take no chances.
431 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
433 return FontSpan(pos, pos);
437 // Gets uninstantiated font setting at position 0
438 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
440 if (!empty() && !pimpl_->fontlist.empty())
441 return pimpl_->fontlist[0].font();
443 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
447 // Gets the fully instantiated font at a given position in a paragraph
448 // This is basically the same function as LyXText::GetFont() in text2.C.
449 // The difference is that this one is used for generating the LaTeX file,
450 // and thus cosmetic "improvements" are disallowed: This has to deliver
451 // the true picture of the buffer. (Asger)
452 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
453 LyXFont const & outerfont) const
455 BOOST_ASSERT(pos >= 0);
457 LyXLayout_ptr const & lout = layout();
459 pos_type const body_pos = beginOfBody();
463 layoutfont = lout->labelfont;
465 layoutfont = lout->font;
467 LyXFont font = getFontSettings(bparams, pos);
468 font.realize(layoutfont);
469 font.realize(outerfont);
470 font.realize(bparams.getFont());
476 LyXFont const Paragraph::getLabelFont
477 (BufferParams const & bparams, LyXFont const & outerfont) const
479 LyXFont tmpfont = layout()->labelfont;
480 tmpfont.setLanguage(getParLanguage(bparams));
481 tmpfont.realize(outerfont);
482 tmpfont.realize(bparams.getFont());
487 LyXFont const Paragraph::getLayoutFont
488 (BufferParams const & bparams, LyXFont const & outerfont) const
490 LyXFont tmpfont = layout()->font;
491 tmpfont.setLanguage(getParLanguage(bparams));
492 tmpfont.realize(outerfont);
493 tmpfont.realize(bparams.getFont());
498 /// Returns the height of the highest font in range
499 LyXFont_size Paragraph::highestFontInRange
500 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
502 if (pimpl_->fontlist.empty())
505 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
506 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
507 for (; end_it != end; ++end_it) {
508 if (end_it->pos() >= endpos)
515 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
516 for (; cit != end; ++cit) {
517 if (cit->pos() >= startpos)
521 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
522 for (; cit != end_it; ++cit) {
523 LyXFont::FONT_SIZE size = cit->font().size();
524 if (size == LyXFont::INHERIT_SIZE)
526 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
533 Paragraph::value_type
534 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
536 value_type c = getChar(pos);
537 if (!lyxrc.rtl_support)
567 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
574 void Paragraph::setFont(pos_type pos, LyXFont const & font)
576 BOOST_ASSERT(pos <= size());
578 // First, reduce font against layout/label font
579 // Update: The setCharFont() routine in text2.C already
580 // reduces font, so we don't need to do that here. (Asger)
581 // No need to simplify this because it will disappear
582 // in a new kernel. (Asger)
583 // Next search font table
585 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
586 Pimpl::FontList::iterator it = beg;
587 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
588 for (; it != endit; ++it) {
589 if (it->pos() >= pos)
592 size_t const i = distance(beg, it);
593 bool notfound = (it == endit);
595 if (!notfound && pimpl_->fontlist[i].font() == font)
598 bool begin = pos == 0 || notfound ||
599 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
600 // Is position pos is a beginning of a font block?
601 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
602 // Is position pos is the end of a font block?
603 if (begin && end) { // A single char block
604 if (i + 1 < pimpl_->fontlist.size() &&
605 pimpl_->fontlist[i + 1].font() == font) {
606 // Merge the singleton block with the next block
607 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
608 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
609 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
610 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
611 // Merge the singleton block with the previous block
612 pimpl_->fontlist[i - 1].pos(pos);
613 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
615 pimpl_->fontlist[i].font(font);
617 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
618 pimpl_->fontlist[i - 1].pos(pos);
620 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
621 Pimpl::FontTable(pos, font));
623 pimpl_->fontlist[i].pos(pos - 1);
624 if (!(i + 1 < pimpl_->fontlist.size() &&
625 pimpl_->fontlist[i + 1].font() == font))
626 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
627 Pimpl::FontTable(pos, font));
628 } else { // The general case. The block is splitted into 3 blocks
629 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
630 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
631 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
632 Pimpl::FontTable(pos, font));
637 void Paragraph::makeSameLayout(Paragraph const & par)
639 layout(par.layout());
641 params() = par.params();
645 int Paragraph::stripLeadingSpaces()
651 while (!empty() && (isNewline(0) || isLineSeparator(0))
653 eraseChar(0, false); // no change tracking here
661 bool Paragraph::hasSameLayout(Paragraph const & par) const
663 return par.layout() == layout() && params().sameLayout(par.params());
667 depth_type Paragraph::getDepth() const
669 return params().depth();
673 depth_type Paragraph::getMaxDepthAfter() const
675 if (layout()->isEnvironment())
676 return params().depth() + 1;
678 return params().depth();
682 char Paragraph::getAlign() const
684 if (params().align() == LYX_ALIGN_LAYOUT)
685 return layout()->align;
687 return params().align();
691 docstring const & Paragraph::getLabelstring() const
693 return params().labelString();
697 // the next two functions are for the manual labels
698 docstring const Paragraph::getLabelWidthString() const
700 if (!params().labelWidthString().empty())
701 return params().labelWidthString();
703 return _("Senseless with this layout!");
707 void Paragraph::setLabelWidthString(docstring const & s)
709 params().labelWidthString(s);
713 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
716 params().labelWidthString(docstring());
717 params().align(LYX_ALIGN_LAYOUT);
718 params().spacing(Spacing(Spacing::Default));
722 pos_type Paragraph::beginOfBody() const
724 return begin_of_body_;
728 void Paragraph::setBeginOfBody()
730 if (layout()->labeltype != LABEL_MANUAL) {
735 // Unroll the first two cycles of the loop
736 // and remember the previous character to
737 // remove unnecessary getChar() calls
739 pos_type end = size();
740 if (i < end && !isNewline(i)) {
742 char_type previous_char = 0;
745 previous_char = text_[i];
748 while (i < end && previous_char != ' ') {
753 previous_char = temp;
763 // returns -1 if inset not found
764 int Paragraph::getPositionOfInset(InsetBase const * inset) const
767 InsetList::const_iterator it = insetlist.begin();
768 InsetList::const_iterator end = insetlist.end();
769 for (; it != end; ++it)
770 if (it->inset == inset)
776 InsetBibitem * Paragraph::bibitem() const
778 if (!insetlist.empty()) {
779 InsetBase * inset = insetlist.begin()->inset;
780 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
781 return static_cast<InsetBibitem *>(inset);
787 bool Paragraph::forceDefaultParagraphs() const
789 return inInset() && inInset()->forceDefaultParagraphs(0);
795 // paragraphs inside floats need different alignment tags to avoid
798 bool noTrivlistCentering(InsetBase::Code code)
800 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
804 string correction(string const & orig)
806 if (orig == "flushleft")
807 return "raggedright";
808 if (orig == "flushright")
810 if (orig == "center")
816 string const corrected_env(string const & suffix, string const & env,
817 InsetBase::Code code)
819 string output = suffix + "{";
820 if (noTrivlistCentering(code))
821 output += correction(env);
825 if (suffix == "\\begin")
831 int adjust_column_count(string const & str, int oldcol)
833 if (!contains(str, "\n"))
834 return oldcol + str.size();
837 return rsplit(str, tmp, '\n').size();
844 // This could go to ParagraphParameters if we want to
845 int Paragraph::startTeXParParams(BufferParams const & bparams,
846 odocstream & os, bool moving_arg) const
850 if (params().noindent()) {
855 switch (params().align()) {
857 case LYX_ALIGN_BLOCK:
858 case LYX_ALIGN_LAYOUT:
859 case LYX_ALIGN_SPECIAL:
862 case LYX_ALIGN_RIGHT:
863 case LYX_ALIGN_CENTER:
871 switch (params().align()) {
873 case LYX_ALIGN_BLOCK:
874 case LYX_ALIGN_LAYOUT:
875 case LYX_ALIGN_SPECIAL:
877 case LYX_ALIGN_LEFT: {
879 if (getParLanguage(bparams)->babel() != "hebrew")
880 output = corrected_env("\\begin", "flushleft", ownerCode());
882 output = corrected_env("\\begin", "flushright", ownerCode());
883 os << from_ascii(output);
884 column = adjust_column_count(output, column);
886 } case LYX_ALIGN_RIGHT: {
888 if (getParLanguage(bparams)->babel() != "hebrew")
889 output = corrected_env("\\begin", "flushright", ownerCode());
891 output = corrected_env("\\begin", "flushleft", ownerCode());
892 os << from_ascii(output);
893 column = adjust_column_count(output, column);
895 } case LYX_ALIGN_CENTER: {
897 output = corrected_env("\\begin", "center", ownerCode());
898 os << from_ascii(output);
899 column = adjust_column_count(output, column);
908 // This could go to ParagraphParameters if we want to
909 int Paragraph::endTeXParParams(BufferParams const & bparams,
910 odocstream & os, bool moving_arg) const
914 switch (params().align()) {
916 case LYX_ALIGN_BLOCK:
917 case LYX_ALIGN_LAYOUT:
918 case LYX_ALIGN_SPECIAL:
921 case LYX_ALIGN_RIGHT:
922 case LYX_ALIGN_CENTER:
930 switch (params().align()) {
932 case LYX_ALIGN_BLOCK:
933 case LYX_ALIGN_LAYOUT:
934 case LYX_ALIGN_SPECIAL:
936 case LYX_ALIGN_LEFT: {
938 if (getParLanguage(bparams)->babel() != "hebrew")
939 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
941 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
942 os << from_ascii(output);
943 column = adjust_column_count(output, column);
945 } case LYX_ALIGN_RIGHT: {
947 if (getParLanguage(bparams)->babel() != "hebrew")
948 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
950 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
951 os << from_ascii(output);
952 column = adjust_column_count(output, column);
954 } case LYX_ALIGN_CENTER: {
956 output = corrected_env("\n\\par\\end", "center", ownerCode());
957 os << from_ascii(output);
958 column = adjust_column_count(output, column);
967 // This one spits out the text of the paragraph
968 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
969 BufferParams const & bparams,
970 LyXFont const & outerfont,
971 odocstream & os, TexRow & texrow,
972 OutputParams const & runparams) const
974 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
976 bool return_value = false;
980 // well we have to check if we are in an inset with unlimited
981 // length (all in one row) if that is true then we don't allow
982 // any special options in the paragraph and also we don't allow
983 // any environment other then "Standard" to be valid!
984 bool asdefault = forceDefaultParagraphs();
987 style = bparams.getLyXTextClass().defaultLayout();
994 LaTeXFeatures features(buf, bparams, runparams);
996 // output change tracking marks only if desired,
997 // if dvipost is installed,
998 // and with dvi/ps (other formats don't work)
999 bool const output = bparams.outputChanges
1000 && runparams.flavor == OutputParams::LATEX
1001 && features.isAvailable("dvipost");
1003 // Maybe we have to create a optional argument.
1004 pos_type body_pos = beginOfBody();
1005 unsigned int column = 0;
1008 // the optional argument is kept in curly brackets in
1009 // case it contains a ']'
1012 basefont = getLabelFont(bparams, outerfont);
1014 basefont = getLayoutFont(bparams, outerfont);
1017 // Which font is currently active?
1018 LyXFont running_font(basefont);
1019 // Do we have an open font change?
1020 bool open_font = false;
1022 Change::Type runningChangeType = Change::UNCHANGED;
1024 texrow.start(id(), 0);
1026 // if the paragraph is empty, the loop will not be entered at all
1028 if (style->isCommand()) {
1033 column += startTeXParParams(bparams, os,
1034 runparams.moving_arg);
1037 for (pos_type i = 0; i < size(); ++i) {
1039 // First char in paragraph or after label?
1040 if (i == body_pos) {
1043 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1046 basefont = getLayoutFont(bparams, outerfont);
1047 running_font = basefont;
1051 if (style->isCommand()) {
1057 column += startTeXParParams(bparams, os,
1058 runparams.moving_arg);
1061 value_type c = getChar(i);
1063 // Fully instantiated font
1064 LyXFont font = getFont(bparams, i, outerfont);
1066 LyXFont const last_font = running_font;
1068 // Spaces at end of font change are simulated to be
1069 // outside font change, i.e. we write "\textXX{text} "
1070 // rather than "\textXX{text }". (Asger)
1071 if (open_font && c == ' ' && i <= size() - 2) {
1072 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
1073 if (next_font != running_font && next_font != font) {
1078 // We end font definition before blanks
1080 (font != running_font ||
1081 font.language() != running_font.language()))
1083 column += running_font.latexWriteEndChanges(os,
1085 (i == body_pos-1) ? basefont : font);
1086 running_font = basefont;
1090 // Blanks are printed before start of fontswitch
1092 // Do not print the separation of the optional argument
1093 if (i != body_pos - 1) {
1094 pimpl_->simpleTeXBlanks(os, texrow, i,
1095 column, font, *style);
1099 // Do we need to change font?
1100 if ((font != running_font ||
1101 font.language() != running_font.language()) &&
1104 column += font.latexWriteStartChanges(os, basefont,
1106 running_font = font;
1110 Change::Type changeType = pimpl_->lookupChange(i).type;
1112 column += Changes::latexMarkChange(os, runningChangeType,
1113 changeType, output);
1114 runningChangeType = changeType;
1116 // do not output text which is marked deleted
1117 // if change tracking output is not desired
1118 if (output || runningChangeType != Change::DELETED) {
1119 OutputParams rp = runparams;
1120 rp.free_spacing = style->free_spacing;
1121 rp.local_font = &font;
1122 rp.intitle = style->intitle;
1123 pimpl_->simpleTeXSpecialChars(buf, bparams,
1126 basefont, outerfont, open_font,
1128 *style, i, column, c);
1132 column += Changes::latexMarkChange(os,
1133 runningChangeType, Change::UNCHANGED, output);
1135 // If we have an open font definition, we have to close it
1137 #ifdef FIXED_LANGUAGE_END_DETECTION
1140 .latexWriteEndChanges(os, basefont,
1141 next_->getFont(bparams,
1144 running_font.latexWriteEndChanges(os, basefont,
1148 #ifdef WITH_WARNINGS
1149 //#warning For now we ALWAYS have to close the foreign font settings if they are
1150 //#warning there as we start another \selectlanguage with the next paragraph if
1151 //#warning we are in need of this. This should be fixed sometime (Jug)
1153 running_font.latexWriteEndChanges(os, basefont, basefont);
1157 // Needed if there is an optional argument but no contents.
1158 if (body_pos > 0 && body_pos == size()) {
1160 return_value = false;
1164 column += endTeXParParams(bparams, os, runparams.moving_arg);
1167 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1168 return return_value;
1174 // checks, if newcol chars should be put into this line
1175 // writes newline, if necessary.
1176 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1177 string::size_type newcol)
1180 if (colcount > lyxrc.ascii_linelen) {
1182 colcount = newcol; // assume write after this call
1197 string tag_name(PAR_TAG const & pt) {
1199 case PAR_NONE: return "!-- --";
1200 case TT: return "tt";
1201 case SF: return "sf";
1202 case BF: return "bf";
1203 case IT: return "it";
1204 case SL: return "sl";
1205 case EM: return "em";
1212 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1214 p1 = static_cast<PAR_TAG>(p1 | p2);
1219 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1221 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1227 bool Paragraph::emptyTag() const
1229 for (pos_type i = 0; i < size(); ++i) {
1231 InsetBase const * inset = getInset(i);
1232 InsetBase::Code lyx_code = inset->lyxCode();
1233 if (lyx_code != InsetBase::TOC_CODE &&
1234 lyx_code != InsetBase::INCLUDE_CODE &&
1235 lyx_code != InsetBase::GRAPHICS_CODE &&
1236 lyx_code != InsetBase::ERT_CODE &&
1237 lyx_code != InsetBase::FLOAT_CODE &&
1238 lyx_code != InsetBase::TABULAR_CODE) {
1242 value_type c = getChar(i);
1243 if (c != ' ' && c != '\t')
1251 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1253 for (pos_type i = 0; i < size(); ++i) {
1255 InsetBase const * inset = getInset(i);
1256 InsetBase::Code lyx_code = inset->lyxCode();
1257 if (lyx_code == InsetBase::LABEL_CODE) {
1258 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1259 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
1268 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1271 for (i = 0; i < size(); ++i) {
1273 InsetBase const * inset = getInset(i);
1274 inset->docbook(buf, os, runparams);
1276 value_type c = getChar(i);
1279 os << sgml::escapeChar(c);
1286 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1290 for (pos_type i = initial; i < size(); ++i) {
1291 LyXFont font = getFont(buf.params(), i, outerfont);
1294 if (i != initial && font != font_old)
1303 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1305 OutputParams const & runparams,
1306 LyXFont const & outerfont,
1307 pos_type initial) const
1309 bool emph_flag = false;
1311 LyXLayout_ptr const & style = layout();
1313 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1315 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1318 // parsing main loop
1319 for (pos_type i = initial; i < size(); ++i) {
1320 LyXFont font = getFont(buf.params(), i, outerfont);
1322 // handle <emphasis> tag
1323 if (font_old.emph() != font.emph()) {
1324 if (font.emph() == LyXFont::ON) {
1327 } else if (i != initial) {
1328 os << "</emphasis>";
1334 InsetBase const * inset = getInset(i);
1335 inset->docbook(buf, os, runparams);
1337 value_type c = getChar(i);
1339 if (style->pass_thru)
1342 os << sgml::escapeChar(c);
1348 os << "</emphasis>";
1351 if (style->free_spacing)
1353 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1358 bool Paragraph::isNewline(pos_type pos) const
1361 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1365 bool Paragraph::isLineSeparator(pos_type pos) const
1367 value_type const c = getChar(pos);
1368 return isLineSeparatorChar(c)
1369 || (c == Paragraph::META_INSET && getInset(pos) &&
1370 getInset(pos)->isLineSeparator());
1374 /// Used by the spellchecker
1375 bool Paragraph::isLetter(pos_type pos) const
1378 return getInset(pos)->isLetter();
1380 value_type const c = getChar(pos);
1381 return isLetterChar(c) || isDigit(c);
1387 Paragraph::getParLanguage(BufferParams const & bparams) const
1390 return getFirstFontSettings(bparams).language();
1391 #ifdef WITH_WARNINGS
1392 #warning FIXME we should check the prev par as well (Lgb)
1394 return bparams.language;
1398 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1400 return lyxrc.rtl_support
1401 && getParLanguage(bparams)->rightToLeft()
1402 && ownerCode() != InsetBase::ERT_CODE;
1406 void Paragraph::changeLanguage(BufferParams const & bparams,
1407 Language const * from, Language const * to)
1409 // change language including dummy font change at the end
1410 for (pos_type i = 0; i <= size(); ++i) {
1411 LyXFont font = getFontSettings(bparams, i);
1412 if (font.language() == from) {
1413 font.setLanguage(to);
1420 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1422 Language const * doc_language = bparams.language;
1423 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1424 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1426 for (; cit != end; ++cit)
1427 if (cit->font().language() != ignore_language &&
1428 cit->font().language() != latex_language &&
1429 cit->font().language() != doc_language)
1435 // Convert the paragraph to a string.
1436 // Used for building the table of contents
1437 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1439 OutputParams runparams;
1440 return asString(buffer, runparams, label);
1444 docstring const Paragraph::asString(Buffer const & buffer,
1445 OutputParams const & runparams,
1450 if (label && !params().labelString().empty())
1451 s += params().labelString() + ' ';
1453 for (pos_type i = 0; i < size(); ++i) {
1454 value_type c = getChar(i);
1457 else if (c == META_INSET &&
1458 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1460 getInset(i)->plaintext(buffer, os, runparams);
1461 s += subst(STRCONV(os.str()),'\n',' ');
1467 // This should really be done by the caller and not here.
1468 docstring ret = asString(buffer, runparams, 0, size(), label);
1469 return subst(ret, '\n', ' ');
1474 docstring const Paragraph::asString(Buffer const & buffer,
1475 pos_type beg, pos_type end, bool label) const
1478 OutputParams const runparams;
1479 return asString(buffer, runparams, beg, end, label);
1483 docstring const Paragraph::asString(Buffer const & buffer,
1484 OutputParams const & runparams,
1485 pos_type beg, pos_type end, bool label) const
1487 lyx::odocstringstream os;
1489 if (beg == 0 && label && !params().labelString().empty())
1490 os << params().labelString() << ' ';
1492 for (pos_type i = beg; i < end; ++i) {
1493 value_type const c = getUChar(buffer.params(), i);
1496 else if (c == META_INSET)
1497 getInset(i)->textString(buffer, os, runparams);
1504 void Paragraph::setInsetOwner(InsetBase * inset)
1506 pimpl_->inset_owner = inset;
1510 Change const Paragraph::lookupChange(pos_type pos) const
1512 BOOST_ASSERT(pos <= size());
1513 return pimpl_->lookupChange(pos);
1517 bool Paragraph::isChanged(pos_type start, pos_type end) const
1519 return pimpl_->isChanged(start, end);
1523 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
1525 return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
1529 void Paragraph::setChange(Change const & change)
1531 pimpl_->setChange(change);
1535 void Paragraph::setChange(pos_type pos, Change const & change)
1537 pimpl_->setChange(pos, change);
1541 void Paragraph::acceptChanges(pos_type start, pos_type end)
1543 return pimpl_->acceptChanges(start, end);
1547 void Paragraph::rejectChanges(pos_type start, pos_type end)
1549 return pimpl_->rejectChanges(start, end);
1553 int Paragraph::id() const
1559 LyXLayout_ptr const & Paragraph::layout() const
1565 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1567 layout_ = new_layout;
1571 InsetBase * Paragraph::inInset() const
1573 return pimpl_->inset_owner;
1577 InsetBase::Code Paragraph::ownerCode() const
1579 return pimpl_->inset_owner
1580 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1584 void Paragraph::clearContents()
1590 ParagraphParameters & Paragraph::params()
1592 return pimpl_->params;
1596 ParagraphParameters const & Paragraph::params() const
1598 return pimpl_->params;
1602 bool Paragraph::isFreeSpacing() const
1604 if (layout()->free_spacing)
1607 // for now we just need this, later should we need this in some
1608 // other way we can always add a function to InsetBase too.
1609 return ownerCode() == InsetBase::ERT_CODE;
1613 bool Paragraph::allowEmpty() const
1615 if (layout()->keepempty)
1617 return ownerCode() == InsetBase::ERT_CODE;
1621 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1623 if (!Encodings::is_arabic(c))
1624 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1625 // FIXME UNICODE What does this do?
1626 return c + (0xb0 - '0');
1630 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1631 value_type next_char = ' ';
1633 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1634 value_type const par_char = getChar(i);
1635 if (!Encodings::isComposeChar_arabic(par_char)) {
1636 next_char = par_char;
1641 if (Encodings::is_arabic(next_char)) {
1642 if (Encodings::is_arabic(prev_char) &&
1643 !Encodings::is_arabic_special(prev_char))
1644 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1646 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1648 if (Encodings::is_arabic(prev_char) &&
1649 !Encodings::is_arabic_special(prev_char))
1650 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1652 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1657 bool Paragraph::hfillExpansion(Row const & row, pos_type pos) const
1662 // at the end of a row it does not count
1663 // unless another hfill exists on the line
1664 if (pos >= row.endpos()) {
1665 for (pos_type i = row.pos(); i < pos && !isHfill(i); ++i)
1669 // at the beginning of a row it does not count, if it is not
1670 // the first row of a paragaph
1674 // in some labels it does not count
1675 if (layout()->margintype != MARGIN_MANUAL && pos < beginOfBody())
1678 // if there is anything between the first char of the row and
1679 // the specified position that is not a newline and not a hfill,
1680 // the hfill will count, otherwise not
1681 pos_type i = row.pos();
1682 while (i < pos && (isNewline(i) || isHfill(i)))