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"
19 #include "LaTeXFeatures.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "ParIterator.h"
25 #include "TocBackend.h"
27 #include "support/debug.h"
28 #include "support/docstream.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31 #include "support/textutils.h"
33 using namespace lyx::support;
39 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
40 : InsetCommand(buf, p)
44 InsetRef::InsetRef(InsetRef const & ir)
49 bool InsetRef::isCompatibleCommand(string const & s) {
50 //FIXME This is likely not the best way to handle this.
51 //But this stuff is hardcoded elsewhere already.
62 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
64 static ParamInfo param_info_;
65 if (param_info_.empty()) {
66 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
67 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
68 ParamInfo::HANDLING_ESCAPE);
74 // the ref argument is the label name we are referencing.
75 // we expect ref to be in the form: pfx:suffix.
77 // if it isn't, then we can't produce a formatted reference,
78 // so we return "\ref" and put ref into label.
80 // for refstyle, we return "\pfxcmd", and put suffix into
81 // label and pfx into prefix. this is because refstyle expects
82 // the command: \pfxcmd{suffix}.
84 // for prettyref, we return "\prettyref" and put ref into label
85 // and pfx into prefix. this is because prettyref uses the whole
86 // label, thus: \prettyref{pfx:suffix}.
88 docstring InsetRef::getFormattedCmd(docstring const & ref,
89 docstring & label, docstring & prefix) const
91 static docstring const defcmd = from_ascii("\\ref");
92 static docstring const prtcmd = from_ascii("\\prettyref");
94 label = split(ref, prefix, ':');
96 // we have to have xxx:xxxxx...
98 LYXERR0("Label `" << ref << "' contains no prefix.");
100 prefix = from_ascii("");
104 if (prefix.empty()) {
110 if (!buffer().params().use_refstyle) {
111 // \prettyref uses the whole label
116 // make sure the prefix is legal for a latex command
117 int const len = prefix.size();
118 for (int i = 0; i < len; i++) {
119 char_type const c = prefix[i];
120 if (!isAlphaASCII(c)) {
121 LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
127 return from_ascii("\\") + prefix + from_ascii("ref");
131 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
133 InsetCommandParams const & p = params();
134 ParamInfo const & pi = p.info();
135 ParamInfo::ParamData const & pd = pi["reference"];
136 return p.prepareCommand(rp, getParam("reference"), pd.handling());
140 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
142 string const cmd = getCmdName();
143 docstring const data = getEscapedLabel(rp);
148 if (cmd == "eqref" && buffer().params().use_refstyle) {
149 // we advertise this as printing "(n)", so we'll do that, at least
150 // for refstyle, since refstlye's own \eqref prints, by default,
151 // "equation n". if one wants \eqref, one can get it by using a
152 // formatted label in this case.
153 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
155 else if (cmd == "formatted") {
158 docstring const fcmd = getFormattedCmd(data, label, prefix);
159 os << fcmd << '{' << label << '}';
162 // We don't want to output p_["name"], since that is only used
163 // in docbook. So we construct new params, without it, and use that.
164 InsetCommandParams p(REF_CODE, cmd);
165 docstring const ref = getParam("reference");
166 p["reference"] = ref;
167 os << p.getCommand(rp);
175 int InsetRef::plaintext(odocstringstream & os,
176 OutputParams const &, size_t) const
178 docstring const str = getParam("reference");
179 os << '[' << str << ']';
180 return 2 + str.size();
184 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
186 docstring const & name = getParam("name");
188 if (runparams.flavor == OutputParams::XML) {
189 os << "<xref linkend=\""
190 << sgml::cleanID(buffer(), runparams, getParam("reference"))
193 os << "<xref linkend=\""
194 << sgml::cleanID(buffer(), runparams, getParam("reference"))
198 os << "<link linkend=\""
199 << sgml::cleanID(buffer(), runparams, getParam("reference"))
209 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
211 docstring const & ref = getParam("reference");
212 InsetLabel const * il = buffer().insetLabel(ref);
213 string const & cmd = params().getCmdName();
214 docstring display_string;
216 if (il && !il->counterValue().empty()) {
217 // Try to construct a label from the InsetLabel we reference.
218 docstring const & value = il->counterValue();
220 display_string = value;
221 else if (cmd == "vref")
222 // normally, would be "ref on page #", but we have no pages
223 display_string = value;
224 else if (cmd == "pageref" || cmd == "vpageref")
225 // normally would be "on page #", but we have no pages.
226 // FIXME this is wrong, as it should be the current language,
227 // but it is better than _(), which is what we had before.
228 display_string = buffer().B_("elsewhere");
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 // We could then output the contents of the paragraph using
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 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
248 xs << html::StartTag("a", 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::forToc(docstring & os, size_t) 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 screen_label_ = label;
292 bool shortened = false;
293 unsigned int const maxLabelChars = 24;
294 if (screen_label_.size() > maxLabelChars) {
295 screen_label_.erase(maxLabelChars - 3);
296 screen_label_ += "...";
302 tooltip_ = from_ascii("");
306 void InsetRef::addToToc(DocIterator const & cpit, bool output_active) const
308 docstring const & label = getParam("reference");
309 if (buffer().insetLabel(label))
310 // This InsetRef has already been taken care of in InsetLabel::addToToc().
313 // It seems that this reference does not point to any valid label.
314 screen_label_ = _("BROKEN: ") + screen_label_;
315 Toc & toc = buffer().tocBackend().toc("label");
316 toc.push_back(TocItem(cpit, 0, screen_label_, output_active));
320 void InsetRef::validate(LaTeXFeatures & features) const
322 string const cmd = getCmdName();
323 if (cmd == "vref" || cmd == "vpageref")
324 features.require("varioref");
325 else if (cmd == "formatted") {
326 docstring const data = getEscapedLabel(features.runparams());
329 string const fcmd = to_utf8(getFormattedCmd(data, label, prefix));
330 if (buffer().params().use_refstyle) {
331 features.require("refstyle");
333 features.addPreambleSnippet("\\let\\charef=\\chapref");
334 else if (!prefix.empty()) {
335 string lcmd = "\\AtBeginDocument{\\providecommand" +
336 fcmd + "[1]{\\ref{" + to_utf8(prefix) + ":#1}}}";
337 features.addPreambleSnippet(lcmd);
340 features.require("prettyref");
341 // prettyref uses "cha" for chapters, so we provide a kind of
343 if (prefix == "chap")
344 features.addPreambleSnippet("\\let\\pr@chap=\\pr@cha");
346 } else if (cmd == "eqref" && !buffer().params().use_refstyle)
347 // with refstyle, we simply output "(\ref{label})"
348 features.require("amsmath");
349 else if (cmd == "nameref")
350 features.require("nameref");
354 InsetRef::type_info InsetRef::types[] = {
355 { "ref", N_("Standard"), N_("Ref: ")},
356 { "eqref", N_("Equation"), N_("EqRef: ")},
357 { "pageref", N_("Page Number"), N_("Page: ")},
358 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
359 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
360 { "formatted", N_("Formatted"), N_("Format: ")},
361 { "nameref", N_("Reference to Name"), N_("NameRef:")},
366 int InsetRef::getType(string const & name)
368 for (int i = 0; !types[i].latex_name.empty(); ++i)
369 if (name == types[i].latex_name)
375 string const & InsetRef::getName(int type)
377 return types[type].latex_name;