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::contains;
57 using lyx::support::subst;
65 using std::ostringstream;
68 ParagraphList::ParagraphList()
72 Paragraph::Paragraph()
73 : y(0), height(0), begin_of_body_(0),
74 pimpl_(new Paragraph::Pimpl(this))
76 //lyxerr << "sizeof Paragraph::Pimpl: " << sizeof(Paragraph::Pimpl) << endl;
82 Paragraph::Paragraph(Paragraph const & par)
83 : itemdepth(par.itemdepth), insetlist(par.insetlist),
84 rows(par.rows), y(par.y), height(par.height),
85 width(par.width), layout_(par.layout_),
86 text_(par.text_), begin_of_body_(par.begin_of_body_),
87 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
89 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
90 InsetList::iterator it = insetlist.begin();
91 InsetList::iterator end = insetlist.end();
92 for (; it != end; ++it)
93 it->inset = it->inset->clone().release();
97 Paragraph & Paragraph::operator=(Paragraph const & par)
99 // needed as we will destroy the pimpl_ before copying it
101 itemdepth = par.itemdepth;
103 insetlist = par.insetlist;
104 InsetList::iterator it = insetlist.begin();
105 InsetList::iterator end = insetlist.end();
106 for (; it != end; ++it)
107 it->inset = it->inset->clone().release();
113 layout_ = par.layout();
115 begin_of_body_ = par.begin_of_body_;
118 pimpl_ = new Pimpl(*par.pimpl_, this);
124 Paragraph::~Paragraph()
128 //lyxerr << "Paragraph::paragraph_id = "
129 // << Paragraph::paragraph_id << endl;
133 void Paragraph::write(Buffer const & buf, ostream & os,
134 BufferParams const & bparams,
135 depth_type & dth) const
137 // The beginning or end of a deeper (i.e. nested) area?
138 if (dth != params().depth()) {
139 if (params().depth() > dth) {
140 while (params().depth() > dth) {
141 os << "\n\\begin_deeper ";
145 while (params().depth() < dth) {
146 os << "\n\\end_deeper ";
152 // First write the layout
153 os << "\n\\begin_layout " << layout()->name() << '\n';
157 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
159 Change running_change = Change(Change::UNCHANGED);
160 lyx::time_type const curtime(lyx::current_time());
163 for (pos_type i = 0; i < size(); ++i) {
169 Change change = pimpl_->lookupChangeFull(i);
170 Changes::lyxMarkChange(os, column, curtime, running_change, change);
171 running_change = change;
173 // Write font changes
174 LyXFont font2 = getFontSettings(bparams, i);
175 if (font2 != font1) {
176 font2.lyxWriteChanges(font1, os);
181 value_type const c = getChar(i);
185 InsetBase const * inset = getInset(i);
187 if (inset->directWrite()) {
188 // international char, let it write
189 // code directly so it's shorter in
191 inset->write(buf, os);
193 os << "\n\\begin_inset ";
194 inset->write(buf, os);
195 os << "\n\\end_inset \n\n";
201 os << "\n\\backslash \n";
205 if (i + 1 < size() && getChar(i + 1) == ' ') {
212 if ((column > 70 && c == ' ')
217 // this check is to amend a bug. LyX sometimes
218 // inserts '\0' this could cause problems.
222 lyxerr << "ERROR (Paragraph::writeFile):"
223 " NULL char in structure." << endl;
229 // to make reading work properly
231 running_change = pimpl_->lookupChange(0);
232 Changes::lyxMarkChange(os, column, curtime,
233 Change(Change::UNCHANGED), running_change);
235 Changes::lyxMarkChange(os, column, curtime,
236 running_change, Change(Change::UNCHANGED));
238 os << "\n\\end_layout\n";
242 void Paragraph::validate(LaTeXFeatures & features) const
244 pimpl_->validate(features, *layout());
248 void Paragraph::eraseIntern(lyx::pos_type pos)
250 pimpl_->eraseIntern(pos);
254 bool Paragraph::erase(pos_type pos)
256 return pimpl_->erase(pos);
260 int Paragraph::erase(pos_type start, pos_type end)
262 return pimpl_->erase(start, end);
266 void Paragraph::insert(pos_type start, string const & str,
267 LyXFont const & font)
269 int size = str.size();
270 for (int i = 0 ; i < size ; ++i)
271 insertChar(start + i, str[i], font);
275 bool Paragraph::checkInsertChar(LyXFont &)
281 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
284 pimpl_->insertChar(pos, c, change);
288 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
289 LyXFont const & font, Change change)
291 pimpl_->insertChar(pos, c, change);
296 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
298 pimpl_->insertInset(pos, inset, change);
302 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
303 LyXFont const & font, Change change)
305 pimpl_->insertInset(pos, inset, change);
310 bool Paragraph::insetAllowed(InsetOld_code code)
312 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
316 InsetBase * Paragraph::getInset(pos_type pos)
318 BOOST_ASSERT(pos < size());
319 return insetlist.get(pos);
323 InsetBase const * Paragraph::getInset(pos_type pos) const
325 BOOST_ASSERT(pos < size());
326 return insetlist.get(pos);
330 // Gets uninstantiated font setting at position.
331 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
335 lyxerr << " pos: " << pos << " size: " << size() << endl;
336 BOOST_ASSERT(pos <= size());
339 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
340 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
341 for (; cit != end; ++cit)
342 if (cit->pos() >= pos)
348 if (pos == size() && !empty())
349 return getFontSettings(bparams, pos - 1);
351 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
355 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
357 BOOST_ASSERT(pos <= size());
359 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
360 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
361 for (; cit != end; ++cit)
362 if (cit->pos() >= pos)
365 // This should not happen, but if so, we take no chances.
366 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
372 // Gets uninstantiated font setting at position 0
373 LyXFont const Paragraph::getFirstFontSettings() const
375 if (!empty() && !pimpl_->fontlist.empty())
376 return pimpl_->fontlist[0].font();
378 return LyXFont(LyXFont::ALL_INHERIT);
382 // Gets the fully instantiated font at a given position in a paragraph
383 // This is basically the same function as LyXText::GetFont() in text2.C.
384 // The difference is that this one is used for generating the LaTeX file,
385 // and thus cosmetic "improvements" are disallowed: This has to deliver
386 // the true picture of the buffer. (Asger)
387 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
388 LyXFont const & outerfont) const
390 BOOST_ASSERT(pos >= 0);
392 LyXLayout_ptr const & lout = layout();
394 pos_type const body_pos = beginOfBody();
398 layoutfont = lout->labelfont;
400 layoutfont = lout->font;
402 LyXFont font = getFontSettings(bparams, pos);
403 font.realize(layoutfont);
404 font.realize(outerfont);
405 font.realize(bparams.getLyXTextClass().defaultfont());
411 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
412 LyXFont const & outerfont) const
414 LyXFont tmpfont = layout()->labelfont;
415 tmpfont.setLanguage(getParLanguage(bparams));
416 tmpfont.realize(outerfont);
417 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
422 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
423 LyXFont const & outerfont) const
425 LyXFont tmpfont = layout()->font;
426 tmpfont.setLanguage(getParLanguage(bparams));
427 tmpfont.realize(outerfont);
428 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
433 /// Returns the height of the highest font in range
435 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
436 LyXFont_size def_size) const
438 if (pimpl_->fontlist.empty())
441 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
442 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
443 for (; end_it != end; ++end_it) {
444 if (end_it->pos() >= endpos)
451 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
452 for (; cit != end; ++cit) {
453 if (cit->pos() >= startpos)
457 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
458 for (; cit != end_it; ++cit) {
459 LyXFont::FONT_SIZE size = cit->font().size();
460 if (size == LyXFont::INHERIT_SIZE)
462 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
469 Paragraph::value_type
470 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
472 value_type c = getChar(pos);
473 if (!lyxrc.rtl_support)
503 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
510 void Paragraph::setFont(pos_type pos, LyXFont const & font)
512 BOOST_ASSERT(pos <= size());
514 // First, reduce font against layout/label font
515 // Update: The setCharFont() routine in text2.C already
516 // reduces font, so we don't need to do that here. (Asger)
517 // No need to simplify this because it will disappear
518 // in a new kernel. (Asger)
519 // Next search font table
521 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
522 Pimpl::FontList::iterator it = beg;
523 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
524 for (; it != endit; ++it) {
525 if (it->pos() >= pos)
528 unsigned int i = distance(beg, it);
529 bool notfound = (it == endit);
531 if (!notfound && pimpl_->fontlist[i].font() == font)
534 bool begin = pos == 0 || notfound ||
535 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
536 // Is position pos is a beginning of a font block?
537 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
538 // Is position pos is the end of a font block?
539 if (begin && end) { // A single char block
540 if (i + 1 < pimpl_->fontlist.size() &&
541 pimpl_->fontlist[i + 1].font() == font) {
542 // Merge the singleton block with the next block
543 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
544 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
545 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
546 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
547 // Merge the singleton block with the previous block
548 pimpl_->fontlist[i - 1].pos(pos);
549 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
551 pimpl_->fontlist[i].font(font);
553 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
554 pimpl_->fontlist[i - 1].pos(pos);
556 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
557 Pimpl::FontTable(pos, font));
559 pimpl_->fontlist[i].pos(pos - 1);
560 if (!(i + 1 < pimpl_->fontlist.size() &&
561 pimpl_->fontlist[i + 1].font() == font))
562 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
563 Pimpl::FontTable(pos, font));
564 } else { // The general case. The block is splitted into 3 blocks
565 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
566 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
567 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
568 Pimpl::FontTable(pos, font));
573 void Paragraph::makeSameLayout(Paragraph const & par)
575 layout(par.layout());
577 params() = par.params();
581 int Paragraph::stripLeadingSpaces()
587 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
588 pimpl_->eraseIntern(0);
596 bool Paragraph::hasSameLayout(Paragraph const & par) const
598 return par.layout() == layout() && params().sameLayout(par.params());
602 Paragraph::depth_type Paragraph::getDepth() const
604 return params().depth();
608 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
610 if (layout()->isEnvironment())
611 return params().depth() + 1;
613 return params().depth();
617 char Paragraph::getAlign() const
619 return params().align();
623 string const & Paragraph::getLabelstring() const
625 return params().labelString();
629 // the next two functions are for the manual labels
630 string const Paragraph::getLabelWidthString() const
632 if (!params().labelWidthString().empty())
633 return params().labelWidthString();
635 return _("Senseless with this layout!");
639 void Paragraph::setLabelWidthString(string const & s)
641 params().labelWidthString(s);
645 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
648 params().labelWidthString(string());
649 params().align(LYX_ALIGN_LAYOUT);
650 params().spacing(Spacing(Spacing::Default));
654 pos_type Paragraph::beginOfBody() const
656 return begin_of_body_;
660 void Paragraph::setBeginOfBody()
662 if (layout()->labeltype != LABEL_MANUAL) {
667 // Unroll the first two cycles of the loop
668 // and remember the previous character to
669 // remove unnecessary getChar() calls
671 pos_type end = size();
672 if (i < end && !isNewline(i)) {
674 char previous_char = 0;
677 previous_char = text_[i];
680 while (i < end && previous_char != ' ') {
685 previous_char = temp;
695 // returns -1 if inset not found
696 int Paragraph::getPositionOfInset(InsetBase const * inset) const
699 InsetList::const_iterator it = insetlist.begin();
700 InsetList::const_iterator end = insetlist.end();
701 for (; it != end; ++it)
702 if (it->inset == inset)
708 InsetBibitem * Paragraph::bibitem() const
710 if (!insetlist.empty()) {
711 InsetBase * inset = insetlist.begin()->inset;
712 if (inset->lyxCode() == InsetBase::BIBTEX_CODE)
713 return static_cast<InsetBibitem *>(inset);
719 bool Paragraph::forceDefaultParagraphs() const
721 return inInset() && inInset()->forceDefaultParagraphs(inInset());
725 bool Paragraph::autoBreakRows() const
727 return inInset() && static_cast<InsetText *>(inInset())->getAutoBreakRows();
733 // paragraphs inside floats need different alignment tags to avoid
736 bool noTrivlistCentering(InsetBase::Code code)
738 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
742 string correction(string const & orig)
744 if (orig == "flushleft")
745 return "raggedright";
746 if (orig == "flushright")
748 if (orig == "center")
754 string const corrected_env(string const & suffix, string const & env,
755 InsetBase::Code code)
757 string output = suffix + "{";
758 if (noTrivlistCentering(code))
759 output += correction(env);
768 // This could go to ParagraphParameters if we want to
769 int Paragraph::startTeXParParams(BufferParams const & bparams,
770 ostream & os, bool moving_arg) const
774 if (params().noindent()) {
779 switch (params().align()) {
781 case LYX_ALIGN_BLOCK:
782 case LYX_ALIGN_LAYOUT:
783 case LYX_ALIGN_SPECIAL:
786 case LYX_ALIGN_RIGHT:
787 case LYX_ALIGN_CENTER:
795 switch (params().align()) {
797 case LYX_ALIGN_BLOCK:
798 case LYX_ALIGN_LAYOUT:
799 case LYX_ALIGN_SPECIAL:
801 case LYX_ALIGN_LEFT: {
803 if (getParLanguage(bparams)->babel() != "hebrew")
804 output = corrected_env("\\begin", "flushleft", ownerCode());
806 output = corrected_env("\\begin", "flushright", ownerCode());
808 column += output.size();
810 } case LYX_ALIGN_RIGHT: {
812 if (getParLanguage(bparams)->babel() != "hebrew")
813 output = corrected_env("\\begin", "flushright", ownerCode());
815 output = corrected_env("\\begin", "flushleft", ownerCode());
817 column += output.size();
819 } case LYX_ALIGN_CENTER: {
821 output = corrected_env("\\begin", "center", ownerCode());
823 column += output.size();
832 // This could go to ParagraphParameters if we want to
833 int Paragraph::endTeXParParams(BufferParams const & bparams,
834 ostream & os, bool moving_arg) const
838 switch (params().align()) {
840 case LYX_ALIGN_BLOCK:
841 case LYX_ALIGN_LAYOUT:
842 case LYX_ALIGN_SPECIAL:
845 case LYX_ALIGN_RIGHT:
846 case LYX_ALIGN_CENTER:
854 switch (params().align()) {
856 case LYX_ALIGN_BLOCK:
857 case LYX_ALIGN_LAYOUT:
858 case LYX_ALIGN_SPECIAL:
860 case LYX_ALIGN_LEFT: {
862 if (getParLanguage(bparams)->babel() != "hebrew")
863 output = corrected_env("\\par\\end", "flushleft", ownerCode());
865 output = corrected_env("\\par\\end", "flushright", ownerCode());
867 column += output.size();
869 } case LYX_ALIGN_RIGHT: {
871 if (getParLanguage(bparams)->babel() != "hebrew")
872 output = corrected_env("\\par\\end", "flushright", ownerCode());
874 output = corrected_env("\\par\\end", "flushleft", ownerCode());
876 column += output.size();
878 } case LYX_ALIGN_CENTER: {
880 output = corrected_env("\\par\\end", "center", ownerCode());
882 column += output.size();
891 // This one spits out the text of the paragraph
892 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
893 BufferParams const & bparams,
894 LyXFont const & outerfont,
895 ostream & os, TexRow & texrow,
896 OutputParams const & runparams) const
898 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
900 bool return_value = false;
904 // well we have to check if we are in an inset with unlimited
905 // length (all in one row) if that is true then we don't allow
906 // any special options in the paragraph and also we don't allow
907 // any environment other then "Standard" to be valid!
908 bool asdefault = forceDefaultParagraphs();
911 style = bparams.getLyXTextClass().defaultLayout();
918 // Maybe we have to create a optional argument.
919 pos_type body_pos = beginOfBody();
920 unsigned int column = 0;
925 basefont = getLabelFont(bparams, outerfont);
927 basefont = getLayoutFont(bparams, outerfont);
930 bool const moving_arg = runparams.moving_arg | style->needprotect;
932 // Which font is currently active?
933 LyXFont running_font(basefont);
934 // Do we have an open font change?
935 bool open_font = false;
937 Change::Type running_change = Change::UNCHANGED;
939 texrow.start(id(), 0);
941 // if the paragraph is empty, the loop will not be entered at all
943 if (style->isCommand()) {
948 column += startTeXParParams(bparams, os, 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,
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.moving_arg = moving_arg;
1031 rp.free_spacing = style->free_spacing;
1032 rp.lang = font.language()->babel();
1033 rp.intitle = style->intitle;
1034 pimpl_->simpleTeXSpecialChars(buf, bparams,
1037 basefont, outerfont, open_font,
1039 *style, i, column, c);
1042 column += Changes::latexMarkChange(os,
1043 running_change, Change::UNCHANGED);
1045 // If we have an open font definition, we have to close it
1047 #ifdef FIXED_LANGUAGE_END_DETECTION
1050 .latexWriteEndChanges(os, basefont,
1051 next_->getFont(bparams,
1054 running_font.latexWriteEndChanges(os, basefont,
1058 #ifdef WITH_WARNINGS
1059 //#warning For now we ALWAYS have to close the foreign font settings if they are
1060 //#warning there as we start another \selectlanguage with the next paragraph if
1061 //#warning we are in need of this. This should be fixed sometime (Jug)
1063 running_font.latexWriteEndChanges(os, basefont, basefont);
1067 // Needed if there is an optional argument but no contents.
1068 if (body_pos > 0 && body_pos == size()) {
1070 return_value = false;
1074 column += endTeXParParams(bparams, os, moving_arg);
1077 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1078 return return_value;
1084 // checks, if newcol chars should be put into this line
1085 // writes newline, if necessary.
1086 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1087 string::size_type newcol)
1090 if (colcount > lyxrc.ascii_linelen) {
1092 colcount = newcol; // assume write after this call
1107 string tag_name(PAR_TAG const & pt) {
1109 case PAR_NONE: return "!-- --";
1110 case TT: return "tt";
1111 case SF: return "sf";
1112 case BF: return "bf";
1113 case IT: return "it";
1114 case SL: return "sl";
1115 case EM: return "em";
1122 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1124 p1 = static_cast<PAR_TAG>(p1 | p2);
1129 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1131 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1137 // Handle internal paragraph parsing -- layout already processed.
1138 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1140 LyXFont const & outerfont,
1141 OutputParams const & runparams,
1142 lyx::depth_type /*depth*/) const
1144 LyXLayout_ptr const & style = layout();
1146 string::size_type char_line_count = 5; // Heuristic choice ;-)
1148 // gets paragraph main font
1151 if (style->labeltype == LABEL_MANUAL) {
1152 font_old = style->labelfont;
1155 font_old = style->font;
1159 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1160 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1161 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1164 stack<PAR_TAG> tag_state;
1165 // parsing main loop
1166 for (pos_type i = 0; i < size(); ++i) {
1168 PAR_TAG tag_close = PAR_NONE;
1169 list < PAR_TAG > tag_open;
1171 LyXFont const font = getFont(buf.params(), i, outerfont);
1173 if (font_old.family() != font.family()) {
1174 switch (family_type) {
1175 case LyXFont::SANS_FAMILY:
1178 case LyXFont::TYPEWRITER_FAMILY:
1185 family_type = font.family();
1187 switch (family_type) {
1188 case LyXFont::SANS_FAMILY:
1189 tag_open.push_back(SF);
1191 case LyXFont::TYPEWRITER_FAMILY:
1192 tag_open.push_back(TT);
1199 if (font_old.series() != font.series()) {
1200 switch (series_type) {
1201 case LyXFont::BOLD_SERIES:
1208 series_type = font.series();
1210 switch (series_type) {
1211 case LyXFont::BOLD_SERIES:
1212 tag_open.push_back(BF);
1220 if (font_old.shape() != font.shape()) {
1221 switch (shape_type) {
1222 case LyXFont::ITALIC_SHAPE:
1225 case LyXFont::SLANTED_SHAPE:
1232 shape_type = font.shape();
1234 switch (shape_type) {
1235 case LyXFont::ITALIC_SHAPE:
1236 tag_open.push_back(IT);
1238 case LyXFont::SLANTED_SHAPE:
1239 tag_open.push_back(SL);
1246 if (font_old.emph() != font.emph()) {
1247 if (font.emph() == LyXFont::ON) {
1248 tag_open.push_back(EM);
1257 list < PAR_TAG > temp;
1258 while (!tag_state.empty() && tag_close) {
1259 PAR_TAG k = tag_state.top();
1261 os << "</" << tag_name(k) << '>';
1268 for(list< PAR_TAG >::const_iterator j = temp.begin();
1269 j != temp.end(); ++j) {
1271 os << '<' << tag_name(*j) << '>';
1274 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1275 j != tag_open.end(); ++j) {
1277 os << '<' << tag_name(*j) << '>';
1280 char c = getChar(i);
1283 if (c == Paragraph::META_INSET) {
1284 getInset(i)->linuxdoc(buf, os, runparams);
1289 if (style->latexparam() == "CDATA") {
1290 // "TeX"-Mode on == > SGML-Mode on.
1297 boost::tie(ws, str) = sgml::escapeChar(c);
1298 if (ws && !isFreeSpacing()) {
1299 // in freespacing mode, spaces are
1300 // non-breaking characters
1301 if (desc_on) { // if char is ' ' then...
1303 sgmlLineBreak(os, char_line_count, 6);
1307 sgmlLineBreak(os, char_line_count, 1);
1312 char_line_count += str.length();
1318 while (!tag_state.empty()) {
1319 os << "</" << tag_name(tag_state.top()) << '>';
1323 // resets description flag correctly
1325 // <tag> not closed...
1326 sgmlLineBreak(os, char_line_count, 6);
1332 string Paragraph::getDocbookId() const
1334 for (pos_type i = 0; i < size(); ++i) {
1336 InsetBase const * inset = getInset(i);
1337 InsetBase::Code lyx_code = inset->lyxCode();
1338 if (lyx_code == InsetBase::LABEL_CODE) {
1339 return static_cast<InsetCommand const *>(inset)->getContents();
1348 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1350 LyXFont const & outerfont,
1351 OutputParams const & runparams,
1352 lyx::depth_type depth) const
1354 bool emph_flag = false;
1356 LyXLayout_ptr const & style = layout();
1357 LyXLayout_ptr const & defaultstyle =
1358 buf.params().getLyXTextClass().defaultLayout();
1361 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1363 int char_line_count = depth;
1364 bool label_closed = true;
1365 bool para_closed = true;
1367 if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1369 Counters & counters = buf.params().getLyXTextClass().counters();
1370 if (!style->free_spacing)
1371 os << string(depth,' ');
1372 if (!style->labeltag().empty()) {
1373 os << "<" << style->labeltag() << ">\n";
1374 label_closed = false;
1376 if (!defaultstyle->latexparam().empty()) {
1377 counters.step("para");
1378 ls = tostr(counters.value("para"));
1380 + subst(defaultstyle->latexparam(), "#", ls) + '"';
1382 os << "<" << style->itemtag() << ">\n"
1383 << string(depth, ' ') << "<"
1384 << defaultstyle->latexname() << ls << ">\n";
1385 para_closed = false;
1389 // parsing main loop
1390 for (pos_type i = 0; i < size(); ++i) {
1391 LyXFont font = getFont(buf.params(), i, outerfont);
1393 // handle <emphasis> tag
1394 if (font_old.emph() != font.emph()) {
1395 if (font.emph() == LyXFont::ON) {
1396 if (style->latexparam() == "CDATA")
1399 if (style->latexparam() == "CDATA")
1403 if (style->latexparam() == "CDATA")
1405 os << "</emphasis>";
1406 if (style->latexparam() == "CDATA")
1413 InsetBase const * inset = getInset(i);
1414 if (style->latexparam() == "CDATA")
1416 inset->docbook(buf, os, runparams);
1417 if (style->latexparam() == "CDATA")
1420 char c = getChar(i);
1423 boost::tie(ws, str) = sgml::escapeChar(c);
1425 if (style->pass_thru) {
1427 } else if (isFreeSpacing() || c != ' ') {
1429 } else if (!style->labeltag().empty() && !label_closed) {
1431 os << "\n</" << style->labeltag() << "><"
1432 << style->itemtag() << "><"
1433 << defaultstyle->latexname() << ">";
1434 label_closed = true;
1435 para_closed = false;
1444 if (style->latexparam() == "CDATA")
1446 os << "</emphasis>";
1447 if (style->latexparam() == "CDATA")
1451 // resets description flag correctly
1452 if (!label_closed) {
1453 // <term> not closed...
1454 os << "</" << style->labeltag() << ">\n<"
1455 << style->itemtag() << "><"
1456 << defaultstyle->latexname() << "> ";
1459 os << "\n" << string(depth, ' ') << "</"
1460 << defaultstyle->latexname() << ">\n";
1462 if (style->free_spacing)
1469 /// return true if the char is a meta-character for an inset
1471 bool IsInsetChar(char c)
1473 return (c == Paragraph::META_INSET);
1480 bool Paragraph::isHfill(pos_type pos) const
1483 isInset(pos) && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1487 bool Paragraph::isNewline(pos_type pos) const
1490 isInset(pos) && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1494 bool Paragraph::isSeparator(pos_type pos) const
1496 return IsSeparatorChar(getChar(pos));
1500 bool Paragraph::isLineSeparator(pos_type pos) const
1502 value_type const c = getChar(pos);
1503 return IsLineSeparatorChar(c)
1504 || (IsInsetChar(c) && getInset(pos) &&
1505 getInset(pos)->isLineSeparator());
1509 bool Paragraph::isKomma(pos_type pos) const
1511 return IsKommaChar(getChar(pos));
1515 /// Used by the spellchecker
1516 bool Paragraph::isLetter(pos_type pos) const
1518 value_type const c = getChar(pos);
1519 if (IsLetterChar(c))
1522 return getInset(pos)->isLetter();
1523 // We want to pass the ' and escape chars to ispell
1524 string const extra = lyxrc.isp_esc_chars + '\'';
1525 return contains(extra, c);
1529 bool Paragraph::isWord(pos_type pos) const
1532 return getInset(pos)->isLetter();
1533 value_type const c = getChar(pos);
1534 return !(IsSeparatorChar(c)
1541 Paragraph::getParLanguage(BufferParams const & bparams) const
1544 return getFirstFontSettings().language();
1545 #ifdef WITH_WARNINGS
1546 #warning FIXME we should check the prev par as well (Lgb)
1548 return bparams.language;
1552 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1554 return lyxrc.rtl_support
1555 && getParLanguage(bparams)->RightToLeft()
1556 && ownerCode() != InsetBase::ERT_CODE;
1560 void Paragraph::changeLanguage(BufferParams const & bparams,
1561 Language const * from, Language const * to)
1563 for (pos_type i = 0; i < size(); ++i) {
1564 LyXFont font = getFontSettings(bparams, i);
1565 if (font.language() == from) {
1566 font.setLanguage(to);
1573 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1575 Language const * doc_language = bparams.language;
1576 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1577 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1579 for (; cit != end; ++cit)
1580 if (cit->font().language() != ignore_language &&
1581 cit->font().language() != latex_language &&
1582 cit->font().language() != doc_language)
1588 // Convert the paragraph to a string.
1589 // Used for building the table of contents
1590 string const Paragraph::asString(Buffer const & buffer, bool label) const
1592 OutputParams runparams;
1593 return asString(buffer, runparams, label);
1597 string const Paragraph::asString(Buffer const & buffer,
1598 OutputParams const & runparams,
1603 if (label && !params().labelString().empty())
1604 s += params().labelString() + ' ';
1606 for (pos_type i = 0; i < size(); ++i) {
1607 value_type c = getChar(i);
1610 else if (c == META_INSET &&
1611 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1613 getInset(i)->plaintext(buffer, os, runparams);
1614 s += subst(STRCONV(os.str()),'\n',' ');
1620 // This should really be done by the caller and not here.
1621 string ret = asString(buffer, runparams, 0, size(), label);
1622 return subst(ret, '\n', ' ');
1627 string const Paragraph::asString(Buffer const & buffer,
1628 pos_type beg, pos_type end, bool label) const
1631 OutputParams const runparams;
1632 return asString(buffer, runparams, beg, end, label);
1636 string const Paragraph::asString(Buffer const & buffer,
1637 OutputParams const & runparams,
1638 pos_type beg, pos_type end, bool label) const
1642 if (beg == 0 && label && !params().labelString().empty())
1643 os << params().labelString() << ' ';
1645 for (pos_type i = beg; i < end; ++i) {
1646 value_type const c = getUChar(buffer.params(), i);
1649 else if (c == META_INSET)
1650 getInset(i)->plaintext(buffer, os, runparams);
1657 void Paragraph::setInsetOwner(UpdatableInset * inset)
1659 pimpl_->inset_owner = inset;
1663 void Paragraph::setContentsFromPar(Paragraph const & par)
1665 pimpl_->setContentsFromPar(par);
1669 void Paragraph::trackChanges(Change::Type type)
1671 pimpl_->trackChanges(type);
1675 void Paragraph::untrackChanges()
1677 pimpl_->untrackChanges();
1681 void Paragraph::cleanChanges()
1683 pimpl_->cleanChanges();
1687 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1689 BOOST_ASSERT(empty() || pos < size());
1690 return pimpl_->lookupChange(pos);
1694 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1696 BOOST_ASSERT(empty() || pos < size());
1697 return pimpl_->lookupChangeFull(pos);
1701 bool Paragraph::isChanged(pos_type start, pos_type end) const
1703 return pimpl_->isChanged(start, end);
1707 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1709 return pimpl_->isChangeEdited(start, end);
1713 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1715 pimpl_->setChange(pos, type);
1719 void Paragraph::markErased()
1721 pimpl_->markErased();
1725 void Paragraph::acceptChange(pos_type start, pos_type end)
1727 return pimpl_->acceptChange(start, end);
1731 void Paragraph::rejectChange(pos_type start, pos_type end)
1733 return pimpl_->rejectChange(start, end);
1737 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1739 // This is in the critical path!
1740 pos_type const siz = text_.size();
1742 BOOST_ASSERT(0 <= pos);
1743 BOOST_ASSERT(pos <= siz);
1746 lyxerr << "getChar() on pos " << pos << " in par id "
1747 << id() << " of size " << siz
1748 << " is a bit silly !" << endl;
1749 BOOST_ASSERT(false);
1756 int Paragraph::id() const
1762 LyXLayout_ptr const & Paragraph::layout() const
1768 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1770 layout_ = new_layout;
1774 UpdatableInset * Paragraph::inInset() const
1776 return pimpl_->inset_owner;
1780 InsetBase::Code Paragraph::ownerCode() const
1782 return pimpl_->inset_owner
1783 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1787 void Paragraph::clearContents()
1793 void Paragraph::setChar(pos_type pos, value_type c)
1799 ParagraphParameters & Paragraph::params()
1801 return pimpl_->params;
1805 ParagraphParameters const & Paragraph::params() const
1807 return pimpl_->params;
1811 bool Paragraph::isFreeSpacing() const
1813 if (layout()->free_spacing)
1816 // for now we just need this, later should we need this in some
1817 // other way we can always add a function to InsetBase too.
1818 return ownerCode() == InsetBase::ERT_CODE;
1822 bool Paragraph::allowEmpty() const
1824 if (layout()->keepempty)
1826 return ownerCode() == InsetBase::ERT_CODE;
1830 RowList::iterator Paragraph::getRow(pos_type pos)
1832 RowList::iterator rit = rows.end();
1833 RowList::iterator const begin = rows.begin();
1835 for (--rit; rit != begin && rit->pos() > pos; --rit)
1842 RowList::const_iterator Paragraph::getRow(pos_type pos) const
1844 RowList::const_iterator rit = rows.end();
1845 RowList::const_iterator const begin = rows.begin();
1847 for (--rit; rit != begin && rit->pos() > pos; --rit)
1854 size_t Paragraph::row(pos_type pos) const
1856 RowList::const_iterator rit = rows.end();
1857 RowList::const_iterator const begin = rows.begin();
1859 for (--rit; rit != begin && rit->pos() > pos; --rit)
1866 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1868 if (!Encodings::is_arabic(c))
1869 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1870 return c + (0xb0 - '0');
1874 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1875 unsigned char next_char = ' ';
1877 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1878 unsigned char const par_char = getChar(i);
1879 if (!Encodings::IsComposeChar_arabic(par_char)) {
1880 next_char = par_char;
1885 if (Encodings::is_arabic(next_char)) {
1886 if (Encodings::is_arabic(prev_char) &&
1887 !Encodings::is_arabic_special(prev_char))
1888 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1890 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1892 if (Encodings::is_arabic(prev_char) &&
1893 !Encodings::is_arabic_special(prev_char))
1894 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1896 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);