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 int size = str.size();
264 for (int i = 0 ; i < size ; ++i)
265 insertChar(start + i, str[i], font);
269 bool Paragraph::checkInsertChar(LyXFont &)
275 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
278 pimpl_->insertChar(pos, c, change);
282 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
283 LyXFont const & font, Change change)
285 pimpl_->insertChar(pos, c, change);
290 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
292 pimpl_->insertInset(pos, inset, change);
296 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
297 LyXFont const & font, Change change)
299 pimpl_->insertInset(pos, inset, change);
304 bool Paragraph::insetAllowed(InsetBase_code code)
306 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
310 InsetBase * Paragraph::getInset(pos_type pos)
312 BOOST_ASSERT(pos < size());
313 return insetlist.get(pos);
317 InsetBase const * Paragraph::getInset(pos_type pos) const
319 BOOST_ASSERT(pos < size());
320 return insetlist.get(pos);
324 // Gets uninstantiated font setting at position.
325 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
329 lyxerr << " pos: " << pos << " size: " << size() << endl;
330 BOOST_ASSERT(pos <= size());
333 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
334 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
335 for (; cit != end; ++cit)
336 if (cit->pos() >= pos)
342 if (pos == size() && !empty())
343 return getFontSettings(bparams, pos - 1);
345 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
349 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
351 BOOST_ASSERT(pos <= size());
353 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
354 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
355 for (; cit != end; ++cit)
356 if (cit->pos() >= pos)
359 // This should not happen, but if so, we take no chances.
360 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
366 // Gets uninstantiated font setting at position 0
367 LyXFont const Paragraph::getFirstFontSettings() const
369 if (!empty() && !pimpl_->fontlist.empty())
370 return pimpl_->fontlist[0].font();
372 return LyXFont(LyXFont::ALL_INHERIT);
376 // Gets the fully instantiated font at a given position in a paragraph
377 // This is basically the same function as LyXText::GetFont() in text2.C.
378 // The difference is that this one is used for generating the LaTeX file,
379 // and thus cosmetic "improvements" are disallowed: This has to deliver
380 // the true picture of the buffer. (Asger)
381 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
382 LyXFont const & outerfont) const
384 BOOST_ASSERT(pos >= 0);
386 LyXLayout_ptr const & lout = layout();
388 pos_type const body_pos = beginOfBody();
392 layoutfont = lout->labelfont;
394 layoutfont = lout->font;
396 LyXFont font = getFontSettings(bparams, pos);
397 font.realize(layoutfont);
398 font.realize(outerfont);
399 font.realize(bparams.getLyXTextClass().defaultfont());
405 LyXFont const Paragraph::getLabelFont
406 (BufferParams const & bparams, LyXFont const & outerfont) const
408 LyXFont tmpfont = layout()->labelfont;
409 tmpfont.setLanguage(getParLanguage(bparams));
410 tmpfont.realize(outerfont);
411 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
416 LyXFont const Paragraph::getLayoutFont
417 (BufferParams const & bparams, LyXFont const & outerfont) const
419 LyXFont tmpfont = layout()->font;
420 tmpfont.setLanguage(getParLanguage(bparams));
421 tmpfont.realize(outerfont);
422 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
427 /// Returns the height of the highest font in range
428 LyXFont_size Paragraph::highestFontInRange
429 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
431 if (pimpl_->fontlist.empty())
434 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
435 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
436 for (; end_it != end; ++end_it) {
437 if (end_it->pos() >= endpos)
444 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
445 for (; cit != end; ++cit) {
446 if (cit->pos() >= startpos)
450 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
451 for (; cit != end_it; ++cit) {
452 LyXFont::FONT_SIZE size = cit->font().size();
453 if (size == LyXFont::INHERIT_SIZE)
455 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
462 Paragraph::value_type
463 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
465 value_type c = getChar(pos);
466 if (!lyxrc.rtl_support)
496 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
503 void Paragraph::setFont(pos_type pos, LyXFont const & font)
505 BOOST_ASSERT(pos <= size());
507 // First, reduce font against layout/label font
508 // Update: The setCharFont() routine in text2.C already
509 // reduces font, so we don't need to do that here. (Asger)
510 // No need to simplify this because it will disappear
511 // in a new kernel. (Asger)
512 // Next search font table
514 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
515 Pimpl::FontList::iterator it = beg;
516 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
517 for (; it != endit; ++it) {
518 if (it->pos() >= pos)
521 unsigned int i = distance(beg, it);
522 bool notfound = (it == endit);
524 if (!notfound && pimpl_->fontlist[i].font() == font)
527 bool begin = pos == 0 || notfound ||
528 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
529 // Is position pos is a beginning of a font block?
530 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
531 // Is position pos is the end of a font block?
532 if (begin && end) { // A single char block
533 if (i + 1 < pimpl_->fontlist.size() &&
534 pimpl_->fontlist[i + 1].font() == font) {
535 // Merge the singleton block with the next block
536 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
537 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
538 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
539 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
540 // Merge the singleton block with the previous block
541 pimpl_->fontlist[i - 1].pos(pos);
542 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
544 pimpl_->fontlist[i].font(font);
546 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
547 pimpl_->fontlist[i - 1].pos(pos);
549 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
550 Pimpl::FontTable(pos, font));
552 pimpl_->fontlist[i].pos(pos - 1);
553 if (!(i + 1 < pimpl_->fontlist.size() &&
554 pimpl_->fontlist[i + 1].font() == font))
555 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
556 Pimpl::FontTable(pos, font));
557 } else { // The general case. The block is splitted into 3 blocks
558 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
559 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
560 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
561 Pimpl::FontTable(pos, font));
566 void Paragraph::makeSameLayout(Paragraph const & par)
568 layout(par.layout());
570 params() = par.params();
574 int Paragraph::stripLeadingSpaces()
580 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
581 // Set Change::Type to Change::INSERTED to quietly remove it
582 setChange(0, Change::INSERTED);
591 bool Paragraph::hasSameLayout(Paragraph const & par) const
593 return par.layout() == layout() && params().sameLayout(par.params());
597 Paragraph::depth_type Paragraph::getDepth() const
599 return params().depth();
603 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
605 if (layout()->isEnvironment())
606 return params().depth() + 1;
608 return params().depth();
612 char Paragraph::getAlign() const
614 return params().align();
618 string const & Paragraph::getLabelstring() const
620 return params().labelString();
624 // the next two functions are for the manual labels
625 string const Paragraph::getLabelWidthString() const
627 if (!params().labelWidthString().empty())
628 return params().labelWidthString();
630 return _("Senseless with this layout!");
634 void Paragraph::setLabelWidthString(string const & s)
636 params().labelWidthString(s);
640 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
643 params().labelWidthString(string());
644 params().align(LYX_ALIGN_LAYOUT);
645 params().spacing(Spacing(Spacing::Default));
649 pos_type Paragraph::beginOfBody() const
651 return begin_of_body_;
655 void Paragraph::setBeginOfBody()
657 if (layout()->labeltype != LABEL_MANUAL) {
662 // Unroll the first two cycles of the loop
663 // and remember the previous character to
664 // remove unnecessary getChar() calls
666 pos_type end = size();
667 if (i < end && !isNewline(i)) {
669 char previous_char = 0;
672 previous_char = text_[i];
675 while (i < end && previous_char != ' ') {
680 previous_char = temp;
690 // returns -1 if inset not found
691 int Paragraph::getPositionOfInset(InsetBase const * inset) const
694 InsetList::const_iterator it = insetlist.begin();
695 InsetList::const_iterator end = insetlist.end();
696 for (; it != end; ++it)
697 if (it->inset == inset)
703 InsetBibitem * Paragraph::bibitem() const
705 if (!insetlist.empty()) {
706 InsetBase * inset = insetlist.begin()->inset;
707 if (inset->lyxCode() == InsetBase::BIBTEX_CODE)
708 return static_cast<InsetBibitem *>(inset);
714 bool Paragraph::forceDefaultParagraphs() const
716 return inInset() && inInset()->forceDefaultParagraphs(inInset());
722 // paragraphs inside floats need different alignment tags to avoid
725 bool noTrivlistCentering(InsetBase::Code code)
727 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
731 string correction(string const & orig)
733 if (orig == "flushleft")
734 return "raggedright";
735 if (orig == "flushright")
737 if (orig == "center")
743 string const corrected_env(string const & suffix, string const & env,
744 InsetBase::Code code)
746 string output = suffix + "{";
747 if (noTrivlistCentering(code))
748 output += correction(env);
757 // This could go to ParagraphParameters if we want to
758 int Paragraph::startTeXParParams(BufferParams const & bparams,
759 ostream & os, bool moving_arg) const
763 if (params().noindent()) {
768 switch (params().align()) {
770 case LYX_ALIGN_BLOCK:
771 case LYX_ALIGN_LAYOUT:
772 case LYX_ALIGN_SPECIAL:
775 case LYX_ALIGN_RIGHT:
776 case LYX_ALIGN_CENTER:
784 switch (params().align()) {
786 case LYX_ALIGN_BLOCK:
787 case LYX_ALIGN_LAYOUT:
788 case LYX_ALIGN_SPECIAL:
790 case LYX_ALIGN_LEFT: {
792 if (getParLanguage(bparams)->babel() != "hebrew")
793 output = corrected_env("\\begin", "flushleft", ownerCode());
795 output = corrected_env("\\begin", "flushright", ownerCode());
797 column += output.size();
799 } case LYX_ALIGN_RIGHT: {
801 if (getParLanguage(bparams)->babel() != "hebrew")
802 output = corrected_env("\\begin", "flushright", ownerCode());
804 output = corrected_env("\\begin", "flushleft", ownerCode());
806 column += output.size();
808 } case LYX_ALIGN_CENTER: {
810 output = corrected_env("\\begin", "center", ownerCode());
812 column += output.size();
821 // This could go to ParagraphParameters if we want to
822 int Paragraph::endTeXParParams(BufferParams const & bparams,
823 ostream & os, bool moving_arg) const
827 switch (params().align()) {
829 case LYX_ALIGN_BLOCK:
830 case LYX_ALIGN_LAYOUT:
831 case LYX_ALIGN_SPECIAL:
834 case LYX_ALIGN_RIGHT:
835 case LYX_ALIGN_CENTER:
843 switch (params().align()) {
845 case LYX_ALIGN_BLOCK:
846 case LYX_ALIGN_LAYOUT:
847 case LYX_ALIGN_SPECIAL:
849 case LYX_ALIGN_LEFT: {
851 if (getParLanguage(bparams)->babel() != "hebrew")
852 output = corrected_env("\\par\\end", "flushleft", ownerCode());
854 output = corrected_env("\\par\\end", "flushright", ownerCode());
856 column += output.size();
858 } case LYX_ALIGN_RIGHT: {
860 if (getParLanguage(bparams)->babel() != "hebrew")
861 output = corrected_env("\\par\\end", "flushright", ownerCode());
863 output = corrected_env("\\par\\end", "flushleft", ownerCode());
865 column += output.size();
867 } case LYX_ALIGN_CENTER: {
869 output = corrected_env("\\par\\end", "center", ownerCode());
871 column += output.size();
880 // This one spits out the text of the paragraph
881 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
882 BufferParams const & bparams,
883 LyXFont const & outerfont,
884 ostream & os, TexRow & texrow,
885 OutputParams const & runparams) const
887 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
889 bool return_value = false;
893 // well we have to check if we are in an inset with unlimited
894 // length (all in one row) if that is true then we don't allow
895 // any special options in the paragraph and also we don't allow
896 // any environment other then "Standard" to be valid!
897 bool asdefault = forceDefaultParagraphs();
900 style = bparams.getLyXTextClass().defaultLayout();
907 LaTeXFeatures features(buf, bparams, runparams.nice);
909 // output change tracking marks only if desired,
910 // if dvipost is installed,
911 // and with dvi/ps (other formats don't work)
912 bool const output = bparams.output_changes
913 && runparams.flavor == OutputParams::LATEX
914 && features.isAvailable("dvipost");
916 // Maybe we have to create a optional argument.
917 pos_type body_pos = beginOfBody();
918 unsigned int column = 0;
921 // the optional argument is kept in curly brackets in
922 // case it contains a ']'
925 basefont = getLabelFont(bparams, outerfont);
927 basefont = getLayoutFont(bparams, outerfont);
930 // Which font is currently active?
931 LyXFont running_font(basefont);
932 // Do we have an open font change?
933 bool open_font = false;
935 Change::Type running_change = Change::UNCHANGED;
937 texrow.start(id(), 0);
939 // if the paragraph is empty, the loop will not be entered at all
941 if (style->isCommand()) {
946 column += startTeXParParams(bparams, os,
947 runparams.moving_arg);
950 for (pos_type i = 0; i < size(); ++i) {
952 // First char in paragraph or after label?
956 column += running_font.latexWriteEndChanges(os, basefont, basefont);
959 basefont = getLayoutFont(bparams, outerfont);
960 running_font = basefont;
964 if (style->isCommand()) {
970 column += startTeXParParams(bparams, os,
971 runparams.moving_arg);
974 value_type c = getChar(i);
976 // Fully instantiated font
977 LyXFont font = getFont(bparams, i, outerfont);
979 LyXFont const last_font = running_font;
981 // Spaces at end of font change are simulated to be
982 // outside font change, i.e. we write "\textXX{text} "
983 // rather than "\textXX{text }". (Asger)
984 if (open_font && c == ' ' && i <= size() - 2) {
985 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
986 if (next_font != running_font && next_font != font) {
991 // We end font definition before blanks
993 (font != running_font ||
994 font.language() != running_font.language()))
996 column += running_font.latexWriteEndChanges(os,
998 (i == body_pos-1) ? basefont : font);
999 running_font = basefont;
1003 // Blanks are printed before start of fontswitch
1005 // Do not print the separation of the optional argument
1006 if (i != body_pos - 1) {
1007 pimpl_->simpleTeXBlanks(os, texrow, i,
1008 column, font, *style);
1012 // Do we need to change font?
1013 if ((font != running_font ||
1014 font.language() != running_font.language()) &&
1017 column += font.latexWriteStartChanges(os, basefont,
1019 running_font = font;
1023 Change::Type change = pimpl_->lookupChange(i);
1025 column += Changes::latexMarkChange(os, running_change,
1027 running_change = change;
1029 // do not output text which is marked deleted
1030 // if change tracking output is not desired
1031 if (output || running_change != Change::DELETED) {
1032 OutputParams rp = runparams;
1033 rp.free_spacing = style->free_spacing;
1034 rp.local_language = font.language()->babel();
1035 rp.intitle = style->intitle;
1036 pimpl_->simpleTeXSpecialChars(buf, bparams,
1039 basefont, outerfont, open_font,
1041 *style, i, column, c);
1045 column += Changes::latexMarkChange(os,
1046 running_change, Change::UNCHANGED, output);
1048 // If we have an open font definition, we have to close it
1050 #ifdef FIXED_LANGUAGE_END_DETECTION
1053 .latexWriteEndChanges(os, basefont,
1054 next_->getFont(bparams,
1057 running_font.latexWriteEndChanges(os, basefont,
1061 #ifdef WITH_WARNINGS
1062 //#warning For now we ALWAYS have to close the foreign font settings if they are
1063 //#warning there as we start another \selectlanguage with the next paragraph if
1064 //#warning we are in need of this. This should be fixed sometime (Jug)
1066 running_font.latexWriteEndChanges(os, basefont, basefont);
1070 // Needed if there is an optional argument but no contents.
1071 if (body_pos > 0 && body_pos == size()) {
1073 return_value = false;
1077 column += endTeXParParams(bparams, os, runparams.moving_arg);
1080 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1081 return return_value;
1087 // checks, if newcol chars should be put into this line
1088 // writes newline, if necessary.
1089 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1090 string::size_type newcol)
1093 if (colcount > lyxrc.ascii_linelen) {
1095 colcount = newcol; // assume write after this call
1110 string tag_name(PAR_TAG const & pt) {
1112 case PAR_NONE: return "!-- --";
1113 case TT: return "tt";
1114 case SF: return "sf";
1115 case BF: return "bf";
1116 case IT: return "it";
1117 case SL: return "sl";
1118 case EM: return "em";
1125 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1127 p1 = static_cast<PAR_TAG>(p1 | p2);
1132 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1134 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1140 // Handle internal paragraph parsing -- layout already processed.
1141 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1143 LyXFont const & outerfont,
1144 OutputParams const & runparams,
1145 lyx::depth_type /*depth*/) const
1147 LyXLayout_ptr const & style = layout();
1149 string::size_type char_line_count = 5; // Heuristic choice ;-)
1151 // gets paragraph main font
1154 if (style->labeltype == LABEL_MANUAL) {
1155 font_old = style->labelfont;
1158 font_old = style->font;
1162 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1163 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1164 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1167 stack<PAR_TAG> tag_state;
1168 // parsing main loop
1169 for (pos_type i = 0; i < size(); ++i) {
1171 PAR_TAG tag_close = PAR_NONE;
1172 list < PAR_TAG > tag_open;
1174 LyXFont const font = getFont(buf.params(), i, outerfont);
1176 if (font_old.family() != font.family()) {
1177 switch (family_type) {
1178 case LyXFont::SANS_FAMILY:
1181 case LyXFont::TYPEWRITER_FAMILY:
1188 family_type = font.family();
1190 switch (family_type) {
1191 case LyXFont::SANS_FAMILY:
1192 tag_open.push_back(SF);
1194 case LyXFont::TYPEWRITER_FAMILY:
1195 tag_open.push_back(TT);
1202 if (font_old.series() != font.series()) {
1203 switch (series_type) {
1204 case LyXFont::BOLD_SERIES:
1211 series_type = font.series();
1213 switch (series_type) {
1214 case LyXFont::BOLD_SERIES:
1215 tag_open.push_back(BF);
1223 if (font_old.shape() != font.shape()) {
1224 switch (shape_type) {
1225 case LyXFont::ITALIC_SHAPE:
1228 case LyXFont::SLANTED_SHAPE:
1235 shape_type = font.shape();
1237 switch (shape_type) {
1238 case LyXFont::ITALIC_SHAPE:
1239 tag_open.push_back(IT);
1241 case LyXFont::SLANTED_SHAPE:
1242 tag_open.push_back(SL);
1249 if (font_old.emph() != font.emph()) {
1250 if (font.emph() == LyXFont::ON) {
1251 tag_open.push_back(EM);
1260 list < PAR_TAG > temp;
1261 while (!tag_state.empty() && tag_close) {
1262 PAR_TAG k = tag_state.top();
1264 os << "</" << tag_name(k) << '>';
1271 for(list< PAR_TAG >::const_iterator j = temp.begin();
1272 j != temp.end(); ++j) {
1274 os << '<' << tag_name(*j) << '>';
1277 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1278 j != tag_open.end(); ++j) {
1280 os << '<' << tag_name(*j) << '>';
1283 value_type c = getChar(i);
1286 if (c == Paragraph::META_INSET) {
1287 getInset(i)->linuxdoc(buf, os, runparams);
1292 if (style->latexparam() == "CDATA") {
1293 // "TeX"-Mode on == > SGML-Mode on.
1300 boost::tie(ws, str) = sgml::escapeChar(c);
1301 if (ws && !isFreeSpacing()) {
1302 // in freespacing mode, spaces are
1303 // non-breaking characters
1304 if (desc_on) { // if char is ' ' then...
1306 sgmlLineBreak(os, char_line_count, 6);
1310 sgmlLineBreak(os, char_line_count, 1);
1315 char_line_count += str.length();
1321 while (!tag_state.empty()) {
1322 os << "</" << tag_name(tag_state.top()) << '>';
1326 // resets description flag correctly
1328 // <tag> not closed...
1329 sgmlLineBreak(os, char_line_count, 6);
1335 bool Paragraph::emptyTag() const
1337 for (pos_type i = 0; i < size(); ++i) {
1339 InsetBase const * inset = getInset(i);
1340 InsetBase::Code lyx_code = inset->lyxCode();
1341 if (lyx_code != InsetBase::TOC_CODE &&
1342 lyx_code != InsetBase::INCLUDE_CODE &&
1343 lyx_code != InsetBase::GRAPHICS_CODE &&
1344 lyx_code != InsetBase::ERT_CODE &&
1345 lyx_code != InsetBase::FLOAT_CODE &&
1346 lyx_code != InsetBase::TABULAR_CODE) {
1350 value_type c = getChar(i);
1351 if (c != ' ' && c != '\t')
1359 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1361 for (pos_type i = 0; i < size(); ++i) {
1363 InsetBase const * inset = getInset(i);
1364 InsetBase::Code lyx_code = inset->lyxCode();
1365 if (lyx_code == InsetBase::LABEL_CODE) {
1366 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1367 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1376 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1379 for (i = 0; i < size(); ++i) {
1381 InsetBase const * inset = getInset(i);
1382 inset->docbook(buf, os, runparams);
1384 value_type c = getChar(i);
1389 boost::tie(ws, str) = sgml::escapeChar(c);
1398 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1402 for (pos_type i = initial; i < size(); ++i) {
1403 LyXFont font = getFont(buf.params(), i, outerfont);
1406 if (i != initial && font != font_old)
1415 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1417 OutputParams const & runparams,
1418 LyXFont const & outerfont,
1419 pos_type initial) const
1421 bool emph_flag = false;
1423 LyXLayout_ptr const & style = layout();
1425 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1427 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1430 // parsing main loop
1431 for (pos_type i = initial; i < size(); ++i) {
1432 LyXFont font = getFont(buf.params(), i, outerfont);
1434 // handle <emphasis> tag
1435 if (font_old.emph() != font.emph()) {
1436 if (font.emph() == LyXFont::ON) {
1439 } else if (i != initial) {
1440 os << "</emphasis>";
1446 InsetBase const * inset = getInset(i);
1447 inset->docbook(buf, os, runparams);
1449 value_type c = getChar(i);
1452 boost::tie(ws, str) = sgml::escapeChar(c);
1454 if (style->pass_thru)
1463 os << "</emphasis>";
1466 if (style->free_spacing)
1468 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1475 /// return true if the char is a meta-character for an inset
1477 bool IsInsetChar(char c)
1479 return (c == Paragraph::META_INSET);
1486 bool Paragraph::isHfill(pos_type pos) const
1489 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1493 bool Paragraph::isNewline(pos_type pos) const
1496 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1500 bool Paragraph::isSeparator(pos_type pos) const
1502 return IsSeparatorChar(getChar(pos));
1506 bool Paragraph::isLineSeparator(pos_type pos) const
1508 value_type const c = getChar(pos);
1509 return IsLineSeparatorChar(c)
1510 || (IsInsetChar(c) && getInset(pos) &&
1511 getInset(pos)->isLineSeparator());
1515 /// Used by the spellchecker
1516 bool Paragraph::isLetter(pos_type pos) const
1519 return getInset(pos)->isLetter();
1521 value_type const c = getChar(pos);
1522 return IsLetterChar(c) || IsDigit(c);
1528 Paragraph::getParLanguage(BufferParams const & bparams) const
1531 return getFirstFontSettings().language();
1532 #ifdef WITH_WARNINGS
1533 #warning FIXME we should check the prev par as well (Lgb)
1535 return bparams.language;
1539 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1541 return lyxrc.rtl_support
1542 && getParLanguage(bparams)->RightToLeft()
1543 && ownerCode() != InsetBase::ERT_CODE;
1547 void Paragraph::changeLanguage(BufferParams const & bparams,
1548 Language const * from, Language const * to)
1550 for (pos_type i = 0; i < size(); ++i) {
1551 LyXFont font = getFontSettings(bparams, i);
1552 if (font.language() == from) {
1553 font.setLanguage(to);
1560 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1562 Language const * doc_language = bparams.language;
1563 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1564 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1566 for (; cit != end; ++cit)
1567 if (cit->font().language() != ignore_language &&
1568 cit->font().language() != latex_language &&
1569 cit->font().language() != doc_language)
1575 // Convert the paragraph to a string.
1576 // Used for building the table of contents
1577 string const Paragraph::asString(Buffer const & buffer, bool label) const
1579 OutputParams runparams;
1580 return asString(buffer, runparams, label);
1584 string const Paragraph::asString(Buffer const & buffer,
1585 OutputParams const & runparams,
1590 if (label && !params().labelString().empty())
1591 s += params().labelString() + ' ';
1593 for (pos_type i = 0; i < size(); ++i) {
1594 value_type c = getChar(i);
1597 else if (c == META_INSET &&
1598 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1600 getInset(i)->plaintext(buffer, os, runparams);
1601 s += subst(STRCONV(os.str()),'\n',' ');
1607 // This should really be done by the caller and not here.
1608 string ret = asString(buffer, runparams, 0, size(), label);
1609 return subst(ret, '\n', ' ');
1614 string const Paragraph::asString(Buffer const & buffer,
1615 pos_type beg, pos_type end, bool label) const
1618 OutputParams const runparams;
1619 return asString(buffer, runparams, beg, end, label);
1623 string const Paragraph::asString(Buffer const & buffer,
1624 OutputParams const & runparams,
1625 pos_type beg, pos_type end, bool label) const
1629 if (beg == 0 && label && !params().labelString().empty())
1630 os << params().labelString() << ' ';
1632 for (pos_type i = beg; i < end; ++i) {
1633 value_type const c = getUChar(buffer.params(), i);
1636 else if (c == META_INSET)
1637 getInset(i)->plaintext(buffer, os, runparams);
1644 void Paragraph::setInsetOwner(UpdatableInset * inset)
1646 pimpl_->inset_owner = inset;
1650 void Paragraph::setContentsFromPar(Paragraph const & par)
1652 pimpl_->setContentsFromPar(par);
1656 void Paragraph::trackChanges(Change::Type type)
1658 pimpl_->trackChanges(type);
1662 void Paragraph::untrackChanges()
1664 pimpl_->untrackChanges();
1668 void Paragraph::cleanChanges()
1670 pimpl_->cleanChanges();
1674 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1676 BOOST_ASSERT(empty() || pos < size());
1677 return pimpl_->lookupChange(pos);
1681 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1683 BOOST_ASSERT(empty() || pos < size());
1684 return pimpl_->lookupChangeFull(pos);
1688 bool Paragraph::isChanged(pos_type start, pos_type end) const
1690 return pimpl_->isChanged(start, end);
1694 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1696 return pimpl_->isChangeEdited(start, end);
1700 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1702 pimpl_->setChange(pos, type);
1706 void Paragraph::markErased()
1708 pimpl_->markErased();
1712 void Paragraph::acceptChange(pos_type start, pos_type end)
1714 return pimpl_->acceptChange(start, end);
1718 void Paragraph::rejectChange(pos_type start, pos_type end)
1720 return pimpl_->rejectChange(start, end);
1724 int Paragraph::id() const
1730 LyXLayout_ptr const & Paragraph::layout() const
1736 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1738 layout_ = new_layout;
1742 UpdatableInset * Paragraph::inInset() const
1744 return pimpl_->inset_owner;
1748 InsetBase::Code Paragraph::ownerCode() const
1750 return pimpl_->inset_owner
1751 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1755 void Paragraph::clearContents()
1761 void Paragraph::setChar(pos_type pos, value_type c)
1767 ParagraphParameters & Paragraph::params()
1769 return pimpl_->params;
1773 ParagraphParameters const & Paragraph::params() const
1775 return pimpl_->params;
1779 bool Paragraph::isFreeSpacing() const
1781 if (layout()->free_spacing)
1784 // for now we just need this, later should we need this in some
1785 // other way we can always add a function to InsetBase too.
1786 return ownerCode() == InsetBase::ERT_CODE;
1790 bool Paragraph::allowEmpty() const
1792 if (layout()->keepempty)
1794 return ownerCode() == InsetBase::ERT_CODE;
1798 Row & Paragraph::getRow(pos_type pos)
1800 BOOST_ASSERT(!rows().empty());
1802 RowList::iterator rit = rows_.end();
1803 RowList::iterator const begin = rows_.begin();
1805 for (--rit; rit != begin && rit->pos() > pos; --rit)
1812 Row const & Paragraph::getRow(pos_type pos) const
1814 BOOST_ASSERT(!rows().empty());
1816 RowList::const_iterator rit = rows_.end();
1817 RowList::const_iterator const begin = rows_.begin();
1819 for (--rit; rit != begin && rit->pos() > pos; --rit)
1826 size_t Paragraph::pos2row(pos_type pos) const
1828 BOOST_ASSERT(!rows().empty());
1830 RowList::const_iterator rit = rows_.end();
1831 RowList::const_iterator const begin = rows_.begin();
1833 for (--rit; rit != begin && rit->pos() > pos; --rit)
1840 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1842 if (!Encodings::is_arabic(c))
1843 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1844 return c + (0xb0 - '0');
1848 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1849 unsigned char next_char = ' ';
1851 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1852 unsigned char const par_char = getChar(i);
1853 if (!Encodings::IsComposeChar_arabic(par_char)) {
1854 next_char = par_char;
1859 if (Encodings::is_arabic(next_char)) {
1860 if (Encodings::is_arabic(prev_char) &&
1861 !Encodings::is_arabic_special(prev_char))
1862 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1864 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1866 if (Encodings::is_arabic(prev_char) &&
1867 !Encodings::is_arabic_special(prev_char))
1868 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1870 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1875 void Paragraph::dump() const
1877 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1878 for (size_t i = 0; i != rows_.size(); ++i) {
1879 lyxerr << " row " << i << ": ";