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"
41 #include "insets/insetbibitem.h"
42 #include "insets/insetoptarg.h"
44 #include "support/lstrings.h"
45 #include "support/textutils.h"
46 #include "support/convert.h"
48 #include <boost/tuple/tuple.hpp>
49 #include <boost/bind.hpp>
57 using lyx::support::subst;
65 using std::ostringstream;
68 ParagraphList::ParagraphList()
72 Paragraph::Paragraph()
73 : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
80 Paragraph::Paragraph(Paragraph const & par)
81 : itemdepth(par.itemdepth), insetlist(par.insetlist),
83 rows_(par.rows_), layout_(par.layout_),
84 text_(par.text_), begin_of_body_(par.begin_of_body_),
85 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
87 //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
88 InsetList::iterator it = insetlist.begin();
89 InsetList::iterator end = insetlist.end();
90 for (; it != end; ++it)
91 it->inset = it->inset->clone().release();
95 Paragraph & Paragraph::operator=(Paragraph const & par)
97 // needed as we will destroy the pimpl_ before copying it
99 itemdepth = par.itemdepth;
101 insetlist = par.insetlist;
102 InsetList::iterator it = insetlist.begin();
103 InsetList::iterator end = insetlist.end();
104 for (; it != end; ++it)
105 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);
156 lyx::time_type const curtime(lyx::current_time());
159 for (pos_type i = 0; i < size(); ++i) {
161 Change change = pimpl_->lookupChangeFull(i);
162 Changes::lyxMarkChange(os, column, curtime, running_change, change);
163 running_change = change;
165 // Write font changes
166 LyXFont font2 = getFontSettings(bparams, i);
167 if (font2 != font1) {
168 font2.lyxWriteChanges(font1, os);
173 value_type const c = getChar(i);
177 InsetBase const * inset = getInset(i);
179 if (inset->directWrite()) {
180 // international char, let it write
181 // code directly so it's shorter in
183 inset->write(buf, os);
187 os << "\\begin_inset ";
188 inset->write(buf, os);
189 os << "\n\\end_inset\n\n";
195 os << "\n\\backslash\n";
199 if (i + 1 < size() && getChar(i + 1) == ' ') {
206 if ((column > 70 && c == ' ')
211 // this check is to amend a bug. LyX sometimes
212 // inserts '\0' this could cause problems.
216 lyxerr << "ERROR (Paragraph::writeFile):"
217 " NULL char in structure." << endl;
223 // to make reading work properly
225 running_change = pimpl_->lookupChange(0);
226 Changes::lyxMarkChange(os, column, curtime,
227 Change(Change::UNCHANGED), running_change);
229 Changes::lyxMarkChange(os, column, curtime,
230 running_change, Change(Change::UNCHANGED));
232 os << "\n\\end_layout\n";
236 void Paragraph::validate(LaTeXFeatures & features) const
238 pimpl_->validate(features, *layout());
242 void Paragraph::eraseIntern(lyx::pos_type pos)
244 pimpl_->eraseIntern(pos);
248 bool Paragraph::erase(pos_type pos)
250 return pimpl_->erase(pos);
254 int Paragraph::erase(pos_type start, pos_type end)
256 return pimpl_->erase(start, end);
260 void Paragraph::insert(pos_type start, string const & str,
261 LyXFont const & font)
263 int size = str.size();
264 for (int i = 0 ; i < size ; ++i)
265 insertChar(start + i, str[i], font);
269 bool Paragraph::checkInsertChar(LyXFont &)
275 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
278 pimpl_->insertChar(pos, c, change);
282 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
283 LyXFont const & font, Change change)
285 pimpl_->insertChar(pos, c, change);
290 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change change)
292 pimpl_->insertInset(pos, inset, change);
296 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
297 LyXFont const & font, Change change)
299 pimpl_->insertInset(pos, inset, change);
304 bool Paragraph::insetAllowed(InsetBase_code code)
306 return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
310 InsetBase * Paragraph::getInset(pos_type pos)
312 BOOST_ASSERT(pos < size());
313 return insetlist.get(pos);
317 InsetBase const * Paragraph::getInset(pos_type pos) const
319 BOOST_ASSERT(pos < size());
320 return insetlist.get(pos);
324 // Gets uninstantiated font setting at position.
325 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
329 lyxerr << " pos: " << pos << " size: " << size() << endl;
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)
342 if (pos == size() && !empty())
343 return getFontSettings(bparams, pos - 1);
345 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
349 std::pair<lyx::pos_type, lyx::pos_type> Paragraph::getFontSpan(lyx::pos_type pos) const
351 BOOST_ASSERT(pos <= size());
352 lyx::pos_type start = 0;
354 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
355 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
356 for (; cit != end; ++cit) {
357 if (cit->pos() >= pos)
358 return std::make_pair(start, cit->pos());
359 start = cit->pos() + 1;
362 // This should not happen, but if so, we take no chances.
363 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
365 return std::make_pair(pos, pos);
369 // Gets uninstantiated font setting at position 0
370 LyXFont const Paragraph::getFirstFontSettings() const
372 if (!empty() && !pimpl_->fontlist.empty())
373 return pimpl_->fontlist[0].font();
375 return LyXFont(LyXFont::ALL_INHERIT);
379 // Gets the fully instantiated font at a given position in a paragraph
380 // This is basically the same function as LyXText::GetFont() in text2.C.
381 // The difference is that this one is used for generating the LaTeX file,
382 // and thus cosmetic "improvements" are disallowed: This has to deliver
383 // the true picture of the buffer. (Asger)
384 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
385 LyXFont const & outerfont) const
387 BOOST_ASSERT(pos >= 0);
389 LyXLayout_ptr const & lout = layout();
391 pos_type const body_pos = beginOfBody();
395 layoutfont = lout->labelfont;
397 layoutfont = lout->font;
399 LyXFont font = getFontSettings(bparams, pos);
400 font.realize(layoutfont);
401 font.realize(outerfont);
402 font.realize(bparams.getLyXTextClass().defaultfont());
408 LyXFont const Paragraph::getLabelFont
409 (BufferParams const & bparams, LyXFont const & outerfont) const
411 LyXFont tmpfont = layout()->labelfont;
412 tmpfont.setLanguage(getParLanguage(bparams));
413 tmpfont.realize(outerfont);
414 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
419 LyXFont const Paragraph::getLayoutFont
420 (BufferParams const & bparams, LyXFont const & outerfont) const
422 LyXFont tmpfont = layout()->font;
423 tmpfont.setLanguage(getParLanguage(bparams));
424 tmpfont.realize(outerfont);
425 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
430 /// Returns the height of the highest font in range
431 LyXFont_size Paragraph::highestFontInRange
432 (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
434 if (pimpl_->fontlist.empty())
437 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
438 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
439 for (; end_it != end; ++end_it) {
440 if (end_it->pos() >= endpos)
447 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
448 for (; cit != end; ++cit) {
449 if (cit->pos() >= startpos)
453 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
454 for (; cit != end_it; ++cit) {
455 LyXFont::FONT_SIZE size = cit->font().size();
456 if (size == LyXFont::INHERIT_SIZE)
458 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
465 Paragraph::value_type
466 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
468 value_type c = getChar(pos);
469 if (!lyxrc.rtl_support)
499 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
506 void Paragraph::setFont(pos_type pos, LyXFont const & font)
508 BOOST_ASSERT(pos <= size());
510 // First, reduce font against layout/label font
511 // Update: The setCharFont() routine in text2.C already
512 // reduces font, so we don't need to do that here. (Asger)
513 // No need to simplify this because it will disappear
514 // in a new kernel. (Asger)
515 // Next search font table
517 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
518 Pimpl::FontList::iterator it = beg;
519 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
520 for (; it != endit; ++it) {
521 if (it->pos() >= pos)
524 unsigned int i = distance(beg, it);
525 bool notfound = (it == endit);
527 if (!notfound && pimpl_->fontlist[i].font() == font)
530 bool begin = pos == 0 || notfound ||
531 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
532 // Is position pos is a beginning of a font block?
533 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
534 // Is position pos is the end of a font block?
535 if (begin && end) { // A single char block
536 if (i + 1 < pimpl_->fontlist.size() &&
537 pimpl_->fontlist[i + 1].font() == font) {
538 // Merge the singleton block with the next block
539 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
540 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
541 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
542 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
543 // Merge the singleton block with the previous block
544 pimpl_->fontlist[i - 1].pos(pos);
545 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
547 pimpl_->fontlist[i].font(font);
549 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
550 pimpl_->fontlist[i - 1].pos(pos);
552 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
553 Pimpl::FontTable(pos, font));
555 pimpl_->fontlist[i].pos(pos - 1);
556 if (!(i + 1 < pimpl_->fontlist.size() &&
557 pimpl_->fontlist[i + 1].font() == font))
558 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
559 Pimpl::FontTable(pos, font));
560 } else { // The general case. The block is splitted into 3 blocks
561 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
562 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
563 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
564 Pimpl::FontTable(pos, font));
569 void Paragraph::makeSameLayout(Paragraph const & par)
571 layout(par.layout());
573 params() = par.params();
577 int Paragraph::stripLeadingSpaces()
583 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
584 // Set Change::Type to Change::INSERTED to quietly remove it
585 setChange(0, Change::INSERTED);
594 bool Paragraph::hasSameLayout(Paragraph const & par) const
596 return par.layout() == layout() && params().sameLayout(par.params());
600 Paragraph::depth_type Paragraph::getDepth() const
602 return params().depth();
606 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
608 if (layout()->isEnvironment())
609 return params().depth() + 1;
611 return params().depth();
615 char Paragraph::getAlign() const
617 return params().align();
621 string const & Paragraph::getLabelstring() const
623 return params().labelString();
627 // the next two functions are for the manual labels
628 string const Paragraph::getLabelWidthString() const
630 if (!params().labelWidthString().empty())
631 return params().labelWidthString();
633 return _("Senseless with this layout!");
637 void Paragraph::setLabelWidthString(string const & s)
639 params().labelWidthString(s);
643 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
646 params().labelWidthString(string());
647 params().align(LYX_ALIGN_LAYOUT);
648 params().spacing(Spacing(Spacing::Default));
652 pos_type Paragraph::beginOfBody() const
654 return begin_of_body_;
658 void Paragraph::setBeginOfBody()
660 if (layout()->labeltype != LABEL_MANUAL) {
665 // Unroll the first two cycles of the loop
666 // and remember the previous character to
667 // remove unnecessary getChar() calls
669 pos_type end = size();
670 if (i < end && !isNewline(i)) {
672 char previous_char = 0;
675 previous_char = text_[i];
678 while (i < end && previous_char != ' ') {
683 previous_char = temp;
693 // returns -1 if inset not found
694 int Paragraph::getPositionOfInset(InsetBase const * inset) const
697 InsetList::const_iterator it = insetlist.begin();
698 InsetList::const_iterator end = insetlist.end();
699 for (; it != end; ++it)
700 if (it->inset == inset)
706 InsetBibitem * Paragraph::bibitem() const
708 if (!insetlist.empty()) {
709 InsetBase * inset = insetlist.begin()->inset;
710 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
711 return static_cast<InsetBibitem *>(inset);
717 bool Paragraph::forceDefaultParagraphs() const
719 return inInset() && inInset()->forceDefaultParagraphs(inInset());
725 // paragraphs inside floats need different alignment tags to avoid
728 bool noTrivlistCentering(InsetBase::Code code)
730 return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
734 string correction(string const & orig)
736 if (orig == "flushleft")
737 return "raggedright";
738 if (orig == "flushright")
740 if (orig == "center")
746 string const corrected_env(string const & suffix, string const & env,
747 InsetBase::Code code)
749 string output = suffix + "{";
750 if (noTrivlistCentering(code))
751 output += correction(env);
760 // This could go to ParagraphParameters if we want to
761 int Paragraph::startTeXParParams(BufferParams const & bparams,
762 ostream & os, bool moving_arg) const
766 if (params().noindent()) {
771 switch (params().align()) {
773 case LYX_ALIGN_BLOCK:
774 case LYX_ALIGN_LAYOUT:
775 case LYX_ALIGN_SPECIAL:
778 case LYX_ALIGN_RIGHT:
779 case LYX_ALIGN_CENTER:
787 switch (params().align()) {
789 case LYX_ALIGN_BLOCK:
790 case LYX_ALIGN_LAYOUT:
791 case LYX_ALIGN_SPECIAL:
793 case LYX_ALIGN_LEFT: {
795 if (getParLanguage(bparams)->babel() != "hebrew")
796 output = corrected_env("\\begin", "flushleft", ownerCode());
798 output = corrected_env("\\begin", "flushright", ownerCode());
800 column += output.size();
802 } case LYX_ALIGN_RIGHT: {
804 if (getParLanguage(bparams)->babel() != "hebrew")
805 output = corrected_env("\\begin", "flushright", ownerCode());
807 output = corrected_env("\\begin", "flushleft", ownerCode());
809 column += output.size();
811 } case LYX_ALIGN_CENTER: {
813 output = corrected_env("\\begin", "center", ownerCode());
815 column += output.size();
824 // This could go to ParagraphParameters if we want to
825 int Paragraph::endTeXParParams(BufferParams const & bparams,
826 ostream & os, bool moving_arg) const
830 switch (params().align()) {
832 case LYX_ALIGN_BLOCK:
833 case LYX_ALIGN_LAYOUT:
834 case LYX_ALIGN_SPECIAL:
837 case LYX_ALIGN_RIGHT:
838 case LYX_ALIGN_CENTER:
846 switch (params().align()) {
848 case LYX_ALIGN_BLOCK:
849 case LYX_ALIGN_LAYOUT:
850 case LYX_ALIGN_SPECIAL:
852 case LYX_ALIGN_LEFT: {
854 if (getParLanguage(bparams)->babel() != "hebrew")
855 output = corrected_env("\\par\\end", "flushleft", ownerCode());
857 output = corrected_env("\\par\\end", "flushright", ownerCode());
859 column += output.size();
861 } case LYX_ALIGN_RIGHT: {
863 if (getParLanguage(bparams)->babel() != "hebrew")
864 output = corrected_env("\\par\\end", "flushright", ownerCode());
866 output = corrected_env("\\par\\end", "flushleft", ownerCode());
868 column += output.size();
870 } case LYX_ALIGN_CENTER: {
872 output = corrected_env("\\par\\end", "center", ownerCode());
874 column += output.size();
883 // This one spits out the text of the paragraph
884 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
885 BufferParams const & bparams,
886 LyXFont const & outerfont,
887 ostream & os, TexRow & texrow,
888 OutputParams const & runparams) const
890 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
892 bool return_value = false;
896 // well we have to check if we are in an inset with unlimited
897 // length (all in one row) if that is true then we don't allow
898 // any special options in the paragraph and also we don't allow
899 // any environment other then "Standard" to be valid!
900 bool asdefault = forceDefaultParagraphs();
903 style = bparams.getLyXTextClass().defaultLayout();
910 LaTeXFeatures features(buf, bparams, runparams.nice);
912 // output change tracking marks only if desired,
913 // if dvipost is installed,
914 // and with dvi/ps (other formats don't work)
915 bool const output = bparams.output_changes
916 && runparams.flavor == OutputParams::LATEX
917 && features.isAvailable("dvipost");
919 // Maybe we have to create a optional argument.
920 pos_type body_pos = beginOfBody();
921 unsigned int column = 0;
924 // the optional argument is kept in curly brackets in
925 // case it contains a ']'
928 basefont = getLabelFont(bparams, outerfont);
930 basefont = getLayoutFont(bparams, outerfont);
933 // Which font is currently active?
934 LyXFont running_font(basefont);
935 // Do we have an open font change?
936 bool open_font = false;
938 Change::Type running_change = Change::UNCHANGED;
940 texrow.start(id(), 0);
942 // if the paragraph is empty, the loop will not be entered at all
944 if (style->isCommand()) {
949 column += startTeXParParams(bparams, os,
950 runparams.moving_arg);
953 for (pos_type i = 0; i < size(); ++i) {
955 // First char in paragraph or after label?
959 column += running_font.latexWriteEndChanges(os, basefont, basefont);
962 basefont = getLayoutFont(bparams, outerfont);
963 running_font = basefont;
967 if (style->isCommand()) {
973 column += startTeXParParams(bparams, os,
974 runparams.moving_arg);
977 value_type c = getChar(i);
979 // Fully instantiated font
980 LyXFont font = getFont(bparams, i, outerfont);
982 LyXFont const last_font = running_font;
984 // Spaces at end of font change are simulated to be
985 // outside font change, i.e. we write "\textXX{text} "
986 // rather than "\textXX{text }". (Asger)
987 if (open_font && c == ' ' && i <= size() - 2) {
988 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
989 if (next_font != running_font && next_font != font) {
994 // We end font definition before blanks
996 (font != running_font ||
997 font.language() != running_font.language()))
999 column += running_font.latexWriteEndChanges(os,
1001 (i == body_pos-1) ? basefont : font);
1002 running_font = basefont;
1006 // Blanks are printed before start of fontswitch
1008 // Do not print the separation of the optional argument
1009 if (i != body_pos - 1) {
1010 pimpl_->simpleTeXBlanks(os, texrow, i,
1011 column, font, *style);
1015 // Do we need to change font?
1016 if ((font != running_font ||
1017 font.language() != running_font.language()) &&
1020 column += font.latexWriteStartChanges(os, basefont,
1022 running_font = font;
1026 Change::Type change = pimpl_->lookupChange(i);
1028 column += Changes::latexMarkChange(os, running_change,
1030 running_change = change;
1032 // do not output text which is marked deleted
1033 // if change tracking output is not desired
1034 if (output || running_change != Change::DELETED) {
1035 OutputParams rp = runparams;
1036 rp.free_spacing = style->free_spacing;
1037 rp.local_language = font.language();
1038 rp.intitle = style->intitle;
1039 pimpl_->simpleTeXSpecialChars(buf, bparams,
1042 basefont, outerfont, open_font,
1044 *style, i, column, c);
1048 column += Changes::latexMarkChange(os,
1049 running_change, Change::UNCHANGED, output);
1051 // If we have an open font definition, we have to close it
1053 #ifdef FIXED_LANGUAGE_END_DETECTION
1056 .latexWriteEndChanges(os, basefont,
1057 next_->getFont(bparams,
1060 running_font.latexWriteEndChanges(os, basefont,
1064 #ifdef WITH_WARNINGS
1065 //#warning For now we ALWAYS have to close the foreign font settings if they are
1066 //#warning there as we start another \selectlanguage with the next paragraph if
1067 //#warning we are in need of this. This should be fixed sometime (Jug)
1069 running_font.latexWriteEndChanges(os, basefont, basefont);
1073 // Needed if there is an optional argument but no contents.
1074 if (body_pos > 0 && body_pos == size()) {
1076 return_value = false;
1080 column += endTeXParParams(bparams, os, runparams.moving_arg);
1083 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1084 return return_value;
1090 // checks, if newcol chars should be put into this line
1091 // writes newline, if necessary.
1092 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1093 string::size_type newcol)
1096 if (colcount > lyxrc.ascii_linelen) {
1098 colcount = newcol; // assume write after this call
1113 string tag_name(PAR_TAG const & pt) {
1115 case PAR_NONE: return "!-- --";
1116 case TT: return "tt";
1117 case SF: return "sf";
1118 case BF: return "bf";
1119 case IT: return "it";
1120 case SL: return "sl";
1121 case EM: return "em";
1128 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1130 p1 = static_cast<PAR_TAG>(p1 | p2);
1135 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1137 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1143 // Handle internal paragraph parsing -- layout already processed.
1144 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1146 LyXFont const & outerfont,
1147 OutputParams const & runparams,
1148 lyx::depth_type /*depth*/) const
1150 LyXLayout_ptr const & style = layout();
1152 string::size_type char_line_count = 5; // Heuristic choice ;-)
1154 // gets paragraph main font
1157 if (style->labeltype == LABEL_MANUAL) {
1158 font_old = style->labelfont;
1161 font_old = style->font;
1165 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1166 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1167 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1170 stack<PAR_TAG> tag_state;
1171 // parsing main loop
1172 for (pos_type i = 0; i < size(); ++i) {
1174 PAR_TAG tag_close = PAR_NONE;
1175 list < PAR_TAG > tag_open;
1177 LyXFont const font = getFont(buf.params(), i, outerfont);
1179 if (font_old.family() != font.family()) {
1180 switch (family_type) {
1181 case LyXFont::SANS_FAMILY:
1184 case LyXFont::TYPEWRITER_FAMILY:
1191 family_type = font.family();
1193 switch (family_type) {
1194 case LyXFont::SANS_FAMILY:
1195 tag_open.push_back(SF);
1197 case LyXFont::TYPEWRITER_FAMILY:
1198 tag_open.push_back(TT);
1205 if (font_old.series() != font.series()) {
1206 switch (series_type) {
1207 case LyXFont::BOLD_SERIES:
1214 series_type = font.series();
1216 switch (series_type) {
1217 case LyXFont::BOLD_SERIES:
1218 tag_open.push_back(BF);
1226 if (font_old.shape() != font.shape()) {
1227 switch (shape_type) {
1228 case LyXFont::ITALIC_SHAPE:
1231 case LyXFont::SLANTED_SHAPE:
1238 shape_type = font.shape();
1240 switch (shape_type) {
1241 case LyXFont::ITALIC_SHAPE:
1242 tag_open.push_back(IT);
1244 case LyXFont::SLANTED_SHAPE:
1245 tag_open.push_back(SL);
1252 if (font_old.emph() != font.emph()) {
1253 if (font.emph() == LyXFont::ON) {
1254 tag_open.push_back(EM);
1263 list < PAR_TAG > temp;
1264 while (!tag_state.empty() && tag_close) {
1265 PAR_TAG k = tag_state.top();
1267 os << "</" << tag_name(k) << '>';
1274 for(list< PAR_TAG >::const_iterator j = temp.begin();
1275 j != temp.end(); ++j) {
1277 os << '<' << tag_name(*j) << '>';
1280 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1281 j != tag_open.end(); ++j) {
1283 os << '<' << tag_name(*j) << '>';
1286 value_type c = getChar(i);
1289 if (c == Paragraph::META_INSET) {
1290 getInset(i)->linuxdoc(buf, os, runparams);
1295 if (style->latexparam() == "CDATA") {
1296 // "TeX"-Mode on == > SGML-Mode on.
1303 boost::tie(ws, str) = sgml::escapeChar(c);
1304 if (ws && !isFreeSpacing()) {
1305 // in freespacing mode, spaces are
1306 // non-breaking characters
1307 if (desc_on) { // if char is ' ' then...
1309 sgmlLineBreak(os, char_line_count, 6);
1313 sgmlLineBreak(os, char_line_count, 1);
1318 char_line_count += str.length();
1324 while (!tag_state.empty()) {
1325 os << "</" << tag_name(tag_state.top()) << '>';
1329 // resets description flag correctly
1331 // <tag> not closed...
1332 sgmlLineBreak(os, char_line_count, 6);
1338 bool Paragraph::emptyTag() const
1340 for (pos_type i = 0; i < size(); ++i) {
1342 InsetBase const * inset = getInset(i);
1343 InsetBase::Code lyx_code = inset->lyxCode();
1344 if (lyx_code != InsetBase::TOC_CODE &&
1345 lyx_code != InsetBase::INCLUDE_CODE &&
1346 lyx_code != InsetBase::GRAPHICS_CODE &&
1347 lyx_code != InsetBase::ERT_CODE &&
1348 lyx_code != InsetBase::FLOAT_CODE &&
1349 lyx_code != InsetBase::TABULAR_CODE) {
1353 value_type c = getChar(i);
1354 if (c != ' ' && c != '\t')
1362 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1364 for (pos_type i = 0; i < size(); ++i) {
1366 InsetBase const * inset = getInset(i);
1367 InsetBase::Code lyx_code = inset->lyxCode();
1368 if (lyx_code == InsetBase::LABEL_CODE) {
1369 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1370 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1379 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1382 for (i = 0; i < size(); ++i) {
1384 InsetBase const * inset = getInset(i);
1385 inset->docbook(buf, os, runparams);
1387 value_type c = getChar(i);
1392 boost::tie(ws, str) = sgml::escapeChar(c);
1401 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1405 for (pos_type i = initial; i < size(); ++i) {
1406 LyXFont font = getFont(buf.params(), i, outerfont);
1409 if (i != initial && font != font_old)
1418 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1420 OutputParams const & runparams,
1421 LyXFont const & outerfont,
1422 pos_type initial) const
1424 bool emph_flag = false;
1426 LyXLayout_ptr const & style = layout();
1428 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1430 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1433 // parsing main loop
1434 for (pos_type i = initial; i < size(); ++i) {
1435 LyXFont font = getFont(buf.params(), i, outerfont);
1437 // handle <emphasis> tag
1438 if (font_old.emph() != font.emph()) {
1439 if (font.emph() == LyXFont::ON) {
1442 } else if (i != initial) {
1443 os << "</emphasis>";
1449 InsetBase const * inset = getInset(i);
1450 inset->docbook(buf, os, runparams);
1452 value_type c = getChar(i);
1455 boost::tie(ws, str) = sgml::escapeChar(c);
1457 if (style->pass_thru)
1466 os << "</emphasis>";
1469 if (style->free_spacing)
1471 if (style->pass_thru && !onlyText(buf, outerfont, initial))
1478 /// return true if the char is a meta-character for an inset
1480 bool IsInsetChar(char c)
1482 return (c == Paragraph::META_INSET);
1489 bool Paragraph::isHfill(pos_type pos) const
1492 && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1496 bool Paragraph::isNewline(pos_type pos) const
1499 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1503 bool Paragraph::isSeparator(pos_type pos) const
1505 return IsSeparatorChar(getChar(pos));
1509 bool Paragraph::isLineSeparator(pos_type pos) const
1511 value_type const c = getChar(pos);
1512 return IsLineSeparatorChar(c)
1513 || (IsInsetChar(c) && getInset(pos) &&
1514 getInset(pos)->isLineSeparator());
1518 /// Used by the spellchecker
1519 bool Paragraph::isLetter(pos_type pos) const
1522 return getInset(pos)->isLetter();
1524 value_type const c = getChar(pos);
1525 return IsLetterChar(c) || IsDigit(c);
1531 Paragraph::getParLanguage(BufferParams const & bparams) const
1534 return getFirstFontSettings().language();
1535 #ifdef WITH_WARNINGS
1536 #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 && ownerCode() != InsetBase::ERT_CODE;
1550 void Paragraph::changeLanguage(BufferParams const & bparams,
1551 Language const * from, Language const * to)
1553 for (pos_type i = 0; i < size(); ++i) {
1554 LyXFont font = getFontSettings(bparams, i);
1555 if (font.language() == from) {
1556 font.setLanguage(to);
1563 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1565 Language const * doc_language = bparams.language;
1566 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1567 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1569 for (; cit != end; ++cit)
1570 if (cit->font().language() != ignore_language &&
1571 cit->font().language() != latex_language &&
1572 cit->font().language() != doc_language)
1578 // Convert the paragraph to a string.
1579 // Used for building the table of contents
1580 string const Paragraph::asString(Buffer const & buffer, bool label) const
1582 OutputParams runparams;
1583 return asString(buffer, runparams, label);
1587 string const Paragraph::asString(Buffer const & buffer,
1588 OutputParams const & runparams,
1593 if (label && !params().labelString().empty())
1594 s += params().labelString() + ' ';
1596 for (pos_type i = 0; i < size(); ++i) {
1597 value_type c = getChar(i);
1600 else if (c == META_INSET &&
1601 getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1603 getInset(i)->plaintext(buffer, os, runparams);
1604 s += subst(STRCONV(os.str()),'\n',' ');
1610 // This should really be done by the caller and not here.
1611 string ret = asString(buffer, runparams, 0, size(), label);
1612 return subst(ret, '\n', ' ');
1617 string const Paragraph::asString(Buffer const & buffer,
1618 pos_type beg, pos_type end, bool label) const
1621 OutputParams const runparams;
1622 return asString(buffer, runparams, beg, end, label);
1626 string const Paragraph::asString(Buffer const & buffer,
1627 OutputParams const & runparams,
1628 pos_type beg, pos_type end, bool label) const
1632 if (beg == 0 && label && !params().labelString().empty())
1633 os << params().labelString() << ' ';
1635 for (pos_type i = beg; i < end; ++i) {
1636 value_type const c = getUChar(buffer.params(), i);
1639 else if (c == META_INSET)
1640 getInset(i)->plaintext(buffer, os, runparams);
1647 void Paragraph::setInsetOwner(UpdatableInset * inset)
1649 pimpl_->inset_owner = inset;
1653 void Paragraph::setContentsFromPar(Paragraph const & par)
1655 pimpl_->setContentsFromPar(par);
1659 void Paragraph::trackChanges(Change::Type type)
1661 pimpl_->trackChanges(type);
1665 void Paragraph::untrackChanges()
1667 pimpl_->untrackChanges();
1671 void Paragraph::cleanChanges()
1673 pimpl_->cleanChanges();
1677 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1679 BOOST_ASSERT(empty() || pos < size());
1680 return pimpl_->lookupChange(pos);
1684 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1686 BOOST_ASSERT(empty() || pos < size());
1687 return pimpl_->lookupChangeFull(pos);
1691 bool Paragraph::isChanged(pos_type start, pos_type end) const
1693 return pimpl_->isChanged(start, end);
1697 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1699 return pimpl_->isChangeEdited(start, end);
1703 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1705 pimpl_->setChange(pos, type);
1709 void Paragraph::markErased()
1711 pimpl_->markErased();
1715 void Paragraph::acceptChange(pos_type start, pos_type end)
1717 return pimpl_->acceptChange(start, end);
1721 void Paragraph::rejectChange(pos_type start, pos_type end)
1723 return pimpl_->rejectChange(start, end);
1727 int Paragraph::id() const
1733 LyXLayout_ptr const & Paragraph::layout() const
1739 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1741 layout_ = new_layout;
1745 UpdatableInset * Paragraph::inInset() const
1747 return pimpl_->inset_owner;
1751 InsetBase::Code Paragraph::ownerCode() const
1753 return pimpl_->inset_owner
1754 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1758 void Paragraph::clearContents()
1764 void Paragraph::setChar(pos_type pos, value_type c)
1770 ParagraphParameters & Paragraph::params()
1772 return pimpl_->params;
1776 ParagraphParameters const & Paragraph::params() const
1778 return pimpl_->params;
1782 bool Paragraph::isFreeSpacing() const
1784 if (layout()->free_spacing)
1787 // for now we just need this, later should we need this in some
1788 // other way we can always add a function to InsetBase too.
1789 return ownerCode() == InsetBase::ERT_CODE;
1793 bool Paragraph::allowEmpty() const
1795 if (layout()->keepempty)
1797 return ownerCode() == InsetBase::ERT_CODE;
1801 Row & Paragraph::getRow(pos_type pos)
1803 BOOST_ASSERT(!rows().empty());
1805 RowList::iterator rit = rows_.end();
1806 RowList::iterator const begin = rows_.begin();
1808 for (--rit; rit != begin && rit->pos() > pos; --rit)
1815 Row const & Paragraph::getRow(pos_type pos) const
1817 BOOST_ASSERT(!rows().empty());
1819 RowList::const_iterator rit = rows_.end();
1820 RowList::const_iterator const begin = rows_.begin();
1822 for (--rit; rit != begin && rit->pos() > pos; --rit)
1829 size_t Paragraph::pos2row(pos_type pos) const
1831 BOOST_ASSERT(!rows().empty());
1833 RowList::const_iterator rit = rows_.end();
1834 RowList::const_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);
1878 void Paragraph::dump() const
1880 lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1881 for (size_t i = 0; i != rows_.size(); ++i) {
1882 lyxerr << " row " << i << ": ";