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
87 docstring InsetRef::getFormattedCmd(docstring const & ref,
88 docstring & label, docstring & prefix) const
90 static docstring const defcmd = from_ascii("\\ref");
91 static docstring const prtcmd = from_ascii("\\prettyref");
93 label = split(ref, prefix, ':');
95 // we have to have xxx:xxxxx...
97 LYXERR0("Label `" << ref << "' contains no prefix.");
99 prefix = from_ascii("");
103 if (prefix.empty()) {
109 if (!buffer().params().use_refstyle) {
110 // \prettyref uses the whole label
115 // make sure the prefix is legal for a latex command
116 int const len = prefix.size();
117 for (int i = 0; i < len; i++) {
118 char_type const c = prefix[i];
119 if (!isAlphaASCII(c)) {
120 LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
126 return from_ascii("\\") + prefix + from_ascii("ref");
130 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
132 InsetCommandParams const & p = params();
133 ParamInfo const & pi = p.info();
134 ParamInfo::ParamData const & pd = pi["reference"];
135 return p.prepareCommand(rp, getParam("reference"), pd.handling());
139 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
141 string const cmd = getCmdName();
142 if (cmd != "formatted") {
143 // We don't want to output p_["name"], since that is only used
144 // in docbook. So we construct new params, without it, and use that.
145 InsetCommandParams p(REF_CODE, cmd);
146 docstring const ref = getParam("reference");
147 p["reference"] = ref;
148 os << p.getCommand(rp);
152 // so we're doing a formatted reference.
153 docstring const data = getEscapedLabel(rp);
156 docstring const fcmd = getFormattedCmd(data, label, prefix);
157 os << fcmd << '{' << label << '}';
161 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
163 docstring const str = getParam("reference");
164 os << '[' << str << ']';
165 return 2 + str.size();
169 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
171 docstring const & name = getParam("name");
173 if (runparams.flavor == OutputParams::XML) {
174 os << "<xref linkend=\""
175 << sgml::cleanID(buffer(), runparams, getParam("reference"))
178 os << "<xref linkend=\""
179 << sgml::cleanID(buffer(), runparams, getParam("reference"))
183 os << "<link linkend=\""
184 << sgml::cleanID(buffer(), runparams, getParam("reference"))
194 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
196 docstring const & ref = getParam("reference");
197 InsetLabel const * il = buffer().insetLabel(ref);
198 string const & cmd = params().getCmdName();
199 docstring display_string;
201 if (il && !il->counterValue().empty()) {
202 // Try to construct a label from the InsetLabel we reference.
203 docstring const & value = il->counterValue();
205 display_string = value;
206 else if (cmd == "vref")
207 // normally, would be "ref on page #", but we have no pages
208 display_string = value;
209 else if (cmd == "pageref" || cmd == "vpageref")
210 // normally would be "on page #", but we have no pages
211 display_string = _("elsewhere");
212 else if (cmd == "eqref")
213 display_string = bformat(from_ascii("equation (%1$s)"), value);
214 else if (cmd == "prettyref"
215 // we don't really have the ability to handle these
216 // properly in XHTML output
218 display_string = il->prettyCounter();
220 display_string = ref;
222 // FIXME What we'd really like to do is to be able to output some
223 // appropriate sort of text here. But to do that, we need to associate
224 // some sort of counter with the label, and we don't have that yet.
225 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
226 xs << html::StartTag("a", attr);
227 xs << display_string;
228 xs << html::EndTag("a");
233 void InsetRef::toString(odocstream & os) const
235 plaintext(os, OutputParams(0));
239 void InsetRef::forToc(docstring & os, size_t) const
241 // There's no need for details in the TOC, and a long label
242 // will just get in the way.
247 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
249 docstring const & ref = getParam("reference");
250 // register this inset into the buffer reference cache.
251 buffer().references(ref).push_back(make_pair(this, it));
254 for (int i = 0; !types[i].latex_name.empty(); ++i) {
255 if (getCmdName() == types[i].latex_name) {
256 label = _(types[i].short_gui_name);
262 if (!buffer().isLatex() && !getParam("name").empty()) {
264 label += getParam("name");
267 screen_label_ = label;
268 bool shortened = false;
269 unsigned int const maxLabelChars = 24;
270 if (screen_label_.size() > maxLabelChars) {
271 screen_label_.erase(maxLabelChars - 3);
272 screen_label_ += "...";
278 tooltip_ = from_ascii("");
282 void InsetRef::addToToc(DocIterator const & cpit) const
284 docstring const & label = getParam("reference");
285 if (buffer().insetLabel(label))
286 // This InsetRef has already been taken care of in InsetLabel::addToToc().
289 // It seems that this reference does not point to any valid label.
290 screen_label_ = _("BROKEN: ") + screen_label_;
291 Toc & toc = buffer().tocBackend().toc("label");
292 toc.push_back(TocItem(cpit, 0, screen_label_));
296 void InsetRef::validate(LaTeXFeatures & features) const
298 string const cmd = getCmdName();
299 if (cmd == "vref" || cmd == "vpageref")
300 features.require("varioref");
301 else if (getCmdName() == "formatted") {
302 docstring const data = getEscapedLabel(features.runparams());
305 if (buffer().params().use_refstyle) {
306 features.require("refstyle");
307 string const fcmd = to_utf8(getFormattedCmd(data, label, prefix));
308 if (!prefix.empty()) {
309 string lcmd = "\\AtBeginDocument{\\providecommand" +
310 fcmd + "[1]{\\ref{" + to_utf8(prefix) + ":#1}}}";
311 features.addPreambleSnippet(lcmd);
312 } else if (prefix == "cha")
313 features.addPreambleSnippet("\\let\\charef=\\chapref");
315 features.require("prettyref");
316 // prettyref uses "cha" for chapters, so we provide a kind of
318 if (prefix == "chap")
319 features.addPreambleSnippet("\\let\\pr@chap=\\pr@cha");
321 } else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
322 // refstyle defines its own version
323 features.require("amsmath");
324 else if (cmd == "nameref")
325 features.require("nameref");
329 InsetRef::type_info InsetRef::types[] = {
330 { "ref", N_("Standard"), N_("Ref: ")},
331 { "eqref", N_("Equation"), N_("EqRef: ")},
332 { "pageref", N_("Page Number"), N_("Page: ")},
333 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
334 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
335 { "formatted", N_("Formatted"), N_("Format: ")},
336 { "nameref", N_("Reference to Name"), N_("NameRef:")},
341 int InsetRef::getType(string const & name)
343 for (int i = 0; !types[i].latex_name.empty(); ++i)
344 if (name == types[i].latex_name)
350 string const & InsetRef::getName(int type)
352 return types[type].latex_name;