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 * ====================================================== */
14 #pragma implementation
17 #include "paragraph.h"
18 #include "paragraph_pimpl.h"
22 #include "tex-strings.h"
24 #include "bufferparams.h"
27 #include "BufferView.h"
29 #include "ParameterStruct.h"
32 #include "insets/insetbib.h"
33 #include "insets/insetoptarg.h"
35 #include "support/filetools.h"
36 #include "support/lstrings.h"
37 #include "support/lyxmanip.h"
38 #include "support/FileInfo.h"
39 #include "support/LAssert.h"
40 #include "support/textutils.h"
50 using std::lower_bound;
51 using std::upper_bound;
56 // this is a bad idea, but how can Paragraph find its buffer to get
57 // parameters? (JMarc)
59 extern string bibitemWidest(Buffer const *);
61 // this is a minibuffer
66 LyXFont minibuffer_font;
67 Inset * minibuffer_inset;
72 extern BufferView * current_view;
75 Paragraph::Paragraph()
76 : pimpl_(new Paragraph::Pimpl(this))
84 bibkey = 0; // ale970302
90 // This constructor inserts the new paragraph in a list.
91 Paragraph::Paragraph(Paragraph * par)
92 : pimpl_(new Paragraph::Pimpl(this))
97 // double linked list begin
100 next_->previous_ = this;
102 previous_->next_ = this;
105 bibkey = 0; // ale970302
111 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
112 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
120 // this is because of the dummy layout of the paragraphs that
122 layout_ = lp.layout();
126 bibkey = static_cast<InsetBibKey *>
127 (lp.bibkey->clone(*current_view->buffer()));
132 // copy everything behind the break-position to the new paragraph
133 insetlist = lp.insetlist;
134 InsetList::iterator it = insetlist.begin();
135 InsetList::iterator end = insetlist.end();
136 for (; it != end; ++it) {
137 it.setInset(it.getInset()->clone(*current_view->buffer(),
139 // tell the new inset who is the boss now
140 it.getInset()->parOwner(this);
145 // the destructor removes the new paragraph from the list
146 Paragraph::~Paragraph()
150 previous_->next_ = next_;
152 next_->previous_ = previous_;
160 //lyxerr << "Paragraph::paragraph_id = "
161 // << Paragraph::paragraph_id << endl;
165 void Paragraph::write(Buffer const * buf, ostream & os,
166 BufferParams const & bparams,
167 depth_type & dth) const
169 // The beginning or end of a deeper (i.e. nested) area?
170 if (dth != params().depth()) {
171 if (params().depth() > dth) {
172 while (params().depth() > dth) {
173 os << "\n\\begin_deeper ";
177 while (params().depth() < dth) {
178 os << "\n\\end_deeper ";
184 // First write the layout
185 os << "\n\\layout " << layout()->name() << "\n";
187 // Maybe some vertical spaces.
188 if (params().spaceTop().kind() != VSpace::NONE)
189 os << "\\added_space_top "
190 << params().spaceTop().asLyXCommand() << " ";
191 if (params().spaceBottom().kind() != VSpace::NONE)
192 os << "\\added_space_bottom "
193 << params().spaceBottom().asLyXCommand() << " ";
195 // Maybe the paragraph has special spacing
196 params().spacing().writeFile(os, true);
198 // The labelwidth string used in lists.
199 if (!params().labelWidthString().empty())
200 os << "\\labelwidthstring "
201 << params().labelWidthString() << '\n';
203 // Lines above or below?
204 if (params().lineTop())
206 if (params().lineBottom())
207 os << "\\line_bottom ";
209 // Pagebreaks above or below?
210 if (params().pagebreakTop())
211 os << "\\pagebreak_top ";
212 if (params().pagebreakBottom())
213 os << "\\pagebreak_bottom ";
215 // Start of appendix?
216 if (params().startOfAppendix())
217 os << "\\start_of_appendix ";
220 if (params().noindent())
223 // Do we have a manual left indent?
224 if (!params().leftIndent().zero())
225 os << "\\leftindent " << params().leftIndent().asString() << " ";
228 if (params().align() != LYX_ALIGN_LAYOUT) {
230 switch (params().align()) {
231 case LYX_ALIGN_LEFT: h = 1; break;
232 case LYX_ALIGN_RIGHT: h = 2; break;
233 case LYX_ALIGN_CENTER: h = 3; break;
234 default: h = 0; break;
236 os << "\\align " << string_align[h] << " ";
241 bibkey->write(buf, os);
243 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
246 for (pos_type i = 0; i < size(); ++i) {
252 // Write font changes
253 LyXFont font2 = getFontSettings(bparams, i);
254 if (font2 != font1) {
255 font2.lyxWriteChanges(font1, os);
260 value_type const c = getChar(i);
264 Inset const * inset = getInset(i);
266 if (inset->directWrite()) {
267 // international char, let it write
268 // code directly so it's shorter in
270 inset->write(buf, os);
272 os << "\n\\begin_inset ";
273 inset->write(buf, os);
274 os << "\n\\end_inset \n\n";
280 os << "\n\\newline \n";
284 os << "\n\\hfill \n";
288 os << "\n\\backslash \n";
292 if (i + 1 < size() && getChar(i + 1) == ' ') {
299 if ((column > 70 && c == ' ')
304 // this check is to amend a bug. LyX sometimes
305 // inserts '\0' this could cause problems.
309 lyxerr << "ERROR (Paragraph::writeFile):"
310 " NULL char in structure." << endl;
318 void Paragraph::validate(LaTeXFeatures & features) const
320 pimpl_->validate(features, *layout());
324 // First few functions needed for cut and paste and paragraph breaking.
325 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
327 BufferParams bparams = buffer.params;
329 minibuffer_char = getChar(pos);
330 minibuffer_font = getFontSettings(bparams, pos);
331 minibuffer_inset = 0;
332 if (minibuffer_char == Paragraph::META_INSET) {
334 minibuffer_inset = getInset(pos)->clone(buffer);
336 minibuffer_inset = 0;
337 minibuffer_char = ' ';
338 // This reflects what GetInset() does (ARRae)
344 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
346 minibuffer_char = getChar(pos);
347 minibuffer_font = getFontSettings(bparams, pos);
348 minibuffer_inset = 0;
349 if (minibuffer_char == Paragraph::META_INSET) {
351 // the inset is not in a paragraph anymore
352 minibuffer_inset = insetlist.release(pos);
353 minibuffer_inset->parOwner(0);
355 minibuffer_inset = 0;
356 minibuffer_char = ' ';
357 // This reflects what GetInset() does (ARRae)
362 // Erase(pos); now the caller is responsible for that.
366 bool Paragraph::insertFromMinibuffer(pos_type pos)
368 if (minibuffer_char == Paragraph::META_INSET) {
369 if (!insetAllowed(minibuffer_inset->lyxCode())) {
372 insertInset(pos, minibuffer_inset, minibuffer_font);
374 LyXFont f = minibuffer_font;
375 if (!checkInsertChar(f)) {
378 insertChar(pos, minibuffer_char, f);
386 void Paragraph::erase(pos_type pos)
392 bool Paragraph::checkInsertChar(LyXFont & font)
394 if (pimpl_->inset_owner)
395 return pimpl_->inset_owner->checkInsertChar(font);
400 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
402 LyXFont const f(LyXFont::ALL_INHERIT);
403 insertChar(pos, c, f);
407 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
408 LyXFont const & font)
410 pimpl_->insertChar(pos, c, font);
414 void Paragraph::insertInset(pos_type pos, Inset * inset)
416 LyXFont const f(LyXFont::ALL_INHERIT);
417 insertInset(pos, inset, f);
421 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
423 pimpl_->insertInset(pos, inset, font);
427 bool Paragraph::insetAllowed(Inset::Code code)
429 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
431 if (pimpl_->inset_owner)
432 return pimpl_->inset_owner->insetAllowed(code);
437 Inset * Paragraph::getInset(pos_type pos)
439 lyx::Assert(pos < size());
441 return insetlist.get(pos);
445 Inset const * Paragraph::getInset(pos_type pos) const
447 lyx::Assert(pos < size());
449 return insetlist.get(pos);
453 // Gets uninstantiated font setting at position.
454 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
457 lyx::Assert(pos <= size());
459 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
460 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
461 for (; cit != end; ++cit) {
462 if (cit->pos() >= pos)
468 retfont = cit->font();
469 } else if (pos == size() && !empty()) {
470 retfont = getFontSettings(bparams, pos - 1);
472 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
478 // Gets uninstantiated font setting at position 0
479 LyXFont const Paragraph::getFirstFontSettings() const
481 if (!empty() && !pimpl_->fontlist.empty())
482 return pimpl_->fontlist[0].font();
484 return LyXFont(LyXFont::ALL_INHERIT);
488 // Gets the fully instantiated font at a given position in a paragraph
489 // This is basically the same function as LyXText::GetFont() in text2.C.
490 // The difference is that this one is used for generating the LaTeX file,
491 // and thus cosmetic "improvements" are disallowed: This has to deliver
492 // the true picture of the buffer. (Asger)
493 // If position is -1, we get the layout font of the paragraph.
494 // If position is -2, we get the font of the manual label of the paragraph.
495 LyXFont const Paragraph::getFont(BufferParams const & bparams,
498 lyx::Assert(pos >= 0);
500 LyXLayout_ptr const & lout = layout();
502 pos_type main_body = 0;
503 if (lout->labeltype == LABEL_MANUAL)
504 main_body = beginningOfMainBody();
508 layoutfont = lout->labelfont;
510 layoutfont = lout->font;
512 LyXFont tmpfont = getFontSettings(bparams, pos);
513 tmpfont.realize(layoutfont);
515 return pimpl_->realizeFont(tmpfont, bparams);
519 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
521 LyXLayout_ptr const & lout = layout();
523 LyXFont tmpfont = lout->labelfont;
524 tmpfont.setLanguage(getParLanguage(bparams));
526 return pimpl_->realizeFont(tmpfont, bparams);
530 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
532 LyXLayout_ptr const & lout = layout();
534 LyXFont tmpfont = lout->font;
535 tmpfont.setLanguage(getParLanguage(bparams));
537 return pimpl_->realizeFont(tmpfont, bparams);
541 /// Returns the height of the highest font in range
543 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
544 LyXFont::FONT_SIZE const def_size) const
546 if (pimpl_->fontlist.empty())
549 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
550 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
551 for (; end_it != end; ++end_it) {
552 if (end_it->pos() >= endpos)
559 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
560 for (; cit != end; ++cit) {
561 if (cit->pos() >= startpos)
565 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
566 for (; cit != end_it; ++cit) {
567 LyXFont::FONT_SIZE size = cit->font().size();
568 if (size == LyXFont::INHERIT_SIZE)
570 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
577 Paragraph::value_type
578 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
580 value_type c = getChar(pos);
581 if (!lyxrc.rtl_support)
611 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
618 void Paragraph::setFont(pos_type pos, LyXFont const & font)
620 lyx::Assert(pos <= size());
622 // First, reduce font against layout/label font
623 // Update: The SetCharFont() routine in text2.C already
624 // reduces font, so we don't need to do that here. (Asger)
625 // No need to simplify this because it will disappear
626 // in a new kernel. (Asger)
627 // Next search font table
629 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
630 Pimpl::FontList::iterator it = beg;
631 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
632 for (; it != endit; ++it) {
633 if (it->pos() >= pos)
636 unsigned int i = std::distance(beg, it);
637 bool notfound = (it == endit);
639 if (!notfound && pimpl_->fontlist[i].font() == font)
642 bool begin = pos == 0 || notfound ||
643 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
644 // Is position pos is a beginning of a font block?
645 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
646 // Is position pos is the end of a font block?
647 if (begin && end) { // A single char block
648 if (i + 1 < pimpl_->fontlist.size() &&
649 pimpl_->fontlist[i + 1].font() == font) {
650 // Merge the singleton block with the next block
651 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
652 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
653 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
654 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
655 // Merge the singleton block with the previous block
656 pimpl_->fontlist[i - 1].pos(pos);
657 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
659 pimpl_->fontlist[i].font(font);
661 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
662 pimpl_->fontlist[i - 1].pos(pos);
664 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
665 Pimpl::FontTable(pos, font));
667 pimpl_->fontlist[i].pos(pos - 1);
668 if (!(i + 1 < pimpl_->fontlist.size() &&
669 pimpl_->fontlist[i + 1].font() == font))
670 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
671 Pimpl::FontTable(pos, font));
672 } else { // The general case. The block is splitted into 3 blocks
673 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
674 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
675 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
676 Pimpl::FontTable(pos, font));
682 void Paragraph::next(Paragraph * p)
688 // This function is able to hide closed footnotes.
689 Paragraph * Paragraph::next()
695 Paragraph const * Paragraph::next() const
701 void Paragraph::previous(Paragraph * p)
707 // This function is able to hide closed footnotes.
708 Paragraph * Paragraph::previous()
714 // This function is able to hide closed footnotes.
715 Paragraph const * Paragraph::previous() const
722 void Paragraph::makeSameLayout(Paragraph const * par)
724 layout(par->layout());
726 params() = par->params();
730 int Paragraph::stripLeadingSpaces()
732 if (layout()->free_spacing ||
738 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
747 bool Paragraph::hasSameLayout(Paragraph const * par) const
750 par->layout() == layout() &&
751 params().sameLayout(par->params());
755 int Paragraph::getEndLabel() const
757 Paragraph const * par = this;
758 depth_type par_depth = getDepth();
760 LyXLayout_ptr const & layout = par->layout();
761 int const endlabeltype = layout->endlabeltype;
763 if (endlabeltype != END_LABEL_NO_LABEL) {
767 depth_type const next_depth = next_->getDepth();
768 if (par_depth > next_depth ||
769 (par_depth == next_depth
770 && layout != next_->layout()))
776 par = par->outerHook();
778 par_depth = par->getDepth();
780 return END_LABEL_NO_LABEL;
784 Paragraph::depth_type Paragraph::getDepth() const
786 return params().depth();
790 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
792 bool const isenv = layout()->isEnvironment();
795 return params().depth() + 1;
797 return params().depth();
801 char Paragraph::getAlign() const
803 return params().align();
807 string const & Paragraph::getLabelstring() const
809 return params().labelString();
813 // the next two functions are for the manual labels
814 string const Paragraph::getLabelWidthString() const
816 if (!params().labelWidthString().empty())
817 return params().labelWidthString();
819 return _("Senseless with this layout!");
823 void Paragraph::setLabelWidthString(string const & s)
825 params().labelWidthString(s);
829 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
832 params().labelWidthString(string());
833 params().align(LYX_ALIGN_LAYOUT);
834 params().spaceTop(VSpace(VSpace::NONE));
835 params().spaceBottom(VSpace(VSpace::NONE));
836 params().spacing(Spacing(Spacing::Default));
840 // if the layout of a paragraph contains a manual label, the beginning of the
841 // main body is the beginning of the second word. This is what the par-
842 // function returns. If the layout does not contain a label, the main
843 // body always starts with position 0. This differentiation is necessary,
844 // because there cannot be a newline or a blank <= the beginning of the
847 int Paragraph::beginningOfMainBody() const
849 // Unroll the first two cycles of the loop
850 // and remember the previous character to
851 // remove unnecessary GetChar() calls
853 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
855 char previous_char = 0;
858 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
859 // Yes, this ^ is supposed to be "= " not "=="
862 && previous_char != ' '
863 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
865 previous_char = temp;
874 Paragraph * Paragraph::depthHook(depth_type depth)
876 Paragraph * newpar = this;
879 newpar = newpar->previous();
880 } while (newpar && newpar->getDepth() > depth);
883 if (previous() || getDepth())
884 lyxerr << "ERROR (Paragraph::DepthHook): "
893 Paragraph const * Paragraph::depthHook(depth_type depth) const
895 Paragraph const * newpar = this;
898 newpar = newpar->previous();
899 } while (newpar && newpar->getDepth() > depth);
902 if (previous() || getDepth())
903 lyxerr << "ERROR (Paragraph::DepthHook): "
912 Paragraph * Paragraph::outerHook()
916 return depthHook(depth_type(getDepth() - 1));
920 Paragraph const * Paragraph::outerHook() const
924 return depthHook(depth_type(getDepth() - 1));
928 // returns -1 if inset not found
929 int Paragraph::getPositionOfInset(Inset const * inset) const
932 InsetList::iterator it = insetlist.begin();
933 InsetList::iterator end = insetlist.end();
934 for (; it != end; ++it) {
935 if (it.getInset() == inset) {
947 InsetOptArg * optArgInset(Paragraph const & par)
950 InsetList::iterator it = par.insetlist.begin();
951 InsetList::iterator end = par.insetlist.end();
952 for (; it != end; ++it) {
953 Inset * ins = it.getInset();
954 if (ins->lyxCode() == Inset::OPTARG_CODE) {
955 return static_cast<InsetOptArg *>(ins);
963 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
964 BufferParams const & bparams,
965 ostream & os, TexRow & texrow,
968 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
969 Inset const * in = inInset();
970 bool further_blank_line = false;
973 // well we have to check if we are in an inset with unlimited
974 // lenght (all in one row) if that is true then we don't allow
975 // any special options in the paragraph and also we don't allow
976 // any environment other then "Standard" to be valid!
977 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
980 if (params().startOfAppendix()) {
981 os << "\\appendix\n";
985 if (!params().spacing().isDefault()
986 && (!previous() || !previous()->hasSameLayout(this))) {
987 os << params().spacing().writeEnvirBegin() << "\n";
991 if (style->isCommand()) {
996 if (params().pagebreakTop()) {
998 further_blank_line = true;
1000 if (params().spaceTop().kind() != VSpace::NONE) {
1001 os << params().spaceTop().asLatexCommand(bparams);
1002 further_blank_line = true;
1005 if (params().lineTop()) {
1006 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1007 << "\\vspace{-1\\parskip}";
1008 further_blank_line = true;
1011 if (further_blank_line) {
1016 style = bparams.getLyXTextClass().defaultLayout();
1019 Language const * language = getParLanguage(bparams);
1020 Language const * doc_language = bparams.language;
1021 Language const * previous_language = previous()
1022 ? previous()->getParLanguage(bparams) : doc_language;
1024 if (language->babel() != previous_language->babel()
1025 // check if we already put language command in TeXEnvironment()
1026 && !(style->isEnvironment()
1028 (previous()->layout() != layout() &&
1029 previous()->getDepth() <= getDepth())
1030 || previous()->getDepth() < getDepth())))
1032 if (!lyxrc.language_command_end.empty() &&
1033 previous_language->babel() != doc_language->babel())
1035 os << subst(lyxrc.language_command_end, "$$lang",
1036 previous_language->babel())
1041 if (lyxrc.language_command_end.empty() ||
1042 language->babel() != doc_language->babel())
1044 os << subst(lyxrc.language_command_begin, "$$lang",
1051 if (bparams.inputenc == "auto" &&
1052 language->encoding() != previous_language->encoding()) {
1053 os << "\\inputencoding{"
1054 << language->encoding()->LatexName()
1059 switch (style->latextype) {
1062 << style->latexname();
1064 // Separate handling of optional argument inset.
1065 if (style->optionalargs == 1) {
1066 InsetOptArg * it = optArgInset(*this);
1068 it->latexOptional(buf, os, false, false);
1071 os << style->latexparam();
1073 case LATEX_ITEM_ENVIRONMENT:
1075 bibkey->latex(buf, os, false, false);
1079 case LATEX_LIST_ENVIRONMENT:
1086 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1088 // Make sure that \\par is done with the font of the last
1089 // character if this has another size as the default.
1090 // This is necessary because LaTeX (and LyX on the screen)
1091 // calculates the space between the baselines according
1092 // to this font. (Matthias)
1094 // Is this really needed ? (Dekel)
1095 // We do not need to use to change the font for the last paragraph
1096 // or for a command.
1097 LyXFont const font =
1099 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1101 bool is_command = style->isCommand();
1103 if (style->resfont.size() != font.size() && next_ && !is_command) {
1106 os << "\\" << font.latexSize() << " \\par}";
1107 } else if (need_par) {
1109 } else if (is_command)
1112 switch (style->latextype) {
1113 case LATEX_ITEM_ENVIRONMENT:
1114 case LATEX_LIST_ENVIRONMENT:
1115 if (next_ && (params().depth() < next_->params().depth())) {
1120 case LATEX_ENVIRONMENT:
1121 // if its the last paragraph of the current environment
1122 // skip it otherwise fall through
1124 && (next_->layout() != layout()
1125 || next_->params().depth() != params().depth()))
1127 // fall through possible
1129 // we don't need it for the last paragraph!!!
1136 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1137 further_blank_line = false;
1138 if (params().lineBottom()) {
1139 os << "\\lyxline{\\" << font.latexSize() << '}';
1140 further_blank_line = true;
1143 if (params().spaceBottom().kind() != VSpace::NONE) {
1144 os << params().spaceBottom().asLatexCommand(bparams);
1145 further_blank_line = true;
1148 if (params().pagebreakBottom()) {
1150 further_blank_line = true;
1153 if (further_blank_line) {
1158 if (!params().spacing().isDefault()
1159 && (!next_ || !next_->hasSameLayout(this))) {
1160 os << params().spacing().writeEnvirEnd() << "\n";
1165 // we don't need it for the last paragraph!!!
1170 // Since \selectlanguage write the language to the aux file,
1171 // we need to reset the language at the end of footnote or
1174 if (language->babel() != doc_language->babel()) {
1175 if (lyxrc.language_command_end.empty())
1176 os << subst(lyxrc.language_command_begin,
1178 doc_language->babel())
1181 os << subst(lyxrc.language_command_end,
1189 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1194 // This could go to ParagraphParameters if we want to
1195 int Paragraph::startTeXParParams(BufferParams const & bparams,
1196 ostream & os, bool moving_arg) const
1200 if (params().noindent()) {
1201 os << "\\noindent ";
1205 switch (params().align()) {
1206 case LYX_ALIGN_NONE:
1207 case LYX_ALIGN_BLOCK:
1208 case LYX_ALIGN_LAYOUT:
1209 case LYX_ALIGN_SPECIAL:
1211 case LYX_ALIGN_LEFT:
1212 case LYX_ALIGN_RIGHT:
1213 case LYX_ALIGN_CENTER:
1221 switch (params().align()) {
1222 case LYX_ALIGN_NONE:
1223 case LYX_ALIGN_BLOCK:
1224 case LYX_ALIGN_LAYOUT:
1225 case LYX_ALIGN_SPECIAL:
1227 case LYX_ALIGN_LEFT:
1228 if (getParLanguage(bparams)->babel() != "hebrew") {
1229 os << "\\begin{flushleft}";
1232 os << "\\begin{flushright}";
1236 case LYX_ALIGN_RIGHT:
1237 if (getParLanguage(bparams)->babel() != "hebrew") {
1238 os << "\\begin{flushright}";
1241 os << "\\begin{flushleft}";
1245 case LYX_ALIGN_CENTER:
1246 os << "\\begin{center}";
1255 // This could go to ParagraphParameters if we want to
1256 int Paragraph::endTeXParParams(BufferParams const & bparams,
1257 ostream & os, bool moving_arg) const
1261 switch (params().align()) {
1262 case LYX_ALIGN_NONE:
1263 case LYX_ALIGN_BLOCK:
1264 case LYX_ALIGN_LAYOUT:
1265 case LYX_ALIGN_SPECIAL:
1267 case LYX_ALIGN_LEFT:
1268 case LYX_ALIGN_RIGHT:
1269 case LYX_ALIGN_CENTER:
1277 switch (params().align()) {
1278 case LYX_ALIGN_NONE:
1279 case LYX_ALIGN_BLOCK:
1280 case LYX_ALIGN_LAYOUT:
1281 case LYX_ALIGN_SPECIAL:
1283 case LYX_ALIGN_LEFT:
1284 if (getParLanguage(bparams)->babel() != "hebrew") {
1285 os << "\\end{flushleft}";
1288 os << "\\end{flushright}";
1292 case LYX_ALIGN_RIGHT:
1293 if (getParLanguage(bparams)->babel() != "hebrew") {
1294 os << "\\end{flushright}";
1297 os << "\\end{flushleft}";
1301 case LYX_ALIGN_CENTER:
1302 os << "\\end{center}";
1310 // This one spits out the text of the paragraph
1311 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1312 BufferParams const & bparams,
1313 ostream & os, TexRow & texrow,
1316 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1318 bool return_value = false;
1320 LyXLayout_ptr style;
1322 // well we have to check if we are in an inset with unlimited
1323 // lenght (all in one row) if that is true then we don't allow
1324 // any special options in the paragraph and also we don't allow
1325 // any environment other then "Standard" to be valid!
1327 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1330 style = bparams.getLyXTextClass().defaultLayout();
1337 // Maybe we have to create a optional argument.
1339 if (style->labeltype != LABEL_MANUAL)
1342 main_body = beginningOfMainBody();
1346 if (main_body > 0) {
1349 basefont = getLabelFont(bparams);
1351 basefont = getLayoutFont(bparams);
1354 moving_arg |= style->needprotect;
1356 // Which font is currently active?
1357 LyXFont running_font(basefont);
1358 // Do we have an open font change?
1359 bool open_font = false;
1361 texrow.start(this, 0);
1363 // if the paragraph is empty, the loop will not be entered at all
1365 if (style->isCommand()) {
1370 column += startTeXParParams(bparams, os, moving_arg);
1374 for (pos_type i = 0; i < size(); ++i) {
1376 // First char in paragraph or after label?
1377 if (i == main_body) {
1378 if (main_body > 0) {
1380 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1383 basefont = getLayoutFont(bparams);
1384 running_font = basefont;
1388 if (style->isCommand()) {
1394 column += startTeXParParams(bparams, os,
1398 value_type c = getChar(i);
1400 // Fully instantiated font
1401 LyXFont font = getFont(bparams, i);
1403 LyXFont const last_font = running_font;
1405 // Spaces at end of font change are simulated to be
1406 // outside font change, i.e. we write "\textXX{text} "
1407 // rather than "\textXX{text }". (Asger)
1408 if (open_font && c == ' ' && i <= size() - 2) {
1409 LyXFont const & next_font = getFont(bparams, i + 1);
1410 if (next_font != running_font
1411 && next_font != font) {
1416 // We end font definition before blanks
1418 (font != running_font ||
1419 font.language() != running_font.language()))
1421 column += running_font.latexWriteEndChanges(os,
1423 (i == main_body-1) ? basefont : font);
1424 running_font = basefont;
1428 // Blanks are printed before start of fontswitch
1430 // Do not print the separation of the optional argument
1431 if (i != main_body - 1) {
1432 pimpl_->simpleTeXBlanks(os, texrow, i,
1433 column, font, *style);
1437 // Do we need to change font?
1438 if ((font != running_font ||
1439 font.language() != running_font.language()) &&
1442 column += font.latexWriteStartChanges(os, basefont,
1444 running_font = font;
1448 if (c == Paragraph::META_NEWLINE) {
1449 // newlines are handled differently here than
1450 // the default in SimpleTeXSpecialChars().
1451 if (!style->newline_allowed) {
1455 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1458 basefont = getLayoutFont(bparams);
1459 running_font = basefont;
1460 if (font.family() ==
1461 LyXFont::TYPEWRITER_FAMILY) {
1470 texrow.start(this, i + 1);
1473 pimpl_->simpleTeXSpecialChars(buf, bparams,
1474 os, texrow, moving_arg,
1476 basefont, open_font,
1477 *style, i, column, c);
1481 // If we have an open font definition, we have to close it
1483 #ifdef FIXED_LANGUAGE_END_DETECTION
1486 .latexWriteEndChanges(os, basefont,
1487 next_->getFont(bparams,
1490 running_font.latexWriteEndChanges(os, basefont,
1494 #ifdef WITH_WARNINGS
1495 //#warning For now we ALWAYS have to close the foreign font settings if they are
1496 //#warning there as we start another \selectlanguage with the next paragraph if
1497 //#warning we are in need of this. This should be fixed sometime (Jug)
1499 running_font.latexWriteEndChanges(os, basefont, basefont);
1503 // Needed if there is an optional argument but no contents.
1504 if (main_body > 0 && main_body == size()) {
1506 return_value = false;
1510 column += endTeXParParams(bparams, os, moving_arg);
1513 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1514 return return_value;
1518 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1519 BufferParams const & bparams,
1520 ostream & os, TexRow & texrow)
1522 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1524 LyXLayout_ptr const & style = layout();
1526 Language const * language = getParLanguage(bparams);
1527 Language const * doc_language = bparams.language;
1528 Language const * previous_language = previous_
1529 ? previous_->getParLanguage(bparams) : doc_language;
1530 if (language->babel() != previous_language->babel()) {
1532 if (!lyxrc.language_command_end.empty() &&
1533 previous_language->babel() != doc_language->babel()) {
1534 os << subst(lyxrc.language_command_end, "$$lang",
1535 previous_language->babel())
1540 if (lyxrc.language_command_end.empty() ||
1541 language->babel() != doc_language->babel()) {
1542 os << subst(lyxrc.language_command_begin, "$$lang",
1549 bool leftindent_open = false;
1550 if (!params().leftIndent().zero()) {
1551 os << "\\begin{LyXParagraphLeftIndent}{" <<
1552 params().leftIndent().asLatexString() << "}\n";
1554 leftindent_open = true;
1557 if (style->isEnvironment()) {
1558 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1559 os << "\\begin{" << style->latexname() << "}{"
1560 << params().labelWidthString() << "}\n";
1561 } else if (style->labeltype == LABEL_BIBLIO) {
1563 os << "\\begin{" << style->latexname() << "}{"
1564 << bibitemWidest(buf)
1566 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1567 os << "\\begin{" << style->latexname() << '}'
1568 << style->latexparam() << '\n';
1570 os << "\\begin{" << style->latexname() << '}'
1571 << style->latexparam() << '\n';
1574 Paragraph * par = this;
1576 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1578 if (par && par->params().depth() > params().depth()) {
1579 if (par->layout()->isParagraph()) {
1582 // How to handle this? (Lgb)
1583 //&& !suffixIs(os, "\n\n")
1585 // There should be at least one '\n' already
1586 // but we need there to be two for Standard
1587 // paragraphs that are depth-increment'ed to be
1588 // output correctly. However, tables can
1589 // also be paragraphs so don't adjust them.
1592 // Will it ever harm to have one '\n' too
1593 // many? i.e. that we sometimes will have
1594 // three in a row. (Lgb)
1598 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1601 && par->layout() == layout()
1602 && par->params().depth() == params().depth()
1603 && par->params().leftIndent() == params().leftIndent());
1605 if (style->isEnvironment()) {
1606 os << "\\end{" << style->latexname() << "}\n";
1610 if (leftindent_open) {
1611 os << "\\end{LyXParagraphLeftIndent}\n";
1615 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1616 return par; // ale970302
1620 bool Paragraph::isHfill(pos_type pos) const
1622 return IsHfillChar(getChar(pos));
1626 bool Paragraph::isInset(pos_type pos) const
1628 return IsInsetChar(getChar(pos));
1632 bool Paragraph::isNewline(pos_type pos) const
1634 return pos >= 0 && IsNewlineChar(getChar(pos));
1638 bool Paragraph::isSeparator(pos_type pos) const
1640 return IsSeparatorChar(getChar(pos));
1644 bool Paragraph::isLineSeparator(pos_type pos) const
1646 value_type const c = getChar(pos);
1647 return IsLineSeparatorChar(c)
1648 || (IsInsetChar(c) && getInset(pos) &&
1649 getInset(pos)->isLineSeparator());
1653 bool Paragraph::isKomma(pos_type pos) const
1655 return IsKommaChar(getChar(pos));
1659 /// Used by the spellchecker
1660 bool Paragraph::isLetter(pos_type pos) const
1662 value_type const c = getChar(pos);
1663 if (IsLetterChar(c))
1666 return getInset(pos)->isLetter();
1667 // We want to pass the ' and escape chars to ispell
1668 string const extra = lyxrc.isp_esc_chars + '\'';
1669 return contains(extra, c);
1673 bool Paragraph::isWord(pos_type pos) const
1675 return IsWordChar(getChar(pos)) ;
1680 Paragraph::getParLanguage(BufferParams const & bparams) const
1683 return getFirstFontSettings().language();
1684 } else if (previous_)
1685 return previous_->getParLanguage(bparams);
1687 return bparams.language;
1691 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1693 return lyxrc.rtl_support
1694 && getParLanguage(bparams)->RightToLeft()
1695 && !(inInset() && inInset()->owner() &&
1696 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1700 void Paragraph::changeLanguage(BufferParams const & bparams,
1701 Language const * from, Language const * to)
1703 for (pos_type i = 0; i < size(); ++i) {
1704 LyXFont font = getFontSettings(bparams, i);
1705 if (font.language() == from) {
1706 font.setLanguage(to);
1713 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1715 Language const * doc_language = bparams.language;
1716 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1717 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1719 for (; cit != end; ++cit)
1720 if (cit->font().language() != ignore_language &&
1721 cit->font().language() != latex_language &&
1722 cit->font().language() != doc_language)
1728 // Convert the paragraph to a string.
1729 // Used for building the table of contents
1730 string const Paragraph::asString(Buffer const * buffer, bool label) const
1732 BufferParams const & bparams = buffer->params;
1734 if (label && !params().labelString().empty())
1735 s += params().labelString() + ' ';
1736 string::size_type const len = s.size();
1738 for (pos_type i = 0; i < size(); ++i) {
1739 value_type c = getChar(i);
1742 else if (c == META_INSET &&
1743 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1745 getInset(i)->ascii(buffer, ost);
1746 s += subst(STRCONV(ost.str()),'\n',' ');
1750 if (isRightToLeftPar(bparams))
1751 reverse(s.begin() + len,s.end());
1757 string const Paragraph::asString(Buffer const * buffer,
1758 pos_type beg, pos_type end, bool label) const
1762 if (beg == 0 && label && !params().labelString().empty())
1763 ost << params().labelString() << ' ';
1765 for (pos_type i = beg; i < end; ++i) {
1766 value_type const c = getUChar(buffer->params, i);
1769 else if (c == META_NEWLINE)
1771 else if (c == META_HFILL)
1773 else if (c == META_INSET) {
1774 getInset(i)->ascii(buffer, ost);
1778 return STRCONV(ost.str());
1782 void Paragraph::setInsetOwner(Inset * i)
1784 pimpl_->inset_owner = i;
1785 InsetList::iterator it = insetlist.begin();
1786 InsetList::iterator end = insetlist.end();
1787 for (; it != end; ++it) {
1789 it.getInset()->setOwner(i);
1794 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1797 insetlist.deleteInsetsLyXText(bv);
1801 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1804 insetlist.resizeInsetsLyXText(bv);
1808 void Paragraph::setContentsFromPar(Paragraph * par)
1810 pimpl_->setContentsFromPar(par);
1814 lyx::pos_type Paragraph::size() const
1816 return pimpl_->size();
1820 bool Paragraph::empty() const
1822 return pimpl_->empty();
1826 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1828 return pimpl_->getChar(pos);
1832 int Paragraph::id() const
1838 LyXLayout_ptr const & Paragraph::layout() const
1844 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1846 layout_ = new_layout;
1850 bool Paragraph::isFirstInSequence() const
1852 Paragraph const * dhook = depthHook(getDepth());
1853 return (dhook == this
1854 || dhook->layout() != layout()
1855 || dhook->getDepth() != getDepth());
1859 Inset * Paragraph::inInset() const
1861 return pimpl_->inset_owner;
1865 void Paragraph::clearContents()
1870 void Paragraph::setChar(pos_type pos, value_type c)
1872 pimpl_->setChar(pos, c);
1876 ParagraphParameters & Paragraph::params()
1878 return pimpl_->params;
1882 ParagraphParameters const & Paragraph::params() const
1884 return pimpl_->params;
1888 Paragraph * Paragraph::getParFromID(int id) const
1890 return pimpl_->getParFromID(id);
1894 bool Paragraph::isFreeSpacing() const
1896 // for now we just need this, later should we need this in some
1897 // other way we can always add a function to Inset::() too.
1898 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1899 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);