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();
144 // refstyle defines its own version of \eqref
145 if (cmd != "formatted" &&
146 !(cmd == "eqref" && buffer().params().use_refstyle)
148 // We don't want to output p_["name"], since that is only used
149 // in docbook. So we construct new params, without it, and use that.
150 InsetCommandParams p(REF_CODE, cmd);
151 docstring const ref = getParam("reference");
152 p["reference"] = ref;
153 os << p.getCommand(rp);
157 // so we're doing a formatted reference of some kind.
158 docstring const data = getEscapedLabel(rp);
160 // what we say in the UI is that an "eqref" is supposed to surround the
161 // reference with parentheses, so let's do that.
162 if (cmd == "eqref" /* && buffer().params().use_refstyle */) {
163 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
169 docstring const fcmd = getFormattedCmd(data, label, prefix);
170 os << fcmd << '{' << label << '}';
174 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
176 docstring const str = getParam("reference");
177 os << '[' << str << ']';
178 return 2 + str.size();
182 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
184 docstring const & name = getParam("name");
186 if (runparams.flavor == OutputParams::XML) {
187 os << "<xref linkend=\""
188 << sgml::cleanID(buffer(), runparams, getParam("reference"))
191 os << "<xref linkend=\""
192 << sgml::cleanID(buffer(), runparams, getParam("reference"))
196 os << "<link linkend=\""
197 << sgml::cleanID(buffer(), runparams, getParam("reference"))
207 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
209 docstring const & ref = getParam("reference");
210 InsetLabel const * il = buffer().insetLabel(ref);
211 string const & cmd = params().getCmdName();
212 docstring display_string;
214 if (il && !il->counterValue().empty()) {
215 // Try to construct a label from the InsetLabel we reference.
216 docstring const & value = il->counterValue();
218 display_string = value;
219 else if (cmd == "vref")
220 // normally, would be "ref on page #", but we have no pages
221 display_string = value;
222 else if (cmd == "pageref" || cmd == "vpageref")
223 // normally would be "on page #", but we have no pages
224 display_string = _("elsewhere");
225 else if (cmd == "eqref")
226 display_string = bformat(from_ascii("equation (%1$s)"), value);
227 else if (cmd == "prettyref"
228 // we don't really have the ability to handle these
229 // properly in XHTML output
231 display_string = il->prettyCounter();
233 display_string = ref;
235 // FIXME What we'd really like to do is to be able to output some
236 // appropriate sort of text here. But to do that, we need to associate
237 // some sort of counter with the label, and we don't have that yet.
238 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
239 xs << html::StartTag("a", attr);
240 xs << display_string;
241 xs << html::EndTag("a");
246 void InsetRef::toString(odocstream & os) const
248 plaintext(os, OutputParams(0));
252 void InsetRef::forToc(docstring & os, size_t) const
254 // There's no need for details in the TOC, and a long label
255 // will just get in the way.
260 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
262 docstring const & ref = getParam("reference");
263 // register this inset into the buffer reference cache.
264 buffer().references(ref).push_back(make_pair(this, it));
267 for (int i = 0; !types[i].latex_name.empty(); ++i) {
268 if (getCmdName() == types[i].latex_name) {
269 label = _(types[i].short_gui_name);
275 if (!buffer().isLatex() && !getParam("name").empty()) {
277 label += getParam("name");
280 screen_label_ = label;
281 bool shortened = false;
282 unsigned int const maxLabelChars = 24;
283 if (screen_label_.size() > maxLabelChars) {
284 screen_label_.erase(maxLabelChars - 3);
285 screen_label_ += "...";
291 tooltip_ = from_ascii("");
295 void InsetRef::addToToc(DocIterator const & cpit) const
297 docstring const & label = getParam("reference");
298 if (buffer().insetLabel(label))
299 // This InsetRef has already been taken care of in InsetLabel::addToToc().
302 // It seems that this reference does not point to any valid label.
303 screen_label_ = _("BROKEN: ") + screen_label_;
304 Toc & toc = buffer().tocBackend().toc("label");
305 toc.push_back(TocItem(cpit, 0, screen_label_));
309 void InsetRef::validate(LaTeXFeatures & features) const
311 string const cmd = getCmdName();
312 if (cmd == "vref" || cmd == "vpageref")
313 features.require("varioref");
314 else if (getCmdName() == "formatted") {
315 docstring const data = getEscapedLabel(features.runparams());
318 if (buffer().params().use_refstyle) {
319 features.require("refstyle");
320 string const fcmd = to_utf8(getFormattedCmd(data, label, prefix));
321 if (!prefix.empty()) {
322 string lcmd = "\\AtBeginDocument{\\providecommand" +
323 fcmd + "[1]{\\ref{" + to_utf8(prefix) + ":#1}}}";
324 features.addPreambleSnippet(lcmd);
325 } else if (prefix == "cha")
326 features.addPreambleSnippet("\\let\\charef=\\chapref");
328 features.require("prettyref");
329 // prettyref uses "cha" for chapters, so we provide a kind of
331 if (prefix == "chap")
332 features.addPreambleSnippet("\\let\\pr@chap=\\pr@cha");
334 } else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
335 // refstyle defines its own version
336 features.require("amsmath");
337 else if (cmd == "nameref")
338 features.require("nameref");
342 InsetRef::type_info InsetRef::types[] = {
343 { "ref", N_("Standard"), N_("Ref: ")},
344 { "eqref", N_("Equation"), N_("EqRef: ")},
345 { "pageref", N_("Page Number"), N_("Page: ")},
346 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
347 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
348 { "formatted", N_("Formatted"), N_("Format: ")},
349 { "nameref", N_("Reference to Name"), N_("NameRef:")},
354 int InsetRef::getType(string const & name)
356 for (int i = 0; !types[i].latex_name.empty(); ++i)
357 if (name == types[i].latex_name)
363 string const & InsetRef::getName(int type)
365 return types[type].latex_name;