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
21 #include "paragraph.h"
22 #include "paragraph_pimpl.h"
23 #include "support/textutils.h"
27 #include "tex-strings.h"
29 #include "bufferparams.h"
30 #include "support/FileInfo.h"
31 #include "support/LAssert.h"
33 #include "LaTeXFeatures.h"
34 #include "insets/insetinclude.h"
35 #include "insets/insetbib.h"
36 #include "insets/insettext.h"
37 #include "support/filetools.h"
38 #include "support/lstrings.h"
39 #include "lyx_gui_misc.h"
41 #include "support/lyxmanip.h"
42 #include "BufferView.h"
44 #include "ParameterStruct.h"
51 using std::lower_bound;
52 using std::upper_bound;
55 int tex_code_break_column = 72; // needs non-zero initialization. set later.
56 // this is a bad idea, but how can Paragraph find its buffer to get
57 // parameters? (JMarc)
59 extern string bibitemWidest(Buffer const *);
61 // this is a minibuffer
66 LyXFont minibuffer_font;
67 Inset * minibuffer_inset;
72 extern BufferView * current_view;
75 Paragraph::Paragraph()
76 : pimpl_(new Paragraph::Pimpl(this))
78 for (int i = 0; i < 10; ++i) setCounter(i , 0);
83 bibkey = 0; // ale970302
88 // This konstruktor inserts the new paragraph in a list.
89 Paragraph::Paragraph(Paragraph * par)
90 : pimpl_(new Paragraph::Pimpl(this))
92 for (int i = 0; i < 10; ++i)
97 // double linked list begin
100 next_->previous_ = this;
102 previous_->next_ = this;
105 bibkey = 0; // ale970302
111 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
112 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
114 for (int i = 0; i < 10; ++i)
121 // this is because of the dummy layout of the paragraphs that
127 bibkey = static_cast<InsetBibKey *>
128 (lp.bibkey->clone(*current_view->buffer()));
133 // copy everything behind the break-position to the new paragraph
135 insetlist = lp.insetlist;
136 for (InsetList::iterator it = insetlist.begin();
137 it != insetlist.end(); ++it)
139 it->inset = it->inset->clone(*current_view->buffer(), same_ids);
144 // the destructor removes the new paragraph from the list
145 Paragraph::~Paragraph()
148 previous_->next_ = next_;
150 next_->previous_ = previous_;
152 for (InsetList::iterator it = insetlist.begin();
153 it != insetlist.end(); ++it) {
162 //lyxerr << "Paragraph::paragraph_id = "
163 // << Paragraph::paragraph_id << endl;
167 void Paragraph::writeFile(Buffer const * buf, ostream & os,
168 BufferParams const & bparams,
169 depth_type dth) const
171 // The beginning or end of a deeper (i.e. nested) area?
172 if (dth != params().depth()) {
173 if (params().depth() > dth) {
174 while (params().depth() > dth) {
175 os << "\n\\begin_deeper ";
179 while (params().depth() < dth) {
180 os << "\n\\end_deeper ";
186 // First write the layout
188 << textclasslist.NameOfLayout(bparams.textclass, layout)
191 // Maybe some vertical spaces.
192 if (params().spaceTop().kind() != VSpace::NONE)
193 os << "\\added_space_top "
194 << params().spaceTop().asLyXCommand() << " ";
195 if (params().spaceBottom().kind() != VSpace::NONE)
196 os << "\\added_space_bottom "
197 << params().spaceBottom().asLyXCommand() << " ";
199 // Maybe the paragraph has special spacing
200 params().spacing().writeFile(os, true);
202 // The labelwidth string used in lists.
203 if (!params().labelWidthString().empty())
204 os << "\\labelwidthstring "
205 << params().labelWidthString() << '\n';
207 // Lines above or below?
208 if (params().lineTop())
210 if (params().lineBottom())
211 os << "\\line_bottom ";
213 // Pagebreaks above or below?
214 if (params().pagebreakTop())
215 os << "\\pagebreak_top ";
216 if (params().pagebreakBottom())
217 os << "\\pagebreak_bottom ";
219 // Start of appendix?
220 if (params().startOfAppendix())
221 os << "\\start_of_appendix ";
224 if (params().noindent())
228 if (params().align() != LYX_ALIGN_LAYOUT) {
230 switch (params().align()) {
231 case LYX_ALIGN_LEFT: h = 1; break;
232 case LYX_ALIGN_RIGHT: h = 2; break;
233 case LYX_ALIGN_CENTER: h = 3; break;
234 default: h = 0; break;
236 os << "\\align " << string_align[h] << " ";
241 bibkey->write(buf, os);
243 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
246 for (size_type i = 0; i < size(); ++i) {
252 // Write font changes
253 LyXFont font2 = getFontSettings(bparams, i);
254 if (font2 != font1) {
255 #ifndef INHERIT_LANGUAGE
256 font2.lyxWriteChanges(font1, os);
258 font2.lyxWriteChanges(font1, bparams.language, os);
264 value_type const c = getChar(i);
268 Inset const * inset = getInset(i);
270 if (inset->directWrite()) {
271 // international char, let it write
272 // code directly so it's shorter in
274 inset->write(buf, os);
276 os << "\n\\begin_inset ";
277 inset->write(buf, os);
278 os << "\n\\end_inset \n\n";
284 os << "\n\\newline \n";
288 os << "\n\\hfill \n";
292 os << "\n\\backslash \n";
296 if (i + 1 < size() && getChar(i + 1) == ' ') {
303 if ((column > 70 && c == ' ')
308 // this check is to amend a bug. LyX sometimes
309 // inserts '\0' this could cause problems.
313 lyxerr << "ERROR (Paragraph::writeFile):"
314 " NULL char in structure." << endl;
320 // now write the next paragraph
322 next_->writeFile(buf, os, bparams, dth);
326 void Paragraph::validate(LaTeXFeatures & features) const
328 BufferParams const & bparams = features.bufferParams();
331 if (params().lineTop() || params().lineBottom())
332 features.lyxline = true;
333 if (!params().spacing().isDefault())
334 features.setspace = true;
337 features.layout[getLayout()] = true;
340 Language const * doc_language = bparams.language;
342 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
343 cit != pimpl_->fontlist.end(); ++cit) {
344 if (cit->font().noun() == LyXFont::ON) {
345 lyxerr[Debug::LATEX] << "font.noun: "
346 << cit->font().noun()
348 features.noun = true;
349 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
350 << cit->font().stateText(0)
353 switch (cit->font().color()) {
355 case LColor::inherit:
357 // probably we should put here all interface colors used for
358 // font displaying! For now I just add this ones I know of (Jug)
363 features.color = true;
364 lyxerr[Debug::LATEX] << "Color enabled. Font: "
365 << cit->font().stateText(0)
369 Language const * language = cit->font().language();
370 if (language->babel() != doc_language->babel() &&
371 language != ignore_language &&
372 #ifdef INHERIT_LANGUAGE
373 language != inherit_language &&
375 language != latex_language)
377 features.UsedLanguages.insert(language);
378 lyxerr[Debug::LATEX] << "Found language "
379 << language->babel() << endl;
384 LyXLayout const & layout =
385 textclasslist.Style(bparams.textclass,
388 for (InsetList::const_iterator cit = insetlist.begin();
389 cit != insetlist.end(); ++cit) {
391 cit->inset->validate(features);
392 if (layout.needprotect &&
393 cit->inset->lyxCode() == Inset::FOOT_CODE)
394 features.NeedLyXFootnoteCode = true;
400 // First few functions needed for cut and paste and paragraph breaking.
401 void Paragraph::copyIntoMinibuffer(Buffer const & buffer,
402 Paragraph::size_type pos) const
404 BufferParams bparams = buffer.params;
406 minibuffer_char = getChar(pos);
407 minibuffer_font = getFontSettings(bparams, pos);
408 minibuffer_inset = 0;
409 if (minibuffer_char == Paragraph::META_INSET) {
411 minibuffer_inset = getInset(pos)->clone(buffer);
413 minibuffer_inset = 0;
414 minibuffer_char = ' ';
415 // This reflects what GetInset() does (ARRae)
421 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
422 Paragraph::size_type pos)
424 minibuffer_char = getChar(pos);
425 minibuffer_font = getFontSettings(bparams, pos);
426 minibuffer_inset = 0;
427 if (minibuffer_char == Paragraph::META_INSET) {
429 minibuffer_inset = getInset(pos);
430 // This is a little hack since I want exactly
431 // the inset, not just a clone. Otherwise
432 // the inset would be deleted when calling Erase(pos)
434 InsetTable search_elem(pos, 0);
435 InsetList::iterator it =
436 lower_bound(insetlist.begin(),
438 search_elem, Pimpl::matchIT());
439 if (it != insetlist.end() && it->pos == pos)
442 minibuffer_inset = 0;
443 minibuffer_char = ' ';
444 // This reflects what GetInset() does (ARRae)
449 // Erase(pos); now the caller is responsible for that.
453 bool Paragraph::insertFromMinibuffer(Paragraph::size_type pos)
455 if ((minibuffer_char == Paragraph::META_INSET) &&
456 !insetAllowed(minibuffer_inset->lyxCode()))
458 if (minibuffer_char == Paragraph::META_INSET)
459 insertInset(pos, minibuffer_inset, minibuffer_font);
461 LyXFont f = minibuffer_font;
462 if (checkInsertChar(f))
463 insertChar(pos, minibuffer_char, f);
472 void Paragraph::clear()
481 void Paragraph::erase(Paragraph::size_type pos)
487 bool Paragraph::checkInsertChar(LyXFont & font)
489 if (pimpl_->inset_owner)
490 return pimpl_->inset_owner->checkInsertChar(font);
495 void Paragraph::insertChar(Paragraph::size_type pos,
496 Paragraph::value_type c)
498 LyXFont const f(LyXFont::ALL_INHERIT);
499 insertChar(pos, c, f);
503 void Paragraph::insertChar(Paragraph::size_type pos,
504 Paragraph::value_type c,
505 LyXFont const & font)
507 pimpl_->insertChar(pos, c, font);
511 void Paragraph::insertInset(Paragraph::size_type pos,
514 LyXFont const f(LyXFont::ALL_INHERIT);
515 insertInset(pos, inset, f);
519 void Paragraph::insertInset(Paragraph::size_type pos,
520 Inset * inset, LyXFont const & font)
522 pimpl_->insertInset(pos, inset, font);
526 bool Paragraph::insetAllowed(Inset::Code code)
528 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
530 if (pimpl_->inset_owner)
531 return pimpl_->inset_owner->insetAllowed(code);
536 Inset * Paragraph::getInset(Paragraph::size_type pos)
538 lyx::Assert(pos < size());
541 InsetTable search_inset(pos, 0);
542 InsetList::iterator it = lower_bound(insetlist.begin(),
544 search_inset, Pimpl::matchIT());
545 if (it != insetlist.end() && it->pos == pos)
548 lyxerr << "ERROR (Paragraph::getInset): "
549 "Inset does not exist: " << pos << endl;
552 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
553 // Did this commenting out introduce a bug? So far I have not
554 // see any, please enlighten me. (Lgb)
555 // My guess is that since the inset does not exist, we might
556 // as well replace it with a space to prevent craches. (Asger)
561 Inset const * Paragraph::getInset(Paragraph::size_type pos) const
563 lyx::Assert(pos < size());
566 InsetTable search_inset(pos, 0);
567 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
569 search_inset, Pimpl::matchIT());
570 if (cit != insetlist.end() && cit->pos == pos)
573 lyxerr << "ERROR (Paragraph::GetInset): "
574 "Inset does not exist: " << pos << endl;
576 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
577 // Did this commenting out introduce a bug? So far I have not
578 // see any, please enlighten me. (Lgb)
579 // My guess is that since the inset does not exist, we might
580 // as well replace it with a space to prevent craches. (Asger)
585 // Gets uninstantiated font setting at position.
586 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
587 Paragraph::size_type pos) const
589 lyx::Assert(pos <= size());
591 Pimpl::FontTable search_font(pos, LyXFont());
592 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
593 pimpl_->fontlist.end(),
594 search_font, Pimpl::matchFT());
596 if (cit != pimpl_->fontlist.end()) {
597 retfont = cit->font();
598 } else if (pos == size() && size()) {
599 retfont = getFontSettings(bparams, pos - 1);
601 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
602 #ifdef INHERIT_LANGUAGE
603 if (retfont.language() == inherit_language)
604 retfont.setLanguage(bparams.language);
611 // Gets uninstantiated font setting at position 0
612 LyXFont const Paragraph::getFirstFontSettings() const
615 if (!pimpl_->fontlist.empty())
616 return pimpl_->fontlist[0].font();
619 return LyXFont(LyXFont::ALL_INHERIT);
623 // Gets the fully instantiated font at a given position in a paragraph
624 // This is basically the same function as LyXText::GetFont() in text2.C.
625 // The difference is that this one is used for generating the LaTeX file,
626 // and thus cosmetic "improvements" are disallowed: This has to deliver
627 // the true picture of the buffer. (Asger)
628 // If position is -1, we get the layout font of the paragraph.
629 // If position is -2, we get the font of the manual label of the paragraph.
630 LyXFont const Paragraph::getFont(BufferParams const & bparams,
631 Paragraph::size_type pos) const
633 lyx::Assert(pos >= 0);
635 LyXLayout const & layout =
636 textclasslist.Style(bparams.textclass,
638 Paragraph::size_type main_body = 0;
639 if (layout.labeltype == LABEL_MANUAL)
640 main_body = beginningOfMainBody();
644 layoutfont = layout.labelfont;
646 layoutfont = layout.font;
648 LyXFont tmpfont = getFontSettings(bparams, pos);
649 #ifndef INHERIT_LANGUAGE
650 tmpfont.realize(layoutfont);
652 tmpfont.realize(layoutfont, bparams.language);
655 return pimpl_->realizeFont(tmpfont, bparams);
659 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
661 LyXLayout const & layout =
662 textclasslist.Style(bparams.textclass, getLayout());
664 LyXFont tmpfont = layout.labelfont;
665 tmpfont.setLanguage(getParLanguage(bparams));
667 return pimpl_->realizeFont(tmpfont, bparams);
671 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
673 LyXLayout const & layout =
674 textclasslist.Style(bparams.textclass,
677 LyXFont tmpfont = layout.font;
678 tmpfont.setLanguage(getParLanguage(bparams));
680 return pimpl_->realizeFont(tmpfont, bparams);
684 /// Returns the height of the highest font in range
686 Paragraph::highestFontInRange(Paragraph::size_type startpos,
687 Paragraph::size_type endpos,
688 LyXFont::FONT_SIZE const def_size) const
690 if (pimpl_->fontlist.empty())
693 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
694 Pimpl::FontTable end_search(endpos, LyXFont());
695 Pimpl::FontList::const_iterator end_it =
696 lower_bound(pimpl_->fontlist.begin(),
697 pimpl_->fontlist.end(),
698 end_search, Pimpl::matchFT());
699 if (end_it != pimpl_->fontlist.end())
702 Pimpl::FontTable start_search(startpos, LyXFont());
703 Pimpl::FontList::const_iterator cit =
704 lower_bound(pimpl_->fontlist.begin(),
705 pimpl_->fontlist.end(),
706 start_search, Pimpl::matchFT());
708 for (; cit != end_it; ++cit) {
709 LyXFont::FONT_SIZE size = cit->font().size();
710 if (size == LyXFont::INHERIT_SIZE)
712 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
719 Paragraph::value_type
720 Paragraph::getUChar(BufferParams const & bparams,
721 Paragraph::size_type pos) const
723 value_type c = getChar(pos);
724 if (!lyxrc.rtl_support)
754 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
761 void Paragraph::setFont(Paragraph::size_type pos,
762 LyXFont const & font)
764 lyx::Assert(pos <= size());
766 // First, reduce font against layout/label font
767 // Update: The SetCharFont() routine in text2.C already
768 // reduces font, so we don't need to do that here. (Asger)
769 // No need to simplify this because it will disappear
770 // in a new kernel. (Asger)
771 // Next search font table
773 Pimpl::FontTable search_font(pos, LyXFont());
774 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
775 pimpl_->fontlist.end(),
776 search_font, Pimpl::matchFT());
777 unsigned int i = it - pimpl_->fontlist.begin();
778 bool notfound = it == pimpl_->fontlist.end();
780 if (!notfound && pimpl_->fontlist[i].font() == font)
783 bool begin = pos == 0 || notfound ||
784 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
785 // Is position pos is a beginning of a font block?
786 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
787 // Is position pos is the end of a font block?
788 if (begin && end) { // A single char block
789 if (i + 1 < pimpl_->fontlist.size() &&
790 pimpl_->fontlist[i + 1].font() == font) {
791 // Merge the singleton block with the next block
792 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
793 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
794 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
795 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
796 // Merge the singleton block with the previous block
797 pimpl_->fontlist[i - 1].pos(pos);
798 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
800 pimpl_->fontlist[i].font(font);
802 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
803 pimpl_->fontlist[i - 1].pos(pos);
805 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
806 Pimpl::FontTable(pos, font));
808 pimpl_->fontlist[i].pos(pos - 1);
809 if (!(i + 1 < pimpl_->fontlist.size() &&
810 pimpl_->fontlist[i + 1].font() == font))
811 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
812 Pimpl::FontTable(pos, font));
813 } else { // The general case. The block is splitted into 3 blocks
814 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
815 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
816 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
817 Pimpl::FontTable(pos, font));
823 void Paragraph::next(Paragraph * p)
829 // This function is able to hide closed footnotes.
830 Paragraph * Paragraph::next()
836 Paragraph const * Paragraph::next() const
842 void Paragraph::previous(Paragraph * p)
848 // This function is able to hide closed footnotes.
849 Paragraph * Paragraph::previous()
855 // This function is able to hide closed footnotes.
856 Paragraph const * Paragraph::previous() const
862 void Paragraph::breakParagraph(BufferParams const & bparams,
863 Paragraph::size_type pos,
866 // create a new paragraph
867 Paragraph * tmp = new Paragraph(this);
868 // remember to set the inset_owner
869 tmp->setInsetOwner(inInset());
871 // this is an idea for a more userfriendly layout handling, I will
872 // see what the users say
874 // layout stays the same with latex-environments
876 tmp->setOnlyLayout(layout);
877 tmp->setLabelWidthString(params().labelWidthString());
880 if (size() > pos || !size() || flag == 2) {
881 tmp->setOnlyLayout(layout);
882 tmp->params().align(params().align());
883 tmp->setLabelWidthString(params().labelWidthString());
885 tmp->params().lineBottom(params().lineBottom());
886 params().lineBottom(false);
887 tmp->params().pagebreakBottom(params().pagebreakBottom());
888 params().pagebreakBottom(false);
889 tmp->params().spaceBottom(params().spaceBottom());
890 params().spaceBottom(VSpace(VSpace::NONE));
892 tmp->params().depth(params().depth());
893 tmp->params().noindent(params().noindent());
895 // copy everything behind the break-position
896 // to the new paragraph
897 size_type pos_end = pimpl_->size() - 1;
900 for (; i <= pos_end; ++i) {
901 cutIntoMinibuffer(bparams, i);
902 if (tmp->insertFromMinibuffer(j - pos))
905 for (i = pos_end; i >= pos; --i) {
910 // just an idea of me
912 tmp->params().lineTop(params().lineTop());
913 tmp->params().pagebreakTop(params().pagebreakTop());
914 tmp->params().spaceTop(params().spaceTop());
915 tmp->bibkey = bibkey;
917 // layout stays the same with latex-environments
919 setOnlyLayout(tmp->layout);
920 setLabelWidthString(tmp->params().labelWidthString());
921 params().depth(tmp->params().depth());
927 void Paragraph::makeSameLayout(Paragraph const * par)
929 layout = par->layout;
931 params() = par->params();
935 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass)
937 if (textclasslist.Style(tclass, getLayout()).free_spacing)
942 && (isNewline(0) || isLineSeparator(0))){
951 bool Paragraph::hasSameLayout(Paragraph const * par) const
954 par->layout == layout &&
955 params().sameLayout(par->params());
959 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
960 Paragraph::size_type pos)
962 // create a new paragraph
963 Paragraph * tmp = new Paragraph(this);
964 tmp->makeSameLayout(this);
966 // When can pos > Last()?
967 // I guess pos == Last() is possible.
969 // copy everything behind the break-position to the new
971 size_type pos_end = pimpl_->size() - 1;
975 for (size_type i = pos, j = pos; i <= pos_end; ++i) {
976 cutIntoMinibuffer(bparams, i);
977 if (tmp->insertFromMinibuffer(j - pos))
981 for (size_type k = pos_end; k >= pos; --k) {
988 // Be carefull, this does not make any check at all.
989 // This method has wrong name, it combined this par with the next par.
990 // In that sense it is the reverse of break paragraph. (Lgb)
991 void Paragraph::pasteParagraph(BufferParams const & bparams)
993 // copy the next paragraph to this one
994 Paragraph * the_next = next();
996 // first the DTP-stuff
997 params().lineBottom(the_next->params().lineBottom());
998 params().spaceBottom(the_next->params().spaceBottom());
999 params().pagebreakBottom(the_next->params().pagebreakBottom());
1001 size_type pos_end = the_next->pimpl_->size() - 1;
1002 size_type pos_insert = size();
1004 // ok, now copy the paragraph
1005 for (size_type i = 0, j = 0; i <= pos_end; ++i) {
1006 the_next->cutIntoMinibuffer(bparams, i);
1007 if (insertFromMinibuffer(pos_insert + j))
1011 // delete the next paragraph
1012 Paragraph * ppar = the_next->previous_;
1013 Paragraph * npar = the_next->next_;
1019 int Paragraph::getEndLabel(BufferParams const & bparams) const
1021 Paragraph const * par = this;
1022 depth_type par_depth = getDepth();
1024 LyXTextClass::LayoutList::size_type layout = par->getLayout();
1025 int const endlabeltype =
1026 textclasslist.Style(bparams.textclass,
1027 layout).endlabeltype;
1028 if (endlabeltype != END_LABEL_NO_LABEL) {
1030 return endlabeltype;
1032 depth_type const next_depth = next_->getDepth();
1033 if (par_depth > next_depth ||
1034 (par_depth == next_depth
1035 && layout != next_->getLayout()))
1036 return endlabeltype;
1041 par = par->outerHook();
1043 par_depth = par->getDepth();
1045 return END_LABEL_NO_LABEL;
1049 Paragraph::depth_type Paragraph::getDepth() const
1051 return params().depth();
1055 char Paragraph::getAlign() const
1057 return params().align();
1061 string const & Paragraph::getLabelstring() const
1063 return params().labelString();
1067 int Paragraph::getFirstCounter(int i) const
1069 return pimpl_->counter_[i];
1073 // the next two functions are for the manual labels
1074 string const Paragraph::getLabelWidthString() const
1076 if (!params().labelWidthString().empty())
1077 return params().labelWidthString();
1079 return _("Senseless with this layout!");
1083 void Paragraph::setLabelWidthString(string const & s)
1085 params().labelWidthString(s);
1089 void Paragraph::setOnlyLayout(LyXTextClass::size_type new_layout)
1091 layout = new_layout;
1095 void Paragraph::setLayout(LyXTextClass::size_type new_layout)
1097 layout = new_layout;
1098 params().labelWidthString(string());
1099 params().align(LYX_ALIGN_LAYOUT);
1100 params().spaceTop(VSpace(VSpace::NONE));
1101 params().spaceBottom(VSpace(VSpace::NONE));
1102 params().spacing(Spacing(Spacing::Default));
1106 // if the layout of a paragraph contains a manual label, the beginning of the
1107 // main body is the beginning of the second word. This is what the par-
1108 // function returns. If the layout does not contain a label, the main
1109 // body always starts with position 0. This differentiation is necessary,
1110 // because there cannot be a newline or a blank <= the beginning of the
1111 // main body in TeX.
1113 int Paragraph::beginningOfMainBody() const
1115 // Unroll the first two cycles of the loop
1116 // and remember the previous character to
1117 // remove unnecessary GetChar() calls
1120 && getChar(i) != Paragraph::META_NEWLINE) {
1122 char previous_char = 0;
1125 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1126 // Yes, this ^ is supposed to be "= " not "=="
1129 && previous_char != ' '
1130 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1132 previous_char = temp;
1141 Paragraph * Paragraph::depthHook(depth_type depth)
1143 Paragraph * newpar = this;
1146 newpar = newpar->previous();
1147 } while (newpar && newpar->getDepth() > depth);
1150 if (previous() || getDepth())
1151 lyxerr << "ERROR (Paragraph::DepthHook): "
1160 Paragraph const * Paragraph::depthHook(depth_type depth) const
1162 Paragraph const * newpar = this;
1165 newpar = newpar->previous();
1166 } while (newpar && newpar->getDepth() > depth);
1169 if (previous() || getDepth())
1170 lyxerr << "ERROR (Paragraph::DepthHook): "
1178 Paragraph * Paragraph::outerHook()
1182 return depthHook(depth_type(getDepth() - 1));
1185 Paragraph const * Paragraph::outerHook() const
1189 return depthHook(depth_type(getDepth() - 1));
1192 int Paragraph::autoDeleteInsets()
1195 InsetList::size_type index = 0;
1196 while (index < insetlist.size()) {
1197 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1198 erase(insetlist[index].pos);
1199 // Erase() calls to insetlist.erase(&insetlist[index])
1200 // so index shouldn't be increased.
1209 Paragraph::inset_iterator
1210 Paragraph::InsetIterator(Paragraph::size_type pos)
1212 InsetTable search_inset(pos, 0);
1213 InsetList::iterator it = lower_bound(insetlist.begin(),
1215 search_inset, Pimpl::matchIT());
1216 return inset_iterator(it);
1220 // returns -1 if inset not found
1221 int Paragraph::getPositionOfInset(Inset * inset) const
1224 for (InsetList::const_iterator cit = insetlist.begin();
1225 cit != insetlist.end(); ++cit) {
1226 if (cit->inset == inset) {
1230 if (inset == bibkey)
1237 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1238 BufferParams const & bparams,
1239 ostream & os, TexRow & texrow,
1242 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1243 LyXLayout const & style =
1244 textclasslist.Style(bparams.textclass,
1247 bool further_blank_line = false;
1249 if (params().startOfAppendix()) {
1250 os << "\\appendix\n";
1254 if (!params().spacing().isDefault()
1255 && (!previous() || !previous()->hasSameLayout(this))) {
1256 os << params().spacing().writeEnvirBegin() << "\n";
1260 if (tex_code_break_column && style.isCommand()){
1265 if (params().pagebreakTop()) {
1267 further_blank_line = true;
1269 if (params().spaceTop().kind() != VSpace::NONE) {
1270 os << params().spaceTop().asLatexCommand(bparams);
1271 further_blank_line = true;
1274 if (params().lineTop()) {
1275 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1276 << "\\vspace{-1\\parskip}";
1277 further_blank_line = true;
1280 if (further_blank_line){
1285 Language const * language = getParLanguage(bparams);
1286 Language const * doc_language = bparams.language;
1287 Language const * previous_language = previous_
1288 ? previous_->getParLanguage(bparams) : doc_language;
1289 if (language->babel() != doc_language->babel() &&
1290 language->babel() != previous_language->babel()) {
1291 os << subst(lyxrc.language_command_begin, "$$lang",
1297 if (bparams.inputenc == "auto" &&
1298 language->encoding() != previous_language->encoding()) {
1299 os << "\\inputencoding{"
1300 << language->encoding()->LatexName()
1305 switch (style.latextype) {
1308 << style.latexname()
1309 << style.latexparam();
1311 case LATEX_ITEM_ENVIRONMENT:
1313 bibkey->latex(buf, os, false, false);
1317 case LATEX_LIST_ENVIRONMENT:
1324 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1326 // Make sure that \\par is done with the font of the last
1327 // character if this has another size as the default.
1328 // This is necessary because LaTeX (and LyX on the screen)
1329 // calculates the space between the baselines according
1330 // to this font. (Matthias)
1332 // Is this really needed ? (Dekel)
1333 // We do not need to use to change the font for the last paragraph
1334 // or for a command.
1335 LyXFont const font =
1337 ? getLayoutFont(bparams)
1338 : getFont(bparams, size() - 1));
1340 bool is_command = textclasslist.Style(bparams.textclass,
1341 getLayout()).isCommand();
1342 if (style.resfont.size() != font.size() && next_ && !is_command) {
1345 os << "\\" << font.latexSize() << " \\par}";
1346 } else if (need_par) {
1348 } else if (is_command)
1351 if (language->babel() != doc_language->babel() &&
1353 next_->getParLanguage(bparams)->babel() != language->babel()))
1356 << subst(lyxrc.language_command_end, "$$lang",
1357 doc_language->babel());
1360 switch (style.latextype) {
1361 case LATEX_ITEM_ENVIRONMENT:
1362 case LATEX_LIST_ENVIRONMENT:
1363 if (next_ && (params().depth() < next_->params().depth())) {
1368 case LATEX_ENVIRONMENT:
1369 // if its the last paragraph of the current environment
1370 // skip it otherwise fall through
1372 && (next_->layout != layout
1373 || next_->params().depth() != params().depth()))
1376 // we don't need it for the last paragraph!!!
1377 // or for tables in floats
1378 // -- effectively creates a \par where there isn't one which
1379 // breaks a \subfigure or \subtable.
1386 further_blank_line = false;
1387 if (params().lineBottom()) {
1388 os << "\\lyxline{\\" << font.latexSize() << '}';
1389 further_blank_line = true;
1392 if (params().spaceBottom().kind() != VSpace::NONE) {
1393 os << params().spaceBottom().asLatexCommand(bparams);
1394 further_blank_line = true;
1397 if (params().pagebreakBottom()) {
1399 further_blank_line = true;
1402 if (further_blank_line){
1407 if (!params().spacing().isDefault()
1408 && (!next_ || !next_->hasSameLayout(this))) {
1409 os << params().spacing().writeEnvirEnd() << "\n";
1413 // we don't need it for the last paragraph!!!
1419 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1424 // This one spits out the text of the paragraph
1425 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1426 BufferParams const & bparams,
1427 ostream & os, TexRow & texrow,
1430 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1432 bool return_value = false;
1434 LyXLayout const & style =
1435 textclasslist.Style(bparams.textclass,
1439 // Maybe we have to create a optional argument.
1440 size_type main_body;
1441 if (style.labeltype != LABEL_MANUAL)
1444 main_body = beginningOfMainBody();
1448 if (main_body > 0) {
1451 basefont = getLabelFont(bparams);
1453 basefont = getLayoutFont(bparams);
1457 && !pimpl_->size()) {
1458 if (style.isCommand()) {
1464 moving_arg |= style.needprotect;
1466 // Which font is currently active?
1467 LyXFont running_font(basefont);
1468 // Do we have an open font change?
1469 bool open_font = false;
1471 texrow.start(this, 0);
1473 for (size_type i = 0; i < size(); ++i) {
1475 // First char in paragraph or after label?
1476 if (i == main_body) {
1477 if (main_body > 0) {
1479 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1482 basefont = getLayoutFont(bparams);
1483 running_font = basefont;
1487 if (style.isCommand()) {
1492 if (params().noindent()) {
1493 os << "\\noindent ";
1496 switch (params().align()) {
1497 case LYX_ALIGN_NONE:
1498 case LYX_ALIGN_BLOCK:
1499 case LYX_ALIGN_LAYOUT:
1500 case LYX_ALIGN_SPECIAL:
1502 case LYX_ALIGN_LEFT:
1503 if (getParLanguage(bparams)->babel() != "hebrew") {
1504 os << "\\begin{flushleft}";
1507 os << "\\begin{flushright}";
1511 case LYX_ALIGN_RIGHT:
1512 if (getParLanguage(bparams)->babel() != "hebrew") {
1513 os << "\\begin{flushright}";
1516 os << "\\begin{flushleft}";
1520 case LYX_ALIGN_CENTER:
1521 os << "\\begin{center}";
1527 value_type c = getChar(i);
1529 // Fully instantiated font
1530 LyXFont font = getFont(bparams, i);
1532 LyXFont const last_font = running_font;
1534 // Spaces at end of font change are simulated to be
1535 // outside font change, i.e. we write "\textXX{text} "
1536 // rather than "\textXX{text }". (Asger)
1537 if (open_font && c == ' ' && i <= size() - 2) {
1538 LyXFont const next_font = getFont(bparams, i + 1);
1539 if (next_font != running_font
1540 && next_font != font) {
1545 // We end font definition before blanks
1546 if (font != running_font && open_font) {
1547 column += running_font.latexWriteEndChanges(os,
1549 (i == main_body-1) ? basefont : font);
1550 running_font = basefont;
1554 // Blanks are printed before start of fontswitch
1556 // Do not print the separation of the optional argument
1557 if (i != main_body - 1) {
1558 pimpl_->simpleTeXBlanks(os, texrow, i,
1559 column, font, style);
1563 // Do we need to change font?
1564 if (font != running_font && i != main_body - 1) {
1565 column += font.latexWriteStartChanges(os, basefont,
1567 running_font = font;
1571 if (c == Paragraph::META_NEWLINE) {
1572 // newlines are handled differently here than
1573 // the default in SimpleTeXSpecialChars().
1574 if (!style.newline_allowed) {
1578 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1581 basefont = getLayoutFont(bparams);
1582 running_font = basefont;
1583 if (font.family() ==
1584 LyXFont::TYPEWRITER_FAMILY) {
1592 texrow.start(this, i + 1);
1595 pimpl_->simpleTeXSpecialChars(buf, bparams,
1596 os, texrow, moving_arg,
1598 basefont, open_font,
1599 style, i, column, c);
1603 // If we have an open font definition, we have to close it
1605 #ifdef FIXED_LANGUAGE_END_DETECTION
1608 .latexWriteEndChanges(os, basefont,
1609 next_->getFont(bparams,
1612 running_font.latexWriteEndChanges(os, basefont,
1616 #ifdef WITH_WARNINGS
1617 //#warning For now we ALWAYS have to close the foreign font settings if they are
1618 //#warning there as we start another \selectlanguage with the next paragraph if
1619 //#warning we are in need of this. This should be fixed sometime (Jug)
1621 running_font.latexWriteEndChanges(os, basefont, basefont);
1625 // Needed if there is an optional argument but no contents.
1626 if (main_body > 0 && main_body == size()) {
1628 return_value = false;
1631 switch (params().align()) {
1632 case LYX_ALIGN_NONE:
1633 case LYX_ALIGN_BLOCK:
1634 case LYX_ALIGN_LAYOUT:
1635 case LYX_ALIGN_SPECIAL:
1637 case LYX_ALIGN_LEFT:
1638 if (getParLanguage(bparams)->babel() != "hebrew") {
1639 os << "\\end{flushleft}";
1642 os << "\\end{flushright}";
1646 case LYX_ALIGN_RIGHT:
1647 if (getParLanguage(bparams)->babel() != "hebrew") {
1648 os << "\\end{flushright}";
1651 os << "\\end{flushleft}";
1655 case LYX_ALIGN_CENTER:
1656 os << "\\end{center}";
1661 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1662 return return_value;
1666 bool Paragraph::linuxDocConvertChar(char c, string & sgml_string)
1668 bool retval = false;
1670 case Paragraph::META_HFILL:
1671 sgml_string.erase();
1673 case Paragraph::META_NEWLINE:
1677 sgml_string = "&";
1680 sgml_string = "<";
1683 sgml_string = ">";
1686 sgml_string = "$";
1689 sgml_string = "#";
1692 sgml_string = "%";
1695 sgml_string = "[";
1698 sgml_string = "]";
1701 sgml_string = "{";
1704 sgml_string = "}";
1707 sgml_string = "˜";
1710 sgml_string = """;
1713 sgml_string = "\";
1719 case '\0': // Ignore :-)
1720 sgml_string.erase();
1730 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1731 BufferParams const & bparams,
1732 ostream & os, TexRow & texrow)
1734 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1736 LyXLayout const & style =
1737 textclasslist.Style(bparams.textclass,
1740 if (style.isEnvironment()){
1741 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1742 os << "\\begin{" << style.latexname() << "}{"
1743 << params().labelWidthString() << "}\n";
1744 } else if (style.labeltype == LABEL_BIBLIO) {
1746 os << "\\begin{" << style.latexname() << "}{"
1747 << bibitemWidest(buf)
1749 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1750 os << "\\begin{" << style.latexname() << '}'
1751 << style.latexparam() << '\n';
1753 os << "\\begin{" << style.latexname() << '}'
1754 << style.latexparam() << '\n';
1757 Paragraph * par = this;
1759 par = par->TeXOnePar(buf, bparams,
1762 if (par && par->params().depth() > params().depth()) {
1763 if (textclasslist.Style(bparams.textclass,
1764 par->layout).isParagraph()
1766 // How to handle this? (Lgb)
1767 //&& !suffixIs(os, "\n\n")
1769 // There should be at least one '\n' already
1770 // but we need there to be two for Standard
1771 // paragraphs that are depth-increment'ed to be
1772 // output correctly. However, tables can
1773 // also be paragraphs so don't adjust them.
1776 // Will it ever harm to have one '\n' too
1777 // many? i.e. that we sometimes will have
1778 // three in a row. (Lgb)
1782 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1785 && par->layout == layout
1786 && par->params().depth() == params().depth());
1788 if (style.isEnvironment()) {
1789 os << "\\end{" << style.latexname() << "}\n";
1792 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1793 return par; // ale970302
1797 bool Paragraph::isHfill(size_type pos) const
1799 return IsHfillChar(getChar(pos));
1803 bool Paragraph::isInset(size_type pos) const
1805 return IsInsetChar(getChar(pos));
1809 bool Paragraph::isNewline(size_type pos) const
1811 return pos >= 0 && IsNewlineChar(getChar(pos));
1815 bool Paragraph::isSeparator(size_type pos) const
1817 return IsSeparatorChar(getChar(pos));
1821 bool Paragraph::isLineSeparator(size_type pos) const
1823 return IsLineSeparatorChar(getChar(pos));
1827 bool Paragraph::isKomma(size_type pos) const
1829 return IsKommaChar(getChar(pos));
1833 /// Used by the spellchecker
1834 bool Paragraph::isLetter(Paragraph::size_type pos) const
1836 value_type const c = getChar(pos);
1837 if (IsLetterChar(c))
1839 // '\0' is not a letter, allthough every string contains "" (below)
1842 // We want to pass the ' and escape chars to ispell
1843 string const extra = lyxrc.isp_esc_chars + '\'';
1844 char ch[2] = { c, 0 };
1845 return contains(extra, ch);
1849 bool Paragraph::isWord(size_type pos ) const
1851 return IsWordChar(getChar(pos)) ;
1856 Paragraph::getParLanguage(BufferParams const & bparams) const
1859 #ifndef INHERIT_LANGUAGE
1860 return getFirstFontSettings().language();
1862 Language const * lang = getFirstFontSettings().language();
1863 #warning We should make this somewhat better, any ideas? (Jug)
1864 if (lang == inherit_language || lang == ignore_language)
1865 lang = bparams.language;
1868 } else if (previous_)
1869 return previous_->getParLanguage(bparams);
1871 return bparams.language;
1875 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1877 return lyxrc.rtl_support
1878 && getParLanguage(bparams)->RightToLeft();
1882 void Paragraph::changeLanguage(BufferParams const & bparams,
1883 Language const * from, Language const * to)
1885 for (size_type i = 0; i < size(); ++i) {
1886 LyXFont font = getFontSettings(bparams, i);
1887 if (font.language() == from) {
1888 font.setLanguage(to);
1895 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1897 Language const * doc_language = bparams.language;
1898 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1899 cit != pimpl_->fontlist.end(); ++cit)
1900 if (cit->font().language() != ignore_language &&
1901 cit->font().language() != latex_language &&
1902 #ifdef INHERIT_LANGUAGE
1903 cit->font().language() != inherit_language &&
1905 cit->font().language() != doc_language)
1911 // Convert the paragraph to a string.
1912 // Used for building the table of contents
1913 string const Paragraph::asString(Buffer const * buffer, bool label)
1915 BufferParams const & bparams = buffer->params;
1917 if (label && !params().labelString().empty())
1918 s += params().labelString() + ' ';
1919 string::size_type const len = s.size();
1921 for (Paragraph::size_type i = 0; i < size(); ++i) {
1922 value_type c = getChar(i);
1925 else if (c == META_INSET &&
1926 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1928 getInset(i)->ascii(buffer, ost);
1929 s += subst(ost.str().c_str(),'\n',' ');
1933 if (isRightToLeftPar(bparams))
1934 reverse(s.begin() + len,s.end());
1940 string const Paragraph::asString(Buffer const * buffer,
1941 Paragraph::size_type beg,
1942 Paragraph::size_type end,
1947 if (beg == 0 && label && !params().labelString().empty())
1948 ost << params().labelString() << ' ';
1950 for (Paragraph::size_type i = beg; i < end; ++i) {
1951 value_type const c = getUChar(buffer->params, i);
1954 else if (c == META_NEWLINE)
1956 else if (c == META_HFILL)
1958 else if (c == META_INSET) {
1959 getInset(i)->ascii(buffer, ost);
1963 return ost.str().c_str();
1967 void Paragraph::setInsetOwner(Inset * i)
1969 pimpl_->inset_owner = i;
1970 for (InsetList::const_iterator cit = insetlist.begin();
1971 cit != insetlist.end(); ++cit) {
1973 cit->inset->setOwner(i);
1978 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1981 for (InsetList::const_iterator cit = insetlist.begin();
1982 cit != insetlist.end(); ++cit) {
1984 if (cit->inset->isTextInset()) {
1985 static_cast<UpdatableInset *>
1986 (cit->inset)->deleteLyXText(bv, true);
1993 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1996 for (InsetList::const_iterator cit = insetlist.begin();
1997 cit != insetlist.end(); ++cit)
2000 if (cit->inset->isTextInset()) {
2001 static_cast<UpdatableInset *>
2002 (cit->inset)->resizeLyXText(bv, true);
2009 void Paragraph::setContentsFromPar(Paragraph * par)
2011 pimpl_->setContentsFromPar(par);
2015 Paragraph::size_type Paragraph::size() const
2017 return pimpl_->size();
2021 Paragraph::value_type
2022 Paragraph::getChar(Paragraph::size_type pos) const
2024 return pimpl_->getChar(pos);
2028 int Paragraph::id() const
2034 void Paragraph::id(int id_arg)
2036 pimpl_->id_ = id_arg;
2040 LyXTextClass::LayoutList::size_type Paragraph::getLayout() const
2046 bool Paragraph::isFirstInSequence() const
2048 Paragraph const * dhook = depthHook(getDepth());
2049 return (dhook == this
2050 || dhook->getLayout() != getLayout()
2051 || dhook->getDepth() != getDepth());
2055 Inset * Paragraph::inInset() const
2057 return pimpl_->inset_owner;
2061 void Paragraph::clearContents()
2067 void Paragraph::setCounter(int i, int v)
2069 pimpl_->counter_[i] = v;
2073 int Paragraph::getCounter(int i) const
2075 return pimpl_->counter_[i];
2079 void Paragraph::incCounter(int i)
2081 pimpl_->counter_[i]++;
2085 void Paragraph::setChar(size_type pos, value_type c)
2087 pimpl_->setChar(pos, c);
2091 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2096 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2098 return inset_iterator(insetlist.begin());
2102 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2104 return inset_iterator(insetlist.end());
2108 ParagraphParameters & Paragraph::params()
2110 return pimpl_->params;
2114 ParagraphParameters const & Paragraph::params() const
2116 return pimpl_->params;
2120 Paragraph * Paragraph::getParFromID(int id) const
2122 return pimpl_->getParFromID(id);