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.
65 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
67 static ParamInfo param_info_;
68 if (param_info_.empty()) {
69 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
70 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
71 ParamInfo::HANDLING_ESCAPE);
72 param_info_.add("plural", ParamInfo::LYX_INTERNAL);
73 param_info_.add("caps", ParamInfo::LYX_INTERNAL);
74 param_info_.add("noprefix", ParamInfo::LYX_INTERNAL);
82 void capitalize(docstring & s) {
83 char_type t = uppercase(s[0]);
90 // the ref argument is the label name we are referencing.
91 // we expect ref to be in the form: pfx:suffix.
93 // if it isn't, then we can't produce a formatted reference,
94 // so we return "\ref" and put ref into label.
96 // for refstyle, we return "\pfxcmd", and put suffix into
97 // label and pfx into prefix. this is because refstyle expects
98 // the command: \pfxcmd{suffix}.
100 // for prettyref, we return "\prettyref" and put ref into label
101 // and pfx into prefix. this is because prettyref uses the whole
102 // label, thus: \prettyref{pfx:suffix}.
104 docstring InsetRef::getFormattedCmd(docstring const & ref,
105 docstring & label, docstring & prefix, docstring const & caps) const
107 static docstring const defcmd = from_ascii("\\ref");
108 static docstring const prtcmd = from_ascii("\\prettyref");
110 label = split(ref, prefix, ':');
112 // we have to have xxx:xxxxx...
114 LYXERR0("Label `" << ref << "' contains no `:' separator.");
116 prefix = from_ascii("");
120 if (prefix.empty()) {
126 if (!buffer().params().use_refstyle) {
127 // \prettyref uses the whole label
132 // make sure the prefix is legal for a latex command
133 int const len = prefix.size();
134 for (int i = 0; i < len; i++) {
135 char_type const c = prefix[i];
136 if (!isAlphaASCII(c)) {
137 LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
143 if (caps == "true") {
146 return from_ascii("\\") + prefix + from_ascii("ref");
150 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
152 InsetCommandParams const & p = params();
153 ParamInfo const & pi = p.info();
154 ParamInfo::ParamData const & pd = pi["reference"];
155 return p.prepareCommand(rp, getParam("reference"), pd.handling());
159 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
161 string const & cmd = getCmdName();
162 docstring const & data = getEscapedLabel(rp);
164 if (rp.inulemcmd > 0)
167 if (cmd == "eqref" && buffer().params().use_refstyle) {
168 // we advertise this as printing "(n)", so we'll do that, at least
169 // for refstyle, since refstlye's own \eqref prints, by default,
170 // "equation n". if one wants \eqref, one can get it by using a
171 // formatted label in this case.
172 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
174 else if (cmd == "formatted") {
177 docstring const fcmd =
178 getFormattedCmd(data, label, prefix, getParam("caps"));
180 if (buffer().params().use_refstyle && getParam("plural") == "true")
182 os << '{' << label << '}';
184 else if (cmd == "labelonly") {
185 docstring const & ref = getParam("reference");
186 if (getParam("noprefix") != "true")
190 docstring suffix = split(ref, prefix, ':');
191 if (suffix.empty()) {
192 LYXERR0("Label `" << ref << "' contains no `:' separator.");
200 // We don't want to output p_["name"], since that is only used
201 // in docbook. So we construct new params, without it, and use that.
202 InsetCommandParams p(REF_CODE, cmd);
203 docstring const ref = getParam("reference");
204 p["reference"] = ref;
205 os << p.getCommand(rp);
208 if (rp.inulemcmd > 0)
213 int InsetRef::plaintext(odocstringstream & os,
214 OutputParams const &, size_t) const
216 docstring const str = getParam("reference");
217 os << '[' << str << ']';
218 return 2 + str.size();
222 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
224 docstring const & name = getParam("name");
226 if (runparams.flavor == OutputParams::XML) {
227 os << "<xref linkend=\""
228 << sgml::cleanID(buffer(), runparams, getParam("reference"))
231 os << "<xref linkend=\""
232 << sgml::cleanID(buffer(), runparams, getParam("reference"))
236 os << "<link linkend=\""
237 << sgml::cleanID(buffer(), runparams, getParam("reference"))
247 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
249 docstring const & ref = getParam("reference");
250 InsetLabel const * il = buffer().insetLabel(ref);
251 string const & cmd = params().getCmdName();
252 docstring display_string;
254 if (il && !il->counterValue().empty()) {
255 // Try to construct a label from the InsetLabel we reference.
256 docstring const & value = il->counterValue();
258 display_string = value;
259 else if (cmd == "vref")
260 // normally, would be "ref on page #", but we have no pages
261 display_string = value;
262 else if (cmd == "pageref" || cmd == "vpageref")
263 // normally would be "on page #", but we have no pages.
264 display_string = translateIfPossible(from_ascii("elsewhere"),
265 op.local_font->language()->lang());
266 else if (cmd == "eqref")
267 display_string = '(' + value + ')';
268 else if (cmd == "formatted") {
269 display_string = il->prettyCounter();
270 if (buffer().params().use_refstyle && getParam("caps") == "true")
271 capitalize(display_string);
272 // it is hard to see what to do about plurals...
274 else if (cmd == "nameref")
275 // FIXME We don't really have the ability to handle these
276 // properly in XHTML output yet (bug #8599).
277 // It might not be that hard to do. We have the InsetLabel,
278 // and we can presumably find its paragraph using the TOC.
279 // But the label might be referencing a section, yet not be
280 // in that section. So this is not trivial.
281 display_string = il->prettyCounter();
283 display_string = ref;
285 // FIXME What we'd really like to do is to be able to output some
286 // appropriate sort of text here. But to do that, we need to associate
287 // some sort of counter with the label, and we don't have that yet.
288 docstring const attr = "href=\"#" + html::cleanAttr(ref) + '"';
289 xs << html::StartTag("a", to_utf8(attr));
290 xs << display_string;
291 xs << html::EndTag("a");
296 void InsetRef::toString(odocstream & os) const
298 odocstringstream ods;
299 plaintext(ods, OutputParams(0));
304 void InsetRef::forOutliner(docstring & os, size_t const, bool const) const
306 // There's no need for details in the TOC, and a long label
307 // will just get in the way.
312 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
314 docstring const & ref = getParam("reference");
315 // register this inset into the buffer reference cache.
316 buffer().addReference(ref, this, it);
319 string const & cmd = getCmdName();
320 for (int i = 0; !types[i].latex_name.empty(); ++i) {
321 if (cmd == types[i].latex_name) {
322 label = _(types[i].short_gui_name);
327 if (cmd != "labelonly")
330 if (getParam("noprefix") != "true")
334 docstring suffix = split(ref, prefix, ':');
335 if (suffix.empty()) {
343 if (!buffer().params().isLatex() && !getParam("name").empty()) {
345 label += getParam("name");
348 unsigned int const maxLabelChars = 24;
349 if (label.size() > maxLabelChars) {
351 support::truncateWithEllipsis(label, maxLabelChars);
353 tooltip_ = from_ascii("");
354 screen_label_ = label;
358 void InsetRef::addToToc(DocIterator const & cpit, bool output_active,
359 UpdateType, TocBackend & backend) const
361 docstring const & label = getParam("reference");
362 if (buffer().insetLabel(label))
363 // This InsetRef has already been taken care of in InsetLabel::addToToc().
366 // It seems that this reference does not point to any valid label.
367 screen_label_ = _("BROKEN: ") + screen_label_;
368 shared_ptr<Toc> toc = backend.toc("label");
369 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
373 void InsetRef::validate(LaTeXFeatures & features) const
375 string const cmd = getCmdName();
376 if (cmd == "vref" || cmd == "vpageref")
377 features.require("varioref");
378 else if (cmd == "formatted") {
379 docstring const data = getEscapedLabel(features.runparams());
382 docstring const fcmd =
383 getFormattedCmd(data, label, prefix, getParam("caps"));
384 if (buffer().params().use_refstyle) {
385 features.require("refstyle");
387 features.addPreambleSnippet(from_ascii("\\let\\charef=\\chapref"));
388 else if (!prefix.empty()) {
389 docstring lcmd = "\\AtBeginDocument{\\providecommand" +
390 fcmd + "[1]{\\ref{" + prefix + ":#1}}}";
391 features.addPreambleSnippet(lcmd);
394 features.require("prettyref");
395 // prettyref uses "cha" for chapters, so we provide a kind of
397 if (prefix == "chap")
398 features.addPreambleSnippet(from_ascii("\\let\\pr@chap=\\pr@cha"));
400 } else if (cmd == "eqref" && !buffer().params().use_refstyle)
401 // with refstyle, we simply output "(\ref{label})"
402 features.require("amsmath");
403 else if (cmd == "nameref")
404 features.require("nameref");
408 InsetRef::type_info const InsetRef::types[] = {
409 { "ref", N_("Standard"), N_("Ref: ")},
410 { "eqref", N_("Equation"), N_("EqRef: ")},
411 { "pageref", N_("Page Number"), N_("Page: ")},
412 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
413 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
414 { "formatted", N_("Formatted"), N_("Format: ")},
415 { "nameref", N_("Reference to Name"), N_("NameRef: ")},
416 { "labelonly", N_("Label Only"), N_("Label: ")},
421 int InsetRef::getType(string const & name)
423 for (int i = 0; !types[i].latex_name.empty(); ++i)
424 if (name == types[i].latex_name)
430 string const & InsetRef::getName(int type)
432 return types[type].latex_name;
436 docstring InsetRef::getTOCString() const
438 return tooltip_.empty() ? screen_label_ : tooltip_;