3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "paragraph.h"
21 #include "paragraph_pimpl.h"
24 #include "bufferparams.h"
30 #include "LaTeXFeatures.h"
35 #include "outputparams.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphList_fwd.h"
39 #include "rowpainter.h"
45 #include "frontends/FontMetrics.h"
47 #include "insets/insetbibitem.h"
48 #include "insets/insetoptarg.h"
50 #include "support/lstrings.h"
51 #include "support/textutils.h"
52 #include "support/convert.h"
53 #include "support/unicode.h"
55 #include <boost/bind.hpp>
68 using std::ostringstream;
72 using support::contains;
73 using support::rsplit;
76 Paragraph::Paragraph()
77 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
84 Paragraph::Paragraph(Paragraph const & par)
85 : itemdepth(par.itemdepth), insetlist(par.insetlist),
87 text_(par.text_), begin_of_body_(par.begin_of_body_),
88 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
90 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
91 InsetList::iterator it = insetlist.begin();
92 InsetList::iterator end = insetlist.end();
93 for (; it != end; ++it)
94 it->inset = it->inset->clone().release();
98 Paragraph & Paragraph::operator=(Paragraph const & par)
100 // needed as we will destroy the pimpl_ before copying it
102 itemdepth = par.itemdepth;
104 insetlist = par.insetlist;
105 InsetList::iterator it = insetlist.begin();
106 InsetList::iterator end = insetlist.end();
107 for (; it != end; ++it)
108 it->inset = it->inset->clone().release();
110 layout_ = par.layout();
112 begin_of_body_ = par.begin_of_body_;
115 pimpl_ = new Pimpl(*par.pimpl_, this);
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);
159 for (pos_type i = 0; i <= size(); ++i) {
161 Change change = pimpl_->lookupChange(i);
162 Changes::lyxMarkChange(os, column, running_change, change);
163 running_change = change;
168 // Write font changes
169 LyXFont font2 = getFontSettings(bparams, i);
170 if (font2 != font1) {
171 font2.lyxWriteChanges(font1, os);
176 value_type const c = getChar(i);
180 InsetBase const * inset = getInset(i);
182 if (inset->directWrite()) {
183 // international char, let it write
184 // code directly so it's shorter in
186 inset->write(buf, os);
190 os << "\\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.
217 std::vector<char> tmp = ucs4_to_utf8(c);
221 lyxerr << "ERROR (Paragraph::writeFile):"
222 " NULL char in structure." << endl;
228 os << "\n\\end_layout\n";
232 void Paragraph::validate(LaTeXFeatures & features) const
234 pimpl_->validate(features, *layout());
238 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
240 return pimpl_->eraseChar(pos, trackChanges);
244 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
246 return pimpl_->eraseChars(start, end, trackChanges);
250 void Paragraph::insert(pos_type start, docstring const & str,
251 LyXFont const & font, Change const & change)
253 for (size_t i = 0, n = str.size(); i != n ; ++i)
254 insertChar(start + i, str[i], font, change);
258 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
261 pimpl_->insertChar(pos, c, Change(trackChanges ?
262 Change::INSERTED : Change::UNCHANGED));
266 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
267 LyXFont const & font, bool trackChanges)
269 pimpl_->insertChar(pos, c, Change(trackChanges ?
270 Change::INSERTED : Change::UNCHANGED));
275 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
276 LyXFont const & font, Change const & change)
278 pimpl_->insertChar(pos, c, change);
283 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
284 Change const & change)
286 pimpl_->insertInset(pos, inset, change);
290 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
291 LyXFont const & font, Change const & change)
293 pimpl_->insertInset(pos, inset, change);
298 bool Paragraph::insetAllowed(InsetBase_code code)
300 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
304 // Gets uninstantiated font setting at position.
305 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
309 lyxerr << " pos: " << pos << " size: " << size() << endl;
310 BOOST_ASSERT(pos <= size());
313 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
314 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
315 for (; cit != end; ++cit)
316 if (cit->pos() >= pos)
322 if (pos == size() && !empty())
323 return getFontSettings(bparams, pos - 1);
325 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
329 FontSpan Paragraph::fontSpan(pos_type pos) const
331 BOOST_ASSERT(pos <= size());
334 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
335 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
336 for (; cit != end; ++cit) {
337 if (cit->pos() >= pos) {
338 if (pos >= beginOfBody())
339 return FontSpan(std::max(start, beginOfBody()),
342 return FontSpan(start,
343 std::min(beginOfBody() - 1,
346 start = cit->pos() + 1;
349 // This should not happen, but if so, we take no chances.
350 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
352 return FontSpan(pos, pos);
356 // Gets uninstantiated font setting at position 0
357 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
359 if (!empty() && !pimpl_->fontlist.empty())
360 return pimpl_->fontlist[0].font();
362 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
366 // Gets the fully instantiated font at a given position in a paragraph
367 // This is basically the same function as LyXText::GetFont() in text2.C.
368 // The difference is that this one is used for generating the LaTeX file,
369 // and thus cosmetic "improvements" are disallowed: This has to deliver
370 // the true picture of the buffer. (Asger)
371 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
372 LyXFont const & outerfont) const
374 BOOST_ASSERT(pos >= 0);
376 LyXLayout_ptr const & lout = layout();
378 pos_type const body_pos = beginOfBody();
382 layoutfont = lout->labelfont;
384 layoutfont = lout->font;
386 LyXFont font = getFontSettings(bparams, pos);
387 font.realize(layoutfont);
388 font.realize(outerfont);
389 font.realize(bparams.getFont());
395 LyXFont const Paragraph::getLabelFont
396 (BufferParams const & bparams, LyXFont const & outerfont) const
398 LyXFont tmpfont = layout()->labelfont;
399 tmpfont.setLanguage(getParLanguage(bparams));
400 tmpfont.realize(outerfont);
401 tmpfont.realize(bparams.getFont());
406 LyXFont const Paragraph::getLayoutFont
407 (BufferParams const & bparams, LyXFont const & outerfont) const
409 LyXFont tmpfont = layout()->font;
410 tmpfont.setLanguage(getParLanguage(bparams));
411 tmpfont.realize(outerfont);
412 tmpfont.realize(bparams.getFont());
417 /// Returns the height of the highest font in range
418 LyXFont_size Paragraph::highestFontInRange
419 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
421 if (pimpl_->fontlist.empty())
424 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
425 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
426 for (; end_it != end; ++end_it) {
427 if (end_it->pos() >= endpos)
434 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
435 for (; cit != end; ++cit) {
436 if (cit->pos() >= startpos)
440 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
441 for (; cit != end_it; ++cit) {
442 LyXFont::FONT_SIZE size = cit->font().size();
443 if (size == LyXFont::INHERIT_SIZE)
445 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
452 Paragraph::value_type
453 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
455 value_type c = getChar(pos);
456 if (!lyxrc.rtl_support)
486 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
493 void Paragraph::setFont(pos_type pos, LyXFont const & font)
495 BOOST_ASSERT(pos <= size());
497 // First, reduce font against layout/label font
498 // Update: The setCharFont() routine in text2.C already
499 // reduces font, so we don't need to do that here. (Asger)
500 // No need to simplify this because it will disappear
501 // in a new kernel. (Asger)
502 // Next search font table
504 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
505 Pimpl::FontList::iterator it = beg;
506 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
507 for (; it != endit; ++it) {
508 if (it->pos() >= pos)
511 size_t const i = distance(beg, it);
512 bool notfound = (it == endit);
514 if (!notfound && pimpl_->fontlist[i].font() == font)
517 bool begin = pos == 0 || notfound ||
518 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
519 // Is position pos is a beginning of a font block?
520 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
521 // Is position pos is the end of a font block?
522 if (begin && end) { // A single char block
523 if (i + 1 < pimpl_->fontlist.size() &&
524 pimpl_->fontlist[i + 1].font() == font) {
525 // Merge the singleton block with the next block
526 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
527 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
528 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
529 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
530 // Merge the singleton block with the previous block
531 pimpl_->fontlist[i - 1].pos(pos);
532 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
534 pimpl_->fontlist[i].font(font);
536 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
537 pimpl_->fontlist[i - 1].pos(pos);
539 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
540 Pimpl::FontTable(pos, font));
542 pimpl_->fontlist[i].pos(pos - 1);
543 if (!(i + 1 < pimpl_->fontlist.size() &&
544 pimpl_->fontlist[i + 1].font() == font))
545 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
546 Pimpl::FontTable(pos, font));
547 } else { // The general case. The block is splitted into 3 blocks
548 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
549 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
550 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
551 Pimpl::FontTable(pos, font));
556 void Paragraph::makeSameLayout(Paragraph const & par)
558 layout(par.layout());
560 params() = par.params();
564 int Paragraph::stripLeadingSpaces(bool trackChanges)
572 while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
573 if (eraseChar(pos, trackChanges))
583 bool Paragraph::hasSameLayout(Paragraph const & par) const
585 return par.layout() == layout() && params().sameLayout(par.params());
589 depth_type Paragraph::getDepth() const
591 return params().depth();
595 depth_type Paragraph::getMaxDepthAfter() const
597 if (layout()->isEnvironment())
598 return params().depth() + 1;
600 return params().depth();
604 char Paragraph::getAlign() const
606 if (params().align() == LYX_ALIGN_LAYOUT)
607 return layout()->align;
609 return params().align();
613 docstring const & Paragraph::getLabelstring() const
615 return params().labelString();
619 // the next two functions are for the manual labels
620 docstring const Paragraph::getLabelWidthString() const
622 if (!params().labelWidthString().empty())
623 return params().labelWidthString();
625 return _("Senseless with this layout!");
629 void Paragraph::setLabelWidthString(docstring const & s)
631 params().labelWidthString(s);
635 docstring const Paragraph::translateIfPossible(docstring const & s,
636 BufferParams const & bparams) const
638 if (!support::isAscii(s) || s.empty()) {
639 // This must be a user defined layout. We cannot translate
640 // this, since gettext accepts only ascii keys.
643 // Probably standard layout, try to translate
644 Messages & m = getMessages(getParLanguage(bparams)->code());
645 return m.get(to_ascii(s));
649 docstring Paragraph::expandLabel(LyXLayout_ptr const & layout,
650 BufferParams const & bparams, bool process_appendix) const
652 LyXTextClass const & tclass = bparams.getLyXTextClass();
655 if (process_appendix && params().appendix())
656 fmt = translateIfPossible(layout->labelstring_appendix(),
659 fmt = translateIfPossible(layout->labelstring(), bparams);
661 // handle 'inherited level parts' in 'fmt',
662 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
663 size_t const i = fmt.find('@', 0);
664 if (i != docstring::npos) {
665 size_t const j = fmt.find('@', i + 1);
666 if (j != docstring::npos) {
667 docstring parent(fmt, i + 1, j - i - 1);
669 docstring label = expandLabel(tclass[to_utf8(parent)], bparams);
670 fmt = docstring(fmt, 0, i) + label + docstring(fmt, j + 1, docstring::npos);
674 return tclass.counters().counterLabel(fmt);
678 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
681 params().labelWidthString(docstring());
682 params().align(LYX_ALIGN_LAYOUT);
683 params().spacing(Spacing(Spacing::Default));
687 pos_type Paragraph::beginOfBody() const
689 return begin_of_body_;
693 void Paragraph::setBeginOfBody()
695 if (layout()->labeltype != LABEL_MANUAL) {
700 // Unroll the first two cycles of the loop
701 // and remember the previous character to
702 // remove unnecessary getChar() calls
704 pos_type end = size();
705 if (i < end && !isNewline(i)) {
707 char_type previous_char = 0;
710 previous_char = text_[i];
713 while (i < end && previous_char != ' ') {
718 previous_char = temp;
728 // returns -1 if inset not found
729 int Paragraph::getPositionOfInset(InsetBase const * inset) const
732 InsetList::const_iterator it = insetlist.begin();
733 InsetList::const_iterator end = insetlist.end();
734 for (; it != end; ++it)
735 if (it->inset == inset)
741 InsetBibitem * Paragraph::bibitem() const
743 if (!insetlist.empty()) {
744 InsetBase * inset = insetlist.begin()->inset;
745 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
746 return static_cast<InsetBibitem *>(inset);
752 bool Paragraph::forceDefaultParagraphs() const
754 return inInset() && inInset()->forceDefaultParagraphs(0);
760 // paragraphs inside floats need different alignment tags to avoid
763 bool noTrivlistCentering(InsetBase::Code code)
765 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
769 string correction(string const & orig)
771 if (orig == "flushleft")
772 return "raggedright";
773 if (orig == "flushright")
775 if (orig == "center")
781 string const corrected_env(string const & suffix, string const & env,
782 InsetBase::Code code)
784 string output = suffix + "{";
785 if (noTrivlistCentering(code))
786 output += correction(env);
790 if (suffix == "\\begin")
796 int adjust_column_count(string const & str, int oldcol)
798 if (!contains(str, "\n"))
799 return oldcol + str.size();
802 return rsplit(str, tmp, '\n').size();
809 // This could go to ParagraphParameters if we want to
810 int Paragraph::startTeXParParams(BufferParams const & bparams,
811 odocstream & os, bool moving_arg) const
815 if (params().noindent()) {
820 switch (params().align()) {
822 case LYX_ALIGN_BLOCK:
823 case LYX_ALIGN_LAYOUT:
824 case LYX_ALIGN_SPECIAL:
827 case LYX_ALIGN_RIGHT:
828 case LYX_ALIGN_CENTER:
836 switch (params().align()) {
838 case LYX_ALIGN_BLOCK:
839 case LYX_ALIGN_LAYOUT:
840 case LYX_ALIGN_SPECIAL:
842 case LYX_ALIGN_LEFT: {
844 if (getParLanguage(bparams)->babel() != "hebrew")
845 output = corrected_env("\\begin", "flushleft", ownerCode());
847 output = corrected_env("\\begin", "flushright", ownerCode());
848 os << from_ascii(output);
849 column = adjust_column_count(output, column);
851 } case LYX_ALIGN_RIGHT: {
853 if (getParLanguage(bparams)->babel() != "hebrew")
854 output = corrected_env("\\begin", "flushright", ownerCode());
856 output = corrected_env("\\begin", "flushleft", ownerCode());
857 os << from_ascii(output);
858 column = adjust_column_count(output, column);
860 } case LYX_ALIGN_CENTER: {
862 output = corrected_env("\\begin", "center", ownerCode());
863 os << from_ascii(output);
864 column = adjust_column_count(output, column);
873 // This could go to ParagraphParameters if we want to
874 int Paragraph::endTeXParParams(BufferParams const & bparams,
875 odocstream & os, bool moving_arg) const
879 switch (params().align()) {
881 case LYX_ALIGN_BLOCK:
882 case LYX_ALIGN_LAYOUT:
883 case LYX_ALIGN_SPECIAL:
886 case LYX_ALIGN_RIGHT:
887 case LYX_ALIGN_CENTER:
895 switch (params().align()) {
897 case LYX_ALIGN_BLOCK:
898 case LYX_ALIGN_LAYOUT:
899 case LYX_ALIGN_SPECIAL:
901 case LYX_ALIGN_LEFT: {
903 if (getParLanguage(bparams)->babel() != "hebrew")
904 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
906 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
907 os << from_ascii(output);
908 column = adjust_column_count(output, column);
910 } case LYX_ALIGN_RIGHT: {
912 if (getParLanguage(bparams)->babel() != "hebrew")
913 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
915 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
916 os << from_ascii(output);
917 column = adjust_column_count(output, column);
919 } case LYX_ALIGN_CENTER: {
921 output = corrected_env("\n\\par\\end", "center", ownerCode());
922 os << from_ascii(output);
923 column = adjust_column_count(output, column);
932 // This one spits out the text of the paragraph
933 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
934 BufferParams const & bparams,
935 LyXFont const & outerfont,
936 odocstream & os, TexRow & texrow,
937 OutputParams const & runparams) const
939 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
941 bool return_value = false;
945 // well we have to check if we are in an inset with unlimited
946 // length (all in one row) if that is true then we don't allow
947 // any special options in the paragraph and also we don't allow
948 // any environment other than the default layout of the text class
950 bool asdefault = forceDefaultParagraphs();
953 style = bparams.getLyXTextClass().defaultLayout();
958 // Current base font for all inherited font changes, without any
959 // change caused by an individual character, except for the language:
960 // It is set to the language of the first character.
961 // As long as we are in the label, this font is the base font of the
962 // label. Before the first body character it is set to the base font
964 // This must be identical to basefont in TeXOnePar().
967 LaTeXFeatures features(buf, bparams, runparams);
969 // output change tracking marks only if desired,
970 // if dvipost is installed,
971 // and with dvi/ps (other formats don't work)
972 bool const output = bparams.outputChanges
973 && runparams.flavor == OutputParams::LATEX
974 && features.isAvailable("dvipost");
976 // Maybe we have to create a optional argument.
977 pos_type body_pos = beginOfBody();
978 unsigned int column = 0;
981 // the optional argument is kept in curly brackets in
982 // case it contains a ']'
985 basefont = getLabelFont(bparams, outerfont);
987 basefont = getLayoutFont(bparams, outerfont);
990 // Which font is currently active?
991 LyXFont running_font(basefont);
992 // Do we have an open font change?
993 bool open_font = false;
995 Change::Type runningChangeType = Change::UNCHANGED;
997 texrow.start(id(), 0);
999 // if the paragraph is empty, the loop will not be entered at all
1001 if (style->isCommand()) {
1006 column += startTeXParParams(bparams, os,
1007 runparams.moving_arg);
1010 for (pos_type i = 0; i < size(); ++i) {
1012 // First char in paragraph or after label?
1013 if (i == body_pos) {
1016 column += running_font.latexWriteEndChanges(
1017 os, basefont, basefont, bparams);
1020 basefont = getLayoutFont(bparams, outerfont);
1021 running_font = basefont;
1025 if (style->isCommand()) {
1031 column += startTeXParParams(bparams, os,
1032 runparams.moving_arg);
1035 value_type c = getChar(i);
1037 // Fully instantiated font
1038 LyXFont font = getFont(bparams, i, outerfont);
1040 LyXFont const last_font = running_font;
1042 // Spaces at end of font change are simulated to be
1043 // outside font change, i.e. we write "\textXX{text} "
1044 // rather than "\textXX{text }". (Asger)
1045 if (open_font && c == ' ' && i <= size() - 2) {
1046 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
1047 if (next_font != running_font && next_font != font) {
1052 // We end font definition before blanks
1054 (font != running_font ||
1055 font.language() != running_font.language()))
1057 column += running_font.latexWriteEndChanges(
1059 (i == body_pos-1) ? basefont : font,
1061 running_font = basefont;
1065 // Blanks are printed before start of fontswitch
1067 // Do not print the separation of the optional argument
1068 if (i != body_pos - 1) {
1069 pimpl_->simpleTeXBlanks(os, texrow, i,
1070 column, font, *style);
1074 // Do we need to change font?
1075 if ((font != running_font ||
1076 font.language() != running_font.language()) &&
1079 column += font.latexWriteStartChanges(
1080 os, basefont, last_font, bparams);
1081 running_font = font;
1085 Change::Type changeType = pimpl_->lookupChange(i).type;
1087 column += Changes::latexMarkChange(os, runningChangeType,
1088 changeType, output);
1089 runningChangeType = changeType;
1091 // do not output text which is marked deleted
1092 // if change tracking output is not desired
1093 if (output || runningChangeType != Change::DELETED) {
1094 OutputParams rp = runparams;
1095 rp.free_spacing = style->free_spacing;
1096 rp.local_font = &font;
1097 rp.intitle = style->intitle;
1098 pimpl_->simpleTeXSpecialChars(buf, bparams,
1101 basefont, outerfont, open_font,
1103 *style, i, column, c);
1107 column += Changes::latexMarkChange(os,
1108 runningChangeType, Change::UNCHANGED, output);
1110 // If we have an open font definition, we have to close it
1112 #ifdef FIXED_LANGUAGE_END_DETECTION
1115 .latexWriteEndChanges(os, basefont,
1116 next_->getFont(bparams, 0, outerfont),
1119 running_font.latexWriteEndChanges(os, basefont,
1123 #ifdef WITH_WARNINGS
1124 //#warning For now we ALWAYS have to close the foreign font settings if they are
1125 //#warning there as we start another \selectlanguage with the next paragraph if
1126 //#warning we are in need of this. This should be fixed sometime (Jug)
1128 running_font.latexWriteEndChanges(os, basefont, basefont,
1133 // Needed if there is an optional argument but no contents.
1134 if (body_pos > 0 && body_pos == size()) {
1136 return_value = false;
1140 column += endTeXParParams(bparams, os, runparams.moving_arg);
1143 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1144 return return_value;
1161 string tag_name(PAR_TAG const & pt) {
1163 case PAR_NONE: return "!-- --";
1164 case TT: return "tt";
1165 case SF: return "sf";
1166 case BF: return "bf";
1167 case IT: return "it";
1168 case SL: return "sl";
1169 case EM: return "em";
1176 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1178 p1 = static_cast<PAR_TAG>(p1 | p2);
1183 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1185 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1191 bool Paragraph::emptyTag() const
1193 for (pos_type i = 0; i < size(); ++i) {
1195 InsetBase const * inset = getInset(i);
1196 InsetBase::Code lyx_code = inset->lyxCode();
1197 if (lyx_code != InsetBase::TOC_CODE &&
1198 lyx_code != InsetBase::INCLUDE_CODE &&
1199 lyx_code != InsetBase::GRAPHICS_CODE &&
1200 lyx_code != InsetBase::ERT_CODE &&
1201 lyx_code != InsetBase::FLOAT_CODE &&
1202 lyx_code != InsetBase::TABULAR_CODE) {
1206 value_type c = getChar(i);
1207 if (c != ' ' && c != '\t')
1215 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1217 for (pos_type i = 0; i < size(); ++i) {
1219 InsetBase const * inset = getInset(i);
1220 InsetBase::Code lyx_code = inset->lyxCode();
1221 if (lyx_code == InsetBase::LABEL_CODE) {
1222 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1223 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
1232 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1235 for (i = 0; i < size(); ++i) {
1237 InsetBase const * inset = getInset(i);
1238 inset->docbook(buf, os, runparams);
1240 value_type c = getChar(i);
1243 os << sgml::escapeChar(c);
1250 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1254 for (pos_type i = initial; i < size(); ++i) {
1255 LyXFont font = getFont(buf.params(), i, outerfont);
1258 if (i != initial && font != font_old)
1267 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1269 OutputParams const & runparams,
1270 LyXFont const & outerfont,
1271 pos_type initial) const
1273 bool emph_flag = false;
1275 LyXLayout_ptr const & style = layout();
1277 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1279 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1282 // parsing main loop
1283 for (pos_type i = initial; i < size(); ++i) {
1284 LyXFont font = getFont(buf.params(), i, outerfont);
1286 // handle <emphasis> tag
1287 if (font_old.emph() != font.emph()) {
1288 if (font.emph() == LyXFont::ON) {
1291 } else if (i != initial) {
1292 os << "</emphasis>";
1298 InsetBase const * inset = getInset(i);
1299 inset->docbook(buf, os, runparams);
1301 value_type c = getChar(i);
1303 if (style->pass_thru)
1306 os << sgml::escapeChar(c);
1312 os << "</emphasis>";
1315 if (style->free_spacing)
1317 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1322 bool Paragraph::isNewline(pos_type pos) const
1325 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1329 bool Paragraph::isLineSeparator(pos_type pos) const
1331 value_type const c = getChar(pos);
1332 return isLineSeparatorChar(c)
1333 || (c == Paragraph::META_INSET && getInset(pos) &&
1334 getInset(pos)->isLineSeparator());
1338 /// Used by the spellchecker
1339 bool Paragraph::isLetter(pos_type pos) const
1342 return getInset(pos)->isLetter();
1344 value_type const c = getChar(pos);
1345 return isLetterChar(c) || isDigit(c);
1351 Paragraph::getParLanguage(BufferParams const & bparams) const
1354 return getFirstFontSettings(bparams).language();
1355 #ifdef WITH_WARNINGS
1356 #warning FIXME we should check the prev par as well (Lgb)
1358 return bparams.language;
1362 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1364 return lyxrc.rtl_support
1365 && getParLanguage(bparams)->rightToLeft()
1366 && ownerCode() != InsetBase::ERT_CODE;
1370 void Paragraph::changeLanguage(BufferParams const & bparams,
1371 Language const * from, Language const * to)
1373 // change language including dummy font change at the end
1374 for (pos_type i = 0; i <= size(); ++i) {
1375 LyXFont font = getFontSettings(bparams, i);
1376 if (font.language() == from) {
1377 font.setLanguage(to);
1384 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1386 Language const * doc_language = bparams.language;
1387 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1388 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1390 for (; cit != end; ++cit)
1391 if (cit->font().language() != ignore_language &&
1392 cit->font().language() != latex_language &&
1393 cit->font().language() != doc_language)
1399 // Convert the paragraph to a string.
1400 // Used for building the table of contents
1401 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1403 OutputParams runparams;
1404 return asString(buffer, runparams, label);
1408 docstring const Paragraph::asString(Buffer const & buffer,
1409 OutputParams const & runparams,
1414 if (label && !params().labelString().empty())
1415 s += params().labelString() + ' ';
1417 for (pos_type i = 0; i < size(); ++i) {
1418 value_type c = getChar(i);
1421 else if (c == META_INSET &&
1422 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1424 getInset(i)->plaintext(buffer, os, runparams);
1425 s += subst(STRCONV(os.str()),'\n',' ');
1431 // This should really be done by the caller and not here.
1432 docstring ret = asString(buffer, runparams, 0, size(), label);
1433 return subst(ret, '\n', ' ');
1438 docstring const Paragraph::asString(Buffer const & buffer,
1439 pos_type beg, pos_type end, bool label) const
1442 OutputParams const runparams;
1443 return asString(buffer, runparams, beg, end, label);
1447 docstring const Paragraph::asString(Buffer const & buffer,
1448 OutputParams const & runparams,
1449 pos_type beg, pos_type end, bool label) const
1451 lyx::odocstringstream os;
1453 if (beg == 0 && label && !params().labelString().empty())
1454 os << params().labelString() << ' ';
1456 for (pos_type i = beg; i < end; ++i) {
1457 value_type const c = getUChar(buffer.params(), i);
1460 else if (c == META_INSET)
1461 getInset(i)->textString(buffer, os, runparams);
1468 void Paragraph::setInsetOwner(InsetBase * inset)
1470 pimpl_->inset_owner = inset;
1474 Change const & Paragraph::lookupChange(pos_type pos) const
1476 BOOST_ASSERT(pos <= size());
1477 return pimpl_->lookupChange(pos);
1481 bool Paragraph::isChanged(pos_type start, pos_type end) const
1483 return pimpl_->isChanged(start, end);
1487 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
1489 return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
1493 void Paragraph::setChange(Change const & change)
1495 pimpl_->setChange(change);
1499 void Paragraph::setChange(pos_type pos, Change const & change)
1501 pimpl_->setChange(pos, change);
1505 void Paragraph::acceptChanges(pos_type start, pos_type end)
1507 return pimpl_->acceptChanges(start, end);
1511 void Paragraph::rejectChanges(pos_type start, pos_type end)
1513 return pimpl_->rejectChanges(start, end);
1517 int Paragraph::id() const
1523 LyXLayout_ptr const & Paragraph::layout() const
1529 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1531 layout_ = new_layout;
1535 InsetBase * Paragraph::inInset() const
1537 return pimpl_->inset_owner;
1541 InsetBase::Code Paragraph::ownerCode() const
1543 return pimpl_->inset_owner
1544 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1548 void Paragraph::clearContents()
1554 ParagraphParameters & Paragraph::params()
1556 return pimpl_->params;
1560 ParagraphParameters const & Paragraph::params() const
1562 return pimpl_->params;
1566 bool Paragraph::isFreeSpacing() const
1568 if (layout()->free_spacing)
1571 // for now we just need this, later should we need this in some
1572 // other way we can always add a function to InsetBase too.
1573 return ownerCode() == InsetBase::ERT_CODE;
1577 bool Paragraph::allowEmpty() const
1579 if (layout()->keepempty)
1581 return ownerCode() == InsetBase::ERT_CODE;
1585 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1587 if (!Encodings::is_arabic(c))
1588 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1589 // FIXME UNICODE What does this do?
1590 return c + (0xb0 - '0');
1594 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1595 value_type next_char = ' ';
1597 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1598 value_type const par_char = getChar(i);
1599 if (!Encodings::isComposeChar_arabic(par_char)) {
1600 next_char = par_char;
1605 if (Encodings::is_arabic(next_char)) {
1606 if (Encodings::is_arabic(prev_char) &&
1607 !Encodings::is_arabic_special(prev_char))
1608 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1610 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1612 if (Encodings::is_arabic(prev_char) &&
1613 !Encodings::is_arabic_special(prev_char))
1614 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1616 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1621 bool Paragraph::hfillExpansion(Row const & row, pos_type pos) const
1626 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
1628 // expand at the end of a row only if there is another hfill on the same row
1629 if (pos == row.endpos() - 1) {
1630 for (pos_type i = row.pos(); i < pos; i++) {
1637 // expand at the beginning of a row only if it is the first row of a paragraph
1638 if (pos == row.pos()) {
1642 // do not expand in some labels
1643 if (layout()->margintype != MARGIN_MANUAL && pos < beginOfBody())
1646 // if there is anything between the first char of the row and
1647 // the specified position that is neither a newline nor an hfill,
1648 // the hfill will be expanded, otherwise it won't
1649 for (pos_type i = row.pos(); i < pos; i++) {
1650 if (!isNewline(i) && !isHfill(i))
1657 bool Paragraph::checkBiblio(bool track_changes)
1659 // Add bibitem insets if necessary
1660 if (layout()->labeltype != LABEL_BIBLIO)
1663 bool hasbibitem = !insetlist.empty()
1664 // Insist on it being in pos 0
1665 && getChar(0) == Paragraph::META_INSET
1666 && insetlist.begin()->inset->lyxCode() == InsetBase::BIBITEM_CODE;
1671 InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem")));
1672 insertInset(0, static_cast<InsetBase *>(inset),
1673 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));