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 int tex_code_break_column = 72; // needs non-zero initialization. set later.
58 // this is a bad idea, but how can Paragraph find its buffer to get
59 // parameters? (JMarc)
61 extern string bibitemWidest(Buffer const *);
63 // this is a minibuffer
68 LyXFont minibuffer_font;
69 Inset * minibuffer_inset;
74 extern BufferView * current_view;
77 Paragraph::Paragraph()
78 : pimpl_(new Paragraph::Pimpl(this))
84 bibkey = 0; // ale970302
89 // This constructor inserts the new paragraph in a list.
90 Paragraph::Paragraph(Paragraph * par)
91 : pimpl_(new Paragraph::Pimpl(this))
96 // double linked list begin
99 next_->previous_ = this;
101 previous_->next_ = this;
104 bibkey = 0; // ale970302
109 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
110 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
117 // this is because of the dummy layout of the paragraphs that
119 layout_ = lp.layout();
123 bibkey = static_cast<InsetBibKey *>
124 (lp.bibkey->clone(*current_view->buffer()));
129 // copy everything behind the break-position to the new paragraph
130 insetlist = lp.insetlist;
131 for (InsetList::iterator it = insetlist.begin();
132 it != insetlist.end(); ++it)
134 it->inset = it->inset->clone(*current_view->buffer(), same_ids);
135 // tell the new inset who is the boss now
136 it->inset->parOwner(this);
141 // the destructor removes the new paragraph from the list
142 Paragraph::~Paragraph()
145 previous_->next_ = next_;
147 next_->previous_ = previous_;
149 for (InsetList::iterator it = insetlist.begin();
150 it != insetlist.end(); ++it) {
159 //lyxerr << "Paragraph::paragraph_id = "
160 // << Paragraph::paragraph_id << endl;
164 void Paragraph::writeFile(Buffer const * buf, ostream & os,
165 BufferParams const & bparams,
166 depth_type dth) const
168 // The beginning or end of a deeper (i.e. nested) area?
169 if (dth != params().depth()) {
170 if (params().depth() > dth) {
171 while (params().depth() > dth) {
172 os << "\n\\begin_deeper ";
176 while (params().depth() < dth) {
177 os << "\n\\end_deeper ";
183 // First write the layout
184 os << "\n\\layout " << layout()->name() << "\n";
186 // Maybe some vertical spaces.
187 if (params().spaceTop().kind() != VSpace::NONE)
188 os << "\\added_space_top "
189 << params().spaceTop().asLyXCommand() << " ";
190 if (params().spaceBottom().kind() != VSpace::NONE)
191 os << "\\added_space_bottom "
192 << params().spaceBottom().asLyXCommand() << " ";
194 // Maybe the paragraph has special spacing
195 params().spacing().writeFile(os, true);
197 // The labelwidth string used in lists.
198 if (!params().labelWidthString().empty())
199 os << "\\labelwidthstring "
200 << params().labelWidthString() << '\n';
202 // Lines above or below?
203 if (params().lineTop())
205 if (params().lineBottom())
206 os << "\\line_bottom ";
208 // Pagebreaks above or below?
209 if (params().pagebreakTop())
210 os << "\\pagebreak_top ";
211 if (params().pagebreakBottom())
212 os << "\\pagebreak_bottom ";
214 // Start of appendix?
215 if (params().startOfAppendix())
216 os << "\\start_of_appendix ";
219 if (params().noindent())
222 // Do we have a manual left indent?
223 if (!params().leftIndent().zero())
224 os << "\\leftindent " << params().leftIndent().asString() << " ";
227 if (params().align() != LYX_ALIGN_LAYOUT) {
229 switch (params().align()) {
230 case LYX_ALIGN_LEFT: h = 1; break;
231 case LYX_ALIGN_RIGHT: h = 2; break;
232 case LYX_ALIGN_CENTER: h = 3; break;
233 default: h = 0; break;
235 os << "\\align " << string_align[h] << " ";
240 bibkey->write(buf, os);
242 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
245 for (pos_type i = 0; i < size(); ++i) {
251 // Write font changes
252 LyXFont font2 = getFontSettings(bparams, i);
253 if (font2 != font1) {
254 #ifndef INHERIT_LANGUAGE
255 font2.lyxWriteChanges(font1, os);
257 font2.lyxWriteChanges(font1, bparams.language, os);
263 value_type const c = getChar(i);
267 Inset const * inset = getInset(i);
269 if (inset->directWrite()) {
270 // international char, let it write
271 // code directly so it's shorter in
273 inset->write(buf, os);
275 os << "\n\\begin_inset ";
276 inset->write(buf, os);
277 os << "\n\\end_inset \n\n";
283 os << "\n\\newline \n";
287 os << "\n\\hfill \n";
291 os << "\n\\backslash \n";
295 if (i + 1 < size() && getChar(i + 1) == ' ') {
302 if ((column > 70 && c == ' ')
307 // this check is to amend a bug. LyX sometimes
308 // inserts '\0' this could cause problems.
312 lyxerr << "ERROR (Paragraph::writeFile):"
313 " NULL char in structure." << endl;
319 // now write the next paragraph
321 next_->writeFile(buf, os, bparams, dth);
325 void Paragraph::validate(LaTeXFeatures & features) const
327 pimpl_->validate(features, *layout());
331 // First few functions needed for cut and paste and paragraph breaking.
332 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
334 BufferParams bparams = buffer.params;
336 minibuffer_char = getChar(pos);
337 minibuffer_font = getFontSettings(bparams, pos);
338 minibuffer_inset = 0;
339 if (minibuffer_char == Paragraph::META_INSET) {
341 minibuffer_inset = getInset(pos)->clone(buffer);
343 minibuffer_inset = 0;
344 minibuffer_char = ' ';
345 // This reflects what GetInset() does (ARRae)
351 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
353 minibuffer_char = getChar(pos);
354 minibuffer_font = getFontSettings(bparams, pos);
355 minibuffer_inset = 0;
356 if (minibuffer_char == Paragraph::META_INSET) {
358 minibuffer_inset = getInset(pos);
359 // This is a little hack since I want exactly
360 // the inset, not just a clone. Otherwise
361 // the inset would be deleted when calling Erase(pos)
363 InsetList::iterator it = insetlist.begin();
364 InsetList::iterator end = insetlist.end();
365 for (; it != end; ++it) {
370 if (it != end && it->pos == pos)
372 // the inset is not in a paragraph anymore
373 minibuffer_inset->parOwner(0);
375 minibuffer_inset = 0;
376 minibuffer_char = ' ';
377 // This reflects what GetInset() does (ARRae)
382 // Erase(pos); now the caller is responsible for that.
386 bool Paragraph::insertFromMinibuffer(pos_type pos)
388 if (minibuffer_char == Paragraph::META_INSET) {
389 if (!insetAllowed(minibuffer_inset->lyxCode())) {
392 insertInset(pos, minibuffer_inset, minibuffer_font);
394 LyXFont f = minibuffer_font;
395 if (!checkInsertChar(f)) {
398 insertChar(pos, minibuffer_char, f);
406 void Paragraph::erase(pos_type pos)
412 bool Paragraph::checkInsertChar(LyXFont & font)
414 if (pimpl_->inset_owner)
415 return pimpl_->inset_owner->checkInsertChar(font);
420 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
422 LyXFont const f(LyXFont::ALL_INHERIT);
423 insertChar(pos, c, f);
427 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
428 LyXFont const & font)
430 pimpl_->insertChar(pos, c, font);
434 void Paragraph::insertInset(pos_type pos, Inset * inset)
436 LyXFont const f(LyXFont::ALL_INHERIT);
437 insertInset(pos, inset, f);
441 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
443 pimpl_->insertInset(pos, inset, font);
447 bool Paragraph::insetAllowed(Inset::Code code)
449 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
451 if (pimpl_->inset_owner)
452 return pimpl_->inset_owner->insetAllowed(code);
457 Inset * Paragraph::getInset(pos_type pos)
459 lyx::Assert(pos < size());
462 InsetList::iterator it = insetlist.begin();
463 InsetList::iterator end = insetlist.end();
464 for (; it != end; ++it) {
469 if (it != end && it->pos == pos)
472 lyxerr << "ERROR (Paragraph::getInset): "
473 << "Inset does not exist: " << pos << endl;
476 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
477 // Did this commenting out introduce a bug? So far I have not
478 // see any, please enlighten me. (Lgb)
479 // My guess is that since the inset does not exist, we might
480 // as well replace it with a space to prevent craches. (Asger)
485 Inset const * Paragraph::getInset(pos_type pos) const
487 lyx::Assert(pos < size());
490 InsetList::const_iterator cit = insetlist.begin();
491 InsetList::const_iterator end = insetlist.end();
492 for (; cit != end; ++cit) {
497 if (cit != end && cit->pos == pos)
500 lyxerr << "ERROR (Paragraph::getInset): "
501 << "Inset does not exist: " << pos << endl;
503 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
504 // Did this commenting out introduce a bug? So far I have not
505 // see any, please enlighten me. (Lgb)
506 // My guess is that since the inset does not exist, we might
507 // as well replace it with a space to prevent craches. (Asger)
512 // Gets uninstantiated font setting at position.
513 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
516 lyx::Assert(pos <= size());
518 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
519 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
520 for (; cit != end; ++cit) {
521 if (cit->pos() >= pos)
527 retfont = cit->font();
528 } else if (pos == size() && size()) {
529 retfont = getFontSettings(bparams, pos - 1);
531 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
532 #ifdef INHERIT_LANGUAGE
533 if (retfont.language() == inherit_language)
534 retfont.setLanguage(bparams.language);
541 // Gets uninstantiated font setting at position 0
542 LyXFont const Paragraph::getFirstFontSettings() const
544 if (size() > 0 && !pimpl_->fontlist.empty())
545 return pimpl_->fontlist[0].font();
547 return LyXFont(LyXFont::ALL_INHERIT);
551 // Gets the fully instantiated font at a given position in a paragraph
552 // This is basically the same function as LyXText::GetFont() in text2.C.
553 // The difference is that this one is used for generating the LaTeX file,
554 // and thus cosmetic "improvements" are disallowed: This has to deliver
555 // the true picture of the buffer. (Asger)
556 // If position is -1, we get the layout font of the paragraph.
557 // If position is -2, we get the font of the manual label of the paragraph.
558 LyXFont const Paragraph::getFont(BufferParams const & bparams,
561 lyx::Assert(pos >= 0);
563 LyXLayout_ptr const & lout = layout();
565 pos_type main_body = 0;
566 if (lout->labeltype == LABEL_MANUAL)
567 main_body = beginningOfMainBody();
571 layoutfont = lout->labelfont;
573 layoutfont = lout->font;
575 LyXFont tmpfont = getFontSettings(bparams, pos);
576 #ifndef INHERIT_LANGUAGE
577 tmpfont.realize(layoutfont);
579 tmpfont.realize(layoutfont, bparams.language);
582 return pimpl_->realizeFont(tmpfont, bparams);
586 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
588 LyXLayout_ptr const & lout = layout();
590 LyXFont tmpfont = lout->labelfont;
591 tmpfont.setLanguage(getParLanguage(bparams));
593 return pimpl_->realizeFont(tmpfont, bparams);
597 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
599 LyXLayout_ptr const & lout = layout();
601 LyXFont tmpfont = lout->font;
602 tmpfont.setLanguage(getParLanguage(bparams));
604 return pimpl_->realizeFont(tmpfont, bparams);
608 /// Returns the height of the highest font in range
610 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
611 LyXFont::FONT_SIZE const def_size) const
613 if (pimpl_->fontlist.empty())
616 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
617 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
618 for (; end_it != end; ++end_it) {
619 if (end_it->pos() >= endpos)
626 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
627 for (; cit != end; ++cit) {
628 if (cit->pos() >= startpos)
632 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
633 for (; cit != end_it; ++cit) {
634 LyXFont::FONT_SIZE size = cit->font().size();
635 if (size == LyXFont::INHERIT_SIZE)
637 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
644 Paragraph::value_type
645 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
647 value_type c = getChar(pos);
648 if (!lyxrc.rtl_support)
678 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
685 void Paragraph::setFont(pos_type pos, LyXFont const & font)
687 lyx::Assert(pos <= size());
689 // First, reduce font against layout/label font
690 // Update: The SetCharFont() routine in text2.C already
691 // reduces font, so we don't need to do that here. (Asger)
692 // No need to simplify this because it will disappear
693 // in a new kernel. (Asger)
694 // Next search font table
696 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
697 Pimpl::FontList::iterator it = beg;
698 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
699 for (; it != endit; ++it) {
700 if (it->pos() >= pos)
703 unsigned int i = std::distance(beg, it);
704 bool notfound = (it == endit);
706 if (!notfound && pimpl_->fontlist[i].font() == font)
709 bool begin = pos == 0 || notfound ||
710 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
711 // Is position pos is a beginning of a font block?
712 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
713 // Is position pos is the end of a font block?
714 if (begin && end) { // A single char block
715 if (i + 1 < pimpl_->fontlist.size() &&
716 pimpl_->fontlist[i + 1].font() == font) {
717 // Merge the singleton block with the next block
718 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
719 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
720 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
721 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
722 // Merge the singleton block with the previous block
723 pimpl_->fontlist[i - 1].pos(pos);
724 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
726 pimpl_->fontlist[i].font(font);
728 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
729 pimpl_->fontlist[i - 1].pos(pos);
731 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
732 Pimpl::FontTable(pos, font));
734 pimpl_->fontlist[i].pos(pos - 1);
735 if (!(i + 1 < pimpl_->fontlist.size() &&
736 pimpl_->fontlist[i + 1].font() == font))
737 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
738 Pimpl::FontTable(pos, font));
739 } else { // The general case. The block is splitted into 3 blocks
740 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
741 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
742 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
743 Pimpl::FontTable(pos, font));
749 void Paragraph::next(Paragraph * p)
755 // This function is able to hide closed footnotes.
756 Paragraph * Paragraph::next()
762 Paragraph const * Paragraph::next() const
768 void Paragraph::previous(Paragraph * p)
774 // This function is able to hide closed footnotes.
775 Paragraph * Paragraph::previous()
781 // This function is able to hide closed footnotes.
782 Paragraph const * Paragraph::previous() const
788 void Paragraph::breakParagraph(BufferParams const & bparams,
792 // create a new paragraph
793 Paragraph * tmp = new Paragraph(this);
794 tmp->layout(bparams.getLyXTextClass().defaultLayout());
795 // remember to set the inset_owner
796 tmp->setInsetOwner(inInset());
798 // this is an idea for a more userfriendly layout handling, I will
799 // see what the users say
801 // layout stays the same with latex-environments
803 tmp->layout(layout());
804 tmp->setLabelWidthString(params().labelWidthString());
807 bool isempty = (layout()->keepempty && !size());
809 if (!isempty && (size() > pos || !size() || flag == 2)) {
810 tmp->layout(layout());
811 tmp->params().align(params().align());
812 tmp->setLabelWidthString(params().labelWidthString());
814 tmp->params().lineBottom(params().lineBottom());
815 params().lineBottom(false);
816 tmp->params().pagebreakBottom(params().pagebreakBottom());
817 params().pagebreakBottom(false);
818 tmp->params().spaceBottom(params().spaceBottom());
819 params().spaceBottom(VSpace(VSpace::NONE));
821 tmp->params().depth(params().depth());
822 tmp->params().noindent(params().noindent());
824 // copy everything behind the break-position
825 // to the new paragraph
826 pos_type pos_end = size() - 1;
829 for (; i <= pos_end; ++i) {
830 cutIntoMinibuffer(bparams, i);
831 if (tmp->insertFromMinibuffer(j - pos))
834 for (i = pos_end; i >= pos; --i) {
839 // just an idea of me
840 if (!isempty && !pos) {
841 tmp->params().lineTop(params().lineTop());
842 tmp->params().pagebreakTop(params().pagebreakTop());
843 tmp->params().spaceTop(params().spaceTop());
844 tmp->bibkey = bibkey;
849 layout(bparams.getLyXTextClass().defaultLayout());
851 // layout stays the same with latex-environments
853 layout(tmp->layout());
854 setLabelWidthString(tmp->params().labelWidthString());
855 params().depth(tmp->params().depth());
861 void Paragraph::makeSameLayout(Paragraph const * par)
863 layout(par->layout());
865 params() = par->params();
869 int Paragraph::stripLeadingSpaces()
871 if (layout()->free_spacing ||
877 while (size() && (isNewline(0) || isLineSeparator(0))) {
886 bool Paragraph::hasSameLayout(Paragraph const * par) const
889 par->layout() == layout() &&
890 params().sameLayout(par->params());
894 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
897 // create a new paragraph
898 Paragraph * tmp = new Paragraph(this);
899 tmp->makeSameLayout(this);
901 // When can pos > Last()?
902 // I guess pos == Last() is possible.
904 // copy everything behind the break-position to the new
906 pos_type pos_end = size() - 1;
910 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
911 cutIntoMinibuffer(bparams, i);
912 if (tmp->insertFromMinibuffer(j - pos))
916 for (pos_type k = pos_end; k >= pos; --k) {
923 // Be carefull, this does not make any check at all.
924 // This method has wrong name, it combined this par with the next par.
925 // In that sense it is the reverse of break paragraph. (Lgb)
926 void Paragraph::pasteParagraph(BufferParams const & bparams)
928 // copy the next paragraph to this one
929 Paragraph * the_next = next();
931 // first the DTP-stuff
932 params().lineBottom(the_next->params().lineBottom());
933 params().spaceBottom(the_next->params().spaceBottom());
934 params().pagebreakBottom(the_next->params().pagebreakBottom());
936 pos_type pos_end = the_next->pimpl_->size() - 1;
937 pos_type pos_insert = size();
939 // ok, now copy the paragraph
940 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
941 the_next->cutIntoMinibuffer(bparams, i);
942 if (insertFromMinibuffer(pos_insert + j))
946 // delete the next paragraph
947 Paragraph * ppar = the_next->previous_;
948 Paragraph * npar = the_next->next_;
954 int Paragraph::getEndLabel() const
956 Paragraph const * par = this;
957 depth_type par_depth = getDepth();
959 LyXLayout_ptr const & layout = par->layout();
960 int const endlabeltype = layout->endlabeltype;
962 if (endlabeltype != END_LABEL_NO_LABEL) {
966 depth_type const next_depth = next_->getDepth();
967 if (par_depth > next_depth ||
968 (par_depth == next_depth
969 && layout != next_->layout()))
975 par = par->outerHook();
977 par_depth = par->getDepth();
979 return END_LABEL_NO_LABEL;
983 Paragraph::depth_type Paragraph::getDepth() const
985 return params().depth();
989 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
991 bool const isenv = layout()->isEnvironment();
994 return params().depth() + 1;
996 return params().depth();
1000 char Paragraph::getAlign() const
1002 return params().align();
1006 string const & Paragraph::getLabelstring() const
1008 return params().labelString();
1012 int Paragraph::getFirstCounter(int i) const
1014 return pimpl_->counter_[i];
1018 // the next two functions are for the manual labels
1019 string const Paragraph::getLabelWidthString() const
1021 if (!params().labelWidthString().empty())
1022 return params().labelWidthString();
1024 return _("Senseless with this layout!");
1028 void Paragraph::setLabelWidthString(string const & s)
1030 params().labelWidthString(s);
1034 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
1037 params().labelWidthString(string());
1038 params().align(LYX_ALIGN_LAYOUT);
1039 params().spaceTop(VSpace(VSpace::NONE));
1040 params().spaceBottom(VSpace(VSpace::NONE));
1041 params().spacing(Spacing(Spacing::Default));
1045 // if the layout of a paragraph contains a manual label, the beginning of the
1046 // main body is the beginning of the second word. This is what the par-
1047 // function returns. If the layout does not contain a label, the main
1048 // body always starts with position 0. This differentiation is necessary,
1049 // because there cannot be a newline or a blank <= the beginning of the
1050 // main body in TeX.
1052 int Paragraph::beginningOfMainBody() const
1054 // Unroll the first two cycles of the loop
1055 // and remember the previous character to
1056 // remove unnecessary GetChar() calls
1058 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
1060 char previous_char = 0;
1063 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1064 // Yes, this ^ is supposed to be "= " not "=="
1067 && previous_char != ' '
1068 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1070 previous_char = temp;
1079 Paragraph * Paragraph::depthHook(depth_type depth)
1081 Paragraph * newpar = this;
1084 newpar = newpar->previous();
1085 } while (newpar && newpar->getDepth() > depth);
1088 if (previous() || getDepth())
1089 lyxerr << "ERROR (Paragraph::DepthHook): "
1098 Paragraph const * Paragraph::depthHook(depth_type depth) const
1100 Paragraph const * newpar = this;
1103 newpar = newpar->previous();
1104 } while (newpar && newpar->getDepth() > depth);
1107 if (previous() || getDepth())
1108 lyxerr << "ERROR (Paragraph::DepthHook): "
1116 Paragraph * Paragraph::outerHook()
1120 return depthHook(depth_type(getDepth() - 1));
1123 Paragraph const * Paragraph::outerHook() const
1127 return depthHook(depth_type(getDepth() - 1));
1131 Paragraph::inset_iterator
1132 Paragraph::InsetIterator(pos_type pos)
1134 InsetList::iterator it = insetlist.begin();
1135 InsetList::iterator end = insetlist.end();
1136 for (; it != end; ++it) {
1141 return inset_iterator(it);
1145 // returns -1 if inset not found
1146 int Paragraph::getPositionOfInset(Inset const * inset) const
1149 InsetList::const_iterator cit = insetlist.begin();
1150 InsetList::const_iterator end = insetlist.end();
1151 for (; cit != end; ++cit) {
1152 if (cit->inset == inset) {
1156 if (inset == bibkey)
1163 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1164 BufferParams const & bparams,
1165 ostream & os, TexRow & texrow,
1168 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1169 Inset const * in = inInset();
1170 bool further_blank_line = false;
1171 LyXLayout_ptr style;
1173 // well we have to check if we are in an inset with unlimited
1174 // lenght (all in one row) if that is true then we don't allow
1175 // any special options in the paragraph and also we don't allow
1176 // any environment other then "Standard" to be valid!
1177 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1180 if (params().startOfAppendix()) {
1181 os << "\\appendix\n";
1185 if (!params().spacing().isDefault()
1186 && (!previous() || !previous()->hasSameLayout(this))) {
1187 os << params().spacing().writeEnvirBegin() << "\n";
1191 if (tex_code_break_column && style->isCommand()) {
1196 if (params().pagebreakTop()) {
1198 further_blank_line = true;
1200 if (params().spaceTop().kind() != VSpace::NONE) {
1201 os << params().spaceTop().asLatexCommand(bparams);
1202 further_blank_line = true;
1205 if (params().lineTop()) {
1206 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1207 << "\\vspace{-1\\parskip}";
1208 further_blank_line = true;
1211 if (further_blank_line) {
1216 style = bparams.getLyXTextClass().defaultLayout();
1219 Language const * language = getParLanguage(bparams);
1220 Language const * doc_language = bparams.language;
1221 Language const * previous_language = previous()
1222 ? previous()->getParLanguage(bparams) : doc_language;
1224 if (language->babel() != previous_language->babel()
1225 // check if we already put language command in TeXEnvironment()
1226 && !(style->isEnvironment()
1227 && (!previous() || previous()->layout() != layout() ||
1228 previous()->params().depth() != params().depth())))
1230 if (!lyxrc.language_command_end.empty() &&
1231 previous_language->babel() != doc_language->babel())
1233 os << subst(lyxrc.language_command_end, "$$lang",
1234 previous_language->babel())
1239 if (lyxrc.language_command_end.empty() ||
1240 language->babel() != doc_language->babel())
1242 os << subst(lyxrc.language_command_begin, "$$lang",
1249 if (bparams.inputenc == "auto" &&
1250 language->encoding() != previous_language->encoding()) {
1251 os << "\\inputencoding{"
1252 << language->encoding()->LatexName()
1257 switch (style->latextype) {
1260 << style->latexname()
1261 << style->latexparam();
1263 case LATEX_ITEM_ENVIRONMENT:
1265 bibkey->latex(buf, os, false, false);
1269 case LATEX_LIST_ENVIRONMENT:
1276 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1278 // Make sure that \\par is done with the font of the last
1279 // character if this has another size as the default.
1280 // This is necessary because LaTeX (and LyX on the screen)
1281 // calculates the space between the baselines according
1282 // to this font. (Matthias)
1284 // Is this really needed ? (Dekel)
1285 // We do not need to use to change the font for the last paragraph
1286 // or for a command.
1287 LyXFont const font =
1289 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1291 bool is_command = style->isCommand();
1293 if (style->resfont.size() != font.size() && next_ && !is_command) {
1296 os << "\\" << font.latexSize() << " \\par}";
1297 } else if (need_par) {
1299 } else if (is_command)
1302 switch (style->latextype) {
1303 case LATEX_ITEM_ENVIRONMENT:
1304 case LATEX_LIST_ENVIRONMENT:
1305 if (next_ && (params().depth() < next_->params().depth())) {
1310 case LATEX_ENVIRONMENT:
1311 // if its the last paragraph of the current environment
1312 // skip it otherwise fall through
1314 && (next_->layout() != layout()
1315 || next_->params().depth() != params().depth()))
1317 // fall through possible
1319 // we don't need it for the last paragraph!!!
1326 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1327 further_blank_line = false;
1328 if (params().lineBottom()) {
1329 os << "\\lyxline{\\" << font.latexSize() << '}';
1330 further_blank_line = true;
1333 if (params().spaceBottom().kind() != VSpace::NONE) {
1334 os << params().spaceBottom().asLatexCommand(bparams);
1335 further_blank_line = true;
1338 if (params().pagebreakBottom()) {
1340 further_blank_line = true;
1343 if (further_blank_line) {
1348 if (!params().spacing().isDefault()
1349 && (!next_ || !next_->hasSameLayout(this))) {
1350 os << params().spacing().writeEnvirEnd() << "\n";
1355 // we don't need it for the last paragraph!!!
1360 // Since \selectlanguage write the language to the aux file,
1361 // we need to reset the language at the end of footnote or
1364 if (language->babel() != doc_language->babel()) {
1365 if (lyxrc.language_command_end.empty())
1366 os << subst(lyxrc.language_command_begin,
1368 doc_language->babel())
1371 os << subst(lyxrc.language_command_end,
1379 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1383 // This could go to ParagraphParameters if we want to
1384 int Paragraph::startTeXParParams(BufferParams const & bparams,
1385 ostream & os, bool moving_arg) const
1389 if (params().noindent()) {
1390 os << "\\noindent ";
1394 switch (params().align()) {
1395 case LYX_ALIGN_NONE:
1396 case LYX_ALIGN_BLOCK:
1397 case LYX_ALIGN_LAYOUT:
1398 case LYX_ALIGN_SPECIAL:
1400 case LYX_ALIGN_LEFT:
1401 case LYX_ALIGN_RIGHT:
1402 case LYX_ALIGN_CENTER:
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 if (getParLanguage(bparams)->babel() != "hebrew") {
1418 os << "\\begin{flushleft}";
1421 os << "\\begin{flushright}";
1425 case LYX_ALIGN_RIGHT:
1426 if (getParLanguage(bparams)->babel() != "hebrew") {
1427 os << "\\begin{flushright}";
1430 os << "\\begin{flushleft}";
1434 case LYX_ALIGN_CENTER:
1435 os << "\\begin{center}";
1443 // This could go to ParagraphParameters if we want to
1444 int Paragraph::endTeXParParams(BufferParams const & bparams,
1445 ostream & os, bool moving_arg) const
1449 switch (params().align()) {
1450 case LYX_ALIGN_NONE:
1451 case LYX_ALIGN_BLOCK:
1452 case LYX_ALIGN_LAYOUT:
1453 case LYX_ALIGN_SPECIAL:
1455 case LYX_ALIGN_LEFT:
1456 case LYX_ALIGN_RIGHT:
1457 case LYX_ALIGN_CENTER:
1465 switch (params().align()) {
1466 case LYX_ALIGN_NONE:
1467 case LYX_ALIGN_BLOCK:
1468 case LYX_ALIGN_LAYOUT:
1469 case LYX_ALIGN_SPECIAL:
1471 case LYX_ALIGN_LEFT:
1472 if (getParLanguage(bparams)->babel() != "hebrew") {
1473 os << "\\end{flushleft}";
1476 os << "\\end{flushright}";
1480 case LYX_ALIGN_RIGHT:
1481 if (getParLanguage(bparams)->babel() != "hebrew") {
1482 os << "\\end{flushright}";
1485 os << "\\end{flushleft}";
1489 case LYX_ALIGN_CENTER:
1490 os << "\\end{center}";
1498 // This one spits out the text of the paragraph
1499 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1500 BufferParams const & bparams,
1501 ostream & os, TexRow & texrow,
1504 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1506 bool return_value = false;
1508 LyXLayout_ptr style;
1510 // well we have to check if we are in an inset with unlimited
1511 // lenght (all in one row) if that is true then we don't allow
1512 // any special options in the paragraph and also we don't allow
1513 // any environment other then "Standard" to be valid!
1515 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1518 style = bparams.getLyXTextClass().defaultLayout();
1525 // Maybe we have to create a optional argument.
1527 if (style->labeltype != LABEL_MANUAL)
1530 main_body = beginningOfMainBody();
1534 if (main_body > 0) {
1537 basefont = getLabelFont(bparams);
1539 basefont = getLayoutFont(bparams);
1542 moving_arg |= style->needprotect;
1544 // Which font is currently active?
1545 LyXFont running_font(basefont);
1546 // Do we have an open font change?
1547 bool open_font = false;
1549 texrow.start(this, 0);
1551 // if the paragraph is empty, the loop will not be entered at all
1553 if (style->isCommand()) {
1558 column += startTeXParParams(bparams, os, moving_arg);
1562 for (pos_type i = 0; i < size(); ++i) {
1564 // First char in paragraph or after label?
1565 if (i == main_body) {
1566 if (main_body > 0) {
1568 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1571 basefont = getLayoutFont(bparams);
1572 running_font = basefont;
1576 if (style->isCommand()) {
1582 column += startTeXParParams(bparams, os,
1586 value_type c = getChar(i);
1588 // Fully instantiated font
1589 LyXFont font = getFont(bparams, i);
1591 LyXFont const last_font = running_font;
1593 // Spaces at end of font change are simulated to be
1594 // outside font change, i.e. we write "\textXX{text} "
1595 // rather than "\textXX{text }". (Asger)
1596 if (open_font && c == ' ' && i <= size() - 2) {
1597 LyXFont const & next_font = getFont(bparams, i + 1);
1598 if (next_font != running_font
1599 && next_font != font) {
1604 // We end font definition before blanks
1606 (font != running_font ||
1607 font.language() != running_font.language()))
1609 column += running_font.latexWriteEndChanges(os,
1611 (i == main_body-1) ? basefont : font);
1612 running_font = basefont;
1616 // Blanks are printed before start of fontswitch
1618 // Do not print the separation of the optional argument
1619 if (i != main_body - 1) {
1620 pimpl_->simpleTeXBlanks(os, texrow, i,
1621 column, font, *style);
1625 // Do we need to change font?
1626 if ((font != running_font ||
1627 font.language() != running_font.language()) &&
1630 column += font.latexWriteStartChanges(os, basefont,
1632 running_font = font;
1636 if (c == Paragraph::META_NEWLINE) {
1637 // newlines are handled differently here than
1638 // the default in SimpleTeXSpecialChars().
1639 if (!style->newline_allowed) {
1643 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1646 basefont = getLayoutFont(bparams);
1647 running_font = basefont;
1648 if (font.family() ==
1649 LyXFont::TYPEWRITER_FAMILY) {
1658 texrow.start(this, i + 1);
1661 pimpl_->simpleTeXSpecialChars(buf, bparams,
1662 os, texrow, moving_arg,
1664 basefont, open_font,
1665 *style, i, column, c);
1669 // If we have an open font definition, we have to close it
1671 #ifdef FIXED_LANGUAGE_END_DETECTION
1674 .latexWriteEndChanges(os, basefont,
1675 next_->getFont(bparams,
1678 running_font.latexWriteEndChanges(os, basefont,
1682 #ifdef WITH_WARNINGS
1683 //#warning For now we ALWAYS have to close the foreign font settings if they are
1684 //#warning there as we start another \selectlanguage with the next paragraph if
1685 //#warning we are in need of this. This should be fixed sometime (Jug)
1687 running_font.latexWriteEndChanges(os, basefont, basefont);
1691 // Needed if there is an optional argument but no contents.
1692 if (main_body > 0 && main_body == size()) {
1694 return_value = false;
1698 column += endTeXParParams(bparams, os, moving_arg);
1701 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1702 return return_value;
1706 bool Paragraph::sgmlConvertChar(char c, string & sgml_string)
1708 bool retval = false;
1710 case Paragraph::META_HFILL:
1711 sgml_string.erase();
1713 case Paragraph::META_NEWLINE:
1717 sgml_string = "&";
1720 sgml_string = "<";
1723 sgml_string = ">";
1726 sgml_string = "$";
1729 sgml_string = "#";
1732 sgml_string = "%";
1735 sgml_string = "[";
1738 sgml_string = "]";
1741 sgml_string = "{";
1744 sgml_string = "}";
1747 sgml_string = "˜";
1750 sgml_string = """;
1753 sgml_string = "\";
1759 case '\0': // Ignore :-)
1760 sgml_string.erase();
1770 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1771 BufferParams const & bparams,
1772 ostream & os, TexRow & texrow)
1774 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1776 LyXLayout_ptr const & style = layout();
1778 Language const * language = getParLanguage(bparams);
1779 Language const * doc_language = bparams.language;
1780 Language const * previous_language = previous_
1781 ? previous_->getParLanguage(bparams) : doc_language;
1782 if (language->babel() != previous_language->babel()) {
1784 if (!lyxrc.language_command_end.empty() &&
1785 previous_language->babel() != doc_language->babel()) {
1786 os << subst(lyxrc.language_command_end, "$$lang",
1787 previous_language->babel())
1792 if (lyxrc.language_command_end.empty() ||
1793 language->babel() != doc_language->babel()) {
1794 os << subst(lyxrc.language_command_begin, "$$lang",
1801 bool leftindent_open = false;
1802 if (!params().leftIndent().zero()) {
1803 os << "\\begin{LyXParagraphLeftIndent}{" <<
1804 params().leftIndent().asLatexString() << "}\n";
1806 leftindent_open = true;
1809 if (style->isEnvironment()) {
1810 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1811 os << "\\begin{" << style->latexname() << "}{"
1812 << params().labelWidthString() << "}\n";
1813 } else if (style->labeltype == LABEL_BIBLIO) {
1815 os << "\\begin{" << style->latexname() << "}{"
1816 << bibitemWidest(buf)
1818 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1819 os << "\\begin{" << style->latexname() << '}'
1820 << style->latexparam() << '\n';
1822 os << "\\begin{" << style->latexname() << '}'
1823 << style->latexparam() << '\n';
1826 Paragraph * par = this;
1828 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1830 if (par && par->params().depth() > params().depth()) {
1831 if (par->layout()->isParagraph()) {
1834 // How to handle this? (Lgb)
1835 //&& !suffixIs(os, "\n\n")
1837 // There should be at least one '\n' already
1838 // but we need there to be two for Standard
1839 // paragraphs that are depth-increment'ed to be
1840 // output correctly. However, tables can
1841 // also be paragraphs so don't adjust them.
1844 // Will it ever harm to have one '\n' too
1845 // many? i.e. that we sometimes will have
1846 // three in a row. (Lgb)
1850 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1853 && par->layout() == layout()
1854 && par->params().depth() == params().depth()
1855 && par->params().leftIndent() == params().leftIndent());
1857 if (style->isEnvironment()) {
1858 os << "\\end{" << style->latexname() << "}\n";
1862 if (leftindent_open) {
1863 os << "\\end{LyXParagraphLeftIndent}\n";
1867 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1868 return par; // ale970302
1872 bool Paragraph::isHfill(pos_type pos) const
1874 return IsHfillChar(getChar(pos));
1878 bool Paragraph::isInset(pos_type pos) const
1880 return IsInsetChar(getChar(pos));
1884 bool Paragraph::isNewline(pos_type pos) const
1886 return pos >= 0 && IsNewlineChar(getChar(pos));
1890 bool Paragraph::isSeparator(pos_type pos) const
1892 return IsSeparatorChar(getChar(pos));
1896 bool Paragraph::isLineSeparator(pos_type pos) const
1898 value_type const c = getChar(pos);
1899 return IsLineSeparatorChar(c)
1900 || (IsInsetChar(c) && getInset(pos) &&
1901 getInset(pos)->isLineSeparator());
1905 bool Paragraph::isKomma(pos_type pos) const
1907 return IsKommaChar(getChar(pos));
1911 /// Used by the spellchecker
1912 bool Paragraph::isLetter(pos_type pos) const
1914 value_type const c = getChar(pos);
1915 if (IsLetterChar(c))
1918 return getInset(pos)->isLetter();
1919 // We want to pass the ' and escape chars to ispell
1920 string const extra = lyxrc.isp_esc_chars + '\'';
1921 return contains(extra, c);
1925 bool Paragraph::isWord(pos_type pos) const
1927 return IsWordChar(getChar(pos)) ;
1932 Paragraph::getParLanguage(BufferParams const & bparams) const
1935 #ifndef INHERIT_LANGUAGE
1936 return getFirstFontSettings().language();
1938 Language const * lang = getFirstFontSettings().language();
1939 #ifdef WITH_WARNINGS
1940 #warning We should make this somewhat better, any ideas? (Jug)
1942 if (lang == inherit_language || lang == ignore_language)
1943 lang = bparams.language;
1946 } else if (previous_)
1947 return previous_->getParLanguage(bparams);
1949 return bparams.language;
1953 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1955 return lyxrc.rtl_support
1956 && getParLanguage(bparams)->RightToLeft()
1957 && !(inInset() && inInset()->owner() &&
1958 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1962 void Paragraph::changeLanguage(BufferParams const & bparams,
1963 Language const * from, Language const * to)
1965 for (pos_type i = 0; i < size(); ++i) {
1966 LyXFont font = getFontSettings(bparams, i);
1967 if (font.language() == from) {
1968 font.setLanguage(to);
1975 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1977 Language const * doc_language = bparams.language;
1978 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1979 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1981 for (; cit != end; ++cit)
1982 if (cit->font().language() != ignore_language &&
1983 cit->font().language() != latex_language &&
1984 #ifdef INHERIT_LANGUAGE
1985 cit->font().language() != inherit_language &&
1987 cit->font().language() != doc_language)
1993 // Convert the paragraph to a string.
1994 // Used for building the table of contents
1995 string const Paragraph::asString(Buffer const * buffer, bool label)
1997 BufferParams const & bparams = buffer->params;
1999 if (label && !params().labelString().empty())
2000 s += params().labelString() + ' ';
2001 string::size_type const len = s.size();
2003 for (pos_type i = 0; i < size(); ++i) {
2004 value_type c = getChar(i);
2007 else if (c == META_INSET &&
2008 getInset(i)->lyxCode() == Inset::MATH_CODE) {
2010 getInset(i)->ascii(buffer, ost);
2011 s += subst(ost.str().c_str(),'\n',' ');
2015 if (isRightToLeftPar(bparams))
2016 reverse(s.begin() + len,s.end());
2022 string const Paragraph::asString(Buffer const * buffer,
2023 pos_type beg, pos_type end, bool label)
2027 if (beg == 0 && label && !params().labelString().empty())
2028 ost << params().labelString() << ' ';
2030 for (pos_type i = beg; i < end; ++i) {
2031 value_type const c = getUChar(buffer->params, i);
2034 else if (c == META_NEWLINE)
2036 else if (c == META_HFILL)
2038 else if (c == META_INSET) {
2039 getInset(i)->ascii(buffer, ost);
2043 return ost.str().c_str();
2047 void Paragraph::setInsetOwner(Inset * i)
2049 pimpl_->inset_owner = i;
2050 InsetList::const_iterator cit = insetlist.begin();
2051 InsetList::const_iterator end = insetlist.end();
2052 for (; cit != end; ++cit) {
2054 cit->inset->setOwner(i);
2059 void Paragraph::deleteInsetsLyXText(BufferView * bv)
2062 InsetList::const_iterator cit = insetlist.begin();
2063 InsetList::const_iterator end = insetlist.end();
2064 for (; cit != end; ++cit) {
2065 if (cit->inset && cit->inset->isTextInset()) {
2066 static_cast<UpdatableInset *>
2067 (cit->inset)->deleteLyXText(bv, true);
2073 void Paragraph::resizeInsetsLyXText(BufferView * bv)
2076 InsetList::const_iterator cit = insetlist.begin();
2077 InsetList::const_iterator end = insetlist.end();
2078 for (; cit != end; ++cit) {
2080 if (cit->inset->isTextInset()) {
2081 static_cast<UpdatableInset *>
2082 (cit->inset)->resizeLyXText(bv, true);
2089 void Paragraph::setContentsFromPar(Paragraph * par)
2091 pimpl_->setContentsFromPar(par);
2095 lyx::pos_type Paragraph::size() const
2097 return pimpl_->size();
2101 Paragraph::value_type Paragraph::getChar(pos_type pos) const
2103 return pimpl_->getChar(pos);
2107 int Paragraph::id() const
2113 void Paragraph::id(int id_arg)
2115 pimpl_->id_ = id_arg;
2119 LyXLayout_ptr const & Paragraph::layout() const
2125 void Paragraph::layout(LyXLayout_ptr const & new_layout)
2127 layout_ = new_layout;
2131 bool Paragraph::isFirstInSequence() const
2133 Paragraph const * dhook = depthHook(getDepth());
2134 return (dhook == this
2135 || dhook->layout() != layout()
2136 || dhook->getDepth() != getDepth());
2140 Inset * Paragraph::inInset() const
2142 return pimpl_->inset_owner;
2146 void Paragraph::clearContents()
2151 void Paragraph::setChar(pos_type pos, value_type c)
2153 pimpl_->setChar(pos, c);
2157 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2162 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2164 return inset_iterator(insetlist.begin());
2168 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2170 return inset_iterator(insetlist.end());
2174 ParagraphParameters & Paragraph::params()
2176 return pimpl_->params;
2180 ParagraphParameters const & Paragraph::params() const
2182 return pimpl_->params;
2186 Paragraph * Paragraph::getParFromID(int id) const
2188 return pimpl_->getParFromID(id);
2192 bool Paragraph::isFreeSpacing() const
2194 // for now we just need this, later should we need this in some
2195 // other way we can always add a function to Inset::() too.
2196 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2197 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);