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()
229 if (params().align() != LYX_ALIGN_LAYOUT) {
231 switch (params().align()) {
232 case LYX_ALIGN_LEFT: h = 1; break;
233 case LYX_ALIGN_RIGHT: h = 2; break;
234 case LYX_ALIGN_CENTER: h = 3; break;
235 default: h = 0; break;
237 os << "\\align " << string_align[h] << ' ';
242 bibkey->write(buf, os);
244 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
247 for (pos_type i = 0; i < size(); ++i) {
253 // Write font changes
254 LyXFont font2 = getFontSettings(bparams, i);
255 if (font2 != font1) {
256 font2.lyxWriteChanges(font1, os);
261 value_type const c = getChar(i);
265 Inset const * inset = getInset(i);
267 if (inset->directWrite()) {
268 // international char, let it write
269 // code directly so it's shorter in
271 inset->write(buf, os);
273 os << "\n\\begin_inset ";
274 inset->write(buf, os);
275 os << "\n\\end_inset \n\n";
281 os << "\n\\newline \n";
285 os << "\n\\hfill \n";
289 os << "\n\\backslash \n";
293 if (i + 1 < size() && getChar(i + 1) == ' ') {
300 if ((column > 70 && c == ' ')
305 // this check is to amend a bug. LyX sometimes
306 // inserts '\0' this could cause problems.
310 lyxerr << "ERROR (Paragraph::writeFile):"
311 " NULL char in structure." << endl;
319 void Paragraph::validate(LaTeXFeatures & features) const
321 pimpl_->validate(features, *layout());
325 // First few functions needed for cut and paste and paragraph breaking.
326 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
328 BufferParams bparams = buffer.params;
330 minibuffer_char = getChar(pos);
331 minibuffer_font = getFontSettings(bparams, pos);
332 minibuffer_inset = 0;
333 if (minibuffer_char == Paragraph::META_INSET) {
335 minibuffer_inset = getInset(pos)->clone(buffer);
337 minibuffer_inset = 0;
338 minibuffer_char = ' ';
339 // This reflects what GetInset() does (ARRae)
345 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
347 minibuffer_char = getChar(pos);
348 minibuffer_font = getFontSettings(bparams, pos);
349 minibuffer_inset = 0;
350 if (minibuffer_char == Paragraph::META_INSET) {
352 // the inset is not in a paragraph anymore
353 minibuffer_inset = insetlist.release(pos);
354 minibuffer_inset->parOwner(0);
356 minibuffer_inset = 0;
357 minibuffer_char = ' ';
358 // This reflects what GetInset() does (ARRae)
363 // Erase(pos); now the caller is responsible for that.
367 bool Paragraph::insertFromMinibuffer(pos_type pos)
369 if (minibuffer_char == Paragraph::META_INSET) {
370 if (!insetAllowed(minibuffer_inset->lyxCode())) {
373 insertInset(pos, minibuffer_inset, minibuffer_font);
375 LyXFont f = minibuffer_font;
376 if (!checkInsertChar(f)) {
379 insertChar(pos, minibuffer_char, f);
387 void Paragraph::erase(pos_type pos)
393 bool Paragraph::checkInsertChar(LyXFont & font)
395 if (pimpl_->inset_owner)
396 return pimpl_->inset_owner->checkInsertChar(font);
401 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
403 LyXFont const f(LyXFont::ALL_INHERIT);
404 insertChar(pos, c, f);
408 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
409 LyXFont const & font)
411 pimpl_->insertChar(pos, c, font);
415 void Paragraph::insertInset(pos_type pos, Inset * inset)
417 LyXFont const f(LyXFont::ALL_INHERIT);
418 insertInset(pos, inset, f);
422 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
424 pimpl_->insertInset(pos, inset, font);
428 bool Paragraph::insetAllowed(Inset::Code code)
430 //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());
442 return insetlist.get(pos);
446 Inset const * Paragraph::getInset(pos_type pos) const
448 lyx::Assert(pos < size());
450 return insetlist.get(pos);
454 // Gets uninstantiated font setting at position.
455 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
458 lyx::Assert(pos <= size());
460 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
461 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
462 for (; cit != end; ++cit) {
463 if (cit->pos() >= pos)
469 retfont = cit->font();
470 } else if (pos == size() && !empty()) {
471 retfont = getFontSettings(bparams, pos - 1);
473 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
479 // Gets uninstantiated font setting at position 0
480 LyXFont const Paragraph::getFirstFontSettings() const
482 if (!empty() && !pimpl_->fontlist.empty())
483 return pimpl_->fontlist[0].font();
485 return LyXFont(LyXFont::ALL_INHERIT);
489 // Gets the fully instantiated font at a given position in a paragraph
490 // This is basically the same function as LyXText::GetFont() in text2.C.
491 // The difference is that this one is used for generating the LaTeX file,
492 // and thus cosmetic "improvements" are disallowed: This has to deliver
493 // the true picture of the buffer. (Asger)
494 // If position is -1, we get the layout font of the paragraph.
495 // If position is -2, we get the font of the manual label of the paragraph.
496 LyXFont const Paragraph::getFont(BufferParams const & bparams,
499 lyx::Assert(pos >= 0);
501 LyXLayout_ptr const & lout = layout();
503 pos_type main_body = 0;
504 if (lout->labeltype == LABEL_MANUAL)
505 main_body = beginningOfMainBody();
509 layoutfont = lout->labelfont;
511 layoutfont = lout->font;
513 LyXFont tmpfont = getFontSettings(bparams, pos);
514 tmpfont.realize(layoutfont);
516 return pimpl_->realizeFont(tmpfont, bparams);
520 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
522 LyXLayout_ptr const & lout = layout();
524 LyXFont tmpfont = lout->labelfont;
525 tmpfont.setLanguage(getParLanguage(bparams));
527 return pimpl_->realizeFont(tmpfont, bparams);
531 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
533 LyXLayout_ptr const & lout = layout();
535 LyXFont tmpfont = lout->font;
536 tmpfont.setLanguage(getParLanguage(bparams));
538 return pimpl_->realizeFont(tmpfont, bparams);
542 /// Returns the height of the highest font in range
544 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
545 LyXFont::FONT_SIZE const def_size) const
547 if (pimpl_->fontlist.empty())
550 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
551 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
552 for (; end_it != end; ++end_it) {
553 if (end_it->pos() >= endpos)
560 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
561 for (; cit != end; ++cit) {
562 if (cit->pos() >= startpos)
566 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
567 for (; cit != end_it; ++cit) {
568 LyXFont::FONT_SIZE size = cit->font().size();
569 if (size == LyXFont::INHERIT_SIZE)
571 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
578 Paragraph::value_type
579 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
581 value_type c = getChar(pos);
582 if (!lyxrc.rtl_support)
612 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
619 void Paragraph::setFont(pos_type pos, LyXFont const & font)
621 lyx::Assert(pos <= size());
623 // First, reduce font against layout/label font
624 // Update: The SetCharFont() routine in text2.C already
625 // reduces font, so we don't need to do that here. (Asger)
626 // No need to simplify this because it will disappear
627 // in a new kernel. (Asger)
628 // Next search font table
630 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
631 Pimpl::FontList::iterator it = beg;
632 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
633 for (; it != endit; ++it) {
634 if (it->pos() >= pos)
637 unsigned int i = std::distance(beg, it);
638 bool notfound = (it == endit);
640 if (!notfound && pimpl_->fontlist[i].font() == font)
643 bool begin = pos == 0 || notfound ||
644 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
645 // Is position pos is a beginning of a font block?
646 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
647 // Is position pos is the end of a font block?
648 if (begin && end) { // A single char block
649 if (i + 1 < pimpl_->fontlist.size() &&
650 pimpl_->fontlist[i + 1].font() == font) {
651 // Merge the singleton block with the next block
652 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
653 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
654 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
655 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
656 // Merge the singleton block with the previous block
657 pimpl_->fontlist[i - 1].pos(pos);
658 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
660 pimpl_->fontlist[i].font(font);
662 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
663 pimpl_->fontlist[i - 1].pos(pos);
665 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
666 Pimpl::FontTable(pos, font));
668 pimpl_->fontlist[i].pos(pos - 1);
669 if (!(i + 1 < pimpl_->fontlist.size() &&
670 pimpl_->fontlist[i + 1].font() == font))
671 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
672 Pimpl::FontTable(pos, font));
673 } else { // The general case. The block is splitted into 3 blocks
674 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
675 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
676 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
677 Pimpl::FontTable(pos, font));
683 void Paragraph::next(Paragraph * p)
689 // This function is able to hide closed footnotes.
690 Paragraph * Paragraph::next()
696 Paragraph const * Paragraph::next() const
702 void Paragraph::previous(Paragraph * p)
708 // This function is able to hide closed footnotes.
709 Paragraph * Paragraph::previous()
715 // This function is able to hide closed footnotes.
716 Paragraph const * Paragraph::previous() const
723 void Paragraph::makeSameLayout(Paragraph const * par)
725 layout(par->layout());
727 params() = par->params();
731 int Paragraph::stripLeadingSpaces()
733 if (layout()->free_spacing ||
739 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
748 bool Paragraph::hasSameLayout(Paragraph const * par) const
751 par->layout() == layout() &&
752 params().sameLayout(par->params());
756 int Paragraph::getEndLabel() const
758 Paragraph const * par = this;
759 depth_type par_depth = getDepth();
761 LyXLayout_ptr const & layout = par->layout();
762 int const endlabeltype = layout->endlabeltype;
764 if (endlabeltype != END_LABEL_NO_LABEL) {
768 depth_type const next_depth = next_->getDepth();
769 if (par_depth > next_depth ||
770 (par_depth == next_depth
771 && layout != next_->layout()))
777 par = par->outerHook();
779 par_depth = par->getDepth();
781 return END_LABEL_NO_LABEL;
785 Paragraph::depth_type Paragraph::getDepth() const
787 return params().depth();
791 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
793 bool const isenv = layout()->isEnvironment();
796 return params().depth() + 1;
798 return params().depth();
802 char Paragraph::getAlign() const
804 return params().align();
808 string const & Paragraph::getLabelstring() const
810 return params().labelString();
814 // the next two functions are for the manual labels
815 string const Paragraph::getLabelWidthString() const
817 if (!params().labelWidthString().empty())
818 return params().labelWidthString();
820 return _("Senseless with this layout!");
824 void Paragraph::setLabelWidthString(string const & s)
826 params().labelWidthString(s);
830 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
833 params().labelWidthString(string());
834 params().align(LYX_ALIGN_LAYOUT);
835 params().spaceTop(VSpace(VSpace::NONE));
836 params().spaceBottom(VSpace(VSpace::NONE));
837 params().spacing(Spacing(Spacing::Default));
841 // if the layout of a paragraph contains a manual label, the beginning of the
842 // main body is the beginning of the second word. This is what the par-
843 // function returns. If the layout does not contain a label, the main
844 // body always starts with position 0. This differentiation is necessary,
845 // because there cannot be a newline or a blank <= the beginning of the
848 int Paragraph::beginningOfMainBody() const
850 // Unroll the first two cycles of the loop
851 // and remember the previous character to
852 // remove unnecessary GetChar() calls
854 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
856 char previous_char = 0;
859 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
860 // Yes, this ^ is supposed to be "= " not "=="
863 && previous_char != ' '
864 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
866 previous_char = temp;
875 Paragraph * Paragraph::depthHook(depth_type depth)
877 Paragraph * newpar = this;
880 newpar = newpar->previous();
881 } while (newpar && newpar->getDepth() > depth);
884 if (previous() || getDepth())
885 lyxerr << "ERROR (Paragraph::DepthHook): "
894 Paragraph const * Paragraph::depthHook(depth_type depth) const
896 Paragraph const * newpar = this;
899 newpar = newpar->previous();
900 } while (newpar && newpar->getDepth() > depth);
903 if (previous() || getDepth())
904 lyxerr << "ERROR (Paragraph::DepthHook): "
913 Paragraph * Paragraph::outerHook()
917 return depthHook(depth_type(getDepth() - 1));
921 Paragraph const * Paragraph::outerHook() const
925 return depthHook(depth_type(getDepth() - 1));
929 // returns -1 if inset not found
930 int Paragraph::getPositionOfInset(Inset const * inset) const
933 InsetList::iterator it = insetlist.begin();
934 InsetList::iterator end = insetlist.end();
935 for (; it != end; ++it) {
936 if (it.getInset() == inset) {
948 InsetOptArg * optArgInset(Paragraph const & par)
951 InsetList::iterator it = par.insetlist.begin();
952 InsetList::iterator end = par.insetlist.end();
953 for (; it != end; ++it) {
954 Inset * ins = it.getInset();
955 if (ins->lyxCode() == Inset::OPTARG_CODE) {
956 return static_cast<InsetOptArg *>(ins);
964 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
965 BufferParams const & bparams,
966 ostream & os, TexRow & texrow,
969 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
970 Inset const * in = inInset();
971 bool further_blank_line = false;
974 // well we have to check if we are in an inset with unlimited
975 // lenght (all in one row) if that is true then we don't allow
976 // any special options in the paragraph and also we don't allow
977 // any environment other then "Standard" to be valid!
978 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
981 if (params().startOfAppendix()) {
982 os << "\\appendix\n";
986 if (!params().spacing().isDefault()
987 && (!previous() || !previous()->hasSameLayout(this))) {
988 os << params().spacing().writeEnvirBegin() << '\n';
992 if (style->isCommand()) {
997 if (params().pagebreakTop()) {
999 further_blank_line = true;
1001 if (params().spaceTop().kind() != VSpace::NONE) {
1002 os << params().spaceTop().asLatexCommand(bparams);
1003 further_blank_line = true;
1006 if (params().lineTop()) {
1007 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1008 << "\\vspace{-1\\parskip}";
1009 further_blank_line = true;
1012 if (further_blank_line) {
1017 style = bparams.getLyXTextClass().defaultLayout();
1020 Language const * language = getParLanguage(bparams);
1021 Language const * doc_language = bparams.language;
1022 Language const * previous_language = previous()
1023 ? previous()->getParLanguage(bparams) : doc_language;
1025 if (language->babel() != previous_language->babel()
1026 // check if we already put language command in TeXEnvironment()
1027 && !(style->isEnvironment()
1029 (previous()->layout() != layout() &&
1030 previous()->getDepth() <= getDepth())
1031 || previous()->getDepth() < getDepth())))
1033 if (!lyxrc.language_command_end.empty() &&
1034 previous_language->babel() != doc_language->babel())
1036 os << subst(lyxrc.language_command_end, "$$lang",
1037 previous_language->babel())
1042 if (lyxrc.language_command_end.empty() ||
1043 language->babel() != doc_language->babel())
1045 os << subst(lyxrc.language_command_begin, "$$lang",
1052 if (bparams.inputenc == "auto" &&
1053 language->encoding() != previous_language->encoding()) {
1054 os << "\\inputencoding{"
1055 << language->encoding()->LatexName()
1060 switch (style->latextype) {
1063 << style->latexname();
1065 // Separate handling of optional argument inset.
1066 if (style->optionalargs == 1) {
1067 InsetOptArg * it = optArgInset(*this);
1069 it->latexOptional(buf, os, false, false);
1072 os << style->latexparam();
1074 case LATEX_ITEM_ENVIRONMENT:
1076 bibkey->latex(buf, os, false, false);
1080 case LATEX_LIST_ENVIRONMENT:
1087 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1089 // Make sure that \\par is done with the font of the last
1090 // character if this has another size as the default.
1091 // This is necessary because LaTeX (and LyX on the screen)
1092 // calculates the space between the baselines according
1093 // to this font. (Matthias)
1095 // Is this really needed ? (Dekel)
1096 // We do not need to use to change the font for the last paragraph
1097 // or for a command.
1098 LyXFont const font =
1100 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1102 bool is_command = style->isCommand();
1104 if (style->resfont.size() != font.size() && next_ && !is_command) {
1107 os << "\\" << font.latexSize() << " \\par}";
1108 } else if (need_par) {
1110 } else if (is_command)
1113 switch (style->latextype) {
1114 case LATEX_ITEM_ENVIRONMENT:
1115 case LATEX_LIST_ENVIRONMENT:
1116 if (next_ && (params().depth() < next_->params().depth())) {
1121 case LATEX_ENVIRONMENT:
1122 // if its the last paragraph of the current environment
1123 // skip it otherwise fall through
1125 && (next_->layout() != layout()
1126 || next_->params().depth() != params().depth()))
1128 // fall through possible
1130 // we don't need it for the last paragraph!!!
1137 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1138 further_blank_line = false;
1139 if (params().lineBottom()) {
1140 os << "\\lyxline{\\" << font.latexSize() << '}';
1141 further_blank_line = true;
1144 if (params().spaceBottom().kind() != VSpace::NONE) {
1145 os << params().spaceBottom().asLatexCommand(bparams);
1146 further_blank_line = true;
1149 if (params().pagebreakBottom()) {
1151 further_blank_line = true;
1154 if (further_blank_line) {
1159 if (!params().spacing().isDefault()
1160 && (!next_ || !next_->hasSameLayout(this))) {
1161 os << params().spacing().writeEnvirEnd() << '\n';
1166 // we don't need it for the last paragraph!!!
1171 // Since \selectlanguage write the language to the aux file,
1172 // we need to reset the language at the end of footnote or
1175 if (language->babel() != doc_language->babel()) {
1176 if (lyxrc.language_command_end.empty())
1177 os << subst(lyxrc.language_command_begin,
1179 doc_language->babel())
1182 os << subst(lyxrc.language_command_end,
1190 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1195 // This could go to ParagraphParameters if we want to
1196 int Paragraph::startTeXParParams(BufferParams const & bparams,
1197 ostream & os, bool moving_arg) const
1201 if (params().noindent()) {
1202 os << "\\noindent ";
1206 switch (params().align()) {
1207 case LYX_ALIGN_NONE:
1208 case LYX_ALIGN_BLOCK:
1209 case LYX_ALIGN_LAYOUT:
1210 case LYX_ALIGN_SPECIAL:
1212 case LYX_ALIGN_LEFT:
1213 case LYX_ALIGN_RIGHT:
1214 case LYX_ALIGN_CENTER:
1222 switch (params().align()) {
1223 case LYX_ALIGN_NONE:
1224 case LYX_ALIGN_BLOCK:
1225 case LYX_ALIGN_LAYOUT:
1226 case LYX_ALIGN_SPECIAL:
1228 case LYX_ALIGN_LEFT:
1229 if (getParLanguage(bparams)->babel() != "hebrew") {
1230 os << "\\begin{flushleft}";
1233 os << "\\begin{flushright}";
1237 case LYX_ALIGN_RIGHT:
1238 if (getParLanguage(bparams)->babel() != "hebrew") {
1239 os << "\\begin{flushright}";
1242 os << "\\begin{flushleft}";
1246 case LYX_ALIGN_CENTER:
1247 os << "\\begin{center}";
1256 // This could go to ParagraphParameters if we want to
1257 int Paragraph::endTeXParParams(BufferParams const & bparams,
1258 ostream & os, bool moving_arg) const
1262 switch (params().align()) {
1263 case LYX_ALIGN_NONE:
1264 case LYX_ALIGN_BLOCK:
1265 case LYX_ALIGN_LAYOUT:
1266 case LYX_ALIGN_SPECIAL:
1268 case LYX_ALIGN_LEFT:
1269 case LYX_ALIGN_RIGHT:
1270 case LYX_ALIGN_CENTER:
1278 switch (params().align()) {
1279 case LYX_ALIGN_NONE:
1280 case LYX_ALIGN_BLOCK:
1281 case LYX_ALIGN_LAYOUT:
1282 case LYX_ALIGN_SPECIAL:
1284 case LYX_ALIGN_LEFT:
1285 if (getParLanguage(bparams)->babel() != "hebrew") {
1286 os << "\\end{flushleft}";
1289 os << "\\end{flushright}";
1293 case LYX_ALIGN_RIGHT:
1294 if (getParLanguage(bparams)->babel() != "hebrew") {
1295 os << "\\end{flushright}";
1298 os << "\\end{flushleft}";
1302 case LYX_ALIGN_CENTER:
1303 os << "\\end{center}";
1311 // This one spits out the text of the paragraph
1312 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1313 BufferParams const & bparams,
1314 ostream & os, TexRow & texrow,
1317 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1319 bool return_value = false;
1321 LyXLayout_ptr style;
1323 // well we have to check if we are in an inset with unlimited
1324 // lenght (all in one row) if that is true then we don't allow
1325 // any special options in the paragraph and also we don't allow
1326 // any environment other then "Standard" to be valid!
1328 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1331 style = bparams.getLyXTextClass().defaultLayout();
1338 // Maybe we have to create a optional argument.
1340 if (style->labeltype != LABEL_MANUAL)
1343 main_body = beginningOfMainBody();
1347 if (main_body > 0) {
1350 basefont = getLabelFont(bparams);
1352 basefont = getLayoutFont(bparams);
1355 moving_arg |= style->needprotect;
1357 // Which font is currently active?
1358 LyXFont running_font(basefont);
1359 // Do we have an open font change?
1360 bool open_font = false;
1362 texrow.start(this, 0);
1364 // if the paragraph is empty, the loop will not be entered at all
1366 if (style->isCommand()) {
1371 column += startTeXParParams(bparams, os, moving_arg);
1375 for (pos_type i = 0; i < size(); ++i) {
1377 // First char in paragraph or after label?
1378 if (i == main_body) {
1379 if (main_body > 0) {
1381 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1384 basefont = getLayoutFont(bparams);
1385 running_font = basefont;
1389 if (style->isCommand()) {
1395 column += startTeXParParams(bparams, os,
1399 value_type c = getChar(i);
1401 // Fully instantiated font
1402 LyXFont font = getFont(bparams, i);
1404 LyXFont const last_font = running_font;
1406 // Spaces at end of font change are simulated to be
1407 // outside font change, i.e. we write "\textXX{text} "
1408 // rather than "\textXX{text }". (Asger)
1409 if (open_font && c == ' ' && i <= size() - 2) {
1410 LyXFont const & next_font = getFont(bparams, i + 1);
1411 if (next_font != running_font
1412 && next_font != font) {
1417 // We end font definition before blanks
1419 (font != running_font ||
1420 font.language() != running_font.language()))
1422 column += running_font.latexWriteEndChanges(os,
1424 (i == main_body-1) ? basefont : font);
1425 running_font = basefont;
1429 // Blanks are printed before start of fontswitch
1431 // Do not print the separation of the optional argument
1432 if (i != main_body - 1) {
1433 pimpl_->simpleTeXBlanks(os, texrow, i,
1434 column, font, *style);
1438 // Do we need to change font?
1439 if ((font != running_font ||
1440 font.language() != running_font.language()) &&
1443 column += font.latexWriteStartChanges(os, basefont,
1445 running_font = font;
1449 if (c == Paragraph::META_NEWLINE) {
1450 // newlines are handled differently here than
1451 // the default in SimpleTeXSpecialChars().
1452 if (!style->newline_allowed) {
1456 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1459 basefont = getLayoutFont(bparams);
1460 running_font = basefont;
1461 if (font.family() ==
1462 LyXFont::TYPEWRITER_FAMILY) {
1471 texrow.start(this, i + 1);
1474 pimpl_->simpleTeXSpecialChars(buf, bparams,
1475 os, texrow, moving_arg,
1477 basefont, open_font,
1478 *style, i, column, c);
1482 // If we have an open font definition, we have to close it
1484 #ifdef FIXED_LANGUAGE_END_DETECTION
1487 .latexWriteEndChanges(os, basefont,
1488 next_->getFont(bparams,
1491 running_font.latexWriteEndChanges(os, basefont,
1495 #ifdef WITH_WARNINGS
1496 //#warning For now we ALWAYS have to close the foreign font settings if they are
1497 //#warning there as we start another \selectlanguage with the next paragraph if
1498 //#warning we are in need of this. This should be fixed sometime (Jug)
1500 running_font.latexWriteEndChanges(os, basefont, basefont);
1504 // Needed if there is an optional argument but no contents.
1505 if (main_body > 0 && main_body == size()) {
1507 return_value = false;
1511 column += endTeXParParams(bparams, os, moving_arg);
1514 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1515 return return_value;
1519 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1520 BufferParams const & bparams,
1521 ostream & os, TexRow & texrow)
1523 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1525 LyXLayout_ptr const & style = layout();
1527 Language const * language = getParLanguage(bparams);
1528 Language const * doc_language = bparams.language;
1529 Language const * previous_language = previous_
1530 ? previous_->getParLanguage(bparams) : doc_language;
1531 if (language->babel() != previous_language->babel()) {
1533 if (!lyxrc.language_command_end.empty() &&
1534 previous_language->babel() != doc_language->babel()) {
1535 os << subst(lyxrc.language_command_end, "$$lang",
1536 previous_language->babel())
1541 if (lyxrc.language_command_end.empty() ||
1542 language->babel() != doc_language->babel()) {
1543 os << subst(lyxrc.language_command_begin, "$$lang",
1550 bool leftindent_open = false;
1551 if (!params().leftIndent().zero()) {
1552 os << "\\begin{LyXParagraphLeftIndent}{" <<
1553 params().leftIndent().asLatexString() << "}\n";
1555 leftindent_open = true;
1558 if (style->isEnvironment()) {
1559 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1560 os << "\\begin{" << style->latexname() << "}{"
1561 << params().labelWidthString() << "}\n";
1562 } else if (style->labeltype == LABEL_BIBLIO) {
1564 os << "\\begin{" << style->latexname() << "}{"
1565 << bibitemWidest(buf)
1567 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1568 os << "\\begin{" << style->latexname() << '}'
1569 << style->latexparam() << '\n';
1571 os << "\\begin{" << style->latexname() << '}'
1572 << style->latexparam() << '\n';
1575 Paragraph * par = this;
1577 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1579 if (par && par->params().depth() > params().depth()) {
1580 if (par->layout()->isParagraph()) {
1583 // How to handle this? (Lgb)
1584 //&& !suffixIs(os, "\n\n")
1586 // There should be at least one '\n' already
1587 // but we need there to be two for Standard
1588 // paragraphs that are depth-increment'ed to be
1589 // output correctly. However, tables can
1590 // also be paragraphs so don't adjust them.
1593 // Will it ever harm to have one '\n' too
1594 // many? i.e. that we sometimes will have
1595 // three in a row. (Lgb)
1599 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1602 && par->layout() == layout()
1603 && par->params().depth() == params().depth()
1604 && par->params().leftIndent() == params().leftIndent());
1606 if (style->isEnvironment()) {
1607 os << "\\end{" << style->latexname() << "}\n";
1611 if (leftindent_open) {
1612 os << "\\end{LyXParagraphLeftIndent}\n";
1616 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1617 return par; // ale970302
1621 bool Paragraph::isHfill(pos_type pos) const
1623 return IsHfillChar(getChar(pos));
1627 bool Paragraph::isInset(pos_type pos) const
1629 return IsInsetChar(getChar(pos));
1633 bool Paragraph::isNewline(pos_type pos) const
1635 return pos >= 0 && IsNewlineChar(getChar(pos));
1639 bool Paragraph::isSeparator(pos_type pos) const
1641 return IsSeparatorChar(getChar(pos));
1645 bool Paragraph::isLineSeparator(pos_type pos) const
1647 value_type const c = getChar(pos);
1648 return IsLineSeparatorChar(c)
1649 || (IsInsetChar(c) && getInset(pos) &&
1650 getInset(pos)->isLineSeparator());
1654 bool Paragraph::isKomma(pos_type pos) const
1656 return IsKommaChar(getChar(pos));
1660 /// Used by the spellchecker
1661 bool Paragraph::isLetter(pos_type pos) const
1663 value_type const c = getChar(pos);
1664 if (IsLetterChar(c))
1667 return getInset(pos)->isLetter();
1668 // We want to pass the ' and escape chars to ispell
1669 string const extra = lyxrc.isp_esc_chars + '\'';
1670 return contains(extra, c);
1674 bool Paragraph::isWord(pos_type pos) const
1676 return IsWordChar(getChar(pos)) ;
1681 Paragraph::getParLanguage(BufferParams const & bparams) const
1684 return getFirstFontSettings().language();
1685 } else if (previous_)
1686 return previous_->getParLanguage(bparams);
1688 return bparams.language;
1692 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1694 return lyxrc.rtl_support
1695 && getParLanguage(bparams)->RightToLeft()
1696 && !(inInset() && inInset()->owner() &&
1697 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1701 void Paragraph::changeLanguage(BufferParams const & bparams,
1702 Language const * from, Language const * to)
1704 for (pos_type i = 0; i < size(); ++i) {
1705 LyXFont font = getFontSettings(bparams, i);
1706 if (font.language() == from) {
1707 font.setLanguage(to);
1714 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1716 Language const * doc_language = bparams.language;
1717 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1718 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1720 for (; cit != end; ++cit)
1721 if (cit->font().language() != ignore_language &&
1722 cit->font().language() != latex_language &&
1723 cit->font().language() != doc_language)
1729 // Convert the paragraph to a string.
1730 // Used for building the table of contents
1731 string const Paragraph::asString(Buffer const * buffer, bool label) const
1733 BufferParams const & bparams = buffer->params;
1735 if (label && !params().labelString().empty())
1736 s += params().labelString() + ' ';
1737 string::size_type const len = s.size();
1739 for (pos_type i = 0; i < size(); ++i) {
1740 value_type c = getChar(i);
1743 else if (c == META_INSET &&
1744 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1746 getInset(i)->ascii(buffer, ost);
1747 s += subst(STRCONV(ost.str()),'\n',' ');
1751 if (isRightToLeftPar(bparams))
1752 reverse(s.begin() + len,s.end());
1758 string const Paragraph::asString(Buffer const * buffer,
1759 pos_type beg, pos_type end, bool label) const
1763 if (beg == 0 && label && !params().labelString().empty())
1764 ost << params().labelString() << ' ';
1766 for (pos_type i = beg; i < end; ++i) {
1767 value_type const c = getUChar(buffer->params, i);
1770 else if (c == META_NEWLINE)
1772 else if (c == META_HFILL)
1774 else if (c == META_INSET) {
1775 getInset(i)->ascii(buffer, ost);
1779 return STRCONV(ost.str());
1783 void Paragraph::setInsetOwner(Inset * i)
1785 pimpl_->inset_owner = i;
1786 InsetList::iterator it = insetlist.begin();
1787 InsetList::iterator end = insetlist.end();
1788 for (; it != end; ++it) {
1790 it.getInset()->setOwner(i);
1795 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1798 insetlist.deleteInsetsLyXText(bv);
1802 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1805 insetlist.resizeInsetsLyXText(bv);
1809 void Paragraph::setContentsFromPar(Paragraph * par)
1811 pimpl_->setContentsFromPar(par);
1815 lyx::pos_type Paragraph::size() const
1817 return pimpl_->size();
1821 bool Paragraph::empty() const
1823 return pimpl_->empty();
1827 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1829 return pimpl_->getChar(pos);
1833 int Paragraph::id() const
1839 LyXLayout_ptr const & Paragraph::layout() const
1845 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1847 layout_ = new_layout;
1851 bool Paragraph::isFirstInSequence() const
1853 Paragraph const * dhook = depthHook(getDepth());
1854 return (dhook == this
1855 || dhook->layout() != layout()
1856 || dhook->getDepth() != getDepth());
1860 Inset * Paragraph::inInset() const
1862 return pimpl_->inset_owner;
1866 void Paragraph::clearContents()
1871 void Paragraph::setChar(pos_type pos, value_type c)
1873 pimpl_->setChar(pos, c);
1877 ParagraphParameters & Paragraph::params()
1879 return pimpl_->params;
1883 ParagraphParameters const & Paragraph::params() const
1885 return pimpl_->params;
1889 Paragraph * Paragraph::getParFromID(int id) const
1891 return pimpl_->getParFromID(id);
1895 bool Paragraph::isFreeSpacing() const
1897 // for now we just need this, later should we need this in some
1898 // other way we can always add a function to Inset::() too.
1899 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1900 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);