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 * ====================================================== */
13 #include "paragraph.h"
14 #include "paragraph_pimpl.h"
18 #include "tex-strings.h"
20 #include "bufferparams.h"
23 #include "BufferView.h"
25 #include "ParameterStruct.h"
29 #include "insets/insetbib.h"
30 #include "insets/insetoptarg.h"
32 #include "support/filetools.h"
33 #include "support/lstrings.h"
34 #include "support/lyxmanip.h"
35 #include "support/FileInfo.h"
36 #include "support/LAssert.h"
37 #include "support/textutils.h"
48 using std::lower_bound;
49 using std::upper_bound;
53 // this is a bad idea, but how can Paragraph find its buffer to get
54 // parameters? (JMarc)
56 extern string bibitemWidest(Buffer const *);
58 // this is a minibuffer
63 LyXFont minibuffer_font;
64 Inset * minibuffer_inset;
69 extern BufferView * current_view;
72 Paragraph::Paragraph()
73 : pimpl_(new Paragraph::Pimpl(this))
81 bibkey = 0; // ale970302
87 // This constructor inserts the new paragraph in a list.
88 Paragraph::Paragraph(Paragraph * par)
89 : pimpl_(new Paragraph::Pimpl(this))
94 // double linked list begin
97 next_->previous_ = this;
99 previous_->next_ = this;
102 bibkey = 0; // ale970302
108 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
109 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
117 // this is because of the dummy layout of the paragraphs that
119 layout_ = lp.layout();
123 bibkey = static_cast<InsetBibKey *>
124 (lp.bibkey->clone(*current_view->buffer()));
129 // copy everything behind the break-position to the new paragraph
130 insetlist = lp.insetlist;
131 InsetList::iterator it = insetlist.begin();
132 InsetList::iterator end = insetlist.end();
133 for (; it != end; ++it) {
134 it.setInset(it.getInset()->clone(*current_view->buffer(),
136 // tell the new inset who is the boss now
137 it.getInset()->parOwner(this);
142 // the destructor removes the new paragraph from the list
143 Paragraph::~Paragraph()
147 previous_->next_ = next_;
149 next_->previous_ = previous_;
157 //lyxerr << "Paragraph::paragraph_id = "
158 // << Paragraph::paragraph_id << endl;
162 void Paragraph::write(Buffer const * buf, ostream & os,
163 BufferParams const & bparams,
164 depth_type & dth) const
166 // The beginning or end of a deeper (i.e. nested) area?
167 if (dth != params().depth()) {
168 if (params().depth() > dth) {
169 while (params().depth() > dth) {
170 os << "\n\\begin_deeper ";
174 while (params().depth() < dth) {
175 os << "\n\\end_deeper ";
181 // First write the layout
182 os << "\n\\layout " << layout()->name() << '\n';
184 // Maybe some vertical spaces.
185 if (params().spaceTop().kind() != VSpace::NONE)
186 os << "\\added_space_top "
187 << params().spaceTop().asLyXCommand() << ' ';
188 if (params().spaceBottom().kind() != VSpace::NONE)
189 os << "\\added_space_bottom "
190 << params().spaceBottom().asLyXCommand() << ' ';
192 // Maybe the paragraph has special spacing
193 params().spacing().writeFile(os, true);
195 // The labelwidth string used in lists.
196 if (!params().labelWidthString().empty())
197 os << "\\labelwidthstring "
198 << params().labelWidthString() << '\n';
200 // Lines above or below?
201 if (params().lineTop())
203 if (params().lineBottom())
204 os << "\\line_bottom ";
206 // Pagebreaks above or below?
207 if (params().pagebreakTop())
208 os << "\\pagebreak_top ";
209 if (params().pagebreakBottom())
210 os << "\\pagebreak_bottom ";
212 // Start of appendix?
213 if (params().startOfAppendix())
214 os << "\\start_of_appendix ";
217 if (params().noindent())
220 // Do we have a manual left indent?
221 if (!params().leftIndent().zero())
222 os << "\\leftindent " << params().leftIndent().asString()
226 if (params().align() != LYX_ALIGN_LAYOUT) {
228 switch (params().align()) {
229 case LYX_ALIGN_LEFT: h = 1; break;
230 case LYX_ALIGN_RIGHT: h = 2; break;
231 case LYX_ALIGN_CENTER: h = 3; break;
232 default: h = 0; break;
234 os << "\\align " << string_align[h] << ' ';
239 bibkey->write(buf, os);
241 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
243 Change running_change = Change(Change::UNCHANGED);
244 lyx::time_type const curtime(lyx::current_time());
247 for (pos_type i = 0; i < size(); ++i) {
253 Change change = pimpl_->lookupChangeFull(i);
254 Changes::lyxMarkChange(os, column, curtime, running_change, change);
255 running_change = change;
257 // Write font changes
258 LyXFont font2 = getFontSettings(bparams, i);
259 if (font2 != font1) {
260 font2.lyxWriteChanges(font1, os);
265 value_type const c = getChar(i);
269 Inset const * inset = getInset(i);
271 if (inset->directWrite()) {
272 // international char, let it write
273 // code directly so it's shorter in
275 inset->write(buf, os);
277 os << "\n\\begin_inset ";
278 inset->write(buf, os);
279 os << "\n\\end_inset \n\n";
285 os << "\n\\newline \n";
289 os << "\n\\hfill \n";
293 os << "\n\\backslash \n";
297 if (i + 1 < size() && getChar(i + 1) == ' ') {
304 if ((column > 70 && c == ' ')
309 // this check is to amend a bug. LyX sometimes
310 // inserts '\0' this could cause problems.
314 lyxerr << "ERROR (Paragraph::writeFile):"
315 " NULL char in structure." << endl;
321 // to make reading work properly
323 running_change = pimpl_->lookupChange(0);
324 Changes::lyxMarkChange(os, column, curtime,
325 Change(Change::UNCHANGED), running_change);
327 Changes::lyxMarkChange(os, column, curtime,
328 running_change, Change(Change::UNCHANGED));
332 void Paragraph::validate(LaTeXFeatures & features) const
334 pimpl_->validate(features, *layout());
338 // First few functions needed for cut and paste and paragraph breaking.
339 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
341 BufferParams bparams = buffer.params;
343 minibuffer_char = getChar(pos);
344 minibuffer_font = getFontSettings(bparams, pos);
345 minibuffer_inset = 0;
346 if (minibuffer_char == Paragraph::META_INSET) {
348 minibuffer_inset = getInset(pos)->clone(buffer);
350 minibuffer_inset = 0;
351 minibuffer_char = ' ';
352 // This reflects what GetInset() does (ARRae)
358 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
360 minibuffer_char = getChar(pos);
361 minibuffer_font = getFontSettings(bparams, pos);
362 minibuffer_inset = 0;
363 if (minibuffer_char == Paragraph::META_INSET) {
365 // the inset is not in a paragraph anymore
366 minibuffer_inset = insetlist.release(pos);
367 minibuffer_inset->parOwner(0);
369 minibuffer_inset = 0;
370 minibuffer_char = ' ';
371 // This reflects what GetInset() does (ARRae)
376 // Erase(pos); now the caller is responsible for that.
380 bool Paragraph::insertFromMinibuffer(pos_type pos)
382 if (minibuffer_char == Paragraph::META_INSET) {
383 if (!insetAllowed(minibuffer_inset->lyxCode())) {
386 insertInset(pos, minibuffer_inset, minibuffer_font);
388 LyXFont f = minibuffer_font;
389 if (!checkInsertChar(f)) {
392 insertChar(pos, minibuffer_char, f);
400 void Paragraph::erase(pos_type pos)
406 bool Paragraph::erase(pos_type start, pos_type end)
408 return pimpl_->erase(start, end);
412 bool Paragraph::checkInsertChar(LyXFont & font)
414 if (pimpl_->inset_owner)
415 return pimpl_->inset_owner->checkInsertChar(font);
420 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
422 LyXFont const f(LyXFont::ALL_INHERIT);
423 insertChar(pos, c, f);
427 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
428 LyXFont const & font, Change change)
430 pimpl_->insertChar(pos, c, font, change);
434 void Paragraph::insertInset(pos_type pos, Inset * inset)
436 LyXFont const f(LyXFont::ALL_INHERIT);
437 insertInset(pos, inset, f);
441 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font, Change change)
443 pimpl_->insertInset(pos, inset, font, change);
447 bool Paragraph::insetAllowed(Inset::Code code)
449 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
451 if (pimpl_->inset_owner)
452 return pimpl_->inset_owner->insetAllowed(code);
457 Inset * Paragraph::getInset(pos_type pos)
459 lyx::Assert(pos < size());
461 return insetlist.get(pos);
465 Inset const * Paragraph::getInset(pos_type pos) const
467 lyx::Assert(pos < size());
469 return insetlist.get(pos);
473 // Gets uninstantiated font setting at position.
474 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
477 lyx::Assert(pos <= size());
479 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
480 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
481 for (; cit != end; ++cit) {
482 if (cit->pos() >= pos)
488 retfont = cit->font();
489 } else if (pos == size() && !empty()) {
490 retfont = getFontSettings(bparams, pos - 1);
492 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
498 // Gets uninstantiated font setting at position 0
499 LyXFont const Paragraph::getFirstFontSettings() const
501 if (!empty() && !pimpl_->fontlist.empty())
502 return pimpl_->fontlist[0].font();
504 return LyXFont(LyXFont::ALL_INHERIT);
508 // Gets the fully instantiated font at a given position in a paragraph
509 // This is basically the same function as LyXText::GetFont() in text2.C.
510 // The difference is that this one is used for generating the LaTeX file,
511 // and thus cosmetic "improvements" are disallowed: This has to deliver
512 // the true picture of the buffer. (Asger)
513 // If position is -1, we get the layout font of the paragraph.
514 // If position is -2, we get the font of the manual label of the paragraph.
515 LyXFont const Paragraph::getFont(BufferParams const & bparams,
518 lyx::Assert(pos >= 0);
520 LyXLayout_ptr const & lout = layout();
522 pos_type const main_body = beginningOfMainBody();
526 layoutfont = lout->labelfont;
528 layoutfont = lout->font;
530 LyXFont tmpfont = getFontSettings(bparams, pos);
531 tmpfont.realize(layoutfont);
533 return pimpl_->realizeFont(tmpfont, bparams);
537 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
539 LyXLayout_ptr const & lout = layout();
541 LyXFont tmpfont = lout->labelfont;
542 tmpfont.setLanguage(getParLanguage(bparams));
544 return pimpl_->realizeFont(tmpfont, bparams);
548 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
550 LyXLayout_ptr const & lout = layout();
552 LyXFont tmpfont = lout->font;
553 tmpfont.setLanguage(getParLanguage(bparams));
555 return pimpl_->realizeFont(tmpfont, bparams);
559 /// Returns the height of the highest font in range
561 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
562 LyXFont::FONT_SIZE const def_size) const
564 if (pimpl_->fontlist.empty())
567 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
568 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
569 for (; end_it != end; ++end_it) {
570 if (end_it->pos() >= endpos)
577 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
578 for (; cit != end; ++cit) {
579 if (cit->pos() >= startpos)
583 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
584 for (; cit != end_it; ++cit) {
585 LyXFont::FONT_SIZE size = cit->font().size();
586 if (size == LyXFont::INHERIT_SIZE)
588 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
595 Paragraph::value_type
596 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
598 value_type c = getChar(pos);
599 if (!lyxrc.rtl_support)
629 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
636 void Paragraph::setFont(pos_type pos, LyXFont const & font)
638 lyx::Assert(pos <= size());
640 // First, reduce font against layout/label font
641 // Update: The SetCharFont() routine in text2.C already
642 // reduces font, so we don't need to do that here. (Asger)
643 // No need to simplify this because it will disappear
644 // in a new kernel. (Asger)
645 // Next search font table
647 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
648 Pimpl::FontList::iterator it = beg;
649 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
650 for (; it != endit; ++it) {
651 if (it->pos() >= pos)
654 unsigned int i = std::distance(beg, it);
655 bool notfound = (it == endit);
657 if (!notfound && pimpl_->fontlist[i].font() == font)
660 bool begin = pos == 0 || notfound ||
661 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
662 // Is position pos is a beginning of a font block?
663 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
664 // Is position pos is the end of a font block?
665 if (begin && end) { // A single char block
666 if (i + 1 < pimpl_->fontlist.size() &&
667 pimpl_->fontlist[i + 1].font() == font) {
668 // Merge the singleton block with the next block
669 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
670 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
671 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
672 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
673 // Merge the singleton block with the previous block
674 pimpl_->fontlist[i - 1].pos(pos);
675 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
677 pimpl_->fontlist[i].font(font);
679 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
680 pimpl_->fontlist[i - 1].pos(pos);
682 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
683 Pimpl::FontTable(pos, font));
685 pimpl_->fontlist[i].pos(pos - 1);
686 if (!(i + 1 < pimpl_->fontlist.size() &&
687 pimpl_->fontlist[i + 1].font() == font))
688 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
689 Pimpl::FontTable(pos, font));
690 } else { // The general case. The block is splitted into 3 blocks
691 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
692 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
693 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
694 Pimpl::FontTable(pos, font));
700 void Paragraph::next(Paragraph * p)
706 // This function is able to hide closed footnotes.
707 Paragraph * Paragraph::next()
713 Paragraph const * Paragraph::next() const
719 void Paragraph::previous(Paragraph * p)
725 // This function is able to hide closed footnotes.
726 Paragraph * Paragraph::previous()
732 // This function is able to hide closed footnotes.
733 Paragraph const * Paragraph::previous() const
740 void Paragraph::makeSameLayout(Paragraph const * par)
742 layout(par->layout());
744 params() = par->params();
748 int Paragraph::stripLeadingSpaces()
750 if (layout()->free_spacing ||
756 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
757 pimpl_->eraseIntern(0);
765 bool Paragraph::hasSameLayout(Paragraph const * par) const
768 par->layout() == layout() &&
769 params().sameLayout(par->params());
773 int Paragraph::getEndLabel() const
775 Paragraph const * par = this;
776 depth_type par_depth = getDepth();
778 LyXLayout_ptr const & layout = par->layout();
779 int const endlabeltype = layout->endlabeltype;
781 if (endlabeltype != END_LABEL_NO_LABEL) {
785 depth_type const next_depth = next_->getDepth();
786 if (par_depth > next_depth ||
787 (par_depth == next_depth
788 && layout != next_->layout()))
794 par = par->outerHook();
796 par_depth = par->getDepth();
798 return END_LABEL_NO_LABEL;
802 Paragraph::depth_type Paragraph::getDepth() const
804 return params().depth();
808 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
810 bool const isenv = layout()->isEnvironment();
813 return params().depth() + 1;
815 return params().depth();
819 char Paragraph::getAlign() const
821 return params().align();
825 string const & Paragraph::getLabelstring() const
827 return params().labelString();
831 // the next two functions are for the manual labels
832 string const Paragraph::getLabelWidthString() const
834 if (!params().labelWidthString().empty())
835 return params().labelWidthString();
837 return _("Senseless with this layout!");
841 void Paragraph::setLabelWidthString(string const & s)
843 params().labelWidthString(s);
847 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
850 params().labelWidthString(string());
851 params().align(LYX_ALIGN_LAYOUT);
852 params().spaceTop(VSpace(VSpace::NONE));
853 params().spaceBottom(VSpace(VSpace::NONE));
854 params().spacing(Spacing(Spacing::Default));
858 // if the layout of a paragraph contains a manual label, the beginning of the
859 // main body is the beginning of the second word. This is what the par-
860 // function returns. If the layout does not contain a label, the main
861 // body always starts with position 0. This differentiation is necessary,
862 // because there cannot be a newline or a blank <= the beginning of the
865 int Paragraph::beginningOfMainBody() const
867 if (layout()->labeltype != LABEL_MANUAL)
870 // Unroll the first two cycles of the loop
871 // and remember the previous character to
872 // remove unnecessary GetChar() calls
874 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
876 char previous_char = 0;
879 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
880 // Yes, this ^ is supposed to be "= " not "=="
883 && previous_char != ' '
884 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
886 previous_char = temp;
895 Paragraph * Paragraph::depthHook(depth_type depth)
897 Paragraph * newpar = this;
900 newpar = newpar->previous();
901 } while (newpar && newpar->getDepth() > depth);
904 if (previous() || getDepth())
905 lyxerr << "ERROR (Paragraph::DepthHook): "
914 Paragraph const * Paragraph::depthHook(depth_type depth) const
916 Paragraph const * newpar = this;
919 newpar = newpar->previous();
920 } while (newpar && newpar->getDepth() > depth);
923 if (previous() || getDepth())
924 lyxerr << "ERROR (Paragraph::DepthHook): "
933 Paragraph * Paragraph::outerHook()
937 return depthHook(depth_type(getDepth() - 1));
941 Paragraph const * Paragraph::outerHook() const
945 return depthHook(depth_type(getDepth() - 1));
949 // returns -1 if inset not found
950 int Paragraph::getPositionOfInset(Inset const * inset) const
953 InsetList::iterator it = insetlist.begin();
954 InsetList::iterator end = insetlist.end();
955 for (; it != end; ++it) {
956 if (it.getInset() == inset) {
968 InsetOptArg * optArgInset(Paragraph const & par)
971 InsetList::iterator it = par.insetlist.begin();
972 InsetList::iterator end = par.insetlist.end();
973 for (; it != end; ++it) {
974 Inset * ins = it.getInset();
975 if (ins->lyxCode() == Inset::OPTARG_CODE) {
976 return static_cast<InsetOptArg *>(ins);
984 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
985 BufferParams const & bparams,
986 ostream & os, TexRow & texrow,
989 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
990 Inset const * in = inInset();
991 bool further_blank_line = false;
994 // well we have to check if we are in an inset with unlimited
995 // lenght (all in one row) if that is true then we don't allow
996 // any special options in the paragraph and also we don't allow
997 // any environment other then "Standard" to be valid!
998 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1001 if (params().startOfAppendix()) {
1002 os << "\\appendix\n";
1006 if (!params().spacing().isDefault()
1007 && (!previous() || !previous()->hasSameLayout(this))) {
1008 os << params().spacing().writeEnvirBegin() << '\n';
1012 if (style->isCommand()) {
1017 if (params().pagebreakTop()) {
1019 further_blank_line = true;
1021 if (params().spaceTop().kind() != VSpace::NONE) {
1022 os << params().spaceTop().asLatexCommand(bparams);
1023 further_blank_line = true;
1026 if (params().lineTop()) {
1027 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1028 << "\\vspace{-1\\parskip}";
1029 further_blank_line = true;
1032 if (further_blank_line) {
1037 style = bparams.getLyXTextClass().defaultLayout();
1040 Language const * language = getParLanguage(bparams);
1041 Language const * doc_language = bparams.language;
1042 Language const * previous_language = previous()
1043 ? previous()->getParLanguage(bparams) : doc_language;
1045 if (language->babel() != previous_language->babel()
1046 // check if we already put language command in TeXEnvironment()
1047 && !(style->isEnvironment()
1049 (previous()->layout() != layout() &&
1050 previous()->getDepth() <= getDepth())
1051 || previous()->getDepth() < getDepth())))
1053 if (!lyxrc.language_command_end.empty() &&
1054 previous_language->babel() != doc_language->babel())
1056 os << subst(lyxrc.language_command_end, "$$lang",
1057 previous_language->babel())
1062 if (lyxrc.language_command_end.empty() ||
1063 language->babel() != doc_language->babel())
1065 os << subst(lyxrc.language_command_begin, "$$lang",
1072 if (bparams.inputenc == "auto" &&
1073 language->encoding() != previous_language->encoding()) {
1074 os << "\\inputencoding{"
1075 << language->encoding()->LatexName()
1080 switch (style->latextype) {
1083 << style->latexname();
1085 // Separate handling of optional argument inset.
1086 if (style->optionalargs == 1) {
1087 InsetOptArg * it = optArgInset(*this);
1089 it->latexOptional(buf, os, false, false);
1092 os << style->latexparam();
1094 case LATEX_ITEM_ENVIRONMENT:
1096 bibkey->latex(buf, os, false, false);
1100 case LATEX_LIST_ENVIRONMENT:
1107 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1109 // Make sure that \\par is done with the font of the last
1110 // character if this has another size as the default.
1111 // This is necessary because LaTeX (and LyX on the screen)
1112 // calculates the space between the baselines according
1113 // to this font. (Matthias)
1115 // Is this really needed ? (Dekel)
1116 // We do not need to use to change the font for the last paragraph
1117 // or for a command.
1118 LyXFont const font =
1120 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1122 bool is_command = style->isCommand();
1124 if (style->resfont.size() != font.size() && next_ && !is_command) {
1127 os << "\\" << font.latexSize() << " \\par}";
1128 } else if (need_par) {
1130 } else if (is_command)
1133 switch (style->latextype) {
1134 case LATEX_ITEM_ENVIRONMENT:
1135 case LATEX_LIST_ENVIRONMENT:
1136 if (next_ && (params().depth() < next_->params().depth())) {
1141 case LATEX_ENVIRONMENT:
1142 // if its the last paragraph of the current environment
1143 // skip it otherwise fall through
1145 && (next_->layout() != layout()
1146 || next_->params().depth() != params().depth()))
1148 // fall through possible
1150 // we don't need it for the last paragraph!!!
1157 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1158 further_blank_line = false;
1159 if (params().lineBottom()) {
1160 os << "\\lyxline{\\" << font.latexSize() << '}';
1161 further_blank_line = true;
1164 if (params().spaceBottom().kind() != VSpace::NONE) {
1165 os << params().spaceBottom().asLatexCommand(bparams);
1166 further_blank_line = true;
1169 if (params().pagebreakBottom()) {
1171 further_blank_line = true;
1174 if (further_blank_line) {
1179 if (!params().spacing().isDefault()
1180 && (!next_ || !next_->hasSameLayout(this))) {
1181 os << params().spacing().writeEnvirEnd() << '\n';
1186 // we don't need it for the last paragraph!!!
1191 // Since \selectlanguage write the language to the aux file,
1192 // we need to reset the language at the end of footnote or
1195 if (language->babel() != doc_language->babel()) {
1196 if (lyxrc.language_command_end.empty())
1197 os << subst(lyxrc.language_command_begin,
1199 doc_language->babel())
1202 os << subst(lyxrc.language_command_end,
1210 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1215 // This could go to ParagraphParameters if we want to
1216 int Paragraph::startTeXParParams(BufferParams const & bparams,
1217 ostream & os, bool moving_arg) const
1221 if (params().noindent()) {
1222 os << "\\noindent ";
1226 switch (params().align()) {
1227 case LYX_ALIGN_NONE:
1228 case LYX_ALIGN_BLOCK:
1229 case LYX_ALIGN_LAYOUT:
1230 case LYX_ALIGN_SPECIAL:
1232 case LYX_ALIGN_LEFT:
1233 case LYX_ALIGN_RIGHT:
1234 case LYX_ALIGN_CENTER:
1242 switch (params().align()) {
1243 case LYX_ALIGN_NONE:
1244 case LYX_ALIGN_BLOCK:
1245 case LYX_ALIGN_LAYOUT:
1246 case LYX_ALIGN_SPECIAL:
1248 case LYX_ALIGN_LEFT:
1249 if (getParLanguage(bparams)->babel() != "hebrew") {
1250 os << "\\begin{flushleft}";
1253 os << "\\begin{flushright}";
1257 case LYX_ALIGN_RIGHT:
1258 if (getParLanguage(bparams)->babel() != "hebrew") {
1259 os << "\\begin{flushright}";
1262 os << "\\begin{flushleft}";
1266 case LYX_ALIGN_CENTER:
1267 os << "\\begin{center}";
1276 // This could go to ParagraphParameters if we want to
1277 int Paragraph::endTeXParParams(BufferParams const & bparams,
1278 ostream & os, bool moving_arg) const
1282 switch (params().align()) {
1283 case LYX_ALIGN_NONE:
1284 case LYX_ALIGN_BLOCK:
1285 case LYX_ALIGN_LAYOUT:
1286 case LYX_ALIGN_SPECIAL:
1288 case LYX_ALIGN_LEFT:
1289 case LYX_ALIGN_RIGHT:
1290 case LYX_ALIGN_CENTER:
1298 switch (params().align()) {
1299 case LYX_ALIGN_NONE:
1300 case LYX_ALIGN_BLOCK:
1301 case LYX_ALIGN_LAYOUT:
1302 case LYX_ALIGN_SPECIAL:
1304 case LYX_ALIGN_LEFT:
1305 if (getParLanguage(bparams)->babel() != "hebrew") {
1306 os << "\\end{flushleft}";
1309 os << "\\end{flushright}";
1313 case LYX_ALIGN_RIGHT:
1314 if (getParLanguage(bparams)->babel() != "hebrew") {
1315 os << "\\end{flushright}";
1318 os << "\\end{flushleft}";
1322 case LYX_ALIGN_CENTER:
1323 os << "\\end{center}";
1331 // This one spits out the text of the paragraph
1332 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1333 BufferParams const & bparams,
1334 ostream & os, TexRow & texrow,
1337 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1339 bool return_value = false;
1341 LyXLayout_ptr style;
1343 // well we have to check if we are in an inset with unlimited
1344 // lenght (all in one row) if that is true then we don't allow
1345 // any special options in the paragraph and also we don't allow
1346 // any environment other then "Standard" to be valid!
1348 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1351 style = bparams.getLyXTextClass().defaultLayout();
1358 // Maybe we have to create a optional argument.
1361 // FIXME: can we actually skip this check and just call
1362 // beginningOfMainBody() ??
1363 if (style->labeltype != LABEL_MANUAL) {
1366 main_body = beginningOfMainBody();
1369 unsigned int column = 0;
1371 if (main_body > 0) {
1374 basefont = getLabelFont(bparams);
1376 basefont = getLayoutFont(bparams);
1379 moving_arg |= style->needprotect;
1381 // Which font is currently active?
1382 LyXFont running_font(basefont);
1383 // Do we have an open font change?
1384 bool open_font = false;
1386 Change::Type running_change = Change::UNCHANGED;
1388 texrow.start(this, 0);
1390 // if the paragraph is empty, the loop will not be entered at all
1392 if (style->isCommand()) {
1397 column += startTeXParParams(bparams, os, moving_arg);
1401 for (pos_type i = 0; i < size(); ++i) {
1403 // First char in paragraph or after label?
1404 if (i == main_body) {
1405 if (main_body > 0) {
1407 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1410 basefont = getLayoutFont(bparams);
1411 running_font = basefont;
1415 if (style->isCommand()) {
1421 column += startTeXParParams(bparams, os,
1425 value_type c = getChar(i);
1427 // Fully instantiated font
1428 LyXFont font = getFont(bparams, i);
1430 LyXFont const last_font = running_font;
1432 // Spaces at end of font change are simulated to be
1433 // outside font change, i.e. we write "\textXX{text} "
1434 // rather than "\textXX{text }". (Asger)
1435 if (open_font && c == ' ' && i <= size() - 2) {
1436 LyXFont const & next_font = getFont(bparams, i + 1);
1437 if (next_font != running_font
1438 && next_font != font) {
1443 // We end font definition before blanks
1445 (font != running_font ||
1446 font.language() != running_font.language()))
1448 column += running_font.latexWriteEndChanges(os,
1450 (i == main_body-1) ? basefont : font);
1451 running_font = basefont;
1455 // Blanks are printed before start of fontswitch
1457 // Do not print the separation of the optional argument
1458 if (i != main_body - 1) {
1459 pimpl_->simpleTeXBlanks(os, texrow, i,
1460 column, font, *style);
1464 // Do we need to change font?
1465 if ((font != running_font ||
1466 font.language() != running_font.language()) &&
1469 column += font.latexWriteStartChanges(os, basefont,
1471 running_font = font;
1475 Change::Type change = pimpl_->lookupChange(i);
1477 column += Changes::latexMarkChange(os, running_change, change);
1478 running_change = change;
1480 if (c == Paragraph::META_NEWLINE) {
1481 // newlines are handled differently here than
1482 // the default in SimpleTeXSpecialChars().
1483 if (!style->newline_allowed) {
1487 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1490 basefont = getLayoutFont(bparams);
1491 running_font = basefont;
1492 if (font.family() ==
1493 LyXFont::TYPEWRITER_FAMILY) {
1502 texrow.start(this, i + 1);
1505 pimpl_->simpleTeXSpecialChars(buf, bparams,
1506 os, texrow, moving_arg,
1508 basefont, open_font,
1510 *style, i, column, c);
1514 column += Changes::latexMarkChange(os,
1515 running_change, Change::UNCHANGED);
1517 // If we have an open font definition, we have to close it
1519 #ifdef FIXED_LANGUAGE_END_DETECTION
1522 .latexWriteEndChanges(os, basefont,
1523 next_->getFont(bparams,
1526 running_font.latexWriteEndChanges(os, basefont,
1530 #ifdef WITH_WARNINGS
1531 //#warning For now we ALWAYS have to close the foreign font settings if they are
1532 //#warning there as we start another \selectlanguage with the next paragraph if
1533 //#warning we are in need of this. This should be fixed sometime (Jug)
1535 running_font.latexWriteEndChanges(os, basefont, basefont);
1539 // Needed if there is an optional argument but no contents.
1540 if (main_body > 0 && main_body == size()) {
1542 return_value = false;
1546 column += endTeXParParams(bparams, os, moving_arg);
1549 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1550 return return_value;
1554 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1555 BufferParams const & bparams,
1556 ostream & os, TexRow & texrow)
1558 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1560 LyXLayout_ptr const & style = layout();
1562 Language const * language = getParLanguage(bparams);
1563 Language const * doc_language = bparams.language;
1564 Language const * previous_language = previous_
1565 ? previous_->getParLanguage(bparams) : doc_language;
1566 if (language->babel() != previous_language->babel()) {
1568 if (!lyxrc.language_command_end.empty() &&
1569 previous_language->babel() != doc_language->babel()) {
1570 os << subst(lyxrc.language_command_end, "$$lang",
1571 previous_language->babel())
1576 if (lyxrc.language_command_end.empty() ||
1577 language->babel() != doc_language->babel()) {
1578 os << subst(lyxrc.language_command_begin, "$$lang",
1585 bool leftindent_open = false;
1586 if (!params().leftIndent().zero()) {
1587 os << "\\begin{LyXParagraphLeftIndent}{" <<
1588 params().leftIndent().asLatexString() << "}\n";
1590 leftindent_open = true;
1593 if (style->isEnvironment()) {
1594 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1595 os << "\\begin{" << style->latexname() << "}{"
1596 << params().labelWidthString() << "}\n";
1597 } else if (style->labeltype == LABEL_BIBLIO) {
1599 os << "\\begin{" << style->latexname() << "}{"
1600 << bibitemWidest(buf)
1602 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1603 os << "\\begin{" << style->latexname() << '}'
1604 << style->latexparam() << '\n';
1606 os << "\\begin{" << style->latexname() << '}'
1607 << style->latexparam() << '\n';
1610 Paragraph * par = this;
1612 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1614 if (par && par->params().depth() > params().depth()) {
1615 if (par->layout()->isParagraph()) {
1618 // How to handle this? (Lgb)
1619 //&& !suffixIs(os, "\n\n")
1621 // There should be at least one '\n' already
1622 // but we need there to be two for Standard
1623 // paragraphs that are depth-increment'ed to be
1624 // output correctly. However, tables can
1625 // also be paragraphs so don't adjust them.
1628 // Will it ever harm to have one '\n' too
1629 // many? i.e. that we sometimes will have
1630 // three in a row. (Lgb)
1634 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1637 && par->layout() == layout()
1638 && par->params().depth() == params().depth()
1639 && par->params().leftIndent() == params().leftIndent());
1641 if (style->isEnvironment()) {
1642 os << "\\end{" << style->latexname() << "}\n";
1646 if (leftindent_open) {
1647 os << "\\end{LyXParagraphLeftIndent}\n";
1651 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1652 return par; // ale970302
1656 bool Paragraph::isHfill(pos_type pos) const
1658 return IsHfillChar(getChar(pos));
1662 bool Paragraph::isInset(pos_type pos) const
1664 return IsInsetChar(getChar(pos));
1668 bool Paragraph::isNewline(pos_type pos) const
1670 return pos >= 0 && IsNewlineChar(getChar(pos));
1674 bool Paragraph::isSeparator(pos_type pos) const
1676 return IsSeparatorChar(getChar(pos));
1680 bool Paragraph::isLineSeparator(pos_type pos) const
1682 value_type const c = getChar(pos);
1683 return IsLineSeparatorChar(c)
1684 || (IsInsetChar(c) && getInset(pos) &&
1685 getInset(pos)->isLineSeparator());
1689 bool Paragraph::isKomma(pos_type pos) const
1691 return IsKommaChar(getChar(pos));
1695 /// Used by the spellchecker
1696 bool Paragraph::isLetter(pos_type pos) const
1698 value_type const c = getChar(pos);
1699 if (IsLetterChar(c))
1702 return getInset(pos)->isLetter();
1703 // We want to pass the ' and escape chars to ispell
1704 string const extra = lyxrc.isp_esc_chars + '\'';
1705 return contains(extra, c);
1709 bool Paragraph::isWord(pos_type pos) const
1711 return IsWordChar(getChar(pos)) ;
1716 Paragraph::getParLanguage(BufferParams const & bparams) const
1719 return getFirstFontSettings().language();
1720 } else if (previous_)
1721 return previous_->getParLanguage(bparams);
1723 return bparams.language;
1727 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1729 return lyxrc.rtl_support
1730 && getParLanguage(bparams)->RightToLeft()
1731 && !(inInset() && inInset()->owner() &&
1732 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1736 void Paragraph::changeLanguage(BufferParams const & bparams,
1737 Language const * from, Language const * to)
1739 for (pos_type i = 0; i < size(); ++i) {
1740 LyXFont font = getFontSettings(bparams, i);
1741 if (font.language() == from) {
1742 font.setLanguage(to);
1749 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1751 Language const * doc_language = bparams.language;
1752 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1753 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1755 for (; cit != end; ++cit)
1756 if (cit->font().language() != ignore_language &&
1757 cit->font().language() != latex_language &&
1758 cit->font().language() != doc_language)
1764 // Convert the paragraph to a string.
1765 // Used for building the table of contents
1766 string const Paragraph::asString(Buffer const * buffer, bool label) const
1769 if (label && !params().labelString().empty())
1770 s += params().labelString() + ' ';
1772 for (pos_type i = 0; i < size(); ++i) {
1773 value_type c = getChar(i);
1776 else if (c == META_INSET &&
1777 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1779 getInset(i)->ascii(buffer, ost);
1780 s += subst(STRCONV(ost.str()),'\n',' ');
1788 string const Paragraph::asString(Buffer const * buffer,
1789 pos_type beg, pos_type end, bool label) const
1793 if (beg == 0 && label && !params().labelString().empty())
1794 ost << params().labelString() << ' ';
1796 for (pos_type i = beg; i < end; ++i) {
1797 value_type const c = getUChar(buffer->params, i);
1800 else if (c == META_NEWLINE)
1802 else if (c == META_HFILL)
1804 else if (c == META_INSET) {
1805 getInset(i)->ascii(buffer, ost);
1809 return STRCONV(ost.str());
1813 void Paragraph::setInsetOwner(Inset * i)
1815 pimpl_->inset_owner = i;
1816 InsetList::iterator it = insetlist.begin();
1817 InsetList::iterator end = insetlist.end();
1818 for (; it != end; ++it) {
1820 it.getInset()->setOwner(i);
1825 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1828 insetlist.deleteInsetsLyXText(bv);
1832 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1835 insetlist.resizeInsetsLyXText(bv);
1839 void Paragraph::setContentsFromPar(Paragraph * par)
1841 pimpl_->setContentsFromPar(par);
1845 void Paragraph::trackChanges(Change::Type type)
1847 pimpl_->trackChanges(type);
1851 void Paragraph::untrackChanges()
1853 pimpl_->untrackChanges();
1857 void Paragraph::cleanChanges()
1859 pimpl_->cleanChanges();
1863 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1865 lyx::Assert(!size() || pos < size());
1866 return pimpl_->lookupChange(pos);
1870 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1872 lyx::Assert(!size() || pos < size());
1873 return pimpl_->lookupChangeFull(pos);
1877 bool Paragraph::isChanged(pos_type start, pos_type end) const
1879 return pimpl_->isChanged(start, end);
1883 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1885 return pimpl_->isChangeEdited(start, end);
1889 void Paragraph::markErased()
1891 pimpl_->markErased();
1895 void Paragraph::acceptChange(pos_type start, pos_type end)
1897 return pimpl_->acceptChange(start, end);
1901 void Paragraph::rejectChange(pos_type start, pos_type end)
1903 return pimpl_->rejectChange(start, end);
1907 lyx::pos_type Paragraph::size() const
1909 return pimpl_->size();
1913 bool Paragraph::empty() const
1915 return pimpl_->empty();
1919 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1921 return pimpl_->getChar(pos);
1925 int Paragraph::id() const
1931 LyXLayout_ptr const & Paragraph::layout() const
1937 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1939 layout_ = new_layout;
1943 bool Paragraph::isFirstInSequence() const
1945 Paragraph const * dhook = depthHook(getDepth());
1946 return (dhook == this
1947 || dhook->layout() != layout()
1948 || dhook->getDepth() != getDepth());
1952 Inset * Paragraph::inInset() const
1954 return pimpl_->inset_owner;
1958 void Paragraph::clearContents()
1963 void Paragraph::setChar(pos_type pos, value_type c)
1965 pimpl_->setChar(pos, c);
1969 ParagraphParameters & Paragraph::params()
1971 return pimpl_->params;
1975 ParagraphParameters const & Paragraph::params() const
1977 return pimpl_->params;
1981 Paragraph * Paragraph::getParFromID(int id) const
1983 return pimpl_->getParFromID(id);
1987 bool Paragraph::isFreeSpacing() const
1989 // for now we just need this, later should we need this in some
1990 // other way we can always add a function to Inset::() too.
1991 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1992 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);