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 font2.lyxWriteChanges(font1, os);
262 value_type const c = getChar(i);
266 Inset const * inset = getInset(i);
268 if (inset->directWrite()) {
269 // international char, let it write
270 // code directly so it's shorter in
272 inset->write(buf, os);
274 os << "\n\\begin_inset ";
275 inset->write(buf, os);
276 os << "\n\\end_inset \n\n";
282 os << "\n\\newline \n";
286 os << "\n\\hfill \n";
290 os << "\n\\backslash \n";
294 if (i + 1 < size() && getChar(i + 1) == ' ') {
301 if ((column > 70 && c == ' ')
306 // this check is to amend a bug. LyX sometimes
307 // inserts '\0' this could cause problems.
311 lyxerr << "ERROR (Paragraph::writeFile):"
312 " NULL char in structure." << endl;
320 void Paragraph::validate(LaTeXFeatures & features) const
322 pimpl_->validate(features, *layout());
326 // First few functions needed for cut and paste and paragraph breaking.
327 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
329 BufferParams bparams = buffer.params;
331 minibuffer_char = getChar(pos);
332 minibuffer_font = getFontSettings(bparams, pos);
333 minibuffer_inset = 0;
334 if (minibuffer_char == Paragraph::META_INSET) {
336 minibuffer_inset = getInset(pos)->clone(buffer);
338 minibuffer_inset = 0;
339 minibuffer_char = ' ';
340 // This reflects what GetInset() does (ARRae)
346 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
348 minibuffer_char = getChar(pos);
349 minibuffer_font = getFontSettings(bparams, pos);
350 minibuffer_inset = 0;
351 if (minibuffer_char == Paragraph::META_INSET) {
353 // the inset is not in a paragraph anymore
354 minibuffer_inset = insetlist.release(pos);
355 minibuffer_inset->parOwner(0);
357 minibuffer_inset = 0;
358 minibuffer_char = ' ';
359 // This reflects what GetInset() does (ARRae)
364 // Erase(pos); now the caller is responsible for that.
368 bool Paragraph::insertFromMinibuffer(pos_type pos)
370 if (minibuffer_char == Paragraph::META_INSET) {
371 if (!insetAllowed(minibuffer_inset->lyxCode())) {
374 insertInset(pos, minibuffer_inset, minibuffer_font);
376 LyXFont f = minibuffer_font;
377 if (!checkInsertChar(f)) {
380 insertChar(pos, minibuffer_char, f);
388 void Paragraph::erase(pos_type pos)
394 bool Paragraph::checkInsertChar(LyXFont & font)
396 if (pimpl_->inset_owner)
397 return pimpl_->inset_owner->checkInsertChar(font);
402 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
404 LyXFont const f(LyXFont::ALL_INHERIT);
405 insertChar(pos, c, f);
409 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
410 LyXFont const & font)
412 pimpl_->insertChar(pos, c, font);
416 void Paragraph::insertInset(pos_type pos, Inset * inset)
418 LyXFont const f(LyXFont::ALL_INHERIT);
419 insertInset(pos, inset, f);
423 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
425 pimpl_->insertInset(pos, inset, font);
429 bool Paragraph::insetAllowed(Inset::Code code)
431 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
433 if (pimpl_->inset_owner)
434 return pimpl_->inset_owner->insetAllowed(code);
439 Inset * Paragraph::getInset(pos_type pos)
441 lyx::Assert(pos < size());
443 return insetlist.get(pos);
447 Inset const * Paragraph::getInset(pos_type pos) const
449 lyx::Assert(pos < size());
451 return insetlist.get(pos);
455 // Gets uninstantiated font setting at position.
456 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
459 lyx::Assert(pos <= size());
461 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
462 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
463 for (; cit != end; ++cit) {
464 if (cit->pos() >= pos)
470 retfont = cit->font();
471 } else if (pos == size() && !empty()) {
472 retfont = getFontSettings(bparams, pos - 1);
474 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
480 // Gets uninstantiated font setting at position 0
481 LyXFont const Paragraph::getFirstFontSettings() const
483 if (!empty() && !pimpl_->fontlist.empty())
484 return pimpl_->fontlist[0].font();
486 return LyXFont(LyXFont::ALL_INHERIT);
490 // Gets the fully instantiated font at a given position in a paragraph
491 // This is basically the same function as LyXText::GetFont() in text2.C.
492 // The difference is that this one is used for generating the LaTeX file,
493 // and thus cosmetic "improvements" are disallowed: This has to deliver
494 // the true picture of the buffer. (Asger)
495 // If position is -1, we get the layout font of the paragraph.
496 // If position is -2, we get the font of the manual label of the paragraph.
497 LyXFont const Paragraph::getFont(BufferParams const & bparams,
500 lyx::Assert(pos >= 0);
502 LyXLayout_ptr const & lout = layout();
504 pos_type main_body = 0;
505 if (lout->labeltype == LABEL_MANUAL)
506 main_body = beginningOfMainBody();
510 layoutfont = lout->labelfont;
512 layoutfont = lout->font;
514 LyXFont tmpfont = getFontSettings(bparams, pos);
515 tmpfont.realize(layoutfont);
517 return pimpl_->realizeFont(tmpfont, bparams);
521 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
523 LyXLayout_ptr const & lout = layout();
525 LyXFont tmpfont = lout->labelfont;
526 tmpfont.setLanguage(getParLanguage(bparams));
528 return pimpl_->realizeFont(tmpfont, bparams);
532 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
534 LyXLayout_ptr const & lout = layout();
536 LyXFont tmpfont = lout->font;
537 tmpfont.setLanguage(getParLanguage(bparams));
539 return pimpl_->realizeFont(tmpfont, bparams);
543 /// Returns the height of the highest font in range
545 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
546 LyXFont::FONT_SIZE const def_size) const
548 if (pimpl_->fontlist.empty())
551 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
552 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
553 for (; end_it != end; ++end_it) {
554 if (end_it->pos() >= endpos)
561 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
562 for (; cit != end; ++cit) {
563 if (cit->pos() >= startpos)
567 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
568 for (; cit != end_it; ++cit) {
569 LyXFont::FONT_SIZE size = cit->font().size();
570 if (size == LyXFont::INHERIT_SIZE)
572 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
579 Paragraph::value_type
580 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
582 value_type c = getChar(pos);
583 if (!lyxrc.rtl_support)
613 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
620 void Paragraph::setFont(pos_type pos, LyXFont const & font)
622 lyx::Assert(pos <= size());
624 // First, reduce font against layout/label font
625 // Update: The SetCharFont() routine in text2.C already
626 // reduces font, so we don't need to do that here. (Asger)
627 // No need to simplify this because it will disappear
628 // in a new kernel. (Asger)
629 // Next search font table
631 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
632 Pimpl::FontList::iterator it = beg;
633 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
634 for (; it != endit; ++it) {
635 if (it->pos() >= pos)
638 unsigned int i = std::distance(beg, it);
639 bool notfound = (it == endit);
641 if (!notfound && pimpl_->fontlist[i].font() == font)
644 bool begin = pos == 0 || notfound ||
645 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
646 // Is position pos is a beginning of a font block?
647 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
648 // Is position pos is the end of a font block?
649 if (begin && end) { // A single char block
650 if (i + 1 < pimpl_->fontlist.size() &&
651 pimpl_->fontlist[i + 1].font() == font) {
652 // Merge the singleton block with the next block
653 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
654 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
655 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
656 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
657 // Merge the singleton block with the previous block
658 pimpl_->fontlist[i - 1].pos(pos);
659 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
661 pimpl_->fontlist[i].font(font);
663 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
664 pimpl_->fontlist[i - 1].pos(pos);
666 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
667 Pimpl::FontTable(pos, font));
669 pimpl_->fontlist[i].pos(pos - 1);
670 if (!(i + 1 < pimpl_->fontlist.size() &&
671 pimpl_->fontlist[i + 1].font() == font))
672 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
673 Pimpl::FontTable(pos, font));
674 } else { // The general case. The block is splitted into 3 blocks
675 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
676 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
677 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
678 Pimpl::FontTable(pos, font));
684 void Paragraph::next(Paragraph * p)
690 // This function is able to hide closed footnotes.
691 Paragraph * Paragraph::next()
697 Paragraph const * Paragraph::next() const
703 void Paragraph::previous(Paragraph * p)
709 // This function is able to hide closed footnotes.
710 Paragraph * Paragraph::previous()
716 // This function is able to hide closed footnotes.
717 Paragraph const * Paragraph::previous() const
724 void Paragraph::makeSameLayout(Paragraph const * par)
726 layout(par->layout());
728 params() = par->params();
732 int Paragraph::stripLeadingSpaces()
734 if (layout()->free_spacing ||
740 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
749 bool Paragraph::hasSameLayout(Paragraph const * par) const
752 par->layout() == layout() &&
753 params().sameLayout(par->params());
757 int Paragraph::getEndLabel() const
759 Paragraph const * par = this;
760 depth_type par_depth = getDepth();
762 LyXLayout_ptr const & layout = par->layout();
763 int const endlabeltype = layout->endlabeltype;
765 if (endlabeltype != END_LABEL_NO_LABEL) {
769 depth_type const next_depth = next_->getDepth();
770 if (par_depth > next_depth ||
771 (par_depth == next_depth
772 && layout != next_->layout()))
778 par = par->outerHook();
780 par_depth = par->getDepth();
782 return END_LABEL_NO_LABEL;
786 Paragraph::depth_type Paragraph::getDepth() const
788 return params().depth();
792 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
794 bool const isenv = layout()->isEnvironment();
797 return params().depth() + 1;
799 return params().depth();
803 char Paragraph::getAlign() const
805 return params().align();
809 string const & Paragraph::getLabelstring() const
811 return params().labelString();
815 // the next two functions are for the manual labels
816 string const Paragraph::getLabelWidthString() const
818 if (!params().labelWidthString().empty())
819 return params().labelWidthString();
821 return _("Senseless with this layout!");
825 void Paragraph::setLabelWidthString(string const & s)
827 params().labelWidthString(s);
831 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
834 params().labelWidthString(string());
835 params().align(LYX_ALIGN_LAYOUT);
836 params().spaceTop(VSpace(VSpace::NONE));
837 params().spaceBottom(VSpace(VSpace::NONE));
838 params().spacing(Spacing(Spacing::Default));
842 // if the layout of a paragraph contains a manual label, the beginning of the
843 // main body is the beginning of the second word. This is what the par-
844 // function returns. If the layout does not contain a label, the main
845 // body always starts with position 0. This differentiation is necessary,
846 // because there cannot be a newline or a blank <= the beginning of the
849 int Paragraph::beginningOfMainBody() const
851 // Unroll the first two cycles of the loop
852 // and remember the previous character to
853 // remove unnecessary GetChar() calls
855 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
857 char previous_char = 0;
860 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
861 // Yes, this ^ is supposed to be "= " not "=="
864 && previous_char != ' '
865 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
867 previous_char = temp;
876 Paragraph * Paragraph::depthHook(depth_type depth)
878 Paragraph * newpar = this;
881 newpar = newpar->previous();
882 } while (newpar && newpar->getDepth() > depth);
885 if (previous() || getDepth())
886 lyxerr << "ERROR (Paragraph::DepthHook): "
895 Paragraph const * Paragraph::depthHook(depth_type depth) const
897 Paragraph const * newpar = this;
900 newpar = newpar->previous();
901 } while (newpar && newpar->getDepth() > depth);
904 if (previous() || getDepth())
905 lyxerr << "ERROR (Paragraph::DepthHook): "
914 Paragraph * Paragraph::outerHook()
918 return depthHook(depth_type(getDepth() - 1));
922 Paragraph const * Paragraph::outerHook() const
926 return depthHook(depth_type(getDepth() - 1));
930 // returns -1 if inset not found
931 int Paragraph::getPositionOfInset(Inset const * inset) const
934 InsetList::iterator it = insetlist.begin();
935 InsetList::iterator end = insetlist.end();
936 for (; it != end; ++it) {
937 if (it.getInset() == inset) {
949 InsetOptArg * optArgInset(Paragraph const & par)
952 InsetList::iterator it = par.insetlist.begin();
953 InsetList::iterator end = par.insetlist.end();
954 for (; it != end; ++it) {
955 Inset * ins = it.getInset();
956 if (ins->lyxCode() == Inset::OPTARG_CODE) {
957 return static_cast<InsetOptArg *>(ins);
965 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
966 BufferParams const & bparams,
967 ostream & os, TexRow & texrow,
970 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
971 Inset const * in = inInset();
972 bool further_blank_line = false;
975 // well we have to check if we are in an inset with unlimited
976 // lenght (all in one row) if that is true then we don't allow
977 // any special options in the paragraph and also we don't allow
978 // any environment other then "Standard" to be valid!
979 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
982 if (params().startOfAppendix()) {
983 os << "\\appendix\n";
987 if (!params().spacing().isDefault()
988 && (!previous() || !previous()->hasSameLayout(this))) {
989 os << params().spacing().writeEnvirBegin() << "\n";
993 if (style->isCommand()) {
998 if (params().pagebreakTop()) {
1000 further_blank_line = true;
1002 if (params().spaceTop().kind() != VSpace::NONE) {
1003 os << params().spaceTop().asLatexCommand(bparams);
1004 further_blank_line = true;
1007 if (params().lineTop()) {
1008 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1009 << "\\vspace{-1\\parskip}";
1010 further_blank_line = true;
1013 if (further_blank_line) {
1018 style = bparams.getLyXTextClass().defaultLayout();
1021 Language const * language = getParLanguage(bparams);
1022 Language const * doc_language = bparams.language;
1023 Language const * previous_language = previous()
1024 ? previous()->getParLanguage(bparams) : doc_language;
1026 if (language->babel() != previous_language->babel()
1027 // check if we already put language command in TeXEnvironment()
1028 && !(style->isEnvironment()
1029 && (!previous() || previous()->layout() != layout() ||
1030 previous()->params().depth() != params().depth())))
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)
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(ost.str().c_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)
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 ost.str().c_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);