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 for (InsetList::iterator it = insetlist.begin();
131 it != insetlist.end(); ++it)
133 it.setInset(it.getInset()->clone(*current_view->buffer(), same_ids));
134 // tell the new inset who is the boss now
135 it.getInset()->parOwner(this);
140 // the destructor removes the new paragraph from the list
141 Paragraph::~Paragraph()
144 previous_->next_ = next_;
146 next_->previous_ = previous_;
153 //lyxerr << "Paragraph::paragraph_id = "
154 // << Paragraph::paragraph_id << endl;
158 void Paragraph::writeFile(Buffer const * buf, ostream & os,
159 BufferParams const & bparams,
160 depth_type dth) const
162 // The beginning or end of a deeper (i.e. nested) area?
163 if (dth != params().depth()) {
164 if (params().depth() > dth) {
165 while (params().depth() > dth) {
166 os << "\n\\begin_deeper ";
170 while (params().depth() < dth) {
171 os << "\n\\end_deeper ";
177 // First write the layout
178 os << "\n\\layout " << layout()->name() << "\n";
180 // Maybe some vertical spaces.
181 if (params().spaceTop().kind() != VSpace::NONE)
182 os << "\\added_space_top "
183 << params().spaceTop().asLyXCommand() << " ";
184 if (params().spaceBottom().kind() != VSpace::NONE)
185 os << "\\added_space_bottom "
186 << params().spaceBottom().asLyXCommand() << " ";
188 // Maybe the paragraph has special spacing
189 params().spacing().writeFile(os, true);
191 // The labelwidth string used in lists.
192 if (!params().labelWidthString().empty())
193 os << "\\labelwidthstring "
194 << params().labelWidthString() << '\n';
196 // Lines above or below?
197 if (params().lineTop())
199 if (params().lineBottom())
200 os << "\\line_bottom ";
202 // Pagebreaks above or below?
203 if (params().pagebreakTop())
204 os << "\\pagebreak_top ";
205 if (params().pagebreakBottom())
206 os << "\\pagebreak_bottom ";
208 // Start of appendix?
209 if (params().startOfAppendix())
210 os << "\\start_of_appendix ";
213 if (params().noindent())
216 // Do we have a manual left indent?
217 if (!params().leftIndent().zero())
218 os << "\\leftindent " << params().leftIndent().asString() << " ";
221 if (params().align() != LYX_ALIGN_LAYOUT) {
223 switch (params().align()) {
224 case LYX_ALIGN_LEFT: h = 1; break;
225 case LYX_ALIGN_RIGHT: h = 2; break;
226 case LYX_ALIGN_CENTER: h = 3; break;
227 default: h = 0; break;
229 os << "\\align " << string_align[h] << " ";
234 bibkey->write(buf, os);
236 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
239 for (pos_type i = 0; i < size(); ++i) {
245 // Write font changes
246 LyXFont font2 = getFontSettings(bparams, i);
247 if (font2 != font1) {
248 #ifndef INHERIT_LANGUAGE
249 font2.lyxWriteChanges(font1, os);
251 font2.lyxWriteChanges(font1, bparams.language, os);
257 value_type const c = getChar(i);
261 Inset const * inset = getInset(i);
263 if (inset->directWrite()) {
264 // international char, let it write
265 // code directly so it's shorter in
267 inset->write(buf, os);
269 os << "\n\\begin_inset ";
270 inset->write(buf, os);
271 os << "\n\\end_inset \n\n";
277 os << "\n\\newline \n";
281 os << "\n\\hfill \n";
285 os << "\n\\backslash \n";
289 if (i + 1 < size() && getChar(i + 1) == ' ') {
296 if ((column > 70 && c == ' ')
301 // this check is to amend a bug. LyX sometimes
302 // inserts '\0' this could cause problems.
306 lyxerr << "ERROR (Paragraph::writeFile):"
307 " NULL char in structure." << endl;
313 // now write the next paragraph
315 next_->writeFile(buf, os, bparams, dth);
319 void Paragraph::validate(LaTeXFeatures & features) const
321 pimpl_->validate(features, *layout());
325 // First few functions needed for cut and paste and paragraph breaking.
326 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
328 BufferParams bparams = buffer.params;
330 minibuffer_char = getChar(pos);
331 minibuffer_font = getFontSettings(bparams, pos);
332 minibuffer_inset = 0;
333 if (minibuffer_char == Paragraph::META_INSET) {
335 minibuffer_inset = getInset(pos)->clone(buffer);
337 minibuffer_inset = 0;
338 minibuffer_char = ' ';
339 // This reflects what GetInset() does (ARRae)
345 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
347 minibuffer_char = getChar(pos);
348 minibuffer_font = getFontSettings(bparams, pos);
349 minibuffer_inset = 0;
350 if (minibuffer_char == Paragraph::META_INSET) {
352 minibuffer_inset = getInset(pos);
353 // This is a little hack since I want exactly
354 // the inset, not just a clone. Otherwise
355 // the inset would be deleted when calling Erase(pos)
357 InsetList::iterator it = insetlist.begin();
358 InsetList::iterator end = insetlist.end();
359 for (; it != end; ++it) {
360 if (it.getPos() == pos)
364 if (it != end && it.getPos() == pos)
366 // the inset is not in a paragraph anymore
367 minibuffer_inset->parOwner(0);
369 minibuffer_inset = 0;
370 minibuffer_char = ' ';
371 // This reflects what GetInset() does (ARRae)
376 // Erase(pos); now the caller is responsible for that.
380 bool Paragraph::insertFromMinibuffer(pos_type pos)
382 if (minibuffer_char == Paragraph::META_INSET) {
383 if (!insetAllowed(minibuffer_inset->lyxCode())) {
386 insertInset(pos, minibuffer_inset, minibuffer_font);
388 LyXFont f = minibuffer_font;
389 if (!checkInsertChar(f)) {
392 insertChar(pos, minibuffer_char, f);
400 void Paragraph::erase(pos_type pos)
406 bool Paragraph::checkInsertChar(LyXFont & font)
408 if (pimpl_->inset_owner)
409 return pimpl_->inset_owner->checkInsertChar(font);
414 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
416 LyXFont const f(LyXFont::ALL_INHERIT);
417 insertChar(pos, c, f);
421 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
422 LyXFont const & font)
424 pimpl_->insertChar(pos, c, font);
428 void Paragraph::insertInset(pos_type pos, Inset * inset)
430 LyXFont const f(LyXFont::ALL_INHERIT);
431 insertInset(pos, inset, f);
435 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
437 pimpl_->insertInset(pos, inset, font);
441 bool Paragraph::insetAllowed(Inset::Code code)
443 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
445 if (pimpl_->inset_owner)
446 return pimpl_->inset_owner->insetAllowed(code);
451 Inset * Paragraph::getInset(pos_type pos)
453 lyx::Assert(pos < size());
456 InsetList::iterator it = insetlist.begin();
457 InsetList::iterator end = insetlist.end();
458 for (; it != end; ++it) {
459 if (it.getPos() == pos)
463 if (it != end && it.getPos() == pos)
464 return it.getInset();
466 lyxerr << "ERROR (Paragraph::getInset): "
467 << "Inset does not exist: " << pos << endl;
470 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
471 // Did this commenting out introduce a bug? So far I have not
472 // see any, please enlighten me. (Lgb)
473 // My guess is that since the inset does not exist, we might
474 // as well replace it with a space to prevent craches. (Asger)
479 Inset const * Paragraph::getInset(pos_type pos) const
481 lyx::Assert(pos < size());
483 return insetlist.get(pos);
487 // Gets uninstantiated font setting at position.
488 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
491 lyx::Assert(pos <= size());
493 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
494 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
495 for (; cit != end; ++cit) {
496 if (cit->pos() >= pos)
502 retfont = cit->font();
503 } else if (pos == size() && !empty()) {
504 retfont = getFontSettings(bparams, pos - 1);
506 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
507 #ifdef INHERIT_LANGUAGE
508 if (retfont.language() == inherit_language)
509 retfont.setLanguage(bparams.language);
516 // Gets uninstantiated font setting at position 0
517 LyXFont const Paragraph::getFirstFontSettings() const
519 if (!empty() && !pimpl_->fontlist.empty())
520 return pimpl_->fontlist[0].font();
522 return LyXFont(LyXFont::ALL_INHERIT);
526 // Gets the fully instantiated font at a given position in a paragraph
527 // This is basically the same function as LyXText::GetFont() in text2.C.
528 // The difference is that this one is used for generating the LaTeX file,
529 // and thus cosmetic "improvements" are disallowed: This has to deliver
530 // the true picture of the buffer. (Asger)
531 // If position is -1, we get the layout font of the paragraph.
532 // If position is -2, we get the font of the manual label of the paragraph.
533 LyXFont const Paragraph::getFont(BufferParams const & bparams,
536 lyx::Assert(pos >= 0);
538 LyXLayout_ptr const & lout = layout();
540 pos_type main_body = 0;
541 if (lout->labeltype == LABEL_MANUAL)
542 main_body = beginningOfMainBody();
546 layoutfont = lout->labelfont;
548 layoutfont = lout->font;
550 LyXFont tmpfont = getFontSettings(bparams, pos);
551 #ifndef INHERIT_LANGUAGE
552 tmpfont.realize(layoutfont);
554 tmpfont.realize(layoutfont, bparams.language);
557 return pimpl_->realizeFont(tmpfont, bparams);
561 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
563 LyXLayout_ptr const & lout = layout();
565 LyXFont tmpfont = lout->labelfont;
566 tmpfont.setLanguage(getParLanguage(bparams));
568 return pimpl_->realizeFont(tmpfont, bparams);
572 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
574 LyXLayout_ptr const & lout = layout();
576 LyXFont tmpfont = lout->font;
577 tmpfont.setLanguage(getParLanguage(bparams));
579 return pimpl_->realizeFont(tmpfont, bparams);
583 /// Returns the height of the highest font in range
585 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
586 LyXFont::FONT_SIZE const def_size) const
588 if (pimpl_->fontlist.empty())
591 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
592 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
593 for (; end_it != end; ++end_it) {
594 if (end_it->pos() >= endpos)
601 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
602 for (; cit != end; ++cit) {
603 if (cit->pos() >= startpos)
607 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
608 for (; cit != end_it; ++cit) {
609 LyXFont::FONT_SIZE size = cit->font().size();
610 if (size == LyXFont::INHERIT_SIZE)
612 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
619 Paragraph::value_type
620 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
622 value_type c = getChar(pos);
623 if (!lyxrc.rtl_support)
653 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
660 void Paragraph::setFont(pos_type pos, LyXFont const & font)
662 lyx::Assert(pos <= size());
664 // First, reduce font against layout/label font
665 // Update: The SetCharFont() routine in text2.C already
666 // reduces font, so we don't need to do that here. (Asger)
667 // No need to simplify this because it will disappear
668 // in a new kernel. (Asger)
669 // Next search font table
671 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
672 Pimpl::FontList::iterator it = beg;
673 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
674 for (; it != endit; ++it) {
675 if (it->pos() >= pos)
678 unsigned int i = std::distance(beg, it);
679 bool notfound = (it == endit);
681 if (!notfound && pimpl_->fontlist[i].font() == font)
684 bool begin = pos == 0 || notfound ||
685 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
686 // Is position pos is a beginning of a font block?
687 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
688 // Is position pos is the end of a font block?
689 if (begin && end) { // A single char block
690 if (i + 1 < pimpl_->fontlist.size() &&
691 pimpl_->fontlist[i + 1].font() == font) {
692 // Merge the singleton block with the next block
693 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
694 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
695 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
696 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
697 // Merge the singleton block with the previous block
698 pimpl_->fontlist[i - 1].pos(pos);
699 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
701 pimpl_->fontlist[i].font(font);
703 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
704 pimpl_->fontlist[i - 1].pos(pos);
706 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
707 Pimpl::FontTable(pos, font));
709 pimpl_->fontlist[i].pos(pos - 1);
710 if (!(i + 1 < pimpl_->fontlist.size() &&
711 pimpl_->fontlist[i + 1].font() == font))
712 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
713 Pimpl::FontTable(pos, font));
714 } else { // The general case. The block is splitted into 3 blocks
715 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
716 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
717 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
718 Pimpl::FontTable(pos, font));
724 void Paragraph::next(Paragraph * p)
730 // This function is able to hide closed footnotes.
731 Paragraph * Paragraph::next()
737 Paragraph const * Paragraph::next() const
743 void Paragraph::previous(Paragraph * p)
749 // This function is able to hide closed footnotes.
750 Paragraph * Paragraph::previous()
756 // This function is able to hide closed footnotes.
757 Paragraph const * Paragraph::previous() const
763 void Paragraph::breakParagraph(BufferParams const & bparams,
767 // create a new paragraph
768 Paragraph * tmp = new Paragraph(this);
769 tmp->layout(bparams.getLyXTextClass().defaultLayout());
770 // remember to set the inset_owner
771 tmp->setInsetOwner(inInset());
773 // this is an idea for a more userfriendly layout handling, I will
774 // see what the users say
776 // layout stays the same with latex-environments
778 tmp->layout(layout());
779 tmp->setLabelWidthString(params().labelWidthString());
782 bool isempty = (layout()->keepempty && empty());
784 if (!isempty && (size() > pos || empty() || flag == 2)) {
785 tmp->layout(layout());
786 tmp->params().align(params().align());
787 tmp->setLabelWidthString(params().labelWidthString());
789 tmp->params().lineBottom(params().lineBottom());
790 params().lineBottom(false);
791 tmp->params().pagebreakBottom(params().pagebreakBottom());
792 params().pagebreakBottom(false);
793 tmp->params().spaceBottom(params().spaceBottom());
794 params().spaceBottom(VSpace(VSpace::NONE));
796 tmp->params().depth(params().depth());
797 tmp->params().noindent(params().noindent());
799 // copy everything behind the break-position
800 // to the new paragraph
801 pos_type pos_end = size() - 1;
804 for (; i <= pos_end; ++i) {
805 cutIntoMinibuffer(bparams, i);
806 if (tmp->insertFromMinibuffer(j - pos))
809 for (i = pos_end; i >= pos; --i) {
814 // just an idea of me
815 if (!isempty && !pos) {
816 tmp->params().lineTop(params().lineTop());
817 tmp->params().pagebreakTop(params().pagebreakTop());
818 tmp->params().spaceTop(params().spaceTop());
819 tmp->bibkey = bibkey;
824 layout(bparams.getLyXTextClass().defaultLayout());
826 // layout stays the same with latex-environments
828 layout(tmp->layout());
829 setLabelWidthString(tmp->params().labelWidthString());
830 params().depth(tmp->params().depth());
836 void Paragraph::makeSameLayout(Paragraph const * par)
838 layout(par->layout());
840 params() = par->params();
844 int Paragraph::stripLeadingSpaces()
846 if (layout()->free_spacing ||
852 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
861 bool Paragraph::hasSameLayout(Paragraph const * par) const
864 par->layout() == layout() &&
865 params().sameLayout(par->params());
869 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
872 // create a new paragraph
873 Paragraph * tmp = new Paragraph(this);
874 tmp->makeSameLayout(this);
876 // When can pos > Last()?
877 // I guess pos == Last() is possible.
879 // copy everything behind the break-position to the new
881 pos_type pos_end = size() - 1;
885 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
886 cutIntoMinibuffer(bparams, i);
887 if (tmp->insertFromMinibuffer(j - pos))
891 for (pos_type k = pos_end; k >= pos; --k) {
898 // Be carefull, this does not make any check at all.
899 // This method has wrong name, it combined this par with the next par.
900 // In that sense it is the reverse of break paragraph. (Lgb)
901 void Paragraph::pasteParagraph(BufferParams const & bparams)
903 // copy the next paragraph to this one
904 Paragraph * the_next = next();
906 // first the DTP-stuff
907 params().lineBottom(the_next->params().lineBottom());
908 params().spaceBottom(the_next->params().spaceBottom());
909 params().pagebreakBottom(the_next->params().pagebreakBottom());
911 pos_type pos_end = the_next->pimpl_->size() - 1;
912 pos_type pos_insert = size();
914 // ok, now copy the paragraph
915 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
916 the_next->cutIntoMinibuffer(bparams, i);
917 if (insertFromMinibuffer(pos_insert + j))
921 // delete the next paragraph
922 Paragraph * ppar = the_next->previous_;
923 Paragraph * npar = the_next->next_;
929 int Paragraph::getEndLabel() const
931 Paragraph const * par = this;
932 depth_type par_depth = getDepth();
934 LyXLayout_ptr const & layout = par->layout();
935 int const endlabeltype = layout->endlabeltype;
937 if (endlabeltype != END_LABEL_NO_LABEL) {
941 depth_type const next_depth = next_->getDepth();
942 if (par_depth > next_depth ||
943 (par_depth == next_depth
944 && layout != next_->layout()))
950 par = par->outerHook();
952 par_depth = par->getDepth();
954 return END_LABEL_NO_LABEL;
958 Paragraph::depth_type Paragraph::getDepth() const
960 return params().depth();
964 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
966 bool const isenv = layout()->isEnvironment();
969 return params().depth() + 1;
971 return params().depth();
975 char Paragraph::getAlign() const
977 return params().align();
981 string const & Paragraph::getLabelstring() const
983 return params().labelString();
987 int Paragraph::getFirstCounter(int i) const
989 return pimpl_->counter_[i];
993 // the next two functions are for the manual labels
994 string const Paragraph::getLabelWidthString() const
996 if (!params().labelWidthString().empty())
997 return params().labelWidthString();
999 return _("Senseless with this layout!");
1003 void Paragraph::setLabelWidthString(string const & s)
1005 params().labelWidthString(s);
1009 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
1012 params().labelWidthString(string());
1013 params().align(LYX_ALIGN_LAYOUT);
1014 params().spaceTop(VSpace(VSpace::NONE));
1015 params().spaceBottom(VSpace(VSpace::NONE));
1016 params().spacing(Spacing(Spacing::Default));
1020 // if the layout of a paragraph contains a manual label, the beginning of the
1021 // main body is the beginning of the second word. This is what the par-
1022 // function returns. If the layout does not contain a label, the main
1023 // body always starts with position 0. This differentiation is necessary,
1024 // because there cannot be a newline or a blank <= the beginning of the
1025 // main body in TeX.
1027 int Paragraph::beginningOfMainBody() const
1029 // Unroll the first two cycles of the loop
1030 // and remember the previous character to
1031 // remove unnecessary GetChar() calls
1033 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
1035 char previous_char = 0;
1038 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1039 // Yes, this ^ is supposed to be "= " not "=="
1042 && previous_char != ' '
1043 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1045 previous_char = temp;
1054 Paragraph * Paragraph::depthHook(depth_type depth)
1056 Paragraph * newpar = this;
1059 newpar = newpar->previous();
1060 } while (newpar && newpar->getDepth() > depth);
1063 if (previous() || getDepth())
1064 lyxerr << "ERROR (Paragraph::DepthHook): "
1073 Paragraph const * Paragraph::depthHook(depth_type depth) const
1075 Paragraph const * newpar = this;
1078 newpar = newpar->previous();
1079 } while (newpar && newpar->getDepth() > depth);
1082 if (previous() || getDepth())
1083 lyxerr << "ERROR (Paragraph::DepthHook): "
1091 Paragraph * Paragraph::outerHook()
1095 return depthHook(depth_type(getDepth() - 1));
1098 Paragraph const * Paragraph::outerHook() const
1102 return depthHook(depth_type(getDepth() - 1));
1106 // returns -1 if inset not found
1107 int Paragraph::getPositionOfInset(Inset const * inset) const
1110 InsetList::iterator it = insetlist.begin();
1111 InsetList::iterator end = insetlist.end();
1112 for (; it != end; ++it) {
1113 if (it.getInset() == inset) {
1117 if (inset == bibkey)
1124 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1125 BufferParams const & bparams,
1126 ostream & os, TexRow & texrow,
1129 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1130 Inset const * in = inInset();
1131 bool further_blank_line = false;
1132 LyXLayout_ptr style;
1134 // well we have to check if we are in an inset with unlimited
1135 // lenght (all in one row) if that is true then we don't allow
1136 // any special options in the paragraph and also we don't allow
1137 // any environment other then "Standard" to be valid!
1138 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1141 if (params().startOfAppendix()) {
1142 os << "\\appendix\n";
1146 if (!params().spacing().isDefault()
1147 && (!previous() || !previous()->hasSameLayout(this))) {
1148 os << params().spacing().writeEnvirBegin() << "\n";
1152 if (style->isCommand()) {
1157 if (params().pagebreakTop()) {
1159 further_blank_line = true;
1161 if (params().spaceTop().kind() != VSpace::NONE) {
1162 os << params().spaceTop().asLatexCommand(bparams);
1163 further_blank_line = true;
1166 if (params().lineTop()) {
1167 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1168 << "\\vspace{-1\\parskip}";
1169 further_blank_line = true;
1172 if (further_blank_line) {
1177 style = bparams.getLyXTextClass().defaultLayout();
1180 Language const * language = getParLanguage(bparams);
1181 Language const * doc_language = bparams.language;
1182 Language const * previous_language = previous()
1183 ? previous()->getParLanguage(bparams) : doc_language;
1185 if (language->babel() != previous_language->babel()
1186 // check if we already put language command in TeXEnvironment()
1187 && !(style->isEnvironment()
1188 && (!previous() || previous()->layout() != layout() ||
1189 previous()->params().depth() != params().depth())))
1191 if (!lyxrc.language_command_end.empty() &&
1192 previous_language->babel() != doc_language->babel())
1194 os << subst(lyxrc.language_command_end, "$$lang",
1195 previous_language->babel())
1200 if (lyxrc.language_command_end.empty() ||
1201 language->babel() != doc_language->babel())
1203 os << subst(lyxrc.language_command_begin, "$$lang",
1210 if (bparams.inputenc == "auto" &&
1211 language->encoding() != previous_language->encoding()) {
1212 os << "\\inputencoding{"
1213 << language->encoding()->LatexName()
1218 switch (style->latextype) {
1221 << style->latexname()
1222 << style->latexparam();
1224 case LATEX_ITEM_ENVIRONMENT:
1226 bibkey->latex(buf, os, false, false);
1230 case LATEX_LIST_ENVIRONMENT:
1237 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1239 // Make sure that \\par is done with the font of the last
1240 // character if this has another size as the default.
1241 // This is necessary because LaTeX (and LyX on the screen)
1242 // calculates the space between the baselines according
1243 // to this font. (Matthias)
1245 // Is this really needed ? (Dekel)
1246 // We do not need to use to change the font for the last paragraph
1247 // or for a command.
1248 LyXFont const font =
1250 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1252 bool is_command = style->isCommand();
1254 if (style->resfont.size() != font.size() && next_ && !is_command) {
1257 os << "\\" << font.latexSize() << " \\par}";
1258 } else if (need_par) {
1260 } else if (is_command)
1263 switch (style->latextype) {
1264 case LATEX_ITEM_ENVIRONMENT:
1265 case LATEX_LIST_ENVIRONMENT:
1266 if (next_ && (params().depth() < next_->params().depth())) {
1271 case LATEX_ENVIRONMENT:
1272 // if its the last paragraph of the current environment
1273 // skip it otherwise fall through
1275 && (next_->layout() != layout()
1276 || next_->params().depth() != params().depth()))
1278 // fall through possible
1280 // we don't need it for the last paragraph!!!
1287 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1288 further_blank_line = false;
1289 if (params().lineBottom()) {
1290 os << "\\lyxline{\\" << font.latexSize() << '}';
1291 further_blank_line = true;
1294 if (params().spaceBottom().kind() != VSpace::NONE) {
1295 os << params().spaceBottom().asLatexCommand(bparams);
1296 further_blank_line = true;
1299 if (params().pagebreakBottom()) {
1301 further_blank_line = true;
1304 if (further_blank_line) {
1309 if (!params().spacing().isDefault()
1310 && (!next_ || !next_->hasSameLayout(this))) {
1311 os << params().spacing().writeEnvirEnd() << "\n";
1316 // we don't need it for the last paragraph!!!
1321 // Since \selectlanguage write the language to the aux file,
1322 // we need to reset the language at the end of footnote or
1325 if (language->babel() != doc_language->babel()) {
1326 if (lyxrc.language_command_end.empty())
1327 os << subst(lyxrc.language_command_begin,
1329 doc_language->babel())
1332 os << subst(lyxrc.language_command_end,
1340 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1344 // This could go to ParagraphParameters if we want to
1345 int Paragraph::startTeXParParams(BufferParams const & bparams,
1346 ostream & os, bool moving_arg) const
1350 if (params().noindent()) {
1351 os << "\\noindent ";
1355 switch (params().align()) {
1356 case LYX_ALIGN_NONE:
1357 case LYX_ALIGN_BLOCK:
1358 case LYX_ALIGN_LAYOUT:
1359 case LYX_ALIGN_SPECIAL:
1361 case LYX_ALIGN_LEFT:
1362 case LYX_ALIGN_RIGHT:
1363 case LYX_ALIGN_CENTER:
1371 switch (params().align()) {
1372 case LYX_ALIGN_NONE:
1373 case LYX_ALIGN_BLOCK:
1374 case LYX_ALIGN_LAYOUT:
1375 case LYX_ALIGN_SPECIAL:
1377 case LYX_ALIGN_LEFT:
1378 if (getParLanguage(bparams)->babel() != "hebrew") {
1379 os << "\\begin{flushleft}";
1382 os << "\\begin{flushright}";
1386 case LYX_ALIGN_RIGHT:
1387 if (getParLanguage(bparams)->babel() != "hebrew") {
1388 os << "\\begin{flushright}";
1391 os << "\\begin{flushleft}";
1395 case LYX_ALIGN_CENTER:
1396 os << "\\begin{center}";
1404 // This could go to ParagraphParameters if we want to
1405 int Paragraph::endTeXParParams(BufferParams const & bparams,
1406 ostream & os, bool moving_arg) const
1410 switch (params().align()) {
1411 case LYX_ALIGN_NONE:
1412 case LYX_ALIGN_BLOCK:
1413 case LYX_ALIGN_LAYOUT:
1414 case LYX_ALIGN_SPECIAL:
1416 case LYX_ALIGN_LEFT:
1417 case LYX_ALIGN_RIGHT:
1418 case LYX_ALIGN_CENTER:
1426 switch (params().align()) {
1427 case LYX_ALIGN_NONE:
1428 case LYX_ALIGN_BLOCK:
1429 case LYX_ALIGN_LAYOUT:
1430 case LYX_ALIGN_SPECIAL:
1432 case LYX_ALIGN_LEFT:
1433 if (getParLanguage(bparams)->babel() != "hebrew") {
1434 os << "\\end{flushleft}";
1437 os << "\\end{flushright}";
1441 case LYX_ALIGN_RIGHT:
1442 if (getParLanguage(bparams)->babel() != "hebrew") {
1443 os << "\\end{flushright}";
1446 os << "\\end{flushleft}";
1450 case LYX_ALIGN_CENTER:
1451 os << "\\end{center}";
1459 // This one spits out the text of the paragraph
1460 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1461 BufferParams const & bparams,
1462 ostream & os, TexRow & texrow,
1465 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1467 bool return_value = false;
1469 LyXLayout_ptr style;
1471 // well we have to check if we are in an inset with unlimited
1472 // lenght (all in one row) if that is true then we don't allow
1473 // any special options in the paragraph and also we don't allow
1474 // any environment other then "Standard" to be valid!
1476 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1479 style = bparams.getLyXTextClass().defaultLayout();
1486 // Maybe we have to create a optional argument.
1488 if (style->labeltype != LABEL_MANUAL)
1491 main_body = beginningOfMainBody();
1495 if (main_body > 0) {
1498 basefont = getLabelFont(bparams);
1500 basefont = getLayoutFont(bparams);
1503 moving_arg |= style->needprotect;
1505 // Which font is currently active?
1506 LyXFont running_font(basefont);
1507 // Do we have an open font change?
1508 bool open_font = false;
1510 texrow.start(this, 0);
1512 // if the paragraph is empty, the loop will not be entered at all
1514 if (style->isCommand()) {
1519 column += startTeXParParams(bparams, os, moving_arg);
1523 for (pos_type i = 0; i < size(); ++i) {
1525 // First char in paragraph or after label?
1526 if (i == main_body) {
1527 if (main_body > 0) {
1529 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1532 basefont = getLayoutFont(bparams);
1533 running_font = basefont;
1537 if (style->isCommand()) {
1543 column += startTeXParParams(bparams, os,
1547 value_type c = getChar(i);
1549 // Fully instantiated font
1550 LyXFont font = getFont(bparams, i);
1552 LyXFont const last_font = running_font;
1554 // Spaces at end of font change are simulated to be
1555 // outside font change, i.e. we write "\textXX{text} "
1556 // rather than "\textXX{text }". (Asger)
1557 if (open_font && c == ' ' && i <= size() - 2) {
1558 LyXFont const & next_font = getFont(bparams, i + 1);
1559 if (next_font != running_font
1560 && next_font != font) {
1565 // We end font definition before blanks
1567 (font != running_font ||
1568 font.language() != running_font.language()))
1570 column += running_font.latexWriteEndChanges(os,
1572 (i == main_body-1) ? basefont : font);
1573 running_font = basefont;
1577 // Blanks are printed before start of fontswitch
1579 // Do not print the separation of the optional argument
1580 if (i != main_body - 1) {
1581 pimpl_->simpleTeXBlanks(os, texrow, i,
1582 column, font, *style);
1586 // Do we need to change font?
1587 if ((font != running_font ||
1588 font.language() != running_font.language()) &&
1591 column += font.latexWriteStartChanges(os, basefont,
1593 running_font = font;
1597 if (c == Paragraph::META_NEWLINE) {
1598 // newlines are handled differently here than
1599 // the default in SimpleTeXSpecialChars().
1600 if (!style->newline_allowed) {
1604 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1607 basefont = getLayoutFont(bparams);
1608 running_font = basefont;
1609 if (font.family() ==
1610 LyXFont::TYPEWRITER_FAMILY) {
1619 texrow.start(this, i + 1);
1622 pimpl_->simpleTeXSpecialChars(buf, bparams,
1623 os, texrow, moving_arg,
1625 basefont, open_font,
1626 *style, i, column, c);
1630 // If we have an open font definition, we have to close it
1632 #ifdef FIXED_LANGUAGE_END_DETECTION
1635 .latexWriteEndChanges(os, basefont,
1636 next_->getFont(bparams,
1639 running_font.latexWriteEndChanges(os, basefont,
1643 #ifdef WITH_WARNINGS
1644 //#warning For now we ALWAYS have to close the foreign font settings if they are
1645 //#warning there as we start another \selectlanguage with the next paragraph if
1646 //#warning we are in need of this. This should be fixed sometime (Jug)
1648 running_font.latexWriteEndChanges(os, basefont, basefont);
1652 // Needed if there is an optional argument but no contents.
1653 if (main_body > 0 && main_body == size()) {
1655 return_value = false;
1659 column += endTeXParParams(bparams, os, moving_arg);
1662 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1663 return return_value;
1667 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1668 BufferParams const & bparams,
1669 ostream & os, TexRow & texrow)
1671 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1673 LyXLayout_ptr const & style = layout();
1675 Language const * language = getParLanguage(bparams);
1676 Language const * doc_language = bparams.language;
1677 Language const * previous_language = previous_
1678 ? previous_->getParLanguage(bparams) : doc_language;
1679 if (language->babel() != previous_language->babel()) {
1681 if (!lyxrc.language_command_end.empty() &&
1682 previous_language->babel() != doc_language->babel()) {
1683 os << subst(lyxrc.language_command_end, "$$lang",
1684 previous_language->babel())
1689 if (lyxrc.language_command_end.empty() ||
1690 language->babel() != doc_language->babel()) {
1691 os << subst(lyxrc.language_command_begin, "$$lang",
1698 bool leftindent_open = false;
1699 if (!params().leftIndent().zero()) {
1700 os << "\\begin{LyXParagraphLeftIndent}{" <<
1701 params().leftIndent().asLatexString() << "}\n";
1703 leftindent_open = true;
1706 if (style->isEnvironment()) {
1707 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1708 os << "\\begin{" << style->latexname() << "}{"
1709 << params().labelWidthString() << "}\n";
1710 } else if (style->labeltype == LABEL_BIBLIO) {
1712 os << "\\begin{" << style->latexname() << "}{"
1713 << bibitemWidest(buf)
1715 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1716 os << "\\begin{" << style->latexname() << '}'
1717 << style->latexparam() << '\n';
1719 os << "\\begin{" << style->latexname() << '}'
1720 << style->latexparam() << '\n';
1723 Paragraph * par = this;
1725 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1727 if (par && par->params().depth() > params().depth()) {
1728 if (par->layout()->isParagraph()) {
1731 // How to handle this? (Lgb)
1732 //&& !suffixIs(os, "\n\n")
1734 // There should be at least one '\n' already
1735 // but we need there to be two for Standard
1736 // paragraphs that are depth-increment'ed to be
1737 // output correctly. However, tables can
1738 // also be paragraphs so don't adjust them.
1741 // Will it ever harm to have one '\n' too
1742 // many? i.e. that we sometimes will have
1743 // three in a row. (Lgb)
1747 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1750 && par->layout() == layout()
1751 && par->params().depth() == params().depth()
1752 && par->params().leftIndent() == params().leftIndent());
1754 if (style->isEnvironment()) {
1755 os << "\\end{" << style->latexname() << "}\n";
1759 if (leftindent_open) {
1760 os << "\\end{LyXParagraphLeftIndent}\n";
1764 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1765 return par; // ale970302
1769 bool Paragraph::isHfill(pos_type pos) const
1771 return IsHfillChar(getChar(pos));
1775 bool Paragraph::isInset(pos_type pos) const
1777 return IsInsetChar(getChar(pos));
1781 bool Paragraph::isNewline(pos_type pos) const
1783 return pos >= 0 && IsNewlineChar(getChar(pos));
1787 bool Paragraph::isSeparator(pos_type pos) const
1789 return IsSeparatorChar(getChar(pos));
1793 bool Paragraph::isLineSeparator(pos_type pos) const
1795 value_type const c = getChar(pos);
1796 return IsLineSeparatorChar(c)
1797 || (IsInsetChar(c) && getInset(pos) &&
1798 getInset(pos)->isLineSeparator());
1802 bool Paragraph::isKomma(pos_type pos) const
1804 return IsKommaChar(getChar(pos));
1808 /// Used by the spellchecker
1809 bool Paragraph::isLetter(pos_type pos) const
1811 value_type const c = getChar(pos);
1812 if (IsLetterChar(c))
1815 return getInset(pos)->isLetter();
1816 // We want to pass the ' and escape chars to ispell
1817 string const extra = lyxrc.isp_esc_chars + '\'';
1818 return contains(extra, c);
1822 bool Paragraph::isWord(pos_type pos) const
1824 return IsWordChar(getChar(pos)) ;
1829 Paragraph::getParLanguage(BufferParams const & bparams) const
1832 #ifndef INHERIT_LANGUAGE
1833 return getFirstFontSettings().language();
1835 Language const * lang = getFirstFontSettings().language();
1836 #ifdef WITH_WARNINGS
1837 #warning We should make this somewhat better, any ideas? (Jug)
1839 if (lang == inherit_language || lang == ignore_language)
1840 lang = bparams.language;
1843 } else if (previous_)
1844 return previous_->getParLanguage(bparams);
1846 return bparams.language;
1850 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1852 return lyxrc.rtl_support
1853 && getParLanguage(bparams)->RightToLeft()
1854 && !(inInset() && inInset()->owner() &&
1855 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1859 void Paragraph::changeLanguage(BufferParams const & bparams,
1860 Language const * from, Language const * to)
1862 for (pos_type i = 0; i < size(); ++i) {
1863 LyXFont font = getFontSettings(bparams, i);
1864 if (font.language() == from) {
1865 font.setLanguage(to);
1872 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1874 Language const * doc_language = bparams.language;
1875 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1876 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1878 for (; cit != end; ++cit)
1879 if (cit->font().language() != ignore_language &&
1880 cit->font().language() != latex_language &&
1881 #ifdef INHERIT_LANGUAGE
1882 cit->font().language() != inherit_language &&
1884 cit->font().language() != doc_language)
1890 // Convert the paragraph to a string.
1891 // Used for building the table of contents
1892 string const Paragraph::asString(Buffer const * buffer, bool label)
1894 BufferParams const & bparams = buffer->params;
1896 if (label && !params().labelString().empty())
1897 s += params().labelString() + ' ';
1898 string::size_type const len = s.size();
1900 for (pos_type i = 0; i < size(); ++i) {
1901 value_type c = getChar(i);
1904 else if (c == META_INSET &&
1905 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1907 getInset(i)->ascii(buffer, ost);
1908 s += subst(ost.str().c_str(),'\n',' ');
1912 if (isRightToLeftPar(bparams))
1913 reverse(s.begin() + len,s.end());
1919 string const Paragraph::asString(Buffer const * buffer,
1920 pos_type beg, pos_type end, bool label)
1924 if (beg == 0 && label && !params().labelString().empty())
1925 ost << params().labelString() << ' ';
1927 for (pos_type i = beg; i < end; ++i) {
1928 value_type const c = getUChar(buffer->params, i);
1931 else if (c == META_NEWLINE)
1933 else if (c == META_HFILL)
1935 else if (c == META_INSET) {
1936 getInset(i)->ascii(buffer, ost);
1940 return ost.str().c_str();
1944 void Paragraph::setInsetOwner(Inset * i)
1946 pimpl_->inset_owner = i;
1947 InsetList::iterator it = insetlist.begin();
1948 InsetList::iterator end = insetlist.end();
1949 for (; it != end; ++it) {
1951 it.getInset()->setOwner(i);
1956 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1959 insetlist.deleteInsetsLyXText(bv);
1963 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1966 insetlist.resizeInsetsLyXText(bv);
1970 void Paragraph::setContentsFromPar(Paragraph * par)
1972 pimpl_->setContentsFromPar(par);
1976 lyx::pos_type Paragraph::size() const
1978 return pimpl_->size();
1982 bool Paragraph::empty() const
1984 return pimpl_->empty();
1988 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1990 return pimpl_->getChar(pos);
1994 int Paragraph::id() const
2000 LyXLayout_ptr const & Paragraph::layout() const
2006 void Paragraph::layout(LyXLayout_ptr const & new_layout)
2008 layout_ = new_layout;
2012 bool Paragraph::isFirstInSequence() const
2014 Paragraph const * dhook = depthHook(getDepth());
2015 return (dhook == this
2016 || dhook->layout() != layout()
2017 || dhook->getDepth() != getDepth());
2021 Inset * Paragraph::inInset() const
2023 return pimpl_->inset_owner;
2027 void Paragraph::clearContents()
2032 void Paragraph::setChar(pos_type pos, value_type c)
2034 pimpl_->setChar(pos, c);
2038 ParagraphParameters & Paragraph::params()
2040 return pimpl_->params;
2044 ParagraphParameters const & Paragraph::params() const
2046 return pimpl_->params;
2050 Paragraph * Paragraph::getParFromID(int id) const
2052 return pimpl_->getParFromID(id);
2056 bool Paragraph::isFreeSpacing() const
2058 // for now we just need this, later should we need this in some
2059 // other way we can always add a function to Inset::() too.
2060 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2061 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
2066 Counters & Paragraph::counters()
2068 return pimpl_->ctrs;