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"
32 #include "outputparams.h"
33 #include "paragraph_funcs.h"
38 #include "insets/insetbibitem.h"
39 #include "insets/insetoptarg.h"
41 #include "support/lstrings.h"
42 #include "support/textutils.h"
43 #include "support/std_sstream.h"
45 #include <boost/tuple/tuple.hpp>
52 using lyx::support::contains;
53 using lyx::support::subst;
60 using std::ostringstream;
63 Paragraph::Paragraph()
64 : y(0), height(0), begin_of_body_(0),
65 pimpl_(new Paragraph::Pimpl(this))
72 Paragraph::Paragraph(Paragraph const & par)
73 : y(0), height(0), text_(par.text_), begin_of_body_(par.begin_of_body_),
74 pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
77 // this is because of the dummy layout of the paragraphs that
79 layout_ = par.layout();
81 // copy everything behind the break-position to the new paragraph
82 insetlist = par.insetlist;
83 InsetList::iterator it = insetlist.begin();
84 InsetList::iterator end = insetlist.end();
85 for (; it != end; ++it) {
86 // currently we hold Inset*, not InsetBase*
87 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
92 void Paragraph::operator=(Paragraph const & par)
94 // needed as we will destroy the pimpl_ before copying it
98 lyxerr << "Paragraph::operator=()" << endl;
103 pimpl_ = new Pimpl(*par.pimpl_, this);
105 itemdepth = par.itemdepth;
106 // this is because of the dummy layout of the paragraphs that
108 layout_ = par.layout();
110 // copy everything behind the break-position to the new paragraph
111 insetlist = par.insetlist;
112 InsetList::iterator it = insetlist.begin();
113 InsetList::iterator end = insetlist.end();
114 for (; it != end; ++it) {
115 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
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) {
165 Change change = pimpl_->lookupChangeFull(i);
166 Changes::lyxMarkChange(os, column, curtime, running_change, change);
167 running_change = change;
169 // Write font changes
170 LyXFont font2 = getFontSettings(bparams, i);
171 if (font2 != font1) {
172 font2.lyxWriteChanges(font1, os);
177 value_type const c = getChar(i);
181 InsetOld const * inset = getInset(i);
183 if (inset->directWrite()) {
184 // international char, let it write
185 // code directly so it's shorter in
187 inset->write(buf, os);
189 os << "\n\\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.
218 lyxerr << "ERROR (Paragraph::writeFile):"
219 " NULL char in structure." << endl;
225 // to make reading work properly
227 running_change = pimpl_->lookupChange(0);
228 Changes::lyxMarkChange(os, column, curtime,
229 Change(Change::UNCHANGED), running_change);
231 Changes::lyxMarkChange(os, column, curtime,
232 running_change, Change(Change::UNCHANGED));
234 os << "\n\\end_layout\n";
238 void Paragraph::validate(LaTeXFeatures & features) const
240 pimpl_->validate(features, *layout());
244 void Paragraph::eraseIntern(lyx::pos_type pos)
246 pimpl_->eraseIntern(pos);
250 bool Paragraph::erase(pos_type pos)
252 return pimpl_->erase(pos);
256 int Paragraph::erase(pos_type start, pos_type end)
258 return pimpl_->erase(start, end);
262 void Paragraph::insert(pos_type start, string const & str,
263 LyXFont const & font)
265 int size = str.size();
266 for (int i = 0 ; i < size ; ++i)
267 insertChar(start + i, str[i], font);
271 bool Paragraph::checkInsertChar(LyXFont & font)
273 if (pimpl_->inset_owner)
274 return pimpl_->inset_owner->checkInsertChar(font);
279 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
281 insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
285 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
286 LyXFont const & font, Change change)
288 pimpl_->insertChar(pos, c, font, change);
292 void Paragraph::insertInset(pos_type pos, InsetOld * inset)
294 insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
298 void Paragraph::insertInset(pos_type pos, InsetOld * inset,
299 LyXFont const & font, Change change)
301 pimpl_->insertInset(pos, inset, font, change);
305 bool Paragraph::insetAllowed(InsetOld_code code)
307 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
308 if (pimpl_->inset_owner)
309 return pimpl_->inset_owner->insetAllowed(code);
314 InsetOld * Paragraph::getInset(pos_type pos)
316 BOOST_ASSERT(pos < size());
317 return insetlist.get(pos);
321 InsetOld const * Paragraph::getInset(pos_type pos) const
323 BOOST_ASSERT(pos < size());
324 return insetlist.get(pos);
328 // Gets uninstantiated font setting at position.
329 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
332 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)
343 if (pos == size() && !empty())
344 return getFontSettings(bparams, pos - 1);
346 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
350 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
352 BOOST_ASSERT(pos <= size());
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)
360 // This should not happen, but if so, we take no chances.
361 //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
367 // Gets uninstantiated font setting at position 0
368 LyXFont const Paragraph::getFirstFontSettings() const
370 if (!empty() && !pimpl_->fontlist.empty())
371 return pimpl_->fontlist[0].font();
373 return LyXFont(LyXFont::ALL_INHERIT);
377 // Gets the fully instantiated font at a given position in a paragraph
378 // This is basically the same function as LyXText::GetFont() in text2.C.
379 // The difference is that this one is used for generating the LaTeX file,
380 // and thus cosmetic "improvements" are disallowed: This has to deliver
381 // the true picture of the buffer. (Asger)
382 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
383 LyXFont const & outerfont) const
385 BOOST_ASSERT(pos >= 0);
387 LyXLayout_ptr const & lout = layout();
389 pos_type const body_pos = beginOfBody();
393 layoutfont = lout->labelfont;
395 layoutfont = lout->font;
397 LyXFont font = getFontSettings(bparams, pos);
398 font.realize(layoutfont);
399 font.realize(outerfont);
400 font.realize(bparams.getLyXTextClass().defaultfont());
406 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
407 LyXFont const & outerfont) const
409 LyXFont tmpfont = layout()->labelfont;
410 tmpfont.setLanguage(getParLanguage(bparams));
411 tmpfont.realize(outerfont);
412 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
417 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
418 LyXFont const & outerfont) const
420 LyXFont tmpfont = layout()->font;
421 tmpfont.setLanguage(getParLanguage(bparams));
422 tmpfont.realize(outerfont);
423 tmpfont.realize(bparams.getLyXTextClass().defaultfont());
428 /// Returns the height of the highest font in range
430 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
431 LyXFont_size def_size) const
433 if (pimpl_->fontlist.empty())
436 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
437 Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
438 for (; end_it != end; ++end_it) {
439 if (end_it->pos() >= endpos)
446 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
447 for (; cit != end; ++cit) {
448 if (cit->pos() >= startpos)
452 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
453 for (; cit != end_it; ++cit) {
454 LyXFont::FONT_SIZE size = cit->font().size();
455 if (size == LyXFont::INHERIT_SIZE)
457 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
464 Paragraph::value_type
465 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
467 value_type c = getChar(pos);
468 if (!lyxrc.rtl_support)
498 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
505 void Paragraph::setFont(pos_type pos, LyXFont const & font)
507 BOOST_ASSERT(pos <= size());
509 // First, reduce font against layout/label font
510 // Update: The setCharFont() routine in text2.C already
511 // reduces font, so we don't need to do that here. (Asger)
512 // No need to simplify this because it will disappear
513 // in a new kernel. (Asger)
514 // Next search font table
516 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
517 Pimpl::FontList::iterator it = beg;
518 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
519 for (; it != endit; ++it) {
520 if (it->pos() >= pos)
523 unsigned int i = std::distance(beg, it);
524 bool notfound = (it == endit);
526 if (!notfound && pimpl_->fontlist[i].font() == font)
529 bool begin = pos == 0 || notfound ||
530 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
531 // Is position pos is a beginning of a font block?
532 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
533 // Is position pos is the end of a font block?
534 if (begin && end) { // A single char block
535 if (i + 1 < pimpl_->fontlist.size() &&
536 pimpl_->fontlist[i + 1].font() == font) {
537 // Merge the singleton block with the next block
538 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
539 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
540 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
541 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
542 // Merge the singleton block with the previous block
543 pimpl_->fontlist[i - 1].pos(pos);
544 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
546 pimpl_->fontlist[i].font(font);
548 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
549 pimpl_->fontlist[i - 1].pos(pos);
551 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
552 Pimpl::FontTable(pos, font));
554 pimpl_->fontlist[i].pos(pos - 1);
555 if (!(i + 1 < pimpl_->fontlist.size() &&
556 pimpl_->fontlist[i + 1].font() == font))
557 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
558 Pimpl::FontTable(pos, font));
559 } else { // The general case. The block is splitted into 3 blocks
560 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
561 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
562 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
563 Pimpl::FontTable(pos, font));
568 void Paragraph::makeSameLayout(Paragraph const & par)
570 layout(par.layout());
572 params() = par.params();
576 int Paragraph::stripLeadingSpaces()
582 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
583 pimpl_->eraseIntern(0);
591 bool Paragraph::hasSameLayout(Paragraph const & par) const
594 par.layout() == layout() &&
595 params().sameLayout(par.params());
599 Paragraph::depth_type Paragraph::getDepth() const
601 return params().depth();
605 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
607 if (layout()->isEnvironment())
608 return params().depth() + 1;
610 return params().depth();
614 char Paragraph::getAlign() const
616 return params().align();
620 string const & Paragraph::getLabelstring() const
622 return params().labelString();
626 // the next two functions are for the manual labels
627 string const Paragraph::getLabelWidthString() const
629 if (!params().labelWidthString().empty())
630 return params().labelWidthString();
632 return _("Senseless with this layout!");
636 void Paragraph::setLabelWidthString(string const & s)
638 params().labelWidthString(s);
642 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
645 params().labelWidthString(string());
646 params().align(LYX_ALIGN_LAYOUT);
647 params().spaceTop(VSpace(VSpace::NONE));
648 params().spaceBottom(VSpace(VSpace::NONE));
649 params().spacing(Spacing(Spacing::Default));
653 int Paragraph::beginOfBody() const
655 return begin_of_body_;
659 void Paragraph::setBeginOfBody()
661 if (layout()->labeltype != LABEL_MANUAL) {
666 // Unroll the first two cycles of the loop
667 // and remember the previous character to
668 // remove unnecessary getChar() calls
670 pos_type end = size();
671 if (i < end && !isNewline(i)) {
673 char previous_char = 0;
676 previous_char = text_[i];
679 while (i < end && previous_char != ' ') {
684 previous_char = temp;
694 // returns -1 if inset not found
695 int Paragraph::getPositionOfInset(InsetOld const * inset) const
698 InsetList::const_iterator it = insetlist.begin();
699 InsetList::const_iterator end = insetlist.end();
700 for (; it != end; ++it)
701 if (it->inset == inset)
707 InsetBibitem * Paragraph::bibitem() const
709 InsetList::const_iterator it = insetlist.begin();
710 if (it != insetlist.end() && it->inset->lyxCode() == InsetOld::BIBTEX_CODE)
711 return static_cast<InsetBibitem *>(it->inset);
718 /* paragraphs inside floats need different alignment tags to avoid
721 bool noTrivlistCentering(UpdatableInset const * inset)
723 if (inset && inset->owner()) {
724 InsetOld::Code const code = inset->owner()->lyxCode();
725 return code == InsetOld::FLOAT_CODE ||
726 code == InsetOld::WRAP_CODE;
732 string correction(string const & orig)
734 if (orig == "flushleft")
735 return "raggedright";
736 if (orig == "flushright")
738 if (orig == "center")
744 string const corrected_env(string const & suffix, string const & env,
745 UpdatableInset const * inset)
747 string output = suffix + "{";
748 if (noTrivlistCentering(inset))
749 output += correction(env);
758 // This could go to ParagraphParameters if we want to
759 int Paragraph::startTeXParParams(BufferParams const & bparams,
760 ostream & os, bool moving_arg) const
764 if (params().noindent()) {
769 switch (params().align()) {
771 case LYX_ALIGN_BLOCK:
772 case LYX_ALIGN_LAYOUT:
773 case LYX_ALIGN_SPECIAL:
776 case LYX_ALIGN_RIGHT:
777 case LYX_ALIGN_CENTER:
785 switch (params().align()) {
787 case LYX_ALIGN_BLOCK:
788 case LYX_ALIGN_LAYOUT:
789 case LYX_ALIGN_SPECIAL:
791 case LYX_ALIGN_LEFT: {
793 UpdatableInset const * const inset = pimpl_->inset_owner;
794 if (getParLanguage(bparams)->babel() != "hebrew")
795 output = corrected_env("\\begin", "flushleft", inset);
797 output = corrected_env("\\begin", "flushright", inset);
799 column += output.size();
801 } case LYX_ALIGN_RIGHT: {
803 UpdatableInset const * const inset = pimpl_->inset_owner;
804 if (getParLanguage(bparams)->babel() != "hebrew")
805 output = corrected_env("\\begin", "flushright", inset);
807 output = corrected_env("\\begin", "flushleft", inset);
809 column += output.size();
811 } case LYX_ALIGN_CENTER: {
813 output = corrected_env("\\begin", "center", pimpl_->inset_owner);
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 UpdatableInset const * const inset = pimpl_->inset_owner;
855 if (getParLanguage(bparams)->babel() != "hebrew")
856 output = corrected_env("\\par\\end", "flushleft", inset);
858 output = corrected_env("\\par\\end", "flushright", inset);
860 column += output.size();
862 } case LYX_ALIGN_RIGHT: {
864 UpdatableInset const * const inset = pimpl_->inset_owner;
865 if (getParLanguage(bparams)->babel() != "hebrew")
866 output = corrected_env("\\par\\end", "flushright", inset);
868 output = corrected_env("\\par\\end", "flushleft", inset);
870 column += output.size();
872 } case LYX_ALIGN_CENTER: {
874 output = corrected_env("\\par\\end", "center", pimpl_->inset_owner);
876 column += output.size();
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 ostream & os, TexRow & texrow,
890 OutputParams const & runparams)
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 then "Standard" to be valid!
903 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
906 style = bparams.getLyXTextClass().defaultLayout();
913 // Maybe we have to create a optional argument.
914 pos_type body_pos = beginOfBody();
915 unsigned int column = 0;
920 basefont = getLabelFont(bparams, outerfont);
922 basefont = getLayoutFont(bparams, outerfont);
925 bool moving_arg = runparams.moving_arg;
926 moving_arg |= style->needprotect;
928 // Which font is currently active?
929 LyXFont running_font(basefont);
930 // Do we have an open font change?
931 bool open_font = false;
933 Change::Type running_change = Change::UNCHANGED;
935 texrow.start(id(), 0);
937 // if the paragraph is empty, the loop will not be entered at all
939 if (style->isCommand()) {
944 column += startTeXParParams(bparams, os, moving_arg);
947 for (pos_type i = 0; i < size(); ++i) {
949 // First char in paragraph or after label?
953 column += running_font.latexWriteEndChanges(os, basefont, basefont);
956 basefont = getLayoutFont(bparams, outerfont);
957 running_font = basefont;
961 if (style->isCommand()) {
967 column += startTeXParParams(bparams, os,
971 value_type c = getChar(i);
973 // Fully instantiated font
974 LyXFont font = getFont(bparams, i, outerfont);
976 LyXFont const last_font = running_font;
978 // Spaces at end of font change are simulated to be
979 // outside font change, i.e. we write "\textXX{text} "
980 // rather than "\textXX{text }". (Asger)
981 if (open_font && c == ' ' && i <= size() - 2) {
982 LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
983 if (next_font != running_font && next_font != font) {
988 // We end font definition before blanks
990 (font != running_font ||
991 font.language() != running_font.language()))
993 column += running_font.latexWriteEndChanges(os,
995 (i == body_pos-1) ? basefont : font);
996 running_font = basefont;
1000 // Blanks are printed before start of fontswitch
1002 // Do not print the separation of the optional argument
1003 if (i != body_pos - 1) {
1004 pimpl_->simpleTeXBlanks(os, texrow, i,
1005 column, font, *style);
1009 // Do we need to change font?
1010 if ((font != running_font ||
1011 font.language() != running_font.language()) &&
1014 column += font.latexWriteStartChanges(os, basefont,
1016 running_font = font;
1020 Change::Type change = pimpl_->lookupChange(i);
1022 column += Changes::latexMarkChange(os, running_change, change);
1023 running_change = change;
1025 OutputParams rp = runparams;
1026 rp.moving_arg = moving_arg;
1027 rp.free_spacing = style->free_spacing;
1028 pimpl_->simpleTeXSpecialChars(buf, bparams,
1029 os, texrow, runparams,
1031 basefont, outerfont, open_font,
1033 *style, i, column, c);
1036 column += Changes::latexMarkChange(os,
1037 running_change, Change::UNCHANGED);
1039 // If we have an open font definition, we have to close it
1041 #ifdef FIXED_LANGUAGE_END_DETECTION
1044 .latexWriteEndChanges(os, basefont,
1045 next_->getFont(bparams,
1048 running_font.latexWriteEndChanges(os, basefont,
1052 #ifdef WITH_WARNINGS
1053 //#warning For now we ALWAYS have to close the foreign font settings if they are
1054 //#warning there as we start another \selectlanguage with the next paragraph if
1055 //#warning we are in need of this. This should be fixed sometime (Jug)
1057 running_font.latexWriteEndChanges(os, basefont, basefont);
1061 // Needed if there is an optional argument but no contents.
1062 if (body_pos > 0 && body_pos == size()) {
1064 return_value = false;
1068 column += endTeXParParams(bparams, os, moving_arg);
1071 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1072 return return_value;
1078 // checks, if newcol chars should be put into this line
1079 // writes newline, if necessary.
1080 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1081 string::size_type newcol)
1084 if (colcount > lyxrc.ascii_linelen) {
1086 colcount = newcol; // assume write after this call
1101 string tag_name(PAR_TAG const & pt) {
1103 case NONE: return "!-- --";
1104 case TT: return "tt";
1105 case SF: return "sf";
1106 case BF: return "bf";
1107 case IT: return "it";
1108 case SL: return "sl";
1109 case EM: return "em";
1116 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1118 p1 = static_cast<PAR_TAG>(p1 | p2);
1123 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1125 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1131 // Handle internal paragraph parsing -- layout already processed.
1132 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1134 LyXFont const & outerfont,
1135 OutputParams const & runparams,
1136 lyx::depth_type /*depth*/) const
1138 LyXLayout_ptr const & style = layout();
1140 string::size_type char_line_count = 5; // Heuristic choice ;-)
1142 // gets paragraph main font
1145 if (style->labeltype == LABEL_MANUAL) {
1146 font_old = style->labelfont;
1149 font_old = style->font;
1153 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1154 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1155 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1158 stack<PAR_TAG> tag_state;
1159 // parsing main loop
1160 for (pos_type i = 0; i < size(); ++i) {
1162 PAR_TAG tag_close = NONE;
1163 list < PAR_TAG > tag_open;
1165 LyXFont const font = getFont(buf.params(), i, outerfont);
1167 if (font_old.family() != font.family()) {
1168 switch (family_type) {
1169 case LyXFont::SANS_FAMILY:
1172 case LyXFont::TYPEWRITER_FAMILY:
1179 family_type = font.family();
1181 switch (family_type) {
1182 case LyXFont::SANS_FAMILY:
1183 tag_open.push_back(SF);
1185 case LyXFont::TYPEWRITER_FAMILY:
1186 tag_open.push_back(TT);
1193 if (font_old.series() != font.series()) {
1194 switch (series_type) {
1195 case LyXFont::BOLD_SERIES:
1202 series_type = font.series();
1204 switch (series_type) {
1205 case LyXFont::BOLD_SERIES:
1206 tag_open.push_back(BF);
1214 if (font_old.shape() != font.shape()) {
1215 switch (shape_type) {
1216 case LyXFont::ITALIC_SHAPE:
1219 case LyXFont::SLANTED_SHAPE:
1226 shape_type = font.shape();
1228 switch (shape_type) {
1229 case LyXFont::ITALIC_SHAPE:
1230 tag_open.push_back(IT);
1232 case LyXFont::SLANTED_SHAPE:
1233 tag_open.push_back(SL);
1240 if (font_old.emph() != font.emph()) {
1241 if (font.emph() == LyXFont::ON) {
1242 tag_open.push_back(EM);
1251 list < PAR_TAG > temp;
1252 while (!tag_state.empty() && tag_close) {
1253 PAR_TAG k = tag_state.top();
1255 os << "</" << tag_name(k) << '>';
1262 for(list< PAR_TAG >::const_iterator j = temp.begin();
1263 j != temp.end(); ++j) {
1265 os << '<' << tag_name(*j) << '>';
1268 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1269 j != tag_open.end(); ++j) {
1271 os << '<' << tag_name(*j) << '>';
1274 char c = getChar(i);
1276 if (c == Paragraph::META_INSET) {
1277 InsetOld const * inset = getInset(i);
1278 inset->linuxdoc(buf, os, runparams);
1283 if (style->latexparam() == "CDATA") {
1284 // "TeX"-Mode on == > SGML-Mode on.
1291 boost::tie(ws, str) = sgml::escapeChar(c);
1292 if (ws && !isFreeSpacing()) {
1293 // in freespacing mode, spaces are
1294 // non-breaking characters
1295 if (desc_on) {// if char is ' ' then...
1298 sgmlLineBreak(os, char_line_count, 6);
1302 sgmlLineBreak(os, char_line_count, 1);
1307 char_line_count += str.length();
1313 while (!tag_state.empty()) {
1314 os << "</" << tag_name(tag_state.top()) << '>';
1318 // resets description flag correctly
1320 // <tag> not closed...
1321 sgmlLineBreak(os, char_line_count, 6);
1327 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1329 LyXFont const & outerfont,
1331 OutputParams const & runparams,
1332 lyx::depth_type depth) const
1334 bool emph_flag = false;
1336 LyXLayout_ptr const & style = layout();
1338 LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1340 int char_line_count = depth;
1341 //if (!style.free_spacing)
1342 // os << string(depth,' ');
1344 // parsing main loop
1345 for (pos_type i = 0; i < size(); ++i) {
1346 LyXFont font = getFont(buf.params(), i, outerfont);
1348 // handle <emphasis> tag
1349 if (font_old.emph() != font.emph()) {
1350 if (font.emph() == LyXFont::ON) {
1351 if (style->latexparam() == "CDATA")
1354 if (style->latexparam() == "CDATA")
1358 if (style->latexparam() == "CDATA")
1360 os << "</emphasis>";
1361 if (style->latexparam() == "CDATA")
1369 InsetOld const * inset = getInset(i);
1370 // don't print the inset in position 0 if desc_on == 3 (label)
1371 if (i || desc_on != 3) {
1372 if (style->latexparam() == "CDATA")
1374 inset->docbook(buf, os, runparams);
1375 if (style->latexparam() == "CDATA")
1379 char c = getChar(i);
1382 boost::tie(ws, str) = sgml::escapeChar(c);
1384 if (style->pass_thru) {
1386 } else if (isFreeSpacing() || c != ' ') {
1388 } else if (desc_on == 1) {
1390 os << "\n</term><listitem><para>";
1400 if (style->latexparam() == "CDATA")
1402 os << "</emphasis>";
1403 if (style->latexparam() == "CDATA")
1407 // resets description flag correctly
1409 // <term> not closed...
1410 os << "</term>\n<listitem><para> </para>";
1412 if (style->free_spacing)
1419 /// return true if the char is a meta-character for an inset
1421 bool IsInsetChar(char c)
1423 return (c == Paragraph::META_INSET);
1430 bool Paragraph::isHfill(pos_type pos) const
1432 return IsInsetChar(getChar(pos))
1433 && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
1437 bool Paragraph::isInset(pos_type pos) const
1439 return IsInsetChar(getChar(pos));
1443 bool Paragraph::isNewline(pos_type pos) const
1445 return IsInsetChar(getChar(pos))
1446 && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
1450 bool Paragraph::isSeparator(pos_type pos) const
1452 return IsSeparatorChar(getChar(pos));
1456 bool Paragraph::isLineSeparator(pos_type pos) const
1458 value_type const c = getChar(pos);
1459 return IsLineSeparatorChar(c)
1460 || (IsInsetChar(c) && getInset(pos) &&
1461 getInset(pos)->isLineSeparator());
1465 bool Paragraph::isKomma(pos_type pos) const
1467 return IsKommaChar(getChar(pos));
1471 /// Used by the spellchecker
1472 bool Paragraph::isLetter(pos_type pos) const
1474 value_type const c = getChar(pos);
1475 if (IsLetterChar(c))
1478 return getInset(pos)->isLetter();
1479 // We want to pass the ' and escape chars to ispell
1480 string const extra = lyxrc.isp_esc_chars + '\'';
1481 return contains(extra, c);
1485 bool Paragraph::isWord(pos_type pos) const
1487 unsigned char const c = getChar(pos);
1488 return !(IsSeparatorChar(c)
1495 Paragraph::getParLanguage(BufferParams const & bparams) const
1498 return getFirstFontSettings().language();
1499 #warning FIXME we should check the prev par as well (Lgb)
1500 return bparams.language;
1504 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1506 return lyxrc.rtl_support
1507 && getParLanguage(bparams)->RightToLeft()
1508 && !(inInset() && inInset()->owner() &&
1509 inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
1513 void Paragraph::changeLanguage(BufferParams const & bparams,
1514 Language const * from, Language const * to)
1516 for (pos_type i = 0; i < size(); ++i) {
1517 LyXFont font = getFontSettings(bparams, i);
1518 if (font.language() == from) {
1519 font.setLanguage(to);
1526 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1528 Language const * doc_language = bparams.language;
1529 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1530 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1532 for (; cit != end; ++cit)
1533 if (cit->font().language() != ignore_language &&
1534 cit->font().language() != latex_language &&
1535 cit->font().language() != doc_language)
1541 // Convert the paragraph to a string.
1542 // Used for building the table of contents
1543 string const Paragraph::asString(Buffer const & buffer,
1546 OutputParams runparams;
1547 return asString(buffer, runparams, label);
1551 string const Paragraph::asString(Buffer const & buffer,
1552 OutputParams const & runparams,
1557 if (label && !params().labelString().empty())
1558 s += params().labelString() + ' ';
1560 for (pos_type i = 0; i < size(); ++i) {
1561 value_type c = getChar(i);
1564 else if (c == META_INSET &&
1565 getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
1567 getInset(i)->plaintext(buffer, os, runparams);
1568 s += subst(STRCONV(os.str()),'\n',' ');
1574 // This should really be done by the caller and not here.
1575 string ret = asString(buffer, runparams, 0, size(), label);
1576 return subst(ret, '\n', ' ');
1581 string const Paragraph::asString(Buffer const & buffer,
1582 pos_type beg, pos_type end, bool label) const
1585 OutputParams const runparams;
1586 return asString(buffer, runparams, beg, end, label);
1590 string const Paragraph::asString(Buffer const & buffer,
1591 OutputParams const & runparams,
1592 pos_type beg, pos_type end, bool label) const
1596 if (beg == 0 && label && !params().labelString().empty())
1597 os << params().labelString() << ' ';
1599 for (pos_type i = beg; i < end; ++i) {
1600 value_type const c = getUChar(buffer.params(), i);
1603 else if (c == META_INSET)
1604 getInset(i)->plaintext(buffer, os, runparams);
1611 void Paragraph::setInsetOwner(UpdatableInset * inset)
1613 pimpl_->inset_owner = inset;
1614 InsetList::iterator it = insetlist.begin();
1615 InsetList::iterator end = insetlist.end();
1616 for (; it != end; ++it)
1618 it->inset->setOwner(inset);
1622 void Paragraph::setContentsFromPar(Paragraph const & par)
1624 pimpl_->setContentsFromPar(par);
1628 void Paragraph::trackChanges(Change::Type type)
1630 pimpl_->trackChanges(type);
1634 void Paragraph::untrackChanges()
1636 pimpl_->untrackChanges();
1640 void Paragraph::cleanChanges()
1642 pimpl_->cleanChanges();
1646 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1648 BOOST_ASSERT(!size() || pos < size());
1649 return pimpl_->lookupChange(pos);
1653 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1655 BOOST_ASSERT(!size() || pos < size());
1656 return pimpl_->lookupChangeFull(pos);
1660 bool Paragraph::isChanged(pos_type start, pos_type end) const
1662 return pimpl_->isChanged(start, end);
1666 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1668 return pimpl_->isChangeEdited(start, end);
1672 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1674 pimpl_->setChange(pos, type);
1679 void Paragraph::markErased()
1681 pimpl_->markErased();
1685 void Paragraph::acceptChange(pos_type start, pos_type end)
1687 return pimpl_->acceptChange(start, end);
1691 void Paragraph::rejectChange(pos_type start, pos_type end)
1693 return pimpl_->rejectChange(start, end);
1697 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1699 // This is in the critical path!
1700 pos_type const siz = text_.size();
1702 BOOST_ASSERT(0 <= pos);
1703 BOOST_ASSERT(pos <= siz);
1706 lyxerr << "getChar() on pos " << pos << " in par id "
1707 << id() << " of size " << siz
1708 << " is a bit silly !" << endl;
1709 BOOST_ASSERT(false);
1717 int Paragraph::id() const
1723 LyXLayout_ptr const & Paragraph::layout() const
1726 InsetOld * inset = inInset();
1727 if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
1728 return static_cast<InsetEnvironment*>(inset)->layout();
1734 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1736 layout_ = new_layout;
1740 UpdatableInset * Paragraph::inInset() const
1742 return pimpl_->inset_owner;
1746 void Paragraph::clearContents()
1752 void Paragraph::setChar(pos_type pos, value_type c)
1758 ParagraphParameters & Paragraph::params()
1760 return pimpl_->params;
1764 ParagraphParameters const & Paragraph::params() const
1766 return pimpl_->params;
1770 bool Paragraph::isFreeSpacing() const
1772 if (layout()->free_spacing)
1775 // for now we just need this, later should we need this in some
1776 // other way we can always add a function to InsetOld too.
1777 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1778 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1783 bool Paragraph::allowEmpty() const
1785 if (layout()->keepempty)
1787 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1788 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1793 RowList::iterator Paragraph::getRow(pos_type pos)
1795 RowList::iterator rit = rows.end();
1796 RowList::iterator const begin = rows.begin();
1798 for (--rit; rit != begin && rit->pos() > pos; --rit)
1805 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1807 if (!Encodings::is_arabic(c))
1808 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1809 return c + (0xb0 - '0');
1813 unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1814 unsigned char next_char = ' ';
1816 for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1817 unsigned char const par_char = getChar(i);
1818 if (!Encodings::IsComposeChar_arabic(par_char)) {
1819 next_char = par_char;
1824 if (Encodings::is_arabic(next_char)) {
1825 if (Encodings::is_arabic(prev_char) &&
1826 !Encodings::is_arabic_special(prev_char))
1827 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1829 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1831 if (Encodings::is_arabic(prev_char) &&
1832 !Encodings::is_arabic_special(prev_char))
1833 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1835 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);