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
616 lyx::Assert(pos >= 0);
618 LyXLayout const & layout =
619 textclasslist.Style(bparams.textclass,
621 Paragraph::size_type main_body = 0;
622 if (layout.labeltype == LABEL_MANUAL)
623 main_body = beginningOfMainBody();
627 layoutfont = layout.labelfont;
629 layoutfont = layout.font;
631 LyXFont tmpfont = getFontSettings(bparams, pos);
632 tmpfont.realize(layoutfont, bparams.language);
634 return pimpl_->realizeFont(tmpfont, bparams);
638 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
640 LyXLayout const & layout =
641 textclasslist.Style(bparams.textclass, getLayout());
643 LyXFont tmpfont = layout.labelfont;
644 tmpfont.setLanguage(getParLanguage(bparams));
646 return pimpl_->realizeFont(tmpfont, bparams);
650 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
652 LyXLayout const & layout =
653 textclasslist.Style(bparams.textclass,
656 LyXFont tmpfont = layout.font;
657 tmpfont.setLanguage(getParLanguage(bparams));
659 return pimpl_->realizeFont(tmpfont, bparams);
663 /// Returns the height of the highest font in range
665 Paragraph::highestFontInRange(Paragraph::size_type startpos,
666 Paragraph::size_type endpos,
667 LyXFont::FONT_SIZE const def_size) const
669 if (pimpl_->fontlist.empty())
672 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
673 Pimpl::FontTable end_search(endpos, LyXFont());
674 Pimpl::FontList::const_iterator end_it =
675 lower_bound(pimpl_->fontlist.begin(),
676 pimpl_->fontlist.end(),
677 end_search, Pimpl::matchFT());
678 if (end_it != pimpl_->fontlist.end())
681 Pimpl::FontTable start_search(startpos, LyXFont());
682 Pimpl::FontList::const_iterator cit =
683 lower_bound(pimpl_->fontlist.begin(),
684 pimpl_->fontlist.end(),
685 start_search, Pimpl::matchFT());
687 for (; cit != end_it; ++cit) {
688 LyXFont::FONT_SIZE size = cit->font().size();
689 if (size == LyXFont::INHERIT_SIZE)
691 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
698 Paragraph::value_type
699 Paragraph::getUChar(BufferParams const & bparams,
700 Paragraph::size_type pos) const
702 value_type c = getChar(pos);
703 if (!lyxrc.rtl_support)
733 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
740 void Paragraph::setFont(Paragraph::size_type pos,
741 LyXFont const & font)
743 lyx::Assert(pos <= size());
745 // First, reduce font against layout/label font
746 // Update: The SetCharFont() routine in text2.C already
747 // reduces font, so we don't need to do that here. (Asger)
748 // No need to simplify this because it will disappear
749 // in a new kernel. (Asger)
750 // Next search font table
752 Pimpl::FontTable search_font(pos, LyXFont());
753 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
754 pimpl_->fontlist.end(),
755 search_font, Pimpl::matchFT());
756 unsigned int i = it - pimpl_->fontlist.begin();
757 bool notfound = it == pimpl_->fontlist.end();
759 if (!notfound && pimpl_->fontlist[i].font() == font)
762 bool begin = pos == 0 || notfound ||
763 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
764 // Is position pos is a beginning of a font block?
765 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
766 // Is position pos is the end of a font block?
767 if (begin && end) { // A single char block
768 if (i + 1 < pimpl_->fontlist.size() &&
769 pimpl_->fontlist[i + 1].font() == font) {
770 // Merge the singleton block with the next block
771 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
772 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
773 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
774 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
775 // Merge the singleton block with the previous block
776 pimpl_->fontlist[i - 1].pos(pos);
777 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
779 pimpl_->fontlist[i].font(font);
781 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
782 pimpl_->fontlist[i - 1].pos(pos);
784 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
785 Pimpl::FontTable(pos, font));
787 pimpl_->fontlist[i].pos(pos - 1);
788 if (!(i + 1 < pimpl_->fontlist.size() &&
789 pimpl_->fontlist[i + 1].font() == font))
790 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
791 Pimpl::FontTable(pos, font));
792 } else { // The general case. The block is splitted into 3 blocks
793 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
794 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
795 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
796 Pimpl::FontTable(pos, font));
802 void Paragraph::next(Paragraph * p)
808 // This function is able to hide closed footnotes.
809 Paragraph * Paragraph::next()
815 Paragraph const * Paragraph::next() const
821 void Paragraph::previous(Paragraph * p)
827 // This function is able to hide closed footnotes.
828 Paragraph * Paragraph::previous()
834 // This function is able to hide closed footnotes.
835 Paragraph const * Paragraph::previous() const
841 void Paragraph::breakParagraph(BufferParams const & bparams,
842 Paragraph::size_type pos,
845 // create a new paragraph
846 Paragraph * tmp = new Paragraph(this);
847 // remember to set the inset_owner
848 tmp->setInsetOwner(inInset());
850 // this is an idea for a more userfriendly layout handling, I will
851 // see what the users say
853 // layout stays the same with latex-environments
855 tmp->setOnlyLayout(layout);
856 tmp->setLabelWidthString(params().labelWidthString());
859 if (size() > pos || !size() || flag == 2) {
860 tmp->setOnlyLayout(layout);
861 tmp->params().align(params().align());
862 tmp->setLabelWidthString(params().labelWidthString());
864 tmp->params().lineBottom(params().lineBottom());
865 params().lineBottom(false);
866 tmp->params().pagebreakBottom(params().pagebreakBottom());
867 params().pagebreakBottom(false);
868 tmp->params().spaceBottom(params().spaceBottom());
869 params().spaceBottom(VSpace(VSpace::NONE));
871 tmp->params().depth(params().depth());
872 tmp->params().noindent(params().noindent());
874 // copy everything behind the break-position
875 // to the new paragraph
876 size_type pos_end = pimpl_->size() - 1;
879 for (; i <= pos_end; ++i) {
880 cutIntoMinibuffer(bparams, i);
881 if (tmp->insertFromMinibuffer(j - pos))
884 for (i = pos_end; i >= pos; --i) {
889 // just an idea of me
891 tmp->params().lineTop(params().lineTop());
892 tmp->params().pagebreakTop(params().pagebreakTop());
893 tmp->params().spaceTop(params().spaceTop());
894 tmp->bibkey = bibkey;
896 // layout stays the same with latex-environments
898 setOnlyLayout(tmp->layout);
899 setLabelWidthString(tmp->params().labelWidthString());
900 params().depth(tmp->params().depth());
906 void Paragraph::makeSameLayout(Paragraph const * par)
908 layout = par->layout;
910 params() = par->params();
914 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass)
916 if (textclasslist.Style(tclass, getLayout()).free_spacing)
921 && (isNewline(0) || isLineSeparator(0))){
930 bool Paragraph::hasSameLayout(Paragraph const * par) const
933 par->layout == layout &&
934 params().sameLayout(par->params());
938 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
939 Paragraph::size_type pos)
941 // create a new paragraph
942 Paragraph * tmp = new Paragraph(this);
943 tmp->makeSameLayout(this);
945 // When can pos > Last()?
946 // I guess pos == Last() is possible.
948 // copy everything behind the break-position to the new
950 size_type pos_end = pimpl_->size() - 1;
954 for (size_type i = pos, j = pos; i <= pos_end; ++i) {
955 cutIntoMinibuffer(bparams, i);
956 if (tmp->insertFromMinibuffer(j - pos))
960 for (size_type k = pos_end; k >= pos; --k) {
967 // Be carefull, this does not make any check at all.
968 // This method has wrong name, it combined this par with the next par.
969 // In that sense it is the reverse of break paragraph. (Lgb)
970 void Paragraph::pasteParagraph(BufferParams const & bparams)
972 // copy the next paragraph to this one
973 Paragraph * the_next = next();
975 // first the DTP-stuff
976 params().lineBottom(the_next->params().lineBottom());
977 params().spaceBottom(the_next->params().spaceBottom());
978 params().pagebreakBottom(the_next->params().pagebreakBottom());
980 size_type pos_end = the_next->pimpl_->size() - 1;
981 size_type pos_insert = size();
983 // ok, now copy the paragraph
984 for (size_type i = 0, j = 0; i <= pos_end; ++i) {
985 the_next->cutIntoMinibuffer(bparams, i);
986 if (insertFromMinibuffer(pos_insert + j))
990 // delete the next paragraph
991 Paragraph * ppar = the_next->previous_;
992 Paragraph * npar = the_next->next_;
998 int Paragraph::getEndLabel(BufferParams const & bparams) const
1000 Paragraph const * par = this;
1001 depth_type par_depth = getDepth();
1003 LyXTextClass::LayoutList::size_type layout = par->getLayout();
1004 int const endlabeltype =
1005 textclasslist.Style(bparams.textclass,
1006 layout).endlabeltype;
1007 if (endlabeltype != END_LABEL_NO_LABEL) {
1009 return endlabeltype;
1011 depth_type const next_depth = next_->getDepth();
1012 if (par_depth > next_depth ||
1013 (par_depth == next_depth
1014 && layout != next_->getLayout()))
1015 return endlabeltype;
1020 par = par->outerHook();
1022 par_depth = par->getDepth();
1024 return END_LABEL_NO_LABEL;
1028 Paragraph::depth_type Paragraph::getDepth() const
1030 return params().depth();
1034 char Paragraph::getAlign() const
1036 return params().align();
1040 string const & Paragraph::getLabelstring() const
1042 return params().labelString();
1046 int Paragraph::getFirstCounter(int i) const
1048 return pimpl_->counter_[i];
1052 // the next two functions are for the manual labels
1053 string const Paragraph::getLabelWidthString() const
1055 if (!params().labelWidthString().empty())
1056 return params().labelWidthString();
1058 return _("Senseless with this layout!");
1062 void Paragraph::setLabelWidthString(string const & s)
1064 params().labelWidthString(s);
1068 void Paragraph::setOnlyLayout(LyXTextClass::size_type new_layout)
1070 layout = new_layout;
1074 void Paragraph::setLayout(LyXTextClass::size_type new_layout)
1076 layout = new_layout;
1077 params().labelWidthString(string());
1078 params().align(LYX_ALIGN_LAYOUT);
1079 params().spaceTop(VSpace(VSpace::NONE));
1080 params().spaceBottom(VSpace(VSpace::NONE));
1081 params().spacing(Spacing(Spacing::Default));
1085 // if the layout of a paragraph contains a manual label, the beginning of the
1086 // main body is the beginning of the second word. This is what the par-
1087 // function returns. If the layout does not contain a label, the main
1088 // body always starts with position 0. This differentiation is necessary,
1089 // because there cannot be a newline or a blank <= the beginning of the
1090 // main body in TeX.
1092 int Paragraph::beginningOfMainBody() const
1094 // Unroll the first two cycles of the loop
1095 // and remember the previous character to
1096 // remove unnecessary GetChar() calls
1099 && getChar(i) != Paragraph::META_NEWLINE) {
1101 char previous_char = 0;
1104 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1105 // Yes, this ^ is supposed to be "= " not "=="
1108 && previous_char != ' '
1109 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1111 previous_char = temp;
1120 Paragraph * Paragraph::depthHook(depth_type depth)
1122 Paragraph * newpar = this;
1125 newpar = newpar->previous();
1126 } while (newpar && newpar->getDepth() > depth);
1129 if (previous() || getDepth())
1130 lyxerr << "ERROR (Paragraph::DepthHook): "
1139 Paragraph const * Paragraph::depthHook(depth_type depth) const
1141 Paragraph const * newpar = this;
1144 newpar = newpar->previous();
1145 } while (newpar && newpar->getDepth() > depth);
1148 if (previous() || getDepth())
1149 lyxerr << "ERROR (Paragraph::DepthHook): "
1157 Paragraph * Paragraph::outerHook()
1161 return depthHook(depth_type(getDepth() - 1));
1164 Paragraph const * Paragraph::outerHook() const
1168 return depthHook(depth_type(getDepth() - 1));
1171 int Paragraph::autoDeleteInsets()
1174 InsetList::size_type index = 0;
1175 while (index < insetlist.size()) {
1176 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1177 erase(insetlist[index].pos);
1178 // Erase() calls to insetlist.erase(&insetlist[index])
1179 // so index shouldn't be increased.
1188 Paragraph::inset_iterator
1189 Paragraph::InsetIterator(Paragraph::size_type pos)
1191 InsetTable search_inset(pos, 0);
1192 InsetList::iterator it = lower_bound(insetlist.begin(),
1194 search_inset, Pimpl::matchIT());
1195 return inset_iterator(it);
1199 // returns -1 if inset not found
1200 int Paragraph::getPositionOfInset(Inset * inset) const
1203 for (InsetList::const_iterator cit = insetlist.begin();
1204 cit != insetlist.end(); ++cit) {
1205 if (cit->inset == inset) {
1209 if (inset == bibkey)
1216 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1217 BufferParams const & bparams,
1218 ostream & os, TexRow & texrow,
1221 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1222 LyXLayout const & style =
1223 textclasslist.Style(bparams.textclass,
1226 bool further_blank_line = false;
1228 if (params().startOfAppendix()) {
1229 os << "\\appendix\n";
1233 if (!params().spacing().isDefault()
1234 && (!previous() || !previous()->hasSameLayout(this))) {
1235 os << params().spacing().writeEnvirBegin() << "\n";
1239 if (tex_code_break_column && style.isCommand()){
1244 if (params().pagebreakTop()) {
1246 further_blank_line = true;
1248 if (params().spaceTop().kind() != VSpace::NONE) {
1249 os << params().spaceTop().asLatexCommand(bparams);
1250 further_blank_line = true;
1253 if (params().lineTop()) {
1254 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1255 << "\\vspace{-1\\parskip}";
1256 further_blank_line = true;
1259 if (further_blank_line){
1264 Language const * language = getParLanguage(bparams);
1265 Language const * doc_language = bparams.language;
1266 Language const * previous_language = previous_
1267 ? previous_->getParLanguage(bparams) : doc_language;
1268 if (language == ignore_language || language == inherit_language)
1269 lyxerr << "1:" << language->lang() << endl;
1270 if (language->babel() != doc_language->babel() &&
1271 language->babel() != previous_language->babel()) {
1272 os << subst(lyxrc.language_command_begin, "$$lang",
1278 if (bparams.inputenc == "auto" &&
1279 language->encoding() != previous_language->encoding()) {
1280 os << "\\inputencoding{"
1281 << language->encoding()->LatexName()
1286 switch (style.latextype) {
1289 << style.latexname()
1290 << style.latexparam();
1292 case LATEX_ITEM_ENVIRONMENT:
1294 bibkey->latex(buf, os, false, false);
1298 case LATEX_LIST_ENVIRONMENT:
1305 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1307 // Make sure that \\par is done with the font of the last
1308 // character if this has another size as the default.
1309 // This is necessary because LaTeX (and LyX on the screen)
1310 // calculates the space between the baselines according
1311 // to this font. (Matthias)
1313 // Is this really needed ? (Dekel)
1314 // We do not need to use to change the font for the last paragraph
1315 // or for a command.
1316 LyXFont const font =
1318 ? getLayoutFont(bparams)
1319 : getFont(bparams, size() - 1));
1321 bool is_command = textclasslist.Style(bparams.textclass,
1322 getLayout()).isCommand();
1323 if (style.resfont.size() != font.size() && next_ && !is_command) {
1326 os << "\\" << font.latexSize() << " \\par}";
1327 } else if (need_par) {
1329 } else if (is_command)
1332 if (language->babel() != doc_language->babel() &&
1334 next_->getParLanguage(bparams)->babel() != language->babel()))
1337 << subst(lyxrc.language_command_end, "$$lang",
1338 doc_language->babel());
1341 switch (style.latextype) {
1342 case LATEX_ITEM_ENVIRONMENT:
1343 case LATEX_LIST_ENVIRONMENT:
1344 if (next_ && (params().depth() < next_->params().depth())) {
1349 case LATEX_ENVIRONMENT:
1350 // if its the last paragraph of the current environment
1351 // skip it otherwise fall through
1353 && (next_->layout != layout
1354 || next_->params().depth() != params().depth()))
1357 // we don't need it for the last paragraph!!!
1358 // or for tables in floats
1359 // -- effectively creates a \par where there isn't one which
1360 // breaks a \subfigure or \subtable.
1367 further_blank_line = false;
1368 if (params().lineBottom()) {
1369 os << "\\lyxline{\\" << font.latexSize() << '}';
1370 further_blank_line = true;
1373 if (params().spaceBottom().kind() != VSpace::NONE) {
1374 os << params().spaceBottom().asLatexCommand(bparams);
1375 further_blank_line = true;
1378 if (params().pagebreakBottom()) {
1380 further_blank_line = true;
1383 if (further_blank_line){
1388 if (!params().spacing().isDefault()
1389 && (!next_ || !next_->hasSameLayout(this))) {
1390 os << params().spacing().writeEnvirEnd() << "\n";
1394 // we don't need it for the last paragraph!!!
1400 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1405 // This one spits out the text of the paragraph
1406 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1407 BufferParams const & bparams,
1408 ostream & os, TexRow & texrow,
1411 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1413 bool return_value = false;
1415 LyXLayout const & style =
1416 textclasslist.Style(bparams.textclass,
1420 // Maybe we have to create a optional argument.
1421 size_type main_body;
1422 if (style.labeltype != LABEL_MANUAL)
1425 main_body = beginningOfMainBody();
1429 if (main_body > 0) {
1432 basefont = getLabelFont(bparams);
1434 basefont = getLayoutFont(bparams);
1438 && !pimpl_->size()) {
1439 if (style.isCommand()) {
1445 moving_arg |= style.needprotect;
1447 // Which font is currently active?
1448 LyXFont running_font(basefont);
1449 // Do we have an open font change?
1450 bool open_font = false;
1452 texrow.start(this, 0);
1454 for (size_type i = 0; i < size(); ++i) {
1456 // First char in paragraph or after label?
1457 if (i == main_body) {
1458 if (main_body > 0) {
1460 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1463 basefont = getLayoutFont(bparams);
1464 running_font = basefont;
1468 if (style.isCommand()) {
1473 if (params().noindent()) {
1474 os << "\\noindent ";
1477 switch (params().align()) {
1478 case LYX_ALIGN_NONE:
1479 case LYX_ALIGN_BLOCK:
1480 case LYX_ALIGN_LAYOUT:
1481 case LYX_ALIGN_SPECIAL:
1483 case LYX_ALIGN_LEFT:
1484 if (getParLanguage(bparams)->babel() != "hebrew") {
1485 os << "\\begin{flushleft}";
1488 os << "\\begin{flushright}";
1492 case LYX_ALIGN_RIGHT:
1493 if (getParLanguage(bparams)->babel() != "hebrew") {
1494 os << "\\begin{flushright}";
1497 os << "\\begin{flushleft}";
1501 case LYX_ALIGN_CENTER:
1502 os << "\\begin{center}";
1508 value_type c = getChar(i);
1510 // Fully instantiated font
1511 LyXFont font = getFont(bparams, i);
1513 LyXFont const last_font = running_font;
1515 // Spaces at end of font change are simulated to be
1516 // outside font change, i.e. we write "\textXX{text} "
1517 // rather than "\textXX{text }". (Asger)
1518 if (open_font && c == ' ' && i <= size() - 2) {
1519 LyXFont const next_font = getFont(bparams, i + 1);
1520 if (next_font != running_font
1521 && next_font != font) {
1526 // We end font definition before blanks
1527 if (font != running_font && open_font) {
1528 column += running_font.latexWriteEndChanges(os,
1530 (i == main_body-1) ? basefont : font);
1531 running_font = basefont;
1535 // Blanks are printed before start of fontswitch
1537 // Do not print the separation of the optional argument
1538 if (i != main_body - 1) {
1539 pimpl_->simpleTeXBlanks(os, texrow, i,
1540 column, font, style);
1544 // Do we need to change font?
1545 if (font != running_font && i != main_body - 1) {
1546 column += font.latexWriteStartChanges(os, basefont,
1548 running_font = font;
1552 if (c == Paragraph::META_NEWLINE) {
1553 // newlines are handled differently here than
1554 // the default in SimpleTeXSpecialChars().
1555 if (!style.newline_allowed) {
1559 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1562 basefont = getLayoutFont(bparams);
1563 running_font = basefont;
1564 if (font.family() ==
1565 LyXFont::TYPEWRITER_FAMILY) {
1573 texrow.start(this, i + 1);
1576 pimpl_->simpleTeXSpecialChars(buf, bparams,
1577 os, texrow, moving_arg,
1579 basefont, open_font,
1580 style, i, column, c);
1584 // If we have an open font definition, we have to close it
1586 #ifdef FIXED_LANGUAGE_END_DETECTION
1589 .latexWriteEndChanges(os, basefont,
1590 next_->getFont(bparams,
1593 running_font.latexWriteEndChanges(os, basefont,
1597 #ifdef WITH_WARNINGS
1598 #warning For now we ALWAYS have to close the foreign font settings if they are
1599 #warning there as we start another \selectlanguage with the next paragraph if
1600 #warning we are in need of this. This should be fixed sometime (Jug)
1602 running_font.latexWriteEndChanges(os, basefont, basefont);
1606 // Needed if there is an optional argument but no contents.
1607 if (main_body > 0 && main_body == size()) {
1609 return_value = false;
1612 switch (params().align()) {
1613 case LYX_ALIGN_NONE:
1614 case LYX_ALIGN_BLOCK:
1615 case LYX_ALIGN_LAYOUT:
1616 case LYX_ALIGN_SPECIAL:
1618 case LYX_ALIGN_LEFT:
1619 if (getParLanguage(bparams)->babel() != "hebrew") {
1620 os << "\\end{flushleft}";
1623 os << "\\end{flushright}";
1627 case LYX_ALIGN_RIGHT:
1628 if (getParLanguage(bparams)->babel() != "hebrew") {
1629 os << "\\end{flushright}";
1632 os << "\\end{flushleft}";
1636 case LYX_ALIGN_CENTER:
1637 os << "\\end{center}";
1642 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1643 return return_value;
1647 bool Paragraph::linuxDocConvertChar(char c, string & sgml_string)
1649 bool retval = false;
1651 case Paragraph::META_HFILL:
1652 sgml_string.erase();
1654 case Paragraph::META_NEWLINE:
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 = "˜";
1691 sgml_string = """;
1694 sgml_string = "\";
1700 case '\0': // Ignore :-)
1701 sgml_string.erase();
1711 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1712 BufferParams const & bparams,
1713 ostream & os, TexRow & texrow)
1715 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1717 LyXLayout const & style =
1718 textclasslist.Style(bparams.textclass,
1721 if (style.isEnvironment()){
1722 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1723 os << "\\begin{" << style.latexname() << "}{"
1724 << params().labelWidthString() << "}\n";
1725 } else if (style.labeltype == LABEL_BIBLIO) {
1727 os << "\\begin{" << style.latexname() << "}{"
1728 << bibitemWidest(buf)
1730 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1731 os << "\\begin{" << style.latexname() << '}'
1732 << style.latexparam() << '\n';
1734 os << "\\begin{" << style.latexname() << '}'
1735 << style.latexparam() << '\n';
1738 Paragraph * par = this;
1740 par = par->TeXOnePar(buf, bparams,
1743 if (par && par->params().depth() > params().depth()) {
1744 if (textclasslist.Style(bparams.textclass,
1745 par->layout).isParagraph()
1747 // How to handle this? (Lgb)
1748 //&& !suffixIs(os, "\n\n")
1750 // There should be at least one '\n' already
1751 // but we need there to be two for Standard
1752 // paragraphs that are depth-increment'ed to be
1753 // output correctly. However, tables can
1754 // also be paragraphs so don't adjust them.
1757 // Will it ever harm to have one '\n' too
1758 // many? i.e. that we sometimes will have
1759 // three in a row. (Lgb)
1763 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1766 && par->layout == layout
1767 && par->params().depth() == params().depth());
1769 if (style.isEnvironment()) {
1770 os << "\\end{" << style.latexname() << "}\n";
1773 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1774 return par; // ale970302
1778 bool Paragraph::isHfill(size_type pos) const
1780 return IsHfillChar(getChar(pos));
1784 bool Paragraph::isInset(size_type pos) const
1786 return IsInsetChar(getChar(pos));
1790 bool Paragraph::isNewline(size_type pos) const
1792 return pos >= 0 && IsNewlineChar(getChar(pos));
1796 bool Paragraph::isSeparator(size_type pos) const
1798 return IsSeparatorChar(getChar(pos));
1802 bool Paragraph::isLineSeparator(size_type pos) const
1804 return IsLineSeparatorChar(getChar(pos));
1808 bool Paragraph::isKomma(size_type pos) const
1810 return IsKommaChar(getChar(pos));
1814 /// Used by the spellchecker
1815 bool Paragraph::isLetter(Paragraph::size_type pos) const
1817 value_type const c = getChar(pos);
1818 if (IsLetterChar(c))
1820 // '\0' is not a letter, allthough every string contains "" (below)
1823 // We want to pass the ' and escape chars to ispell
1824 string const extra = lyxrc.isp_esc_chars + '\'';
1825 char ch[2] = { c, 0 };
1826 return contains(extra, ch);
1830 bool Paragraph::isWord(size_type pos ) const
1832 return IsWordChar(getChar(pos)) ;
1837 Paragraph::getParLanguage(BufferParams const & bparams) const
1840 Language const * lang = getFirstFontSettings().language();
1841 #warning We should make this somewhat better, any ideas? (Jug)
1842 if (lang == inherit_language || lang == ignore_language)
1843 lang = bparams.language;
1845 } else if (previous_)
1846 return previous_->getParLanguage(bparams);
1848 return bparams.language;
1852 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1854 return lyxrc.rtl_support
1855 && getParLanguage(bparams)->RightToLeft();
1859 void Paragraph::changeLanguage(BufferParams const & bparams,
1860 Language const * from, Language const * to)
1862 for (size_type i = 0; i < size(); ++i) {
1863 LyXFont font = getFontSettings(bparams, i);
1864 if (font.language() == from) {
1865 font.setLanguage(to);
1872 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1874 Language const * doc_language = bparams.language;
1875 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1876 cit != pimpl_->fontlist.end(); ++cit)
1877 if (cit->font().language() != inherit_language &&
1878 cit->font().language() != ignore_language &&
1879 cit->font().language() != doc_language)
1885 // Convert the paragraph to a string.
1886 // Used for building the table of contents
1887 string const Paragraph::asString(Buffer const * buffer, bool label)
1889 BufferParams const & bparams = buffer->params;
1891 if (label && !params().labelString().empty())
1892 s += params().labelString() + ' ';
1893 string::size_type const len = s.size();
1895 for (Paragraph::size_type i = 0; i < size(); ++i) {
1896 value_type c = getChar(i);
1899 else if (c == META_INSET &&
1900 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1902 getInset(i)->ascii(buffer, ost);
1903 s += subst(ost.str().c_str(),'\n',' ');
1907 if (isRightToLeftPar(bparams))
1908 reverse(s.begin() + len,s.end());
1914 string const Paragraph::asString(Buffer const * buffer,
1915 Paragraph::size_type beg,
1916 Paragraph::size_type end)
1920 if (beg == 0 && !params().labelString().empty())
1921 ost << params().labelString() << ' ';
1923 for (Paragraph::size_type i = beg; i < end; ++i) {
1924 value_type const c = getUChar(buffer->params, i);
1927 else if (c == META_INSET) {
1928 getInset(i)->ascii(buffer, ost);
1932 return ost.str().c_str();
1936 void Paragraph::setInsetOwner(Inset * i)
1938 pimpl_->inset_owner = i;
1939 for (InsetList::const_iterator cit = insetlist.begin();
1940 cit != insetlist.end(); ++cit) {
1942 cit->inset->setOwner(i);
1947 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1950 for (InsetList::const_iterator cit = insetlist.begin();
1951 cit != insetlist.end(); ++cit) {
1953 if (cit->inset->isTextInset()) {
1954 static_cast<UpdatableInset *>
1955 (cit->inset)->deleteLyXText(bv, true);
1962 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1965 for (InsetList::const_iterator cit = insetlist.begin();
1966 cit != insetlist.end(); ++cit) {
1968 if (cit->inset->isTextInset()) {
1969 static_cast<UpdatableInset *>
1970 (cit->inset)->resizeLyXText(bv, true);
1977 void Paragraph::setContentsFromPar(Paragraph * par)
1979 pimpl_->setContentsFromPar(par);
1983 Paragraph::size_type Paragraph::size() const
1985 return pimpl_->size();
1989 Paragraph::value_type
1990 Paragraph::getChar(Paragraph::size_type pos) const
1992 return pimpl_->getChar(pos);
1996 int Paragraph::id() const
2002 void Paragraph::id(int id_arg)
2004 pimpl_->id_ = id_arg;
2008 LyXTextClass::LayoutList::size_type Paragraph::getLayout() const
2014 bool Paragraph::isFirstInSequence() const
2016 Paragraph const * dhook = depthHook(getDepth());
2017 return (dhook == this
2018 || dhook->getLayout() != getLayout()
2019 || dhook->getDepth() != getDepth());
2023 Inset * Paragraph::inInset() const
2025 return pimpl_->inset_owner;
2029 void Paragraph::clearContents()
2035 void Paragraph::setCounter(int i, int v)
2037 pimpl_->counter_[i] = v;
2041 int Paragraph::getCounter(int i) const
2043 return pimpl_->counter_[i];
2047 void Paragraph::incCounter(int i)
2049 pimpl_->counter_[i]++;
2053 void Paragraph::setChar(size_type pos, value_type c)
2055 pimpl_->setChar(pos, c);
2059 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2064 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2066 return inset_iterator(insetlist.begin());
2070 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2072 return inset_iterator(insetlist.end());
2076 ParagraphParameters & Paragraph::params()
2078 return pimpl_->params;
2082 ParagraphParameters const & Paragraph::params() const
2084 return pimpl_->params;
2088 Paragraph * Paragraph::getParFromID(int id) const
2090 return pimpl_->getParFromID(id);