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;
56 using lyx::layout_type;
58 int tex_code_break_column = 72; // needs non-zero initialization. set later.
59 // this is a bad idea, but how can Paragraph find its buffer to get
60 // parameters? (JMarc)
62 extern string bibitemWidest(Buffer const *);
64 // this is a minibuffer
69 LyXFont minibuffer_font;
70 Inset * minibuffer_inset;
75 extern BufferView * current_view;
78 Paragraph::Paragraph()
79 : layout(0), pimpl_(new Paragraph::Pimpl(this))
81 for (int i = 0; i < 10; ++i)
87 bibkey = 0; // ale970302
92 // This constructor inserts the new paragraph in a list.
93 Paragraph::Paragraph(Paragraph * par)
94 : layout(0), pimpl_(new Paragraph::Pimpl(this))
96 for (int i = 0; i < 10; ++i)
101 // double linked list begin
104 next_->previous_ = this;
106 previous_->next_ = this;
109 bibkey = 0; // ale970302
115 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
116 : layout(0), pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
118 for (int i = 0; i < 10; ++i)
125 // this is because of the dummy layout of the paragraphs that
131 bibkey = static_cast<InsetBibKey *>
132 (lp.bibkey->clone(*current_view->buffer()));
137 // copy everything behind the break-position to the new paragraph
138 insetlist = lp.insetlist;
139 for (InsetList::iterator it = insetlist.begin();
140 it != insetlist.end(); ++it)
142 it->inset = it->inset->clone(*current_view->buffer(), same_ids);
143 // tell the new inset who is the boss now
144 it->inset->parOwner(this);
149 // the destructor removes the new paragraph from the list
150 Paragraph::~Paragraph()
153 previous_->next_ = next_;
155 next_->previous_ = previous_;
157 for (InsetList::iterator it = insetlist.begin();
158 it != insetlist.end(); ++it) {
167 //lyxerr << "Paragraph::paragraph_id = "
168 // << Paragraph::paragraph_id << endl;
172 void Paragraph::writeFile(Buffer const * buf, ostream & os,
173 BufferParams const & bparams,
174 depth_type dth) const
176 // The beginning or end of a deeper (i.e. nested) area?
177 if (dth != params().depth()) {
178 if (params().depth() > dth) {
179 while (params().depth() > dth) {
180 os << "\n\\begin_deeper ";
184 while (params().depth() < dth) {
185 os << "\n\\end_deeper ";
191 // First write the layout
193 << textclasslist.NameOfLayout(bparams.textclass, layout)
196 // Maybe some vertical spaces.
197 if (params().spaceTop().kind() != VSpace::NONE)
198 os << "\\added_space_top "
199 << params().spaceTop().asLyXCommand() << " ";
200 if (params().spaceBottom().kind() != VSpace::NONE)
201 os << "\\added_space_bottom "
202 << params().spaceBottom().asLyXCommand() << " ";
204 // Maybe the paragraph has special spacing
205 params().spacing().writeFile(os, true);
207 // The labelwidth string used in lists.
208 if (!params().labelWidthString().empty())
209 os << "\\labelwidthstring "
210 << params().labelWidthString() << '\n';
212 // Lines above or below?
213 if (params().lineTop())
215 if (params().lineBottom())
216 os << "\\line_bottom ";
218 // Pagebreaks above or below?
219 if (params().pagebreakTop())
220 os << "\\pagebreak_top ";
221 if (params().pagebreakBottom())
222 os << "\\pagebreak_bottom ";
224 // Start of appendix?
225 if (params().startOfAppendix())
226 os << "\\start_of_appendix ";
229 if (params().noindent())
233 if (params().align() != LYX_ALIGN_LAYOUT) {
235 switch (params().align()) {
236 case LYX_ALIGN_LEFT: h = 1; break;
237 case LYX_ALIGN_RIGHT: h = 2; break;
238 case LYX_ALIGN_CENTER: h = 3; break;
239 default: h = 0; break;
241 os << "\\align " << string_align[h] << " ";
246 bibkey->write(buf, os);
248 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
251 for (pos_type i = 0; i < size(); ++i) {
257 // Write font changes
258 LyXFont font2 = getFontSettings(bparams, i);
259 if (font2 != font1) {
260 #ifndef INHERIT_LANGUAGE
261 font2.lyxWriteChanges(font1, os);
263 font2.lyxWriteChanges(font1, bparams.language, os);
269 value_type const c = getChar(i);
273 Inset const * inset = getInset(i);
275 if (inset->directWrite()) {
276 // international char, let it write
277 // code directly so it's shorter in
279 inset->write(buf, os);
281 os << "\n\\begin_inset ";
282 inset->write(buf, os);
283 os << "\n\\end_inset \n\n";
289 os << "\n\\newline \n";
293 os << "\n\\hfill \n";
297 os << "\n\\backslash \n";
301 if (i + 1 < size() && getChar(i + 1) == ' ') {
308 if ((column > 70 && c == ' ')
313 // this check is to amend a bug. LyX sometimes
314 // inserts '\0' this could cause problems.
318 lyxerr << "ERROR (Paragraph::writeFile):"
319 " NULL char in structure." << endl;
325 // now write the next paragraph
327 next_->writeFile(buf, os, bparams, dth);
331 void Paragraph::validate(LaTeXFeatures & features) const
333 BufferParams const & bparams = features.bufferParams();
336 if (params().lineTop() || params().lineBottom())
337 features.require("lyxline");
338 if (!params().spacing().isDefault())
339 features.require("setspace");
342 features.useLayout(getLayout());
345 Language const * doc_language = bparams.language;
347 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
348 cit != pimpl_->fontlist.end(); ++cit) {
349 if (cit->font().noun() == LyXFont::ON) {
350 lyxerr[Debug::LATEX] << "font.noun: "
351 << cit->font().noun()
353 features.require("noun");
354 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
355 << cit->font().stateText(0)
358 switch (cit->font().color()) {
360 case LColor::inherit:
362 // probably we should put here all interface colors used for
363 // font displaying! For now I just add this ones I know of (Jug)
368 features.require("color");
369 lyxerr[Debug::LATEX] << "Color enabled. Font: "
370 << cit->font().stateText(0)
374 Language const * language = cit->font().language();
375 if (language->babel() != doc_language->babel() &&
376 language != ignore_language &&
377 #ifdef INHERIT_LANGUAGE
378 language != inherit_language &&
380 language != latex_language)
382 features.useLanguage(language);
383 lyxerr[Debug::LATEX] << "Found language "
384 << language->babel() << endl;
389 LyXLayout const & layout =
390 textclasslist.Style(bparams.textclass, getLayout());
392 for (InsetList::const_iterator cit = insetlist.begin();
393 cit != insetlist.end(); ++cit) {
395 cit->inset->validate(features);
396 if (layout.needprotect &&
397 cit->inset->lyxCode() == Inset::FOOT_CODE)
398 features.require("NeedLyXFootnoteCode");
404 // First few functions needed for cut and paste and paragraph breaking.
405 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
407 BufferParams bparams = buffer.params;
409 minibuffer_char = getChar(pos);
410 minibuffer_font = getFontSettings(bparams, pos);
411 minibuffer_inset = 0;
412 if (minibuffer_char == Paragraph::META_INSET) {
414 minibuffer_inset = getInset(pos)->clone(buffer);
416 minibuffer_inset = 0;
417 minibuffer_char = ' ';
418 // This reflects what GetInset() does (ARRae)
424 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
426 minibuffer_char = getChar(pos);
427 minibuffer_font = getFontSettings(bparams, pos);
428 minibuffer_inset = 0;
429 if (minibuffer_char == Paragraph::META_INSET) {
431 minibuffer_inset = getInset(pos);
432 // This is a little hack since I want exactly
433 // the inset, not just a clone. Otherwise
434 // the inset would be deleted when calling Erase(pos)
436 InsetTable search_elem(pos, 0);
437 InsetList::iterator it =
438 lower_bound(insetlist.begin(),
440 search_elem, Pimpl::matchIT());
441 if (it != insetlist.end() && it->pos == pos)
443 // the inset is not in a paragraph anymore
444 minibuffer_inset->parOwner(0);
446 minibuffer_inset = 0;
447 minibuffer_char = ' ';
448 // This reflects what GetInset() does (ARRae)
453 // Erase(pos); now the caller is responsible for that.
457 bool Paragraph::insertFromMinibuffer(pos_type pos)
459 if (minibuffer_char == Paragraph::META_INSET) {
460 if (!insetAllowed(minibuffer_inset->lyxCode())) {
463 insertInset(pos, minibuffer_inset, minibuffer_font);
465 LyXFont f = minibuffer_font;
466 if (!checkInsertChar(f)) {
469 insertChar(pos, minibuffer_char, f);
478 void Paragraph::clear()
487 void Paragraph::erase(pos_type pos)
493 bool Paragraph::checkInsertChar(LyXFont & font)
495 if (pimpl_->inset_owner)
496 return pimpl_->inset_owner->checkInsertChar(font);
501 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
503 LyXFont const f(LyXFont::ALL_INHERIT);
504 insertChar(pos, c, f);
508 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
509 LyXFont const & font)
511 pimpl_->insertChar(pos, c, font);
515 void Paragraph::insertInset(pos_type pos, Inset * inset)
517 LyXFont const f(LyXFont::ALL_INHERIT);
518 insertInset(pos, inset, f);
522 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
524 pimpl_->insertInset(pos, inset, font);
528 bool Paragraph::insetAllowed(Inset::Code code)
530 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
532 if (pimpl_->inset_owner)
533 return pimpl_->inset_owner->insetAllowed(code);
538 Inset * Paragraph::getInset(pos_type pos)
540 lyx::Assert(pos < size());
543 InsetTable search_inset(pos, 0);
544 InsetList::iterator it = lower_bound(insetlist.begin(),
546 search_inset, Pimpl::matchIT());
547 if (it != insetlist.end() && it->pos == pos)
550 lyxerr << "ERROR (Paragraph::getInset): "
551 "Inset does not exist: " << pos << endl;
554 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
555 // Did this commenting out introduce a bug? So far I have not
556 // see any, please enlighten me. (Lgb)
557 // My guess is that since the inset does not exist, we might
558 // as well replace it with a space to prevent craches. (Asger)
563 Inset const * Paragraph::getInset(pos_type pos) const
565 lyx::Assert(pos < size());
568 InsetTable search_inset(pos, 0);
569 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
571 search_inset, Pimpl::matchIT());
572 if (cit != insetlist.end() && cit->pos == pos)
575 lyxerr << "ERROR (Paragraph::GetInset): "
576 "Inset does not exist: " << pos << endl;
578 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
579 // Did this commenting out introduce a bug? So far I have not
580 // see any, please enlighten me. (Lgb)
581 // My guess is that since the inset does not exist, we might
582 // as well replace it with a space to prevent craches. (Asger)
587 // Gets uninstantiated font setting at position.
588 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
591 lyx::Assert(pos <= size());
593 Pimpl::FontTable search_font(pos, LyXFont());
594 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
595 pimpl_->fontlist.end(),
596 search_font, Pimpl::matchFT());
598 if (cit != pimpl_->fontlist.end()) {
599 retfont = cit->font();
600 } else if (pos == size() && size()) {
601 retfont = getFontSettings(bparams, pos - 1);
603 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
604 #ifdef INHERIT_LANGUAGE
605 if (retfont.language() == inherit_language)
606 retfont.setLanguage(bparams.language);
613 // Gets uninstantiated font setting at position 0
614 LyXFont const Paragraph::getFirstFontSettings() const
616 if (size() > 0 && !pimpl_->fontlist.empty())
617 return pimpl_->fontlist[0].font();
619 return LyXFont(LyXFont::ALL_INHERIT);
623 // Gets the fully instantiated font at a given position in a paragraph
624 // This is basically the same function as LyXText::GetFont() in text2.C.
625 // The difference is that this one is used for generating the LaTeX file,
626 // and thus cosmetic "improvements" are disallowed: This has to deliver
627 // the true picture of the buffer. (Asger)
628 // If position is -1, we get the layout font of the paragraph.
629 // If position is -2, we get the font of the manual label of the paragraph.
630 LyXFont const Paragraph::getFont(BufferParams const & bparams,
633 lyx::Assert(pos >= 0);
635 LyXLayout const & layout =
636 textclasslist.Style(bparams.textclass,
638 pos_type main_body = 0;
639 if (layout.labeltype == LABEL_MANUAL)
640 main_body = beginningOfMainBody();
644 layoutfont = layout.labelfont;
646 layoutfont = layout.font;
648 LyXFont tmpfont = getFontSettings(bparams, pos);
649 #ifndef INHERIT_LANGUAGE
650 tmpfont.realize(layoutfont);
652 tmpfont.realize(layoutfont, bparams.language);
655 return pimpl_->realizeFont(tmpfont, bparams);
659 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
661 LyXLayout const & layout =
662 textclasslist.Style(bparams.textclass, getLayout());
664 LyXFont tmpfont = layout.labelfont;
665 tmpfont.setLanguage(getParLanguage(bparams));
667 return pimpl_->realizeFont(tmpfont, bparams);
671 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
673 LyXLayout const & layout =
674 textclasslist.Style(bparams.textclass,
677 LyXFont tmpfont = layout.font;
678 tmpfont.setLanguage(getParLanguage(bparams));
680 return pimpl_->realizeFont(tmpfont, bparams);
684 /// Returns the height of the highest font in range
686 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
687 LyXFont::FONT_SIZE const def_size) const
689 if (pimpl_->fontlist.empty())
692 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
693 Pimpl::FontTable end_search(endpos, LyXFont());
694 Pimpl::FontList::const_iterator end_it =
695 lower_bound(pimpl_->fontlist.begin(),
696 pimpl_->fontlist.end(),
697 end_search, Pimpl::matchFT());
698 if (end_it != pimpl_->fontlist.end())
701 Pimpl::FontTable start_search(startpos, LyXFont());
702 Pimpl::FontList::const_iterator cit =
703 lower_bound(pimpl_->fontlist.begin(),
704 pimpl_->fontlist.end(),
705 start_search, Pimpl::matchFT());
707 for (; cit != end_it; ++cit) {
708 LyXFont::FONT_SIZE size = cit->font().size();
709 if (size == LyXFont::INHERIT_SIZE)
711 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
718 Paragraph::value_type
719 Paragraph::getUChar(BufferParams const & bparams,
722 value_type c = getChar(pos);
723 if (!lyxrc.rtl_support)
753 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
760 void Paragraph::setFont(pos_type pos,
761 LyXFont const & font)
763 lyx::Assert(pos <= size());
765 // First, reduce font against layout/label font
766 // Update: The SetCharFont() routine in text2.C already
767 // reduces font, so we don't need to do that here. (Asger)
768 // No need to simplify this because it will disappear
769 // in a new kernel. (Asger)
770 // Next search font table
772 Pimpl::FontTable search_font(pos, LyXFont());
773 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
774 pimpl_->fontlist.end(),
775 search_font, Pimpl::matchFT());
776 unsigned int i = it - pimpl_->fontlist.begin();
777 bool notfound = it == pimpl_->fontlist.end();
779 if (!notfound && pimpl_->fontlist[i].font() == font)
782 bool begin = pos == 0 || notfound ||
783 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
784 // Is position pos is a beginning of a font block?
785 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
786 // Is position pos is the end of a font block?
787 if (begin && end) { // A single char block
788 if (i + 1 < pimpl_->fontlist.size() &&
789 pimpl_->fontlist[i + 1].font() == font) {
790 // Merge the singleton block with the next block
791 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
792 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
793 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
794 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
795 // Merge the singleton block with the previous block
796 pimpl_->fontlist[i - 1].pos(pos);
797 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
799 pimpl_->fontlist[i].font(font);
801 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
802 pimpl_->fontlist[i - 1].pos(pos);
804 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
805 Pimpl::FontTable(pos, font));
807 pimpl_->fontlist[i].pos(pos - 1);
808 if (!(i + 1 < pimpl_->fontlist.size() &&
809 pimpl_->fontlist[i + 1].font() == font))
810 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
811 Pimpl::FontTable(pos, font));
812 } else { // The general case. The block is splitted into 3 blocks
813 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
814 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
815 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
816 Pimpl::FontTable(pos, font));
822 void Paragraph::next(Paragraph * p)
828 // This function is able to hide closed footnotes.
829 Paragraph * Paragraph::next()
835 Paragraph const * Paragraph::next() const
841 void Paragraph::previous(Paragraph * p)
847 // This function is able to hide closed footnotes.
848 Paragraph * Paragraph::previous()
854 // This function is able to hide closed footnotes.
855 Paragraph const * Paragraph::previous() const
861 void Paragraph::breakParagraph(BufferParams const & bparams,
865 // create a new paragraph
866 Paragraph * tmp = new Paragraph(this);
867 // remember to set the inset_owner
868 tmp->setInsetOwner(inInset());
870 // this is an idea for a more userfriendly layout handling, I will
871 // see what the users say
873 // layout stays the same with latex-environments
875 tmp->setOnlyLayout(layout);
876 tmp->setLabelWidthString(params().labelWidthString());
879 if (size() > pos || !size() || flag == 2) {
880 tmp->setOnlyLayout(layout);
881 tmp->params().align(params().align());
882 tmp->setLabelWidthString(params().labelWidthString());
884 tmp->params().lineBottom(params().lineBottom());
885 params().lineBottom(false);
886 tmp->params().pagebreakBottom(params().pagebreakBottom());
887 params().pagebreakBottom(false);
888 tmp->params().spaceBottom(params().spaceBottom());
889 params().spaceBottom(VSpace(VSpace::NONE));
891 tmp->params().depth(params().depth());
892 tmp->params().noindent(params().noindent());
894 // copy everything behind the break-position
895 // to the new paragraph
896 pos_type pos_end = pimpl_->size() - 1;
899 for (; i <= pos_end; ++i) {
900 cutIntoMinibuffer(bparams, i);
901 if (tmp->insertFromMinibuffer(j - pos))
904 for (i = pos_end; i >= pos; --i) {
909 // just an idea of me
911 tmp->params().lineTop(params().lineTop());
912 tmp->params().pagebreakTop(params().pagebreakTop());
913 tmp->params().spaceTop(params().spaceTop());
914 tmp->bibkey = bibkey;
916 // layout stays the same with latex-environments
918 setOnlyLayout(tmp->layout);
919 setLabelWidthString(tmp->params().labelWidthString());
920 params().depth(tmp->params().depth());
926 void Paragraph::makeSameLayout(Paragraph const * par)
928 layout = par->layout;
930 params() = par->params();
934 int Paragraph::stripLeadingSpaces(lyx::textclass_type tclass)
936 if (textclasslist.Style(tclass, getLayout()).free_spacing ||
943 while (size() && (isNewline(0) || isLineSeparator(0))) {
952 bool Paragraph::hasSameLayout(Paragraph const * par) const
955 par->layout == layout &&
956 params().sameLayout(par->params());
960 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
963 // create a new paragraph
964 Paragraph * tmp = new Paragraph(this);
965 tmp->makeSameLayout(this);
967 // When can pos > Last()?
968 // I guess pos == Last() is possible.
970 // copy everything behind the break-position to the new
972 pos_type pos_end = pimpl_->size() - 1;
976 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
977 cutIntoMinibuffer(bparams, i);
978 if (tmp->insertFromMinibuffer(j - pos))
982 for (pos_type k = pos_end; k >= pos; --k) {
989 // Be carefull, this does not make any check at all.
990 // This method has wrong name, it combined this par with the next par.
991 // In that sense it is the reverse of break paragraph. (Lgb)
992 void Paragraph::pasteParagraph(BufferParams const & bparams)
994 // copy the next paragraph to this one
995 Paragraph * the_next = next();
997 // first the DTP-stuff
998 params().lineBottom(the_next->params().lineBottom());
999 params().spaceBottom(the_next->params().spaceBottom());
1000 params().pagebreakBottom(the_next->params().pagebreakBottom());
1002 pos_type pos_end = the_next->pimpl_->size() - 1;
1003 pos_type pos_insert = size();
1005 // ok, now copy the paragraph
1006 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
1007 the_next->cutIntoMinibuffer(bparams, i);
1008 if (insertFromMinibuffer(pos_insert + j))
1012 // delete the next paragraph
1013 Paragraph * ppar = the_next->previous_;
1014 Paragraph * npar = the_next->next_;
1020 int Paragraph::getEndLabel(BufferParams const & bparams) const
1022 Paragraph const * par = this;
1023 depth_type par_depth = getDepth();
1025 layout_type layout = par->getLayout();
1026 int const endlabeltype =
1027 textclasslist.Style(bparams.textclass,
1028 layout).endlabeltype;
1029 if (endlabeltype != END_LABEL_NO_LABEL) {
1031 return endlabeltype;
1033 depth_type const next_depth = next_->getDepth();
1034 if (par_depth > next_depth ||
1035 (par_depth == next_depth
1036 && layout != next_->getLayout()))
1037 return endlabeltype;
1042 par = par->outerHook();
1044 par_depth = par->getDepth();
1046 return END_LABEL_NO_LABEL;
1050 Paragraph::depth_type Paragraph::getDepth() const
1052 return params().depth();
1056 char Paragraph::getAlign() const
1058 return params().align();
1062 string const & Paragraph::getLabelstring() const
1064 return params().labelString();
1068 int Paragraph::getFirstCounter(int i) const
1070 return pimpl_->counter_[i];
1074 // the next two functions are for the manual labels
1075 string const Paragraph::getLabelWidthString() const
1077 if (!params().labelWidthString().empty())
1078 return params().labelWidthString();
1080 return _("Senseless with this layout!");
1084 void Paragraph::setLabelWidthString(string const & s)
1086 params().labelWidthString(s);
1090 void Paragraph::setOnlyLayout(layout_type new_layout)
1092 layout = new_layout;
1096 void Paragraph::setLayout(layout_type new_layout)
1098 layout = new_layout;
1099 params().labelWidthString(string());
1100 params().align(LYX_ALIGN_LAYOUT);
1101 params().spaceTop(VSpace(VSpace::NONE));
1102 params().spaceBottom(VSpace(VSpace::NONE));
1103 params().spacing(Spacing(Spacing::Default));
1107 // if the layout of a paragraph contains a manual label, the beginning of the
1108 // main body is the beginning of the second word. This is what the par-
1109 // function returns. If the layout does not contain a label, the main
1110 // body always starts with position 0. This differentiation is necessary,
1111 // because there cannot be a newline or a blank <= the beginning of the
1112 // main body in TeX.
1114 int Paragraph::beginningOfMainBody() const
1116 // Unroll the first two cycles of the loop
1117 // and remember the previous character to
1118 // remove unnecessary GetChar() calls
1121 && getChar(i) != Paragraph::META_NEWLINE) {
1123 char previous_char = 0;
1126 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1127 // Yes, this ^ is supposed to be "= " not "=="
1130 && previous_char != ' '
1131 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1133 previous_char = temp;
1142 Paragraph * Paragraph::depthHook(depth_type depth)
1144 Paragraph * newpar = this;
1147 newpar = newpar->previous();
1148 } while (newpar && newpar->getDepth() > depth);
1151 if (previous() || getDepth())
1152 lyxerr << "ERROR (Paragraph::DepthHook): "
1161 Paragraph const * Paragraph::depthHook(depth_type depth) const
1163 Paragraph const * newpar = this;
1166 newpar = newpar->previous();
1167 } while (newpar && newpar->getDepth() > depth);
1170 if (previous() || getDepth())
1171 lyxerr << "ERROR (Paragraph::DepthHook): "
1179 Paragraph * Paragraph::outerHook()
1183 return depthHook(depth_type(getDepth() - 1));
1186 Paragraph const * Paragraph::outerHook() const
1190 return depthHook(depth_type(getDepth() - 1));
1193 int Paragraph::autoDeleteInsets()
1196 InsetList::size_type index = 0;
1197 while (index < insetlist.size()) {
1198 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1199 erase(insetlist[index].pos);
1200 // erase() calls to insetlist.erase(&insetlist[index])
1201 // so index shouldn't be increased.
1210 Paragraph::inset_iterator
1211 Paragraph::InsetIterator(pos_type pos)
1213 InsetTable search_inset(pos, 0);
1214 InsetList::iterator it = lower_bound(insetlist.begin(),
1216 search_inset, Pimpl::matchIT());
1217 return inset_iterator(it);
1221 // returns -1 if inset not found
1222 int Paragraph::getPositionOfInset(Inset const * inset) const
1225 for (InsetList::const_iterator cit = insetlist.begin();
1226 cit != insetlist.end(); ++cit) {
1227 if (cit->inset == inset) {
1231 if (inset == bibkey)
1238 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1239 BufferParams const & bparams,
1240 ostream & os, TexRow & texrow,
1243 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1244 LyXLayout const & style =
1245 textclasslist.Style(bparams.textclass,
1248 bool further_blank_line = false;
1250 if (params().startOfAppendix()) {
1251 os << "\\appendix\n";
1255 if (!params().spacing().isDefault()
1256 && (!previous() || !previous()->hasSameLayout(this))) {
1257 os << params().spacing().writeEnvirBegin() << "\n";
1261 if (tex_code_break_column && style.isCommand()){
1266 if (params().pagebreakTop()) {
1268 further_blank_line = true;
1270 if (params().spaceTop().kind() != VSpace::NONE) {
1271 os << params().spaceTop().asLatexCommand(bparams);
1272 further_blank_line = true;
1275 if (params().lineTop()) {
1276 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1277 << "\\vspace{-1\\parskip}";
1278 further_blank_line = true;
1281 if (further_blank_line){
1286 Language const * language = getParLanguage(bparams);
1287 Language const * doc_language = bparams.language;
1288 Language const * previous_language = previous()
1289 ? previous()->getParLanguage(bparams) : doc_language;
1291 if (language->babel() != previous_language->babel()
1292 // check if we already put language command in TeXEnvironment()
1293 && !(textclasslist.Style(bparams.textclass, layout).isEnvironment()
1294 && (!previous() || previous()->layout != layout ||
1295 previous()->params().depth() != params().depth()))) {
1297 if (!lyxrc.language_command_end.empty() &&
1298 previous_language->babel() != doc_language->babel()) {
1299 os << subst(lyxrc.language_command_end, "$$lang",
1300 previous_language->babel())
1305 if (lyxrc.language_command_end.empty() ||
1306 language->babel() != doc_language->babel()) {
1307 os << subst(lyxrc.language_command_begin, "$$lang",
1314 if (bparams.inputenc == "auto" &&
1315 language->encoding() != previous_language->encoding()) {
1316 os << "\\inputencoding{"
1317 << language->encoding()->LatexName()
1322 switch (style.latextype) {
1325 << style.latexname()
1326 << style.latexparam();
1328 case LATEX_ITEM_ENVIRONMENT:
1330 bibkey->latex(buf, os, false, false);
1334 case LATEX_LIST_ENVIRONMENT:
1341 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1343 // Make sure that \\par is done with the font of the last
1344 // character if this has another size as the default.
1345 // This is necessary because LaTeX (and LyX on the screen)
1346 // calculates the space between the baselines according
1347 // to this font. (Matthias)
1349 // Is this really needed ? (Dekel)
1350 // We do not need to use to change the font for the last paragraph
1351 // or for a command.
1352 LyXFont const font =
1354 ? getLayoutFont(bparams)
1355 : getFont(bparams, size() - 1));
1357 bool is_command = textclasslist.Style(bparams.textclass,
1358 getLayout()).isCommand();
1359 if (style.resfont.size() != font.size() && next_ && !is_command) {
1362 os << "\\" << font.latexSize() << " \\par}";
1363 } else if (need_par) {
1365 } else if (is_command)
1368 switch (style.latextype) {
1369 case LATEX_ITEM_ENVIRONMENT:
1370 case LATEX_LIST_ENVIRONMENT:
1371 if (next_ && (params().depth() < next_->params().depth())) {
1376 case LATEX_ENVIRONMENT:
1377 // if its the last paragraph of the current environment
1378 // skip it otherwise fall through
1380 && (next_->layout != layout
1381 || next_->params().depth() != params().depth()))
1383 // fall through possible
1385 // we don't need it for the last paragraph!!!
1392 further_blank_line = false;
1393 if (params().lineBottom()) {
1394 os << "\\lyxline{\\" << font.latexSize() << '}';
1395 further_blank_line = true;
1398 if (params().spaceBottom().kind() != VSpace::NONE) {
1399 os << params().spaceBottom().asLatexCommand(bparams);
1400 further_blank_line = true;
1403 if (params().pagebreakBottom()) {
1405 further_blank_line = true;
1408 if (further_blank_line){
1413 if (!params().spacing().isDefault()
1414 && (!next_ || !next_->hasSameLayout(this))) {
1415 os << params().spacing().writeEnvirEnd() << "\n";
1419 // we don't need it for the last paragraph!!!
1424 // Since \selectlanguage write the language to the aux file,
1425 // we need to reset the language at the end of footnote or
1428 if (language->babel() != doc_language->babel()) {
1429 if (lyxrc.language_command_end.empty())
1430 os << subst(lyxrc.language_command_begin,
1432 doc_language->babel())
1435 os << subst(lyxrc.language_command_end,
1443 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1448 // This one spits out the text of the paragraph
1449 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1450 BufferParams const & bparams,
1451 ostream & os, TexRow & texrow,
1454 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1456 bool return_value = false;
1458 LyXLayout const & style =
1459 textclasslist.Style(bparams.textclass,
1463 // Maybe we have to create a optional argument.
1465 if (style.labeltype != LABEL_MANUAL)
1468 main_body = beginningOfMainBody();
1472 if (main_body > 0) {
1475 basefont = getLabelFont(bparams);
1477 basefont = getLayoutFont(bparams);
1481 && !pimpl_->size()) {
1482 if (style.isCommand()) {
1488 moving_arg |= style.needprotect;
1490 // Which font is currently active?
1491 LyXFont running_font(basefont);
1492 // Do we have an open font change?
1493 bool open_font = false;
1495 texrow.start(this, 0);
1497 for (pos_type i = 0; i < size(); ++i) {
1499 // First char in paragraph or after label?
1500 if (i == main_body) {
1501 if (main_body > 0) {
1503 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1506 basefont = getLayoutFont(bparams);
1507 running_font = basefont;
1511 if (style.isCommand()) {
1516 if (params().noindent()) {
1517 os << "\\noindent ";
1520 switch (params().align()) {
1521 case LYX_ALIGN_NONE:
1522 case LYX_ALIGN_BLOCK:
1523 case LYX_ALIGN_LAYOUT:
1524 case LYX_ALIGN_SPECIAL:
1526 case LYX_ALIGN_LEFT:
1527 if (getParLanguage(bparams)->babel() != "hebrew") {
1528 os << "\\begin{flushleft}";
1531 os << "\\begin{flushright}";
1535 case LYX_ALIGN_RIGHT:
1536 if (getParLanguage(bparams)->babel() != "hebrew") {
1537 os << "\\begin{flushright}";
1540 os << "\\begin{flushleft}";
1544 case LYX_ALIGN_CENTER:
1545 os << "\\begin{center}";
1551 value_type c = getChar(i);
1553 // Fully instantiated font
1554 LyXFont font = getFont(bparams, i);
1556 LyXFont const last_font = running_font;
1558 // Spaces at end of font change are simulated to be
1559 // outside font change, i.e. we write "\textXX{text} "
1560 // rather than "\textXX{text }". (Asger)
1561 if (open_font && c == ' ' && i <= size() - 2) {
1562 LyXFont const next_font = getFont(bparams, i + 1);
1563 if (next_font != running_font
1564 && next_font != font) {
1569 // We end font definition before blanks
1570 if (font != running_font && open_font) {
1571 column += running_font.latexWriteEndChanges(os,
1573 (i == main_body-1) ? basefont : font);
1574 running_font = basefont;
1578 // Blanks are printed before start of fontswitch
1580 // Do not print the separation of the optional argument
1581 if (i != main_body - 1) {
1582 pimpl_->simpleTeXBlanks(os, texrow, i,
1583 column, font, style);
1587 // Do we need to change font?
1588 if (font != running_font && i != main_body - 1) {
1589 column += font.latexWriteStartChanges(os, basefont,
1591 running_font = font;
1595 if (c == Paragraph::META_NEWLINE) {
1596 // newlines are handled differently here than
1597 // the default in SimpleTeXSpecialChars().
1598 if (!style.newline_allowed) {
1602 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1605 basefont = getLayoutFont(bparams);
1606 running_font = basefont;
1607 if (font.family() ==
1608 LyXFont::TYPEWRITER_FAMILY) {
1616 texrow.start(this, i + 1);
1619 pimpl_->simpleTeXSpecialChars(buf, bparams,
1620 os, texrow, moving_arg,
1622 basefont, open_font,
1623 style, i, column, c);
1627 // If we have an open font definition, we have to close it
1629 #ifdef FIXED_LANGUAGE_END_DETECTION
1632 .latexWriteEndChanges(os, basefont,
1633 next_->getFont(bparams,
1636 running_font.latexWriteEndChanges(os, basefont,
1640 #ifdef WITH_WARNINGS
1641 //#warning For now we ALWAYS have to close the foreign font settings if they are
1642 //#warning there as we start another \selectlanguage with the next paragraph if
1643 //#warning we are in need of this. This should be fixed sometime (Jug)
1645 running_font.latexWriteEndChanges(os, basefont, basefont);
1649 // Needed if there is an optional argument but no contents.
1650 if (main_body > 0 && main_body == size()) {
1652 return_value = false;
1655 switch (params().align()) {
1656 case LYX_ALIGN_NONE:
1657 case LYX_ALIGN_BLOCK:
1658 case LYX_ALIGN_LAYOUT:
1659 case LYX_ALIGN_SPECIAL:
1661 case LYX_ALIGN_LEFT:
1662 if (getParLanguage(bparams)->babel() != "hebrew") {
1663 os << "\\end{flushleft}";
1666 os << "\\end{flushright}";
1670 case LYX_ALIGN_RIGHT:
1671 if (getParLanguage(bparams)->babel() != "hebrew") {
1672 os << "\\end{flushright}";
1675 os << "\\end{flushleft}";
1679 case LYX_ALIGN_CENTER:
1680 os << "\\end{center}";
1685 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1686 return return_value;
1690 bool Paragraph::sgmlConvertChar(char c, string & sgml_string)
1692 bool retval = false;
1694 case Paragraph::META_HFILL:
1695 sgml_string.erase();
1697 case Paragraph::META_NEWLINE:
1701 sgml_string = "&";
1704 sgml_string = "<";
1707 sgml_string = ">";
1710 sgml_string = "$";
1713 sgml_string = "#";
1716 sgml_string = "%";
1719 sgml_string = "[";
1722 sgml_string = "]";
1725 sgml_string = "{";
1728 sgml_string = "}";
1731 sgml_string = "˜";
1734 sgml_string = """;
1737 sgml_string = "\";
1743 case '\0': // Ignore :-)
1744 sgml_string.erase();
1754 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1755 BufferParams const & bparams,
1756 ostream & os, TexRow & texrow)
1758 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1760 LyXLayout const & style =
1761 textclasslist.Style(bparams.textclass,
1764 Language const * language = getParLanguage(bparams);
1765 Language const * doc_language = bparams.language;
1766 Language const * previous_language = previous_
1767 ? previous_->getParLanguage(bparams) : doc_language;
1768 if (language->babel() != previous_language->babel()) {
1770 if (!lyxrc.language_command_end.empty() &&
1771 previous_language->babel() != doc_language->babel()) {
1772 os << subst(lyxrc.language_command_end, "$$lang",
1773 previous_language->babel())
1778 if (lyxrc.language_command_end.empty() ||
1779 language->babel() != doc_language->babel()) {
1780 os << subst(lyxrc.language_command_begin, "$$lang",
1787 if (style.isEnvironment()) {
1788 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1789 os << "\\begin{" << style.latexname() << "}{"
1790 << params().labelWidthString() << "}\n";
1791 } else if (style.labeltype == LABEL_BIBLIO) {
1793 os << "\\begin{" << style.latexname() << "}{"
1794 << bibitemWidest(buf)
1796 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1797 os << "\\begin{" << style.latexname() << '}'
1798 << style.latexparam() << '\n';
1800 os << "\\begin{" << style.latexname() << '}'
1801 << style.latexparam() << '\n';
1804 Paragraph * par = this;
1806 par = par->TeXOnePar(buf, bparams,
1809 if (par && par->params().depth() > params().depth()) {
1810 if (textclasslist.Style(bparams.textclass,
1811 par->layout).isParagraph()
1813 // How to handle this? (Lgb)
1814 //&& !suffixIs(os, "\n\n")
1816 // There should be at least one '\n' already
1817 // but we need there to be two for Standard
1818 // paragraphs that are depth-increment'ed to be
1819 // output correctly. However, tables can
1820 // also be paragraphs so don't adjust them.
1823 // Will it ever harm to have one '\n' too
1824 // many? i.e. that we sometimes will have
1825 // three in a row. (Lgb)
1829 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1832 && par->layout == layout
1833 && par->params().depth() == params().depth());
1835 if (style.isEnvironment()) {
1836 os << "\\end{" << style.latexname() << "}\n";
1840 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1841 return par; // ale970302
1845 bool Paragraph::isHfill(pos_type pos) const
1847 return IsHfillChar(getChar(pos));
1851 bool Paragraph::isInset(pos_type pos) const
1853 return IsInsetChar(getChar(pos));
1857 bool Paragraph::isNewline(pos_type pos) const
1859 return pos >= 0 && IsNewlineChar(getChar(pos));
1863 bool Paragraph::isSeparator(pos_type pos) const
1865 return IsSeparatorChar(getChar(pos));
1869 bool Paragraph::isLineSeparator(pos_type pos) const
1871 return IsLineSeparatorChar(getChar(pos));
1875 bool Paragraph::isKomma(pos_type pos) const
1877 return IsKommaChar(getChar(pos));
1881 /// Used by the spellchecker
1882 bool Paragraph::isLetter(pos_type pos) const
1884 value_type const c = getChar(pos);
1885 if (IsLetterChar(c))
1888 return getInset(pos)->isLetter();
1889 // We want to pass the ' and escape chars to ispell
1890 string const extra = lyxrc.isp_esc_chars + '\'';
1891 return contains(extra, c);
1895 bool Paragraph::isWord(pos_type pos ) const
1897 return IsWordChar(getChar(pos)) ;
1902 Paragraph::getParLanguage(BufferParams const & bparams) const
1905 #ifndef INHERIT_LANGUAGE
1906 return getFirstFontSettings().language();
1908 Language const * lang = getFirstFontSettings().language();
1909 #warning We should make this somewhat better, any ideas? (Jug)
1910 if (lang == inherit_language || lang == ignore_language)
1911 lang = bparams.language;
1914 } else if (previous_)
1915 return previous_->getParLanguage(bparams);
1917 return bparams.language;
1921 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1923 return lyxrc.rtl_support
1924 && getParLanguage(bparams)->RightToLeft();
1928 void Paragraph::changeLanguage(BufferParams const & bparams,
1929 Language const * from, Language const * to)
1931 for (pos_type i = 0; i < size(); ++i) {
1932 LyXFont font = getFontSettings(bparams, i);
1933 if (font.language() == from) {
1934 font.setLanguage(to);
1941 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1943 Language const * doc_language = bparams.language;
1944 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1945 cit != pimpl_->fontlist.end(); ++cit)
1946 if (cit->font().language() != ignore_language &&
1947 cit->font().language() != latex_language &&
1948 #ifdef INHERIT_LANGUAGE
1949 cit->font().language() != inherit_language &&
1951 cit->font().language() != doc_language)
1957 // Convert the paragraph to a string.
1958 // Used for building the table of contents
1959 string const Paragraph::asString(Buffer const * buffer, bool label)
1961 BufferParams const & bparams = buffer->params;
1963 if (label && !params().labelString().empty())
1964 s += params().labelString() + ' ';
1965 string::size_type const len = s.size();
1967 for (pos_type i = 0; i < size(); ++i) {
1968 value_type c = getChar(i);
1971 else if (c == META_INSET &&
1972 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1974 getInset(i)->ascii(buffer, ost);
1975 s += subst(ost.str().c_str(),'\n',' ');
1979 if (isRightToLeftPar(bparams))
1980 reverse(s.begin() + len,s.end());
1986 string const Paragraph::asString(Buffer const * buffer,
1987 pos_type beg, pos_type end, bool label)
1991 if (beg == 0 && label && !params().labelString().empty())
1992 ost << params().labelString() << ' ';
1994 for (pos_type i = beg; i < end; ++i) {
1995 value_type const c = getUChar(buffer->params, i);
1998 else if (c == META_NEWLINE)
2000 else if (c == META_HFILL)
2002 else if (c == META_INSET) {
2003 getInset(i)->ascii(buffer, ost);
2007 return ost.str().c_str();
2011 void Paragraph::setInsetOwner(Inset * i)
2013 pimpl_->inset_owner = i;
2014 for (InsetList::const_iterator cit = insetlist.begin();
2015 cit != insetlist.end(); ++cit) {
2017 cit->inset->setOwner(i);
2022 void Paragraph::deleteInsetsLyXText(BufferView * bv)
2025 for (InsetList::const_iterator cit = insetlist.begin();
2026 cit != insetlist.end(); ++cit) {
2028 if (cit->inset->isTextInset()) {
2029 static_cast<UpdatableInset *>
2030 (cit->inset)->deleteLyXText(bv, true);
2037 void Paragraph::resizeInsetsLyXText(BufferView * bv)
2040 for (InsetList::const_iterator cit = insetlist.begin();
2041 cit != insetlist.end(); ++cit)
2044 if (cit->inset->isTextInset()) {
2045 static_cast<UpdatableInset *>
2046 (cit->inset)->resizeLyXText(bv, true);
2053 void Paragraph::setContentsFromPar(Paragraph * par)
2055 pimpl_->setContentsFromPar(par);
2059 lyx::pos_type Paragraph::size() const
2061 return pimpl_->size();
2065 Paragraph::value_type Paragraph::getChar(pos_type pos) const
2067 return pimpl_->getChar(pos);
2071 int Paragraph::id() const
2077 void Paragraph::id(int id_arg)
2079 pimpl_->id_ = id_arg;
2083 layout_type Paragraph::getLayout() const
2089 bool Paragraph::isFirstInSequence() const
2091 Paragraph const * dhook = depthHook(getDepth());
2092 return (dhook == this
2093 || dhook->getLayout() != getLayout()
2094 || dhook->getDepth() != getDepth());
2098 Inset * Paragraph::inInset() const
2100 return pimpl_->inset_owner;
2104 void Paragraph::clearContents()
2110 void Paragraph::setCounter(int i, int v)
2112 pimpl_->counter_[i] = v;
2116 int Paragraph::getCounter(int i) const
2118 return pimpl_->counter_[i];
2122 void Paragraph::incCounter(int i)
2124 pimpl_->counter_[i]++;
2128 void Paragraph::setChar(pos_type pos, value_type c)
2130 pimpl_->setChar(pos, c);
2134 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2139 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2141 return inset_iterator(insetlist.begin());
2145 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2147 return inset_iterator(insetlist.end());
2151 ParagraphParameters & Paragraph::params()
2153 return pimpl_->params;
2157 ParagraphParameters const & Paragraph::params() const
2159 return pimpl_->params;
2163 Paragraph * Paragraph::getParFromID(int id) const
2165 return pimpl_->getParFromID(id);
2169 bool Paragraph::isFreeSpacing() const
2171 // for now we just need this, later should we need this in some
2172 // other way we can always add a function to Inset::() too.
2173 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2174 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);