1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
13 #include "paragraph.h"
14 #include "paragraph_pimpl.h"
18 #include "tex-strings.h"
20 #include "bufferparams.h"
23 #include "BufferView.h"
25 #include "ParameterStruct.h"
29 #include "insets/insetbibitem.h"
30 #include "insets/insetoptarg.h"
32 #include "support/filetools.h"
33 #include "support/lstrings.h"
34 #include "support/lyxmanip.h"
35 #include "support/FileInfo.h"
36 #include "support/LAssert.h"
37 #include "support/textutils.h"
48 using std::lower_bound;
49 using std::upper_bound;
54 // this is a minibuffer
59 LyXFont minibuffer_font;
60 Inset * minibuffer_inset;
65 extern BufferView * current_view;
68 Paragraph::Paragraph()
69 : pimpl_(new Paragraph::Pimpl(this))
82 // This constructor inserts the new paragraph in a list.
83 // It is placed after par.
84 Paragraph::Paragraph(Paragraph * par)
85 : pimpl_(new Paragraph::Pimpl(this))
90 // double linked list begin
93 next_->previous_ = this;
95 previous_->next_ = this;
103 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
104 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
112 // this is because of the dummy layout of the paragraphs that
114 layout_ = lp.layout();
116 // copy everything behind the break-position to the new paragraph
117 insetlist = lp.insetlist;
118 InsetList::iterator it = insetlist.begin();
119 InsetList::iterator end = insetlist.end();
120 for (; it != end; ++it) {
121 it.setInset(it.getInset()->clone(*current_view->buffer(),
123 // tell the new inset who is the boss now
124 it.getInset()->parOwner(this);
129 // the destructor removes the new paragraph from the list
130 Paragraph::~Paragraph()
134 previous_->next_ = next_;
136 next_->previous_ = previous_;
141 //lyxerr << "Paragraph::paragraph_id = "
142 // << Paragraph::paragraph_id << endl;
146 void Paragraph::write(Buffer const * buf, ostream & os,
147 BufferParams const & bparams,
148 depth_type & dth) const
150 // The beginning or end of a deeper (i.e. nested) area?
151 if (dth != params().depth()) {
152 if (params().depth() > dth) {
153 while (params().depth() > dth) {
154 os << "\n\\begin_deeper ";
158 while (params().depth() < dth) {
159 os << "\n\\end_deeper ";
165 // First write the layout
166 os << "\n\\layout " << layout()->name() << '\n';
168 // Maybe some vertical spaces.
169 if (params().spaceTop().kind() != VSpace::NONE)
170 os << "\\added_space_top "
171 << params().spaceTop().asLyXCommand() << ' ';
172 if (params().spaceBottom().kind() != VSpace::NONE)
173 os << "\\added_space_bottom "
174 << params().spaceBottom().asLyXCommand() << ' ';
176 // Maybe the paragraph has special spacing
177 params().spacing().writeFile(os, true);
179 // The labelwidth string used in lists.
180 if (!params().labelWidthString().empty())
181 os << "\\labelwidthstring "
182 << params().labelWidthString() << '\n';
184 // Lines above or below?
185 if (params().lineTop())
187 if (params().lineBottom())
188 os << "\\line_bottom ";
190 // Pagebreaks above or below?
191 if (params().pagebreakTop())
192 os << "\\pagebreak_top ";
193 if (params().pagebreakBottom())
194 os << "\\pagebreak_bottom ";
196 // Start of appendix?
197 if (params().startOfAppendix())
198 os << "\\start_of_appendix ";
201 if (params().noindent())
204 // Do we have a manual left indent?
205 if (!params().leftIndent().zero())
206 os << "\\leftindent " << params().leftIndent().asString()
210 if (params().align() != LYX_ALIGN_LAYOUT) {
212 switch (params().align()) {
213 case LYX_ALIGN_LEFT: h = 1; break;
214 case LYX_ALIGN_RIGHT: h = 2; break;
215 case LYX_ALIGN_CENTER: h = 3; break;
216 default: h = 0; break;
218 os << "\\align " << string_align[h] << ' ';
221 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
223 Change running_change = Change(Change::UNCHANGED);
224 lyx::time_type const curtime(lyx::current_time());
227 for (pos_type i = 0; i < size(); ++i) {
233 Change change = pimpl_->lookupChangeFull(i);
234 Changes::lyxMarkChange(os, column, curtime, running_change, change);
235 running_change = change;
237 // Write font changes
238 LyXFont font2 = getFontSettings(bparams, i);
239 if (font2 != font1) {
240 font2.lyxWriteChanges(font1, os);
245 value_type const c = getChar(i);
249 Inset const * inset = getInset(i);
251 if (inset->directWrite()) {
252 // international char, let it write
253 // code directly so it's shorter in
255 inset->write(buf, os);
257 os << "\n\\begin_inset ";
258 inset->write(buf, os);
259 os << "\n\\end_inset \n\n";
265 os << "\n\\newline \n";
269 os << "\n\\backslash \n";
273 if (i + 1 < size() && getChar(i + 1) == ' ') {
280 if ((column > 70 && c == ' ')
285 // this check is to amend a bug. LyX sometimes
286 // inserts '\0' this could cause problems.
290 lyxerr << "ERROR (Paragraph::writeFile):"
291 " NULL char in structure." << endl;
297 // to make reading work properly
299 running_change = pimpl_->lookupChange(0);
300 Changes::lyxMarkChange(os, column, curtime,
301 Change(Change::UNCHANGED), running_change);
303 Changes::lyxMarkChange(os, column, curtime,
304 running_change, Change(Change::UNCHANGED));
308 void Paragraph::validate(LaTeXFeatures & features) const
310 pimpl_->validate(features, *layout());
314 // First few functions needed for cut and paste and paragraph breaking.
315 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
317 BufferParams bparams = buffer.params;
319 minibuffer_char = getChar(pos);
320 minibuffer_font = getFontSettings(bparams, pos);
321 minibuffer_inset = 0;
322 if (minibuffer_char == Paragraph::META_INSET) {
324 minibuffer_inset = getInset(pos)->clone(buffer);
326 minibuffer_inset = 0;
327 minibuffer_char = ' ';
328 // This reflects what GetInset() does (ARRae)
334 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
336 minibuffer_char = getChar(pos);
337 minibuffer_font = getFontSettings(bparams, pos);
338 minibuffer_inset = 0;
339 if (minibuffer_char == Paragraph::META_INSET) {
341 // the inset is not in a paragraph anymore
342 minibuffer_inset = insetlist.release(pos);
343 minibuffer_inset->parOwner(0);
345 minibuffer_inset = 0;
346 minibuffer_char = ' ';
347 // This reflects what GetInset() does (ARRae)
352 // Erase(pos); now the caller is responsible for that.
356 bool Paragraph::insertFromMinibuffer(pos_type pos)
358 if (minibuffer_char == Paragraph::META_INSET) {
359 if (!insetAllowed(minibuffer_inset->lyxCode())) {
362 insertInset(pos, minibuffer_inset, minibuffer_font);
364 LyXFont f = minibuffer_font;
365 if (!checkInsertChar(f)) {
368 insertChar(pos, minibuffer_char, f);
376 void Paragraph::eraseIntern(lyx::pos_type pos)
378 pimpl_->eraseIntern(pos);
382 void Paragraph::erase(pos_type pos)
388 bool Paragraph::erase(pos_type start, pos_type end)
390 return pimpl_->erase(start, end);
394 bool Paragraph::checkInsertChar(LyXFont & font)
396 if (pimpl_->inset_owner)
397 return pimpl_->inset_owner->checkInsertChar(font);
402 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
404 LyXFont const f(LyXFont::ALL_INHERIT);
405 insertChar(pos, c, f);
409 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
410 LyXFont const & font, Change change)
412 pimpl_->insertChar(pos, c, font, change);
416 void Paragraph::insertInset(pos_type pos, Inset * inset)
418 LyXFont const f(LyXFont::ALL_INHERIT);
419 insertInset(pos, inset, f);
423 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font, Change change)
425 pimpl_->insertInset(pos, inset, font, change);
429 bool Paragraph::insetAllowed(Inset::Code code)
431 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
432 if (pimpl_->inset_owner)
433 return pimpl_->inset_owner->insetAllowed(code);
438 Inset * Paragraph::getInset(pos_type pos)
440 lyx::Assert(pos < size());
441 return insetlist.get(pos);
445 Inset const * Paragraph::getInset(pos_type pos) const
447 lyx::Assert(pos < size());
448 return insetlist.get(pos);
452 // Gets uninstantiated font setting at position.
453 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
456 lyx::Assert(pos <= size());
458 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
459 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
460 for (; cit != end; ++cit) {
461 if (cit->pos() >= pos)
467 retfont = cit->font();
468 else if (pos == size() && !empty())
469 retfont = getFontSettings(bparams, pos - 1);
471 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
477 // Gets uninstantiated font setting at position 0
478 LyXFont const Paragraph::getFirstFontSettings() const
480 if (!empty() && !pimpl_->fontlist.empty())
481 return pimpl_->fontlist[0].font();
483 return LyXFont(LyXFont::ALL_INHERIT);
487 // Gets the fully instantiated font at a given position in a paragraph
488 // This is basically the same function as LyXText::GetFont() in text2.C.
489 // The difference is that this one is used for generating the LaTeX file,
490 // and thus cosmetic "improvements" are disallowed: This has to deliver
491 // the true picture of the buffer. (Asger)
492 // If position is -1, we get the layout font of the paragraph.
493 // If position is -2, we get the font of the manual label of the paragraph.
494 LyXFont const Paragraph::getFont(BufferParams const & bparams,
497 lyx::Assert(pos >= 0);
499 LyXLayout_ptr const & lout = layout();
501 pos_type const body_pos = beginningOfBody();
505 layoutfont = lout->labelfont;
507 layoutfont = lout->font;
509 LyXFont tmpfont = getFontSettings(bparams, pos);
510 tmpfont.realize(layoutfont);
512 return pimpl_->realizeFont(tmpfont, bparams);
516 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
518 LyXLayout_ptr const & lout = layout();
520 LyXFont tmpfont = lout->labelfont;
521 tmpfont.setLanguage(getParLanguage(bparams));
523 return pimpl_->realizeFont(tmpfont, bparams);
527 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
529 LyXLayout_ptr const & lout = layout();
531 LyXFont tmpfont = lout->font;
532 tmpfont.setLanguage(getParLanguage(bparams));
534 return pimpl_->realizeFont(tmpfont, bparams);
538 /// Returns the height of the highest font in range
540 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
541 LyXFont::FONT_SIZE const def_size) const
543 if (pimpl_->fontlist.empty())
546 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
547 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
548 for (; end_it != end; ++end_it) {
549 if (end_it->pos() >= endpos)
556 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
557 for (; cit != end; ++cit) {
558 if (cit->pos() >= startpos)
562 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
563 for (; cit != end_it; ++cit) {
564 LyXFont::FONT_SIZE size = cit->font().size();
565 if (size == LyXFont::INHERIT_SIZE)
567 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
574 Paragraph::value_type
575 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
577 value_type c = getChar(pos);
578 if (!lyxrc.rtl_support)
608 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
615 void Paragraph::setFont(pos_type pos, LyXFont const & font)
617 lyx::Assert(pos <= size());
619 // First, reduce font against layout/label font
620 // Update: The SetCharFont() routine in text2.C already
621 // reduces font, so we don't need to do that here. (Asger)
622 // No need to simplify this because it will disappear
623 // in a new kernel. (Asger)
624 // Next search font table
626 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
627 Pimpl::FontList::iterator it = beg;
628 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
629 for (; it != endit; ++it) {
630 if (it->pos() >= pos)
633 unsigned int i = std::distance(beg, it);
634 bool notfound = (it == endit);
636 if (!notfound && pimpl_->fontlist[i].font() == font)
639 bool begin = pos == 0 || notfound ||
640 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
641 // Is position pos is a beginning of a font block?
642 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
643 // Is position pos is the end of a font block?
644 if (begin && end) { // A single char block
645 if (i + 1 < pimpl_->fontlist.size() &&
646 pimpl_->fontlist[i + 1].font() == font) {
647 // Merge the singleton block with the next block
648 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
649 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
650 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
651 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
652 // Merge the singleton block with the previous block
653 pimpl_->fontlist[i - 1].pos(pos);
654 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
656 pimpl_->fontlist[i].font(font);
658 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
659 pimpl_->fontlist[i - 1].pos(pos);
661 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
662 Pimpl::FontTable(pos, font));
664 pimpl_->fontlist[i].pos(pos - 1);
665 if (!(i + 1 < pimpl_->fontlist.size() &&
666 pimpl_->fontlist[i + 1].font() == font))
667 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
668 Pimpl::FontTable(pos, font));
669 } else { // The general case. The block is splitted into 3 blocks
670 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
671 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
672 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
673 Pimpl::FontTable(pos, font));
679 void Paragraph::next(Paragraph * p)
685 // This function is able to hide closed footnotes.
686 Paragraph * Paragraph::next()
692 Paragraph const * Paragraph::next() const
698 void Paragraph::previous(Paragraph * p)
704 // This function is able to hide closed footnotes.
705 Paragraph * Paragraph::previous()
711 // This function is able to hide closed footnotes.
712 Paragraph const * Paragraph::previous() const
719 void Paragraph::makeSameLayout(Paragraph const * par)
721 layout(par->layout());
723 params() = par->params();
727 int Paragraph::stripLeadingSpaces()
729 if (layout()->free_spacing ||
735 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
736 pimpl_->eraseIntern(0);
744 bool Paragraph::hasSameLayout(Paragraph const * par) const
747 par->layout() == layout() &&
748 params().sameLayout(par->params());
752 int Paragraph::getEndLabel() const
754 Paragraph const * par = this;
755 depth_type par_depth = getDepth();
757 LyXLayout_ptr const & layout = par->layout();
758 int const endlabeltype = layout->endlabeltype;
760 if (endlabeltype != END_LABEL_NO_LABEL) {
764 depth_type const next_depth = next_->getDepth();
765 if (par_depth > next_depth ||
766 (par_depth == next_depth
767 && layout != next_->layout()))
773 par = par->outerHook();
775 par_depth = par->getDepth();
777 return END_LABEL_NO_LABEL;
781 Paragraph::depth_type Paragraph::getDepth() const
783 return params().depth();
787 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
789 bool const isenv = layout()->isEnvironment();
792 return params().depth() + 1;
794 return params().depth();
798 char Paragraph::getAlign() const
800 return params().align();
804 string const & Paragraph::getLabelstring() const
806 return params().labelString();
810 // the next two functions are for the manual labels
811 string const Paragraph::getLabelWidthString() const
813 if (!params().labelWidthString().empty())
814 return params().labelWidthString();
816 return _("Senseless with this layout!");
820 void Paragraph::setLabelWidthString(string const & s)
822 params().labelWidthString(s);
826 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
829 params().labelWidthString(string());
830 params().align(LYX_ALIGN_LAYOUT);
831 params().spaceTop(VSpace(VSpace::NONE));
832 params().spaceBottom(VSpace(VSpace::NONE));
833 params().spacing(Spacing(Spacing::Default));
837 int Paragraph::beginningOfBody() const
839 if (layout()->labeltype != LABEL_MANUAL)
842 // Unroll the first two cycles of the loop
843 // and remember the previous character to
844 // remove unnecessary GetChar() calls
846 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
848 char previous_char = 0;
851 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
852 // Yes, this ^ is supposed to be "= " not "=="
855 && previous_char != ' '
856 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
858 previous_char = temp;
867 Paragraph * Paragraph::depthHook(depth_type depth)
869 Paragraph * newpar = this;
872 newpar = newpar->previous();
873 } while (newpar && newpar->getDepth() > depth);
876 if (previous() || getDepth())
877 lyxerr << "ERROR (Paragraph::DepthHook): "
886 Paragraph const * Paragraph::depthHook(depth_type depth) const
888 Paragraph const * newpar = this;
891 newpar = newpar->previous();
892 } while (newpar && newpar->getDepth() > depth);
895 if (previous() || getDepth())
896 lyxerr << "ERROR (Paragraph::DepthHook): "
905 Paragraph * Paragraph::outerHook()
909 return depthHook(depth_type(getDepth() - 1));
913 Paragraph const * Paragraph::outerHook() const
917 return depthHook(depth_type(getDepth() - 1));
921 // returns -1 if inset not found
922 int Paragraph::getPositionOfInset(Inset const * inset) const
925 InsetList::iterator it = insetlist.begin();
926 InsetList::iterator end = insetlist.end();
927 for (; it != end; ++it)
928 if (it.getInset() == inset)
934 InsetBibitem * Paragraph::bibitem()
936 InsetList::iterator it = insetlist.begin();
937 if (it != insetlist.end() && it.getInset()->lyxCode() == Inset::BIBTEX_CODE)
938 return static_cast<InsetBibitem *>(it.getInset());
944 // This could go to ParagraphParameters if we want to
945 int Paragraph::startTeXParParams(BufferParams const & bparams,
946 ostream & os, bool moving_arg) const
950 if (params().noindent()) {
955 switch (params().align()) {
957 case LYX_ALIGN_BLOCK:
958 case LYX_ALIGN_LAYOUT:
959 case LYX_ALIGN_SPECIAL:
962 case LYX_ALIGN_RIGHT:
963 case LYX_ALIGN_CENTER:
971 switch (params().align()) {
973 case LYX_ALIGN_BLOCK:
974 case LYX_ALIGN_LAYOUT:
975 case LYX_ALIGN_SPECIAL:
978 if (getParLanguage(bparams)->babel() != "hebrew") {
979 os << "\\begin{flushleft}";
982 os << "\\begin{flushright}";
986 case LYX_ALIGN_RIGHT:
987 if (getParLanguage(bparams)->babel() != "hebrew") {
988 os << "\\begin{flushright}";
991 os << "\\begin{flushleft}";
995 case LYX_ALIGN_CENTER:
996 os << "\\begin{center}";
1005 // This could go to ParagraphParameters if we want to
1006 int Paragraph::endTeXParParams(BufferParams const & bparams,
1007 ostream & os, bool moving_arg) const
1011 switch (params().align()) {
1012 case LYX_ALIGN_NONE:
1013 case LYX_ALIGN_BLOCK:
1014 case LYX_ALIGN_LAYOUT:
1015 case LYX_ALIGN_SPECIAL:
1017 case LYX_ALIGN_LEFT:
1018 case LYX_ALIGN_RIGHT:
1019 case LYX_ALIGN_CENTER:
1027 switch (params().align()) {
1028 case LYX_ALIGN_NONE:
1029 case LYX_ALIGN_BLOCK:
1030 case LYX_ALIGN_LAYOUT:
1031 case LYX_ALIGN_SPECIAL:
1033 case LYX_ALIGN_LEFT:
1034 if (getParLanguage(bparams)->babel() != "hebrew") {
1035 os << "\\end{flushleft}";
1038 os << "\\end{flushright}";
1042 case LYX_ALIGN_RIGHT:
1043 if (getParLanguage(bparams)->babel() != "hebrew") {
1044 os << "\\end{flushright}";
1047 os << "\\end{flushleft}";
1051 case LYX_ALIGN_CENTER:
1052 os << "\\end{center}";
1060 // This one spits out the text of the paragraph
1061 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1062 BufferParams const & bparams,
1063 ostream & os, TexRow & texrow,
1066 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1068 bool return_value = false;
1070 LyXLayout_ptr style;
1072 // well we have to check if we are in an inset with unlimited
1073 // lenght (all in one row) if that is true then we don't allow
1074 // any special options in the paragraph and also we don't allow
1075 // any environment other then "Standard" to be valid!
1077 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1080 style = bparams.getLyXTextClass().defaultLayout();
1087 // Maybe we have to create a optional argument.
1090 // FIXME: can we actually skip this check and just call
1091 // beginningOfBody() ??
1092 if (style->labeltype != LABEL_MANUAL) {
1095 body_pos = beginningOfBody();
1098 unsigned int column = 0;
1103 basefont = getLabelFont(bparams);
1105 basefont = getLayoutFont(bparams);
1108 moving_arg |= style->needprotect;
1110 // Which font is currently active?
1111 LyXFont running_font(basefont);
1112 // Do we have an open font change?
1113 bool open_font = false;
1115 Change::Type running_change = Change::UNCHANGED;
1117 texrow.start(this, 0);
1119 // if the paragraph is empty, the loop will not be entered at all
1121 if (style->isCommand()) {
1126 column += startTeXParParams(bparams, os, moving_arg);
1130 for (pos_type i = 0; i < size(); ++i) {
1132 // First char in paragraph or after label?
1133 if (i == body_pos) {
1136 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1139 basefont = getLayoutFont(bparams);
1140 running_font = basefont;
1144 if (style->isCommand()) {
1150 column += startTeXParParams(bparams, os,
1154 value_type c = getChar(i);
1156 // Fully instantiated font
1157 LyXFont font = getFont(bparams, i);
1159 LyXFont const last_font = running_font;
1161 // Spaces at end of font change are simulated to be
1162 // outside font change, i.e. we write "\textXX{text} "
1163 // rather than "\textXX{text }". (Asger)
1164 if (open_font && c == ' ' && i <= size() - 2) {
1165 LyXFont const & next_font = getFont(bparams, i + 1);
1166 if (next_font != running_font
1167 && next_font != font) {
1172 // We end font definition before blanks
1174 (font != running_font ||
1175 font.language() != running_font.language()))
1177 column += running_font.latexWriteEndChanges(os,
1179 (i == body_pos-1) ? basefont : font);
1180 running_font = basefont;
1184 // Blanks are printed before start of fontswitch
1186 // Do not print the separation of the optional argument
1187 if (i != body_pos - 1) {
1188 pimpl_->simpleTeXBlanks(os, texrow, i,
1189 column, font, *style);
1193 // Do we need to change font?
1194 if ((font != running_font ||
1195 font.language() != running_font.language()) &&
1198 column += font.latexWriteStartChanges(os, basefont,
1200 running_font = font;
1204 Change::Type change = pimpl_->lookupChange(i);
1206 column += Changes::latexMarkChange(os, running_change, change);
1207 running_change = change;
1209 if (c == Paragraph::META_NEWLINE) {
1210 // newlines are handled differently here than
1211 // the default in SimpleTeXSpecialChars().
1212 if (!style->newline_allowed) {
1216 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1219 basefont = getLayoutFont(bparams);
1220 running_font = basefont;
1221 if (font.family() ==
1222 LyXFont::TYPEWRITER_FAMILY) {
1231 texrow.start(this, i + 1);
1234 pimpl_->simpleTeXSpecialChars(buf, bparams,
1235 os, texrow, moving_arg,
1237 basefont, open_font,
1239 *style, i, column, c);
1243 column += Changes::latexMarkChange(os,
1244 running_change, Change::UNCHANGED);
1246 // If we have an open font definition, we have to close it
1248 #ifdef FIXED_LANGUAGE_END_DETECTION
1251 .latexWriteEndChanges(os, basefont,
1252 next_->getFont(bparams,
1255 running_font.latexWriteEndChanges(os, basefont,
1259 #ifdef WITH_WARNINGS
1260 //#warning For now we ALWAYS have to close the foreign font settings if they are
1261 //#warning there as we start another \selectlanguage with the next paragraph if
1262 //#warning we are in need of this. This should be fixed sometime (Jug)
1264 running_font.latexWriteEndChanges(os, basefont, basefont);
1268 // Needed if there is an optional argument but no contents.
1269 if (body_pos > 0 && body_pos == size()) {
1271 return_value = false;
1275 column += endTeXParParams(bparams, os, moving_arg);
1278 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1279 return return_value;
1285 bool Paragraph::isHfill(pos_type pos) const
1287 return IsInsetChar(getChar(pos))
1288 && getInset(pos)->lyxCode() == Inset::HFILL_CODE;
1292 bool Paragraph::isInset(pos_type pos) const
1294 return IsInsetChar(getChar(pos));
1298 bool Paragraph::isNewline(pos_type pos) const
1300 return pos >= 0 && IsNewlineChar(getChar(pos));
1304 bool Paragraph::isSeparator(pos_type pos) const
1306 return IsSeparatorChar(getChar(pos));
1310 bool Paragraph::isLineSeparator(pos_type pos) const
1312 value_type const c = getChar(pos);
1313 return IsLineSeparatorChar(c)
1314 || (IsInsetChar(c) && getInset(pos) &&
1315 getInset(pos)->isLineSeparator());
1319 bool Paragraph::isKomma(pos_type pos) const
1321 return IsKommaChar(getChar(pos));
1325 /// Used by the spellchecker
1326 bool Paragraph::isLetter(pos_type pos) const
1328 value_type const c = getChar(pos);
1329 if (IsLetterChar(c))
1332 return getInset(pos)->isLetter();
1333 // We want to pass the ' and escape chars to ispell
1334 string const extra = lyxrc.isp_esc_chars + '\'';
1335 return contains(extra, c);
1339 bool Paragraph::isWord(pos_type pos) const
1341 return IsWordChar(getChar(pos)) ;
1346 Paragraph::getParLanguage(BufferParams const & bparams) const
1349 return getFirstFontSettings().language();
1350 } else if (previous_)
1351 return previous_->getParLanguage(bparams);
1353 return bparams.language;
1357 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1359 return lyxrc.rtl_support
1360 && getParLanguage(bparams)->RightToLeft()
1361 && !(inInset() && inInset()->owner() &&
1362 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1366 void Paragraph::changeLanguage(BufferParams const & bparams,
1367 Language const * from, Language const * to)
1369 for (pos_type i = 0; i < size(); ++i) {
1370 LyXFont font = getFontSettings(bparams, i);
1371 if (font.language() == from) {
1372 font.setLanguage(to);
1379 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1381 Language const * doc_language = bparams.language;
1382 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1383 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1385 for (; cit != end; ++cit)
1386 if (cit->font().language() != ignore_language &&
1387 cit->font().language() != latex_language &&
1388 cit->font().language() != doc_language)
1394 // Convert the paragraph to a string.
1395 // Used for building the table of contents
1396 string const Paragraph::asString(Buffer const * buffer, bool label) const
1399 if (label && !params().labelString().empty())
1400 s += params().labelString() + ' ';
1402 for (pos_type i = 0; i < size(); ++i) {
1403 value_type c = getChar(i);
1406 else if (c == META_INSET &&
1407 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1409 getInset(i)->ascii(buffer, ost);
1410 s += subst(STRCONV(ost.str()),'\n',' ');
1418 string const Paragraph::asString(Buffer const * buffer,
1419 pos_type beg, pos_type end, bool label) const
1423 if (beg == 0 && label && !params().labelString().empty())
1424 os << params().labelString() << ' ';
1426 for (pos_type i = beg; i < end; ++i) {
1427 value_type const c = getUChar(buffer->params, i);
1430 else if (c == META_NEWLINE)
1432 else if (c == META_INSET)
1433 getInset(i)->ascii(buffer, os);
1436 return STRCONV(os.str());
1440 void Paragraph::setInsetOwner(Inset * i)
1442 pimpl_->inset_owner = i;
1443 InsetList::iterator it = insetlist.begin();
1444 InsetList::iterator end = insetlist.end();
1445 for (; it != end; ++it)
1447 it.getInset()->setOwner(i);
1451 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1454 insetlist.deleteInsetsLyXText(bv);
1458 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1461 insetlist.resizeInsetsLyXText(bv);
1465 void Paragraph::setContentsFromPar(Paragraph * par)
1467 pimpl_->setContentsFromPar(par);
1471 void Paragraph::trackChanges(Change::Type type)
1473 pimpl_->trackChanges(type);
1477 void Paragraph::untrackChanges()
1479 pimpl_->untrackChanges();
1483 void Paragraph::cleanChanges()
1485 pimpl_->cleanChanges();
1489 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1491 lyx::Assert(!size() || pos < size());
1492 return pimpl_->lookupChange(pos);
1496 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1498 lyx::Assert(!size() || pos < size());
1499 return pimpl_->lookupChangeFull(pos);
1503 bool Paragraph::isChanged(pos_type start, pos_type end) const
1505 return pimpl_->isChanged(start, end);
1509 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1511 return pimpl_->isChangeEdited(start, end);
1515 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1517 pimpl_->setChange(pos, type);
1522 void Paragraph::markErased()
1524 pimpl_->markErased();
1528 void Paragraph::acceptChange(pos_type start, pos_type end)
1530 return pimpl_->acceptChange(start, end);
1534 void Paragraph::rejectChange(pos_type start, pos_type end)
1536 return pimpl_->rejectChange(start, end);
1540 lyx::pos_type Paragraph::size() const
1542 return pimpl_->size();
1546 bool Paragraph::empty() const
1548 return pimpl_->empty();
1552 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1554 return pimpl_->getChar(pos);
1558 int Paragraph::id() const
1564 LyXLayout_ptr const & Paragraph::layout() const
1570 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1572 layout_ = new_layout;
1576 bool Paragraph::isFirstInSequence() const
1578 Paragraph const * dhook = depthHook(getDepth());
1579 return (dhook == this
1580 || dhook->layout() != layout()
1581 || dhook->getDepth() != getDepth());
1585 Inset * Paragraph::inInset() const
1587 return pimpl_->inset_owner;
1591 void Paragraph::clearContents()
1596 void Paragraph::setChar(pos_type pos, value_type c)
1598 pimpl_->setChar(pos, c);
1602 ParagraphParameters & Paragraph::params()
1604 return pimpl_->params;
1608 ParagraphParameters const & Paragraph::params() const
1610 return pimpl_->params;
1614 bool Paragraph::isFreeSpacing() const
1616 // for now we just need this, later should we need this in some
1617 // other way we can always add a function to Inset::() too.
1618 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1619 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);