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;
66 using std::istringstream;
70 extern string bibitemWidest(Buffer const &);
75 bool moveItem(Paragraph & from, Paragraph & to,
76 BufferParams const & params, pos_type i, pos_type j)
78 char const tmpchar = from.getChar(i);
79 LyXFont tmpfont = from.getFontSettings(params, i);
81 if (tmpchar == Paragraph::META_INSET) {
82 InsetOld * tmpinset = 0;
83 if (from.getInset(i)) {
84 // the inset is not in a paragraph anymore
85 tmpinset = from.insetlist.release(i);
88 if (!to.insetAllowed(tmpinset->lyxCode()))
90 to.insertInset(j, tmpinset, tmpfont);
92 if (!to.checkInsertChar(tmpfont))
94 to.insertChar(j, tmpchar, tmpfont);
102 void breakParagraph(BufferParams const & bparams,
103 ParagraphList & paragraphs,
104 ParagraphList::iterator par,
108 // create a new paragraph, and insert into the list
109 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
112 // without doing that we get a crash when typing <Return> at the
113 // end of a paragraph
114 tmp->layout(bparams.getLyXTextClass().defaultLayout());
115 // remember to set the inset_owner
116 tmp->setInsetOwner(par->inInset());
118 if (bparams.tracking_changes)
121 // this is an idea for a more userfriendly layout handling, I will
122 // see what the users say
124 // layout stays the same with latex-environments
126 tmp->layout(par->layout());
127 tmp->setLabelWidthString(par->params().labelWidthString());
130 bool const isempty = (par->allowEmpty() && par->empty());
132 if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
133 tmp->layout(par->layout());
134 tmp->params().align(par->params().align());
135 tmp->setLabelWidthString(par->params().labelWidthString());
137 tmp->params().spaceBottom(par->params().spaceBottom());
138 par->params().spaceBottom(VSpace(VSpace::NONE));
140 tmp->params().depth(par->params().depth());
141 tmp->params().noindent(par->params().noindent());
143 // copy everything behind the break-position
144 // to the new paragraph
147 #warning this seems wrong
149 /* FIXME: if !keepempty, empty() == true, then we reach
150 * here with size() == 0. So pos_end becomes - 1. Why
151 * doesn't this cause problems ???
153 pos_type pos_end = par->size() - 1;
157 for (; i <= pos_end; ++i) {
158 Change::Type change = par->lookupChange(i);
159 if (moveItem(*par, *tmp, bparams, i, j - pos)) {
160 tmp->setChange(j - pos, change);
165 for (i = pos_end; i >= pos; --i)
172 tmp->params().spaceTop(par->params().spaceTop());
173 par->params().clear();
175 par->layout(bparams.getLyXTextClass().defaultLayout());
177 // layout stays the same with latex-environments
179 par->layout(tmp->layout());
180 par->setLabelWidthString(tmp->params().labelWidthString());
181 par->params().depth(tmp->params().depth());
184 // subtle, but needed to get empty pars working right
185 if (bparams.tracking_changes) {
188 } else if (!tmp->size()) {
195 void breakParagraphConservative(BufferParams const & bparams,
196 ParagraphList & paragraphs,
197 ParagraphList::iterator par,
200 // create a new paragraph
201 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
203 tmp->makeSameLayout(*par);
205 // When can pos > size()?
206 // I guess pos == size() is possible.
207 if (par->size() > pos) {
208 // copy everything behind the break-position to the new
210 pos_type pos_end = par->size() - 1;
212 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
213 if (moveItem(*par, *tmp, bparams, i, j - pos))
216 for (pos_type k = pos_end; k >= pos; --k)
222 void mergeParagraph(BufferParams const & bparams,
223 ParagraphList & paragraphs,
224 ParagraphList::iterator par)
226 ParagraphList::iterator the_next = boost::next(par);
228 // first the DTP-stuff
229 par->params().spaceBottom(the_next->params().spaceBottom());
231 pos_type pos_end = the_next->size() - 1;
232 pos_type pos_insert = par->size();
234 // ok, now copy the paragraph
235 for (pos_type i = 0, j = 0; i <= pos_end; ++i)
236 if (moveItem(*the_next, *par, bparams, i, pos_insert + j))
239 paragraphs.erase(the_next);
243 ParagraphList::iterator depthHook(ParagraphList::iterator pit,
244 ParagraphList const & plist,
245 Paragraph::depth_type depth)
247 ParagraphList::iterator newpit = pit;
248 ParagraphList::iterator beg = const_cast<ParagraphList&>(plist).begin();
253 while (newpit != beg && newpit->getDepth() > depth) {
257 if (newpit->getDepth() > depth)
264 ParagraphList::iterator outerHook(ParagraphList::iterator pit,
265 ParagraphList const & plist)
267 if (!pit->getDepth())
268 return const_cast<ParagraphList&>(plist).end();
269 return depthHook(pit, plist,
270 Paragraph::depth_type(pit->getDepth() - 1));
274 bool isFirstInSequence(ParagraphList::iterator pit,
275 ParagraphList const & plist)
277 ParagraphList::iterator dhook = depthHook(pit, plist, pit->getDepth());
279 || dhook->layout() != pit->layout()
280 || dhook->getDepth() != pit->getDepth());
284 int getEndLabel(ParagraphList::iterator p, ParagraphList const & plist)
286 ParagraphList::iterator pit = p;
287 Paragraph::depth_type par_depth = p->getDepth();
288 while (pit != const_cast<ParagraphList&>(plist).end()) {
289 LyXLayout_ptr const & layout = pit->layout();
290 int const endlabeltype = layout->endlabeltype;
292 if (endlabeltype != END_LABEL_NO_LABEL) {
293 if (boost::next(p) == const_cast<ParagraphList&>(plist).end())
296 Paragraph::depth_type const next_depth = boost::next(p)->getDepth();
297 if (par_depth > next_depth ||
298 (par_depth == next_depth &&
299 layout != boost::next(p)->layout()))
305 pit = outerHook(pit, plist);
306 if (pit != const_cast<ParagraphList&>(plist).end())
307 par_depth = pit->getDepth();
309 return END_LABEL_NO_LABEL;
315 ParagraphList::iterator
316 TeXEnvironment(Buffer const & buf,
317 ParagraphList const & paragraphs,
318 ParagraphList::iterator pit,
319 ostream & os, TexRow & texrow,
320 LatexRunParams const & runparams);
322 ParagraphList::iterator
323 TeXOnePar(Buffer const & buf,
324 ParagraphList const & paragraphs,
325 ParagraphList::iterator pit,
326 ostream & os, TexRow & texrow,
327 LatexRunParams const & runparams,
328 string const & everypar = string());
331 ParagraphList::iterator
332 TeXDeeper(Buffer const & buf,
333 ParagraphList const & paragraphs,
334 ParagraphList::iterator pit,
335 ostream & os, TexRow & texrow,
336 LatexRunParams const & runparams)
338 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
339 ParagraphList::iterator par = pit;
341 while (par != const_cast<ParagraphList&>(paragraphs).end() &&
342 par->params().depth() == pit->params().depth()) {
343 if (par->layout()->isEnvironment()) {
344 par = TeXEnvironment(buf, paragraphs, par,
345 os, texrow, runparams);
347 par = TeXOnePar(buf, paragraphs, par,
348 os, texrow, runparams);
351 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
357 ParagraphList::iterator
358 TeXEnvironment(Buffer const & buf,
359 ParagraphList const & paragraphs,
360 ParagraphList::iterator pit,
361 ostream & os, TexRow & texrow,
362 LatexRunParams const & runparams)
364 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
366 BufferParams const & bparams = buf.params();
368 LyXLayout_ptr const & style = pit->layout();
370 Language const * language = pit->getParLanguage(bparams);
371 Language const * doc_language = bparams.language;
372 Language const * previous_language =
373 (pit != const_cast<ParagraphList&>(paragraphs).begin())
374 ? boost::prior(pit)->getParLanguage(bparams)
376 if (language->babel() != previous_language->babel()) {
378 if (!lyxrc.language_command_end.empty() &&
379 previous_language->babel() != doc_language->babel()) {
380 os << subst(lyxrc.language_command_end, "$$lang",
381 previous_language->babel())
386 if (lyxrc.language_command_end.empty() ||
387 language->babel() != doc_language->babel()) {
388 os << subst(lyxrc.language_command_begin, "$$lang",
395 bool leftindent_open = false;
396 if (!pit->params().leftIndent().zero()) {
397 os << "\\begin{LyXParagraphLeftIndent}{" <<
398 pit->params().leftIndent().asLatexString() << "}\n";
400 leftindent_open = true;
403 if (style->isEnvironment()) {
404 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
405 os << "\\begin{" << style->latexname() << "}{"
406 << pit->params().labelWidthString() << "}\n";
407 } else if (style->labeltype == LABEL_BIBLIO) {
409 os << "\\begin{" << style->latexname() << "}{"
410 << bibitemWidest(buf)
412 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
413 os << "\\begin{" << style->latexname() << '}'
414 << style->latexparam() << '\n';
416 os << "\\begin{" << style->latexname() << '}'
417 << style->latexparam() << '\n';
420 ParagraphList::iterator par = pit;
422 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
424 if (par != const_cast<ParagraphList&>(paragraphs).end() && par->params().depth() > pit->params().depth()) {
425 if (par->layout()->isParagraph()) {
428 // How to handle this? (Lgb)
429 //&& !suffixIs(os, "\n\n")
431 // There should be at least one '\n' already
432 // but we need there to be two for Standard
433 // paragraphs that are depth-increment'ed to be
434 // output correctly. However, tables can
435 // also be paragraphs so don't adjust them.
438 // Will it ever harm to have one '\n' too
439 // many? i.e. that we sometimes will have
440 // three in a row. (Lgb)
444 par = TeXDeeper(buf, paragraphs, par, os, texrow,
447 } while (par != const_cast<ParagraphList&>(paragraphs).end()
448 && par->layout() == pit->layout()
449 && par->params().depth() == pit->params().depth()
450 && par->params().leftIndent() == pit->params().leftIndent());
452 if (style->isEnvironment()) {
453 os << "\\end{" << style->latexname() << "}\n";
457 if (leftindent_open) {
458 os << "\\end{LyXParagraphLeftIndent}\n";
462 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
463 return par; // ale970302
467 InsetOptArg * optArgInset(Paragraph const & par)
470 InsetList::const_iterator it = par.insetlist.begin();
471 InsetList::const_iterator end = par.insetlist.end();
472 for (; it != end; ++it) {
473 InsetOld * ins = it->inset;
474 if (ins->lyxCode() == InsetOld::OPTARG_CODE) {
475 return static_cast<InsetOptArg *>(ins);
482 ParagraphList::iterator
483 TeXOnePar(Buffer const & buf,
484 ParagraphList const & paragraphs,
485 ParagraphList::iterator pit,
486 ostream & os, TexRow & texrow,
487 LatexRunParams const & runparams,
488 string const & everypar)
490 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
491 << everypar << "'" << endl;
492 BufferParams const & bparams = buf.params();
494 InsetOld const * in = pit->inInset();
495 bool further_blank_line = false;
498 // well we have to check if we are in an inset with unlimited
499 // length (all in one row) if that is true then we don't allow
500 // any special options in the paragraph and also we don't allow
501 // any environment other then "Standard" to be valid!
502 if (in == 0 || !in->forceDefaultParagraphs(in)) {
503 style = pit->layout();
505 if (pit->params().startOfAppendix()) {
506 os << "\\appendix\n";
510 if (!pit->params().spacing().isDefault()
511 && (pit == const_cast<ParagraphList&>(paragraphs).begin()
512 || !boost::prior(pit)->hasSameLayout(*pit)))
514 os << pit->params().spacing().writeEnvirBegin() << '\n';
518 if (style->isCommand()) {
523 if (pit->params().spaceTop().kind() != VSpace::NONE) {
524 os << pit->params().spaceTop().asLatexCommand(bparams);
525 further_blank_line = true;
528 if (further_blank_line) {
533 style = bparams.getLyXTextClass().defaultLayout();
536 Language const * language = pit->getParLanguage(bparams);
537 Language const * doc_language = bparams.language;
538 Language const * previous_language =
539 (pit != const_cast<ParagraphList&>(paragraphs).begin())
540 ? boost::prior(pit)->getParLanguage(bparams)
543 if (language->babel() != previous_language->babel()
544 // check if we already put language command in TeXEnvironment()
545 && !(style->isEnvironment()
546 && (pit == const_cast<ParagraphList&>(paragraphs).begin() ||
547 (boost::prior(pit)->layout() != pit->layout() &&
548 boost::prior(pit)->getDepth() <= pit->getDepth())
549 || boost::prior(pit)->getDepth() < pit->getDepth())))
551 if (!lyxrc.language_command_end.empty() &&
552 previous_language->babel() != doc_language->babel())
554 os << subst(lyxrc.language_command_end, "$$lang",
555 previous_language->babel())
560 if (lyxrc.language_command_end.empty() ||
561 language->babel() != doc_language->babel())
563 os << subst(lyxrc.language_command_begin, "$$lang",
570 if (bparams.inputenc == "auto" &&
571 language->encoding() != previous_language->encoding()) {
572 os << "\\inputencoding{"
573 << language->encoding()->LatexName()
578 switch (style->latextype) {
580 os << '\\' << style->latexname();
582 // Separate handling of optional argument inset.
583 if (style->optionalargs == 1) {
584 InsetOptArg * it = optArgInset(*pit);
586 it->latexOptional(buf, os, runparams);
589 os << style->latexparam();
591 case LATEX_ITEM_ENVIRONMENT:
592 case LATEX_LIST_ENVIRONMENT:
595 case LATEX_BIB_ENVIRONMENT:
596 // ignore this, the inset will write itself
603 bool need_par = pit->simpleTeXOnePar(buf, bparams,
604 outerFont(pit, paragraphs),
605 os, texrow, runparams);
607 // Make sure that \\par is done with the font of the last
608 // character if this has another size as the default.
609 // This is necessary because LaTeX (and LyX on the screen)
610 // calculates the space between the baselines according
611 // to this font. (Matthias)
613 // Is this really needed ? (Dekel)
614 // We do not need to use to change the font for the last paragraph
616 LyXFont const outerfont(outerFont(pit, paragraphs));
620 ? pit->getLayoutFont(bparams, outerfont)
621 : pit->getFont(bparams, pit->size() - 1, outerfont));
623 bool is_command = style->isCommand();
625 if (style->resfont.size() != font.size()
626 && boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
630 os << "\\" << font.latexSize() << " \\par}";
631 } else if (need_par) {
633 } else if (is_command)
636 switch (style->latextype) {
637 case LATEX_ITEM_ENVIRONMENT:
638 case LATEX_LIST_ENVIRONMENT:
639 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
640 && (pit->params().depth() < boost::next(pit)->params().depth())) {
645 case LATEX_ENVIRONMENT: {
646 // if its the last paragraph of the current environment
647 // skip it otherwise fall through
648 ParagraphList::iterator next = boost::next(pit);
650 if (next != const_cast<ParagraphList&>(paragraphs).end()
651 && (next->layout() != pit->layout()
652 || next->params().depth() != pit->params().depth()))
656 // fall through possible
658 // we don't need it for the last paragraph!!!
659 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
665 if (in == 0 || !in->forceDefaultParagraphs(in)) {
666 further_blank_line = false;
668 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
669 os << pit->params().spaceBottom().asLatexCommand(bparams);
670 further_blank_line = true;
673 if (further_blank_line) {
678 if (!pit->params().spacing().isDefault()
679 && (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
680 || !boost::next(pit)->hasSameLayout(*pit)))
682 os << pit->params().spacing().writeEnvirEnd() << '\n';
687 // we don't need it for the last paragraph!!!
688 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
692 // Since \selectlanguage write the language to the aux file,
693 // we need to reset the language at the end of footnote or
696 if (language->babel() != doc_language->babel()) {
697 if (lyxrc.language_command_end.empty())
698 os << subst(lyxrc.language_command_begin,
700 doc_language->babel())
703 os << subst(lyxrc.language_command_end,
711 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
719 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
721 void latexParagraphs(Buffer const & buf,
722 ParagraphList const & paragraphs,
725 LatexRunParams const & runparams,
726 string const & everypar)
728 bool was_title = false;
729 bool already_title = false;
730 LyXTextClass const & tclass = buf.params().getLyXTextClass();
731 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
732 ParagraphList::iterator endpar = const_cast<ParagraphList&>(paragraphs).end();
735 while (par != endpar) {
736 InsetOld * in = par->inInset();
737 // well we have to check if we are in an inset with unlimited
738 // length (all in one row) if that is true then we don't allow
739 // any special options in the paragraph and also we don't allow
740 // any environment other then "Standard" to be valid!
741 if (in == 0 || !in->forceDefaultParagraphs(in)) {
742 LyXLayout_ptr const & layout = par->layout();
744 if (layout->intitle) {
746 lyxerr <<"Error in latexParagraphs: You"
747 " should not mix title layouts"
748 " with normal ones." << endl;
749 } else if (!was_title) {
751 if (tclass.titletype() == TITLE_ENVIRONMENT) {
753 << tclass.titlename()
758 } else if (was_title && !already_title) {
759 if (tclass.titletype() == TITLE_ENVIRONMENT) {
760 os << "\\end{" << tclass.titlename()
764 os << "\\" << tclass.titlename()
768 already_title = true;
772 if (layout->is_environment) {
773 par = TeXOnePar(buf, paragraphs, par, os, texrow,
774 runparams, everypar);
775 } else if (layout->isEnvironment() ||
776 !par->params().leftIndent().zero())
778 par = TeXEnvironment(buf, paragraphs, par, os,
781 par = TeXOnePar(buf, paragraphs, par, os, texrow,
782 runparams, everypar);
785 par = TeXOnePar(buf, paragraphs, par, os, texrow,
786 runparams, everypar);
789 // It might be that we only have a title in this document
790 if (was_title && !already_title) {
791 if (tclass.titletype() == TITLE_ENVIRONMENT) {
792 os << "\\end{" << tclass.titlename()
796 os << "\\" << tclass.titlename()
806 pair<int, string> const addDepth(int depth, int ldepth)
810 d += (ldepth - depth) * 2;
811 return make_pair(d, string(d, ' '));
817 void asciiParagraph(Buffer const & buf,
818 Paragraph const & par,
820 LatexRunParams const & runparams,
824 Paragraph::depth_type ltype_depth = 0;
825 bool ref_printed = false;
826 Paragraph::depth_type depth = par.params().depth();
828 // First write the layout
829 string const & tmp = par.layout()->name();
830 if (compare_no_case(tmp, "itemize") == 0) {
832 ltype_depth = depth + 1;
833 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
835 ltype_depth = depth + 1;
836 } else if (contains(ascii_lowercase(tmp), "ection")) {
838 ltype_depth = depth + 1;
839 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
841 ltype_depth = depth + 1;
842 } else if (compare_ascii_no_case(tmp, "description") == 0) {
844 ltype_depth = depth + 1;
845 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
848 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
856 /* maybe some vertical spaces */
858 /* the labelwidthstring used in lists */
862 /* some pagebreaks? */
866 /* what about the alignment */
868 // runparams.linelen <= 0 is special and means we don't have paragraph breaks
870 string::size_type currlinelen = 0;
873 if (runparams.linelen > 0)
876 os << string(depth * 2, ' ');
877 currlinelen += depth * 2;
880 // we should probably change to the paragraph language in the
881 // gettext here (if possible) so that strings are outputted in
882 // the correct language! (20012712 Jug)
886 case 4: // (Sub)Paragraph
887 case 5: // Description
890 if (runparams.linelen > 0) {
891 os << _("Abstract") << "\n\n";
894 string const abst = _("Abstract: ");
896 currlinelen += abst.length();
899 case 7: // Bibliography
901 if (runparams.linelen > 0) {
902 os << _("References") << "\n\n";
905 string const refs = _("References: ");
907 currlinelen += refs.length();
915 string const parlab = par.params().labelString();
917 currlinelen += parlab.length() + 1;
925 pair<int, string> p = addDepth(depth, ltype_depth);
927 currlinelen += p.first;
930 // this is to change the linebreak to do it by word a bit more
931 // intelligent hopefully! (only in the case where we have a
932 // max runparams.linelength!) (Jug)
936 for (pos_type i = 0; i < par.size(); ++i) {
937 char c = par.getUChar(buf.params(), i);
939 case Paragraph::META_INSET:
941 InsetOld const * inset = par.getInset(i);
943 if (runparams.linelen > 0) {
945 currlinelen += word.length();
948 if (inset->ascii(buf, os, runparams)) {
949 // to be sure it breaks paragraph
950 currlinelen += runparams.linelen;
958 if (runparams.linelen > 0 &&
959 currlinelen + word.length() > runparams.linelen - 10) {
961 pair<int, string> p = addDepth(depth, ltype_depth);
963 currlinelen = p.first;
967 currlinelen += word.length() + 1;
974 lyxerr[Debug::INFO] <<
975 "writeAsciiFile: NULL char in structure." << endl;
977 if ((runparams.linelen > 0) &&
978 (currlinelen + word.length()) > runparams.linelen)
982 pair<int, string> p =
983 addDepth(depth, ltype_depth);
985 currlinelen = p.first;
995 void linuxdocParagraphs(Buffer const & buf,
996 ParagraphList const & paragraphs,
998 LatexRunParams const & runparams)
1001 Paragraph::depth_type depth = 0; // paragraph depth
1003 vector<string> environment_stack(5);
1005 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1006 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1007 for (; pit != pend; ++pit) {
1008 LyXLayout_ptr const & style = pit->layout();
1009 // treat <toc> as a special case for compatibility with old code
1010 if (pit->isInset(0)) {
1011 InsetOld * inset = pit->getInset(0);
1012 InsetOld::Code lyx_code = inset->lyxCode();
1013 if (lyx_code == InsetOld::TOC_CODE) {
1014 string const temp = "toc";
1015 sgml::openTag(os, depth, false, temp);
1020 // environment tag closing
1021 for (; depth > pit->params().depth(); --depth) {
1022 sgml::closeTag(os, depth, false, environment_stack[depth]);
1023 environment_stack[depth].erase();
1026 // write opening SGML tags
1027 switch (style->latextype) {
1028 case LATEX_PARAGRAPH:
1029 if (depth == pit->params().depth()
1030 && !environment_stack[depth].empty()) {
1031 sgml::closeTag(os, depth, false, environment_stack[depth]);
1032 environment_stack[depth].erase();
1038 sgml::openTag(os, depth, false, style->latexname());
1043 //error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
1046 if (!environment_stack[depth].empty()) {
1047 sgml::closeTag(os, depth, false, environment_stack[depth]);
1051 environment_stack[depth].erase();
1052 sgml::openTag(os, depth, false, style->latexname());
1055 case LATEX_ENVIRONMENT:
1056 case LATEX_ITEM_ENVIRONMENT:
1057 case LATEX_BIB_ENVIRONMENT:
1059 string const & latexname = style->latexname();
1061 if (depth == pit->params().depth()
1062 && environment_stack[depth] != latexname) {
1063 sgml::closeTag(os, depth, false,
1064 environment_stack[depth]);
1065 environment_stack[depth].erase();
1067 if (depth < pit->params().depth()) {
1068 depth = pit->params().depth();
1069 environment_stack[depth].erase();
1071 if (environment_stack[depth] != latexname) {
1073 sgml::openTag(os, depth, false, "p");
1075 sgml::openTag(os, depth, false, latexname);
1077 if (environment_stack.size() == depth + 1)
1078 environment_stack.push_back("!-- --");
1079 environment_stack[depth] = latexname;
1082 if (style->latexparam() == "CDATA")
1085 if (style->latextype == LATEX_ENVIRONMENT) break;
1087 if (style->labeltype == LABEL_MANUAL)
1092 sgml::openTag(os, depth + 1, false, item_name);
1097 sgml::openTag(os, depth, false, style->latexname());
1101 pit->simpleLinuxDocOnePar(buf, os, outerFont(pit, paragraphs),
1105 // write closing SGML tags
1106 switch (style->latextype) {
1109 case LATEX_ENVIRONMENT:
1110 case LATEX_ITEM_ENVIRONMENT:
1111 case LATEX_BIB_ENVIRONMENT:
1112 if (style->latexparam() == "CDATA")
1116 sgml::closeTag(os, depth, false, style->latexname());
1122 for (int i = depth; i >= 0; --i)
1123 sgml::closeTag(os, depth, false, environment_stack[i]);
1127 void docbookParagraphs(Buffer const & buf,
1128 ParagraphList const & paragraphs,
1130 LatexRunParams const & runparams)
1132 vector<string> environment_stack(10);
1133 vector<string> environment_inner(10);
1134 vector<string> command_stack(10);
1136 bool command_flag = false;
1137 Paragraph::depth_type command_depth = 0;
1138 Paragraph::depth_type command_base = 0;
1139 Paragraph::depth_type cmd_depth = 0;
1140 Paragraph::depth_type depth = 0; // paragraph depth
1143 string command_name;
1145 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
1146 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1148 for (; par != pend; ++par) {
1152 int desc_on = 0; // description mode
1154 LyXLayout_ptr const & style = par->layout();
1156 // environment tag closing
1157 for (; depth > par->params().depth(); --depth) {
1158 if (!environment_inner[depth].empty())
1159 sgml::closeEnvTags(os, false, environment_inner[depth],
1160 command_depth + depth);
1161 sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
1162 environment_stack[depth].erase();
1163 environment_inner[depth].erase();
1166 if (depth == par->params().depth()
1167 && environment_stack[depth] != style->latexname()
1168 && !environment_stack[depth].empty()) {
1169 sgml::closeEnvTags(os, false, environment_inner[depth],
1170 command_depth + depth);
1171 sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
1173 environment_stack[depth].erase();
1174 environment_inner[depth].erase();
1177 // Write opening SGML tags.
1178 switch (style->latextype) {
1179 case LATEX_PARAGRAPH:
1180 sgml::openTag(os, depth + command_depth,
1181 false, style->latexname());
1186 //error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
1189 command_name = style->latexname();
1191 sgmlparam = style->latexparam();
1192 c_params = split(sgmlparam, c_depth,'|');
1194 cmd_depth = atoi(c_depth);
1197 if (cmd_depth < command_base) {
1198 for (Paragraph::depth_type j = command_depth;
1199 j >= command_base; --j) {
1200 sgml::closeTag(os, j, false, command_stack[j]);
1203 command_depth = command_base = cmd_depth;
1204 } else if (cmd_depth <= command_depth) {
1205 for (int j = command_depth;
1206 j >= int(cmd_depth); --j) {
1207 sgml::closeTag(os, j, false, command_stack[j]);
1210 command_depth = cmd_depth;
1212 command_depth = cmd_depth;
1214 command_depth = command_base = cmd_depth;
1215 command_flag = true;
1217 if (command_stack.size() == command_depth + 1)
1218 command_stack.push_back(string());
1219 command_stack[command_depth] = command_name;
1221 // treat label as a special case for
1222 // more WYSIWYM handling.
1223 // This is a hack while paragraphs can't have
1224 // attributes, like id in this case.
1225 if (par->isInset(0)) {
1226 InsetOld * inset = par->getInset(0);
1227 InsetOld::Code lyx_code = inset->lyxCode();
1228 if (lyx_code == InsetOld::LABEL_CODE) {
1229 command_name += " id=\"";
1230 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1231 command_name += '"';
1236 sgml::openTag(os, depth + command_depth, false, command_name);
1238 item_name = c_params.empty() ? "title" : c_params;
1239 sgml::openTag(os, depth + 1 + command_depth, false, item_name);
1242 case LATEX_ENVIRONMENT:
1243 case LATEX_ITEM_ENVIRONMENT:
1244 if (depth < par->params().depth()) {
1245 depth = par->params().depth();
1246 environment_stack[depth].erase();
1249 if (environment_stack[depth] != style->latexname()) {
1250 if (environment_stack.size() == depth + 1) {
1251 environment_stack.push_back("!-- --");
1252 environment_inner.push_back("!-- --");
1254 environment_stack[depth] = style->latexname();
1255 environment_inner[depth] = "!-- --";
1256 sgml::openTag(os, depth + command_depth, false, environment_stack[depth]);
1258 sgml::closeEnvTags(os, false, environment_inner[depth],
1259 command_depth + depth);
1262 if (style->latextype == LATEX_ENVIRONMENT) {
1263 if (!style->latexparam().empty()) {
1264 if (style->latexparam() == "CDATA")
1267 sgml::openTag(os, depth + command_depth, false, style->latexparam());
1272 desc_on = (style->labeltype == LABEL_MANUAL);
1274 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1275 sgml::openTag(os, depth + 1 + command_depth,
1276 false, environment_inner[depth]);
1278 item_name = desc_on ? "term" : "para";
1279 sgml::openTag(os, depth + 1 + command_depth,
1283 sgml::openTag(os, depth + command_depth,
1284 false, style->latexname());
1288 par->simpleDocBookOnePar(buf, os, outerFont(par, paragraphs), desc_on,
1289 runparams, depth + 1 + command_depth);
1292 // write closing SGML tags
1293 switch (style->latextype) {
1295 end_tag = c_params.empty() ? "title" : c_params;
1296 sgml::closeTag(os, depth + command_depth,
1299 case LATEX_ENVIRONMENT:
1300 if (!style->latexparam().empty()) {
1301 if (style->latexparam() == "CDATA")
1304 sgml::closeTag(os, depth + command_depth, false, style->latexparam());
1307 case LATEX_ITEM_ENVIRONMENT:
1308 if (desc_on == 1) break;
1310 sgml::closeTag(os, depth + 1 + command_depth, false, end_tag);
1312 case LATEX_PARAGRAPH:
1313 sgml::closeTag(os, depth + command_depth, false, style->latexname());
1316 sgml::closeTag(os, depth + command_depth, false, style->latexname());
1322 for (int d = depth; d >= 0; --d) {
1323 if (!environment_stack[depth].empty()) {
1324 sgml::closeEnvTags(os, false, environment_inner[depth],
1325 command_depth + depth);
1329 for (int j = command_depth; j >= 0 ; --j)
1330 if (!command_stack[j].empty()) {
1331 sgml::closeTag(os, j, false, command_stack[j]);
1338 int readParToken(Buffer & buf, Paragraph & par, LyXLex & lex, string const & token)
1340 static LyXFont font;
1341 static Change change;
1343 BufferParams const & bp = buf.params();
1345 if (token[0] != '\\') {
1346 string::const_iterator cit = token.begin();
1347 for (; cit != token.end(); ++cit) {
1348 par.insertChar(par.size(), (*cit), font, change);
1350 } else if (token == "\\begin_layout") {
1352 string layoutname = lex.getString();
1354 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
1357 LyXTextClass const & tclass = bp.getLyXTextClass();
1359 if (layoutname.empty()) {
1360 layoutname = tclass.defaultLayoutName();
1363 bool hasLayout = tclass.hasLayout(layoutname);
1366 lyxerr << "Layout '" << layoutname << "' does not"
1367 << " exist in textclass '" << tclass.name()
1369 lyxerr << "Trying to use default layout instead."
1371 layoutname = tclass.defaultLayoutName();
1374 par.layout(bp.getLyXTextClass()[layoutname]);
1376 // Test whether the layout is obsolete.
1377 LyXLayout_ptr const & layout = par.layout();
1378 if (!layout->obsoleted_by().empty())
1379 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
1381 par.params().read(lex);
1383 } else if (token == "\\end_layout") {
1384 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
1385 << "Missing \\begin_layout?.\n";
1386 } else if (token == "\\end_inset") {
1387 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
1388 << "Missing \\begin_inset?.\n";
1389 } else if (token == "\\begin_inset") {
1390 InsetOld * inset = readInset(lex, buf);
1392 par.insertInset(par.size(), inset, font, change);
1395 string line = lex.getString();
1396 buf.error(ErrorItem(_("Unknown Inset"), line,
1397 buf.paragraphs().back().id(),
1401 } else if (token == "\\family") {
1403 font.setLyXFamily(lex.getString());
1404 } else if (token == "\\series") {
1406 font.setLyXSeries(lex.getString());
1407 } else if (token == "\\shape") {
1409 font.setLyXShape(lex.getString());
1410 } else if (token == "\\size") {
1412 font.setLyXSize(lex.getString());
1413 } else if (token == "\\lang") {
1415 string const tok = lex.getString();
1416 Language const * lang = languages.getLanguage(tok);
1418 font.setLanguage(lang);
1420 font.setLanguage(bp.language);
1421 lex.printError("Unknown language `$$Token'");
1423 } else if (token == "\\numeric") {
1425 font.setNumber(font.setLyXMisc(lex.getString()));
1426 } else if (token == "\\emph") {
1428 font.setEmph(font.setLyXMisc(lex.getString()));
1429 } else if (token == "\\bar") {
1431 string const tok = lex.getString();
1434 font.setUnderbar(LyXFont::ON);
1435 else if (tok == "no")
1436 font.setUnderbar(LyXFont::OFF);
1437 else if (tok == "default")
1438 font.setUnderbar(LyXFont::INHERIT);
1440 lex.printError("Unknown bar font flag "
1442 } else if (token == "\\noun") {
1444 font.setNoun(font.setLyXMisc(lex.getString()));
1445 } else if (token == "\\color") {
1447 font.setLyXColor(lex.getString());
1448 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
1450 // Insets don't make sense in a free-spacing context! ---Kayvan
1451 if (par.isFreeSpacing()) {
1452 if (token == "\\InsetSpace")
1453 par.insertChar(par.size(), ' ', font, change);
1454 else if (lex.isOK()) {
1456 string const next_token = lex.getString();
1457 if (next_token == "\\-")
1458 par.insertChar(par.size(), '-', font, change);
1460 lex.printError("Token `$$Token' "
1462 "paragraph layout!");
1466 auto_ptr<InsetOld> inset;
1467 if (token == "\\SpecialChar" )
1468 inset.reset(new InsetSpecialChar);
1470 inset.reset(new InsetSpace);
1471 inset->read(buf, lex);
1472 par.insertInset(par.size(), inset.release(),
1475 } else if (token == "\\i") {
1476 auto_ptr<InsetOld> inset(new InsetLatexAccent);
1477 inset->read(buf, lex);
1478 par.insertInset(par.size(), inset.release(), font, change);
1479 } else if (token == "\\backslash") {
1480 par.insertChar(par.size(), '\\', font, change);
1481 } else if (token == "\\newline") {
1482 auto_ptr<InsetOld> inset(new InsetNewline);
1483 inset->read(buf, lex);
1484 par.insertInset(par.size(), inset.release(), font, change);
1485 } else if (token == "\\LyXTable") {
1486 auto_ptr<InsetOld> inset(new InsetTabular(buf));
1487 inset->read(buf, lex);
1488 par.insertInset(par.size(), inset.release(), font, change);
1489 } else if (token == "\\bibitem") {
1490 InsetCommandParams p("bibitem", "dummy");
1491 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
1492 inset->read(buf, lex);
1493 par.insertInset(par.size(), inset.release(), font, change);
1494 } else if (token == "\\hfill") {
1495 par.insertInset(par.size(), new InsetHFill, font, change);
1496 } else if (token == "\\lyxline") {
1497 par.insertInset(par.size(), new InsetLine, font, change);
1498 } else if (token == "\\newpage") {
1499 par.insertInset(par.size(), new InsetPagebreak, font, change);
1500 } else if (token == "\\change_unchanged") {
1501 // Hack ! Needed for empty paragraphs :/
1502 // FIXME: is it still ??
1505 change = Change(Change::UNCHANGED);
1506 } else if (token == "\\change_inserted") {
1508 istringstream is(lex.getString());
1512 change = Change(Change::INSERTED, bp.author_map[aid], ct);
1513 } else if (token == "\\change_deleted") {
1515 istringstream is(lex.getString());
1519 change = Change(Change::DELETED, bp.author_map[aid], ct);
1522 string const s = bformat(_("Unknown token: %1$s %2$s\n"),
1523 token, lex.getString());
1525 buf.error(ErrorItem(_("Unknown token"), s,
1526 buf.paragraphs().back().id(),
1536 int readParagraph(Buffer & buf, Paragraph & par, LyXLex & lex)
1541 string token = lex.getString();
1543 while (lex.isOK()) {
1545 unknown += readParToken(buf, par, lex, token);
1548 token = lex.getString();
1553 if (token == "\\end_layout") {
1554 //Ok, paragraph finished
1558 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
1559 << token << '\'' << endl;
1560 if (token == "\\begin_layout" || token == "\\end_document"
1561 || token == "\\end_inset" || token == "\\begin_deeper"
1562 || token == "\\end_deeper") {
1563 lex.pushToken(token);
1564 lyxerr << "Paragraph ended in line "
1565 << lex.getLineNo() << "\n"
1566 << "Missing \\end_layout.\n";
1575 LyXFont const outerFont(ParagraphList::iterator pit,
1576 ParagraphList const & plist)
1578 Paragraph::depth_type par_depth = pit->getDepth();
1579 LyXFont tmpfont(LyXFont::ALL_INHERIT);
1581 // Resolve against environment font information
1582 while (pit != const_cast<ParagraphList&>(plist).end() &&
1583 par_depth && !tmpfont.resolved()) {
1584 pit = outerHook(pit, plist);
1585 if (pit != const_cast<ParagraphList&>(plist).end()) {
1586 tmpfont.realize(pit->layout()->font);
1587 par_depth = pit->getDepth();
1595 ParagraphList::iterator outerPar(Buffer const & buf, InsetOld const * inset)
1597 ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
1598 ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
1599 for ( ; pit != end; ++pit) {
1601 ParagraphList * plist;
1602 // the second '=' below is intentional
1603 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
1604 if (plist == &pit.plist())
1605 return pit.outerPar();
1607 InsetList::iterator ii = pit->insetlist.begin();
1608 InsetList::iterator iend = pit->insetlist.end();
1609 for ( ; ii != iend; ++ii)
1610 if (ii->inset == inset)
1611 return pit.outerPar();
1613 lyxerr << "outerPar: should not happen" << endl;
1614 BOOST_ASSERT(false);
1615 return const_cast<Buffer &>(buf).paragraphs().end(); // shut up compiler
1619 Paragraph const & ownerPar(Buffer const & buf, InsetOld const * inset)
1621 ParConstIterator pit = buf.par_iterator_begin();
1622 ParConstIterator end = buf.par_iterator_end();
1623 for ( ; pit != end; ++pit) {
1624 ParagraphList * plist;
1625 // the second '=' below is intentional
1626 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
1627 if (plist == &pit.plist())
1630 InsetList::const_iterator ii = pit->insetlist.begin();
1631 InsetList::const_iterator iend = pit->insetlist.end();
1632 for ( ; ii != iend; ++ii)
1633 if (ii->inset == inset)
1636 lyxerr << "ownerPar: should not happen" << endl;
1637 BOOST_ASSERT(false);
1638 return buf.paragraphs().front(); // shut up compiler