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 bool Paragraph::erase(pos_type pos, bool trackChanges)
243 return pimpl_->erase(pos, trackChanges);
247 int Paragraph::erase(pos_type start, pos_type end, bool trackChanges)
249 return pimpl_->erase(start, end, trackChanges);
253 void Paragraph::insert(pos_type start, string const & str,
254 LyXFont const & font)
256 for (size_t i = 0, n = str.size(); i != n ; ++i)
257 // FIXME: change tracking (MG)
258 insertChar(start + i, str[i], font, Change(Change::INSERTED));
262 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
263 Change const & change)
265 pimpl_->insertChar(pos, c, change);
269 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
270 LyXFont const & font, Change const & change)
272 pimpl_->insertChar(pos, c, change);
277 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change const & change)
279 pimpl_->insertInset(pos, inset, change);
283 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
284 LyXFont const & font, Change const & change)
286 pimpl_->insertInset(pos, inset, change);
291 bool Paragraph::insetAllowed(InsetBase_code code)
293 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
297 // Gets uninstantiated font setting at position.
298 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
302 lyxerr << " pos: " << pos << " size: " << size() << endl;
303 BOOST_ASSERT(pos <= size());
306 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
307 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
308 for (; cit != end; ++cit)
309 if (cit->pos() >= pos)
315 if (pos == size() && !empty())
316 return getFontSettings(bparams, pos - 1);
318 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
322 FontSpan Paragraph::fontSpan(lyx::pos_type pos) const
324 BOOST_ASSERT(pos <= size());
325 lyx::pos_type start = 0;
327 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
328 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
329 for (; cit != end; ++cit) {
330 if (cit->pos() >= pos) {
331 if (pos >= beginOfBody())
332 return FontSpan(std::max(start, beginOfBody()),
335 return FontSpan(start,
336 std::min(beginOfBody() - 1,
339 start = cit->pos() + 1;
342 // This should not happen, but if so, we take no chances.
343 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
345 return FontSpan(pos, pos);
349 // Gets uninstantiated font setting at position 0
350 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
352 if (!empty() && !pimpl_->fontlist.empty())
353 return pimpl_->fontlist[0].font();
355 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
359 // Gets the fully instantiated font at a given position in a paragraph
360 // This is basically the same function as LyXText::GetFont() in text2.C.
361 // The difference is that this one is used for generating the LaTeX file,
362 // and thus cosmetic "improvements" are disallowed: This has to deliver
363 // the true picture of the buffer. (Asger)
364 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
365 LyXFont const & outerfont) const
367 BOOST_ASSERT(pos >= 0);
369 LyXLayout_ptr const & lout = layout();
371 pos_type const body_pos = beginOfBody();
375 layoutfont = lout->labelfont;
377 layoutfont = lout->font;
379 LyXFont font = getFontSettings(bparams, pos);
380 font.realize(layoutfont);
381 font.realize(outerfont);
382 font.realize(bparams.getFont());
388 LyXFont const Paragraph::getLabelFont
389 (BufferParams const & bparams, LyXFont const & outerfont) const
391 LyXFont tmpfont = layout()->labelfont;
392 tmpfont.setLanguage(getParLanguage(bparams));
393 tmpfont.realize(outerfont);
394 tmpfont.realize(bparams.getFont());
399 LyXFont const Paragraph::getLayoutFont
400 (BufferParams const & bparams, LyXFont const & outerfont) const
402 LyXFont tmpfont = layout()->font;
403 tmpfont.setLanguage(getParLanguage(bparams));
404 tmpfont.realize(outerfont);
405 tmpfont.realize(bparams.getFont());
410 /// Returns the height of the highest font in range
411 LyXFont_size Paragraph::highestFontInRange
412 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
414 if (pimpl_->fontlist.empty())
417 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
418 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
419 for (; end_it != end; ++end_it) {
420 if (end_it->pos() >= endpos)
427 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
428 for (; cit != end; ++cit) {
429 if (cit->pos() >= startpos)
433 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
434 for (; cit != end_it; ++cit) {
435 LyXFont::FONT_SIZE size = cit->font().size();
436 if (size == LyXFont::INHERIT_SIZE)
438 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
445 Paragraph::value_type
446 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
448 value_type c = getChar(pos);
449 if (!lyxrc.rtl_support)
479 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
486 void Paragraph::setFont(pos_type pos, LyXFont const & font)
488 BOOST_ASSERT(pos <= size());
490 // First, reduce font against layout/label font
491 // Update: The setCharFont() routine in text2.C already
492 // reduces font, so we don't need to do that here. (Asger)
493 // No need to simplify this because it will disappear
494 // in a new kernel. (Asger)
495 // Next search font table
497 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
498 Pimpl::FontList::iterator it = beg;
499 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
500 for (; it != endit; ++it) {
501 if (it->pos() >= pos)
504 size_t const i = distance(beg, it);
505 bool notfound = (it == endit);
507 if (!notfound && pimpl_->fontlist[i].font() == font)
510 bool begin = pos == 0 || notfound ||
511 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
512 // Is position pos is a beginning of a font block?
513 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
514 // Is position pos is the end of a font block?
515 if (begin && end) { // A single char block
516 if (i + 1 < pimpl_->fontlist.size() &&
517 pimpl_->fontlist[i + 1].font() == font) {
518 // Merge the singleton block with the next block
519 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
520 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
521 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
522 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
523 // Merge the singleton block with the previous block
524 pimpl_->fontlist[i - 1].pos(pos);
525 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
527 pimpl_->fontlist[i].font(font);
529 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
530 pimpl_->fontlist[i - 1].pos(pos);
532 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
533 Pimpl::FontTable(pos, font));
535 pimpl_->fontlist[i].pos(pos - 1);
536 if (!(i + 1 < pimpl_->fontlist.size() &&
537 pimpl_->fontlist[i + 1].font() == font))
538 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
539 Pimpl::FontTable(pos, font));
540 } else { // The general case. The block is splitted into 3 blocks
541 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
542 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
543 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
544 Pimpl::FontTable(pos, font));
549 void Paragraph::makeSameLayout(Paragraph const & par)
551 layout(par.layout());
553 params() = par.params();
557 int Paragraph::stripLeadingSpaces()
563 while (!empty() && (isNewline(0) || isLineSeparator(0))
564 && (lookupChange(0).type != Change::DELETED)) {
565 erase(0, false); // no change tracking here
573 bool Paragraph::hasSameLayout(Paragraph const & par) const
575 return par.layout() == layout() && params().sameLayout(par.params());
579 Paragraph::depth_type Paragraph::getDepth() const
581 return params().depth();
585 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
587 if (layout()->isEnvironment())
588 return params().depth() + 1;
590 return params().depth();
594 char Paragraph::getAlign() const
596 return params().align();
600 string const & Paragraph::getLabelstring() const
602 return params().labelString();
606 // the next two functions are for the manual labels
607 string const Paragraph::getLabelWidthString() const
609 if (!params().labelWidthString().empty())
610 return params().labelWidthString();
612 return lyx::to_utf8(_("Senseless with this layout!"));
616 void Paragraph::setLabelWidthString(string const & s)
618 params().labelWidthString(s);
622 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
625 params().labelWidthString(string());
626 params().align(LYX_ALIGN_LAYOUT);
627 params().spacing(Spacing(Spacing::Default));
631 pos_type Paragraph::beginOfBody() const
633 return begin_of_body_;
637 void Paragraph::setBeginOfBody()
639 if (layout()->labeltype != LABEL_MANUAL) {
644 // Unroll the first two cycles of the loop
645 // and remember the previous character to
646 // remove unnecessary getChar() calls
648 pos_type end = size();
649 if (i < end && !isNewline(i)) {
651 char previous_char = 0;
654 previous_char = text_[i];
657 while (i < end && previous_char != ' ') {
662 previous_char = temp;
672 // returns -1 if inset not found
673 int Paragraph::getPositionOfInset(InsetBase const * inset) const
676 InsetList::const_iterator it = insetlist.begin();
677 InsetList::const_iterator end = insetlist.end();
678 for (; it != end; ++it)
679 if (it->inset == inset)
685 InsetBibitem * Paragraph::bibitem() const
687 if (!insetlist.empty()) {
688 InsetBase * inset = insetlist.begin()->inset;
689 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
690 return static_cast<InsetBibitem *>(inset);
696 bool Paragraph::forceDefaultParagraphs() const
698 return inInset() && inInset()->forceDefaultParagraphs(0);
704 // paragraphs inside floats need different alignment tags to avoid
707 bool noTrivlistCentering(InsetBase::Code code)
709 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
713 string correction(string const & orig)
715 if (orig == "flushleft")
716 return "raggedright";
717 if (orig == "flushright")
719 if (orig == "center")
725 string const corrected_env(string const & suffix, string const & env,
726 InsetBase::Code code)
728 string output = suffix + "{";
729 if (noTrivlistCentering(code))
730 output += correction(env);
739 // This could go to ParagraphParameters if we want to
740 int Paragraph::startTeXParParams(BufferParams const & bparams,
741 odocstream & os, bool moving_arg) const
745 if (params().noindent()) {
750 switch (params().align()) {
752 case LYX_ALIGN_BLOCK:
753 case LYX_ALIGN_LAYOUT:
754 case LYX_ALIGN_SPECIAL:
757 case LYX_ALIGN_RIGHT:
758 case LYX_ALIGN_CENTER:
766 switch (params().align()) {
768 case LYX_ALIGN_BLOCK:
769 case LYX_ALIGN_LAYOUT:
770 case LYX_ALIGN_SPECIAL:
772 case LYX_ALIGN_LEFT: {
774 if (getParLanguage(bparams)->babel() != "hebrew")
775 output = corrected_env("\\begin", "flushleft", ownerCode());
777 output = corrected_env("\\begin", "flushright", ownerCode());
778 os << lyx::from_ascii(output);
779 column += output.size();
781 } case LYX_ALIGN_RIGHT: {
783 if (getParLanguage(bparams)->babel() != "hebrew")
784 output = corrected_env("\\begin", "flushright", ownerCode());
786 output = corrected_env("\\begin", "flushleft", ownerCode());
787 os << lyx::from_ascii(output);
788 column += output.size();
790 } case LYX_ALIGN_CENTER: {
792 output = corrected_env("\\begin", "center", ownerCode());
793 os << lyx::from_ascii(output);
794 column += output.size();
803 // This could go to ParagraphParameters if we want to
804 int Paragraph::endTeXParParams(BufferParams const & bparams,
805 odocstream & os, bool moving_arg) const
809 switch (params().align()) {
811 case LYX_ALIGN_BLOCK:
812 case LYX_ALIGN_LAYOUT:
813 case LYX_ALIGN_SPECIAL:
816 case LYX_ALIGN_RIGHT:
817 case LYX_ALIGN_CENTER:
825 switch (params().align()) {
827 case LYX_ALIGN_BLOCK:
828 case LYX_ALIGN_LAYOUT:
829 case LYX_ALIGN_SPECIAL:
831 case LYX_ALIGN_LEFT: {
833 if (getParLanguage(bparams)->babel() != "hebrew")
834 output = corrected_env("\\par\\end", "flushleft", ownerCode());
836 output = corrected_env("\\par\\end", "flushright", ownerCode());
837 os << lyx::from_ascii(output);
838 column += output.size();
840 } case LYX_ALIGN_RIGHT: {
842 if (getParLanguage(bparams)->babel() != "hebrew")
843 output = corrected_env("\\par\\end", "flushright", ownerCode());
845 output = corrected_env("\\par\\end", "flushleft", ownerCode());
846 os << lyx::from_ascii(output);
847 column += output.size();
849 } case LYX_ALIGN_CENTER: {
851 output = corrected_env("\\par\\end", "center", ownerCode());
852 os << lyx::from_ascii(output);
853 column += output.size();
862 // This one spits out the text of the paragraph
863 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
864 BufferParams const & bparams,
865 LyXFont const & outerfont,
866 odocstream & os, TexRow & texrow,
867 OutputParams const & runparams) const
869 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
871 bool return_value = false;
875 // well we have to check if we are in an inset with unlimited
876 // length (all in one row) if that is true then we don't allow
877 // any special options in the paragraph and also we don't allow
878 // any environment other then "Standard" to be valid!
879 bool asdefault = forceDefaultParagraphs();
882 style = bparams.getLyXTextClass().defaultLayout();
889 LaTeXFeatures features(buf, bparams, runparams);
891 // output change tracking marks only if desired,
892 // if dvipost is installed,
893 // and with dvi/ps (other formats don't work)
894 bool const output = bparams.outputChanges
895 && runparams.flavor == OutputParams::LATEX
896 && features.isAvailable("dvipost");
898 // Maybe we have to create a optional argument.
899 pos_type body_pos = beginOfBody();
900 unsigned int column = 0;
903 // the optional argument is kept in curly brackets in
904 // case it contains a ']'
907 basefont = getLabelFont(bparams, outerfont);
909 basefont = getLayoutFont(bparams, outerfont);
912 // Which font is currently active?
913 LyXFont running_font(basefont);
914 // Do we have an open font change?
915 bool open_font = false;
917 Change::Type running_change = Change::UNCHANGED;
919 texrow.start(id(), 0);
921 // if the paragraph is empty, the loop will not be entered at all
923 if (style->isCommand()) {
928 column += startTeXParParams(bparams, os,
929 runparams.moving_arg);
932 for (pos_type i = 0; i < size(); ++i) {
934 // First char in paragraph or after label?
938 column += running_font.latexWriteEndChanges(os, basefont, basefont);
941 basefont = getLayoutFont(bparams, outerfont);
942 running_font = basefont;
946 if (style->isCommand()) {
952 column += startTeXParParams(bparams, os,
953 runparams.moving_arg);
956 value_type c = getChar(i);
958 // Fully instantiated font
959 LyXFont font = getFont(bparams, i, outerfont);
961 LyXFont const last_font = running_font;
963 // Spaces at end of font change are simulated to be
964 // outside font change, i.e. we write "\textXX{text} "
965 // rather than "\textXX{text }". (Asger)
966 if (open_font && c == ' ' && i <= size() - 2) {
967 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
968 if (next_font != running_font && next_font != font) {
973 // We end font definition before blanks
975 (font != running_font ||
976 font.language() != running_font.language()))
978 column += running_font.latexWriteEndChanges(os,
980 (i == body_pos-1) ? basefont : font);
981 running_font = basefont;
985 // Blanks are printed before start of fontswitch
987 // Do not print the separation of the optional argument
988 if (i != body_pos - 1) {
989 pimpl_->simpleTeXBlanks(os, texrow, i,
990 column, font, *style);
994 // Do we need to change font?
995 if ((font != running_font ||
996 font.language() != running_font.language()) &&
999 column += font.latexWriteStartChanges(os, basefont,
1001 running_font = font;
1005 Change::Type change = pimpl_->lookupChange(i).type;
1007 column += Changes::latexMarkChange(os, running_change,
1009 running_change = change;
1011 // do not output text which is marked deleted
1012 // if change tracking output is not desired
1013 if (output || running_change != Change::DELETED) {
1014 OutputParams rp = runparams;
1015 rp.free_spacing = style->free_spacing;
1016 rp.local_font = &font;
1017 rp.intitle = style->intitle;
1018 pimpl_->simpleTeXSpecialChars(buf, bparams,
1021 basefont, outerfont, open_font,
1023 *style, i, column, c);
1027 column += Changes::latexMarkChange(os,
1028 running_change, Change::UNCHANGED, output);
1030 // If we have an open font definition, we have to close it
1032 #ifdef FIXED_LANGUAGE_END_DETECTION
1035 .latexWriteEndChanges(os, basefont,
1036 next_->getFont(bparams,
1039 running_font.latexWriteEndChanges(os, basefont,
1043 #ifdef WITH_WARNINGS
1044 //#warning For now we ALWAYS have to close the foreign font settings if they are
1045 //#warning there as we start another \selectlanguage with the next paragraph if
1046 //#warning we are in need of this. This should be fixed sometime (Jug)
1048 running_font.latexWriteEndChanges(os, basefont, basefont);
1052 // Needed if there is an optional argument but no contents.
1053 if (body_pos > 0 && body_pos == size()) {
1055 return_value = false;
1059 column += endTeXParParams(bparams, os, runparams.moving_arg);
1062 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1063 return return_value;
1069 // checks, if newcol chars should be put into this line
1070 // writes newline, if necessary.
1071 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1072 string::size_type newcol)
1075 if (colcount > lyxrc.ascii_linelen) {
1077 colcount = newcol; // assume write after this call
1092 string tag_name(PAR_TAG const & pt) {
1094 case PAR_NONE: return "!-- --";
1095 case TT: return "tt";
1096 case SF: return "sf";
1097 case BF: return "bf";
1098 case IT: return "it";
1099 case SL: return "sl";
1100 case EM: return "em";
1107 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1109 p1 = static_cast<PAR_TAG>(p1 | p2);
1114 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1116 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1122 bool Paragraph::emptyTag() const
1124 for (pos_type i = 0; i < size(); ++i) {
1126 InsetBase const * inset = getInset(i);
1127 InsetBase::Code lyx_code = inset->lyxCode();
1128 if (lyx_code != InsetBase::TOC_CODE &&
1129 lyx_code != InsetBase::INCLUDE_CODE &&
1130 lyx_code != InsetBase::GRAPHICS_CODE &&
1131 lyx_code != InsetBase::ERT_CODE &&
1132 lyx_code != InsetBase::FLOAT_CODE &&
1133 lyx_code != InsetBase::TABULAR_CODE) {
1137 value_type c = getChar(i);
1138 if (c != ' ' && c != '\t')
1146 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1148 for (pos_type i = 0; i < size(); ++i) {
1150 InsetBase const * inset = getInset(i);
1151 InsetBase::Code lyx_code = inset->lyxCode();
1152 if (lyx_code == InsetBase::LABEL_CODE) {
1153 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1154 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1163 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1166 for (i = 0; i < size(); ++i) {
1168 InsetBase const * inset = getInset(i);
1169 inset->docbook(buf, os, runparams);
1171 value_type c = getChar(i);
1177 // sgml::escapeChar takes a char, not lyx::char_type
1178 boost::tie(ws, str) = sgml::escapeChar(c);
1180 os << lyx::from_ascii(str);
1187 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1191 for (pos_type i = initial; i < size(); ++i) {
1192 LyXFont font = getFont(buf.params(), i, outerfont);
1195 if (i != initial && font != font_old)
1204 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1206 OutputParams const & runparams,
1207 LyXFont const & outerfont,
1208 pos_type initial) const
1210 bool emph_flag = false;
1212 LyXLayout_ptr const & style = layout();
1214 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1216 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1219 // parsing main loop
1220 for (pos_type i = initial; i < size(); ++i) {
1221 LyXFont font = getFont(buf.params(), i, outerfont);
1223 // handle <emphasis> tag
1224 if (font_old.emph() != font.emph()) {
1225 if (font.emph() == LyXFont::ON) {
1228 } else if (i != initial) {
1229 os << "</emphasis>";
1235 InsetBase const * inset = getInset(i);
1236 inset->docbook(buf, os, runparams);
1238 value_type c = getChar(i);
1242 // sgml::escapeChar takes a char, not lyx::char_type
1243 boost::tie(ws, str) = sgml::escapeChar(c);
1245 if (style->pass_thru)
1249 os << lyx::from_ascii(str);
1255 os << "</emphasis>";
1258 if (style->free_spacing)
1260 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1265 bool Paragraph::isNewline(pos_type pos) const
1268 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1272 bool Paragraph::isLineSeparator(pos_type pos) const
1274 value_type const c = getChar(pos);
1275 return isLineSeparatorChar(c)
1276 || (c == Paragraph::META_INSET && getInset(pos) &&
1277 getInset(pos)->isLineSeparator());
1281 /// Used by the spellchecker
1282 bool Paragraph::isLetter(pos_type pos) const
1285 return getInset(pos)->isLetter();
1287 value_type const c = getChar(pos);
1288 return isLetterChar(c) || isDigit(c);
1294 Paragraph::getParLanguage(BufferParams const & bparams) const
1297 return getFirstFontSettings(bparams).language();
1298 #ifdef WITH_WARNINGS
1299 #warning FIXME we should check the prev par as well (Lgb)
1301 return bparams.language;
1305 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1307 return lyxrc.rtl_support
1308 && getParLanguage(bparams)->rightToLeft()
1309 && ownerCode() != InsetBase::ERT_CODE;
1313 void Paragraph::changeLanguage(BufferParams const & bparams,
1314 Language const * from, Language const * to)
1316 for (pos_type i = 0; i < size(); ++i) {
1317 LyXFont font = getFontSettings(bparams, i);
1318 if (font.language() == from) {
1319 font.setLanguage(to);
1326 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1328 Language const * doc_language = bparams.language;
1329 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1330 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1332 for (; cit != end; ++cit)
1333 if (cit->font().language() != ignore_language &&
1334 cit->font().language() != latex_language &&
1335 cit->font().language() != doc_language)
1341 // Convert the paragraph to a string.
1342 // Used for building the table of contents
1343 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1345 OutputParams runparams;
1346 return asString(buffer, runparams, label);
1350 docstring const Paragraph::asString(Buffer const & buffer,
1351 OutputParams const & runparams,
1356 if (label && !params().labelString().empty())
1357 s += params().labelString() + ' ';
1359 for (pos_type i = 0; i < size(); ++i) {
1360 value_type c = getChar(i);
1363 else if (c == META_INSET &&
1364 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1366 getInset(i)->plaintext(buffer, os, runparams);
1367 s += subst(STRCONV(os.str()),'\n',' ');
1373 // This should really be done by the caller and not here.
1374 docstring ret = asString(buffer, runparams, 0, size(), label);
1375 return subst(ret, '\n', ' ');
1380 docstring const Paragraph::asString(Buffer const & buffer,
1381 pos_type beg, pos_type end, bool label) const
1384 OutputParams const runparams;
1385 return asString(buffer, runparams, beg, end, label);
1389 docstring const Paragraph::asString(Buffer const & buffer,
1390 OutputParams const & runparams,
1391 pos_type beg, pos_type end, bool label) const
1393 lyx::odocstringstream os;
1395 if (beg == 0 && label && !params().labelString().empty())
1397 os << lyx::from_utf8(params().labelString()) << ' ';
1399 for (pos_type i = beg; i < end; ++i) {
1400 value_type const c = getUChar(buffer.params(), i);
1401 // FIXME: isPrintable does not work for lyx::char_type
1404 else if (c == META_INSET)
1405 getInset(i)->textString(buffer, os, runparams);
1412 void Paragraph::setInsetOwner(InsetBase * inset)
1414 pimpl_->inset_owner = inset;
1418 void Paragraph::setContentsFromPar(Paragraph const & par)
1420 pimpl_->setContentsFromPar(par);
1424 Change const Paragraph::lookupChange(lyx::pos_type pos) const
1426 BOOST_ASSERT(pos <= size());
1427 return pimpl_->lookupChange(pos);
1431 bool Paragraph::isChanged(pos_type start, pos_type end) const
1433 return pimpl_->isChanged(start, end);
1437 void Paragraph::setChange(Change const & change)
1439 pimpl_->setChange(change);
1443 void Paragraph::setChange(lyx::pos_type pos, Change const & change)
1445 pimpl_->setChange(pos, change);
1449 void Paragraph::acceptChange(pos_type start, pos_type end)
1451 return pimpl_->acceptChange(start, end);
1455 void Paragraph::rejectChange(pos_type start, pos_type end)
1457 return pimpl_->rejectChange(start, end);
1461 int Paragraph::id() const
1467 LyXLayout_ptr const & Paragraph::layout() const
1473 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1475 layout_ = new_layout;
1479 InsetBase * Paragraph::inInset() const
1481 return pimpl_->inset_owner;
1485 InsetBase::Code Paragraph::ownerCode() const
1487 return pimpl_->inset_owner
1488 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1492 void Paragraph::clearContents()
1498 void Paragraph::setChar(pos_type pos, value_type c)
1504 ParagraphParameters & Paragraph::params()
1506 return pimpl_->params;
1510 ParagraphParameters const & Paragraph::params() const
1512 return pimpl_->params;
1516 bool Paragraph::isFreeSpacing() const
1518 if (layout()->free_spacing)
1521 // for now we just need this, later should we need this in some
1522 // other way we can always add a function to InsetBase too.
1523 return ownerCode() == InsetBase::ERT_CODE;
1527 bool Paragraph::allowEmpty() const
1529 if (layout()->keepempty)
1531 return ownerCode() == InsetBase::ERT_CODE;
1535 Row & Paragraph::getRow(pos_type pos, bool boundary)
1537 BOOST_ASSERT(!rows().empty());
1539 // If boundary is set we should return the row on which
1540 // the character before is inside.
1541 if (pos > 0 && boundary)
1544 RowList::iterator rit = rows_.end();
1545 RowList::iterator const begin = rows_.begin();
1547 for (--rit; rit != begin && rit->pos() > pos; --rit)
1554 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1556 BOOST_ASSERT(!rows().empty());
1558 // If boundary is set we should return the row on which
1559 // the character before is inside.
1560 if (pos > 0 && boundary)
1563 RowList::const_iterator rit = rows_.end();
1564 RowList::const_iterator const begin = rows_.begin();
1566 for (--rit; rit != begin && rit->pos() > pos; --rit)
1573 size_t Paragraph::pos2row(pos_type pos) const
1575 BOOST_ASSERT(!rows().empty());
1577 RowList::const_iterator rit = rows_.end();
1578 RowList::const_iterator const begin = rows_.begin();
1580 for (--rit; rit != begin && rit->pos() > pos; --rit)
1587 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1589 if (!Encodings::is_arabic(c))
1590 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1591 return c + (0xb0 - '0');
1595 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1596 value_type next_char = ' ';
1598 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1599 value_type const par_char = getChar(i);
1600 if (!Encodings::isComposeChar_arabic(par_char)) {
1601 next_char = par_char;
1606 if (Encodings::is_arabic(next_char)) {
1607 if (Encodings::is_arabic(prev_char) &&
1608 !Encodings::is_arabic_special(prev_char))
1609 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1611 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1613 if (Encodings::is_arabic(prev_char) &&
1614 !Encodings::is_arabic_special(prev_char))
1615 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1617 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1622 void Paragraph::dump() const
1624 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1625 for (size_t i = 0; i != rows_.size(); ++i) {
1626 lyxerr << " row " << i << ": ";