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"
33 #include "outputparams.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphList_fwd.h"
40 #include "insets/insetbibitem.h"
41 #include "insets/insetoptarg.h"
43 #include "support/lstrings.h"
44 #include "support/textutils.h"
45 #include "support/tostr.h"
47 #include <boost/tuple/tuple.hpp>
48 #include <boost/bind.hpp>
56 using lyx::support::subst;
64 using std::ostringstream;
67 ParagraphList::ParagraphList()
71 Paragraph::Paragraph()
72 : y(0), height(0), begin_of_body_(0),
73 pimpl_(new Paragraph::Pimpl(this))
75 //lyxerr << "sizeof Paragraph::Pimpl: " << sizeof(Paragraph::Pimpl) << endl;
81 Paragraph::Paragraph(Paragraph const & par)
82 : itemdepth(par.itemdepth), insetlist(par.insetlist),
83 rows(par.rows), y(par.y), height(par.height),
84 width(par.width), layout_(par.layout_),
85 text_(par.text_), begin_of_body_(par.begin_of_body_),
86 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
88 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
89 InsetList::iterator it = insetlist.begin();
90 InsetList::iterator end = insetlist.end();
91 for (; it != end; ++it)
92 it->inset = it->inset->clone().release();
96 Paragraph & Paragraph::operator=(Paragraph const & par)
98 // needed as we will destroy the pimpl_ before copying it
100 itemdepth = par.itemdepth;
102 insetlist = par.insetlist;
103 InsetList::iterator it = insetlist.begin();
104 InsetList::iterator end = insetlist.end();
105 for (; it != end; ++it)
106 it->inset = it->inset->clone().release();
112 layout_ = par.layout();
114 begin_of_body_ = par.begin_of_body_;
117 pimpl_ = new Pimpl(*par.pimpl_, this);
123 Paragraph::~Paragraph()
127 //lyxerr << "Paragraph::paragraph_id = "
128 // << Paragraph::paragraph_id << endl;
132 void Paragraph::write(Buffer const & buf, ostream & os,
133 BufferParams const & bparams,
134 depth_type & dth) const
136 // The beginning or end of a deeper (i.e. nested) area?
137 if (dth != params().depth()) {
138 if (params().depth() > dth) {
139 while (params().depth() > dth) {
140 os << "\n\\begin_deeper";
144 while (params().depth() < dth) {
145 os << "\n\\end_deeper";
151 // First write the layout
152 os << "\n\\begin_layout " << layout()->name() << '\n';
156 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
158 Change running_change = Change(Change::UNCHANGED);
159 lyx::time_type const curtime(lyx::current_time());
162 for (pos_type i = 0; i < size(); ++i) {
168 Change change = pimpl_->lookupChangeFull(i);
169 Changes::lyxMarkChange(os, column, curtime, running_change, change);
170 running_change = change;
172 // Write font changes
173 LyXFont font2 = getFontSettings(bparams, i);
174 if (font2 != font1) {
175 font2.lyxWriteChanges(font1, os);
180 value_type const c = getChar(i);
184 InsetBase const * inset = getInset(i);
186 if (inset->directWrite()) {
187 // international char, let it write
188 // code directly so it's shorter in
190 inset->write(buf, os);
192 os << "\n\\begin_inset ";
193 inset->write(buf, os);
194 os << "\n\\end_inset\n\n";
200 os << "\n\\backslash\n";
204 if (i + 1 < size() && getChar(i + 1) == ' ') {
211 if ((column > 70 && c == ' ')
216 // this check is to amend a bug. LyX sometimes
217 // inserts '\0' this could cause problems.
221 lyxerr << "ERROR (Paragraph::writeFile):"
222 " NULL char in structure." << endl;
228 // to make reading work properly
230 running_change = pimpl_->lookupChange(0);
231 Changes::lyxMarkChange(os, column, curtime,
232 Change(Change::UNCHANGED), running_change);
234 Changes::lyxMarkChange(os, column, curtime,
235 running_change, Change(Change::UNCHANGED));
237 os << "\n\\end_layout\n";
241 void Paragraph::validate(LaTeXFeatures & features) const
243 pimpl_->validate(features, *layout());
247 void Paragraph::eraseIntern(lyx::pos_type pos)
249 pimpl_->eraseIntern(pos);
253 bool Paragraph::erase(pos_type pos)
255 return pimpl_->erase(pos);
259 int Paragraph::erase(pos_type start, pos_type end)
261 return pimpl_->erase(start, end);
265 void Paragraph::insert(pos_type start, string const & str,
266 LyXFont const & font)
268 int size = str.size();
269 for (int i = 0 ; i < size ; ++i)
270 insertChar(start + i, str[i], font);
274 bool Paragraph::checkInsertChar(LyXFont &)
280 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
283 pimpl_->insertChar(pos, c, change);
287 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
288 LyXFont const & font, Change change)
290 pimpl_->insertChar(pos, c, change);
295 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
297 pimpl_->insertInset(pos, inset, change);
301 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
302 LyXFont const & font, Change change)
304 pimpl_->insertInset(pos, inset, change);
309 bool Paragraph::insetAllowed(InsetOld_code code)
311 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
315 InsetBase * Paragraph::getInset(pos_type pos)
317 BOOST_ASSERT(pos < size());
318 return insetlist.get(pos);
322 InsetBase const * Paragraph::getInset(pos_type pos) const
324 BOOST_ASSERT(pos < size());
325 return insetlist.get(pos);
329 // Gets uninstantiated font setting at position.
330 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
334 lyxerr << " pos: " << pos << " size: " << size() << endl;
335 BOOST_ASSERT(pos <= size());
338 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
339 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
340 for (; cit != end; ++cit)
341 if (cit->pos() >= pos)
347 if (pos == size() && !empty())
348 return getFontSettings(bparams, pos - 1);
350 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
354 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
356 BOOST_ASSERT(pos <= size());
358 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
359 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
360 for (; cit != end; ++cit)
361 if (cit->pos() >= pos)
364 // This should not happen, but if so, we take no chances.
365 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
371 // Gets uninstantiated font setting at position 0
372 LyXFont const Paragraph::getFirstFontSettings() const
374 if (!empty() && !pimpl_->fontlist.empty())
375 return pimpl_->fontlist[0].font();
377 return LyXFont(LyXFont::ALL_INHERIT);
381 // Gets the fully instantiated font at a given position in a paragraph
382 // This is basically the same function as LyXText::GetFont() in text2.C.
383 // The difference is that this one is used for generating the LaTeX file,
384 // and thus cosmetic "improvements" are disallowed: This has to deliver
385 // the true picture of the buffer. (Asger)
386 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
387 LyXFont const & outerfont) const
389 BOOST_ASSERT(pos >= 0);
391 LyXLayout_ptr const & lout = layout();
393 pos_type const body_pos = beginOfBody();
397 layoutfont = lout->labelfont;
399 layoutfont = lout->font;
401 LyXFont font = getFontSettings(bparams, pos);
402 font.realize(layoutfont);
403 font.realize(outerfont);
404 font.realize(bparams.getLyXTextClass().defaultfont());
410 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
411 LyXFont const & outerfont) const
413 LyXFont tmpfont = layout()->labelfont;
414 tmpfont.setLanguage(getParLanguage(bparams));
415 tmpfont.realize(outerfont);
416 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
421 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
422 LyXFont const & outerfont) const
424 LyXFont tmpfont = layout()->font;
425 tmpfont.setLanguage(getParLanguage(bparams));
426 tmpfont.realize(outerfont);
427 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
432 /// Returns the height of the highest font in range
434 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
435 LyXFont_size def_size) const
437 if (pimpl_->fontlist.empty())
440 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
441 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
442 for (; end_it != end; ++end_it) {
443 if (end_it->pos() >= endpos)
450 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
451 for (; cit != end; ++cit) {
452 if (cit->pos() >= startpos)
456 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
457 for (; cit != end_it; ++cit) {
458 LyXFont::FONT_SIZE size = cit->font().size();
459 if (size == LyXFont::INHERIT_SIZE)
461 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
468 Paragraph::value_type
469 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
471 value_type c = getChar(pos);
472 if (!lyxrc.rtl_support)
502 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
509 void Paragraph::setFont(pos_type pos, LyXFont const & font)
511 BOOST_ASSERT(pos <= size());
513 // First, reduce font against layout/label font
514 // Update: The setCharFont() routine in text2.C already
515 // reduces font, so we don't need to do that here. (Asger)
516 // No need to simplify this because it will disappear
517 // in a new kernel. (Asger)
518 // Next search font table
520 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
521 Pimpl::FontList::iterator it = beg;
522 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
523 for (; it != endit; ++it) {
524 if (it->pos() >= pos)
527 unsigned int i = distance(beg, it);
528 bool notfound = (it == endit);
530 if (!notfound && pimpl_->fontlist[i].font() == font)
533 bool begin = pos == 0 || notfound ||
534 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
535 // Is position pos is a beginning of a font block?
536 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
537 // Is position pos is the end of a font block?
538 if (begin && end) { // A single char block
539 if (i + 1 < pimpl_->fontlist.size() &&
540 pimpl_->fontlist[i + 1].font() == font) {
541 // Merge the singleton block with the next block
542 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
543 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
544 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
545 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
546 // Merge the singleton block with the previous block
547 pimpl_->fontlist[i - 1].pos(pos);
548 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
550 pimpl_->fontlist[i].font(font);
552 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
553 pimpl_->fontlist[i - 1].pos(pos);
555 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
556 Pimpl::FontTable(pos, font));
558 pimpl_->fontlist[i].pos(pos - 1);
559 if (!(i + 1 < pimpl_->fontlist.size() &&
560 pimpl_->fontlist[i + 1].font() == font))
561 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
562 Pimpl::FontTable(pos, font));
563 } else { // The general case. The block is splitted into 3 blocks
564 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
565 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
566 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
567 Pimpl::FontTable(pos, font));
572 void Paragraph::makeSameLayout(Paragraph const & par)
574 layout(par.layout());
576 params() = par.params();
580 int Paragraph::stripLeadingSpaces()
586 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
587 pimpl_->eraseIntern(0);
595 bool Paragraph::hasSameLayout(Paragraph const & par) const
597 return par.layout() == layout() && params().sameLayout(par.params());
601 Paragraph::depth_type Paragraph::getDepth() const
603 return params().depth();
607 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
609 if (layout()->isEnvironment())
610 return params().depth() + 1;
612 return params().depth();
616 char Paragraph::getAlign() const
618 return params().align();
622 string const & Paragraph::getLabelstring() const
624 return params().labelString();
628 // the next two functions are for the manual labels
629 string const Paragraph::getLabelWidthString() const
631 if (!params().labelWidthString().empty())
632 return params().labelWidthString();
634 return _("Senseless with this layout!");
638 void Paragraph::setLabelWidthString(string const & s)
640 params().labelWidthString(s);
644 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
647 params().labelWidthString(string());
648 params().align(LYX_ALIGN_LAYOUT);
649 params().spacing(Spacing(Spacing::Default));
653 pos_type Paragraph::beginOfBody() const
655 return begin_of_body_;
659 void Paragraph::setBeginOfBody()
661 if (layout()->labeltype != LABEL_MANUAL) {
666 // Unroll the first two cycles of the loop
667 // and remember the previous character to
668 // remove unnecessary getChar() calls
670 pos_type end = size();
671 if (i < end && !isNewline(i)) {
673 char previous_char = 0;
676 previous_char = text_[i];
679 while (i < end && previous_char != ' ') {
684 previous_char = temp;
694 // returns -1 if inset not found
695 int Paragraph::getPositionOfInset(InsetBase const * inset) const
698 InsetList::const_iterator it = insetlist.begin();
699 InsetList::const_iterator end = insetlist.end();
700 for (; it != end; ++it)
701 if (it->inset == inset)
707 InsetBibitem * Paragraph::bibitem() const
709 if (!insetlist.empty()) {
710 InsetBase * inset = insetlist.begin()->inset;
711 if (inset->lyxCode() == InsetBase::BIBTEX_CODE)
712 return static_cast<InsetBibitem *>(inset);
718 bool Paragraph::forceDefaultParagraphs() const
720 return inInset() && inInset()->forceDefaultParagraphs(inInset());
724 bool Paragraph::autoBreakRows() const
726 return inInset() && static_cast<InsetText *>(inInset())->getAutoBreakRows();
732 // paragraphs inside floats need different alignment tags to avoid
735 bool noTrivlistCentering(InsetBase::Code code)
737 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
741 string correction(string const & orig)
743 if (orig == "flushleft")
744 return "raggedright";
745 if (orig == "flushright")
747 if (orig == "center")
753 string const corrected_env(string const & suffix, string const & env,
754 InsetBase::Code code)
756 string output = suffix + "{";
757 if (noTrivlistCentering(code))
758 output += correction(env);
767 // This could go to ParagraphParameters if we want to
768 int Paragraph::startTeXParParams(BufferParams const & bparams,
769 ostream & os, bool moving_arg) const
773 if (params().noindent()) {
778 switch (params().align()) {
780 case LYX_ALIGN_BLOCK:
781 case LYX_ALIGN_LAYOUT:
782 case LYX_ALIGN_SPECIAL:
785 case LYX_ALIGN_RIGHT:
786 case LYX_ALIGN_CENTER:
794 switch (params().align()) {
796 case LYX_ALIGN_BLOCK:
797 case LYX_ALIGN_LAYOUT:
798 case LYX_ALIGN_SPECIAL:
800 case LYX_ALIGN_LEFT: {
802 if (getParLanguage(bparams)->babel() != "hebrew")
803 output = corrected_env("\\begin", "flushleft", ownerCode());
805 output = corrected_env("\\begin", "flushright", ownerCode());
807 column += output.size();
809 } case LYX_ALIGN_RIGHT: {
811 if (getParLanguage(bparams)->babel() != "hebrew")
812 output = corrected_env("\\begin", "flushright", ownerCode());
814 output = corrected_env("\\begin", "flushleft", ownerCode());
816 column += output.size();
818 } case LYX_ALIGN_CENTER: {
820 output = corrected_env("\\begin", "center", ownerCode());
822 column += output.size();
831 // This could go to ParagraphParameters if we want to
832 int Paragraph::endTeXParParams(BufferParams const & bparams,
833 ostream & os, bool moving_arg) const
837 switch (params().align()) {
839 case LYX_ALIGN_BLOCK:
840 case LYX_ALIGN_LAYOUT:
841 case LYX_ALIGN_SPECIAL:
844 case LYX_ALIGN_RIGHT:
845 case LYX_ALIGN_CENTER:
853 switch (params().align()) {
855 case LYX_ALIGN_BLOCK:
856 case LYX_ALIGN_LAYOUT:
857 case LYX_ALIGN_SPECIAL:
859 case LYX_ALIGN_LEFT: {
861 if (getParLanguage(bparams)->babel() != "hebrew")
862 output = corrected_env("\\par\\end", "flushleft", ownerCode());
864 output = corrected_env("\\par\\end", "flushright", ownerCode());
866 column += output.size();
868 } case LYX_ALIGN_RIGHT: {
870 if (getParLanguage(bparams)->babel() != "hebrew")
871 output = corrected_env("\\par\\end", "flushright", ownerCode());
873 output = corrected_env("\\par\\end", "flushleft", ownerCode());
875 column += output.size();
877 } case LYX_ALIGN_CENTER: {
879 output = corrected_env("\\par\\end", "center", ownerCode());
881 column += output.size();
890 // This one spits out the text of the paragraph
891 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
892 BufferParams const & bparams,
893 LyXFont const & outerfont,
894 ostream & os, TexRow & texrow,
895 OutputParams const & runparams) const
897 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
899 bool return_value = false;
903 // well we have to check if we are in an inset with unlimited
904 // length (all in one row) if that is true then we don't allow
905 // any special options in the paragraph and also we don't allow
906 // any environment other then "Standard" to be valid!
907 bool asdefault = forceDefaultParagraphs();
910 style = bparams.getLyXTextClass().defaultLayout();
917 // Maybe we have to create a optional argument.
918 pos_type body_pos = beginOfBody();
919 unsigned int column = 0;
922 // the optional argument is kept in curly brackets in
923 // case it contains a ']'
926 basefont = getLabelFont(bparams, outerfont);
928 basefont = getLayoutFont(bparams, outerfont);
931 // Which font is currently active?
932 LyXFont running_font(basefont);
933 // Do we have an open font change?
934 bool open_font = false;
936 Change::Type running_change = Change::UNCHANGED;
938 texrow.start(id(), 0);
940 // if the paragraph is empty, the loop will not be entered at all
942 if (style->isCommand()) {
947 column += startTeXParParams(bparams, os,
948 runparams.moving_arg);
951 for (pos_type i = 0; i < size(); ++i) {
953 // First char in paragraph or after label?
957 column += running_font.latexWriteEndChanges(os, basefont, basefont);
960 basefont = getLayoutFont(bparams, outerfont);
961 running_font = basefont;
965 if (style->isCommand()) {
971 column += startTeXParParams(bparams, os,
972 runparams.moving_arg);
975 value_type c = getChar(i);
977 // Fully instantiated font
978 LyXFont font = getFont(bparams, i, outerfont);
980 LyXFont const last_font = running_font;
982 // Spaces at end of font change are simulated to be
983 // outside font change, i.e. we write "\textXX{text} "
984 // rather than "\textXX{text }". (Asger)
985 if (open_font && c == ' ' && i <= size() - 2) {
986 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
987 if (next_font != running_font && next_font != font) {
992 // We end font definition before blanks
994 (font != running_font ||
995 font.language() != running_font.language()))
997 column += running_font.latexWriteEndChanges(os,
999 (i == body_pos-1) ? basefont : font);
1000 running_font = basefont;
1004 // Blanks are printed before start of fontswitch
1006 // Do not print the separation of the optional argument
1007 if (i != body_pos - 1) {
1008 pimpl_->simpleTeXBlanks(os, texrow, i,
1009 column, font, *style);
1013 // Do we need to change font?
1014 if ((font != running_font ||
1015 font.language() != running_font.language()) &&
1018 column += font.latexWriteStartChanges(os, basefont,
1020 running_font = font;
1024 Change::Type change = pimpl_->lookupChange(i);
1026 column += Changes::latexMarkChange(os, running_change, change);
1027 running_change = change;
1029 OutputParams rp = runparams;
1030 rp.free_spacing = style->free_spacing;
1031 rp.local_language = font.language()->babel();
1032 rp.intitle = style->intitle;
1033 pimpl_->simpleTeXSpecialChars(buf, bparams,
1036 basefont, outerfont, open_font,
1038 *style, i, column, c);
1041 column += Changes::latexMarkChange(os,
1042 running_change, Change::UNCHANGED);
1044 // If we have an open font definition, we have to close it
1046 #ifdef FIXED_LANGUAGE_END_DETECTION
1049 .latexWriteEndChanges(os, basefont,
1050 next_->getFont(bparams,
1053 running_font.latexWriteEndChanges(os, basefont,
1057 #ifdef WITH_WARNINGS
1058 //#warning For now we ALWAYS have to close the foreign font settings if they are
1059 //#warning there as we start another \selectlanguage with the next paragraph if
1060 //#warning we are in need of this. This should be fixed sometime (Jug)
1062 running_font.latexWriteEndChanges(os, basefont, basefont);
1066 // Needed if there is an optional argument but no contents.
1067 if (body_pos > 0 && body_pos == size()) {
1069 return_value = false;
1073 column += endTeXParParams(bparams, os, runparams.moving_arg);
1076 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1077 return return_value;
1083 // checks, if newcol chars should be put into this line
1084 // writes newline, if necessary.
1085 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1086 string::size_type newcol)
1089 if (colcount > lyxrc.ascii_linelen) {
1091 colcount = newcol; // assume write after this call
1106 string tag_name(PAR_TAG const & pt) {
1108 case PAR_NONE: return "!-- --";
1109 case TT: return "tt";
1110 case SF: return "sf";
1111 case BF: return "bf";
1112 case IT: return "it";
1113 case SL: return "sl";
1114 case EM: return "em";
1121 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1123 p1 = static_cast<PAR_TAG>(p1 | p2);
1128 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1130 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1136 // Handle internal paragraph parsing -- layout already processed.
1137 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1139 LyXFont const & outerfont,
1140 OutputParams const & runparams,
1141 lyx::depth_type /*depth*/) const
1143 LyXLayout_ptr const & style = layout();
1145 string::size_type char_line_count = 5; // Heuristic choice ;-)
1147 // gets paragraph main font
1150 if (style->labeltype == LABEL_MANUAL) {
1151 font_old = style->labelfont;
1154 font_old = style->font;
1158 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1159 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1160 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1163 stack<PAR_TAG> tag_state;
1164 // parsing main loop
1165 for (pos_type i = 0; i < size(); ++i) {
1167 PAR_TAG tag_close = PAR_NONE;
1168 list < PAR_TAG > tag_open;
1170 LyXFont const font = getFont(buf.params(), i, outerfont);
1172 if (font_old.family() != font.family()) {
1173 switch (family_type) {
1174 case LyXFont::SANS_FAMILY:
1177 case LyXFont::TYPEWRITER_FAMILY:
1184 family_type = font.family();
1186 switch (family_type) {
1187 case LyXFont::SANS_FAMILY:
1188 tag_open.push_back(SF);
1190 case LyXFont::TYPEWRITER_FAMILY:
1191 tag_open.push_back(TT);
1198 if (font_old.series() != font.series()) {
1199 switch (series_type) {
1200 case LyXFont::BOLD_SERIES:
1207 series_type = font.series();
1209 switch (series_type) {
1210 case LyXFont::BOLD_SERIES:
1211 tag_open.push_back(BF);
1219 if (font_old.shape() != font.shape()) {
1220 switch (shape_type) {
1221 case LyXFont::ITALIC_SHAPE:
1224 case LyXFont::SLANTED_SHAPE:
1231 shape_type = font.shape();
1233 switch (shape_type) {
1234 case LyXFont::ITALIC_SHAPE:
1235 tag_open.push_back(IT);
1237 case LyXFont::SLANTED_SHAPE:
1238 tag_open.push_back(SL);
1245 if (font_old.emph() != font.emph()) {
1246 if (font.emph() == LyXFont::ON) {
1247 tag_open.push_back(EM);
1256 list < PAR_TAG > temp;
1257 while (!tag_state.empty() && tag_close) {
1258 PAR_TAG k = tag_state.top();
1260 os << "</" << tag_name(k) << '>';
1267 for(list< PAR_TAG >::const_iterator j = temp.begin();
1268 j != temp.end(); ++j) {
1270 os << '<' << tag_name(*j) << '>';
1273 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1274 j != tag_open.end(); ++j) {
1276 os << '<' << tag_name(*j) << '>';
1279 char c = getChar(i);
1282 if (c == Paragraph::META_INSET) {
1283 getInset(i)->linuxdoc(buf, os, runparams);
1288 if (style->latexparam() == "CDATA") {
1289 // "TeX"-Mode on == > SGML-Mode on.
1296 boost::tie(ws, str) = sgml::escapeChar(c);
1297 if (ws && !isFreeSpacing()) {
1298 // in freespacing mode, spaces are
1299 // non-breaking characters
1300 if (desc_on) { // if char is ' ' then...
1302 sgmlLineBreak(os, char_line_count, 6);
1306 sgmlLineBreak(os, char_line_count, 1);
1311 char_line_count += str.length();
1317 while (!tag_state.empty()) {
1318 os << "</" << tag_name(tag_state.top()) << '>';
1322 // resets description flag correctly
1324 // <tag> not closed...
1325 sgmlLineBreak(os, char_line_count, 6);
1331 bool Paragraph::emptyTag() const
1333 for (pos_type i = 0; i < size(); ++i) {
1335 InsetBase const * inset = getInset(i);
1336 InsetBase::Code lyx_code = inset->lyxCode();
1337 if (lyx_code != InsetBase::TOC_CODE and
1338 lyx_code != InsetBase::INCLUDE_CODE and
1339 lyx_code != InsetBase::GRAPHICS_CODE and
1340 lyx_code != InsetBase::ERT_CODE and
1341 lyx_code != InsetBase::FLOAT_CODE and
1342 lyx_code != InsetBase::TABULAR_CODE) {
1346 char c = getChar(i);
1347 if(c!= ' ' and c!= '\t')
1356 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1358 for (pos_type i = 0; i < size(); ++i) {
1360 InsetBase const * inset = getInset(i);
1361 InsetBase::Code lyx_code = inset->lyxCode();
1362 if (lyx_code == InsetBase::LABEL_CODE) {
1363 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1364 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1373 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1376 for (i = 0; i < size(); ++i) {
1378 InsetBase const * inset = getInset(i);
1379 inset->docbook(buf, os, runparams);
1381 char c = getChar(i);
1386 boost::tie(ws, str) = sgml::escapeChar(c);
1395 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1397 LyXLayout_ptr const & style = layout();
1400 for (pos_type i = initial; i < size(); ++i) {
1401 LyXFont font = getFont(buf.params(), i, outerfont);
1404 if ( i != initial and font != font_old)
1413 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1415 OutputParams const & runparams,
1416 LyXFont const & outerfont,
1417 pos_type initial) const
1419 bool emph_flag = false;
1421 LyXLayout_ptr const & style = layout();
1423 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1425 if (style->pass_thru and not onlyText(buf, outerfont, initial))
1427 // parsing main loop
1428 for (pos_type i = initial; i < size(); ++i) {
1429 LyXFont font = getFont(buf.params(), i, outerfont);
1431 // handle <emphasis> tag
1432 if (font_old.emph() != font.emph()) {
1433 if (font.emph() == LyXFont::ON) {
1436 } else if (i != initial) {
1437 os << "</emphasis>";
1443 InsetBase const * inset = getInset(i);
1444 inset->docbook(buf, os, runparams);
1446 char c = getChar(i);
1449 boost::tie(ws, str) = sgml::escapeChar(c);
1451 if (style->pass_thru)
1460 os << "</emphasis>";
1463 if (style->free_spacing)
1465 if (style->pass_thru and not onlyText(buf, outerfont, initial))
1472 /// return true if the char is a meta-character for an inset
1474 bool IsInsetChar(char c)
1476 return (c == Paragraph::META_INSET);
1483 bool Paragraph::isHfill(pos_type pos) const
1486 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1490 bool Paragraph::isNewline(pos_type pos) const
1493 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1497 bool Paragraph::isSeparator(pos_type pos) const
1499 return IsSeparatorChar(getChar(pos));
1503 bool Paragraph::isLineSeparator(pos_type pos) const
1505 value_type const c = getChar(pos);
1506 return IsLineSeparatorChar(c)
1507 || (IsInsetChar(c) && getInset(pos) &&
1508 getInset(pos)->isLineSeparator());
1512 /// Used by the spellchecker
1513 bool Paragraph::isLetter(pos_type pos) const
1516 return getInset(pos)->isLetter();
1518 value_type const c = getChar(pos);
1519 return IsLetterChar(c) || IsDigit(c);
1525 Paragraph::getParLanguage(BufferParams const & bparams) const
1528 return getFirstFontSettings().language();
1529 #ifdef WITH_WARNINGS
1530 #warning FIXME we should check the prev par as well (Lgb)
1532 return bparams.language;
1536 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1538 return lyxrc.rtl_support
1539 && getParLanguage(bparams)->RightToLeft()
1540 && ownerCode() != InsetBase::ERT_CODE;
1544 void Paragraph::changeLanguage(BufferParams const & bparams,
1545 Language const * from, Language const * to)
1547 for (pos_type i = 0; i < size(); ++i) {
1548 LyXFont font = getFontSettings(bparams, i);
1549 if (font.language() == from) {
1550 font.setLanguage(to);
1557 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1559 Language const * doc_language = bparams.language;
1560 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1561 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1563 for (; cit != end; ++cit)
1564 if (cit->font().language() != ignore_language &&
1565 cit->font().language() != latex_language &&
1566 cit->font().language() != doc_language)
1572 // Convert the paragraph to a string.
1573 // Used for building the table of contents
1574 string const Paragraph::asString(Buffer const & buffer, bool label) const
1576 OutputParams runparams;
1577 return asString(buffer, runparams, label);
1581 string const Paragraph::asString(Buffer const & buffer,
1582 OutputParams const & runparams,
1587 if (label && !params().labelString().empty())
1588 s += params().labelString() + ' ';
1590 for (pos_type i = 0; i < size(); ++i) {
1591 value_type c = getChar(i);
1594 else if (c == META_INSET &&
1595 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1597 getInset(i)->plaintext(buffer, os, runparams);
1598 s += subst(STRCONV(os.str()),'\n',' ');
1604 // This should really be done by the caller and not here.
1605 string ret = asString(buffer, runparams, 0, size(), label);
1606 return subst(ret, '\n', ' ');
1611 string const Paragraph::asString(Buffer const & buffer,
1612 pos_type beg, pos_type end, bool label) const
1615 OutputParams const runparams;
1616 return asString(buffer, runparams, beg, end, label);
1620 string const Paragraph::asString(Buffer const & buffer,
1621 OutputParams const & runparams,
1622 pos_type beg, pos_type end, bool label) const
1626 if (beg == 0 && label && !params().labelString().empty())
1627 os << params().labelString() << ' ';
1629 for (pos_type i = beg; i < end; ++i) {
1630 value_type const c = getUChar(buffer.params(), i);
1633 else if (c == META_INSET)
1634 getInset(i)->plaintext(buffer, os, runparams);
1641 void Paragraph::setInsetOwner(UpdatableInset * inset)
1643 pimpl_->inset_owner = inset;
1647 void Paragraph::setContentsFromPar(Paragraph const & par)
1649 pimpl_->setContentsFromPar(par);
1653 void Paragraph::trackChanges(Change::Type type)
1655 pimpl_->trackChanges(type);
1659 void Paragraph::untrackChanges()
1661 pimpl_->untrackChanges();
1665 void Paragraph::cleanChanges()
1667 pimpl_->cleanChanges();
1671 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1673 BOOST_ASSERT(empty() || pos < size());
1674 return pimpl_->lookupChange(pos);
1678 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1680 BOOST_ASSERT(empty() || pos < size());
1681 return pimpl_->lookupChangeFull(pos);
1685 bool Paragraph::isChanged(pos_type start, pos_type end) const
1687 return pimpl_->isChanged(start, end);
1691 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1693 return pimpl_->isChangeEdited(start, end);
1697 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1699 pimpl_->setChange(pos, type);
1703 void Paragraph::markErased()
1705 pimpl_->markErased();
1709 void Paragraph::acceptChange(pos_type start, pos_type end)
1711 return pimpl_->acceptChange(start, end);
1715 void Paragraph::rejectChange(pos_type start, pos_type end)
1717 return pimpl_->rejectChange(start, end);
1721 int Paragraph::id() const
1727 LyXLayout_ptr const & Paragraph::layout() const
1733 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1735 layout_ = new_layout;
1739 UpdatableInset * Paragraph::inInset() const
1741 return pimpl_->inset_owner;
1745 InsetBase::Code Paragraph::ownerCode() const
1747 return pimpl_->inset_owner
1748 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1752 void Paragraph::clearContents()
1758 void Paragraph::setChar(pos_type pos, value_type c)
1764 ParagraphParameters & Paragraph::params()
1766 return pimpl_->params;
1770 ParagraphParameters const & Paragraph::params() const
1772 return pimpl_->params;
1776 bool Paragraph::isFreeSpacing() const
1778 if (layout()->free_spacing)
1781 // for now we just need this, later should we need this in some
1782 // other way we can always add a function to InsetBase too.
1783 return ownerCode() == InsetBase::ERT_CODE;
1787 bool Paragraph::allowEmpty() const
1789 if (layout()->keepempty)
1791 return ownerCode() == InsetBase::ERT_CODE;
1795 RowList::iterator Paragraph::getRow(pos_type pos)
1797 RowList::iterator rit = rows.end();
1798 RowList::iterator const begin = rows.begin();
1800 for (--rit; rit != begin && rit->pos() > pos; --rit)
1807 RowList::const_iterator Paragraph::getRow(pos_type pos) const
1809 RowList::const_iterator rit = rows.end();
1810 RowList::const_iterator const begin = rows.begin();
1812 for (--rit; rit != begin && rit->pos() > pos; --rit)
1819 size_t Paragraph::row(pos_type pos) const
1821 RowList::const_iterator rit = rows.end();
1822 RowList::const_iterator const begin = rows.begin();
1824 for (--rit; rit != begin && rit->pos() > pos; --rit)
1831 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1833 if (!Encodings::is_arabic(c))
1834 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1835 return c + (0xb0 - '0');
1839 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1840 unsigned char next_char = ' ';
1842 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1843 unsigned char const par_char = getChar(i);
1844 if (!Encodings::IsComposeChar_arabic(par_char)) {
1845 next_char = par_char;
1850 if (Encodings::is_arabic(next_char)) {
1851 if (Encodings::is_arabic(prev_char) &&
1852 !Encodings::is_arabic_special(prev_char))
1853 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1855 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1857 if (Encodings::is_arabic(prev_char) &&
1858 !Encodings::is_arabic_special(prev_char))
1859 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1861 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);