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 main_body = 0;
523 if (lout->labeltype == LABEL_MANUAL)
524 main_body = beginningOfMainBody();
528 layoutfont = lout->labelfont;
530 layoutfont = lout->font;
532 LyXFont tmpfont = getFontSettings(bparams, pos);
533 tmpfont.realize(layoutfont);
535 return pimpl_->realizeFont(tmpfont, bparams);
539 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
541 LyXLayout_ptr const & lout = layout();
543 LyXFont tmpfont = lout->labelfont;
544 tmpfont.setLanguage(getParLanguage(bparams));
546 return pimpl_->realizeFont(tmpfont, bparams);
550 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
552 LyXLayout_ptr const & lout = layout();
554 LyXFont tmpfont = lout->font;
555 tmpfont.setLanguage(getParLanguage(bparams));
557 return pimpl_->realizeFont(tmpfont, bparams);
561 /// Returns the height of the highest font in range
563 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
564 LyXFont::FONT_SIZE const def_size) const
566 if (pimpl_->fontlist.empty())
569 Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
570 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
571 for (; end_it != end; ++end_it) {
572 if (end_it->pos() >= endpos)
579 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
580 for (; cit != end; ++cit) {
581 if (cit->pos() >= startpos)
585 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
586 for (; cit != end_it; ++cit) {
587 LyXFont::FONT_SIZE size = cit->font().size();
588 if (size == LyXFont::INHERIT_SIZE)
590 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
597 Paragraph::value_type
598 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
600 value_type c = getChar(pos);
601 if (!lyxrc.rtl_support)
631 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
638 void Paragraph::setFont(pos_type pos, LyXFont const & font)
640 lyx::Assert(pos <= size());
642 // First, reduce font against layout/label font
643 // Update: The SetCharFont() routine in text2.C already
644 // reduces font, so we don't need to do that here. (Asger)
645 // No need to simplify this because it will disappear
646 // in a new kernel. (Asger)
647 // Next search font table
649 Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
650 Pimpl::FontList::iterator it = beg;
651 Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
652 for (; it != endit; ++it) {
653 if (it->pos() >= pos)
656 unsigned int i = std::distance(beg, it);
657 bool notfound = (it == endit);
659 if (!notfound && pimpl_->fontlist[i].font() == font)
662 bool begin = pos == 0 || notfound ||
663 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
664 // Is position pos is a beginning of a font block?
665 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
666 // Is position pos is the end of a font block?
667 if (begin && end) { // A single char block
668 if (i + 1 < pimpl_->fontlist.size() &&
669 pimpl_->fontlist[i + 1].font() == font) {
670 // Merge the singleton block with the next block
671 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
672 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
673 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
674 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
675 // Merge the singleton block with the previous block
676 pimpl_->fontlist[i - 1].pos(pos);
677 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
679 pimpl_->fontlist[i].font(font);
681 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
682 pimpl_->fontlist[i - 1].pos(pos);
684 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
685 Pimpl::FontTable(pos, font));
687 pimpl_->fontlist[i].pos(pos - 1);
688 if (!(i + 1 < pimpl_->fontlist.size() &&
689 pimpl_->fontlist[i + 1].font() == font))
690 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
691 Pimpl::FontTable(pos, font));
692 } else { // The general case. The block is splitted into 3 blocks
693 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
694 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
695 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
696 Pimpl::FontTable(pos, font));
702 void Paragraph::next(Paragraph * p)
708 // This function is able to hide closed footnotes.
709 Paragraph * Paragraph::next()
715 Paragraph const * Paragraph::next() const
721 void Paragraph::previous(Paragraph * p)
727 // This function is able to hide closed footnotes.
728 Paragraph * Paragraph::previous()
734 // This function is able to hide closed footnotes.
735 Paragraph const * Paragraph::previous() const
742 void Paragraph::makeSameLayout(Paragraph const * par)
744 layout(par->layout());
746 params() = par->params();
750 int Paragraph::stripLeadingSpaces()
752 if (layout()->free_spacing ||
758 while (!empty() && (isNewline(0) || isLineSeparator(0))) {
759 pimpl_->eraseIntern(0);
767 bool Paragraph::hasSameLayout(Paragraph const * par) const
770 par->layout() == layout() &&
771 params().sameLayout(par->params());
775 int Paragraph::getEndLabel() const
777 Paragraph const * par = this;
778 depth_type par_depth = getDepth();
780 LyXLayout_ptr const & layout = par->layout();
781 int const endlabeltype = layout->endlabeltype;
783 if (endlabeltype != END_LABEL_NO_LABEL) {
787 depth_type const next_depth = next_->getDepth();
788 if (par_depth > next_depth ||
789 (par_depth == next_depth
790 && layout != next_->layout()))
796 par = par->outerHook();
798 par_depth = par->getDepth();
800 return END_LABEL_NO_LABEL;
804 Paragraph::depth_type Paragraph::getDepth() const
806 return params().depth();
810 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
812 bool const isenv = layout()->isEnvironment();
815 return params().depth() + 1;
817 return params().depth();
821 char Paragraph::getAlign() const
823 return params().align();
827 string const & Paragraph::getLabelstring() const
829 return params().labelString();
833 // the next two functions are for the manual labels
834 string const Paragraph::getLabelWidthString() const
836 if (!params().labelWidthString().empty())
837 return params().labelWidthString();
839 return _("Senseless with this layout!");
843 void Paragraph::setLabelWidthString(string const & s)
845 params().labelWidthString(s);
849 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
852 params().labelWidthString(string());
853 params().align(LYX_ALIGN_LAYOUT);
854 params().spaceTop(VSpace(VSpace::NONE));
855 params().spaceBottom(VSpace(VSpace::NONE));
856 params().spacing(Spacing(Spacing::Default));
860 // if the layout of a paragraph contains a manual label, the beginning of the
861 // main body is the beginning of the second word. This is what the par-
862 // function returns. If the layout does not contain a label, the main
863 // body always starts with position 0. This differentiation is necessary,
864 // because there cannot be a newline or a blank <= the beginning of the
867 int Paragraph::beginningOfMainBody() const
869 // Unroll the first two cycles of the loop
870 // and remember the previous character to
871 // remove unnecessary GetChar() calls
873 if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
875 char previous_char = 0;
878 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
879 // Yes, this ^ is supposed to be "= " not "=="
882 && previous_char != ' '
883 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
885 previous_char = temp;
894 Paragraph * Paragraph::depthHook(depth_type depth)
896 Paragraph * newpar = this;
899 newpar = newpar->previous();
900 } while (newpar && newpar->getDepth() > depth);
903 if (previous() || getDepth())
904 lyxerr << "ERROR (Paragraph::DepthHook): "
913 Paragraph const * Paragraph::depthHook(depth_type depth) const
915 Paragraph const * newpar = this;
918 newpar = newpar->previous();
919 } while (newpar && newpar->getDepth() > depth);
922 if (previous() || getDepth())
923 lyxerr << "ERROR (Paragraph::DepthHook): "
932 Paragraph * Paragraph::outerHook()
936 return depthHook(depth_type(getDepth() - 1));
940 Paragraph const * Paragraph::outerHook() const
944 return depthHook(depth_type(getDepth() - 1));
948 // returns -1 if inset not found
949 int Paragraph::getPositionOfInset(Inset const * inset) const
952 InsetList::iterator it = insetlist.begin();
953 InsetList::iterator end = insetlist.end();
954 for (; it != end; ++it) {
955 if (it.getInset() == inset) {
967 InsetOptArg * optArgInset(Paragraph const & par)
970 InsetList::iterator it = par.insetlist.begin();
971 InsetList::iterator end = par.insetlist.end();
972 for (; it != end; ++it) {
973 Inset * ins = it.getInset();
974 if (ins->lyxCode() == Inset::OPTARG_CODE) {
975 return static_cast<InsetOptArg *>(ins);
983 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
984 BufferParams const & bparams,
985 ostream & os, TexRow & texrow,
988 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
989 Inset const * in = inInset();
990 bool further_blank_line = false;
993 // well we have to check if we are in an inset with unlimited
994 // lenght (all in one row) if that is true then we don't allow
995 // any special options in the paragraph and also we don't allow
996 // any environment other then "Standard" to be valid!
997 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1000 if (params().startOfAppendix()) {
1001 os << "\\appendix\n";
1005 if (!params().spacing().isDefault()
1006 && (!previous() || !previous()->hasSameLayout(this))) {
1007 os << params().spacing().writeEnvirBegin() << '\n';
1011 if (style->isCommand()) {
1016 if (params().pagebreakTop()) {
1018 further_blank_line = true;
1020 if (params().spaceTop().kind() != VSpace::NONE) {
1021 os << params().spaceTop().asLatexCommand(bparams);
1022 further_blank_line = true;
1025 if (params().lineTop()) {
1026 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1027 << "\\vspace{-1\\parskip}";
1028 further_blank_line = true;
1031 if (further_blank_line) {
1036 style = bparams.getLyXTextClass().defaultLayout();
1039 Language const * language = getParLanguage(bparams);
1040 Language const * doc_language = bparams.language;
1041 Language const * previous_language = previous()
1042 ? previous()->getParLanguage(bparams) : doc_language;
1044 if (language->babel() != previous_language->babel()
1045 // check if we already put language command in TeXEnvironment()
1046 && !(style->isEnvironment()
1048 (previous()->layout() != layout() &&
1049 previous()->getDepth() <= getDepth())
1050 || previous()->getDepth() < getDepth())))
1052 if (!lyxrc.language_command_end.empty() &&
1053 previous_language->babel() != doc_language->babel())
1055 os << subst(lyxrc.language_command_end, "$$lang",
1056 previous_language->babel())
1061 if (lyxrc.language_command_end.empty() ||
1062 language->babel() != doc_language->babel())
1064 os << subst(lyxrc.language_command_begin, "$$lang",
1071 if (bparams.inputenc == "auto" &&
1072 language->encoding() != previous_language->encoding()) {
1073 os << "\\inputencoding{"
1074 << language->encoding()->LatexName()
1079 switch (style->latextype) {
1082 << style->latexname();
1084 // Separate handling of optional argument inset.
1085 if (style->optionalargs == 1) {
1086 InsetOptArg * it = optArgInset(*this);
1088 it->latexOptional(buf, os, false, false);
1091 os << style->latexparam();
1093 case LATEX_ITEM_ENVIRONMENT:
1095 bibkey->latex(buf, os, false, false);
1099 case LATEX_LIST_ENVIRONMENT:
1106 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1108 // Make sure that \\par is done with the font of the last
1109 // character if this has another size as the default.
1110 // This is necessary because LaTeX (and LyX on the screen)
1111 // calculates the space between the baselines according
1112 // to this font. (Matthias)
1114 // Is this really needed ? (Dekel)
1115 // We do not need to use to change the font for the last paragraph
1116 // or for a command.
1117 LyXFont const font =
1119 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1121 bool is_command = style->isCommand();
1123 if (style->resfont.size() != font.size() && next_ && !is_command) {
1126 os << "\\" << font.latexSize() << " \\par}";
1127 } else if (need_par) {
1129 } else if (is_command)
1132 switch (style->latextype) {
1133 case LATEX_ITEM_ENVIRONMENT:
1134 case LATEX_LIST_ENVIRONMENT:
1135 if (next_ && (params().depth() < next_->params().depth())) {
1140 case LATEX_ENVIRONMENT:
1141 // if its the last paragraph of the current environment
1142 // skip it otherwise fall through
1144 && (next_->layout() != layout()
1145 || next_->params().depth() != params().depth()))
1147 // fall through possible
1149 // we don't need it for the last paragraph!!!
1156 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1157 further_blank_line = false;
1158 if (params().lineBottom()) {
1159 os << "\\lyxline{\\" << font.latexSize() << '}';
1160 further_blank_line = true;
1163 if (params().spaceBottom().kind() != VSpace::NONE) {
1164 os << params().spaceBottom().asLatexCommand(bparams);
1165 further_blank_line = true;
1168 if (params().pagebreakBottom()) {
1170 further_blank_line = true;
1173 if (further_blank_line) {
1178 if (!params().spacing().isDefault()
1179 && (!next_ || !next_->hasSameLayout(this))) {
1180 os << params().spacing().writeEnvirEnd() << '\n';
1185 // we don't need it for the last paragraph!!!
1190 // Since \selectlanguage write the language to the aux file,
1191 // we need to reset the language at the end of footnote or
1194 if (language->babel() != doc_language->babel()) {
1195 if (lyxrc.language_command_end.empty())
1196 os << subst(lyxrc.language_command_begin,
1198 doc_language->babel())
1201 os << subst(lyxrc.language_command_end,
1209 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1214 // This could go to ParagraphParameters if we want to
1215 int Paragraph::startTeXParParams(BufferParams const & bparams,
1216 ostream & os, bool moving_arg) const
1220 if (params().noindent()) {
1221 os << "\\noindent ";
1225 switch (params().align()) {
1226 case LYX_ALIGN_NONE:
1227 case LYX_ALIGN_BLOCK:
1228 case LYX_ALIGN_LAYOUT:
1229 case LYX_ALIGN_SPECIAL:
1231 case LYX_ALIGN_LEFT:
1232 case LYX_ALIGN_RIGHT:
1233 case LYX_ALIGN_CENTER:
1241 switch (params().align()) {
1242 case LYX_ALIGN_NONE:
1243 case LYX_ALIGN_BLOCK:
1244 case LYX_ALIGN_LAYOUT:
1245 case LYX_ALIGN_SPECIAL:
1247 case LYX_ALIGN_LEFT:
1248 if (getParLanguage(bparams)->babel() != "hebrew") {
1249 os << "\\begin{flushleft}";
1252 os << "\\begin{flushright}";
1256 case LYX_ALIGN_RIGHT:
1257 if (getParLanguage(bparams)->babel() != "hebrew") {
1258 os << "\\begin{flushright}";
1261 os << "\\begin{flushleft}";
1265 case LYX_ALIGN_CENTER:
1266 os << "\\begin{center}";
1275 // This could go to ParagraphParameters if we want to
1276 int Paragraph::endTeXParParams(BufferParams const & bparams,
1277 ostream & os, bool moving_arg) const
1281 switch (params().align()) {
1282 case LYX_ALIGN_NONE:
1283 case LYX_ALIGN_BLOCK:
1284 case LYX_ALIGN_LAYOUT:
1285 case LYX_ALIGN_SPECIAL:
1287 case LYX_ALIGN_LEFT:
1288 case LYX_ALIGN_RIGHT:
1289 case LYX_ALIGN_CENTER:
1297 switch (params().align()) {
1298 case LYX_ALIGN_NONE:
1299 case LYX_ALIGN_BLOCK:
1300 case LYX_ALIGN_LAYOUT:
1301 case LYX_ALIGN_SPECIAL:
1303 case LYX_ALIGN_LEFT:
1304 if (getParLanguage(bparams)->babel() != "hebrew") {
1305 os << "\\end{flushleft}";
1308 os << "\\end{flushright}";
1312 case LYX_ALIGN_RIGHT:
1313 if (getParLanguage(bparams)->babel() != "hebrew") {
1314 os << "\\end{flushright}";
1317 os << "\\end{flushleft}";
1321 case LYX_ALIGN_CENTER:
1322 os << "\\end{center}";
1330 // This one spits out the text of the paragraph
1331 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1332 BufferParams const & bparams,
1333 ostream & os, TexRow & texrow,
1336 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1338 bool return_value = false;
1340 LyXLayout_ptr style;
1342 // well we have to check if we are in an inset with unlimited
1343 // lenght (all in one row) if that is true then we don't allow
1344 // any special options in the paragraph and also we don't allow
1345 // any environment other then "Standard" to be valid!
1347 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1350 style = bparams.getLyXTextClass().defaultLayout();
1357 // Maybe we have to create a optional argument.
1359 if (style->labeltype != LABEL_MANUAL)
1362 main_body = beginningOfMainBody();
1364 unsigned int column = 0;
1366 if (main_body > 0) {
1369 basefont = getLabelFont(bparams);
1371 basefont = getLayoutFont(bparams);
1374 moving_arg |= style->needprotect;
1376 // Which font is currently active?
1377 LyXFont running_font(basefont);
1378 // Do we have an open font change?
1379 bool open_font = false;
1381 Change::Type running_change = Change::UNCHANGED;
1383 texrow.start(this, 0);
1385 // if the paragraph is empty, the loop will not be entered at all
1387 if (style->isCommand()) {
1392 column += startTeXParParams(bparams, os, moving_arg);
1396 for (pos_type i = 0; i < size(); ++i) {
1398 // First char in paragraph or after label?
1399 if (i == main_body) {
1400 if (main_body > 0) {
1402 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1405 basefont = getLayoutFont(bparams);
1406 running_font = basefont;
1410 if (style->isCommand()) {
1416 column += startTeXParParams(bparams, os,
1420 value_type c = getChar(i);
1422 // Fully instantiated font
1423 LyXFont font = getFont(bparams, i);
1425 LyXFont const last_font = running_font;
1427 // Spaces at end of font change are simulated to be
1428 // outside font change, i.e. we write "\textXX{text} "
1429 // rather than "\textXX{text }". (Asger)
1430 if (open_font && c == ' ' && i <= size() - 2) {
1431 LyXFont const & next_font = getFont(bparams, i + 1);
1432 if (next_font != running_font
1433 && next_font != font) {
1438 // We end font definition before blanks
1440 (font != running_font ||
1441 font.language() != running_font.language()))
1443 column += running_font.latexWriteEndChanges(os,
1445 (i == main_body-1) ? basefont : font);
1446 running_font = basefont;
1450 // Blanks are printed before start of fontswitch
1452 // Do not print the separation of the optional argument
1453 if (i != main_body - 1) {
1454 pimpl_->simpleTeXBlanks(os, texrow, i,
1455 column, font, *style);
1459 // Do we need to change font?
1460 if ((font != running_font ||
1461 font.language() != running_font.language()) &&
1464 column += font.latexWriteStartChanges(os, basefont,
1466 running_font = font;
1470 Change::Type change = pimpl_->lookupChange(i);
1472 column += Changes::latexMarkChange(os, running_change, change);
1473 running_change = change;
1475 if (c == Paragraph::META_NEWLINE) {
1476 // newlines are handled differently here than
1477 // the default in SimpleTeXSpecialChars().
1478 if (!style->newline_allowed) {
1482 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1485 basefont = getLayoutFont(bparams);
1486 running_font = basefont;
1487 if (font.family() ==
1488 LyXFont::TYPEWRITER_FAMILY) {
1497 texrow.start(this, i + 1);
1500 pimpl_->simpleTeXSpecialChars(buf, bparams,
1501 os, texrow, moving_arg,
1503 basefont, open_font,
1505 *style, i, column, c);
1509 column += Changes::latexMarkChange(os,
1510 running_change, Change::UNCHANGED);
1512 // If we have an open font definition, we have to close it
1514 #ifdef FIXED_LANGUAGE_END_DETECTION
1517 .latexWriteEndChanges(os, basefont,
1518 next_->getFont(bparams,
1521 running_font.latexWriteEndChanges(os, basefont,
1525 #ifdef WITH_WARNINGS
1526 //#warning For now we ALWAYS have to close the foreign font settings if they are
1527 //#warning there as we start another \selectlanguage with the next paragraph if
1528 //#warning we are in need of this. This should be fixed sometime (Jug)
1530 running_font.latexWriteEndChanges(os, basefont, basefont);
1534 // Needed if there is an optional argument but no contents.
1535 if (main_body > 0 && main_body == size()) {
1537 return_value = false;
1541 column += endTeXParParams(bparams, os, moving_arg);
1544 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1545 return return_value;
1549 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1550 BufferParams const & bparams,
1551 ostream & os, TexRow & texrow)
1553 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1555 LyXLayout_ptr const & style = layout();
1557 Language const * language = getParLanguage(bparams);
1558 Language const * doc_language = bparams.language;
1559 Language const * previous_language = previous_
1560 ? previous_->getParLanguage(bparams) : doc_language;
1561 if (language->babel() != previous_language->babel()) {
1563 if (!lyxrc.language_command_end.empty() &&
1564 previous_language->babel() != doc_language->babel()) {
1565 os << subst(lyxrc.language_command_end, "$$lang",
1566 previous_language->babel())
1571 if (lyxrc.language_command_end.empty() ||
1572 language->babel() != doc_language->babel()) {
1573 os << subst(lyxrc.language_command_begin, "$$lang",
1580 bool leftindent_open = false;
1581 if (!params().leftIndent().zero()) {
1582 os << "\\begin{LyXParagraphLeftIndent}{" <<
1583 params().leftIndent().asLatexString() << "}\n";
1585 leftindent_open = true;
1588 if (style->isEnvironment()) {
1589 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1590 os << "\\begin{" << style->latexname() << "}{"
1591 << params().labelWidthString() << "}\n";
1592 } else if (style->labeltype == LABEL_BIBLIO) {
1594 os << "\\begin{" << style->latexname() << "}{"
1595 << bibitemWidest(buf)
1597 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1598 os << "\\begin{" << style->latexname() << '}'
1599 << style->latexparam() << '\n';
1601 os << "\\begin{" << style->latexname() << '}'
1602 << style->latexparam() << '\n';
1605 Paragraph * par = this;
1607 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1609 if (par && par->params().depth() > params().depth()) {
1610 if (par->layout()->isParagraph()) {
1613 // How to handle this? (Lgb)
1614 //&& !suffixIs(os, "\n\n")
1616 // There should be at least one '\n' already
1617 // but we need there to be two for Standard
1618 // paragraphs that are depth-increment'ed to be
1619 // output correctly. However, tables can
1620 // also be paragraphs so don't adjust them.
1623 // Will it ever harm to have one '\n' too
1624 // many? i.e. that we sometimes will have
1625 // three in a row. (Lgb)
1629 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1632 && par->layout() == layout()
1633 && par->params().depth() == params().depth()
1634 && par->params().leftIndent() == params().leftIndent());
1636 if (style->isEnvironment()) {
1637 os << "\\end{" << style->latexname() << "}\n";
1641 if (leftindent_open) {
1642 os << "\\end{LyXParagraphLeftIndent}\n";
1646 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1647 return par; // ale970302
1651 bool Paragraph::isHfill(pos_type pos) const
1653 return IsHfillChar(getChar(pos));
1657 bool Paragraph::isInset(pos_type pos) const
1659 return IsInsetChar(getChar(pos));
1663 bool Paragraph::isNewline(pos_type pos) const
1665 return pos >= 0 && IsNewlineChar(getChar(pos));
1669 bool Paragraph::isSeparator(pos_type pos) const
1671 return IsSeparatorChar(getChar(pos));
1675 bool Paragraph::isLineSeparator(pos_type pos) const
1677 value_type const c = getChar(pos);
1678 return IsLineSeparatorChar(c)
1679 || (IsInsetChar(c) && getInset(pos) &&
1680 getInset(pos)->isLineSeparator());
1684 bool Paragraph::isKomma(pos_type pos) const
1686 return IsKommaChar(getChar(pos));
1690 /// Used by the spellchecker
1691 bool Paragraph::isLetter(pos_type pos) const
1693 value_type const c = getChar(pos);
1694 if (IsLetterChar(c))
1697 return getInset(pos)->isLetter();
1698 // We want to pass the ' and escape chars to ispell
1699 string const extra = lyxrc.isp_esc_chars + '\'';
1700 return contains(extra, c);
1704 bool Paragraph::isWord(pos_type pos) const
1706 return IsWordChar(getChar(pos)) ;
1711 Paragraph::getParLanguage(BufferParams const & bparams) const
1714 return getFirstFontSettings().language();
1715 } else if (previous_)
1716 return previous_->getParLanguage(bparams);
1718 return bparams.language;
1722 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1724 return lyxrc.rtl_support
1725 && getParLanguage(bparams)->RightToLeft()
1726 && !(inInset() && inInset()->owner() &&
1727 inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1731 void Paragraph::changeLanguage(BufferParams const & bparams,
1732 Language const * from, Language const * to)
1734 for (pos_type i = 0; i < size(); ++i) {
1735 LyXFont font = getFontSettings(bparams, i);
1736 if (font.language() == from) {
1737 font.setLanguage(to);
1744 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1746 Language const * doc_language = bparams.language;
1747 Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1748 Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1750 for (; cit != end; ++cit)
1751 if (cit->font().language() != ignore_language &&
1752 cit->font().language() != latex_language &&
1753 cit->font().language() != doc_language)
1759 // Convert the paragraph to a string.
1760 // Used for building the table of contents
1761 string const Paragraph::asString(Buffer const * buffer, bool label) const
1763 BufferParams const & bparams = buffer->params;
1765 if (label && !params().labelString().empty())
1766 s += params().labelString() + ' ';
1767 string::size_type const len = s.size();
1769 for (pos_type i = 0; i < size(); ++i) {
1770 value_type c = getChar(i);
1773 else if (c == META_INSET &&
1774 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1776 getInset(i)->ascii(buffer, ost);
1777 s += subst(STRCONV(ost.str()),'\n',' ');
1785 string const Paragraph::asString(Buffer const * buffer,
1786 pos_type beg, pos_type end, bool label) const
1790 if (beg == 0 && label && !params().labelString().empty())
1791 ost << params().labelString() << ' ';
1793 for (pos_type i = beg; i < end; ++i) {
1794 value_type const c = getUChar(buffer->params, i);
1797 else if (c == META_NEWLINE)
1799 else if (c == META_HFILL)
1801 else if (c == META_INSET) {
1802 getInset(i)->ascii(buffer, ost);
1806 return STRCONV(ost.str());
1810 void Paragraph::setInsetOwner(Inset * i)
1812 pimpl_->inset_owner = i;
1813 InsetList::iterator it = insetlist.begin();
1814 InsetList::iterator end = insetlist.end();
1815 for (; it != end; ++it) {
1817 it.getInset()->setOwner(i);
1822 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1825 insetlist.deleteInsetsLyXText(bv);
1829 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1832 insetlist.resizeInsetsLyXText(bv);
1836 void Paragraph::setContentsFromPar(Paragraph * par)
1838 pimpl_->setContentsFromPar(par);
1842 void Paragraph::trackChanges(Change::Type type)
1844 pimpl_->trackChanges(type);
1848 void Paragraph::untrackChanges()
1850 pimpl_->untrackChanges();
1854 void Paragraph::cleanChanges()
1856 pimpl_->cleanChanges();
1860 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1862 lyx::Assert(!size() || pos < size());
1863 return pimpl_->lookupChange(pos);
1867 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1869 lyx::Assert(!size() || pos < size());
1870 return pimpl_->lookupChangeFull(pos);
1874 bool Paragraph::isChanged(pos_type start, pos_type end) const
1876 return pimpl_->isChanged(start, end);
1880 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1882 return pimpl_->isChangeEdited(start, end);
1886 void Paragraph::markErased()
1888 pimpl_->markErased();
1892 void Paragraph::acceptChange(pos_type start, pos_type end)
1894 return pimpl_->acceptChange(start, end);
1898 void Paragraph::rejectChange(pos_type start, pos_type end)
1900 return pimpl_->rejectChange(start, end);
1904 lyx::pos_type Paragraph::size() const
1906 return pimpl_->size();
1910 bool Paragraph::empty() const
1912 return pimpl_->empty();
1916 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1918 return pimpl_->getChar(pos);
1922 int Paragraph::id() const
1928 LyXLayout_ptr const & Paragraph::layout() const
1934 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1936 layout_ = new_layout;
1940 bool Paragraph::isFirstInSequence() const
1942 Paragraph const * dhook = depthHook(getDepth());
1943 return (dhook == this
1944 || dhook->layout() != layout()
1945 || dhook->getDepth() != getDepth());
1949 Inset * Paragraph::inInset() const
1951 return pimpl_->inset_owner;
1955 void Paragraph::clearContents()
1960 void Paragraph::setChar(pos_type pos, value_type c)
1962 pimpl_->setChar(pos, c);
1966 ParagraphParameters & Paragraph::params()
1968 return pimpl_->params;
1972 ParagraphParameters const & Paragraph::params() const
1974 return pimpl_->params;
1978 Paragraph * Paragraph::getParFromID(int id) const
1980 return pimpl_->getParFromID(id);
1984 bool Paragraph::isFreeSpacing() const
1986 // for now we just need this, later should we need this in some
1987 // other way we can always add a function to Inset::() too.
1988 if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1989 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);