2 * \file paragraph_funcs.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
13 #include "paragraph_funcs.h"
14 #include "paragraph_pimpl.h"
16 #include "ParagraphParameters.h"
17 #include "lyxtextclasslist.h"
22 #include "support/lstrings.h"
23 #include "insets/insetoptarg.h"
25 extern string bibitemWidest(Buffer const *);
28 //using lyx::layout_type;
32 void breakParagraph(BufferParams const & bparams,
33 ParagraphList & paragraphs,
34 ParagraphList::iterator par,
38 // create a new paragraph, and insert into the list
39 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
42 // without doing that we get a crash when typing <Return> at the
44 tmp->layout(bparams.getLyXTextClass().defaultLayout());
45 // remember to set the inset_owner
46 tmp->setInsetOwner(par->inInset());
48 if (bparams.tracking_changes)
51 // this is an idea for a more userfriendly layout handling, I will
52 // see what the users say
54 // layout stays the same with latex-environments
56 tmp->layout(par->layout());
57 tmp->setLabelWidthString(par->params().labelWidthString());
60 bool const isempty = (par->layout()->keepempty && par->empty());
62 if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
63 tmp->layout(par->layout());
64 tmp->params().align(par->params().align());
65 tmp->setLabelWidthString(par->params().labelWidthString());
67 tmp->params().lineBottom(par->params().lineBottom());
68 par->params().lineBottom(false);
69 tmp->params().pagebreakBottom(par->params().pagebreakBottom());
70 par->params().pagebreakBottom(false);
71 tmp->params().spaceBottom(par->params().spaceBottom());
72 par->params().spaceBottom(VSpace(VSpace::NONE));
74 tmp->params().depth(par->params().depth());
75 tmp->params().noindent(par->params().noindent());
77 // copy everything behind the break-position
78 // to the new paragraph
81 #warning this seems wrong
83 /* FIXME: if !keepempty, empty() == true, then we reach
84 * here with size() == 0. So pos_end becomes - 1. Why
85 * doesn't this cause problems ???
87 pos_type pos_end = par->size() - 1;
91 for (; i <= pos_end; ++i) {
92 Change::Type change(par->lookupChange(i));
93 par->cutIntoMinibuffer(bparams, i);
94 if (tmp->insertFromMinibuffer(j - pos)) {
95 tmp->setChange(j - pos, change);
99 for (i = pos_end; i >= pos; --i) {
107 tmp->params().lineTop(par->params().lineTop());
108 tmp->params().pagebreakTop(par->params().pagebreakTop());
109 tmp->params().spaceTop(par->params().spaceTop());
110 par->params().clear();
112 par->layout(bparams.getLyXTextClass().defaultLayout());
114 // layout stays the same with latex-environments
116 par->layout(tmp->layout());
117 par->setLabelWidthString(tmp->params().labelWidthString());
118 par->params().depth(tmp->params().depth());
121 // subtle, but needed to get empty pars working right
122 if (bparams.tracking_changes) {
125 } else if (!tmp->size()) {
132 void breakParagraphConservative(BufferParams const & bparams,
133 ParagraphList & paragraphs,
134 ParagraphList::iterator par,
137 // create a new paragraph
138 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
140 tmp->makeSameLayout(&*par);
142 // When can pos > Last()?
143 // I guess pos == Last() is possible.
144 if (par->size() > pos) {
145 // copy everything behind the break-position to the new
147 pos_type pos_end = par->size() - 1;
149 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
150 par->cutIntoMinibuffer(bparams, i);
151 if (tmp->insertFromMinibuffer(j - pos))
155 for (pos_type k = pos_end; k >= pos; --k) {
162 void mergeParagraph(BufferParams const & bparams,
163 ParagraphList & paragraphs,
164 ParagraphList::iterator par)
166 ParagraphList::iterator the_next = boost::next(par);
168 // first the DTP-stuff
169 par->params().lineBottom(the_next->params().lineBottom());
170 par->params().spaceBottom(the_next->params().spaceBottom());
171 par->params().pagebreakBottom(the_next->params().pagebreakBottom());
173 pos_type pos_end = the_next->size() - 1;
174 pos_type pos_insert = par->size();
176 // ok, now copy the paragraph
177 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
178 the_next->cutIntoMinibuffer(bparams, i);
179 if (par->insertFromMinibuffer(pos_insert + j))
183 paragraphs.erase(the_next);
188 Paragraph * depthHook(Paragraph * par, Paragraph::depth_type depth)
190 Paragraph * newpar = par;
193 newpar = newpar->previous();
194 } while (newpar && newpar->getDepth() > depth);
197 if (par->previous() || par->getDepth())
198 lyxerr << "Error (depthHook): "
199 << "no hook." << endl;
206 Paragraph * outerHook(Paragraph * par)
208 if (!par->getDepth())
210 return depthHook(par, Paragraph::depth_type(par->getDepth() - 1));
214 bool isFirstInSequence(Paragraph * par)
216 Paragraph const * dhook = depthHook(par, par->getDepth());
218 || dhook->getLayout() != par->getLayout()
219 || dhook->getDepth() != par->getDepth());
223 int getEndLabel(Paragraph * para, BufferParams const & bparams)
225 Paragraph * par = para;
227 Paragraph::depth_type par_depth = par->getDepth();
228 layout_type layout = par->getLayout();
229 int const endlabeltype =
230 textclasslist.Style(bparams.textclass,
231 layout).endlabeltype;
232 if (endlabeltype != END_LABEL_NO_LABEL) {
236 Paragraph::depth_type const next_depth =
237 para->next()->getDepth();
238 if (par_depth > next_depth ||
239 (par_depth == next_depth
240 && layout != para->next()->getLayout()))
246 par = outerHook(par);
248 return END_LABEL_NO_LABEL;
253 ParagraphList::iterator
254 TeXDeeper(Buffer const * buf,
255 BufferParams const & bparams,
256 ParagraphList const & paragraphs,
257 ParagraphList::iterator pit,
258 ostream & os, TexRow & texrow)
260 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
261 ParagraphList::iterator par = pit;
263 while (par != paragraphs.end()&& par->params().depth() == pit->params().depth()) {
264 if (par->layout()->isEnvironment()) {
265 par = TeXEnvironment(buf, bparams, paragraphs, par,
268 par = TeXOnePar(buf, bparams, paragraphs, par,
272 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
278 ParagraphList::iterator
279 TeXEnvironment(Buffer const * buf,
280 BufferParams const & bparams,
281 ParagraphList const & paragraphs,
282 ParagraphList::iterator pit,
283 ostream & os, TexRow & texrow)
285 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
287 LyXLayout_ptr const & style = pit->layout();
289 Language const * language = pit->getParLanguage(bparams);
290 Language const * doc_language = bparams.language;
291 Language const * previous_language =
292 (pit != paragraphs.begin())
293 ? boost::prior(pit)->getParLanguage(bparams)
295 if (language->babel() != previous_language->babel()) {
297 if (!lyxrc.language_command_end.empty() &&
298 previous_language->babel() != doc_language->babel()) {
299 os << subst(lyxrc.language_command_end, "$$lang",
300 previous_language->babel())
305 if (lyxrc.language_command_end.empty() ||
306 language->babel() != doc_language->babel()) {
307 os << subst(lyxrc.language_command_begin, "$$lang",
314 bool leftindent_open = false;
315 if (!pit->params().leftIndent().zero()) {
316 os << "\\begin{LyXParagraphLeftIndent}{" <<
317 pit->params().leftIndent().asLatexString() << "}\n";
319 leftindent_open = true;
322 if (style->isEnvironment()) {
323 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
324 os << "\\begin{" << style->latexname() << "}{"
325 << pit->params().labelWidthString() << "}\n";
326 } else if (style->labeltype == LABEL_BIBLIO) {
328 os << "\\begin{" << style->latexname() << "}{"
329 << bibitemWidest(buf)
331 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
332 os << "\\begin{" << style->latexname() << '}'
333 << style->latexparam() << '\n';
335 os << "\\begin{" << style->latexname() << '}'
336 << style->latexparam() << '\n';
339 ParagraphList::iterator par = pit;
341 par = TeXOnePar(buf, bparams, paragraphs, par, os, texrow, false);
343 if (par != paragraphs.end()&& par->params().depth() > pit->params().depth()) {
344 if (par->layout()->isParagraph()) {
347 // How to handle this? (Lgb)
348 //&& !suffixIs(os, "\n\n")
350 // There should be at least one '\n' already
351 // but we need there to be two for Standard
352 // paragraphs that are depth-increment'ed to be
353 // output correctly. However, tables can
354 // also be paragraphs so don't adjust them.
357 // Will it ever harm to have one '\n' too
358 // many? i.e. that we sometimes will have
359 // three in a row. (Lgb)
363 par = TeXDeeper(buf, bparams, paragraphs, par, os, texrow);
365 } while (par != paragraphs.end()
366 && par->layout() == pit->layout()
367 && par->params().depth() == pit->params().depth()
368 && par->params().leftIndent() == pit->params().leftIndent());
370 if (style->isEnvironment()) {
371 os << "\\end{" << style->latexname() << "}\n";
375 if (leftindent_open) {
376 os << "\\end{LyXParagraphLeftIndent}\n";
380 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
381 return par; // ale970302
387 InsetOptArg * optArgInset(Paragraph const & par)
390 InsetList::iterator it = par.insetlist.begin();
391 InsetList::iterator end = par.insetlist.end();
392 for (; it != end; ++it) {
393 Inset * ins = it.getInset();
394 if (ins->lyxCode() == Inset::OPTARG_CODE) {
395 return static_cast<InsetOptArg *>(ins);
404 ParagraphList::iterator
405 TeXOnePar(Buffer const * buf,
406 BufferParams const & bparams,
407 ParagraphList const & paragraphs,
408 ParagraphList::iterator pit,
409 ostream & os, TexRow & texrow,
412 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << endl;
413 Inset const * in = pit->inInset();
414 bool further_blank_line = false;
417 // well we have to check if we are in an inset with unlimited
418 // lenght (all in one row) if that is true then we don't allow
419 // any special options in the paragraph and also we don't allow
420 // any environment other then "Standard" to be valid!
421 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
422 style = pit->layout();
424 if (pit->params().startOfAppendix()) {
425 os << "\\appendix\n";
429 if (!pit->params().spacing().isDefault()
430 && (pit == paragraphs.begin() || !boost::prior(pit)->hasSameLayout(&*pit))) {
431 os << pit->params().spacing().writeEnvirBegin() << '\n';
435 if (style->isCommand()) {
440 if (pit->params().pagebreakTop()) {
442 further_blank_line = true;
444 if (pit->params().spaceTop().kind() != VSpace::NONE) {
445 os << pit->params().spaceTop().asLatexCommand(bparams);
446 further_blank_line = true;
449 if (pit->params().lineTop()) {
450 os << "\\lyxline{\\" << pit->getFont(bparams, 0).latexSize() << '}'
451 << "\\vspace{-1\\parskip}";
452 further_blank_line = true;
455 if (further_blank_line) {
460 style = bparams.getLyXTextClass().defaultLayout();
463 Language const * language = pit->getParLanguage(bparams);
464 Language const * doc_language = bparams.language;
465 Language const * previous_language =
466 (pit != paragraphs.begin())
467 ? boost::prior(pit)->getParLanguage(bparams)
470 if (language->babel() != previous_language->babel()
471 // check if we already put language command in TeXEnvironment()
472 && !(style->isEnvironment()
473 && (pit == paragraphs.begin() ||
474 (boost::prior(pit)->layout() != pit->layout() &&
475 boost::prior(pit)->getDepth() <= pit->getDepth())
476 || boost::prior(pit)->getDepth() < pit->getDepth())))
478 if (!lyxrc.language_command_end.empty() &&
479 previous_language->babel() != doc_language->babel())
481 os << subst(lyxrc.language_command_end, "$$lang",
482 previous_language->babel())
487 if (lyxrc.language_command_end.empty() ||
488 language->babel() != doc_language->babel())
490 os << subst(lyxrc.language_command_begin, "$$lang",
497 if (bparams.inputenc == "auto" &&
498 language->encoding() != previous_language->encoding()) {
499 os << "\\inputencoding{"
500 << language->encoding()->LatexName()
505 switch (style->latextype) {
507 os << '\\' << style->latexname();
509 // Separate handling of optional argument inset.
510 if (style->optionalargs == 1) {
511 InsetOptArg * it = optArgInset(*pit);
513 it->latexOptional(buf, os, false, false);
516 os << style->latexparam();
518 case LATEX_ITEM_ENVIRONMENT:
519 case LATEX_LIST_ENVIRONMENT:
522 case LATEX_BIB_ENVIRONMENT:
523 // ignore this, the inset will write itself
529 bool need_par = pit->simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
531 // Make sure that \\par is done with the font of the last
532 // character if this has another size as the default.
533 // This is necessary because LaTeX (and LyX on the screen)
534 // calculates the space between the baselines according
535 // to this font. (Matthias)
537 // Is this really needed ? (Dekel)
538 // We do not need to use to change the font for the last paragraph
542 ? pit->getLayoutFont(bparams) : pit->getFont(bparams, pit->size() - 1));
544 bool is_command = style->isCommand();
546 if (style->resfont.size() != font.size()
547 && boost::next(pit) != paragraphs.end()
551 os << "\\" << font.latexSize() << " \\par}";
552 } else if (need_par) {
554 } else if (is_command)
557 switch (style->latextype) {
558 case LATEX_ITEM_ENVIRONMENT:
559 case LATEX_LIST_ENVIRONMENT:
560 if (boost::next(pit) != paragraphs.end()
561 && (pit->params().depth() < boost::next(pit)->params().depth())) {
566 case LATEX_ENVIRONMENT:
567 // if its the last paragraph of the current environment
568 // skip it otherwise fall through
569 if (boost::next(pit) != paragraphs.end()
570 && (boost::next(pit)->layout() != pit->layout()
571 || boost::next(pit)->params().depth() != pit->params().depth()))
573 // fall through possible
575 // we don't need it for the last paragraph!!!
576 if (boost::next(pit) != paragraphs.end()) {
582 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
583 further_blank_line = false;
584 if (pit->params().lineBottom()) {
585 os << "\\lyxline{\\" << font.latexSize() << '}';
586 further_blank_line = true;
589 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
590 os << pit->params().spaceBottom().asLatexCommand(bparams);
591 further_blank_line = true;
594 if (pit->params().pagebreakBottom()) {
596 further_blank_line = true;
599 if (further_blank_line) {
604 if (!pit->params().spacing().isDefault()
605 && (boost::next(pit) == paragraphs.end()|| !boost::next(pit)->hasSameLayout(&*pit))) {
606 os << pit->params().spacing().writeEnvirEnd() << '\n';
611 // we don't need it for the last paragraph!!!
612 if (boost::next(pit) != paragraphs.end()) {
616 // Since \selectlanguage write the language to the aux file,
617 // we need to reset the language at the end of footnote or
620 if (language->babel() != doc_language->babel()) {
621 if (lyxrc.language_command_end.empty())
622 os << subst(lyxrc.language_command_begin,
624 doc_language->babel())
627 os << subst(lyxrc.language_command_end,
635 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;