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, Change const & change)
256 for (size_t i = 0, n = str.size(); i != n ; ++i)
257 insertChar(start + i, str[i], font, change);
261 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
264 pimpl_->insertChar(pos, c, Change(trackChanges ?
265 Change::INSERTED : Change::UNCHANGED));
269 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
270 LyXFont const & font, bool trackChanges)
272 pimpl_->insertChar(pos, c, Change(trackChanges ?
273 Change::INSERTED : Change::UNCHANGED));
278 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
279 LyXFont const & font, Change const & change)
281 pimpl_->insertChar(pos, c, change);
286 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change const & change)
288 pimpl_->insertInset(pos, inset, change);
292 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
293 LyXFont const & font, Change const & change)
295 pimpl_->insertInset(pos, inset, change);
300 bool Paragraph::insetAllowed(InsetBase_code code)
302 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
306 // Gets uninstantiated font setting at position.
307 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
311 lyxerr << " pos: " << pos << " size: " << size() << endl;
312 BOOST_ASSERT(pos <= size());
315 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
316 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
317 for (; cit != end; ++cit)
318 if (cit->pos() >= pos)
324 if (pos == size() && !empty())
325 return getFontSettings(bparams, pos - 1);
327 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
331 FontSpan Paragraph::fontSpan(lyx::pos_type pos) const
333 BOOST_ASSERT(pos <= size());
334 lyx::pos_type start = 0;
336 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
337 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
338 for (; cit != end; ++cit) {
339 if (cit->pos() >= pos) {
340 if (pos >= beginOfBody())
341 return FontSpan(std::max(start, beginOfBody()),
344 return FontSpan(start,
345 std::min(beginOfBody() - 1,
348 start = cit->pos() + 1;
351 // This should not happen, but if so, we take no chances.
352 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
354 return FontSpan(pos, pos);
358 // Gets uninstantiated font setting at position 0
359 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
361 if (!empty() && !pimpl_->fontlist.empty())
362 return pimpl_->fontlist[0].font();
364 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
368 // Gets the fully instantiated font at a given position in a paragraph
369 // This is basically the same function as LyXText::GetFont() in text2.C.
370 // The difference is that this one is used for generating the LaTeX file,
371 // and thus cosmetic "improvements" are disallowed: This has to deliver
372 // the true picture of the buffer. (Asger)
373 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
374 LyXFont const & outerfont) const
376 BOOST_ASSERT(pos >= 0);
378 LyXLayout_ptr const & lout = layout();
380 pos_type const body_pos = beginOfBody();
384 layoutfont = lout->labelfont;
386 layoutfont = lout->font;
388 LyXFont font = getFontSettings(bparams, pos);
389 font.realize(layoutfont);
390 font.realize(outerfont);
391 font.realize(bparams.getFont());
397 LyXFont const Paragraph::getLabelFont
398 (BufferParams const & bparams, LyXFont const & outerfont) const
400 LyXFont tmpfont = layout()->labelfont;
401 tmpfont.setLanguage(getParLanguage(bparams));
402 tmpfont.realize(outerfont);
403 tmpfont.realize(bparams.getFont());
408 LyXFont const Paragraph::getLayoutFont
409 (BufferParams const & bparams, LyXFont const & outerfont) const
411 LyXFont tmpfont = layout()->font;
412 tmpfont.setLanguage(getParLanguage(bparams));
413 tmpfont.realize(outerfont);
414 tmpfont.realize(bparams.getFont());
419 /// Returns the height of the highest font in range
420 LyXFont_size Paragraph::highestFontInRange
421 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
423 if (pimpl_->fontlist.empty())
426 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
427 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
428 for (; end_it != end; ++end_it) {
429 if (end_it->pos() >= endpos)
436 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
437 for (; cit != end; ++cit) {
438 if (cit->pos() >= startpos)
442 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
443 for (; cit != end_it; ++cit) {
444 LyXFont::FONT_SIZE size = cit->font().size();
445 if (size == LyXFont::INHERIT_SIZE)
447 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
454 Paragraph::value_type
455 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
457 value_type c = getChar(pos);
458 if (!lyxrc.rtl_support)
488 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
495 void Paragraph::setFont(pos_type pos, LyXFont const & font)
497 BOOST_ASSERT(pos <= size());
499 // First, reduce font against layout/label font
500 // Update: The setCharFont() routine in text2.C already
501 // reduces font, so we don't need to do that here. (Asger)
502 // No need to simplify this because it will disappear
503 // in a new kernel. (Asger)
504 // Next search font table
506 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
507 Pimpl::FontList::iterator it = beg;
508 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
509 for (; it != endit; ++it) {
510 if (it->pos() >= pos)
513 size_t const i = distance(beg, it);
514 bool notfound = (it == endit);
516 if (!notfound && pimpl_->fontlist[i].font() == font)
519 bool begin = pos == 0 || notfound ||
520 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
521 // Is position pos is a beginning of a font block?
522 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
523 // Is position pos is the end of a font block?
524 if (begin && end) { // A single char block
525 if (i + 1 < pimpl_->fontlist.size() &&
526 pimpl_->fontlist[i + 1].font() == font) {
527 // Merge the singleton block with the next block
528 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
529 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
530 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
531 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
532 // Merge the singleton block with the previous block
533 pimpl_->fontlist[i - 1].pos(pos);
534 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
536 pimpl_->fontlist[i].font(font);
538 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
539 pimpl_->fontlist[i - 1].pos(pos);
541 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
542 Pimpl::FontTable(pos, font));
544 pimpl_->fontlist[i].pos(pos - 1);
545 if (!(i + 1 < pimpl_->fontlist.size() &&
546 pimpl_->fontlist[i + 1].font() == font))
547 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
548 Pimpl::FontTable(pos, font));
549 } else { // The general case. The block is splitted into 3 blocks
550 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
551 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
552 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
553 Pimpl::FontTable(pos, font));
558 void Paragraph::makeSameLayout(Paragraph const & par)
560 layout(par.layout());
562 params() = par.params();
566 int Paragraph::stripLeadingSpaces()
572 while (!empty() && (isNewline(0) || isLineSeparator(0))
573 && (lookupChange(0).type != Change::DELETED)) {
574 erase(0, false); // no change tracking here
582 bool Paragraph::hasSameLayout(Paragraph const & par) const
584 return par.layout() == layout() && params().sameLayout(par.params());
588 Paragraph::depth_type Paragraph::getDepth() const
590 return params().depth();
594 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
596 if (layout()->isEnvironment())
597 return params().depth() + 1;
599 return params().depth();
603 char Paragraph::getAlign() const
605 return params().align();
609 string const & Paragraph::getLabelstring() const
611 return params().labelString();
615 // the next two functions are for the manual labels
616 string const Paragraph::getLabelWidthString() const
618 if (!params().labelWidthString().empty())
619 return params().labelWidthString();
621 return lyx::to_utf8(_("Senseless with this layout!"));
625 void Paragraph::setLabelWidthString(string const & s)
627 params().labelWidthString(s);
631 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
634 params().labelWidthString(string());
635 params().align(LYX_ALIGN_LAYOUT);
636 params().spacing(Spacing(Spacing::Default));
640 pos_type Paragraph::beginOfBody() const
642 return begin_of_body_;
646 void Paragraph::setBeginOfBody()
648 if (layout()->labeltype != LABEL_MANUAL) {
653 // Unroll the first two cycles of the loop
654 // and remember the previous character to
655 // remove unnecessary getChar() calls
657 pos_type end = size();
658 if (i < end && !isNewline(i)) {
660 char previous_char = 0;
663 previous_char = text_[i];
666 while (i < end && previous_char != ' ') {
671 previous_char = temp;
681 // returns -1 if inset not found
682 int Paragraph::getPositionOfInset(InsetBase const * inset) const
685 InsetList::const_iterator it = insetlist.begin();
686 InsetList::const_iterator end = insetlist.end();
687 for (; it != end; ++it)
688 if (it->inset == inset)
694 InsetBibitem * Paragraph::bibitem() const
696 if (!insetlist.empty()) {
697 InsetBase * inset = insetlist.begin()->inset;
698 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
699 return static_cast<InsetBibitem *>(inset);
705 bool Paragraph::forceDefaultParagraphs() const
707 return inInset() && inInset()->forceDefaultParagraphs(0);
713 // paragraphs inside floats need different alignment tags to avoid
716 bool noTrivlistCentering(InsetBase::Code code)
718 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
722 string correction(string const & orig)
724 if (orig == "flushleft")
725 return "raggedright";
726 if (orig == "flushright")
728 if (orig == "center")
734 string const corrected_env(string const & suffix, string const & env,
735 InsetBase::Code code)
737 string output = suffix + "{";
738 if (noTrivlistCentering(code))
739 output += correction(env);
748 // This could go to ParagraphParameters if we want to
749 int Paragraph::startTeXParParams(BufferParams const & bparams,
750 odocstream & os, bool moving_arg) const
754 if (params().noindent()) {
759 switch (params().align()) {
761 case LYX_ALIGN_BLOCK:
762 case LYX_ALIGN_LAYOUT:
763 case LYX_ALIGN_SPECIAL:
766 case LYX_ALIGN_RIGHT:
767 case LYX_ALIGN_CENTER:
775 switch (params().align()) {
777 case LYX_ALIGN_BLOCK:
778 case LYX_ALIGN_LAYOUT:
779 case LYX_ALIGN_SPECIAL:
781 case LYX_ALIGN_LEFT: {
783 if (getParLanguage(bparams)->babel() != "hebrew")
784 output = corrected_env("\\begin", "flushleft", ownerCode());
786 output = corrected_env("\\begin", "flushright", ownerCode());
787 os << lyx::from_ascii(output);
788 column += output.size();
790 } case LYX_ALIGN_RIGHT: {
792 if (getParLanguage(bparams)->babel() != "hebrew")
793 output = corrected_env("\\begin", "flushright", ownerCode());
795 output = corrected_env("\\begin", "flushleft", ownerCode());
796 os << lyx::from_ascii(output);
797 column += output.size();
799 } case LYX_ALIGN_CENTER: {
801 output = corrected_env("\\begin", "center", ownerCode());
802 os << lyx::from_ascii(output);
803 column += output.size();
812 // This could go to ParagraphParameters if we want to
813 int Paragraph::endTeXParParams(BufferParams const & bparams,
814 odocstream & os, bool moving_arg) const
818 switch (params().align()) {
820 case LYX_ALIGN_BLOCK:
821 case LYX_ALIGN_LAYOUT:
822 case LYX_ALIGN_SPECIAL:
825 case LYX_ALIGN_RIGHT:
826 case LYX_ALIGN_CENTER:
834 switch (params().align()) {
836 case LYX_ALIGN_BLOCK:
837 case LYX_ALIGN_LAYOUT:
838 case LYX_ALIGN_SPECIAL:
840 case LYX_ALIGN_LEFT: {
842 if (getParLanguage(bparams)->babel() != "hebrew")
843 output = corrected_env("\\par\\end", "flushleft", ownerCode());
845 output = corrected_env("\\par\\end", "flushright", ownerCode());
846 os << lyx::from_ascii(output);
847 column += output.size();
849 } case LYX_ALIGN_RIGHT: {
851 if (getParLanguage(bparams)->babel() != "hebrew")
852 output = corrected_env("\\par\\end", "flushright", ownerCode());
854 output = corrected_env("\\par\\end", "flushleft", ownerCode());
855 os << lyx::from_ascii(output);
856 column += output.size();
858 } case LYX_ALIGN_CENTER: {
860 output = corrected_env("\\par\\end", "center", ownerCode());
861 os << lyx::from_ascii(output);
862 column += output.size();
871 // This one spits out the text of the paragraph
872 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
873 BufferParams const & bparams,
874 LyXFont const & outerfont,
875 odocstream & os, TexRow & texrow,
876 OutputParams const & runparams) const
878 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
880 bool return_value = false;
884 // well we have to check if we are in an inset with unlimited
885 // length (all in one row) if that is true then we don't allow
886 // any special options in the paragraph and also we don't allow
887 // any environment other then "Standard" to be valid!
888 bool asdefault = forceDefaultParagraphs();
891 style = bparams.getLyXTextClass().defaultLayout();
898 LaTeXFeatures features(buf, bparams, runparams);
900 // output change tracking marks only if desired,
901 // if dvipost is installed,
902 // and with dvi/ps (other formats don't work)
903 bool const output = bparams.outputChanges
904 && runparams.flavor == OutputParams::LATEX
905 && features.isAvailable("dvipost");
907 // Maybe we have to create a optional argument.
908 pos_type body_pos = beginOfBody();
909 unsigned int column = 0;
912 // the optional argument is kept in curly brackets in
913 // case it contains a ']'
916 basefont = getLabelFont(bparams, outerfont);
918 basefont = getLayoutFont(bparams, outerfont);
921 // Which font is currently active?
922 LyXFont running_font(basefont);
923 // Do we have an open font change?
924 bool open_font = false;
926 Change::Type running_change = Change::UNCHANGED;
928 texrow.start(id(), 0);
930 // if the paragraph is empty, the loop will not be entered at all
932 if (style->isCommand()) {
937 column += startTeXParParams(bparams, os,
938 runparams.moving_arg);
941 for (pos_type i = 0; i < size(); ++i) {
943 // First char in paragraph or after label?
947 column += running_font.latexWriteEndChanges(os, basefont, basefont);
950 basefont = getLayoutFont(bparams, outerfont);
951 running_font = basefont;
955 if (style->isCommand()) {
961 column += startTeXParParams(bparams, os,
962 runparams.moving_arg);
965 value_type c = getChar(i);
967 // Fully instantiated font
968 LyXFont font = getFont(bparams, i, outerfont);
970 LyXFont const last_font = running_font;
972 // Spaces at end of font change are simulated to be
973 // outside font change, i.e. we write "\textXX{text} "
974 // rather than "\textXX{text }". (Asger)
975 if (open_font && c == ' ' && i <= size() - 2) {
976 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
977 if (next_font != running_font && next_font != font) {
982 // We end font definition before blanks
984 (font != running_font ||
985 font.language() != running_font.language()))
987 column += running_font.latexWriteEndChanges(os,
989 (i == body_pos-1) ? basefont : font);
990 running_font = basefont;
994 // Blanks are printed before start of fontswitch
996 // Do not print the separation of the optional argument
997 if (i != body_pos - 1) {
998 pimpl_->simpleTeXBlanks(os, texrow, i,
999 column, font, *style);
1003 // Do we need to change font?
1004 if ((font != running_font ||
1005 font.language() != running_font.language()) &&
1008 column += font.latexWriteStartChanges(os, basefont,
1010 running_font = font;
1014 Change::Type change = pimpl_->lookupChange(i).type;
1016 column += Changes::latexMarkChange(os, running_change,
1018 running_change = change;
1020 // do not output text which is marked deleted
1021 // if change tracking output is not desired
1022 if (output || running_change != Change::DELETED) {
1023 OutputParams rp = runparams;
1024 rp.free_spacing = style->free_spacing;
1025 rp.local_font = &font;
1026 rp.intitle = style->intitle;
1027 pimpl_->simpleTeXSpecialChars(buf, bparams,
1030 basefont, outerfont, open_font,
1032 *style, i, column, c);
1036 column += Changes::latexMarkChange(os,
1037 running_change, Change::UNCHANGED, output);
1039 // If we have an open font definition, we have to close it
1041 #ifdef FIXED_LANGUAGE_END_DETECTION
1044 .latexWriteEndChanges(os, basefont,
1045 next_->getFont(bparams,
1048 running_font.latexWriteEndChanges(os, basefont,
1052 #ifdef WITH_WARNINGS
1053 //#warning For now we ALWAYS have to close the foreign font settings if they are
1054 //#warning there as we start another \selectlanguage with the next paragraph if
1055 //#warning we are in need of this. This should be fixed sometime (Jug)
1057 running_font.latexWriteEndChanges(os, basefont, basefont);
1061 // Needed if there is an optional argument but no contents.
1062 if (body_pos > 0 && body_pos == size()) {
1064 return_value = false;
1068 column += endTeXParParams(bparams, os, runparams.moving_arg);
1071 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1072 return return_value;
1078 // checks, if newcol chars should be put into this line
1079 // writes newline, if necessary.
1080 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1081 string::size_type newcol)
1084 if (colcount > lyxrc.ascii_linelen) {
1086 colcount = newcol; // assume write after this call
1101 string tag_name(PAR_TAG const & pt) {
1103 case PAR_NONE: return "!-- --";
1104 case TT: return "tt";
1105 case SF: return "sf";
1106 case BF: return "bf";
1107 case IT: return "it";
1108 case SL: return "sl";
1109 case EM: return "em";
1116 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1118 p1 = static_cast<PAR_TAG>(p1 | p2);
1123 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1125 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1131 bool Paragraph::emptyTag() const
1133 for (pos_type i = 0; i < size(); ++i) {
1135 InsetBase const * inset = getInset(i);
1136 InsetBase::Code lyx_code = inset->lyxCode();
1137 if (lyx_code != InsetBase::TOC_CODE &&
1138 lyx_code != InsetBase::INCLUDE_CODE &&
1139 lyx_code != InsetBase::GRAPHICS_CODE &&
1140 lyx_code != InsetBase::ERT_CODE &&
1141 lyx_code != InsetBase::FLOAT_CODE &&
1142 lyx_code != InsetBase::TABULAR_CODE) {
1146 value_type c = getChar(i);
1147 if (c != ' ' && c != '\t')
1155 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1157 for (pos_type i = 0; i < size(); ++i) {
1159 InsetBase const * inset = getInset(i);
1160 InsetBase::Code lyx_code = inset->lyxCode();
1161 if (lyx_code == InsetBase::LABEL_CODE) {
1162 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1163 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1172 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1175 for (i = 0; i < size(); ++i) {
1177 InsetBase const * inset = getInset(i);
1178 inset->docbook(buf, os, runparams);
1180 value_type c = getChar(i);
1186 // sgml::escapeChar takes a char, not lyx::char_type
1187 boost::tie(ws, str) = sgml::escapeChar(c);
1189 os << lyx::from_ascii(str);
1196 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1200 for (pos_type i = initial; i < size(); ++i) {
1201 LyXFont font = getFont(buf.params(), i, outerfont);
1204 if (i != initial && font != font_old)
1213 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1215 OutputParams const & runparams,
1216 LyXFont const & outerfont,
1217 pos_type initial) const
1219 bool emph_flag = false;
1221 LyXLayout_ptr const & style = layout();
1223 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1225 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1228 // parsing main loop
1229 for (pos_type i = initial; i < size(); ++i) {
1230 LyXFont font = getFont(buf.params(), i, outerfont);
1232 // handle <emphasis> tag
1233 if (font_old.emph() != font.emph()) {
1234 if (font.emph() == LyXFont::ON) {
1237 } else if (i != initial) {
1238 os << "</emphasis>";
1244 InsetBase const * inset = getInset(i);
1245 inset->docbook(buf, os, runparams);
1247 value_type c = getChar(i);
1251 // sgml::escapeChar takes a char, not lyx::char_type
1252 boost::tie(ws, str) = sgml::escapeChar(c);
1254 if (style->pass_thru)
1258 os << lyx::from_ascii(str);
1264 os << "</emphasis>";
1267 if (style->free_spacing)
1269 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1274 bool Paragraph::isNewline(pos_type pos) const
1277 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1281 bool Paragraph::isLineSeparator(pos_type pos) const
1283 value_type const c = getChar(pos);
1284 return isLineSeparatorChar(c)
1285 || (c == Paragraph::META_INSET && getInset(pos) &&
1286 getInset(pos)->isLineSeparator());
1290 /// Used by the spellchecker
1291 bool Paragraph::isLetter(pos_type pos) const
1294 return getInset(pos)->isLetter();
1296 value_type const c = getChar(pos);
1297 return isLetterChar(c) || isDigit(c);
1303 Paragraph::getParLanguage(BufferParams const & bparams) const
1306 return getFirstFontSettings(bparams).language();
1307 #ifdef WITH_WARNINGS
1308 #warning FIXME we should check the prev par as well (Lgb)
1310 return bparams.language;
1314 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1316 return lyxrc.rtl_support
1317 && getParLanguage(bparams)->rightToLeft()
1318 && ownerCode() != InsetBase::ERT_CODE;
1322 void Paragraph::changeLanguage(BufferParams const & bparams,
1323 Language const * from, Language const * to)
1325 for (pos_type i = 0; i < size(); ++i) {
1326 LyXFont font = getFontSettings(bparams, i);
1327 if (font.language() == from) {
1328 font.setLanguage(to);
1335 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1337 Language const * doc_language = bparams.language;
1338 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1339 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1341 for (; cit != end; ++cit)
1342 if (cit->font().language() != ignore_language &&
1343 cit->font().language() != latex_language &&
1344 cit->font().language() != doc_language)
1350 // Convert the paragraph to a string.
1351 // Used for building the table of contents
1352 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1354 OutputParams runparams;
1355 return asString(buffer, runparams, label);
1359 docstring const Paragraph::asString(Buffer const & buffer,
1360 OutputParams const & runparams,
1365 if (label && !params().labelString().empty())
1366 s += params().labelString() + ' ';
1368 for (pos_type i = 0; i < size(); ++i) {
1369 value_type c = getChar(i);
1372 else if (c == META_INSET &&
1373 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1375 getInset(i)->plaintext(buffer, os, runparams);
1376 s += subst(STRCONV(os.str()),'\n',' ');
1382 // This should really be done by the caller and not here.
1383 docstring ret = asString(buffer, runparams, 0, size(), label);
1384 return subst(ret, '\n', ' ');
1389 docstring const Paragraph::asString(Buffer const & buffer,
1390 pos_type beg, pos_type end, bool label) const
1393 OutputParams const runparams;
1394 return asString(buffer, runparams, beg, end, label);
1398 docstring const Paragraph::asString(Buffer const & buffer,
1399 OutputParams const & runparams,
1400 pos_type beg, pos_type end, bool label) const
1402 lyx::odocstringstream os;
1404 if (beg == 0 && label && !params().labelString().empty())
1406 os << lyx::from_utf8(params().labelString()) << ' ';
1408 for (pos_type i = beg; i < end; ++i) {
1409 value_type const c = getUChar(buffer.params(), i);
1410 // FIXME: isPrintable does not work for lyx::char_type
1413 else if (c == META_INSET)
1414 getInset(i)->textString(buffer, os, runparams);
1421 void Paragraph::setInsetOwner(InsetBase * inset)
1423 pimpl_->inset_owner = inset;
1427 void Paragraph::setContentsFromPar(Paragraph const & par)
1429 pimpl_->setContentsFromPar(par);
1433 Change const Paragraph::lookupChange(lyx::pos_type pos) const
1435 BOOST_ASSERT(pos <= size());
1436 return pimpl_->lookupChange(pos);
1440 bool Paragraph::isChanged(pos_type start, pos_type end) const
1442 return pimpl_->isChanged(start, end);
1446 void Paragraph::setChange(Change const & change)
1448 pimpl_->setChange(change);
1452 void Paragraph::setChange(lyx::pos_type pos, Change const & change)
1454 pimpl_->setChange(pos, change);
1458 void Paragraph::acceptChange(pos_type start, pos_type end)
1460 return pimpl_->acceptChange(start, end);
1464 void Paragraph::rejectChange(pos_type start, pos_type end)
1466 return pimpl_->rejectChange(start, end);
1470 int Paragraph::id() const
1476 LyXLayout_ptr const & Paragraph::layout() const
1482 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1484 layout_ = new_layout;
1488 InsetBase * Paragraph::inInset() const
1490 return pimpl_->inset_owner;
1494 InsetBase::Code Paragraph::ownerCode() const
1496 return pimpl_->inset_owner
1497 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1501 void Paragraph::clearContents()
1507 void Paragraph::setChar(pos_type pos, value_type c)
1513 ParagraphParameters & Paragraph::params()
1515 return pimpl_->params;
1519 ParagraphParameters const & Paragraph::params() const
1521 return pimpl_->params;
1525 bool Paragraph::isFreeSpacing() const
1527 if (layout()->free_spacing)
1530 // for now we just need this, later should we need this in some
1531 // other way we can always add a function to InsetBase too.
1532 return ownerCode() == InsetBase::ERT_CODE;
1536 bool Paragraph::allowEmpty() const
1538 if (layout()->keepempty)
1540 return ownerCode() == InsetBase::ERT_CODE;
1544 Row & Paragraph::getRow(pos_type pos, bool boundary)
1546 BOOST_ASSERT(!rows().empty());
1548 // If boundary is set we should return the row on which
1549 // the character before is inside.
1550 if (pos > 0 && boundary)
1553 RowList::iterator rit = rows_.end();
1554 RowList::iterator const begin = rows_.begin();
1556 for (--rit; rit != begin && rit->pos() > pos; --rit)
1563 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1565 BOOST_ASSERT(!rows().empty());
1567 // If boundary is set we should return the row on which
1568 // the character before is inside.
1569 if (pos > 0 && boundary)
1572 RowList::const_iterator rit = rows_.end();
1573 RowList::const_iterator const begin = rows_.begin();
1575 for (--rit; rit != begin && rit->pos() > pos; --rit)
1582 size_t Paragraph::pos2row(pos_type pos) const
1584 BOOST_ASSERT(!rows().empty());
1586 RowList::const_iterator rit = rows_.end();
1587 RowList::const_iterator const begin = rows_.begin();
1589 for (--rit; rit != begin && rit->pos() > pos; --rit)
1596 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1598 if (!Encodings::is_arabic(c))
1599 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1600 return c + (0xb0 - '0');
1604 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1605 value_type next_char = ' ';
1607 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1608 value_type const par_char = getChar(i);
1609 if (!Encodings::isComposeChar_arabic(par_char)) {
1610 next_char = par_char;
1615 if (Encodings::is_arabic(next_char)) {
1616 if (Encodings::is_arabic(prev_char) &&
1617 !Encodings::is_arabic_special(prev_char))
1618 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1620 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1622 if (Encodings::is_arabic(prev_char) &&
1623 !Encodings::is_arabic_special(prev_char))
1624 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1626 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1631 void Paragraph::dump() const
1633 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1634 for (size_t i = 0; i != rows_.size(); ++i) {
1635 lyxerr << " row " << i << ": ";