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 construktor 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);
147 // the destructor removes the new paragraph from the list
148 Paragraph::~Paragraph()
151 previous_->next_ = next_;
153 next_->previous_ = previous_;
155 for (InsetList::iterator it = insetlist.begin();
156 it != insetlist.end(); ++it) {
165 //lyxerr << "Paragraph::paragraph_id = "
166 // << Paragraph::paragraph_id << endl;
170 void Paragraph::writeFile(Buffer const * buf, ostream & os,
171 BufferParams const & bparams,
172 depth_type dth) const
174 // The beginning or end of a deeper (i.e. nested) area?
175 if (dth != params().depth()) {
176 if (params().depth() > dth) {
177 while (params().depth() > dth) {
178 os << "\n\\begin_deeper ";
182 while (params().depth() < dth) {
183 os << "\n\\end_deeper ";
189 // First write the layout
191 << textclasslist.NameOfLayout(bparams.textclass, layout)
194 // Maybe some vertical spaces.
195 if (params().spaceTop().kind() != VSpace::NONE)
196 os << "\\added_space_top "
197 << params().spaceTop().asLyXCommand() << " ";
198 if (params().spaceBottom().kind() != VSpace::NONE)
199 os << "\\added_space_bottom "
200 << params().spaceBottom().asLyXCommand() << " ";
202 // Maybe the paragraph has special spacing
203 params().spacing().writeFile(os, true);
205 // The labelwidth string used in lists.
206 if (!params().labelWidthString().empty())
207 os << "\\labelwidthstring "
208 << params().labelWidthString() << '\n';
210 // Lines above or below?
211 if (params().lineTop())
213 if (params().lineBottom())
214 os << "\\line_bottom ";
216 // Pagebreaks above or below?
217 if (params().pagebreakTop())
218 os << "\\pagebreak_top ";
219 if (params().pagebreakBottom())
220 os << "\\pagebreak_bottom ";
222 // Start of appendix?
223 if (params().startOfAppendix())
224 os << "\\start_of_appendix ";
227 if (params().noindent())
231 if (params().align() != LYX_ALIGN_LAYOUT) {
233 switch (params().align()) {
234 case LYX_ALIGN_LEFT: h = 1; break;
235 case LYX_ALIGN_RIGHT: h = 2; break;
236 case LYX_ALIGN_CENTER: h = 3; break;
237 default: h = 0; break;
239 os << "\\align " << string_align[h] << " ";
244 bibkey->write(buf, os);
246 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
249 for (pos_type i = 0; i < size(); ++i) {
255 // Write font changes
256 LyXFont font2 = getFontSettings(bparams, i);
257 if (font2 != font1) {
258 #ifndef INHERIT_LANGUAGE
259 font2.lyxWriteChanges(font1, os);
261 font2.lyxWriteChanges(font1, bparams.language, os);
267 value_type const c = getChar(i);
271 Inset const * inset = getInset(i);
273 if (inset->directWrite()) {
274 // international char, let it write
275 // code directly so it's shorter in
277 inset->write(buf, os);
279 os << "\n\\begin_inset ";
280 inset->write(buf, os);
281 os << "\n\\end_inset \n\n";
287 os << "\n\\newline \n";
291 os << "\n\\hfill \n";
295 os << "\n\\backslash \n";
299 if (i + 1 < size() && getChar(i + 1) == ' ') {
306 if ((column > 70 && c == ' ')
311 // this check is to amend a bug. LyX sometimes
312 // inserts '\0' this could cause problems.
316 lyxerr << "ERROR (Paragraph::writeFile):"
317 " NULL char in structure." << endl;
323 // now write the next paragraph
325 next_->writeFile(buf, os, bparams, dth);
329 void Paragraph::validate(LaTeXFeatures & features) const
331 BufferParams const & bparams = features.bufferParams();
334 if (params().lineTop() || params().lineBottom())
335 features.require("lyxline");
336 if (!params().spacing().isDefault())
337 features.require("setspace");
340 features.useLayout(getLayout());
343 Language const * doc_language = bparams.language;
345 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
346 cit != pimpl_->fontlist.end(); ++cit) {
347 if (cit->font().noun() == LyXFont::ON) {
348 lyxerr[Debug::LATEX] << "font.noun: "
349 << cit->font().noun()
351 features.require("noun");
352 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
353 << cit->font().stateText(0)
356 switch (cit->font().color()) {
358 case LColor::inherit:
360 // probably we should put here all interface colors used for
361 // font displaying! For now I just add this ones I know of (Jug)
366 features.require("color");
367 lyxerr[Debug::LATEX] << "Color enabled. Font: "
368 << cit->font().stateText(0)
372 Language const * language = cit->font().language();
373 if (language->babel() != doc_language->babel() &&
374 language != ignore_language &&
375 #ifdef INHERIT_LANGUAGE
376 language != inherit_language &&
378 language != latex_language)
380 features.useLanguage(language);
381 lyxerr[Debug::LATEX] << "Found language "
382 << language->babel() << endl;
387 LyXLayout const & layout =
388 textclasslist.Style(bparams.textclass, getLayout());
390 for (InsetList::const_iterator cit = insetlist.begin();
391 cit != insetlist.end(); ++cit) {
393 cit->inset->validate(features);
394 if (layout.needprotect &&
395 cit->inset->lyxCode() == Inset::FOOT_CODE)
396 features.require("NeedLyXFootnoteCode");
402 // First few functions needed for cut and paste and paragraph breaking.
403 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
405 BufferParams bparams = buffer.params;
407 minibuffer_char = getChar(pos);
408 minibuffer_font = getFontSettings(bparams, pos);
409 minibuffer_inset = 0;
410 if (minibuffer_char == Paragraph::META_INSET) {
412 minibuffer_inset = getInset(pos)->clone(buffer);
414 minibuffer_inset = 0;
415 minibuffer_char = ' ';
416 // This reflects what GetInset() does (ARRae)
422 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
424 minibuffer_char = getChar(pos);
425 minibuffer_font = getFontSettings(bparams, pos);
426 minibuffer_inset = 0;
427 if (minibuffer_char == Paragraph::META_INSET) {
429 minibuffer_inset = getInset(pos);
430 // This is a little hack since I want exactly
431 // the inset, not just a clone. Otherwise
432 // the inset would be deleted when calling Erase(pos)
434 InsetTable search_elem(pos, 0);
435 InsetList::iterator it =
436 lower_bound(insetlist.begin(),
438 search_elem, Pimpl::matchIT());
439 if (it != insetlist.end() && it->pos == pos)
442 minibuffer_inset = 0;
443 minibuffer_char = ' ';
444 // This reflects what GetInset() does (ARRae)
449 // Erase(pos); now the caller is responsible for that.
453 bool Paragraph::insertFromMinibuffer(pos_type pos)
455 if (minibuffer_char == Paragraph::META_INSET) {
456 if (!insetAllowed(minibuffer_inset->lyxCode())) {
459 insertInset(pos, minibuffer_inset, minibuffer_font);
461 LyXFont f = minibuffer_font;
462 if (!checkInsertChar(f)) {
465 insertChar(pos, minibuffer_char, f);
474 void Paragraph::clear()
483 void Paragraph::erase(pos_type pos)
489 bool Paragraph::checkInsertChar(LyXFont & font)
491 if (pimpl_->inset_owner)
492 return pimpl_->inset_owner->checkInsertChar(font);
497 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
499 LyXFont const f(LyXFont::ALL_INHERIT);
500 insertChar(pos, c, f);
504 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
505 LyXFont const & font)
507 pimpl_->insertChar(pos, c, font);
511 void Paragraph::insertInset(pos_type pos, Inset * inset)
513 LyXFont const f(LyXFont::ALL_INHERIT);
514 insertInset(pos, inset, f);
518 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
520 pimpl_->insertInset(pos, inset, font);
524 bool Paragraph::insetAllowed(Inset::Code code)
526 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
528 if (pimpl_->inset_owner)
529 return pimpl_->inset_owner->insetAllowed(code);
534 Inset * Paragraph::getInset(pos_type pos)
536 lyx::Assert(pos < size());
539 InsetTable search_inset(pos, 0);
540 InsetList::iterator it = lower_bound(insetlist.begin(),
542 search_inset, Pimpl::matchIT());
543 if (it != insetlist.end() && it->pos == pos)
546 lyxerr << "ERROR (Paragraph::getInset): "
547 "Inset does not exist: " << pos << endl;
550 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
551 // Did this commenting out introduce a bug? So far I have not
552 // see any, please enlighten me. (Lgb)
553 // My guess is that since the inset does not exist, we might
554 // as well replace it with a space to prevent craches. (Asger)
559 Inset const * Paragraph::getInset(pos_type pos) const
561 lyx::Assert(pos < size());
564 InsetTable search_inset(pos, 0);
565 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
567 search_inset, Pimpl::matchIT());
568 if (cit != insetlist.end() && cit->pos == pos)
571 lyxerr << "ERROR (Paragraph::GetInset): "
572 "Inset does not exist: " << pos << endl;
574 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
575 // Did this commenting out introduce a bug? So far I have not
576 // see any, please enlighten me. (Lgb)
577 // My guess is that since the inset does not exist, we might
578 // as well replace it with a space to prevent craches. (Asger)
583 // Gets uninstantiated font setting at position.
584 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
587 lyx::Assert(pos <= size());
589 Pimpl::FontTable search_font(pos, LyXFont());
590 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
591 pimpl_->fontlist.end(),
592 search_font, Pimpl::matchFT());
594 if (cit != pimpl_->fontlist.end()) {
595 retfont = cit->font();
596 } else if (pos == size() && size()) {
597 retfont = getFontSettings(bparams, pos - 1);
599 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
600 #ifdef INHERIT_LANGUAGE
601 if (retfont.language() == inherit_language)
602 retfont.setLanguage(bparams.language);
609 // Gets uninstantiated font setting at position 0
610 LyXFont const Paragraph::getFirstFontSettings() const
612 if (size() > 0 && !pimpl_->fontlist.empty())
613 return pimpl_->fontlist[0].font();
615 return LyXFont(LyXFont::ALL_INHERIT);
619 // Gets the fully instantiated font at a given position in a paragraph
620 // This is basically the same function as LyXText::GetFont() in text2.C.
621 // The difference is that this one is used for generating the LaTeX file,
622 // and thus cosmetic "improvements" are disallowed: This has to deliver
623 // the true picture of the buffer. (Asger)
624 // If position is -1, we get the layout font of the paragraph.
625 // If position is -2, we get the font of the manual label of the paragraph.
626 LyXFont const Paragraph::getFont(BufferParams const & bparams,
629 lyx::Assert(pos >= 0);
631 LyXLayout const & layout =
632 textclasslist.Style(bparams.textclass,
634 pos_type main_body = 0;
635 if (layout.labeltype == LABEL_MANUAL)
636 main_body = beginningOfMainBody();
640 layoutfont = layout.labelfont;
642 layoutfont = layout.font;
644 LyXFont tmpfont = getFontSettings(bparams, pos);
645 #ifndef INHERIT_LANGUAGE
646 tmpfont.realize(layoutfont);
648 tmpfont.realize(layoutfont, bparams.language);
651 return pimpl_->realizeFont(tmpfont, bparams);
655 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
657 LyXLayout const & layout =
658 textclasslist.Style(bparams.textclass, getLayout());
660 LyXFont tmpfont = layout.labelfont;
661 tmpfont.setLanguage(getParLanguage(bparams));
663 return pimpl_->realizeFont(tmpfont, bparams);
667 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
669 LyXLayout const & layout =
670 textclasslist.Style(bparams.textclass,
673 LyXFont tmpfont = layout.font;
674 tmpfont.setLanguage(getParLanguage(bparams));
676 return pimpl_->realizeFont(tmpfont, bparams);
680 /// Returns the height of the highest font in range
682 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
683 LyXFont::FONT_SIZE const def_size) const
685 if (pimpl_->fontlist.empty())
688 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
689 Pimpl::FontTable end_search(endpos, LyXFont());
690 Pimpl::FontList::const_iterator end_it =
691 lower_bound(pimpl_->fontlist.begin(),
692 pimpl_->fontlist.end(),
693 end_search, Pimpl::matchFT());
694 if (end_it != pimpl_->fontlist.end())
697 Pimpl::FontTable start_search(startpos, LyXFont());
698 Pimpl::FontList::const_iterator cit =
699 lower_bound(pimpl_->fontlist.begin(),
700 pimpl_->fontlist.end(),
701 start_search, Pimpl::matchFT());
703 for (; cit != end_it; ++cit) {
704 LyXFont::FONT_SIZE size = cit->font().size();
705 if (size == LyXFont::INHERIT_SIZE)
707 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
714 Paragraph::value_type
715 Paragraph::getUChar(BufferParams const & bparams,
718 value_type c = getChar(pos);
719 if (!lyxrc.rtl_support)
749 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
756 void Paragraph::setFont(pos_type pos,
757 LyXFont const & font)
759 lyx::Assert(pos <= size());
761 // First, reduce font against layout/label font
762 // Update: The SetCharFont() routine in text2.C already
763 // reduces font, so we don't need to do that here. (Asger)
764 // No need to simplify this because it will disappear
765 // in a new kernel. (Asger)
766 // Next search font table
768 Pimpl::FontTable search_font(pos, LyXFont());
769 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
770 pimpl_->fontlist.end(),
771 search_font, Pimpl::matchFT());
772 unsigned int i = it - pimpl_->fontlist.begin();
773 bool notfound = it == pimpl_->fontlist.end();
775 if (!notfound && pimpl_->fontlist[i].font() == font)
778 bool begin = pos == 0 || notfound ||
779 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
780 // Is position pos is a beginning of a font block?
781 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
782 // Is position pos is the end of a font block?
783 if (begin && end) { // A single char block
784 if (i + 1 < pimpl_->fontlist.size() &&
785 pimpl_->fontlist[i + 1].font() == font) {
786 // Merge the singleton block with the next block
787 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
788 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
789 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
790 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
791 // Merge the singleton block with the previous block
792 pimpl_->fontlist[i - 1].pos(pos);
793 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
795 pimpl_->fontlist[i].font(font);
797 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
798 pimpl_->fontlist[i - 1].pos(pos);
800 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
801 Pimpl::FontTable(pos, font));
803 pimpl_->fontlist[i].pos(pos - 1);
804 if (!(i + 1 < pimpl_->fontlist.size() &&
805 pimpl_->fontlist[i + 1].font() == font))
806 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
807 Pimpl::FontTable(pos, font));
808 } else { // The general case. The block is splitted into 3 blocks
809 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
810 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
811 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
812 Pimpl::FontTable(pos, font));
818 void Paragraph::next(Paragraph * p)
824 // This function is able to hide closed footnotes.
825 Paragraph * Paragraph::next()
831 Paragraph const * Paragraph::next() const
837 void Paragraph::previous(Paragraph * p)
843 // This function is able to hide closed footnotes.
844 Paragraph * Paragraph::previous()
850 // This function is able to hide closed footnotes.
851 Paragraph const * Paragraph::previous() const
857 void Paragraph::breakParagraph(BufferParams const & bparams,
861 // create a new paragraph
862 Paragraph * tmp = new Paragraph(this);
863 // remember to set the inset_owner
864 tmp->setInsetOwner(inInset());
866 // this is an idea for a more userfriendly layout handling, I will
867 // see what the users say
869 // layout stays the same with latex-environments
871 tmp->setOnlyLayout(layout);
872 tmp->setLabelWidthString(params().labelWidthString());
875 if (size() > pos || !size() || flag == 2) {
876 tmp->setOnlyLayout(layout);
877 tmp->params().align(params().align());
878 tmp->setLabelWidthString(params().labelWidthString());
880 tmp->params().lineBottom(params().lineBottom());
881 params().lineBottom(false);
882 tmp->params().pagebreakBottom(params().pagebreakBottom());
883 params().pagebreakBottom(false);
884 tmp->params().spaceBottom(params().spaceBottom());
885 params().spaceBottom(VSpace(VSpace::NONE));
887 tmp->params().depth(params().depth());
888 tmp->params().noindent(params().noindent());
890 // copy everything behind the break-position
891 // to the new paragraph
892 pos_type pos_end = pimpl_->size() - 1;
895 for (; i <= pos_end; ++i) {
896 cutIntoMinibuffer(bparams, i);
897 if (tmp->insertFromMinibuffer(j - pos))
900 for (i = pos_end; i >= pos; --i) {
905 // just an idea of me
907 tmp->params().lineTop(params().lineTop());
908 tmp->params().pagebreakTop(params().pagebreakTop());
909 tmp->params().spaceTop(params().spaceTop());
910 tmp->bibkey = bibkey;
912 // layout stays the same with latex-environments
914 setOnlyLayout(tmp->layout);
915 setLabelWidthString(tmp->params().labelWidthString());
916 params().depth(tmp->params().depth());
922 void Paragraph::makeSameLayout(Paragraph const * par)
924 layout = par->layout;
926 params() = par->params();
930 int Paragraph::stripLeadingSpaces(lyx::textclass_type tclass)
932 if (textclasslist.Style(tclass, getLayout()).free_spacing ||
939 while (size() && (isNewline(0) || isLineSeparator(0))) {
948 bool Paragraph::hasSameLayout(Paragraph const * par) const
951 par->layout == layout &&
952 params().sameLayout(par->params());
956 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
959 // create a new paragraph
960 Paragraph * tmp = new Paragraph(this);
961 tmp->makeSameLayout(this);
963 // When can pos > Last()?
964 // I guess pos == Last() is possible.
966 // copy everything behind the break-position to the new
968 pos_type pos_end = pimpl_->size() - 1;
972 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
973 cutIntoMinibuffer(bparams, i);
974 if (tmp->insertFromMinibuffer(j - pos))
978 for (pos_type k = pos_end; k >= pos; --k) {
985 // Be carefull, this does not make any check at all.
986 // This method has wrong name, it combined this par with the next par.
987 // In that sense it is the reverse of break paragraph. (Lgb)
988 void Paragraph::pasteParagraph(BufferParams const & bparams)
990 // copy the next paragraph to this one
991 Paragraph * the_next = next();
993 // first the DTP-stuff
994 params().lineBottom(the_next->params().lineBottom());
995 params().spaceBottom(the_next->params().spaceBottom());
996 params().pagebreakBottom(the_next->params().pagebreakBottom());
998 pos_type pos_end = the_next->pimpl_->size() - 1;
999 pos_type pos_insert = size();
1001 // ok, now copy the paragraph
1002 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
1003 the_next->cutIntoMinibuffer(bparams, i);
1004 if (insertFromMinibuffer(pos_insert + j))
1008 // delete the next paragraph
1009 Paragraph * ppar = the_next->previous_;
1010 Paragraph * npar = the_next->next_;
1016 int Paragraph::getEndLabel(BufferParams const & bparams) const
1018 Paragraph const * par = this;
1019 depth_type par_depth = getDepth();
1021 layout_type layout = par->getLayout();
1022 int const endlabeltype =
1023 textclasslist.Style(bparams.textclass,
1024 layout).endlabeltype;
1025 if (endlabeltype != END_LABEL_NO_LABEL) {
1027 return endlabeltype;
1029 depth_type const next_depth = next_->getDepth();
1030 if (par_depth > next_depth ||
1031 (par_depth == next_depth
1032 && layout != next_->getLayout()))
1033 return endlabeltype;
1038 par = par->outerHook();
1040 par_depth = par->getDepth();
1042 return END_LABEL_NO_LABEL;
1046 Paragraph::depth_type Paragraph::getDepth() const
1048 return params().depth();
1052 char Paragraph::getAlign() const
1054 return params().align();
1058 string const & Paragraph::getLabelstring() const
1060 return params().labelString();
1064 int Paragraph::getFirstCounter(int i) const
1066 return pimpl_->counter_[i];
1070 // the next two functions are for the manual labels
1071 string const Paragraph::getLabelWidthString() const
1073 if (!params().labelWidthString().empty())
1074 return params().labelWidthString();
1076 return _("Senseless with this layout!");
1080 void Paragraph::setLabelWidthString(string const & s)
1082 params().labelWidthString(s);
1086 void Paragraph::setOnlyLayout(layout_type new_layout)
1088 layout = new_layout;
1092 void Paragraph::setLayout(layout_type new_layout)
1094 layout = new_layout;
1095 params().labelWidthString(string());
1096 params().align(LYX_ALIGN_LAYOUT);
1097 params().spaceTop(VSpace(VSpace::NONE));
1098 params().spaceBottom(VSpace(VSpace::NONE));
1099 params().spacing(Spacing(Spacing::Default));
1103 // if the layout of a paragraph contains a manual label, the beginning of the
1104 // main body is the beginning of the second word. This is what the par-
1105 // function returns. If the layout does not contain a label, the main
1106 // body always starts with position 0. This differentiation is necessary,
1107 // because there cannot be a newline or a blank <= the beginning of the
1108 // main body in TeX.
1110 int Paragraph::beginningOfMainBody() const
1112 // Unroll the first two cycles of the loop
1113 // and remember the previous character to
1114 // remove unnecessary GetChar() calls
1117 && getChar(i) != Paragraph::META_NEWLINE) {
1119 char previous_char = 0;
1122 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1123 // Yes, this ^ is supposed to be "= " not "=="
1126 && previous_char != ' '
1127 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1129 previous_char = temp;
1138 Paragraph * Paragraph::depthHook(depth_type depth)
1140 Paragraph * newpar = this;
1143 newpar = newpar->previous();
1144 } while (newpar && newpar->getDepth() > depth);
1147 if (previous() || getDepth())
1148 lyxerr << "ERROR (Paragraph::DepthHook): "
1157 Paragraph const * Paragraph::depthHook(depth_type depth) const
1159 Paragraph const * newpar = this;
1162 newpar = newpar->previous();
1163 } while (newpar && newpar->getDepth() > depth);
1166 if (previous() || getDepth())
1167 lyxerr << "ERROR (Paragraph::DepthHook): "
1175 Paragraph * Paragraph::outerHook()
1179 return depthHook(depth_type(getDepth() - 1));
1182 Paragraph const * Paragraph::outerHook() const
1186 return depthHook(depth_type(getDepth() - 1));
1189 int Paragraph::autoDeleteInsets()
1192 InsetList::size_type index = 0;
1193 while (index < insetlist.size()) {
1194 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1195 erase(insetlist[index].pos);
1196 // Erase() calls to insetlist.erase(&insetlist[index])
1197 // so index shouldn't be increased.
1206 Paragraph::inset_iterator
1207 Paragraph::InsetIterator(pos_type pos)
1209 InsetTable search_inset(pos, 0);
1210 InsetList::iterator it = lower_bound(insetlist.begin(),
1212 search_inset, Pimpl::matchIT());
1213 return inset_iterator(it);
1217 // returns -1 if inset not found
1218 int Paragraph::getPositionOfInset(Inset * inset) const
1221 for (InsetList::const_iterator cit = insetlist.begin();
1222 cit != insetlist.end(); ++cit) {
1223 if (cit->inset == inset) {
1227 if (inset == bibkey)
1234 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1235 BufferParams const & bparams,
1236 ostream & os, TexRow & texrow,
1239 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1240 LyXLayout const & style =
1241 textclasslist.Style(bparams.textclass,
1244 bool further_blank_line = false;
1246 if (params().startOfAppendix()) {
1247 os << "\\appendix\n";
1251 if (!params().spacing().isDefault()
1252 && (!previous() || !previous()->hasSameLayout(this))) {
1253 os << params().spacing().writeEnvirBegin() << "\n";
1257 if (tex_code_break_column && style.isCommand()){
1262 if (params().pagebreakTop()) {
1264 further_blank_line = true;
1266 if (params().spaceTop().kind() != VSpace::NONE) {
1267 os << params().spaceTop().asLatexCommand(bparams);
1268 further_blank_line = true;
1271 if (params().lineTop()) {
1272 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1273 << "\\vspace{-1\\parskip}";
1274 further_blank_line = true;
1277 if (further_blank_line){
1282 Language const * language = getParLanguage(bparams);
1283 Language const * doc_language = bparams.language;
1284 Language const * previous_language = previous()
1285 ? previous()->getParLanguage(bparams) : doc_language;
1287 if (language->babel() != previous_language->babel()
1288 // check if we already put language command in TeXEnvironment()
1289 && !(textclasslist.Style(bparams.textclass, layout).isEnvironment()
1290 && (!previous() || previous()->layout != layout ||
1291 previous()->params().depth() != params().depth()))) {
1293 if (!lyxrc.language_command_end.empty() &&
1294 previous_language->babel() != doc_language->babel()) {
1295 os << subst(lyxrc.language_command_end, "$$lang",
1296 previous_language->babel())
1301 if (lyxrc.language_command_end.empty() ||
1302 language->babel() != doc_language->babel()) {
1303 os << subst(lyxrc.language_command_begin, "$$lang",
1310 if (bparams.inputenc == "auto" &&
1311 language->encoding() != previous_language->encoding()) {
1312 os << "\\inputencoding{"
1313 << language->encoding()->LatexName()
1318 switch (style.latextype) {
1321 << style.latexname()
1322 << style.latexparam();
1324 case LATEX_ITEM_ENVIRONMENT:
1326 bibkey->latex(buf, os, false, false);
1330 case LATEX_LIST_ENVIRONMENT:
1337 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1339 // Make sure that \\par is done with the font of the last
1340 // character if this has another size as the default.
1341 // This is necessary because LaTeX (and LyX on the screen)
1342 // calculates the space between the baselines according
1343 // to this font. (Matthias)
1345 // Is this really needed ? (Dekel)
1346 // We do not need to use to change the font for the last paragraph
1347 // or for a command.
1348 LyXFont const font =
1350 ? getLayoutFont(bparams)
1351 : getFont(bparams, size() - 1));
1353 bool is_command = textclasslist.Style(bparams.textclass,
1354 getLayout()).isCommand();
1355 if (style.resfont.size() != font.size() && next_ && !is_command) {
1358 os << "\\" << font.latexSize() << " \\par}";
1359 } else if (need_par) {
1361 } else if (is_command)
1364 switch (style.latextype) {
1365 case LATEX_ITEM_ENVIRONMENT:
1366 case LATEX_LIST_ENVIRONMENT:
1367 if (next_ && (params().depth() < next_->params().depth())) {
1372 case LATEX_ENVIRONMENT:
1373 // if its the last paragraph of the current environment
1374 // skip it otherwise fall through
1376 && (next_->layout != layout
1377 || next_->params().depth() != params().depth()))
1379 // fall through possible
1381 // we don't need it for the last paragraph!!!
1388 further_blank_line = false;
1389 if (params().lineBottom()) {
1390 os << "\\lyxline{\\" << font.latexSize() << '}';
1391 further_blank_line = true;
1394 if (params().spaceBottom().kind() != VSpace::NONE) {
1395 os << params().spaceBottom().asLatexCommand(bparams);
1396 further_blank_line = true;
1399 if (params().pagebreakBottom()) {
1401 further_blank_line = true;
1404 if (further_blank_line){
1409 if (!params().spacing().isDefault()
1410 && (!next_ || !next_->hasSameLayout(this))) {
1411 os << params().spacing().writeEnvirEnd() << "\n";
1415 // we don't need it for the last paragraph!!!
1420 // Since \selectlanguage write the language to the aux file,
1421 // we need to reset the language at the end of footnote or
1424 if (language->babel() != doc_language->babel()) {
1425 if (lyxrc.language_command_end.empty())
1426 os << subst(lyxrc.language_command_begin,
1428 doc_language->babel())
1431 os << subst(lyxrc.language_command_end,
1439 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1444 // This one spits out the text of the paragraph
1445 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1446 BufferParams const & bparams,
1447 ostream & os, TexRow & texrow,
1450 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1452 bool return_value = false;
1454 LyXLayout const & style =
1455 textclasslist.Style(bparams.textclass,
1459 // Maybe we have to create a optional argument.
1461 if (style.labeltype != LABEL_MANUAL)
1464 main_body = beginningOfMainBody();
1468 if (main_body > 0) {
1471 basefont = getLabelFont(bparams);
1473 basefont = getLayoutFont(bparams);
1477 && !pimpl_->size()) {
1478 if (style.isCommand()) {
1484 moving_arg |= style.needprotect;
1486 // Which font is currently active?
1487 LyXFont running_font(basefont);
1488 // Do we have an open font change?
1489 bool open_font = false;
1491 texrow.start(this, 0);
1493 for (pos_type i = 0; i < size(); ++i) {
1495 // First char in paragraph or after label?
1496 if (i == main_body) {
1497 if (main_body > 0) {
1499 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1502 basefont = getLayoutFont(bparams);
1503 running_font = basefont;
1507 if (style.isCommand()) {
1512 if (params().noindent()) {
1513 os << "\\noindent ";
1516 switch (params().align()) {
1517 case LYX_ALIGN_NONE:
1518 case LYX_ALIGN_BLOCK:
1519 case LYX_ALIGN_LAYOUT:
1520 case LYX_ALIGN_SPECIAL:
1522 case LYX_ALIGN_LEFT:
1523 if (getParLanguage(bparams)->babel() != "hebrew") {
1524 os << "\\begin{flushleft}";
1527 os << "\\begin{flushright}";
1531 case LYX_ALIGN_RIGHT:
1532 if (getParLanguage(bparams)->babel() != "hebrew") {
1533 os << "\\begin{flushright}";
1536 os << "\\begin{flushleft}";
1540 case LYX_ALIGN_CENTER:
1541 os << "\\begin{center}";
1547 value_type c = getChar(i);
1549 // Fully instantiated font
1550 LyXFont font = getFont(bparams, i);
1552 LyXFont const last_font = running_font;
1554 // Spaces at end of font change are simulated to be
1555 // outside font change, i.e. we write "\textXX{text} "
1556 // rather than "\textXX{text }". (Asger)
1557 if (open_font && c == ' ' && i <= size() - 2) {
1558 LyXFont const next_font = getFont(bparams, i + 1);
1559 if (next_font != running_font
1560 && next_font != font) {
1565 // We end font definition before blanks
1566 if (font != running_font && open_font) {
1567 column += running_font.latexWriteEndChanges(os,
1569 (i == main_body-1) ? basefont : font);
1570 running_font = basefont;
1574 // Blanks are printed before start of fontswitch
1576 // Do not print the separation of the optional argument
1577 if (i != main_body - 1) {
1578 pimpl_->simpleTeXBlanks(os, texrow, i,
1579 column, font, style);
1583 // Do we need to change font?
1584 if (font != running_font && i != main_body - 1) {
1585 column += font.latexWriteStartChanges(os, basefont,
1587 running_font = font;
1591 if (c == Paragraph::META_NEWLINE) {
1592 // newlines are handled differently here than
1593 // the default in SimpleTeXSpecialChars().
1594 if (!style.newline_allowed) {
1598 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1601 basefont = getLayoutFont(bparams);
1602 running_font = basefont;
1603 if (font.family() ==
1604 LyXFont::TYPEWRITER_FAMILY) {
1612 texrow.start(this, i + 1);
1615 pimpl_->simpleTeXSpecialChars(buf, bparams,
1616 os, texrow, moving_arg,
1618 basefont, open_font,
1619 style, i, column, c);
1623 // If we have an open font definition, we have to close it
1625 #ifdef FIXED_LANGUAGE_END_DETECTION
1628 .latexWriteEndChanges(os, basefont,
1629 next_->getFont(bparams,
1632 running_font.latexWriteEndChanges(os, basefont,
1636 #ifdef WITH_WARNINGS
1637 //#warning For now we ALWAYS have to close the foreign font settings if they are
1638 //#warning there as we start another \selectlanguage with the next paragraph if
1639 //#warning we are in need of this. This should be fixed sometime (Jug)
1641 running_font.latexWriteEndChanges(os, basefont, basefont);
1645 // Needed if there is an optional argument but no contents.
1646 if (main_body > 0 && main_body == size()) {
1648 return_value = false;
1651 switch (params().align()) {
1652 case LYX_ALIGN_NONE:
1653 case LYX_ALIGN_BLOCK:
1654 case LYX_ALIGN_LAYOUT:
1655 case LYX_ALIGN_SPECIAL:
1657 case LYX_ALIGN_LEFT:
1658 if (getParLanguage(bparams)->babel() != "hebrew") {
1659 os << "\\end{flushleft}";
1662 os << "\\end{flushright}";
1666 case LYX_ALIGN_RIGHT:
1667 if (getParLanguage(bparams)->babel() != "hebrew") {
1668 os << "\\end{flushright}";
1671 os << "\\end{flushleft}";
1675 case LYX_ALIGN_CENTER:
1676 os << "\\end{center}";
1681 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1682 return return_value;
1686 bool Paragraph::sgmlConvertChar(char c, string & sgml_string)
1688 bool retval = false;
1690 case Paragraph::META_HFILL:
1691 sgml_string.erase();
1693 case Paragraph::META_NEWLINE:
1697 sgml_string = "&";
1700 sgml_string = "<";
1703 sgml_string = ">";
1706 sgml_string = "$";
1709 sgml_string = "#";
1712 sgml_string = "%";
1715 sgml_string = "[";
1718 sgml_string = "]";
1721 sgml_string = "{";
1724 sgml_string = "}";
1727 sgml_string = "˜";
1730 sgml_string = """;
1733 sgml_string = "\";
1739 case '\0': // Ignore :-)
1740 sgml_string.erase();
1750 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1751 BufferParams const & bparams,
1752 ostream & os, TexRow & texrow)
1754 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1756 LyXLayout const & style =
1757 textclasslist.Style(bparams.textclass,
1760 Language const * language = getParLanguage(bparams);
1761 Language const * doc_language = bparams.language;
1762 Language const * previous_language = previous_
1763 ? previous_->getParLanguage(bparams) : doc_language;
1764 if (language->babel() != previous_language->babel()) {
1766 if (!lyxrc.language_command_end.empty() &&
1767 previous_language->babel() != doc_language->babel()) {
1768 os << subst(lyxrc.language_command_end, "$$lang",
1769 previous_language->babel())
1774 if (lyxrc.language_command_end.empty() ||
1775 language->babel() != doc_language->babel()) {
1776 os << subst(lyxrc.language_command_begin, "$$lang",
1783 if (style.isEnvironment()) {
1784 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1785 os << "\\begin{" << style.latexname() << "}{"
1786 << params().labelWidthString() << "}\n";
1787 } else if (style.labeltype == LABEL_BIBLIO) {
1789 os << "\\begin{" << style.latexname() << "}{"
1790 << bibitemWidest(buf)
1792 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1793 os << "\\begin{" << style.latexname() << '}'
1794 << style.latexparam() << '\n';
1796 os << "\\begin{" << style.latexname() << '}'
1797 << style.latexparam() << '\n';
1800 Paragraph * par = this;
1802 par = par->TeXOnePar(buf, bparams,
1805 if (par && par->params().depth() > params().depth()) {
1806 if (textclasslist.Style(bparams.textclass,
1807 par->layout).isParagraph()
1809 // How to handle this? (Lgb)
1810 //&& !suffixIs(os, "\n\n")
1812 // There should be at least one '\n' already
1813 // but we need there to be two for Standard
1814 // paragraphs that are depth-increment'ed to be
1815 // output correctly. However, tables can
1816 // also be paragraphs so don't adjust them.
1819 // Will it ever harm to have one '\n' too
1820 // many? i.e. that we sometimes will have
1821 // three in a row. (Lgb)
1825 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1828 && par->layout == layout
1829 && par->params().depth() == params().depth());
1831 if (style.isEnvironment()) {
1832 os << "\\end{" << style.latexname() << "}\n";
1835 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1836 return par; // ale970302
1840 bool Paragraph::isHfill(pos_type pos) const
1842 return IsHfillChar(getChar(pos));
1846 bool Paragraph::isInset(pos_type pos) const
1848 return IsInsetChar(getChar(pos));
1852 bool Paragraph::isNewline(pos_type pos) const
1854 return pos >= 0 && IsNewlineChar(getChar(pos));
1858 bool Paragraph::isSeparator(pos_type pos) const
1860 return IsSeparatorChar(getChar(pos));
1864 bool Paragraph::isLineSeparator(pos_type pos) const
1866 return IsLineSeparatorChar(getChar(pos));
1870 bool Paragraph::isKomma(pos_type pos) const
1872 return IsKommaChar(getChar(pos));
1876 /// Used by the spellchecker
1877 bool Paragraph::isLetter(pos_type pos) const
1879 value_type const c = getChar(pos);
1880 if (IsLetterChar(c))
1883 return getInset(pos)->isLetter();
1884 // We want to pass the ' and escape chars to ispell
1885 string const extra = lyxrc.isp_esc_chars + '\'';
1886 return contains(extra, c);
1890 bool Paragraph::isWord(pos_type pos ) const
1892 return IsWordChar(getChar(pos)) ;
1897 Paragraph::getParLanguage(BufferParams const & bparams) const
1900 #ifndef INHERIT_LANGUAGE
1901 return getFirstFontSettings().language();
1903 Language const * lang = getFirstFontSettings().language();
1904 #warning We should make this somewhat better, any ideas? (Jug)
1905 if (lang == inherit_language || lang == ignore_language)
1906 lang = bparams.language;
1909 } else if (previous_)
1910 return previous_->getParLanguage(bparams);
1912 return bparams.language;
1916 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1918 return lyxrc.rtl_support
1919 && getParLanguage(bparams)->RightToLeft();
1923 void Paragraph::changeLanguage(BufferParams const & bparams,
1924 Language const * from, Language const * to)
1926 for (pos_type i = 0; i < size(); ++i) {
1927 LyXFont font = getFontSettings(bparams, i);
1928 if (font.language() == from) {
1929 font.setLanguage(to);
1936 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1938 Language const * doc_language = bparams.language;
1939 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1940 cit != pimpl_->fontlist.end(); ++cit)
1941 if (cit->font().language() != ignore_language &&
1942 cit->font().language() != latex_language &&
1943 #ifdef INHERIT_LANGUAGE
1944 cit->font().language() != inherit_language &&
1946 cit->font().language() != doc_language)
1952 // Convert the paragraph to a string.
1953 // Used for building the table of contents
1954 string const Paragraph::asString(Buffer const * buffer, bool label)
1956 BufferParams const & bparams = buffer->params;
1958 if (label && !params().labelString().empty())
1959 s += params().labelString() + ' ';
1960 string::size_type const len = s.size();
1962 for (pos_type i = 0; i < size(); ++i) {
1963 value_type c = getChar(i);
1966 else if (c == META_INSET &&
1967 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1969 getInset(i)->ascii(buffer, ost);
1970 s += subst(ost.str().c_str(),'\n',' ');
1974 if (isRightToLeftPar(bparams))
1975 reverse(s.begin() + len,s.end());
1981 string const Paragraph::asString(Buffer const * buffer,
1982 pos_type beg, pos_type end, bool label)
1986 if (beg == 0 && label && !params().labelString().empty())
1987 ost << params().labelString() << ' ';
1989 for (pos_type i = beg; i < end; ++i) {
1990 value_type const c = getUChar(buffer->params, i);
1993 else if (c == META_NEWLINE)
1995 else if (c == META_HFILL)
1997 else if (c == META_INSET) {
1998 getInset(i)->ascii(buffer, ost);
2002 return ost.str().c_str();
2006 void Paragraph::setInsetOwner(Inset * i)
2008 pimpl_->inset_owner = i;
2009 for (InsetList::const_iterator cit = insetlist.begin();
2010 cit != insetlist.end(); ++cit) {
2012 cit->inset->setOwner(i);
2017 void Paragraph::deleteInsetsLyXText(BufferView * bv)
2020 for (InsetList::const_iterator cit = insetlist.begin();
2021 cit != insetlist.end(); ++cit) {
2023 if (cit->inset->isTextInset()) {
2024 static_cast<UpdatableInset *>
2025 (cit->inset)->deleteLyXText(bv, true);
2032 void Paragraph::resizeInsetsLyXText(BufferView * bv)
2035 for (InsetList::const_iterator cit = insetlist.begin();
2036 cit != insetlist.end(); ++cit)
2039 if (cit->inset->isTextInset()) {
2040 static_cast<UpdatableInset *>
2041 (cit->inset)->resizeLyXText(bv, true);
2048 void Paragraph::setContentsFromPar(Paragraph * par)
2050 pimpl_->setContentsFromPar(par);
2054 lyx::pos_type Paragraph::size() const
2056 return pimpl_->size();
2060 Paragraph::value_type Paragraph::getChar(pos_type pos) const
2062 return pimpl_->getChar(pos);
2066 int Paragraph::id() const
2072 void Paragraph::id(int id_arg)
2074 pimpl_->id_ = id_arg;
2078 layout_type Paragraph::getLayout() const
2084 bool Paragraph::isFirstInSequence() const
2086 Paragraph const * dhook = depthHook(getDepth());
2087 return (dhook == this
2088 || dhook->getLayout() != getLayout()
2089 || dhook->getDepth() != getDepth());
2093 Inset * Paragraph::inInset() const
2095 return pimpl_->inset_owner;
2099 void Paragraph::clearContents()
2105 void Paragraph::setCounter(int i, int v)
2107 pimpl_->counter_[i] = v;
2111 int Paragraph::getCounter(int i) const
2113 return pimpl_->counter_[i];
2117 void Paragraph::incCounter(int i)
2119 pimpl_->counter_[i]++;
2123 void Paragraph::setChar(pos_type pos, value_type c)
2125 pimpl_->setChar(pos, c);
2129 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2134 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2136 return inset_iterator(insetlist.begin());
2140 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2142 return inset_iterator(insetlist.end());
2146 ParagraphParameters & Paragraph::params()
2148 return pimpl_->params;
2152 ParagraphParameters const & Paragraph::params() const
2154 return pimpl_->params;
2158 Paragraph * Paragraph::getParFromID(int id) const
2160 return pimpl_->getParFromID(id);
2164 bool Paragraph::isFreeSpacing() const
2166 // for now we just need this, later should we need this in some
2167 // other way we can always add a function to Inset::() too.
2168 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2169 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);