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 for (InsetList::const_iterator cit = insetlist.begin();
385 cit != insetlist.end(); ++cit) {
387 cit->inset->validate(features);
392 // First few functions needed for cut and paste and paragraph breaking.
393 void Paragraph::copyIntoMinibuffer(Buffer const & buffer,
394 Paragraph::size_type pos) const
396 BufferParams bparams = buffer.params;
398 minibuffer_char = getChar(pos);
399 minibuffer_font = getFontSettings(bparams, pos);
400 minibuffer_inset = 0;
401 if (minibuffer_char == Paragraph::META_INSET) {
403 minibuffer_inset = getInset(pos)->clone(buffer);
405 minibuffer_inset = 0;
406 minibuffer_char = ' ';
407 // This reflects what GetInset() does (ARRae)
413 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
414 Paragraph::size_type pos)
416 minibuffer_char = getChar(pos);
417 minibuffer_font = getFontSettings(bparams, pos);
418 minibuffer_inset = 0;
419 if (minibuffer_char == Paragraph::META_INSET) {
421 minibuffer_inset = getInset(pos);
422 // This is a little hack since I want exactly
423 // the inset, not just a clone. Otherwise
424 // the inset would be deleted when calling Erase(pos)
426 InsetTable search_elem(pos, 0);
427 InsetList::iterator it =
428 lower_bound(insetlist.begin(),
430 search_elem, Pimpl::matchIT());
431 if (it != insetlist.end() && it->pos == pos)
434 minibuffer_inset = 0;
435 minibuffer_char = ' ';
436 // This reflects what GetInset() does (ARRae)
441 // Erase(pos); now the caller is responsible for that.
445 bool Paragraph::insertFromMinibuffer(Paragraph::size_type pos)
447 if ((minibuffer_char == Paragraph::META_INSET) &&
448 !insetAllowed(minibuffer_inset->lyxCode()))
450 if (minibuffer_char == Paragraph::META_INSET)
451 insertInset(pos, minibuffer_inset, minibuffer_font);
453 LyXFont f = minibuffer_font;
454 if (checkInsertChar(f))
455 insertChar(pos, minibuffer_char, f);
464 void Paragraph::clear()
473 void Paragraph::erase(Paragraph::size_type pos)
479 bool Paragraph::checkInsertChar(LyXFont & font)
481 if (pimpl_->inset_owner)
482 return pimpl_->inset_owner->checkInsertChar(font);
487 void Paragraph::insertChar(Paragraph::size_type pos,
488 Paragraph::value_type c)
490 LyXFont const f(LyXFont::ALL_INHERIT);
491 insertChar(pos, c, f);
495 void Paragraph::insertChar(Paragraph::size_type pos,
496 Paragraph::value_type c,
497 LyXFont const & font)
499 pimpl_->insertChar(pos, c, font);
503 void Paragraph::insertInset(Paragraph::size_type pos,
506 LyXFont const f(LyXFont::ALL_INHERIT);
507 insertInset(pos, inset, f);
511 void Paragraph::insertInset(Paragraph::size_type pos,
512 Inset * inset, LyXFont const & font)
514 pimpl_->insertInset(pos, inset, font);
518 bool Paragraph::insetAllowed(Inset::Code code)
520 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
522 if (pimpl_->inset_owner)
523 return pimpl_->inset_owner->insetAllowed(code);
528 Inset * Paragraph::getInset(Paragraph::size_type pos)
530 lyx::Assert(pos < size());
533 InsetTable search_inset(pos, 0);
534 InsetList::iterator it = lower_bound(insetlist.begin(),
536 search_inset, Pimpl::matchIT());
537 if (it != insetlist.end() && it->pos == pos)
540 lyxerr << "ERROR (Paragraph::getInset): "
541 "Inset does not exist: " << pos << endl;
544 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
545 // Did this commenting out introduce a bug? So far I have not
546 // see any, please enlighten me. (Lgb)
547 // My guess is that since the inset does not exist, we might
548 // as well replace it with a space to prevent craches. (Asger)
553 Inset const * Paragraph::getInset(Paragraph::size_type pos) const
555 lyx::Assert(pos < size());
558 InsetTable search_inset(pos, 0);
559 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
561 search_inset, Pimpl::matchIT());
562 if (cit != insetlist.end() && cit->pos == pos)
565 lyxerr << "ERROR (Paragraph::GetInset): "
566 "Inset does not exist: " << pos << endl;
568 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
569 // Did this commenting out introduce a bug? So far I have not
570 // see any, please enlighten me. (Lgb)
571 // My guess is that since the inset does not exist, we might
572 // as well replace it with a space to prevent craches. (Asger)
577 // Gets uninstantiated font setting at position.
578 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
579 Paragraph::size_type pos) const
581 lyx::Assert(pos <= size());
583 Pimpl::FontTable search_font(pos, LyXFont());
584 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
585 pimpl_->fontlist.end(),
586 search_font, Pimpl::matchFT());
588 if (cit != pimpl_->fontlist.end()) {
589 retfont = cit->font();
590 } else if (pos == size() && size()) {
591 retfont = getFontSettings(bparams, pos - 1);
593 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
594 #ifdef INHERIT_LANGUAGE
595 if (retfont.language() == inherit_language)
596 retfont.setLanguage(bparams.language);
603 // Gets uninstantiated font setting at position 0
604 LyXFont const Paragraph::getFirstFontSettings() const
607 if (!pimpl_->fontlist.empty())
608 return pimpl_->fontlist[0].font();
611 return LyXFont(LyXFont::ALL_INHERIT);
615 // Gets the fully instantiated font at a given position in a paragraph
616 // This is basically the same function as LyXText::GetFont() in text2.C.
617 // The difference is that this one is used for generating the LaTeX file,
618 // and thus cosmetic "improvements" are disallowed: This has to deliver
619 // the true picture of the buffer. (Asger)
620 // If position is -1, we get the layout font of the paragraph.
621 // If position is -2, we get the font of the manual label of the paragraph.
622 LyXFont const Paragraph::getFont(BufferParams const & bparams,
623 Paragraph::size_type pos) const
625 lyx::Assert(pos >= 0);
627 LyXLayout const & layout =
628 textclasslist.Style(bparams.textclass,
630 Paragraph::size_type main_body = 0;
631 if (layout.labeltype == LABEL_MANUAL)
632 main_body = beginningOfMainBody();
636 layoutfont = layout.labelfont;
638 layoutfont = layout.font;
640 LyXFont tmpfont = getFontSettings(bparams, pos);
641 #ifndef INHERIT_LANGUAGE
642 tmpfont.realize(layoutfont);
644 tmpfont.realize(layoutfont, bparams.language);
647 return pimpl_->realizeFont(tmpfont, bparams);
651 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
653 LyXLayout const & layout =
654 textclasslist.Style(bparams.textclass, getLayout());
656 LyXFont tmpfont = layout.labelfont;
657 tmpfont.setLanguage(getParLanguage(bparams));
659 return pimpl_->realizeFont(tmpfont, bparams);
663 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
665 LyXLayout const & layout =
666 textclasslist.Style(bparams.textclass,
669 LyXFont tmpfont = layout.font;
670 tmpfont.setLanguage(getParLanguage(bparams));
672 return pimpl_->realizeFont(tmpfont, bparams);
676 /// Returns the height of the highest font in range
678 Paragraph::highestFontInRange(Paragraph::size_type startpos,
679 Paragraph::size_type endpos,
680 LyXFont::FONT_SIZE const def_size) const
682 if (pimpl_->fontlist.empty())
685 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
686 Pimpl::FontTable end_search(endpos, LyXFont());
687 Pimpl::FontList::const_iterator end_it =
688 lower_bound(pimpl_->fontlist.begin(),
689 pimpl_->fontlist.end(),
690 end_search, Pimpl::matchFT());
691 if (end_it != pimpl_->fontlist.end())
694 Pimpl::FontTable start_search(startpos, LyXFont());
695 Pimpl::FontList::const_iterator cit =
696 lower_bound(pimpl_->fontlist.begin(),
697 pimpl_->fontlist.end(),
698 start_search, Pimpl::matchFT());
700 for (; cit != end_it; ++cit) {
701 LyXFont::FONT_SIZE size = cit->font().size();
702 if (size == LyXFont::INHERIT_SIZE)
704 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
711 Paragraph::value_type
712 Paragraph::getUChar(BufferParams const & bparams,
713 Paragraph::size_type pos) const
715 value_type c = getChar(pos);
716 if (!lyxrc.rtl_support)
746 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
753 void Paragraph::setFont(Paragraph::size_type pos,
754 LyXFont const & font)
756 lyx::Assert(pos <= size());
758 // First, reduce font against layout/label font
759 // Update: The SetCharFont() routine in text2.C already
760 // reduces font, so we don't need to do that here. (Asger)
761 // No need to simplify this because it will disappear
762 // in a new kernel. (Asger)
763 // Next search font table
765 Pimpl::FontTable search_font(pos, LyXFont());
766 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
767 pimpl_->fontlist.end(),
768 search_font, Pimpl::matchFT());
769 unsigned int i = it - pimpl_->fontlist.begin();
770 bool notfound = it == pimpl_->fontlist.end();
772 if (!notfound && pimpl_->fontlist[i].font() == font)
775 bool begin = pos == 0 || notfound ||
776 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
777 // Is position pos is a beginning of a font block?
778 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
779 // Is position pos is the end of a font block?
780 if (begin && end) { // A single char block
781 if (i + 1 < pimpl_->fontlist.size() &&
782 pimpl_->fontlist[i + 1].font() == font) {
783 // Merge the singleton block with the next block
784 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
785 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
786 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
787 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
788 // Merge the singleton block with the previous block
789 pimpl_->fontlist[i - 1].pos(pos);
790 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
792 pimpl_->fontlist[i].font(font);
794 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
795 pimpl_->fontlist[i - 1].pos(pos);
797 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
798 Pimpl::FontTable(pos, font));
800 pimpl_->fontlist[i].pos(pos - 1);
801 if (!(i + 1 < pimpl_->fontlist.size() &&
802 pimpl_->fontlist[i + 1].font() == font))
803 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
804 Pimpl::FontTable(pos, font));
805 } else { // The general case. The block is splitted into 3 blocks
806 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
807 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
808 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
809 Pimpl::FontTable(pos, font));
815 void Paragraph::next(Paragraph * p)
821 // This function is able to hide closed footnotes.
822 Paragraph * Paragraph::next()
828 Paragraph const * Paragraph::next() const
834 void Paragraph::previous(Paragraph * p)
840 // This function is able to hide closed footnotes.
841 Paragraph * Paragraph::previous()
847 // This function is able to hide closed footnotes.
848 Paragraph const * Paragraph::previous() const
854 void Paragraph::breakParagraph(BufferParams const & bparams,
855 Paragraph::size_type pos,
858 // create a new paragraph
859 Paragraph * tmp = new Paragraph(this);
860 // remember to set the inset_owner
861 tmp->setInsetOwner(inInset());
863 // this is an idea for a more userfriendly layout handling, I will
864 // see what the users say
866 // layout stays the same with latex-environments
868 tmp->setOnlyLayout(layout);
869 tmp->setLabelWidthString(params().labelWidthString());
872 if (size() > pos || !size() || flag == 2) {
873 tmp->setOnlyLayout(layout);
874 tmp->params().align(params().align());
875 tmp->setLabelWidthString(params().labelWidthString());
877 tmp->params().lineBottom(params().lineBottom());
878 params().lineBottom(false);
879 tmp->params().pagebreakBottom(params().pagebreakBottom());
880 params().pagebreakBottom(false);
881 tmp->params().spaceBottom(params().spaceBottom());
882 params().spaceBottom(VSpace(VSpace::NONE));
884 tmp->params().depth(params().depth());
885 tmp->params().noindent(params().noindent());
887 // copy everything behind the break-position
888 // to the new paragraph
889 size_type pos_end = pimpl_->size() - 1;
892 for (; i <= pos_end; ++i) {
893 cutIntoMinibuffer(bparams, i);
894 if (tmp->insertFromMinibuffer(j - pos))
897 for (i = pos_end; i >= pos; --i) {
902 // just an idea of me
904 tmp->params().lineTop(params().lineTop());
905 tmp->params().pagebreakTop(params().pagebreakTop());
906 tmp->params().spaceTop(params().spaceTop());
907 tmp->bibkey = bibkey;
909 // layout stays the same with latex-environments
911 setOnlyLayout(tmp->layout);
912 setLabelWidthString(tmp->params().labelWidthString());
913 params().depth(tmp->params().depth());
919 void Paragraph::makeSameLayout(Paragraph const * par)
921 layout = par->layout;
923 params() = par->params();
927 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass)
929 if (textclasslist.Style(tclass, getLayout()).free_spacing)
934 && (isNewline(0) || isLineSeparator(0))){
943 bool Paragraph::hasSameLayout(Paragraph const * par) const
946 par->layout == layout &&
947 params().sameLayout(par->params());
951 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
952 Paragraph::size_type pos)
954 // create a new paragraph
955 Paragraph * tmp = new Paragraph(this);
956 tmp->makeSameLayout(this);
958 // When can pos > Last()?
959 // I guess pos == Last() is possible.
961 // copy everything behind the break-position to the new
963 size_type pos_end = pimpl_->size() - 1;
967 for (size_type i = pos, j = pos; i <= pos_end; ++i) {
968 cutIntoMinibuffer(bparams, i);
969 if (tmp->insertFromMinibuffer(j - pos))
973 for (size_type k = pos_end; k >= pos; --k) {
980 // Be carefull, this does not make any check at all.
981 // This method has wrong name, it combined this par with the next par.
982 // In that sense it is the reverse of break paragraph. (Lgb)
983 void Paragraph::pasteParagraph(BufferParams const & bparams)
985 // copy the next paragraph to this one
986 Paragraph * the_next = next();
988 // first the DTP-stuff
989 params().lineBottom(the_next->params().lineBottom());
990 params().spaceBottom(the_next->params().spaceBottom());
991 params().pagebreakBottom(the_next->params().pagebreakBottom());
993 size_type pos_end = the_next->pimpl_->size() - 1;
994 size_type pos_insert = size();
996 // ok, now copy the paragraph
997 for (size_type i = 0, j = 0; i <= pos_end; ++i) {
998 the_next->cutIntoMinibuffer(bparams, i);
999 if (insertFromMinibuffer(pos_insert + j))
1003 // delete the next paragraph
1004 Paragraph * ppar = the_next->previous_;
1005 Paragraph * npar = the_next->next_;
1011 int Paragraph::getEndLabel(BufferParams const & bparams) const
1013 Paragraph const * par = this;
1014 depth_type par_depth = getDepth();
1016 LyXTextClass::LayoutList::size_type layout = par->getLayout();
1017 int const endlabeltype =
1018 textclasslist.Style(bparams.textclass,
1019 layout).endlabeltype;
1020 if (endlabeltype != END_LABEL_NO_LABEL) {
1022 return endlabeltype;
1024 depth_type const next_depth = next_->getDepth();
1025 if (par_depth > next_depth ||
1026 (par_depth == next_depth
1027 && layout != next_->getLayout()))
1028 return endlabeltype;
1033 par = par->outerHook();
1035 par_depth = par->getDepth();
1037 return END_LABEL_NO_LABEL;
1041 Paragraph::depth_type Paragraph::getDepth() const
1043 return params().depth();
1047 char Paragraph::getAlign() const
1049 return params().align();
1053 string const & Paragraph::getLabelstring() const
1055 return params().labelString();
1059 int Paragraph::getFirstCounter(int i) const
1061 return pimpl_->counter_[i];
1065 // the next two functions are for the manual labels
1066 string const Paragraph::getLabelWidthString() const
1068 if (!params().labelWidthString().empty())
1069 return params().labelWidthString();
1071 return _("Senseless with this layout!");
1075 void Paragraph::setLabelWidthString(string const & s)
1077 params().labelWidthString(s);
1081 void Paragraph::setOnlyLayout(LyXTextClass::size_type new_layout)
1083 layout = new_layout;
1087 void Paragraph::setLayout(LyXTextClass::size_type new_layout)
1089 layout = new_layout;
1090 params().labelWidthString(string());
1091 params().align(LYX_ALIGN_LAYOUT);
1092 params().spaceTop(VSpace(VSpace::NONE));
1093 params().spaceBottom(VSpace(VSpace::NONE));
1094 params().spacing(Spacing(Spacing::Default));
1098 // if the layout of a paragraph contains a manual label, the beginning of the
1099 // main body is the beginning of the second word. This is what the par-
1100 // function returns. If the layout does not contain a label, the main
1101 // body always starts with position 0. This differentiation is necessary,
1102 // because there cannot be a newline or a blank <= the beginning of the
1103 // main body in TeX.
1105 int Paragraph::beginningOfMainBody() const
1107 // Unroll the first two cycles of the loop
1108 // and remember the previous character to
1109 // remove unnecessary GetChar() calls
1112 && getChar(i) != Paragraph::META_NEWLINE) {
1114 char previous_char = 0;
1117 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1118 // Yes, this ^ is supposed to be "= " not "=="
1121 && previous_char != ' '
1122 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1124 previous_char = temp;
1133 Paragraph * Paragraph::depthHook(depth_type depth)
1135 Paragraph * newpar = this;
1138 newpar = newpar->previous();
1139 } while (newpar && newpar->getDepth() > depth);
1142 if (previous() || getDepth())
1143 lyxerr << "ERROR (Paragraph::DepthHook): "
1152 Paragraph const * Paragraph::depthHook(depth_type depth) const
1154 Paragraph const * newpar = this;
1157 newpar = newpar->previous();
1158 } while (newpar && newpar->getDepth() > depth);
1161 if (previous() || getDepth())
1162 lyxerr << "ERROR (Paragraph::DepthHook): "
1170 Paragraph * Paragraph::outerHook()
1174 return depthHook(depth_type(getDepth() - 1));
1177 Paragraph const * Paragraph::outerHook() const
1181 return depthHook(depth_type(getDepth() - 1));
1184 int Paragraph::autoDeleteInsets()
1187 InsetList::size_type index = 0;
1188 while (index < insetlist.size()) {
1189 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1190 erase(insetlist[index].pos);
1191 // Erase() calls to insetlist.erase(&insetlist[index])
1192 // so index shouldn't be increased.
1201 Paragraph::inset_iterator
1202 Paragraph::InsetIterator(Paragraph::size_type pos)
1204 InsetTable search_inset(pos, 0);
1205 InsetList::iterator it = lower_bound(insetlist.begin(),
1207 search_inset, Pimpl::matchIT());
1208 return inset_iterator(it);
1212 // returns -1 if inset not found
1213 int Paragraph::getPositionOfInset(Inset * inset) const
1216 for (InsetList::const_iterator cit = insetlist.begin();
1217 cit != insetlist.end(); ++cit) {
1218 if (cit->inset == inset) {
1222 if (inset == bibkey)
1229 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1230 BufferParams const & bparams,
1231 ostream & os, TexRow & texrow,
1234 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1235 LyXLayout const & style =
1236 textclasslist.Style(bparams.textclass,
1239 bool further_blank_line = false;
1241 if (params().startOfAppendix()) {
1242 os << "\\appendix\n";
1246 if (!params().spacing().isDefault()
1247 && (!previous() || !previous()->hasSameLayout(this))) {
1248 os << params().spacing().writeEnvirBegin() << "\n";
1252 if (tex_code_break_column && style.isCommand()){
1257 if (params().pagebreakTop()) {
1259 further_blank_line = true;
1261 if (params().spaceTop().kind() != VSpace::NONE) {
1262 os << params().spaceTop().asLatexCommand(bparams);
1263 further_blank_line = true;
1266 if (params().lineTop()) {
1267 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1268 << "\\vspace{-1\\parskip}";
1269 further_blank_line = true;
1272 if (further_blank_line){
1277 Language const * language = getParLanguage(bparams);
1278 Language const * doc_language = bparams.language;
1279 Language const * previous_language = previous_
1280 ? previous_->getParLanguage(bparams) : doc_language;
1281 if (language->babel() != doc_language->babel() &&
1282 language->babel() != previous_language->babel()) {
1283 os << subst(lyxrc.language_command_begin, "$$lang",
1289 if (bparams.inputenc == "auto" &&
1290 language->encoding() != previous_language->encoding()) {
1291 os << "\\inputencoding{"
1292 << language->encoding()->LatexName()
1297 switch (style.latextype) {
1300 << style.latexname()
1301 << style.latexparam();
1303 case LATEX_ITEM_ENVIRONMENT:
1305 bibkey->latex(buf, os, false, false);
1309 case LATEX_LIST_ENVIRONMENT:
1316 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1318 // Make sure that \\par is done with the font of the last
1319 // character if this has another size as the default.
1320 // This is necessary because LaTeX (and LyX on the screen)
1321 // calculates the space between the baselines according
1322 // to this font. (Matthias)
1324 // Is this really needed ? (Dekel)
1325 // We do not need to use to change the font for the last paragraph
1326 // or for a command.
1327 LyXFont const font =
1329 ? getLayoutFont(bparams)
1330 : getFont(bparams, size() - 1));
1332 bool is_command = textclasslist.Style(bparams.textclass,
1333 getLayout()).isCommand();
1334 if (style.resfont.size() != font.size() && next_ && !is_command) {
1337 os << "\\" << font.latexSize() << " \\par}";
1338 } else if (need_par) {
1340 } else if (is_command)
1343 if (language->babel() != doc_language->babel() &&
1345 next_->getParLanguage(bparams)->babel() != language->babel()))
1348 << subst(lyxrc.language_command_end, "$$lang",
1349 doc_language->babel());
1352 switch (style.latextype) {
1353 case LATEX_ITEM_ENVIRONMENT:
1354 case LATEX_LIST_ENVIRONMENT:
1355 if (next_ && (params().depth() < next_->params().depth())) {
1360 case LATEX_ENVIRONMENT:
1361 // if its the last paragraph of the current environment
1362 // skip it otherwise fall through
1364 && (next_->layout != layout
1365 || next_->params().depth() != params().depth()))
1368 // we don't need it for the last paragraph!!!
1369 // or for tables in floats
1370 // -- effectively creates a \par where there isn't one which
1371 // breaks a \subfigure or \subtable.
1378 further_blank_line = false;
1379 if (params().lineBottom()) {
1380 os << "\\lyxline{\\" << font.latexSize() << '}';
1381 further_blank_line = true;
1384 if (params().spaceBottom().kind() != VSpace::NONE) {
1385 os << params().spaceBottom().asLatexCommand(bparams);
1386 further_blank_line = true;
1389 if (params().pagebreakBottom()) {
1391 further_blank_line = true;
1394 if (further_blank_line){
1399 if (!params().spacing().isDefault()
1400 && (!next_ || !next_->hasSameLayout(this))) {
1401 os << params().spacing().writeEnvirEnd() << "\n";
1405 // we don't need it for the last paragraph!!!
1411 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1416 // This one spits out the text of the paragraph
1417 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1418 BufferParams const & bparams,
1419 ostream & os, TexRow & texrow,
1422 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1424 bool return_value = false;
1426 LyXLayout const & style =
1427 textclasslist.Style(bparams.textclass,
1431 // Maybe we have to create a optional argument.
1432 size_type main_body;
1433 if (style.labeltype != LABEL_MANUAL)
1436 main_body = beginningOfMainBody();
1440 if (main_body > 0) {
1443 basefont = getLabelFont(bparams);
1445 basefont = getLayoutFont(bparams);
1449 && !pimpl_->size()) {
1450 if (style.isCommand()) {
1456 moving_arg |= style.needprotect;
1458 // Which font is currently active?
1459 LyXFont running_font(basefont);
1460 // Do we have an open font change?
1461 bool open_font = false;
1463 texrow.start(this, 0);
1465 for (size_type i = 0; i < size(); ++i) {
1467 // First char in paragraph or after label?
1468 if (i == main_body) {
1469 if (main_body > 0) {
1471 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1474 basefont = getLayoutFont(bparams);
1475 running_font = basefont;
1479 if (style.isCommand()) {
1484 if (params().noindent()) {
1485 os << "\\noindent ";
1488 switch (params().align()) {
1489 case LYX_ALIGN_NONE:
1490 case LYX_ALIGN_BLOCK:
1491 case LYX_ALIGN_LAYOUT:
1492 case LYX_ALIGN_SPECIAL:
1494 case LYX_ALIGN_LEFT:
1495 if (getParLanguage(bparams)->babel() != "hebrew") {
1496 os << "\\begin{flushleft}";
1499 os << "\\begin{flushright}";
1503 case LYX_ALIGN_RIGHT:
1504 if (getParLanguage(bparams)->babel() != "hebrew") {
1505 os << "\\begin{flushright}";
1508 os << "\\begin{flushleft}";
1512 case LYX_ALIGN_CENTER:
1513 os << "\\begin{center}";
1519 value_type c = getChar(i);
1521 // Fully instantiated font
1522 LyXFont font = getFont(bparams, i);
1524 LyXFont const last_font = running_font;
1526 // Spaces at end of font change are simulated to be
1527 // outside font change, i.e. we write "\textXX{text} "
1528 // rather than "\textXX{text }". (Asger)
1529 if (open_font && c == ' ' && i <= size() - 2) {
1530 LyXFont const next_font = getFont(bparams, i + 1);
1531 if (next_font != running_font
1532 && next_font != font) {
1537 // We end font definition before blanks
1538 if (font != running_font && open_font) {
1539 column += running_font.latexWriteEndChanges(os,
1541 (i == main_body-1) ? basefont : font);
1542 running_font = basefont;
1546 // Blanks are printed before start of fontswitch
1548 // Do not print the separation of the optional argument
1549 if (i != main_body - 1) {
1550 pimpl_->simpleTeXBlanks(os, texrow, i,
1551 column, font, style);
1555 // Do we need to change font?
1556 if (font != running_font && i != main_body - 1) {
1557 column += font.latexWriteStartChanges(os, basefont,
1559 running_font = font;
1563 if (c == Paragraph::META_NEWLINE) {
1564 // newlines are handled differently here than
1565 // the default in SimpleTeXSpecialChars().
1566 if (!style.newline_allowed) {
1570 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1573 basefont = getLayoutFont(bparams);
1574 running_font = basefont;
1575 if (font.family() ==
1576 LyXFont::TYPEWRITER_FAMILY) {
1584 texrow.start(this, i + 1);
1587 pimpl_->simpleTeXSpecialChars(buf, bparams,
1588 os, texrow, moving_arg,
1590 basefont, open_font,
1591 style, i, column, c);
1595 // If we have an open font definition, we have to close it
1597 #ifdef FIXED_LANGUAGE_END_DETECTION
1600 .latexWriteEndChanges(os, basefont,
1601 next_->getFont(bparams,
1604 running_font.latexWriteEndChanges(os, basefont,
1608 #ifdef WITH_WARNINGS
1609 //#warning For now we ALWAYS have to close the foreign font settings if they are
1610 //#warning there as we start another \selectlanguage with the next paragraph if
1611 //#warning we are in need of this. This should be fixed sometime (Jug)
1613 running_font.latexWriteEndChanges(os, basefont, basefont);
1617 // Needed if there is an optional argument but no contents.
1618 if (main_body > 0 && main_body == size()) {
1620 return_value = false;
1623 switch (params().align()) {
1624 case LYX_ALIGN_NONE:
1625 case LYX_ALIGN_BLOCK:
1626 case LYX_ALIGN_LAYOUT:
1627 case LYX_ALIGN_SPECIAL:
1629 case LYX_ALIGN_LEFT:
1630 if (getParLanguage(bparams)->babel() != "hebrew") {
1631 os << "\\end{flushleft}";
1634 os << "\\end{flushright}";
1638 case LYX_ALIGN_RIGHT:
1639 if (getParLanguage(bparams)->babel() != "hebrew") {
1640 os << "\\end{flushright}";
1643 os << "\\end{flushleft}";
1647 case LYX_ALIGN_CENTER:
1648 os << "\\end{center}";
1653 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1654 return return_value;
1658 bool Paragraph::linuxDocConvertChar(char c, string & sgml_string)
1660 bool retval = false;
1662 case Paragraph::META_HFILL:
1663 sgml_string.erase();
1665 case Paragraph::META_NEWLINE:
1669 sgml_string = "&";
1672 sgml_string = "<";
1675 sgml_string = ">";
1678 sgml_string = "$";
1681 sgml_string = "#";
1684 sgml_string = "%";
1687 sgml_string = "[";
1690 sgml_string = "]";
1693 sgml_string = "{";
1696 sgml_string = "}";
1699 sgml_string = "˜";
1702 sgml_string = """;
1705 sgml_string = "\";
1711 case '\0': // Ignore :-)
1712 sgml_string.erase();
1722 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1723 BufferParams const & bparams,
1724 ostream & os, TexRow & texrow)
1726 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1728 LyXLayout const & style =
1729 textclasslist.Style(bparams.textclass,
1732 if (style.isEnvironment()){
1733 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1734 os << "\\begin{" << style.latexname() << "}{"
1735 << params().labelWidthString() << "}\n";
1736 } else if (style.labeltype == LABEL_BIBLIO) {
1738 os << "\\begin{" << style.latexname() << "}{"
1739 << bibitemWidest(buf)
1741 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1742 os << "\\begin{" << style.latexname() << '}'
1743 << style.latexparam() << '\n';
1745 os << "\\begin{" << style.latexname() << '}'
1746 << style.latexparam() << '\n';
1749 Paragraph * par = this;
1751 par = par->TeXOnePar(buf, bparams,
1754 if (par && par->params().depth() > params().depth()) {
1755 if (textclasslist.Style(bparams.textclass,
1756 par->layout).isParagraph()
1758 // How to handle this? (Lgb)
1759 //&& !suffixIs(os, "\n\n")
1761 // There should be at least one '\n' already
1762 // but we need there to be two for Standard
1763 // paragraphs that are depth-increment'ed to be
1764 // output correctly. However, tables can
1765 // also be paragraphs so don't adjust them.
1768 // Will it ever harm to have one '\n' too
1769 // many? i.e. that we sometimes will have
1770 // three in a row. (Lgb)
1774 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1777 && par->layout == layout
1778 && par->params().depth() == params().depth());
1780 if (style.isEnvironment()) {
1781 os << "\\end{" << style.latexname() << "}\n";
1784 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1785 return par; // ale970302
1789 bool Paragraph::isHfill(size_type pos) const
1791 return IsHfillChar(getChar(pos));
1795 bool Paragraph::isInset(size_type pos) const
1797 return IsInsetChar(getChar(pos));
1801 bool Paragraph::isNewline(size_type pos) const
1803 return pos >= 0 && IsNewlineChar(getChar(pos));
1807 bool Paragraph::isSeparator(size_type pos) const
1809 return IsSeparatorChar(getChar(pos));
1813 bool Paragraph::isLineSeparator(size_type pos) const
1815 return IsLineSeparatorChar(getChar(pos));
1819 bool Paragraph::isKomma(size_type pos) const
1821 return IsKommaChar(getChar(pos));
1825 /// Used by the spellchecker
1826 bool Paragraph::isLetter(Paragraph::size_type pos) const
1828 value_type const c = getChar(pos);
1829 if (IsLetterChar(c))
1831 // '\0' is not a letter, allthough every string contains "" (below)
1834 // We want to pass the ' and escape chars to ispell
1835 string const extra = lyxrc.isp_esc_chars + '\'';
1836 char ch[2] = { c, 0 };
1837 return contains(extra, ch);
1841 bool Paragraph::isWord(size_type pos ) const
1843 return IsWordChar(getChar(pos)) ;
1848 Paragraph::getParLanguage(BufferParams const & bparams) const
1851 #ifndef INHERIT_LANGUAGE
1852 return getFirstFontSettings().language();
1854 Language const * lang = getFirstFontSettings().language();
1855 #warning We should make this somewhat better, any ideas? (Jug)
1856 if (lang == inherit_language || lang == ignore_language)
1857 lang = bparams.language;
1860 } else if (previous_)
1861 return previous_->getParLanguage(bparams);
1863 return bparams.language;
1867 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1869 return lyxrc.rtl_support
1870 && getParLanguage(bparams)->RightToLeft();
1874 void Paragraph::changeLanguage(BufferParams const & bparams,
1875 Language const * from, Language const * to)
1877 for (size_type i = 0; i < size(); ++i) {
1878 LyXFont font = getFontSettings(bparams, i);
1879 if (font.language() == from) {
1880 font.setLanguage(to);
1887 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1889 Language const * doc_language = bparams.language;
1890 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1891 cit != pimpl_->fontlist.end(); ++cit)
1892 if (cit->font().language() != ignore_language &&
1893 cit->font().language() != latex_language &&
1894 #ifdef INHERIT_LANGUAGE
1895 cit->font().language() != inherit_language &&
1897 cit->font().language() != doc_language)
1903 // Convert the paragraph to a string.
1904 // Used for building the table of contents
1905 string const Paragraph::asString(Buffer const * buffer, bool label)
1907 BufferParams const & bparams = buffer->params;
1909 if (label && !params().labelString().empty())
1910 s += params().labelString() + ' ';
1911 string::size_type const len = s.size();
1913 for (Paragraph::size_type i = 0; i < size(); ++i) {
1914 value_type c = getChar(i);
1917 else if (c == META_INSET &&
1918 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1920 getInset(i)->ascii(buffer, ost);
1921 s += subst(ost.str().c_str(),'\n',' ');
1925 if (isRightToLeftPar(bparams))
1926 reverse(s.begin() + len,s.end());
1932 string const Paragraph::asString(Buffer const * buffer,
1933 Paragraph::size_type beg,
1934 Paragraph::size_type end)
1938 if (beg == 0 && !params().labelString().empty())
1939 ost << params().labelString() << ' ';
1941 for (Paragraph::size_type i = beg; i < end; ++i) {
1942 value_type const c = getUChar(buffer->params, i);
1945 else if (c == META_INSET) {
1946 getInset(i)->ascii(buffer, ost);
1950 return ost.str().c_str();
1954 void Paragraph::setInsetOwner(Inset * i)
1956 pimpl_->inset_owner = i;
1957 for (InsetList::const_iterator cit = insetlist.begin();
1958 cit != insetlist.end(); ++cit) {
1960 cit->inset->setOwner(i);
1965 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1968 for (InsetList::const_iterator cit = insetlist.begin();
1969 cit != insetlist.end(); ++cit) {
1971 if (cit->inset->isTextInset()) {
1972 static_cast<UpdatableInset *>
1973 (cit->inset)->deleteLyXText(bv, true);
1980 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1983 for (InsetList::const_iterator cit = insetlist.begin();
1984 cit != insetlist.end(); ++cit)
1987 if (cit->inset->isTextInset()) {
1988 static_cast<UpdatableInset *>
1989 (cit->inset)->resizeLyXText(bv, true);
1996 void Paragraph::setContentsFromPar(Paragraph * par)
1998 pimpl_->setContentsFromPar(par);
2002 Paragraph::size_type Paragraph::size() const
2004 return pimpl_->size();
2008 Paragraph::value_type
2009 Paragraph::getChar(Paragraph::size_type pos) const
2011 return pimpl_->getChar(pos);
2015 int Paragraph::id() const
2021 void Paragraph::id(int id_arg)
2023 pimpl_->id_ = id_arg;
2027 LyXTextClass::LayoutList::size_type Paragraph::getLayout() const
2033 bool Paragraph::isFirstInSequence() const
2035 Paragraph const * dhook = depthHook(getDepth());
2036 return (dhook == this
2037 || dhook->getLayout() != getLayout()
2038 || dhook->getDepth() != getDepth());
2042 Inset * Paragraph::inInset() const
2044 return pimpl_->inset_owner;
2048 void Paragraph::clearContents()
2054 void Paragraph::setCounter(int i, int v)
2056 pimpl_->counter_[i] = v;
2060 int Paragraph::getCounter(int i) const
2062 return pimpl_->counter_[i];
2066 void Paragraph::incCounter(int i)
2068 pimpl_->counter_[i]++;
2072 void Paragraph::setChar(size_type pos, value_type c)
2074 pimpl_->setChar(pos, c);
2078 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2083 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2085 return inset_iterator(insetlist.begin());
2089 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2091 return inset_iterator(insetlist.end());
2095 ParagraphParameters & Paragraph::params()
2097 return pimpl_->params;
2101 ParagraphParameters const & Paragraph::params() const
2103 return pimpl_->params;
2107 Paragraph * Paragraph::getParFromID(int id) const
2109 return pimpl_->getParFromID(id);