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"
33 #include "outputparams.h"
34 #include "paragraph_funcs.h"
39 #include "insets/insetbibitem.h"
40 #include "insets/insetoptarg.h"
42 #include "support/lstrings.h"
43 #include "support/std_sstream.h"
44 #include "support/textutils.h"
45 #include "support/tostr.h"
47 #include <boost/tuple/tuple.hpp>
48 #include <boost/bind.hpp>
55 using lyx::support::contains;
56 using lyx::support::subst;
64 using std::ostringstream;
67 Paragraph::Paragraph()
68 : y(0), height(0), begin_of_body_(0),
69 pimpl_(new Paragraph::Pimpl(this))
76 Paragraph::Paragraph(Paragraph const & par)
77 : y(0), height(0), text_(par.text_), begin_of_body_(par.begin_of_body_),
78 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
81 // this is because of the dummy layout of the paragraphs that
83 layout_ = par.layout();
85 // copy everything behind the break-position to the new paragraph
86 insetlist = par.insetlist;
87 InsetList::iterator it = insetlist.begin();
88 InsetList::iterator end = insetlist.end();
89 for (; it != end; ++it)
90 it->inset = it->inset->clone().release();
94 void Paragraph::operator=(Paragraph const & par)
96 // needed as we will destroy the pimpl_ before copying it
100 lyxerr << "Paragraph::operator=()" << endl;
105 pimpl_ = new Pimpl(*par.pimpl_, this);
107 itemdepth = par.itemdepth;
108 // this is because of the dummy layout of the paragraphs that
110 layout_ = par.layout();
112 // copy everything behind the break-position to the new paragraph
113 insetlist = par.insetlist;
114 InsetList::iterator it = insetlist.begin();
115 InsetList::iterator end = insetlist.end();
116 for (; it != end; ++it)
117 it->inset = it->inset->clone().release();
121 Paragraph::~Paragraph()
125 //lyxerr << "Paragraph::paragraph_id = "
126 // << Paragraph::paragraph_id << endl;
130 void Paragraph::write(Buffer const & buf, ostream & os,
131 BufferParams const & bparams,
132 depth_type & dth) const
134 // The beginning or end of a deeper (i.e. nested) area?
135 if (dth != params().depth()) {
136 if (params().depth() > dth) {
137 while (params().depth() > dth) {
138 os << "\n\\begin_deeper ";
142 while (params().depth() < dth) {
143 os << "\n\\end_deeper ";
149 // First write the layout
150 os << "\n\\begin_layout " << layout()->name() << '\n';
154 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
156 Change running_change = Change(Change::UNCHANGED);
157 lyx::time_type const curtime(lyx::current_time());
160 for (pos_type i = 0; i < size(); ++i) {
166 Change change = pimpl_->lookupChangeFull(i);
167 Changes::lyxMarkChange(os, column, curtime, running_change, change);
168 running_change = change;
170 // Write font changes
171 LyXFont font2 = getFontSettings(bparams, i);
172 if (font2 != font1) {
173 font2.lyxWriteChanges(font1, os);
178 value_type const c = getChar(i);
182 InsetBase const * inset = getInset(i);
184 if (inset->directWrite()) {
185 // international char, let it write
186 // code directly so it's shorter in
188 inset->write(buf, os);
190 os << "\n\\begin_inset ";
191 inset->write(buf, os);
192 os << "\n\\end_inset \n\n";
198 os << "\n\\backslash \n";
202 if (i + 1 < size() && getChar(i + 1) == ' ') {
209 if ((column > 70 && c == ' ')
214 // this check is to amend a bug. LyX sometimes
215 // inserts '\0' this could cause problems.
219 lyxerr << "ERROR (Paragraph::writeFile):"
220 " NULL char in structure." << endl;
226 // to make reading work properly
228 running_change = pimpl_->lookupChange(0);
229 Changes::lyxMarkChange(os, column, curtime,
230 Change(Change::UNCHANGED), running_change);
232 Changes::lyxMarkChange(os, column, curtime,
233 running_change, Change(Change::UNCHANGED));
235 os << "\n\\end_layout\n";
239 void Paragraph::validate(LaTeXFeatures & features) const
241 pimpl_->validate(features, *layout());
245 void Paragraph::eraseIntern(lyx::pos_type pos)
247 pimpl_->eraseIntern(pos);
251 bool Paragraph::erase(pos_type pos)
253 return pimpl_->erase(pos);
257 int Paragraph::erase(pos_type start, pos_type end)
259 return pimpl_->erase(start, end);
263 void Paragraph::insert(pos_type start, string const & str,
264 LyXFont const & font)
266 int size = str.size();
267 for (int i = 0 ; i < size ; ++i)
268 insertChar(start + i, str[i], font);
272 bool Paragraph::checkInsertChar(LyXFont &)
278 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
280 insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
284 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
285 LyXFont const & font, Change change)
287 pimpl_->insertChar(pos, c, font, change);
291 void Paragraph::insertInset(pos_type pos, InsetBase * inset)
293 insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
297 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
298 LyXFont const & font, Change change)
300 pimpl_->insertInset(pos, inset, font, change);
304 bool Paragraph::insetAllowed(InsetOld_code code)
306 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
307 if (pimpl_->inset_owner)
308 return pimpl_->inset_owner->insetAllowed(code);
313 InsetBase * Paragraph::getInset(pos_type pos)
315 BOOST_ASSERT(pos < size());
316 return insetlist.get(pos);
320 InsetBase const * Paragraph::getInset(pos_type pos) const
322 BOOST_ASSERT(pos < size());
323 return insetlist.get(pos);
327 // Gets uninstantiated font setting at position.
328 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
331 BOOST_ASSERT(pos <= size());
333 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
334 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
335 for (; cit != end; ++cit)
336 if (cit->pos() >= pos)
342 if (pos == size() && !empty())
343 return getFontSettings(bparams, pos - 1);
345 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
349 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
351 BOOST_ASSERT(pos <= size());
353 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
354 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
355 for (; cit != end; ++cit)
356 if (cit->pos() >= pos)
359 // This should not happen, but if so, we take no chances.
360 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
366 // Gets uninstantiated font setting at position 0
367 LyXFont const Paragraph::getFirstFontSettings() const
369 if (!empty() && !pimpl_->fontlist.empty())
370 return pimpl_->fontlist[0].font();
372 return LyXFont(LyXFont::ALL_INHERIT);
376 // Gets the fully instantiated font at a given position in a paragraph
377 // This is basically the same function as LyXText::GetFont() in text2.C.
378 // The difference is that this one is used for generating the LaTeX file,
379 // and thus cosmetic "improvements" are disallowed: This has to deliver
380 // the true picture of the buffer. (Asger)
381 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
382 LyXFont const & outerfont) const
384 BOOST_ASSERT(pos >= 0);
386 LyXLayout_ptr const & lout = layout();
388 pos_type const body_pos = beginOfBody();
392 layoutfont = lout->labelfont;
394 layoutfont = lout->font;
396 LyXFont font = getFontSettings(bparams, pos);
397 font.realize(layoutfont);
398 font.realize(outerfont);
399 font.realize(bparams.getLyXTextClass().defaultfont());
405 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
406 LyXFont const & outerfont) const
408 LyXFont tmpfont = layout()->labelfont;
409 tmpfont.setLanguage(getParLanguage(bparams));
410 tmpfont.realize(outerfont);
411 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
416 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
417 LyXFont const & outerfont) const
419 LyXFont tmpfont = layout()->font;
420 tmpfont.setLanguage(getParLanguage(bparams));
421 tmpfont.realize(outerfont);
422 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
427 /// Returns the height of the highest font in range
429 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
430 LyXFont_size def_size) const
432 if (pimpl_->fontlist.empty())
435 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
436 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
437 for (; end_it != end; ++end_it) {
438 if (end_it->pos() >= endpos)
445 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
446 for (; cit != end; ++cit) {
447 if (cit->pos() >= startpos)
451 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
452 for (; cit != end_it; ++cit) {
453 LyXFont::FONT_SIZE size = cit->font().size();
454 if (size == LyXFont::INHERIT_SIZE)
456 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
463 Paragraph::value_type
464 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
466 value_type c = getChar(pos);
467 if (!lyxrc.rtl_support)
497 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
504 void Paragraph::setFont(pos_type pos, LyXFont const & font)
506 BOOST_ASSERT(pos <= size());
508 // First, reduce font against layout/label font
509 // Update: The setCharFont() routine in text2.C already
510 // reduces font, so we don't need to do that here. (Asger)
511 // No need to simplify this because it will disappear
512 // in a new kernel. (Asger)
513 // Next search font table
515 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
516 Pimpl::FontList::iterator it = beg;
517 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
518 for (; it != endit; ++it) {
519 if (it->pos() >= pos)
522 unsigned int i = distance(beg, it);
523 bool notfound = (it == endit);
525 if (!notfound && pimpl_->fontlist[i].font() == font)
528 bool begin = pos == 0 || notfound ||
529 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
530 // Is position pos is a beginning of a font block?
531 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
532 // Is position pos is the end of a font block?
533 if (begin && end) { // A single char block
534 if (i + 1 < pimpl_->fontlist.size() &&
535 pimpl_->fontlist[i + 1].font() == font) {
536 // Merge the singleton block with the next block
537 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
538 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
539 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
540 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
541 // Merge the singleton block with the previous block
542 pimpl_->fontlist[i - 1].pos(pos);
543 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
545 pimpl_->fontlist[i].font(font);
547 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
548 pimpl_->fontlist[i - 1].pos(pos);
550 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
551 Pimpl::FontTable(pos, font));
553 pimpl_->fontlist[i].pos(pos - 1);
554 if (!(i + 1 < pimpl_->fontlist.size() &&
555 pimpl_->fontlist[i + 1].font() == font))
556 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
557 Pimpl::FontTable(pos, font));
558 } else { // The general case. The block is splitted into 3 blocks
559 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
560 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
561 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
562 Pimpl::FontTable(pos, font));
567 void Paragraph::makeSameLayout(Paragraph const & par)
569 layout(par.layout());
571 params() = par.params();
575 int Paragraph::stripLeadingSpaces()
581 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
582 pimpl_->eraseIntern(0);
590 bool Paragraph::hasSameLayout(Paragraph const & par) const
592 return par.layout() == layout() && params().sameLayout(par.params());
596 Paragraph::depth_type Paragraph::getDepth() const
598 return params().depth();
602 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
604 if (layout()->isEnvironment())
605 return params().depth() + 1;
607 return params().depth();
611 char Paragraph::getAlign() const
613 return params().align();
617 string const & Paragraph::getLabelstring() const
619 return params().labelString();
623 // the next two functions are for the manual labels
624 string const Paragraph::getLabelWidthString() const
626 if (!params().labelWidthString().empty())
627 return params().labelWidthString();
629 return _("Senseless with this layout!");
633 void Paragraph::setLabelWidthString(string const & s)
635 params().labelWidthString(s);
639 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
642 params().labelWidthString(string());
643 params().align(LYX_ALIGN_LAYOUT);
644 params().spacing(Spacing(Spacing::Default));
648 int Paragraph::beginOfBody() const
650 return begin_of_body_;
654 void Paragraph::setBeginOfBody()
656 if (layout()->labeltype != LABEL_MANUAL) {
661 // Unroll the first two cycles of the loop
662 // and remember the previous character to
663 // remove unnecessary getChar() calls
665 pos_type end = size();
666 if (i < end && !isNewline(i)) {
668 char previous_char = 0;
671 previous_char = text_[i];
674 while (i < end && previous_char != ' ') {
679 previous_char = temp;
689 // returns -1 if inset not found
690 int Paragraph::getPositionOfInset(InsetBase const * inset) const
693 InsetList::const_iterator it = insetlist.begin();
694 InsetList::const_iterator end = insetlist.end();
695 for (; it != end; ++it)
696 if (it->inset == inset)
702 InsetBibitem * Paragraph::bibitem() const
704 if (!insetlist.empty()) {
705 InsetBase * inset = insetlist.begin()->inset;
706 if (inset->lyxCode() == InsetBase::BIBTEX_CODE)
707 return static_cast<InsetBibitem *>(inset);
715 // paragraphs inside floats need different alignment tags to avoid
718 bool noTrivlistCentering(UpdatableInset const * inset)
720 if (inset && inset->owner()) {
721 InsetBase::Code const code = inset->owner()->lyxCode();
722 return code == InsetBase::FLOAT_CODE ||
723 code == InsetBase::WRAP_CODE;
729 string correction(string const & orig)
731 if (orig == "flushleft")
732 return "raggedright";
733 if (orig == "flushright")
735 if (orig == "center")
741 string const corrected_env(string const & suffix, string const & env,
742 UpdatableInset const * inset)
744 string output = suffix + "{";
745 if (noTrivlistCentering(inset))
746 output += correction(env);
755 // This could go to ParagraphParameters if we want to
756 int Paragraph::startTeXParParams(BufferParams const & bparams,
757 ostream & os, bool moving_arg) const
761 if (params().noindent()) {
766 switch (params().align()) {
768 case LYX_ALIGN_BLOCK:
769 case LYX_ALIGN_LAYOUT:
770 case LYX_ALIGN_SPECIAL:
773 case LYX_ALIGN_RIGHT:
774 case LYX_ALIGN_CENTER:
782 switch (params().align()) {
784 case LYX_ALIGN_BLOCK:
785 case LYX_ALIGN_LAYOUT:
786 case LYX_ALIGN_SPECIAL:
788 case LYX_ALIGN_LEFT: {
790 UpdatableInset const * const inset = pimpl_->inset_owner;
791 if (getParLanguage(bparams)->babel() != "hebrew")
792 output = corrected_env("\\begin", "flushleft", inset);
794 output = corrected_env("\\begin", "flushright", inset);
796 column += output.size();
798 } case LYX_ALIGN_RIGHT: {
800 UpdatableInset const * const inset = pimpl_->inset_owner;
801 if (getParLanguage(bparams)->babel() != "hebrew")
802 output = corrected_env("\\begin", "flushright", inset);
804 output = corrected_env("\\begin", "flushleft", inset);
806 column += output.size();
808 } case LYX_ALIGN_CENTER: {
810 output = corrected_env("\\begin", "center", pimpl_->inset_owner);
812 column += output.size();
821 // This could go to ParagraphParameters if we want to
822 int Paragraph::endTeXParParams(BufferParams const & bparams,
823 ostream & os, bool moving_arg) const
827 switch (params().align()) {
829 case LYX_ALIGN_BLOCK:
830 case LYX_ALIGN_LAYOUT:
831 case LYX_ALIGN_SPECIAL:
834 case LYX_ALIGN_RIGHT:
835 case LYX_ALIGN_CENTER:
843 switch (params().align()) {
845 case LYX_ALIGN_BLOCK:
846 case LYX_ALIGN_LAYOUT:
847 case LYX_ALIGN_SPECIAL:
849 case LYX_ALIGN_LEFT: {
851 UpdatableInset const * const inset = pimpl_->inset_owner;
852 if (getParLanguage(bparams)->babel() != "hebrew")
853 output = corrected_env("\\par\\end", "flushleft", inset);
855 output = corrected_env("\\par\\end", "flushright", inset);
857 column += output.size();
859 } case LYX_ALIGN_RIGHT: {
861 UpdatableInset const * const inset = pimpl_->inset_owner;
862 if (getParLanguage(bparams)->babel() != "hebrew")
863 output = corrected_env("\\par\\end", "flushright", inset);
865 output = corrected_env("\\par\\end", "flushleft", inset);
867 column += output.size();
869 } case LYX_ALIGN_CENTER: {
871 output = corrected_env("\\par\\end", "center", pimpl_->inset_owner);
873 column += output.size();
882 // This one spits out the text of the paragraph
883 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
884 BufferParams const & bparams,
885 LyXFont const & outerfont,
886 ostream & os, TexRow & texrow,
887 OutputParams const & runparams)
889 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
891 bool return_value = false;
895 // well we have to check if we are in an inset with unlimited
896 // length (all in one row) if that is true then we don't allow
897 // any special options in the paragraph and also we don't allow
898 // any environment other then "Standard" to be valid!
900 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
903 style = bparams.getLyXTextClass().defaultLayout();
910 // Maybe we have to create a optional argument.
911 pos_type body_pos = beginOfBody();
912 unsigned int column = 0;
917 basefont = getLabelFont(bparams, outerfont);
919 basefont = getLayoutFont(bparams, outerfont);
922 bool moving_arg = runparams.moving_arg;
923 moving_arg |= style->needprotect;
925 // Which font is currently active?
926 LyXFont running_font(basefont);
927 // Do we have an open font change?
928 bool open_font = false;
930 Change::Type running_change = Change::UNCHANGED;
932 texrow.start(id(), 0);
934 // if the paragraph is empty, the loop will not be entered at all
936 if (style->isCommand()) {
941 column += startTeXParParams(bparams, os, moving_arg);
944 for (pos_type i = 0; i < size(); ++i) {
946 // First char in paragraph or after label?
950 column += running_font.latexWriteEndChanges(os, basefont, basefont);
953 basefont = getLayoutFont(bparams, outerfont);
954 running_font = basefont;
958 if (style->isCommand()) {
964 column += startTeXParParams(bparams, os,
968 value_type c = getChar(i);
970 // Fully instantiated font
971 LyXFont font = getFont(bparams, i, outerfont);
973 LyXFont const last_font = running_font;
975 // Spaces at end of font change are simulated to be
976 // outside font change, i.e. we write "\textXX{text} "
977 // rather than "\textXX{text }". (Asger)
978 if (open_font && c == ' ' && i <= size() - 2) {
979 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
980 if (next_font != running_font && next_font != font) {
985 // We end font definition before blanks
987 (font != running_font ||
988 font.language() != running_font.language()))
990 column += running_font.latexWriteEndChanges(os,
992 (i == body_pos-1) ? basefont : font);
993 running_font = basefont;
997 // Blanks are printed before start of fontswitch
999 // Do not print the separation of the optional argument
1000 if (i != body_pos - 1) {
1001 pimpl_->simpleTeXBlanks(os, texrow, i,
1002 column, font, *style);
1006 // Do we need to change font?
1007 if ((font != running_font ||
1008 font.language() != running_font.language()) &&
1011 column += font.latexWriteStartChanges(os, basefont,
1013 running_font = font;
1017 Change::Type change = pimpl_->lookupChange(i);
1019 column += Changes::latexMarkChange(os, running_change, change);
1020 running_change = change;
1022 OutputParams rp = runparams;
1023 rp.moving_arg = moving_arg;
1024 rp.free_spacing = style->free_spacing;
1025 pimpl_->simpleTeXSpecialChars(buf, bparams,
1026 os, texrow, runparams,
1028 basefont, outerfont, open_font,
1030 *style, i, column, c);
1033 column += Changes::latexMarkChange(os,
1034 running_change, Change::UNCHANGED);
1036 // If we have an open font definition, we have to close it
1038 #ifdef FIXED_LANGUAGE_END_DETECTION
1041 .latexWriteEndChanges(os, basefont,
1042 next_->getFont(bparams,
1045 running_font.latexWriteEndChanges(os, basefont,
1049 #ifdef WITH_WARNINGS
1050 //#warning For now we ALWAYS have to close the foreign font settings if they are
1051 //#warning there as we start another \selectlanguage with the next paragraph if
1052 //#warning we are in need of this. This should be fixed sometime (Jug)
1054 running_font.latexWriteEndChanges(os, basefont, basefont);
1058 // Needed if there is an optional argument but no contents.
1059 if (body_pos > 0 && body_pos == size()) {
1061 return_value = false;
1065 column += endTeXParParams(bparams, os, moving_arg);
1068 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1069 return return_value;
1075 // checks, if newcol chars should be put into this line
1076 // writes newline, if necessary.
1077 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1078 string::size_type newcol)
1081 if (colcount > lyxrc.ascii_linelen) {
1083 colcount = newcol; // assume write after this call
1098 string tag_name(PAR_TAG const & pt) {
1100 case PAR_NONE: return "!-- --";
1101 case TT: return "tt";
1102 case SF: return "sf";
1103 case BF: return "bf";
1104 case IT: return "it";
1105 case SL: return "sl";
1106 case EM: return "em";
1113 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1115 p1 = static_cast<PAR_TAG>(p1 | p2);
1120 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1122 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1128 // Handle internal paragraph parsing -- layout already processed.
1129 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1131 LyXFont const & outerfont,
1132 OutputParams const & runparams,
1133 lyx::depth_type /*depth*/) const
1135 LyXLayout_ptr const & style = layout();
1137 string::size_type char_line_count = 5; // Heuristic choice ;-)
1139 // gets paragraph main font
1142 if (style->labeltype == LABEL_MANUAL) {
1143 font_old = style->labelfont;
1146 font_old = style->font;
1150 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1151 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1152 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1155 stack<PAR_TAG> tag_state;
1156 // parsing main loop
1157 for (pos_type i = 0; i < size(); ++i) {
1159 PAR_TAG tag_close = PAR_NONE;
1160 list < PAR_TAG > tag_open;
1162 LyXFont const font = getFont(buf.params(), i, outerfont);
1164 if (font_old.family() != font.family()) {
1165 switch (family_type) {
1166 case LyXFont::SANS_FAMILY:
1169 case LyXFont::TYPEWRITER_FAMILY:
1176 family_type = font.family();
1178 switch (family_type) {
1179 case LyXFont::SANS_FAMILY:
1180 tag_open.push_back(SF);
1182 case LyXFont::TYPEWRITER_FAMILY:
1183 tag_open.push_back(TT);
1190 if (font_old.series() != font.series()) {
1191 switch (series_type) {
1192 case LyXFont::BOLD_SERIES:
1199 series_type = font.series();
1201 switch (series_type) {
1202 case LyXFont::BOLD_SERIES:
1203 tag_open.push_back(BF);
1211 if (font_old.shape() != font.shape()) {
1212 switch (shape_type) {
1213 case LyXFont::ITALIC_SHAPE:
1216 case LyXFont::SLANTED_SHAPE:
1223 shape_type = font.shape();
1225 switch (shape_type) {
1226 case LyXFont::ITALIC_SHAPE:
1227 tag_open.push_back(IT);
1229 case LyXFont::SLANTED_SHAPE:
1230 tag_open.push_back(SL);
1237 if (font_old.emph() != font.emph()) {
1238 if (font.emph() == LyXFont::ON) {
1239 tag_open.push_back(EM);
1248 list < PAR_TAG > temp;
1249 while (!tag_state.empty() && tag_close) {
1250 PAR_TAG k = tag_state.top();
1252 os << "</" << tag_name(k) << '>';
1259 for(list< PAR_TAG >::const_iterator j = temp.begin();
1260 j != temp.end(); ++j) {
1262 os << '<' << tag_name(*j) << '>';
1265 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1266 j != tag_open.end(); ++j) {
1268 os << '<' << tag_name(*j) << '>';
1271 char c = getChar(i);
1274 if (c == Paragraph::META_INSET) {
1275 getInset(i)->linuxdoc(buf, os, runparams);
1280 if (style->latexparam() == "CDATA") {
1281 // "TeX"-Mode on == > SGML-Mode on.
1288 boost::tie(ws, str) = sgml::escapeChar(c);
1289 if (ws && !isFreeSpacing()) {
1290 // in freespacing mode, spaces are
1291 // non-breaking characters
1292 if (desc_on) { // if char is ' ' then...
1294 sgmlLineBreak(os, char_line_count, 6);
1298 sgmlLineBreak(os, char_line_count, 1);
1303 char_line_count += str.length();
1309 while (!tag_state.empty()) {
1310 os << "</" << tag_name(tag_state.top()) << '>';
1314 // resets description flag correctly
1316 // <tag> not closed...
1317 sgmlLineBreak(os, char_line_count, 6);
1323 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1325 LyXFont const & outerfont,
1326 OutputParams const & runparams,
1327 lyx::depth_type depth,
1330 bool emph_flag = false;
1332 LyXLayout_ptr const & style = layout();
1333 LyXLayout_ptr const & defaultstyle =
1334 buf.params().getLyXTextClass().defaultLayout();
1337 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1339 int char_line_count = depth;
1340 bool label_closed = true;
1341 bool para_closed = true;
1343 if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1345 Counters & counters = buf.params().getLyXTextClass().counters();
1346 if (!style->free_spacing)
1347 os << string(depth,' ');
1348 if (!style->labeltag().empty()) {
1349 os << "<" << style->labeltag() << ">\n";
1350 label_closed = false;
1352 if (!defaultstyle->latexparam().empty()) {
1353 counters.step("para");
1354 ls = tostr(counters.value("para"));
1356 + subst(defaultstyle->latexparam(), "#", ls) + '"';
1358 os << "<" << style->itemtag() << ">\n"
1359 << string(depth, ' ') << "<"
1360 << defaultstyle->latexname() << ls << ">\n";
1361 para_closed = false;
1365 // parsing main loop
1366 for (pos_type i = 0; i < size(); ++i) {
1367 LyXFont font = getFont(buf.params(), i, outerfont);
1369 // handle <emphasis> tag
1370 if (font_old.emph() != font.emph()) {
1371 if (font.emph() == LyXFont::ON) {
1372 if (style->latexparam() == "CDATA")
1375 if (style->latexparam() == "CDATA")
1379 if (style->latexparam() == "CDATA")
1381 os << "</emphasis>";
1382 if (style->latexparam() == "CDATA")
1390 InsetBase const * inset = getInset(i);
1391 // don't print the inset in position 0 if desc_on == 3 (label)
1392 //if (i || desc_on != 3) {
1394 if (style->latexparam() == "CDATA")
1396 inset->docbook(buf, os, runparams);
1397 if (style->latexparam() == "CDATA")
1401 char c = getChar(i);
1404 boost::tie(ws, str) = sgml::escapeChar(c);
1406 if (style->pass_thru) {
1408 } else if (isFreeSpacing() || c != ' ') {
1410 } else if (!style->labeltag().empty() && !label_closed) {
1412 os << "\n</" << style->labeltag() << "><"
1413 << style->itemtag() << "><"
1414 << defaultstyle->latexname() << ">";
1415 label_closed = true;
1416 para_closed = false;
1425 if (style->latexparam() == "CDATA")
1427 os << "</emphasis>";
1428 if (style->latexparam() == "CDATA")
1432 // resets description flag correctly
1433 if (!label_closed) {
1434 // <term> not closed...
1435 os << "</" << style->labeltag() << ">\n<"
1436 << style->itemtag() << "><"
1437 << defaultstyle->latexname() << "> ";
1440 os << "\n" << string(depth, ' ') << "</"
1441 << defaultstyle->latexname() << ">\n";
1443 if (style->free_spacing)
1450 /// return true if the char is a meta-character for an inset
1452 bool IsInsetChar(char c)
1454 return (c == Paragraph::META_INSET);
1461 bool Paragraph::isHfill(pos_type pos) const
1463 return IsInsetChar(getChar(pos))
1464 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1468 bool Paragraph::isInset(pos_type pos) const
1470 return IsInsetChar(getChar(pos));
1474 bool Paragraph::isNewline(pos_type pos) const
1476 return IsInsetChar(getChar(pos))
1477 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1481 bool Paragraph::isSeparator(pos_type pos) const
1483 return IsSeparatorChar(getChar(pos));
1487 bool Paragraph::isLineSeparator(pos_type pos) const
1489 value_type const c = getChar(pos);
1490 return IsLineSeparatorChar(c)
1491 || (IsInsetChar(c) && getInset(pos) &&
1492 getInset(pos)->isLineSeparator());
1496 bool Paragraph::isKomma(pos_type pos) const
1498 return IsKommaChar(getChar(pos));
1502 /// Used by the spellchecker
1503 bool Paragraph::isLetter(pos_type pos) const
1505 value_type const c = getChar(pos);
1506 if (IsLetterChar(c))
1509 return getInset(pos)->isLetter();
1510 // We want to pass the ' and escape chars to ispell
1511 string const extra = lyxrc.isp_esc_chars + '\'';
1512 return contains(extra, c);
1516 bool Paragraph::isWord(pos_type pos) const
1518 unsigned char const c = getChar(pos);
1519 return !(IsSeparatorChar(c)
1526 Paragraph::getParLanguage(BufferParams const & bparams) const
1529 return getFirstFontSettings().language();
1530 #warning FIXME we should check the prev par as well (Lgb)
1531 return bparams.language;
1535 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1537 return lyxrc.rtl_support
1538 && getParLanguage(bparams)->RightToLeft()
1539 && !(inInset() && inInset()->owner() &&
1540 inInset()->owner()->lyxCode() == InsetBase::ERT_CODE);
1544 void Paragraph::changeLanguage(BufferParams const & bparams,
1545 Language const * from, Language const * to)
1547 for (pos_type i = 0; i < size(); ++i) {
1548 LyXFont font = getFontSettings(bparams, i);
1549 if (font.language() == from) {
1550 font.setLanguage(to);
1557 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1559 Language const * doc_language = bparams.language;
1560 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1561 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1563 for (; cit != end; ++cit)
1564 if (cit->font().language() != ignore_language &&
1565 cit->font().language() != latex_language &&
1566 cit->font().language() != doc_language)
1572 // Convert the paragraph to a string.
1573 // Used for building the table of contents
1574 string const Paragraph::asString(Buffer const & buffer,
1577 OutputParams runparams;
1578 return asString(buffer, runparams, label);
1582 string const Paragraph::asString(Buffer const & buffer,
1583 OutputParams const & runparams,
1588 if (label && !params().labelString().empty())
1589 s += params().labelString() + ' ';
1591 for (pos_type i = 0; i < size(); ++i) {
1592 value_type c = getChar(i);
1595 else if (c == META_INSET &&
1596 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1598 getInset(i)->plaintext(buffer, os, runparams);
1599 s += subst(STRCONV(os.str()),'\n',' ');
1605 // This should really be done by the caller and not here.
1606 string ret = asString(buffer, runparams, 0, size(), label);
1607 return subst(ret, '\n', ' ');
1612 string const Paragraph::asString(Buffer const & buffer,
1613 pos_type beg, pos_type end, bool label) const
1616 OutputParams const runparams;
1617 return asString(buffer, runparams, beg, end, label);
1621 string const Paragraph::asString(Buffer const & buffer,
1622 OutputParams const & runparams,
1623 pos_type beg, pos_type end, bool label) const
1627 if (beg == 0 && label && !params().labelString().empty())
1628 os << params().labelString() << ' ';
1630 for (pos_type i = beg; i < end; ++i) {
1631 value_type const c = getUChar(buffer.params(), i);
1634 else if (c == META_INSET)
1635 getInset(i)->plaintext(buffer, os, runparams);
1642 void Paragraph::setInsetOwner(UpdatableInset * inset)
1644 pimpl_->inset_owner = inset;
1645 InsetList::iterator it = insetlist.begin();
1646 InsetList::iterator end = insetlist.end();
1647 for (; it != end; ++it)
1649 it->inset->setOwner(inset);
1653 void Paragraph::setContentsFromPar(Paragraph const & par)
1655 pimpl_->setContentsFromPar(par);
1659 void Paragraph::trackChanges(Change::Type type)
1661 pimpl_->trackChanges(type);
1665 void Paragraph::untrackChanges()
1667 pimpl_->untrackChanges();
1671 void Paragraph::cleanChanges()
1673 pimpl_->cleanChanges();
1677 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1679 BOOST_ASSERT(!size() || pos < size());
1680 return pimpl_->lookupChange(pos);
1684 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1686 BOOST_ASSERT(!size() || pos < size());
1687 return pimpl_->lookupChangeFull(pos);
1691 bool Paragraph::isChanged(pos_type start, pos_type end) const
1693 return pimpl_->isChanged(start, end);
1697 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1699 return pimpl_->isChangeEdited(start, end);
1703 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1705 pimpl_->setChange(pos, type);
1710 void Paragraph::markErased()
1712 pimpl_->markErased();
1716 void Paragraph::acceptChange(pos_type start, pos_type end)
1718 return pimpl_->acceptChange(start, end);
1722 void Paragraph::rejectChange(pos_type start, pos_type end)
1724 return pimpl_->rejectChange(start, end);
1728 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1730 // This is in the critical path!
1731 pos_type const siz = text_.size();
1733 BOOST_ASSERT(0 <= pos);
1734 BOOST_ASSERT(pos <= siz);
1737 lyxerr << "getChar() on pos " << pos << " in par id "
1738 << id() << " of size " << siz
1739 << " is a bit silly !" << endl;
1740 BOOST_ASSERT(false);
1748 int Paragraph::id() const
1754 LyXLayout_ptr const & Paragraph::layout() const
1757 InsetBase * inset = inInset();
1758 if (inset && inset->lyxCode() == InsetBase::ENVIRONMENT_CODE)
1759 return static_cast<InsetEnvironment*>(inset)->layout();
1765 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1767 layout_ = new_layout;
1771 UpdatableInset * Paragraph::inInset() const
1773 return pimpl_->inset_owner;
1777 void Paragraph::clearContents()
1783 void Paragraph::setChar(pos_type pos, value_type c)
1789 ParagraphParameters & Paragraph::params()
1791 return pimpl_->params;
1795 ParagraphParameters const & Paragraph::params() const
1797 return pimpl_->params;
1801 bool Paragraph::isFreeSpacing() const
1803 if (layout()->free_spacing)
1806 // for now we just need this, later should we need this in some
1807 // other way we can always add a function to InsetBase too.
1808 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1809 return pimpl_->inset_owner->owner()->lyxCode() == InsetBase::ERT_CODE;
1814 bool Paragraph::allowEmpty() const
1816 if (layout()->keepempty)
1818 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1819 return pimpl_->inset_owner->owner()->lyxCode() == InsetBase::ERT_CODE;
1824 RowList::iterator Paragraph::getRow(pos_type pos)
1826 RowList::iterator rit = rows.end();
1827 RowList::iterator const begin = rows.begin();
1829 for (--rit; rit != begin && rit->pos() > pos; --rit)
1836 size_t Paragraph::row(pos_type pos) const
1838 RowList::const_iterator rit = rows.end();
1839 RowList::const_iterator const begin = rows.begin();
1841 for (--rit; rit != begin && rit->pos() > pos; --rit)
1848 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1850 if (!Encodings::is_arabic(c))
1851 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1852 return c + (0xb0 - '0');
1856 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1857 unsigned char next_char = ' ';
1859 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1860 unsigned char const par_char = getChar(i);
1861 if (!Encodings::IsComposeChar_arabic(par_char)) {
1862 next_char = par_char;
1867 if (Encodings::is_arabic(next_char)) {
1868 if (Encodings::is_arabic(prev_char) &&
1869 !Encodings::is_arabic_special(prev_char))
1870 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1872 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1874 if (Encodings::is_arabic(prev_char) &&
1875 !Encodings::is_arabic_special(prev_char))
1876 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1878 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);