3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
15 #include "BufferParams.h"
17 #include "DispatchResult.h"
18 #include "InsetLabel.h"
20 #include "LaTeXFeatures.h"
22 #include "OutputParams.h"
23 #include "output_xhtml.h"
24 #include "ParIterator.h"
26 #include "texstream.h"
27 #include "TocBackend.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33 #include "support/textutils.h"
35 using namespace lyx::support;
41 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
42 : InsetCommand(buf, p)
46 InsetRef::InsetRef(InsetRef const & ir)
51 bool InsetRef::isCompatibleCommand(string const & s) {
52 //FIXME This is likely not the best way to handle this.
53 //But this stuff is hardcoded elsewhere already.
64 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
66 static ParamInfo param_info_;
67 if (param_info_.empty()) {
68 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
69 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
70 ParamInfo::HANDLING_ESCAPE);
76 // the ref argument is the label name we are referencing.
77 // we expect ref to be in the form: pfx:suffix.
79 // if it isn't, then we can't produce a formatted reference,
80 // so we return "\ref" and put ref into label.
82 // for refstyle, we return "\pfxcmd", and put suffix into
83 // label and pfx into prefix. this is because refstyle expects
84 // the command: \pfxcmd{suffix}.
86 // for prettyref, we return "\prettyref" and put ref into label
87 // and pfx into prefix. this is because prettyref uses the whole
88 // label, thus: \prettyref{pfx:suffix}.
90 docstring InsetRef::getFormattedCmd(docstring const & ref,
91 docstring & label, docstring & prefix) const
93 static docstring const defcmd = from_ascii("\\ref");
94 static docstring const prtcmd = from_ascii("\\prettyref");
96 label = split(ref, prefix, ':');
98 // we have to have xxx:xxxxx...
100 LYXERR0("Label `" << ref << "' contains no prefix.");
102 prefix = from_ascii("");
106 if (prefix.empty()) {
112 if (!buffer().params().use_refstyle) {
113 // \prettyref uses the whole label
118 // make sure the prefix is legal for a latex command
119 int const len = prefix.size();
120 for (int i = 0; i < len; i++) {
121 char_type const c = prefix[i];
122 if (!isAlphaASCII(c)) {
123 LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
129 return from_ascii("\\") + prefix + from_ascii("ref");
133 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
135 InsetCommandParams const & p = params();
136 ParamInfo const & pi = p.info();
137 ParamInfo::ParamData const & pd = pi["reference"];
138 return p.prepareCommand(rp, getParam("reference"), pd.handling());
142 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
144 string const & cmd = getCmdName();
145 docstring const & data = getEscapedLabel(rp);
147 if (rp.inulemcmd > 0)
150 if (cmd == "eqref" && buffer().params().use_refstyle) {
151 // we advertise this as printing "(n)", so we'll do that, at least
152 // for refstyle, since refstlye's own \eqref prints, by default,
153 // "equation n". if one wants \eqref, one can get it by using a
154 // formatted label in this case.
155 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
157 else if (cmd == "formatted") {
160 docstring const fcmd = getFormattedCmd(data, label, prefix);
161 os << fcmd << '{' << label << '}';
164 // We don't want to output p_["name"], since that is only used
165 // in docbook. So we construct new params, without it, and use that.
166 InsetCommandParams p(REF_CODE, cmd);
167 docstring const ref = getParam("reference");
168 p["reference"] = ref;
169 os << p.getCommand(rp);
172 if (rp.inulemcmd > 0)
177 int InsetRef::plaintext(odocstringstream & os,
178 OutputParams const &, size_t) const
180 docstring const str = getParam("reference");
181 os << '[' << str << ']';
182 return 2 + str.size();
186 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
188 docstring const & name = getParam("name");
190 if (runparams.flavor == OutputParams::XML) {
191 os << "<xref linkend=\""
192 << sgml::cleanID(buffer(), runparams, getParam("reference"))
195 os << "<xref linkend=\""
196 << sgml::cleanID(buffer(), runparams, getParam("reference"))
200 os << "<link linkend=\""
201 << sgml::cleanID(buffer(), runparams, getParam("reference"))
211 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
213 docstring const & ref = getParam("reference");
214 InsetLabel const * il = buffer().insetLabel(ref);
215 string const & cmd = params().getCmdName();
216 docstring display_string;
218 if (il && !il->counterValue().empty()) {
219 // Try to construct a label from the InsetLabel we reference.
220 docstring const & value = il->counterValue();
222 display_string = value;
223 else if (cmd == "vref")
224 // normally, would be "ref on page #", but we have no pages
225 display_string = value;
226 else if (cmd == "pageref" || cmd == "vpageref")
227 // normally would be "on page #", but we have no pages.
228 display_string = translateIfPossible(from_ascii("elsewhere"),
229 op.local_font->language()->lang());
230 else if (cmd == "eqref")
231 display_string = '(' + value + ')';
232 else if (cmd == "formatted")
233 display_string = il->prettyCounter();
234 else if (cmd == "nameref")
235 // FIXME We don't really have the ability to handle these
236 // properly in XHTML output yet (bug #8599).
237 // It might not be that hard to do. We have the InsetLabel,
238 // and we can presumably find its paragraph using the TOC.
239 // But the label might be referencing a section, yet not be
240 // in that section. So this is not trivial.
241 display_string = il->prettyCounter();
243 display_string = ref;
245 // FIXME What we'd really like to do is to be able to output some
246 // appropriate sort of text here. But to do that, we need to associate
247 // some sort of counter with the label, and we don't have that yet.
248 docstring const attr = "href=\"#" + html::cleanAttr(ref) + '"';
249 xs << html::StartTag("a", to_utf8(attr));
250 xs << display_string;
251 xs << html::EndTag("a");
256 void InsetRef::toString(odocstream & os) const
258 odocstringstream ods;
259 plaintext(ods, OutputParams(0));
264 void InsetRef::forOutliner(docstring & os, size_t const, bool const) const
266 // There's no need for details in the TOC, and a long label
267 // will just get in the way.
272 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
274 docstring const & ref = getParam("reference");
275 // register this inset into the buffer reference cache.
276 buffer().addReference(ref, this, it);
279 for (int i = 0; !types[i].latex_name.empty(); ++i) {
280 if (getCmdName() == types[i].latex_name) {
281 label = _(types[i].short_gui_name);
287 if (!buffer().params().isLatex() && !getParam("name").empty()) {
289 label += getParam("name");
292 unsigned int const maxLabelChars = 24;
293 if (label.size() > maxLabelChars) {
295 support::truncateWithEllipsis(label, maxLabelChars);
297 tooltip_ = from_ascii("");
298 screen_label_ = label;
302 void InsetRef::addToToc(DocIterator const & cpit, bool output_active,
305 docstring const & label = getParam("reference");
306 if (buffer().insetLabel(label))
307 // This InsetRef has already been taken care of in InsetLabel::addToToc().
310 // It seems that this reference does not point to any valid label.
311 screen_label_ = _("BROKEN: ") + screen_label_;
312 shared_ptr<Toc> toc = buffer().tocBackend().toc("label");
313 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
317 void InsetRef::validate(LaTeXFeatures & features) const
319 string const cmd = getCmdName();
320 if (cmd == "vref" || cmd == "vpageref")
321 features.require("varioref");
322 else if (cmd == "formatted") {
323 docstring const data = getEscapedLabel(features.runparams());
326 docstring const fcmd = getFormattedCmd(data, label, prefix);
327 if (buffer().params().use_refstyle) {
328 features.require("refstyle");
330 features.addPreambleSnippet(from_ascii("\\let\\charef=\\chapref"));
331 else if (!prefix.empty()) {
332 docstring lcmd = "\\AtBeginDocument{\\providecommand" +
333 fcmd + "[1]{\\ref{" + prefix + ":#1}}}";
334 features.addPreambleSnippet(lcmd);
337 features.require("prettyref");
338 // prettyref uses "cha" for chapters, so we provide a kind of
340 if (prefix == "chap")
341 features.addPreambleSnippet(from_ascii("\\let\\pr@chap=\\pr@cha"));
343 } else if (cmd == "eqref" && !buffer().params().use_refstyle)
344 // with refstyle, we simply output "(\ref{label})"
345 features.require("amsmath");
346 else if (cmd == "nameref")
347 features.require("nameref");
351 InsetRef::type_info const InsetRef::types[] = {
352 { "ref", N_("Standard"), N_("Ref: ")},
353 { "eqref", N_("Equation"), N_("EqRef: ")},
354 { "pageref", N_("Page Number"), N_("Page: ")},
355 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
356 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
357 { "formatted", N_("Formatted"), N_("Format: ")},
358 { "nameref", N_("Reference to Name"), N_("NameRef: ")},
363 int InsetRef::getType(string const & name)
365 for (int i = 0; !types[i].latex_name.empty(); ++i)
366 if (name == types[i].latex_name)
372 string const & InsetRef::getName(int type)
374 return types[type].latex_name;
378 docstring InsetRef::getTOCString() const
380 return tooltip_.empty() ? screen_label_ : tooltip_;