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 "TocBackend.h"
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/textutils.h"
34 using namespace lyx::support;
40 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
41 : InsetCommand(buf, p)
45 InsetRef::InsetRef(InsetRef const & ir)
50 bool InsetRef::isCompatibleCommand(string const & s) {
51 //FIXME This is likely not the best way to handle this.
52 //But this stuff is hardcoded elsewhere already.
63 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
65 static ParamInfo param_info_;
66 if (param_info_.empty()) {
67 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
68 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
69 ParamInfo::HANDLING_ESCAPE);
75 // the ref argument is the label name we are referencing.
76 // we expect ref to be in the form: pfx:suffix.
78 // if it isn't, then we can't produce a formatted reference,
79 // so we return "\ref" and put ref into label.
81 // for refstyle, we return "\pfxcmd", and put suffix into
82 // label and pfx into prefix. this is because refstyle expects
83 // the command: \pfxcmd{suffix}.
85 // for prettyref, we return "\prettyref" and put ref into label
86 // and pfx into prefix. this is because prettyref uses the whole
87 // label, thus: \prettyref{pfx:suffix}.
89 docstring InsetRef::getFormattedCmd(docstring const & ref,
90 docstring & label, docstring & prefix) const
92 static docstring const defcmd = from_ascii("\\ref");
93 static docstring const prtcmd = from_ascii("\\prettyref");
95 label = split(ref, prefix, ':');
97 // we have to have xxx:xxxxx...
99 LYXERR0("Label `" << ref << "' contains no prefix.");
101 prefix = from_ascii("");
105 if (prefix.empty()) {
111 if (!buffer().params().use_refstyle) {
112 // \prettyref uses the whole label
117 // make sure the prefix is legal for a latex command
118 int const len = prefix.size();
119 for (int i = 0; i < len; i++) {
120 char_type const c = prefix[i];
121 if (!isAlphaASCII(c)) {
122 LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
128 return from_ascii("\\") + prefix + from_ascii("ref");
132 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
134 InsetCommandParams const & p = params();
135 ParamInfo const & pi = p.info();
136 ParamInfo::ParamData const & pd = pi["reference"];
137 return p.prepareCommand(rp, getParam("reference"), pd.handling());
141 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
143 string const & cmd = getCmdName();
144 docstring const & data = getEscapedLabel(rp);
146 if (rp.inulemcmd > 0)
149 if (cmd == "eqref" && buffer().params().use_refstyle) {
150 // we advertise this as printing "(n)", so we'll do that, at least
151 // for refstyle, since refstlye's own \eqref prints, by default,
152 // "equation n". if one wants \eqref, one can get it by using a
153 // formatted label in this case.
154 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
156 else if (cmd == "formatted") {
159 docstring const fcmd = getFormattedCmd(data, label, prefix);
160 os << fcmd << '{' << label << '}';
163 // We don't want to output p_["name"], since that is only used
164 // in docbook. So we construct new params, without it, and use that.
165 InsetCommandParams p(REF_CODE, cmd);
166 docstring const ref = getParam("reference");
167 p["reference"] = ref;
168 os << p.getCommand(rp);
171 if (rp.inulemcmd > 0)
176 int InsetRef::plaintext(odocstringstream & os,
177 OutputParams const &, size_t) const
179 docstring const str = getParam("reference");
180 os << '[' << str << ']';
181 return 2 + str.size();
185 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
187 docstring const & name = getParam("name");
189 if (runparams.flavor == OutputParams::XML) {
190 os << "<xref linkend=\""
191 << sgml::cleanID(buffer(), runparams, getParam("reference"))
194 os << "<xref linkend=\""
195 << sgml::cleanID(buffer(), runparams, getParam("reference"))
199 os << "<link linkend=\""
200 << sgml::cleanID(buffer(), runparams, getParam("reference"))
210 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
212 docstring const & ref = getParam("reference");
213 InsetLabel const * il = buffer().insetLabel(ref);
214 string const & cmd = params().getCmdName();
215 docstring display_string;
217 if (il && !il->counterValue().empty()) {
218 // Try to construct a label from the InsetLabel we reference.
219 docstring const & value = il->counterValue();
221 display_string = value;
222 else if (cmd == "vref")
223 // normally, would be "ref on page #", but we have no pages
224 display_string = value;
225 else if (cmd == "pageref" || cmd == "vpageref")
226 // normally would be "on page #", but we have no pages.
227 display_string = translateIfPossible(from_ascii("elsewhere"),
228 op.local_font->language()->lang());
229 else if (cmd == "eqref")
230 display_string = '(' + value + ')';
231 else if (cmd == "formatted")
232 display_string = il->prettyCounter();
233 else if (cmd == "nameref")
234 // FIXME We don't really have the ability to handle these
235 // properly in XHTML output yet (bug #8599).
236 // It might not be that hard to do. We have the InsetLabel,
237 // and we can presumably find its paragraph using the TOC.
238 // But the label might be referencing a section, yet not be
239 // in that section. So this is not trivial.
240 display_string = il->prettyCounter();
242 display_string = ref;
244 // FIXME What we'd really like to do is to be able to output some
245 // appropriate sort of text here. But to do that, we need to associate
246 // some sort of counter with the label, and we don't have that yet.
247 docstring const attr = "href=\"#" + html::cleanAttr(ref) + '"';
248 xs << html::StartTag("a", to_utf8(attr));
249 xs << display_string;
250 xs << html::EndTag("a");
255 void InsetRef::toString(odocstream & os) const
257 odocstringstream ods;
258 plaintext(ods, OutputParams(0));
263 void InsetRef::forOutliner(docstring & os, size_t const, bool const) const
265 // There's no need for details in the TOC, and a long label
266 // will just get in the way.
271 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
273 docstring const & ref = getParam("reference");
274 // register this inset into the buffer reference cache.
275 buffer().addReference(ref, this, it);
278 for (int i = 0; !types[i].latex_name.empty(); ++i) {
279 if (getCmdName() == types[i].latex_name) {
280 label = _(types[i].short_gui_name);
286 if (!buffer().params().isLatex() && !getParam("name").empty()) {
288 label += getParam("name");
291 unsigned int const maxLabelChars = 24;
292 if (label.size() > maxLabelChars) {
294 support::truncateWithEllipsis(label, maxLabelChars);
296 tooltip_ = from_ascii("");
297 screen_label_ = label;
301 void InsetRef::addToToc(DocIterator const & cpit, bool output_active,
304 docstring const & label = getParam("reference");
305 if (buffer().insetLabel(label))
306 // This InsetRef has already been taken care of in InsetLabel::addToToc().
309 // It seems that this reference does not point to any valid label.
310 screen_label_ = _("BROKEN: ") + screen_label_;
311 shared_ptr<Toc> toc = buffer().tocBackend().toc("label");
312 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
316 void InsetRef::validate(LaTeXFeatures & features) const
318 string const cmd = getCmdName();
319 if (cmd == "vref" || cmd == "vpageref")
320 features.require("varioref");
321 else if (cmd == "formatted") {
322 docstring const data = getEscapedLabel(features.runparams());
325 string const fcmd = to_utf8(getFormattedCmd(data, label, prefix));
326 if (buffer().params().use_refstyle) {
327 features.require("refstyle");
329 features.addPreambleSnippet("\\let\\charef=\\chapref");
330 else if (!prefix.empty()) {
331 string lcmd = "\\AtBeginDocument{\\providecommand" +
332 fcmd + "[1]{\\ref{" + to_utf8(prefix) + ":#1}}}";
333 features.addPreambleSnippet(lcmd);
336 features.require("prettyref");
337 // prettyref uses "cha" for chapters, so we provide a kind of
339 if (prefix == "chap")
340 features.addPreambleSnippet("\\let\\pr@chap=\\pr@cha");
342 } else if (cmd == "eqref" && !buffer().params().use_refstyle)
343 // with refstyle, we simply output "(\ref{label})"
344 features.require("amsmath");
345 else if (cmd == "nameref")
346 features.require("nameref");
350 InsetRef::type_info const InsetRef::types[] = {
351 { "ref", N_("Standard"), N_("Ref: ")},
352 { "eqref", N_("Equation"), N_("EqRef: ")},
353 { "pageref", N_("Page Number"), N_("Page: ")},
354 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
355 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
356 { "formatted", N_("Formatted"), N_("Format: ")},
357 { "nameref", N_("Reference to Name"), N_("NameRef:")},
362 int InsetRef::getType(string const & name)
364 for (int i = 0; !types[i].latex_name.empty(); ++i)
365 if (name == types[i].latex_name)
371 string const & InsetRef::getName(int type)
373 return types[type].latex_name;
377 docstring InsetRef::getTOCString() const
379 return tooltip_.empty() ? screen_label_ : tooltip_;