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"
25 #include "latexrunparams.h"
28 #include "paragraph_pimpl.h"
33 #include "insets/insetbibitem.h"
34 #include "insets/insethfill.h"
35 #include "insets/insetlatexaccent.h"
36 #include "insets/insetline.h"
37 #include "insets/insetnewline.h"
38 #include "insets/insetpagebreak.h"
39 #include "insets/insetoptarg.h"
40 #include "insets/insetspace.h"
41 #include "insets/insetspecialchar.h"
42 #include "insets/insettabular.h"
44 #include "support/filetools.h"
45 #include "support/lstrings.h"
46 #include "support/lyxlib.h"
47 #include "support/std_sstream.h"
53 using lyx::support::ascii_lowercase;
54 using lyx::support::atoi;
55 using lyx::support::bformat;
56 using lyx::support::compare_ascii_no_case;
57 using lyx::support::compare_no_case;
58 using lyx::support::contains;
59 using lyx::support::split;
60 using lyx::support::subst;
65 using std::istringstream;
69 extern string bibitemWidest(Buffer const &);
74 bool moveItem(Paragraph & from, Paragraph & to,
75 BufferParams const & params, pos_type i, pos_type j)
77 char const tmpchar = from.getChar(i);
78 LyXFont tmpfont = from.getFontSettings(params, i);
80 if (tmpchar == Paragraph::META_INSET) {
81 InsetOld * tmpinset = 0;
82 if (from.getInset(i)) {
83 // the inset is not in a paragraph anymore
84 tmpinset = from.insetlist.release(i);
87 if (!to.insetAllowed(tmpinset->lyxCode()))
89 to.insertInset(j, tmpinset, tmpfont);
91 if (!to.checkInsertChar(tmpfont))
93 to.insertChar(j, tmpchar, tmpfont);
101 void breakParagraph(BufferParams const & bparams,
102 ParagraphList & paragraphs,
103 ParagraphList::iterator par,
107 // create a new paragraph, and insert into the list
108 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
111 // without doing that we get a crash when typing <Return> at the
112 // end of a paragraph
113 tmp->layout(bparams.getLyXTextClass().defaultLayout());
114 // remember to set the inset_owner
115 tmp->setInsetOwner(par->inInset());
117 if (bparams.tracking_changes)
120 // this is an idea for a more userfriendly layout handling, I will
121 // see what the users say
123 // layout stays the same with latex-environments
125 tmp->layout(par->layout());
126 tmp->setLabelWidthString(par->params().labelWidthString());
129 bool const isempty = (par->allowEmpty() && par->empty());
131 if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
132 tmp->layout(par->layout());
133 tmp->params().align(par->params().align());
134 tmp->setLabelWidthString(par->params().labelWidthString());
136 tmp->params().spaceBottom(par->params().spaceBottom());
137 par->params().spaceBottom(VSpace(VSpace::NONE));
139 tmp->params().depth(par->params().depth());
140 tmp->params().noindent(par->params().noindent());
142 // copy everything behind the break-position
143 // to the new paragraph
146 #warning this seems wrong
148 /* FIXME: if !keepempty, empty() == true, then we reach
149 * here with size() == 0. So pos_end becomes - 1. Why
150 * doesn't this cause problems ???
152 pos_type pos_end = par->size() - 1;
156 for (; i <= pos_end; ++i) {
157 Change::Type change = par->lookupChange(i);
158 if (moveItem(*par, *tmp, bparams, i, j - pos)) {
159 tmp->setChange(j - pos, change);
164 for (i = pos_end; i >= pos; --i)
171 tmp->params().spaceTop(par->params().spaceTop());
172 par->params().clear();
174 par->layout(bparams.getLyXTextClass().defaultLayout());
176 // layout stays the same with latex-environments
178 par->layout(tmp->layout());
179 par->setLabelWidthString(tmp->params().labelWidthString());
180 par->params().depth(tmp->params().depth());
183 // subtle, but needed to get empty pars working right
184 if (bparams.tracking_changes) {
187 } else if (!tmp->size()) {
194 void breakParagraphConservative(BufferParams const & bparams,
195 ParagraphList & paragraphs,
196 ParagraphList::iterator par,
199 // create a new paragraph
200 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
202 tmp->makeSameLayout(*par);
204 // When can pos > size()?
205 // I guess pos == size() is possible.
206 if (par->size() > pos) {
207 // copy everything behind the break-position to the new
209 pos_type pos_end = par->size() - 1;
211 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
212 if (moveItem(*par, *tmp, bparams, i, j - pos))
215 for (pos_type k = pos_end; k >= pos; --k)
221 void mergeParagraph(BufferParams const & bparams,
222 ParagraphList & paragraphs,
223 ParagraphList::iterator par)
225 ParagraphList::iterator the_next = boost::next(par);
227 // first the DTP-stuff
228 par->params().spaceBottom(the_next->params().spaceBottom());
230 pos_type pos_end = the_next->size() - 1;
231 pos_type pos_insert = par->size();
233 // ok, now copy the paragraph
234 for (pos_type i = 0, j = 0; i <= pos_end; ++i)
235 if (moveItem(*the_next, *par, bparams, i, pos_insert + j))
238 paragraphs.erase(the_next);
242 ParagraphList::iterator depthHook(ParagraphList::iterator pit,
243 ParagraphList const & plist,
244 Paragraph::depth_type depth)
246 ParagraphList::iterator newpit = pit;
247 ParagraphList::iterator beg = const_cast<ParagraphList&>(plist).begin();
252 while (newpit != beg && newpit->getDepth() > depth) {
256 if (newpit->getDepth() > depth)
263 ParagraphList::iterator outerHook(ParagraphList::iterator pit,
264 ParagraphList const & plist)
266 if (!pit->getDepth())
267 return const_cast<ParagraphList&>(plist).end();
268 return depthHook(pit, plist,
269 Paragraph::depth_type(pit->getDepth() - 1));
273 bool isFirstInSequence(ParagraphList::iterator pit,
274 ParagraphList const & plist)
276 ParagraphList::iterator dhook = depthHook(pit, plist, pit->getDepth());
278 || dhook->layout() != pit->layout()
279 || dhook->getDepth() != pit->getDepth());
283 int getEndLabel(ParagraphList::iterator p, ParagraphList const & plist)
285 ParagraphList::iterator pit = p;
286 Paragraph::depth_type par_depth = p->getDepth();
287 while (pit != const_cast<ParagraphList&>(plist).end()) {
288 LyXLayout_ptr const & layout = pit->layout();
289 int const endlabeltype = layout->endlabeltype;
291 if (endlabeltype != END_LABEL_NO_LABEL) {
292 if (boost::next(p) == const_cast<ParagraphList&>(plist).end())
295 Paragraph::depth_type const next_depth = boost::next(p)->getDepth();
296 if (par_depth > next_depth ||
297 (par_depth == next_depth &&
298 layout != boost::next(p)->layout()))
304 pit = outerHook(pit, plist);
305 if (pit != const_cast<ParagraphList&>(plist).end())
306 par_depth = pit->getDepth();
308 return END_LABEL_NO_LABEL;
314 ParagraphList::iterator
315 TeXEnvironment(Buffer const & buf,
316 ParagraphList const & paragraphs,
317 ParagraphList::iterator pit,
318 ostream & os, TexRow & texrow,
319 LatexRunParams const & runparams);
321 ParagraphList::iterator
322 TeXOnePar(Buffer const & buf,
323 ParagraphList const & paragraphs,
324 ParagraphList::iterator pit,
325 ostream & os, TexRow & texrow,
326 LatexRunParams const & runparams,
327 string const & everypar = string());
330 ParagraphList::iterator
331 TeXDeeper(Buffer const & buf,
332 ParagraphList const & paragraphs,
333 ParagraphList::iterator pit,
334 ostream & os, TexRow & texrow,
335 LatexRunParams const & runparams)
337 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
338 ParagraphList::iterator par = pit;
340 while (par != const_cast<ParagraphList&>(paragraphs).end() &&
341 par->params().depth() == pit->params().depth()) {
342 if (par->layout()->isEnvironment()) {
343 par = TeXEnvironment(buf, paragraphs, par,
344 os, texrow, runparams);
346 par = TeXOnePar(buf, paragraphs, par,
347 os, texrow, runparams);
350 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
356 ParagraphList::iterator
357 TeXEnvironment(Buffer const & buf,
358 ParagraphList const & paragraphs,
359 ParagraphList::iterator pit,
360 ostream & os, TexRow & texrow,
361 LatexRunParams const & runparams)
363 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
365 BufferParams const & bparams = buf.params();
367 LyXLayout_ptr const & style = pit->layout();
369 Language const * language = pit->getParLanguage(bparams);
370 Language const * doc_language = bparams.language;
371 Language const * previous_language =
372 (pit != const_cast<ParagraphList&>(paragraphs).begin())
373 ? boost::prior(pit)->getParLanguage(bparams)
375 if (language->babel() != previous_language->babel()) {
377 if (!lyxrc.language_command_end.empty() &&
378 previous_language->babel() != doc_language->babel()) {
379 os << subst(lyxrc.language_command_end, "$$lang",
380 previous_language->babel())
385 if (lyxrc.language_command_end.empty() ||
386 language->babel() != doc_language->babel()) {
387 os << subst(lyxrc.language_command_begin, "$$lang",
394 bool leftindent_open = false;
395 if (!pit->params().leftIndent().zero()) {
396 os << "\\begin{LyXParagraphLeftIndent}{" <<
397 pit->params().leftIndent().asLatexString() << "}\n";
399 leftindent_open = true;
402 if (style->isEnvironment()) {
403 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
404 os << "\\begin{" << style->latexname() << "}{"
405 << pit->params().labelWidthString() << "}\n";
406 } else if (style->labeltype == LABEL_BIBLIO) {
408 os << "\\begin{" << style->latexname() << "}{"
409 << bibitemWidest(buf)
411 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
412 os << "\\begin{" << style->latexname() << '}'
413 << style->latexparam() << '\n';
415 os << "\\begin{" << style->latexname() << '}'
416 << style->latexparam() << '\n';
419 ParagraphList::iterator par = pit;
421 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
423 if (par != const_cast<ParagraphList&>(paragraphs).end() && par->params().depth() > pit->params().depth()) {
424 if (par->layout()->isParagraph()) {
427 // How to handle this? (Lgb)
428 //&& !suffixIs(os, "\n\n")
430 // There should be at least one '\n' already
431 // but we need there to be two for Standard
432 // paragraphs that are depth-increment'ed to be
433 // output correctly. However, tables can
434 // also be paragraphs so don't adjust them.
437 // Will it ever harm to have one '\n' too
438 // many? i.e. that we sometimes will have
439 // three in a row. (Lgb)
443 par = TeXDeeper(buf, paragraphs, par, os, texrow,
446 } while (par != const_cast<ParagraphList&>(paragraphs).end()
447 && par->layout() == pit->layout()
448 && par->params().depth() == pit->params().depth()
449 && par->params().leftIndent() == pit->params().leftIndent());
451 if (style->isEnvironment()) {
452 os << "\\end{" << style->latexname() << "}\n";
456 if (leftindent_open) {
457 os << "\\end{LyXParagraphLeftIndent}\n";
461 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
462 return par; // ale970302
466 InsetOptArg * optArgInset(Paragraph const & par)
469 InsetList::const_iterator it = par.insetlist.begin();
470 InsetList::const_iterator end = par.insetlist.end();
471 for (; it != end; ++it) {
472 InsetOld * ins = it->inset;
473 if (ins->lyxCode() == InsetOld::OPTARG_CODE) {
474 return static_cast<InsetOptArg *>(ins);
481 ParagraphList::iterator
482 TeXOnePar(Buffer const & buf,
483 ParagraphList const & paragraphs,
484 ParagraphList::iterator pit,
485 ostream & os, TexRow & texrow,
486 LatexRunParams const & runparams,
487 string const & everypar)
489 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
490 << everypar << "'" << endl;
491 BufferParams const & bparams = buf.params();
493 InsetOld const * in = pit->inInset();
494 bool further_blank_line = false;
497 // well we have to check if we are in an inset with unlimited
498 // length (all in one row) if that is true then we don't allow
499 // any special options in the paragraph and also we don't allow
500 // any environment other then "Standard" to be valid!
501 if (in == 0 || !in->forceDefaultParagraphs(in)) {
502 style = pit->layout();
504 if (pit->params().startOfAppendix()) {
505 os << "\\appendix\n";
509 if (!pit->params().spacing().isDefault()
510 && (pit == const_cast<ParagraphList&>(paragraphs).begin()
511 || !boost::prior(pit)->hasSameLayout(*pit)))
513 os << pit->params().spacing().writeEnvirBegin() << '\n';
517 if (style->isCommand()) {
522 if (pit->params().spaceTop().kind() != VSpace::NONE) {
523 os << pit->params().spaceTop().asLatexCommand(bparams);
524 further_blank_line = true;
527 if (further_blank_line) {
532 style = bparams.getLyXTextClass().defaultLayout();
535 Language const * language = pit->getParLanguage(bparams);
536 Language const * doc_language = bparams.language;
537 Language const * previous_language =
538 (pit != const_cast<ParagraphList&>(paragraphs).begin())
539 ? boost::prior(pit)->getParLanguage(bparams)
542 if (language->babel() != previous_language->babel()
543 // check if we already put language command in TeXEnvironment()
544 && !(style->isEnvironment()
545 && (pit == const_cast<ParagraphList&>(paragraphs).begin() ||
546 (boost::prior(pit)->layout() != pit->layout() &&
547 boost::prior(pit)->getDepth() <= pit->getDepth())
548 || boost::prior(pit)->getDepth() < pit->getDepth())))
550 if (!lyxrc.language_command_end.empty() &&
551 previous_language->babel() != doc_language->babel())
553 os << subst(lyxrc.language_command_end, "$$lang",
554 previous_language->babel())
559 if (lyxrc.language_command_end.empty() ||
560 language->babel() != doc_language->babel())
562 os << subst(lyxrc.language_command_begin, "$$lang",
569 if (bparams.inputenc == "auto" &&
570 language->encoding() != previous_language->encoding()) {
571 os << "\\inputencoding{"
572 << language->encoding()->LatexName()
577 switch (style->latextype) {
579 os << '\\' << style->latexname();
581 // Separate handling of optional argument inset.
582 if (style->optionalargs == 1) {
583 InsetOptArg * it = optArgInset(*pit);
585 it->latexOptional(buf, os, runparams);
588 os << style->latexparam();
590 case LATEX_ITEM_ENVIRONMENT:
591 case LATEX_LIST_ENVIRONMENT:
594 case LATEX_BIB_ENVIRONMENT:
595 // ignore this, the inset will write itself
602 bool need_par = pit->simpleTeXOnePar(buf, bparams,
603 outerFont(pit, paragraphs),
604 os, texrow, runparams);
606 // Make sure that \\par is done with the font of the last
607 // character if this has another size as the default.
608 // This is necessary because LaTeX (and LyX on the screen)
609 // calculates the space between the baselines according
610 // to this font. (Matthias)
612 // Is this really needed ? (Dekel)
613 // We do not need to use to change the font for the last paragraph
615 LyXFont const outerfont(outerFont(pit, paragraphs));
619 ? pit->getLayoutFont(bparams, outerfont)
620 : pit->getFont(bparams, pit->size() - 1, outerfont));
622 bool is_command = style->isCommand();
624 if (style->resfont.size() != font.size()
625 && boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
629 os << "\\" << font.latexSize() << " \\par}";
630 } else if (need_par) {
632 } else if (is_command)
635 switch (style->latextype) {
636 case LATEX_ITEM_ENVIRONMENT:
637 case LATEX_LIST_ENVIRONMENT:
638 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
639 && (pit->params().depth() < boost::next(pit)->params().depth())) {
644 case LATEX_ENVIRONMENT: {
645 // if its the last paragraph of the current environment
646 // skip it otherwise fall through
647 ParagraphList::iterator next = boost::next(pit);
649 if (next != const_cast<ParagraphList&>(paragraphs).end()
650 && (next->layout() != pit->layout()
651 || next->params().depth() != pit->params().depth()))
655 // fall through possible
657 // we don't need it for the last paragraph!!!
658 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
664 if (in == 0 || !in->forceDefaultParagraphs(in)) {
665 further_blank_line = false;
667 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
668 os << pit->params().spaceBottom().asLatexCommand(bparams);
669 further_blank_line = true;
672 if (further_blank_line) {
677 if (!pit->params().spacing().isDefault()
678 && (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
679 || !boost::next(pit)->hasSameLayout(*pit)))
681 os << pit->params().spacing().writeEnvirEnd() << '\n';
686 // we don't need it for the last paragraph!!!
687 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
691 // Since \selectlanguage write the language to the aux file,
692 // we need to reset the language at the end of footnote or
695 if (language->babel() != doc_language->babel()) {
696 if (lyxrc.language_command_end.empty())
697 os << subst(lyxrc.language_command_begin,
699 doc_language->babel())
702 os << subst(lyxrc.language_command_end,
710 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
718 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
720 void latexParagraphs(Buffer const & buf,
721 ParagraphList const & paragraphs,
724 LatexRunParams const & runparams,
725 string const & everypar)
727 bool was_title = false;
728 bool already_title = false;
729 LyXTextClass const & tclass = buf.params().getLyXTextClass();
730 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
731 ParagraphList::iterator endpar = const_cast<ParagraphList&>(paragraphs).end();
734 while (par != endpar) {
735 InsetOld * in = par->inInset();
736 // well we have to check if we are in an inset with unlimited
737 // length (all in one row) if that is true then we don't allow
738 // any special options in the paragraph and also we don't allow
739 // any environment other then "Standard" to be valid!
740 if (in == 0 || !in->forceDefaultParagraphs(in)) {
741 LyXLayout_ptr const & layout = par->layout();
743 if (layout->intitle) {
745 lyxerr <<"Error in latexParagraphs: You"
746 " should not mix title layouts"
747 " with normal ones." << endl;
748 } else if (!was_title) {
750 if (tclass.titletype() == TITLE_ENVIRONMENT) {
752 << tclass.titlename()
757 } else if (was_title && !already_title) {
758 if (tclass.titletype() == TITLE_ENVIRONMENT) {
759 os << "\\end{" << tclass.titlename()
763 os << "\\" << tclass.titlename()
767 already_title = true;
771 if (layout->is_environment) {
772 par = TeXOnePar(buf, paragraphs, par, os, texrow,
773 runparams, everypar);
774 } else if (layout->isEnvironment() ||
775 !par->params().leftIndent().zero())
777 par = TeXEnvironment(buf, paragraphs, par, os,
780 par = TeXOnePar(buf, paragraphs, par, os, texrow,
781 runparams, everypar);
784 par = TeXOnePar(buf, paragraphs, par, os, texrow,
785 runparams, everypar);
788 // It might be that we only have a title in this document
789 if (was_title && !already_title) {
790 if (tclass.titletype() == TITLE_ENVIRONMENT) {
791 os << "\\end{" << tclass.titlename()
795 os << "\\" << tclass.titlename()
805 pair<int, string> const addDepth(int depth, int ldepth)
809 d += (ldepth - depth) * 2;
810 return make_pair(d, string(d, ' '));
816 void asciiParagraph(Buffer const & buf,
817 Paragraph const & par,
819 LatexRunParams const & runparams,
823 Paragraph::depth_type ltype_depth = 0;
824 bool ref_printed = false;
825 Paragraph::depth_type depth = par.params().depth();
827 // First write the layout
828 string const & tmp = par.layout()->name();
829 if (compare_no_case(tmp, "itemize") == 0) {
831 ltype_depth = depth + 1;
832 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
834 ltype_depth = depth + 1;
835 } else if (contains(ascii_lowercase(tmp), "ection")) {
837 ltype_depth = depth + 1;
838 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
840 ltype_depth = depth + 1;
841 } else if (compare_ascii_no_case(tmp, "description") == 0) {
843 ltype_depth = depth + 1;
844 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
847 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
855 /* maybe some vertical spaces */
857 /* the labelwidthstring used in lists */
861 /* some pagebreaks? */
865 /* what about the alignment */
867 // runparams.linelen <= 0 is special and means we don't have paragraph breaks
869 string::size_type currlinelen = 0;
872 if (runparams.linelen > 0)
875 os << string(depth * 2, ' ');
876 currlinelen += depth * 2;
879 // we should probably change to the paragraph language in the
880 // gettext here (if possible) so that strings are outputted in
881 // the correct language! (20012712 Jug)
885 case 4: // (Sub)Paragraph
886 case 5: // Description
889 if (runparams.linelen > 0) {
890 os << _("Abstract") << "\n\n";
893 string const abst = _("Abstract: ");
895 currlinelen += abst.length();
898 case 7: // Bibliography
900 if (runparams.linelen > 0) {
901 os << _("References") << "\n\n";
904 string const refs = _("References: ");
906 currlinelen += refs.length();
914 string const parlab = par.params().labelString();
916 currlinelen += parlab.length() + 1;
924 pair<int, string> p = addDepth(depth, ltype_depth);
926 currlinelen += p.first;
929 // this is to change the linebreak to do it by word a bit more
930 // intelligent hopefully! (only in the case where we have a
931 // max runparams.linelength!) (Jug)
935 for (pos_type i = 0; i < par.size(); ++i) {
936 char c = par.getUChar(buf.params(), i);
938 case Paragraph::META_INSET:
940 InsetOld const * inset = par.getInset(i);
942 if (runparams.linelen > 0) {
944 currlinelen += word.length();
947 if (inset->ascii(buf, os, runparams)) {
948 // to be sure it breaks paragraph
949 currlinelen += runparams.linelen;
957 if (runparams.linelen > 0 &&
958 currlinelen + word.length() > runparams.linelen - 10) {
960 pair<int, string> p = addDepth(depth, ltype_depth);
962 currlinelen = p.first;
966 currlinelen += word.length() + 1;
973 lyxerr[Debug::INFO] <<
974 "writeAsciiFile: NULL char in structure." << endl;
976 if ((runparams.linelen > 0) &&
977 (currlinelen + word.length()) > runparams.linelen)
981 pair<int, string> p =
982 addDepth(depth, ltype_depth);
984 currlinelen = p.first;
994 void linuxdocParagraphs(Buffer const & buf,
995 ParagraphList const & paragraphs,
997 LatexRunParams const & runparams)
1000 Paragraph::depth_type depth = 0; // paragraph depth
1002 vector<string> environment_stack(5);
1004 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1005 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1006 for (; pit != pend; ++pit) {
1007 LyXLayout_ptr const & style = pit->layout();
1008 // treat <toc> as a special case for compatibility with old code
1009 if (pit->isInset(0)) {
1010 InsetOld * inset = pit->getInset(0);
1011 InsetOld::Code lyx_code = inset->lyxCode();
1012 if (lyx_code == InsetOld::TOC_CODE) {
1013 string const temp = "toc";
1014 sgml::openTag(os, depth, false, temp);
1019 // environment tag closing
1020 for (; depth > pit->params().depth(); --depth) {
1021 sgml::closeTag(os, depth, false, environment_stack[depth]);
1022 environment_stack[depth].erase();
1025 // write opening SGML tags
1026 switch (style->latextype) {
1027 case LATEX_PARAGRAPH:
1028 if (depth == pit->params().depth()
1029 && !environment_stack[depth].empty()) {
1030 sgml::closeTag(os, depth, false, environment_stack[depth]);
1031 environment_stack[depth].erase();
1037 sgml::openTag(os, depth, false, style->latexname());
1042 //error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
1045 if (!environment_stack[depth].empty()) {
1046 sgml::closeTag(os, depth, false, environment_stack[depth]);
1050 environment_stack[depth].erase();
1051 sgml::openTag(os, depth, false, style->latexname());
1054 case LATEX_ENVIRONMENT:
1055 case LATEX_ITEM_ENVIRONMENT:
1056 case LATEX_BIB_ENVIRONMENT:
1058 string const & latexname = style->latexname();
1060 if (depth == pit->params().depth()
1061 && environment_stack[depth] != latexname) {
1062 sgml::closeTag(os, depth, false,
1063 environment_stack[depth]);
1064 environment_stack[depth].erase();
1066 if (depth < pit->params().depth()) {
1067 depth = pit->params().depth();
1068 environment_stack[depth].erase();
1070 if (environment_stack[depth] != latexname) {
1072 sgml::openTag(os, depth, false, "p");
1074 sgml::openTag(os, depth, false, latexname);
1076 if (environment_stack.size() == depth + 1)
1077 environment_stack.push_back("!-- --");
1078 environment_stack[depth] = latexname;
1081 if (style->latexparam() == "CDATA")
1084 if (style->latextype == LATEX_ENVIRONMENT) break;
1086 if (style->labeltype == LABEL_MANUAL)
1091 sgml::openTag(os, depth + 1, false, item_name);
1096 sgml::openTag(os, depth, false, style->latexname());
1100 pit->simpleLinuxDocOnePar(buf, os, outerFont(pit, paragraphs),
1104 // write closing SGML tags
1105 switch (style->latextype) {
1108 case LATEX_ENVIRONMENT:
1109 case LATEX_ITEM_ENVIRONMENT:
1110 case LATEX_BIB_ENVIRONMENT:
1111 if (style->latexparam() == "CDATA")
1115 sgml::closeTag(os, depth, false, style->latexname());
1121 for (int i = depth; i >= 0; --i)
1122 sgml::closeTag(os, depth, false, environment_stack[i]);
1126 void docbookParagraphs(Buffer const & buf,
1127 ParagraphList const & paragraphs,
1129 LatexRunParams const & runparams)
1131 vector<string> environment_stack(10);
1132 vector<string> environment_inner(10);
1133 vector<string> command_stack(10);
1135 bool command_flag = false;
1136 Paragraph::depth_type command_depth = 0;
1137 Paragraph::depth_type command_base = 0;
1138 Paragraph::depth_type cmd_depth = 0;
1139 Paragraph::depth_type depth = 0; // paragraph depth
1142 string command_name;
1144 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
1145 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1147 for (; par != pend; ++par) {
1151 int desc_on = 0; // description mode
1153 LyXLayout_ptr const & style = par->layout();
1155 // environment tag closing
1156 for (; depth > par->params().depth(); --depth) {
1157 if (!environment_inner[depth].empty())
1158 sgml::closeEnvTags(os, false, environment_inner[depth],
1159 command_depth + depth);
1160 sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
1161 environment_stack[depth].erase();
1162 environment_inner[depth].erase();
1165 if (depth == par->params().depth()
1166 && environment_stack[depth] != style->latexname()
1167 && !environment_stack[depth].empty()) {
1168 sgml::closeEnvTags(os, false, environment_inner[depth],
1169 command_depth + depth);
1170 sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
1172 environment_stack[depth].erase();
1173 environment_inner[depth].erase();
1176 // Write opening SGML tags.
1177 switch (style->latextype) {
1178 case LATEX_PARAGRAPH:
1179 sgml::openTag(os, depth + command_depth,
1180 false, style->latexname());
1185 //error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
1188 command_name = style->latexname();
1190 sgmlparam = style->latexparam();
1191 c_params = split(sgmlparam, c_depth,'|');
1193 cmd_depth = atoi(c_depth);
1196 if (cmd_depth < command_base) {
1197 for (Paragraph::depth_type j = command_depth;
1198 j >= command_base; --j) {
1199 sgml::closeTag(os, j, false, command_stack[j]);
1202 command_depth = command_base = cmd_depth;
1203 } else if (cmd_depth <= command_depth) {
1204 for (int j = command_depth;
1205 j >= int(cmd_depth); --j) {
1206 sgml::closeTag(os, j, false, command_stack[j]);
1209 command_depth = cmd_depth;
1211 command_depth = cmd_depth;
1213 command_depth = command_base = cmd_depth;
1214 command_flag = true;
1216 if (command_stack.size() == command_depth + 1)
1217 command_stack.push_back(string());
1218 command_stack[command_depth] = command_name;
1220 // treat label as a special case for
1221 // more WYSIWYM handling.
1222 // This is a hack while paragraphs can't have
1223 // attributes, like id in this case.
1224 if (par->isInset(0)) {
1225 InsetOld * inset = par->getInset(0);
1226 InsetOld::Code lyx_code = inset->lyxCode();
1227 if (lyx_code == InsetOld::LABEL_CODE) {
1228 command_name += " id=\"";
1229 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1230 command_name += '"';
1235 sgml::openTag(os, depth + command_depth, false, command_name);
1237 item_name = c_params.empty() ? "title" : c_params;
1238 sgml::openTag(os, depth + 1 + command_depth, false, item_name);
1241 case LATEX_ENVIRONMENT:
1242 case LATEX_ITEM_ENVIRONMENT:
1243 if (depth < par->params().depth()) {
1244 depth = par->params().depth();
1245 environment_stack[depth].erase();
1248 if (environment_stack[depth] != style->latexname()) {
1249 if (environment_stack.size() == depth + 1) {
1250 environment_stack.push_back("!-- --");
1251 environment_inner.push_back("!-- --");
1253 environment_stack[depth] = style->latexname();
1254 environment_inner[depth] = "!-- --";
1255 sgml::openTag(os, depth + command_depth, false, environment_stack[depth]);
1257 sgml::closeEnvTags(os, false, environment_inner[depth],
1258 command_depth + depth);
1261 if (style->latextype == LATEX_ENVIRONMENT) {
1262 if (!style->latexparam().empty()) {
1263 if (style->latexparam() == "CDATA")
1266 sgml::openTag(os, depth + command_depth, false, style->latexparam());
1271 desc_on = (style->labeltype == LABEL_MANUAL);
1273 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1274 sgml::openTag(os, depth + 1 + command_depth,
1275 false, environment_inner[depth]);
1277 item_name = desc_on ? "term" : "para";
1278 sgml::openTag(os, depth + 1 + command_depth,
1282 sgml::openTag(os, depth + command_depth,
1283 false, style->latexname());
1287 par->simpleDocBookOnePar(buf, os, outerFont(par, paragraphs), desc_on,
1288 runparams, depth + 1 + command_depth);
1291 // write closing SGML tags
1292 switch (style->latextype) {
1294 end_tag = c_params.empty() ? "title" : c_params;
1295 sgml::closeTag(os, depth + command_depth,
1298 case LATEX_ENVIRONMENT:
1299 if (!style->latexparam().empty()) {
1300 if (style->latexparam() == "CDATA")
1303 sgml::closeTag(os, depth + command_depth, false, style->latexparam());
1306 case LATEX_ITEM_ENVIRONMENT:
1307 if (desc_on == 1) break;
1309 sgml::closeTag(os, depth + 1 + command_depth, false, end_tag);
1311 case LATEX_PARAGRAPH:
1312 sgml::closeTag(os, depth + command_depth, false, style->latexname());
1315 sgml::closeTag(os, depth + command_depth, false, style->latexname());
1321 for (int d = depth; d >= 0; --d) {
1322 if (!environment_stack[depth].empty()) {
1323 sgml::closeEnvTags(os, false, environment_inner[depth],
1324 command_depth + depth);
1328 for (int j = command_depth; j >= 0 ; --j)
1329 if (!command_stack[j].empty()) {
1330 sgml::closeTag(os, j, false, command_stack[j]);
1337 int readParToken(Buffer & buf, Paragraph & par, LyXLex & lex, string const & token)
1339 static LyXFont font;
1340 static Change change;
1342 BufferParams const & bp = buf.params();
1344 if (token[0] != '\\') {
1345 string::const_iterator cit = token.begin();
1346 for (; cit != token.end(); ++cit) {
1347 par.insertChar(par.size(), (*cit), font, change);
1349 } else if (token == "\\begin_layout") {
1351 string layoutname = lex.getString();
1353 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
1356 LyXTextClass const & tclass = bp.getLyXTextClass();
1358 if (layoutname.empty()) {
1359 layoutname = tclass.defaultLayoutName();
1362 bool hasLayout = tclass.hasLayout(layoutname);
1365 lyxerr << "Layout '" << layoutname << "' does not"
1366 << " exist in textclass '" << tclass.name()
1368 lyxerr << "Trying to use default layout instead."
1370 layoutname = tclass.defaultLayoutName();
1373 par.layout(bp.getLyXTextClass()[layoutname]);
1375 // Test whether the layout is obsolete.
1376 LyXLayout_ptr const & layout = par.layout();
1377 if (!layout->obsoleted_by().empty())
1378 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
1380 par.params().read(lex);
1382 } else if (token == "\\end_layout") {
1383 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
1384 << "Missing \\begin_layout?.\n";
1385 } else if (token == "\\end_inset") {
1386 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
1387 << "Missing \\begin_inset?.\n";
1388 } else if (token == "\\begin_inset") {
1389 InsetOld * inset = readInset(lex, buf);
1391 par.insertInset(par.size(), inset, font, change);
1394 string line = lex.getString();
1395 buf.error(ErrorItem(_("Unknown Inset"), line,
1396 buf.paragraphs().back().id(),
1400 } else if (token == "\\family") {
1402 font.setLyXFamily(lex.getString());
1403 } else if (token == "\\series") {
1405 font.setLyXSeries(lex.getString());
1406 } else if (token == "\\shape") {
1408 font.setLyXShape(lex.getString());
1409 } else if (token == "\\size") {
1411 font.setLyXSize(lex.getString());
1412 } else if (token == "\\lang") {
1414 string const tok = lex.getString();
1415 Language const * lang = languages.getLanguage(tok);
1417 font.setLanguage(lang);
1419 font.setLanguage(bp.language);
1420 lex.printError("Unknown language `$$Token'");
1422 } else if (token == "\\numeric") {
1424 font.setNumber(font.setLyXMisc(lex.getString()));
1425 } else if (token == "\\emph") {
1427 font.setEmph(font.setLyXMisc(lex.getString()));
1428 } else if (token == "\\bar") {
1430 string const tok = lex.getString();
1433 font.setUnderbar(LyXFont::ON);
1434 else if (tok == "no")
1435 font.setUnderbar(LyXFont::OFF);
1436 else if (tok == "default")
1437 font.setUnderbar(LyXFont::INHERIT);
1439 lex.printError("Unknown bar font flag "
1441 } else if (token == "\\noun") {
1443 font.setNoun(font.setLyXMisc(lex.getString()));
1444 } else if (token == "\\color") {
1446 font.setLyXColor(lex.getString());
1447 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
1449 // Insets don't make sense in a free-spacing context! ---Kayvan
1450 if (par.isFreeSpacing()) {
1451 if (token == "\\InsetSpace")
1452 par.insertChar(par.size(), ' ', font, change);
1453 else if (lex.isOK()) {
1455 string const next_token = lex.getString();
1456 if (next_token == "\\-")
1457 par.insertChar(par.size(), '-', font, change);
1459 lex.printError("Token `$$Token' "
1461 "paragraph layout!");
1465 InsetOld * inset = 0;
1466 if (token == "\\SpecialChar" )
1467 inset = new InsetSpecialChar;
1469 inset = new InsetSpace;
1470 inset->read(buf, lex);
1471 par.insertInset(par.size(), inset, font, change);
1473 } else if (token == "\\i") {
1474 InsetOld * inset = new InsetLatexAccent;
1475 inset->read(buf, lex);
1476 par.insertInset(par.size(), inset, font, change);
1477 } else if (token == "\\backslash") {
1478 par.insertChar(par.size(), '\\', font, change);
1479 } else if (token == "\\newline") {
1480 InsetOld * inset = new InsetNewline;
1481 inset->read(buf, lex);
1482 par.insertInset(par.size(), inset, font, change);
1483 } else if (token == "\\LyXTable") {
1484 InsetOld * inset = new InsetTabular(buf);
1485 inset->read(buf, lex);
1486 par.insertInset(par.size(), inset, font, change);
1487 } else if (token == "\\bibitem") {
1488 InsetCommandParams p("bibitem", "dummy");
1489 InsetBibitem * inset = new InsetBibitem(p);
1490 inset->read(buf, lex);
1491 par.insertInset(par.size(), inset, font, change);
1492 } else if (token == "\\hfill") {
1493 par.insertInset(par.size(), new InsetHFill, font, change);
1494 } else if (token == "\\lyxline") {
1495 par.insertInset(par.size(), new InsetLine, font, change);
1496 } else if (token == "\\newpage") {
1497 par.insertInset(par.size(), new InsetPagebreak, font, change);
1498 } else if (token == "\\change_unchanged") {
1499 // Hack ! Needed for empty paragraphs :/
1500 // FIXME: is it still ??
1503 change = Change(Change::UNCHANGED);
1504 } else if (token == "\\change_inserted") {
1506 istringstream is(lex.getString());
1510 change = Change(Change::INSERTED, bp.author_map[aid], ct);
1511 } else if (token == "\\change_deleted") {
1513 istringstream is(lex.getString());
1517 change = Change(Change::DELETED, bp.author_map[aid], ct);
1520 string const s = bformat(_("Unknown token: %1$s %2$s\n"),
1521 token, lex.getString());
1523 buf.error(ErrorItem(_("Unknown token"), s,
1524 buf.paragraphs().back().id(),
1534 int readParagraph(Buffer & buf, Paragraph & par, LyXLex & lex)
1539 string token = lex.getString();
1541 while (lex.isOK()) {
1543 unknown += readParToken(buf, par, lex, token);
1546 token = lex.getString();
1551 if (token == "\\end_layout") {
1552 //Ok, paragraph finished
1556 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
1557 << token << '\'' << endl;
1558 if (token == "\\begin_layout" || token == "\\end_document"
1559 || token == "\\end_inset" || token == "\\begin_deeper"
1560 || token == "\\end_deeper") {
1561 lex.pushToken(token);
1562 lyxerr << "Paragraph ended in line "
1563 << lex.getLineNo() << "\n"
1564 << "Missing \\end_layout.\n";
1573 LyXFont const outerFont(ParagraphList::iterator pit,
1574 ParagraphList const & plist)
1576 Paragraph::depth_type par_depth = pit->getDepth();
1577 LyXFont tmpfont(LyXFont::ALL_INHERIT);
1579 // Resolve against environment font information
1580 while (pit != const_cast<ParagraphList&>(plist).end() &&
1581 par_depth && !tmpfont.resolved()) {
1582 pit = outerHook(pit, plist);
1583 if (pit != const_cast<ParagraphList&>(plist).end()) {
1584 tmpfont.realize(pit->layout()->font);
1585 par_depth = pit->getDepth();
1593 ParagraphList::iterator outerPar(Buffer const & buf, InsetOld const * inset)
1595 ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
1596 ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
1597 for ( ; pit != end; ++pit) {
1599 ParagraphList * plist;
1600 // the second '=' below is intentional
1601 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
1602 if (plist == &pit.plist())
1603 return pit.outerPar();
1605 InsetList::iterator ii = pit->insetlist.begin();
1606 InsetList::iterator iend = pit->insetlist.end();
1607 for ( ; ii != iend; ++ii)
1608 if (ii->inset == inset)
1609 return pit.outerPar();
1611 lyxerr << "outerPar: should not happen" << endl;
1612 BOOST_ASSERT(false);
1613 return const_cast<Buffer &>(buf).paragraphs().end(); // shut up compiler
1617 Paragraph const & ownerPar(Buffer const & buf, InsetOld const * inset)
1619 ParConstIterator pit = buf.par_iterator_begin();
1620 ParConstIterator end = buf.par_iterator_end();
1621 for ( ; pit != end; ++pit) {
1622 ParagraphList * plist;
1623 // the second '=' below is intentional
1624 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
1625 if (plist == &pit.plist())
1628 InsetList::const_iterator ii = pit->insetlist.begin();
1629 InsetList::const_iterator iend = pit->insetlist.end();
1630 for ( ; ii != iend; ++ii)
1631 if (ii->inset == inset)
1634 lyxerr << "ownerPar: should not happen" << endl;
1635 BOOST_ASSERT(false);
1636 return buf.paragraphs().front(); // shut up compiler