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"
48 #include <boost/tuple/tuple.hpp>
49 #include <boost/bind.hpp>
57 using lyx::support::subst;
65 using std::ostringstream;
68 ParagraphList::ParagraphList()
72 Paragraph::Paragraph()
73 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
80 Paragraph::Paragraph(Paragraph const & par)
81 : itemdepth(par.itemdepth), insetlist(par.insetlist),
83 rows_(par.rows_), layout_(par.layout_),
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 layout_ = par.layout();
111 begin_of_body_ = par.begin_of_body_;
114 pimpl_ = new Pimpl(*par.pimpl_, this);
120 Paragraph::~Paragraph()
124 //lyxerr << "Paragraph::paragraph_id = "
125 // << Paragraph::paragraph_id << endl;
129 void Paragraph::write(Buffer const & buf, ostream & os,
130 BufferParams const & bparams,
131 depth_type & dth) const
133 // The beginning or end of a deeper (i.e. nested) area?
134 if (dth != params().depth()) {
135 if (params().depth() > dth) {
136 while (params().depth() > dth) {
137 os << "\n\\begin_deeper";
141 while (params().depth() < dth) {
142 os << "\n\\end_deeper";
148 // First write the layout
149 os << "\n\\begin_layout " << layout()->name() << '\n';
153 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
155 Change running_change = Change(Change::UNCHANGED);
156 lyx::time_type const curtime(lyx::current_time());
159 for (pos_type i = 0; i < size(); ++i) {
161 Change change = pimpl_->lookupChangeFull(i);
162 Changes::lyxMarkChange(os, column, curtime, running_change, change);
163 running_change = change;
165 // Write font changes
166 LyXFont font2 = getFontSettings(bparams, i);
167 if (font2 != font1) {
168 font2.lyxWriteChanges(font1, os);
173 value_type const c = getChar(i);
177 InsetBase const * inset = getInset(i);
179 if (inset->directWrite()) {
180 // international char, let it write
181 // code directly so it's shorter in
183 inset->write(buf, os);
187 os << "\\begin_inset ";
188 inset->write(buf, os);
189 os << "\n\\end_inset\n\n";
195 os << "\n\\backslash\n";
199 if (i + 1 < size() && getChar(i + 1) == ' ') {
206 if ((column > 70 && c == ' ')
211 // this check is to amend a bug. LyX sometimes
212 // inserts '\0' this could cause problems.
216 lyxerr << "ERROR (Paragraph::writeFile):"
217 " NULL char in structure." << endl;
223 // to make reading work properly
225 running_change = pimpl_->lookupChange(0);
226 Changes::lyxMarkChange(os, column, curtime,
227 Change(Change::UNCHANGED), running_change);
229 Changes::lyxMarkChange(os, column, curtime,
230 running_change, Change(Change::UNCHANGED));
232 os << "\n\\end_layout\n";
236 void Paragraph::validate(LaTeXFeatures & features) const
238 pimpl_->validate(features, *layout());
242 void Paragraph::eraseIntern(lyx::pos_type pos)
244 pimpl_->eraseIntern(pos);
248 bool Paragraph::erase(pos_type pos)
250 return pimpl_->erase(pos);
254 int Paragraph::erase(pos_type start, pos_type end)
256 return pimpl_->erase(start, end);
260 void Paragraph::insert(pos_type start, string const & str,
261 LyXFont const & font)
263 for (size_t i = 0, n = str.size(); i != n ; ++i)
264 insertChar(start + i, str[i], font);
268 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
271 pimpl_->insertChar(pos, c, change);
275 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
276 LyXFont const & font, Change change)
278 pimpl_->insertChar(pos, c, change);
283 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
285 pimpl_->insertInset(pos, inset, change);
289 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
290 LyXFont const & font, Change change)
292 pimpl_->insertInset(pos, inset, change);
297 bool Paragraph::insetAllowed(InsetBase_code code)
299 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
303 // Gets uninstantiated font setting at position.
304 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
308 lyxerr << " pos: " << pos << " size: " << size() << endl;
309 BOOST_ASSERT(pos <= size());
312 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
313 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
314 for (; cit != end; ++cit)
315 if (cit->pos() >= pos)
321 if (pos == size() && !empty())
322 return getFontSettings(bparams, pos - 1);
324 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
328 FontSpan Paragraph::fontSpan(lyx::pos_type pos) const
330 BOOST_ASSERT(pos <= size());
331 lyx::pos_type start = 0;
333 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
334 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
335 for (; cit != end; ++cit) {
336 if (cit->pos() >= pos)
337 return FontSpan(start, cit->pos());
338 start = cit->pos() + 1;
341 // This should not happen, but if so, we take no chances.
342 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
344 return FontSpan(pos, pos);
348 // Gets uninstantiated font setting at position 0
349 LyXFont const Paragraph::getFirstFontSettings() const
351 if (!empty() && !pimpl_->fontlist.empty())
352 return pimpl_->fontlist[0].font();
354 return LyXFont(LyXFont::ALL_INHERIT);
358 // Gets the fully instantiated font at a given position in a paragraph
359 // This is basically the same function as LyXText::GetFont() in text2.C.
360 // The difference is that this one is used for generating the LaTeX file,
361 // and thus cosmetic "improvements" are disallowed: This has to deliver
362 // the true picture of the buffer. (Asger)
363 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
364 LyXFont const & outerfont) const
366 BOOST_ASSERT(pos >= 0);
368 LyXLayout_ptr const & lout = layout();
370 pos_type const body_pos = beginOfBody();
374 layoutfont = lout->labelfont;
376 layoutfont = lout->font;
378 LyXFont font = getFontSettings(bparams, pos);
379 font.realize(layoutfont);
380 font.realize(outerfont);
381 font.realize(bparams.getLyXTextClass().defaultfont());
387 LyXFont const Paragraph::getLabelFont
388 (BufferParams const & bparams, LyXFont const & outerfont) const
390 LyXFont tmpfont = layout()->labelfont;
391 tmpfont.setLanguage(getParLanguage(bparams));
392 tmpfont.realize(outerfont);
393 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
398 LyXFont const Paragraph::getLayoutFont
399 (BufferParams const & bparams, LyXFont const & outerfont) const
401 LyXFont tmpfont = layout()->font;
402 tmpfont.setLanguage(getParLanguage(bparams));
403 tmpfont.realize(outerfont);
404 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
409 /// Returns the height of the highest font in range
410 LyXFont_size Paragraph::highestFontInRange
411 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
413 if (pimpl_->fontlist.empty())
416 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
417 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
418 for (; end_it != end; ++end_it) {
419 if (end_it->pos() >= endpos)
426 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
427 for (; cit != end; ++cit) {
428 if (cit->pos() >= startpos)
432 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
433 for (; cit != end_it; ++cit) {
434 LyXFont::FONT_SIZE size = cit->font().size();
435 if (size == LyXFont::INHERIT_SIZE)
437 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
444 Paragraph::value_type
445 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
447 value_type c = getChar(pos);
448 if (!lyxrc.rtl_support)
478 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
485 void Paragraph::setFont(pos_type pos, LyXFont const & font)
487 BOOST_ASSERT(pos <= size());
489 // First, reduce font against layout/label font
490 // Update: The setCharFont() routine in text2.C already
491 // reduces font, so we don't need to do that here. (Asger)
492 // No need to simplify this because it will disappear
493 // in a new kernel. (Asger)
494 // Next search font table
496 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
497 Pimpl::FontList::iterator it = beg;
498 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
499 for (; it != endit; ++it) {
500 if (it->pos() >= pos)
503 size_t const i = distance(beg, it);
504 bool notfound = (it == endit);
506 if (!notfound && pimpl_->fontlist[i].font() == font)
509 bool begin = pos == 0 || notfound ||
510 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
511 // Is position pos is a beginning of a font block?
512 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
513 // Is position pos is the end of a font block?
514 if (begin && end) { // A single char block
515 if (i + 1 < pimpl_->fontlist.size() &&
516 pimpl_->fontlist[i + 1].font() == font) {
517 // Merge the singleton block with the next block
518 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
519 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
520 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
521 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
522 // Merge the singleton block with the previous block
523 pimpl_->fontlist[i - 1].pos(pos);
524 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
526 pimpl_->fontlist[i].font(font);
528 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
529 pimpl_->fontlist[i - 1].pos(pos);
531 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
532 Pimpl::FontTable(pos, font));
534 pimpl_->fontlist[i].pos(pos - 1);
535 if (!(i + 1 < pimpl_->fontlist.size() &&
536 pimpl_->fontlist[i + 1].font() == font))
537 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
538 Pimpl::FontTable(pos, font));
539 } else { // The general case. The block is splitted into 3 blocks
540 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
541 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
542 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
543 Pimpl::FontTable(pos, font));
548 void Paragraph::makeSameLayout(Paragraph const & par)
550 layout(par.layout());
552 params() = par.params();
556 int Paragraph::stripLeadingSpaces()
562 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
563 // Set Change::Type to Change::INSERTED to quietly remove it
564 setChange(0, Change::INSERTED);
573 bool Paragraph::hasSameLayout(Paragraph const & par) const
575 return par.layout() == layout() && params().sameLayout(par.params());
579 Paragraph::depth_type Paragraph::getDepth() const
581 return params().depth();
585 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
587 if (layout()->isEnvironment())
588 return params().depth() + 1;
590 return params().depth();
594 char Paragraph::getAlign() const
596 return params().align();
600 string const & Paragraph::getLabelstring() const
602 return params().labelString();
606 // the next two functions are for the manual labels
607 string const Paragraph::getLabelWidthString() const
609 if (!params().labelWidthString().empty())
610 return params().labelWidthString();
612 return _("Senseless with this layout!");
616 void Paragraph::setLabelWidthString(string const & s)
618 params().labelWidthString(s);
622 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
625 params().labelWidthString(string());
626 params().align(LYX_ALIGN_LAYOUT);
627 params().spacing(Spacing(Spacing::Default));
631 pos_type Paragraph::beginOfBody() const
633 return begin_of_body_;
637 void Paragraph::setBeginOfBody()
639 if (layout()->labeltype != LABEL_MANUAL) {
644 // Unroll the first two cycles of the loop
645 // and remember the previous character to
646 // remove unnecessary getChar() calls
648 pos_type end = size();
649 if (i < end && !isNewline(i)) {
651 char previous_char = 0;
654 previous_char = text_[i];
657 while (i < end && previous_char != ' ') {
662 previous_char = temp;
672 // returns -1 if inset not found
673 int Paragraph::getPositionOfInset(InsetBase const * inset) const
676 InsetList::const_iterator it = insetlist.begin();
677 InsetList::const_iterator end = insetlist.end();
678 for (; it != end; ++it)
679 if (it->inset == inset)
685 InsetBibitem * Paragraph::bibitem() const
687 if (!insetlist.empty()) {
688 InsetBase * inset = insetlist.begin()->inset;
689 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
690 return static_cast<InsetBibitem *>(inset);
696 bool Paragraph::forceDefaultParagraphs() const
698 return inInset() && inInset()->forceDefaultParagraphs(inInset());
704 // paragraphs inside floats need different alignment tags to avoid
707 bool noTrivlistCentering(InsetBase::Code code)
709 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
713 string correction(string const & orig)
715 if (orig == "flushleft")
716 return "raggedright";
717 if (orig == "flushright")
719 if (orig == "center")
725 string const corrected_env(string const & suffix, string const & env,
726 InsetBase::Code code)
728 string output = suffix + "{";
729 if (noTrivlistCentering(code))
730 output += correction(env);
739 // This could go to ParagraphParameters if we want to
740 int Paragraph::startTeXParParams(BufferParams const & bparams,
741 ostream & os, bool moving_arg) const
745 if (params().noindent()) {
750 switch (params().align()) {
752 case LYX_ALIGN_BLOCK:
753 case LYX_ALIGN_LAYOUT:
754 case LYX_ALIGN_SPECIAL:
757 case LYX_ALIGN_RIGHT:
758 case LYX_ALIGN_CENTER:
766 switch (params().align()) {
768 case LYX_ALIGN_BLOCK:
769 case LYX_ALIGN_LAYOUT:
770 case LYX_ALIGN_SPECIAL:
772 case LYX_ALIGN_LEFT: {
774 if (getParLanguage(bparams)->babel() != "hebrew")
775 output = corrected_env("\\begin", "flushleft", ownerCode());
777 output = corrected_env("\\begin", "flushright", ownerCode());
779 column += output.size();
781 } case LYX_ALIGN_RIGHT: {
783 if (getParLanguage(bparams)->babel() != "hebrew")
784 output = corrected_env("\\begin", "flushright", ownerCode());
786 output = corrected_env("\\begin", "flushleft", ownerCode());
788 column += output.size();
790 } case LYX_ALIGN_CENTER: {
792 output = corrected_env("\\begin", "center", ownerCode());
794 column += output.size();
803 // This could go to ParagraphParameters if we want to
804 int Paragraph::endTeXParParams(BufferParams const & bparams,
805 ostream & os, bool moving_arg) const
809 switch (params().align()) {
811 case LYX_ALIGN_BLOCK:
812 case LYX_ALIGN_LAYOUT:
813 case LYX_ALIGN_SPECIAL:
816 case LYX_ALIGN_RIGHT:
817 case LYX_ALIGN_CENTER:
825 switch (params().align()) {
827 case LYX_ALIGN_BLOCK:
828 case LYX_ALIGN_LAYOUT:
829 case LYX_ALIGN_SPECIAL:
831 case LYX_ALIGN_LEFT: {
833 if (getParLanguage(bparams)->babel() != "hebrew")
834 output = corrected_env("\\par\\end", "flushleft", ownerCode());
836 output = corrected_env("\\par\\end", "flushright", ownerCode());
838 column += output.size();
840 } case LYX_ALIGN_RIGHT: {
842 if (getParLanguage(bparams)->babel() != "hebrew")
843 output = corrected_env("\\par\\end", "flushright", ownerCode());
845 output = corrected_env("\\par\\end", "flushleft", ownerCode());
847 column += output.size();
849 } case LYX_ALIGN_CENTER: {
851 output = corrected_env("\\par\\end", "center", ownerCode());
853 column += output.size();
862 // This one spits out the text of the paragraph
863 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
864 BufferParams const & bparams,
865 LyXFont const & outerfont,
866 ostream & os, TexRow & texrow,
867 OutputParams const & runparams) const
869 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
871 bool return_value = false;
875 // well we have to check if we are in an inset with unlimited
876 // length (all in one row) if that is true then we don't allow
877 // any special options in the paragraph and also we don't allow
878 // any environment other then "Standard" to be valid!
879 bool asdefault = forceDefaultParagraphs();
882 style = bparams.getLyXTextClass().defaultLayout();
889 LaTeXFeatures features(buf, bparams, runparams.nice);
891 // output change tracking marks only if desired,
892 // if dvipost is installed,
893 // and with dvi/ps (other formats don't work)
894 bool const output = bparams.output_changes
895 && runparams.flavor == OutputParams::LATEX
896 && features.isAvailable("dvipost");
898 // Maybe we have to create a optional argument.
899 pos_type body_pos = beginOfBody();
900 unsigned int column = 0;
903 // the optional argument is kept in curly brackets in
904 // case it contains a ']'
907 basefont = getLabelFont(bparams, outerfont);
909 basefont = getLayoutFont(bparams, outerfont);
912 // Which font is currently active?
913 LyXFont running_font(basefont);
914 // Do we have an open font change?
915 bool open_font = false;
917 Change::Type running_change = Change::UNCHANGED;
919 texrow.start(id(), 0);
921 // if the paragraph is empty, the loop will not be entered at all
923 if (style->isCommand()) {
928 column += startTeXParParams(bparams, os,
929 runparams.moving_arg);
932 for (pos_type i = 0; i < size(); ++i) {
934 // First char in paragraph or after label?
938 column += running_font.latexWriteEndChanges(os, basefont, basefont);
941 basefont = getLayoutFont(bparams, outerfont);
942 running_font = basefont;
946 if (style->isCommand()) {
952 column += startTeXParParams(bparams, os,
953 runparams.moving_arg);
956 value_type c = getChar(i);
958 // Fully instantiated font
959 LyXFont font = getFont(bparams, i, outerfont);
961 LyXFont const last_font = running_font;
963 // Spaces at end of font change are simulated to be
964 // outside font change, i.e. we write "\textXX{text} "
965 // rather than "\textXX{text }". (Asger)
966 if (open_font && c == ' ' && i <= size() - 2) {
967 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
968 if (next_font != running_font && next_font != font) {
973 // We end font definition before blanks
975 (font != running_font ||
976 font.language() != running_font.language()))
978 column += running_font.latexWriteEndChanges(os,
980 (i == body_pos-1) ? basefont : font);
981 running_font = basefont;
985 // Blanks are printed before start of fontswitch
987 // Do not print the separation of the optional argument
988 if (i != body_pos - 1) {
989 pimpl_->simpleTeXBlanks(os, texrow, i,
990 column, font, *style);
994 // Do we need to change font?
995 if ((font != running_font ||
996 font.language() != running_font.language()) &&
999 column += font.latexWriteStartChanges(os, basefont,
1001 running_font = font;
1005 Change::Type change = pimpl_->lookupChange(i);
1007 column += Changes::latexMarkChange(os, running_change,
1009 running_change = change;
1011 // do not output text which is marked deleted
1012 // if change tracking output is not desired
1013 if (output || running_change != Change::DELETED) {
1014 OutputParams rp = runparams;
1015 rp.free_spacing = style->free_spacing;
1016 rp.local_language = font.language();
1017 rp.intitle = style->intitle;
1018 pimpl_->simpleTeXSpecialChars(buf, bparams,
1021 basefont, outerfont, open_font,
1023 *style, i, column, c);
1027 column += Changes::latexMarkChange(os,
1028 running_change, Change::UNCHANGED, output);
1030 // If we have an open font definition, we have to close it
1032 #ifdef FIXED_LANGUAGE_END_DETECTION
1035 .latexWriteEndChanges(os, basefont,
1036 next_->getFont(bparams,
1039 running_font.latexWriteEndChanges(os, basefont,
1043 #ifdef WITH_WARNINGS
1044 //#warning For now we ALWAYS have to close the foreign font settings if they are
1045 //#warning there as we start another \selectlanguage with the next paragraph if
1046 //#warning we are in need of this. This should be fixed sometime (Jug)
1048 running_font.latexWriteEndChanges(os, basefont, basefont);
1052 // Needed if there is an optional argument but no contents.
1053 if (body_pos > 0 && body_pos == size()) {
1055 return_value = false;
1059 column += endTeXParParams(bparams, os, runparams.moving_arg);
1062 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1063 return return_value;
1069 // checks, if newcol chars should be put into this line
1070 // writes newline, if necessary.
1071 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1072 string::size_type newcol)
1075 if (colcount > lyxrc.ascii_linelen) {
1077 colcount = newcol; // assume write after this call
1092 string tag_name(PAR_TAG const & pt) {
1094 case PAR_NONE: return "!-- --";
1095 case TT: return "tt";
1096 case SF: return "sf";
1097 case BF: return "bf";
1098 case IT: return "it";
1099 case SL: return "sl";
1100 case EM: return "em";
1107 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1109 p1 = static_cast<PAR_TAG>(p1 | p2);
1114 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1116 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1122 // Handle internal paragraph parsing -- layout already processed.
1123 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1125 LyXFont const & outerfont,
1126 OutputParams const & runparams,
1127 lyx::depth_type /*depth*/) const
1129 LyXLayout_ptr const & style = layout();
1131 string::size_type char_line_count = 5; // Heuristic choice ;-)
1133 // gets paragraph main font
1136 if (style->labeltype == LABEL_MANUAL) {
1137 font_old = style->labelfont;
1140 font_old = style->font;
1144 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1145 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1146 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1149 stack<PAR_TAG> tag_state;
1150 // parsing main loop
1151 for (pos_type i = 0; i < size(); ++i) {
1153 PAR_TAG tag_close = PAR_NONE;
1154 list < PAR_TAG > tag_open;
1156 LyXFont const font = getFont(buf.params(), i, outerfont);
1158 if (font_old.family() != font.family()) {
1159 switch (family_type) {
1160 case LyXFont::SANS_FAMILY:
1163 case LyXFont::TYPEWRITER_FAMILY:
1170 family_type = font.family();
1172 switch (family_type) {
1173 case LyXFont::SANS_FAMILY:
1174 tag_open.push_back(SF);
1176 case LyXFont::TYPEWRITER_FAMILY:
1177 tag_open.push_back(TT);
1184 if (font_old.series() != font.series()) {
1185 switch (series_type) {
1186 case LyXFont::BOLD_SERIES:
1193 series_type = font.series();
1195 switch (series_type) {
1196 case LyXFont::BOLD_SERIES:
1197 tag_open.push_back(BF);
1205 if (font_old.shape() != font.shape()) {
1206 switch (shape_type) {
1207 case LyXFont::ITALIC_SHAPE:
1210 case LyXFont::SLANTED_SHAPE:
1217 shape_type = font.shape();
1219 switch (shape_type) {
1220 case LyXFont::ITALIC_SHAPE:
1221 tag_open.push_back(IT);
1223 case LyXFont::SLANTED_SHAPE:
1224 tag_open.push_back(SL);
1231 if (font_old.emph() != font.emph()) {
1232 if (font.emph() == LyXFont::ON) {
1233 tag_open.push_back(EM);
1242 list < PAR_TAG > temp;
1243 while (!tag_state.empty() && tag_close) {
1244 PAR_TAG k = tag_state.top();
1246 os << "</" << tag_name(k) << '>';
1253 for(list< PAR_TAG >::const_iterator j = temp.begin();
1254 j != temp.end(); ++j) {
1256 os << '<' << tag_name(*j) << '>';
1259 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1260 j != tag_open.end(); ++j) {
1262 os << '<' << tag_name(*j) << '>';
1265 value_type c = getChar(i);
1268 if (c == Paragraph::META_INSET) {
1269 getInset(i)->linuxdoc(buf, os, runparams);
1274 if (style->latexparam() == "CDATA") {
1275 // "TeX"-Mode on == > SGML-Mode on.
1282 boost::tie(ws, str) = sgml::escapeChar(c);
1283 if (ws && !isFreeSpacing()) {
1284 // in freespacing mode, spaces are
1285 // non-breaking characters
1286 if (desc_on) { // if char is ' ' then...
1288 sgmlLineBreak(os, char_line_count, 6);
1292 sgmlLineBreak(os, char_line_count, 1);
1297 char_line_count += str.length();
1303 while (!tag_state.empty()) {
1304 os << "</" << tag_name(tag_state.top()) << '>';
1308 // resets description flag correctly
1310 // <tag> not closed...
1311 sgmlLineBreak(os, char_line_count, 6);
1317 bool Paragraph::emptyTag() const
1319 for (pos_type i = 0; i < size(); ++i) {
1321 InsetBase const * inset = getInset(i);
1322 InsetBase::Code lyx_code = inset->lyxCode();
1323 if (lyx_code != InsetBase::TOC_CODE &&
1324 lyx_code != InsetBase::INCLUDE_CODE &&
1325 lyx_code != InsetBase::GRAPHICS_CODE &&
1326 lyx_code != InsetBase::ERT_CODE &&
1327 lyx_code != InsetBase::FLOAT_CODE &&
1328 lyx_code != InsetBase::TABULAR_CODE) {
1332 value_type c = getChar(i);
1333 if (c != ' ' && c != '\t')
1341 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1343 for (pos_type i = 0; i < size(); ++i) {
1345 InsetBase const * inset = getInset(i);
1346 InsetBase::Code lyx_code = inset->lyxCode();
1347 if (lyx_code == InsetBase::LABEL_CODE) {
1348 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1349 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1358 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1361 for (i = 0; i < size(); ++i) {
1363 InsetBase const * inset = getInset(i);
1364 inset->docbook(buf, os, runparams);
1366 value_type c = getChar(i);
1371 boost::tie(ws, str) = sgml::escapeChar(c);
1380 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1384 for (pos_type i = initial; i < size(); ++i) {
1385 LyXFont font = getFont(buf.params(), i, outerfont);
1388 if (i != initial && font != font_old)
1397 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1399 OutputParams const & runparams,
1400 LyXFont const & outerfont,
1401 pos_type initial) const
1403 bool emph_flag = false;
1405 LyXLayout_ptr const & style = layout();
1407 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1409 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1412 // parsing main loop
1413 for (pos_type i = initial; i < size(); ++i) {
1414 LyXFont font = getFont(buf.params(), i, outerfont);
1416 // handle <emphasis> tag
1417 if (font_old.emph() != font.emph()) {
1418 if (font.emph() == LyXFont::ON) {
1421 } else if (i != initial) {
1422 os << "</emphasis>";
1428 InsetBase const * inset = getInset(i);
1429 inset->docbook(buf, os, runparams);
1431 value_type c = getChar(i);
1434 boost::tie(ws, str) = sgml::escapeChar(c);
1436 if (style->pass_thru)
1445 os << "</emphasis>";
1448 if (style->free_spacing)
1450 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1455 bool Paragraph::isNewline(pos_type pos) const
1458 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1462 bool Paragraph::isLineSeparator(pos_type pos) const
1464 value_type const c = getChar(pos);
1465 return IsLineSeparatorChar(c)
1466 || (c == Paragraph::META_INSET && getInset(pos) &&
1467 getInset(pos)->isLineSeparator());
1471 /// Used by the spellchecker
1472 bool Paragraph::isLetter(pos_type pos) const
1475 return getInset(pos)->isLetter();
1477 value_type const c = getChar(pos);
1478 return IsLetterChar(c) || IsDigit(c);
1484 Paragraph::getParLanguage(BufferParams const & bparams) const
1487 return getFirstFontSettings().language();
1488 #ifdef WITH_WARNINGS
1489 #warning FIXME we should check the prev par as well (Lgb)
1491 return bparams.language;
1495 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1497 return lyxrc.rtl_support
1498 && getParLanguage(bparams)->RightToLeft()
1499 && ownerCode() != InsetBase::ERT_CODE;
1503 void Paragraph::changeLanguage(BufferParams const & bparams,
1504 Language const * from, Language const * to)
1506 for (pos_type i = 0; i < size(); ++i) {
1507 LyXFont font = getFontSettings(bparams, i);
1508 if (font.language() == from) {
1509 font.setLanguage(to);
1516 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1518 Language const * doc_language = bparams.language;
1519 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1520 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1522 for (; cit != end; ++cit)
1523 if (cit->font().language() != ignore_language &&
1524 cit->font().language() != latex_language &&
1525 cit->font().language() != doc_language)
1531 // Convert the paragraph to a string.
1532 // Used for building the table of contents
1533 string const Paragraph::asString(Buffer const & buffer, bool label) const
1535 OutputParams runparams;
1536 return asString(buffer, runparams, label);
1540 string const Paragraph::asString(Buffer const & buffer,
1541 OutputParams const & runparams,
1546 if (label && !params().labelString().empty())
1547 s += params().labelString() + ' ';
1549 for (pos_type i = 0; i < size(); ++i) {
1550 value_type c = getChar(i);
1553 else if (c == META_INSET &&
1554 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1556 getInset(i)->plaintext(buffer, os, runparams);
1557 s += subst(STRCONV(os.str()),'\n',' ');
1563 // This should really be done by the caller and not here.
1564 string ret = asString(buffer, runparams, 0, size(), label);
1565 return subst(ret, '\n', ' ');
1570 string const Paragraph::asString(Buffer const & buffer,
1571 pos_type beg, pos_type end, bool label) const
1574 OutputParams const runparams;
1575 return asString(buffer, runparams, beg, end, label);
1579 string const Paragraph::asString(Buffer const & buffer,
1580 OutputParams const & runparams,
1581 pos_type beg, pos_type end, bool label) const
1585 if (beg == 0 && label && !params().labelString().empty())
1586 os << params().labelString() << ' ';
1588 for (pos_type i = beg; i < end; ++i) {
1589 value_type const c = getUChar(buffer.params(), i);
1592 else if (c == META_INSET)
1593 getInset(i)->plaintext(buffer, os, runparams);
1600 void Paragraph::setInsetOwner(InsetBase * inset)
1602 pimpl_->inset_owner = inset;
1606 void Paragraph::setContentsFromPar(Paragraph const & par)
1608 pimpl_->setContentsFromPar(par);
1612 void Paragraph::trackChanges(Change::Type type)
1614 pimpl_->trackChanges(type);
1618 void Paragraph::untrackChanges()
1620 pimpl_->untrackChanges();
1624 void Paragraph::cleanChanges()
1626 pimpl_->cleanChanges();
1630 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1632 BOOST_ASSERT(empty() || pos < size());
1633 return pimpl_->lookupChange(pos);
1637 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1639 BOOST_ASSERT(empty() || pos < size());
1640 return pimpl_->lookupChangeFull(pos);
1644 bool Paragraph::isChanged(pos_type start, pos_type end) const
1646 return pimpl_->isChanged(start, end);
1650 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1652 return pimpl_->isChangeEdited(start, end);
1656 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1658 pimpl_->setChange(pos, type);
1662 void Paragraph::markErased(bool erased)
1664 pimpl_->markErased(erased);
1668 void Paragraph::acceptChange(pos_type start, pos_type end)
1670 return pimpl_->acceptChange(start, end);
1674 void Paragraph::rejectChange(pos_type start, pos_type end)
1676 return pimpl_->rejectChange(start, end);
1680 int Paragraph::id() const
1686 LyXLayout_ptr const & Paragraph::layout() const
1692 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1694 layout_ = new_layout;
1698 InsetBase * Paragraph::inInset() const
1700 return pimpl_->inset_owner;
1704 InsetBase::Code Paragraph::ownerCode() const
1706 return pimpl_->inset_owner
1707 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1711 void Paragraph::clearContents()
1717 void Paragraph::setChar(pos_type pos, value_type c)
1723 ParagraphParameters & Paragraph::params()
1725 return pimpl_->params;
1729 ParagraphParameters const & Paragraph::params() const
1731 return pimpl_->params;
1735 bool Paragraph::isFreeSpacing() const
1737 if (layout()->free_spacing)
1740 // for now we just need this, later should we need this in some
1741 // other way we can always add a function to InsetBase too.
1742 return ownerCode() == InsetBase::ERT_CODE;
1746 bool Paragraph::allowEmpty() const
1748 if (layout()->keepempty)
1750 return ownerCode() == InsetBase::ERT_CODE;
1754 Row & Paragraph::getRow(pos_type pos, bool boundary)
1756 BOOST_ASSERT(!rows().empty());
1758 // If boundary is set we should return the row on which
1759 // the character before is inside.
1760 if (pos > 0 && boundary)
1763 RowList::iterator rit = rows_.end();
1764 RowList::iterator const begin = rows_.begin();
1766 for (--rit; rit != begin && rit->pos() > pos; --rit)
1773 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1775 BOOST_ASSERT(!rows().empty());
1777 // If boundary is set we should return the row on which
1778 // the character before is inside.
1779 if (pos > 0 && boundary)
1782 RowList::const_iterator rit = rows_.end();
1783 RowList::const_iterator const begin = rows_.begin();
1785 for (--rit; rit != begin && rit->pos() > pos; --rit)
1792 size_t Paragraph::pos2row(pos_type pos) const
1794 BOOST_ASSERT(!rows().empty());
1796 RowList::const_iterator rit = rows_.end();
1797 RowList::const_iterator const begin = rows_.begin();
1799 for (--rit; rit != begin && rit->pos() > pos; --rit)
1806 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1808 if (!Encodings::is_arabic(c))
1809 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1810 return c + (0xb0 - '0');
1814 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1815 unsigned char next_char = ' ';
1817 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1818 unsigned char const par_char = getChar(i);
1819 if (!Encodings::IsComposeChar_arabic(par_char)) {
1820 next_char = par_char;
1825 if (Encodings::is_arabic(next_char)) {
1826 if (Encodings::is_arabic(prev_char) &&
1827 !Encodings::is_arabic_special(prev_char))
1828 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1830 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1832 if (Encodings::is_arabic(prev_char) &&
1833 !Encodings::is_arabic_special(prev_char))
1834 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1836 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1841 void Paragraph::dump() const
1843 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1844 for (size_t i = 0; i != rows_.size(); ++i) {
1845 lyxerr << " row " << i << ": ";