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 InsetBase * Paragraph::getInset(pos_type pos)
305 BOOST_ASSERT(pos < size());
306 return insetlist.get(pos);
310 InsetBase const * Paragraph::getInset(pos_type pos) const
312 BOOST_ASSERT(pos < size());
313 return insetlist.get(pos);
317 // Gets uninstantiated font setting at position.
318 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
322 lyxerr << " pos: " << pos << " size: " << size() << endl;
323 BOOST_ASSERT(pos <= size());
326 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
327 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
328 for (; cit != end; ++cit)
329 if (cit->pos() >= pos)
335 if (pos == size() && !empty())
336 return getFontSettings(bparams, pos - 1);
338 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
342 std::pair<lyx::pos_type, lyx::pos_type> Paragraph::getFontSpan(lyx::pos_type pos) const
344 BOOST_ASSERT(pos <= size());
345 lyx::pos_type start = 0;
347 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
348 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
349 for (; cit != end; ++cit) {
350 if (cit->pos() >= pos)
351 return std::make_pair(start, cit->pos());
352 start = cit->pos() + 1;
355 // This should not happen, but if so, we take no chances.
356 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
358 return std::make_pair(pos, pos);
362 // Gets uninstantiated font setting at position 0
363 LyXFont const Paragraph::getFirstFontSettings() const
365 if (!empty() && !pimpl_->fontlist.empty())
366 return pimpl_->fontlist[0].font();
368 return LyXFont(LyXFont::ALL_INHERIT);
372 // Gets the fully instantiated font at a given position in a paragraph
373 // This is basically the same function as LyXText::GetFont() in text2.C.
374 // The difference is that this one is used for generating the LaTeX file,
375 // and thus cosmetic "improvements" are disallowed: This has to deliver
376 // the true picture of the buffer. (Asger)
377 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
378 LyXFont const & outerfont) const
380 BOOST_ASSERT(pos >= 0);
382 LyXLayout_ptr const & lout = layout();
384 pos_type const body_pos = beginOfBody();
388 layoutfont = lout->labelfont;
390 layoutfont = lout->font;
392 LyXFont font = getFontSettings(bparams, pos);
393 font.realize(layoutfont);
394 font.realize(outerfont);
395 font.realize(bparams.getLyXTextClass().defaultfont());
401 LyXFont const Paragraph::getLabelFont
402 (BufferParams const & bparams, LyXFont const & outerfont) const
404 LyXFont tmpfont = layout()->labelfont;
405 tmpfont.setLanguage(getParLanguage(bparams));
406 tmpfont.realize(outerfont);
407 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
412 LyXFont const Paragraph::getLayoutFont
413 (BufferParams const & bparams, LyXFont const & outerfont) const
415 LyXFont tmpfont = layout()->font;
416 tmpfont.setLanguage(getParLanguage(bparams));
417 tmpfont.realize(outerfont);
418 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
423 /// Returns the height of the highest font in range
424 LyXFont_size Paragraph::highestFontInRange
425 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
427 if (pimpl_->fontlist.empty())
430 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
431 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
432 for (; end_it != end; ++end_it) {
433 if (end_it->pos() >= endpos)
440 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
441 for (; cit != end; ++cit) {
442 if (cit->pos() >= startpos)
446 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
447 for (; cit != end_it; ++cit) {
448 LyXFont::FONT_SIZE size = cit->font().size();
449 if (size == LyXFont::INHERIT_SIZE)
451 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
458 Paragraph::value_type
459 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
461 value_type c = getChar(pos);
462 if (!lyxrc.rtl_support)
492 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
499 void Paragraph::setFont(pos_type pos, LyXFont const & font)
501 BOOST_ASSERT(pos <= size());
503 // First, reduce font against layout/label font
504 // Update: The setCharFont() routine in text2.C already
505 // reduces font, so we don't need to do that here. (Asger)
506 // No need to simplify this because it will disappear
507 // in a new kernel. (Asger)
508 // Next search font table
510 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
511 Pimpl::FontList::iterator it = beg;
512 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
513 for (; it != endit; ++it) {
514 if (it->pos() >= pos)
517 size_t const i = distance(beg, it);
518 bool notfound = (it == endit);
520 if (!notfound && pimpl_->fontlist[i].font() == font)
523 bool begin = pos == 0 || notfound ||
524 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
525 // Is position pos is a beginning of a font block?
526 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
527 // Is position pos is the end of a font block?
528 if (begin && end) { // A single char block
529 if (i + 1 < pimpl_->fontlist.size() &&
530 pimpl_->fontlist[i + 1].font() == font) {
531 // Merge the singleton block with the next block
532 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
533 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
534 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
535 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
536 // Merge the singleton block with the previous block
537 pimpl_->fontlist[i - 1].pos(pos);
538 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
540 pimpl_->fontlist[i].font(font);
542 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
543 pimpl_->fontlist[i - 1].pos(pos);
545 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
546 Pimpl::FontTable(pos, font));
548 pimpl_->fontlist[i].pos(pos - 1);
549 if (!(i + 1 < pimpl_->fontlist.size() &&
550 pimpl_->fontlist[i + 1].font() == font))
551 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
552 Pimpl::FontTable(pos, font));
553 } else { // The general case. The block is splitted into 3 blocks
554 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
555 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
556 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
557 Pimpl::FontTable(pos, font));
562 void Paragraph::makeSameLayout(Paragraph const & par)
564 layout(par.layout());
566 params() = par.params();
570 int Paragraph::stripLeadingSpaces()
576 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
577 // Set Change::Type to Change::INSERTED to quietly remove it
578 setChange(0, Change::INSERTED);
587 bool Paragraph::hasSameLayout(Paragraph const & par) const
589 return par.layout() == layout() && params().sameLayout(par.params());
593 Paragraph::depth_type Paragraph::getDepth() const
595 return params().depth();
599 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
601 if (layout()->isEnvironment())
602 return params().depth() + 1;
604 return params().depth();
608 char Paragraph::getAlign() const
610 return params().align();
614 string const & Paragraph::getLabelstring() const
616 return params().labelString();
620 // the next two functions are for the manual labels
621 string const Paragraph::getLabelWidthString() const
623 if (!params().labelWidthString().empty())
624 return params().labelWidthString();
626 return _("Senseless with this layout!");
630 void Paragraph::setLabelWidthString(string const & s)
632 params().labelWidthString(s);
636 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
639 params().labelWidthString(string());
640 params().align(LYX_ALIGN_LAYOUT);
641 params().spacing(Spacing(Spacing::Default));
645 pos_type Paragraph::beginOfBody() const
647 return begin_of_body_;
651 void Paragraph::setBeginOfBody()
653 if (layout()->labeltype != LABEL_MANUAL) {
658 // Unroll the first two cycles of the loop
659 // and remember the previous character to
660 // remove unnecessary getChar() calls
662 pos_type end = size();
663 if (i < end && !isNewline(i)) {
665 char previous_char = 0;
668 previous_char = text_[i];
671 while (i < end && previous_char != ' ') {
676 previous_char = temp;
686 // returns -1 if inset not found
687 int Paragraph::getPositionOfInset(InsetBase const * inset) const
690 InsetList::const_iterator it = insetlist.begin();
691 InsetList::const_iterator end = insetlist.end();
692 for (; it != end; ++it)
693 if (it->inset == inset)
699 InsetBibitem * Paragraph::bibitem() const
701 if (!insetlist.empty()) {
702 InsetBase * inset = insetlist.begin()->inset;
703 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
704 return static_cast<InsetBibitem *>(inset);
710 bool Paragraph::forceDefaultParagraphs() const
712 return inInset() && inInset()->forceDefaultParagraphs(inInset());
718 // paragraphs inside floats need different alignment tags to avoid
721 bool noTrivlistCentering(InsetBase::Code code)
723 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
727 string correction(string const & orig)
729 if (orig == "flushleft")
730 return "raggedright";
731 if (orig == "flushright")
733 if (orig == "center")
739 string const corrected_env(string const & suffix, string const & env,
740 InsetBase::Code code)
742 string output = suffix + "{";
743 if (noTrivlistCentering(code))
744 output += correction(env);
753 // This could go to ParagraphParameters if we want to
754 int Paragraph::startTeXParParams(BufferParams const & bparams,
755 ostream & os, bool moving_arg) const
759 if (params().noindent()) {
764 switch (params().align()) {
766 case LYX_ALIGN_BLOCK:
767 case LYX_ALIGN_LAYOUT:
768 case LYX_ALIGN_SPECIAL:
771 case LYX_ALIGN_RIGHT:
772 case LYX_ALIGN_CENTER:
780 switch (params().align()) {
782 case LYX_ALIGN_BLOCK:
783 case LYX_ALIGN_LAYOUT:
784 case LYX_ALIGN_SPECIAL:
786 case LYX_ALIGN_LEFT: {
788 if (getParLanguage(bparams)->babel() != "hebrew")
789 output = corrected_env("\\begin", "flushleft", ownerCode());
791 output = corrected_env("\\begin", "flushright", ownerCode());
793 column += output.size();
795 } case LYX_ALIGN_RIGHT: {
797 if (getParLanguage(bparams)->babel() != "hebrew")
798 output = corrected_env("\\begin", "flushright", ownerCode());
800 output = corrected_env("\\begin", "flushleft", ownerCode());
802 column += output.size();
804 } case LYX_ALIGN_CENTER: {
806 output = corrected_env("\\begin", "center", ownerCode());
808 column += output.size();
817 // This could go to ParagraphParameters if we want to
818 int Paragraph::endTeXParParams(BufferParams const & bparams,
819 ostream & os, bool moving_arg) const
823 switch (params().align()) {
825 case LYX_ALIGN_BLOCK:
826 case LYX_ALIGN_LAYOUT:
827 case LYX_ALIGN_SPECIAL:
830 case LYX_ALIGN_RIGHT:
831 case LYX_ALIGN_CENTER:
839 switch (params().align()) {
841 case LYX_ALIGN_BLOCK:
842 case LYX_ALIGN_LAYOUT:
843 case LYX_ALIGN_SPECIAL:
845 case LYX_ALIGN_LEFT: {
847 if (getParLanguage(bparams)->babel() != "hebrew")
848 output = corrected_env("\\par\\end", "flushleft", ownerCode());
850 output = corrected_env("\\par\\end", "flushright", ownerCode());
852 column += output.size();
854 } case LYX_ALIGN_RIGHT: {
856 if (getParLanguage(bparams)->babel() != "hebrew")
857 output = corrected_env("\\par\\end", "flushright", ownerCode());
859 output = corrected_env("\\par\\end", "flushleft", ownerCode());
861 column += output.size();
863 } case LYX_ALIGN_CENTER: {
865 output = corrected_env("\\par\\end", "center", ownerCode());
867 column += output.size();
876 // This one spits out the text of the paragraph
877 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
878 BufferParams const & bparams,
879 LyXFont const & outerfont,
880 ostream & os, TexRow & texrow,
881 OutputParams const & runparams) const
883 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
885 bool return_value = false;
889 // well we have to check if we are in an inset with unlimited
890 // length (all in one row) if that is true then we don't allow
891 // any special options in the paragraph and also we don't allow
892 // any environment other then "Standard" to be valid!
893 bool asdefault = forceDefaultParagraphs();
896 style = bparams.getLyXTextClass().defaultLayout();
903 LaTeXFeatures features(buf, bparams, runparams.nice);
905 // output change tracking marks only if desired,
906 // if dvipost is installed,
907 // and with dvi/ps (other formats don't work)
908 bool const output = bparams.output_changes
909 && runparams.flavor == OutputParams::LATEX
910 && features.isAvailable("dvipost");
912 // Maybe we have to create a optional argument.
913 pos_type body_pos = beginOfBody();
914 unsigned int column = 0;
917 // the optional argument is kept in curly brackets in
918 // case it contains a ']'
921 basefont = getLabelFont(bparams, outerfont);
923 basefont = getLayoutFont(bparams, outerfont);
926 // Which font is currently active?
927 LyXFont running_font(basefont);
928 // Do we have an open font change?
929 bool open_font = false;
931 Change::Type running_change = Change::UNCHANGED;
933 texrow.start(id(), 0);
935 // if the paragraph is empty, the loop will not be entered at all
937 if (style->isCommand()) {
942 column += startTeXParParams(bparams, os,
943 runparams.moving_arg);
946 for (pos_type i = 0; i < size(); ++i) {
948 // First char in paragraph or after label?
952 column += running_font.latexWriteEndChanges(os, basefont, basefont);
955 basefont = getLayoutFont(bparams, outerfont);
956 running_font = basefont;
960 if (style->isCommand()) {
966 column += startTeXParParams(bparams, os,
967 runparams.moving_arg);
970 value_type c = getChar(i);
972 // Fully instantiated font
973 LyXFont font = getFont(bparams, i, outerfont);
975 LyXFont const last_font = running_font;
977 // Spaces at end of font change are simulated to be
978 // outside font change, i.e. we write "\textXX{text} "
979 // rather than "\textXX{text }". (Asger)
980 if (open_font && c == ' ' && i <= size() - 2) {
981 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
982 if (next_font != running_font && next_font != font) {
987 // We end font definition before blanks
989 (font != running_font ||
990 font.language() != running_font.language()))
992 column += running_font.latexWriteEndChanges(os,
994 (i == body_pos-1) ? basefont : font);
995 running_font = basefont;
999 // Blanks are printed before start of fontswitch
1001 // Do not print the separation of the optional argument
1002 if (i != body_pos - 1) {
1003 pimpl_->simpleTeXBlanks(os, texrow, i,
1004 column, font, *style);
1008 // Do we need to change font?
1009 if ((font != running_font ||
1010 font.language() != running_font.language()) &&
1013 column += font.latexWriteStartChanges(os, basefont,
1015 running_font = font;
1019 Change::Type change = pimpl_->lookupChange(i);
1021 column += Changes::latexMarkChange(os, running_change,
1023 running_change = change;
1025 // do not output text which is marked deleted
1026 // if change tracking output is not desired
1027 if (output || running_change != Change::DELETED) {
1028 OutputParams rp = runparams;
1029 rp.free_spacing = style->free_spacing;
1030 rp.local_language = font.language();
1031 rp.intitle = style->intitle;
1032 pimpl_->simpleTeXSpecialChars(buf, bparams,
1035 basefont, outerfont, open_font,
1037 *style, i, column, c);
1041 column += Changes::latexMarkChange(os,
1042 running_change, Change::UNCHANGED, output);
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 value_type 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 &&
1338 lyx_code != InsetBase::INCLUDE_CODE &&
1339 lyx_code != InsetBase::GRAPHICS_CODE &&
1340 lyx_code != InsetBase::ERT_CODE &&
1341 lyx_code != InsetBase::FLOAT_CODE &&
1342 lyx_code != InsetBase::TABULAR_CODE) {
1346 value_type c = getChar(i);
1347 if (c != ' ' && c != '\t')
1355 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1357 for (pos_type i = 0; i < size(); ++i) {
1359 InsetBase const * inset = getInset(i);
1360 InsetBase::Code lyx_code = inset->lyxCode();
1361 if (lyx_code == InsetBase::LABEL_CODE) {
1362 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1363 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1372 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1375 for (i = 0; i < size(); ++i) {
1377 InsetBase const * inset = getInset(i);
1378 inset->docbook(buf, os, runparams);
1380 value_type c = getChar(i);
1385 boost::tie(ws, str) = sgml::escapeChar(c);
1394 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1398 for (pos_type i = initial; i < size(); ++i) {
1399 LyXFont font = getFont(buf.params(), i, outerfont);
1402 if (i != initial && font != font_old)
1411 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1413 OutputParams const & runparams,
1414 LyXFont const & outerfont,
1415 pos_type initial) const
1417 bool emph_flag = false;
1419 LyXLayout_ptr const & style = layout();
1421 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1423 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1426 // parsing main loop
1427 for (pos_type i = initial; i < size(); ++i) {
1428 LyXFont font = getFont(buf.params(), i, outerfont);
1430 // handle <emphasis> tag
1431 if (font_old.emph() != font.emph()) {
1432 if (font.emph() == LyXFont::ON) {
1435 } else if (i != initial) {
1436 os << "</emphasis>";
1442 InsetBase const * inset = getInset(i);
1443 inset->docbook(buf, os, runparams);
1445 value_type c = getChar(i);
1448 boost::tie(ws, str) = sgml::escapeChar(c);
1450 if (style->pass_thru)
1459 os << "</emphasis>";
1462 if (style->free_spacing)
1464 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1471 /// return true if the char is a meta-character for an inset
1473 bool IsInsetChar(char c)
1475 return (c == Paragraph::META_INSET);
1482 bool Paragraph::isHfill(pos_type pos) const
1485 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1489 bool Paragraph::isNewline(pos_type pos) const
1492 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1496 bool Paragraph::isSeparator(pos_type pos) const
1498 return IsSeparatorChar(getChar(pos));
1502 bool Paragraph::isLineSeparator(pos_type pos) const
1504 value_type const c = getChar(pos);
1505 return IsLineSeparatorChar(c)
1506 || (IsInsetChar(c) && getInset(pos) &&
1507 getInset(pos)->isLineSeparator());
1511 /// Used by the spellchecker
1512 bool Paragraph::isLetter(pos_type pos) const
1515 return getInset(pos)->isLetter();
1517 value_type const c = getChar(pos);
1518 return IsLetterChar(c) || IsDigit(c);
1524 Paragraph::getParLanguage(BufferParams const & bparams) const
1527 return getFirstFontSettings().language();
1528 #ifdef WITH_WARNINGS
1529 #warning FIXME we should check the prev par as well (Lgb)
1531 return bparams.language;
1535 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1537 return lyxrc.rtl_support
1538 && getParLanguage(bparams)->RightToLeft()
1539 && ownerCode() != InsetBase::ERT_CODE;
1543 void Paragraph::changeLanguage(BufferParams const & bparams,
1544 Language const * from, Language const * to)
1546 for (pos_type i = 0; i < size(); ++i) {
1547 LyXFont font = getFontSettings(bparams, i);
1548 if (font.language() == from) {
1549 font.setLanguage(to);
1556 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1558 Language const * doc_language = bparams.language;
1559 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1560 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1562 for (; cit != end; ++cit)
1563 if (cit->font().language() != ignore_language &&
1564 cit->font().language() != latex_language &&
1565 cit->font().language() != doc_language)
1571 // Convert the paragraph to a string.
1572 // Used for building the table of contents
1573 string const Paragraph::asString(Buffer const & buffer, bool label) const
1575 OutputParams runparams;
1576 return asString(buffer, runparams, label);
1580 string const Paragraph::asString(Buffer const & buffer,
1581 OutputParams const & runparams,
1586 if (label && !params().labelString().empty())
1587 s += params().labelString() + ' ';
1589 for (pos_type i = 0; i < size(); ++i) {
1590 value_type c = getChar(i);
1593 else if (c == META_INSET &&
1594 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1596 getInset(i)->plaintext(buffer, os, runparams);
1597 s += subst(STRCONV(os.str()),'\n',' ');
1603 // This should really be done by the caller and not here.
1604 string ret = asString(buffer, runparams, 0, size(), label);
1605 return subst(ret, '\n', ' ');
1610 string const Paragraph::asString(Buffer const & buffer,
1611 pos_type beg, pos_type end, bool label) const
1614 OutputParams const runparams;
1615 return asString(buffer, runparams, beg, end, label);
1619 string const Paragraph::asString(Buffer const & buffer,
1620 OutputParams const & runparams,
1621 pos_type beg, pos_type end, bool label) const
1625 if (beg == 0 && label && !params().labelString().empty())
1626 os << params().labelString() << ' ';
1628 for (pos_type i = beg; i < end; ++i) {
1629 value_type const c = getUChar(buffer.params(), i);
1632 else if (c == META_INSET)
1633 getInset(i)->plaintext(buffer, os, runparams);
1640 void Paragraph::setInsetOwner(InsetBase * inset)
1642 pimpl_->inset_owner = inset;
1646 void Paragraph::setContentsFromPar(Paragraph const & par)
1648 pimpl_->setContentsFromPar(par);
1652 void Paragraph::trackChanges(Change::Type type)
1654 pimpl_->trackChanges(type);
1658 void Paragraph::untrackChanges()
1660 pimpl_->untrackChanges();
1664 void Paragraph::cleanChanges()
1666 pimpl_->cleanChanges();
1670 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1672 BOOST_ASSERT(empty() || pos < size());
1673 return pimpl_->lookupChange(pos);
1677 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1679 BOOST_ASSERT(empty() || pos < size());
1680 return pimpl_->lookupChangeFull(pos);
1684 bool Paragraph::isChanged(pos_type start, pos_type end) const
1686 return pimpl_->isChanged(start, end);
1690 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1692 return pimpl_->isChangeEdited(start, end);
1696 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1698 pimpl_->setChange(pos, type);
1702 void Paragraph::markErased()
1704 pimpl_->markErased();
1708 void Paragraph::acceptChange(pos_type start, pos_type end)
1710 return pimpl_->acceptChange(start, end);
1714 void Paragraph::rejectChange(pos_type start, pos_type end)
1716 return pimpl_->rejectChange(start, end);
1720 int Paragraph::id() const
1726 LyXLayout_ptr const & Paragraph::layout() const
1732 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1734 layout_ = new_layout;
1738 InsetBase * Paragraph::inInset() const
1740 return pimpl_->inset_owner;
1744 InsetBase::Code Paragraph::ownerCode() const
1746 return pimpl_->inset_owner
1747 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1751 void Paragraph::clearContents()
1757 void Paragraph::setChar(pos_type pos, value_type c)
1763 ParagraphParameters & Paragraph::params()
1765 return pimpl_->params;
1769 ParagraphParameters const & Paragraph::params() const
1771 return pimpl_->params;
1775 bool Paragraph::isFreeSpacing() const
1777 if (layout()->free_spacing)
1780 // for now we just need this, later should we need this in some
1781 // other way we can always add a function to InsetBase too.
1782 return ownerCode() == InsetBase::ERT_CODE;
1786 bool Paragraph::allowEmpty() const
1788 if (layout()->keepempty)
1790 return ownerCode() == InsetBase::ERT_CODE;
1794 Row & Paragraph::getRow(pos_type pos)
1796 BOOST_ASSERT(!rows().empty());
1798 RowList::iterator rit = rows_.end();
1799 RowList::iterator const begin = rows_.begin();
1801 for (--rit; rit != begin && rit->pos() > pos; --rit)
1808 Row const & Paragraph::getRow(pos_type pos) const
1810 BOOST_ASSERT(!rows().empty());
1812 RowList::const_iterator rit = rows_.end();
1813 RowList::const_iterator const begin = rows_.begin();
1815 for (--rit; rit != begin && rit->pos() > pos; --rit)
1822 size_t Paragraph::pos2row(pos_type pos) const
1824 BOOST_ASSERT(!rows().empty());
1826 RowList::const_iterator rit = rows_.end();
1827 RowList::const_iterator const begin = rows_.begin();
1829 for (--rit; rit != begin && rit->pos() > pos; --rit)
1836 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1838 if (!Encodings::is_arabic(c))
1839 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1840 return c + (0xb0 - '0');
1844 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1845 unsigned char next_char = ' ';
1847 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1848 unsigned char const par_char = getChar(i);
1849 if (!Encodings::IsComposeChar_arabic(par_char)) {
1850 next_char = par_char;
1855 if (Encodings::is_arabic(next_char)) {
1856 if (Encodings::is_arabic(prev_char) &&
1857 !Encodings::is_arabic_special(prev_char))
1858 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1860 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1862 if (Encodings::is_arabic(prev_char) &&
1863 !Encodings::is_arabic_special(prev_char))
1864 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1866 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1871 void Paragraph::dump() const
1873 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1874 for (size_t i = 0; i != rows_.size(); ++i) {
1875 lyxerr << " row " << i << ": ";