From d4f58c96b5d855b1b0c9ebefd44956c079ab7c55 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Fri, 26 Jul 2024 12:48:12 +0200 Subject: [PATCH] Add support for multiple indexes in Memoir As with almost everything, this class bakes its own cake also with multiple indexes. So we need to account for this to produce compilable output. Not very nice, but there you go! --- lib/layouts/memoir.layout | 2 ++ src/Buffer.cpp | 2 ++ src/BufferParams.cpp | 12 +++++++++-- src/Converter.cpp | 10 +++++++++ src/LaTeX.cpp | 44 ++++++++++++++++++++++++++++++++++++--- src/LaTeX.h | 2 +- src/LaTeXFeatures.cpp | 2 +- src/OutputParams.h | 5 +++++ src/insets/InsetIndex.cpp | 17 +++++++++++++-- 9 files changed, 87 insertions(+), 9 deletions(-) diff --git a/lib/layouts/memoir.layout b/lib/layouts/memoir.layout index 61184d2398..89f0da70db 100644 --- a/lib/layouts/memoir.layout +++ b/lib/layouts/memoir.layout @@ -18,6 +18,8 @@ PageSize letter Provides makeidx 1 Provides framed 1 Provides subscript 1 +# Memoir has its own implementation +Provides memoir-idx 1 # Geometry is supported, but the package options and paper sizes # are ignored Provides geometry-light 1 diff --git a/src/Buffer.cpp b/src/Buffer.cpp index a13c8e5f9f..efa6492863 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -1886,6 +1886,7 @@ Buffer::ExportStatus Buffer::writeLaTeXSource(otexstream & os, runparams.use_hyperref = features.isRequired("hyperref") || features.isProvided("hyperref"); runparams.use_CJK = features.mustProvide("CJK"); + runparams.use_memindex = features.isProvided("memoir-idx"); } LYXERR(Debug::OUTFILE, " Buffer validation done."); @@ -4220,6 +4221,7 @@ unique_ptr Buffer::getSourceCode(odocstream & os, string const & format, runparams.use_babel = features.useBabel(); runparams.use_hyperref = features.isRequired("hyperref") || features.isProvided("hyperref"); + runparams.use_memindex = features.isProvided("memoir-idx"); // latex or literate otexstream ots(os); // output above diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index a5215f6fc8..0a01f9bb4f 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -2279,9 +2279,17 @@ bool BufferParams::writeLaTeX(otexstream & os, LaTeXFeatures & features, // Additional Indices if (features.isRequired("splitidx")) { for (auto const & idx : indiceslist()) { - os << "\\newindex{"; + if (features.isProvided("memoir-idx")) { + if (idx.shortcut() == "idx") + continue; + os << "\\makeindex["; + } else + os << "\\newindex{"; os << escape(idx.shortcut()); - os << "}\n"; + if (features.isProvided("memoir-idx")) + os << "]\n"; + else + os << "}\n"; } } diff --git a/src/Converter.cpp b/src/Converter.cpp index 881aac6bd3..21c1938398 100644 --- a/src/Converter.cpp +++ b/src/Converter.cpp @@ -21,6 +21,7 @@ #include "Format.h" #include "InsetList.h" #include "Language.h" +#include "LaTeXFeatures.h" #include "LaTeX.h" #include "LyXRC.h" #include "Mover.h" @@ -555,6 +556,15 @@ Converters::RetVal Converters::convert(Buffer const * buffer, "tmpfile.out")); } + if (buffer && buffer->params().use_indices && conv.latex()) { + // We need to validate the buffer to get access to features. + // FIXME Not nice that we need to do this here. + LYXERR(Debug::OUTFILE, " Validating buffer..."); + LaTeXFeatures features(*buffer, buffer->params(), runparams); + buffer->validate(features); + runparams.use_memindex = features.isProvided("memoir-idx"); + } + if (buffer && buffer->params().use_minted && lyxrc.pygmentize_command.empty() && conv.latex()) { bool dowarn = false; diff --git a/src/LaTeX.cpp b/src/LaTeX.cpp index 84141d8341..afc3eb5c32 100644 --- a/src/LaTeX.cpp +++ b/src/LaTeX.cpp @@ -20,6 +20,7 @@ #include "Buffer.h" #include "BufferList.h" #include "BufferParams.h" +#include "IndicesList.h" #include "LyXRC.h" #include "LyX.h" #include "DepTable.h" @@ -466,6 +467,41 @@ int LaTeX::run(TeXErrors & terr) iscanres = scanIlgFile(terr); rerun = true; } + // This is Memoir's multi-index idiosyncracy + if (runparams.use_indices && runparams.use_memindex) { + Buffer const * buf = theBufferList().getBufferFromTmp(file.absFileName()); + if (buf) { + IndicesList const & indiceslist = buf->params().indiceslist(); + if (!indiceslist.empty()) { + IndicesList::const_iterator it = indiceslist.begin(); + IndicesList::const_iterator const end = indiceslist.end(); + for (; it != end; ++it) { + docstring const & ci = it->shortcut(); + if (ci == "idx") + continue; + FileName const aidxfile(to_utf8(ci + ".idx")); + LYXERR(Debug::OUTFILE, "Running Index Processor for " << ci); + message(_("Running Index Processor.")); + // onlyFileName() is needed for cygwin + int const ret = + runMakeIndex(onlyFileName(aidxfile.absFileName()), runparams); + if (ret == Systemcall::KILLED || ret == Systemcall::TIMEOUT) + return ret; + else if (ret != Systemcall::OK) { + iscanres |= INDEX_ERROR; + terr.insertError(0, + _("Index Processor Error"), + _("The index processor did not run successfully. " + "Please check the output of View > Messages Pane!")); + } + FileName const ailgfile(changeExtension(aidxfile.absFileName(), ".ilg")); + if (ailgfile.exists()) + iscanres = scanIlgFile(terr, ailgfile); + rerun = true; + } + } + } + } if (run_nomencl) { int const ret = runMakeIndexNomencl(file, ".nlo", ".nls"); if (ret == Systemcall::KILLED || ret == Systemcall::TIMEOUT) @@ -592,7 +628,7 @@ int LaTeX::runMakeIndex(string const & f, OutputParams const & rp, tmp = subst(tmp, "$$lang", doc_lang->babel()); tmp = subst(tmp, "$$lcode", doc_lang->code()); } - if (rp.use_indices) { + if (rp.use_indices && !rp.use_memindex) { tmp = lyxrc.splitindex_command + " -m " + quoteName(tmp); LYXERR(Debug::OUTFILE, "Multiple indices. Using splitindex command: " << tmp); @@ -1640,9 +1676,11 @@ int LaTeX::scanBlgFile(DepTable & dep, TeXErrors & terr) } -int LaTeX::scanIlgFile(TeXErrors & terr) +int LaTeX::scanIlgFile(TeXErrors & terr, FileName const fn) { - FileName const ilg_file(changeExtension(file.absFileName(), "ilg")); + FileName const ilg_file = fn.empty() + ? FileName(changeExtension(file.absFileName(), "ilg")) + : fn; LYXERR(Debug::OUTFILE, "Scanning ilg file: " << ilg_file); ifstream ifs(ilg_file.toFilesystemEncoding().c_str()); diff --git a/src/LaTeX.h b/src/LaTeX.h index 569114d369..502834348b 100644 --- a/src/LaTeX.h +++ b/src/LaTeX.h @@ -230,7 +230,7 @@ private: int scanBlgFile(DepTable & head, TeXErrors & terr); /// - int scanIlgFile(TeXErrors & terr); + int scanIlgFile(TeXErrors & terr, support::FileName const fn = support::FileName()); /// bool runBibTeX(std::vector const &, diff --git a/src/LaTeXFeatures.cpp b/src/LaTeXFeatures.cpp index 74941459e6..81022cad01 100644 --- a/src/LaTeXFeatures.cpp +++ b/src/LaTeXFeatures.cpp @@ -1360,7 +1360,7 @@ string const LaTeXFeatures::getPackages() const if (isRequired("makeidx") || isRequired("splitidx")) { if (!isProvided("makeidx") && !isRequired("splitidx")) packages << "\\usepackage{makeidx}\n"; - if (mustProvide("splitidx")) + if (mustProvide("splitidx") && !isProvided("memoir-idx")) packages << "\\usepackage{splitidx}\n"; packages << "\\makeindex\n"; } diff --git a/src/OutputParams.h b/src/OutputParams.h index 671bc23892..306087dfdf 100644 --- a/src/OutputParams.h +++ b/src/OutputParams.h @@ -223,6 +223,11 @@ public: */ bool use_indices = false; + /** Are we using the memoir way to + * to multiple indices? + */ + bool use_memindex = false; + /** Are we using japanese (pLaTeX)? */ bool use_japanese = false; diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp index 4a4c036a99..81870f8282 100644 --- a/src/insets/InsetIndex.cpp +++ b/src/insets/InsetIndex.cpp @@ -142,7 +142,10 @@ void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) cons if (buffer().masterBuffer()->params().use_indices && !params_.index.empty() && params_.index != "idx") { - os << "\\sindex["; + if (runparams.use_memindex) + os << "\\index["; + else + os << "\\sindex["; os << escape(params_.index); os << "]{"; } else { @@ -1406,7 +1409,17 @@ void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) return; } OutputParams runparams = runparams_in; - os << getCommand(runparams); + if (runparams.use_memindex) { + if (getParam("type") == from_ascii("idx")) + os << "\\printindex" << termcmd; + else { + os << "\\begingroup" << breakln; + os << "\\renewcommand{\\indexname}{" << getParam("name") << "}" << breakln; + os << "\\printindex[" << getParam("type") << "]" << breakln; + os << "\\endgroup" << breakln; + } + } else + os << getCommand(runparams); }