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))
83 bibkey = 0; // ale970302
88 // This constructor inserts the new paragraph in a list.
89 Paragraph::Paragraph(Paragraph * par)
90 : pimpl_(new Paragraph::Pimpl(this))
95 // double linked list begin
98 next_->previous_ = this;
100 previous_->next_ = this;
103 bibkey = 0; // ale970302
108 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
109 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
116 // this is because of the dummy layout of the paragraphs that
118 layout_ = lp.layout();
122 bibkey = static_cast<InsetBibKey *>
123 (lp.bibkey->clone(*current_view->buffer()));
128 // copy everything behind the break-position to the new paragraph
129 insetlist = lp.insetlist;
130 InsetList::iterator it = insetlist.begin();
131 InsetList::iterator end = insetlist.end();
132 for (; it != end; ++it) {
133 it.setInset(it.getInset()->clone(*current_view->buffer(),
135 // tell the new inset who is the boss now
136 it.getInset()->parOwner(this);
141 // the destructor removes the new paragraph from the list
142 Paragraph::~Paragraph()
145 previous_->next_ = next_;
147 next_->previous_ = previous_;
154 //lyxerr << "Paragraph::paragraph_id = "
155 // << Paragraph::paragraph_id << endl;
159 void Paragraph::writeFile(Buffer const * buf, ostream & os,
160 BufferParams const & bparams,
161 depth_type dth) const
163 // The beginning or end of a deeper (i.e. nested) area?
164 if (dth != params().depth()) {
165 if (params().depth() > dth) {
166 while (params().depth() > dth) {
167 os << "\n\\begin_deeper ";
171 while (params().depth() < dth) {
172 os << "\n\\end_deeper ";
178 // First write the layout
179 os << "\n\\layout " << layout()->name() << "\n";
181 // Maybe some vertical spaces.
182 if (params().spaceTop().kind() != VSpace::NONE)
183 os << "\\added_space_top "
184 << params().spaceTop().asLyXCommand() << " ";
185 if (params().spaceBottom().kind() != VSpace::NONE)
186 os << "\\added_space_bottom "
187 << params().spaceBottom().asLyXCommand() << " ";
189 // Maybe the paragraph has special spacing
190 params().spacing().writeFile(os, true);
192 // The labelwidth string used in lists.
193 if (!params().labelWidthString().empty())
194 os << "\\labelwidthstring "
195 << params().labelWidthString() << '\n';
197 // Lines above or below?
198 if (params().lineTop())
200 if (params().lineBottom())
201 os << "\\line_bottom ";
203 // Pagebreaks above or below?
204 if (params().pagebreakTop())
205 os << "\\pagebreak_top ";
206 if (params().pagebreakBottom())
207 os << "\\pagebreak_bottom ";
209 // Start of appendix?
210 if (params().startOfAppendix())
211 os << "\\start_of_appendix ";
214 if (params().noindent())
217 // Do we have a manual left indent?
218 if (!params().leftIndent().zero())
219 os << "\\leftindent " << params().leftIndent().asString() << " ";
222 if (params().align() != LYX_ALIGN_LAYOUT) {
224 switch (params().align()) {
225 case LYX_ALIGN_LEFT: h = 1; break;
226 case LYX_ALIGN_RIGHT: h = 2; break;
227 case LYX_ALIGN_CENTER: h = 3; break;
228 default: h = 0; break;
230 os << "\\align " << string_align[h] << " ";
235 bibkey->write(buf, os);
237 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
240 for (pos_type i = 0; i < size(); ++i) {
246 // Write font changes
247 LyXFont font2 = getFontSettings(bparams, i);
248 if (font2 != font1) {
249 #ifndef INHERIT_LANGUAGE
250 font2.lyxWriteChanges(font1, os);
252 font2.lyxWriteChanges(font1, bparams.language, os);
258 value_type const c = getChar(i);
262 Inset const * inset = getInset(i);
264 if (inset->directWrite()) {
265 // international char, let it write
266 // code directly so it's shorter in
268 inset->write(buf, os);
270 os << "\n\\begin_inset ";
271 inset->write(buf, os);
272 os << "\n\\end_inset \n\n";
278 os << "\n\\newline \n";
282 os << "\n\\hfill \n";
286 os << "\n\\backslash \n";
290 if (i + 1 < size() && getChar(i + 1) == ' ') {
297 if ((column > 70 && c == ' ')
302 // this check is to amend a bug. LyX sometimes
303 // inserts '\0' this could cause problems.
307 lyxerr << "ERROR (Paragraph::writeFile):"
308 " NULL char in structure." << endl;
314 // now write the next paragraph
316 next_->writeFile(buf, os, bparams, dth);
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));
475 #ifdef INHERIT_LANGUAGE
476 if (retfont.language() == inherit_language)
477 retfont.setLanguage(bparams.language);
484 // Gets uninstantiated font setting at position 0
485 LyXFont const Paragraph::getFirstFontSettings() const
487 if (!empty() && !pimpl_->fontlist.empty())
488 return pimpl_->fontlist[0].font();
490 return LyXFont(LyXFont::ALL_INHERIT);
494 // Gets the fully instantiated font at a given position in a paragraph
495 // This is basically the same function as LyXText::GetFont() in text2.C.
496 // The difference is that this one is used for generating the LaTeX file,
497 // and thus cosmetic "improvements" are disallowed: This has to deliver
498 // the true picture of the buffer. (Asger)
499 // If position is -1, we get the layout font of the paragraph.
500 // If position is -2, we get the font of the manual label of the paragraph.
501 LyXFont const Paragraph::getFont(BufferParams const & bparams,
504 lyx::Assert(pos >= 0);
506 LyXLayout_ptr const & lout = layout();
508 pos_type main_body = 0;
509 if (lout->labeltype == LABEL_MANUAL)
510 main_body = beginningOfMainBody();
514 layoutfont = lout->labelfont;
516 layoutfont = lout->font;
518 LyXFont tmpfont = getFontSettings(bparams, pos);
519 #ifndef INHERIT_LANGUAGE
520 tmpfont.realize(layoutfont);
522 tmpfont.realize(layoutfont, bparams.language);
525 return pimpl_->realizeFont(tmpfont, bparams);
529 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
531 LyXLayout_ptr const & lout = layout();
533 LyXFont tmpfont = lout->labelfont;
534 tmpfont.setLanguage(getParLanguage(bparams));
536 return pimpl_->realizeFont(tmpfont, bparams);
540 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
542 LyXLayout_ptr const & lout = layout();
544 LyXFont tmpfont = lout->font;
545 tmpfont.setLanguage(getParLanguage(bparams));
547 return pimpl_->realizeFont(tmpfont, bparams);
551 /// Returns the height of the highest font in range
553 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
554 LyXFont::FONT_SIZE const def_size) const
556 if (pimpl_->fontlist.empty())
559 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
560 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
561 for (; end_it != end; ++end_it) {
562 if (end_it->pos() >= endpos)
569 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
570 for (; cit != end; ++cit) {
571 if (cit->pos() >= startpos)
575 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
576 for (; cit != end_it; ++cit) {
577 LyXFont::FONT_SIZE size = cit->font().size();
578 if (size == LyXFont::INHERIT_SIZE)
580 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
587 Paragraph::value_type
588 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
590 value_type c = getChar(pos);
591 if (!lyxrc.rtl_support)
621 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
628 void Paragraph::setFont(pos_type pos, LyXFont const & font)
630 lyx::Assert(pos <= size());
632 // First, reduce font against layout/label font
633 // Update: The SetCharFont() routine in text2.C already
634 // reduces font, so we don't need to do that here. (Asger)
635 // No need to simplify this because it will disappear
636 // in a new kernel. (Asger)
637 // Next search font table
639 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
640 Pimpl::FontList::iterator it = beg;
641 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
642 for (; it != endit; ++it) {
643 if (it->pos() >= pos)
646 unsigned int i = std::distance(beg, it);
647 bool notfound = (it == endit);
649 if (!notfound && pimpl_->fontlist[i].font() == font)
652 bool begin = pos == 0 || notfound ||
653 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
654 // Is position pos is a beginning of a font block?
655 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
656 // Is position pos is the end of a font block?
657 if (begin && end) { // A single char block
658 if (i + 1 < pimpl_->fontlist.size() &&
659 pimpl_->fontlist[i + 1].font() == font) {
660 // Merge the singleton block with the next block
661 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
662 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
663 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
664 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
665 // Merge the singleton block with the previous block
666 pimpl_->fontlist[i - 1].pos(pos);
667 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
669 pimpl_->fontlist[i].font(font);
671 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
672 pimpl_->fontlist[i - 1].pos(pos);
674 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
675 Pimpl::FontTable(pos, font));
677 pimpl_->fontlist[i].pos(pos - 1);
678 if (!(i + 1 < pimpl_->fontlist.size() &&
679 pimpl_->fontlist[i + 1].font() == font))
680 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
681 Pimpl::FontTable(pos, font));
682 } else { // The general case. The block is splitted into 3 blocks
683 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
684 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
685 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
686 Pimpl::FontTable(pos, font));
692 void Paragraph::next(Paragraph * p)
698 // This function is able to hide closed footnotes.
699 Paragraph * Paragraph::next()
705 Paragraph const * Paragraph::next() const
711 void Paragraph::previous(Paragraph * p)
717 // This function is able to hide closed footnotes.
718 Paragraph * Paragraph::previous()
724 // This function is able to hide closed footnotes.
725 Paragraph const * Paragraph::previous() const
731 void Paragraph::breakParagraph(BufferParams const & bparams,
735 // create a new paragraph
736 Paragraph * tmp = new Paragraph(this);
737 tmp->layout(bparams.getLyXTextClass().defaultLayout());
738 // remember to set the inset_owner
739 tmp->setInsetOwner(inInset());
741 // this is an idea for a more userfriendly layout handling, I will
742 // see what the users say
744 // layout stays the same with latex-environments
746 tmp->layout(layout());
747 tmp->setLabelWidthString(params().labelWidthString());
750 bool isempty = (layout()->keepempty && empty());
752 if (!isempty && (size() > pos || empty() || flag == 2)) {
753 tmp->layout(layout());
754 tmp->params().align(params().align());
755 tmp->setLabelWidthString(params().labelWidthString());
757 tmp->params().lineBottom(params().lineBottom());
758 params().lineBottom(false);
759 tmp->params().pagebreakBottom(params().pagebreakBottom());
760 params().pagebreakBottom(false);
761 tmp->params().spaceBottom(params().spaceBottom());
762 params().spaceBottom(VSpace(VSpace::NONE));
764 tmp->params().depth(params().depth());
765 tmp->params().noindent(params().noindent());
767 // copy everything behind the break-position
768 // to the new paragraph
769 pos_type pos_end = size() - 1;
772 for (; i <= pos_end; ++i) {
773 cutIntoMinibuffer(bparams, i);
774 if (tmp->insertFromMinibuffer(j - pos))
777 for (i = pos_end; i >= pos; --i) {
782 // just an idea of me
783 if (!isempty && !pos) {
784 tmp->params().lineTop(params().lineTop());
785 tmp->params().pagebreakTop(params().pagebreakTop());
786 tmp->params().spaceTop(params().spaceTop());
787 tmp->bibkey = bibkey;
792 layout(bparams.getLyXTextClass().defaultLayout());
794 // layout stays the same with latex-environments
796 layout(tmp->layout());
797 setLabelWidthString(tmp->params().labelWidthString());
798 params().depth(tmp->params().depth());
804 void Paragraph::makeSameLayout(Paragraph const * par)
806 layout(par->layout());
808 params() = par->params();
812 int Paragraph::stripLeadingSpaces()
814 if (layout()->free_spacing ||
820 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
829 bool Paragraph::hasSameLayout(Paragraph const * par) const
832 par->layout() == layout() &&
833 params().sameLayout(par->params());
837 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
840 // create a new paragraph
841 Paragraph * tmp = new Paragraph(this);
842 tmp->makeSameLayout(this);
844 // When can pos > Last()?
845 // I guess pos == Last() is possible.
847 // copy everything behind the break-position to the new
849 pos_type pos_end = size() - 1;
853 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
854 cutIntoMinibuffer(bparams, i);
855 if (tmp->insertFromMinibuffer(j - pos))
859 for (pos_type k = pos_end; k >= pos; --k) {
866 // Be carefull, this does not make any check at all.
867 // This method has wrong name, it combined this par with the next par.
868 // In that sense it is the reverse of break paragraph. (Lgb)
869 void Paragraph::pasteParagraph(BufferParams const & bparams)
871 // copy the next paragraph to this one
872 Paragraph * the_next = next();
874 // first the DTP-stuff
875 params().lineBottom(the_next->params().lineBottom());
876 params().spaceBottom(the_next->params().spaceBottom());
877 params().pagebreakBottom(the_next->params().pagebreakBottom());
879 pos_type pos_end = the_next->pimpl_->size() - 1;
880 pos_type pos_insert = size();
882 // ok, now copy the paragraph
883 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
884 the_next->cutIntoMinibuffer(bparams, i);
885 if (insertFromMinibuffer(pos_insert + j))
889 // delete the next paragraph
890 Paragraph * ppar = the_next->previous_;
891 Paragraph * npar = the_next->next_;
897 int Paragraph::getEndLabel() const
899 Paragraph const * par = this;
900 depth_type par_depth = getDepth();
902 LyXLayout_ptr const & layout = par->layout();
903 int const endlabeltype = layout->endlabeltype;
905 if (endlabeltype != END_LABEL_NO_LABEL) {
909 depth_type const next_depth = next_->getDepth();
910 if (par_depth > next_depth ||
911 (par_depth == next_depth
912 && layout != next_->layout()))
918 par = par->outerHook();
920 par_depth = par->getDepth();
922 return END_LABEL_NO_LABEL;
926 Paragraph::depth_type Paragraph::getDepth() const
928 return params().depth();
932 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
934 bool const isenv = layout()->isEnvironment();
937 return params().depth() + 1;
939 return params().depth();
943 char Paragraph::getAlign() const
945 return params().align();
949 string const & Paragraph::getLabelstring() const
951 return params().labelString();
955 // the next two functions are for the manual labels
956 string const Paragraph::getLabelWidthString() const
958 if (!params().labelWidthString().empty())
959 return params().labelWidthString();
961 return _("Senseless with this layout!");
965 void Paragraph::setLabelWidthString(string const & s)
967 params().labelWidthString(s);
971 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
974 params().labelWidthString(string());
975 params().align(LYX_ALIGN_LAYOUT);
976 params().spaceTop(VSpace(VSpace::NONE));
977 params().spaceBottom(VSpace(VSpace::NONE));
978 params().spacing(Spacing(Spacing::Default));
982 // if the layout of a paragraph contains a manual label, the beginning of the
983 // main body is the beginning of the second word. This is what the par-
984 // function returns. If the layout does not contain a label, the main
985 // body always starts with position 0. This differentiation is necessary,
986 // because there cannot be a newline or a blank <= the beginning of the
989 int Paragraph::beginningOfMainBody() const
991 // Unroll the first two cycles of the loop
992 // and remember the previous character to
993 // remove unnecessary GetChar() calls
995 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
997 char previous_char = 0;
1000 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1001 // Yes, this ^ is supposed to be "= " not "=="
1004 && previous_char != ' '
1005 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1007 previous_char = temp;
1016 Paragraph * Paragraph::depthHook(depth_type depth)
1018 Paragraph * newpar = this;
1021 newpar = newpar->previous();
1022 } while (newpar && newpar->getDepth() > depth);
1025 if (previous() || getDepth())
1026 lyxerr << "ERROR (Paragraph::DepthHook): "
1035 Paragraph const * Paragraph::depthHook(depth_type depth) const
1037 Paragraph const * newpar = this;
1040 newpar = newpar->previous();
1041 } while (newpar && newpar->getDepth() > depth);
1044 if (previous() || getDepth())
1045 lyxerr << "ERROR (Paragraph::DepthHook): "
1053 Paragraph * Paragraph::outerHook()
1057 return depthHook(depth_type(getDepth() - 1));
1060 Paragraph const * Paragraph::outerHook() const
1064 return depthHook(depth_type(getDepth() - 1));
1068 // returns -1 if inset not found
1069 int Paragraph::getPositionOfInset(Inset const * inset) const
1072 InsetList::iterator it = insetlist.begin();
1073 InsetList::iterator end = insetlist.end();
1074 for (; it != end; ++it) {
1075 if (it.getInset() == inset) {
1079 if (inset == bibkey)
1086 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1087 BufferParams const & bparams,
1088 ostream & os, TexRow & texrow,
1091 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1092 Inset const * in = inInset();
1093 bool further_blank_line = false;
1094 LyXLayout_ptr style;
1096 // well we have to check if we are in an inset with unlimited
1097 // lenght (all in one row) if that is true then we don't allow
1098 // any special options in the paragraph and also we don't allow
1099 // any environment other then "Standard" to be valid!
1100 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1103 if (params().startOfAppendix()) {
1104 os << "\\appendix\n";
1108 if (!params().spacing().isDefault()
1109 && (!previous() || !previous()->hasSameLayout(this))) {
1110 os << params().spacing().writeEnvirBegin() << "\n";
1114 if (style->isCommand()) {
1119 if (params().pagebreakTop()) {
1121 further_blank_line = true;
1123 if (params().spaceTop().kind() != VSpace::NONE) {
1124 os << params().spaceTop().asLatexCommand(bparams);
1125 further_blank_line = true;
1128 if (params().lineTop()) {
1129 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1130 << "\\vspace{-1\\parskip}";
1131 further_blank_line = true;
1134 if (further_blank_line) {
1139 style = bparams.getLyXTextClass().defaultLayout();
1142 Language const * language = getParLanguage(bparams);
1143 Language const * doc_language = bparams.language;
1144 Language const * previous_language = previous()
1145 ? previous()->getParLanguage(bparams) : doc_language;
1147 if (language->babel() != previous_language->babel()
1148 // check if we already put language command in TeXEnvironment()
1149 && !(style->isEnvironment()
1150 && (!previous() || previous()->layout() != layout() ||
1151 previous()->params().depth() != params().depth())))
1153 if (!lyxrc.language_command_end.empty() &&
1154 previous_language->babel() != doc_language->babel())
1156 os << subst(lyxrc.language_command_end, "$$lang",
1157 previous_language->babel())
1162 if (lyxrc.language_command_end.empty() ||
1163 language->babel() != doc_language->babel())
1165 os << subst(lyxrc.language_command_begin, "$$lang",
1172 if (bparams.inputenc == "auto" &&
1173 language->encoding() != previous_language->encoding()) {
1174 os << "\\inputencoding{"
1175 << language->encoding()->LatexName()
1180 switch (style->latextype) {
1183 << style->latexname()
1184 << style->latexparam();
1186 case LATEX_ITEM_ENVIRONMENT:
1188 bibkey->latex(buf, os, false, false);
1192 case LATEX_LIST_ENVIRONMENT:
1199 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1201 // Make sure that \\par is done with the font of the last
1202 // character if this has another size as the default.
1203 // This is necessary because LaTeX (and LyX on the screen)
1204 // calculates the space between the baselines according
1205 // to this font. (Matthias)
1207 // Is this really needed ? (Dekel)
1208 // We do not need to use to change the font for the last paragraph
1209 // or for a command.
1210 LyXFont const font =
1212 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1214 bool is_command = style->isCommand();
1216 if (style->resfont.size() != font.size() && next_ && !is_command) {
1219 os << "\\" << font.latexSize() << " \\par}";
1220 } else if (need_par) {
1222 } else if (is_command)
1225 switch (style->latextype) {
1226 case LATEX_ITEM_ENVIRONMENT:
1227 case LATEX_LIST_ENVIRONMENT:
1228 if (next_ && (params().depth() < next_->params().depth())) {
1233 case LATEX_ENVIRONMENT:
1234 // if its the last paragraph of the current environment
1235 // skip it otherwise fall through
1237 && (next_->layout() != layout()
1238 || next_->params().depth() != params().depth()))
1240 // fall through possible
1242 // we don't need it for the last paragraph!!!
1249 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1250 further_blank_line = false;
1251 if (params().lineBottom()) {
1252 os << "\\lyxline{\\" << font.latexSize() << '}';
1253 further_blank_line = true;
1256 if (params().spaceBottom().kind() != VSpace::NONE) {
1257 os << params().spaceBottom().asLatexCommand(bparams);
1258 further_blank_line = true;
1261 if (params().pagebreakBottom()) {
1263 further_blank_line = true;
1266 if (further_blank_line) {
1271 if (!params().spacing().isDefault()
1272 && (!next_ || !next_->hasSameLayout(this))) {
1273 os << params().spacing().writeEnvirEnd() << "\n";
1278 // we don't need it for the last paragraph!!!
1283 // Since \selectlanguage write the language to the aux file,
1284 // we need to reset the language at the end of footnote or
1287 if (language->babel() != doc_language->babel()) {
1288 if (lyxrc.language_command_end.empty())
1289 os << subst(lyxrc.language_command_begin,
1291 doc_language->babel())
1294 os << subst(lyxrc.language_command_end,
1302 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1306 // This could go to ParagraphParameters if we want to
1307 int Paragraph::startTeXParParams(BufferParams const & bparams,
1308 ostream & os, bool moving_arg) const
1312 if (params().noindent()) {
1313 os << "\\noindent ";
1317 switch (params().align()) {
1318 case LYX_ALIGN_NONE:
1319 case LYX_ALIGN_BLOCK:
1320 case LYX_ALIGN_LAYOUT:
1321 case LYX_ALIGN_SPECIAL:
1323 case LYX_ALIGN_LEFT:
1324 case LYX_ALIGN_RIGHT:
1325 case LYX_ALIGN_CENTER:
1333 switch (params().align()) {
1334 case LYX_ALIGN_NONE:
1335 case LYX_ALIGN_BLOCK:
1336 case LYX_ALIGN_LAYOUT:
1337 case LYX_ALIGN_SPECIAL:
1339 case LYX_ALIGN_LEFT:
1340 if (getParLanguage(bparams)->babel() != "hebrew") {
1341 os << "\\begin{flushleft}";
1344 os << "\\begin{flushright}";
1348 case LYX_ALIGN_RIGHT:
1349 if (getParLanguage(bparams)->babel() != "hebrew") {
1350 os << "\\begin{flushright}";
1353 os << "\\begin{flushleft}";
1357 case LYX_ALIGN_CENTER:
1358 os << "\\begin{center}";
1366 // This could go to ParagraphParameters if we want to
1367 int Paragraph::endTeXParParams(BufferParams const & bparams,
1368 ostream & os, bool moving_arg) const
1372 switch (params().align()) {
1373 case LYX_ALIGN_NONE:
1374 case LYX_ALIGN_BLOCK:
1375 case LYX_ALIGN_LAYOUT:
1376 case LYX_ALIGN_SPECIAL:
1378 case LYX_ALIGN_LEFT:
1379 case LYX_ALIGN_RIGHT:
1380 case LYX_ALIGN_CENTER:
1388 switch (params().align()) {
1389 case LYX_ALIGN_NONE:
1390 case LYX_ALIGN_BLOCK:
1391 case LYX_ALIGN_LAYOUT:
1392 case LYX_ALIGN_SPECIAL:
1394 case LYX_ALIGN_LEFT:
1395 if (getParLanguage(bparams)->babel() != "hebrew") {
1396 os << "\\end{flushleft}";
1399 os << "\\end{flushright}";
1403 case LYX_ALIGN_RIGHT:
1404 if (getParLanguage(bparams)->babel() != "hebrew") {
1405 os << "\\end{flushright}";
1408 os << "\\end{flushleft}";
1412 case LYX_ALIGN_CENTER:
1413 os << "\\end{center}";
1421 // This one spits out the text of the paragraph
1422 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1423 BufferParams const & bparams,
1424 ostream & os, TexRow & texrow,
1427 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1429 bool return_value = false;
1431 LyXLayout_ptr style;
1433 // well we have to check if we are in an inset with unlimited
1434 // lenght (all in one row) if that is true then we don't allow
1435 // any special options in the paragraph and also we don't allow
1436 // any environment other then "Standard" to be valid!
1438 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1441 style = bparams.getLyXTextClass().defaultLayout();
1448 // Maybe we have to create a optional argument.
1450 if (style->labeltype != LABEL_MANUAL)
1453 main_body = beginningOfMainBody();
1457 if (main_body > 0) {
1460 basefont = getLabelFont(bparams);
1462 basefont = getLayoutFont(bparams);
1465 moving_arg |= style->needprotect;
1467 // Which font is currently active?
1468 LyXFont running_font(basefont);
1469 // Do we have an open font change?
1470 bool open_font = false;
1472 texrow.start(this, 0);
1474 // if the paragraph is empty, the loop will not be entered at all
1476 if (style->isCommand()) {
1481 column += startTeXParParams(bparams, os, moving_arg);
1485 for (pos_type i = 0; i < size(); ++i) {
1487 // First char in paragraph or after label?
1488 if (i == main_body) {
1489 if (main_body > 0) {
1491 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1494 basefont = getLayoutFont(bparams);
1495 running_font = basefont;
1499 if (style->isCommand()) {
1505 column += startTeXParParams(bparams, os,
1509 value_type c = getChar(i);
1511 // Fully instantiated font
1512 LyXFont font = getFont(bparams, i);
1514 LyXFont const last_font = running_font;
1516 // Spaces at end of font change are simulated to be
1517 // outside font change, i.e. we write "\textXX{text} "
1518 // rather than "\textXX{text }". (Asger)
1519 if (open_font && c == ' ' && i <= size() - 2) {
1520 LyXFont const & next_font = getFont(bparams, i + 1);
1521 if (next_font != running_font
1522 && next_font != font) {
1527 // We end font definition before blanks
1529 (font != running_font ||
1530 font.language() != running_font.language()))
1532 column += running_font.latexWriteEndChanges(os,
1534 (i == main_body-1) ? basefont : font);
1535 running_font = basefont;
1539 // Blanks are printed before start of fontswitch
1541 // Do not print the separation of the optional argument
1542 if (i != main_body - 1) {
1543 pimpl_->simpleTeXBlanks(os, texrow, i,
1544 column, font, *style);
1548 // Do we need to change font?
1549 if ((font != running_font ||
1550 font.language() != running_font.language()) &&
1553 column += font.latexWriteStartChanges(os, basefont,
1555 running_font = font;
1559 if (c == Paragraph::META_NEWLINE) {
1560 // newlines are handled differently here than
1561 // the default in SimpleTeXSpecialChars().
1562 if (!style->newline_allowed) {
1566 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1569 basefont = getLayoutFont(bparams);
1570 running_font = basefont;
1571 if (font.family() ==
1572 LyXFont::TYPEWRITER_FAMILY) {
1581 texrow.start(this, i + 1);
1584 pimpl_->simpleTeXSpecialChars(buf, bparams,
1585 os, texrow, moving_arg,
1587 basefont, open_font,
1588 *style, i, column, c);
1592 // If we have an open font definition, we have to close it
1594 #ifdef FIXED_LANGUAGE_END_DETECTION
1597 .latexWriteEndChanges(os, basefont,
1598 next_->getFont(bparams,
1601 running_font.latexWriteEndChanges(os, basefont,
1605 #ifdef WITH_WARNINGS
1606 //#warning For now we ALWAYS have to close the foreign font settings if they are
1607 //#warning there as we start another \selectlanguage with the next paragraph if
1608 //#warning we are in need of this. This should be fixed sometime (Jug)
1610 running_font.latexWriteEndChanges(os, basefont, basefont);
1614 // Needed if there is an optional argument but no contents.
1615 if (main_body > 0 && main_body == size()) {
1617 return_value = false;
1621 column += endTeXParParams(bparams, os, moving_arg);
1624 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1625 return return_value;
1629 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1630 BufferParams const & bparams,
1631 ostream & os, TexRow & texrow)
1633 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1635 LyXLayout_ptr const & style = layout();
1637 Language const * language = getParLanguage(bparams);
1638 Language const * doc_language = bparams.language;
1639 Language const * previous_language = previous_
1640 ? previous_->getParLanguage(bparams) : doc_language;
1641 if (language->babel() != previous_language->babel()) {
1643 if (!lyxrc.language_command_end.empty() &&
1644 previous_language->babel() != doc_language->babel()) {
1645 os << subst(lyxrc.language_command_end, "$$lang",
1646 previous_language->babel())
1651 if (lyxrc.language_command_end.empty() ||
1652 language->babel() != doc_language->babel()) {
1653 os << subst(lyxrc.language_command_begin, "$$lang",
1660 bool leftindent_open = false;
1661 if (!params().leftIndent().zero()) {
1662 os << "\\begin{LyXParagraphLeftIndent}{" <<
1663 params().leftIndent().asLatexString() << "}\n";
1665 leftindent_open = true;
1668 if (style->isEnvironment()) {
1669 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1670 os << "\\begin{" << style->latexname() << "}{"
1671 << params().labelWidthString() << "}\n";
1672 } else if (style->labeltype == LABEL_BIBLIO) {
1674 os << "\\begin{" << style->latexname() << "}{"
1675 << bibitemWidest(buf)
1677 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1678 os << "\\begin{" << style->latexname() << '}'
1679 << style->latexparam() << '\n';
1681 os << "\\begin{" << style->latexname() << '}'
1682 << style->latexparam() << '\n';
1685 Paragraph * par = this;
1687 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1689 if (par && par->params().depth() > params().depth()) {
1690 if (par->layout()->isParagraph()) {
1693 // How to handle this? (Lgb)
1694 //&& !suffixIs(os, "\n\n")
1696 // There should be at least one '\n' already
1697 // but we need there to be two for Standard
1698 // paragraphs that are depth-increment'ed to be
1699 // output correctly. However, tables can
1700 // also be paragraphs so don't adjust them.
1703 // Will it ever harm to have one '\n' too
1704 // many? i.e. that we sometimes will have
1705 // three in a row. (Lgb)
1709 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1712 && par->layout() == layout()
1713 && par->params().depth() == params().depth()
1714 && par->params().leftIndent() == params().leftIndent());
1716 if (style->isEnvironment()) {
1717 os << "\\end{" << style->latexname() << "}\n";
1721 if (leftindent_open) {
1722 os << "\\end{LyXParagraphLeftIndent}\n";
1726 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1727 return par; // ale970302
1731 bool Paragraph::isHfill(pos_type pos) const
1733 return IsHfillChar(getChar(pos));
1737 bool Paragraph::isInset(pos_type pos) const
1739 return IsInsetChar(getChar(pos));
1743 bool Paragraph::isNewline(pos_type pos) const
1745 return pos >= 0 && IsNewlineChar(getChar(pos));
1749 bool Paragraph::isSeparator(pos_type pos) const
1751 return IsSeparatorChar(getChar(pos));
1755 bool Paragraph::isLineSeparator(pos_type pos) const
1757 value_type const c = getChar(pos);
1758 return IsLineSeparatorChar(c)
1759 || (IsInsetChar(c) && getInset(pos) &&
1760 getInset(pos)->isLineSeparator());
1764 bool Paragraph::isKomma(pos_type pos) const
1766 return IsKommaChar(getChar(pos));
1770 /// Used by the spellchecker
1771 bool Paragraph::isLetter(pos_type pos) const
1773 value_type const c = getChar(pos);
1774 if (IsLetterChar(c))
1777 return getInset(pos)->isLetter();
1778 // We want to pass the ' and escape chars to ispell
1779 string const extra = lyxrc.isp_esc_chars + '\'';
1780 return contains(extra, c);
1784 bool Paragraph::isWord(pos_type pos) const
1786 return IsWordChar(getChar(pos)) ;
1791 Paragraph::getParLanguage(BufferParams const & bparams) const
1794 #ifndef INHERIT_LANGUAGE
1795 return getFirstFontSettings().language();
1797 Language const * lang = getFirstFontSettings().language();
1798 #ifdef WITH_WARNINGS
1799 #warning We should make this somewhat better, any ideas? (Jug)
1801 if (lang == inherit_language || lang == ignore_language)
1802 lang = bparams.language;
1805 } else if (previous_)
1806 return previous_->getParLanguage(bparams);
1808 return bparams.language;
1812 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1814 return lyxrc.rtl_support
1815 && getParLanguage(bparams)->RightToLeft()
1816 && !(inInset() && inInset()->owner() &&
1817 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1821 void Paragraph::changeLanguage(BufferParams const & bparams,
1822 Language const * from, Language const * to)
1824 for (pos_type i = 0; i < size(); ++i) {
1825 LyXFont font = getFontSettings(bparams, i);
1826 if (font.language() == from) {
1827 font.setLanguage(to);
1834 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1836 Language const * doc_language = bparams.language;
1837 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1838 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1840 for (; cit != end; ++cit)
1841 if (cit->font().language() != ignore_language &&
1842 cit->font().language() != latex_language &&
1843 #ifdef INHERIT_LANGUAGE
1844 cit->font().language() != inherit_language &&
1846 cit->font().language() != doc_language)
1852 // Convert the paragraph to a string.
1853 // Used for building the table of contents
1854 string const Paragraph::asString(Buffer const * buffer, bool label)
1856 BufferParams const & bparams = buffer->params;
1858 if (label && !params().labelString().empty())
1859 s += params().labelString() + ' ';
1860 string::size_type const len = s.size();
1862 for (pos_type i = 0; i < size(); ++i) {
1863 value_type c = getChar(i);
1866 else if (c == META_INSET &&
1867 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1869 getInset(i)->ascii(buffer, ost);
1870 s += subst(ost.str().c_str(),'\n',' ');
1874 if (isRightToLeftPar(bparams))
1875 reverse(s.begin() + len,s.end());
1881 string const Paragraph::asString(Buffer const * buffer,
1882 pos_type beg, pos_type end, bool label)
1886 if (beg == 0 && label && !params().labelString().empty())
1887 ost << params().labelString() << ' ';
1889 for (pos_type i = beg; i < end; ++i) {
1890 value_type const c = getUChar(buffer->params, i);
1893 else if (c == META_NEWLINE)
1895 else if (c == META_HFILL)
1897 else if (c == META_INSET) {
1898 getInset(i)->ascii(buffer, ost);
1902 return ost.str().c_str();
1906 void Paragraph::setInsetOwner(Inset * i)
1908 pimpl_->inset_owner = i;
1909 InsetList::iterator it = insetlist.begin();
1910 InsetList::iterator end = insetlist.end();
1911 for (; it != end; ++it) {
1913 it.getInset()->setOwner(i);
1918 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1921 insetlist.deleteInsetsLyXText(bv);
1925 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1928 insetlist.resizeInsetsLyXText(bv);
1932 void Paragraph::setContentsFromPar(Paragraph * par)
1934 pimpl_->setContentsFromPar(par);
1938 lyx::pos_type Paragraph::size() const
1940 return pimpl_->size();
1944 bool Paragraph::empty() const
1946 return pimpl_->empty();
1950 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1952 return pimpl_->getChar(pos);
1956 int Paragraph::id() const
1962 LyXLayout_ptr const & Paragraph::layout() const
1968 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1970 layout_ = new_layout;
1974 bool Paragraph::isFirstInSequence() const
1976 Paragraph const * dhook = depthHook(getDepth());
1977 return (dhook == this
1978 || dhook->layout() != layout()
1979 || dhook->getDepth() != getDepth());
1983 Inset * Paragraph::inInset() const
1985 return pimpl_->inset_owner;
1989 void Paragraph::clearContents()
1994 void Paragraph::setChar(pos_type pos, value_type c)
1996 pimpl_->setChar(pos, c);
2000 ParagraphParameters & Paragraph::params()
2002 return pimpl_->params;
2006 ParagraphParameters const & Paragraph::params() const
2008 return pimpl_->params;
2012 Paragraph * Paragraph::getParFromID(int id) const
2014 return pimpl_->getParFromID(id);
2018 bool Paragraph::isFreeSpacing() const
2020 // for now we just need this, later should we need this in some
2021 // other way we can always add a function to Inset::() too.
2022 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2023 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);