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/insetbibitem.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))
86 // This constructor inserts the new paragraph in a list.
87 Paragraph::Paragraph(Paragraph * par)
88 : pimpl_(new Paragraph::Pimpl(this))
93 // double linked list begin
96 next_->previous_ = this;
98 previous_->next_ = this;
106 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
107 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
115 // this is because of the dummy layout of the paragraphs that
117 layout_ = lp.layout();
119 // copy everything behind the break-position to the new paragraph
120 insetlist = lp.insetlist;
121 InsetList::iterator it = insetlist.begin();
122 InsetList::iterator end = insetlist.end();
123 for (; it != end; ++it) {
124 it.setInset(it.getInset()->clone(*current_view->buffer(),
126 // tell the new inset who is the boss now
127 it.getInset()->parOwner(this);
132 // the destructor removes the new paragraph from the list
133 Paragraph::~Paragraph()
137 previous_->next_ = next_;
139 next_->previous_ = previous_;
144 //lyxerr << "Paragraph::paragraph_id = "
145 // << Paragraph::paragraph_id << endl;
149 void Paragraph::write(Buffer const * buf, ostream & os,
150 BufferParams const & bparams,
151 depth_type & dth) const
153 // The beginning or end of a deeper (i.e. nested) area?
154 if (dth != params().depth()) {
155 if (params().depth() > dth) {
156 while (params().depth() > dth) {
157 os << "\n\\begin_deeper ";
161 while (params().depth() < dth) {
162 os << "\n\\end_deeper ";
168 // First write the layout
169 os << "\n\\layout " << layout()->name() << '\n';
171 // Maybe some vertical spaces.
172 if (params().spaceTop().kind() != VSpace::NONE)
173 os << "\\added_space_top "
174 << params().spaceTop().asLyXCommand() << ' ';
175 if (params().spaceBottom().kind() != VSpace::NONE)
176 os << "\\added_space_bottom "
177 << params().spaceBottom().asLyXCommand() << ' ';
179 // Maybe the paragraph has special spacing
180 params().spacing().writeFile(os, true);
182 // The labelwidth string used in lists.
183 if (!params().labelWidthString().empty())
184 os << "\\labelwidthstring "
185 << params().labelWidthString() << '\n';
187 // Lines above or below?
188 if (params().lineTop())
190 if (params().lineBottom())
191 os << "\\line_bottom ";
193 // Pagebreaks above or below?
194 if (params().pagebreakTop())
195 os << "\\pagebreak_top ";
196 if (params().pagebreakBottom())
197 os << "\\pagebreak_bottom ";
199 // Start of appendix?
200 if (params().startOfAppendix())
201 os << "\\start_of_appendix ";
204 if (params().noindent())
207 // Do we have a manual left indent?
208 if (!params().leftIndent().zero())
209 os << "\\leftindent " << params().leftIndent().asString()
213 if (params().align() != LYX_ALIGN_LAYOUT) {
215 switch (params().align()) {
216 case LYX_ALIGN_LEFT: h = 1; break;
217 case LYX_ALIGN_RIGHT: h = 2; break;
218 case LYX_ALIGN_CENTER: h = 3; break;
219 default: h = 0; break;
221 os << "\\align " << string_align[h] << ' ';
224 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
226 Change running_change = Change(Change::UNCHANGED);
227 lyx::time_type const curtime(lyx::current_time());
230 for (pos_type i = 0; i < size(); ++i) {
236 Change change = pimpl_->lookupChangeFull(i);
237 Changes::lyxMarkChange(os, column, curtime, running_change, change);
238 running_change = change;
240 // Write font changes
241 LyXFont font2 = getFontSettings(bparams, i);
242 if (font2 != font1) {
243 font2.lyxWriteChanges(font1, os);
248 value_type const c = getChar(i);
252 Inset const * inset = getInset(i);
254 if (inset->directWrite()) {
255 // international char, let it write
256 // code directly so it's shorter in
258 inset->write(buf, os);
260 os << "\n\\begin_inset ";
261 inset->write(buf, os);
262 os << "\n\\end_inset \n\n";
268 os << "\n\\newline \n";
272 os << "\n\\hfill \n";
276 os << "\n\\backslash \n";
280 if (i + 1 < size() && getChar(i + 1) == ' ') {
287 if ((column > 70 && c == ' ')
292 // this check is to amend a bug. LyX sometimes
293 // inserts '\0' this could cause problems.
297 lyxerr << "ERROR (Paragraph::writeFile):"
298 " NULL char in structure." << endl;
304 // to make reading work properly
306 running_change = pimpl_->lookupChange(0);
307 Changes::lyxMarkChange(os, column, curtime,
308 Change(Change::UNCHANGED), running_change);
310 Changes::lyxMarkChange(os, column, curtime,
311 running_change, Change(Change::UNCHANGED));
315 void Paragraph::validate(LaTeXFeatures & features) const
317 pimpl_->validate(features, *layout());
321 // First few functions needed for cut and paste and paragraph breaking.
322 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
324 BufferParams bparams = buffer.params;
326 minibuffer_char = getChar(pos);
327 minibuffer_font = getFontSettings(bparams, pos);
328 minibuffer_inset = 0;
329 if (minibuffer_char == Paragraph::META_INSET) {
331 minibuffer_inset = getInset(pos)->clone(buffer);
333 minibuffer_inset = 0;
334 minibuffer_char = ' ';
335 // This reflects what GetInset() does (ARRae)
341 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
343 minibuffer_char = getChar(pos);
344 minibuffer_font = getFontSettings(bparams, pos);
345 minibuffer_inset = 0;
346 if (minibuffer_char == Paragraph::META_INSET) {
348 // the inset is not in a paragraph anymore
349 minibuffer_inset = insetlist.release(pos);
350 minibuffer_inset->parOwner(0);
352 minibuffer_inset = 0;
353 minibuffer_char = ' ';
354 // This reflects what GetInset() does (ARRae)
359 // Erase(pos); now the caller is responsible for that.
363 bool Paragraph::insertFromMinibuffer(pos_type pos)
365 if (minibuffer_char == Paragraph::META_INSET) {
366 if (!insetAllowed(minibuffer_inset->lyxCode())) {
369 insertInset(pos, minibuffer_inset, minibuffer_font);
371 LyXFont f = minibuffer_font;
372 if (!checkInsertChar(f)) {
375 insertChar(pos, minibuffer_char, f);
383 void Paragraph::erase(pos_type pos)
389 bool Paragraph::erase(pos_type start, pos_type end)
391 return pimpl_->erase(start, end);
395 bool Paragraph::checkInsertChar(LyXFont & font)
397 if (pimpl_->inset_owner)
398 return pimpl_->inset_owner->checkInsertChar(font);
403 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
405 LyXFont const f(LyXFont::ALL_INHERIT);
406 insertChar(pos, c, f);
410 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
411 LyXFont const & font, Change change)
413 pimpl_->insertChar(pos, c, font, change);
417 void Paragraph::insertInset(pos_type pos, Inset * inset)
419 LyXFont const f(LyXFont::ALL_INHERIT);
420 insertInset(pos, inset, f);
424 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font, Change change)
426 pimpl_->insertInset(pos, inset, font, change);
430 bool Paragraph::insetAllowed(Inset::Code code)
432 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
434 if (pimpl_->inset_owner)
435 return pimpl_->inset_owner->insetAllowed(code);
440 Inset * Paragraph::getInset(pos_type pos)
442 lyx::Assert(pos < size());
444 return insetlist.get(pos);
448 Inset const * Paragraph::getInset(pos_type pos) const
450 lyx::Assert(pos < size());
452 return insetlist.get(pos);
456 // Gets uninstantiated font setting at position.
457 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
460 lyx::Assert(pos <= size());
462 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
463 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
464 for (; cit != end; ++cit) {
465 if (cit->pos() >= pos)
471 retfont = cit->font();
472 } else if (pos == size() && !empty()) {
473 retfont = getFontSettings(bparams, pos - 1);
475 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
481 // Gets uninstantiated font setting at position 0
482 LyXFont const Paragraph::getFirstFontSettings() const
484 if (!empty() && !pimpl_->fontlist.empty())
485 return pimpl_->fontlist[0].font();
487 return LyXFont(LyXFont::ALL_INHERIT);
491 // Gets the fully instantiated font at a given position in a paragraph
492 // This is basically the same function as LyXText::GetFont() in text2.C.
493 // The difference is that this one is used for generating the LaTeX file,
494 // and thus cosmetic "improvements" are disallowed: This has to deliver
495 // the true picture of the buffer. (Asger)
496 // If position is -1, we get the layout font of the paragraph.
497 // If position is -2, we get the font of the manual label of the paragraph.
498 LyXFont const Paragraph::getFont(BufferParams const & bparams,
501 lyx::Assert(pos >= 0);
503 LyXLayout_ptr const & lout = layout();
505 pos_type const main_body = beginningOfMainBody();
509 layoutfont = lout->labelfont;
511 layoutfont = lout->font;
513 LyXFont tmpfont = getFontSettings(bparams, pos);
514 tmpfont.realize(layoutfont);
516 return pimpl_->realizeFont(tmpfont, bparams);
520 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
522 LyXLayout_ptr const & lout = layout();
524 LyXFont tmpfont = lout->labelfont;
525 tmpfont.setLanguage(getParLanguage(bparams));
527 return pimpl_->realizeFont(tmpfont, bparams);
531 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
533 LyXLayout_ptr const & lout = layout();
535 LyXFont tmpfont = lout->font;
536 tmpfont.setLanguage(getParLanguage(bparams));
538 return pimpl_->realizeFont(tmpfont, bparams);
542 /// Returns the height of the highest font in range
544 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
545 LyXFont::FONT_SIZE const def_size) const
547 if (pimpl_->fontlist.empty())
550 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
551 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
552 for (; end_it != end; ++end_it) {
553 if (end_it->pos() >= endpos)
560 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
561 for (; cit != end; ++cit) {
562 if (cit->pos() >= startpos)
566 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
567 for (; cit != end_it; ++cit) {
568 LyXFont::FONT_SIZE size = cit->font().size();
569 if (size == LyXFont::INHERIT_SIZE)
571 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
578 Paragraph::value_type
579 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
581 value_type c = getChar(pos);
582 if (!lyxrc.rtl_support)
612 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
619 void Paragraph::setFont(pos_type pos, LyXFont const & font)
621 lyx::Assert(pos <= size());
623 // First, reduce font against layout/label font
624 // Update: The SetCharFont() routine in text2.C already
625 // reduces font, so we don't need to do that here. (Asger)
626 // No need to simplify this because it will disappear
627 // in a new kernel. (Asger)
628 // Next search font table
630 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
631 Pimpl::FontList::iterator it = beg;
632 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
633 for (; it != endit; ++it) {
634 if (it->pos() >= pos)
637 unsigned int i = std::distance(beg, it);
638 bool notfound = (it == endit);
640 if (!notfound && pimpl_->fontlist[i].font() == font)
643 bool begin = pos == 0 || notfound ||
644 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
645 // Is position pos is a beginning of a font block?
646 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
647 // Is position pos is the end of a font block?
648 if (begin && end) { // A single char block
649 if (i + 1 < pimpl_->fontlist.size() &&
650 pimpl_->fontlist[i + 1].font() == font) {
651 // Merge the singleton block with the next block
652 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
653 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
654 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
655 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
656 // Merge the singleton block with the previous block
657 pimpl_->fontlist[i - 1].pos(pos);
658 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
660 pimpl_->fontlist[i].font(font);
662 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
663 pimpl_->fontlist[i - 1].pos(pos);
665 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
666 Pimpl::FontTable(pos, font));
668 pimpl_->fontlist[i].pos(pos - 1);
669 if (!(i + 1 < pimpl_->fontlist.size() &&
670 pimpl_->fontlist[i + 1].font() == font))
671 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
672 Pimpl::FontTable(pos, font));
673 } else { // The general case. The block is splitted into 3 blocks
674 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
675 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
676 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
677 Pimpl::FontTable(pos, font));
683 void Paragraph::next(Paragraph * p)
689 // This function is able to hide closed footnotes.
690 Paragraph * Paragraph::next()
696 Paragraph const * Paragraph::next() const
702 void Paragraph::previous(Paragraph * p)
708 // This function is able to hide closed footnotes.
709 Paragraph * Paragraph::previous()
715 // This function is able to hide closed footnotes.
716 Paragraph const * Paragraph::previous() const
723 void Paragraph::makeSameLayout(Paragraph const * par)
725 layout(par->layout());
727 params() = par->params();
731 int Paragraph::stripLeadingSpaces()
733 if (layout()->free_spacing ||
739 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
740 pimpl_->eraseIntern(0);
748 bool Paragraph::hasSameLayout(Paragraph const * par) const
751 par->layout() == layout() &&
752 params().sameLayout(par->params());
756 int Paragraph::getEndLabel() const
758 Paragraph const * par = this;
759 depth_type par_depth = getDepth();
761 LyXLayout_ptr const & layout = par->layout();
762 int const endlabeltype = layout->endlabeltype;
764 if (endlabeltype != END_LABEL_NO_LABEL) {
768 depth_type const next_depth = next_->getDepth();
769 if (par_depth > next_depth ||
770 (par_depth == next_depth
771 && layout != next_->layout()))
777 par = par->outerHook();
779 par_depth = par->getDepth();
781 return END_LABEL_NO_LABEL;
785 Paragraph::depth_type Paragraph::getDepth() const
787 return params().depth();
791 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
793 bool const isenv = layout()->isEnvironment();
796 return params().depth() + 1;
798 return params().depth();
802 char Paragraph::getAlign() const
804 return params().align();
808 string const & Paragraph::getLabelstring() const
810 return params().labelString();
814 // the next two functions are for the manual labels
815 string const Paragraph::getLabelWidthString() const
817 if (!params().labelWidthString().empty())
818 return params().labelWidthString();
820 return _("Senseless with this layout!");
824 void Paragraph::setLabelWidthString(string const & s)
826 params().labelWidthString(s);
830 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
833 params().labelWidthString(string());
834 params().align(LYX_ALIGN_LAYOUT);
835 params().spaceTop(VSpace(VSpace::NONE));
836 params().spaceBottom(VSpace(VSpace::NONE));
837 params().spacing(Spacing(Spacing::Default));
841 // if the layout of a paragraph contains a manual label, the beginning of the
842 // main body is the beginning of the second word. This is what the par-
843 // function returns. If the layout does not contain a label, the main
844 // body always starts with position 0. This differentiation is necessary,
845 // because there cannot be a newline or a blank <= the beginning of the
848 int Paragraph::beginningOfMainBody() const
850 if (layout()->labeltype != LABEL_MANUAL)
853 // Unroll the first two cycles of the loop
854 // and remember the previous character to
855 // remove unnecessary GetChar() calls
857 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
859 char previous_char = 0;
862 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
863 // Yes, this ^ is supposed to be "= " not "=="
866 && previous_char != ' '
867 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
869 previous_char = temp;
878 Paragraph * Paragraph::depthHook(depth_type depth)
880 Paragraph * newpar = this;
883 newpar = newpar->previous();
884 } while (newpar && newpar->getDepth() > depth);
887 if (previous() || getDepth())
888 lyxerr << "ERROR (Paragraph::DepthHook): "
897 Paragraph const * Paragraph::depthHook(depth_type depth) const
899 Paragraph const * newpar = this;
902 newpar = newpar->previous();
903 } while (newpar && newpar->getDepth() > depth);
906 if (previous() || getDepth())
907 lyxerr << "ERROR (Paragraph::DepthHook): "
916 Paragraph * Paragraph::outerHook()
920 return depthHook(depth_type(getDepth() - 1));
924 Paragraph const * Paragraph::outerHook() const
928 return depthHook(depth_type(getDepth() - 1));
932 // returns -1 if inset not found
933 int Paragraph::getPositionOfInset(Inset const * inset) const
936 InsetList::iterator it = insetlist.begin();
937 InsetList::iterator end = insetlist.end();
938 for (; it != end; ++it)
939 if (it.getInset() == inset)
945 InsetBibitem * Paragraph::bibitem()
947 InsetList::iterator it = insetlist.begin();
948 if (it != insetlist.end() && it.getInset()->lyxCode() == Inset::BIBTEX_CODE)
949 return static_cast<InsetBibitem *>(it.getInset());
955 InsetOptArg * optArgInset(Paragraph const & par)
958 InsetList::iterator it = par.insetlist.begin();
959 InsetList::iterator end = par.insetlist.end();
960 for (; it != end; ++it) {
961 Inset * ins = it.getInset();
962 if (ins->lyxCode() == Inset::OPTARG_CODE) {
963 return static_cast<InsetOptArg *>(ins);
971 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
972 BufferParams const & bparams,
973 ostream & os, TexRow & texrow,
976 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
977 Inset const * in = inInset();
978 bool further_blank_line = false;
981 // well we have to check if we are in an inset with unlimited
982 // lenght (all in one row) if that is true then we don't allow
983 // any special options in the paragraph and also we don't allow
984 // any environment other then "Standard" to be valid!
985 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
988 if (params().startOfAppendix()) {
989 os << "\\appendix\n";
993 if (!params().spacing().isDefault()
994 && (!previous() || !previous()->hasSameLayout(this))) {
995 os << params().spacing().writeEnvirBegin() << '\n';
999 if (style->isCommand()) {
1004 if (params().pagebreakTop()) {
1006 further_blank_line = true;
1008 if (params().spaceTop().kind() != VSpace::NONE) {
1009 os << params().spaceTop().asLatexCommand(bparams);
1010 further_blank_line = true;
1013 if (params().lineTop()) {
1014 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1015 << "\\vspace{-1\\parskip}";
1016 further_blank_line = true;
1019 if (further_blank_line) {
1024 style = bparams.getLyXTextClass().defaultLayout();
1027 Language const * language = getParLanguage(bparams);
1028 Language const * doc_language = bparams.language;
1029 Language const * previous_language = previous()
1030 ? previous()->getParLanguage(bparams) : doc_language;
1032 if (language->babel() != previous_language->babel()
1033 // check if we already put language command in TeXEnvironment()
1034 && !(style->isEnvironment()
1036 (previous()->layout() != layout() &&
1037 previous()->getDepth() <= getDepth())
1038 || previous()->getDepth() < getDepth())))
1040 if (!lyxrc.language_command_end.empty() &&
1041 previous_language->babel() != doc_language->babel())
1043 os << subst(lyxrc.language_command_end, "$$lang",
1044 previous_language->babel())
1049 if (lyxrc.language_command_end.empty() ||
1050 language->babel() != doc_language->babel())
1052 os << subst(lyxrc.language_command_begin, "$$lang",
1059 if (bparams.inputenc == "auto" &&
1060 language->encoding() != previous_language->encoding()) {
1061 os << "\\inputencoding{"
1062 << language->encoding()->LatexName()
1067 switch (style->latextype) {
1069 os << '\\' << style->latexname();
1071 // Separate handling of optional argument inset.
1072 if (style->optionalargs == 1) {
1073 InsetOptArg * it = optArgInset(*this);
1075 it->latexOptional(buf, os, false, false);
1078 os << style->latexparam();
1080 case LATEX_ITEM_ENVIRONMENT:
1081 case LATEX_LIST_ENVIRONMENT:
1084 case LATEX_BIB_ENVIRONMENT:
1085 // ignore this, the inset will write itself
1091 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1093 // Make sure that \\par is done with the font of the last
1094 // character if this has another size as the default.
1095 // This is necessary because LaTeX (and LyX on the screen)
1096 // calculates the space between the baselines according
1097 // to this font. (Matthias)
1099 // Is this really needed ? (Dekel)
1100 // We do not need to use to change the font for the last paragraph
1101 // or for a command.
1102 LyXFont const font =
1104 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1106 bool is_command = style->isCommand();
1108 if (style->resfont.size() != font.size() && next_ && !is_command) {
1111 os << "\\" << font.latexSize() << " \\par}";
1112 } else if (need_par) {
1114 } else if (is_command)
1117 switch (style->latextype) {
1118 case LATEX_ITEM_ENVIRONMENT:
1119 case LATEX_LIST_ENVIRONMENT:
1120 if (next_ && (params().depth() < next_->params().depth())) {
1125 case LATEX_ENVIRONMENT:
1126 // if its the last paragraph of the current environment
1127 // skip it otherwise fall through
1129 && (next_->layout() != layout()
1130 || next_->params().depth() != params().depth()))
1132 // fall through possible
1134 // we don't need it for the last paragraph!!!
1141 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1142 further_blank_line = false;
1143 if (params().lineBottom()) {
1144 os << "\\lyxline{\\" << font.latexSize() << '}';
1145 further_blank_line = true;
1148 if (params().spaceBottom().kind() != VSpace::NONE) {
1149 os << params().spaceBottom().asLatexCommand(bparams);
1150 further_blank_line = true;
1153 if (params().pagebreakBottom()) {
1155 further_blank_line = true;
1158 if (further_blank_line) {
1163 if (!params().spacing().isDefault()
1164 && (!next_ || !next_->hasSameLayout(this))) {
1165 os << params().spacing().writeEnvirEnd() << '\n';
1170 // we don't need it for the last paragraph!!!
1175 // Since \selectlanguage write the language to the aux file,
1176 // we need to reset the language at the end of footnote or
1179 if (language->babel() != doc_language->babel()) {
1180 if (lyxrc.language_command_end.empty())
1181 os << subst(lyxrc.language_command_begin,
1183 doc_language->babel())
1186 os << subst(lyxrc.language_command_end,
1194 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1199 // This could go to ParagraphParameters if we want to
1200 int Paragraph::startTeXParParams(BufferParams const & bparams,
1201 ostream & os, bool moving_arg) const
1205 if (params().noindent()) {
1206 os << "\\noindent ";
1210 switch (params().align()) {
1211 case LYX_ALIGN_NONE:
1212 case LYX_ALIGN_BLOCK:
1213 case LYX_ALIGN_LAYOUT:
1214 case LYX_ALIGN_SPECIAL:
1216 case LYX_ALIGN_LEFT:
1217 case LYX_ALIGN_RIGHT:
1218 case LYX_ALIGN_CENTER:
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 if (getParLanguage(bparams)->babel() != "hebrew") {
1234 os << "\\begin{flushleft}";
1237 os << "\\begin{flushright}";
1241 case LYX_ALIGN_RIGHT:
1242 if (getParLanguage(bparams)->babel() != "hebrew") {
1243 os << "\\begin{flushright}";
1246 os << "\\begin{flushleft}";
1250 case LYX_ALIGN_CENTER:
1251 os << "\\begin{center}";
1260 // This could go to ParagraphParameters if we want to
1261 int Paragraph::endTeXParParams(BufferParams const & bparams,
1262 ostream & os, bool moving_arg) const
1266 switch (params().align()) {
1267 case LYX_ALIGN_NONE:
1268 case LYX_ALIGN_BLOCK:
1269 case LYX_ALIGN_LAYOUT:
1270 case LYX_ALIGN_SPECIAL:
1272 case LYX_ALIGN_LEFT:
1273 case LYX_ALIGN_RIGHT:
1274 case LYX_ALIGN_CENTER:
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 if (getParLanguage(bparams)->babel() != "hebrew") {
1290 os << "\\end{flushleft}";
1293 os << "\\end{flushright}";
1297 case LYX_ALIGN_RIGHT:
1298 if (getParLanguage(bparams)->babel() != "hebrew") {
1299 os << "\\end{flushright}";
1302 os << "\\end{flushleft}";
1306 case LYX_ALIGN_CENTER:
1307 os << "\\end{center}";
1315 // This one spits out the text of the paragraph
1316 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1317 BufferParams const & bparams,
1318 ostream & os, TexRow & texrow,
1321 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1323 bool return_value = false;
1325 LyXLayout_ptr style;
1327 // well we have to check if we are in an inset with unlimited
1328 // lenght (all in one row) if that is true then we don't allow
1329 // any special options in the paragraph and also we don't allow
1330 // any environment other then "Standard" to be valid!
1332 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1335 style = bparams.getLyXTextClass().defaultLayout();
1342 // Maybe we have to create a optional argument.
1345 // FIXME: can we actually skip this check and just call
1346 // beginningOfMainBody() ??
1347 if (style->labeltype != LABEL_MANUAL) {
1350 main_body = beginningOfMainBody();
1353 unsigned int column = 0;
1355 if (main_body > 0) {
1358 basefont = getLabelFont(bparams);
1360 basefont = getLayoutFont(bparams);
1363 moving_arg |= style->needprotect;
1365 // Which font is currently active?
1366 LyXFont running_font(basefont);
1367 // Do we have an open font change?
1368 bool open_font = false;
1370 Change::Type running_change = Change::UNCHANGED;
1372 texrow.start(this, 0);
1374 // if the paragraph is empty, the loop will not be entered at all
1376 if (style->isCommand()) {
1381 column += startTeXParParams(bparams, os, moving_arg);
1385 for (pos_type i = 0; i < size(); ++i) {
1387 // First char in paragraph or after label?
1388 if (i == main_body) {
1389 if (main_body > 0) {
1391 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1394 basefont = getLayoutFont(bparams);
1395 running_font = basefont;
1399 if (style->isCommand()) {
1405 column += startTeXParParams(bparams, os,
1409 value_type c = getChar(i);
1411 // Fully instantiated font
1412 LyXFont font = getFont(bparams, i);
1414 LyXFont const last_font = running_font;
1416 // Spaces at end of font change are simulated to be
1417 // outside font change, i.e. we write "\textXX{text} "
1418 // rather than "\textXX{text }". (Asger)
1419 if (open_font && c == ' ' && i <= size() - 2) {
1420 LyXFont const & next_font = getFont(bparams, i + 1);
1421 if (next_font != running_font
1422 && next_font != font) {
1427 // We end font definition before blanks
1429 (font != running_font ||
1430 font.language() != running_font.language()))
1432 column += running_font.latexWriteEndChanges(os,
1434 (i == main_body-1) ? basefont : font);
1435 running_font = basefont;
1439 // Blanks are printed before start of fontswitch
1441 // Do not print the separation of the optional argument
1442 if (i != main_body - 1) {
1443 pimpl_->simpleTeXBlanks(os, texrow, i,
1444 column, font, *style);
1448 // Do we need to change font?
1449 if ((font != running_font ||
1450 font.language() != running_font.language()) &&
1453 column += font.latexWriteStartChanges(os, basefont,
1455 running_font = font;
1459 Change::Type change = pimpl_->lookupChange(i);
1461 column += Changes::latexMarkChange(os, running_change, change);
1462 running_change = change;
1464 if (c == Paragraph::META_NEWLINE) {
1465 // newlines are handled differently here than
1466 // the default in SimpleTeXSpecialChars().
1467 if (!style->newline_allowed) {
1471 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1474 basefont = getLayoutFont(bparams);
1475 running_font = basefont;
1476 if (font.family() ==
1477 LyXFont::TYPEWRITER_FAMILY) {
1486 texrow.start(this, i + 1);
1489 pimpl_->simpleTeXSpecialChars(buf, bparams,
1490 os, texrow, moving_arg,
1492 basefont, open_font,
1494 *style, i, column, c);
1498 column += Changes::latexMarkChange(os,
1499 running_change, Change::UNCHANGED);
1501 // If we have an open font definition, we have to close it
1503 #ifdef FIXED_LANGUAGE_END_DETECTION
1506 .latexWriteEndChanges(os, basefont,
1507 next_->getFont(bparams,
1510 running_font.latexWriteEndChanges(os, basefont,
1514 #ifdef WITH_WARNINGS
1515 //#warning For now we ALWAYS have to close the foreign font settings if they are
1516 //#warning there as we start another \selectlanguage with the next paragraph if
1517 //#warning we are in need of this. This should be fixed sometime (Jug)
1519 running_font.latexWriteEndChanges(os, basefont, basefont);
1523 // Needed if there is an optional argument but no contents.
1524 if (main_body > 0 && main_body == size()) {
1526 return_value = false;
1530 column += endTeXParParams(bparams, os, moving_arg);
1533 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1534 return return_value;
1538 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1539 BufferParams const & bparams,
1540 ostream & os, TexRow & texrow)
1542 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1544 LyXLayout_ptr const & style = layout();
1546 Language const * language = getParLanguage(bparams);
1547 Language const * doc_language = bparams.language;
1548 Language const * previous_language = previous_
1549 ? previous_->getParLanguage(bparams) : doc_language;
1550 if (language->babel() != previous_language->babel()) {
1552 if (!lyxrc.language_command_end.empty() &&
1553 previous_language->babel() != doc_language->babel()) {
1554 os << subst(lyxrc.language_command_end, "$$lang",
1555 previous_language->babel())
1560 if (lyxrc.language_command_end.empty() ||
1561 language->babel() != doc_language->babel()) {
1562 os << subst(lyxrc.language_command_begin, "$$lang",
1569 bool leftindent_open = false;
1570 if (!params().leftIndent().zero()) {
1571 os << "\\begin{LyXParagraphLeftIndent}{" <<
1572 params().leftIndent().asLatexString() << "}\n";
1574 leftindent_open = true;
1577 if (style->isEnvironment()) {
1578 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1579 os << "\\begin{" << style->latexname() << "}{"
1580 << params().labelWidthString() << "}\n";
1581 } else if (style->labeltype == LABEL_BIBLIO) {
1583 os << "\\begin{" << style->latexname() << "}{"
1584 << bibitemWidest(buf)
1586 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1587 os << "\\begin{" << style->latexname() << '}'
1588 << style->latexparam() << '\n';
1590 os << "\\begin{" << style->latexname() << '}'
1591 << style->latexparam() << '\n';
1594 Paragraph * par = this;
1596 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1598 if (par && par->params().depth() > params().depth()) {
1599 if (par->layout()->isParagraph()) {
1602 // How to handle this? (Lgb)
1603 //&& !suffixIs(os, "\n\n")
1605 // There should be at least one '\n' already
1606 // but we need there to be two for Standard
1607 // paragraphs that are depth-increment'ed to be
1608 // output correctly. However, tables can
1609 // also be paragraphs so don't adjust them.
1612 // Will it ever harm to have one '\n' too
1613 // many? i.e. that we sometimes will have
1614 // three in a row. (Lgb)
1618 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1621 && par->layout() == layout()
1622 && par->params().depth() == params().depth()
1623 && par->params().leftIndent() == params().leftIndent());
1625 if (style->isEnvironment()) {
1626 os << "\\end{" << style->latexname() << "}\n";
1630 if (leftindent_open) {
1631 os << "\\end{LyXParagraphLeftIndent}\n";
1635 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1636 return par; // ale970302
1640 bool Paragraph::isHfill(pos_type pos) const
1642 return IsHfillChar(getChar(pos));
1646 bool Paragraph::isInset(pos_type pos) const
1648 return IsInsetChar(getChar(pos));
1652 bool Paragraph::isNewline(pos_type pos) const
1654 return pos >= 0 && IsNewlineChar(getChar(pos));
1658 bool Paragraph::isSeparator(pos_type pos) const
1660 return IsSeparatorChar(getChar(pos));
1664 bool Paragraph::isLineSeparator(pos_type pos) const
1666 value_type const c = getChar(pos);
1667 return IsLineSeparatorChar(c)
1668 || (IsInsetChar(c) && getInset(pos) &&
1669 getInset(pos)->isLineSeparator());
1673 bool Paragraph::isKomma(pos_type pos) const
1675 return IsKommaChar(getChar(pos));
1679 /// Used by the spellchecker
1680 bool Paragraph::isLetter(pos_type pos) const
1682 value_type const c = getChar(pos);
1683 if (IsLetterChar(c))
1686 return getInset(pos)->isLetter();
1687 // We want to pass the ' and escape chars to ispell
1688 string const extra = lyxrc.isp_esc_chars + '\'';
1689 return contains(extra, c);
1693 bool Paragraph::isWord(pos_type pos) const
1695 return IsWordChar(getChar(pos)) ;
1700 Paragraph::getParLanguage(BufferParams const & bparams) const
1703 return getFirstFontSettings().language();
1704 } else if (previous_)
1705 return previous_->getParLanguage(bparams);
1707 return bparams.language;
1711 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1713 return lyxrc.rtl_support
1714 && getParLanguage(bparams)->RightToLeft()
1715 && !(inInset() && inInset()->owner() &&
1716 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1720 void Paragraph::changeLanguage(BufferParams const & bparams,
1721 Language const * from, Language const * to)
1723 for (pos_type i = 0; i < size(); ++i) {
1724 LyXFont font = getFontSettings(bparams, i);
1725 if (font.language() == from) {
1726 font.setLanguage(to);
1733 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1735 Language const * doc_language = bparams.language;
1736 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1737 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1739 for (; cit != end; ++cit)
1740 if (cit->font().language() != ignore_language &&
1741 cit->font().language() != latex_language &&
1742 cit->font().language() != doc_language)
1748 // Convert the paragraph to a string.
1749 // Used for building the table of contents
1750 string const Paragraph::asString(Buffer const * buffer, bool label) const
1753 if (label && !params().labelString().empty())
1754 s += params().labelString() + ' ';
1756 for (pos_type i = 0; i < size(); ++i) {
1757 value_type c = getChar(i);
1760 else if (c == META_INSET &&
1761 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1763 getInset(i)->ascii(buffer, ost);
1764 s += subst(STRCONV(ost.str()),'\n',' ');
1772 string const Paragraph::asString(Buffer const * buffer,
1773 pos_type beg, pos_type end, bool label) const
1777 if (beg == 0 && label && !params().labelString().empty())
1778 ost << params().labelString() << ' ';
1780 for (pos_type i = beg; i < end; ++i) {
1781 value_type const c = getUChar(buffer->params, i);
1784 else if (c == META_NEWLINE)
1786 else if (c == META_HFILL)
1788 else if (c == META_INSET) {
1789 getInset(i)->ascii(buffer, ost);
1793 return STRCONV(ost.str());
1797 void Paragraph::setInsetOwner(Inset * i)
1799 pimpl_->inset_owner = i;
1800 InsetList::iterator it = insetlist.begin();
1801 InsetList::iterator end = insetlist.end();
1802 for (; it != end; ++it) {
1804 it.getInset()->setOwner(i);
1809 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1812 insetlist.deleteInsetsLyXText(bv);
1816 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1819 insetlist.resizeInsetsLyXText(bv);
1823 void Paragraph::setContentsFromPar(Paragraph * par)
1825 pimpl_->setContentsFromPar(par);
1829 void Paragraph::trackChanges(Change::Type type)
1831 pimpl_->trackChanges(type);
1835 void Paragraph::untrackChanges()
1837 pimpl_->untrackChanges();
1841 void Paragraph::cleanChanges()
1843 pimpl_->cleanChanges();
1847 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1849 lyx::Assert(!size() || pos < size());
1850 return pimpl_->lookupChange(pos);
1854 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1856 lyx::Assert(!size() || pos < size());
1857 return pimpl_->lookupChangeFull(pos);
1861 bool Paragraph::isChanged(pos_type start, pos_type end) const
1863 return pimpl_->isChanged(start, end);
1867 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1869 return pimpl_->isChangeEdited(start, end);
1873 void Paragraph::markErased()
1875 pimpl_->markErased();
1879 void Paragraph::acceptChange(pos_type start, pos_type end)
1881 return pimpl_->acceptChange(start, end);
1885 void Paragraph::rejectChange(pos_type start, pos_type end)
1887 return pimpl_->rejectChange(start, end);
1891 lyx::pos_type Paragraph::size() const
1893 return pimpl_->size();
1897 bool Paragraph::empty() const
1899 return pimpl_->empty();
1903 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1905 return pimpl_->getChar(pos);
1909 int Paragraph::id() const
1915 LyXLayout_ptr const & Paragraph::layout() const
1921 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1923 layout_ = new_layout;
1927 bool Paragraph::isFirstInSequence() const
1929 Paragraph const * dhook = depthHook(getDepth());
1930 return (dhook == this
1931 || dhook->layout() != layout()
1932 || dhook->getDepth() != getDepth());
1936 Inset * Paragraph::inInset() const
1938 return pimpl_->inset_owner;
1942 void Paragraph::clearContents()
1947 void Paragraph::setChar(pos_type pos, value_type c)
1949 pimpl_->setChar(pos, c);
1953 ParagraphParameters & Paragraph::params()
1955 return pimpl_->params;
1959 ParagraphParameters const & Paragraph::params() const
1961 return pimpl_->params;
1965 bool Paragraph::isFreeSpacing() const
1967 // for now we just need this, later should we need this in some
1968 // other way we can always add a function to Inset::() too.
1969 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1970 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);