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/insetinclude.h"
33 #include "insets/insetbib.h"
34 #include "insets/insettext.h"
35 #include "insets/insetoptarg.h"
37 #include "support/filetools.h"
38 #include "support/lstrings.h"
39 #include "support/lyxmanip.h"
40 #include "support/FileInfo.h"
41 #include "support/LAssert.h"
42 #include "support/textutils.h"
52 using std::lower_bound;
53 using std::upper_bound;
58 // this is a bad idea, but how can Paragraph find its buffer to get
59 // parameters? (JMarc)
61 extern string bibitemWidest(Buffer const *);
63 // this is a minibuffer
68 LyXFont minibuffer_font;
69 Inset * minibuffer_inset;
74 extern BufferView * current_view;
77 Paragraph::Paragraph()
78 : pimpl_(new Paragraph::Pimpl(this))
86 bibkey = 0; // ale970302
92 // This constructor inserts the new paragraph in a list.
93 Paragraph::Paragraph(Paragraph * par)
94 : pimpl_(new Paragraph::Pimpl(this))
99 // double linked list begin
102 next_->previous_ = this;
104 previous_->next_ = this;
107 bibkey = 0; // ale970302
113 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
114 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
122 // this is because of the dummy layout of the paragraphs that
124 layout_ = lp.layout();
128 bibkey = static_cast<InsetBibKey *>
129 (lp.bibkey->clone(*current_view->buffer()));
134 // copy everything behind the break-position to the new paragraph
135 insetlist = lp.insetlist;
136 InsetList::iterator it = insetlist.begin();
137 InsetList::iterator end = insetlist.end();
138 for (; it != end; ++it) {
139 it.setInset(it.getInset()->clone(*current_view->buffer(),
141 // tell the new inset who is the boss now
142 it.getInset()->parOwner(this);
147 // the destructor removes the new paragraph from the list
148 Paragraph::~Paragraph()
152 previous_->next_ = next_;
154 next_->previous_ = previous_;
162 //lyxerr << "Paragraph::paragraph_id = "
163 // << Paragraph::paragraph_id << endl;
167 void Paragraph::write(Buffer const * buf, ostream & os,
168 BufferParams const & bparams,
169 depth_type & dth) const
171 // The beginning or end of a deeper (i.e. nested) area?
172 if (dth != params().depth()) {
173 if (params().depth() > dth) {
174 while (params().depth() > dth) {
175 os << "\n\\begin_deeper ";
179 while (params().depth() < dth) {
180 os << "\n\\end_deeper ";
186 // First write the layout
187 os << "\n\\layout " << layout()->name() << "\n";
189 // Maybe some vertical spaces.
190 if (params().spaceTop().kind() != VSpace::NONE)
191 os << "\\added_space_top "
192 << params().spaceTop().asLyXCommand() << " ";
193 if (params().spaceBottom().kind() != VSpace::NONE)
194 os << "\\added_space_bottom "
195 << params().spaceBottom().asLyXCommand() << " ";
197 // Maybe the paragraph has special spacing
198 params().spacing().writeFile(os, true);
200 // The labelwidth string used in lists.
201 if (!params().labelWidthString().empty())
202 os << "\\labelwidthstring "
203 << params().labelWidthString() << '\n';
205 // Lines above or below?
206 if (params().lineTop())
208 if (params().lineBottom())
209 os << "\\line_bottom ";
211 // Pagebreaks above or below?
212 if (params().pagebreakTop())
213 os << "\\pagebreak_top ";
214 if (params().pagebreakBottom())
215 os << "\\pagebreak_bottom ";
217 // Start of appendix?
218 if (params().startOfAppendix())
219 os << "\\start_of_appendix ";
222 if (params().noindent())
225 // Do we have a manual left indent?
226 if (!params().leftIndent().zero())
227 os << "\\leftindent " << params().leftIndent().asString() << " ";
230 if (params().align() != LYX_ALIGN_LAYOUT) {
232 switch (params().align()) {
233 case LYX_ALIGN_LEFT: h = 1; break;
234 case LYX_ALIGN_RIGHT: h = 2; break;
235 case LYX_ALIGN_CENTER: h = 3; break;
236 default: h = 0; break;
238 os << "\\align " << string_align[h] << " ";
243 bibkey->write(buf, os);
245 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
248 for (pos_type i = 0; i < size(); ++i) {
254 // Write font changes
255 LyXFont font2 = getFontSettings(bparams, i);
256 if (font2 != font1) {
257 #ifndef INHERIT_LANGUAGE
258 font2.lyxWriteChanges(font1, os);
260 font2.lyxWriteChanges(font1, bparams.language, os);
266 value_type const c = getChar(i);
270 Inset const * inset = getInset(i);
272 if (inset->directWrite()) {
273 // international char, let it write
274 // code directly so it's shorter in
276 inset->write(buf, os);
278 os << "\n\\begin_inset ";
279 inset->write(buf, os);
280 os << "\n\\end_inset \n\n";
286 os << "\n\\newline \n";
290 os << "\n\\hfill \n";
294 os << "\n\\backslash \n";
298 if (i + 1 < size() && getChar(i + 1) == ' ') {
305 if ((column > 70 && c == ' ')
310 // this check is to amend a bug. LyX sometimes
311 // inserts '\0' this could cause problems.
315 lyxerr << "ERROR (Paragraph::writeFile):"
316 " NULL char in structure." << endl;
324 void Paragraph::validate(LaTeXFeatures & features) const
326 pimpl_->validate(features, *layout());
330 // First few functions needed for cut and paste and paragraph breaking.
331 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
333 BufferParams bparams = buffer.params;
335 minibuffer_char = getChar(pos);
336 minibuffer_font = getFontSettings(bparams, pos);
337 minibuffer_inset = 0;
338 if (minibuffer_char == Paragraph::META_INSET) {
340 minibuffer_inset = getInset(pos)->clone(buffer);
342 minibuffer_inset = 0;
343 minibuffer_char = ' ';
344 // This reflects what GetInset() does (ARRae)
350 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
352 minibuffer_char = getChar(pos);
353 minibuffer_font = getFontSettings(bparams, pos);
354 minibuffer_inset = 0;
355 if (minibuffer_char == Paragraph::META_INSET) {
357 // the inset is not in a paragraph anymore
358 minibuffer_inset = insetlist.release(pos);
359 minibuffer_inset->parOwner(0);
361 minibuffer_inset = 0;
362 minibuffer_char = ' ';
363 // This reflects what GetInset() does (ARRae)
368 // Erase(pos); now the caller is responsible for that.
372 bool Paragraph::insertFromMinibuffer(pos_type pos)
374 if (minibuffer_char == Paragraph::META_INSET) {
375 if (!insetAllowed(minibuffer_inset->lyxCode())) {
378 insertInset(pos, minibuffer_inset, minibuffer_font);
380 LyXFont f = minibuffer_font;
381 if (!checkInsertChar(f)) {
384 insertChar(pos, minibuffer_char, f);
392 void Paragraph::erase(pos_type pos)
398 bool Paragraph::checkInsertChar(LyXFont & font)
400 if (pimpl_->inset_owner)
401 return pimpl_->inset_owner->checkInsertChar(font);
406 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
408 LyXFont const f(LyXFont::ALL_INHERIT);
409 insertChar(pos, c, f);
413 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
414 LyXFont const & font)
416 pimpl_->insertChar(pos, c, font);
420 void Paragraph::insertInset(pos_type pos, Inset * inset)
422 LyXFont const f(LyXFont::ALL_INHERIT);
423 insertInset(pos, inset, f);
427 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
429 pimpl_->insertInset(pos, inset, font);
433 bool Paragraph::insetAllowed(Inset::Code code)
435 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
437 if (pimpl_->inset_owner)
438 return pimpl_->inset_owner->insetAllowed(code);
443 Inset * Paragraph::getInset(pos_type pos)
445 lyx::Assert(pos < size());
447 return insetlist.get(pos);
451 Inset const * Paragraph::getInset(pos_type pos) const
453 lyx::Assert(pos < size());
455 return insetlist.get(pos);
459 // Gets uninstantiated font setting at position.
460 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
463 lyx::Assert(pos <= size());
465 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
466 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
467 for (; cit != end; ++cit) {
468 if (cit->pos() >= pos)
474 retfont = cit->font();
475 } else if (pos == size() && !empty()) {
476 retfont = getFontSettings(bparams, pos - 1);
478 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
479 #ifdef INHERIT_LANGUAGE
480 if (retfont.language() == inherit_language)
481 retfont.setLanguage(bparams.language);
488 // Gets uninstantiated font setting at position 0
489 LyXFont const Paragraph::getFirstFontSettings() const
491 if (!empty() && !pimpl_->fontlist.empty())
492 return pimpl_->fontlist[0].font();
494 return LyXFont(LyXFont::ALL_INHERIT);
498 // Gets the fully instantiated font at a given position in a paragraph
499 // This is basically the same function as LyXText::GetFont() in text2.C.
500 // The difference is that this one is used for generating the LaTeX file,
501 // and thus cosmetic "improvements" are disallowed: This has to deliver
502 // the true picture of the buffer. (Asger)
503 // If position is -1, we get the layout font of the paragraph.
504 // If position is -2, we get the font of the manual label of the paragraph.
505 LyXFont const Paragraph::getFont(BufferParams const & bparams,
508 lyx::Assert(pos >= 0);
510 LyXLayout_ptr const & lout = layout();
512 pos_type main_body = 0;
513 if (lout->labeltype == LABEL_MANUAL)
514 main_body = beginningOfMainBody();
518 layoutfont = lout->labelfont;
520 layoutfont = lout->font;
522 LyXFont tmpfont = getFontSettings(bparams, pos);
523 #ifndef INHERIT_LANGUAGE
524 tmpfont.realize(layoutfont);
526 tmpfont.realize(layoutfont, bparams.language);
529 return pimpl_->realizeFont(tmpfont, bparams);
533 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
535 LyXLayout_ptr const & lout = layout();
537 LyXFont tmpfont = lout->labelfont;
538 tmpfont.setLanguage(getParLanguage(bparams));
540 return pimpl_->realizeFont(tmpfont, bparams);
544 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
546 LyXLayout_ptr const & lout = layout();
548 LyXFont tmpfont = lout->font;
549 tmpfont.setLanguage(getParLanguage(bparams));
551 return pimpl_->realizeFont(tmpfont, bparams);
555 /// Returns the height of the highest font in range
557 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
558 LyXFont::FONT_SIZE const def_size) const
560 if (pimpl_->fontlist.empty())
563 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
564 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
565 for (; end_it != end; ++end_it) {
566 if (end_it->pos() >= endpos)
573 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
574 for (; cit != end; ++cit) {
575 if (cit->pos() >= startpos)
579 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
580 for (; cit != end_it; ++cit) {
581 LyXFont::FONT_SIZE size = cit->font().size();
582 if (size == LyXFont::INHERIT_SIZE)
584 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
591 Paragraph::value_type
592 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
594 value_type c = getChar(pos);
595 if (!lyxrc.rtl_support)
625 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
632 void Paragraph::setFont(pos_type pos, LyXFont const & font)
634 lyx::Assert(pos <= size());
636 // First, reduce font against layout/label font
637 // Update: The SetCharFont() routine in text2.C already
638 // reduces font, so we don't need to do that here. (Asger)
639 // No need to simplify this because it will disappear
640 // in a new kernel. (Asger)
641 // Next search font table
643 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
644 Pimpl::FontList::iterator it = beg;
645 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
646 for (; it != endit; ++it) {
647 if (it->pos() >= pos)
650 unsigned int i = std::distance(beg, it);
651 bool notfound = (it == endit);
653 if (!notfound && pimpl_->fontlist[i].font() == font)
656 bool begin = pos == 0 || notfound ||
657 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
658 // Is position pos is a beginning of a font block?
659 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
660 // Is position pos is the end of a font block?
661 if (begin && end) { // A single char block
662 if (i + 1 < pimpl_->fontlist.size() &&
663 pimpl_->fontlist[i + 1].font() == font) {
664 // Merge the singleton block with the next block
665 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
666 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
667 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
668 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
669 // Merge the singleton block with the previous block
670 pimpl_->fontlist[i - 1].pos(pos);
671 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
673 pimpl_->fontlist[i].font(font);
675 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
676 pimpl_->fontlist[i - 1].pos(pos);
678 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
679 Pimpl::FontTable(pos, font));
681 pimpl_->fontlist[i].pos(pos - 1);
682 if (!(i + 1 < pimpl_->fontlist.size() &&
683 pimpl_->fontlist[i + 1].font() == font))
684 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
685 Pimpl::FontTable(pos, font));
686 } else { // The general case. The block is splitted into 3 blocks
687 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
688 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
689 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
690 Pimpl::FontTable(pos, font));
696 void Paragraph::next(Paragraph * p)
702 // This function is able to hide closed footnotes.
703 Paragraph * Paragraph::next()
709 Paragraph const * Paragraph::next() const
715 void Paragraph::previous(Paragraph * p)
721 // This function is able to hide closed footnotes.
722 Paragraph * Paragraph::previous()
728 // This function is able to hide closed footnotes.
729 Paragraph const * Paragraph::previous() const
736 void Paragraph::makeSameLayout(Paragraph const * par)
738 layout(par->layout());
740 params() = par->params();
744 int Paragraph::stripLeadingSpaces()
746 if (layout()->free_spacing ||
752 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
761 bool Paragraph::hasSameLayout(Paragraph const * par) const
764 par->layout() == layout() &&
765 params().sameLayout(par->params());
769 int Paragraph::getEndLabel() const
771 Paragraph const * par = this;
772 depth_type par_depth = getDepth();
774 LyXLayout_ptr const & layout = par->layout();
775 int const endlabeltype = layout->endlabeltype;
777 if (endlabeltype != END_LABEL_NO_LABEL) {
781 depth_type const next_depth = next_->getDepth();
782 if (par_depth > next_depth ||
783 (par_depth == next_depth
784 && layout != next_->layout()))
790 par = par->outerHook();
792 par_depth = par->getDepth();
794 return END_LABEL_NO_LABEL;
798 Paragraph::depth_type Paragraph::getDepth() const
800 return params().depth();
804 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
806 bool const isenv = layout()->isEnvironment();
809 return params().depth() + 1;
811 return params().depth();
815 char Paragraph::getAlign() const
817 return params().align();
821 string const & Paragraph::getLabelstring() const
823 return params().labelString();
827 // the next two functions are for the manual labels
828 string const Paragraph::getLabelWidthString() const
830 if (!params().labelWidthString().empty())
831 return params().labelWidthString();
833 return _("Senseless with this layout!");
837 void Paragraph::setLabelWidthString(string const & s)
839 params().labelWidthString(s);
843 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
846 params().labelWidthString(string());
847 params().align(LYX_ALIGN_LAYOUT);
848 params().spaceTop(VSpace(VSpace::NONE));
849 params().spaceBottom(VSpace(VSpace::NONE));
850 params().spacing(Spacing(Spacing::Default));
854 // if the layout of a paragraph contains a manual label, the beginning of the
855 // main body is the beginning of the second word. This is what the par-
856 // function returns. If the layout does not contain a label, the main
857 // body always starts with position 0. This differentiation is necessary,
858 // because there cannot be a newline or a blank <= the beginning of the
861 int Paragraph::beginningOfMainBody() const
863 // Unroll the first two cycles of the loop
864 // and remember the previous character to
865 // remove unnecessary GetChar() calls
867 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
869 char previous_char = 0;
872 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
873 // Yes, this ^ is supposed to be "= " not "=="
876 && previous_char != ' '
877 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
879 previous_char = temp;
888 Paragraph * Paragraph::depthHook(depth_type depth)
890 Paragraph * newpar = this;
893 newpar = newpar->previous();
894 } while (newpar && newpar->getDepth() > depth);
897 if (previous() || getDepth())
898 lyxerr << "ERROR (Paragraph::DepthHook): "
907 Paragraph const * Paragraph::depthHook(depth_type depth) const
909 Paragraph const * newpar = this;
912 newpar = newpar->previous();
913 } while (newpar && newpar->getDepth() > depth);
916 if (previous() || getDepth())
917 lyxerr << "ERROR (Paragraph::DepthHook): "
926 Paragraph * Paragraph::outerHook()
930 return depthHook(depth_type(getDepth() - 1));
934 Paragraph const * Paragraph::outerHook() const
938 return depthHook(depth_type(getDepth() - 1));
942 // returns -1 if inset not found
943 int Paragraph::getPositionOfInset(Inset const * inset) const
946 InsetList::iterator it = insetlist.begin();
947 InsetList::iterator end = insetlist.end();
948 for (; it != end; ++it) {
949 if (it.getInset() == inset) {
961 InsetOptArg * optArgInset(Paragraph const & par)
964 InsetList::iterator it = par.insetlist.begin();
965 InsetList::iterator end = par.insetlist.end();
966 for (; it != end; ++it) {
967 Inset * ins = it.getInset();
968 if (ins->lyxCode() == Inset::OPTARG_CODE) {
969 return static_cast<InsetOptArg *>(ins);
977 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
978 BufferParams const & bparams,
979 ostream & os, TexRow & texrow,
982 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
983 Inset const * in = inInset();
984 bool further_blank_line = false;
987 // well we have to check if we are in an inset with unlimited
988 // lenght (all in one row) if that is true then we don't allow
989 // any special options in the paragraph and also we don't allow
990 // any environment other then "Standard" to be valid!
991 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
994 if (params().startOfAppendix()) {
995 os << "\\appendix\n";
999 if (!params().spacing().isDefault()
1000 && (!previous() || !previous()->hasSameLayout(this))) {
1001 os << params().spacing().writeEnvirBegin() << "\n";
1005 if (style->isCommand()) {
1010 if (params().pagebreakTop()) {
1012 further_blank_line = true;
1014 if (params().spaceTop().kind() != VSpace::NONE) {
1015 os << params().spaceTop().asLatexCommand(bparams);
1016 further_blank_line = true;
1019 if (params().lineTop()) {
1020 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1021 << "\\vspace{-1\\parskip}";
1022 further_blank_line = true;
1025 if (further_blank_line) {
1030 style = bparams.getLyXTextClass().defaultLayout();
1033 Language const * language = getParLanguage(bparams);
1034 Language const * doc_language = bparams.language;
1035 Language const * previous_language = previous()
1036 ? previous()->getParLanguage(bparams) : doc_language;
1038 if (language->babel() != previous_language->babel()
1039 // check if we already put language command in TeXEnvironment()
1040 && !(style->isEnvironment()
1041 && (!previous() || previous()->layout() != layout() ||
1042 previous()->params().depth() != params().depth())))
1044 if (!lyxrc.language_command_end.empty() &&
1045 previous_language->babel() != doc_language->babel())
1047 os << subst(lyxrc.language_command_end, "$$lang",
1048 previous_language->babel())
1053 if (lyxrc.language_command_end.empty() ||
1054 language->babel() != doc_language->babel())
1056 os << subst(lyxrc.language_command_begin, "$$lang",
1063 if (bparams.inputenc == "auto" &&
1064 language->encoding() != previous_language->encoding()) {
1065 os << "\\inputencoding{"
1066 << language->encoding()->LatexName()
1071 switch (style->latextype) {
1074 << style->latexname();
1076 // Separate handling of optional argument inset.
1077 if (style->optionalargs == 1) {
1078 InsetOptArg * it = optArgInset(*this);
1080 it->latexOptional(buf, os, false, false);
1083 os << style->latexparam();
1085 case LATEX_ITEM_ENVIRONMENT:
1087 bibkey->latex(buf, os, false, false);
1091 case LATEX_LIST_ENVIRONMENT:
1098 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1100 // Make sure that \\par is done with the font of the last
1101 // character if this has another size as the default.
1102 // This is necessary because LaTeX (and LyX on the screen)
1103 // calculates the space between the baselines according
1104 // to this font. (Matthias)
1106 // Is this really needed ? (Dekel)
1107 // We do not need to use to change the font for the last paragraph
1108 // or for a command.
1109 LyXFont const font =
1111 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1113 bool is_command = style->isCommand();
1115 if (style->resfont.size() != font.size() && next_ && !is_command) {
1118 os << "\\" << font.latexSize() << " \\par}";
1119 } else if (need_par) {
1121 } else if (is_command)
1124 switch (style->latextype) {
1125 case LATEX_ITEM_ENVIRONMENT:
1126 case LATEX_LIST_ENVIRONMENT:
1127 if (next_ && (params().depth() < next_->params().depth())) {
1132 case LATEX_ENVIRONMENT:
1133 // if its the last paragraph of the current environment
1134 // skip it otherwise fall through
1136 && (next_->layout() != layout()
1137 || next_->params().depth() != params().depth()))
1139 // fall through possible
1141 // we don't need it for the last paragraph!!!
1148 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1149 further_blank_line = false;
1150 if (params().lineBottom()) {
1151 os << "\\lyxline{\\" << font.latexSize() << '}';
1152 further_blank_line = true;
1155 if (params().spaceBottom().kind() != VSpace::NONE) {
1156 os << params().spaceBottom().asLatexCommand(bparams);
1157 further_blank_line = true;
1160 if (params().pagebreakBottom()) {
1162 further_blank_line = true;
1165 if (further_blank_line) {
1170 if (!params().spacing().isDefault()
1171 && (!next_ || !next_->hasSameLayout(this))) {
1172 os << params().spacing().writeEnvirEnd() << "\n";
1177 // we don't need it for the last paragraph!!!
1182 // Since \selectlanguage write the language to the aux file,
1183 // we need to reset the language at the end of footnote or
1186 if (language->babel() != doc_language->babel()) {
1187 if (lyxrc.language_command_end.empty())
1188 os << subst(lyxrc.language_command_begin,
1190 doc_language->babel())
1193 os << subst(lyxrc.language_command_end,
1201 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1206 // This could go to ParagraphParameters if we want to
1207 int Paragraph::startTeXParParams(BufferParams const & bparams,
1208 ostream & os, bool moving_arg) const
1212 if (params().noindent()) {
1213 os << "\\noindent ";
1217 switch (params().align()) {
1218 case LYX_ALIGN_NONE:
1219 case LYX_ALIGN_BLOCK:
1220 case LYX_ALIGN_LAYOUT:
1221 case LYX_ALIGN_SPECIAL:
1223 case LYX_ALIGN_LEFT:
1224 case LYX_ALIGN_RIGHT:
1225 case LYX_ALIGN_CENTER:
1233 switch (params().align()) {
1234 case LYX_ALIGN_NONE:
1235 case LYX_ALIGN_BLOCK:
1236 case LYX_ALIGN_LAYOUT:
1237 case LYX_ALIGN_SPECIAL:
1239 case LYX_ALIGN_LEFT:
1240 if (getParLanguage(bparams)->babel() != "hebrew") {
1241 os << "\\begin{flushleft}";
1244 os << "\\begin{flushright}";
1248 case LYX_ALIGN_RIGHT:
1249 if (getParLanguage(bparams)->babel() != "hebrew") {
1250 os << "\\begin{flushright}";
1253 os << "\\begin{flushleft}";
1257 case LYX_ALIGN_CENTER:
1258 os << "\\begin{center}";
1267 // This could go to ParagraphParameters if we want to
1268 int Paragraph::endTeXParParams(BufferParams const & bparams,
1269 ostream & os, bool moving_arg) const
1273 switch (params().align()) {
1274 case LYX_ALIGN_NONE:
1275 case LYX_ALIGN_BLOCK:
1276 case LYX_ALIGN_LAYOUT:
1277 case LYX_ALIGN_SPECIAL:
1279 case LYX_ALIGN_LEFT:
1280 case LYX_ALIGN_RIGHT:
1281 case LYX_ALIGN_CENTER:
1289 switch (params().align()) {
1290 case LYX_ALIGN_NONE:
1291 case LYX_ALIGN_BLOCK:
1292 case LYX_ALIGN_LAYOUT:
1293 case LYX_ALIGN_SPECIAL:
1295 case LYX_ALIGN_LEFT:
1296 if (getParLanguage(bparams)->babel() != "hebrew") {
1297 os << "\\end{flushleft}";
1300 os << "\\end{flushright}";
1304 case LYX_ALIGN_RIGHT:
1305 if (getParLanguage(bparams)->babel() != "hebrew") {
1306 os << "\\end{flushright}";
1309 os << "\\end{flushleft}";
1313 case LYX_ALIGN_CENTER:
1314 os << "\\end{center}";
1322 // This one spits out the text of the paragraph
1323 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1324 BufferParams const & bparams,
1325 ostream & os, TexRow & texrow,
1328 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1330 bool return_value = false;
1332 LyXLayout_ptr style;
1334 // well we have to check if we are in an inset with unlimited
1335 // lenght (all in one row) if that is true then we don't allow
1336 // any special options in the paragraph and also we don't allow
1337 // any environment other then "Standard" to be valid!
1339 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1342 style = bparams.getLyXTextClass().defaultLayout();
1349 // Maybe we have to create a optional argument.
1351 if (style->labeltype != LABEL_MANUAL)
1354 main_body = beginningOfMainBody();
1358 if (main_body > 0) {
1361 basefont = getLabelFont(bparams);
1363 basefont = getLayoutFont(bparams);
1366 moving_arg |= style->needprotect;
1368 // Which font is currently active?
1369 LyXFont running_font(basefont);
1370 // Do we have an open font change?
1371 bool open_font = false;
1373 texrow.start(this, 0);
1375 // if the paragraph is empty, the loop will not be entered at all
1377 if (style->isCommand()) {
1382 column += startTeXParParams(bparams, os, moving_arg);
1386 for (pos_type i = 0; i < size(); ++i) {
1388 // First char in paragraph or after label?
1389 if (i == main_body) {
1390 if (main_body > 0) {
1392 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1395 basefont = getLayoutFont(bparams);
1396 running_font = basefont;
1400 if (style->isCommand()) {
1406 column += startTeXParParams(bparams, os,
1410 value_type c = getChar(i);
1412 // Fully instantiated font
1413 LyXFont font = getFont(bparams, i);
1415 LyXFont const last_font = running_font;
1417 // Spaces at end of font change are simulated to be
1418 // outside font change, i.e. we write "\textXX{text} "
1419 // rather than "\textXX{text }". (Asger)
1420 if (open_font && c == ' ' && i <= size() - 2) {
1421 LyXFont const & next_font = getFont(bparams, i + 1);
1422 if (next_font != running_font
1423 && next_font != font) {
1428 // We end font definition before blanks
1430 (font != running_font ||
1431 font.language() != running_font.language()))
1433 column += running_font.latexWriteEndChanges(os,
1435 (i == main_body-1) ? basefont : font);
1436 running_font = basefont;
1440 // Blanks are printed before start of fontswitch
1442 // Do not print the separation of the optional argument
1443 if (i != main_body - 1) {
1444 pimpl_->simpleTeXBlanks(os, texrow, i,
1445 column, font, *style);
1449 // Do we need to change font?
1450 if ((font != running_font ||
1451 font.language() != running_font.language()) &&
1454 column += font.latexWriteStartChanges(os, basefont,
1456 running_font = font;
1460 if (c == Paragraph::META_NEWLINE) {
1461 // newlines are handled differently here than
1462 // the default in SimpleTeXSpecialChars().
1463 if (!style->newline_allowed) {
1467 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1470 basefont = getLayoutFont(bparams);
1471 running_font = basefont;
1472 if (font.family() ==
1473 LyXFont::TYPEWRITER_FAMILY) {
1482 texrow.start(this, i + 1);
1485 pimpl_->simpleTeXSpecialChars(buf, bparams,
1486 os, texrow, moving_arg,
1488 basefont, open_font,
1489 *style, i, column, c);
1493 // If we have an open font definition, we have to close it
1495 #ifdef FIXED_LANGUAGE_END_DETECTION
1498 .latexWriteEndChanges(os, basefont,
1499 next_->getFont(bparams,
1502 running_font.latexWriteEndChanges(os, basefont,
1506 #ifdef WITH_WARNINGS
1507 //#warning For now we ALWAYS have to close the foreign font settings if they are
1508 //#warning there as we start another \selectlanguage with the next paragraph if
1509 //#warning we are in need of this. This should be fixed sometime (Jug)
1511 running_font.latexWriteEndChanges(os, basefont, basefont);
1515 // Needed if there is an optional argument but no contents.
1516 if (main_body > 0 && main_body == size()) {
1518 return_value = false;
1522 column += endTeXParParams(bparams, os, moving_arg);
1525 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1526 return return_value;
1530 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1531 BufferParams const & bparams,
1532 ostream & os, TexRow & texrow)
1534 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1536 LyXLayout_ptr const & style = layout();
1538 Language const * language = getParLanguage(bparams);
1539 Language const * doc_language = bparams.language;
1540 Language const * previous_language = previous_
1541 ? previous_->getParLanguage(bparams) : doc_language;
1542 if (language->babel() != previous_language->babel()) {
1544 if (!lyxrc.language_command_end.empty() &&
1545 previous_language->babel() != doc_language->babel()) {
1546 os << subst(lyxrc.language_command_end, "$$lang",
1547 previous_language->babel())
1552 if (lyxrc.language_command_end.empty() ||
1553 language->babel() != doc_language->babel()) {
1554 os << subst(lyxrc.language_command_begin, "$$lang",
1561 bool leftindent_open = false;
1562 if (!params().leftIndent().zero()) {
1563 os << "\\begin{LyXParagraphLeftIndent}{" <<
1564 params().leftIndent().asLatexString() << "}\n";
1566 leftindent_open = true;
1569 if (style->isEnvironment()) {
1570 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1571 os << "\\begin{" << style->latexname() << "}{"
1572 << params().labelWidthString() << "}\n";
1573 } else if (style->labeltype == LABEL_BIBLIO) {
1575 os << "\\begin{" << style->latexname() << "}{"
1576 << bibitemWidest(buf)
1578 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1579 os << "\\begin{" << style->latexname() << '}'
1580 << style->latexparam() << '\n';
1582 os << "\\begin{" << style->latexname() << '}'
1583 << style->latexparam() << '\n';
1586 Paragraph * par = this;
1588 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1590 if (par && par->params().depth() > params().depth()) {
1591 if (par->layout()->isParagraph()) {
1594 // How to handle this? (Lgb)
1595 //&& !suffixIs(os, "\n\n")
1597 // There should be at least one '\n' already
1598 // but we need there to be two for Standard
1599 // paragraphs that are depth-increment'ed to be
1600 // output correctly. However, tables can
1601 // also be paragraphs so don't adjust them.
1604 // Will it ever harm to have one '\n' too
1605 // many? i.e. that we sometimes will have
1606 // three in a row. (Lgb)
1610 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1613 && par->layout() == layout()
1614 && par->params().depth() == params().depth()
1615 && par->params().leftIndent() == params().leftIndent());
1617 if (style->isEnvironment()) {
1618 os << "\\end{" << style->latexname() << "}\n";
1622 if (leftindent_open) {
1623 os << "\\end{LyXParagraphLeftIndent}\n";
1627 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1628 return par; // ale970302
1632 bool Paragraph::isHfill(pos_type pos) const
1634 return IsHfillChar(getChar(pos));
1638 bool Paragraph::isInset(pos_type pos) const
1640 return IsInsetChar(getChar(pos));
1644 bool Paragraph::isNewline(pos_type pos) const
1646 return pos >= 0 && IsNewlineChar(getChar(pos));
1650 bool Paragraph::isSeparator(pos_type pos) const
1652 return IsSeparatorChar(getChar(pos));
1656 bool Paragraph::isLineSeparator(pos_type pos) const
1658 value_type const c = getChar(pos);
1659 return IsLineSeparatorChar(c)
1660 || (IsInsetChar(c) && getInset(pos) &&
1661 getInset(pos)->isLineSeparator());
1665 bool Paragraph::isKomma(pos_type pos) const
1667 return IsKommaChar(getChar(pos));
1671 /// Used by the spellchecker
1672 bool Paragraph::isLetter(pos_type pos) const
1674 value_type const c = getChar(pos);
1675 if (IsLetterChar(c))
1678 return getInset(pos)->isLetter();
1679 // We want to pass the ' and escape chars to ispell
1680 string const extra = lyxrc.isp_esc_chars + '\'';
1681 return contains(extra, c);
1685 bool Paragraph::isWord(pos_type pos) const
1687 return IsWordChar(getChar(pos)) ;
1692 Paragraph::getParLanguage(BufferParams const & bparams) const
1695 #ifndef INHERIT_LANGUAGE
1696 return getFirstFontSettings().language();
1698 Language const * lang = getFirstFontSettings().language();
1699 #ifdef WITH_WARNINGS
1700 #warning We should make this somewhat better, any ideas? (Jug)
1702 if (lang == inherit_language || lang == ignore_language)
1703 lang = bparams.language;
1706 } else if (previous_)
1707 return previous_->getParLanguage(bparams);
1709 return bparams.language;
1713 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1715 return lyxrc.rtl_support
1716 && getParLanguage(bparams)->RightToLeft()
1717 && !(inInset() && inInset()->owner() &&
1718 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1722 void Paragraph::changeLanguage(BufferParams const & bparams,
1723 Language const * from, Language const * to)
1725 for (pos_type i = 0; i < size(); ++i) {
1726 LyXFont font = getFontSettings(bparams, i);
1727 if (font.language() == from) {
1728 font.setLanguage(to);
1735 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1737 Language const * doc_language = bparams.language;
1738 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1739 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1741 for (; cit != end; ++cit)
1742 if (cit->font().language() != ignore_language &&
1743 cit->font().language() != latex_language &&
1744 #ifdef INHERIT_LANGUAGE
1745 cit->font().language() != inherit_language &&
1747 cit->font().language() != doc_language)
1753 // Convert the paragraph to a string.
1754 // Used for building the table of contents
1755 string const Paragraph::asString(Buffer const * buffer, bool label)
1757 BufferParams const & bparams = buffer->params;
1759 if (label && !params().labelString().empty())
1760 s += params().labelString() + ' ';
1761 string::size_type const len = s.size();
1763 for (pos_type i = 0; i < size(); ++i) {
1764 value_type c = getChar(i);
1767 else if (c == META_INSET &&
1768 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1770 getInset(i)->ascii(buffer, ost);
1771 s += subst(ost.str().c_str(),'\n',' ');
1775 if (isRightToLeftPar(bparams))
1776 reverse(s.begin() + len,s.end());
1782 string const Paragraph::asString(Buffer const * buffer,
1783 pos_type beg, pos_type end, bool label)
1787 if (beg == 0 && label && !params().labelString().empty())
1788 ost << params().labelString() << ' ';
1790 for (pos_type i = beg; i < end; ++i) {
1791 value_type const c = getUChar(buffer->params, i);
1794 else if (c == META_NEWLINE)
1796 else if (c == META_HFILL)
1798 else if (c == META_INSET) {
1799 getInset(i)->ascii(buffer, ost);
1803 return ost.str().c_str();
1807 void Paragraph::setInsetOwner(Inset * i)
1809 pimpl_->inset_owner = i;
1810 InsetList::iterator it = insetlist.begin();
1811 InsetList::iterator end = insetlist.end();
1812 for (; it != end; ++it) {
1814 it.getInset()->setOwner(i);
1819 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1822 insetlist.deleteInsetsLyXText(bv);
1826 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1829 insetlist.resizeInsetsLyXText(bv);
1833 void Paragraph::setContentsFromPar(Paragraph * par)
1835 pimpl_->setContentsFromPar(par);
1839 lyx::pos_type Paragraph::size() const
1841 return pimpl_->size();
1845 bool Paragraph::empty() const
1847 return pimpl_->empty();
1851 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1853 return pimpl_->getChar(pos);
1857 int Paragraph::id() const
1863 LyXLayout_ptr const & Paragraph::layout() const
1869 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1871 layout_ = new_layout;
1875 bool Paragraph::isFirstInSequence() const
1877 Paragraph const * dhook = depthHook(getDepth());
1878 return (dhook == this
1879 || dhook->layout() != layout()
1880 || dhook->getDepth() != getDepth());
1884 Inset * Paragraph::inInset() const
1886 return pimpl_->inset_owner;
1890 void Paragraph::clearContents()
1895 void Paragraph::setChar(pos_type pos, value_type c)
1897 pimpl_->setChar(pos, c);
1901 ParagraphParameters & Paragraph::params()
1903 return pimpl_->params;
1907 ParagraphParameters const & Paragraph::params() const
1909 return pimpl_->params;
1913 Paragraph * Paragraph::getParFromID(int id) const
1915 return pimpl_->getParFromID(id);
1919 bool Paragraph::isFreeSpacing() const
1921 // for now we just need this, later should we need this in some
1922 // other way we can always add a function to Inset::() too.
1923 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1924 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);