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 font2.lyxWriteChanges(font1, bparams.language, os);
260 value_type const c = getChar(i);
264 Inset const * inset = getInset(i);
266 if (inset->directWrite()) {
267 // international char, let it write
268 // code directly so it's shorter in
270 inset->write(buf, os);
272 os << "\n\\begin_inset ";
273 inset->write(buf, os);
274 os << "\n\\end_inset \n\n";
280 os << "\n\\newline \n";
284 os << "\n\\hfill \n";
288 os << "\n\\backslash \n";
292 if (i + 1 < size() && getChar(i + 1) == ' ') {
299 if ((column > 70 && c == ' ')
304 // this check is to amend a bug. LyX sometimes
305 // inserts '\0' this could cause problems.
309 lyxerr << "ERROR (Paragraph::writeFile):"
310 " NULL char in structure." << endl;
316 // now write the next paragraph
318 next_->writeFile(buf, os, bparams, dth);
322 void Paragraph::validate(LaTeXFeatures & features) const
324 BufferParams const & bparams = features.bufferParams();
327 if (params().lineTop() || params().lineBottom())
328 features.lyxline = true;
329 if (!params().spacing().isDefault())
330 features.setspace = true;
333 features.layout[getLayout()] = true;
336 Language const * doc_language = bparams.language;
338 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
339 cit != pimpl_->fontlist.end(); ++cit) {
340 if (cit->font().noun() == LyXFont::ON) {
341 lyxerr[Debug::LATEX] << "font.noun: "
342 << cit->font().noun()
344 features.noun = true;
345 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
346 << cit->font().stateText(0)
349 switch (cit->font().color()) {
351 case LColor::inherit:
353 // probably we should put here all interface colors used for
354 // font displaying! For now I just add this ones I know of (Jug)
359 features.color = true;
360 lyxerr[Debug::LATEX] << "Color enabled. Font: "
361 << cit->font().stateText(0)
365 Language const * language = cit->font().language();
366 if (language != ignore_language &&
367 language != inherit_language &&
368 language->babel() != doc_language->babel())
370 features.UsedLanguages.insert(language);
371 lyxerr[Debug::LATEX] << "Found language "
372 << language->babel() << endl;
377 for (InsetList::const_iterator cit = insetlist.begin();
378 cit != insetlist.end(); ++cit) {
380 cit->inset->validate(features);
385 // First few functions needed for cut and paste and paragraph breaking.
386 void Paragraph::copyIntoMinibuffer(Buffer const & buffer,
387 Paragraph::size_type pos) const
389 BufferParams bparams = buffer.params;
391 minibuffer_char = getChar(pos);
392 minibuffer_font = getFontSettings(bparams, pos);
393 minibuffer_inset = 0;
394 if (minibuffer_char == Paragraph::META_INSET) {
396 minibuffer_inset = getInset(pos)->clone(buffer);
398 minibuffer_inset = 0;
399 minibuffer_char = ' ';
400 // This reflects what GetInset() does (ARRae)
406 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
407 Paragraph::size_type pos)
409 minibuffer_char = getChar(pos);
410 minibuffer_font = getFontSettings(bparams, pos);
411 minibuffer_inset = 0;
412 if (minibuffer_char == Paragraph::META_INSET) {
414 minibuffer_inset = getInset(pos);
415 // This is a little hack since I want exactly
416 // the inset, not just a clone. Otherwise
417 // the inset would be deleted when calling Erase(pos)
419 InsetTable search_elem(pos, 0);
420 InsetList::iterator it =
421 lower_bound(insetlist.begin(),
423 search_elem, Pimpl::matchIT());
424 if (it != insetlist.end() && it->pos == pos)
427 minibuffer_inset = 0;
428 minibuffer_char = ' ';
429 // This reflects what GetInset() does (ARRae)
434 // Erase(pos); now the caller is responsible for that.
438 bool Paragraph::insertFromMinibuffer(Paragraph::size_type pos)
440 if ((minibuffer_char == Paragraph::META_INSET) &&
441 !insetAllowed(minibuffer_inset->lyxCode()))
443 if (minibuffer_char == Paragraph::META_INSET)
444 insertInset(pos, minibuffer_inset, minibuffer_font);
446 LyXFont f = minibuffer_font;
447 if (checkInsertChar(f))
448 insertChar(pos, minibuffer_char, f);
457 void Paragraph::clear()
466 void Paragraph::erase(Paragraph::size_type pos)
472 bool Paragraph::checkInsertChar(LyXFont & font)
474 if (pimpl_->inset_owner)
475 return pimpl_->inset_owner->checkInsertChar(font);
480 void Paragraph::insertChar(Paragraph::size_type pos,
481 Paragraph::value_type c)
483 LyXFont const f(LyXFont::ALL_INHERIT);
484 insertChar(pos, c, f);
488 void Paragraph::insertChar(Paragraph::size_type pos,
489 Paragraph::value_type c,
490 LyXFont const & font)
492 pimpl_->insertChar(pos, c, font);
496 void Paragraph::insertInset(Paragraph::size_type pos,
499 LyXFont const f(LyXFont::ALL_INHERIT);
500 insertInset(pos, inset, f);
504 void Paragraph::insertInset(Paragraph::size_type pos,
505 Inset * inset, LyXFont const & font)
507 pimpl_->insertInset(pos, inset, font);
511 bool Paragraph::insetAllowed(Inset::Code code)
513 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
515 if (pimpl_->inset_owner)
516 return pimpl_->inset_owner->insetAllowed(code);
521 Inset * Paragraph::getInset(Paragraph::size_type pos)
523 lyx::Assert(pos < size());
526 InsetTable search_inset(pos, 0);
527 InsetList::iterator it = lower_bound(insetlist.begin(),
529 search_inset, Pimpl::matchIT());
530 if (it != insetlist.end() && it->pos == pos)
533 lyxerr << "ERROR (Paragraph::GetInset): "
534 "Inset does not exist: " << pos << endl;
537 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
538 // Did this commenting out introduce a bug? So far I have not
539 // see any, please enlighten me. (Lgb)
540 // My guess is that since the inset does not exist, we might
541 // as well replace it with a space to prevent craches. (Asger)
546 Inset const * Paragraph::getInset(Paragraph::size_type pos) const
548 lyx::Assert(pos < size());
551 InsetTable search_inset(pos, 0);
552 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
554 search_inset, Pimpl::matchIT());
555 if (cit != insetlist.end() && cit->pos == pos)
558 lyxerr << "ERROR (Paragraph::GetInset): "
559 "Inset does not exist: " << pos << endl;
561 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
562 // Did this commenting out introduce a bug? So far I have not
563 // see any, please enlighten me. (Lgb)
564 // My guess is that since the inset does not exist, we might
565 // as well replace it with a space to prevent craches. (Asger)
570 // Gets uninstantiated font setting at position.
571 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
572 Paragraph::size_type pos) const
574 lyx::Assert(pos <= size());
576 Pimpl::FontTable search_font(pos, LyXFont());
577 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
578 pimpl_->fontlist.end(),
579 search_font, Pimpl::matchFT());
581 if (cit != pimpl_->fontlist.end()) {
582 retfont = cit->font();
583 } else if (pos == size() && size()) {
584 retfont = getFontSettings(bparams, pos - 1);
586 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
587 if (retfont.language() == inherit_language)
588 retfont.setLanguage(bparams.language);
594 // Gets uninstantiated font setting at position 0
595 LyXFont const Paragraph::getFirstFontSettings() const
598 if (!pimpl_->fontlist.empty())
599 return pimpl_->fontlist[0].font();
602 return LyXFont(LyXFont::ALL_INHERIT);
606 // Gets the fully instantiated font at a given position in a paragraph
607 // This is basically the same function as LyXText::GetFont() in text2.C.
608 // The difference is that this one is used for generating the LaTeX file,
609 // and thus cosmetic "improvements" are disallowed: This has to deliver
610 // the true picture of the buffer. (Asger)
611 // If position is -1, we get the layout font of the paragraph.
612 // If position is -2, we get the font of the manual label of the paragraph.
613 LyXFont const Paragraph::getFont(BufferParams const & bparams,
614 Paragraph::size_type pos) const
617 LyXLayout const & layout =
618 textclasslist.Style(bparams.textclass,
620 Paragraph::size_type main_body = 0;
621 if (layout.labeltype == LABEL_MANUAL)
622 main_body = beginningOfMainBody();
627 layoutfont = layout.labelfont;
629 layoutfont = layout.font;
630 tmpfont = getFontSettings(bparams, pos);
631 tmpfont.realize(layoutfont, bparams.language);
633 // process layoutfont for pos == -1 and labelfont for pos < -1
635 tmpfont = layout.font;
637 tmpfont = layout.labelfont;
638 tmpfont.setLanguage(getParLanguage(bparams));
641 // check for environment font information
642 char par_depth = getDepth();
643 Paragraph const * par = this;
644 while (par && par->getDepth() && !tmpfont.resolved()) {
645 par = par->outerHook();
647 tmpfont.realize(textclasslist.
648 Style(bparams.textclass,
649 par->getLayout()).font, bparams.language);
650 par_depth = par->getDepth();
654 tmpfont.realize(textclasslist
655 .TextClass(bparams.textclass)
656 .defaultfont(), bparams.language);
661 /// Returns the height of the highest font in range
663 Paragraph::highestFontInRange(Paragraph::size_type startpos,
664 Paragraph::size_type endpos) const
666 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
667 if (pimpl_->fontlist.empty())
670 Pimpl::FontTable end_search(endpos, LyXFont());
671 Pimpl::FontList::const_iterator end_it = lower_bound(pimpl_->fontlist.begin(),
672 pimpl_->fontlist.end(),
673 end_search, Pimpl::matchFT());
674 if (end_it != pimpl_->fontlist.end())
677 Pimpl::FontTable start_search(startpos, LyXFont());
678 for (Pimpl::FontList::const_iterator cit =
679 lower_bound(pimpl_->fontlist.begin(),
680 pimpl_->fontlist.end(),
681 start_search, Pimpl::matchFT());
682 cit != end_it; ++cit) {
683 LyXFont::FONT_SIZE size = cit->font().size();
684 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
691 Paragraph::value_type
692 Paragraph::getUChar(BufferParams const & bparams,
693 Paragraph::size_type pos) const
695 value_type c = getChar(pos);
696 if (!lyxrc.rtl_support)
726 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
733 void Paragraph::setFont(Paragraph::size_type pos,
734 LyXFont const & font)
736 lyx::Assert(pos <= size());
738 // First, reduce font against layout/label font
739 // Update: The SetCharFont() routine in text2.C already
740 // reduces font, so we don't need to do that here. (Asger)
741 // No need to simplify this because it will disappear
742 // in a new kernel. (Asger)
743 // Next search font table
745 Pimpl::FontTable search_font(pos, LyXFont());
746 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
747 pimpl_->fontlist.end(),
748 search_font, Pimpl::matchFT());
749 unsigned int i = it - pimpl_->fontlist.begin();
750 bool notfound = it == pimpl_->fontlist.end();
752 if (!notfound && pimpl_->fontlist[i].font() == font)
755 bool begin = pos == 0 || notfound ||
756 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
757 // Is position pos is a beginning of a font block?
758 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
759 // Is position pos is the end of a font block?
760 if (begin && end) { // A single char block
761 if (i + 1 < pimpl_->fontlist.size() &&
762 pimpl_->fontlist[i + 1].font() == font) {
763 // Merge the singleton block with the next block
764 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
765 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
766 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
767 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
768 // Merge the singleton block with the previous block
769 pimpl_->fontlist[i - 1].pos(pos);
770 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
772 pimpl_->fontlist[i].font(font);
774 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
775 pimpl_->fontlist[i - 1].pos(pos);
777 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
778 Pimpl::FontTable(pos, font));
780 pimpl_->fontlist[i].pos(pos - 1);
781 if (!(i + 1 < pimpl_->fontlist.size() &&
782 pimpl_->fontlist[i + 1].font() == font))
783 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
784 Pimpl::FontTable(pos, font));
785 } else { // The general case. The block is splitted into 3 blocks
786 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
787 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
788 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
789 Pimpl::FontTable(pos, font));
795 void Paragraph::next(Paragraph * p)
801 // This function is able to hide closed footnotes.
802 Paragraph * Paragraph::next()
808 Paragraph const * Paragraph::next() const
814 void Paragraph::previous(Paragraph * p)
820 // This function is able to hide closed footnotes.
821 Paragraph * Paragraph::previous()
827 // This function is able to hide closed footnotes.
828 Paragraph const * Paragraph::previous() const
834 void Paragraph::breakParagraph(BufferParams const & bparams,
835 Paragraph::size_type pos,
838 // create a new paragraph
839 Paragraph * tmp = new Paragraph(this);
840 // remember to set the inset_owner
841 tmp->setInsetOwner(inInset());
843 // this is an idea for a more userfriendly layout handling, I will
844 // see what the users say
846 // layout stays the same with latex-environments
848 tmp->setOnlyLayout(layout);
849 tmp->setLabelWidthString(params().labelWidthString());
852 if (size() > pos || !size() || flag == 2) {
853 tmp->setOnlyLayout(layout);
854 tmp->params().align(params().align());
855 tmp->setLabelWidthString(params().labelWidthString());
857 tmp->params().lineBottom(params().lineBottom());
858 params().lineBottom(false);
859 tmp->params().pagebreakBottom(params().pagebreakBottom());
860 params().pagebreakBottom(false);
861 tmp->params().spaceBottom(params().spaceBottom());
862 params().spaceBottom(VSpace(VSpace::NONE));
864 tmp->params().depth(params().depth());
865 tmp->params().noindent(params().noindent());
867 // copy everything behind the break-position
868 // to the new paragraph
869 size_type pos_end = pimpl_->size() - 1;
872 for (; i <= pos_end; ++i) {
873 cutIntoMinibuffer(bparams, i);
874 if (tmp->insertFromMinibuffer(j - pos))
877 for (i = pos_end; i >= pos; --i) {
882 // just an idea of me
884 tmp->params().lineTop(params().lineTop());
885 tmp->params().pagebreakTop(params().pagebreakTop());
886 tmp->params().spaceTop(params().spaceTop());
887 tmp->bibkey = bibkey;
889 // layout stays the same with latex-environments
891 setOnlyLayout(tmp->layout);
892 setLabelWidthString(tmp->params().labelWidthString());
893 params().depth(tmp->params().depth());
899 void Paragraph::makeSameLayout(Paragraph const * par)
901 layout = par->layout;
903 params() = par->params();
907 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass)
909 if (textclasslist.Style(tclass, getLayout()).free_spacing)
914 && (isNewline(0) || isLineSeparator(0))){
923 bool Paragraph::hasSameLayout(Paragraph const * par) const
926 par->layout == layout &&
927 params().sameLayout(par->params());
931 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
932 Paragraph::size_type pos)
934 // create a new paragraph
935 Paragraph * tmp = new Paragraph(this);
936 tmp->makeSameLayout(this);
938 // When can pos > Last()?
939 // I guess pos == Last() is possible.
941 // copy everything behind the break-position to the new
943 size_type pos_end = pimpl_->size() - 1;
947 for (size_type i = pos, j = pos; i <= pos_end; ++i) {
948 cutIntoMinibuffer(bparams, i);
949 if (tmp->insertFromMinibuffer(j - pos))
953 for (size_type k = pos_end; k >= pos; --k) {
960 // Be carefull, this does not make any check at all.
961 // This method has wrong name, it combined this par with the next par.
962 // In that sense it is the reverse of break paragraph. (Lgb)
963 void Paragraph::pasteParagraph(BufferParams const & bparams)
965 // copy the next paragraph to this one
966 Paragraph * the_next = next();
968 // first the DTP-stuff
969 params().lineBottom(the_next->params().lineBottom());
970 params().spaceBottom(the_next->params().spaceBottom());
971 params().pagebreakBottom(the_next->params().pagebreakBottom());
973 size_type pos_end = the_next->pimpl_->size() - 1;
974 size_type pos_insert = size();
976 // ok, now copy the paragraph
977 for (size_type i = 0, j = 0; i <= pos_end; ++i) {
978 the_next->cutIntoMinibuffer(bparams, i);
979 if (insertFromMinibuffer(pos_insert + j))
983 // delete the next paragraph
984 Paragraph * ppar = the_next->previous_;
985 Paragraph * npar = the_next->next_;
991 int Paragraph::getEndLabel(BufferParams const & bparams) const
993 Paragraph const * par = this;
994 depth_type par_depth = getDepth();
996 LyXTextClass::LayoutList::size_type layout = par->getLayout();
997 int const endlabeltype =
998 textclasslist.Style(bparams.textclass,
999 layout).endlabeltype;
1000 if (endlabeltype != END_LABEL_NO_LABEL) {
1002 return endlabeltype;
1004 depth_type const next_depth = next_->getDepth();
1005 if (par_depth > next_depth ||
1006 (par_depth == next_depth
1007 && layout != next_->getLayout()))
1008 return endlabeltype;
1013 par = par->outerHook();
1015 par_depth = par->getDepth();
1017 return END_LABEL_NO_LABEL;
1021 Paragraph::depth_type Paragraph::getDepth() const
1023 return params().depth();
1027 char Paragraph::getAlign() const
1029 return params().align();
1033 string const & Paragraph::getLabelstring() const
1035 return params().labelString();
1039 int Paragraph::getFirstCounter(int i) const
1041 return pimpl_->counter_[i];
1045 // the next two functions are for the manual labels
1046 string const Paragraph::getLabelWidthString() const
1048 if (!params().labelWidthString().empty())
1049 return params().labelWidthString();
1051 return _("Senseless with this layout!");
1055 void Paragraph::setLabelWidthString(string const & s)
1057 params().labelWidthString(s);
1061 void Paragraph::setOnlyLayout(LyXTextClass::size_type new_layout)
1063 layout = new_layout;
1067 void Paragraph::setLayout(LyXTextClass::size_type new_layout)
1069 layout = new_layout;
1070 params().labelWidthString(string());
1071 params().align(LYX_ALIGN_LAYOUT);
1072 params().spaceTop(VSpace(VSpace::NONE));
1073 params().spaceBottom(VSpace(VSpace::NONE));
1074 params().spacing(Spacing(Spacing::Default));
1078 // if the layout of a paragraph contains a manual label, the beginning of the
1079 // main body is the beginning of the second word. This is what the par-
1080 // function returns. If the layout does not contain a label, the main
1081 // body always starts with position 0. This differentiation is necessary,
1082 // because there cannot be a newline or a blank <= the beginning of the
1083 // main body in TeX.
1085 int Paragraph::beginningOfMainBody() const
1087 // Unroll the first two cycles of the loop
1088 // and remember the previous character to
1089 // remove unnecessary GetChar() calls
1092 && getChar(i) != Paragraph::META_NEWLINE) {
1094 char previous_char = 0;
1097 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1098 // Yes, this ^ is supposed to be "= " not "=="
1101 && previous_char != ' '
1102 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1104 previous_char = temp;
1113 Paragraph * Paragraph::depthHook(depth_type depth)
1115 Paragraph * newpar = this;
1118 newpar = newpar->previous();
1119 } while (newpar && newpar->getDepth() > depth);
1122 if (previous() || getDepth())
1123 lyxerr << "ERROR (Paragraph::DepthHook): "
1132 Paragraph const * Paragraph::depthHook(depth_type depth) const
1134 Paragraph const * newpar = this;
1137 newpar = newpar->previous();
1138 } while (newpar && newpar->getDepth() > depth);
1141 if (previous() || getDepth())
1142 lyxerr << "ERROR (Paragraph::DepthHook): "
1150 Paragraph * Paragraph::outerHook()
1154 return depthHook(depth_type(getDepth() - 1));
1157 Paragraph const * Paragraph::outerHook() const
1161 return depthHook(depth_type(getDepth() - 1));
1164 int Paragraph::autoDeleteInsets()
1167 InsetList::size_type index = 0;
1168 while (index < insetlist.size()) {
1169 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1170 erase(insetlist[index].pos);
1171 // Erase() calls to insetlist.erase(&insetlist[index])
1172 // so index shouldn't be increased.
1181 Paragraph::inset_iterator
1182 Paragraph::InsetIterator(Paragraph::size_type pos)
1184 InsetTable search_inset(pos, 0);
1185 InsetList::iterator it = lower_bound(insetlist.begin(),
1187 search_inset, Pimpl::matchIT());
1188 return inset_iterator(it);
1192 // returns -1 if inset not found
1193 int Paragraph::getPositionOfInset(Inset * inset) const
1196 for (InsetList::const_iterator cit = insetlist.begin();
1197 cit != insetlist.end(); ++cit) {
1198 if (cit->inset == inset) {
1202 if (inset == bibkey)
1209 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1210 BufferParams const & bparams,
1211 ostream & os, TexRow & texrow,
1214 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1215 LyXLayout const & style =
1216 textclasslist.Style(bparams.textclass,
1219 bool further_blank_line = false;
1221 if (params().startOfAppendix()) {
1222 os << "\\appendix\n";
1226 if (!params().spacing().isDefault()
1227 && (!previous() || !previous()->hasSameLayout(this))) {
1228 os << params().spacing().writeEnvirBegin() << "\n";
1232 if (tex_code_break_column && style.isCommand()){
1237 if (params().pagebreakTop()) {
1239 further_blank_line = true;
1241 if (params().spaceTop().kind() != VSpace::NONE) {
1242 os << params().spaceTop().asLatexCommand(bparams);
1243 further_blank_line = true;
1246 if (params().lineTop()) {
1247 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1248 << "\\vspace{-1\\parskip}";
1249 further_blank_line = true;
1252 if (further_blank_line){
1257 Language const * language = getParLanguage(bparams);
1258 Language const * doc_language = bparams.language;
1259 Language const * previous_language = previous_
1260 ? previous_->getParLanguage(bparams) : doc_language;
1261 if (language == ignore_language || language == inherit_language)
1262 lyxerr << "1:" << language->lang() << endl;
1263 if (language->babel() != doc_language->babel() &&
1264 language->babel() != previous_language->babel()) {
1265 os << subst(lyxrc.language_command_begin, "$$lang",
1271 if (bparams.inputenc == "auto" &&
1272 language->encoding() != previous_language->encoding())
1274 os << "\\inputencoding{"
1275 << language->encoding()->LatexName()
1280 switch (style.latextype) {
1283 << style.latexname()
1284 << style.latexparam();
1286 case LATEX_ITEM_ENVIRONMENT:
1288 bibkey->latex(buf, os, false, false);
1292 case LATEX_LIST_ENVIRONMENT:
1299 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1301 // Make sure that \\par is done with the font of the last
1302 // character if this has another size as the default.
1303 // This is necessary because LaTeX (and LyX on the screen)
1304 // calculates the space between the baselines according
1305 // to this font. (Matthias)
1307 // Is this really needed ? (Dekel)
1308 // We do not need to use to change the font for the last paragraph
1309 // or for a command.
1310 LyXFont font = getFont(bparams, size() - 1);
1312 bool is_command = textclasslist.Style(bparams.textclass,
1313 getLayout()).isCommand();
1314 if (style.resfont.size() != font.size() && next_ && !is_command) {
1317 os << "\\" << font.latexSize() << " \\par}";
1318 } else if (need_par) {
1320 } else if (is_command)
1323 if (language->babel() != doc_language->babel() &&
1325 next_->getParLanguage(bparams)->babel() != language->babel()))
1328 << subst(lyxrc.language_command_end, "$$lang",
1329 doc_language->babel());
1332 switch (style.latextype) {
1333 case LATEX_ITEM_ENVIRONMENT:
1334 case LATEX_LIST_ENVIRONMENT:
1335 if (next_ && (params().depth() < next_->params().depth())) {
1340 case LATEX_ENVIRONMENT:
1341 // if its the last paragraph of the current environment
1342 // skip it otherwise fall through
1344 && (next_->layout != layout
1345 || next_->params().depth() != params().depth()))
1348 // we don't need it for the last paragraph!!!
1349 // or for tables in floats
1350 // -- effectively creates a \par where there isn't one which
1351 // breaks a \subfigure or \subtable.
1358 further_blank_line = false;
1359 if (params().lineBottom()) {
1360 os << "\\lyxline{\\" << getFont(bparams,
1361 size() - 1).latexSize() << '}';
1362 further_blank_line = true;
1365 if (params().spaceBottom().kind() != VSpace::NONE) {
1366 os << params().spaceBottom().asLatexCommand(bparams);
1367 further_blank_line = true;
1370 if (params().pagebreakBottom()) {
1372 further_blank_line = true;
1375 if (further_blank_line){
1380 if (!params().spacing().isDefault()
1381 && (!next_ || !next_->hasSameLayout(this))) {
1382 os << params().spacing().writeEnvirEnd() << "\n";
1386 // we don't need it for the last paragraph!!!
1392 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1397 // This one spits out the text of the paragraph
1398 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1399 BufferParams const & bparams,
1400 ostream & os, TexRow & texrow,
1403 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1405 bool return_value = false;
1407 LyXLayout const & style =
1408 textclasslist.Style(bparams.textclass,
1412 // Maybe we have to create a optional argument.
1413 size_type main_body;
1414 if (style.labeltype != LABEL_MANUAL)
1417 main_body = beginningOfMainBody();
1421 if (main_body > 0) {
1424 basefont = getFont(bparams, -2); // Get label font
1426 basefont = getFont(bparams, -1); // Get layout font
1430 && !pimpl_->size()) {
1431 if (style.isCommand()) {
1437 moving_arg |= style.needprotect;
1439 // Which font is currently active?
1440 LyXFont running_font(basefont);
1441 // Do we have an open font change?
1442 bool open_font = false;
1444 texrow.start(this, 0);
1446 for (size_type i = 0; i < size(); ++i) {
1448 // First char in paragraph or after label?
1449 if (i == main_body) {
1450 if (main_body > 0) {
1452 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1455 basefont = getFont(bparams, -1); // Now use the layout font
1456 running_font = basefont;
1460 if (style.isCommand()) {
1465 if (params().noindent()) {
1466 os << "\\noindent ";
1469 switch (params().align()) {
1470 case LYX_ALIGN_NONE:
1471 case LYX_ALIGN_BLOCK:
1472 case LYX_ALIGN_LAYOUT:
1473 case LYX_ALIGN_SPECIAL:
1475 case LYX_ALIGN_LEFT:
1476 if (getParLanguage(bparams)->babel() != "hebrew") {
1477 os << "\\begin{flushleft}";
1480 os << "\\begin{flushright}";
1484 case LYX_ALIGN_RIGHT:
1485 if (getParLanguage(bparams)->babel() != "hebrew") {
1486 os << "\\begin{flushright}";
1489 os << "\\begin{flushleft}";
1493 case LYX_ALIGN_CENTER:
1494 os << "\\begin{center}";
1500 value_type c = getChar(i);
1502 // Fully instantiated font
1503 LyXFont font = getFont(bparams, i);
1505 LyXFont last_font = running_font;
1507 // Spaces at end of font change are simulated to be
1508 // outside font change, i.e. we write "\textXX{text} "
1509 // rather than "\textXX{text }". (Asger)
1510 if (open_font && c == ' ' && i <= size() - 2
1511 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
1512 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
1513 font = getFont(bparams, i + 1);
1515 // We end font definition before blanks
1516 if (!font.equalExceptLatex(running_font) && open_font) {
1517 column += running_font.latexWriteEndChanges(os,
1519 (i == main_body-1) ? basefont : font);
1520 running_font = basefont;
1524 // Blanks are printed before start of fontswitch
1526 // Do not print the separation of the optional argument
1527 if (i != main_body - 1) {
1528 pimpl_->simpleTeXBlanks(os, texrow, i,
1529 column, font, style);
1533 // Do we need to change font?
1534 if (!font.equalExceptLatex(running_font)
1535 && i != main_body-1) {
1536 column += font.latexWriteStartChanges(os, basefont,
1538 running_font = font;
1542 if (c == Paragraph::META_NEWLINE) {
1543 // newlines are handled differently here than
1544 // the default in SimpleTeXSpecialChars().
1545 if (!style.newline_allowed
1547 || font.latex() == LyXFont::ON
1553 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1556 basefont = getFont(bparams, -1);
1557 running_font = basefont;
1558 if (font.family() ==
1559 LyXFont::TYPEWRITER_FAMILY) {
1567 texrow.start(this, i + 1);
1570 pimpl_->simpleTeXSpecialChars(buf, bparams,
1571 os, texrow, moving_arg,
1573 basefont, open_font,
1574 style, i, column, c);
1578 // If we have an open font definition, we have to close it
1580 #ifdef FIXED_LANGUAGE_END_DETECTION
1583 .latexWriteEndChanges(os, basefont,
1584 next_->getFont(bparams,
1587 running_font.latexWriteEndChanges(os, basefont,
1591 #ifdef WITH_WARNINGS
1592 #warning For now we ALWAYS have to close the foreign font settings if they are
1593 #warning there as we start another \selectlanguage with the next paragraph if
1594 #warning we are in need of this. This should be fixed sometime (Jug)
1596 running_font.latexWriteEndChanges(os, basefont, basefont);
1600 // Needed if there is an optional argument but no contents.
1601 if (main_body > 0 && main_body == size()) {
1603 return_value = false;
1606 switch (params().align()) {
1607 case LYX_ALIGN_NONE:
1608 case LYX_ALIGN_BLOCK:
1609 case LYX_ALIGN_LAYOUT:
1610 case LYX_ALIGN_SPECIAL:
1612 case LYX_ALIGN_LEFT:
1613 if (getParLanguage(bparams)->babel() != "hebrew") {
1614 os << "\\end{flushleft}";
1617 os << "\\end{flushright}";
1621 case LYX_ALIGN_RIGHT:
1622 if (getParLanguage(bparams)->babel() != "hebrew") {
1623 os << "\\end{flushright}";
1626 os << "\\end{flushleft}";
1630 case LYX_ALIGN_CENTER:
1631 os << "\\end{center}";
1636 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1637 return return_value;
1641 bool Paragraph::linuxDocConvertChar(char c, string & sgml_string)
1643 bool retval = false;
1645 case Paragraph::META_HFILL:
1646 sgml_string.erase();
1648 case Paragraph::META_NEWLINE:
1652 sgml_string = "&";
1655 sgml_string = "<";
1658 sgml_string = ">";
1661 sgml_string = "$";
1664 sgml_string = "#";
1667 sgml_string = "%";
1670 sgml_string = "[";
1673 sgml_string = "]";
1676 sgml_string = "{";
1679 sgml_string = "}";
1682 sgml_string = "˜";
1685 sgml_string = """;
1688 sgml_string = "\";
1694 case '\0': // Ignore :-)
1695 sgml_string.erase();
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 const & style =
1712 textclasslist.Style(bparams.textclass,
1715 if (style.isEnvironment()){
1716 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1717 os << "\\begin{" << style.latexname() << "}{"
1718 << params().labelWidthString() << "}\n";
1719 } else if (style.labeltype == LABEL_BIBLIO) {
1721 os << "\\begin{" << style.latexname() << "}{"
1722 << bibitemWidest(buf)
1724 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1725 os << "\\begin{" << style.latexname() << '}'
1726 << style.latexparam() << '\n';
1728 os << "\\begin{" << style.latexname() << '}'
1729 << style.latexparam() << '\n';
1732 Paragraph * par = this;
1734 par = par->TeXOnePar(buf, bparams,
1737 if (par && par->params().depth() > params().depth()) {
1738 if (textclasslist.Style(bparams.textclass,
1739 par->layout).isParagraph()
1741 // How to handle this? (Lgb)
1742 //&& !suffixIs(os, "\n\n")
1744 // There should be at least one '\n' already
1745 // but we need there to be two for Standard
1746 // paragraphs that are depth-increment'ed to be
1747 // output correctly. However, tables can
1748 // also be paragraphs so don't adjust them.
1751 // Will it ever harm to have one '\n' too
1752 // many? i.e. that we sometimes will have
1753 // three in a row. (Lgb)
1757 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1760 && par->layout == layout
1761 && par->params().depth() == params().depth());
1763 if (style.isEnvironment()) {
1764 os << "\\end{" << style.latexname() << "}\n";
1767 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1768 return par; // ale970302
1772 bool Paragraph::isHfill(size_type pos) const
1774 return IsHfillChar(getChar(pos));
1778 bool Paragraph::isInset(size_type pos) const
1780 return IsInsetChar(getChar(pos));
1784 bool Paragraph::isNewline(size_type pos) const
1786 return pos >= 0 && IsNewlineChar(getChar(pos));
1790 bool Paragraph::isSeparator(size_type pos) const
1792 return IsSeparatorChar(getChar(pos));
1796 bool Paragraph::isLineSeparator(size_type pos) const
1798 return IsLineSeparatorChar(getChar(pos));
1802 bool Paragraph::isKomma(size_type pos) const
1804 return IsKommaChar(getChar(pos));
1808 /// Used by the spellchecker
1809 bool Paragraph::isLetter(Paragraph::size_type pos) const
1811 value_type const c = getChar(pos);
1812 if (IsLetterChar(c))
1814 // '\0' is not a letter, allthough every string contains "" (below)
1817 // We want to pass the ' and escape chars to ispell
1818 string const extra = lyxrc.isp_esc_chars + '\'';
1819 char ch[2] = { c, 0 };
1820 return contains(extra, ch);
1824 bool Paragraph::isWord(size_type pos ) const
1826 return IsWordChar(getChar(pos)) ;
1831 Paragraph::getParLanguage(BufferParams const & bparams) const
1834 Language const * lang = getFirstFontSettings().language();
1835 #warning We should make this somewhat better, any ideas? (Jug)
1836 if (lang == inherit_language || lang == ignore_language)
1837 lang = bparams.language;
1839 } else if (previous_)
1840 return previous_->getParLanguage(bparams);
1842 return bparams.language;
1846 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1848 return lyxrc.rtl_support
1849 && getParLanguage(bparams)->RightToLeft();
1853 void Paragraph::changeLanguage(BufferParams const & bparams,
1854 Language const * from, Language const * to)
1856 for (size_type i = 0; i < size(); ++i) {
1857 LyXFont font = getFontSettings(bparams, i);
1858 if (font.language() == from) {
1859 font.setLanguage(to);
1866 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1868 Language const * doc_language = bparams.language;
1869 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1870 cit != pimpl_->fontlist.end(); ++cit)
1871 if (cit->font().language() != inherit_language &&
1872 cit->font().language() != ignore_language &&
1873 cit->font().language() != doc_language)
1879 // Convert the paragraph to a string.
1880 // Used for building the table of contents
1881 string const Paragraph::asString(Buffer const * buffer, bool label)
1883 BufferParams const & bparams = buffer->params;
1885 if (label && !params().labelString().empty())
1886 s += params().labelString() + ' ';
1887 string::size_type const len = s.size();
1889 for (Paragraph::size_type i = 0; i < size(); ++i) {
1890 value_type c = getChar(i);
1893 else if (c == META_INSET &&
1894 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1896 getInset(i)->ascii(buffer, ost);
1897 s += subst(ost.str().c_str(),'\n',' ');
1901 if (isRightToLeftPar(bparams))
1902 reverse(s.begin() + len,s.end());
1908 string const Paragraph::asString(Buffer const * buffer,
1909 Paragraph::size_type beg,
1910 Paragraph::size_type end)
1914 if (beg == 0 && !params().labelString().empty())
1915 ost << params().labelString() << ' ';
1917 for (Paragraph::size_type i = beg; i < end; ++i) {
1918 value_type const c = getUChar(buffer->params, i);
1921 else if (c == META_INSET) {
1922 getInset(i)->ascii(buffer, ost);
1926 return ost.str().c_str();
1930 void Paragraph::setInsetOwner(Inset * i)
1932 pimpl_->inset_owner = i;
1933 for (InsetList::const_iterator cit = insetlist.begin();
1934 cit != insetlist.end(); ++cit) {
1936 cit->inset->setOwner(i);
1941 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1944 for (InsetList::const_iterator cit = insetlist.begin();
1945 cit != insetlist.end(); ++cit) {
1947 if (cit->inset->isTextInset()) {
1948 static_cast<UpdatableInset *>
1949 (cit->inset)->deleteLyXText(bv, true);
1956 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1959 for (InsetList::const_iterator cit = insetlist.begin();
1960 cit != insetlist.end(); ++cit) {
1962 if (cit->inset->isTextInset()) {
1963 static_cast<UpdatableInset *>
1964 (cit->inset)->resizeLyXText(bv, true);
1971 void Paragraph::setContentsFromPar(Paragraph * par)
1973 pimpl_->setContentsFromPar(par);
1977 Paragraph::size_type Paragraph::size() const
1979 return pimpl_->size();
1983 Paragraph::value_type
1984 Paragraph::getChar(Paragraph::size_type pos) const
1986 return pimpl_->getChar(pos);
1990 int Paragraph::id() const
1996 void Paragraph::id(int id_arg)
1998 pimpl_->id_ = id_arg;
2002 LyXTextClass::LayoutList::size_type Paragraph::getLayout() const
2008 bool Paragraph::isFirstInSequence() const
2010 Paragraph const * dhook = depthHook(getDepth());
2011 return (dhook == this
2012 || dhook->getLayout() != getLayout()
2013 || dhook->getDepth() != getDepth());
2017 Inset * Paragraph::inInset() const
2019 return pimpl_->inset_owner;
2023 void Paragraph::clearContents()
2029 void Paragraph::setCounter(int i, int v)
2031 pimpl_->counter_[i] = v;
2035 int Paragraph::getCounter(int i) const
2037 return pimpl_->counter_[i];
2041 void Paragraph::incCounter(int i)
2043 pimpl_->counter_[i]++;
2047 void Paragraph::setChar(size_type pos, value_type c)
2049 pimpl_->setChar(pos, c);
2053 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2058 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2060 return inset_iterator(insetlist.begin());
2064 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2066 return inset_iterator(insetlist.end());
2070 ParagraphParameters & Paragraph::params()
2072 return pimpl_->params;
2076 ParagraphParameters const & Paragraph::params() const
2078 return pimpl_->params;
2082 Paragraph * Paragraph::getParFromID(int id) const
2084 return pimpl_->getParFromID(id);