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::writeFile(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;
321 // now write the next paragraph
323 next_->writeFile(buf, os, bparams, dth);
327 void Paragraph::validate(LaTeXFeatures & features) const
329 pimpl_->validate(features, *layout());
333 // First few functions needed for cut and paste and paragraph breaking.
334 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
336 BufferParams bparams = buffer.params;
338 minibuffer_char = getChar(pos);
339 minibuffer_font = getFontSettings(bparams, pos);
340 minibuffer_inset = 0;
341 if (minibuffer_char == Paragraph::META_INSET) {
343 minibuffer_inset = getInset(pos)->clone(buffer);
345 minibuffer_inset = 0;
346 minibuffer_char = ' ';
347 // This reflects what GetInset() does (ARRae)
353 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
355 minibuffer_char = getChar(pos);
356 minibuffer_font = getFontSettings(bparams, pos);
357 minibuffer_inset = 0;
358 if (minibuffer_char == Paragraph::META_INSET) {
360 // the inset is not in a paragraph anymore
361 minibuffer_inset = insetlist.release(pos);
362 minibuffer_inset->parOwner(0);
364 minibuffer_inset = 0;
365 minibuffer_char = ' ';
366 // This reflects what GetInset() does (ARRae)
371 // Erase(pos); now the caller is responsible for that.
375 bool Paragraph::insertFromMinibuffer(pos_type pos)
377 if (minibuffer_char == Paragraph::META_INSET) {
378 if (!insetAllowed(minibuffer_inset->lyxCode())) {
381 insertInset(pos, minibuffer_inset, minibuffer_font);
383 LyXFont f = minibuffer_font;
384 if (!checkInsertChar(f)) {
387 insertChar(pos, minibuffer_char, f);
395 void Paragraph::erase(pos_type pos)
401 bool Paragraph::checkInsertChar(LyXFont & font)
403 if (pimpl_->inset_owner)
404 return pimpl_->inset_owner->checkInsertChar(font);
409 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
411 LyXFont const f(LyXFont::ALL_INHERIT);
412 insertChar(pos, c, f);
416 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
417 LyXFont const & font)
419 pimpl_->insertChar(pos, c, font);
423 void Paragraph::insertInset(pos_type pos, Inset * inset)
425 LyXFont const f(LyXFont::ALL_INHERIT);
426 insertInset(pos, inset, f);
430 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
432 pimpl_->insertInset(pos, inset, font);
436 bool Paragraph::insetAllowed(Inset::Code code)
438 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
440 if (pimpl_->inset_owner)
441 return pimpl_->inset_owner->insetAllowed(code);
446 Inset * Paragraph::getInset(pos_type pos)
448 lyx::Assert(pos < size());
450 return insetlist.get(pos);
454 Inset const * Paragraph::getInset(pos_type pos) const
456 lyx::Assert(pos < size());
458 return insetlist.get(pos);
462 // Gets uninstantiated font setting at position.
463 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
466 lyx::Assert(pos <= size());
468 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
469 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
470 for (; cit != end; ++cit) {
471 if (cit->pos() >= pos)
477 retfont = cit->font();
478 } else if (pos == size() && !empty()) {
479 retfont = getFontSettings(bparams, pos - 1);
481 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
482 #ifdef INHERIT_LANGUAGE
483 if (retfont.language() == inherit_language)
484 retfont.setLanguage(bparams.language);
491 // Gets uninstantiated font setting at position 0
492 LyXFont const Paragraph::getFirstFontSettings() const
494 if (!empty() && !pimpl_->fontlist.empty())
495 return pimpl_->fontlist[0].font();
497 return LyXFont(LyXFont::ALL_INHERIT);
501 // Gets the fully instantiated font at a given position in a paragraph
502 // This is basically the same function as LyXText::GetFont() in text2.C.
503 // The difference is that this one is used for generating the LaTeX file,
504 // and thus cosmetic "improvements" are disallowed: This has to deliver
505 // the true picture of the buffer. (Asger)
506 // If position is -1, we get the layout font of the paragraph.
507 // If position is -2, we get the font of the manual label of the paragraph.
508 LyXFont const Paragraph::getFont(BufferParams const & bparams,
511 lyx::Assert(pos >= 0);
513 LyXLayout_ptr const & lout = layout();
515 pos_type main_body = 0;
516 if (lout->labeltype == LABEL_MANUAL)
517 main_body = beginningOfMainBody();
521 layoutfont = lout->labelfont;
523 layoutfont = lout->font;
525 LyXFont tmpfont = getFontSettings(bparams, pos);
526 #ifndef INHERIT_LANGUAGE
527 tmpfont.realize(layoutfont);
529 tmpfont.realize(layoutfont, bparams.language);
532 return pimpl_->realizeFont(tmpfont, bparams);
536 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
538 LyXLayout_ptr const & lout = layout();
540 LyXFont tmpfont = lout->labelfont;
541 tmpfont.setLanguage(getParLanguage(bparams));
543 return pimpl_->realizeFont(tmpfont, bparams);
547 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
549 LyXLayout_ptr const & lout = layout();
551 LyXFont tmpfont = lout->font;
552 tmpfont.setLanguage(getParLanguage(bparams));
554 return pimpl_->realizeFont(tmpfont, bparams);
558 /// Returns the height of the highest font in range
560 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
561 LyXFont::FONT_SIZE const def_size) const
563 if (pimpl_->fontlist.empty())
566 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
567 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
568 for (; end_it != end; ++end_it) {
569 if (end_it->pos() >= endpos)
576 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
577 for (; cit != end; ++cit) {
578 if (cit->pos() >= startpos)
582 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
583 for (; cit != end_it; ++cit) {
584 LyXFont::FONT_SIZE size = cit->font().size();
585 if (size == LyXFont::INHERIT_SIZE)
587 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
594 Paragraph::value_type
595 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
597 value_type c = getChar(pos);
598 if (!lyxrc.rtl_support)
628 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
635 void Paragraph::setFont(pos_type pos, LyXFont const & font)
637 lyx::Assert(pos <= size());
639 // First, reduce font against layout/label font
640 // Update: The SetCharFont() routine in text2.C already
641 // reduces font, so we don't need to do that here. (Asger)
642 // No need to simplify this because it will disappear
643 // in a new kernel. (Asger)
644 // Next search font table
646 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
647 Pimpl::FontList::iterator it = beg;
648 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
649 for (; it != endit; ++it) {
650 if (it->pos() >= pos)
653 unsigned int i = std::distance(beg, it);
654 bool notfound = (it == endit);
656 if (!notfound && pimpl_->fontlist[i].font() == font)
659 bool begin = pos == 0 || notfound ||
660 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
661 // Is position pos is a beginning of a font block?
662 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
663 // Is position pos is the end of a font block?
664 if (begin && end) { // A single char block
665 if (i + 1 < pimpl_->fontlist.size() &&
666 pimpl_->fontlist[i + 1].font() == font) {
667 // Merge the singleton block with the next block
668 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
669 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
670 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
671 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
672 // Merge the singleton block with the previous block
673 pimpl_->fontlist[i - 1].pos(pos);
674 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
676 pimpl_->fontlist[i].font(font);
678 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
679 pimpl_->fontlist[i - 1].pos(pos);
681 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
682 Pimpl::FontTable(pos, font));
684 pimpl_->fontlist[i].pos(pos - 1);
685 if (!(i + 1 < pimpl_->fontlist.size() &&
686 pimpl_->fontlist[i + 1].font() == font))
687 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
688 Pimpl::FontTable(pos, font));
689 } else { // The general case. The block is splitted into 3 blocks
690 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
691 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
692 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
693 Pimpl::FontTable(pos, font));
699 void Paragraph::next(Paragraph * p)
705 // This function is able to hide closed footnotes.
706 Paragraph * Paragraph::next()
712 Paragraph const * Paragraph::next() const
718 void Paragraph::previous(Paragraph * p)
724 // This function is able to hide closed footnotes.
725 Paragraph * Paragraph::previous()
731 // This function is able to hide closed footnotes.
732 Paragraph const * Paragraph::previous() const
739 void Paragraph::breakParagraph(BufferParams const & bparams,
743 // create a new paragraph
744 Paragraph * tmp = new Paragraph(this);
745 tmp->layout(bparams.getLyXTextClass().defaultLayout());
746 // remember to set the inset_owner
747 tmp->setInsetOwner(inInset());
749 // this is an idea for a more userfriendly layout handling, I will
750 // see what the users say
752 // layout stays the same with latex-environments
754 tmp->layout(layout());
755 tmp->setLabelWidthString(params().labelWidthString());
758 bool isempty = (layout()->keepempty && empty());
760 if (!isempty && (size() > pos || empty() || flag == 2)) {
761 tmp->layout(layout());
762 tmp->params().align(params().align());
763 tmp->setLabelWidthString(params().labelWidthString());
765 tmp->params().lineBottom(params().lineBottom());
766 params().lineBottom(false);
767 tmp->params().pagebreakBottom(params().pagebreakBottom());
768 params().pagebreakBottom(false);
769 tmp->params().spaceBottom(params().spaceBottom());
770 params().spaceBottom(VSpace(VSpace::NONE));
772 tmp->params().depth(params().depth());
773 tmp->params().noindent(params().noindent());
775 // copy everything behind the break-position
776 // to the new paragraph
777 pos_type pos_end = size() - 1;
780 for (; i <= pos_end; ++i) {
781 cutIntoMinibuffer(bparams, i);
782 if (tmp->insertFromMinibuffer(j - pos))
785 for (i = pos_end; i >= pos; --i) {
790 // just an idea of me
791 if (!isempty && !pos) {
792 tmp->params().lineTop(params().lineTop());
793 tmp->params().pagebreakTop(params().pagebreakTop());
794 tmp->params().spaceTop(params().spaceTop());
795 tmp->bibkey = bibkey;
800 layout(bparams.getLyXTextClass().defaultLayout());
802 // layout stays the same with latex-environments
804 layout(tmp->layout());
805 setLabelWidthString(tmp->params().labelWidthString());
806 params().depth(tmp->params().depth());
812 void Paragraph::makeSameLayout(Paragraph const * par)
814 layout(par->layout());
816 params() = par->params();
820 int Paragraph::stripLeadingSpaces()
822 if (layout()->free_spacing ||
828 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
837 bool Paragraph::hasSameLayout(Paragraph const * par) const
840 par->layout() == layout() &&
841 params().sameLayout(par->params());
845 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
848 // create a new paragraph
849 Paragraph * tmp = new Paragraph(this);
850 tmp->makeSameLayout(this);
852 // When can pos > Last()?
853 // I guess pos == Last() is possible.
855 // copy everything behind the break-position to the new
857 pos_type pos_end = size() - 1;
861 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
862 cutIntoMinibuffer(bparams, i);
863 if (tmp->insertFromMinibuffer(j - pos))
867 for (pos_type k = pos_end; k >= pos; --k) {
874 // Be carefull, this does not make any check at all.
875 // This method has wrong name, it combined this par with the next par.
876 // In that sense it is the reverse of break paragraph. (Lgb)
877 void Paragraph::pasteParagraph(BufferParams const & bparams)
879 // copy the next paragraph to this one
880 Paragraph * the_next = next();
882 // first the DTP-stuff
883 params().lineBottom(the_next->params().lineBottom());
884 params().spaceBottom(the_next->params().spaceBottom());
885 params().pagebreakBottom(the_next->params().pagebreakBottom());
887 pos_type pos_end = the_next->pimpl_->size() - 1;
888 pos_type pos_insert = size();
890 // ok, now copy the paragraph
891 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
892 the_next->cutIntoMinibuffer(bparams, i);
893 if (insertFromMinibuffer(pos_insert + j))
897 // delete the next paragraph
898 Paragraph * ppar = the_next->previous_;
899 Paragraph * npar = the_next->next_;
905 int Paragraph::getEndLabel() const
907 Paragraph const * par = this;
908 depth_type par_depth = getDepth();
910 LyXLayout_ptr const & layout = par->layout();
911 int const endlabeltype = layout->endlabeltype;
913 if (endlabeltype != END_LABEL_NO_LABEL) {
917 depth_type const next_depth = next_->getDepth();
918 if (par_depth > next_depth ||
919 (par_depth == next_depth
920 && layout != next_->layout()))
926 par = par->outerHook();
928 par_depth = par->getDepth();
930 return END_LABEL_NO_LABEL;
934 Paragraph::depth_type Paragraph::getDepth() const
936 return params().depth();
940 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
942 bool const isenv = layout()->isEnvironment();
945 return params().depth() + 1;
947 return params().depth();
951 char Paragraph::getAlign() const
953 return params().align();
957 string const & Paragraph::getLabelstring() const
959 return params().labelString();
963 // the next two functions are for the manual labels
964 string const Paragraph::getLabelWidthString() const
966 if (!params().labelWidthString().empty())
967 return params().labelWidthString();
969 return _("Senseless with this layout!");
973 void Paragraph::setLabelWidthString(string const & s)
975 params().labelWidthString(s);
979 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
982 params().labelWidthString(string());
983 params().align(LYX_ALIGN_LAYOUT);
984 params().spaceTop(VSpace(VSpace::NONE));
985 params().spaceBottom(VSpace(VSpace::NONE));
986 params().spacing(Spacing(Spacing::Default));
990 // if the layout of a paragraph contains a manual label, the beginning of the
991 // main body is the beginning of the second word. This is what the par-
992 // function returns. If the layout does not contain a label, the main
993 // body always starts with position 0. This differentiation is necessary,
994 // because there cannot be a newline or a blank <= the beginning of the
997 int Paragraph::beginningOfMainBody() const
999 // Unroll the first two cycles of the loop
1000 // and remember the previous character to
1001 // remove unnecessary GetChar() calls
1003 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
1005 char previous_char = 0;
1008 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1009 // Yes, this ^ is supposed to be "= " not "=="
1012 && previous_char != ' '
1013 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1015 previous_char = temp;
1024 Paragraph * Paragraph::depthHook(depth_type depth)
1026 Paragraph * newpar = this;
1029 newpar = newpar->previous();
1030 } while (newpar && newpar->getDepth() > depth);
1033 if (previous() || getDepth())
1034 lyxerr << "ERROR (Paragraph::DepthHook): "
1043 Paragraph const * Paragraph::depthHook(depth_type depth) const
1045 Paragraph const * newpar = this;
1048 newpar = newpar->previous();
1049 } while (newpar && newpar->getDepth() > depth);
1052 if (previous() || getDepth())
1053 lyxerr << "ERROR (Paragraph::DepthHook): "
1062 Paragraph * Paragraph::outerHook()
1066 return depthHook(depth_type(getDepth() - 1));
1070 Paragraph const * Paragraph::outerHook() const
1074 return depthHook(depth_type(getDepth() - 1));
1078 // returns -1 if inset not found
1079 int Paragraph::getPositionOfInset(Inset const * inset) const
1082 InsetList::iterator it = insetlist.begin();
1083 InsetList::iterator end = insetlist.end();
1084 for (; it != end; ++it) {
1085 if (it.getInset() == inset) {
1089 if (inset == bibkey)
1096 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1097 BufferParams const & bparams,
1098 ostream & os, TexRow & texrow,
1101 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1102 Inset const * in = inInset();
1103 bool further_blank_line = false;
1104 LyXLayout_ptr style;
1106 // well we have to check if we are in an inset with unlimited
1107 // lenght (all in one row) if that is true then we don't allow
1108 // any special options in the paragraph and also we don't allow
1109 // any environment other then "Standard" to be valid!
1110 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1113 if (params().startOfAppendix()) {
1114 os << "\\appendix\n";
1118 if (!params().spacing().isDefault()
1119 && (!previous() || !previous()->hasSameLayout(this))) {
1120 os << params().spacing().writeEnvirBegin() << "\n";
1124 if (style->isCommand()) {
1129 if (params().pagebreakTop()) {
1131 further_blank_line = true;
1133 if (params().spaceTop().kind() != VSpace::NONE) {
1134 os << params().spaceTop().asLatexCommand(bparams);
1135 further_blank_line = true;
1138 if (params().lineTop()) {
1139 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1140 << "\\vspace{-1\\parskip}";
1141 further_blank_line = true;
1144 if (further_blank_line) {
1149 style = bparams.getLyXTextClass().defaultLayout();
1152 Language const * language = getParLanguage(bparams);
1153 Language const * doc_language = bparams.language;
1154 Language const * previous_language = previous()
1155 ? previous()->getParLanguage(bparams) : doc_language;
1157 if (language->babel() != previous_language->babel()
1158 // check if we already put language command in TeXEnvironment()
1159 && !(style->isEnvironment()
1160 && (!previous() || previous()->layout() != layout() ||
1161 previous()->params().depth() != params().depth())))
1163 if (!lyxrc.language_command_end.empty() &&
1164 previous_language->babel() != doc_language->babel())
1166 os << subst(lyxrc.language_command_end, "$$lang",
1167 previous_language->babel())
1172 if (lyxrc.language_command_end.empty() ||
1173 language->babel() != doc_language->babel())
1175 os << subst(lyxrc.language_command_begin, "$$lang",
1182 if (bparams.inputenc == "auto" &&
1183 language->encoding() != previous_language->encoding()) {
1184 os << "\\inputencoding{"
1185 << language->encoding()->LatexName()
1190 switch (style->latextype) {
1193 << style->latexname()
1194 << style->latexparam();
1196 case LATEX_ITEM_ENVIRONMENT:
1198 bibkey->latex(buf, os, false, false);
1202 case LATEX_LIST_ENVIRONMENT:
1209 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1211 // Make sure that \\par is done with the font of the last
1212 // character if this has another size as the default.
1213 // This is necessary because LaTeX (and LyX on the screen)
1214 // calculates the space between the baselines according
1215 // to this font. (Matthias)
1217 // Is this really needed ? (Dekel)
1218 // We do not need to use to change the font for the last paragraph
1219 // or for a command.
1220 LyXFont const font =
1222 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1224 bool is_command = style->isCommand();
1226 if (style->resfont.size() != font.size() && next_ && !is_command) {
1229 os << "\\" << font.latexSize() << " \\par}";
1230 } else if (need_par) {
1232 } else if (is_command)
1235 switch (style->latextype) {
1236 case LATEX_ITEM_ENVIRONMENT:
1237 case LATEX_LIST_ENVIRONMENT:
1238 if (next_ && (params().depth() < next_->params().depth())) {
1243 case LATEX_ENVIRONMENT:
1244 // if its the last paragraph of the current environment
1245 // skip it otherwise fall through
1247 && (next_->layout() != layout()
1248 || next_->params().depth() != params().depth()))
1250 // fall through possible
1252 // we don't need it for the last paragraph!!!
1259 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1260 further_blank_line = false;
1261 if (params().lineBottom()) {
1262 os << "\\lyxline{\\" << font.latexSize() << '}';
1263 further_blank_line = true;
1266 if (params().spaceBottom().kind() != VSpace::NONE) {
1267 os << params().spaceBottom().asLatexCommand(bparams);
1268 further_blank_line = true;
1271 if (params().pagebreakBottom()) {
1273 further_blank_line = true;
1276 if (further_blank_line) {
1281 if (!params().spacing().isDefault()
1282 && (!next_ || !next_->hasSameLayout(this))) {
1283 os << params().spacing().writeEnvirEnd() << "\n";
1288 // we don't need it for the last paragraph!!!
1293 // Since \selectlanguage write the language to the aux file,
1294 // we need to reset the language at the end of footnote or
1297 if (language->babel() != doc_language->babel()) {
1298 if (lyxrc.language_command_end.empty())
1299 os << subst(lyxrc.language_command_begin,
1301 doc_language->babel())
1304 os << subst(lyxrc.language_command_end,
1312 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1317 // This could go to ParagraphParameters if we want to
1318 int Paragraph::startTeXParParams(BufferParams const & bparams,
1319 ostream & os, bool moving_arg) const
1323 if (params().noindent()) {
1324 os << "\\noindent ";
1328 switch (params().align()) {
1329 case LYX_ALIGN_NONE:
1330 case LYX_ALIGN_BLOCK:
1331 case LYX_ALIGN_LAYOUT:
1332 case LYX_ALIGN_SPECIAL:
1334 case LYX_ALIGN_LEFT:
1335 case LYX_ALIGN_RIGHT:
1336 case LYX_ALIGN_CENTER:
1344 switch (params().align()) {
1345 case LYX_ALIGN_NONE:
1346 case LYX_ALIGN_BLOCK:
1347 case LYX_ALIGN_LAYOUT:
1348 case LYX_ALIGN_SPECIAL:
1350 case LYX_ALIGN_LEFT:
1351 if (getParLanguage(bparams)->babel() != "hebrew") {
1352 os << "\\begin{flushleft}";
1355 os << "\\begin{flushright}";
1359 case LYX_ALIGN_RIGHT:
1360 if (getParLanguage(bparams)->babel() != "hebrew") {
1361 os << "\\begin{flushright}";
1364 os << "\\begin{flushleft}";
1368 case LYX_ALIGN_CENTER:
1369 os << "\\begin{center}";
1378 // This could go to ParagraphParameters if we want to
1379 int Paragraph::endTeXParParams(BufferParams const & bparams,
1380 ostream & os, bool moving_arg) const
1384 switch (params().align()) {
1385 case LYX_ALIGN_NONE:
1386 case LYX_ALIGN_BLOCK:
1387 case LYX_ALIGN_LAYOUT:
1388 case LYX_ALIGN_SPECIAL:
1390 case LYX_ALIGN_LEFT:
1391 case LYX_ALIGN_RIGHT:
1392 case LYX_ALIGN_CENTER:
1400 switch (params().align()) {
1401 case LYX_ALIGN_NONE:
1402 case LYX_ALIGN_BLOCK:
1403 case LYX_ALIGN_LAYOUT:
1404 case LYX_ALIGN_SPECIAL:
1406 case LYX_ALIGN_LEFT:
1407 if (getParLanguage(bparams)->babel() != "hebrew") {
1408 os << "\\end{flushleft}";
1411 os << "\\end{flushright}";
1415 case LYX_ALIGN_RIGHT:
1416 if (getParLanguage(bparams)->babel() != "hebrew") {
1417 os << "\\end{flushright}";
1420 os << "\\end{flushleft}";
1424 case LYX_ALIGN_CENTER:
1425 os << "\\end{center}";
1433 // This one spits out the text of the paragraph
1434 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1435 BufferParams const & bparams,
1436 ostream & os, TexRow & texrow,
1439 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1441 bool return_value = false;
1443 LyXLayout_ptr style;
1445 // well we have to check if we are in an inset with unlimited
1446 // lenght (all in one row) if that is true then we don't allow
1447 // any special options in the paragraph and also we don't allow
1448 // any environment other then "Standard" to be valid!
1450 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1453 style = bparams.getLyXTextClass().defaultLayout();
1460 // Maybe we have to create a optional argument.
1462 if (style->labeltype != LABEL_MANUAL)
1465 main_body = beginningOfMainBody();
1469 if (main_body > 0) {
1472 basefont = getLabelFont(bparams);
1474 basefont = getLayoutFont(bparams);
1477 moving_arg |= style->needprotect;
1479 // Which font is currently active?
1480 LyXFont running_font(basefont);
1481 // Do we have an open font change?
1482 bool open_font = false;
1484 texrow.start(this, 0);
1486 // if the paragraph is empty, the loop will not be entered at all
1488 if (style->isCommand()) {
1493 column += startTeXParParams(bparams, os, moving_arg);
1497 for (pos_type i = 0; i < size(); ++i) {
1499 // First char in paragraph or after label?
1500 if (i == main_body) {
1501 if (main_body > 0) {
1503 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1506 basefont = getLayoutFont(bparams);
1507 running_font = basefont;
1511 if (style->isCommand()) {
1517 column += startTeXParParams(bparams, os,
1521 value_type c = getChar(i);
1523 // Fully instantiated font
1524 LyXFont font = getFont(bparams, i);
1526 LyXFont const last_font = running_font;
1528 // Spaces at end of font change are simulated to be
1529 // outside font change, i.e. we write "\textXX{text} "
1530 // rather than "\textXX{text }". (Asger)
1531 if (open_font && c == ' ' && i <= size() - 2) {
1532 LyXFont const & next_font = getFont(bparams, i + 1);
1533 if (next_font != running_font
1534 && next_font != font) {
1539 // We end font definition before blanks
1541 (font != running_font ||
1542 font.language() != running_font.language()))
1544 column += running_font.latexWriteEndChanges(os,
1546 (i == main_body-1) ? basefont : font);
1547 running_font = basefont;
1551 // Blanks are printed before start of fontswitch
1553 // Do not print the separation of the optional argument
1554 if (i != main_body - 1) {
1555 pimpl_->simpleTeXBlanks(os, texrow, i,
1556 column, font, *style);
1560 // Do we need to change font?
1561 if ((font != running_font ||
1562 font.language() != running_font.language()) &&
1565 column += font.latexWriteStartChanges(os, basefont,
1567 running_font = font;
1571 if (c == Paragraph::META_NEWLINE) {
1572 // newlines are handled differently here than
1573 // the default in SimpleTeXSpecialChars().
1574 if (!style->newline_allowed) {
1578 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1581 basefont = getLayoutFont(bparams);
1582 running_font = basefont;
1583 if (font.family() ==
1584 LyXFont::TYPEWRITER_FAMILY) {
1593 texrow.start(this, i + 1);
1596 pimpl_->simpleTeXSpecialChars(buf, bparams,
1597 os, texrow, moving_arg,
1599 basefont, open_font,
1600 *style, i, column, c);
1604 // If we have an open font definition, we have to close it
1606 #ifdef FIXED_LANGUAGE_END_DETECTION
1609 .latexWriteEndChanges(os, basefont,
1610 next_->getFont(bparams,
1613 running_font.latexWriteEndChanges(os, basefont,
1617 #ifdef WITH_WARNINGS
1618 //#warning For now we ALWAYS have to close the foreign font settings if they are
1619 //#warning there as we start another \selectlanguage with the next paragraph if
1620 //#warning we are in need of this. This should be fixed sometime (Jug)
1622 running_font.latexWriteEndChanges(os, basefont, basefont);
1626 // Needed if there is an optional argument but no contents.
1627 if (main_body > 0 && main_body == size()) {
1629 return_value = false;
1633 column += endTeXParParams(bparams, os, moving_arg);
1636 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1637 return return_value;
1641 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1642 BufferParams const & bparams,
1643 ostream & os, TexRow & texrow)
1645 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1647 LyXLayout_ptr const & style = layout();
1649 Language const * language = getParLanguage(bparams);
1650 Language const * doc_language = bparams.language;
1651 Language const * previous_language = previous_
1652 ? previous_->getParLanguage(bparams) : doc_language;
1653 if (language->babel() != previous_language->babel()) {
1655 if (!lyxrc.language_command_end.empty() &&
1656 previous_language->babel() != doc_language->babel()) {
1657 os << subst(lyxrc.language_command_end, "$$lang",
1658 previous_language->babel())
1663 if (lyxrc.language_command_end.empty() ||
1664 language->babel() != doc_language->babel()) {
1665 os << subst(lyxrc.language_command_begin, "$$lang",
1672 bool leftindent_open = false;
1673 if (!params().leftIndent().zero()) {
1674 os << "\\begin{LyXParagraphLeftIndent}{" <<
1675 params().leftIndent().asLatexString() << "}\n";
1677 leftindent_open = true;
1680 if (style->isEnvironment()) {
1681 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1682 os << "\\begin{" << style->latexname() << "}{"
1683 << params().labelWidthString() << "}\n";
1684 } else if (style->labeltype == LABEL_BIBLIO) {
1686 os << "\\begin{" << style->latexname() << "}{"
1687 << bibitemWidest(buf)
1689 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1690 os << "\\begin{" << style->latexname() << '}'
1691 << style->latexparam() << '\n';
1693 os << "\\begin{" << style->latexname() << '}'
1694 << style->latexparam() << '\n';
1697 Paragraph * par = this;
1699 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1701 if (par && par->params().depth() > params().depth()) {
1702 if (par->layout()->isParagraph()) {
1705 // How to handle this? (Lgb)
1706 //&& !suffixIs(os, "\n\n")
1708 // There should be at least one '\n' already
1709 // but we need there to be two for Standard
1710 // paragraphs that are depth-increment'ed to be
1711 // output correctly. However, tables can
1712 // also be paragraphs so don't adjust them.
1715 // Will it ever harm to have one '\n' too
1716 // many? i.e. that we sometimes will have
1717 // three in a row. (Lgb)
1721 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1724 && par->layout() == layout()
1725 && par->params().depth() == params().depth()
1726 && par->params().leftIndent() == params().leftIndent());
1728 if (style->isEnvironment()) {
1729 os << "\\end{" << style->latexname() << "}\n";
1733 if (leftindent_open) {
1734 os << "\\end{LyXParagraphLeftIndent}\n";
1738 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1739 return par; // ale970302
1743 bool Paragraph::isHfill(pos_type pos) const
1745 return IsHfillChar(getChar(pos));
1749 bool Paragraph::isInset(pos_type pos) const
1751 return IsInsetChar(getChar(pos));
1755 bool Paragraph::isNewline(pos_type pos) const
1757 return pos >= 0 && IsNewlineChar(getChar(pos));
1761 bool Paragraph::isSeparator(pos_type pos) const
1763 return IsSeparatorChar(getChar(pos));
1767 bool Paragraph::isLineSeparator(pos_type pos) const
1769 value_type const c = getChar(pos);
1770 return IsLineSeparatorChar(c)
1771 || (IsInsetChar(c) && getInset(pos) &&
1772 getInset(pos)->isLineSeparator());
1776 bool Paragraph::isKomma(pos_type pos) const
1778 return IsKommaChar(getChar(pos));
1782 /// Used by the spellchecker
1783 bool Paragraph::isLetter(pos_type pos) const
1785 value_type const c = getChar(pos);
1786 if (IsLetterChar(c))
1789 return getInset(pos)->isLetter();
1790 // We want to pass the ' and escape chars to ispell
1791 string const extra = lyxrc.isp_esc_chars + '\'';
1792 return contains(extra, c);
1796 bool Paragraph::isWord(pos_type pos) const
1798 return IsWordChar(getChar(pos)) ;
1803 Paragraph::getParLanguage(BufferParams const & bparams) const
1806 #ifndef INHERIT_LANGUAGE
1807 return getFirstFontSettings().language();
1809 Language const * lang = getFirstFontSettings().language();
1810 #ifdef WITH_WARNINGS
1811 #warning We should make this somewhat better, any ideas? (Jug)
1813 if (lang == inherit_language || lang == ignore_language)
1814 lang = bparams.language;
1817 } else if (previous_)
1818 return previous_->getParLanguage(bparams);
1820 return bparams.language;
1824 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1826 return lyxrc.rtl_support
1827 && getParLanguage(bparams)->RightToLeft()
1828 && !(inInset() && inInset()->owner() &&
1829 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1833 void Paragraph::changeLanguage(BufferParams const & bparams,
1834 Language const * from, Language const * to)
1836 for (pos_type i = 0; i < size(); ++i) {
1837 LyXFont font = getFontSettings(bparams, i);
1838 if (font.language() == from) {
1839 font.setLanguage(to);
1846 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1848 Language const * doc_language = bparams.language;
1849 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1850 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1852 for (; cit != end; ++cit)
1853 if (cit->font().language() != ignore_language &&
1854 cit->font().language() != latex_language &&
1855 #ifdef INHERIT_LANGUAGE
1856 cit->font().language() != inherit_language &&
1858 cit->font().language() != doc_language)
1864 // Convert the paragraph to a string.
1865 // Used for building the table of contents
1866 string const Paragraph::asString(Buffer const * buffer, bool label)
1868 BufferParams const & bparams = buffer->params;
1870 if (label && !params().labelString().empty())
1871 s += params().labelString() + ' ';
1872 string::size_type const len = s.size();
1874 for (pos_type i = 0; i < size(); ++i) {
1875 value_type c = getChar(i);
1878 else if (c == META_INSET &&
1879 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1881 getInset(i)->ascii(buffer, ost);
1882 s += subst(ost.str().c_str(),'\n',' ');
1886 if (isRightToLeftPar(bparams))
1887 reverse(s.begin() + len,s.end());
1893 string const Paragraph::asString(Buffer const * buffer,
1894 pos_type beg, pos_type end, bool label)
1898 if (beg == 0 && label && !params().labelString().empty())
1899 ost << params().labelString() << ' ';
1901 for (pos_type i = beg; i < end; ++i) {
1902 value_type const c = getUChar(buffer->params, i);
1905 else if (c == META_NEWLINE)
1907 else if (c == META_HFILL)
1909 else if (c == META_INSET) {
1910 getInset(i)->ascii(buffer, ost);
1914 return ost.str().c_str();
1918 void Paragraph::setInsetOwner(Inset * i)
1920 pimpl_->inset_owner = i;
1921 InsetList::iterator it = insetlist.begin();
1922 InsetList::iterator end = insetlist.end();
1923 for (; it != end; ++it) {
1925 it.getInset()->setOwner(i);
1930 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1933 insetlist.deleteInsetsLyXText(bv);
1937 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1940 insetlist.resizeInsetsLyXText(bv);
1944 void Paragraph::setContentsFromPar(Paragraph * par)
1946 pimpl_->setContentsFromPar(par);
1950 lyx::pos_type Paragraph::size() const
1952 return pimpl_->size();
1956 bool Paragraph::empty() const
1958 return pimpl_->empty();
1962 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1964 return pimpl_->getChar(pos);
1968 int Paragraph::id() const
1974 LyXLayout_ptr const & Paragraph::layout() const
1980 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1982 layout_ = new_layout;
1986 bool Paragraph::isFirstInSequence() const
1988 Paragraph const * dhook = depthHook(getDepth());
1989 return (dhook == this
1990 || dhook->layout() != layout()
1991 || dhook->getDepth() != getDepth());
1995 Inset * Paragraph::inInset() const
1997 return pimpl_->inset_owner;
2001 void Paragraph::clearContents()
2006 void Paragraph::setChar(pos_type pos, value_type c)
2008 pimpl_->setChar(pos, c);
2012 ParagraphParameters & Paragraph::params()
2014 return pimpl_->params;
2018 ParagraphParameters const & Paragraph::params() const
2020 return pimpl_->params;
2024 Paragraph * Paragraph::getParFromID(int id) const
2026 return pimpl_->getParFromID(id);
2030 bool Paragraph::isFreeSpacing() const
2032 // for now we just need this, later should we need this in some
2033 // other way we can always add a function to Inset::() too.
2034 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2035 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);