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"
47 #include "support/unicode.h"
49 #include <boost/tuple/tuple.hpp>
50 #include <boost/bind.hpp>
58 using lyx::odocstream;
62 using lyx::support::subst;
70 using std::ostringstream;
73 Paragraph::Paragraph()
74 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
81 Paragraph::Paragraph(Paragraph const & par)
82 : itemdepth(par.itemdepth), insetlist(par.insetlist),
84 rows_(par.rows_), rowSignature_(par.rowSignature_),
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();
111 rowSignature_ = par.rowSignature_;
112 layout_ = par.layout();
114 begin_of_body_ = par.begin_of_body_;
117 pimpl_ = new Pimpl(*par.pimpl_, this);
123 Paragraph::~Paragraph()
127 //lyxerr << "Paragraph::paragraph_id = "
128 // << Paragraph::paragraph_id << endl;
132 void Paragraph::write(Buffer const & buf, ostream & os,
133 BufferParams const & bparams,
134 depth_type & dth) const
136 // The beginning or end of a deeper (i.e. nested) area?
137 if (dth != params().depth()) {
138 if (params().depth() > dth) {
139 while (params().depth() > dth) {
140 os << "\n\\begin_deeper";
144 while (params().depth() < dth) {
145 os << "\n\\end_deeper";
151 // First write the layout
152 os << "\n\\begin_layout " << layout()->name() << '\n';
156 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
158 Change running_change = Change(Change::UNCHANGED);
159 lyx::time_type const curtime(lyx::current_time());
162 for (pos_type i = 0; i <= size(); ++i) {
164 Change change = pimpl_->lookupChange(i);
165 Changes::lyxMarkChange(os, column, curtime, running_change, change);
166 running_change = change;
171 // Write font changes
172 LyXFont font2 = getFontSettings(bparams, i);
173 if (font2 != font1) {
174 font2.lyxWriteChanges(font1, os);
179 value_type const c = getChar(i);
183 InsetBase const * inset = getInset(i);
185 if (inset->directWrite()) {
186 // international char, let it write
187 // code directly so it's shorter in
189 inset->write(buf, os);
193 os << "\\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.
220 std::vector<char> tmp = ucs4_to_utf8(c);
224 lyxerr << "ERROR (Paragraph::writeFile):"
225 " NULL char in structure." << endl;
231 os << "\n\\end_layout\n";
235 void Paragraph::validate(LaTeXFeatures & features) const
237 pimpl_->validate(features, *layout());
241 void Paragraph::eraseIntern(lyx::pos_type pos)
243 pimpl_->eraseIntern(pos);
247 bool Paragraph::erase(pos_type pos)
249 return pimpl_->erase(pos);
253 int Paragraph::erase(pos_type start, pos_type end)
255 return pimpl_->erase(start, end);
259 void Paragraph::insert(pos_type start, string const & str,
260 LyXFont const & font)
262 for (size_t i = 0, n = str.size(); i != n ; ++i)
263 // FIXME: change tracking (MG)
264 insertChar(start + i, str[i], font, Change(Change::INSERTED));
268 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
269 Change const & change)
271 pimpl_->insertChar(pos, c, change);
275 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
276 LyXFont const & font, Change const & change)
278 pimpl_->insertChar(pos, c, change);
283 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change const & change)
285 pimpl_->insertInset(pos, inset, change);
289 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
290 LyXFont const & font, Change const & change)
292 pimpl_->insertInset(pos, inset, change);
297 bool Paragraph::insetAllowed(InsetBase_code code)
299 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
303 // Gets uninstantiated font setting at position.
304 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
308 lyxerr << " pos: " << pos << " size: " << size() << endl;
309 BOOST_ASSERT(pos <= size());
312 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
313 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
314 for (; cit != end; ++cit)
315 if (cit->pos() >= pos)
321 if (pos == size() && !empty())
322 return getFontSettings(bparams, pos - 1);
324 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
328 FontSpan Paragraph::fontSpan(lyx::pos_type pos) const
330 BOOST_ASSERT(pos <= size());
331 lyx::pos_type start = 0;
333 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
334 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
335 for (; cit != end; ++cit) {
336 if (cit->pos() >= pos) {
337 if (pos >= beginOfBody())
338 return FontSpan(std::max(start, beginOfBody()),
341 return FontSpan(start,
342 std::min(beginOfBody() - 1,
345 start = cit->pos() + 1;
348 // This should not happen, but if so, we take no chances.
349 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
351 return FontSpan(pos, pos);
355 // Gets uninstantiated font setting at position 0
356 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
358 if (!empty() && !pimpl_->fontlist.empty())
359 return pimpl_->fontlist[0].font();
361 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
365 // Gets the fully instantiated font at a given position in a paragraph
366 // This is basically the same function as LyXText::GetFont() in text2.C.
367 // The difference is that this one is used for generating the LaTeX file,
368 // and thus cosmetic "improvements" are disallowed: This has to deliver
369 // the true picture of the buffer. (Asger)
370 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
371 LyXFont const & outerfont) const
373 BOOST_ASSERT(pos >= 0);
375 LyXLayout_ptr const & lout = layout();
377 pos_type const body_pos = beginOfBody();
381 layoutfont = lout->labelfont;
383 layoutfont = lout->font;
385 LyXFont font = getFontSettings(bparams, pos);
386 font.realize(layoutfont);
387 font.realize(outerfont);
388 font.realize(bparams.getFont());
394 LyXFont const Paragraph::getLabelFont
395 (BufferParams const & bparams, LyXFont const & outerfont) const
397 LyXFont tmpfont = layout()->labelfont;
398 tmpfont.setLanguage(getParLanguage(bparams));
399 tmpfont.realize(outerfont);
400 tmpfont.realize(bparams.getFont());
405 LyXFont const Paragraph::getLayoutFont
406 (BufferParams const & bparams, LyXFont const & outerfont) const
408 LyXFont tmpfont = layout()->font;
409 tmpfont.setLanguage(getParLanguage(bparams));
410 tmpfont.realize(outerfont);
411 tmpfont.realize(bparams.getFont());
416 /// Returns the height of the highest font in range
417 LyXFont_size Paragraph::highestFontInRange
418 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
420 if (pimpl_->fontlist.empty())
423 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
424 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
425 for (; end_it != end; ++end_it) {
426 if (end_it->pos() >= endpos)
433 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
434 for (; cit != end; ++cit) {
435 if (cit->pos() >= startpos)
439 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
440 for (; cit != end_it; ++cit) {
441 LyXFont::FONT_SIZE size = cit->font().size();
442 if (size == LyXFont::INHERIT_SIZE)
444 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
451 Paragraph::value_type
452 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
454 value_type c = getChar(pos);
455 if (!lyxrc.rtl_support)
485 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
492 void Paragraph::setFont(pos_type pos, LyXFont const & font)
494 BOOST_ASSERT(pos <= size());
496 // First, reduce font against layout/label font
497 // Update: The setCharFont() routine in text2.C already
498 // reduces font, so we don't need to do that here. (Asger)
499 // No need to simplify this because it will disappear
500 // in a new kernel. (Asger)
501 // Next search font table
503 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
504 Pimpl::FontList::iterator it = beg;
505 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
506 for (; it != endit; ++it) {
507 if (it->pos() >= pos)
510 size_t const i = distance(beg, it);
511 bool notfound = (it == endit);
513 if (!notfound && pimpl_->fontlist[i].font() == font)
516 bool begin = pos == 0 || notfound ||
517 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
518 // Is position pos is a beginning of a font block?
519 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
520 // Is position pos is the end of a font block?
521 if (begin && end) { // A single char block
522 if (i + 1 < pimpl_->fontlist.size() &&
523 pimpl_->fontlist[i + 1].font() == font) {
524 // Merge the singleton block with the next block
525 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
526 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
527 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
528 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
529 // Merge the singleton block with the previous block
530 pimpl_->fontlist[i - 1].pos(pos);
531 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
533 pimpl_->fontlist[i].font(font);
535 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
536 pimpl_->fontlist[i - 1].pos(pos);
538 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
539 Pimpl::FontTable(pos, font));
541 pimpl_->fontlist[i].pos(pos - 1);
542 if (!(i + 1 < pimpl_->fontlist.size() &&
543 pimpl_->fontlist[i + 1].font() == font))
544 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
545 Pimpl::FontTable(pos, font));
546 } else { // The general case. The block is splitted into 3 blocks
547 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
548 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
549 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
550 Pimpl::FontTable(pos, font));
555 void Paragraph::makeSameLayout(Paragraph const & par)
557 layout(par.layout());
559 params() = par.params();
563 int Paragraph::stripLeadingSpaces()
569 while (!empty() && (isNewline(0) || isLineSeparator(0))
570 && (lookupChange(0).type != Change::DELETED)) {
579 bool Paragraph::hasSameLayout(Paragraph const & par) const
581 return par.layout() == layout() && params().sameLayout(par.params());
585 Paragraph::depth_type Paragraph::getDepth() const
587 return params().depth();
591 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
593 if (layout()->isEnvironment())
594 return params().depth() + 1;
596 return params().depth();
600 char Paragraph::getAlign() const
602 return params().align();
606 string const & Paragraph::getLabelstring() const
608 return params().labelString();
612 // the next two functions are for the manual labels
613 string const Paragraph::getLabelWidthString() const
615 if (!params().labelWidthString().empty())
616 return params().labelWidthString();
618 return lyx::to_utf8(_("Senseless with this layout!"));
622 void Paragraph::setLabelWidthString(string const & s)
624 params().labelWidthString(s);
628 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
631 params().labelWidthString(string());
632 params().align(LYX_ALIGN_LAYOUT);
633 params().spacing(Spacing(Spacing::Default));
637 pos_type Paragraph::beginOfBody() const
639 return begin_of_body_;
643 void Paragraph::setBeginOfBody()
645 if (layout()->labeltype != LABEL_MANUAL) {
650 // Unroll the first two cycles of the loop
651 // and remember the previous character to
652 // remove unnecessary getChar() calls
654 pos_type end = size();
655 if (i < end && !isNewline(i)) {
657 char previous_char = 0;
660 previous_char = text_[i];
663 while (i < end && previous_char != ' ') {
668 previous_char = temp;
678 // returns -1 if inset not found
679 int Paragraph::getPositionOfInset(InsetBase const * inset) const
682 InsetList::const_iterator it = insetlist.begin();
683 InsetList::const_iterator end = insetlist.end();
684 for (; it != end; ++it)
685 if (it->inset == inset)
691 InsetBibitem * Paragraph::bibitem() const
693 if (!insetlist.empty()) {
694 InsetBase * inset = insetlist.begin()->inset;
695 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
696 return static_cast<InsetBibitem *>(inset);
702 bool Paragraph::forceDefaultParagraphs() const
704 return inInset() && inInset()->forceDefaultParagraphs(0);
710 // paragraphs inside floats need different alignment tags to avoid
713 bool noTrivlistCentering(InsetBase::Code code)
715 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
719 string correction(string const & orig)
721 if (orig == "flushleft")
722 return "raggedright";
723 if (orig == "flushright")
725 if (orig == "center")
731 string const corrected_env(string const & suffix, string const & env,
732 InsetBase::Code code)
734 string output = suffix + "{";
735 if (noTrivlistCentering(code))
736 output += correction(env);
745 // This could go to ParagraphParameters if we want to
746 int Paragraph::startTeXParParams(BufferParams const & bparams,
747 odocstream & os, bool moving_arg) const
751 if (params().noindent()) {
756 switch (params().align()) {
758 case LYX_ALIGN_BLOCK:
759 case LYX_ALIGN_LAYOUT:
760 case LYX_ALIGN_SPECIAL:
763 case LYX_ALIGN_RIGHT:
764 case LYX_ALIGN_CENTER:
772 switch (params().align()) {
774 case LYX_ALIGN_BLOCK:
775 case LYX_ALIGN_LAYOUT:
776 case LYX_ALIGN_SPECIAL:
778 case LYX_ALIGN_LEFT: {
780 if (getParLanguage(bparams)->babel() != "hebrew")
781 output = corrected_env("\\begin", "flushleft", ownerCode());
783 output = corrected_env("\\begin", "flushright", ownerCode());
784 os << lyx::from_ascii(output);
785 column += output.size();
787 } case LYX_ALIGN_RIGHT: {
789 if (getParLanguage(bparams)->babel() != "hebrew")
790 output = corrected_env("\\begin", "flushright", ownerCode());
792 output = corrected_env("\\begin", "flushleft", ownerCode());
793 os << lyx::from_ascii(output);
794 column += output.size();
796 } case LYX_ALIGN_CENTER: {
798 output = corrected_env("\\begin", "center", ownerCode());
799 os << lyx::from_ascii(output);
800 column += output.size();
809 // This could go to ParagraphParameters if we want to
810 int Paragraph::endTeXParParams(BufferParams const & bparams,
811 odocstream & os, bool moving_arg) const
815 switch (params().align()) {
817 case LYX_ALIGN_BLOCK:
818 case LYX_ALIGN_LAYOUT:
819 case LYX_ALIGN_SPECIAL:
822 case LYX_ALIGN_RIGHT:
823 case LYX_ALIGN_CENTER:
831 switch (params().align()) {
833 case LYX_ALIGN_BLOCK:
834 case LYX_ALIGN_LAYOUT:
835 case LYX_ALIGN_SPECIAL:
837 case LYX_ALIGN_LEFT: {
839 if (getParLanguage(bparams)->babel() != "hebrew")
840 output = corrected_env("\\par\\end", "flushleft", ownerCode());
842 output = corrected_env("\\par\\end", "flushright", ownerCode());
843 os << lyx::from_ascii(output);
844 column += output.size();
846 } case LYX_ALIGN_RIGHT: {
848 if (getParLanguage(bparams)->babel() != "hebrew")
849 output = corrected_env("\\par\\end", "flushright", ownerCode());
851 output = corrected_env("\\par\\end", "flushleft", ownerCode());
852 os << lyx::from_ascii(output);
853 column += output.size();
855 } case LYX_ALIGN_CENTER: {
857 output = corrected_env("\\par\\end", "center", ownerCode());
858 os << lyx::from_ascii(output);
859 column += output.size();
868 // This one spits out the text of the paragraph
869 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
870 BufferParams const & bparams,
871 LyXFont const & outerfont,
872 odocstream & os, TexRow & texrow,
873 OutputParams const & runparams) const
875 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
877 bool return_value = false;
881 // well we have to check if we are in an inset with unlimited
882 // length (all in one row) if that is true then we don't allow
883 // any special options in the paragraph and also we don't allow
884 // any environment other then "Standard" to be valid!
885 bool asdefault = forceDefaultParagraphs();
888 style = bparams.getLyXTextClass().defaultLayout();
895 LaTeXFeatures features(buf, bparams, runparams);
897 // output change tracking marks only if desired,
898 // if dvipost is installed,
899 // and with dvi/ps (other formats don't work)
900 bool const output = bparams.outputChanges
901 && runparams.flavor == OutputParams::LATEX
902 && features.isAvailable("dvipost");
904 // Maybe we have to create a optional argument.
905 pos_type body_pos = beginOfBody();
906 unsigned int column = 0;
909 // the optional argument is kept in curly brackets in
910 // case it contains a ']'
913 basefont = getLabelFont(bparams, outerfont);
915 basefont = getLayoutFont(bparams, outerfont);
918 // Which font is currently active?
919 LyXFont running_font(basefont);
920 // Do we have an open font change?
921 bool open_font = false;
923 Change::Type running_change = Change::UNCHANGED;
925 texrow.start(id(), 0);
927 // if the paragraph is empty, the loop will not be entered at all
929 if (style->isCommand()) {
934 column += startTeXParParams(bparams, os,
935 runparams.moving_arg);
938 for (pos_type i = 0; i < size(); ++i) {
940 // First char in paragraph or after label?
944 column += running_font.latexWriteEndChanges(os, basefont, basefont);
947 basefont = getLayoutFont(bparams, outerfont);
948 running_font = basefont;
952 if (style->isCommand()) {
958 column += startTeXParParams(bparams, os,
959 runparams.moving_arg);
962 value_type c = getChar(i);
964 // Fully instantiated font
965 LyXFont font = getFont(bparams, i, outerfont);
967 LyXFont const last_font = running_font;
969 // Spaces at end of font change are simulated to be
970 // outside font change, i.e. we write "\textXX{text} "
971 // rather than "\textXX{text }". (Asger)
972 if (open_font && c == ' ' && i <= size() - 2) {
973 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
974 if (next_font != running_font && next_font != font) {
979 // We end font definition before blanks
981 (font != running_font ||
982 font.language() != running_font.language()))
984 column += running_font.latexWriteEndChanges(os,
986 (i == body_pos-1) ? basefont : font);
987 running_font = basefont;
991 // Blanks are printed before start of fontswitch
993 // Do not print the separation of the optional argument
994 if (i != body_pos - 1) {
995 pimpl_->simpleTeXBlanks(os, texrow, i,
996 column, font, *style);
1000 // Do we need to change font?
1001 if ((font != running_font ||
1002 font.language() != running_font.language()) &&
1005 column += font.latexWriteStartChanges(os, basefont,
1007 running_font = font;
1011 Change::Type change = pimpl_->lookupChange(i).type;
1013 column += Changes::latexMarkChange(os, running_change,
1015 running_change = change;
1017 // do not output text which is marked deleted
1018 // if change tracking output is not desired
1019 if (output || running_change != Change::DELETED) {
1020 OutputParams rp = runparams;
1021 rp.free_spacing = style->free_spacing;
1022 rp.local_font = &font;
1023 rp.intitle = style->intitle;
1024 pimpl_->simpleTeXSpecialChars(buf, bparams,
1027 basefont, outerfont, open_font,
1029 *style, i, column, c);
1033 column += Changes::latexMarkChange(os,
1034 running_change, Change::UNCHANGED, output);
1036 // If we have an open font definition, we have to close it
1038 #ifdef FIXED_LANGUAGE_END_DETECTION
1041 .latexWriteEndChanges(os, basefont,
1042 next_->getFont(bparams,
1045 running_font.latexWriteEndChanges(os, basefont,
1049 #ifdef WITH_WARNINGS
1050 //#warning For now we ALWAYS have to close the foreign font settings if they are
1051 //#warning there as we start another \selectlanguage with the next paragraph if
1052 //#warning we are in need of this. This should be fixed sometime (Jug)
1054 running_font.latexWriteEndChanges(os, basefont, basefont);
1058 // Needed if there is an optional argument but no contents.
1059 if (body_pos > 0 && body_pos == size()) {
1061 return_value = false;
1065 column += endTeXParParams(bparams, os, runparams.moving_arg);
1068 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1069 return return_value;
1075 // checks, if newcol chars should be put into this line
1076 // writes newline, if necessary.
1077 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1078 string::size_type newcol)
1081 if (colcount > lyxrc.ascii_linelen) {
1083 colcount = newcol; // assume write after this call
1098 string tag_name(PAR_TAG const & pt) {
1100 case PAR_NONE: return "!-- --";
1101 case TT: return "tt";
1102 case SF: return "sf";
1103 case BF: return "bf";
1104 case IT: return "it";
1105 case SL: return "sl";
1106 case EM: return "em";
1113 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1115 p1 = static_cast<PAR_TAG>(p1 | p2);
1120 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1122 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1128 bool Paragraph::emptyTag() const
1130 for (pos_type i = 0; i < size(); ++i) {
1132 InsetBase const * inset = getInset(i);
1133 InsetBase::Code lyx_code = inset->lyxCode();
1134 if (lyx_code != InsetBase::TOC_CODE &&
1135 lyx_code != InsetBase::INCLUDE_CODE &&
1136 lyx_code != InsetBase::GRAPHICS_CODE &&
1137 lyx_code != InsetBase::ERT_CODE &&
1138 lyx_code != InsetBase::FLOAT_CODE &&
1139 lyx_code != InsetBase::TABULAR_CODE) {
1143 value_type c = getChar(i);
1144 if (c != ' ' && c != '\t')
1152 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1154 for (pos_type i = 0; i < size(); ++i) {
1156 InsetBase const * inset = getInset(i);
1157 InsetBase::Code lyx_code = inset->lyxCode();
1158 if (lyx_code == InsetBase::LABEL_CODE) {
1159 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1160 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1169 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1172 for (i = 0; i < size(); ++i) {
1174 InsetBase const * inset = getInset(i);
1175 inset->docbook(buf, os, runparams);
1177 value_type c = getChar(i);
1183 // sgml::escapeChar takes a char, not lyx::char_type
1184 boost::tie(ws, str) = sgml::escapeChar(c);
1193 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1197 for (pos_type i = initial; i < size(); ++i) {
1198 LyXFont font = getFont(buf.params(), i, outerfont);
1201 if (i != initial && font != font_old)
1210 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1212 OutputParams const & runparams,
1213 LyXFont const & outerfont,
1214 pos_type initial) const
1216 bool emph_flag = false;
1218 LyXLayout_ptr const & style = layout();
1220 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1222 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1225 // parsing main loop
1226 for (pos_type i = initial; i < size(); ++i) {
1227 LyXFont font = getFont(buf.params(), i, outerfont);
1229 // handle <emphasis> tag
1230 if (font_old.emph() != font.emph()) {
1231 if (font.emph() == LyXFont::ON) {
1234 } else if (i != initial) {
1235 os << "</emphasis>";
1241 InsetBase const * inset = getInset(i);
1242 inset->docbook(buf, os, runparams);
1244 value_type c = getChar(i);
1248 // sgml::escapeChar takes a char, not lyx::char_type
1249 boost::tie(ws, str) = sgml::escapeChar(c);
1251 if (style->pass_thru)
1260 os << "</emphasis>";
1263 if (style->free_spacing)
1265 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1270 bool Paragraph::isNewline(pos_type pos) const
1273 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1277 bool Paragraph::isLineSeparator(pos_type pos) const
1279 value_type const c = getChar(pos);
1280 return isLineSeparatorChar(c)
1281 || (c == Paragraph::META_INSET && getInset(pos) &&
1282 getInset(pos)->isLineSeparator());
1286 /// Used by the spellchecker
1287 bool Paragraph::isLetter(pos_type pos) const
1290 return getInset(pos)->isLetter();
1292 value_type const c = getChar(pos);
1293 return isLetterChar(c) || isDigit(c);
1299 Paragraph::getParLanguage(BufferParams const & bparams) const
1302 return getFirstFontSettings(bparams).language();
1303 #ifdef WITH_WARNINGS
1304 #warning FIXME we should check the prev par as well (Lgb)
1306 return bparams.language;
1310 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1312 return lyxrc.rtl_support
1313 && getParLanguage(bparams)->rightToLeft()
1314 && ownerCode() != InsetBase::ERT_CODE;
1318 void Paragraph::changeLanguage(BufferParams const & bparams,
1319 Language const * from, Language const * to)
1321 for (pos_type i = 0; i < size(); ++i) {
1322 LyXFont font = getFontSettings(bparams, i);
1323 if (font.language() == from) {
1324 font.setLanguage(to);
1331 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1333 Language const * doc_language = bparams.language;
1334 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1335 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1337 for (; cit != end; ++cit)
1338 if (cit->font().language() != ignore_language &&
1339 cit->font().language() != latex_language &&
1340 cit->font().language() != doc_language)
1346 // Convert the paragraph to a string.
1347 // Used for building the table of contents
1348 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1350 OutputParams runparams;
1351 return asString(buffer, runparams, label);
1355 docstring const Paragraph::asString(Buffer const & buffer,
1356 OutputParams const & runparams,
1361 if (label && !params().labelString().empty())
1362 s += params().labelString() + ' ';
1364 for (pos_type i = 0; i < size(); ++i) {
1365 value_type c = getChar(i);
1368 else if (c == META_INSET &&
1369 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1371 getInset(i)->plaintext(buffer, os, runparams);
1372 s += subst(STRCONV(os.str()),'\n',' ');
1378 // This should really be done by the caller and not here.
1379 docstring ret = asString(buffer, runparams, 0, size(), label);
1380 return subst(ret, '\n', ' ');
1385 docstring const Paragraph::asString(Buffer const & buffer,
1386 pos_type beg, pos_type end, bool label) const
1389 OutputParams const runparams;
1390 return asString(buffer, runparams, beg, end, label);
1394 docstring const Paragraph::asString(Buffer const & buffer,
1395 OutputParams const & runparams,
1396 pos_type beg, pos_type end, bool label) const
1398 lyx::odocstringstream os;
1400 if (beg == 0 && label && !params().labelString().empty())
1402 os << lyx::from_utf8(params().labelString()) << ' ';
1404 for (pos_type i = beg; i < end; ++i) {
1405 value_type const c = getUChar(buffer.params(), i);
1406 // FIXME: isPrintable does not work for lyx::char_type
1409 else if (c == META_INSET)
1410 getInset(i)->textString(buffer, os, runparams);
1417 void Paragraph::setInsetOwner(InsetBase * inset)
1419 pimpl_->inset_owner = inset;
1423 void Paragraph::setContentsFromPar(Paragraph const & par)
1425 pimpl_->setContentsFromPar(par);
1429 Change const Paragraph::lookupChange(lyx::pos_type pos) const
1431 BOOST_ASSERT(pos <= size());
1432 return pimpl_->lookupChange(pos);
1436 bool Paragraph::isChanged(pos_type start, pos_type end) const
1438 return pimpl_->isChanged(start, end);
1442 void Paragraph::setChange(Change const & change)
1444 pimpl_->setChange(change);
1448 void Paragraph::setChange(lyx::pos_type pos, Change const & change)
1450 pimpl_->setChange(pos, change);
1454 void Paragraph::acceptChange(pos_type start, pos_type end)
1456 return pimpl_->acceptChange(start, end);
1460 void Paragraph::rejectChange(pos_type start, pos_type end)
1462 return pimpl_->rejectChange(start, end);
1466 int Paragraph::id() const
1472 LyXLayout_ptr const & Paragraph::layout() const
1478 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1480 layout_ = new_layout;
1484 InsetBase * Paragraph::inInset() const
1486 return pimpl_->inset_owner;
1490 InsetBase::Code Paragraph::ownerCode() const
1492 return pimpl_->inset_owner
1493 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1497 void Paragraph::clearContents()
1503 void Paragraph::setChar(pos_type pos, value_type c)
1509 ParagraphParameters & Paragraph::params()
1511 return pimpl_->params;
1515 ParagraphParameters const & Paragraph::params() const
1517 return pimpl_->params;
1521 bool Paragraph::isFreeSpacing() const
1523 if (layout()->free_spacing)
1526 // for now we just need this, later should we need this in some
1527 // other way we can always add a function to InsetBase too.
1528 return ownerCode() == InsetBase::ERT_CODE;
1532 bool Paragraph::allowEmpty() const
1534 if (layout()->keepempty)
1536 return ownerCode() == InsetBase::ERT_CODE;
1540 Row & Paragraph::getRow(pos_type pos, bool boundary)
1542 BOOST_ASSERT(!rows().empty());
1544 // If boundary is set we should return the row on which
1545 // the character before is inside.
1546 if (pos > 0 && boundary)
1549 RowList::iterator rit = rows_.end();
1550 RowList::iterator const begin = rows_.begin();
1552 for (--rit; rit != begin && rit->pos() > pos; --rit)
1559 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1561 BOOST_ASSERT(!rows().empty());
1563 // If boundary is set we should return the row on which
1564 // the character before is inside.
1565 if (pos > 0 && boundary)
1568 RowList::const_iterator rit = rows_.end();
1569 RowList::const_iterator const begin = rows_.begin();
1571 for (--rit; rit != begin && rit->pos() > pos; --rit)
1578 size_t Paragraph::pos2row(pos_type pos) const
1580 BOOST_ASSERT(!rows().empty());
1582 RowList::const_iterator rit = rows_.end();
1583 RowList::const_iterator const begin = rows_.begin();
1585 for (--rit; rit != begin && rit->pos() > pos; --rit)
1592 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1594 if (!Encodings::is_arabic(c))
1595 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1596 return c + (0xb0 - '0');
1600 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1601 value_type next_char = ' ';
1603 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1604 value_type const par_char = getChar(i);
1605 if (!Encodings::isComposeChar_arabic(par_char)) {
1606 next_char = par_char;
1611 if (Encodings::is_arabic(next_char)) {
1612 if (Encodings::is_arabic(prev_char) &&
1613 !Encodings::is_arabic_special(prev_char))
1614 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1616 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1618 if (Encodings::is_arabic(prev_char) &&
1619 !Encodings::is_arabic_special(prev_char))
1620 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1622 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1627 void Paragraph::dump() const
1629 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1630 for (size_t i = 0; i != rows_.size(); ++i) {
1631 lyxerr << " row " << i << ": ";