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;
63 using std::ostringstream;
66 Paragraph::Paragraph()
67 : y(0), height(0), begin_of_body_(0),
68 pimpl_(new Paragraph::Pimpl(this))
75 Paragraph::Paragraph(Paragraph const & par)
76 : y(0), height(0), text_(par.text_), begin_of_body_(par.begin_of_body_),
77 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
80 // this is because of the dummy layout of the paragraphs that
82 layout_ = par.layout();
84 // copy everything behind the break-position to the new paragraph
85 insetlist = par.insetlist;
86 InsetList::iterator it = insetlist.begin();
87 InsetList::iterator end = insetlist.end();
88 for (; it != end; ++it) {
89 // currently we hold Inset*, not InsetBase*
90 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
95 void Paragraph::operator=(Paragraph const & par)
97 // needed as we will destroy the pimpl_ before copying it
101 lyxerr << "Paragraph::operator=()" << endl;
106 pimpl_ = new Pimpl(*par.pimpl_, this);
108 itemdepth = par.itemdepth;
109 // this is because of the dummy layout of the paragraphs that
111 layout_ = par.layout();
113 // copy everything behind the break-position to the new paragraph
114 insetlist = par.insetlist;
115 InsetList::iterator it = insetlist.begin();
116 InsetList::iterator end = insetlist.end();
117 for (; it != end; ++it) {
118 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
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) {
168 Change change = pimpl_->lookupChangeFull(i);
169 Changes::lyxMarkChange(os, column, curtime, running_change, change);
170 running_change = change;
172 // Write font changes
173 LyXFont font2 = getFontSettings(bparams, i);
174 if (font2 != font1) {
175 font2.lyxWriteChanges(font1, os);
180 value_type const c = getChar(i);
184 InsetOld const * inset = getInset(i);
186 if (inset->directWrite()) {
187 // international char, let it write
188 // code directly so it's shorter in
190 inset->write(buf, os);
192 os << "\n\\begin_inset ";
193 inset->write(buf, os);
194 os << "\n\\end_inset \n\n";
200 os << "\n\\backslash \n";
204 if (i + 1 < size() && getChar(i + 1) == ' ') {
211 if ((column > 70 && c == ' ')
216 // this check is to amend a bug. LyX sometimes
217 // inserts '\0' this could cause problems.
221 lyxerr << "ERROR (Paragraph::writeFile):"
222 " NULL char in structure." << endl;
228 // to make reading work properly
230 running_change = pimpl_->lookupChange(0);
231 Changes::lyxMarkChange(os, column, curtime,
232 Change(Change::UNCHANGED), running_change);
234 Changes::lyxMarkChange(os, column, curtime,
235 running_change, Change(Change::UNCHANGED));
237 os << "\n\\end_layout\n";
241 void Paragraph::validate(LaTeXFeatures & features) const
243 pimpl_->validate(features, *layout());
247 void Paragraph::eraseIntern(lyx::pos_type pos)
249 pimpl_->eraseIntern(pos);
253 bool Paragraph::erase(pos_type pos)
255 return pimpl_->erase(pos);
259 int Paragraph::erase(pos_type start, pos_type end)
261 return pimpl_->erase(start, end);
265 void Paragraph::insert(pos_type start, string const & str,
266 LyXFont const & font)
268 int size = str.size();
269 for (int i = 0 ; i < size ; ++i)
270 insertChar(start + i, str[i], font);
274 bool Paragraph::checkInsertChar(LyXFont &)
280 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
282 insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
286 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
287 LyXFont const & font, Change change)
289 pimpl_->insertChar(pos, c, font, change);
293 void Paragraph::insertInset(pos_type pos, InsetOld * inset)
295 insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
299 void Paragraph::insertInset(pos_type pos, InsetOld * inset,
300 LyXFont const & font, Change change)
302 pimpl_->insertInset(pos, inset, font, change);
306 bool Paragraph::insetAllowed(InsetOld_code code)
308 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
309 if (pimpl_->inset_owner)
310 return pimpl_->inset_owner->insetAllowed(code);
315 InsetOld * Paragraph::getInset(pos_type pos)
317 BOOST_ASSERT(pos < size());
318 return insetlist.get(pos);
322 InsetOld const * Paragraph::getInset(pos_type pos) const
324 BOOST_ASSERT(pos < size());
325 return insetlist.get(pos);
329 // Gets uninstantiated font setting at position.
330 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
333 BOOST_ASSERT(pos <= size());
335 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
336 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
337 for (; cit != end; ++cit)
338 if (cit->pos() >= pos)
344 if (pos == size() && !empty())
345 return getFontSettings(bparams, pos - 1);
347 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
351 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
353 BOOST_ASSERT(pos <= size());
355 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
356 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
357 for (; cit != end; ++cit)
358 if (cit->pos() >= pos)
361 // This should not happen, but if so, we take no chances.
362 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
368 // Gets uninstantiated font setting at position 0
369 LyXFont const Paragraph::getFirstFontSettings() const
371 if (!empty() && !pimpl_->fontlist.empty())
372 return pimpl_->fontlist[0].font();
374 return LyXFont(LyXFont::ALL_INHERIT);
378 // Gets the fully instantiated font at a given position in a paragraph
379 // This is basically the same function as LyXText::GetFont() in text2.C.
380 // The difference is that this one is used for generating the LaTeX file,
381 // and thus cosmetic "improvements" are disallowed: This has to deliver
382 // the true picture of the buffer. (Asger)
383 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
384 LyXFont const & outerfont) const
386 BOOST_ASSERT(pos >= 0);
388 LyXLayout_ptr const & lout = layout();
390 pos_type const body_pos = beginOfBody();
394 layoutfont = lout->labelfont;
396 layoutfont = lout->font;
398 LyXFont font = getFontSettings(bparams, pos);
399 font.realize(layoutfont);
400 font.realize(outerfont);
401 font.realize(bparams.getLyXTextClass().defaultfont());
407 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
408 LyXFont const & outerfont) const
410 LyXFont tmpfont = layout()->labelfont;
411 tmpfont.setLanguage(getParLanguage(bparams));
412 tmpfont.realize(outerfont);
413 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
418 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
419 LyXFont const & outerfont) const
421 LyXFont tmpfont = layout()->font;
422 tmpfont.setLanguage(getParLanguage(bparams));
423 tmpfont.realize(outerfont);
424 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
429 /// Returns the height of the highest font in range
431 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
432 LyXFont_size def_size) const
434 if (pimpl_->fontlist.empty())
437 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
438 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
439 for (; end_it != end; ++end_it) {
440 if (end_it->pos() >= endpos)
447 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
448 for (; cit != end; ++cit) {
449 if (cit->pos() >= startpos)
453 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
454 for (; cit != end_it; ++cit) {
455 LyXFont::FONT_SIZE size = cit->font().size();
456 if (size == LyXFont::INHERIT_SIZE)
458 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
465 Paragraph::value_type
466 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
468 value_type c = getChar(pos);
469 if (!lyxrc.rtl_support)
499 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
506 void Paragraph::setFont(pos_type pos, LyXFont const & font)
508 BOOST_ASSERT(pos <= size());
510 // First, reduce font against layout/label font
511 // Update: The setCharFont() routine in text2.C already
512 // reduces font, so we don't need to do that here. (Asger)
513 // No need to simplify this because it will disappear
514 // in a new kernel. (Asger)
515 // Next search font table
517 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
518 Pimpl::FontList::iterator it = beg;
519 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
520 for (; it != endit; ++it) {
521 if (it->pos() >= pos)
524 unsigned int i = std::distance(beg, it);
525 bool notfound = (it == endit);
527 if (!notfound && pimpl_->fontlist[i].font() == font)
530 bool begin = pos == 0 || notfound ||
531 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
532 // Is position pos is a beginning of a font block?
533 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
534 // Is position pos is the end of a font block?
535 if (begin && end) { // A single char block
536 if (i + 1 < pimpl_->fontlist.size() &&
537 pimpl_->fontlist[i + 1].font() == font) {
538 // Merge the singleton block with the next block
539 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
540 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
541 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
542 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
543 // Merge the singleton block with the previous block
544 pimpl_->fontlist[i - 1].pos(pos);
545 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
547 pimpl_->fontlist[i].font(font);
549 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
550 pimpl_->fontlist[i - 1].pos(pos);
552 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
553 Pimpl::FontTable(pos, font));
555 pimpl_->fontlist[i].pos(pos - 1);
556 if (!(i + 1 < pimpl_->fontlist.size() &&
557 pimpl_->fontlist[i + 1].font() == font))
558 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
559 Pimpl::FontTable(pos, font));
560 } else { // The general case. The block is splitted into 3 blocks
561 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
562 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
563 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
564 Pimpl::FontTable(pos, font));
569 void Paragraph::makeSameLayout(Paragraph const & par)
571 layout(par.layout());
573 params() = par.params();
577 int Paragraph::stripLeadingSpaces()
583 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
584 pimpl_->eraseIntern(0);
592 bool Paragraph::hasSameLayout(Paragraph const & par) const
594 return par.layout() == layout() && params().sameLayout(par.params());
598 Paragraph::depth_type Paragraph::getDepth() const
600 return params().depth();
604 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
606 if (layout()->isEnvironment())
607 return params().depth() + 1;
609 return params().depth();
613 char Paragraph::getAlign() const
615 return params().align();
619 string const & Paragraph::getLabelstring() const
621 return params().labelString();
625 // the next two functions are for the manual labels
626 string const Paragraph::getLabelWidthString() const
628 if (!params().labelWidthString().empty())
629 return params().labelWidthString();
631 return _("Senseless with this layout!");
635 void Paragraph::setLabelWidthString(string const & s)
637 params().labelWidthString(s);
641 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
644 params().labelWidthString(string());
645 params().align(LYX_ALIGN_LAYOUT);
646 params().spacing(Spacing(Spacing::Default));
650 int Paragraph::beginOfBody() const
652 return begin_of_body_;
656 void Paragraph::setBeginOfBody()
658 if (layout()->labeltype != LABEL_MANUAL) {
663 // Unroll the first two cycles of the loop
664 // and remember the previous character to
665 // remove unnecessary getChar() calls
667 pos_type end = size();
668 if (i < end && !isNewline(i)) {
670 char previous_char = 0;
673 previous_char = text_[i];
676 while (i < end && previous_char != ' ') {
681 previous_char = temp;
691 // returns -1 if inset not found
692 int Paragraph::getPositionOfInset(InsetOld const * inset) const
695 InsetList::const_iterator it = insetlist.begin();
696 InsetList::const_iterator end = insetlist.end();
697 for (; it != end; ++it)
698 if (it->inset == inset)
704 InsetBibitem * Paragraph::bibitem() const
706 if (!insetlist.empty()) {
707 InsetOld * inset = insetlist.begin()->inset;
708 if (inset->lyxCode() == InsetOld::BIBTEX_CODE)
709 return static_cast<InsetBibitem *>(inset);
717 // paragraphs inside floats need different alignment tags to avoid
720 bool noTrivlistCentering(UpdatableInset const * inset)
722 if (inset && inset->owner()) {
723 InsetOld::Code const code = inset->owner()->lyxCode();
724 return code == InsetOld::FLOAT_CODE ||
725 code == InsetOld::WRAP_CODE;
731 string correction(string const & orig)
733 if (orig == "flushleft")
734 return "raggedright";
735 if (orig == "flushright")
737 if (orig == "center")
743 string const corrected_env(string const & suffix, string const & env,
744 UpdatableInset const * inset)
746 string output = suffix + "{";
747 if (noTrivlistCentering(inset))
748 output += correction(env);
757 // This could go to ParagraphParameters if we want to
758 int Paragraph::startTeXParParams(BufferParams const & bparams,
759 ostream & os, bool moving_arg) const
763 if (params().noindent()) {
768 switch (params().align()) {
770 case LYX_ALIGN_BLOCK:
771 case LYX_ALIGN_LAYOUT:
772 case LYX_ALIGN_SPECIAL:
775 case LYX_ALIGN_RIGHT:
776 case LYX_ALIGN_CENTER:
784 switch (params().align()) {
786 case LYX_ALIGN_BLOCK:
787 case LYX_ALIGN_LAYOUT:
788 case LYX_ALIGN_SPECIAL:
790 case LYX_ALIGN_LEFT: {
792 UpdatableInset const * const inset = pimpl_->inset_owner;
793 if (getParLanguage(bparams)->babel() != "hebrew")
794 output = corrected_env("\\begin", "flushleft", inset);
796 output = corrected_env("\\begin", "flushright", inset);
798 column += output.size();
800 } case LYX_ALIGN_RIGHT: {
802 UpdatableInset const * const inset = pimpl_->inset_owner;
803 if (getParLanguage(bparams)->babel() != "hebrew")
804 output = corrected_env("\\begin", "flushright", inset);
806 output = corrected_env("\\begin", "flushleft", inset);
808 column += output.size();
810 } case LYX_ALIGN_CENTER: {
812 output = corrected_env("\\begin", "center", pimpl_->inset_owner);
814 column += output.size();
823 // This could go to ParagraphParameters if we want to
824 int Paragraph::endTeXParParams(BufferParams const & bparams,
825 ostream & os, bool moving_arg) const
829 switch (params().align()) {
831 case LYX_ALIGN_BLOCK:
832 case LYX_ALIGN_LAYOUT:
833 case LYX_ALIGN_SPECIAL:
836 case LYX_ALIGN_RIGHT:
837 case LYX_ALIGN_CENTER:
845 switch (params().align()) {
847 case LYX_ALIGN_BLOCK:
848 case LYX_ALIGN_LAYOUT:
849 case LYX_ALIGN_SPECIAL:
851 case LYX_ALIGN_LEFT: {
853 UpdatableInset const * const inset = pimpl_->inset_owner;
854 if (getParLanguage(bparams)->babel() != "hebrew")
855 output = corrected_env("\\par\\end", "flushleft", inset);
857 output = corrected_env("\\par\\end", "flushright", inset);
859 column += output.size();
861 } case LYX_ALIGN_RIGHT: {
863 UpdatableInset const * const inset = pimpl_->inset_owner;
864 if (getParLanguage(bparams)->babel() != "hebrew")
865 output = corrected_env("\\par\\end", "flushright", inset);
867 output = corrected_env("\\par\\end", "flushleft", inset);
869 column += output.size();
871 } case LYX_ALIGN_CENTER: {
873 output = corrected_env("\\par\\end", "center", pimpl_->inset_owner);
875 column += output.size();
884 // This one spits out the text of the paragraph
885 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
886 BufferParams const & bparams,
887 LyXFont const & outerfont,
888 ostream & os, TexRow & texrow,
889 OutputParams const & runparams)
891 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
893 bool return_value = false;
897 // well we have to check if we are in an inset with unlimited
898 // length (all in one row) if that is true then we don't allow
899 // any special options in the paragraph and also we don't allow
900 // any environment other then "Standard" to be valid!
902 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
905 style = bparams.getLyXTextClass().defaultLayout();
912 // Maybe we have to create a optional argument.
913 pos_type body_pos = beginOfBody();
914 unsigned int column = 0;
919 basefont = getLabelFont(bparams, outerfont);
921 basefont = getLayoutFont(bparams, outerfont);
924 bool moving_arg = runparams.moving_arg;
925 moving_arg |= style->needprotect;
927 // Which font is currently active?
928 LyXFont running_font(basefont);
929 // Do we have an open font change?
930 bool open_font = false;
932 Change::Type running_change = Change::UNCHANGED;
934 texrow.start(id(), 0);
936 // if the paragraph is empty, the loop will not be entered at all
938 if (style->isCommand()) {
943 column += startTeXParParams(bparams, os, moving_arg);
946 for (pos_type i = 0; i < size(); ++i) {
948 // First char in paragraph or after label?
952 column += running_font.latexWriteEndChanges(os, basefont, basefont);
955 basefont = getLayoutFont(bparams, outerfont);
956 running_font = basefont;
960 if (style->isCommand()) {
966 column += startTeXParParams(bparams, os,
970 value_type c = getChar(i);
972 // Fully instantiated font
973 LyXFont font = getFont(bparams, i, outerfont);
975 LyXFont const last_font = running_font;
977 // Spaces at end of font change are simulated to be
978 // outside font change, i.e. we write "\textXX{text} "
979 // rather than "\textXX{text }". (Asger)
980 if (open_font && c == ' ' && i <= size() - 2) {
981 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
982 if (next_font != running_font && next_font != font) {
987 // We end font definition before blanks
989 (font != running_font ||
990 font.language() != running_font.language()))
992 column += running_font.latexWriteEndChanges(os,
994 (i == body_pos-1) ? basefont : font);
995 running_font = basefont;
999 // Blanks are printed before start of fontswitch
1001 // Do not print the separation of the optional argument
1002 if (i != body_pos - 1) {
1003 pimpl_->simpleTeXBlanks(os, texrow, i,
1004 column, font, *style);
1008 // Do we need to change font?
1009 if ((font != running_font ||
1010 font.language() != running_font.language()) &&
1013 column += font.latexWriteStartChanges(os, basefont,
1015 running_font = font;
1019 Change::Type change = pimpl_->lookupChange(i);
1021 column += Changes::latexMarkChange(os, running_change, change);
1022 running_change = change;
1024 OutputParams rp = runparams;
1025 rp.moving_arg = moving_arg;
1026 rp.free_spacing = style->free_spacing;
1027 pimpl_->simpleTeXSpecialChars(buf, bparams,
1028 os, texrow, runparams,
1030 basefont, outerfont, open_font,
1032 *style, i, column, c);
1035 column += Changes::latexMarkChange(os,
1036 running_change, Change::UNCHANGED);
1038 // If we have an open font definition, we have to close it
1040 #ifdef FIXED_LANGUAGE_END_DETECTION
1043 .latexWriteEndChanges(os, basefont,
1044 next_->getFont(bparams,
1047 running_font.latexWriteEndChanges(os, basefont,
1051 #ifdef WITH_WARNINGS
1052 //#warning For now we ALWAYS have to close the foreign font settings if they are
1053 //#warning there as we start another \selectlanguage with the next paragraph if
1054 //#warning we are in need of this. This should be fixed sometime (Jug)
1056 running_font.latexWriteEndChanges(os, basefont, basefont);
1060 // Needed if there is an optional argument but no contents.
1061 if (body_pos > 0 && body_pos == size()) {
1063 return_value = false;
1067 column += endTeXParParams(bparams, os, moving_arg);
1070 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1071 return return_value;
1077 // checks, if newcol chars should be put into this line
1078 // writes newline, if necessary.
1079 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1080 string::size_type newcol)
1083 if (colcount > lyxrc.ascii_linelen) {
1085 colcount = newcol; // assume write after this call
1100 string tag_name(PAR_TAG const & pt) {
1102 case NONE: return "!-- --";
1103 case TT: return "tt";
1104 case SF: return "sf";
1105 case BF: return "bf";
1106 case IT: return "it";
1107 case SL: return "sl";
1108 case EM: return "em";
1115 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1117 p1 = static_cast<PAR_TAG>(p1 | p2);
1122 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1124 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1130 // Handle internal paragraph parsing -- layout already processed.
1131 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1133 LyXFont const & outerfont,
1134 OutputParams const & runparams,
1135 lyx::depth_type /*depth*/) const
1137 LyXLayout_ptr const & style = layout();
1139 string::size_type char_line_count = 5; // Heuristic choice ;-)
1141 // gets paragraph main font
1144 if (style->labeltype == LABEL_MANUAL) {
1145 font_old = style->labelfont;
1148 font_old = style->font;
1152 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1153 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1154 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1157 stack<PAR_TAG> tag_state;
1158 // parsing main loop
1159 for (pos_type i = 0; i < size(); ++i) {
1161 PAR_TAG tag_close = NONE;
1162 list < PAR_TAG > tag_open;
1164 LyXFont const font = getFont(buf.params(), i, outerfont);
1166 if (font_old.family() != font.family()) {
1167 switch (family_type) {
1168 case LyXFont::SANS_FAMILY:
1171 case LyXFont::TYPEWRITER_FAMILY:
1178 family_type = font.family();
1180 switch (family_type) {
1181 case LyXFont::SANS_FAMILY:
1182 tag_open.push_back(SF);
1184 case LyXFont::TYPEWRITER_FAMILY:
1185 tag_open.push_back(TT);
1192 if (font_old.series() != font.series()) {
1193 switch (series_type) {
1194 case LyXFont::BOLD_SERIES:
1201 series_type = font.series();
1203 switch (series_type) {
1204 case LyXFont::BOLD_SERIES:
1205 tag_open.push_back(BF);
1213 if (font_old.shape() != font.shape()) {
1214 switch (shape_type) {
1215 case LyXFont::ITALIC_SHAPE:
1218 case LyXFont::SLANTED_SHAPE:
1225 shape_type = font.shape();
1227 switch (shape_type) {
1228 case LyXFont::ITALIC_SHAPE:
1229 tag_open.push_back(IT);
1231 case LyXFont::SLANTED_SHAPE:
1232 tag_open.push_back(SL);
1239 if (font_old.emph() != font.emph()) {
1240 if (font.emph() == LyXFont::ON) {
1241 tag_open.push_back(EM);
1250 list < PAR_TAG > temp;
1251 while (!tag_state.empty() && tag_close) {
1252 PAR_TAG k = tag_state.top();
1254 os << "</" << tag_name(k) << '>';
1261 for(list< PAR_TAG >::const_iterator j = temp.begin();
1262 j != temp.end(); ++j) {
1264 os << '<' << tag_name(*j) << '>';
1267 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1268 j != tag_open.end(); ++j) {
1270 os << '<' << tag_name(*j) << '>';
1273 char c = getChar(i);
1276 if (c == Paragraph::META_INSET) {
1277 getInset(i)->linuxdoc(buf, os, runparams);
1282 if (style->latexparam() == "CDATA") {
1283 // "TeX"-Mode on == > SGML-Mode on.
1290 boost::tie(ws, str) = sgml::escapeChar(c);
1291 if (ws && !isFreeSpacing()) {
1292 // in freespacing mode, spaces are
1293 // non-breaking characters
1294 if (desc_on) { // if char is ' ' then...
1296 sgmlLineBreak(os, char_line_count, 6);
1300 sgmlLineBreak(os, char_line_count, 1);
1305 char_line_count += str.length();
1311 while (!tag_state.empty()) {
1312 os << "</" << tag_name(tag_state.top()) << '>';
1316 // resets description flag correctly
1318 // <tag> not closed...
1319 sgmlLineBreak(os, char_line_count, 6);
1325 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1327 LyXFont const & outerfont,
1328 OutputParams const & runparams,
1329 lyx::depth_type depth,
1332 bool emph_flag = false;
1334 LyXLayout_ptr const & style = layout();
1335 LyXLayout_ptr const & defaultstyle =
1336 buf.params().getLyXTextClass().defaultLayout();
1339 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1341 int char_line_count = depth;
1342 bool label_closed = true;
1343 bool para_closed = true;
1345 if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1347 Counters & counters = buf.params().getLyXTextClass().counters();
1348 if (!style->free_spacing)
1349 os << string(depth,' ');
1350 if (!style->labeltag().empty()) {
1351 os << "<" << style->labeltag() << ">\n";
1352 label_closed = false;
1354 if (!defaultstyle->latexparam().empty()) {
1355 counters.step("para");
1356 ls = tostr(counters.value("para"));
1358 + subst(defaultstyle->latexparam(), "#", ls) + '"';
1360 os << "<" << style->itemtag() << ">\n"
1361 << string(depth, ' ') << "<"
1362 << defaultstyle->latexname() << ls << ">\n";
1363 para_closed = false;
1367 // parsing main loop
1368 for (pos_type i = 0; i < size(); ++i) {
1369 LyXFont font = getFont(buf.params(), i, outerfont);
1371 // handle <emphasis> tag
1372 if (font_old.emph() != font.emph()) {
1373 if (font.emph() == LyXFont::ON) {
1374 if (style->latexparam() == "CDATA")
1377 if (style->latexparam() == "CDATA")
1381 if (style->latexparam() == "CDATA")
1383 os << "</emphasis>";
1384 if (style->latexparam() == "CDATA")
1392 InsetOld const * inset = getInset(i);
1393 // don't print the inset in position 0 if desc_on == 3 (label)
1394 //if (i || desc_on != 3) {
1396 if (style->latexparam() == "CDATA")
1398 inset->docbook(buf, os, runparams);
1399 if (style->latexparam() == "CDATA")
1403 char c = getChar(i);
1406 boost::tie(ws, str) = sgml::escapeChar(c);
1408 if (style->pass_thru) {
1410 } else if (isFreeSpacing() || c != ' ') {
1412 } else if (!style->labeltag().empty() && !label_closed) {
1414 os << "\n</" << style->labeltag() << "><"
1415 << style->itemtag() << "><"
1416 << defaultstyle->latexname() << ">";
1417 label_closed = true;
1418 para_closed = false;
1427 if (style->latexparam() == "CDATA")
1429 os << "</emphasis>";
1430 if (style->latexparam() == "CDATA")
1434 // resets description flag correctly
1435 if (!label_closed) {
1436 // <term> not closed...
1437 os << "</" << style->labeltag() << ">\n<"
1438 << style->itemtag() << "><"
1439 << defaultstyle->latexname() << "> ";
1442 os << "\n" << string(depth, ' ') << "</"
1443 << defaultstyle->latexname() << ">\n";
1445 if (style->free_spacing)
1452 /// return true if the char is a meta-character for an inset
1454 bool IsInsetChar(char c)
1456 return (c == Paragraph::META_INSET);
1463 bool Paragraph::isHfill(pos_type pos) const
1465 return IsInsetChar(getChar(pos))
1466 && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
1470 bool Paragraph::isInset(pos_type pos) const
1472 return IsInsetChar(getChar(pos));
1476 bool Paragraph::isNewline(pos_type pos) const
1478 return IsInsetChar(getChar(pos))
1479 && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
1483 bool Paragraph::isSeparator(pos_type pos) const
1485 return IsSeparatorChar(getChar(pos));
1489 bool Paragraph::isLineSeparator(pos_type pos) const
1491 value_type const c = getChar(pos);
1492 return IsLineSeparatorChar(c)
1493 || (IsInsetChar(c) && getInset(pos) &&
1494 getInset(pos)->isLineSeparator());
1498 bool Paragraph::isKomma(pos_type pos) const
1500 return IsKommaChar(getChar(pos));
1504 /// Used by the spellchecker
1505 bool Paragraph::isLetter(pos_type pos) const
1507 value_type const c = getChar(pos);
1508 if (IsLetterChar(c))
1511 return getInset(pos)->isLetter();
1512 // We want to pass the ' and escape chars to ispell
1513 string const extra = lyxrc.isp_esc_chars + '\'';
1514 return contains(extra, c);
1518 bool Paragraph::isWord(pos_type pos) const
1520 unsigned char const c = getChar(pos);
1521 return !(IsSeparatorChar(c)
1528 Paragraph::getParLanguage(BufferParams const & bparams) const
1531 return getFirstFontSettings().language();
1532 #warning FIXME we should check the prev par as well (Lgb)
1533 return bparams.language;
1537 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1539 return lyxrc.rtl_support
1540 && getParLanguage(bparams)->RightToLeft()
1541 && !(inInset() && inInset()->owner() &&
1542 inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
1546 void Paragraph::changeLanguage(BufferParams const & bparams,
1547 Language const * from, Language const * to)
1549 for (pos_type i = 0; i < size(); ++i) {
1550 LyXFont font = getFontSettings(bparams, i);
1551 if (font.language() == from) {
1552 font.setLanguage(to);
1559 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1561 Language const * doc_language = bparams.language;
1562 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1563 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1565 for (; cit != end; ++cit)
1566 if (cit->font().language() != ignore_language &&
1567 cit->font().language() != latex_language &&
1568 cit->font().language() != doc_language)
1574 // Convert the paragraph to a string.
1575 // Used for building the table of contents
1576 string const Paragraph::asString(Buffer const & buffer,
1579 OutputParams runparams;
1580 return asString(buffer, runparams, label);
1584 string const Paragraph::asString(Buffer const & buffer,
1585 OutputParams const & runparams,
1590 if (label && !params().labelString().empty())
1591 s += params().labelString() + ' ';
1593 for (pos_type i = 0; i < size(); ++i) {
1594 value_type c = getChar(i);
1597 else if (c == META_INSET &&
1598 getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
1600 getInset(i)->plaintext(buffer, os, runparams);
1601 s += subst(STRCONV(os.str()),'\n',' ');
1607 // This should really be done by the caller and not here.
1608 string ret = asString(buffer, runparams, 0, size(), label);
1609 return subst(ret, '\n', ' ');
1614 string const Paragraph::asString(Buffer const & buffer,
1615 pos_type beg, pos_type end, bool label) const
1618 OutputParams const runparams;
1619 return asString(buffer, runparams, beg, end, label);
1623 string const Paragraph::asString(Buffer const & buffer,
1624 OutputParams const & runparams,
1625 pos_type beg, pos_type end, bool label) const
1629 if (beg == 0 && label && !params().labelString().empty())
1630 os << params().labelString() << ' ';
1632 for (pos_type i = beg; i < end; ++i) {
1633 value_type const c = getUChar(buffer.params(), i);
1636 else if (c == META_INSET)
1637 getInset(i)->plaintext(buffer, os, runparams);
1644 void Paragraph::setInsetOwner(UpdatableInset * inset)
1646 pimpl_->inset_owner = inset;
1647 InsetList::iterator it = insetlist.begin();
1648 InsetList::iterator end = insetlist.end();
1649 for (; it != end; ++it)
1651 it->inset->setOwner(inset);
1655 void Paragraph::setContentsFromPar(Paragraph const & par)
1657 pimpl_->setContentsFromPar(par);
1661 void Paragraph::trackChanges(Change::Type type)
1663 pimpl_->trackChanges(type);
1667 void Paragraph::untrackChanges()
1669 pimpl_->untrackChanges();
1673 void Paragraph::cleanChanges()
1675 pimpl_->cleanChanges();
1679 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1681 BOOST_ASSERT(!size() || pos < size());
1682 return pimpl_->lookupChange(pos);
1686 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1688 BOOST_ASSERT(!size() || pos < size());
1689 return pimpl_->lookupChangeFull(pos);
1693 bool Paragraph::isChanged(pos_type start, pos_type end) const
1695 return pimpl_->isChanged(start, end);
1699 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1701 return pimpl_->isChangeEdited(start, end);
1705 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1707 pimpl_->setChange(pos, type);
1712 void Paragraph::markErased()
1714 pimpl_->markErased();
1718 void Paragraph::acceptChange(pos_type start, pos_type end)
1720 return pimpl_->acceptChange(start, end);
1724 void Paragraph::rejectChange(pos_type start, pos_type end)
1726 return pimpl_->rejectChange(start, end);
1730 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1732 // This is in the critical path!
1733 pos_type const siz = text_.size();
1735 BOOST_ASSERT(0 <= pos);
1736 BOOST_ASSERT(pos <= siz);
1739 lyxerr << "getChar() on pos " << pos << " in par id "
1740 << id() << " of size " << siz
1741 << " is a bit silly !" << endl;
1742 BOOST_ASSERT(false);
1750 int Paragraph::id() const
1756 LyXLayout_ptr const & Paragraph::layout() const
1759 InsetOld * inset = inInset();
1760 if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
1761 return static_cast<InsetEnvironment*>(inset)->layout();
1767 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1769 layout_ = new_layout;
1773 UpdatableInset * Paragraph::inInset() const
1775 return pimpl_->inset_owner;
1779 void Paragraph::clearContents()
1785 void Paragraph::setChar(pos_type pos, value_type c)
1791 ParagraphParameters & Paragraph::params()
1793 return pimpl_->params;
1797 ParagraphParameters const & Paragraph::params() const
1799 return pimpl_->params;
1803 bool Paragraph::isFreeSpacing() const
1805 if (layout()->free_spacing)
1808 // for now we just need this, later should we need this in some
1809 // other way we can always add a function to InsetOld too.
1810 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1811 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1816 bool Paragraph::allowEmpty() const
1818 if (layout()->keepempty)
1820 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1821 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1826 RowList::iterator Paragraph::getRow(pos_type pos)
1828 RowList::iterator rit = rows.end();
1829 RowList::iterator const begin = rows.begin();
1831 for (--rit; rit != begin && rit->pos() > pos; --rit)
1838 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1840 if (!Encodings::is_arabic(c))
1841 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1842 return c + (0xb0 - '0');
1846 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1847 unsigned char next_char = ' ';
1849 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1850 unsigned char const par_char = getChar(i);
1851 if (!Encodings::IsComposeChar_arabic(par_char)) {
1852 next_char = par_char;
1857 if (Encodings::is_arabic(next_char)) {
1858 if (Encodings::is_arabic(prev_char) &&
1859 !Encodings::is_arabic_special(prev_char))
1860 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1862 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1864 if (Encodings::is_arabic(prev_char) &&
1865 !Encodings::is_arabic_special(prev_char))
1866 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1868 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);