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(Buffer * buf,
33 ParagraphList::iterator par,
37 BufferParams const & bparams = buf->params;
39 // create a new paragraph, and insert into the list
40 ParagraphList::iterator tmp = buf->paragraphs.insert(boost::next(par),
43 // without doing that we get a crash when typing <Return> at the
45 tmp->layout(bparams.getLyXTextClass().defaultLayout());
46 // remember to set the inset_owner
47 tmp->setInsetOwner(par->inInset());
49 if (bparams.tracking_changes)
52 // this is an idea for a more userfriendly layout handling, I will
53 // see what the users say
55 // layout stays the same with latex-environments
57 tmp->layout(par->layout());
58 tmp->setLabelWidthString(par->params().labelWidthString());
61 bool const isempty = (par->layout()->keepempty && par->empty());
63 if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
64 tmp->layout(par->layout());
65 tmp->params().align(par->params().align());
66 tmp->setLabelWidthString(par->params().labelWidthString());
68 tmp->params().lineBottom(par->params().lineBottom());
69 par->params().lineBottom(false);
70 tmp->params().pagebreakBottom(par->params().pagebreakBottom());
71 par->params().pagebreakBottom(false);
72 tmp->params().spaceBottom(par->params().spaceBottom());
73 par->params().spaceBottom(VSpace(VSpace::NONE));
75 tmp->params().depth(par->params().depth());
76 tmp->params().noindent(par->params().noindent());
78 // copy everything behind the break-position
79 // to the new paragraph
82 #warning this seems wrong
84 /* FIXME: if !keepempty, empty() == true, then we reach
85 * here with size() == 0. So pos_end becomes - 1. Why
86 * doesn't this cause problems ???
88 pos_type pos_end = par->size() - 1;
92 for (; i <= pos_end; ++i) {
93 Change::Type change(par->lookupChange(i));
94 par->cutIntoMinibuffer(bparams, i);
95 if (tmp->insertFromMinibuffer(j - pos)) {
96 tmp->setChange(j - pos, change);
100 for (i = pos_end; i >= pos; --i) {
108 tmp->params().lineTop(par->params().lineTop());
109 tmp->params().pagebreakTop(par->params().pagebreakTop());
110 tmp->params().spaceTop(par->params().spaceTop());
111 par->params().clear();
113 par->layout(bparams.getLyXTextClass().defaultLayout());
115 // layout stays the same with latex-environments
117 par->layout(tmp->layout());
118 par->setLabelWidthString(tmp->params().labelWidthString());
119 par->params().depth(tmp->params().depth());
122 // subtle, but needed to get empty pars working right
123 if (bparams.tracking_changes) {
126 } else if (!tmp->size()) {
133 void breakParagraphConservative(Buffer * buf,
134 ParagraphList::iterator par,
137 BufferParams const & bparams = buf->params;
139 // create a new paragraph
140 ParagraphList::iterator tmp = buf->paragraphs.insert(boost::next(par),
142 tmp->makeSameLayout(&*par);
144 // When can pos > Last()?
145 // I guess pos == Last() is possible.
146 if (par->size() > pos) {
147 // copy everything behind the break-position to the new
149 pos_type pos_end = par->size() - 1;
151 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
152 par->cutIntoMinibuffer(bparams, i);
153 if (tmp->insertFromMinibuffer(j - pos))
157 for (pos_type k = pos_end; k >= pos; --k) {
164 void mergeParagraph(Buffer * buf, ParagraphList::iterator par)
166 BufferParams const & bparams = buf->params;
168 ParagraphList::iterator the_next = boost::next(par);
170 // first the DTP-stuff
171 par->params().lineBottom(the_next->params().lineBottom());
172 par->params().spaceBottom(the_next->params().spaceBottom());
173 par->params().pagebreakBottom(the_next->params().pagebreakBottom());
175 pos_type pos_end = the_next->size() - 1;
176 pos_type pos_insert = par->size();
178 // ok, now copy the paragraph
179 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
180 the_next->cutIntoMinibuffer(bparams, i);
181 if (par->insertFromMinibuffer(pos_insert + j))
185 buf->paragraphs.erase(the_next);
190 Paragraph * depthHook(Paragraph * par, Paragraph::depth_type depth)
192 Paragraph * newpar = par;
195 newpar = newpar->previous();
196 } while (newpar && newpar->getDepth() > depth);
199 if (par->previous() || par->getDepth())
200 lyxerr << "Error (depthHook): "
201 << "no hook." << endl;
208 Paragraph * outerHook(Paragraph * par)
210 if (!par->getDepth())
212 return depthHook(par, Paragraph::depth_type(par->getDepth() - 1));
216 bool isFirstInSequence(Paragraph * par)
218 Paragraph const * dhook = depthHook(par, par->getDepth());
220 || dhook->getLayout() != par->getLayout()
221 || dhook->getDepth() != par->getDepth());
225 int getEndLabel(Paragraph * para, BufferParams const & bparams)
227 Paragraph * par = para;
229 Paragraph::depth_type par_depth = par->getDepth();
230 layout_type layout = par->getLayout();
231 int const endlabeltype =
232 textclasslist.Style(bparams.textclass,
233 layout).endlabeltype;
234 if (endlabeltype != END_LABEL_NO_LABEL) {
238 Paragraph::depth_type const next_depth =
239 para->next()->getDepth();
240 if (par_depth > next_depth ||
241 (par_depth == next_depth
242 && layout != para->next()->getLayout()))
248 par = outerHook(par);
250 return END_LABEL_NO_LABEL;
255 ParagraphList::iterator
256 TeXDeeper(Buffer const * buf,
257 BufferParams const & bparams,
258 ParagraphList::iterator pit,
259 ostream & os, TexRow & texrow)
261 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
262 ParagraphList::iterator par = pit;
264 while (par != buf->paragraphs.end()&& par->params().depth() == pit->params().depth()) {
265 if (par->layout()->isEnvironment()) {
266 par = TeXEnvironment(buf, bparams, par,
269 par = TeXOnePar(buf, bparams, par,
273 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
279 ParagraphList::iterator
280 TeXEnvironment(Buffer const * buf,
281 BufferParams const & bparams,
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 != buf->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, par, os, texrow, false);
343 if (par != buf->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, par, os, texrow);
365 } while (par != buf->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::iterator pit,
408 ostream & os, TexRow & texrow,
411 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << endl;
412 Inset const * in = pit->inInset();
413 bool further_blank_line = false;
416 // well we have to check if we are in an inset with unlimited
417 // lenght (all in one row) if that is true then we don't allow
418 // any special options in the paragraph and also we don't allow
419 // any environment other then "Standard" to be valid!
420 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
421 style = pit->layout();
423 if (pit->params().startOfAppendix()) {
424 os << "\\appendix\n";
428 if (!pit->params().spacing().isDefault()
429 && (pit == buf->paragraphs.begin() || !boost::prior(pit)->hasSameLayout(&*pit))) {
430 os << pit->params().spacing().writeEnvirBegin() << '\n';
434 if (style->isCommand()) {
439 if (pit->params().pagebreakTop()) {
441 further_blank_line = true;
443 if (pit->params().spaceTop().kind() != VSpace::NONE) {
444 os << pit->params().spaceTop().asLatexCommand(bparams);
445 further_blank_line = true;
448 if (pit->params().lineTop()) {
449 os << "\\lyxline{\\" << pit->getFont(bparams, 0).latexSize() << '}'
450 << "\\vspace{-1\\parskip}";
451 further_blank_line = true;
454 if (further_blank_line) {
459 style = bparams.getLyXTextClass().defaultLayout();
462 Language const * language = pit->getParLanguage(bparams);
463 Language const * doc_language = bparams.language;
464 Language const * previous_language =
465 (pit != buf->paragraphs.begin())
466 ? boost::prior(pit)->getParLanguage(bparams)
469 if (language->babel() != previous_language->babel()
470 // check if we already put language command in TeXEnvironment()
471 && !(style->isEnvironment()
472 && (pit == buf->paragraphs.begin() ||
473 (boost::prior(pit)->layout() != pit->layout() &&
474 boost::prior(pit)->getDepth() <= pit->getDepth())
475 || boost::prior(pit)->getDepth() < pit->getDepth())))
477 if (!lyxrc.language_command_end.empty() &&
478 previous_language->babel() != doc_language->babel())
480 os << subst(lyxrc.language_command_end, "$$lang",
481 previous_language->babel())
486 if (lyxrc.language_command_end.empty() ||
487 language->babel() != doc_language->babel())
489 os << subst(lyxrc.language_command_begin, "$$lang",
496 if (bparams.inputenc == "auto" &&
497 language->encoding() != previous_language->encoding()) {
498 os << "\\inputencoding{"
499 << language->encoding()->LatexName()
504 switch (style->latextype) {
506 os << '\\' << style->latexname();
508 // Separate handling of optional argument inset.
509 if (style->optionalargs == 1) {
510 InsetOptArg * it = optArgInset(*pit);
512 it->latexOptional(buf, os, false, false);
515 os << style->latexparam();
517 case LATEX_ITEM_ENVIRONMENT:
518 case LATEX_LIST_ENVIRONMENT:
521 case LATEX_BIB_ENVIRONMENT:
522 // ignore this, the inset will write itself
528 bool need_par = pit->simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
530 // Make sure that \\par is done with the font of the last
531 // character if this has another size as the default.
532 // This is necessary because LaTeX (and LyX on the screen)
533 // calculates the space between the baselines according
534 // to this font. (Matthias)
536 // Is this really needed ? (Dekel)
537 // We do not need to use to change the font for the last paragraph
541 ? pit->getLayoutFont(bparams) : pit->getFont(bparams, pit->size() - 1));
543 bool is_command = style->isCommand();
545 if (style->resfont.size() != font.size()
546 && boost::next(pit) != buf->paragraphs.end()
550 os << "\\" << font.latexSize() << " \\par}";
551 } else if (need_par) {
553 } else if (is_command)
556 switch (style->latextype) {
557 case LATEX_ITEM_ENVIRONMENT:
558 case LATEX_LIST_ENVIRONMENT:
559 if (boost::next(pit) != buf->paragraphs.end()
560 && (pit->params().depth() < boost::next(pit)->params().depth())) {
565 case LATEX_ENVIRONMENT:
566 // if its the last paragraph of the current environment
567 // skip it otherwise fall through
568 if (boost::next(pit) != buf->paragraphs.end()
569 && (boost::next(pit)->layout() != pit->layout()
570 || boost::next(pit)->params().depth() != pit->params().depth()))
572 // fall through possible
574 // we don't need it for the last paragraph!!!
575 if (boost::next(pit) != buf->paragraphs.end()) {
581 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
582 further_blank_line = false;
583 if (pit->params().lineBottom()) {
584 os << "\\lyxline{\\" << font.latexSize() << '}';
585 further_blank_line = true;
588 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
589 os << pit->params().spaceBottom().asLatexCommand(bparams);
590 further_blank_line = true;
593 if (pit->params().pagebreakBottom()) {
595 further_blank_line = true;
598 if (further_blank_line) {
603 if (!pit->params().spacing().isDefault()
604 && (boost::next(pit) == buf->paragraphs.end()|| !boost::next(pit)->hasSameLayout(&*pit))) {
605 os << pit->params().spacing().writeEnvirEnd() << '\n';
610 // we don't need it for the last paragraph!!!
611 if (boost::next(pit) != buf->paragraphs.end()) {
615 // Since \selectlanguage write the language to the aux file,
616 // we need to reset the language at the end of footnote or
619 if (language->babel() != doc_language->babel()) {
620 if (lyxrc.language_command_end.empty())
621 os << subst(lyxrc.language_command_begin,
623 doc_language->babel())
626 os << subst(lyxrc.language_command_end,
634 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;