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"
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
38 #include "support/lyxmanip.h"
39 #include "support/FileInfo.h"
40 #include "support/LAssert.h"
41 #include "support/textutils.h"
51 using std::lower_bound;
52 using std::upper_bound;
57 // this is a bad idea, but how can Paragraph find its buffer to get
58 // parameters? (JMarc)
60 extern string bibitemWidest(Buffer const *);
62 // this is a minibuffer
67 LyXFont minibuffer_font;
68 Inset * minibuffer_inset;
73 extern BufferView * current_view;
76 Paragraph::Paragraph()
77 : pimpl_(new Paragraph::Pimpl(this))
85 bibkey = 0; // ale970302
91 // This constructor inserts the new paragraph in a list.
92 Paragraph::Paragraph(Paragraph * par)
93 : pimpl_(new Paragraph::Pimpl(this))
98 // double linked list begin
101 next_->previous_ = this;
103 previous_->next_ = this;
106 bibkey = 0; // ale970302
112 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
113 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
121 // this is because of the dummy layout of the paragraphs that
123 layout_ = lp.layout();
127 bibkey = static_cast<InsetBibKey *>
128 (lp.bibkey->clone(*current_view->buffer()));
133 // copy everything behind the break-position to the new paragraph
134 insetlist = lp.insetlist;
135 InsetList::iterator it = insetlist.begin();
136 InsetList::iterator end = insetlist.end();
137 for (; it != end; ++it) {
138 it.setInset(it.getInset()->clone(*current_view->buffer(),
140 // tell the new inset who is the boss now
141 it.getInset()->parOwner(this);
146 // the destructor removes the new paragraph from the list
147 Paragraph::~Paragraph()
151 previous_->next_ = next_;
153 next_->previous_ = previous_;
161 //lyxerr << "Paragraph::paragraph_id = "
162 // << Paragraph::paragraph_id << endl;
166 void Paragraph::write(Buffer const * buf, ostream & os,
167 BufferParams const & bparams,
168 depth_type dth) const
170 // The beginning or end of a deeper (i.e. nested) area?
171 if (dth != params().depth()) {
172 if (params().depth() > dth) {
173 while (params().depth() > dth) {
174 os << "\n\\begin_deeper ";
178 while (params().depth() < dth) {
179 os << "\n\\end_deeper ";
185 // First write the layout
186 os << "\n\\layout " << layout()->name() << "\n";
188 // Maybe some vertical spaces.
189 if (params().spaceTop().kind() != VSpace::NONE)
190 os << "\\added_space_top "
191 << params().spaceTop().asLyXCommand() << " ";
192 if (params().spaceBottom().kind() != VSpace::NONE)
193 os << "\\added_space_bottom "
194 << params().spaceBottom().asLyXCommand() << " ";
196 // Maybe the paragraph has special spacing
197 params().spacing().writeFile(os, true);
199 // The labelwidth string used in lists.
200 if (!params().labelWidthString().empty())
201 os << "\\labelwidthstring "
202 << params().labelWidthString() << '\n';
204 // Lines above or below?
205 if (params().lineTop())
207 if (params().lineBottom())
208 os << "\\line_bottom ";
210 // Pagebreaks above or below?
211 if (params().pagebreakTop())
212 os << "\\pagebreak_top ";
213 if (params().pagebreakBottom())
214 os << "\\pagebreak_bottom ";
216 // Start of appendix?
217 if (params().startOfAppendix())
218 os << "\\start_of_appendix ";
221 if (params().noindent())
224 // Do we have a manual left indent?
225 if (!params().leftIndent().zero())
226 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 #ifndef INHERIT_LANGUAGE
257 font2.lyxWriteChanges(font1, os);
259 font2.lyxWriteChanges(font1, bparams.language, os);
265 value_type const c = getChar(i);
269 Inset const * inset = getInset(i);
271 if (inset->directWrite()) {
272 // international char, let it write
273 // code directly so it's shorter in
275 inset->write(buf, os);
277 os << "\n\\begin_inset ";
278 inset->write(buf, os);
279 os << "\n\\end_inset \n\n";
285 os << "\n\\newline \n";
289 os << "\n\\hfill \n";
293 os << "\n\\backslash \n";
297 if (i + 1 < size() && getChar(i + 1) == ' ') {
304 if ((column > 70 && c == ' ')
309 // this check is to amend a bug. LyX sometimes
310 // inserts '\0' this could cause problems.
314 lyxerr << "ERROR (Paragraph::writeFile):"
315 " NULL char in structure." << endl;
323 void Paragraph::validate(LaTeXFeatures & features) const
325 pimpl_->validate(features, *layout());
329 // First few functions needed for cut and paste and paragraph breaking.
330 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
332 BufferParams bparams = buffer.params;
334 minibuffer_char = getChar(pos);
335 minibuffer_font = getFontSettings(bparams, pos);
336 minibuffer_inset = 0;
337 if (minibuffer_char == Paragraph::META_INSET) {
339 minibuffer_inset = getInset(pos)->clone(buffer);
341 minibuffer_inset = 0;
342 minibuffer_char = ' ';
343 // This reflects what GetInset() does (ARRae)
349 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
351 minibuffer_char = getChar(pos);
352 minibuffer_font = getFontSettings(bparams, pos);
353 minibuffer_inset = 0;
354 if (minibuffer_char == Paragraph::META_INSET) {
356 // the inset is not in a paragraph anymore
357 minibuffer_inset = insetlist.release(pos);
358 minibuffer_inset->parOwner(0);
360 minibuffer_inset = 0;
361 minibuffer_char = ' ';
362 // This reflects what GetInset() does (ARRae)
367 // Erase(pos); now the caller is responsible for that.
371 bool Paragraph::insertFromMinibuffer(pos_type pos)
373 if (minibuffer_char == Paragraph::META_INSET) {
374 if (!insetAllowed(minibuffer_inset->lyxCode())) {
377 insertInset(pos, minibuffer_inset, minibuffer_font);
379 LyXFont f = minibuffer_font;
380 if (!checkInsertChar(f)) {
383 insertChar(pos, minibuffer_char, f);
391 void Paragraph::erase(pos_type pos)
397 bool Paragraph::checkInsertChar(LyXFont & font)
399 if (pimpl_->inset_owner)
400 return pimpl_->inset_owner->checkInsertChar(font);
405 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
407 LyXFont const f(LyXFont::ALL_INHERIT);
408 insertChar(pos, c, f);
412 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
413 LyXFont const & font)
415 pimpl_->insertChar(pos, c, font);
419 void Paragraph::insertInset(pos_type pos, Inset * inset)
421 LyXFont const f(LyXFont::ALL_INHERIT);
422 insertInset(pos, inset, f);
426 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
428 pimpl_->insertInset(pos, inset, font);
432 bool Paragraph::insetAllowed(Inset::Code code)
434 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
436 if (pimpl_->inset_owner)
437 return pimpl_->inset_owner->insetAllowed(code);
442 Inset * Paragraph::getInset(pos_type pos)
444 lyx::Assert(pos < size());
446 return insetlist.get(pos);
450 Inset const * Paragraph::getInset(pos_type pos) const
452 lyx::Assert(pos < size());
454 return insetlist.get(pos);
458 // Gets uninstantiated font setting at position.
459 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
462 lyx::Assert(pos <= size());
464 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
465 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
466 for (; cit != end; ++cit) {
467 if (cit->pos() >= pos)
473 retfont = cit->font();
474 } else if (pos == size() && !empty()) {
475 retfont = getFontSettings(bparams, pos - 1);
477 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
478 #ifdef INHERIT_LANGUAGE
479 if (retfont.language() == inherit_language)
480 retfont.setLanguage(bparams.language);
487 // Gets uninstantiated font setting at position 0
488 LyXFont const Paragraph::getFirstFontSettings() const
490 if (!empty() && !pimpl_->fontlist.empty())
491 return pimpl_->fontlist[0].font();
493 return LyXFont(LyXFont::ALL_INHERIT);
497 // Gets the fully instantiated font at a given position in a paragraph
498 // This is basically the same function as LyXText::GetFont() in text2.C.
499 // The difference is that this one is used for generating the LaTeX file,
500 // and thus cosmetic "improvements" are disallowed: This has to deliver
501 // the true picture of the buffer. (Asger)
502 // If position is -1, we get the layout font of the paragraph.
503 // If position is -2, we get the font of the manual label of the paragraph.
504 LyXFont const Paragraph::getFont(BufferParams const & bparams,
507 lyx::Assert(pos >= 0);
509 LyXLayout_ptr const & lout = layout();
511 pos_type main_body = 0;
512 if (lout->labeltype == LABEL_MANUAL)
513 main_body = beginningOfMainBody();
517 layoutfont = lout->labelfont;
519 layoutfont = lout->font;
521 LyXFont tmpfont = getFontSettings(bparams, pos);
522 #ifndef INHERIT_LANGUAGE
523 tmpfont.realize(layoutfont);
525 tmpfont.realize(layoutfont, bparams.language);
528 return pimpl_->realizeFont(tmpfont, bparams);
532 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
534 LyXLayout_ptr const & lout = layout();
536 LyXFont tmpfont = lout->labelfont;
537 tmpfont.setLanguage(getParLanguage(bparams));
539 return pimpl_->realizeFont(tmpfont, bparams);
543 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
545 LyXLayout_ptr const & lout = layout();
547 LyXFont tmpfont = lout->font;
548 tmpfont.setLanguage(getParLanguage(bparams));
550 return pimpl_->realizeFont(tmpfont, bparams);
554 /// Returns the height of the highest font in range
556 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
557 LyXFont::FONT_SIZE const def_size) const
559 if (pimpl_->fontlist.empty())
562 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
563 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
564 for (; end_it != end; ++end_it) {
565 if (end_it->pos() >= endpos)
572 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
573 for (; cit != end; ++cit) {
574 if (cit->pos() >= startpos)
578 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
579 for (; cit != end_it; ++cit) {
580 LyXFont::FONT_SIZE size = cit->font().size();
581 if (size == LyXFont::INHERIT_SIZE)
583 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
590 Paragraph::value_type
591 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
593 value_type c = getChar(pos);
594 if (!lyxrc.rtl_support)
624 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
631 void Paragraph::setFont(pos_type pos, LyXFont const & font)
633 lyx::Assert(pos <= size());
635 // First, reduce font against layout/label font
636 // Update: The SetCharFont() routine in text2.C already
637 // reduces font, so we don't need to do that here. (Asger)
638 // No need to simplify this because it will disappear
639 // in a new kernel. (Asger)
640 // Next search font table
642 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
643 Pimpl::FontList::iterator it = beg;
644 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
645 for (; it != endit; ++it) {
646 if (it->pos() >= pos)
649 unsigned int i = std::distance(beg, it);
650 bool notfound = (it == endit);
652 if (!notfound && pimpl_->fontlist[i].font() == font)
655 bool begin = pos == 0 || notfound ||
656 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
657 // Is position pos is a beginning of a font block?
658 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
659 // Is position pos is the end of a font block?
660 if (begin && end) { // A single char block
661 if (i + 1 < pimpl_->fontlist.size() &&
662 pimpl_->fontlist[i + 1].font() == font) {
663 // Merge the singleton block with the next block
664 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
665 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
666 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
667 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
668 // Merge the singleton block with the previous block
669 pimpl_->fontlist[i - 1].pos(pos);
670 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
672 pimpl_->fontlist[i].font(font);
674 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
675 pimpl_->fontlist[i - 1].pos(pos);
677 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
678 Pimpl::FontTable(pos, font));
680 pimpl_->fontlist[i].pos(pos - 1);
681 if (!(i + 1 < pimpl_->fontlist.size() &&
682 pimpl_->fontlist[i + 1].font() == font))
683 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
684 Pimpl::FontTable(pos, font));
685 } else { // The general case. The block is splitted into 3 blocks
686 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
687 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
688 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
689 Pimpl::FontTable(pos, font));
695 void Paragraph::next(Paragraph * p)
701 // This function is able to hide closed footnotes.
702 Paragraph * Paragraph::next()
708 Paragraph const * Paragraph::next() const
714 void Paragraph::previous(Paragraph * p)
720 // This function is able to hide closed footnotes.
721 Paragraph * Paragraph::previous()
727 // This function is able to hide closed footnotes.
728 Paragraph const * Paragraph::previous() const
735 void Paragraph::makeSameLayout(Paragraph const * par)
737 layout(par->layout());
739 params() = par->params();
743 int Paragraph::stripLeadingSpaces()
745 if (layout()->free_spacing ||
751 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
760 bool Paragraph::hasSameLayout(Paragraph const * par) const
763 par->layout() == layout() &&
764 params().sameLayout(par->params());
768 // Be carefull, this does not make any check at all.
769 // This method has wrong name, it combined this par with the next par.
770 // In that sense it is the reverse of break paragraph. (Lgb)
771 void Paragraph::pasteParagraph(BufferParams const & bparams)
773 // copy the next paragraph to this one
774 Paragraph * the_next = next();
776 // first the DTP-stuff
777 params().lineBottom(the_next->params().lineBottom());
778 params().spaceBottom(the_next->params().spaceBottom());
779 params().pagebreakBottom(the_next->params().pagebreakBottom());
781 pos_type pos_end = the_next->pimpl_->size() - 1;
782 pos_type pos_insert = size();
784 // ok, now copy the paragraph
785 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
786 the_next->cutIntoMinibuffer(bparams, i);
787 if (insertFromMinibuffer(pos_insert + j))
791 // delete the next paragraph
792 Paragraph * ppar = the_next->previous_;
793 Paragraph * npar = the_next->next_;
799 int Paragraph::getEndLabel() const
801 Paragraph const * par = this;
802 depth_type par_depth = getDepth();
804 LyXLayout_ptr const & layout = par->layout();
805 int const endlabeltype = layout->endlabeltype;
807 if (endlabeltype != END_LABEL_NO_LABEL) {
811 depth_type const next_depth = next_->getDepth();
812 if (par_depth > next_depth ||
813 (par_depth == next_depth
814 && layout != next_->layout()))
820 par = par->outerHook();
822 par_depth = par->getDepth();
824 return END_LABEL_NO_LABEL;
828 Paragraph::depth_type Paragraph::getDepth() const
830 return params().depth();
834 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
836 bool const isenv = layout()->isEnvironment();
839 return params().depth() + 1;
841 return params().depth();
845 char Paragraph::getAlign() const
847 return params().align();
851 string const & Paragraph::getLabelstring() const
853 return params().labelString();
857 // the next two functions are for the manual labels
858 string const Paragraph::getLabelWidthString() const
860 if (!params().labelWidthString().empty())
861 return params().labelWidthString();
863 return _("Senseless with this layout!");
867 void Paragraph::setLabelWidthString(string const & s)
869 params().labelWidthString(s);
873 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
876 params().labelWidthString(string());
877 params().align(LYX_ALIGN_LAYOUT);
878 params().spaceTop(VSpace(VSpace::NONE));
879 params().spaceBottom(VSpace(VSpace::NONE));
880 params().spacing(Spacing(Spacing::Default));
884 // if the layout of a paragraph contains a manual label, the beginning of the
885 // main body is the beginning of the second word. This is what the par-
886 // function returns. If the layout does not contain a label, the main
887 // body always starts with position 0. This differentiation is necessary,
888 // because there cannot be a newline or a blank <= the beginning of the
891 int Paragraph::beginningOfMainBody() const
893 // Unroll the first two cycles of the loop
894 // and remember the previous character to
895 // remove unnecessary GetChar() calls
897 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
899 char previous_char = 0;
902 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
903 // Yes, this ^ is supposed to be "= " not "=="
906 && previous_char != ' '
907 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
909 previous_char = temp;
918 Paragraph * Paragraph::depthHook(depth_type depth)
920 Paragraph * newpar = this;
923 newpar = newpar->previous();
924 } while (newpar && newpar->getDepth() > depth);
927 if (previous() || getDepth())
928 lyxerr << "ERROR (Paragraph::DepthHook): "
937 Paragraph const * Paragraph::depthHook(depth_type depth) const
939 Paragraph const * newpar = this;
942 newpar = newpar->previous();
943 } while (newpar && newpar->getDepth() > depth);
946 if (previous() || getDepth())
947 lyxerr << "ERROR (Paragraph::DepthHook): "
956 Paragraph * Paragraph::outerHook()
960 return depthHook(depth_type(getDepth() - 1));
964 Paragraph const * Paragraph::outerHook() const
968 return depthHook(depth_type(getDepth() - 1));
972 // returns -1 if inset not found
973 int Paragraph::getPositionOfInset(Inset const * inset) const
976 InsetList::iterator it = insetlist.begin();
977 InsetList::iterator end = insetlist.end();
978 for (; it != end; ++it) {
979 if (it.getInset() == inset) {
990 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
991 BufferParams const & bparams,
992 ostream & os, TexRow & texrow,
995 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
996 Inset const * in = inInset();
997 bool further_blank_line = false;
1000 // well we have to check if we are in an inset with unlimited
1001 // lenght (all in one row) if that is true then we don't allow
1002 // any special options in the paragraph and also we don't allow
1003 // any environment other then "Standard" to be valid!
1004 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1007 if (params().startOfAppendix()) {
1008 os << "\\appendix\n";
1012 if (!params().spacing().isDefault()
1013 && (!previous() || !previous()->hasSameLayout(this))) {
1014 os << params().spacing().writeEnvirBegin() << "\n";
1018 if (style->isCommand()) {
1023 if (params().pagebreakTop()) {
1025 further_blank_line = true;
1027 if (params().spaceTop().kind() != VSpace::NONE) {
1028 os << params().spaceTop().asLatexCommand(bparams);
1029 further_blank_line = true;
1032 if (params().lineTop()) {
1033 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1034 << "\\vspace{-1\\parskip}";
1035 further_blank_line = true;
1038 if (further_blank_line) {
1043 style = bparams.getLyXTextClass().defaultLayout();
1046 Language const * language = getParLanguage(bparams);
1047 Language const * doc_language = bparams.language;
1048 Language const * previous_language = previous()
1049 ? previous()->getParLanguage(bparams) : doc_language;
1051 if (language->babel() != previous_language->babel()
1052 // check if we already put language command in TeXEnvironment()
1053 && !(style->isEnvironment()
1054 && (!previous() || previous()->layout() != layout() ||
1055 previous()->params().depth() != params().depth())))
1057 if (!lyxrc.language_command_end.empty() &&
1058 previous_language->babel() != doc_language->babel())
1060 os << subst(lyxrc.language_command_end, "$$lang",
1061 previous_language->babel())
1066 if (lyxrc.language_command_end.empty() ||
1067 language->babel() != doc_language->babel())
1069 os << subst(lyxrc.language_command_begin, "$$lang",
1076 if (bparams.inputenc == "auto" &&
1077 language->encoding() != previous_language->encoding()) {
1078 os << "\\inputencoding{"
1079 << language->encoding()->LatexName()
1084 switch (style->latextype) {
1087 << style->latexname()
1088 << style->latexparam();
1090 case LATEX_ITEM_ENVIRONMENT:
1092 bibkey->latex(buf, os, false, false);
1096 case LATEX_LIST_ENVIRONMENT:
1103 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1105 // Make sure that \\par is done with the font of the last
1106 // character if this has another size as the default.
1107 // This is necessary because LaTeX (and LyX on the screen)
1108 // calculates the space between the baselines according
1109 // to this font. (Matthias)
1111 // Is this really needed ? (Dekel)
1112 // We do not need to use to change the font for the last paragraph
1113 // or for a command.
1114 LyXFont const font =
1116 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1118 bool is_command = style->isCommand();
1120 if (style->resfont.size() != font.size() && next_ && !is_command) {
1123 os << "\\" << font.latexSize() << " \\par}";
1124 } else if (need_par) {
1126 } else if (is_command)
1129 switch (style->latextype) {
1130 case LATEX_ITEM_ENVIRONMENT:
1131 case LATEX_LIST_ENVIRONMENT:
1132 if (next_ && (params().depth() < next_->params().depth())) {
1137 case LATEX_ENVIRONMENT:
1138 // if its the last paragraph of the current environment
1139 // skip it otherwise fall through
1141 && (next_->layout() != layout()
1142 || next_->params().depth() != params().depth()))
1144 // fall through possible
1146 // we don't need it for the last paragraph!!!
1153 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1154 further_blank_line = false;
1155 if (params().lineBottom()) {
1156 os << "\\lyxline{\\" << font.latexSize() << '}';
1157 further_blank_line = true;
1160 if (params().spaceBottom().kind() != VSpace::NONE) {
1161 os << params().spaceBottom().asLatexCommand(bparams);
1162 further_blank_line = true;
1165 if (params().pagebreakBottom()) {
1167 further_blank_line = true;
1170 if (further_blank_line) {
1175 if (!params().spacing().isDefault()
1176 && (!next_ || !next_->hasSameLayout(this))) {
1177 os << params().spacing().writeEnvirEnd() << "\n";
1182 // we don't need it for the last paragraph!!!
1187 // Since \selectlanguage write the language to the aux file,
1188 // we need to reset the language at the end of footnote or
1191 if (language->babel() != doc_language->babel()) {
1192 if (lyxrc.language_command_end.empty())
1193 os << subst(lyxrc.language_command_begin,
1195 doc_language->babel())
1198 os << subst(lyxrc.language_command_end,
1206 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1211 // This could go to ParagraphParameters if we want to
1212 int Paragraph::startTeXParParams(BufferParams const & bparams,
1213 ostream & os, bool moving_arg) const
1217 if (params().noindent()) {
1218 os << "\\noindent ";
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 case LYX_ALIGN_RIGHT:
1230 case LYX_ALIGN_CENTER:
1238 switch (params().align()) {
1239 case LYX_ALIGN_NONE:
1240 case LYX_ALIGN_BLOCK:
1241 case LYX_ALIGN_LAYOUT:
1242 case LYX_ALIGN_SPECIAL:
1244 case LYX_ALIGN_LEFT:
1245 if (getParLanguage(bparams)->babel() != "hebrew") {
1246 os << "\\begin{flushleft}";
1249 os << "\\begin{flushright}";
1253 case LYX_ALIGN_RIGHT:
1254 if (getParLanguage(bparams)->babel() != "hebrew") {
1255 os << "\\begin{flushright}";
1258 os << "\\begin{flushleft}";
1262 case LYX_ALIGN_CENTER:
1263 os << "\\begin{center}";
1272 // This could go to ParagraphParameters if we want to
1273 int Paragraph::endTeXParParams(BufferParams const & bparams,
1274 ostream & os, bool moving_arg) const
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 case LYX_ALIGN_RIGHT:
1286 case LYX_ALIGN_CENTER:
1294 switch (params().align()) {
1295 case LYX_ALIGN_NONE:
1296 case LYX_ALIGN_BLOCK:
1297 case LYX_ALIGN_LAYOUT:
1298 case LYX_ALIGN_SPECIAL:
1300 case LYX_ALIGN_LEFT:
1301 if (getParLanguage(bparams)->babel() != "hebrew") {
1302 os << "\\end{flushleft}";
1305 os << "\\end{flushright}";
1309 case LYX_ALIGN_RIGHT:
1310 if (getParLanguage(bparams)->babel() != "hebrew") {
1311 os << "\\end{flushright}";
1314 os << "\\end{flushleft}";
1318 case LYX_ALIGN_CENTER:
1319 os << "\\end{center}";
1327 // This one spits out the text of the paragraph
1328 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1329 BufferParams const & bparams,
1330 ostream & os, TexRow & texrow,
1333 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1335 bool return_value = false;
1337 LyXLayout_ptr style;
1339 // well we have to check if we are in an inset with unlimited
1340 // lenght (all in one row) if that is true then we don't allow
1341 // any special options in the paragraph and also we don't allow
1342 // any environment other then "Standard" to be valid!
1344 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1347 style = bparams.getLyXTextClass().defaultLayout();
1354 // Maybe we have to create a optional argument.
1356 if (style->labeltype != LABEL_MANUAL)
1359 main_body = beginningOfMainBody();
1363 if (main_body > 0) {
1366 basefont = getLabelFont(bparams);
1368 basefont = getLayoutFont(bparams);
1371 moving_arg |= style->needprotect;
1373 // Which font is currently active?
1374 LyXFont running_font(basefont);
1375 // Do we have an open font change?
1376 bool open_font = false;
1378 texrow.start(this, 0);
1380 // if the paragraph is empty, the loop will not be entered at all
1382 if (style->isCommand()) {
1387 column += startTeXParParams(bparams, os, moving_arg);
1391 for (pos_type i = 0; i < size(); ++i) {
1393 // First char in paragraph or after label?
1394 if (i == main_body) {
1395 if (main_body > 0) {
1397 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1400 basefont = getLayoutFont(bparams);
1401 running_font = basefont;
1405 if (style->isCommand()) {
1411 column += startTeXParParams(bparams, os,
1415 value_type c = getChar(i);
1417 // Fully instantiated font
1418 LyXFont font = getFont(bparams, i);
1420 LyXFont const last_font = running_font;
1422 // Spaces at end of font change are simulated to be
1423 // outside font change, i.e. we write "\textXX{text} "
1424 // rather than "\textXX{text }". (Asger)
1425 if (open_font && c == ' ' && i <= size() - 2) {
1426 LyXFont const & next_font = getFont(bparams, i + 1);
1427 if (next_font != running_font
1428 && next_font != font) {
1433 // We end font definition before blanks
1435 (font != running_font ||
1436 font.language() != running_font.language()))
1438 column += running_font.latexWriteEndChanges(os,
1440 (i == main_body-1) ? basefont : font);
1441 running_font = basefont;
1445 // Blanks are printed before start of fontswitch
1447 // Do not print the separation of the optional argument
1448 if (i != main_body - 1) {
1449 pimpl_->simpleTeXBlanks(os, texrow, i,
1450 column, font, *style);
1454 // Do we need to change font?
1455 if ((font != running_font ||
1456 font.language() != running_font.language()) &&
1459 column += font.latexWriteStartChanges(os, basefont,
1461 running_font = font;
1465 if (c == Paragraph::META_NEWLINE) {
1466 // newlines are handled differently here than
1467 // the default in SimpleTeXSpecialChars().
1468 if (!style->newline_allowed) {
1472 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1475 basefont = getLayoutFont(bparams);
1476 running_font = basefont;
1477 if (font.family() ==
1478 LyXFont::TYPEWRITER_FAMILY) {
1487 texrow.start(this, i + 1);
1490 pimpl_->simpleTeXSpecialChars(buf, bparams,
1491 os, texrow, moving_arg,
1493 basefont, open_font,
1494 *style, i, column, c);
1498 // If we have an open font definition, we have to close it
1500 #ifdef FIXED_LANGUAGE_END_DETECTION
1503 .latexWriteEndChanges(os, basefont,
1504 next_->getFont(bparams,
1507 running_font.latexWriteEndChanges(os, basefont,
1511 #ifdef WITH_WARNINGS
1512 //#warning For now we ALWAYS have to close the foreign font settings if they are
1513 //#warning there as we start another \selectlanguage with the next paragraph if
1514 //#warning we are in need of this. This should be fixed sometime (Jug)
1516 running_font.latexWriteEndChanges(os, basefont, basefont);
1520 // Needed if there is an optional argument but no contents.
1521 if (main_body > 0 && main_body == size()) {
1523 return_value = false;
1527 column += endTeXParParams(bparams, os, moving_arg);
1530 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1531 return return_value;
1535 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1536 BufferParams const & bparams,
1537 ostream & os, TexRow & texrow)
1539 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1541 LyXLayout_ptr const & style = layout();
1543 Language const * language = getParLanguage(bparams);
1544 Language const * doc_language = bparams.language;
1545 Language const * previous_language = previous_
1546 ? previous_->getParLanguage(bparams) : doc_language;
1547 if (language->babel() != previous_language->babel()) {
1549 if (!lyxrc.language_command_end.empty() &&
1550 previous_language->babel() != doc_language->babel()) {
1551 os << subst(lyxrc.language_command_end, "$$lang",
1552 previous_language->babel())
1557 if (lyxrc.language_command_end.empty() ||
1558 language->babel() != doc_language->babel()) {
1559 os << subst(lyxrc.language_command_begin, "$$lang",
1566 bool leftindent_open = false;
1567 if (!params().leftIndent().zero()) {
1568 os << "\\begin{LyXParagraphLeftIndent}{" <<
1569 params().leftIndent().asLatexString() << "}\n";
1571 leftindent_open = true;
1574 if (style->isEnvironment()) {
1575 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1576 os << "\\begin{" << style->latexname() << "}{"
1577 << params().labelWidthString() << "}\n";
1578 } else if (style->labeltype == LABEL_BIBLIO) {
1580 os << "\\begin{" << style->latexname() << "}{"
1581 << bibitemWidest(buf)
1583 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1584 os << "\\begin{" << style->latexname() << '}'
1585 << style->latexparam() << '\n';
1587 os << "\\begin{" << style->latexname() << '}'
1588 << style->latexparam() << '\n';
1591 Paragraph * par = this;
1593 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1595 if (par && par->params().depth() > params().depth()) {
1596 if (par->layout()->isParagraph()) {
1599 // How to handle this? (Lgb)
1600 //&& !suffixIs(os, "\n\n")
1602 // There should be at least one '\n' already
1603 // but we need there to be two for Standard
1604 // paragraphs that are depth-increment'ed to be
1605 // output correctly. However, tables can
1606 // also be paragraphs so don't adjust them.
1609 // Will it ever harm to have one '\n' too
1610 // many? i.e. that we sometimes will have
1611 // three in a row. (Lgb)
1615 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1618 && par->layout() == layout()
1619 && par->params().depth() == params().depth()
1620 && par->params().leftIndent() == params().leftIndent());
1622 if (style->isEnvironment()) {
1623 os << "\\end{" << style->latexname() << "}\n";
1627 if (leftindent_open) {
1628 os << "\\end{LyXParagraphLeftIndent}\n";
1632 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1633 return par; // ale970302
1637 bool Paragraph::isHfill(pos_type pos) const
1639 return IsHfillChar(getChar(pos));
1643 bool Paragraph::isInset(pos_type pos) const
1645 return IsInsetChar(getChar(pos));
1649 bool Paragraph::isNewline(pos_type pos) const
1651 return pos >= 0 && IsNewlineChar(getChar(pos));
1655 bool Paragraph::isSeparator(pos_type pos) const
1657 return IsSeparatorChar(getChar(pos));
1661 bool Paragraph::isLineSeparator(pos_type pos) const
1663 value_type const c = getChar(pos);
1664 return IsLineSeparatorChar(c)
1665 || (IsInsetChar(c) && getInset(pos) &&
1666 getInset(pos)->isLineSeparator());
1670 bool Paragraph::isKomma(pos_type pos) const
1672 return IsKommaChar(getChar(pos));
1676 /// Used by the spellchecker
1677 bool Paragraph::isLetter(pos_type pos) const
1679 value_type const c = getChar(pos);
1680 if (IsLetterChar(c))
1683 return getInset(pos)->isLetter();
1684 // We want to pass the ' and escape chars to ispell
1685 string const extra = lyxrc.isp_esc_chars + '\'';
1686 return contains(extra, c);
1690 bool Paragraph::isWord(pos_type pos) const
1692 return IsWordChar(getChar(pos)) ;
1697 Paragraph::getParLanguage(BufferParams const & bparams) const
1700 #ifndef INHERIT_LANGUAGE
1701 return getFirstFontSettings().language();
1703 Language const * lang = getFirstFontSettings().language();
1704 #ifdef WITH_WARNINGS
1705 #warning We should make this somewhat better, any ideas? (Jug)
1707 if (lang == inherit_language || lang == ignore_language)
1708 lang = bparams.language;
1711 } else if (previous_)
1712 return previous_->getParLanguage(bparams);
1714 return bparams.language;
1718 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1720 return lyxrc.rtl_support
1721 && getParLanguage(bparams)->RightToLeft()
1722 && !(inInset() && inInset()->owner() &&
1723 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1727 void Paragraph::changeLanguage(BufferParams const & bparams,
1728 Language const * from, Language const * to)
1730 for (pos_type i = 0; i < size(); ++i) {
1731 LyXFont font = getFontSettings(bparams, i);
1732 if (font.language() == from) {
1733 font.setLanguage(to);
1740 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1742 Language const * doc_language = bparams.language;
1743 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1744 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1746 for (; cit != end; ++cit)
1747 if (cit->font().language() != ignore_language &&
1748 cit->font().language() != latex_language &&
1749 #ifdef INHERIT_LANGUAGE
1750 cit->font().language() != inherit_language &&
1752 cit->font().language() != doc_language)
1758 // Convert the paragraph to a string.
1759 // Used for building the table of contents
1760 string const Paragraph::asString(Buffer const * buffer, bool label)
1762 BufferParams const & bparams = buffer->params;
1764 if (label && !params().labelString().empty())
1765 s += params().labelString() + ' ';
1766 string::size_type const len = s.size();
1768 for (pos_type i = 0; i < size(); ++i) {
1769 value_type c = getChar(i);
1772 else if (c == META_INSET &&
1773 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1775 getInset(i)->ascii(buffer, ost);
1776 s += subst(ost.str().c_str(),'\n',' ');
1780 if (isRightToLeftPar(bparams))
1781 reverse(s.begin() + len,s.end());
1787 string const Paragraph::asString(Buffer const * buffer,
1788 pos_type beg, pos_type end, bool label)
1792 if (beg == 0 && label && !params().labelString().empty())
1793 ost << params().labelString() << ' ';
1795 for (pos_type i = beg; i < end; ++i) {
1796 value_type const c = getUChar(buffer->params, i);
1799 else if (c == META_NEWLINE)
1801 else if (c == META_HFILL)
1803 else if (c == META_INSET) {
1804 getInset(i)->ascii(buffer, ost);
1808 return ost.str().c_str();
1812 void Paragraph::setInsetOwner(Inset * i)
1814 pimpl_->inset_owner = i;
1815 InsetList::iterator it = insetlist.begin();
1816 InsetList::iterator end = insetlist.end();
1817 for (; it != end; ++it) {
1819 it.getInset()->setOwner(i);
1824 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1827 insetlist.deleteInsetsLyXText(bv);
1831 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1834 insetlist.resizeInsetsLyXText(bv);
1838 void Paragraph::setContentsFromPar(Paragraph * par)
1840 pimpl_->setContentsFromPar(par);
1844 lyx::pos_type Paragraph::size() const
1846 return pimpl_->size();
1850 bool Paragraph::empty() const
1852 return pimpl_->empty();
1856 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1858 return pimpl_->getChar(pos);
1862 int Paragraph::id() const
1868 LyXLayout_ptr const & Paragraph::layout() const
1874 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1876 layout_ = new_layout;
1880 bool Paragraph::isFirstInSequence() const
1882 Paragraph const * dhook = depthHook(getDepth());
1883 return (dhook == this
1884 || dhook->layout() != layout()
1885 || dhook->getDepth() != getDepth());
1889 Inset * Paragraph::inInset() const
1891 return pimpl_->inset_owner;
1895 void Paragraph::clearContents()
1900 void Paragraph::setChar(pos_type pos, value_type c)
1902 pimpl_->setChar(pos, c);
1906 ParagraphParameters & Paragraph::params()
1908 return pimpl_->params;
1912 ParagraphParameters const & Paragraph::params() const
1914 return pimpl_->params;
1918 Paragraph * Paragraph::getParFromID(int id) const
1920 return pimpl_->getParFromID(id);
1924 bool Paragraph::isFreeSpacing() const
1926 // for now we just need this, later should we need this in some
1927 // other way we can always add a function to Inset::() too.
1928 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1929 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);