3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "paragraph.h"
21 #include "paragraph_pimpl.h"
24 #include "bufferparams.h"
30 #include "LaTeXFeatures.h"
34 #include "outputparams.h"
35 #include "paragraph_funcs.h"
36 #include "ParagraphList_fwd.h"
41 #include "insets/insetbibitem.h"
42 #include "insets/insetoptarg.h"
44 #include "support/lstrings.h"
45 #include "support/textutils.h"
46 #include "support/convert.h"
48 #include <boost/tuple/tuple.hpp>
49 #include <boost/bind.hpp>
58 using lyx::support::subst;
66 using std::ostringstream;
69 ParagraphList::ParagraphList()
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_->lookupChangeFull(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.
222 lyxerr << "ERROR (Paragraph::writeFile):"
223 " NULL char in structure." << endl;
229 os << "\n\\end_layout\n";
233 void Paragraph::validate(LaTeXFeatures & features) const
235 pimpl_->validate(features, *layout());
239 void Paragraph::eraseIntern(lyx::pos_type pos)
241 pimpl_->eraseIntern(pos);
245 bool Paragraph::erase(pos_type pos)
247 return pimpl_->erase(pos);
251 int Paragraph::erase(pos_type start, pos_type end)
253 return pimpl_->erase(start, end);
257 void Paragraph::insert(pos_type start, string const & str,
258 LyXFont const & font)
260 for (size_t i = 0, n = str.size(); i != n ; ++i)
261 insertChar(start + i, str[i], font);
265 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
268 pimpl_->insertChar(pos, c, change);
272 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
273 LyXFont const & font, Change change)
275 pimpl_->insertChar(pos, c, change);
280 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
282 pimpl_->insertInset(pos, inset, change);
286 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
287 LyXFont const & font, Change change)
289 pimpl_->insertInset(pos, inset, change);
294 bool Paragraph::insetAllowed(InsetBase_code code)
296 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
300 // Gets uninstantiated font setting at position.
301 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
305 lyxerr << " pos: " << pos << " size: " << size() << endl;
306 BOOST_ASSERT(pos <= size());
309 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
310 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
311 for (; cit != end; ++cit)
312 if (cit->pos() >= pos)
318 if (pos == size() && !empty())
319 return getFontSettings(bparams, pos - 1);
321 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
325 FontSpan Paragraph::fontSpan(lyx::pos_type pos) const
327 BOOST_ASSERT(pos <= size());
328 lyx::pos_type start = 0;
330 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
331 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
332 for (; cit != end; ++cit) {
333 if (cit->pos() >= pos) {
334 if (pos >= beginOfBody())
335 return FontSpan(std::max(start, beginOfBody()),
338 return FontSpan(start,
339 std::min(beginOfBody() - 1,
342 start = cit->pos() + 1;
345 // This should not happen, but if so, we take no chances.
346 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
348 return FontSpan(pos, pos);
352 // Gets uninstantiated font setting at position 0
353 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
355 if (!empty() && !pimpl_->fontlist.empty())
356 return pimpl_->fontlist[0].font();
358 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
362 // Gets the fully instantiated font at a given position in a paragraph
363 // This is basically the same function as LyXText::GetFont() in text2.C.
364 // The difference is that this one is used for generating the LaTeX file,
365 // and thus cosmetic "improvements" are disallowed: This has to deliver
366 // the true picture of the buffer. (Asger)
367 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
368 LyXFont const & outerfont) const
370 BOOST_ASSERT(pos >= 0);
372 LyXLayout_ptr const & lout = layout();
374 pos_type const body_pos = beginOfBody();
378 layoutfont = lout->labelfont;
380 layoutfont = lout->font;
382 LyXFont font = getFontSettings(bparams, pos);
383 font.realize(layoutfont);
384 font.realize(outerfont);
385 font.realize(bparams.getLyXTextClass().defaultfont());
391 LyXFont const Paragraph::getLabelFont
392 (BufferParams const & bparams, LyXFont const & outerfont) const
394 LyXFont tmpfont = layout()->labelfont;
395 tmpfont.setLanguage(getParLanguage(bparams));
396 tmpfont.realize(outerfont);
397 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
402 LyXFont const Paragraph::getLayoutFont
403 (BufferParams const & bparams, LyXFont const & outerfont) const
405 LyXFont tmpfont = layout()->font;
406 tmpfont.setLanguage(getParLanguage(bparams));
407 tmpfont.realize(outerfont);
408 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
413 /// Returns the height of the highest font in range
414 LyXFont_size Paragraph::highestFontInRange
415 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
417 if (pimpl_->fontlist.empty())
420 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
421 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
422 for (; end_it != end; ++end_it) {
423 if (end_it->pos() >= endpos)
430 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
431 for (; cit != end; ++cit) {
432 if (cit->pos() >= startpos)
436 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
437 for (; cit != end_it; ++cit) {
438 LyXFont::FONT_SIZE size = cit->font().size();
439 if (size == LyXFont::INHERIT_SIZE)
441 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
448 Paragraph::value_type
449 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
451 value_type c = getChar(pos);
452 if (!lyxrc.rtl_support)
482 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
489 void Paragraph::setFont(pos_type pos, LyXFont const & font)
491 BOOST_ASSERT(pos <= size());
493 // First, reduce font against layout/label font
494 // Update: The setCharFont() routine in text2.C already
495 // reduces font, so we don't need to do that here. (Asger)
496 // No need to simplify this because it will disappear
497 // in a new kernel. (Asger)
498 // Next search font table
500 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
501 Pimpl::FontList::iterator it = beg;
502 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
503 for (; it != endit; ++it) {
504 if (it->pos() >= pos)
507 size_t const i = distance(beg, it);
508 bool notfound = (it == endit);
510 if (!notfound && pimpl_->fontlist[i].font() == font)
513 bool begin = pos == 0 || notfound ||
514 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
515 // Is position pos is a beginning of a font block?
516 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
517 // Is position pos is the end of a font block?
518 if (begin && end) { // A single char block
519 if (i + 1 < pimpl_->fontlist.size() &&
520 pimpl_->fontlist[i + 1].font() == font) {
521 // Merge the singleton block with the next block
522 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
523 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
524 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
525 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
526 // Merge the singleton block with the previous block
527 pimpl_->fontlist[i - 1].pos(pos);
528 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
530 pimpl_->fontlist[i].font(font);
532 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
533 pimpl_->fontlist[i - 1].pos(pos);
535 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
536 Pimpl::FontTable(pos, font));
538 pimpl_->fontlist[i].pos(pos - 1);
539 if (!(i + 1 < pimpl_->fontlist.size() &&
540 pimpl_->fontlist[i + 1].font() == font))
541 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
542 Pimpl::FontTable(pos, font));
543 } else { // The general case. The block is splitted into 3 blocks
544 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
545 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
546 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
547 Pimpl::FontTable(pos, font));
552 void Paragraph::makeSameLayout(Paragraph const & par)
554 layout(par.layout());
556 params() = par.params();
560 int Paragraph::stripLeadingSpaces()
566 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
567 // Set Change::Type to Change::INSERTED to quietly remove it
568 setChange(0, Change::INSERTED);
577 bool Paragraph::hasSameLayout(Paragraph const & par) const
579 return par.layout() == layout() && params().sameLayout(par.params());
583 Paragraph::depth_type Paragraph::getDepth() const
585 return params().depth();
589 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
591 if (layout()->isEnvironment())
592 return params().depth() + 1;
594 return params().depth();
598 char Paragraph::getAlign() const
600 return params().align();
604 string const & Paragraph::getLabelstring() const
606 return params().labelString();
610 // the next two functions are for the manual labels
611 string const Paragraph::getLabelWidthString() const
613 if (!params().labelWidthString().empty())
614 return params().labelWidthString();
616 return _("Senseless with this layout!");
620 void Paragraph::setLabelWidthString(string const & s)
622 params().labelWidthString(s);
626 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
629 params().labelWidthString(string());
630 params().align(LYX_ALIGN_LAYOUT);
631 params().spacing(Spacing(Spacing::Default));
635 pos_type Paragraph::beginOfBody() const
637 return begin_of_body_;
641 void Paragraph::setBeginOfBody()
643 if (layout()->labeltype != LABEL_MANUAL) {
648 // Unroll the first two cycles of the loop
649 // and remember the previous character to
650 // remove unnecessary getChar() calls
652 pos_type end = size();
653 if (i < end && !isNewline(i)) {
655 char previous_char = 0;
658 previous_char = text_[i];
661 while (i < end && previous_char != ' ') {
666 previous_char = temp;
676 // returns -1 if inset not found
677 int Paragraph::getPositionOfInset(InsetBase const * inset) const
680 InsetList::const_iterator it = insetlist.begin();
681 InsetList::const_iterator end = insetlist.end();
682 for (; it != end; ++it)
683 if (it->inset == inset)
689 InsetBibitem * Paragraph::bibitem() const
691 if (!insetlist.empty()) {
692 InsetBase * inset = insetlist.begin()->inset;
693 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
694 return static_cast<InsetBibitem *>(inset);
700 bool Paragraph::forceDefaultParagraphs() const
702 return inInset() && inInset()->forceDefaultParagraphs(0);
708 // paragraphs inside floats need different alignment tags to avoid
711 bool noTrivlistCentering(InsetBase::Code code)
713 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
717 string correction(string const & orig)
719 if (orig == "flushleft")
720 return "raggedright";
721 if (orig == "flushright")
723 if (orig == "center")
729 string const corrected_env(string const & suffix, string const & env,
730 InsetBase::Code code)
732 string output = suffix + "{";
733 if (noTrivlistCentering(code))
734 output += correction(env);
743 // This could go to ParagraphParameters if we want to
744 int Paragraph::startTeXParParams(BufferParams const & bparams,
745 ostream & os, bool moving_arg) const
749 if (params().noindent()) {
754 switch (params().align()) {
756 case LYX_ALIGN_BLOCK:
757 case LYX_ALIGN_LAYOUT:
758 case LYX_ALIGN_SPECIAL:
761 case LYX_ALIGN_RIGHT:
762 case LYX_ALIGN_CENTER:
770 switch (params().align()) {
772 case LYX_ALIGN_BLOCK:
773 case LYX_ALIGN_LAYOUT:
774 case LYX_ALIGN_SPECIAL:
776 case LYX_ALIGN_LEFT: {
778 if (getParLanguage(bparams)->babel() != "hebrew")
779 output = corrected_env("\\begin", "flushleft", ownerCode());
781 output = corrected_env("\\begin", "flushright", ownerCode());
783 column += output.size();
785 } case LYX_ALIGN_RIGHT: {
787 if (getParLanguage(bparams)->babel() != "hebrew")
788 output = corrected_env("\\begin", "flushright", ownerCode());
790 output = corrected_env("\\begin", "flushleft", ownerCode());
792 column += output.size();
794 } case LYX_ALIGN_CENTER: {
796 output = corrected_env("\\begin", "center", ownerCode());
798 column += output.size();
807 // This could go to ParagraphParameters if we want to
808 int Paragraph::endTeXParParams(BufferParams const & bparams,
809 ostream & os, bool moving_arg) const
813 switch (params().align()) {
815 case LYX_ALIGN_BLOCK:
816 case LYX_ALIGN_LAYOUT:
817 case LYX_ALIGN_SPECIAL:
820 case LYX_ALIGN_RIGHT:
821 case LYX_ALIGN_CENTER:
829 switch (params().align()) {
831 case LYX_ALIGN_BLOCK:
832 case LYX_ALIGN_LAYOUT:
833 case LYX_ALIGN_SPECIAL:
835 case LYX_ALIGN_LEFT: {
837 if (getParLanguage(bparams)->babel() != "hebrew")
838 output = corrected_env("\\par\\end", "flushleft", ownerCode());
840 output = corrected_env("\\par\\end", "flushright", ownerCode());
842 column += output.size();
844 } case LYX_ALIGN_RIGHT: {
846 if (getParLanguage(bparams)->babel() != "hebrew")
847 output = corrected_env("\\par\\end", "flushright", ownerCode());
849 output = corrected_env("\\par\\end", "flushleft", ownerCode());
851 column += output.size();
853 } case LYX_ALIGN_CENTER: {
855 output = corrected_env("\\par\\end", "center", ownerCode());
857 column += output.size();
866 // This one spits out the text of the paragraph
867 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
868 BufferParams const & bparams,
869 LyXFont const & outerfont,
870 ostream & os, TexRow & texrow,
871 OutputParams const & runparams) const
873 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
875 bool return_value = false;
879 // well we have to check if we are in an inset with unlimited
880 // length (all in one row) if that is true then we don't allow
881 // any special options in the paragraph and also we don't allow
882 // any environment other then "Standard" to be valid!
883 bool asdefault = forceDefaultParagraphs();
886 style = bparams.getLyXTextClass().defaultLayout();
893 LaTeXFeatures features(buf, bparams, runparams.nice);
895 // output change tracking marks only if desired,
896 // if dvipost is installed,
897 // and with dvi/ps (other formats don't work)
898 bool const output = bparams.output_changes
899 && runparams.flavor == OutputParams::LATEX
900 && features.isAvailable("dvipost");
902 // Maybe we have to create a optional argument.
903 pos_type body_pos = beginOfBody();
904 unsigned int column = 0;
907 // the optional argument is kept in curly brackets in
908 // case it contains a ']'
911 basefont = getLabelFont(bparams, outerfont);
913 basefont = getLayoutFont(bparams, outerfont);
916 // Which font is currently active?
917 LyXFont running_font(basefont);
918 // Do we have an open font change?
919 bool open_font = false;
921 Change::Type running_change = Change::UNCHANGED;
923 texrow.start(id(), 0);
925 // if the paragraph is empty, the loop will not be entered at all
927 if (style->isCommand()) {
932 column += startTeXParParams(bparams, os,
933 runparams.moving_arg);
936 for (pos_type i = 0; i < size(); ++i) {
938 // First char in paragraph or after label?
942 column += running_font.latexWriteEndChanges(os, basefont, basefont);
945 basefont = getLayoutFont(bparams, outerfont);
946 running_font = basefont;
950 if (style->isCommand()) {
956 column += startTeXParParams(bparams, os,
957 runparams.moving_arg);
960 value_type c = getChar(i);
962 // Fully instantiated font
963 LyXFont font = getFont(bparams, i, outerfont);
965 LyXFont const last_font = running_font;
967 // Spaces at end of font change are simulated to be
968 // outside font change, i.e. we write "\textXX{text} "
969 // rather than "\textXX{text }". (Asger)
970 if (open_font && c == ' ' && i <= size() - 2) {
971 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
972 if (next_font != running_font && next_font != font) {
977 // We end font definition before blanks
979 (font != running_font ||
980 font.language() != running_font.language()))
982 column += running_font.latexWriteEndChanges(os,
984 (i == body_pos-1) ? basefont : font);
985 running_font = basefont;
989 // Blanks are printed before start of fontswitch
991 // Do not print the separation of the optional argument
992 if (i != body_pos - 1) {
993 pimpl_->simpleTeXBlanks(os, texrow, i,
994 column, font, *style);
998 // Do we need to change font?
999 if ((font != running_font ||
1000 font.language() != running_font.language()) &&
1003 column += font.latexWriteStartChanges(os, basefont,
1005 running_font = font;
1009 Change::Type change = pimpl_->lookupChange(i);
1011 column += Changes::latexMarkChange(os, running_change,
1013 running_change = change;
1015 // do not output text which is marked deleted
1016 // if change tracking output is not desired
1017 if (output || running_change != Change::DELETED) {
1018 OutputParams rp = runparams;
1019 rp.free_spacing = style->free_spacing;
1020 rp.local_font = &font;
1021 rp.intitle = style->intitle;
1022 pimpl_->simpleTeXSpecialChars(buf, bparams,
1025 basefont, outerfont, open_font,
1027 *style, i, column, c);
1031 column += Changes::latexMarkChange(os,
1032 running_change, Change::UNCHANGED, output);
1034 // If we have an open font definition, we have to close it
1036 #ifdef FIXED_LANGUAGE_END_DETECTION
1039 .latexWriteEndChanges(os, basefont,
1040 next_->getFont(bparams,
1043 running_font.latexWriteEndChanges(os, basefont,
1047 #ifdef WITH_WARNINGS
1048 //#warning For now we ALWAYS have to close the foreign font settings if they are
1049 //#warning there as we start another \selectlanguage with the next paragraph if
1050 //#warning we are in need of this. This should be fixed sometime (Jug)
1052 running_font.latexWriteEndChanges(os, basefont, basefont);
1056 // Needed if there is an optional argument but no contents.
1057 if (body_pos > 0 && body_pos == size()) {
1059 return_value = false;
1063 column += endTeXParParams(bparams, os, runparams.moving_arg);
1066 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1067 return return_value;
1073 // checks, if newcol chars should be put into this line
1074 // writes newline, if necessary.
1075 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1076 string::size_type newcol)
1079 if (colcount > lyxrc.ascii_linelen) {
1081 colcount = newcol; // assume write after this call
1096 string tag_name(PAR_TAG const & pt) {
1098 case PAR_NONE: return "!-- --";
1099 case TT: return "tt";
1100 case SF: return "sf";
1101 case BF: return "bf";
1102 case IT: return "it";
1103 case SL: return "sl";
1104 case EM: return "em";
1111 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1113 p1 = static_cast<PAR_TAG>(p1 | p2);
1118 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1120 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1126 // Handle internal paragraph parsing -- layout already processed.
1127 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1129 LyXFont const & outerfont,
1130 OutputParams const & runparams,
1131 lyx::depth_type /*depth*/) const
1133 LyXLayout_ptr const & style = layout();
1135 string::size_type char_line_count = 5; // Heuristic choice ;-)
1137 // gets paragraph main font
1140 if (style->labeltype == LABEL_MANUAL) {
1141 font_old = style->labelfont;
1144 font_old = style->font;
1148 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1149 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1150 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1153 stack<PAR_TAG> tag_state;
1154 // parsing main loop
1155 for (pos_type i = 0; i < size(); ++i) {
1157 PAR_TAG tag_close = PAR_NONE;
1158 list < PAR_TAG > tag_open;
1160 LyXFont const font = getFont(buf.params(), i, outerfont);
1162 if (font_old.family() != font.family()) {
1163 switch (family_type) {
1164 case LyXFont::SANS_FAMILY:
1167 case LyXFont::TYPEWRITER_FAMILY:
1174 family_type = font.family();
1176 switch (family_type) {
1177 case LyXFont::SANS_FAMILY:
1178 tag_open.push_back(SF);
1180 case LyXFont::TYPEWRITER_FAMILY:
1181 tag_open.push_back(TT);
1188 if (font_old.series() != font.series()) {
1189 switch (series_type) {
1190 case LyXFont::BOLD_SERIES:
1197 series_type = font.series();
1199 switch (series_type) {
1200 case LyXFont::BOLD_SERIES:
1201 tag_open.push_back(BF);
1209 if (font_old.shape() != font.shape()) {
1210 switch (shape_type) {
1211 case LyXFont::ITALIC_SHAPE:
1214 case LyXFont::SLANTED_SHAPE:
1221 shape_type = font.shape();
1223 switch (shape_type) {
1224 case LyXFont::ITALIC_SHAPE:
1225 tag_open.push_back(IT);
1227 case LyXFont::SLANTED_SHAPE:
1228 tag_open.push_back(SL);
1235 if (font_old.emph() != font.emph()) {
1236 if (font.emph() == LyXFont::ON) {
1237 tag_open.push_back(EM);
1246 list < PAR_TAG > temp;
1247 while (!tag_state.empty() && tag_close) {
1248 PAR_TAG k = tag_state.top();
1250 os << "</" << tag_name(k) << '>';
1257 for(list< PAR_TAG >::const_iterator j = temp.begin();
1258 j != temp.end(); ++j) {
1260 os << '<' << tag_name(*j) << '>';
1263 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1264 j != tag_open.end(); ++j) {
1266 os << '<' << tag_name(*j) << '>';
1269 value_type c = getChar(i);
1272 if (c == Paragraph::META_INSET) {
1273 getInset(i)->linuxdoc(buf, os, runparams);
1278 if (style->latexparam() == "CDATA") {
1279 // "TeX"-Mode on == > SGML-Mode on.
1286 boost::tie(ws, str) = sgml::escapeChar(c);
1287 if (ws && !isFreeSpacing()) {
1288 // in freespacing mode, spaces are
1289 // non-breaking characters
1290 if (desc_on) { // if char is ' ' then...
1292 sgmlLineBreak(os, char_line_count, 6);
1296 sgmlLineBreak(os, char_line_count, 1);
1301 char_line_count += str.length();
1307 while (!tag_state.empty()) {
1308 os << "</" << tag_name(tag_state.top()) << '>';
1312 // resets description flag correctly
1314 // <tag> not closed...
1315 sgmlLineBreak(os, char_line_count, 6);
1321 bool Paragraph::emptyTag() const
1323 for (pos_type i = 0; i < size(); ++i) {
1325 InsetBase const * inset = getInset(i);
1326 InsetBase::Code lyx_code = inset->lyxCode();
1327 if (lyx_code != InsetBase::TOC_CODE &&
1328 lyx_code != InsetBase::INCLUDE_CODE &&
1329 lyx_code != InsetBase::GRAPHICS_CODE &&
1330 lyx_code != InsetBase::ERT_CODE &&
1331 lyx_code != InsetBase::FLOAT_CODE &&
1332 lyx_code != InsetBase::TABULAR_CODE) {
1336 value_type c = getChar(i);
1337 if (c != ' ' && c != '\t')
1345 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1347 for (pos_type i = 0; i < size(); ++i) {
1349 InsetBase const * inset = getInset(i);
1350 InsetBase::Code lyx_code = inset->lyxCode();
1351 if (lyx_code == InsetBase::LABEL_CODE) {
1352 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1353 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1362 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1365 for (i = 0; i < size(); ++i) {
1367 InsetBase const * inset = getInset(i);
1368 inset->docbook(buf, os, runparams);
1370 value_type c = getChar(i);
1375 boost::tie(ws, str) = sgml::escapeChar(c);
1384 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1388 for (pos_type i = initial; i < size(); ++i) {
1389 LyXFont font = getFont(buf.params(), i, outerfont);
1392 if (i != initial && font != font_old)
1401 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1403 OutputParams const & runparams,
1404 LyXFont const & outerfont,
1405 pos_type initial) const
1407 bool emph_flag = false;
1409 LyXLayout_ptr const & style = layout();
1411 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1413 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1416 // parsing main loop
1417 for (pos_type i = initial; i < size(); ++i) {
1418 LyXFont font = getFont(buf.params(), i, outerfont);
1420 // handle <emphasis> tag
1421 if (font_old.emph() != font.emph()) {
1422 if (font.emph() == LyXFont::ON) {
1425 } else if (i != initial) {
1426 os << "</emphasis>";
1432 InsetBase const * inset = getInset(i);
1433 inset->docbook(buf, os, runparams);
1435 value_type c = getChar(i);
1438 boost::tie(ws, str) = sgml::escapeChar(c);
1440 if (style->pass_thru)
1449 os << "</emphasis>";
1452 if (style->free_spacing)
1454 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1459 bool Paragraph::isNewline(pos_type pos) const
1462 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1466 bool Paragraph::isLineSeparator(pos_type pos) const
1468 value_type const c = getChar(pos);
1469 return IsLineSeparatorChar(c)
1470 || (c == Paragraph::META_INSET && getInset(pos) &&
1471 getInset(pos)->isLineSeparator());
1475 /// Used by the spellchecker
1476 bool Paragraph::isLetter(pos_type pos) const
1479 return getInset(pos)->isLetter();
1481 value_type const c = getChar(pos);
1482 return IsLetterChar(c) || IsDigit(c);
1488 Paragraph::getParLanguage(BufferParams const & bparams) const
1491 return getFirstFontSettings(bparams).language();
1492 #ifdef WITH_WARNINGS
1493 #warning FIXME we should check the prev par as well (Lgb)
1495 return bparams.language;
1499 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1501 return lyxrc.rtl_support
1502 && getParLanguage(bparams)->RightToLeft()
1503 && ownerCode() != InsetBase::ERT_CODE;
1507 void Paragraph::changeLanguage(BufferParams const & bparams,
1508 Language const * from, Language const * to)
1510 for (pos_type i = 0; i < size(); ++i) {
1511 LyXFont font = getFontSettings(bparams, i);
1512 if (font.language() == from) {
1513 font.setLanguage(to);
1520 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1522 Language const * doc_language = bparams.language;
1523 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1524 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1526 for (; cit != end; ++cit)
1527 if (cit->font().language() != ignore_language &&
1528 cit->font().language() != latex_language &&
1529 cit->font().language() != doc_language)
1535 // Convert the paragraph to a string.
1536 // Used for building the table of contents
1537 string const Paragraph::asString(Buffer const & buffer, bool label) const
1539 OutputParams runparams;
1540 return asString(buffer, runparams, label);
1544 string const Paragraph::asString(Buffer const & buffer,
1545 OutputParams const & runparams,
1550 if (label && !params().labelString().empty())
1551 s += params().labelString() + ' ';
1553 for (pos_type i = 0; i < size(); ++i) {
1554 value_type c = getChar(i);
1557 else if (c == META_INSET &&
1558 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1560 getInset(i)->plaintext(buffer, os, runparams);
1561 s += subst(STRCONV(os.str()),'\n',' ');
1567 // This should really be done by the caller and not here.
1568 string ret = asString(buffer, runparams, 0, size(), label);
1569 return subst(ret, '\n', ' ');
1574 string const Paragraph::asString(Buffer const & buffer,
1575 pos_type beg, pos_type end, bool label) const
1578 OutputParams const runparams;
1579 return asString(buffer, runparams, beg, end, label);
1583 string const Paragraph::asString(Buffer const & buffer,
1584 OutputParams const & runparams,
1585 pos_type beg, pos_type end, bool label) const
1589 if (beg == 0 && label && !params().labelString().empty())
1590 os << params().labelString() << ' ';
1592 for (pos_type i = beg; i < end; ++i) {
1593 value_type const c = getUChar(buffer.params(), i);
1596 else if (c == META_INSET)
1597 getInset(i)->textString(buffer, os, runparams);
1604 void Paragraph::setInsetOwner(InsetBase * inset)
1606 pimpl_->inset_owner = inset;
1610 void Paragraph::setContentsFromPar(Paragraph const & par)
1612 pimpl_->setContentsFromPar(par);
1616 void Paragraph::trackChanges(Change::Type type)
1618 pimpl_->trackChanges(type);
1622 void Paragraph::untrackChanges()
1624 pimpl_->untrackChanges();
1628 void Paragraph::cleanChanges()
1630 pimpl_->cleanChanges();
1634 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1636 BOOST_ASSERT(pos <= size());
1637 return pimpl_->lookupChange(pos);
1641 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1643 BOOST_ASSERT(pos <= size());
1644 return pimpl_->lookupChangeFull(pos);
1648 bool Paragraph::isChanged(pos_type start, pos_type end) const
1650 return pimpl_->isChanged(start, end);
1654 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1656 return pimpl_->isChangeEdited(start, end);
1660 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1662 pimpl_->setChange(pos, type);
1666 void Paragraph::setChangeFull(lyx::pos_type pos, Change change)
1668 pimpl_->setChangeFull(pos, change);
1672 void Paragraph::markErased(bool erased)
1674 pimpl_->markErased(erased);
1678 void Paragraph::acceptChange(pos_type start, pos_type end)
1680 return pimpl_->acceptChange(start, end);
1684 void Paragraph::rejectChange(pos_type start, pos_type end)
1686 return pimpl_->rejectChange(start, end);
1690 int Paragraph::id() const
1696 LyXLayout_ptr const & Paragraph::layout() const
1702 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1704 layout_ = new_layout;
1708 InsetBase * Paragraph::inInset() const
1710 return pimpl_->inset_owner;
1714 InsetBase::Code Paragraph::ownerCode() const
1716 return pimpl_->inset_owner
1717 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1721 void Paragraph::clearContents()
1727 void Paragraph::setChar(pos_type pos, value_type c)
1733 ParagraphParameters & Paragraph::params()
1735 return pimpl_->params;
1739 ParagraphParameters const & Paragraph::params() const
1741 return pimpl_->params;
1745 bool Paragraph::isFreeSpacing() const
1747 if (layout()->free_spacing)
1750 // for now we just need this, later should we need this in some
1751 // other way we can always add a function to InsetBase too.
1752 return ownerCode() == InsetBase::ERT_CODE;
1756 bool Paragraph::allowEmpty() const
1758 if (layout()->keepempty)
1760 return ownerCode() == InsetBase::ERT_CODE;
1764 Row & Paragraph::getRow(pos_type pos, bool boundary)
1766 BOOST_ASSERT(!rows().empty());
1768 // If boundary is set we should return the row on which
1769 // the character before is inside.
1770 if (pos > 0 && boundary)
1773 RowList::iterator rit = rows_.end();
1774 RowList::iterator const begin = rows_.begin();
1776 for (--rit; rit != begin && rit->pos() > pos; --rit)
1783 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1785 BOOST_ASSERT(!rows().empty());
1787 // If boundary is set we should return the row on which
1788 // the character before is inside.
1789 if (pos > 0 && boundary)
1792 RowList::const_iterator rit = rows_.end();
1793 RowList::const_iterator const begin = rows_.begin();
1795 for (--rit; rit != begin && rit->pos() > pos; --rit)
1802 size_t Paragraph::pos2row(pos_type pos) const
1804 BOOST_ASSERT(!rows().empty());
1806 RowList::const_iterator rit = rows_.end();
1807 RowList::const_iterator const begin = rows_.begin();
1809 for (--rit; rit != begin && rit->pos() > pos; --rit)
1816 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1818 if (!Encodings::is_arabic(c))
1819 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1820 return c + (0xb0 - '0');
1824 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1825 unsigned char next_char = ' ';
1827 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1828 unsigned char const par_char = getChar(i);
1829 if (!Encodings::IsComposeChar_arabic(par_char)) {
1830 next_char = par_char;
1835 if (Encodings::is_arabic(next_char)) {
1836 if (Encodings::is_arabic(prev_char) &&
1837 !Encodings::is_arabic_special(prev_char))
1838 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1840 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1842 if (Encodings::is_arabic(prev_char) &&
1843 !Encodings::is_arabic_special(prev_char))
1844 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1846 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1851 void Paragraph::dump() const
1853 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1854 for (size_t i = 0; i != rows_.size(); ++i) {
1855 lyxerr << " row " << i << ": ";