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 "lyx_gui_misc.h"
40 #include "support/lyxmanip.h"
41 #include "BufferView.h"
43 #include "ParameterStruct.h"
50 using std::lower_bound;
51 using std::upper_bound;
54 int tex_code_break_column = 72; // needs non-zero initialization. set later.
55 // this is a bad idea, but how can Paragraph find its buffer to get
56 // parameters? (JMarc)
58 extern string bibitemWidest(Buffer const *);
60 // this is a minibuffer
65 LyXFont minibuffer_font;
66 Inset * minibuffer_inset;
71 extern BufferView * current_view;
74 Paragraph::Paragraph()
75 : pimpl_(new Paragraph::Pimpl(this))
77 for (int i = 0; i < 10; ++i) setCounter(i , 0);
82 bibkey = 0; // ale970302
87 // This konstruktor inserts the new paragraph in a list.
88 Paragraph::Paragraph(Paragraph * par)
89 : pimpl_(new Paragraph::Pimpl(this))
91 for (int i = 0; i < 10; ++i)
96 // double linked list begin
99 next_->previous_ = this;
101 previous_->next_ = this;
104 bibkey = 0; // ale970302
110 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
111 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
113 for (int i = 0; i < 10; ++i)
120 // this is because of the dummy layout of the paragraphs that
126 bibkey = static_cast<InsetBibKey *>
127 (lp.bibkey->clone(*current_view->buffer()));
132 // copy everything behind the break-position to the new paragraph
134 insetlist = lp.insetlist;
135 for (InsetList::iterator it = insetlist.begin();
136 it != insetlist.end(); ++it)
138 it->inset = it->inset->clone(*current_view->buffer(), same_ids);
143 // the destructor removes the new paragraph from the list
144 Paragraph::~Paragraph()
147 previous_->next_ = next_;
149 next_->previous_ = previous_;
151 for (InsetList::iterator it = insetlist.begin();
152 it != insetlist.end(); ++it) {
161 //lyxerr << "Paragraph::paragraph_id = "
162 // << Paragraph::paragraph_id << endl;
166 void Paragraph::writeFile(Buffer const * buf, ostream & os,
167 BufferParams const & bparams,
168 depth_type dth) const
170 // The beginning or end of a deeper (i.e. nested) area?
171 if (dth != params().depth()) {
172 if (params().depth() > dth) {
173 while (params().depth() > dth) {
174 os << "\n\\begin_deeper ";
178 while (params().depth() < dth) {
179 os << "\n\\end_deeper ";
185 // First write the layout
187 << textclasslist.NameOfLayout(bparams.textclass, layout)
190 // Maybe some vertical spaces.
191 if (params().spaceTop().kind() != VSpace::NONE)
192 os << "\\added_space_top "
193 << params().spaceTop().asLyXCommand() << " ";
194 if (params().spaceBottom().kind() != VSpace::NONE)
195 os << "\\added_space_bottom "
196 << params().spaceBottom().asLyXCommand() << " ";
198 // Maybe the paragraph has special spacing
199 params().spacing().writeFile(os, true);
201 // The labelwidth string used in lists.
202 if (!params().labelWidthString().empty())
203 os << "\\labelwidthstring "
204 << params().labelWidthString() << '\n';
206 // Lines above or below?
207 if (params().lineTop())
209 if (params().lineBottom())
210 os << "\\line_bottom ";
212 // Pagebreaks above or below?
213 if (params().pagebreakTop())
214 os << "\\pagebreak_top ";
215 if (params().pagebreakBottom())
216 os << "\\pagebreak_bottom ";
218 // Start of appendix?
219 if (params().startOfAppendix())
220 os << "\\start_of_appendix ";
223 if (params().noindent())
227 if (params().align() != LYX_ALIGN_LAYOUT) {
229 switch (params().align()) {
230 case LYX_ALIGN_LEFT: h = 1; break;
231 case LYX_ALIGN_RIGHT: h = 2; break;
232 case LYX_ALIGN_CENTER: h = 3; break;
233 default: h = 0; break;
235 os << "\\align " << string_align[h] << " ";
240 bibkey->write(buf, os);
242 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
245 for (size_type i = 0; i < size(); ++i) {
251 // Write font changes
252 LyXFont font2 = getFontSettings(bparams, i);
253 if (font2 != font1) {
254 font2.lyxWriteChanges(font1, os);
259 value_type const c = getChar(i);
263 Inset const * inset = getInset(i);
265 if (inset->directWrite()) {
266 // international char, let it write
267 // code directly so it's shorter in
269 inset->write(buf, os);
271 os << "\n\\begin_inset ";
272 inset->write(buf, os);
273 os << "\n\\end_inset \n\n";
279 os << "\n\\newline \n";
283 os << "\n\\hfill \n";
287 os << "\n\\backslash \n";
291 if (i + 1 < size() && getChar(i + 1) == ' ') {
298 if ((column > 70 && c == ' ')
303 // this check is to amend a bug. LyX sometimes
304 // inserts '\0' this could cause problems.
308 lyxerr << "ERROR (Paragraph::writeFile):"
309 " NULL char in structure." << endl;
315 // now write the next paragraph
317 next_->writeFile(buf, os, bparams, dth);
321 void Paragraph::validate(LaTeXFeatures & features) const
323 BufferParams const & bparams = features.bufferParams();
326 if (params().lineTop() || params().lineBottom())
327 features.lyxline = true;
328 if (!params().spacing().isDefault())
329 features.setspace = true;
332 features.layout[getLayout()] = true;
335 Language const * doc_language = bparams.language;
337 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
338 cit != pimpl_->fontlist.end(); ++cit) {
339 if (cit->font().noun() == LyXFont::ON) {
340 lyxerr[Debug::LATEX] << "font.noun: "
341 << cit->font().noun()
343 features.noun = true;
344 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
345 << cit->font().stateText(0)
348 switch (cit->font().color()) {
350 case LColor::inherit:
354 features.color = true;
355 lyxerr[Debug::LATEX] << "Color enabled. Font: "
356 << cit->font().stateText(0)
360 Language const * language = cit->font().language();
361 if (language->babel() != doc_language->babel()) {
362 features.UsedLanguages.insert(language);
363 lyxerr[Debug::LATEX] << "Found language "
364 << language->babel() << endl;
369 for (InsetList::const_iterator cit = insetlist.begin();
370 cit != insetlist.end(); ++cit) {
372 cit->inset->validate(features);
377 // First few functions needed for cut and paste and paragraph breaking.
378 void Paragraph::copyIntoMinibuffer(Buffer const & buffer,
379 Paragraph::size_type pos) const
381 BufferParams bparams = buffer.params;
383 minibuffer_char = getChar(pos);
384 minibuffer_font = getFontSettings(bparams, pos);
385 minibuffer_inset = 0;
386 if (minibuffer_char == Paragraph::META_INSET) {
388 minibuffer_inset = getInset(pos)->clone(buffer);
390 minibuffer_inset = 0;
391 minibuffer_char = ' ';
392 // This reflects what GetInset() does (ARRae)
398 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
399 Paragraph::size_type pos)
401 minibuffer_char = getChar(pos);
402 minibuffer_font = getFontSettings(bparams, pos);
403 minibuffer_inset = 0;
404 if (minibuffer_char == Paragraph::META_INSET) {
406 minibuffer_inset = getInset(pos);
407 // This is a little hack since I want exactly
408 // the inset, not just a clone. Otherwise
409 // the inset would be deleted when calling Erase(pos)
411 InsetTable search_elem(pos, 0);
412 InsetList::iterator it =
413 lower_bound(insetlist.begin(),
415 search_elem, Pimpl::matchIT());
416 if (it != insetlist.end() && it->pos == pos)
419 minibuffer_inset = 0;
420 minibuffer_char = ' ';
421 // This reflects what GetInset() does (ARRae)
426 // Erase(pos); now the caller is responsible for that.
430 bool Paragraph::insertFromMinibuffer(Paragraph::size_type pos)
432 if ((minibuffer_char == Paragraph::META_INSET) &&
433 !insetAllowed(minibuffer_inset->lyxCode()))
435 if (minibuffer_char == Paragraph::META_INSET)
436 insertInset(pos, minibuffer_inset, minibuffer_font);
438 insertChar(pos, minibuffer_char, minibuffer_font);
446 void Paragraph::clear()
455 void Paragraph::erase(Paragraph::size_type pos)
461 void Paragraph::insertChar(Paragraph::size_type pos,
462 Paragraph::value_type c)
464 LyXFont const f(LyXFont::ALL_INHERIT);
465 insertChar(pos, c, f);
469 void Paragraph::insertChar(Paragraph::size_type pos,
470 Paragraph::value_type c,
471 LyXFont const & font)
473 pimpl_->insertChar(pos, c, font);
477 void Paragraph::insertInset(Paragraph::size_type pos,
480 LyXFont const f(LyXFont::ALL_INHERIT);
481 insertInset(pos, inset, f);
485 void Paragraph::insertInset(Paragraph::size_type pos,
486 Inset * inset, LyXFont const & font)
488 pimpl_->insertInset(pos, inset, font);
492 bool Paragraph::insetAllowed(Inset::Code code)
494 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
496 if (pimpl_->inset_owner)
497 return pimpl_->inset_owner->insetAllowed(code);
502 Inset * Paragraph::getInset(Paragraph::size_type pos)
504 lyx::Assert(pos < size());
507 InsetTable search_inset(pos, 0);
508 InsetList::iterator it = lower_bound(insetlist.begin(),
510 search_inset, Pimpl::matchIT());
511 if (it != insetlist.end() && it->pos == pos)
514 lyxerr << "ERROR (Paragraph::GetInset): "
515 "Inset does not exist: " << pos << endl;
518 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
519 // Did this commenting out introduce a bug? So far I have not
520 // see any, please enlighten me. (Lgb)
521 // My guess is that since the inset does not exist, we might
522 // as well replace it with a space to prevent craches. (Asger)
527 Inset const * Paragraph::getInset(Paragraph::size_type pos) const
529 lyx::Assert(pos < size());
532 InsetTable search_inset(pos, 0);
533 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
535 search_inset, Pimpl::matchIT());
536 if (cit != insetlist.end() && cit->pos == pos)
539 lyxerr << "ERROR (Paragraph::GetInset): "
540 "Inset does not exist: " << pos << endl;
542 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
543 // Did this commenting out introduce a bug? So far I have not
544 // see any, please enlighten me. (Lgb)
545 // My guess is that since the inset does not exist, we might
546 // as well replace it with a space to prevent craches. (Asger)
551 // Gets uninstantiated font setting at position.
552 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
553 Paragraph::size_type pos) const
555 lyx::Assert(pos <= size());
557 Pimpl::FontTable search_font(pos, LyXFont());
558 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
559 pimpl_->fontlist.end(),
560 search_font, Pimpl::matchFT());
561 if (cit != pimpl_->fontlist.end())
564 if (pos == size() && size())
565 return getFontSettings(bparams, pos - 1);
567 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
571 // Gets uninstantiated font setting at position 0
572 LyXFont const Paragraph::getFirstFontSettings() const
575 if (!pimpl_->fontlist.empty())
576 return pimpl_->fontlist[0].font();
579 return LyXFont(LyXFont::ALL_INHERIT);
583 // Gets the fully instantiated font at a given position in a paragraph
584 // This is basically the same function as LyXText::GetFont() in text2.C.
585 // The difference is that this one is used for generating the LaTeX file,
586 // and thus cosmetic "improvements" are disallowed: This has to deliver
587 // the true picture of the buffer. (Asger)
588 // If position is -1, we get the layout font of the paragraph.
589 // If position is -2, we get the font of the manual label of the paragraph.
590 LyXFont const Paragraph::getFont(BufferParams const & bparams,
591 Paragraph::size_type pos) const
594 LyXLayout const & layout =
595 textclasslist.Style(bparams.textclass,
597 Paragraph::size_type main_body = 0;
598 if (layout.labeltype == LABEL_MANUAL)
599 main_body = beginningOfMainBody();
604 layoutfont = layout.labelfont;
606 layoutfont = layout.font;
607 tmpfont = getFontSettings(bparams, pos);
608 tmpfont.realize(layoutfont);
610 // process layoutfont for pos == -1 and labelfont for pos < -1
612 tmpfont = layout.font;
614 tmpfont = layout.labelfont;
615 tmpfont.setLanguage(getParLanguage(bparams));
618 // check for environment font information
619 char par_depth = getDepth();
620 Paragraph const * par = this;
621 while (par && par->getDepth() && !tmpfont.resolved()) {
622 par = par->outerHook();
624 tmpfont.realize(textclasslist.
625 Style(bparams.textclass,
626 par->getLayout()).font);
627 par_depth = par->getDepth();
631 tmpfont.realize(textclasslist
632 .TextClass(bparams.textclass)
638 /// Returns the height of the highest font in range
640 Paragraph::highestFontInRange(Paragraph::size_type startpos,
641 Paragraph::size_type endpos) const
643 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
644 if (pimpl_->fontlist.empty())
647 Pimpl::FontTable end_search(endpos, LyXFont());
648 Pimpl::FontList::const_iterator end_it = lower_bound(pimpl_->fontlist.begin(),
649 pimpl_->fontlist.end(),
650 end_search, Pimpl::matchFT());
651 if (end_it != pimpl_->fontlist.end())
654 Pimpl::FontTable start_search(startpos, LyXFont());
655 for (Pimpl::FontList::const_iterator cit =
656 lower_bound(pimpl_->fontlist.begin(),
657 pimpl_->fontlist.end(),
658 start_search, Pimpl::matchFT());
659 cit != end_it; ++cit) {
660 LyXFont::FONT_SIZE size = cit->font().size();
661 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
668 Paragraph::value_type
669 Paragraph::getUChar(BufferParams const & bparams,
670 Paragraph::size_type pos) const
672 value_type c = getChar(pos);
673 if (!lyxrc.rtl_support)
703 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
710 void Paragraph::setFont(Paragraph::size_type pos,
711 LyXFont const & font)
713 lyx::Assert(pos <= size());
715 // First, reduce font against layout/label font
716 // Update: The SetCharFont() routine in text2.C already
717 // reduces font, so we don't need to do that here. (Asger)
718 // No need to simplify this because it will disappear
719 // in a new kernel. (Asger)
720 // Next search font table
722 Pimpl::FontTable search_font(pos, LyXFont());
723 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
724 pimpl_->fontlist.end(),
725 search_font, Pimpl::matchFT());
726 unsigned int i = it - pimpl_->fontlist.begin();
727 bool notfound = it == pimpl_->fontlist.end();
729 if (!notfound && pimpl_->fontlist[i].font() == font)
732 bool begin = pos == 0 || notfound ||
733 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
734 // Is position pos is a beginning of a font block?
735 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
736 // Is position pos is the end of a font block?
737 if (begin && end) { // A single char block
738 if (i + 1 < pimpl_->fontlist.size() &&
739 pimpl_->fontlist[i + 1].font() == font) {
740 // Merge the singleton block with the next block
741 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
742 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
743 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
744 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
745 // Merge the singleton block with the previous block
746 pimpl_->fontlist[i - 1].pos(pos);
747 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
749 pimpl_->fontlist[i].font(font);
751 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
752 pimpl_->fontlist[i - 1].pos(pos);
754 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
755 Pimpl::FontTable(pos, font));
757 pimpl_->fontlist[i].pos(pos - 1);
758 if (!(i + 1 < pimpl_->fontlist.size() &&
759 pimpl_->fontlist[i + 1].font() == font))
760 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
761 Pimpl::FontTable(pos, font));
762 } else { // The general case. The block is splitted into 3 blocks
763 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
764 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
765 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
766 Pimpl::FontTable(pos, font));
772 void Paragraph::next(Paragraph * p)
778 // This function is able to hide closed footnotes.
779 Paragraph * Paragraph::next()
785 Paragraph const * Paragraph::next() const
791 void Paragraph::previous(Paragraph * p)
797 // This function is able to hide closed footnotes.
798 Paragraph * Paragraph::previous()
804 // This function is able to hide closed footnotes.
805 Paragraph const * Paragraph::previous() const
811 void Paragraph::breakParagraph(BufferParams const & bparams,
812 Paragraph::size_type pos,
815 // create a new paragraph
816 Paragraph * tmp = new Paragraph(this);
817 // remember to set the inset_owner
818 tmp->setInsetOwner(inInset());
820 // this is an idea for a more userfriendly layout handling, I will
821 // see what the users say
823 // layout stays the same with latex-environments
825 tmp->setOnlyLayout(layout);
826 tmp->setLabelWidthString(params().labelWidthString());
829 if (size() > pos || !size() || flag == 2) {
830 tmp->setOnlyLayout(layout);
831 tmp->params().align(params().align());
832 tmp->setLabelWidthString(params().labelWidthString());
834 tmp->params().lineBottom(params().lineBottom());
835 params().lineBottom(false);
836 tmp->params().pagebreakBottom(params().pagebreakBottom());
837 params().pagebreakBottom(false);
838 tmp->params().spaceBottom(params().spaceBottom());
839 params().spaceBottom(VSpace(VSpace::NONE));
841 tmp->params().depth(params().depth());
842 tmp->params().noindent(params().noindent());
844 // copy everything behind the break-position
845 // to the new paragraph
846 size_type pos_end = pimpl_->size() - 1;
849 for (; i <= pos_end; ++i) {
850 cutIntoMinibuffer(bparams, i);
851 if (tmp->insertFromMinibuffer(j - pos))
854 for (i = pos_end; i >= pos; --i) {
859 // just an idea of me
861 tmp->params().lineTop(params().lineTop());
862 tmp->params().pagebreakTop(params().pagebreakTop());
863 tmp->params().spaceTop(params().spaceTop());
864 tmp->bibkey = bibkey;
866 // layout stays the same with latex-environments
868 setOnlyLayout(tmp->layout);
869 setLabelWidthString(tmp->params().labelWidthString());
870 params().depth(tmp->params().depth());
876 void Paragraph::makeSameLayout(Paragraph const * par)
878 layout = par->layout;
880 params() = par->params();
884 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass)
886 if (textclasslist.Style(tclass, getLayout()).free_spacing)
891 && (isNewline(0) || isLineSeparator(0))){
900 bool Paragraph::hasSameLayout(Paragraph const * par) const
903 par->layout == layout &&
904 params().sameLayout(par->params());
908 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
909 Paragraph::size_type pos)
911 // create a new paragraph
912 Paragraph * tmp = new Paragraph(this);
913 tmp->makeSameLayout(this);
915 // When can pos > Last()?
916 // I guess pos == Last() is possible.
918 // copy everything behind the break-position to the new
920 size_type pos_end = pimpl_->size() - 1;
924 for (size_type i = pos, j = pos; i <= pos_end; ++i) {
925 cutIntoMinibuffer(bparams, i);
926 if (tmp->insertFromMinibuffer(j - pos))
930 for (size_type k = pos_end; k >= pos; --k) {
937 // Be carefull, this does not make any check at all.
938 // This method has wrong name, it combined this par with the next par.
939 // In that sense it is the reverse of break paragraph. (Lgb)
940 void Paragraph::pasteParagraph(BufferParams const & bparams)
942 // copy the next paragraph to this one
943 Paragraph * the_next = next();
945 // first the DTP-stuff
946 params().lineBottom(the_next->params().lineBottom());
947 params().spaceBottom(the_next->params().spaceBottom());
948 params().pagebreakBottom(the_next->params().pagebreakBottom());
950 size_type pos_end = the_next->pimpl_->size() - 1;
951 size_type pos_insert = size();
953 // ok, now copy the paragraph
954 for (size_type i = 0, j = 0; i <= pos_end; ++i) {
955 the_next->cutIntoMinibuffer(bparams, i);
956 if (insertFromMinibuffer(pos_insert + j))
960 // delete the next paragraph
961 Paragraph * ppar = the_next->previous_;
962 Paragraph * npar = the_next->next_;
968 int Paragraph::getEndLabel(BufferParams const & bparams) const
970 Paragraph const * par = this;
971 depth_type par_depth = getDepth();
973 LyXTextClass::LayoutList::size_type layout = par->getLayout();
974 int const endlabeltype =
975 textclasslist.Style(bparams.textclass,
976 layout).endlabeltype;
977 if (endlabeltype != END_LABEL_NO_LABEL) {
981 depth_type const next_depth = next_->getDepth();
982 if (par_depth > next_depth ||
983 (par_depth == next_depth
984 && layout != next_->getLayout()))
990 par = par->outerHook();
992 par_depth = par->getDepth();
994 return END_LABEL_NO_LABEL;
998 Paragraph::depth_type Paragraph::getDepth() const
1000 return params().depth();
1004 char Paragraph::getAlign() const
1006 return params().align();
1010 string const & Paragraph::getLabelstring() const
1012 return params().labelString();
1016 int Paragraph::getFirstCounter(int i) const
1018 return pimpl_->counter_[i];
1022 // the next two functions are for the manual labels
1023 string const Paragraph::getLabelWidthString() const
1025 if (!params().labelWidthString().empty())
1026 return params().labelWidthString();
1028 return _("Senseless with this layout!");
1032 void Paragraph::setLabelWidthString(string const & s)
1034 params().labelWidthString(s);
1038 void Paragraph::setOnlyLayout(LyXTextClass::size_type new_layout)
1040 layout = new_layout;
1044 void Paragraph::setLayout(LyXTextClass::size_type new_layout)
1046 layout = new_layout;
1047 params().labelWidthString(string());
1048 params().align(LYX_ALIGN_LAYOUT);
1049 params().spaceTop(VSpace(VSpace::NONE));
1050 params().spaceBottom(VSpace(VSpace::NONE));
1051 params().spacing(Spacing(Spacing::Default));
1055 // if the layout of a paragraph contains a manual label, the beginning of the
1056 // main body is the beginning of the second word. This is what the par-
1057 // function returns. If the layout does not contain a label, the main
1058 // body always starts with position 0. This differentiation is necessary,
1059 // because there cannot be a newline or a blank <= the beginning of the
1060 // main body in TeX.
1062 int Paragraph::beginningOfMainBody() const
1064 // Unroll the first two cycles of the loop
1065 // and remember the previous character to
1066 // remove unnecessary GetChar() calls
1069 && getChar(i) != Paragraph::META_NEWLINE) {
1071 char previous_char = 0;
1074 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1075 // Yes, this ^ is supposed to be "= " not "=="
1078 && previous_char != ' '
1079 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1081 previous_char = temp;
1090 Paragraph * Paragraph::depthHook(depth_type depth)
1092 Paragraph * newpar = this;
1095 newpar = newpar->previous();
1096 } while (newpar && newpar->getDepth() > depth);
1099 if (previous() || getDepth())
1100 lyxerr << "ERROR (Paragraph::DepthHook): "
1109 Paragraph const * Paragraph::depthHook(depth_type depth) const
1111 Paragraph const * newpar = this;
1114 newpar = newpar->previous();
1115 } while (newpar && newpar->getDepth() > depth);
1118 if (previous() || getDepth())
1119 lyxerr << "ERROR (Paragraph::DepthHook): "
1127 Paragraph * Paragraph::outerHook()
1131 return depthHook(depth_type(getDepth() - 1));
1134 Paragraph const * Paragraph::outerHook() const
1138 return depthHook(depth_type(getDepth() - 1));
1141 int Paragraph::autoDeleteInsets()
1144 InsetList::size_type index = 0;
1145 while (index < insetlist.size()) {
1146 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1147 erase(insetlist[index].pos);
1148 // Erase() calls to insetlist.erase(&insetlist[index])
1149 // so index shouldn't be increased.
1158 Paragraph::inset_iterator
1159 Paragraph::InsetIterator(Paragraph::size_type pos)
1161 InsetTable search_inset(pos, 0);
1162 InsetList::iterator it = lower_bound(insetlist.begin(),
1164 search_inset, Pimpl::matchIT());
1165 return inset_iterator(it);
1169 // returns -1 if inset not found
1170 int Paragraph::getPositionOfInset(Inset * inset) const
1173 for (InsetList::const_iterator cit = insetlist.begin();
1174 cit != insetlist.end(); ++cit) {
1175 if (cit->inset == inset) {
1179 if (inset == bibkey)
1186 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1187 BufferParams const & bparams,
1188 ostream & os, TexRow & texrow,
1191 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1192 LyXLayout const & style =
1193 textclasslist.Style(bparams.textclass,
1196 bool further_blank_line = false;
1198 if (params().startOfAppendix()) {
1199 os << "\\appendix\n";
1203 if (!params().spacing().isDefault()
1204 && (!previous() || !previous()->hasSameLayout(this))) {
1205 os << params().spacing().writeEnvirBegin() << "\n";
1209 if (tex_code_break_column && style.isCommand()){
1214 if (params().pagebreakTop()) {
1216 further_blank_line = true;
1218 if (params().spaceTop().kind() != VSpace::NONE) {
1219 os << params().spaceTop().asLatexCommand(bparams);
1220 further_blank_line = true;
1223 if (params().lineTop()) {
1224 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1225 << "\\vspace{-1\\parskip}";
1226 further_blank_line = true;
1229 if (further_blank_line){
1234 Language const * language = getParLanguage(bparams);
1235 Language const * doc_language = bparams.language;
1236 Language const * previous_language = previous_
1237 ? previous_->getParLanguage(bparams) : doc_language;
1238 if (language->babel() != doc_language->babel() &&
1239 language->babel() != previous_language->babel()) {
1240 os << subst(lyxrc.language_command_begin, "$$lang",
1246 if (bparams.inputenc == "auto" &&
1247 language->encoding() != previous_language->encoding()) {
1248 os << "\\inputencoding{"
1249 << language->encoding()->LatexName()
1254 switch (style.latextype) {
1257 << style.latexname()
1258 << style.latexparam();
1260 case LATEX_ITEM_ENVIRONMENT:
1262 bibkey->latex(buf, os, false, false);
1266 case LATEX_LIST_ENVIRONMENT:
1273 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1275 // Make sure that \\par is done with the font of the last
1276 // character if this has another size as the default.
1277 // This is necessary because LaTeX (and LyX on the screen)
1278 // calculates the space between the baselines according
1279 // to this font. (Matthias)
1281 // Is this really needed ? (Dekel)
1282 // We do not need to use to change the font for the last paragraph
1283 // or for a command.
1284 LyXFont font = getFont(bparams, size() - 1);
1286 bool is_command = textclasslist.Style(bparams.textclass,
1287 getLayout()).isCommand();
1288 if (style.resfont.size() != font.size() && next_ && !is_command) {
1291 os << "\\" << font.latexSize() << " \\par}";
1292 } else if (need_par) {
1294 } else if (is_command)
1297 if (language->babel() != doc_language->babel() &&
1299 || next_->getParLanguage(bparams)->babel() != language->babel())) {
1301 << subst(lyxrc.language_command_end, "$$lang",
1302 doc_language->babel());
1305 switch (style.latextype) {
1306 case LATEX_ITEM_ENVIRONMENT:
1307 case LATEX_LIST_ENVIRONMENT:
1308 if (next_ && (params().depth() < next_->params().depth())) {
1313 case LATEX_ENVIRONMENT:
1314 // if its the last paragraph of the current environment
1315 // skip it otherwise fall through
1317 && (next_->layout != layout
1318 || next_->params().depth() != params().depth()))
1321 // we don't need it for the last paragraph!!!
1322 // or for tables in floats
1323 // -- effectively creates a \par where there isn't one which
1324 // breaks a \subfigure or \subtable.
1331 further_blank_line = false;
1332 if (params().lineBottom()) {
1333 os << "\\lyxline{\\" << getFont(bparams,
1334 size() - 1).latexSize() << '}';
1335 further_blank_line = true;
1338 if (params().spaceBottom().kind() != VSpace::NONE) {
1339 os << params().spaceBottom().asLatexCommand(bparams);
1340 further_blank_line = true;
1343 if (params().pagebreakBottom()) {
1345 further_blank_line = true;
1348 if (further_blank_line){
1353 if (!params().spacing().isDefault()
1354 && (!next_ || !next_->hasSameLayout(this))) {
1355 os << params().spacing().writeEnvirEnd() << "\n";
1359 // we don't need it for the last paragraph!!!
1365 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1370 // This one spits out the text of the paragraph
1371 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1372 BufferParams const & bparams,
1373 ostream & os, TexRow & texrow,
1376 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1378 bool return_value = false;
1380 LyXLayout const & style =
1381 textclasslist.Style(bparams.textclass,
1385 // Maybe we have to create a optional argument.
1386 size_type main_body;
1387 if (style.labeltype != LABEL_MANUAL)
1390 main_body = beginningOfMainBody();
1394 if (main_body > 0) {
1397 basefont = getFont(bparams, -2); // Get label font
1399 basefont = getFont(bparams, -1); // Get layout font
1403 && !pimpl_->size()) {
1404 if (style.isCommand()) {
1410 moving_arg |= style.needprotect;
1412 // Which font is currently active?
1413 LyXFont running_font(basefont);
1414 // Do we have an open font change?
1415 bool open_font = false;
1417 texrow.start(this, 0);
1419 for (size_type i = 0; i < size(); ++i) {
1421 // First char in paragraph or after label?
1422 if (i == main_body) {
1423 if (main_body > 0) {
1425 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1428 basefont = getFont(bparams, -1); // Now use the layout font
1429 running_font = basefont;
1433 if (style.isCommand()) {
1438 if (params().noindent()) {
1439 os << "\\noindent ";
1442 switch (params().align()) {
1443 case LYX_ALIGN_NONE:
1444 case LYX_ALIGN_BLOCK:
1445 case LYX_ALIGN_LAYOUT:
1446 case LYX_ALIGN_SPECIAL:
1448 case LYX_ALIGN_LEFT:
1449 if (getParLanguage(bparams)->babel() != "hebrew") {
1450 os << "\\begin{flushleft}";
1453 os << "\\begin{flushright}";
1457 case LYX_ALIGN_RIGHT:
1458 if (getParLanguage(bparams)->babel() != "hebrew") {
1459 os << "\\begin{flushright}";
1462 os << "\\begin{flushleft}";
1466 case LYX_ALIGN_CENTER:
1467 os << "\\begin{center}";
1473 value_type c = getChar(i);
1475 // Fully instantiated font
1476 LyXFont font = getFont(bparams, i);
1478 LyXFont last_font = running_font;
1480 // Spaces at end of font change are simulated to be
1481 // outside font change, i.e. we write "\textXX{text} "
1482 // rather than "\textXX{text }". (Asger)
1483 if (open_font && c == ' ' && i <= size() - 2
1484 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
1485 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
1486 font = getFont(bparams, i + 1);
1488 // We end font definition before blanks
1489 if (!font.equalExceptLatex(running_font) && open_font) {
1490 column += running_font.latexWriteEndChanges(os,
1492 (i == main_body-1) ? basefont : font);
1493 running_font = basefont;
1497 // Blanks are printed before start of fontswitch
1499 // Do not print the separation of the optional argument
1500 if (i != main_body - 1) {
1501 pimpl_->simpleTeXBlanks(os, texrow, i,
1502 column, font, style);
1506 // Do we need to change font?
1507 if (!font.equalExceptLatex(running_font)
1508 && i != main_body-1) {
1509 column += font.latexWriteStartChanges(os, basefont,
1511 running_font = font;
1515 if (c == Paragraph::META_NEWLINE) {
1516 // newlines are handled differently here than
1517 // the default in SimpleTeXSpecialChars().
1518 if (!style.newline_allowed
1520 || font.latex() == LyXFont::ON
1526 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1529 basefont = getFont(bparams, -1);
1530 running_font = basefont;
1531 if (font.family() ==
1532 LyXFont::TYPEWRITER_FAMILY) {
1540 texrow.start(this, i + 1);
1543 pimpl_->simpleTeXSpecialChars(buf, bparams,
1544 os, texrow, moving_arg,
1546 basefont, open_font,
1547 style, i, column, c);
1551 // If we have an open font definition, we have to close it
1555 .latexWriteEndChanges(os, basefont,
1556 next_->getFont(bparams,
1559 running_font.latexWriteEndChanges(os, basefont,
1564 // Needed if there is an optional argument but no contents.
1565 if (main_body > 0 && main_body == size()) {
1567 return_value = false;
1570 switch (params().align()) {
1571 case LYX_ALIGN_NONE:
1572 case LYX_ALIGN_BLOCK:
1573 case LYX_ALIGN_LAYOUT:
1574 case LYX_ALIGN_SPECIAL:
1576 case LYX_ALIGN_LEFT:
1577 if (getParLanguage(bparams)->babel() != "hebrew") {
1578 os << "\\end{flushleft}";
1581 os << "\\end{flushright}";
1585 case LYX_ALIGN_RIGHT:
1586 if (getParLanguage(bparams)->babel() != "hebrew") {
1587 os << "\\end{flushright}";
1590 os << "\\end{flushleft}";
1594 case LYX_ALIGN_CENTER:
1595 os << "\\end{center}";
1600 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1601 return return_value;
1605 bool Paragraph::linuxDocConvertChar(char c, string & sgml_string)
1607 bool retval = false;
1609 case Paragraph::META_HFILL:
1610 sgml_string.erase();
1612 case Paragraph::META_NEWLINE:
1616 sgml_string = "&";
1619 sgml_string = "<";
1622 sgml_string = ">";
1625 sgml_string = "$";
1628 sgml_string = "#";
1631 sgml_string = "%";
1634 sgml_string = "[";
1637 sgml_string = "]";
1640 sgml_string = "{";
1643 sgml_string = "}";
1646 sgml_string = "˜";
1649 sgml_string = """;
1652 sgml_string = "\";
1658 case '\0': // Ignore :-)
1659 sgml_string.erase();
1669 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1670 BufferParams const & bparams,
1671 ostream & os, TexRow & texrow)
1673 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1675 LyXLayout const & style =
1676 textclasslist.Style(bparams.textclass,
1679 if (style.isEnvironment()){
1680 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1681 os << "\\begin{" << style.latexname() << "}{"
1682 << params().labelWidthString() << "}\n";
1683 } else if (style.labeltype == LABEL_BIBLIO) {
1685 os << "\\begin{" << style.latexname() << "}{"
1686 << bibitemWidest(buf)
1688 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1689 os << "\\begin{" << style.latexname() << '}'
1690 << style.latexparam() << '\n';
1692 os << "\\begin{" << style.latexname() << '}'
1693 << style.latexparam() << '\n';
1696 Paragraph * par = this;
1698 par = par->TeXOnePar(buf, bparams,
1701 if (par && par->params().depth() > params().depth()) {
1702 if (textclasslist.Style(bparams.textclass,
1703 par->layout).isParagraph()
1705 // How to handle this? (Lgb)
1706 //&& !suffixIs(os, "\n\n")
1708 // There should be at least one '\n' already
1709 // but we need there to be two for Standard
1710 // paragraphs that are depth-increment'ed to be
1711 // output correctly. However, tables can
1712 // also be paragraphs so don't adjust them.
1715 // Will it ever harm to have one '\n' too
1716 // many? i.e. that we sometimes will have
1717 // three in a row. (Lgb)
1721 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1724 && par->layout == layout
1725 && par->params().depth() == params().depth());
1727 if (style.isEnvironment()) {
1728 os << "\\end{" << style.latexname() << "}\n";
1731 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1732 return par; // ale970302
1736 bool Paragraph::isHfill(size_type pos) const
1738 return IsHfillChar(getChar(pos));
1742 bool Paragraph::isInset(size_type pos) const
1744 return IsInsetChar(getChar(pos));
1748 bool Paragraph::isNewline(size_type pos) const
1750 return pos >= 0 && IsNewlineChar(getChar(pos));
1754 bool Paragraph::isSeparator(size_type pos) const
1756 return IsSeparatorChar(getChar(pos));
1760 bool Paragraph::isLineSeparator(size_type pos) const
1762 return IsLineSeparatorChar(getChar(pos));
1766 bool Paragraph::isKomma(size_type pos) const
1768 return IsKommaChar(getChar(pos));
1772 /// Used by the spellchecker
1773 bool Paragraph::isLetter(Paragraph::size_type pos) const
1775 value_type const c = getChar(pos);
1776 if (IsLetterChar(c))
1778 // '\0' is not a letter, allthough every string contains "" (below)
1781 // We want to pass the ' and escape chars to ispell
1782 string const extra = lyxrc.isp_esc_chars + '\'';
1783 char ch[2] = { c, 0 };
1784 return contains(extra, ch);
1788 bool Paragraph::isWord(size_type pos ) const
1790 return IsWordChar(getChar(pos)) ;
1795 Paragraph::getParLanguage(BufferParams const & bparams) const
1798 return getFirstFontSettings().language();
1800 return previous_->getParLanguage(bparams);
1802 return bparams.language;
1806 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1808 return lyxrc.rtl_support
1809 && getParLanguage(bparams)->RightToLeft();
1813 void Paragraph::changeLanguage(BufferParams const & bparams,
1814 Language const * from, Language const * to)
1816 for (size_type i = 0; i < size(); ++i) {
1817 LyXFont font = getFontSettings(bparams, i);
1818 if (font.language() == from) {
1819 font.setLanguage(to);
1826 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1828 Language const * doc_language = bparams.language;
1829 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1830 cit != pimpl_->fontlist.end(); ++cit)
1831 if (cit->font().language() != doc_language)
1837 // Convert the paragraph to a string.
1838 // Used for building the table of contents
1839 string const Paragraph::asString(Buffer const * buffer, bool label)
1841 BufferParams const & bparams = buffer->params;
1843 if (label && !params().labelString().empty())
1844 s += params().labelString() + ' ';
1845 string::size_type const len = s.size();
1847 for (Paragraph::size_type i = 0; i < size(); ++i) {
1848 value_type c = getChar(i);
1851 else if (c == META_INSET &&
1852 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1854 getInset(i)->ascii(buffer, ost);
1855 s += subst(ost.str().c_str(),'\n',' ');
1859 if (isRightToLeftPar(bparams))
1860 reverse(s.begin() + len,s.end());
1866 string const Paragraph::asString(Buffer const * buffer,
1867 Paragraph::size_type beg,
1868 Paragraph::size_type end)
1872 if (beg == 0 && !params().labelString().empty())
1873 ost << params().labelString() << ' ';
1875 for (Paragraph::size_type i = beg; i < end; ++i) {
1876 value_type const c = getUChar(buffer->params, i);
1879 else if (c == META_INSET) {
1880 getInset(i)->ascii(buffer, ost);
1884 return ost.str().c_str();
1888 void Paragraph::setInsetOwner(Inset * i)
1890 pimpl_->inset_owner = i;
1891 for (InsetList::const_iterator cit = insetlist.begin();
1892 cit != insetlist.end(); ++cit) {
1894 cit->inset->setOwner(i);
1899 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1902 for (InsetList::const_iterator cit = insetlist.begin();
1903 cit != insetlist.end(); ++cit) {
1905 if (cit->inset->isTextInset()) {
1906 static_cast<UpdatableInset *>
1907 (cit->inset)->deleteLyXText(bv, true);
1914 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1917 for (InsetList::const_iterator cit = insetlist.begin();
1918 cit != insetlist.end(); ++cit) {
1920 if (cit->inset->isTextInset()) {
1921 static_cast<UpdatableInset *>
1922 (cit->inset)->resizeLyXText(bv, true);
1929 void Paragraph::setContentsFromPar(Paragraph * par)
1931 pimpl_->setContentsFromPar(par);
1935 Paragraph::size_type Paragraph::size() const
1937 return pimpl_->size();
1941 Paragraph::value_type
1942 Paragraph::getChar(Paragraph::size_type pos) const
1944 return pimpl_->getChar(pos);
1948 int Paragraph::id() const
1954 void Paragraph::id(int id_arg)
1956 pimpl_->id_ = id_arg;
1960 LyXTextClass::LayoutList::size_type Paragraph::getLayout() const
1966 bool Paragraph::isFirstInSequence() const
1968 Paragraph const * dhook = depthHook(getDepth());
1969 return (dhook == this
1970 || dhook->getLayout() != getLayout()
1971 || dhook->getDepth() != getDepth());
1975 Inset * Paragraph::inInset() const
1977 return pimpl_->inset_owner;
1981 void Paragraph::clearContents()
1987 void Paragraph::setCounter(int i, int v)
1989 pimpl_->counter_[i] = v;
1993 int Paragraph::getCounter(int i) const
1995 return pimpl_->counter_[i];
1999 void Paragraph::incCounter(int i)
2001 pimpl_->counter_[i]++;
2005 void Paragraph::setChar(size_type pos, value_type c)
2007 pimpl_->setChar(pos, c);
2011 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2016 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2018 return inset_iterator(insetlist.begin());
2022 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2024 return inset_iterator(insetlist.end());
2028 ParagraphParameters & Paragraph::params()
2030 return pimpl_->params;
2034 ParagraphParameters const & Paragraph::params() const
2036 return pimpl_->params;
2040 Paragraph * Paragraph::getParFromID(int id) const
2042 return pimpl_->getParFromID(id);