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, bparams.language, 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:
352 // probably we should put here all interface colors used for
353 // font displaying! For now I just add this ones I know of (Jug)
358 features.color = true;
359 lyxerr[Debug::LATEX] << "Color enabled. Font: "
360 << cit->font().stateText(0)
364 Language const * language = cit->font().language();
365 if (language != ignore_language &&
366 language != inherit_language &&
367 language->babel() != doc_language->babel())
369 features.UsedLanguages.insert(language);
370 lyxerr[Debug::LATEX] << "Found language "
371 << language->babel() << endl;
376 for (InsetList::const_iterator cit = insetlist.begin();
377 cit != insetlist.end(); ++cit) {
379 cit->inset->validate(features);
384 // First few functions needed for cut and paste and paragraph breaking.
385 void Paragraph::copyIntoMinibuffer(Buffer const & buffer,
386 Paragraph::size_type pos) const
388 BufferParams bparams = buffer.params;
390 minibuffer_char = getChar(pos);
391 minibuffer_font = getFontSettings(bparams, pos);
392 minibuffer_inset = 0;
393 if (minibuffer_char == Paragraph::META_INSET) {
395 minibuffer_inset = getInset(pos)->clone(buffer);
397 minibuffer_inset = 0;
398 minibuffer_char = ' ';
399 // This reflects what GetInset() does (ARRae)
405 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
406 Paragraph::size_type pos)
408 minibuffer_char = getChar(pos);
409 minibuffer_font = getFontSettings(bparams, pos);
410 minibuffer_inset = 0;
411 if (minibuffer_char == Paragraph::META_INSET) {
413 minibuffer_inset = getInset(pos);
414 // This is a little hack since I want exactly
415 // the inset, not just a clone. Otherwise
416 // the inset would be deleted when calling Erase(pos)
418 InsetTable search_elem(pos, 0);
419 InsetList::iterator it =
420 lower_bound(insetlist.begin(),
422 search_elem, Pimpl::matchIT());
423 if (it != insetlist.end() && it->pos == pos)
426 minibuffer_inset = 0;
427 minibuffer_char = ' ';
428 // This reflects what GetInset() does (ARRae)
433 // Erase(pos); now the caller is responsible for that.
437 bool Paragraph::insertFromMinibuffer(Paragraph::size_type pos)
439 if ((minibuffer_char == Paragraph::META_INSET) &&
440 !insetAllowed(minibuffer_inset->lyxCode()))
442 if (minibuffer_char == Paragraph::META_INSET)
443 insertInset(pos, minibuffer_inset, minibuffer_font);
445 LyXFont f = minibuffer_font;
446 if (checkInsertChar(f))
447 insertChar(pos, minibuffer_char, f);
456 void Paragraph::clear()
465 void Paragraph::erase(Paragraph::size_type pos)
471 bool Paragraph::checkInsertChar(LyXFont & font)
473 if (pimpl_->inset_owner)
474 return pimpl_->inset_owner->checkInsertChar(font);
479 void Paragraph::insertChar(Paragraph::size_type pos,
480 Paragraph::value_type c)
482 LyXFont const f(LyXFont::ALL_INHERIT);
483 insertChar(pos, c, f);
487 void Paragraph::insertChar(Paragraph::size_type pos,
488 Paragraph::value_type c,
489 LyXFont const & font)
491 pimpl_->insertChar(pos, c, font);
495 void Paragraph::insertInset(Paragraph::size_type pos,
498 LyXFont const f(LyXFont::ALL_INHERIT);
499 insertInset(pos, inset, f);
503 void Paragraph::insertInset(Paragraph::size_type pos,
504 Inset * inset, LyXFont const & font)
506 pimpl_->insertInset(pos, inset, font);
510 bool Paragraph::insetAllowed(Inset::Code code)
512 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
514 if (pimpl_->inset_owner)
515 return pimpl_->inset_owner->insetAllowed(code);
520 Inset * Paragraph::getInset(Paragraph::size_type pos)
522 lyx::Assert(pos < size());
525 InsetTable search_inset(pos, 0);
526 InsetList::iterator it = lower_bound(insetlist.begin(),
528 search_inset, Pimpl::matchIT());
529 if (it != insetlist.end() && it->pos == pos)
532 lyxerr << "ERROR (Paragraph::GetInset): "
533 "Inset does not exist: " << pos << endl;
536 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
537 // Did this commenting out introduce a bug? So far I have not
538 // see any, please enlighten me. (Lgb)
539 // My guess is that since the inset does not exist, we might
540 // as well replace it with a space to prevent craches. (Asger)
545 Inset const * Paragraph::getInset(Paragraph::size_type pos) const
547 lyx::Assert(pos < size());
550 InsetTable search_inset(pos, 0);
551 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
553 search_inset, Pimpl::matchIT());
554 if (cit != insetlist.end() && cit->pos == pos)
557 lyxerr << "ERROR (Paragraph::GetInset): "
558 "Inset does not exist: " << pos << endl;
560 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
561 // Did this commenting out introduce a bug? So far I have not
562 // see any, please enlighten me. (Lgb)
563 // My guess is that since the inset does not exist, we might
564 // as well replace it with a space to prevent craches. (Asger)
569 // Gets uninstantiated font setting at position.
570 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
571 Paragraph::size_type pos) const
573 lyx::Assert(pos <= size());
575 Pimpl::FontTable search_font(pos, LyXFont());
576 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
577 pimpl_->fontlist.end(),
578 search_font, Pimpl::matchFT());
580 if (cit != pimpl_->fontlist.end()) {
581 retfont = cit->font();
582 } else if (pos == size() && size()) {
583 retfont = getFontSettings(bparams, pos - 1);
585 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
586 if (retfont.language() == inherit_language)
587 retfont.setLanguage(bparams.language);
593 // Gets uninstantiated font setting at position 0
594 LyXFont const Paragraph::getFirstFontSettings() const
597 if (!pimpl_->fontlist.empty())
598 return pimpl_->fontlist[0].font();
601 return LyXFont(LyXFont::ALL_INHERIT);
605 // Gets the fully instantiated font at a given position in a paragraph
606 // This is basically the same function as LyXText::GetFont() in text2.C.
607 // The difference is that this one is used for generating the LaTeX file,
608 // and thus cosmetic "improvements" are disallowed: This has to deliver
609 // the true picture of the buffer. (Asger)
610 // If position is -1, we get the layout font of the paragraph.
611 // If position is -2, we get the font of the manual label of the paragraph.
612 LyXFont const Paragraph::getFont(BufferParams const & bparams,
613 Paragraph::size_type pos) const
616 LyXLayout const & layout =
617 textclasslist.Style(bparams.textclass,
619 Paragraph::size_type main_body = 0;
620 if (layout.labeltype == LABEL_MANUAL)
621 main_body = beginningOfMainBody();
626 layoutfont = layout.labelfont;
628 layoutfont = layout.font;
629 tmpfont = getFontSettings(bparams, pos);
630 tmpfont.realize(layoutfont, bparams.language);
632 // process layoutfont for pos == -1 and labelfont for pos < -1
634 tmpfont = layout.font;
636 tmpfont = layout.labelfont;
637 tmpfont.setLanguage(getParLanguage(bparams));
640 // check for environment font information
641 char par_depth = getDepth();
642 Paragraph const * par = this;
643 while (par && par->getDepth() && !tmpfont.resolved()) {
644 par = par->outerHook();
646 tmpfont.realize(textclasslist.
647 Style(bparams.textclass,
648 par->getLayout()).font, bparams.language);
649 par_depth = par->getDepth();
653 tmpfont.realize(textclasslist
654 .TextClass(bparams.textclass)
655 .defaultfont(), bparams.language);
660 /// Returns the height of the highest font in range
662 Paragraph::highestFontInRange(Paragraph::size_type startpos,
663 Paragraph::size_type endpos) const
665 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
666 if (pimpl_->fontlist.empty())
669 Pimpl::FontTable end_search(endpos, LyXFont());
670 Pimpl::FontList::const_iterator end_it = lower_bound(pimpl_->fontlist.begin(),
671 pimpl_->fontlist.end(),
672 end_search, Pimpl::matchFT());
673 if (end_it != pimpl_->fontlist.end())
676 Pimpl::FontTable start_search(startpos, LyXFont());
677 for (Pimpl::FontList::const_iterator cit =
678 lower_bound(pimpl_->fontlist.begin(),
679 pimpl_->fontlist.end(),
680 start_search, Pimpl::matchFT());
681 cit != end_it; ++cit) {
682 LyXFont::FONT_SIZE size = cit->font().size();
683 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
690 Paragraph::value_type
691 Paragraph::getUChar(BufferParams const & bparams,
692 Paragraph::size_type pos) const
694 value_type c = getChar(pos);
695 if (!lyxrc.rtl_support)
725 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
732 void Paragraph::setFont(Paragraph::size_type pos,
733 LyXFont const & font)
735 lyx::Assert(pos <= size());
737 // First, reduce font against layout/label font
738 // Update: The SetCharFont() routine in text2.C already
739 // reduces font, so we don't need to do that here. (Asger)
740 // No need to simplify this because it will disappear
741 // in a new kernel. (Asger)
742 // Next search font table
744 Pimpl::FontTable search_font(pos, LyXFont());
745 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
746 pimpl_->fontlist.end(),
747 search_font, Pimpl::matchFT());
748 unsigned int i = it - pimpl_->fontlist.begin();
749 bool notfound = it == pimpl_->fontlist.end();
751 if (!notfound && pimpl_->fontlist[i].font() == font)
754 bool begin = pos == 0 || notfound ||
755 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
756 // Is position pos is a beginning of a font block?
757 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
758 // Is position pos is the end of a font block?
759 if (begin && end) { // A single char block
760 if (i + 1 < pimpl_->fontlist.size() &&
761 pimpl_->fontlist[i + 1].font() == font) {
762 // Merge the singleton block with the next block
763 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
764 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
765 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
766 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
767 // Merge the singleton block with the previous block
768 pimpl_->fontlist[i - 1].pos(pos);
769 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
771 pimpl_->fontlist[i].font(font);
773 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
774 pimpl_->fontlist[i - 1].pos(pos);
776 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
777 Pimpl::FontTable(pos, font));
779 pimpl_->fontlist[i].pos(pos - 1);
780 if (!(i + 1 < pimpl_->fontlist.size() &&
781 pimpl_->fontlist[i + 1].font() == font))
782 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
783 Pimpl::FontTable(pos, font));
784 } else { // The general case. The block is splitted into 3 blocks
785 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
786 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
787 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
788 Pimpl::FontTable(pos, font));
794 void Paragraph::next(Paragraph * p)
800 // This function is able to hide closed footnotes.
801 Paragraph * Paragraph::next()
807 Paragraph const * Paragraph::next() const
813 void Paragraph::previous(Paragraph * p)
819 // This function is able to hide closed footnotes.
820 Paragraph * Paragraph::previous()
826 // This function is able to hide closed footnotes.
827 Paragraph const * Paragraph::previous() const
833 void Paragraph::breakParagraph(BufferParams const & bparams,
834 Paragraph::size_type pos,
837 // create a new paragraph
838 Paragraph * tmp = new Paragraph(this);
839 // remember to set the inset_owner
840 tmp->setInsetOwner(inInset());
842 // this is an idea for a more userfriendly layout handling, I will
843 // see what the users say
845 // layout stays the same with latex-environments
847 tmp->setOnlyLayout(layout);
848 tmp->setLabelWidthString(params().labelWidthString());
851 if (size() > pos || !size() || flag == 2) {
852 tmp->setOnlyLayout(layout);
853 tmp->params().align(params().align());
854 tmp->setLabelWidthString(params().labelWidthString());
856 tmp->params().lineBottom(params().lineBottom());
857 params().lineBottom(false);
858 tmp->params().pagebreakBottom(params().pagebreakBottom());
859 params().pagebreakBottom(false);
860 tmp->params().spaceBottom(params().spaceBottom());
861 params().spaceBottom(VSpace(VSpace::NONE));
863 tmp->params().depth(params().depth());
864 tmp->params().noindent(params().noindent());
866 // copy everything behind the break-position
867 // to the new paragraph
868 size_type pos_end = pimpl_->size() - 1;
871 for (; i <= pos_end; ++i) {
872 cutIntoMinibuffer(bparams, i);
873 if (tmp->insertFromMinibuffer(j - pos))
876 for (i = pos_end; i >= pos; --i) {
881 // just an idea of me
883 tmp->params().lineTop(params().lineTop());
884 tmp->params().pagebreakTop(params().pagebreakTop());
885 tmp->params().spaceTop(params().spaceTop());
886 tmp->bibkey = bibkey;
888 // layout stays the same with latex-environments
890 setOnlyLayout(tmp->layout);
891 setLabelWidthString(tmp->params().labelWidthString());
892 params().depth(tmp->params().depth());
898 void Paragraph::makeSameLayout(Paragraph const * par)
900 layout = par->layout;
902 params() = par->params();
906 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass)
908 if (textclasslist.Style(tclass, getLayout()).free_spacing)
913 && (isNewline(0) || isLineSeparator(0))){
922 bool Paragraph::hasSameLayout(Paragraph const * par) const
925 par->layout == layout &&
926 params().sameLayout(par->params());
930 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
931 Paragraph::size_type pos)
933 // create a new paragraph
934 Paragraph * tmp = new Paragraph(this);
935 tmp->makeSameLayout(this);
937 // When can pos > Last()?
938 // I guess pos == Last() is possible.
940 // copy everything behind the break-position to the new
942 size_type pos_end = pimpl_->size() - 1;
946 for (size_type i = pos, j = pos; i <= pos_end; ++i) {
947 cutIntoMinibuffer(bparams, i);
948 if (tmp->insertFromMinibuffer(j - pos))
952 for (size_type k = pos_end; k >= pos; --k) {
959 // Be carefull, this does not make any check at all.
960 // This method has wrong name, it combined this par with the next par.
961 // In that sense it is the reverse of break paragraph. (Lgb)
962 void Paragraph::pasteParagraph(BufferParams const & bparams)
964 // copy the next paragraph to this one
965 Paragraph * the_next = next();
967 // first the DTP-stuff
968 params().lineBottom(the_next->params().lineBottom());
969 params().spaceBottom(the_next->params().spaceBottom());
970 params().pagebreakBottom(the_next->params().pagebreakBottom());
972 size_type pos_end = the_next->pimpl_->size() - 1;
973 size_type pos_insert = size();
975 // ok, now copy the paragraph
976 for (size_type i = 0, j = 0; i <= pos_end; ++i) {
977 the_next->cutIntoMinibuffer(bparams, i);
978 if (insertFromMinibuffer(pos_insert + j))
982 // delete the next paragraph
983 Paragraph * ppar = the_next->previous_;
984 Paragraph * npar = the_next->next_;
990 int Paragraph::getEndLabel(BufferParams const & bparams) const
992 Paragraph const * par = this;
993 depth_type par_depth = getDepth();
995 LyXTextClass::LayoutList::size_type layout = par->getLayout();
996 int const endlabeltype =
997 textclasslist.Style(bparams.textclass,
998 layout).endlabeltype;
999 if (endlabeltype != END_LABEL_NO_LABEL) {
1001 return endlabeltype;
1003 depth_type const next_depth = next_->getDepth();
1004 if (par_depth > next_depth ||
1005 (par_depth == next_depth
1006 && layout != next_->getLayout()))
1007 return endlabeltype;
1012 par = par->outerHook();
1014 par_depth = par->getDepth();
1016 return END_LABEL_NO_LABEL;
1020 Paragraph::depth_type Paragraph::getDepth() const
1022 return params().depth();
1026 char Paragraph::getAlign() const
1028 return params().align();
1032 string const & Paragraph::getLabelstring() const
1034 return params().labelString();
1038 int Paragraph::getFirstCounter(int i) const
1040 return pimpl_->counter_[i];
1044 // the next two functions are for the manual labels
1045 string const Paragraph::getLabelWidthString() const
1047 if (!params().labelWidthString().empty())
1048 return params().labelWidthString();
1050 return _("Senseless with this layout!");
1054 void Paragraph::setLabelWidthString(string const & s)
1056 params().labelWidthString(s);
1060 void Paragraph::setOnlyLayout(LyXTextClass::size_type new_layout)
1062 layout = new_layout;
1066 void Paragraph::setLayout(LyXTextClass::size_type new_layout)
1068 layout = new_layout;
1069 params().labelWidthString(string());
1070 params().align(LYX_ALIGN_LAYOUT);
1071 params().spaceTop(VSpace(VSpace::NONE));
1072 params().spaceBottom(VSpace(VSpace::NONE));
1073 params().spacing(Spacing(Spacing::Default));
1077 // if the layout of a paragraph contains a manual label, the beginning of the
1078 // main body is the beginning of the second word. This is what the par-
1079 // function returns. If the layout does not contain a label, the main
1080 // body always starts with position 0. This differentiation is necessary,
1081 // because there cannot be a newline or a blank <= the beginning of the
1082 // main body in TeX.
1084 int Paragraph::beginningOfMainBody() const
1086 // Unroll the first two cycles of the loop
1087 // and remember the previous character to
1088 // remove unnecessary GetChar() calls
1091 && getChar(i) != Paragraph::META_NEWLINE) {
1093 char previous_char = 0;
1096 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1097 // Yes, this ^ is supposed to be "= " not "=="
1100 && previous_char != ' '
1101 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1103 previous_char = temp;
1112 Paragraph * Paragraph::depthHook(depth_type depth)
1114 Paragraph * newpar = this;
1117 newpar = newpar->previous();
1118 } while (newpar && newpar->getDepth() > depth);
1121 if (previous() || getDepth())
1122 lyxerr << "ERROR (Paragraph::DepthHook): "
1131 Paragraph const * Paragraph::depthHook(depth_type depth) const
1133 Paragraph const * newpar = this;
1136 newpar = newpar->previous();
1137 } while (newpar && newpar->getDepth() > depth);
1140 if (previous() || getDepth())
1141 lyxerr << "ERROR (Paragraph::DepthHook): "
1149 Paragraph * Paragraph::outerHook()
1153 return depthHook(depth_type(getDepth() - 1));
1156 Paragraph const * Paragraph::outerHook() const
1160 return depthHook(depth_type(getDepth() - 1));
1163 int Paragraph::autoDeleteInsets()
1166 InsetList::size_type index = 0;
1167 while (index < insetlist.size()) {
1168 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1169 erase(insetlist[index].pos);
1170 // Erase() calls to insetlist.erase(&insetlist[index])
1171 // so index shouldn't be increased.
1180 Paragraph::inset_iterator
1181 Paragraph::InsetIterator(Paragraph::size_type pos)
1183 InsetTable search_inset(pos, 0);
1184 InsetList::iterator it = lower_bound(insetlist.begin(),
1186 search_inset, Pimpl::matchIT());
1187 return inset_iterator(it);
1191 // returns -1 if inset not found
1192 int Paragraph::getPositionOfInset(Inset * inset) const
1195 for (InsetList::const_iterator cit = insetlist.begin();
1196 cit != insetlist.end(); ++cit) {
1197 if (cit->inset == inset) {
1201 if (inset == bibkey)
1208 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1209 BufferParams const & bparams,
1210 ostream & os, TexRow & texrow,
1213 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1214 LyXLayout const & style =
1215 textclasslist.Style(bparams.textclass,
1218 bool further_blank_line = false;
1220 if (params().startOfAppendix()) {
1221 os << "\\appendix\n";
1225 if (!params().spacing().isDefault()
1226 && (!previous() || !previous()->hasSameLayout(this))) {
1227 os << params().spacing().writeEnvirBegin() << "\n";
1231 if (tex_code_break_column && style.isCommand()){
1236 if (params().pagebreakTop()) {
1238 further_blank_line = true;
1240 if (params().spaceTop().kind() != VSpace::NONE) {
1241 os << params().spaceTop().asLatexCommand(bparams);
1242 further_blank_line = true;
1245 if (params().lineTop()) {
1246 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1247 << "\\vspace{-1\\parskip}";
1248 further_blank_line = true;
1251 if (further_blank_line){
1256 Language const * language = getParLanguage(bparams);
1257 Language const * doc_language = bparams.language;
1258 Language const * previous_language = previous_
1259 ? previous_->getParLanguage(bparams) : doc_language;
1260 if (language == ignore_language || language == inherit_language)
1261 lyxerr << "1:" << language->lang() << endl;
1262 if (language->babel() != doc_language->babel() &&
1263 language->babel() != previous_language->babel()) {
1264 os << subst(lyxrc.language_command_begin, "$$lang",
1270 if (bparams.inputenc == "auto" &&
1271 language->encoding() != previous_language->encoding())
1273 os << "\\inputencoding{"
1274 << language->encoding()->LatexName()
1279 switch (style.latextype) {
1282 << style.latexname()
1283 << style.latexparam();
1285 case LATEX_ITEM_ENVIRONMENT:
1287 bibkey->latex(buf, os, false, false);
1291 case LATEX_LIST_ENVIRONMENT:
1298 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1300 // Make sure that \\par is done with the font of the last
1301 // character if this has another size as the default.
1302 // This is necessary because LaTeX (and LyX on the screen)
1303 // calculates the space between the baselines according
1304 // to this font. (Matthias)
1306 // Is this really needed ? (Dekel)
1307 // We do not need to use to change the font for the last paragraph
1308 // or for a command.
1309 LyXFont font = getFont(bparams, size() - 1);
1311 bool is_command = textclasslist.Style(bparams.textclass,
1312 getLayout()).isCommand();
1313 if (style.resfont.size() != font.size() && next_ && !is_command) {
1316 os << "\\" << font.latexSize() << " \\par}";
1317 } else if (need_par) {
1319 } else if (is_command)
1322 if (language->babel() != doc_language->babel() &&
1324 next_->getParLanguage(bparams)->babel() != language->babel()))
1327 << subst(lyxrc.language_command_end, "$$lang",
1328 doc_language->babel());
1331 switch (style.latextype) {
1332 case LATEX_ITEM_ENVIRONMENT:
1333 case LATEX_LIST_ENVIRONMENT:
1334 if (next_ && (params().depth() < next_->params().depth())) {
1339 case LATEX_ENVIRONMENT:
1340 // if its the last paragraph of the current environment
1341 // skip it otherwise fall through
1343 && (next_->layout != layout
1344 || next_->params().depth() != params().depth()))
1347 // we don't need it for the last paragraph!!!
1348 // or for tables in floats
1349 // -- effectively creates a \par where there isn't one which
1350 // breaks a \subfigure or \subtable.
1357 further_blank_line = false;
1358 if (params().lineBottom()) {
1359 os << "\\lyxline{\\" << getFont(bparams,
1360 size() - 1).latexSize() << '}';
1361 further_blank_line = true;
1364 if (params().spaceBottom().kind() != VSpace::NONE) {
1365 os << params().spaceBottom().asLatexCommand(bparams);
1366 further_blank_line = true;
1369 if (params().pagebreakBottom()) {
1371 further_blank_line = true;
1374 if (further_blank_line){
1379 if (!params().spacing().isDefault()
1380 && (!next_ || !next_->hasSameLayout(this))) {
1381 os << params().spacing().writeEnvirEnd() << "\n";
1385 // we don't need it for the last paragraph!!!
1391 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1396 // This one spits out the text of the paragraph
1397 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1398 BufferParams const & bparams,
1399 ostream & os, TexRow & texrow,
1402 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1404 bool return_value = false;
1406 LyXLayout const & style =
1407 textclasslist.Style(bparams.textclass,
1411 // Maybe we have to create a optional argument.
1412 size_type main_body;
1413 if (style.labeltype != LABEL_MANUAL)
1416 main_body = beginningOfMainBody();
1420 if (main_body > 0) {
1423 basefont = getFont(bparams, -2); // Get label font
1425 basefont = getFont(bparams, -1); // Get layout font
1429 && !pimpl_->size()) {
1430 if (style.isCommand()) {
1436 moving_arg |= style.needprotect;
1438 // Which font is currently active?
1439 LyXFont running_font(basefont);
1440 // Do we have an open font change?
1441 bool open_font = false;
1443 texrow.start(this, 0);
1445 for (size_type i = 0; i < size(); ++i) {
1447 // First char in paragraph or after label?
1448 if (i == main_body) {
1449 if (main_body > 0) {
1451 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1454 basefont = getFont(bparams, -1); // Now use the layout font
1455 running_font = basefont;
1459 if (style.isCommand()) {
1464 if (params().noindent()) {
1465 os << "\\noindent ";
1468 switch (params().align()) {
1469 case LYX_ALIGN_NONE:
1470 case LYX_ALIGN_BLOCK:
1471 case LYX_ALIGN_LAYOUT:
1472 case LYX_ALIGN_SPECIAL:
1474 case LYX_ALIGN_LEFT:
1475 if (getParLanguage(bparams)->babel() != "hebrew") {
1476 os << "\\begin{flushleft}";
1479 os << "\\begin{flushright}";
1483 case LYX_ALIGN_RIGHT:
1484 if (getParLanguage(bparams)->babel() != "hebrew") {
1485 os << "\\begin{flushright}";
1488 os << "\\begin{flushleft}";
1492 case LYX_ALIGN_CENTER:
1493 os << "\\begin{center}";
1499 value_type c = getChar(i);
1501 // Fully instantiated font
1502 LyXFont font = getFont(bparams, i);
1504 LyXFont last_font = running_font;
1506 // Spaces at end of font change are simulated to be
1507 // outside font change, i.e. we write "\textXX{text} "
1508 // rather than "\textXX{text }". (Asger)
1509 if (open_font && c == ' ' && i <= size() - 2
1510 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
1511 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
1512 font = getFont(bparams, i + 1);
1514 // We end font definition before blanks
1515 if (!font.equalExceptLatex(running_font) && open_font) {
1516 column += running_font.latexWriteEndChanges(os,
1518 (i == main_body-1) ? basefont : font);
1519 running_font = basefont;
1523 // Blanks are printed before start of fontswitch
1525 // Do not print the separation of the optional argument
1526 if (i != main_body - 1) {
1527 pimpl_->simpleTeXBlanks(os, texrow, i,
1528 column, font, style);
1532 // Do we need to change font?
1533 if (!font.equalExceptLatex(running_font)
1534 && i != main_body-1) {
1535 column += font.latexWriteStartChanges(os, basefont,
1537 running_font = font;
1541 if (c == Paragraph::META_NEWLINE) {
1542 // newlines are handled differently here than
1543 // the default in SimpleTeXSpecialChars().
1544 if (!style.newline_allowed
1546 || font.latex() == LyXFont::ON
1552 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1555 basefont = getFont(bparams, -1);
1556 running_font = basefont;
1557 if (font.family() ==
1558 LyXFont::TYPEWRITER_FAMILY) {
1566 texrow.start(this, i + 1);
1569 pimpl_->simpleTeXSpecialChars(buf, bparams,
1570 os, texrow, moving_arg,
1572 basefont, open_font,
1573 style, i, column, c);
1577 // If we have an open font definition, we have to close it
1579 #ifdef FIXED_LANGUAGE_END_DETECTION
1582 .latexWriteEndChanges(os, basefont,
1583 next_->getFont(bparams,
1586 running_font.latexWriteEndChanges(os, basefont,
1590 #ifdef WITH_WARNINGS
1591 #warning For now we ALWAYS have to close the foreign font settings if they are
1592 #warning there as we start another \selectlanguage with the next paragraph if
1593 #warning we are in need of this. This should be fixed sometime (Jug)
1595 running_font.latexWriteEndChanges(os, basefont, basefont);
1599 // Needed if there is an optional argument but no contents.
1600 if (main_body > 0 && main_body == size()) {
1602 return_value = false;
1605 switch (params().align()) {
1606 case LYX_ALIGN_NONE:
1607 case LYX_ALIGN_BLOCK:
1608 case LYX_ALIGN_LAYOUT:
1609 case LYX_ALIGN_SPECIAL:
1611 case LYX_ALIGN_LEFT:
1612 if (getParLanguage(bparams)->babel() != "hebrew") {
1613 os << "\\end{flushleft}";
1616 os << "\\end{flushright}";
1620 case LYX_ALIGN_RIGHT:
1621 if (getParLanguage(bparams)->babel() != "hebrew") {
1622 os << "\\end{flushright}";
1625 os << "\\end{flushleft}";
1629 case LYX_ALIGN_CENTER:
1630 os << "\\end{center}";
1635 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1636 return return_value;
1640 bool Paragraph::linuxDocConvertChar(char c, string & sgml_string)
1642 bool retval = false;
1644 case Paragraph::META_HFILL:
1645 sgml_string.erase();
1647 case Paragraph::META_NEWLINE:
1651 sgml_string = "&";
1654 sgml_string = "<";
1657 sgml_string = ">";
1660 sgml_string = "$";
1663 sgml_string = "#";
1666 sgml_string = "%";
1669 sgml_string = "[";
1672 sgml_string = "]";
1675 sgml_string = "{";
1678 sgml_string = "}";
1681 sgml_string = "˜";
1684 sgml_string = """;
1687 sgml_string = "\";
1693 case '\0': // Ignore :-)
1694 sgml_string.erase();
1704 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1705 BufferParams const & bparams,
1706 ostream & os, TexRow & texrow)
1708 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1710 LyXLayout const & style =
1711 textclasslist.Style(bparams.textclass,
1714 if (style.isEnvironment()){
1715 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1716 os << "\\begin{" << style.latexname() << "}{"
1717 << params().labelWidthString() << "}\n";
1718 } else if (style.labeltype == LABEL_BIBLIO) {
1720 os << "\\begin{" << style.latexname() << "}{"
1721 << bibitemWidest(buf)
1723 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1724 os << "\\begin{" << style.latexname() << '}'
1725 << style.latexparam() << '\n';
1727 os << "\\begin{" << style.latexname() << '}'
1728 << style.latexparam() << '\n';
1731 Paragraph * par = this;
1733 par = par->TeXOnePar(buf, bparams,
1736 if (par && par->params().depth() > params().depth()) {
1737 if (textclasslist.Style(bparams.textclass,
1738 par->layout).isParagraph()
1740 // How to handle this? (Lgb)
1741 //&& !suffixIs(os, "\n\n")
1743 // There should be at least one '\n' already
1744 // but we need there to be two for Standard
1745 // paragraphs that are depth-increment'ed to be
1746 // output correctly. However, tables can
1747 // also be paragraphs so don't adjust them.
1750 // Will it ever harm to have one '\n' too
1751 // many? i.e. that we sometimes will have
1752 // three in a row. (Lgb)
1756 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1759 && par->layout == layout
1760 && par->params().depth() == params().depth());
1762 if (style.isEnvironment()) {
1763 os << "\\end{" << style.latexname() << "}\n";
1766 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1767 return par; // ale970302
1771 bool Paragraph::isHfill(size_type pos) const
1773 return IsHfillChar(getChar(pos));
1777 bool Paragraph::isInset(size_type pos) const
1779 return IsInsetChar(getChar(pos));
1783 bool Paragraph::isNewline(size_type pos) const
1785 return pos >= 0 && IsNewlineChar(getChar(pos));
1789 bool Paragraph::isSeparator(size_type pos) const
1791 return IsSeparatorChar(getChar(pos));
1795 bool Paragraph::isLineSeparator(size_type pos) const
1797 return IsLineSeparatorChar(getChar(pos));
1801 bool Paragraph::isKomma(size_type pos) const
1803 return IsKommaChar(getChar(pos));
1807 /// Used by the spellchecker
1808 bool Paragraph::isLetter(Paragraph::size_type pos) const
1810 value_type const c = getChar(pos);
1811 if (IsLetterChar(c))
1813 // '\0' is not a letter, allthough every string contains "" (below)
1816 // We want to pass the ' and escape chars to ispell
1817 string const extra = lyxrc.isp_esc_chars + '\'';
1818 char ch[2] = { c, 0 };
1819 return contains(extra, ch);
1823 bool Paragraph::isWord(size_type pos ) const
1825 return IsWordChar(getChar(pos)) ;
1830 Paragraph::getParLanguage(BufferParams const & bparams) const
1833 Language const * lang = getFirstFontSettings().language();
1834 #warning We should make this somewhat better, any ideas? (Jug)
1835 if (lang == inherit_language || lang == ignore_language)
1836 lang = bparams.language;
1838 } else if (previous_)
1839 return previous_->getParLanguage(bparams);
1841 return bparams.language;
1845 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1847 return lyxrc.rtl_support
1848 && getParLanguage(bparams)->RightToLeft();
1852 void Paragraph::changeLanguage(BufferParams const & bparams,
1853 Language const * from, Language const * to)
1855 for (size_type i = 0; i < size(); ++i) {
1856 LyXFont font = getFontSettings(bparams, i);
1857 if (font.language() == from) {
1858 font.setLanguage(to);
1865 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1867 Language const * doc_language = bparams.language;
1868 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1869 cit != pimpl_->fontlist.end(); ++cit)
1870 if (cit->font().language() != inherit_language &&
1871 cit->font().language() != ignore_language &&
1872 cit->font().language() != doc_language)
1878 // Convert the paragraph to a string.
1879 // Used for building the table of contents
1880 string const Paragraph::asString(Buffer const * buffer, bool label)
1882 BufferParams const & bparams = buffer->params;
1884 if (label && !params().labelString().empty())
1885 s += params().labelString() + ' ';
1886 string::size_type const len = s.size();
1888 for (Paragraph::size_type i = 0; i < size(); ++i) {
1889 value_type c = getChar(i);
1892 else if (c == META_INSET &&
1893 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1895 getInset(i)->ascii(buffer, ost);
1896 s += subst(ost.str().c_str(),'\n',' ');
1900 if (isRightToLeftPar(bparams))
1901 reverse(s.begin() + len,s.end());
1907 string const Paragraph::asString(Buffer const * buffer,
1908 Paragraph::size_type beg,
1909 Paragraph::size_type end)
1913 if (beg == 0 && !params().labelString().empty())
1914 ost << params().labelString() << ' ';
1916 for (Paragraph::size_type i = beg; i < end; ++i) {
1917 value_type const c = getUChar(buffer->params, i);
1920 else if (c == META_INSET) {
1921 getInset(i)->ascii(buffer, ost);
1925 return ost.str().c_str();
1929 void Paragraph::setInsetOwner(Inset * i)
1931 pimpl_->inset_owner = i;
1932 for (InsetList::const_iterator cit = insetlist.begin();
1933 cit != insetlist.end(); ++cit) {
1935 cit->inset->setOwner(i);
1940 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1943 for (InsetList::const_iterator cit = insetlist.begin();
1944 cit != insetlist.end(); ++cit) {
1946 if (cit->inset->isTextInset()) {
1947 static_cast<UpdatableInset *>
1948 (cit->inset)->deleteLyXText(bv, true);
1955 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1958 for (InsetList::const_iterator cit = insetlist.begin();
1959 cit != insetlist.end(); ++cit) {
1961 if (cit->inset->isTextInset()) {
1962 static_cast<UpdatableInset *>
1963 (cit->inset)->resizeLyXText(bv, true);
1970 void Paragraph::setContentsFromPar(Paragraph * par)
1972 pimpl_->setContentsFromPar(par);
1976 Paragraph::size_type Paragraph::size() const
1978 return pimpl_->size();
1982 Paragraph::value_type
1983 Paragraph::getChar(Paragraph::size_type pos) const
1985 return pimpl_->getChar(pos);
1989 int Paragraph::id() const
1995 void Paragraph::id(int id_arg)
1997 pimpl_->id_ = id_arg;
2001 LyXTextClass::LayoutList::size_type Paragraph::getLayout() const
2007 bool Paragraph::isFirstInSequence() const
2009 Paragraph const * dhook = depthHook(getDepth());
2010 return (dhook == this
2011 || dhook->getLayout() != getLayout()
2012 || dhook->getDepth() != getDepth());
2016 Inset * Paragraph::inInset() const
2018 return pimpl_->inset_owner;
2022 void Paragraph::clearContents()
2028 void Paragraph::setCounter(int i, int v)
2030 pimpl_->counter_[i] = v;
2034 int Paragraph::getCounter(int i) const
2036 return pimpl_->counter_[i];
2040 void Paragraph::incCounter(int i)
2042 pimpl_->counter_[i]++;
2046 void Paragraph::setChar(size_type pos, value_type c)
2048 pimpl_->setChar(pos, c);
2052 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2057 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2059 return inset_iterator(insetlist.begin());
2063 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2065 return inset_iterator(insetlist.end());
2069 ParagraphParameters & Paragraph::params()
2071 return pimpl_->params;
2075 ParagraphParameters const & Paragraph::params() const
2077 return pimpl_->params;
2081 Paragraph * Paragraph::getParFromID(int id) const
2083 return pimpl_->getParFromID(id);