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);
81 void capitalize(docstring & s) {
82 char_type t = uppercase(s[0]);
89 // the ref argument is the label name we are referencing.
90 // we expect ref to be in the form: pfx:suffix.
92 // if it isn't, then we can't produce a formatted reference,
93 // so we return "\ref" and put ref into label.
95 // for refstyle, we return "\pfxcmd", and put suffix into
96 // label and pfx into prefix. this is because refstyle expects
97 // the command: \pfxcmd{suffix}.
99 // for prettyref, we return "\prettyref" and put ref into label
100 // and pfx into prefix. this is because prettyref uses the whole
101 // label, thus: \prettyref{pfx:suffix}.
103 docstring InsetRef::getFormattedCmd(docstring const & ref,
104 docstring & label, docstring & prefix, docstring const & caps) const
106 static docstring const defcmd = from_ascii("\\ref");
107 static docstring const prtcmd = from_ascii("\\prettyref");
109 label = split(ref, prefix, ':');
111 // we have to have xxx:xxxxx...
113 LYXERR0("Label `" << ref << "' contains no prefix.");
115 prefix = from_ascii("");
119 if (prefix.empty()) {
125 if (!buffer().params().use_refstyle) {
126 // \prettyref uses the whole label
131 // make sure the prefix is legal for a latex command
132 int const len = prefix.size();
133 for (int i = 0; i < len; i++) {
134 char_type const c = prefix[i];
135 if (!isAlphaASCII(c)) {
136 LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
142 if (caps == "true") {
145 return from_ascii("\\") + prefix + from_ascii("ref");
149 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
151 InsetCommandParams const & p = params();
152 ParamInfo const & pi = p.info();
153 ParamInfo::ParamData const & pd = pi["reference"];
154 return p.prepareCommand(rp, getParam("reference"), pd.handling());
158 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
160 string const & cmd = getCmdName();
161 docstring const & data = getEscapedLabel(rp);
163 if (rp.inulemcmd > 0)
166 if (cmd == "eqref" && buffer().params().use_refstyle) {
167 // we advertise this as printing "(n)", so we'll do that, at least
168 // for refstyle, since refstlye's own \eqref prints, by default,
169 // "equation n". if one wants \eqref, one can get it by using a
170 // formatted label in this case.
171 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
173 else if (cmd == "formatted") {
176 docstring const fcmd =
177 getFormattedCmd(data, label, prefix, getParam("caps"));
179 if (buffer().params().use_refstyle && getParam("plural") == "true")
181 os << '{' << label << '}';
183 else if (cmd == "labelonly") {
184 os << getParam("reference");
187 // We don't want to output p_["name"], since that is only used
188 // in docbook. So we construct new params, without it, and use that.
189 InsetCommandParams p(REF_CODE, cmd);
190 docstring const ref = getParam("reference");
191 p["reference"] = ref;
192 os << p.getCommand(rp);
195 if (rp.inulemcmd > 0)
200 int InsetRef::plaintext(odocstringstream & os,
201 OutputParams const &, size_t) const
203 docstring const str = getParam("reference");
204 os << '[' << str << ']';
205 return 2 + str.size();
209 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
211 docstring const & name = getParam("name");
213 if (runparams.flavor == OutputParams::XML) {
214 os << "<xref linkend=\""
215 << sgml::cleanID(buffer(), runparams, getParam("reference"))
218 os << "<xref linkend=\""
219 << sgml::cleanID(buffer(), runparams, getParam("reference"))
223 os << "<link linkend=\""
224 << sgml::cleanID(buffer(), runparams, getParam("reference"))
234 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
236 docstring const & ref = getParam("reference");
237 InsetLabel const * il = buffer().insetLabel(ref);
238 string const & cmd = params().getCmdName();
239 docstring display_string;
241 if (il && !il->counterValue().empty()) {
242 // Try to construct a label from the InsetLabel we reference.
243 docstring const & value = il->counterValue();
245 display_string = value;
246 else if (cmd == "vref")
247 // normally, would be "ref on page #", but we have no pages
248 display_string = value;
249 else if (cmd == "pageref" || cmd == "vpageref")
250 // normally would be "on page #", but we have no pages.
251 display_string = translateIfPossible(from_ascii("elsewhere"),
252 op.local_font->language()->lang());
253 else if (cmd == "eqref")
254 display_string = '(' + value + ')';
255 else if (cmd == "formatted") {
256 display_string = il->prettyCounter();
257 if (buffer().params().use_refstyle && getParam("caps") == "true")
258 capitalize(display_string);
259 // it is hard to see what to do about plurals...
261 else if (cmd == "nameref")
262 // FIXME We don't really have the ability to handle these
263 // properly in XHTML output yet (bug #8599).
264 // It might not be that hard to do. We have the InsetLabel,
265 // and we can presumably find its paragraph using the TOC.
266 // But the label might be referencing a section, yet not be
267 // in that section. So this is not trivial.
268 display_string = il->prettyCounter();
270 display_string = ref;
272 // FIXME What we'd really like to do is to be able to output some
273 // appropriate sort of text here. But to do that, we need to associate
274 // some sort of counter with the label, and we don't have that yet.
275 docstring const attr = "href=\"#" + html::cleanAttr(ref) + '"';
276 xs << html::StartTag("a", to_utf8(attr));
277 xs << display_string;
278 xs << html::EndTag("a");
283 void InsetRef::toString(odocstream & os) const
285 odocstringstream ods;
286 plaintext(ods, OutputParams(0));
291 void InsetRef::forOutliner(docstring & os, size_t const, bool const) const
293 // There's no need for details in the TOC, and a long label
294 // will just get in the way.
299 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
301 docstring const & ref = getParam("reference");
302 // register this inset into the buffer reference cache.
303 buffer().addReference(ref, this, it);
306 for (int i = 0; !types[i].latex_name.empty(); ++i) {
307 if (getCmdName() == types[i].latex_name) {
308 label = _(types[i].short_gui_name);
314 if (!buffer().params().isLatex() && !getParam("name").empty()) {
316 label += getParam("name");
319 unsigned int const maxLabelChars = 24;
320 if (label.size() > maxLabelChars) {
322 support::truncateWithEllipsis(label, maxLabelChars);
324 tooltip_ = from_ascii("");
325 screen_label_ = label;
329 void InsetRef::addToToc(DocIterator const & cpit, bool output_active,
332 docstring const & label = getParam("reference");
333 if (buffer().insetLabel(label))
334 // This InsetRef has already been taken care of in InsetLabel::addToToc().
337 // It seems that this reference does not point to any valid label.
338 screen_label_ = _("BROKEN: ") + screen_label_;
339 shared_ptr<Toc> toc = buffer().tocBackend().toc("label");
340 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
344 void InsetRef::validate(LaTeXFeatures & features) const
346 string const cmd = getCmdName();
347 if (cmd == "vref" || cmd == "vpageref")
348 features.require("varioref");
349 else if (cmd == "formatted") {
350 docstring const data = getEscapedLabel(features.runparams());
353 docstring const fcmd =
354 getFormattedCmd(data, label, prefix, getParam("caps"));
355 if (buffer().params().use_refstyle) {
356 features.require("refstyle");
358 features.addPreambleSnippet(from_ascii("\\let\\charef=\\chapref"));
359 else if (!prefix.empty()) {
360 docstring lcmd = "\\AtBeginDocument{\\providecommand" +
361 fcmd + "[1]{\\ref{" + prefix + ":#1}}}";
362 features.addPreambleSnippet(lcmd);
365 features.require("prettyref");
366 // prettyref uses "cha" for chapters, so we provide a kind of
368 if (prefix == "chap")
369 features.addPreambleSnippet(from_ascii("\\let\\pr@chap=\\pr@cha"));
371 } else if (cmd == "eqref" && !buffer().params().use_refstyle)
372 // with refstyle, we simply output "(\ref{label})"
373 features.require("amsmath");
374 else if (cmd == "nameref")
375 features.require("nameref");
379 InsetRef::type_info const InsetRef::types[] = {
380 { "ref", N_("Standard"), N_("Ref: ")},
381 { "eqref", N_("Equation"), N_("EqRef: ")},
382 { "pageref", N_("Page Number"), N_("Page: ")},
383 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
384 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
385 { "formatted", N_("Formatted"), N_("Format: ")},
386 { "nameref", N_("Reference to Name"), N_("NameRef: ")},
387 { "labelonly", N_("Label Only"), N_("Label: ")},
392 int InsetRef::getType(string const & name)
394 for (int i = 0; !types[i].latex_name.empty(); ++i)
395 if (name == types[i].latex_name)
401 string const & InsetRef::getName(int type)
403 return types[type].latex_name;
407 docstring InsetRef::getTOCString() const
409 return tooltip_.empty() ? screen_label_ : tooltip_;