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,
37 // create a new paragraph
38 Paragraph * tmp = new Paragraph(par);
39 // without doing that we get a crash when typing <Return> at the
41 tmp->layout(bparams.getLyXTextClass().defaultLayout());
42 // remember to set the inset_owner
43 tmp->setInsetOwner(par->inInset());
45 if (bparams.tracking_changes)
48 // this is an idea for a more userfriendly layout handling, I will
49 // see what the users say
51 // layout stays the same with latex-environments
53 tmp->layout(par->layout());
54 tmp->setLabelWidthString(par->params().labelWidthString());
57 bool const isempty = (par->layout()->keepempty && par->empty());
59 if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
60 tmp->layout(par->layout());
61 tmp->params().align(par->params().align());
62 tmp->setLabelWidthString(par->params().labelWidthString());
64 tmp->params().lineBottom(par->params().lineBottom());
65 par->params().lineBottom(false);
66 tmp->params().pagebreakBottom(par->params().pagebreakBottom());
67 par->params().pagebreakBottom(false);
68 tmp->params().spaceBottom(par->params().spaceBottom());
69 par->params().spaceBottom(VSpace(VSpace::NONE));
71 tmp->params().depth(par->params().depth());
72 tmp->params().noindent(par->params().noindent());
74 // copy everything behind the break-position
75 // to the new paragraph
78 #warning this seems wrong
80 /* FIXME: if !keepempty, empty() == true, then we reach
81 * here with size() == 0. So pos_end becomes - 1. Why
82 * doesn't this cause problems ???
84 pos_type pos_end = par->size() - 1;
88 for (; i <= pos_end; ++i) {
89 Change::Type change(par->lookupChange(i));
90 par->cutIntoMinibuffer(bparams, i);
91 if (tmp->insertFromMinibuffer(j - pos)) {
92 tmp->pimpl_->setChange(j - pos, change);
96 for (i = pos_end; i >= pos; --i) {
97 par->pimpl_->eraseIntern(i);
104 tmp->params().lineTop(par->params().lineTop());
105 tmp->params().pagebreakTop(par->params().pagebreakTop());
106 tmp->params().spaceTop(par->params().spaceTop());
107 par->params().clear();
109 par->layout(bparams.getLyXTextClass().defaultLayout());
111 // layout stays the same with latex-environments
113 par->layout(tmp->layout());
114 par->setLabelWidthString(tmp->params().labelWidthString());
115 par->params().depth(tmp->params().depth());
118 // subtle, but needed to get empty pars working right
119 if (bparams.tracking_changes) {
122 } else if (!tmp->size()) {
129 void breakParagraphConservative(BufferParams const & bparams,
133 // create a new paragraph
134 Paragraph * tmp = new Paragraph(par);
135 tmp->makeSameLayout(par);
137 // When can pos > Last()?
138 // I guess pos == Last() is possible.
139 if (par->size() > pos) {
140 // copy everything behind the break-position to the new
142 pos_type pos_end = par->size() - 1;
144 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
145 par->cutIntoMinibuffer(bparams, i);
146 if (tmp->insertFromMinibuffer(j - pos))
150 for (pos_type k = pos_end; k >= pos; --k) {
157 void mergeParagraph(BufferParams const & bparams, Paragraph * par)
159 Paragraph * the_next = par->next();
161 // first the DTP-stuff
162 par->params().lineBottom(the_next->params().lineBottom());
163 par->params().spaceBottom(the_next->params().spaceBottom());
164 par->params().pagebreakBottom(the_next->params().pagebreakBottom());
166 pos_type pos_end = the_next->size() - 1;
167 pos_type pos_insert = par->size();
169 // ok, now copy the paragraph
170 for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
171 the_next->cutIntoMinibuffer(bparams, i);
172 if (par->insertFromMinibuffer(pos_insert + j))
176 // delete the next paragraph
177 Paragraph * ppar = the_next->previous();
178 Paragraph * npar = the_next->next();
185 Paragraph * depthHook(Paragraph * par, Paragraph::depth_type depth)
187 Paragraph * newpar = par;
190 newpar = newpar->previous();
191 } while (newpar && newpar->getDepth() > depth);
194 if (par->previous() || par->getDepth())
195 lyxerr << "Error (depthHook): "
196 << "no hook." << endl;
203 Paragraph * outerHook(Paragraph * par)
205 if (!par->getDepth())
207 return depthHook(par, Paragraph::depth_type(par->getDepth() - 1));
211 bool isFirstInSequence(Paragraph * par)
213 Paragraph const * dhook = depthHook(par, par->getDepth());
215 || dhook->getLayout() != par->getLayout()
216 || dhook->getDepth() != par->getDepth());
220 int getEndLabel(Paragraph * para, BufferParams const & bparams)
222 Paragraph * par = para;
224 Paragraph::depth_type par_depth = par->getDepth();
225 layout_type layout = par->getLayout();
226 int const endlabeltype =
227 textclasslist.Style(bparams.textclass,
228 layout).endlabeltype;
229 if (endlabeltype != END_LABEL_NO_LABEL) {
233 Paragraph::depth_type const next_depth =
234 para->next()->getDepth();
235 if (par_depth > next_depth ||
236 (par_depth == next_depth
237 && layout != para->next()->getLayout()))
243 par = outerHook(par);
245 return END_LABEL_NO_LABEL;
250 ParagraphList::iterator
251 TeXDeeper(Buffer const * buf,
252 BufferParams const & bparams,
253 ParagraphList::iterator pit,
254 ostream & os, TexRow & texrow)
256 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
257 ParagraphList::iterator par = pit;
259 while (par != buf->paragraphs.end()&& par->params().depth() == pit->params().depth()) {
260 if (par->layout()->isEnvironment()) {
261 par = TeXEnvironment(buf, bparams, par,
264 par = TeXOnePar(buf, bparams, par,
268 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
274 ParagraphList::iterator
275 TeXEnvironment(Buffer const * buf,
276 BufferParams const & bparams,
277 ParagraphList::iterator pit,
278 ostream & os, TexRow & texrow)
280 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
282 LyXLayout_ptr const & style = pit->layout();
284 Language const * language = pit->getParLanguage(bparams);
285 Language const * doc_language = bparams.language;
286 Language const * previous_language = pit->previous()
287 ? pit->previous()->getParLanguage(bparams) : doc_language;
288 if (language->babel() != previous_language->babel()) {
290 if (!lyxrc.language_command_end.empty() &&
291 previous_language->babel() != doc_language->babel()) {
292 os << subst(lyxrc.language_command_end, "$$lang",
293 previous_language->babel())
298 if (lyxrc.language_command_end.empty() ||
299 language->babel() != doc_language->babel()) {
300 os << subst(lyxrc.language_command_begin, "$$lang",
307 bool leftindent_open = false;
308 if (!pit->params().leftIndent().zero()) {
309 os << "\\begin{LyXParagraphLeftIndent}{" <<
310 pit->params().leftIndent().asLatexString() << "}\n";
312 leftindent_open = true;
315 if (style->isEnvironment()) {
316 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
317 os << "\\begin{" << style->latexname() << "}{"
318 << pit->params().labelWidthString() << "}\n";
319 } else if (style->labeltype == LABEL_BIBLIO) {
321 os << "\\begin{" << style->latexname() << "}{"
322 << bibitemWidest(buf)
324 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
325 os << "\\begin{" << style->latexname() << '}'
326 << style->latexparam() << '\n';
328 os << "\\begin{" << style->latexname() << '}'
329 << style->latexparam() << '\n';
332 ParagraphList::iterator par = pit;
334 par = TeXOnePar(buf, bparams, par, os, texrow, false);
336 if (par != buf->paragraphs.end()&& par->params().depth() > pit->params().depth()) {
337 if (par->layout()->isParagraph()) {
340 // How to handle this? (Lgb)
341 //&& !suffixIs(os, "\n\n")
343 // There should be at least one '\n' already
344 // but we need there to be two for Standard
345 // paragraphs that are depth-increment'ed to be
346 // output correctly. However, tables can
347 // also be paragraphs so don't adjust them.
350 // Will it ever harm to have one '\n' too
351 // many? i.e. that we sometimes will have
352 // three in a row. (Lgb)
356 par = TeXDeeper(buf, bparams, par, os, texrow);
358 } while (par != buf->paragraphs.end()
359 && par->layout() == pit->layout()
360 && par->params().depth() == pit->params().depth()
361 && par->params().leftIndent() == pit->params().leftIndent());
363 if (style->isEnvironment()) {
364 os << "\\end{" << style->latexname() << "}\n";
368 if (leftindent_open) {
369 os << "\\end{LyXParagraphLeftIndent}\n";
373 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
374 return par; // ale970302
380 InsetOptArg * optArgInset(Paragraph const & par)
383 InsetList::iterator it = par.insetlist.begin();
384 InsetList::iterator end = par.insetlist.end();
385 for (; it != end; ++it) {
386 Inset * ins = it.getInset();
387 if (ins->lyxCode() == Inset::OPTARG_CODE) {
388 return static_cast<InsetOptArg *>(ins);
397 ParagraphList::iterator
398 TeXOnePar(Buffer const * buf,
399 BufferParams const & bparams,
400 ParagraphList::iterator pit,
401 ostream & os, TexRow & texrow,
404 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << endl;
405 Inset const * in = pit->inInset();
406 bool further_blank_line = false;
409 // well we have to check if we are in an inset with unlimited
410 // lenght (all in one row) if that is true then we don't allow
411 // any special options in the paragraph and also we don't allow
412 // any environment other then "Standard" to be valid!
413 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
414 style = pit->layout();
416 if (pit->params().startOfAppendix()) {
417 os << "\\appendix\n";
421 if (!pit->params().spacing().isDefault()
422 && (!pit->previous() || !pit->previous()->hasSameLayout(&*pit))) {
423 os << pit->params().spacing().writeEnvirBegin() << '\n';
427 if (style->isCommand()) {
432 if (pit->params().pagebreakTop()) {
434 further_blank_line = true;
436 if (pit->params().spaceTop().kind() != VSpace::NONE) {
437 os << pit->params().spaceTop().asLatexCommand(bparams);
438 further_blank_line = true;
441 if (pit->params().lineTop()) {
442 os << "\\lyxline{\\" << pit->getFont(bparams, 0).latexSize() << '}'
443 << "\\vspace{-1\\parskip}";
444 further_blank_line = true;
447 if (further_blank_line) {
452 style = bparams.getLyXTextClass().defaultLayout();
455 Language const * language = pit->getParLanguage(bparams);
456 Language const * doc_language = bparams.language;
457 Language const * previous_language = pit->previous()
458 ? pit->previous()->getParLanguage(bparams) : doc_language;
460 if (language->babel() != previous_language->babel()
461 // check if we already put language command in TeXEnvironment()
462 && !(style->isEnvironment()
463 && (!pit->previous() ||
464 (pit->previous()->layout() != pit->layout() &&
465 pit->previous()->getDepth() <= pit->getDepth())
466 || pit->previous()->getDepth() < pit->getDepth())))
468 if (!lyxrc.language_command_end.empty() &&
469 previous_language->babel() != doc_language->babel())
471 os << subst(lyxrc.language_command_end, "$$lang",
472 previous_language->babel())
477 if (lyxrc.language_command_end.empty() ||
478 language->babel() != doc_language->babel())
480 os << subst(lyxrc.language_command_begin, "$$lang",
487 if (bparams.inputenc == "auto" &&
488 language->encoding() != previous_language->encoding()) {
489 os << "\\inputencoding{"
490 << language->encoding()->LatexName()
495 switch (style->latextype) {
497 os << '\\' << style->latexname();
499 // Separate handling of optional argument inset.
500 if (style->optionalargs == 1) {
501 InsetOptArg * it = optArgInset(*pit);
503 it->latexOptional(buf, os, false, false);
506 os << style->latexparam();
508 case LATEX_ITEM_ENVIRONMENT:
509 case LATEX_LIST_ENVIRONMENT:
512 case LATEX_BIB_ENVIRONMENT:
513 // ignore this, the inset will write itself
519 bool need_par = pit->simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
521 // Make sure that \\par is done with the font of the last
522 // character if this has another size as the default.
523 // This is necessary because LaTeX (and LyX on the screen)
524 // calculates the space between the baselines according
525 // to this font. (Matthias)
527 // Is this really needed ? (Dekel)
528 // We do not need to use to change the font for the last paragraph
532 ? pit->getLayoutFont(bparams) : pit->getFont(bparams, pit->size() - 1));
534 bool is_command = style->isCommand();
536 if (style->resfont.size() != font.size() && pit->next() && !is_command) {
539 os << "\\" << font.latexSize() << " \\par}";
540 } else if (need_par) {
542 } else if (is_command)
545 switch (style->latextype) {
546 case LATEX_ITEM_ENVIRONMENT:
547 case LATEX_LIST_ENVIRONMENT:
548 if (pit->next() && (pit->params().depth() < pit->next()->params().depth())) {
553 case LATEX_ENVIRONMENT:
554 // if its the last paragraph of the current environment
555 // skip it otherwise fall through
557 && (pit->next()->layout() != pit->layout()
558 || pit->next()->params().depth() != pit->params().depth()))
560 // fall through possible
562 // we don't need it for the last paragraph!!!
569 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
570 further_blank_line = false;
571 if (pit->params().lineBottom()) {
572 os << "\\lyxline{\\" << font.latexSize() << '}';
573 further_blank_line = true;
576 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
577 os << pit->params().spaceBottom().asLatexCommand(bparams);
578 further_blank_line = true;
581 if (pit->params().pagebreakBottom()) {
583 further_blank_line = true;
586 if (further_blank_line) {
591 if (!pit->params().spacing().isDefault()
592 && (!pit->next() || !pit->next()->hasSameLayout(&*pit))) {
593 os << pit->params().spacing().writeEnvirEnd() << '\n';
598 // we don't need it for the last paragraph!!!
603 // Since \selectlanguage write the language to the aux file,
604 // we need to reset the language at the end of footnote or
607 if (language->babel() != doc_language->babel()) {
608 if (lyxrc.language_command_end.empty())
609 os << subst(lyxrc.language_command_begin,
611 doc_language->babel())
614 os << subst(lyxrc.language_command_end,
622 lyxerr[Debug::LATEX] << "TeXOnePar...done " << pit->next() << endl;