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 & font)
276 if (pimpl_->inset_owner)
277 return pimpl_->inset_owner->checkInsertChar(font);
282 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
284 insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
288 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
289 LyXFont const & font, Change change)
291 pimpl_->insertChar(pos, c, font, change);
295 void Paragraph::insertInset(pos_type pos, InsetOld * inset)
297 insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
301 void Paragraph::insertInset(pos_type pos, InsetOld * inset,
302 LyXFont const & font, Change change)
304 pimpl_->insertInset(pos, inset, font, change);
308 bool Paragraph::insetAllowed(InsetOld_code code)
310 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
311 if (pimpl_->inset_owner)
312 return pimpl_->inset_owner->insetAllowed(code);
317 InsetOld * Paragraph::getInset(pos_type pos)
319 BOOST_ASSERT(pos < size());
320 return insetlist.get(pos);
324 InsetOld const * Paragraph::getInset(pos_type pos) const
326 BOOST_ASSERT(pos < size());
327 return insetlist.get(pos);
331 // Gets uninstantiated font setting at position.
332 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
335 BOOST_ASSERT(pos <= size());
337 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
338 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
339 for (; cit != end; ++cit)
340 if (cit->pos() >= pos)
346 if (pos == size() && !empty())
347 return getFontSettings(bparams, pos - 1);
349 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
353 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
355 BOOST_ASSERT(pos <= size());
357 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
358 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
359 for (; cit != end; ++cit)
360 if (cit->pos() >= pos)
363 // This should not happen, but if so, we take no chances.
364 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
370 // Gets uninstantiated font setting at position 0
371 LyXFont const Paragraph::getFirstFontSettings() const
373 if (!empty() && !pimpl_->fontlist.empty())
374 return pimpl_->fontlist[0].font();
376 return LyXFont(LyXFont::ALL_INHERIT);
380 // Gets the fully instantiated font at a given position in a paragraph
381 // This is basically the same function as LyXText::GetFont() in text2.C.
382 // The difference is that this one is used for generating the LaTeX file,
383 // and thus cosmetic "improvements" are disallowed: This has to deliver
384 // the true picture of the buffer. (Asger)
385 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
386 LyXFont const & outerfont) const
388 BOOST_ASSERT(pos >= 0);
390 LyXLayout_ptr const & lout = layout();
392 pos_type const body_pos = beginOfBody();
396 layoutfont = lout->labelfont;
398 layoutfont = lout->font;
400 LyXFont font = getFontSettings(bparams, pos);
401 font.realize(layoutfont);
402 font.realize(outerfont);
403 font.realize(bparams.getLyXTextClass().defaultfont());
409 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
410 LyXFont const & outerfont) const
412 LyXFont tmpfont = layout()->labelfont;
413 tmpfont.setLanguage(getParLanguage(bparams));
414 tmpfont.realize(outerfont);
415 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
420 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
421 LyXFont const & outerfont) const
423 LyXFont tmpfont = layout()->font;
424 tmpfont.setLanguage(getParLanguage(bparams));
425 tmpfont.realize(outerfont);
426 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
431 /// Returns the height of the highest font in range
433 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
434 LyXFont_size def_size) const
436 if (pimpl_->fontlist.empty())
439 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
440 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
441 for (; end_it != end; ++end_it) {
442 if (end_it->pos() >= endpos)
449 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
450 for (; cit != end; ++cit) {
451 if (cit->pos() >= startpos)
455 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
456 for (; cit != end_it; ++cit) {
457 LyXFont::FONT_SIZE size = cit->font().size();
458 if (size == LyXFont::INHERIT_SIZE)
460 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
467 Paragraph::value_type
468 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
470 value_type c = getChar(pos);
471 if (!lyxrc.rtl_support)
501 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
508 void Paragraph::setFont(pos_type pos, LyXFont const & font)
510 BOOST_ASSERT(pos <= size());
512 // First, reduce font against layout/label font
513 // Update: The setCharFont() routine in text2.C already
514 // reduces font, so we don't need to do that here. (Asger)
515 // No need to simplify this because it will disappear
516 // in a new kernel. (Asger)
517 // Next search font table
519 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
520 Pimpl::FontList::iterator it = beg;
521 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
522 for (; it != endit; ++it) {
523 if (it->pos() >= pos)
526 unsigned int i = std::distance(beg, it);
527 bool notfound = (it == endit);
529 if (!notfound && pimpl_->fontlist[i].font() == font)
532 bool begin = pos == 0 || notfound ||
533 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
534 // Is position pos is a beginning of a font block?
535 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
536 // Is position pos is the end of a font block?
537 if (begin && end) { // A single char block
538 if (i + 1 < pimpl_->fontlist.size() &&
539 pimpl_->fontlist[i + 1].font() == font) {
540 // Merge the singleton block with the next block
541 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
542 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
543 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
544 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
545 // Merge the singleton block with the previous block
546 pimpl_->fontlist[i - 1].pos(pos);
547 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
549 pimpl_->fontlist[i].font(font);
551 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
552 pimpl_->fontlist[i - 1].pos(pos);
554 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
555 Pimpl::FontTable(pos, font));
557 pimpl_->fontlist[i].pos(pos - 1);
558 if (!(i + 1 < pimpl_->fontlist.size() &&
559 pimpl_->fontlist[i + 1].font() == font))
560 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
561 Pimpl::FontTable(pos, font));
562 } else { // The general case. The block is splitted into 3 blocks
563 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
564 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
565 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
566 Pimpl::FontTable(pos, font));
571 void Paragraph::makeSameLayout(Paragraph const & par)
573 layout(par.layout());
575 params() = par.params();
579 int Paragraph::stripLeadingSpaces()
585 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
586 pimpl_->eraseIntern(0);
594 bool Paragraph::hasSameLayout(Paragraph const & par) const
597 par.layout() == layout() &&
598 params().sameLayout(par.params());
602 Paragraph::depth_type Paragraph::getDepth() const
604 return params().depth();
608 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
610 if (layout()->isEnvironment())
611 return params().depth() + 1;
613 return params().depth();
617 char Paragraph::getAlign() const
619 return params().align();
623 string const & Paragraph::getLabelstring() const
625 return params().labelString();
629 // the next two functions are for the manual labels
630 string const Paragraph::getLabelWidthString() const
632 if (!params().labelWidthString().empty())
633 return params().labelWidthString();
635 return _("Senseless with this layout!");
639 void Paragraph::setLabelWidthString(string const & s)
641 params().labelWidthString(s);
645 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
648 params().labelWidthString(string());
649 params().align(LYX_ALIGN_LAYOUT);
650 params().spacing(Spacing(Spacing::Default));
654 int Paragraph::beginOfBody() const
656 return begin_of_body_;
660 void Paragraph::setBeginOfBody()
662 if (layout()->labeltype != LABEL_MANUAL) {
667 // Unroll the first two cycles of the loop
668 // and remember the previous character to
669 // remove unnecessary getChar() calls
671 pos_type end = size();
672 if (i < end && !isNewline(i)) {
674 char previous_char = 0;
677 previous_char = text_[i];
680 while (i < end && previous_char != ' ') {
685 previous_char = temp;
695 // returns -1 if inset not found
696 int Paragraph::getPositionOfInset(InsetOld const * inset) const
699 InsetList::const_iterator it = insetlist.begin();
700 InsetList::const_iterator end = insetlist.end();
701 for (; it != end; ++it)
702 if (it->inset == inset)
708 InsetBibitem * Paragraph::bibitem() const
710 if (!insetlist.empty()) {
711 InsetOld * inset = insetlist.begin()->inset;
712 if (inset->lyxCode() == InsetOld::BIBTEX_CODE)
713 return static_cast<InsetBibitem *>(inset);
721 /* paragraphs inside floats need different alignment tags to avoid
724 bool noTrivlistCentering(UpdatableInset const * inset)
726 if (inset && inset->owner()) {
727 InsetOld::Code const code = inset->owner()->lyxCode();
728 return code == InsetOld::FLOAT_CODE ||
729 code == InsetOld::WRAP_CODE;
735 string correction(string const & orig)
737 if (orig == "flushleft")
738 return "raggedright";
739 if (orig == "flushright")
741 if (orig == "center")
747 string const corrected_env(string const & suffix, string const & env,
748 UpdatableInset const * inset)
750 string output = suffix + "{";
751 if (noTrivlistCentering(inset))
752 output += correction(env);
761 // This could go to ParagraphParameters if we want to
762 int Paragraph::startTeXParParams(BufferParams const & bparams,
763 ostream & os, bool moving_arg) const
767 if (params().noindent()) {
772 switch (params().align()) {
774 case LYX_ALIGN_BLOCK:
775 case LYX_ALIGN_LAYOUT:
776 case LYX_ALIGN_SPECIAL:
779 case LYX_ALIGN_RIGHT:
780 case LYX_ALIGN_CENTER:
788 switch (params().align()) {
790 case LYX_ALIGN_BLOCK:
791 case LYX_ALIGN_LAYOUT:
792 case LYX_ALIGN_SPECIAL:
794 case LYX_ALIGN_LEFT: {
796 UpdatableInset const * const inset = pimpl_->inset_owner;
797 if (getParLanguage(bparams)->babel() != "hebrew")
798 output = corrected_env("\\begin", "flushleft", inset);
800 output = corrected_env("\\begin", "flushright", inset);
802 column += output.size();
804 } case LYX_ALIGN_RIGHT: {
806 UpdatableInset const * const inset = pimpl_->inset_owner;
807 if (getParLanguage(bparams)->babel() != "hebrew")
808 output = corrected_env("\\begin", "flushright", inset);
810 output = corrected_env("\\begin", "flushleft", inset);
812 column += output.size();
814 } case LYX_ALIGN_CENTER: {
816 output = corrected_env("\\begin", "center", pimpl_->inset_owner);
818 column += output.size();
827 // This could go to ParagraphParameters if we want to
828 int Paragraph::endTeXParParams(BufferParams const & bparams,
829 ostream & os, bool moving_arg) const
833 switch (params().align()) {
835 case LYX_ALIGN_BLOCK:
836 case LYX_ALIGN_LAYOUT:
837 case LYX_ALIGN_SPECIAL:
840 case LYX_ALIGN_RIGHT:
841 case LYX_ALIGN_CENTER:
849 switch (params().align()) {
851 case LYX_ALIGN_BLOCK:
852 case LYX_ALIGN_LAYOUT:
853 case LYX_ALIGN_SPECIAL:
855 case LYX_ALIGN_LEFT: {
857 UpdatableInset const * const inset = pimpl_->inset_owner;
858 if (getParLanguage(bparams)->babel() != "hebrew")
859 output = corrected_env("\\par\\end", "flushleft", inset);
861 output = corrected_env("\\par\\end", "flushright", inset);
863 column += output.size();
865 } case LYX_ALIGN_RIGHT: {
867 UpdatableInset const * const inset = pimpl_->inset_owner;
868 if (getParLanguage(bparams)->babel() != "hebrew")
869 output = corrected_env("\\par\\end", "flushright", inset);
871 output = corrected_env("\\par\\end", "flushleft", inset);
873 column += output.size();
875 } case LYX_ALIGN_CENTER: {
877 output = corrected_env("\\par\\end", "center", pimpl_->inset_owner);
879 column += output.size();
888 // This one spits out the text of the paragraph
889 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
890 BufferParams const & bparams,
891 LyXFont const & outerfont,
892 ostream & os, TexRow & texrow,
893 OutputParams const & runparams)
895 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
897 bool return_value = false;
901 // well we have to check if we are in an inset with unlimited
902 // length (all in one row) if that is true then we don't allow
903 // any special options in the paragraph and also we don't allow
904 // any environment other then "Standard" to be valid!
906 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
909 style = bparams.getLyXTextClass().defaultLayout();
916 // Maybe we have to create a optional argument.
917 pos_type body_pos = beginOfBody();
918 unsigned int column = 0;
923 basefont = getLabelFont(bparams, outerfont);
925 basefont = getLayoutFont(bparams, outerfont);
928 bool moving_arg = runparams.moving_arg;
929 moving_arg |= style->needprotect;
931 // Which font is currently active?
932 LyXFont running_font(basefont);
933 // Do we have an open font change?
934 bool open_font = false;
936 Change::Type running_change = Change::UNCHANGED;
938 texrow.start(id(), 0);
940 // if the paragraph is empty, the loop will not be entered at all
942 if (style->isCommand()) {
947 column += startTeXParParams(bparams, os, moving_arg);
950 for (pos_type i = 0; i < size(); ++i) {
952 // First char in paragraph or after label?
956 column += running_font.latexWriteEndChanges(os, basefont, basefont);
959 basefont = getLayoutFont(bparams, outerfont);
960 running_font = basefont;
964 if (style->isCommand()) {
970 column += startTeXParParams(bparams, os,
974 value_type c = getChar(i);
976 // Fully instantiated font
977 LyXFont font = getFont(bparams, i, outerfont);
979 LyXFont const last_font = running_font;
981 // Spaces at end of font change are simulated to be
982 // outside font change, i.e. we write "\textXX{text} "
983 // rather than "\textXX{text }". (Asger)
984 if (open_font && c == ' ' && i <= size() - 2) {
985 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
986 if (next_font != running_font && next_font != font) {
991 // We end font definition before blanks
993 (font != running_font ||
994 font.language() != running_font.language()))
996 column += running_font.latexWriteEndChanges(os,
998 (i == body_pos-1) ? basefont : font);
999 running_font = basefont;
1003 // Blanks are printed before start of fontswitch
1005 // Do not print the separation of the optional argument
1006 if (i != body_pos - 1) {
1007 pimpl_->simpleTeXBlanks(os, texrow, i,
1008 column, font, *style);
1012 // Do we need to change font?
1013 if ((font != running_font ||
1014 font.language() != running_font.language()) &&
1017 column += font.latexWriteStartChanges(os, basefont,
1019 running_font = font;
1023 Change::Type change = pimpl_->lookupChange(i);
1025 column += Changes::latexMarkChange(os, running_change, change);
1026 running_change = change;
1028 OutputParams rp = runparams;
1029 rp.moving_arg = moving_arg;
1030 rp.free_spacing = style->free_spacing;
1031 pimpl_->simpleTeXSpecialChars(buf, bparams,
1032 os, texrow, runparams,
1034 basefont, outerfont, open_font,
1036 *style, i, column, c);
1039 column += Changes::latexMarkChange(os,
1040 running_change, Change::UNCHANGED);
1042 // If we have an open font definition, we have to close it
1044 #ifdef FIXED_LANGUAGE_END_DETECTION
1047 .latexWriteEndChanges(os, basefont,
1048 next_->getFont(bparams,
1051 running_font.latexWriteEndChanges(os, basefont,
1055 #ifdef WITH_WARNINGS
1056 //#warning For now we ALWAYS have to close the foreign font settings if they are
1057 //#warning there as we start another \selectlanguage with the next paragraph if
1058 //#warning we are in need of this. This should be fixed sometime (Jug)
1060 running_font.latexWriteEndChanges(os, basefont, basefont);
1064 // Needed if there is an optional argument but no contents.
1065 if (body_pos > 0 && body_pos == size()) {
1067 return_value = false;
1071 column += endTeXParParams(bparams, os, moving_arg);
1074 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1075 return return_value;
1081 // checks, if newcol chars should be put into this line
1082 // writes newline, if necessary.
1083 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1084 string::size_type newcol)
1087 if (colcount > lyxrc.ascii_linelen) {
1089 colcount = newcol; // assume write after this call
1104 string tag_name(PAR_TAG const & pt) {
1106 case NONE: return "!-- --";
1107 case TT: return "tt";
1108 case SF: return "sf";
1109 case BF: return "bf";
1110 case IT: return "it";
1111 case SL: return "sl";
1112 case EM: return "em";
1119 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1121 p1 = static_cast<PAR_TAG>(p1 | p2);
1126 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1128 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1134 // Handle internal paragraph parsing -- layout already processed.
1135 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1137 LyXFont const & outerfont,
1138 OutputParams const & runparams,
1139 lyx::depth_type /*depth*/) const
1141 LyXLayout_ptr const & style = layout();
1143 string::size_type char_line_count = 5; // Heuristic choice ;-)
1145 // gets paragraph main font
1148 if (style->labeltype == LABEL_MANUAL) {
1149 font_old = style->labelfont;
1152 font_old = style->font;
1156 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1157 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1158 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1161 stack<PAR_TAG> tag_state;
1162 // parsing main loop
1163 for (pos_type i = 0; i < size(); ++i) {
1165 PAR_TAG tag_close = NONE;
1166 list < PAR_TAG > tag_open;
1168 LyXFont const font = getFont(buf.params(), i, outerfont);
1170 if (font_old.family() != font.family()) {
1171 switch (family_type) {
1172 case LyXFont::SANS_FAMILY:
1175 case LyXFont::TYPEWRITER_FAMILY:
1182 family_type = font.family();
1184 switch (family_type) {
1185 case LyXFont::SANS_FAMILY:
1186 tag_open.push_back(SF);
1188 case LyXFont::TYPEWRITER_FAMILY:
1189 tag_open.push_back(TT);
1196 if (font_old.series() != font.series()) {
1197 switch (series_type) {
1198 case LyXFont::BOLD_SERIES:
1205 series_type = font.series();
1207 switch (series_type) {
1208 case LyXFont::BOLD_SERIES:
1209 tag_open.push_back(BF);
1217 if (font_old.shape() != font.shape()) {
1218 switch (shape_type) {
1219 case LyXFont::ITALIC_SHAPE:
1222 case LyXFont::SLANTED_SHAPE:
1229 shape_type = font.shape();
1231 switch (shape_type) {
1232 case LyXFont::ITALIC_SHAPE:
1233 tag_open.push_back(IT);
1235 case LyXFont::SLANTED_SHAPE:
1236 tag_open.push_back(SL);
1243 if (font_old.emph() != font.emph()) {
1244 if (font.emph() == LyXFont::ON) {
1245 tag_open.push_back(EM);
1254 list < PAR_TAG > temp;
1255 while (!tag_state.empty() && tag_close) {
1256 PAR_TAG k = tag_state.top();
1258 os << "</" << tag_name(k) << '>';
1265 for(list< PAR_TAG >::const_iterator j = temp.begin();
1266 j != temp.end(); ++j) {
1268 os << '<' << tag_name(*j) << '>';
1271 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1272 j != tag_open.end(); ++j) {
1274 os << '<' << tag_name(*j) << '>';
1277 char c = getChar(i);
1280 if (c == Paragraph::META_INSET) {
1281 InsetOld const * inset = getInset(i);
1282 inset->linuxdoc(buf, os, runparams);
1287 if (style->latexparam() == "CDATA") {
1288 // "TeX"-Mode on == > SGML-Mode on.
1295 boost::tie(ws, str) = sgml::escapeChar(c);
1296 if (ws && !isFreeSpacing()) {
1297 // in freespacing mode, spaces are
1298 // non-breaking characters
1299 if (desc_on) {// if char is ' ' then...
1302 sgmlLineBreak(os, char_line_count, 6);
1306 sgmlLineBreak(os, char_line_count, 1);
1311 char_line_count += str.length();
1317 while (!tag_state.empty()) {
1318 os << "</" << tag_name(tag_state.top()) << '>';
1322 // resets description flag correctly
1324 // <tag> not closed...
1325 sgmlLineBreak(os, char_line_count, 6);
1331 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1333 LyXFont const & outerfont,
1334 OutputParams const & runparams,
1335 lyx::depth_type depth,
1338 bool emph_flag = false;
1340 LyXLayout_ptr const & style = layout();
1341 LyXLayout_ptr const & defaultstyle
1342 = buf.params().getLyXTextClass().defaultLayout();
1344 LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1346 int char_line_count = depth;
1347 bool label_closed = true;
1348 bool para_closed = true;
1350 if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1352 Counters & counters = buf.params().getLyXTextClass().counters();
1353 if (!style->free_spacing)
1354 os << string(depth,' ');
1355 if (!style->labeltag().empty()) {
1356 os << "<" << style->labeltag() << ">\n";
1357 label_closed = false;
1359 if (!defaultstyle->latexparam().empty()) {
1360 counters.step("para");
1361 ls = tostr(counters.value("para"));
1363 + subst(defaultstyle->latexparam(), "#", ls) + '"';
1365 os << "<" << style->itemtag() << ">\n"
1366 << string(depth, ' ') << "<"
1367 << defaultstyle->latexname() << ls << ">\n";
1368 para_closed = false;
1372 // parsing main loop
1373 for (pos_type i = 0; i < size(); ++i) {
1374 LyXFont font = getFont(buf.params(), i, outerfont);
1376 // handle <emphasis> tag
1377 if (font_old.emph() != font.emph()) {
1378 if (font.emph() == LyXFont::ON) {
1379 if (style->latexparam() == "CDATA")
1382 if (style->latexparam() == "CDATA")
1386 if (style->latexparam() == "CDATA")
1388 os << "</emphasis>";
1389 if (style->latexparam() == "CDATA")
1397 InsetOld const * inset = getInset(i);
1398 // don't print the inset in position 0 if desc_on == 3 (label)
1399 //if (i || desc_on != 3) {
1401 if (style->latexparam() == "CDATA")
1403 inset->docbook(buf, os, runparams);
1404 if (style->latexparam() == "CDATA")
1408 char c = getChar(i);
1411 boost::tie(ws, str) = sgml::escapeChar(c);
1413 if (style->pass_thru) {
1415 } else if (isFreeSpacing() || c != ' ') {
1417 } else if (!style->labeltag().empty() && !label_closed) {
1419 os << "\n</" << style->labeltag() << "><"
1420 << style->itemtag() << "><"
1421 << defaultstyle->latexname() << ">";
1422 label_closed = true;
1423 para_closed = false;
1432 if (style->latexparam() == "CDATA")
1434 os << "</emphasis>";
1435 if (style->latexparam() == "CDATA")
1439 // resets description flag correctly
1440 if (!label_closed) {
1441 // <term> not closed...
1442 os << "</" << style->labeltag() << ">\n<"
1443 << style->itemtag() << "><"
1444 << defaultstyle->latexname() << "> ";
1447 os << "\n" << string(depth, ' ') << "</"
1448 << defaultstyle->latexname() << ">\n";
1450 if (style->free_spacing)
1457 /// return true if the char is a meta-character for an inset
1459 bool IsInsetChar(char c)
1461 return (c == Paragraph::META_INSET);
1468 bool Paragraph::isHfill(pos_type pos) const
1470 return IsInsetChar(getChar(pos))
1471 && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
1475 bool Paragraph::isInset(pos_type pos) const
1477 return IsInsetChar(getChar(pos));
1481 bool Paragraph::isNewline(pos_type pos) const
1483 return IsInsetChar(getChar(pos))
1484 && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
1488 bool Paragraph::isSeparator(pos_type pos) const
1490 return IsSeparatorChar(getChar(pos));
1494 bool Paragraph::isLineSeparator(pos_type pos) const
1496 value_type const c = getChar(pos);
1497 return IsLineSeparatorChar(c)
1498 || (IsInsetChar(c) && getInset(pos) &&
1499 getInset(pos)->isLineSeparator());
1503 bool Paragraph::isKomma(pos_type pos) const
1505 return IsKommaChar(getChar(pos));
1509 /// Used by the spellchecker
1510 bool Paragraph::isLetter(pos_type pos) const
1512 value_type const c = getChar(pos);
1513 if (IsLetterChar(c))
1516 return getInset(pos)->isLetter();
1517 // We want to pass the ' and escape chars to ispell
1518 string const extra = lyxrc.isp_esc_chars + '\'';
1519 return contains(extra, c);
1523 bool Paragraph::isWord(pos_type pos) const
1525 unsigned char const c = getChar(pos);
1526 return !(IsSeparatorChar(c)
1533 Paragraph::getParLanguage(BufferParams const & bparams) const
1536 return getFirstFontSettings().language();
1537 #warning FIXME we should check the prev par as well (Lgb)
1538 return bparams.language;
1542 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1544 return lyxrc.rtl_support
1545 && getParLanguage(bparams)->RightToLeft()
1546 && !(inInset() && inInset()->owner() &&
1547 inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
1551 void Paragraph::changeLanguage(BufferParams const & bparams,
1552 Language const * from, Language const * to)
1554 for (pos_type i = 0; i < size(); ++i) {
1555 LyXFont font = getFontSettings(bparams, i);
1556 if (font.language() == from) {
1557 font.setLanguage(to);
1564 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1566 Language const * doc_language = bparams.language;
1567 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1568 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1570 for (; cit != end; ++cit)
1571 if (cit->font().language() != ignore_language &&
1572 cit->font().language() != latex_language &&
1573 cit->font().language() != doc_language)
1579 // Convert the paragraph to a string.
1580 // Used for building the table of contents
1581 string const Paragraph::asString(Buffer const & buffer,
1584 OutputParams runparams;
1585 return asString(buffer, runparams, label);
1589 string const Paragraph::asString(Buffer const & buffer,
1590 OutputParams const & runparams,
1595 if (label && !params().labelString().empty())
1596 s += params().labelString() + ' ';
1598 for (pos_type i = 0; i < size(); ++i) {
1599 value_type c = getChar(i);
1602 else if (c == META_INSET &&
1603 getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
1605 getInset(i)->plaintext(buffer, os, runparams);
1606 s += subst(STRCONV(os.str()),'\n',' ');
1612 // This should really be done by the caller and not here.
1613 string ret = asString(buffer, runparams, 0, size(), label);
1614 return subst(ret, '\n', ' ');
1619 string const Paragraph::asString(Buffer const & buffer,
1620 pos_type beg, pos_type end, bool label) const
1623 OutputParams const runparams;
1624 return asString(buffer, runparams, beg, end, label);
1628 string const Paragraph::asString(Buffer const & buffer,
1629 OutputParams const & runparams,
1630 pos_type beg, pos_type end, bool label) const
1634 if (beg == 0 && label && !params().labelString().empty())
1635 os << params().labelString() << ' ';
1637 for (pos_type i = beg; i < end; ++i) {
1638 value_type const c = getUChar(buffer.params(), i);
1641 else if (c == META_INSET)
1642 getInset(i)->plaintext(buffer, os, runparams);
1649 void Paragraph::setInsetOwner(UpdatableInset * inset)
1651 pimpl_->inset_owner = inset;
1652 InsetList::iterator it = insetlist.begin();
1653 InsetList::iterator end = insetlist.end();
1654 for (; it != end; ++it)
1656 it->inset->setOwner(inset);
1660 void Paragraph::setContentsFromPar(Paragraph const & par)
1662 pimpl_->setContentsFromPar(par);
1666 void Paragraph::trackChanges(Change::Type type)
1668 pimpl_->trackChanges(type);
1672 void Paragraph::untrackChanges()
1674 pimpl_->untrackChanges();
1678 void Paragraph::cleanChanges()
1680 pimpl_->cleanChanges();
1684 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1686 BOOST_ASSERT(!size() || pos < size());
1687 return pimpl_->lookupChange(pos);
1691 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1693 BOOST_ASSERT(!size() || pos < size());
1694 return pimpl_->lookupChangeFull(pos);
1698 bool Paragraph::isChanged(pos_type start, pos_type end) const
1700 return pimpl_->isChanged(start, end);
1704 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1706 return pimpl_->isChangeEdited(start, end);
1710 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1712 pimpl_->setChange(pos, type);
1717 void Paragraph::markErased()
1719 pimpl_->markErased();
1723 void Paragraph::acceptChange(pos_type start, pos_type end)
1725 return pimpl_->acceptChange(start, end);
1729 void Paragraph::rejectChange(pos_type start, pos_type end)
1731 return pimpl_->rejectChange(start, end);
1735 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1737 // This is in the critical path!
1738 pos_type const siz = text_.size();
1740 BOOST_ASSERT(0 <= pos);
1741 BOOST_ASSERT(pos <= siz);
1744 lyxerr << "getChar() on pos " << pos << " in par id "
1745 << id() << " of size " << siz
1746 << " is a bit silly !" << endl;
1747 BOOST_ASSERT(false);
1755 int Paragraph::id() const
1761 LyXLayout_ptr const & Paragraph::layout() const
1764 InsetOld * inset = inInset();
1765 if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
1766 return static_cast<InsetEnvironment*>(inset)->layout();
1772 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1774 layout_ = new_layout;
1778 UpdatableInset * Paragraph::inInset() const
1780 return pimpl_->inset_owner;
1784 void Paragraph::clearContents()
1790 void Paragraph::setChar(pos_type pos, value_type c)
1796 ParagraphParameters & Paragraph::params()
1798 return pimpl_->params;
1802 ParagraphParameters const & Paragraph::params() const
1804 return pimpl_->params;
1808 bool Paragraph::isFreeSpacing() const
1810 if (layout()->free_spacing)
1813 // for now we just need this, later should we need this in some
1814 // other way we can always add a function to InsetOld too.
1815 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1816 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1821 bool Paragraph::allowEmpty() const
1823 if (layout()->keepempty)
1825 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1826 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1831 RowList::iterator Paragraph::getRow(pos_type pos)
1833 RowList::iterator rit = rows.end();
1834 RowList::iterator const begin = rows.begin();
1836 for (--rit; rit != begin && rit->pos() > pos; --rit)
1843 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1845 if (!Encodings::is_arabic(c))
1846 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1847 return c + (0xb0 - '0');
1851 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1852 unsigned char next_char = ' ';
1854 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1855 unsigned char const par_char = getChar(i);
1856 if (!Encodings::IsComposeChar_arabic(par_char)) {
1857 next_char = par_char;
1862 if (Encodings::is_arabic(next_char)) {
1863 if (Encodings::is_arabic(prev_char) &&
1864 !Encodings::is_arabic_special(prev_char))
1865 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1867 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1869 if (Encodings::is_arabic(prev_char) &&
1870 !Encodings::is_arabic_special(prev_char))
1871 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1873 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);