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->inset = it->inset->clone(*current_view->buffer(), same_ids);
134 // tell the new inset who is the boss now
135 it->inset->parOwner(this);
140 // the destructor removes the new paragraph from the list
141 Paragraph::~Paragraph()
144 previous_->next_ = next_;
146 next_->previous_ = previous_;
148 for (InsetList::iterator it = insetlist.begin();
149 it != insetlist.end(); ++it) {
158 //lyxerr << "Paragraph::paragraph_id = "
159 // << Paragraph::paragraph_id << endl;
163 void Paragraph::writeFile(Buffer const * buf, ostream & os,
164 BufferParams const & bparams,
165 depth_type dth) const
167 // The beginning or end of a deeper (i.e. nested) area?
168 if (dth != params().depth()) {
169 if (params().depth() > dth) {
170 while (params().depth() > dth) {
171 os << "\n\\begin_deeper ";
175 while (params().depth() < dth) {
176 os << "\n\\end_deeper ";
182 // First write the layout
183 os << "\n\\layout " << layout()->name() << "\n";
185 // Maybe some vertical spaces.
186 if (params().spaceTop().kind() != VSpace::NONE)
187 os << "\\added_space_top "
188 << params().spaceTop().asLyXCommand() << " ";
189 if (params().spaceBottom().kind() != VSpace::NONE)
190 os << "\\added_space_bottom "
191 << params().spaceBottom().asLyXCommand() << " ";
193 // Maybe the paragraph has special spacing
194 params().spacing().writeFile(os, true);
196 // The labelwidth string used in lists.
197 if (!params().labelWidthString().empty())
198 os << "\\labelwidthstring "
199 << params().labelWidthString() << '\n';
201 // Lines above or below?
202 if (params().lineTop())
204 if (params().lineBottom())
205 os << "\\line_bottom ";
207 // Pagebreaks above or below?
208 if (params().pagebreakTop())
209 os << "\\pagebreak_top ";
210 if (params().pagebreakBottom())
211 os << "\\pagebreak_bottom ";
213 // Start of appendix?
214 if (params().startOfAppendix())
215 os << "\\start_of_appendix ";
218 if (params().noindent())
221 // Do we have a manual left indent?
222 if (!params().leftIndent().zero())
223 os << "\\leftindent " << params().leftIndent().asString() << " ";
226 if (params().align() != LYX_ALIGN_LAYOUT) {
228 switch (params().align()) {
229 case LYX_ALIGN_LEFT: h = 1; break;
230 case LYX_ALIGN_RIGHT: h = 2; break;
231 case LYX_ALIGN_CENTER: h = 3; break;
232 default: h = 0; break;
234 os << "\\align " << string_align[h] << " ";
239 bibkey->write(buf, os);
241 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
244 for (pos_type i = 0; i < size(); ++i) {
250 // Write font changes
251 LyXFont font2 = getFontSettings(bparams, i);
252 if (font2 != font1) {
253 #ifndef INHERIT_LANGUAGE
254 font2.lyxWriteChanges(font1, os);
256 font2.lyxWriteChanges(font1, bparams.language, os);
262 value_type const c = getChar(i);
266 Inset const * inset = getInset(i);
268 if (inset->directWrite()) {
269 // international char, let it write
270 // code directly so it's shorter in
272 inset->write(buf, os);
274 os << "\n\\begin_inset ";
275 inset->write(buf, os);
276 os << "\n\\end_inset \n\n";
282 os << "\n\\newline \n";
286 os << "\n\\hfill \n";
290 os << "\n\\backslash \n";
294 if (i + 1 < size() && getChar(i + 1) == ' ') {
301 if ((column > 70 && c == ' ')
306 // this check is to amend a bug. LyX sometimes
307 // inserts '\0' this could cause problems.
311 lyxerr << "ERROR (Paragraph::writeFile):"
312 " NULL char in structure." << endl;
318 // now write the next paragraph
320 next_->writeFile(buf, os, bparams, dth);
324 void Paragraph::validate(LaTeXFeatures & features) const
326 pimpl_->validate(features, *layout());
330 // First few functions needed for cut and paste and paragraph breaking.
331 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
333 BufferParams bparams = buffer.params;
335 minibuffer_char = getChar(pos);
336 minibuffer_font = getFontSettings(bparams, pos);
337 minibuffer_inset = 0;
338 if (minibuffer_char == Paragraph::META_INSET) {
340 minibuffer_inset = getInset(pos)->clone(buffer);
342 minibuffer_inset = 0;
343 minibuffer_char = ' ';
344 // This reflects what GetInset() does (ARRae)
350 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
352 minibuffer_char = getChar(pos);
353 minibuffer_font = getFontSettings(bparams, pos);
354 minibuffer_inset = 0;
355 if (minibuffer_char == Paragraph::META_INSET) {
357 minibuffer_inset = getInset(pos);
358 // This is a little hack since I want exactly
359 // the inset, not just a clone. Otherwise
360 // the inset would be deleted when calling Erase(pos)
362 InsetList::iterator it = insetlist.begin();
363 InsetList::iterator end = insetlist.end();
364 for (; it != end; ++it) {
369 if (it != end && it->pos == pos)
371 // the inset is not in a paragraph anymore
372 minibuffer_inset->parOwner(0);
374 minibuffer_inset = 0;
375 minibuffer_char = ' ';
376 // This reflects what GetInset() does (ARRae)
381 // Erase(pos); now the caller is responsible for that.
385 bool Paragraph::insertFromMinibuffer(pos_type pos)
387 if (minibuffer_char == Paragraph::META_INSET) {
388 if (!insetAllowed(minibuffer_inset->lyxCode())) {
391 insertInset(pos, minibuffer_inset, minibuffer_font);
393 LyXFont f = minibuffer_font;
394 if (!checkInsertChar(f)) {
397 insertChar(pos, minibuffer_char, f);
405 void Paragraph::erase(pos_type pos)
411 bool Paragraph::checkInsertChar(LyXFont & font)
413 if (pimpl_->inset_owner)
414 return pimpl_->inset_owner->checkInsertChar(font);
419 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
421 LyXFont const f(LyXFont::ALL_INHERIT);
422 insertChar(pos, c, f);
426 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
427 LyXFont const & font)
429 pimpl_->insertChar(pos, c, font);
433 void Paragraph::insertInset(pos_type pos, Inset * inset)
435 LyXFont const f(LyXFont::ALL_INHERIT);
436 insertInset(pos, inset, f);
440 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
442 pimpl_->insertInset(pos, inset, font);
446 bool Paragraph::insetAllowed(Inset::Code code)
448 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
450 if (pimpl_->inset_owner)
451 return pimpl_->inset_owner->insetAllowed(code);
456 Inset * Paragraph::getInset(pos_type pos)
458 lyx::Assert(pos < size());
461 InsetList::iterator it = insetlist.begin();
462 InsetList::iterator end = insetlist.end();
463 for (; it != end; ++it) {
468 if (it != end && it->pos == pos)
471 lyxerr << "ERROR (Paragraph::getInset): "
472 << "Inset does not exist: " << pos << endl;
475 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
476 // Did this commenting out introduce a bug? So far I have not
477 // see any, please enlighten me. (Lgb)
478 // My guess is that since the inset does not exist, we might
479 // as well replace it with a space to prevent craches. (Asger)
484 Inset const * Paragraph::getInset(pos_type pos) const
486 lyx::Assert(pos < size());
489 InsetList::const_iterator cit = insetlist.begin();
490 InsetList::const_iterator end = insetlist.end();
491 for (; cit != end; ++cit) {
496 if (cit != end && cit->pos == pos)
499 lyxerr << "ERROR (Paragraph::getInset): "
500 << "Inset does not exist: " << pos << endl;
502 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
503 // Did this commenting out introduce a bug? So far I have not
504 // see any, please enlighten me. (Lgb)
505 // My guess is that since the inset does not exist, we might
506 // as well replace it with a space to prevent craches. (Asger)
511 // Gets uninstantiated font setting at position.
512 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
515 lyx::Assert(pos <= size());
517 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
518 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
519 for (; cit != end; ++cit) {
520 if (cit->pos() >= pos)
526 retfont = cit->font();
527 } else if (pos == size() && !empty()) {
528 retfont = getFontSettings(bparams, pos - 1);
530 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
531 #ifdef INHERIT_LANGUAGE
532 if (retfont.language() == inherit_language)
533 retfont.setLanguage(bparams.language);
540 // Gets uninstantiated font setting at position 0
541 LyXFont const Paragraph::getFirstFontSettings() const
543 if (!empty() && !pimpl_->fontlist.empty())
544 return pimpl_->fontlist[0].font();
546 return LyXFont(LyXFont::ALL_INHERIT);
550 // Gets the fully instantiated font at a given position in a paragraph
551 // This is basically the same function as LyXText::GetFont() in text2.C.
552 // The difference is that this one is used for generating the LaTeX file,
553 // and thus cosmetic "improvements" are disallowed: This has to deliver
554 // the true picture of the buffer. (Asger)
555 // If position is -1, we get the layout font of the paragraph.
556 // If position is -2, we get the font of the manual label of the paragraph.
557 LyXFont const Paragraph::getFont(BufferParams const & bparams,
560 lyx::Assert(pos >= 0);
562 LyXLayout_ptr const & lout = layout();
564 pos_type main_body = 0;
565 if (lout->labeltype == LABEL_MANUAL)
566 main_body = beginningOfMainBody();
570 layoutfont = lout->labelfont;
572 layoutfont = lout->font;
574 LyXFont tmpfont = getFontSettings(bparams, pos);
575 #ifndef INHERIT_LANGUAGE
576 tmpfont.realize(layoutfont);
578 tmpfont.realize(layoutfont, bparams.language);
581 return pimpl_->realizeFont(tmpfont, bparams);
585 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
587 LyXLayout_ptr const & lout = layout();
589 LyXFont tmpfont = lout->labelfont;
590 tmpfont.setLanguage(getParLanguage(bparams));
592 return pimpl_->realizeFont(tmpfont, bparams);
596 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
598 LyXLayout_ptr const & lout = layout();
600 LyXFont tmpfont = lout->font;
601 tmpfont.setLanguage(getParLanguage(bparams));
603 return pimpl_->realizeFont(tmpfont, bparams);
607 /// Returns the height of the highest font in range
609 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
610 LyXFont::FONT_SIZE const def_size) const
612 if (pimpl_->fontlist.empty())
615 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
616 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
617 for (; end_it != end; ++end_it) {
618 if (end_it->pos() >= endpos)
625 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
626 for (; cit != end; ++cit) {
627 if (cit->pos() >= startpos)
631 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
632 for (; cit != end_it; ++cit) {
633 LyXFont::FONT_SIZE size = cit->font().size();
634 if (size == LyXFont::INHERIT_SIZE)
636 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
643 Paragraph::value_type
644 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
646 value_type c = getChar(pos);
647 if (!lyxrc.rtl_support)
677 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
684 void Paragraph::setFont(pos_type pos, LyXFont const & font)
686 lyx::Assert(pos <= size());
688 // First, reduce font against layout/label font
689 // Update: The SetCharFont() routine in text2.C already
690 // reduces font, so we don't need to do that here. (Asger)
691 // No need to simplify this because it will disappear
692 // in a new kernel. (Asger)
693 // Next search font table
695 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
696 Pimpl::FontList::iterator it = beg;
697 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
698 for (; it != endit; ++it) {
699 if (it->pos() >= pos)
702 unsigned int i = std::distance(beg, it);
703 bool notfound = (it == endit);
705 if (!notfound && pimpl_->fontlist[i].font() == font)
708 bool begin = pos == 0 || notfound ||
709 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
710 // Is position pos is a beginning of a font block?
711 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
712 // Is position pos is the end of a font block?
713 if (begin && end) { // A single char block
714 if (i + 1 < pimpl_->fontlist.size() &&
715 pimpl_->fontlist[i + 1].font() == font) {
716 // Merge the singleton block with the next block
717 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
718 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
719 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
720 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
721 // Merge the singleton block with the previous block
722 pimpl_->fontlist[i - 1].pos(pos);
723 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
725 pimpl_->fontlist[i].font(font);
727 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
728 pimpl_->fontlist[i - 1].pos(pos);
730 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
731 Pimpl::FontTable(pos, font));
733 pimpl_->fontlist[i].pos(pos - 1);
734 if (!(i + 1 < pimpl_->fontlist.size() &&
735 pimpl_->fontlist[i + 1].font() == font))
736 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
737 Pimpl::FontTable(pos, font));
738 } else { // The general case. The block is splitted into 3 blocks
739 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
740 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
741 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
742 Pimpl::FontTable(pos, font));
748 void Paragraph::next(Paragraph * p)
754 // This function is able to hide closed footnotes.
755 Paragraph * Paragraph::next()
761 Paragraph const * Paragraph::next() const
767 void Paragraph::previous(Paragraph * p)
773 // This function is able to hide closed footnotes.
774 Paragraph * Paragraph::previous()
780 // This function is able to hide closed footnotes.
781 Paragraph const * Paragraph::previous() const
787 void Paragraph::breakParagraph(BufferParams const & bparams,
791 // create a new paragraph
792 Paragraph * tmp = new Paragraph(this);
793 tmp->layout(bparams.getLyXTextClass().defaultLayout());
794 // remember to set the inset_owner
795 tmp->setInsetOwner(inInset());
797 // this is an idea for a more userfriendly layout handling, I will
798 // see what the users say
800 // layout stays the same with latex-environments
802 tmp->layout(layout());
803 tmp->setLabelWidthString(params().labelWidthString());
806 bool isempty = (layout()->keepempty && empty());
808 if (!isempty && (size() > pos || empty() || flag == 2)) {
809 tmp->layout(layout());
810 tmp->params().align(params().align());
811 tmp->setLabelWidthString(params().labelWidthString());
813 tmp->params().lineBottom(params().lineBottom());
814 params().lineBottom(false);
815 tmp->params().pagebreakBottom(params().pagebreakBottom());
816 params().pagebreakBottom(false);
817 tmp->params().spaceBottom(params().spaceBottom());
818 params().spaceBottom(VSpace(VSpace::NONE));
820 tmp->params().depth(params().depth());
821 tmp->params().noindent(params().noindent());
823 // copy everything behind the break-position
824 // to the new paragraph
825 pos_type pos_end = size() - 1;
828 for (; i <= pos_end; ++i) {
829 cutIntoMinibuffer(bparams, i);
830 if (tmp->insertFromMinibuffer(j - pos))
833 for (i = pos_end; i >= pos; --i) {
838 // just an idea of me
839 if (!isempty && !pos) {
840 tmp->params().lineTop(params().lineTop());
841 tmp->params().pagebreakTop(params().pagebreakTop());
842 tmp->params().spaceTop(params().spaceTop());
843 tmp->bibkey = bibkey;
848 layout(bparams.getLyXTextClass().defaultLayout());
850 // layout stays the same with latex-environments
852 layout(tmp->layout());
853 setLabelWidthString(tmp->params().labelWidthString());
854 params().depth(tmp->params().depth());
860 void Paragraph::makeSameLayout(Paragraph const * par)
862 layout(par->layout());
864 params() = par->params();
868 int Paragraph::stripLeadingSpaces()
870 if (layout()->free_spacing ||
876 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
885 bool Paragraph::hasSameLayout(Paragraph const * par) const
888 par->layout() == layout() &&
889 params().sameLayout(par->params());
893 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
896 // create a new paragraph
897 Paragraph * tmp = new Paragraph(this);
898 tmp->makeSameLayout(this);
900 // When can pos > Last()?
901 // I guess pos == Last() is possible.
903 // copy everything behind the break-position to the new
905 pos_type pos_end = size() - 1;
909 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
910 cutIntoMinibuffer(bparams, i);
911 if (tmp->insertFromMinibuffer(j - pos))
915 for (pos_type k = pos_end; k >= pos; --k) {
922 // Be carefull, this does not make any check at all.
923 // This method has wrong name, it combined this par with the next par.
924 // In that sense it is the reverse of break paragraph. (Lgb)
925 void Paragraph::pasteParagraph(BufferParams const & bparams)
927 // copy the next paragraph to this one
928 Paragraph * the_next = next();
930 // first the DTP-stuff
931 params().lineBottom(the_next->params().lineBottom());
932 params().spaceBottom(the_next->params().spaceBottom());
933 params().pagebreakBottom(the_next->params().pagebreakBottom());
935 pos_type pos_end = the_next->pimpl_->size() - 1;
936 pos_type pos_insert = size();
938 // ok, now copy the paragraph
939 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
940 the_next->cutIntoMinibuffer(bparams, i);
941 if (insertFromMinibuffer(pos_insert + j))
945 // delete the next paragraph
946 Paragraph * ppar = the_next->previous_;
947 Paragraph * npar = the_next->next_;
953 int Paragraph::getEndLabel() const
955 Paragraph const * par = this;
956 depth_type par_depth = getDepth();
958 LyXLayout_ptr const & layout = par->layout();
959 int const endlabeltype = layout->endlabeltype;
961 if (endlabeltype != END_LABEL_NO_LABEL) {
965 depth_type const next_depth = next_->getDepth();
966 if (par_depth > next_depth ||
967 (par_depth == next_depth
968 && layout != next_->layout()))
974 par = par->outerHook();
976 par_depth = par->getDepth();
978 return END_LABEL_NO_LABEL;
982 Paragraph::depth_type Paragraph::getDepth() const
984 return params().depth();
988 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
990 bool const isenv = layout()->isEnvironment();
993 return params().depth() + 1;
995 return params().depth();
999 char Paragraph::getAlign() const
1001 return params().align();
1005 string const & Paragraph::getLabelstring() const
1007 return params().labelString();
1011 int Paragraph::getFirstCounter(int i) const
1013 return pimpl_->counter_[i];
1017 // the next two functions are for the manual labels
1018 string const Paragraph::getLabelWidthString() const
1020 if (!params().labelWidthString().empty())
1021 return params().labelWidthString();
1023 return _("Senseless with this layout!");
1027 void Paragraph::setLabelWidthString(string const & s)
1029 params().labelWidthString(s);
1033 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
1036 params().labelWidthString(string());
1037 params().align(LYX_ALIGN_LAYOUT);
1038 params().spaceTop(VSpace(VSpace::NONE));
1039 params().spaceBottom(VSpace(VSpace::NONE));
1040 params().spacing(Spacing(Spacing::Default));
1044 // if the layout of a paragraph contains a manual label, the beginning of the
1045 // main body is the beginning of the second word. This is what the par-
1046 // function returns. If the layout does not contain a label, the main
1047 // body always starts with position 0. This differentiation is necessary,
1048 // because there cannot be a newline or a blank <= the beginning of the
1049 // main body in TeX.
1051 int Paragraph::beginningOfMainBody() const
1053 // Unroll the first two cycles of the loop
1054 // and remember the previous character to
1055 // remove unnecessary GetChar() calls
1057 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
1059 char previous_char = 0;
1062 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1063 // Yes, this ^ is supposed to be "= " not "=="
1066 && previous_char != ' '
1067 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1069 previous_char = temp;
1078 Paragraph * Paragraph::depthHook(depth_type depth)
1080 Paragraph * newpar = this;
1083 newpar = newpar->previous();
1084 } while (newpar && newpar->getDepth() > depth);
1087 if (previous() || getDepth())
1088 lyxerr << "ERROR (Paragraph::DepthHook): "
1097 Paragraph const * Paragraph::depthHook(depth_type depth) const
1099 Paragraph const * newpar = this;
1102 newpar = newpar->previous();
1103 } while (newpar && newpar->getDepth() > depth);
1106 if (previous() || getDepth())
1107 lyxerr << "ERROR (Paragraph::DepthHook): "
1115 Paragraph * Paragraph::outerHook()
1119 return depthHook(depth_type(getDepth() - 1));
1122 Paragraph const * Paragraph::outerHook() const
1126 return depthHook(depth_type(getDepth() - 1));
1130 Paragraph::inset_iterator
1131 Paragraph::InsetIterator(pos_type pos)
1133 InsetList::iterator it = insetlist.begin();
1134 InsetList::iterator end = insetlist.end();
1135 for (; it != end; ++it) {
1140 return inset_iterator(it);
1144 // returns -1 if inset not found
1145 int Paragraph::getPositionOfInset(Inset const * inset) const
1148 InsetList::const_iterator cit = insetlist.begin();
1149 InsetList::const_iterator end = insetlist.end();
1150 for (; cit != end; ++cit) {
1151 if (cit->inset == inset) {
1155 if (inset == bibkey)
1162 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1163 BufferParams const & bparams,
1164 ostream & os, TexRow & texrow,
1167 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1168 Inset const * in = inInset();
1169 bool further_blank_line = false;
1170 LyXLayout_ptr style;
1172 // well we have to check if we are in an inset with unlimited
1173 // lenght (all in one row) if that is true then we don't allow
1174 // any special options in the paragraph and also we don't allow
1175 // any environment other then "Standard" to be valid!
1176 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1179 if (params().startOfAppendix()) {
1180 os << "\\appendix\n";
1184 if (!params().spacing().isDefault()
1185 && (!previous() || !previous()->hasSameLayout(this))) {
1186 os << params().spacing().writeEnvirBegin() << "\n";
1190 if (style->isCommand()) {
1195 if (params().pagebreakTop()) {
1197 further_blank_line = true;
1199 if (params().spaceTop().kind() != VSpace::NONE) {
1200 os << params().spaceTop().asLatexCommand(bparams);
1201 further_blank_line = true;
1204 if (params().lineTop()) {
1205 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1206 << "\\vspace{-1\\parskip}";
1207 further_blank_line = true;
1210 if (further_blank_line) {
1215 style = bparams.getLyXTextClass().defaultLayout();
1218 Language const * language = getParLanguage(bparams);
1219 Language const * doc_language = bparams.language;
1220 Language const * previous_language = previous()
1221 ? previous()->getParLanguage(bparams) : doc_language;
1223 if (language->babel() != previous_language->babel()
1224 // check if we already put language command in TeXEnvironment()
1225 && !(style->isEnvironment()
1226 && (!previous() || previous()->layout() != layout() ||
1227 previous()->params().depth() != params().depth())))
1229 if (!lyxrc.language_command_end.empty() &&
1230 previous_language->babel() != doc_language->babel())
1232 os << subst(lyxrc.language_command_end, "$$lang",
1233 previous_language->babel())
1238 if (lyxrc.language_command_end.empty() ||
1239 language->babel() != doc_language->babel())
1241 os << subst(lyxrc.language_command_begin, "$$lang",
1248 if (bparams.inputenc == "auto" &&
1249 language->encoding() != previous_language->encoding()) {
1250 os << "\\inputencoding{"
1251 << language->encoding()->LatexName()
1256 switch (style->latextype) {
1259 << style->latexname()
1260 << style->latexparam();
1262 case LATEX_ITEM_ENVIRONMENT:
1264 bibkey->latex(buf, os, false, false);
1268 case LATEX_LIST_ENVIRONMENT:
1275 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1277 // Make sure that \\par is done with the font of the last
1278 // character if this has another size as the default.
1279 // This is necessary because LaTeX (and LyX on the screen)
1280 // calculates the space between the baselines according
1281 // to this font. (Matthias)
1283 // Is this really needed ? (Dekel)
1284 // We do not need to use to change the font for the last paragraph
1285 // or for a command.
1286 LyXFont const font =
1288 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1290 bool is_command = style->isCommand();
1292 if (style->resfont.size() != font.size() && next_ && !is_command) {
1295 os << "\\" << font.latexSize() << " \\par}";
1296 } else if (need_par) {
1298 } else if (is_command)
1301 switch (style->latextype) {
1302 case LATEX_ITEM_ENVIRONMENT:
1303 case LATEX_LIST_ENVIRONMENT:
1304 if (next_ && (params().depth() < next_->params().depth())) {
1309 case LATEX_ENVIRONMENT:
1310 // if its the last paragraph of the current environment
1311 // skip it otherwise fall through
1313 && (next_->layout() != layout()
1314 || next_->params().depth() != params().depth()))
1316 // fall through possible
1318 // we don't need it for the last paragraph!!!
1325 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1326 further_blank_line = false;
1327 if (params().lineBottom()) {
1328 os << "\\lyxline{\\" << font.latexSize() << '}';
1329 further_blank_line = true;
1332 if (params().spaceBottom().kind() != VSpace::NONE) {
1333 os << params().spaceBottom().asLatexCommand(bparams);
1334 further_blank_line = true;
1337 if (params().pagebreakBottom()) {
1339 further_blank_line = true;
1342 if (further_blank_line) {
1347 if (!params().spacing().isDefault()
1348 && (!next_ || !next_->hasSameLayout(this))) {
1349 os << params().spacing().writeEnvirEnd() << "\n";
1354 // we don't need it for the last paragraph!!!
1359 // Since \selectlanguage write the language to the aux file,
1360 // we need to reset the language at the end of footnote or
1363 if (language->babel() != doc_language->babel()) {
1364 if (lyxrc.language_command_end.empty())
1365 os << subst(lyxrc.language_command_begin,
1367 doc_language->babel())
1370 os << subst(lyxrc.language_command_end,
1378 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1382 // This could go to ParagraphParameters if we want to
1383 int Paragraph::startTeXParParams(BufferParams const & bparams,
1384 ostream & os, bool moving_arg) const
1388 if (params().noindent()) {
1389 os << "\\noindent ";
1393 switch (params().align()) {
1394 case LYX_ALIGN_NONE:
1395 case LYX_ALIGN_BLOCK:
1396 case LYX_ALIGN_LAYOUT:
1397 case LYX_ALIGN_SPECIAL:
1399 case LYX_ALIGN_LEFT:
1400 case LYX_ALIGN_RIGHT:
1401 case LYX_ALIGN_CENTER:
1409 switch (params().align()) {
1410 case LYX_ALIGN_NONE:
1411 case LYX_ALIGN_BLOCK:
1412 case LYX_ALIGN_LAYOUT:
1413 case LYX_ALIGN_SPECIAL:
1415 case LYX_ALIGN_LEFT:
1416 if (getParLanguage(bparams)->babel() != "hebrew") {
1417 os << "\\begin{flushleft}";
1420 os << "\\begin{flushright}";
1424 case LYX_ALIGN_RIGHT:
1425 if (getParLanguage(bparams)->babel() != "hebrew") {
1426 os << "\\begin{flushright}";
1429 os << "\\begin{flushleft}";
1433 case LYX_ALIGN_CENTER:
1434 os << "\\begin{center}";
1442 // This could go to ParagraphParameters if we want to
1443 int Paragraph::endTeXParParams(BufferParams const & bparams,
1444 ostream & os, bool moving_arg) const
1448 switch (params().align()) {
1449 case LYX_ALIGN_NONE:
1450 case LYX_ALIGN_BLOCK:
1451 case LYX_ALIGN_LAYOUT:
1452 case LYX_ALIGN_SPECIAL:
1454 case LYX_ALIGN_LEFT:
1455 case LYX_ALIGN_RIGHT:
1456 case LYX_ALIGN_CENTER:
1464 switch (params().align()) {
1465 case LYX_ALIGN_NONE:
1466 case LYX_ALIGN_BLOCK:
1467 case LYX_ALIGN_LAYOUT:
1468 case LYX_ALIGN_SPECIAL:
1470 case LYX_ALIGN_LEFT:
1471 if (getParLanguage(bparams)->babel() != "hebrew") {
1472 os << "\\end{flushleft}";
1475 os << "\\end{flushright}";
1479 case LYX_ALIGN_RIGHT:
1480 if (getParLanguage(bparams)->babel() != "hebrew") {
1481 os << "\\end{flushright}";
1484 os << "\\end{flushleft}";
1488 case LYX_ALIGN_CENTER:
1489 os << "\\end{center}";
1497 // This one spits out the text of the paragraph
1498 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1499 BufferParams const & bparams,
1500 ostream & os, TexRow & texrow,
1503 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1505 bool return_value = false;
1507 LyXLayout_ptr style;
1509 // well we have to check if we are in an inset with unlimited
1510 // lenght (all in one row) if that is true then we don't allow
1511 // any special options in the paragraph and also we don't allow
1512 // any environment other then "Standard" to be valid!
1514 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1517 style = bparams.getLyXTextClass().defaultLayout();
1524 // Maybe we have to create a optional argument.
1526 if (style->labeltype != LABEL_MANUAL)
1529 main_body = beginningOfMainBody();
1533 if (main_body > 0) {
1536 basefont = getLabelFont(bparams);
1538 basefont = getLayoutFont(bparams);
1541 moving_arg |= style->needprotect;
1543 // Which font is currently active?
1544 LyXFont running_font(basefont);
1545 // Do we have an open font change?
1546 bool open_font = false;
1548 texrow.start(this, 0);
1550 // if the paragraph is empty, the loop will not be entered at all
1552 if (style->isCommand()) {
1557 column += startTeXParParams(bparams, os, moving_arg);
1561 for (pos_type i = 0; i < size(); ++i) {
1563 // First char in paragraph or after label?
1564 if (i == main_body) {
1565 if (main_body > 0) {
1567 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1570 basefont = getLayoutFont(bparams);
1571 running_font = basefont;
1575 if (style->isCommand()) {
1581 column += startTeXParParams(bparams, os,
1585 value_type c = getChar(i);
1587 // Fully instantiated font
1588 LyXFont font = getFont(bparams, i);
1590 LyXFont const last_font = running_font;
1592 // Spaces at end of font change are simulated to be
1593 // outside font change, i.e. we write "\textXX{text} "
1594 // rather than "\textXX{text }". (Asger)
1595 if (open_font && c == ' ' && i <= size() - 2) {
1596 LyXFont const & next_font = getFont(bparams, i + 1);
1597 if (next_font != running_font
1598 && next_font != font) {
1603 // We end font definition before blanks
1605 (font != running_font ||
1606 font.language() != running_font.language()))
1608 column += running_font.latexWriteEndChanges(os,
1610 (i == main_body-1) ? basefont : font);
1611 running_font = basefont;
1615 // Blanks are printed before start of fontswitch
1617 // Do not print the separation of the optional argument
1618 if (i != main_body - 1) {
1619 pimpl_->simpleTeXBlanks(os, texrow, i,
1620 column, font, *style);
1624 // Do we need to change font?
1625 if ((font != running_font ||
1626 font.language() != running_font.language()) &&
1629 column += font.latexWriteStartChanges(os, basefont,
1631 running_font = font;
1635 if (c == Paragraph::META_NEWLINE) {
1636 // newlines are handled differently here than
1637 // the default in SimpleTeXSpecialChars().
1638 if (!style->newline_allowed) {
1642 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1645 basefont = getLayoutFont(bparams);
1646 running_font = basefont;
1647 if (font.family() ==
1648 LyXFont::TYPEWRITER_FAMILY) {
1657 texrow.start(this, i + 1);
1660 pimpl_->simpleTeXSpecialChars(buf, bparams,
1661 os, texrow, moving_arg,
1663 basefont, open_font,
1664 *style, i, column, c);
1668 // If we have an open font definition, we have to close it
1670 #ifdef FIXED_LANGUAGE_END_DETECTION
1673 .latexWriteEndChanges(os, basefont,
1674 next_->getFont(bparams,
1677 running_font.latexWriteEndChanges(os, basefont,
1681 #ifdef WITH_WARNINGS
1682 //#warning For now we ALWAYS have to close the foreign font settings if they are
1683 //#warning there as we start another \selectlanguage with the next paragraph if
1684 //#warning we are in need of this. This should be fixed sometime (Jug)
1686 running_font.latexWriteEndChanges(os, basefont, basefont);
1690 // Needed if there is an optional argument but no contents.
1691 if (main_body > 0 && main_body == size()) {
1693 return_value = false;
1697 column += endTeXParParams(bparams, os, moving_arg);
1700 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1701 return return_value;
1705 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1706 BufferParams const & bparams,
1707 ostream & os, TexRow & texrow)
1709 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1711 LyXLayout_ptr const & style = layout();
1713 Language const * language = getParLanguage(bparams);
1714 Language const * doc_language = bparams.language;
1715 Language const * previous_language = previous_
1716 ? previous_->getParLanguage(bparams) : doc_language;
1717 if (language->babel() != previous_language->babel()) {
1719 if (!lyxrc.language_command_end.empty() &&
1720 previous_language->babel() != doc_language->babel()) {
1721 os << subst(lyxrc.language_command_end, "$$lang",
1722 previous_language->babel())
1727 if (lyxrc.language_command_end.empty() ||
1728 language->babel() != doc_language->babel()) {
1729 os << subst(lyxrc.language_command_begin, "$$lang",
1736 bool leftindent_open = false;
1737 if (!params().leftIndent().zero()) {
1738 os << "\\begin{LyXParagraphLeftIndent}{" <<
1739 params().leftIndent().asLatexString() << "}\n";
1741 leftindent_open = true;
1744 if (style->isEnvironment()) {
1745 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1746 os << "\\begin{" << style->latexname() << "}{"
1747 << params().labelWidthString() << "}\n";
1748 } else if (style->labeltype == LABEL_BIBLIO) {
1750 os << "\\begin{" << style->latexname() << "}{"
1751 << bibitemWidest(buf)
1753 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1754 os << "\\begin{" << style->latexname() << '}'
1755 << style->latexparam() << '\n';
1757 os << "\\begin{" << style->latexname() << '}'
1758 << style->latexparam() << '\n';
1761 Paragraph * par = this;
1763 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1765 if (par && par->params().depth() > params().depth()) {
1766 if (par->layout()->isParagraph()) {
1769 // How to handle this? (Lgb)
1770 //&& !suffixIs(os, "\n\n")
1772 // There should be at least one '\n' already
1773 // but we need there to be two for Standard
1774 // paragraphs that are depth-increment'ed to be
1775 // output correctly. However, tables can
1776 // also be paragraphs so don't adjust them.
1779 // Will it ever harm to have one '\n' too
1780 // many? i.e. that we sometimes will have
1781 // three in a row. (Lgb)
1785 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1788 && par->layout() == layout()
1789 && par->params().depth() == params().depth()
1790 && par->params().leftIndent() == params().leftIndent());
1792 if (style->isEnvironment()) {
1793 os << "\\end{" << style->latexname() << "}\n";
1797 if (leftindent_open) {
1798 os << "\\end{LyXParagraphLeftIndent}\n";
1802 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1803 return par; // ale970302
1807 bool Paragraph::isHfill(pos_type pos) const
1809 return IsHfillChar(getChar(pos));
1813 bool Paragraph::isInset(pos_type pos) const
1815 return IsInsetChar(getChar(pos));
1819 bool Paragraph::isNewline(pos_type pos) const
1821 return pos >= 0 && IsNewlineChar(getChar(pos));
1825 bool Paragraph::isSeparator(pos_type pos) const
1827 return IsSeparatorChar(getChar(pos));
1831 bool Paragraph::isLineSeparator(pos_type pos) const
1833 value_type const c = getChar(pos);
1834 return IsLineSeparatorChar(c)
1835 || (IsInsetChar(c) && getInset(pos) &&
1836 getInset(pos)->isLineSeparator());
1840 bool Paragraph::isKomma(pos_type pos) const
1842 return IsKommaChar(getChar(pos));
1846 /// Used by the spellchecker
1847 bool Paragraph::isLetter(pos_type pos) const
1849 value_type const c = getChar(pos);
1850 if (IsLetterChar(c))
1853 return getInset(pos)->isLetter();
1854 // We want to pass the ' and escape chars to ispell
1855 string const extra = lyxrc.isp_esc_chars + '\'';
1856 return contains(extra, c);
1860 bool Paragraph::isWord(pos_type pos) const
1862 return IsWordChar(getChar(pos)) ;
1867 Paragraph::getParLanguage(BufferParams const & bparams) const
1870 #ifndef INHERIT_LANGUAGE
1871 return getFirstFontSettings().language();
1873 Language const * lang = getFirstFontSettings().language();
1874 #ifdef WITH_WARNINGS
1875 #warning We should make this somewhat better, any ideas? (Jug)
1877 if (lang == inherit_language || lang == ignore_language)
1878 lang = bparams.language;
1881 } else if (previous_)
1882 return previous_->getParLanguage(bparams);
1884 return bparams.language;
1888 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1890 return lyxrc.rtl_support
1891 && getParLanguage(bparams)->RightToLeft()
1892 && !(inInset() && inInset()->owner() &&
1893 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1897 void Paragraph::changeLanguage(BufferParams const & bparams,
1898 Language const * from, Language const * to)
1900 for (pos_type i = 0; i < size(); ++i) {
1901 LyXFont font = getFontSettings(bparams, i);
1902 if (font.language() == from) {
1903 font.setLanguage(to);
1910 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1912 Language const * doc_language = bparams.language;
1913 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1914 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1916 for (; cit != end; ++cit)
1917 if (cit->font().language() != ignore_language &&
1918 cit->font().language() != latex_language &&
1919 #ifdef INHERIT_LANGUAGE
1920 cit->font().language() != inherit_language &&
1922 cit->font().language() != doc_language)
1928 // Convert the paragraph to a string.
1929 // Used for building the table of contents
1930 string const Paragraph::asString(Buffer const * buffer, bool label)
1932 BufferParams const & bparams = buffer->params;
1934 if (label && !params().labelString().empty())
1935 s += params().labelString() + ' ';
1936 string::size_type const len = s.size();
1938 for (pos_type i = 0; i < size(); ++i) {
1939 value_type c = getChar(i);
1942 else if (c == META_INSET &&
1943 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1945 getInset(i)->ascii(buffer, ost);
1946 s += subst(ost.str().c_str(),'\n',' ');
1950 if (isRightToLeftPar(bparams))
1951 reverse(s.begin() + len,s.end());
1957 string const Paragraph::asString(Buffer const * buffer,
1958 pos_type beg, pos_type end, bool label)
1962 if (beg == 0 && label && !params().labelString().empty())
1963 ost << params().labelString() << ' ';
1965 for (pos_type i = beg; i < end; ++i) {
1966 value_type const c = getUChar(buffer->params, i);
1969 else if (c == META_NEWLINE)
1971 else if (c == META_HFILL)
1973 else if (c == META_INSET) {
1974 getInset(i)->ascii(buffer, ost);
1978 return ost.str().c_str();
1982 void Paragraph::setInsetOwner(Inset * i)
1984 pimpl_->inset_owner = i;
1985 InsetList::const_iterator cit = insetlist.begin();
1986 InsetList::const_iterator end = insetlist.end();
1987 for (; cit != end; ++cit) {
1989 cit->inset->setOwner(i);
1994 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1997 InsetList::const_iterator cit = insetlist.begin();
1998 InsetList::const_iterator end = insetlist.end();
1999 for (; cit != end; ++cit) {
2000 if (cit->inset && cit->inset->isTextInset()) {
2001 static_cast<UpdatableInset *>
2002 (cit->inset)->deleteLyXText(bv, true);
2008 void Paragraph::resizeInsetsLyXText(BufferView * bv)
2011 InsetList::const_iterator cit = insetlist.begin();
2012 InsetList::const_iterator end = insetlist.end();
2013 for (; cit != end; ++cit) {
2015 if (cit->inset->isTextInset()) {
2016 static_cast<UpdatableInset *>
2017 (cit->inset)->resizeLyXText(bv, true);
2024 void Paragraph::setContentsFromPar(Paragraph * par)
2026 pimpl_->setContentsFromPar(par);
2030 lyx::pos_type Paragraph::size() const
2032 return pimpl_->size();
2036 bool Paragraph::empty() const
2038 return pimpl_->empty();
2042 Paragraph::value_type Paragraph::getChar(pos_type pos) const
2044 return pimpl_->getChar(pos);
2048 int Paragraph::id() const
2054 LyXLayout_ptr const & Paragraph::layout() const
2060 void Paragraph::layout(LyXLayout_ptr const & new_layout)
2062 layout_ = new_layout;
2066 bool Paragraph::isFirstInSequence() const
2068 Paragraph const * dhook = depthHook(getDepth());
2069 return (dhook == this
2070 || dhook->layout() != layout()
2071 || dhook->getDepth() != getDepth());
2075 Inset * Paragraph::inInset() const
2077 return pimpl_->inset_owner;
2081 void Paragraph::clearContents()
2086 void Paragraph::setChar(pos_type pos, value_type c)
2088 pimpl_->setChar(pos, c);
2092 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2097 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2099 return inset_iterator(insetlist.begin());
2103 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2105 return inset_iterator(insetlist.end());
2109 ParagraphParameters & Paragraph::params()
2111 return pimpl_->params;
2115 ParagraphParameters const & Paragraph::params() const
2117 return pimpl_->params;
2121 Paragraph * Paragraph::getParFromID(int id) const
2123 return pimpl_->getParFromID(id);
2127 bool Paragraph::isFreeSpacing() const
2129 // for now we just need this, later should we need this in some
2130 // other way we can always add a function to Inset::() too.
2131 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2132 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
2137 Counters & Paragraph::counters()
2139 return pimpl_->ctrs;