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
17 #include "paragraph.h"
18 #include "paragraph_pimpl.h"
22 #include "tex-strings.h"
24 #include "bufferparams.h"
27 #include "BufferView.h"
29 #include "ParameterStruct.h"
33 #include "insets/insetbib.h"
34 #include "insets/insetoptarg.h"
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
38 #include "support/lyxmanip.h"
39 #include "support/FileInfo.h"
40 #include "support/LAssert.h"
41 #include "support/textutils.h"
52 using std::lower_bound;
53 using std::upper_bound;
57 // this is a bad idea, but how can Paragraph find its buffer to get
58 // parameters? (JMarc)
60 extern string bibitemWidest(Buffer const *);
62 // this is a minibuffer
67 LyXFont minibuffer_font;
68 Inset * minibuffer_inset;
73 extern BufferView * current_view;
76 Paragraph::Paragraph()
77 : pimpl_(new Paragraph::Pimpl(this))
85 bibkey = 0; // ale970302
91 // This constructor inserts the new paragraph in a list.
92 Paragraph::Paragraph(Paragraph * par)
93 : pimpl_(new Paragraph::Pimpl(this))
98 // double linked list begin
101 next_->previous_ = this;
103 previous_->next_ = this;
106 bibkey = 0; // ale970302
112 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
113 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
121 // this is because of the dummy layout of the paragraphs that
123 layout_ = lp.layout();
127 bibkey = static_cast<InsetBibKey *>
128 (lp.bibkey->clone(*current_view->buffer()));
133 // copy everything behind the break-position to the new paragraph
134 insetlist = lp.insetlist;
135 InsetList::iterator it = insetlist.begin();
136 InsetList::iterator end = insetlist.end();
137 for (; it != end; ++it) {
138 it.setInset(it.getInset()->clone(*current_view->buffer(),
140 // tell the new inset who is the boss now
141 it.getInset()->parOwner(this);
146 // the destructor removes the new paragraph from the list
147 Paragraph::~Paragraph()
151 previous_->next_ = next_;
153 next_->previous_ = previous_;
161 //lyxerr << "Paragraph::paragraph_id = "
162 // << Paragraph::paragraph_id << endl;
166 void Paragraph::write(Buffer const * buf, ostream & os,
167 BufferParams const & bparams,
168 depth_type & dth) const
170 // The beginning or end of a deeper (i.e. nested) area?
171 if (dth != params().depth()) {
172 if (params().depth() > dth) {
173 while (params().depth() > dth) {
174 os << "\n\\begin_deeper ";
178 while (params().depth() < dth) {
179 os << "\n\\end_deeper ";
185 // First write the layout
186 os << "\n\\layout " << layout()->name() << '\n';
188 // Maybe some vertical spaces.
189 if (params().spaceTop().kind() != VSpace::NONE)
190 os << "\\added_space_top "
191 << params().spaceTop().asLyXCommand() << ' ';
192 if (params().spaceBottom().kind() != VSpace::NONE)
193 os << "\\added_space_bottom "
194 << params().spaceBottom().asLyXCommand() << ' ';
196 // Maybe the paragraph has special spacing
197 params().spacing().writeFile(os, true);
199 // The labelwidth string used in lists.
200 if (!params().labelWidthString().empty())
201 os << "\\labelwidthstring "
202 << params().labelWidthString() << '\n';
204 // Lines above or below?
205 if (params().lineTop())
207 if (params().lineBottom())
208 os << "\\line_bottom ";
210 // Pagebreaks above or below?
211 if (params().pagebreakTop())
212 os << "\\pagebreak_top ";
213 if (params().pagebreakBottom())
214 os << "\\pagebreak_bottom ";
216 // Start of appendix?
217 if (params().startOfAppendix())
218 os << "\\start_of_appendix ";
221 if (params().noindent())
224 // Do we have a manual left indent?
225 if (!params().leftIndent().zero())
226 os << "\\leftindent " << params().leftIndent().asString()
230 if (params().align() != LYX_ALIGN_LAYOUT) {
232 switch (params().align()) {
233 case LYX_ALIGN_LEFT: h = 1; break;
234 case LYX_ALIGN_RIGHT: h = 2; break;
235 case LYX_ALIGN_CENTER: h = 3; break;
236 default: h = 0; break;
238 os << "\\align " << string_align[h] << ' ';
243 bibkey->write(buf, os);
245 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
247 Change running_change = Change(Change::UNCHANGED);
248 lyx::time_type const curtime(lyx::current_time());
251 for (pos_type i = 0; i < size(); ++i) {
257 Change change = pimpl_->lookupChangeFull(i);
258 Changes::lyxMarkChange(os, column, curtime, running_change, change);
259 running_change = change;
261 // Write font changes
262 LyXFont font2 = getFontSettings(bparams, i);
263 if (font2 != font1) {
264 font2.lyxWriteChanges(font1, os);
269 value_type const c = getChar(i);
273 Inset const * inset = getInset(i);
275 if (inset->directWrite()) {
276 // international char, let it write
277 // code directly so it's shorter in
279 inset->write(buf, os);
281 os << "\n\\begin_inset ";
282 inset->write(buf, os);
283 os << "\n\\end_inset \n\n";
289 os << "\n\\newline \n";
293 os << "\n\\hfill \n";
297 os << "\n\\backslash \n";
301 if (i + 1 < size() && getChar(i + 1) == ' ') {
308 if ((column > 70 && c == ' ')
313 // this check is to amend a bug. LyX sometimes
314 // inserts '\0' this could cause problems.
318 lyxerr << "ERROR (Paragraph::writeFile):"
319 " NULL char in structure." << endl;
325 // to make reading work properly
327 running_change = pimpl_->lookupChange(0);
328 Changes::lyxMarkChange(os, column, curtime,
329 Change(Change::UNCHANGED), running_change);
331 Changes::lyxMarkChange(os, column, curtime,
332 running_change, Change(Change::UNCHANGED));
336 void Paragraph::validate(LaTeXFeatures & features) const
338 pimpl_->validate(features, *layout());
342 // First few functions needed for cut and paste and paragraph breaking.
343 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
345 BufferParams bparams = buffer.params;
347 minibuffer_char = getChar(pos);
348 minibuffer_font = getFontSettings(bparams, pos);
349 minibuffer_inset = 0;
350 if (minibuffer_char == Paragraph::META_INSET) {
352 minibuffer_inset = getInset(pos)->clone(buffer);
354 minibuffer_inset = 0;
355 minibuffer_char = ' ';
356 // This reflects what GetInset() does (ARRae)
362 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
364 minibuffer_char = getChar(pos);
365 minibuffer_font = getFontSettings(bparams, pos);
366 minibuffer_inset = 0;
367 if (minibuffer_char == Paragraph::META_INSET) {
369 // the inset is not in a paragraph anymore
370 minibuffer_inset = insetlist.release(pos);
371 minibuffer_inset->parOwner(0);
373 minibuffer_inset = 0;
374 minibuffer_char = ' ';
375 // This reflects what GetInset() does (ARRae)
380 // Erase(pos); now the caller is responsible for that.
384 bool Paragraph::insertFromMinibuffer(pos_type pos)
386 if (minibuffer_char == Paragraph::META_INSET) {
387 if (!insetAllowed(minibuffer_inset->lyxCode())) {
390 insertInset(pos, minibuffer_inset, minibuffer_font);
392 LyXFont f = minibuffer_font;
393 if (!checkInsertChar(f)) {
396 insertChar(pos, minibuffer_char, f);
404 void Paragraph::erase(pos_type pos)
410 bool Paragraph::erase(pos_type start, pos_type end)
412 return pimpl_->erase(start, end);
416 bool Paragraph::checkInsertChar(LyXFont & font)
418 if (pimpl_->inset_owner)
419 return pimpl_->inset_owner->checkInsertChar(font);
424 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
426 LyXFont const f(LyXFont::ALL_INHERIT);
427 insertChar(pos, c, f);
431 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
432 LyXFont const & font, Change change)
434 pimpl_->insertChar(pos, c, font, change);
438 void Paragraph::insertInset(pos_type pos, Inset * inset)
440 LyXFont const f(LyXFont::ALL_INHERIT);
441 insertInset(pos, inset, f);
445 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font, Change change)
447 pimpl_->insertInset(pos, inset, font, change);
451 bool Paragraph::insetAllowed(Inset::Code code)
453 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
455 if (pimpl_->inset_owner)
456 return pimpl_->inset_owner->insetAllowed(code);
461 Inset * Paragraph::getInset(pos_type pos)
463 lyx::Assert(pos < size());
465 return insetlist.get(pos);
469 Inset const * Paragraph::getInset(pos_type pos) const
471 lyx::Assert(pos < size());
473 return insetlist.get(pos);
477 // Gets uninstantiated font setting at position.
478 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
481 lyx::Assert(pos <= size());
483 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
484 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
485 for (; cit != end; ++cit) {
486 if (cit->pos() >= pos)
492 retfont = cit->font();
493 } else if (pos == size() && !empty()) {
494 retfont = getFontSettings(bparams, pos - 1);
496 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
502 // Gets uninstantiated font setting at position 0
503 LyXFont const Paragraph::getFirstFontSettings() const
505 if (!empty() && !pimpl_->fontlist.empty())
506 return pimpl_->fontlist[0].font();
508 return LyXFont(LyXFont::ALL_INHERIT);
512 // Gets the fully instantiated font at a given position in a paragraph
513 // This is basically the same function as LyXText::GetFont() in text2.C.
514 // The difference is that this one is used for generating the LaTeX file,
515 // and thus cosmetic "improvements" are disallowed: This has to deliver
516 // the true picture of the buffer. (Asger)
517 // If position is -1, we get the layout font of the paragraph.
518 // If position is -2, we get the font of the manual label of the paragraph.
519 LyXFont const Paragraph::getFont(BufferParams const & bparams,
522 lyx::Assert(pos >= 0);
524 LyXLayout_ptr const & lout = layout();
526 pos_type main_body = 0;
527 if (lout->labeltype == LABEL_MANUAL)
528 main_body = beginningOfMainBody();
532 layoutfont = lout->labelfont;
534 layoutfont = lout->font;
536 LyXFont tmpfont = getFontSettings(bparams, pos);
537 tmpfont.realize(layoutfont);
539 return pimpl_->realizeFont(tmpfont, bparams);
543 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
545 LyXLayout_ptr const & lout = layout();
547 LyXFont tmpfont = lout->labelfont;
548 tmpfont.setLanguage(getParLanguage(bparams));
550 return pimpl_->realizeFont(tmpfont, bparams);
554 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
556 LyXLayout_ptr const & lout = layout();
558 LyXFont tmpfont = lout->font;
559 tmpfont.setLanguage(getParLanguage(bparams));
561 return pimpl_->realizeFont(tmpfont, bparams);
565 /// Returns the height of the highest font in range
567 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
568 LyXFont::FONT_SIZE const def_size) const
570 if (pimpl_->fontlist.empty())
573 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
574 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
575 for (; end_it != end; ++end_it) {
576 if (end_it->pos() >= endpos)
583 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
584 for (; cit != end; ++cit) {
585 if (cit->pos() >= startpos)
589 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
590 for (; cit != end_it; ++cit) {
591 LyXFont::FONT_SIZE size = cit->font().size();
592 if (size == LyXFont::INHERIT_SIZE)
594 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
601 Paragraph::value_type
602 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
604 value_type c = getChar(pos);
605 if (!lyxrc.rtl_support)
635 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
642 void Paragraph::setFont(pos_type pos, LyXFont const & font)
644 lyx::Assert(pos <= size());
646 // First, reduce font against layout/label font
647 // Update: The SetCharFont() routine in text2.C already
648 // reduces font, so we don't need to do that here. (Asger)
649 // No need to simplify this because it will disappear
650 // in a new kernel. (Asger)
651 // Next search font table
653 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
654 Pimpl::FontList::iterator it = beg;
655 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
656 for (; it != endit; ++it) {
657 if (it->pos() >= pos)
660 unsigned int i = std::distance(beg, it);
661 bool notfound = (it == endit);
663 if (!notfound && pimpl_->fontlist[i].font() == font)
666 bool begin = pos == 0 || notfound ||
667 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
668 // Is position pos is a beginning of a font block?
669 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
670 // Is position pos is the end of a font block?
671 if (begin && end) { // A single char block
672 if (i + 1 < pimpl_->fontlist.size() &&
673 pimpl_->fontlist[i + 1].font() == font) {
674 // Merge the singleton block with the next block
675 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
676 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
677 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
678 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
679 // Merge the singleton block with the previous block
680 pimpl_->fontlist[i - 1].pos(pos);
681 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
683 pimpl_->fontlist[i].font(font);
685 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
686 pimpl_->fontlist[i - 1].pos(pos);
688 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
689 Pimpl::FontTable(pos, font));
691 pimpl_->fontlist[i].pos(pos - 1);
692 if (!(i + 1 < pimpl_->fontlist.size() &&
693 pimpl_->fontlist[i + 1].font() == font))
694 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
695 Pimpl::FontTable(pos, font));
696 } else { // The general case. The block is splitted into 3 blocks
697 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
698 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
699 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
700 Pimpl::FontTable(pos, font));
706 void Paragraph::next(Paragraph * p)
712 // This function is able to hide closed footnotes.
713 Paragraph * Paragraph::next()
719 Paragraph const * Paragraph::next() const
725 void Paragraph::previous(Paragraph * p)
731 // This function is able to hide closed footnotes.
732 Paragraph * Paragraph::previous()
738 // This function is able to hide closed footnotes.
739 Paragraph const * Paragraph::previous() const
746 void Paragraph::makeSameLayout(Paragraph const * par)
748 layout(par->layout());
750 params() = par->params();
754 int Paragraph::stripLeadingSpaces()
756 if (layout()->free_spacing ||
762 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
763 pimpl_->eraseIntern(0);
771 bool Paragraph::hasSameLayout(Paragraph const * par) const
774 par->layout() == layout() &&
775 params().sameLayout(par->params());
779 int Paragraph::getEndLabel() const
781 Paragraph const * par = this;
782 depth_type par_depth = getDepth();
784 LyXLayout_ptr const & layout = par->layout();
785 int const endlabeltype = layout->endlabeltype;
787 if (endlabeltype != END_LABEL_NO_LABEL) {
791 depth_type const next_depth = next_->getDepth();
792 if (par_depth > next_depth ||
793 (par_depth == next_depth
794 && layout != next_->layout()))
800 par = par->outerHook();
802 par_depth = par->getDepth();
804 return END_LABEL_NO_LABEL;
808 Paragraph::depth_type Paragraph::getDepth() const
810 return params().depth();
814 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
816 bool const isenv = layout()->isEnvironment();
819 return params().depth() + 1;
821 return params().depth();
825 char Paragraph::getAlign() const
827 return params().align();
831 string const & Paragraph::getLabelstring() const
833 return params().labelString();
837 // the next two functions are for the manual labels
838 string const Paragraph::getLabelWidthString() const
840 if (!params().labelWidthString().empty())
841 return params().labelWidthString();
843 return _("Senseless with this layout!");
847 void Paragraph::setLabelWidthString(string const & s)
849 params().labelWidthString(s);
853 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
856 params().labelWidthString(string());
857 params().align(LYX_ALIGN_LAYOUT);
858 params().spaceTop(VSpace(VSpace::NONE));
859 params().spaceBottom(VSpace(VSpace::NONE));
860 params().spacing(Spacing(Spacing::Default));
864 // if the layout of a paragraph contains a manual label, the beginning of the
865 // main body is the beginning of the second word. This is what the par-
866 // function returns. If the layout does not contain a label, the main
867 // body always starts with position 0. This differentiation is necessary,
868 // because there cannot be a newline or a blank <= the beginning of the
871 int Paragraph::beginningOfMainBody() const
873 // Unroll the first two cycles of the loop
874 // and remember the previous character to
875 // remove unnecessary GetChar() calls
877 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
879 char previous_char = 0;
882 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
883 // Yes, this ^ is supposed to be "= " not "=="
886 && previous_char != ' '
887 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
889 previous_char = temp;
898 Paragraph * Paragraph::depthHook(depth_type depth)
900 Paragraph * newpar = this;
903 newpar = newpar->previous();
904 } while (newpar && newpar->getDepth() > depth);
907 if (previous() || getDepth())
908 lyxerr << "ERROR (Paragraph::DepthHook): "
917 Paragraph const * Paragraph::depthHook(depth_type depth) const
919 Paragraph const * newpar = this;
922 newpar = newpar->previous();
923 } while (newpar && newpar->getDepth() > depth);
926 if (previous() || getDepth())
927 lyxerr << "ERROR (Paragraph::DepthHook): "
936 Paragraph * Paragraph::outerHook()
940 return depthHook(depth_type(getDepth() - 1));
944 Paragraph const * Paragraph::outerHook() const
948 return depthHook(depth_type(getDepth() - 1));
952 // returns -1 if inset not found
953 int Paragraph::getPositionOfInset(Inset const * inset) const
956 InsetList::iterator it = insetlist.begin();
957 InsetList::iterator end = insetlist.end();
958 for (; it != end; ++it) {
959 if (it.getInset() == inset) {
971 InsetOptArg * optArgInset(Paragraph const & par)
974 InsetList::iterator it = par.insetlist.begin();
975 InsetList::iterator end = par.insetlist.end();
976 for (; it != end; ++it) {
977 Inset * ins = it.getInset();
978 if (ins->lyxCode() == Inset::OPTARG_CODE) {
979 return static_cast<InsetOptArg *>(ins);
987 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
988 BufferParams const & bparams,
989 ostream & os, TexRow & texrow,
992 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
993 Inset const * in = inInset();
994 bool further_blank_line = false;
997 // well we have to check if we are in an inset with unlimited
998 // lenght (all in one row) if that is true then we don't allow
999 // any special options in the paragraph and also we don't allow
1000 // any environment other then "Standard" to be valid!
1001 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1004 if (params().startOfAppendix()) {
1005 os << "\\appendix\n";
1009 if (!params().spacing().isDefault()
1010 && (!previous() || !previous()->hasSameLayout(this))) {
1011 os << params().spacing().writeEnvirBegin() << '\n';
1015 if (style->isCommand()) {
1020 if (params().pagebreakTop()) {
1022 further_blank_line = true;
1024 if (params().spaceTop().kind() != VSpace::NONE) {
1025 os << params().spaceTop().asLatexCommand(bparams);
1026 further_blank_line = true;
1029 if (params().lineTop()) {
1030 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1031 << "\\vspace{-1\\parskip}";
1032 further_blank_line = true;
1035 if (further_blank_line) {
1040 style = bparams.getLyXTextClass().defaultLayout();
1043 Language const * language = getParLanguage(bparams);
1044 Language const * doc_language = bparams.language;
1045 Language const * previous_language = previous()
1046 ? previous()->getParLanguage(bparams) : doc_language;
1048 if (language->babel() != previous_language->babel()
1049 // check if we already put language command in TeXEnvironment()
1050 && !(style->isEnvironment()
1052 (previous()->layout() != layout() &&
1053 previous()->getDepth() <= getDepth())
1054 || previous()->getDepth() < getDepth())))
1056 if (!lyxrc.language_command_end.empty() &&
1057 previous_language->babel() != doc_language->babel())
1059 os << subst(lyxrc.language_command_end, "$$lang",
1060 previous_language->babel())
1065 if (lyxrc.language_command_end.empty() ||
1066 language->babel() != doc_language->babel())
1068 os << subst(lyxrc.language_command_begin, "$$lang",
1075 if (bparams.inputenc == "auto" &&
1076 language->encoding() != previous_language->encoding()) {
1077 os << "\\inputencoding{"
1078 << language->encoding()->LatexName()
1083 switch (style->latextype) {
1086 << style->latexname();
1088 // Separate handling of optional argument inset.
1089 if (style->optionalargs == 1) {
1090 InsetOptArg * it = optArgInset(*this);
1092 it->latexOptional(buf, os, false, false);
1095 os << style->latexparam();
1097 case LATEX_ITEM_ENVIRONMENT:
1099 bibkey->latex(buf, os, false, false);
1103 case LATEX_LIST_ENVIRONMENT:
1110 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1112 // Make sure that \\par is done with the font of the last
1113 // character if this has another size as the default.
1114 // This is necessary because LaTeX (and LyX on the screen)
1115 // calculates the space between the baselines according
1116 // to this font. (Matthias)
1118 // Is this really needed ? (Dekel)
1119 // We do not need to use to change the font for the last paragraph
1120 // or for a command.
1121 LyXFont const font =
1123 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1125 bool is_command = style->isCommand();
1127 if (style->resfont.size() != font.size() && next_ && !is_command) {
1130 os << "\\" << font.latexSize() << " \\par}";
1131 } else if (need_par) {
1133 } else if (is_command)
1136 switch (style->latextype) {
1137 case LATEX_ITEM_ENVIRONMENT:
1138 case LATEX_LIST_ENVIRONMENT:
1139 if (next_ && (params().depth() < next_->params().depth())) {
1144 case LATEX_ENVIRONMENT:
1145 // if its the last paragraph of the current environment
1146 // skip it otherwise fall through
1148 && (next_->layout() != layout()
1149 || next_->params().depth() != params().depth()))
1151 // fall through possible
1153 // we don't need it for the last paragraph!!!
1160 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1161 further_blank_line = false;
1162 if (params().lineBottom()) {
1163 os << "\\lyxline{\\" << font.latexSize() << '}';
1164 further_blank_line = true;
1167 if (params().spaceBottom().kind() != VSpace::NONE) {
1168 os << params().spaceBottom().asLatexCommand(bparams);
1169 further_blank_line = true;
1172 if (params().pagebreakBottom()) {
1174 further_blank_line = true;
1177 if (further_blank_line) {
1182 if (!params().spacing().isDefault()
1183 && (!next_ || !next_->hasSameLayout(this))) {
1184 os << params().spacing().writeEnvirEnd() << '\n';
1189 // we don't need it for the last paragraph!!!
1194 // Since \selectlanguage write the language to the aux file,
1195 // we need to reset the language at the end of footnote or
1198 if (language->babel() != doc_language->babel()) {
1199 if (lyxrc.language_command_end.empty())
1200 os << subst(lyxrc.language_command_begin,
1202 doc_language->babel())
1205 os << subst(lyxrc.language_command_end,
1213 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1218 // This could go to ParagraphParameters if we want to
1219 int Paragraph::startTeXParParams(BufferParams const & bparams,
1220 ostream & os, bool moving_arg) const
1224 if (params().noindent()) {
1225 os << "\\noindent ";
1229 switch (params().align()) {
1230 case LYX_ALIGN_NONE:
1231 case LYX_ALIGN_BLOCK:
1232 case LYX_ALIGN_LAYOUT:
1233 case LYX_ALIGN_SPECIAL:
1235 case LYX_ALIGN_LEFT:
1236 case LYX_ALIGN_RIGHT:
1237 case LYX_ALIGN_CENTER:
1245 switch (params().align()) {
1246 case LYX_ALIGN_NONE:
1247 case LYX_ALIGN_BLOCK:
1248 case LYX_ALIGN_LAYOUT:
1249 case LYX_ALIGN_SPECIAL:
1251 case LYX_ALIGN_LEFT:
1252 if (getParLanguage(bparams)->babel() != "hebrew") {
1253 os << "\\begin{flushleft}";
1256 os << "\\begin{flushright}";
1260 case LYX_ALIGN_RIGHT:
1261 if (getParLanguage(bparams)->babel() != "hebrew") {
1262 os << "\\begin{flushright}";
1265 os << "\\begin{flushleft}";
1269 case LYX_ALIGN_CENTER:
1270 os << "\\begin{center}";
1279 // This could go to ParagraphParameters if we want to
1280 int Paragraph::endTeXParParams(BufferParams const & bparams,
1281 ostream & os, bool moving_arg) const
1285 switch (params().align()) {
1286 case LYX_ALIGN_NONE:
1287 case LYX_ALIGN_BLOCK:
1288 case LYX_ALIGN_LAYOUT:
1289 case LYX_ALIGN_SPECIAL:
1291 case LYX_ALIGN_LEFT:
1292 case LYX_ALIGN_RIGHT:
1293 case LYX_ALIGN_CENTER:
1301 switch (params().align()) {
1302 case LYX_ALIGN_NONE:
1303 case LYX_ALIGN_BLOCK:
1304 case LYX_ALIGN_LAYOUT:
1305 case LYX_ALIGN_SPECIAL:
1307 case LYX_ALIGN_LEFT:
1308 if (getParLanguage(bparams)->babel() != "hebrew") {
1309 os << "\\end{flushleft}";
1312 os << "\\end{flushright}";
1316 case LYX_ALIGN_RIGHT:
1317 if (getParLanguage(bparams)->babel() != "hebrew") {
1318 os << "\\end{flushright}";
1321 os << "\\end{flushleft}";
1325 case LYX_ALIGN_CENTER:
1326 os << "\\end{center}";
1334 // This one spits out the text of the paragraph
1335 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1336 BufferParams const & bparams,
1337 ostream & os, TexRow & texrow,
1340 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1342 bool return_value = false;
1344 LyXLayout_ptr style;
1346 // well we have to check if we are in an inset with unlimited
1347 // lenght (all in one row) if that is true then we don't allow
1348 // any special options in the paragraph and also we don't allow
1349 // any environment other then "Standard" to be valid!
1351 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1354 style = bparams.getLyXTextClass().defaultLayout();
1361 // Maybe we have to create a optional argument.
1363 if (style->labeltype != LABEL_MANUAL)
1366 main_body = beginningOfMainBody();
1368 unsigned int column = 0;
1370 if (main_body > 0) {
1373 basefont = getLabelFont(bparams);
1375 basefont = getLayoutFont(bparams);
1378 moving_arg |= style->needprotect;
1380 // Which font is currently active?
1381 LyXFont running_font(basefont);
1382 // Do we have an open font change?
1383 bool open_font = false;
1385 Change::Type running_change = Change::UNCHANGED;
1387 texrow.start(this, 0);
1389 // if the paragraph is empty, the loop will not be entered at all
1391 if (style->isCommand()) {
1396 column += startTeXParParams(bparams, os, moving_arg);
1400 for (pos_type i = 0; i < size(); ++i) {
1402 // First char in paragraph or after label?
1403 if (i == main_body) {
1404 if (main_body > 0) {
1406 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1409 basefont = getLayoutFont(bparams);
1410 running_font = basefont;
1414 if (style->isCommand()) {
1420 column += startTeXParParams(bparams, os,
1424 value_type c = getChar(i);
1426 // Fully instantiated font
1427 LyXFont font = getFont(bparams, i);
1429 LyXFont const last_font = running_font;
1431 // Spaces at end of font change are simulated to be
1432 // outside font change, i.e. we write "\textXX{text} "
1433 // rather than "\textXX{text }". (Asger)
1434 if (open_font && c == ' ' && i <= size() - 2) {
1435 LyXFont const & next_font = getFont(bparams, i + 1);
1436 if (next_font != running_font
1437 && next_font != font) {
1442 // We end font definition before blanks
1444 (font != running_font ||
1445 font.language() != running_font.language()))
1447 column += running_font.latexWriteEndChanges(os,
1449 (i == main_body-1) ? basefont : font);
1450 running_font = basefont;
1454 // Blanks are printed before start of fontswitch
1456 // Do not print the separation of the optional argument
1457 if (i != main_body - 1) {
1458 pimpl_->simpleTeXBlanks(os, texrow, i,
1459 column, font, *style);
1463 // Do we need to change font?
1464 if ((font != running_font ||
1465 font.language() != running_font.language()) &&
1468 column += font.latexWriteStartChanges(os, basefont,
1470 running_font = font;
1474 Change::Type change = pimpl_->lookupChange(i);
1476 column += Changes::latexMarkChange(os, running_change, change);
1477 running_change = change;
1479 if (c == Paragraph::META_NEWLINE) {
1480 // newlines are handled differently here than
1481 // the default in SimpleTeXSpecialChars().
1482 if (!style->newline_allowed) {
1486 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1489 basefont = getLayoutFont(bparams);
1490 running_font = basefont;
1491 if (font.family() ==
1492 LyXFont::TYPEWRITER_FAMILY) {
1501 texrow.start(this, i + 1);
1504 pimpl_->simpleTeXSpecialChars(buf, bparams,
1505 os, texrow, moving_arg,
1507 basefont, open_font,
1509 *style, i, column, c);
1513 column += Changes::latexMarkChange(os,
1514 running_change, Change::UNCHANGED);
1516 // If we have an open font definition, we have to close it
1518 #ifdef FIXED_LANGUAGE_END_DETECTION
1521 .latexWriteEndChanges(os, basefont,
1522 next_->getFont(bparams,
1525 running_font.latexWriteEndChanges(os, basefont,
1529 #ifdef WITH_WARNINGS
1530 //#warning For now we ALWAYS have to close the foreign font settings if they are
1531 //#warning there as we start another \selectlanguage with the next paragraph if
1532 //#warning we are in need of this. This should be fixed sometime (Jug)
1534 running_font.latexWriteEndChanges(os, basefont, basefont);
1538 // Needed if there is an optional argument but no contents.
1539 if (main_body > 0 && main_body == size()) {
1541 return_value = false;
1545 column += endTeXParParams(bparams, os, moving_arg);
1548 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1549 return return_value;
1553 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1554 BufferParams const & bparams,
1555 ostream & os, TexRow & texrow)
1557 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1559 LyXLayout_ptr const & style = layout();
1561 Language const * language = getParLanguage(bparams);
1562 Language const * doc_language = bparams.language;
1563 Language const * previous_language = previous_
1564 ? previous_->getParLanguage(bparams) : doc_language;
1565 if (language->babel() != previous_language->babel()) {
1567 if (!lyxrc.language_command_end.empty() &&
1568 previous_language->babel() != doc_language->babel()) {
1569 os << subst(lyxrc.language_command_end, "$$lang",
1570 previous_language->babel())
1575 if (lyxrc.language_command_end.empty() ||
1576 language->babel() != doc_language->babel()) {
1577 os << subst(lyxrc.language_command_begin, "$$lang",
1584 bool leftindent_open = false;
1585 if (!params().leftIndent().zero()) {
1586 os << "\\begin{LyXParagraphLeftIndent}{" <<
1587 params().leftIndent().asLatexString() << "}\n";
1589 leftindent_open = true;
1592 if (style->isEnvironment()) {
1593 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1594 os << "\\begin{" << style->latexname() << "}{"
1595 << params().labelWidthString() << "}\n";
1596 } else if (style->labeltype == LABEL_BIBLIO) {
1598 os << "\\begin{" << style->latexname() << "}{"
1599 << bibitemWidest(buf)
1601 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1602 os << "\\begin{" << style->latexname() << '}'
1603 << style->latexparam() << '\n';
1605 os << "\\begin{" << style->latexname() << '}'
1606 << style->latexparam() << '\n';
1609 Paragraph * par = this;
1611 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1613 if (par && par->params().depth() > params().depth()) {
1614 if (par->layout()->isParagraph()) {
1617 // How to handle this? (Lgb)
1618 //&& !suffixIs(os, "\n\n")
1620 // There should be at least one '\n' already
1621 // but we need there to be two for Standard
1622 // paragraphs that are depth-increment'ed to be
1623 // output correctly. However, tables can
1624 // also be paragraphs so don't adjust them.
1627 // Will it ever harm to have one '\n' too
1628 // many? i.e. that we sometimes will have
1629 // three in a row. (Lgb)
1633 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1636 && par->layout() == layout()
1637 && par->params().depth() == params().depth()
1638 && par->params().leftIndent() == params().leftIndent());
1640 if (style->isEnvironment()) {
1641 os << "\\end{" << style->latexname() << "}\n";
1645 if (leftindent_open) {
1646 os << "\\end{LyXParagraphLeftIndent}\n";
1650 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1651 return par; // ale970302
1655 bool Paragraph::isHfill(pos_type pos) const
1657 return IsHfillChar(getChar(pos));
1661 bool Paragraph::isInset(pos_type pos) const
1663 return IsInsetChar(getChar(pos));
1667 bool Paragraph::isNewline(pos_type pos) const
1669 return pos >= 0 && IsNewlineChar(getChar(pos));
1673 bool Paragraph::isSeparator(pos_type pos) const
1675 return IsSeparatorChar(getChar(pos));
1679 bool Paragraph::isLineSeparator(pos_type pos) const
1681 value_type const c = getChar(pos);
1682 return IsLineSeparatorChar(c)
1683 || (IsInsetChar(c) && getInset(pos) &&
1684 getInset(pos)->isLineSeparator());
1688 bool Paragraph::isKomma(pos_type pos) const
1690 return IsKommaChar(getChar(pos));
1694 /// Used by the spellchecker
1695 bool Paragraph::isLetter(pos_type pos) const
1697 value_type const c = getChar(pos);
1698 if (IsLetterChar(c))
1701 return getInset(pos)->isLetter();
1702 // We want to pass the ' and escape chars to ispell
1703 string const extra = lyxrc.isp_esc_chars + '\'';
1704 return contains(extra, c);
1708 bool Paragraph::isWord(pos_type pos) const
1710 return IsWordChar(getChar(pos)) ;
1715 Paragraph::getParLanguage(BufferParams const & bparams) const
1718 return getFirstFontSettings().language();
1719 } else if (previous_)
1720 return previous_->getParLanguage(bparams);
1722 return bparams.language;
1726 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1728 return lyxrc.rtl_support
1729 && getParLanguage(bparams)->RightToLeft()
1730 && !(inInset() && inInset()->owner() &&
1731 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1735 void Paragraph::changeLanguage(BufferParams const & bparams,
1736 Language const * from, Language const * to)
1738 for (pos_type i = 0; i < size(); ++i) {
1739 LyXFont font = getFontSettings(bparams, i);
1740 if (font.language() == from) {
1741 font.setLanguage(to);
1748 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1750 Language const * doc_language = bparams.language;
1751 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1752 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1754 for (; cit != end; ++cit)
1755 if (cit->font().language() != ignore_language &&
1756 cit->font().language() != latex_language &&
1757 cit->font().language() != doc_language)
1763 // Convert the paragraph to a string.
1764 // Used for building the table of contents
1765 string const Paragraph::asString(Buffer const * buffer, bool label) const
1767 BufferParams const & bparams = buffer->params;
1769 if (label && !params().labelString().empty())
1770 s += params().labelString() + ' ';
1771 string::size_type const len = s.size();
1773 for (pos_type i = 0; i < size(); ++i) {
1774 value_type c = getChar(i);
1777 else if (c == META_INSET &&
1778 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1780 getInset(i)->ascii(buffer, ost);
1781 s += subst(STRCONV(ost.str()),'\n',' ');
1789 string const Paragraph::asString(Buffer const * buffer,
1790 pos_type beg, pos_type end, bool label) const
1794 if (beg == 0 && label && !params().labelString().empty())
1795 ost << params().labelString() << ' ';
1797 for (pos_type i = beg; i < end; ++i) {
1798 value_type const c = getUChar(buffer->params, i);
1801 else if (c == META_NEWLINE)
1803 else if (c == META_HFILL)
1805 else if (c == META_INSET) {
1806 getInset(i)->ascii(buffer, ost);
1810 return STRCONV(ost.str());
1814 void Paragraph::setInsetOwner(Inset * i)
1816 pimpl_->inset_owner = i;
1817 InsetList::iterator it = insetlist.begin();
1818 InsetList::iterator end = insetlist.end();
1819 for (; it != end; ++it) {
1821 it.getInset()->setOwner(i);
1826 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1829 insetlist.deleteInsetsLyXText(bv);
1833 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1836 insetlist.resizeInsetsLyXText(bv);
1840 void Paragraph::setContentsFromPar(Paragraph * par)
1842 pimpl_->setContentsFromPar(par);
1846 void Paragraph::trackChanges(Change::Type type)
1848 pimpl_->trackChanges(type);
1852 void Paragraph::untrackChanges()
1854 pimpl_->untrackChanges();
1858 void Paragraph::cleanChanges()
1860 pimpl_->cleanChanges();
1864 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1866 lyx::Assert(!size() || pos < size());
1867 return pimpl_->lookupChange(pos);
1871 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1873 lyx::Assert(!size() || pos < size());
1874 return pimpl_->lookupChangeFull(pos);
1878 bool Paragraph::isChanged(pos_type start, pos_type end) const
1880 return pimpl_->isChanged(start, end);
1884 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1886 return pimpl_->isChangeEdited(start, end);
1890 void Paragraph::markErased()
1892 pimpl_->markErased();
1896 void Paragraph::acceptChange(pos_type start, pos_type end)
1898 return pimpl_->acceptChange(start, end);
1902 void Paragraph::rejectChange(pos_type start, pos_type end)
1904 return pimpl_->rejectChange(start, end);
1908 lyx::pos_type Paragraph::size() const
1910 return pimpl_->size();
1914 bool Paragraph::empty() const
1916 return pimpl_->empty();
1920 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1922 return pimpl_->getChar(pos);
1926 int Paragraph::id() const
1932 LyXLayout_ptr const & Paragraph::layout() const
1938 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1940 layout_ = new_layout;
1944 bool Paragraph::isFirstInSequence() const
1946 Paragraph const * dhook = depthHook(getDepth());
1947 return (dhook == this
1948 || dhook->layout() != layout()
1949 || dhook->getDepth() != getDepth());
1953 Inset * Paragraph::inInset() const
1955 return pimpl_->inset_owner;
1959 void Paragraph::clearContents()
1964 void Paragraph::setChar(pos_type pos, value_type c)
1966 pimpl_->setChar(pos, c);
1970 ParagraphParameters & Paragraph::params()
1972 return pimpl_->params;
1976 ParagraphParameters const & Paragraph::params() const
1978 return pimpl_->params;
1982 Paragraph * Paragraph::getParFromID(int id) const
1984 return pimpl_->getParFromID(id);
1988 bool Paragraph::isFreeSpacing() const
1990 // for now we just need this, later should we need this in some
1991 // other way we can always add a function to Inset::() too.
1992 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1993 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);