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"
16 #include "bufferparams.h"
20 #include "errorlist.h"
23 #include "iterators.h"
27 #include "paragraph_pimpl.h"
32 #include "insets/insetbibitem.h"
33 #include "insets/insethfill.h"
34 #include "insets/insetlatexaccent.h"
35 #include "insets/insetline.h"
36 #include "insets/insetnewline.h"
37 #include "insets/insetpagebreak.h"
38 #include "insets/insetoptarg.h"
39 #include "insets/insetspace.h"
40 #include "insets/insetspecialchar.h"
41 #include "insets/insettabular.h"
43 #include "support/filetools.h"
44 #include "support/lstrings.h"
45 #include "support/lyxlib.h"
46 #include "support/std_sstream.h"
52 using lyx::support::atoi;
53 using lyx::support::bformat;
54 using lyx::support::split;
55 using lyx::support::subst;
60 using std::istringstream;
63 extern string bibitemWidest(Buffer const &);
68 bool moveItem(Paragraph & from, Paragraph & to,
69 BufferParams const & params, pos_type i, pos_type j)
71 char const tmpchar = from.getChar(i);
72 LyXFont tmpfont = from.getFontSettings(params, i);
74 if (tmpchar == Paragraph::META_INSET) {
75 InsetOld * tmpinset = 0;
76 if (from.getInset(i)) {
77 // the inset is not in a paragraph anymore
78 tmpinset = from.insetlist.release(i);
81 if (!to.insetAllowed(tmpinset->lyxCode()))
83 to.insertInset(j, tmpinset, tmpfont);
85 if (!to.checkInsertChar(tmpfont))
87 to.insertChar(j, tmpchar, tmpfont);
95 void breakParagraph(BufferParams const & bparams,
96 ParagraphList & paragraphs,
97 ParagraphList::iterator par,
101 // create a new paragraph, and insert into the list
102 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
105 // without doing that we get a crash when typing <Return> at the
106 // end of a paragraph
107 tmp->layout(bparams.getLyXTextClass().defaultLayout());
108 // remember to set the inset_owner
109 tmp->setInsetOwner(par->inInset());
111 if (bparams.tracking_changes)
114 // this is an idea for a more userfriendly layout handling, I will
115 // see what the users say
117 // layout stays the same with latex-environments
119 tmp->layout(par->layout());
120 tmp->setLabelWidthString(par->params().labelWidthString());
123 bool const isempty = (par->allowEmpty() && par->empty());
125 if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
126 tmp->layout(par->layout());
127 tmp->params().align(par->params().align());
128 tmp->setLabelWidthString(par->params().labelWidthString());
130 tmp->params().spaceBottom(par->params().spaceBottom());
131 par->params().spaceBottom(VSpace(VSpace::NONE));
133 tmp->params().depth(par->params().depth());
134 tmp->params().noindent(par->params().noindent());
136 // copy everything behind the break-position
137 // to the new paragraph
140 #warning this seems wrong
142 /* FIXME: if !keepempty, empty() == true, then we reach
143 * here with size() == 0. So pos_end becomes - 1. Why
144 * doesn't this cause problems ???
146 pos_type pos_end = par->size() - 1;
150 for (; i <= pos_end; ++i) {
151 Change::Type change = par->lookupChange(i);
152 if (moveItem(*par, *tmp, bparams, i, j - pos)) {
153 tmp->setChange(j - pos, change);
158 for (i = pos_end; i >= pos; --i)
165 tmp->params().spaceTop(par->params().spaceTop());
166 par->params().clear();
168 par->layout(bparams.getLyXTextClass().defaultLayout());
170 // layout stays the same with latex-environments
172 par->layout(tmp->layout());
173 par->setLabelWidthString(tmp->params().labelWidthString());
174 par->params().depth(tmp->params().depth());
177 // subtle, but needed to get empty pars working right
178 if (bparams.tracking_changes) {
181 } else if (!tmp->size()) {
188 void breakParagraphConservative(BufferParams const & bparams,
189 ParagraphList & paragraphs,
190 ParagraphList::iterator par,
193 // create a new paragraph
194 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
196 tmp->makeSameLayout(*par);
198 // When can pos > size()?
199 // I guess pos == size() is possible.
200 if (par->size() > pos) {
201 // copy everything behind the break-position to the new
203 pos_type pos_end = par->size() - 1;
205 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
206 if (moveItem(*par, *tmp, bparams, i, j - pos))
209 for (pos_type k = pos_end; k >= pos; --k)
215 void mergeParagraph(BufferParams const & bparams,
216 ParagraphList & paragraphs,
217 ParagraphList::iterator par)
219 ParagraphList::iterator the_next = boost::next(par);
221 // first the DTP-stuff
222 par->params().spaceBottom(the_next->params().spaceBottom());
224 pos_type pos_end = the_next->size() - 1;
225 pos_type pos_insert = par->size();
227 // ok, now copy the paragraph
228 for (pos_type i = 0, j = 0; i <= pos_end; ++i)
229 if (moveItem(*the_next, *par, bparams, i, pos_insert + j))
232 paragraphs.erase(the_next);
236 ParagraphList::iterator depthHook(ParagraphList::iterator pit,
237 ParagraphList const & plist,
238 Paragraph::depth_type depth)
240 ParagraphList::iterator newpit = pit;
241 ParagraphList::iterator beg = const_cast<ParagraphList&>(plist).begin();
246 while (newpit != beg && newpit->getDepth() > depth) {
250 if (newpit->getDepth() > depth)
257 ParagraphList::iterator outerHook(ParagraphList::iterator pit,
258 ParagraphList const & plist)
260 if (!pit->getDepth())
261 return const_cast<ParagraphList&>(plist).end();
262 return depthHook(pit, plist,
263 Paragraph::depth_type(pit->getDepth() - 1));
267 bool isFirstInSequence(ParagraphList::iterator pit,
268 ParagraphList const & plist)
270 ParagraphList::iterator dhook = depthHook(pit, plist, pit->getDepth());
272 || dhook->layout() != pit->layout()
273 || dhook->getDepth() != pit->getDepth());
277 int getEndLabel(ParagraphList::iterator p, ParagraphList const & plist)
279 ParagraphList::iterator pit = p;
280 Paragraph::depth_type par_depth = p->getDepth();
281 while (pit != const_cast<ParagraphList&>(plist).end()) {
282 LyXLayout_ptr const & layout = pit->layout();
283 int const endlabeltype = layout->endlabeltype;
285 if (endlabeltype != END_LABEL_NO_LABEL) {
286 if (boost::next(p) == const_cast<ParagraphList&>(plist).end())
289 Paragraph::depth_type const next_depth = boost::next(p)->getDepth();
290 if (par_depth > next_depth ||
291 (par_depth == next_depth &&
292 layout != boost::next(p)->layout()))
298 pit = outerHook(pit, plist);
299 if (pit != const_cast<ParagraphList&>(plist).end())
300 par_depth = pit->getDepth();
302 return END_LABEL_NO_LABEL;
308 ParagraphList::iterator
309 TeXEnvironment(Buffer const & buf,
310 ParagraphList const & paragraphs,
311 ParagraphList::iterator pit,
312 ostream & os, TexRow & texrow,
313 LatexRunParams const & runparams);
315 ParagraphList::iterator
316 TeXOnePar(Buffer const & buf,
317 ParagraphList const & paragraphs,
318 ParagraphList::iterator pit,
319 ostream & os, TexRow & texrow,
320 LatexRunParams const & runparams,
321 string const & everypar = string());
324 ParagraphList::iterator
325 TeXDeeper(Buffer const & buf,
326 ParagraphList const & paragraphs,
327 ParagraphList::iterator pit,
328 ostream & os, TexRow & texrow,
329 LatexRunParams const & runparams)
331 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
332 ParagraphList::iterator par = pit;
334 while (par != const_cast<ParagraphList&>(paragraphs).end() &&
335 par->params().depth() == pit->params().depth()) {
336 if (par->layout()->isEnvironment()) {
337 par = TeXEnvironment(buf, paragraphs, par,
338 os, texrow, runparams);
340 par = TeXOnePar(buf, paragraphs, par,
341 os, texrow, runparams);
344 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
350 ParagraphList::iterator
351 TeXEnvironment(Buffer const & buf,
352 ParagraphList const & paragraphs,
353 ParagraphList::iterator pit,
354 ostream & os, TexRow & texrow,
355 LatexRunParams const & runparams)
357 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
359 BufferParams const & bparams = buf.params();
361 LyXLayout_ptr const & style = pit->layout();
363 Language const * language = pit->getParLanguage(bparams);
364 Language const * doc_language = bparams.language;
365 Language const * previous_language =
366 (pit != const_cast<ParagraphList&>(paragraphs).begin())
367 ? boost::prior(pit)->getParLanguage(bparams)
369 if (language->babel() != previous_language->babel()) {
371 if (!lyxrc.language_command_end.empty() &&
372 previous_language->babel() != doc_language->babel()) {
373 os << subst(lyxrc.language_command_end, "$$lang",
374 previous_language->babel())
379 if (lyxrc.language_command_end.empty() ||
380 language->babel() != doc_language->babel()) {
381 os << subst(lyxrc.language_command_begin, "$$lang",
388 bool leftindent_open = false;
389 if (!pit->params().leftIndent().zero()) {
390 os << "\\begin{LyXParagraphLeftIndent}{" <<
391 pit->params().leftIndent().asLatexString() << "}\n";
393 leftindent_open = true;
396 if (style->isEnvironment()) {
397 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
398 os << "\\begin{" << style->latexname() << "}{"
399 << pit->params().labelWidthString() << "}\n";
400 } else if (style->labeltype == LABEL_BIBLIO) {
402 os << "\\begin{" << style->latexname() << "}{"
403 << bibitemWidest(buf)
405 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
406 os << "\\begin{" << style->latexname() << '}'
407 << style->latexparam() << '\n';
409 os << "\\begin{" << style->latexname() << '}'
410 << style->latexparam() << '\n';
413 ParagraphList::iterator par = pit;
415 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
417 if (par != const_cast<ParagraphList&>(paragraphs).end() && par->params().depth() > pit->params().depth()) {
418 if (par->layout()->isParagraph()) {
421 // How to handle this? (Lgb)
422 //&& !suffixIs(os, "\n\n")
424 // There should be at least one '\n' already
425 // but we need there to be two for Standard
426 // paragraphs that are depth-increment'ed to be
427 // output correctly. However, tables can
428 // also be paragraphs so don't adjust them.
431 // Will it ever harm to have one '\n' too
432 // many? i.e. that we sometimes will have
433 // three in a row. (Lgb)
437 par = TeXDeeper(buf, paragraphs, par, os, texrow,
440 } while (par != const_cast<ParagraphList&>(paragraphs).end()
441 && par->layout() == pit->layout()
442 && par->params().depth() == pit->params().depth()
443 && par->params().leftIndent() == pit->params().leftIndent());
445 if (style->isEnvironment()) {
446 os << "\\end{" << style->latexname() << "}\n";
450 if (leftindent_open) {
451 os << "\\end{LyXParagraphLeftIndent}\n";
455 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
456 return par; // ale970302
460 InsetOptArg * optArgInset(Paragraph const & par)
463 InsetList::const_iterator it = par.insetlist.begin();
464 InsetList::const_iterator end = par.insetlist.end();
465 for (; it != end; ++it) {
466 InsetOld * ins = it->inset;
467 if (ins->lyxCode() == InsetOld::OPTARG_CODE) {
468 return static_cast<InsetOptArg *>(ins);
475 ParagraphList::iterator
476 TeXOnePar(Buffer const & buf,
477 ParagraphList const & paragraphs,
478 ParagraphList::iterator pit,
479 ostream & os, TexRow & texrow,
480 LatexRunParams const & runparams,
481 string const & everypar)
483 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
484 << everypar << "'" << endl;
485 BufferParams const & bparams = buf.params();
487 InsetOld const * in = pit->inInset();
488 bool further_blank_line = false;
491 // well we have to check if we are in an inset with unlimited
492 // length (all in one row) if that is true then we don't allow
493 // any special options in the paragraph and also we don't allow
494 // any environment other then "Standard" to be valid!
495 if (in == 0 || !in->forceDefaultParagraphs(in)) {
496 style = pit->layout();
498 if (pit->params().startOfAppendix()) {
499 os << "\\appendix\n";
503 if (!pit->params().spacing().isDefault()
504 && (pit == const_cast<ParagraphList&>(paragraphs).begin()
505 || !boost::prior(pit)->hasSameLayout(*pit)))
507 os << pit->params().spacing().writeEnvirBegin() << '\n';
511 if (style->isCommand()) {
516 if (pit->params().spaceTop().kind() != VSpace::NONE) {
517 os << pit->params().spaceTop().asLatexCommand(bparams);
518 further_blank_line = true;
521 if (further_blank_line) {
526 style = bparams.getLyXTextClass().defaultLayout();
529 Language const * language = pit->getParLanguage(bparams);
530 Language const * doc_language = bparams.language;
531 Language const * previous_language =
532 (pit != const_cast<ParagraphList&>(paragraphs).begin())
533 ? boost::prior(pit)->getParLanguage(bparams)
536 if (language->babel() != previous_language->babel()
537 // check if we already put language command in TeXEnvironment()
538 && !(style->isEnvironment()
539 && (pit == const_cast<ParagraphList&>(paragraphs).begin() ||
540 (boost::prior(pit)->layout() != pit->layout() &&
541 boost::prior(pit)->getDepth() <= pit->getDepth())
542 || boost::prior(pit)->getDepth() < pit->getDepth())))
544 if (!lyxrc.language_command_end.empty() &&
545 previous_language->babel() != doc_language->babel())
547 os << subst(lyxrc.language_command_end, "$$lang",
548 previous_language->babel())
553 if (lyxrc.language_command_end.empty() ||
554 language->babel() != doc_language->babel())
556 os << subst(lyxrc.language_command_begin, "$$lang",
563 if (bparams.inputenc == "auto" &&
564 language->encoding() != previous_language->encoding()) {
565 os << "\\inputencoding{"
566 << language->encoding()->LatexName()
571 switch (style->latextype) {
573 os << '\\' << style->latexname();
575 // Separate handling of optional argument inset.
576 if (style->optionalargs == 1) {
577 InsetOptArg * it = optArgInset(*pit);
579 it->latexOptional(buf, os, runparams);
582 os << style->latexparam();
584 case LATEX_ITEM_ENVIRONMENT:
585 case LATEX_LIST_ENVIRONMENT:
588 case LATEX_BIB_ENVIRONMENT:
589 // ignore this, the inset will write itself
596 bool need_par = pit->simpleTeXOnePar(buf, bparams,
597 outerFont(pit, paragraphs),
598 os, texrow, runparams);
600 // Make sure that \\par is done with the font of the last
601 // character if this has another size as the default.
602 // This is necessary because LaTeX (and LyX on the screen)
603 // calculates the space between the baselines according
604 // to this font. (Matthias)
606 // Is this really needed ? (Dekel)
607 // We do not need to use to change the font for the last paragraph
609 LyXFont const outerfont(outerFont(pit, paragraphs));
613 ? pit->getLayoutFont(bparams, outerfont)
614 : pit->getFont(bparams, pit->size() - 1, outerfont));
616 bool is_command = style->isCommand();
618 if (style->resfont.size() != font.size()
619 && boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
623 os << "\\" << font.latexSize() << " \\par}";
624 } else if (need_par) {
626 } else if (is_command)
629 switch (style->latextype) {
630 case LATEX_ITEM_ENVIRONMENT:
631 case LATEX_LIST_ENVIRONMENT:
632 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
633 && (pit->params().depth() < boost::next(pit)->params().depth())) {
638 case LATEX_ENVIRONMENT: {
639 // if its the last paragraph of the current environment
640 // skip it otherwise fall through
641 ParagraphList::iterator next = boost::next(pit);
643 if (next != const_cast<ParagraphList&>(paragraphs).end()
644 && (next->layout() != pit->layout()
645 || next->params().depth() != pit->params().depth()))
649 // fall through possible
651 // we don't need it for the last paragraph!!!
652 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
658 if (in == 0 || !in->forceDefaultParagraphs(in)) {
659 further_blank_line = false;
661 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
662 os << pit->params().spaceBottom().asLatexCommand(bparams);
663 further_blank_line = true;
666 if (further_blank_line) {
671 if (!pit->params().spacing().isDefault()
672 && (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
673 || !boost::next(pit)->hasSameLayout(*pit)))
675 os << pit->params().spacing().writeEnvirEnd() << '\n';
680 // we don't need it for the last paragraph!!!
681 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
685 // Since \selectlanguage write the language to the aux file,
686 // we need to reset the language at the end of footnote or
689 if (language->babel() != doc_language->babel()) {
690 if (lyxrc.language_command_end.empty())
691 os << subst(lyxrc.language_command_begin,
693 doc_language->babel())
696 os << subst(lyxrc.language_command_end,
704 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
712 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
714 void latexParagraphs(Buffer const & buf,
715 ParagraphList const & paragraphs,
718 LatexRunParams const & runparams,
719 string const & everypar)
721 bool was_title = false;
722 bool already_title = false;
723 LyXTextClass const & tclass = buf.params().getLyXTextClass();
724 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
725 ParagraphList::iterator endpar = const_cast<ParagraphList&>(paragraphs).end();
728 while (par != endpar) {
729 InsetOld * in = par->inInset();
730 // well we have to check if we are in an inset with unlimited
731 // length (all in one row) if that is true then we don't allow
732 // any special options in the paragraph and also we don't allow
733 // any environment other then "Standard" to be valid!
734 if (in == 0 || !in->forceDefaultParagraphs(in)) {
735 LyXLayout_ptr const & layout = par->layout();
737 if (layout->intitle) {
739 lyxerr <<"Error in latexParagraphs: You"
740 " should not mix title layouts"
741 " with normal ones." << endl;
742 } else if (!was_title) {
744 if (tclass.titletype() == TITLE_ENVIRONMENT) {
746 << tclass.titlename()
751 } else if (was_title && !already_title) {
752 if (tclass.titletype() == TITLE_ENVIRONMENT) {
753 os << "\\end{" << tclass.titlename()
757 os << "\\" << tclass.titlename()
761 already_title = true;
765 if (layout->is_environment) {
766 par = TeXOnePar(buf, paragraphs, par, os, texrow,
767 runparams, everypar);
768 } else if (layout->isEnvironment() ||
769 !par->params().leftIndent().zero())
771 par = TeXEnvironment(buf, paragraphs, par, os,
774 par = TeXOnePar(buf, paragraphs, par, os, texrow,
775 runparams, everypar);
778 par = TeXOnePar(buf, paragraphs, par, os, texrow,
779 runparams, everypar);
782 // It might be that we only have a title in this document
783 if (was_title && !already_title) {
784 if (tclass.titletype() == TITLE_ENVIRONMENT) {
785 os << "\\end{" << tclass.titlename()
789 os << "\\" << tclass.titlename()
797 void linuxdocParagraphs(Buffer const & buf,
798 ParagraphList const & paragraphs,
802 Paragraph::depth_type depth = 0; // paragraph depth
804 vector<string> environment_stack(5);
806 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
807 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
808 for (; pit != pend; ++pit) {
809 LyXLayout_ptr const & style = pit->layout();
810 // treat <toc> as a special case for compatibility with old code
811 if (pit->isInset(0)) {
812 InsetOld * inset = pit->getInset(0);
813 InsetOld::Code lyx_code = inset->lyxCode();
814 if (lyx_code == InsetOld::TOC_CODE) {
815 string const temp = "toc";
816 sgml::openTag(os, depth, false, temp);
821 // environment tag closing
822 for (; depth > pit->params().depth(); --depth) {
823 sgml::closeTag(os, depth, false, environment_stack[depth]);
824 environment_stack[depth].erase();
827 // write opening SGML tags
828 switch (style->latextype) {
829 case LATEX_PARAGRAPH:
830 if (depth == pit->params().depth()
831 && !environment_stack[depth].empty()) {
832 sgml::closeTag(os, depth, false, environment_stack[depth]);
833 environment_stack[depth].erase();
839 sgml::openTag(os, depth, false, style->latexname());
844 //error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
847 if (!environment_stack[depth].empty()) {
848 sgml::closeTag(os, depth, false, environment_stack[depth]);
852 environment_stack[depth].erase();
853 sgml::openTag(os, depth, false, style->latexname());
856 case LATEX_ENVIRONMENT:
857 case LATEX_ITEM_ENVIRONMENT:
858 case LATEX_BIB_ENVIRONMENT:
860 string const & latexname = style->latexname();
862 if (depth == pit->params().depth()
863 && environment_stack[depth] != latexname) {
864 sgml::closeTag(os, depth, false,
865 environment_stack[depth]);
866 environment_stack[depth].erase();
868 if (depth < pit->params().depth()) {
869 depth = pit->params().depth();
870 environment_stack[depth].erase();
872 if (environment_stack[depth] != latexname) {
874 sgml::openTag(os, depth, false, "p");
876 sgml::openTag(os, depth, false, latexname);
878 if (environment_stack.size() == depth + 1)
879 environment_stack.push_back("!-- --");
880 environment_stack[depth] = latexname;
883 if (style->latexparam() == "CDATA")
886 if (style->latextype == LATEX_ENVIRONMENT) break;
888 if (style->labeltype == LABEL_MANUAL)
893 sgml::openTag(os, depth + 1, false, item_name);
898 sgml::openTag(os, depth, false, style->latexname());
902 pit->simpleLinuxDocOnePar(buf, os, outerFont(pit, paragraphs), depth);
905 // write closing SGML tags
906 switch (style->latextype) {
909 case LATEX_ENVIRONMENT:
910 case LATEX_ITEM_ENVIRONMENT:
911 case LATEX_BIB_ENVIRONMENT:
912 if (style->latexparam() == "CDATA")
916 sgml::closeTag(os, depth, false, style->latexname());
922 for (int i = depth; i >= 0; --i)
923 sgml::closeTag(os, depth, false, environment_stack[i]);
927 void docbookParagraphs(Buffer const & buf,
928 ParagraphList const & paragraphs,
931 vector<string> environment_stack(10);
932 vector<string> environment_inner(10);
933 vector<string> command_stack(10);
935 bool command_flag = false;
936 Paragraph::depth_type command_depth = 0;
937 Paragraph::depth_type command_base = 0;
938 Paragraph::depth_type cmd_depth = 0;
939 Paragraph::depth_type depth = 0; // paragraph depth
944 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
945 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
947 for (; par != pend; ++par) {
951 int desc_on = 0; // description mode
953 LyXLayout_ptr const & style = par->layout();
955 // environment tag closing
956 for (; depth > par->params().depth(); --depth) {
957 if (!environment_inner[depth].empty())
958 sgml::closeEnvTags(os, false, environment_inner[depth],
959 command_depth + depth);
960 sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
961 environment_stack[depth].erase();
962 environment_inner[depth].erase();
965 if (depth == par->params().depth()
966 && environment_stack[depth] != style->latexname()
967 && !environment_stack[depth].empty()) {
968 sgml::closeEnvTags(os, false, environment_inner[depth],
969 command_depth + depth);
970 sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
972 environment_stack[depth].erase();
973 environment_inner[depth].erase();
976 // Write opening SGML tags.
977 switch (style->latextype) {
978 case LATEX_PARAGRAPH:
979 sgml::openTag(os, depth + command_depth,
980 false, style->latexname());
985 //error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
988 command_name = style->latexname();
990 sgmlparam = style->latexparam();
991 c_params = split(sgmlparam, c_depth,'|');
993 cmd_depth = atoi(c_depth);
996 if (cmd_depth < command_base) {
997 for (Paragraph::depth_type j = command_depth;
998 j >= command_base; --j) {
999 sgml::closeTag(os, j, false, command_stack[j]);
1002 command_depth = command_base = cmd_depth;
1003 } else if (cmd_depth <= command_depth) {
1004 for (int j = command_depth;
1005 j >= int(cmd_depth); --j) {
1006 sgml::closeTag(os, j, false, command_stack[j]);
1009 command_depth = cmd_depth;
1011 command_depth = cmd_depth;
1013 command_depth = command_base = cmd_depth;
1014 command_flag = true;
1016 if (command_stack.size() == command_depth + 1)
1017 command_stack.push_back(string());
1018 command_stack[command_depth] = command_name;
1020 // treat label as a special case for
1021 // more WYSIWYM handling.
1022 // This is a hack while paragraphs can't have
1023 // attributes, like id in this case.
1024 if (par->isInset(0)) {
1025 InsetOld * inset = par->getInset(0);
1026 InsetOld::Code lyx_code = inset->lyxCode();
1027 if (lyx_code == InsetOld::LABEL_CODE) {
1028 command_name += " id=\"";
1029 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1030 command_name += '"';
1035 sgml::openTag(os, depth + command_depth, false, command_name);
1037 item_name = c_params.empty() ? "title" : c_params;
1038 sgml::openTag(os, depth + 1 + command_depth, false, item_name);
1041 case LATEX_ENVIRONMENT:
1042 case LATEX_ITEM_ENVIRONMENT:
1043 if (depth < par->params().depth()) {
1044 depth = par->params().depth();
1045 environment_stack[depth].erase();
1048 if (environment_stack[depth] != style->latexname()) {
1049 if (environment_stack.size() == depth + 1) {
1050 environment_stack.push_back("!-- --");
1051 environment_inner.push_back("!-- --");
1053 environment_stack[depth] = style->latexname();
1054 environment_inner[depth] = "!-- --";
1055 sgml::openTag(os, depth + command_depth, false, environment_stack[depth]);
1057 sgml::closeEnvTags(os, false, environment_inner[depth],
1058 command_depth + depth);
1061 if (style->latextype == LATEX_ENVIRONMENT) {
1062 if (!style->latexparam().empty()) {
1063 if (style->latexparam() == "CDATA")
1066 sgml::openTag(os, depth + command_depth, false, style->latexparam());
1071 desc_on = (style->labeltype == LABEL_MANUAL);
1073 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1074 sgml::openTag(os, depth + 1 + command_depth,
1075 false, environment_inner[depth]);
1077 item_name = desc_on ? "term" : "para";
1078 sgml::openTag(os, depth + 1 + command_depth,
1082 sgml::openTag(os, depth + command_depth,
1083 false, style->latexname());
1087 par->simpleDocBookOnePar(buf, os, outerFont(par, paragraphs), desc_on,
1088 depth + 1 + command_depth);
1091 // write closing SGML tags
1092 switch (style->latextype) {
1094 end_tag = c_params.empty() ? "title" : c_params;
1095 sgml::closeTag(os, depth + command_depth,
1098 case LATEX_ENVIRONMENT:
1099 if (!style->latexparam().empty()) {
1100 if (style->latexparam() == "CDATA")
1103 sgml::closeTag(os, depth + command_depth, false, style->latexparam());
1106 case LATEX_ITEM_ENVIRONMENT:
1107 if (desc_on == 1) break;
1109 sgml::closeTag(os, depth + 1 + command_depth, false, end_tag);
1111 case LATEX_PARAGRAPH:
1112 sgml::closeTag(os, depth + command_depth, false, style->latexname());
1115 sgml::closeTag(os, depth + command_depth, false, style->latexname());
1121 for (int d = depth; d >= 0; --d) {
1122 if (!environment_stack[depth].empty()) {
1123 sgml::closeEnvTags(os, false, environment_inner[depth],
1124 command_depth + depth);
1128 for (int j = command_depth; j >= 0 ; --j)
1129 if (!command_stack[j].empty()) {
1130 sgml::closeTag(os, j, false, command_stack[j]);
1137 int readParToken(Buffer & buf, Paragraph & par, LyXLex & lex, string const & token)
1139 static LyXFont font;
1140 static Change change;
1142 BufferParams const & bp = buf.params();
1144 if (token[0] != '\\') {
1145 string::const_iterator cit = token.begin();
1146 for (; cit != token.end(); ++cit) {
1147 par.insertChar(par.size(), (*cit), font, change);
1149 } else if (token == "\\begin_layout") {
1151 string layoutname = lex.getString();
1153 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
1156 LyXTextClass const & tclass = bp.getLyXTextClass();
1158 if (layoutname.empty()) {
1159 layoutname = tclass.defaultLayoutName();
1162 bool hasLayout = tclass.hasLayout(layoutname);
1165 lyxerr << "Layout '" << layoutname << "' does not"
1166 << " exist in textclass '" << tclass.name()
1168 lyxerr << "Trying to use default layout instead."
1170 layoutname = tclass.defaultLayoutName();
1173 par.layout(bp.getLyXTextClass()[layoutname]);
1175 // Test whether the layout is obsolete.
1176 LyXLayout_ptr const & layout = par.layout();
1177 if (!layout->obsoleted_by().empty())
1178 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
1180 par.params().read(lex);
1182 } else if (token == "\\end_layout") {
1183 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
1184 << "Missing \\begin_layout?.\n";
1185 } else if (token == "\\end_inset") {
1186 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
1187 << "Missing \\begin_inset?.\n";
1188 } else if (token == "\\begin_inset") {
1189 InsetOld * inset = readInset(lex, buf);
1191 par.insertInset(par.size(), inset, font, change);
1194 string line = lex.getString();
1195 buf.error(ErrorItem(_("Unknown Inset"), line,
1196 buf.paragraphs().back().id(),
1200 } else if (token == "\\family") {
1202 font.setLyXFamily(lex.getString());
1203 } else if (token == "\\series") {
1205 font.setLyXSeries(lex.getString());
1206 } else if (token == "\\shape") {
1208 font.setLyXShape(lex.getString());
1209 } else if (token == "\\size") {
1211 font.setLyXSize(lex.getString());
1212 } else if (token == "\\lang") {
1214 string const tok = lex.getString();
1215 Language const * lang = languages.getLanguage(tok);
1217 font.setLanguage(lang);
1219 font.setLanguage(bp.language);
1220 lex.printError("Unknown language `$$Token'");
1222 } else if (token == "\\numeric") {
1224 font.setNumber(font.setLyXMisc(lex.getString()));
1225 } else if (token == "\\emph") {
1227 font.setEmph(font.setLyXMisc(lex.getString()));
1228 } else if (token == "\\bar") {
1230 string const tok = lex.getString();
1233 font.setUnderbar(LyXFont::ON);
1234 else if (tok == "no")
1235 font.setUnderbar(LyXFont::OFF);
1236 else if (tok == "default")
1237 font.setUnderbar(LyXFont::INHERIT);
1239 lex.printError("Unknown bar font flag "
1241 } else if (token == "\\noun") {
1243 font.setNoun(font.setLyXMisc(lex.getString()));
1244 } else if (token == "\\color") {
1246 font.setLyXColor(lex.getString());
1247 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
1249 // Insets don't make sense in a free-spacing context! ---Kayvan
1250 if (par.isFreeSpacing()) {
1251 if (token == "\\InsetSpace")
1252 par.insertChar(par.size(), ' ', font, change);
1253 else if (lex.isOK()) {
1255 string const next_token = lex.getString();
1256 if (next_token == "\\-")
1257 par.insertChar(par.size(), '-', font, change);
1259 lex.printError("Token `$$Token' "
1261 "paragraph layout!");
1265 InsetOld * inset = 0;
1266 if (token == "\\SpecialChar" )
1267 inset = new InsetSpecialChar;
1269 inset = new InsetSpace;
1270 inset->read(buf, lex);
1271 par.insertInset(par.size(), inset, font, change);
1273 } else if (token == "\\i") {
1274 InsetOld * inset = new InsetLatexAccent;
1275 inset->read(buf, lex);
1276 par.insertInset(par.size(), inset, font, change);
1277 } else if (token == "\\backslash") {
1278 par.insertChar(par.size(), '\\', font, change);
1279 } else if (token == "\\newline") {
1280 InsetOld * inset = new InsetNewline;
1281 inset->read(buf, lex);
1282 par.insertInset(par.size(), inset, font, change);
1283 } else if (token == "\\LyXTable") {
1284 InsetOld * inset = new InsetTabular(buf);
1285 inset->read(buf, lex);
1286 par.insertInset(par.size(), inset, font, change);
1287 } else if (token == "\\bibitem") {
1288 InsetCommandParams p("bibitem", "dummy");
1289 InsetBibitem * inset = new InsetBibitem(p);
1290 inset->read(buf, lex);
1291 par.insertInset(par.size(), inset, font, change);
1292 } else if (token == "\\hfill") {
1293 par.insertInset(par.size(), new InsetHFill, font, change);
1294 } else if (token == "\\lyxline") {
1295 par.insertInset(par.size(), new InsetLine, font, change);
1296 } else if (token == "\\newpage") {
1297 par.insertInset(par.size(), new InsetPagebreak, font, change);
1298 } else if (token == "\\change_unchanged") {
1299 // Hack ! Needed for empty paragraphs :/
1300 // FIXME: is it still ??
1303 change = Change(Change::UNCHANGED);
1304 } else if (token == "\\change_inserted") {
1306 istringstream is(lex.getString());
1310 change = Change(Change::INSERTED, bp.author_map[aid], ct);
1311 } else if (token == "\\change_deleted") {
1313 istringstream is(lex.getString());
1317 change = Change(Change::DELETED, bp.author_map[aid], ct);
1320 string const s = bformat(_("Unknown token: %1$s %2$s\n"),
1321 token, lex.getString());
1323 buf.error(ErrorItem(_("Unknown token"), s,
1324 buf.paragraphs().back().id(),
1334 int readParagraph(Buffer & buf, Paragraph & par, LyXLex & lex)
1339 string token = lex.getString();
1341 while (lex.isOK()) {
1343 unknown += readParToken(buf, par, lex, token);
1346 token = lex.getString();
1351 if (token == "\\end_layout") {
1352 //Ok, paragraph finished
1356 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
1357 << token << '\'' << endl;
1358 if (token == "\\begin_layout" || token == "\\end_document"
1359 || token == "\\end_inset" || token == "\\begin_deeper"
1360 || token == "\\end_deeper") {
1361 lex.pushToken(token);
1362 lyxerr << "Paragraph ended in line "
1363 << lex.getLineNo() << "\n"
1364 << "Missing \\end_layout.\n";
1373 LyXFont const outerFont(ParagraphList::iterator pit,
1374 ParagraphList const & plist)
1376 Paragraph::depth_type par_depth = pit->getDepth();
1377 LyXFont tmpfont(LyXFont::ALL_INHERIT);
1379 // Resolve against environment font information
1380 while (pit != const_cast<ParagraphList&>(plist).end() &&
1381 par_depth && !tmpfont.resolved()) {
1382 pit = outerHook(pit, plist);
1383 if (pit != const_cast<ParagraphList&>(plist).end()) {
1384 tmpfont.realize(pit->layout()->font);
1385 par_depth = pit->getDepth();
1393 ParagraphList::iterator outerPar(Buffer const & buf, InsetOld const * inset)
1395 ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
1396 ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
1397 for ( ; pit != end; ++pit) {
1399 ParagraphList * plist;
1400 // the second '=' below is intentional
1401 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
1402 if (plist == &pit.plist())
1403 return pit.outerPar();
1405 InsetList::iterator ii = pit->insetlist.begin();
1406 InsetList::iterator iend = pit->insetlist.end();
1407 for ( ; ii != iend; ++ii)
1408 if (ii->inset == inset)
1409 return pit.outerPar();
1411 lyxerr << "outerPar: should not happen" << endl;
1412 BOOST_ASSERT(false);
1413 return const_cast<Buffer &>(buf).paragraphs().end(); // shut up compiler
1417 Paragraph const & ownerPar(Buffer const & buf, InsetOld const * inset)
1419 ParConstIterator pit = buf.par_iterator_begin();
1420 ParConstIterator end = buf.par_iterator_end();
1421 for ( ; pit != end; ++pit) {
1422 ParagraphList * plist;
1423 // the second '=' below is intentional
1424 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
1425 if (plist == &pit.plist())
1428 InsetList::const_iterator ii = pit->insetlist.begin();
1429 InsetList::const_iterator iend = pit->insetlist.end();
1430 for ( ; ii != iend; ++ii)
1431 if (ii->inset == inset)
1434 lyxerr << "ownerPar: should not happen" << endl;
1435 BOOST_ASSERT(false);
1436 return buf.paragraphs().front(); // shut up compiler