3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "paragraph.h"
21 #include "paragraph_pimpl.h"
24 #include "bufferparams.h"
30 #include "LaTeXFeatures.h"
34 #include "outputparams.h"
35 #include "paragraph_funcs.h"
36 #include "ParagraphList_fwd.h"
38 #include "rowpainter.h"
44 #include "frontends/FontMetrics.h"
46 #include "insets/insetbibitem.h"
47 #include "insets/insetoptarg.h"
49 #include "support/lstrings.h"
50 #include "support/textutils.h"
51 #include "support/convert.h"
52 #include "support/unicode.h"
54 #include <boost/bind.hpp>
67 using std::ostringstream;
71 using support::contains;
72 using support::rsplit;
75 Paragraph::Paragraph()
76 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
83 Paragraph::Paragraph(Paragraph const & par)
84 : itemdepth(par.itemdepth), insetlist(par.insetlist),
86 text_(par.text_), begin_of_body_(par.begin_of_body_),
87 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
89 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
90 InsetList::iterator it = insetlist.begin();
91 InsetList::iterator end = insetlist.end();
92 for (; it != end; ++it)
93 it->inset = it->inset->clone().release();
97 Paragraph & Paragraph::operator=(Paragraph const & par)
99 // needed as we will destroy the pimpl_ before copying it
101 itemdepth = par.itemdepth;
103 insetlist = par.insetlist;
104 InsetList::iterator it = insetlist.begin();
105 InsetList::iterator end = insetlist.end();
106 for (; it != end; ++it)
107 it->inset = it->inset->clone().release();
109 layout_ = par.layout();
111 begin_of_body_ = par.begin_of_body_;
114 pimpl_ = new Pimpl(*par.pimpl_, this);
120 Paragraph::~Paragraph()
124 //lyxerr << "Paragraph::paragraph_id = "
125 // << Paragraph::paragraph_id << endl;
129 void Paragraph::write(Buffer const & buf, ostream & os,
130 BufferParams const & bparams,
131 depth_type & dth) const
133 // The beginning or end of a deeper (i.e. nested) area?
134 if (dth != params().depth()) {
135 if (params().depth() > dth) {
136 while (params().depth() > dth) {
137 os << "\n\\begin_deeper";
141 while (params().depth() < dth) {
142 os << "\n\\end_deeper";
148 // First write the layout
149 os << "\n\\begin_layout " << layout()->name() << '\n';
153 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
155 Change running_change = Change(Change::UNCHANGED);
158 for (pos_type i = 0; i <= size(); ++i) {
160 Change change = pimpl_->lookupChange(i);
161 Changes::lyxMarkChange(os, column, running_change, change);
162 running_change = change;
167 // Write font changes
168 LyXFont font2 = getFontSettings(bparams, i);
169 if (font2 != font1) {
170 font2.lyxWriteChanges(font1, os);
175 value_type const c = getChar(i);
179 InsetBase const * inset = getInset(i);
181 if (inset->directWrite()) {
182 // international char, let it write
183 // code directly so it's shorter in
185 inset->write(buf, os);
189 os << "\\begin_inset ";
190 inset->write(buf, os);
191 os << "\n\\end_inset\n\n";
197 os << "\n\\backslash\n";
201 if (i + 1 < size() && getChar(i + 1) == ' ') {
208 if ((column > 70 && c == ' ')
213 // this check is to amend a bug. LyX sometimes
214 // inserts '\0' this could cause problems.
216 std::vector<char> tmp = ucs4_to_utf8(c);
220 lyxerr << "ERROR (Paragraph::writeFile):"
221 " NULL char in structure." << endl;
227 os << "\n\\end_layout\n";
231 void Paragraph::validate(LaTeXFeatures & features) const
233 pimpl_->validate(features, *layout());
237 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
239 return pimpl_->eraseChar(pos, trackChanges);
243 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
245 return pimpl_->eraseChars(start, end, trackChanges);
249 void Paragraph::insert(pos_type start, docstring const & str,
250 LyXFont const & font, Change const & change)
252 for (size_t i = 0, n = str.size(); i != n ; ++i)
253 insertChar(start + i, str[i], font, change);
257 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
260 pimpl_->insertChar(pos, c, Change(trackChanges ?
261 Change::INSERTED : Change::UNCHANGED));
265 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
266 LyXFont const & font, bool trackChanges)
268 pimpl_->insertChar(pos, c, Change(trackChanges ?
269 Change::INSERTED : Change::UNCHANGED));
274 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
275 LyXFont const & font, Change const & change)
277 pimpl_->insertChar(pos, c, change);
282 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
283 Change const & change)
285 pimpl_->insertInset(pos, inset, change);
289 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
290 LyXFont const & font, Change const & change)
292 pimpl_->insertInset(pos, inset, change);
297 bool Paragraph::insetAllowed(InsetBase_code code)
299 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
303 // Gets uninstantiated font setting at position.
304 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
308 lyxerr << " pos: " << pos << " size: " << size() << endl;
309 BOOST_ASSERT(pos <= size());
312 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
313 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
314 for (; cit != end; ++cit)
315 if (cit->pos() >= pos)
321 if (pos == size() && !empty())
322 return getFontSettings(bparams, pos - 1);
324 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
328 FontSpan Paragraph::fontSpan(pos_type pos) const
330 BOOST_ASSERT(pos <= size());
333 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
334 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
335 for (; cit != end; ++cit) {
336 if (cit->pos() >= pos) {
337 if (pos >= beginOfBody())
338 return FontSpan(std::max(start, beginOfBody()),
341 return FontSpan(start,
342 std::min(beginOfBody() - 1,
345 start = cit->pos() + 1;
348 // This should not happen, but if so, we take no chances.
349 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
351 return FontSpan(pos, pos);
355 // Gets uninstantiated font setting at position 0
356 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
358 if (!empty() && !pimpl_->fontlist.empty())
359 return pimpl_->fontlist[0].font();
361 return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
365 // Gets the fully instantiated font at a given position in a paragraph
366 // This is basically the same function as LyXText::GetFont() in text2.C.
367 // The difference is that this one is used for generating the LaTeX file,
368 // and thus cosmetic "improvements" are disallowed: This has to deliver
369 // the true picture of the buffer. (Asger)
370 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
371 LyXFont const & outerfont) const
373 BOOST_ASSERT(pos >= 0);
375 LyXLayout_ptr const & lout = layout();
377 pos_type const body_pos = beginOfBody();
381 layoutfont = lout->labelfont;
383 layoutfont = lout->font;
385 LyXFont font = getFontSettings(bparams, pos);
386 font.realize(layoutfont);
387 font.realize(outerfont);
388 font.realize(bparams.getFont());
394 LyXFont const Paragraph::getLabelFont
395 (BufferParams const & bparams, LyXFont const & outerfont) const
397 LyXFont tmpfont = layout()->labelfont;
398 tmpfont.setLanguage(getParLanguage(bparams));
399 tmpfont.realize(outerfont);
400 tmpfont.realize(bparams.getFont());
405 LyXFont const Paragraph::getLayoutFont
406 (BufferParams const & bparams, LyXFont const & outerfont) const
408 LyXFont tmpfont = layout()->font;
409 tmpfont.setLanguage(getParLanguage(bparams));
410 tmpfont.realize(outerfont);
411 tmpfont.realize(bparams.getFont());
416 /// Returns the height of the highest font in range
417 LyXFont_size Paragraph::highestFontInRange
418 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
420 if (pimpl_->fontlist.empty())
423 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
424 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
425 for (; end_it != end; ++end_it) {
426 if (end_it->pos() >= endpos)
433 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
434 for (; cit != end; ++cit) {
435 if (cit->pos() >= startpos)
439 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
440 for (; cit != end_it; ++cit) {
441 LyXFont::FONT_SIZE size = cit->font().size();
442 if (size == LyXFont::INHERIT_SIZE)
444 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
451 Paragraph::value_type
452 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
454 value_type c = getChar(pos);
455 if (!lyxrc.rtl_support)
485 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
492 void Paragraph::setFont(pos_type pos, LyXFont const & font)
494 BOOST_ASSERT(pos <= size());
496 // First, reduce font against layout/label font
497 // Update: The setCharFont() routine in text2.C already
498 // reduces font, so we don't need to do that here. (Asger)
499 // No need to simplify this because it will disappear
500 // in a new kernel. (Asger)
501 // Next search font table
503 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
504 Pimpl::FontList::iterator it = beg;
505 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
506 for (; it != endit; ++it) {
507 if (it->pos() >= pos)
510 size_t const i = distance(beg, it);
511 bool notfound = (it == endit);
513 if (!notfound && pimpl_->fontlist[i].font() == font)
516 bool begin = pos == 0 || notfound ||
517 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
518 // Is position pos is a beginning of a font block?
519 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
520 // Is position pos is the end of a font block?
521 if (begin && end) { // A single char block
522 if (i + 1 < pimpl_->fontlist.size() &&
523 pimpl_->fontlist[i + 1].font() == font) {
524 // Merge the singleton block with the next block
525 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
526 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
527 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
528 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
529 // Merge the singleton block with the previous block
530 pimpl_->fontlist[i - 1].pos(pos);
531 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
533 pimpl_->fontlist[i].font(font);
535 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
536 pimpl_->fontlist[i - 1].pos(pos);
538 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
539 Pimpl::FontTable(pos, font));
541 pimpl_->fontlist[i].pos(pos - 1);
542 if (!(i + 1 < pimpl_->fontlist.size() &&
543 pimpl_->fontlist[i + 1].font() == font))
544 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
545 Pimpl::FontTable(pos, font));
546 } else { // The general case. The block is splitted into 3 blocks
547 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
548 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
549 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
550 Pimpl::FontTable(pos, font));
555 void Paragraph::makeSameLayout(Paragraph const & par)
557 layout(par.layout());
559 params() = par.params();
563 int Paragraph::stripLeadingSpaces()
569 while (!empty() && (isNewline(0) || isLineSeparator(0))
571 eraseChar(0, false); // no change tracking here
579 bool Paragraph::hasSameLayout(Paragraph const & par) const
581 return par.layout() == layout() && params().sameLayout(par.params());
585 depth_type Paragraph::getDepth() const
587 return params().depth();
591 depth_type Paragraph::getMaxDepthAfter() const
593 if (layout()->isEnvironment())
594 return params().depth() + 1;
596 return params().depth();
600 char Paragraph::getAlign() const
602 if (params().align() == LYX_ALIGN_LAYOUT)
603 return layout()->align;
605 return params().align();
609 docstring const & Paragraph::getLabelstring() const
611 return params().labelString();
615 // the next two functions are for the manual labels
616 docstring const Paragraph::getLabelWidthString() const
618 if (!params().labelWidthString().empty())
619 return params().labelWidthString();
621 return _("Senseless with this layout!");
625 void Paragraph::setLabelWidthString(docstring const & s)
627 params().labelWidthString(s);
631 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
634 params().labelWidthString(docstring());
635 params().align(LYX_ALIGN_LAYOUT);
636 params().spacing(Spacing(Spacing::Default));
640 pos_type Paragraph::beginOfBody() const
642 return begin_of_body_;
646 void Paragraph::setBeginOfBody()
648 if (layout()->labeltype != LABEL_MANUAL) {
653 // Unroll the first two cycles of the loop
654 // and remember the previous character to
655 // remove unnecessary getChar() calls
657 pos_type end = size();
658 if (i < end && !isNewline(i)) {
660 char_type previous_char = 0;
663 previous_char = text_[i];
666 while (i < end && previous_char != ' ') {
671 previous_char = temp;
681 // returns -1 if inset not found
682 int Paragraph::getPositionOfInset(InsetBase const * inset) const
685 InsetList::const_iterator it = insetlist.begin();
686 InsetList::const_iterator end = insetlist.end();
687 for (; it != end; ++it)
688 if (it->inset == inset)
694 InsetBibitem * Paragraph::bibitem() const
696 if (!insetlist.empty()) {
697 InsetBase * inset = insetlist.begin()->inset;
698 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
699 return static_cast<InsetBibitem *>(inset);
705 bool Paragraph::forceDefaultParagraphs() const
707 return inInset() && inInset()->forceDefaultParagraphs(0);
713 // paragraphs inside floats need different alignment tags to avoid
716 bool noTrivlistCentering(InsetBase::Code code)
718 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
722 string correction(string const & orig)
724 if (orig == "flushleft")
725 return "raggedright";
726 if (orig == "flushright")
728 if (orig == "center")
734 string const corrected_env(string const & suffix, string const & env,
735 InsetBase::Code code)
737 string output = suffix + "{";
738 if (noTrivlistCentering(code))
739 output += correction(env);
743 if (suffix == "\\begin")
749 int adjust_column_count(string const & str, int oldcol)
751 if (!contains(str, "\n"))
752 return oldcol + str.size();
755 return rsplit(str, tmp, '\n').size();
762 // This could go to ParagraphParameters if we want to
763 int Paragraph::startTeXParParams(BufferParams const & bparams,
764 odocstream & os, bool moving_arg) const
768 if (params().noindent()) {
773 switch (params().align()) {
775 case LYX_ALIGN_BLOCK:
776 case LYX_ALIGN_LAYOUT:
777 case LYX_ALIGN_SPECIAL:
780 case LYX_ALIGN_RIGHT:
781 case LYX_ALIGN_CENTER:
789 switch (params().align()) {
791 case LYX_ALIGN_BLOCK:
792 case LYX_ALIGN_LAYOUT:
793 case LYX_ALIGN_SPECIAL:
795 case LYX_ALIGN_LEFT: {
797 if (getParLanguage(bparams)->babel() != "hebrew")
798 output = corrected_env("\\begin", "flushleft", ownerCode());
800 output = corrected_env("\\begin", "flushright", ownerCode());
801 os << from_ascii(output);
802 column = adjust_column_count(output, column);
804 } case LYX_ALIGN_RIGHT: {
806 if (getParLanguage(bparams)->babel() != "hebrew")
807 output = corrected_env("\\begin", "flushright", ownerCode());
809 output = corrected_env("\\begin", "flushleft", ownerCode());
810 os << from_ascii(output);
811 column = adjust_column_count(output, column);
813 } case LYX_ALIGN_CENTER: {
815 output = corrected_env("\\begin", "center", ownerCode());
816 os << from_ascii(output);
817 column = adjust_column_count(output, column);
826 // This could go to ParagraphParameters if we want to
827 int Paragraph::endTeXParParams(BufferParams const & bparams,
828 odocstream & os, bool moving_arg) const
832 switch (params().align()) {
834 case LYX_ALIGN_BLOCK:
835 case LYX_ALIGN_LAYOUT:
836 case LYX_ALIGN_SPECIAL:
839 case LYX_ALIGN_RIGHT:
840 case LYX_ALIGN_CENTER:
848 switch (params().align()) {
850 case LYX_ALIGN_BLOCK:
851 case LYX_ALIGN_LAYOUT:
852 case LYX_ALIGN_SPECIAL:
854 case LYX_ALIGN_LEFT: {
856 if (getParLanguage(bparams)->babel() != "hebrew")
857 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
859 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
860 os << from_ascii(output);
861 column = adjust_column_count(output, column);
863 } case LYX_ALIGN_RIGHT: {
865 if (getParLanguage(bparams)->babel() != "hebrew")
866 output = corrected_env("\n\\par\\end", "flushright", ownerCode());
868 output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
869 os << from_ascii(output);
870 column = adjust_column_count(output, column);
872 } case LYX_ALIGN_CENTER: {
874 output = corrected_env("\n\\par\\end", "center", ownerCode());
875 os << from_ascii(output);
876 column = adjust_column_count(output, column);
885 // This one spits out the text of the paragraph
886 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
887 BufferParams const & bparams,
888 LyXFont const & outerfont,
889 odocstream & os, TexRow & texrow,
890 OutputParams const & runparams) const
892 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
894 bool return_value = false;
898 // well we have to check if we are in an inset with unlimited
899 // length (all in one row) if that is true then we don't allow
900 // any special options in the paragraph and also we don't allow
901 // any environment other than "Standard" to be valid!
902 bool asdefault = forceDefaultParagraphs();
905 style = bparams.getLyXTextClass().defaultLayout();
910 // Current base font for all inherited font changes, without any
911 // change caused by an individual character, except for the language:
912 // It is set to the language of the first character.
913 // As long as we are in the label, this font is the base font of the
914 // label. Before the first body character it is set to the base font
918 LaTeXFeatures features(buf, bparams, runparams);
920 // output change tracking marks only if desired,
921 // if dvipost is installed,
922 // and with dvi/ps (other formats don't work)
923 bool const output = bparams.outputChanges
924 && runparams.flavor == OutputParams::LATEX
925 && features.isAvailable("dvipost");
927 // Maybe we have to create a optional argument.
928 pos_type body_pos = beginOfBody();
929 unsigned int column = 0;
932 // the optional argument is kept in curly brackets in
933 // case it contains a ']'
936 basefont = getLabelFont(bparams, outerfont);
938 basefont = getLayoutFont(bparams, outerfont);
941 // Which font is currently active?
942 LyXFont running_font(basefont);
943 // Do we have an open font change?
944 bool open_font = false;
946 Change::Type runningChangeType = Change::UNCHANGED;
948 texrow.start(id(), 0);
950 // if the paragraph is empty, the loop will not be entered at all
952 if (style->isCommand()) {
957 column += startTeXParParams(bparams, os,
958 runparams.moving_arg);
961 for (pos_type i = 0; i < size(); ++i) {
963 // First char in paragraph or after label?
967 column += running_font.latexWriteEndChanges(os, basefont, basefont);
970 basefont = getLayoutFont(bparams, outerfont);
971 running_font = basefont;
975 if (style->isCommand()) {
981 column += startTeXParParams(bparams, os,
982 runparams.moving_arg);
985 value_type c = getChar(i);
987 // Fully instantiated font
988 LyXFont font = getFont(bparams, i, outerfont);
990 LyXFont const last_font = running_font;
992 // Spaces at end of font change are simulated to be
993 // outside font change, i.e. we write "\textXX{text} "
994 // rather than "\textXX{text }". (Asger)
995 if (open_font && c == ' ' && i <= size() - 2) {
996 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
997 if (next_font != running_font && next_font != font) {
1002 // We end font definition before blanks
1004 (font != running_font ||
1005 font.language() != running_font.language()))
1007 column += running_font.latexWriteEndChanges(os,
1009 (i == body_pos-1) ? basefont : font);
1010 running_font = basefont;
1014 // Blanks are printed before start of fontswitch
1016 // Do not print the separation of the optional argument
1017 if (i != body_pos - 1) {
1018 pimpl_->simpleTeXBlanks(os, texrow, i,
1019 column, font, *style);
1023 // Do we need to change font?
1024 if ((font != running_font ||
1025 font.language() != running_font.language()) &&
1028 column += font.latexWriteStartChanges(os, basefont,
1030 running_font = font;
1034 Change::Type changeType = pimpl_->lookupChange(i).type;
1036 column += Changes::latexMarkChange(os, runningChangeType,
1037 changeType, output);
1038 runningChangeType = changeType;
1040 // do not output text which is marked deleted
1041 // if change tracking output is not desired
1042 if (output || runningChangeType != Change::DELETED) {
1043 OutputParams rp = runparams;
1044 rp.free_spacing = style->free_spacing;
1045 rp.local_font = &font;
1046 rp.intitle = style->intitle;
1047 pimpl_->simpleTeXSpecialChars(buf, bparams,
1050 basefont, outerfont, open_font,
1052 *style, i, column, c);
1056 column += Changes::latexMarkChange(os,
1057 runningChangeType, Change::UNCHANGED, output);
1059 // If we have an open font definition, we have to close it
1061 #ifdef FIXED_LANGUAGE_END_DETECTION
1064 .latexWriteEndChanges(os, basefont,
1065 next_->getFont(bparams,
1068 running_font.latexWriteEndChanges(os, basefont,
1072 #ifdef WITH_WARNINGS
1073 //#warning For now we ALWAYS have to close the foreign font settings if they are
1074 //#warning there as we start another \selectlanguage with the next paragraph if
1075 //#warning we are in need of this. This should be fixed sometime (Jug)
1077 running_font.latexWriteEndChanges(os, basefont, basefont);
1081 // Needed if there is an optional argument but no contents.
1082 if (body_pos > 0 && body_pos == size()) {
1084 return_value = false;
1088 column += endTeXParParams(bparams, os, runparams.moving_arg);
1091 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1092 return return_value;
1098 // checks, if newcol chars should be put into this line
1099 // writes newline, if necessary.
1100 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1101 string::size_type newcol)
1104 if (colcount > lyxrc.ascii_linelen) {
1106 colcount = newcol; // assume write after this call
1121 string tag_name(PAR_TAG const & pt) {
1123 case PAR_NONE: return "!-- --";
1124 case TT: return "tt";
1125 case SF: return "sf";
1126 case BF: return "bf";
1127 case IT: return "it";
1128 case SL: return "sl";
1129 case EM: return "em";
1136 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1138 p1 = static_cast<PAR_TAG>(p1 | p2);
1143 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1145 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1151 bool Paragraph::emptyTag() const
1153 for (pos_type i = 0; i < size(); ++i) {
1155 InsetBase const * inset = getInset(i);
1156 InsetBase::Code lyx_code = inset->lyxCode();
1157 if (lyx_code != InsetBase::TOC_CODE &&
1158 lyx_code != InsetBase::INCLUDE_CODE &&
1159 lyx_code != InsetBase::GRAPHICS_CODE &&
1160 lyx_code != InsetBase::ERT_CODE &&
1161 lyx_code != InsetBase::FLOAT_CODE &&
1162 lyx_code != InsetBase::TABULAR_CODE) {
1166 value_type c = getChar(i);
1167 if (c != ' ' && c != '\t')
1175 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1177 for (pos_type i = 0; i < size(); ++i) {
1179 InsetBase const * inset = getInset(i);
1180 InsetBase::Code lyx_code = inset->lyxCode();
1181 if (lyx_code == InsetBase::LABEL_CODE) {
1182 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1183 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
1192 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1195 for (i = 0; i < size(); ++i) {
1197 InsetBase const * inset = getInset(i);
1198 inset->docbook(buf, os, runparams);
1200 value_type c = getChar(i);
1203 os << sgml::escapeChar(c);
1210 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1214 for (pos_type i = initial; i < size(); ++i) {
1215 LyXFont font = getFont(buf.params(), i, outerfont);
1218 if (i != initial && font != font_old)
1227 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1229 OutputParams const & runparams,
1230 LyXFont const & outerfont,
1231 pos_type initial) const
1233 bool emph_flag = false;
1235 LyXLayout_ptr const & style = layout();
1237 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1239 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1242 // parsing main loop
1243 for (pos_type i = initial; i < size(); ++i) {
1244 LyXFont font = getFont(buf.params(), i, outerfont);
1246 // handle <emphasis> tag
1247 if (font_old.emph() != font.emph()) {
1248 if (font.emph() == LyXFont::ON) {
1251 } else if (i != initial) {
1252 os << "</emphasis>";
1258 InsetBase const * inset = getInset(i);
1259 inset->docbook(buf, os, runparams);
1261 value_type c = getChar(i);
1263 if (style->pass_thru)
1266 os << sgml::escapeChar(c);
1272 os << "</emphasis>";
1275 if (style->free_spacing)
1277 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1282 bool Paragraph::isNewline(pos_type pos) const
1285 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1289 bool Paragraph::isLineSeparator(pos_type pos) const
1291 value_type const c = getChar(pos);
1292 return isLineSeparatorChar(c)
1293 || (c == Paragraph::META_INSET && getInset(pos) &&
1294 getInset(pos)->isLineSeparator());
1298 /// Used by the spellchecker
1299 bool Paragraph::isLetter(pos_type pos) const
1302 return getInset(pos)->isLetter();
1304 value_type const c = getChar(pos);
1305 return isLetterChar(c) || isDigit(c);
1311 Paragraph::getParLanguage(BufferParams const & bparams) const
1314 return getFirstFontSettings(bparams).language();
1315 #ifdef WITH_WARNINGS
1316 #warning FIXME we should check the prev par as well (Lgb)
1318 return bparams.language;
1322 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1324 return lyxrc.rtl_support
1325 && getParLanguage(bparams)->rightToLeft()
1326 && ownerCode() != InsetBase::ERT_CODE;
1330 void Paragraph::changeLanguage(BufferParams const & bparams,
1331 Language const * from, Language const * to)
1333 // change language including dummy font change at the end
1334 for (pos_type i = 0; i <= size(); ++i) {
1335 LyXFont font = getFontSettings(bparams, i);
1336 if (font.language() == from) {
1337 font.setLanguage(to);
1344 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1346 Language const * doc_language = bparams.language;
1347 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1348 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1350 for (; cit != end; ++cit)
1351 if (cit->font().language() != ignore_language &&
1352 cit->font().language() != latex_language &&
1353 cit->font().language() != doc_language)
1359 // Convert the paragraph to a string.
1360 // Used for building the table of contents
1361 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1363 OutputParams runparams;
1364 return asString(buffer, runparams, label);
1368 docstring const Paragraph::asString(Buffer const & buffer,
1369 OutputParams const & runparams,
1374 if (label && !params().labelString().empty())
1375 s += params().labelString() + ' ';
1377 for (pos_type i = 0; i < size(); ++i) {
1378 value_type c = getChar(i);
1381 else if (c == META_INSET &&
1382 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1384 getInset(i)->plaintext(buffer, os, runparams);
1385 s += subst(STRCONV(os.str()),'\n',' ');
1391 // This should really be done by the caller and not here.
1392 docstring ret = asString(buffer, runparams, 0, size(), label);
1393 return subst(ret, '\n', ' ');
1398 docstring const Paragraph::asString(Buffer const & buffer,
1399 pos_type beg, pos_type end, bool label) const
1402 OutputParams const runparams;
1403 return asString(buffer, runparams, beg, end, label);
1407 docstring const Paragraph::asString(Buffer const & buffer,
1408 OutputParams const & runparams,
1409 pos_type beg, pos_type end, bool label) const
1411 lyx::odocstringstream os;
1413 if (beg == 0 && label && !params().labelString().empty())
1414 os << params().labelString() << ' ';
1416 for (pos_type i = beg; i < end; ++i) {
1417 value_type const c = getUChar(buffer.params(), i);
1420 else if (c == META_INSET)
1421 getInset(i)->textString(buffer, os, runparams);
1428 void Paragraph::setInsetOwner(InsetBase * inset)
1430 pimpl_->inset_owner = inset;
1434 Change const & Paragraph::lookupChange(pos_type pos) const
1436 BOOST_ASSERT(pos <= size());
1437 return pimpl_->lookupChange(pos);
1441 bool Paragraph::isChanged(pos_type start, pos_type end) const
1443 return pimpl_->isChanged(start, end);
1447 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
1449 return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
1453 void Paragraph::setChange(Change const & change)
1455 pimpl_->setChange(change);
1459 void Paragraph::setChange(pos_type pos, Change const & change)
1461 pimpl_->setChange(pos, change);
1465 void Paragraph::acceptChanges(pos_type start, pos_type end)
1467 return pimpl_->acceptChanges(start, end);
1471 void Paragraph::rejectChanges(pos_type start, pos_type end)
1473 return pimpl_->rejectChanges(start, end);
1477 int Paragraph::id() const
1483 LyXLayout_ptr const & Paragraph::layout() const
1489 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1491 layout_ = new_layout;
1495 InsetBase * Paragraph::inInset() const
1497 return pimpl_->inset_owner;
1501 InsetBase::Code Paragraph::ownerCode() const
1503 return pimpl_->inset_owner
1504 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1508 void Paragraph::clearContents()
1514 ParagraphParameters & Paragraph::params()
1516 return pimpl_->params;
1520 ParagraphParameters const & Paragraph::params() const
1522 return pimpl_->params;
1526 bool Paragraph::isFreeSpacing() const
1528 if (layout()->free_spacing)
1531 // for now we just need this, later should we need this in some
1532 // other way we can always add a function to InsetBase too.
1533 return ownerCode() == InsetBase::ERT_CODE;
1537 bool Paragraph::allowEmpty() const
1539 if (layout()->keepempty)
1541 return ownerCode() == InsetBase::ERT_CODE;
1545 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1547 if (!Encodings::is_arabic(c))
1548 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1549 // FIXME UNICODE What does this do?
1550 return c + (0xb0 - '0');
1554 value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1555 value_type next_char = ' ';
1557 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1558 value_type const par_char = getChar(i);
1559 if (!Encodings::isComposeChar_arabic(par_char)) {
1560 next_char = par_char;
1565 if (Encodings::is_arabic(next_char)) {
1566 if (Encodings::is_arabic(prev_char) &&
1567 !Encodings::is_arabic_special(prev_char))
1568 return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1570 return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1572 if (Encodings::is_arabic(prev_char) &&
1573 !Encodings::is_arabic_special(prev_char))
1574 return Encodings::transformChar(c, Encodings::FORM_FINAL);
1576 return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1581 bool Paragraph::hfillExpansion(Row const & row, pos_type pos) const
1586 BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
1588 // expand at the end of a row only if there is another hfill on the same row
1589 if (pos == row.endpos() - 1) {
1590 for (pos_type i = row.pos(); i < pos; i++) {
1597 // expand at the beginning of a row only if it is the first row of a paragraph
1598 if (pos == row.pos()) {
1602 // do not expand in some labels
1603 if (layout()->margintype != MARGIN_MANUAL && pos < beginOfBody())
1606 // if there is anything between the first char of the row and
1607 // the specified position that is neither a newline nor an hfill,
1608 // the hfill will be expanded, otherwise it won't
1609 for (pos_type i = row.pos(); i < pos; i++) {
1610 if (!isNewline(i) && !isHfill(i))
1617 bool Paragraph::checkBiblio(bool track_changes)
1619 // Add bibitem insets if necessary
1620 if (layout()->labeltype != LABEL_BIBLIO)
1623 bool hasbibitem = !insetlist.empty()
1624 // Insist on it being in pos 0
1625 && getChar(0) == Paragraph::META_INSET
1626 && insetlist.begin()->inset->lyxCode() == InsetBase::BIBITEM_CODE;
1631 InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem")));
1632 insertInset(0, static_cast<InsetBase *>(inset),
1633 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));