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), broken_(false)
46 InsetRef::InsetRef(InsetRef const & ir)
47 : InsetCommand(ir), broken_(false)
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.
59 || s == "prettyref" // for InsetMathRef FIXME
66 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
68 static ParamInfo param_info_;
69 if (param_info_.empty()) {
70 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
71 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
72 ParamInfo::HANDLING_ESCAPE);
73 param_info_.add("plural", ParamInfo::LYX_INTERNAL);
74 param_info_.add("caps", ParamInfo::LYX_INTERNAL);
75 param_info_.add("noprefix", ParamInfo::LYX_INTERNAL);
83 void capitalize(docstring & s) {
84 char_type t = uppercase(s[0]);
91 // the ref argument is the label name we are referencing.
92 // we expect ref to be in the form: pfx:suffix.
94 // if it isn't, then we can't produce a formatted reference,
95 // so we return "\ref" and put ref into label.
97 // for refstyle, we return "\pfxcmd", and put suffix into
98 // label and pfx into prefix. this is because refstyle expects
99 // the command: \pfxcmd{suffix}.
101 // for prettyref, we return "\prettyref" and put ref into label
102 // and pfx into prefix. this is because prettyref uses the whole
103 // label, thus: \prettyref{pfx:suffix}.
105 docstring InsetRef::getFormattedCmd(docstring const & ref,
106 docstring & label, docstring & prefix, docstring const & caps) const
108 static docstring const defcmd = from_ascii("\\ref");
109 static docstring const prtcmd = from_ascii("\\prettyref");
111 label = split(ref, prefix, ':');
113 // we have to have xxx:xxxxx...
115 LYXERR0("Label `" << ref << "' contains no `:' separator.");
117 prefix = from_ascii("");
121 if (prefix.empty()) {
127 if (!buffer().params().use_refstyle) {
128 // \prettyref uses the whole label
133 // make sure the prefix is legal for a latex command
134 int const len = prefix.size();
135 for (int i = 0; i < len; i++) {
136 char_type const c = prefix[i];
137 if (!isAlphaASCII(c)) {
138 LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
144 if (caps == "true") {
147 return from_ascii("\\") + prefix + from_ascii("ref");
151 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
153 InsetCommandParams const & p = params();
154 ParamInfo const & pi = p.info();
155 ParamInfo::ParamData const & pd = pi["reference"];
156 return p.prepareCommand(rp, getParam("reference"), pd.handling());
160 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
162 string const & cmd = getCmdName();
163 docstring const & data = getEscapedLabel(rp);
165 if (rp.inulemcmd > 0)
168 if (cmd == "eqref" && buffer().params().use_refstyle) {
169 // we advertise this as printing "(n)", so we'll do that, at least
170 // for refstyle, since refstlye's own \eqref prints, by default,
171 // "equation n". if one wants \eqref, one can get it by using a
172 // formatted label in this case.
173 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
175 else if (cmd == "formatted") {
178 docstring const fcmd =
179 getFormattedCmd(data, label, prefix, getParam("caps"));
181 if (buffer().params().use_refstyle && getParam("plural") == "true")
183 os << '{' << label << '}';
185 else if (cmd == "labelonly") {
186 docstring const & ref = getParam("reference");
187 if (getParam("noprefix") != "true")
191 docstring suffix = split(ref, prefix, ':');
192 if (suffix.empty()) {
193 LYXERR0("Label `" << ref << "' contains no `:' separator.");
201 // We don't want to output p_["name"], since that is only used
202 // in docbook. So we construct new params, without it, and use that.
203 InsetCommandParams p(REF_CODE, cmd);
204 docstring const ref = getParam("reference");
205 p["reference"] = ref;
206 os << p.getCommand(rp);
209 if (rp.inulemcmd > 0)
214 int InsetRef::plaintext(odocstringstream & os,
215 OutputParams const &, size_t) const
217 docstring const str = getParam("reference");
218 os << '[' << str << ']';
219 return 2 + str.size();
223 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
225 docstring const & name = getParam("name");
227 if (runparams.flavor == OutputParams::XML) {
228 os << "<xref linkend=\""
229 << sgml::cleanID(buffer(), runparams, getParam("reference"))
232 os << "<xref linkend=\""
233 << sgml::cleanID(buffer(), runparams, getParam("reference"))
237 os << "<link linkend=\""
238 << sgml::cleanID(buffer(), runparams, getParam("reference"))
248 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
250 docstring const & ref = getParam("reference");
251 InsetLabel const * il = buffer().insetLabel(ref);
252 string const & cmd = params().getCmdName();
253 docstring display_string;
255 if (il && !il->counterValue().empty()) {
256 // Try to construct a label from the InsetLabel we reference.
257 docstring const & value = il->counterValue();
259 display_string = value;
260 else if (cmd == "vref")
261 // normally, would be "ref on page #", but we have no pages
262 display_string = value;
263 else if (cmd == "pageref" || cmd == "vpageref")
264 // normally would be "on page #", but we have no pages.
265 display_string = translateIfPossible(from_ascii("elsewhere"),
266 op.local_font->language()->lang());
267 else if (cmd == "eqref")
268 display_string = '(' + value + ')';
269 else if (cmd == "formatted") {
270 display_string = il->prettyCounter();
271 if (buffer().params().use_refstyle && getParam("caps") == "true")
272 capitalize(display_string);
273 // it is hard to see what to do about plurals...
275 else if (cmd == "nameref")
276 // FIXME We don't really have the ability to handle these
277 // properly in XHTML output yet (bug #8599).
278 // It might not be that hard to do. We have the InsetLabel,
279 // and we can presumably find its paragraph using the TOC.
280 // But the label might be referencing a section, yet not be
281 // in that section. So this is not trivial.
282 display_string = il->prettyCounter();
284 display_string = ref;
286 // FIXME What we'd really like to do is to be able to output some
287 // appropriate sort of text here. But to do that, we need to associate
288 // some sort of counter with the label, and we don't have that yet.
289 docstring const attr = "href=\"#" + html::cleanAttr(ref) + '"';
290 xs << html::StartTag("a", to_utf8(attr));
291 xs << display_string;
292 xs << html::EndTag("a");
297 void InsetRef::toString(odocstream & os) const
299 odocstringstream ods;
300 plaintext(ods, OutputParams(0));
305 void InsetRef::forOutliner(docstring & os, size_t const, bool const) const
307 // There's no need for details in the TOC, and a long label
308 // will just get in the way.
313 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
315 docstring const & ref = getParam("reference");
316 // register this inset into the buffer reference cache.
317 buffer().addReference(ref, this, it);
320 string const & cmd = getCmdName();
321 for (int i = 0; !types[i].latex_name.empty(); ++i) {
322 if (cmd == types[i].latex_name) {
323 label = _(types[i].short_gui_name);
328 if (cmd != "labelonly")
331 if (getParam("noprefix") != "true")
335 docstring suffix = split(ref, prefix, ':');
336 if (suffix.empty()) {
344 if (!buffer().params().isLatex() && !getParam("name").empty()) {
346 label += getParam("name");
349 unsigned int const maxLabelChars = 24;
350 if (label.size() > maxLabelChars) {
352 support::truncateWithEllipsis(label, maxLabelChars);
354 tooltip_ = from_ascii("");
356 screen_label_ = label;
361 docstring InsetRef::screenLabel() const
363 return (broken_ ? _("BROKEN: ") : docstring()) + screen_label_;
367 void InsetRef::addToToc(DocIterator const & cpit, bool output_active,
368 UpdateType, TocBackend & backend) const
370 docstring const & label = getParam("reference");
371 if (buffer().insetLabel(label))
372 // This InsetRef has already been taken care of in InsetLabel::addToToc().
375 // It seems that this reference does not point to any valid label.
378 shared_ptr<Toc> toc = backend.toc("label");
379 toc->push_back(TocItem(cpit, 0, screenLabel(), output_active));
383 void InsetRef::validate(LaTeXFeatures & features) const
385 string const cmd = getCmdName();
386 if (cmd == "vref" || cmd == "vpageref")
387 features.require("varioref");
388 else if (cmd == "formatted") {
389 docstring const data = getEscapedLabel(features.runparams());
392 docstring const fcmd =
393 getFormattedCmd(data, label, prefix, getParam("caps"));
394 if (buffer().params().use_refstyle) {
395 features.require("refstyle");
397 features.addPreambleSnippet(from_ascii("\\let\\charef=\\chapref"));
398 else if (!prefix.empty()) {
399 docstring lcmd = "\\AtBeginDocument{\\providecommand" +
400 fcmd + "[1]{\\ref{" + prefix + ":#1}}}";
401 features.addPreambleSnippet(lcmd);
404 features.require("prettyref");
405 // prettyref uses "cha" for chapters, so we provide a kind of
407 if (prefix == "chap")
408 features.addPreambleSnippet(from_ascii("\\let\\pr@chap=\\pr@cha"));
410 } else if (cmd == "eqref" && !buffer().params().use_refstyle)
411 // with refstyle, we simply output "(\ref{label})"
412 features.require("amsmath");
413 else if (cmd == "nameref")
414 features.require("nameref");
418 InsetRef::type_info const InsetRef::types[] = {
419 { "ref", N_("Standard"), N_("Ref: ")},
420 { "eqref", N_("Equation"), N_("EqRef: ")},
421 { "pageref", N_("Page Number"), N_("Page: ")},
422 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
423 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
424 { "formatted", N_("Formatted"), N_("Format: ")},
425 { "nameref", N_("Reference to Name"), N_("NameRef: ")},
426 { "labelonly", N_("Label Only"), N_("Label: ")},
431 int InsetRef::getType(string const & name)
433 for (int i = 0; !types[i].latex_name.empty(); ++i)
434 if (name == types[i].latex_name)
440 string const & InsetRef::getName(int type)
442 return types[type].latex_name;
446 docstring InsetRef::getTOCString() const
448 return tooltip_.empty() ? screenLabel() : tooltip_;