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 "FuncStatus.h"
19 #include "InsetLabel.h"
21 #include "LaTeXFeatures.h"
23 #include "OutputParams.h"
24 #include "output_xhtml.h"
25 #include "ParIterator.h"
27 #include "texstream.h"
28 #include "TocBackend.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34 #include "support/textutils.h"
36 using namespace lyx::support;
42 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
43 : InsetCommand(buf, p), broken_(false)
47 InsetRef::InsetRef(InsetRef const & ir)
48 : InsetCommand(ir), broken_(false)
52 bool InsetRef::isCompatibleCommand(string const & s) {
53 //FIXME This is likely not the best way to handle this.
54 //But this stuff is hardcoded elsewhere already.
60 || s == "prettyref" // for InsetMathRef FIXME
67 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
69 static ParamInfo param_info_;
70 if (param_info_.empty()) {
71 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
72 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
73 ParamInfo::HANDLING_ESCAPE);
74 param_info_.add("plural", ParamInfo::LYX_INTERNAL);
75 param_info_.add("caps", ParamInfo::LYX_INTERNAL);
76 param_info_.add("noprefix", ParamInfo::LYX_INTERNAL);
82 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
84 string const inset = cmd.getArg(0);
85 string const arg = cmd.getArg(1);
87 if (cmd.action() == LFUN_INSET_MODIFY && inset == "ref") {
88 if (arg == "toggle-plural")
90 else if (arg == "toggle-caps")
92 else if (arg == "toggle-noprefix")
95 // otherwise not for us
97 return InsetCommand::doDispatch(cur, cmd);
99 bool const isSet = (getParam(pstring) == "true");
100 setParam(pstring, from_ascii(isSet ? "false" : "true"));
104 bool InsetRef::getStatus(Cursor & cur, FuncRequest const & cmd,
105 FuncStatus & status) const
107 if (cmd.action() != LFUN_INSET_MODIFY)
108 return InsetCommand::getStatus(cur, cmd, status);
109 if (cmd.getArg(0) != "ref")
110 return InsetCommand::getStatus(cur, cmd, status);
112 string const arg = cmd.getArg(1);
114 if (arg == "toggle-plural")
116 else if (arg == "toggle-caps")
118 if (!pstring.empty()) {
119 status.setEnabled(buffer().params().use_refstyle &&
120 params().getCmdName() == "formatted");
121 bool const isSet = (getParam(pstring) == "true");
122 status.setOnOff(isSet);
125 if (arg == "toggle-noprefix") {
126 status.setEnabled(params().getCmdName() == "labelonly");
127 bool const isSet = (getParam("noprefix") == "true");
128 status.setOnOff(isSet);
131 // otherwise not for us
132 return InsetCommand::getStatus(cur, cmd, status);
138 void capitalize(docstring & s) {
139 char_type t = uppercase(s[0]);
146 // the ref argument is the label name we are referencing.
147 // we expect ref to be in the form: pfx:suffix.
149 // if it isn't, then we can't produce a formatted reference,
150 // so we return "\ref" and put ref into label.
152 // for refstyle, we return "\pfxcmd", and put suffix into
153 // label and pfx into prefix. this is because refstyle expects
154 // the command: \pfxcmd{suffix}.
156 // for prettyref, we return "\prettyref" and put ref into label
157 // and pfx into prefix. this is because prettyref uses the whole
158 // label, thus: \prettyref{pfx:suffix}.
160 docstring InsetRef::getFormattedCmd(docstring const & ref,
161 docstring & label, docstring & prefix, docstring const & caps) const
163 static docstring const defcmd = from_ascii("\\ref");
164 static docstring const prtcmd = from_ascii("\\prettyref");
166 label = split(ref, prefix, ':');
168 // we have to have xxx:xxxxx...
170 LYXERR0("Label `" << ref << "' contains no `:' separator.");
172 prefix = from_ascii("");
176 if (prefix.empty()) {
182 if (!buffer().params().use_refstyle) {
183 // \prettyref uses the whole label
188 // make sure the prefix is legal for a latex command
189 int const len = prefix.size();
190 for (int i = 0; i < len; i++) {
191 char_type const c = prefix[i];
192 if (!isAlphaASCII(c)) {
193 LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
199 if (caps == "true") {
202 return from_ascii("\\") + prefix + from_ascii("ref");
206 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
208 InsetCommandParams const & p = params();
209 ParamInfo const & pi = p.info();
210 ParamInfo::ParamData const & pd = pi["reference"];
211 return p.prepareCommand(rp, getParam("reference"), pd.handling());
215 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
217 string const & cmd = getCmdName();
218 docstring const & data = getEscapedLabel(rp);
220 if (rp.inulemcmd > 0)
223 if (cmd == "eqref" && buffer().params().use_refstyle) {
224 // we advertise this as printing "(n)", so we'll do that, at least
225 // for refstyle, since refstlye's own \eqref prints, by default,
226 // "equation n". if one wants \eqref, one can get it by using a
227 // formatted label in this case.
228 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
230 else if (cmd == "formatted") {
233 docstring const fcmd =
234 getFormattedCmd(data, label, prefix, getParam("caps"));
236 if (buffer().params().use_refstyle && getParam("plural") == "true")
238 os << '{' << label << '}';
240 else if (cmd == "labelonly") {
241 docstring const & ref = getParam("reference");
242 if (getParam("noprefix") != "true")
246 docstring suffix = split(ref, prefix, ':');
247 if (suffix.empty()) {
248 LYXERR0("Label `" << ref << "' contains no `:' separator.");
256 // We don't want to output p_["name"], since that is only used
257 // in docbook. So we construct new params, without it, and use that.
258 InsetCommandParams p(REF_CODE, cmd);
259 docstring const ref = getParam("reference");
260 p["reference"] = ref;
261 os << p.getCommand(rp);
264 if (rp.inulemcmd > 0)
269 int InsetRef::plaintext(odocstringstream & os,
270 OutputParams const &, size_t) const
272 docstring const str = getParam("reference");
273 os << '[' << str << ']';
274 return 2 + str.size();
278 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
280 docstring const & name = getParam("name");
282 if (runparams.flavor == OutputParams::XML) {
283 os << "<xref linkend=\""
284 << sgml::cleanID(buffer(), runparams, getParam("reference"))
287 os << "<xref linkend=\""
288 << sgml::cleanID(buffer(), runparams, getParam("reference"))
292 os << "<link linkend=\""
293 << sgml::cleanID(buffer(), runparams, getParam("reference"))
303 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
305 docstring const & ref = getParam("reference");
306 InsetLabel const * il = buffer().insetLabel(ref, true);
307 string const & cmd = params().getCmdName();
308 docstring display_string;
310 if (il && !il->counterValue().empty()) {
311 // Try to construct a label from the InsetLabel we reference.
312 docstring const & value = il->counterValue();
314 display_string = value;
315 else if (cmd == "vref")
316 // normally, would be "ref on page #", but we have no pages
317 display_string = value;
318 else if (cmd == "pageref" || cmd == "vpageref")
319 // normally would be "on page #", but we have no pages.
320 display_string = translateIfPossible(from_ascii("elsewhere"),
321 op.local_font->language()->lang());
322 else if (cmd == "eqref")
323 display_string = '(' + value + ')';
324 else if (cmd == "formatted") {
325 display_string = il->prettyCounter();
326 if (buffer().params().use_refstyle && getParam("caps") == "true")
327 capitalize(display_string);
328 // it is hard to see what to do about plurals...
330 else if (cmd == "nameref")
331 // FIXME We don't really have the ability to handle these
332 // properly in XHTML output yet (bug #8599).
333 // It might not be that hard to do. We have the InsetLabel,
334 // and we can presumably find its paragraph using the TOC.
335 // But the label might be referencing a section, yet not be
336 // in that section. So this is not trivial.
337 display_string = il->prettyCounter();
339 display_string = ref;
341 // FIXME What we'd really like to do is to be able to output some
342 // appropriate sort of text here. But to do that, we need to associate
343 // some sort of counter with the label, and we don't have that yet.
344 docstring const attr = "href=\"#" + html::cleanAttr(ref) + '"';
345 xs << html::StartTag("a", to_utf8(attr));
346 xs << display_string;
347 xs << html::EndTag("a");
352 void InsetRef::toString(odocstream & os) const
354 odocstringstream ods;
355 plaintext(ods, OutputParams(0));
360 void InsetRef::forOutliner(docstring & os, size_t const, bool const) const
362 // There's no need for details in the TOC, and a long label
363 // will just get in the way.
368 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
370 docstring const & ref = getParam("reference");
371 // register this inset into the buffer reference cache.
372 buffer().addReference(ref, this, it);
375 string const & cmd = getCmdName();
376 for (int i = 0; !types[i].latex_name.empty(); ++i) {
377 if (cmd == types[i].latex_name) {
378 label = _(types[i].short_gui_name);
383 if (cmd != "labelonly")
386 if (getParam("noprefix") != "true")
390 docstring suffix = split(ref, prefix, ':');
391 if (suffix.empty()) {
399 if (!buffer().params().isLatex() && !getParam("name").empty()) {
401 label += getParam("name");
404 unsigned int const maxLabelChars = 24;
405 if (label.size() > maxLabelChars) {
407 support::truncateWithEllipsis(label, maxLabelChars);
409 tooltip_ = from_ascii("");
411 screen_label_ = label;
416 docstring InsetRef::screenLabel() const
418 return (broken_ ? _("BROKEN: ") : docstring()) + screen_label_;
422 void InsetRef::addToToc(DocIterator const & cpit, bool output_active,
423 UpdateType, TocBackend & backend) const
425 docstring const & label = getParam("reference");
426 if (buffer().insetLabel(label)) {
427 broken_ = !buffer().activeLabel(label);
428 // This InsetRef has already been taken care of in InsetLabel::addToToc().
432 // It seems that this reference does not point to any valid label.
435 shared_ptr<Toc> toc = backend.toc("label");
436 toc->push_back(TocItem(cpit, 0, screenLabel(), output_active));
440 void InsetRef::validate(LaTeXFeatures & features) const
442 string const cmd = getCmdName();
443 if (cmd == "vref" || cmd == "vpageref")
444 features.require("varioref");
445 else if (cmd == "formatted") {
446 docstring const data = getEscapedLabel(features.runparams());
449 docstring const fcmd =
450 getFormattedCmd(data, label, prefix, getParam("caps"));
451 if (buffer().params().use_refstyle) {
452 features.require("refstyle");
454 features.addPreambleSnippet(from_ascii("\\let\\charef=\\chapref"));
455 else if (!prefix.empty()) {
456 docstring lcmd = "\\AtBeginDocument{\\providecommand" +
457 fcmd + "[1]{\\ref{" + prefix + ":#1}}}";
458 features.addPreambleSnippet(lcmd);
461 features.require("prettyref");
462 // prettyref uses "cha" for chapters, so we provide a kind of
464 if (prefix == "chap")
465 features.addPreambleSnippet(from_ascii("\\let\\pr@chap=\\pr@cha"));
467 } else if (cmd == "eqref" && !buffer().params().use_refstyle)
468 // with refstyle, we simply output "(\ref{label})"
469 features.require("amsmath");
470 else if (cmd == "nameref")
471 features.require("nameref");
475 InsetRef::type_info const InsetRef::types[] = {
476 { "ref", N_("Standard"), N_("Ref: ")},
477 { "eqref", N_("Equation"), N_("EqRef: ")},
478 { "pageref", N_("Page Number"), N_("Page: ")},
479 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
480 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
481 { "nameref", N_("Reference to Name"), N_("NameRef: ")},
482 { "formatted", N_("Formatted"), N_("Format: ")},
483 { "labelonly", N_("Label Only"), N_("Label: ")},
488 docstring InsetRef::getTOCString() const
490 return tooltip_.empty() ? screenLabel() : tooltip_;