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::iterator par,
37 // create a new paragraph, and insert into the list
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->setChange(j - pos, change);
96 for (i = pos_end; i >= pos; --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,
130 ParagraphList::iterator par,
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, ParagraphList::iterator par)
159 ParagraphList::iterator the_next = boost::next(par);
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 #warning a ParagraphList::erase is needed. (Lgb)
178 // Isn't this really just par?
179 ParagraphList::iterator ppar = boost::prior(the_next);
180 ParagraphList::iterator npar = boost::next(the_next);
187 Paragraph * depthHook(Paragraph * par, Paragraph::depth_type depth)
189 Paragraph * newpar = par;
192 newpar = newpar->previous();
193 } while (newpar && newpar->getDepth() > depth);
196 if (par->previous() || par->getDepth())
197 lyxerr << "Error (depthHook): "
198 << "no hook." << endl;
205 Paragraph * outerHook(Paragraph * par)
207 if (!par->getDepth())
209 return depthHook(par, Paragraph::depth_type(par->getDepth() - 1));
213 bool isFirstInSequence(Paragraph * par)
215 Paragraph const * dhook = depthHook(par, par->getDepth());
217 || dhook->getLayout() != par->getLayout()
218 || dhook->getDepth() != par->getDepth());
222 int getEndLabel(Paragraph * para, BufferParams const & bparams)
224 Paragraph * par = para;
226 Paragraph::depth_type par_depth = par->getDepth();
227 layout_type layout = par->getLayout();
228 int const endlabeltype =
229 textclasslist.Style(bparams.textclass,
230 layout).endlabeltype;
231 if (endlabeltype != END_LABEL_NO_LABEL) {
235 Paragraph::depth_type const next_depth =
236 para->next()->getDepth();
237 if (par_depth > next_depth ||
238 (par_depth == next_depth
239 && layout != para->next()->getLayout()))
245 par = outerHook(par);
247 return END_LABEL_NO_LABEL;
252 ParagraphList::iterator
253 TeXDeeper(Buffer const * buf,
254 BufferParams const & bparams,
255 ParagraphList::iterator pit,
256 ostream & os, TexRow & texrow)
258 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
259 ParagraphList::iterator par = pit;
261 while (par != buf->paragraphs.end()&& par->params().depth() == pit->params().depth()) {
262 if (par->layout()->isEnvironment()) {
263 par = TeXEnvironment(buf, bparams, par,
266 par = TeXOnePar(buf, bparams, par,
270 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
276 ParagraphList::iterator
277 TeXEnvironment(Buffer const * buf,
278 BufferParams const & bparams,
279 ParagraphList::iterator pit,
280 ostream & os, TexRow & texrow)
282 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
284 LyXLayout_ptr const & style = pit->layout();
286 Language const * language = pit->getParLanguage(bparams);
287 Language const * doc_language = bparams.language;
288 Language const * previous_language = pit->previous()
289 ? pit->previous()->getParLanguage(bparams) : doc_language;
290 if (language->babel() != previous_language->babel()) {
292 if (!lyxrc.language_command_end.empty() &&
293 previous_language->babel() != doc_language->babel()) {
294 os << subst(lyxrc.language_command_end, "$$lang",
295 previous_language->babel())
300 if (lyxrc.language_command_end.empty() ||
301 language->babel() != doc_language->babel()) {
302 os << subst(lyxrc.language_command_begin, "$$lang",
309 bool leftindent_open = false;
310 if (!pit->params().leftIndent().zero()) {
311 os << "\\begin{LyXParagraphLeftIndent}{" <<
312 pit->params().leftIndent().asLatexString() << "}\n";
314 leftindent_open = true;
317 if (style->isEnvironment()) {
318 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
319 os << "\\begin{" << style->latexname() << "}{"
320 << pit->params().labelWidthString() << "}\n";
321 } else if (style->labeltype == LABEL_BIBLIO) {
323 os << "\\begin{" << style->latexname() << "}{"
324 << bibitemWidest(buf)
326 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
327 os << "\\begin{" << style->latexname() << '}'
328 << style->latexparam() << '\n';
330 os << "\\begin{" << style->latexname() << '}'
331 << style->latexparam() << '\n';
334 ParagraphList::iterator par = pit;
336 par = TeXOnePar(buf, bparams, par, os, texrow, false);
338 if (par != buf->paragraphs.end()&& par->params().depth() > pit->params().depth()) {
339 if (par->layout()->isParagraph()) {
342 // How to handle this? (Lgb)
343 //&& !suffixIs(os, "\n\n")
345 // There should be at least one '\n' already
346 // but we need there to be two for Standard
347 // paragraphs that are depth-increment'ed to be
348 // output correctly. However, tables can
349 // also be paragraphs so don't adjust them.
352 // Will it ever harm to have one '\n' too
353 // many? i.e. that we sometimes will have
354 // three in a row. (Lgb)
358 par = TeXDeeper(buf, bparams, par, os, texrow);
360 } while (par != buf->paragraphs.end()
361 && par->layout() == pit->layout()
362 && par->params().depth() == pit->params().depth()
363 && par->params().leftIndent() == pit->params().leftIndent());
365 if (style->isEnvironment()) {
366 os << "\\end{" << style->latexname() << "}\n";
370 if (leftindent_open) {
371 os << "\\end{LyXParagraphLeftIndent}\n";
375 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
376 return par; // ale970302
382 InsetOptArg * optArgInset(Paragraph const & par)
385 InsetList::iterator it = par.insetlist.begin();
386 InsetList::iterator end = par.insetlist.end();
387 for (; it != end; ++it) {
388 Inset * ins = it.getInset();
389 if (ins->lyxCode() == Inset::OPTARG_CODE) {
390 return static_cast<InsetOptArg *>(ins);
399 ParagraphList::iterator
400 TeXOnePar(Buffer const * buf,
401 BufferParams const & bparams,
402 ParagraphList::iterator pit,
403 ostream & os, TexRow & texrow,
406 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << endl;
407 Inset const * in = pit->inInset();
408 bool further_blank_line = false;
411 // well we have to check if we are in an inset with unlimited
412 // lenght (all in one row) if that is true then we don't allow
413 // any special options in the paragraph and also we don't allow
414 // any environment other then "Standard" to be valid!
415 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
416 style = pit->layout();
418 if (pit->params().startOfAppendix()) {
419 os << "\\appendix\n";
423 if (!pit->params().spacing().isDefault()
424 && (!pit->previous() || !pit->previous()->hasSameLayout(&*pit))) {
425 os << pit->params().spacing().writeEnvirBegin() << '\n';
429 if (style->isCommand()) {
434 if (pit->params().pagebreakTop()) {
436 further_blank_line = true;
438 if (pit->params().spaceTop().kind() != VSpace::NONE) {
439 os << pit->params().spaceTop().asLatexCommand(bparams);
440 further_blank_line = true;
443 if (pit->params().lineTop()) {
444 os << "\\lyxline{\\" << pit->getFont(bparams, 0).latexSize() << '}'
445 << "\\vspace{-1\\parskip}";
446 further_blank_line = true;
449 if (further_blank_line) {
454 style = bparams.getLyXTextClass().defaultLayout();
457 Language const * language = pit->getParLanguage(bparams);
458 Language const * doc_language = bparams.language;
459 Language const * previous_language = pit->previous()
460 ? pit->previous()->getParLanguage(bparams) : doc_language;
462 if (language->babel() != previous_language->babel()
463 // check if we already put language command in TeXEnvironment()
464 && !(style->isEnvironment()
465 && (!pit->previous() ||
466 (pit->previous()->layout() != pit->layout() &&
467 pit->previous()->getDepth() <= pit->getDepth())
468 || pit->previous()->getDepth() < pit->getDepth())))
470 if (!lyxrc.language_command_end.empty() &&
471 previous_language->babel() != doc_language->babel())
473 os << subst(lyxrc.language_command_end, "$$lang",
474 previous_language->babel())
479 if (lyxrc.language_command_end.empty() ||
480 language->babel() != doc_language->babel())
482 os << subst(lyxrc.language_command_begin, "$$lang",
489 if (bparams.inputenc == "auto" &&
490 language->encoding() != previous_language->encoding()) {
491 os << "\\inputencoding{"
492 << language->encoding()->LatexName()
497 switch (style->latextype) {
499 os << '\\' << style->latexname();
501 // Separate handling of optional argument inset.
502 if (style->optionalargs == 1) {
503 InsetOptArg * it = optArgInset(*pit);
505 it->latexOptional(buf, os, false, false);
508 os << style->latexparam();
510 case LATEX_ITEM_ENVIRONMENT:
511 case LATEX_LIST_ENVIRONMENT:
514 case LATEX_BIB_ENVIRONMENT:
515 // ignore this, the inset will write itself
521 bool need_par = pit->simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
523 // Make sure that \\par is done with the font of the last
524 // character if this has another size as the default.
525 // This is necessary because LaTeX (and LyX on the screen)
526 // calculates the space between the baselines according
527 // to this font. (Matthias)
529 // Is this really needed ? (Dekel)
530 // We do not need to use to change the font for the last paragraph
534 ? pit->getLayoutFont(bparams) : pit->getFont(bparams, pit->size() - 1));
536 bool is_command = style->isCommand();
538 if (style->resfont.size() != font.size() && pit->next() && !is_command) {
541 os << "\\" << font.latexSize() << " \\par}";
542 } else if (need_par) {
544 } else if (is_command)
547 switch (style->latextype) {
548 case LATEX_ITEM_ENVIRONMENT:
549 case LATEX_LIST_ENVIRONMENT:
550 if (pit->next() && (pit->params().depth() < pit->next()->params().depth())) {
555 case LATEX_ENVIRONMENT:
556 // if its the last paragraph of the current environment
557 // skip it otherwise fall through
559 && (pit->next()->layout() != pit->layout()
560 || pit->next()->params().depth() != pit->params().depth()))
562 // fall through possible
564 // we don't need it for the last paragraph!!!
571 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
572 further_blank_line = false;
573 if (pit->params().lineBottom()) {
574 os << "\\lyxline{\\" << font.latexSize() << '}';
575 further_blank_line = true;
578 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
579 os << pit->params().spaceBottom().asLatexCommand(bparams);
580 further_blank_line = true;
583 if (pit->params().pagebreakBottom()) {
585 further_blank_line = true;
588 if (further_blank_line) {
593 if (!pit->params().spacing().isDefault()
594 && (!pit->next() || !pit->next()->hasSameLayout(&*pit))) {
595 os << pit->params().spacing().writeEnvirEnd() << '\n';
600 // we don't need it for the last paragraph!!!
605 // Since \selectlanguage write the language to the aux file,
606 // we need to reset the language at the end of footnote or
609 if (language->babel() != doc_language->babel()) {
610 if (lyxrc.language_command_end.empty())
611 os << subst(lyxrc.language_command_begin,
613 doc_language->babel())
616 os << subst(lyxrc.language_command_end,
624 lyxerr[Debug::LATEX] << "TeXOnePar...done " << pit->next() << endl;