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"
32 using namespace lyx::support;
38 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
39 : InsetCommand(buf, p)
43 InsetRef::InsetRef(InsetRef const & ir)
48 bool InsetRef::isCompatibleCommand(string const & s) {
49 //FIXME This is likely not the best way to handle this.
50 //But this stuff is hardcoded elsewhere already.
61 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
63 static ParamInfo param_info_;
64 if (param_info_.empty()) {
65 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
66 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
67 ParamInfo::HANDLING_ESCAPE);
73 // the ref argument is the label name we are referencing.
74 // we expect ref to be in the form: pfx:suffix.
76 // if it isn't, then we can't produce a formatted reference,
77 // so we return "\ref" and put ref into label.
79 // for refstyle, we return "\pfxcmd", and put suffix into
80 // label and pfx into prefix. this is because refstyle expects
81 // the command: \pfxcmd{suffix}.
83 // for prettyref, we return "\prettyref" and put ref into label
84 // and pfx into prefix. this is because prettyref
86 docstring InsetRef::getFormattedCmd(docstring const & ref,
87 docstring & label, docstring & prefix) const
89 static docstring const defcmd = from_ascii("\\ref");
90 static docstring const prtcmd = from_ascii("\\prettyref");
92 label = split(ref, prefix, ':');
94 // we have to have xxx:xxxxx...
96 LYXERR0("Label `" << label << "' contains no prefix.");
101 if (!buffer().params().use_refstyle) {
102 // \prettyref uses the whole label
107 // make sure the prefix is legal for a latex command
108 int const len = prefix.size();
109 for (int i = 0; i < len; i++) {
110 if (!isalpha(prefix[i])) {
111 LYXERR0("Prefix `" << prefix << "' contains non-letters.");
117 return from_ascii("\\") + prefix + from_ascii("ref");
121 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
123 InsetCommandParams const & p = params();
124 ParamInfo const & pi = p.info();
125 ParamInfo::ParamData const & pd = pi["reference"];
126 return p.prepareCommand(rp, getParam("reference"), pd.handling());
130 int InsetRef::latex(odocstream & os, OutputParams const & rp) const
132 string const cmd = getCmdName();
133 if (cmd != "formatted") {
134 // We don't want to output p_["name"], since that is only used
135 // in docbook. So we construct new params, without it, and use that.
136 InsetCommandParams p(REF_CODE, cmd);
137 docstring const ref = getParam("reference");
138 p["reference"] = ref;
139 os << p.getCommand(rp);
143 // so we're doing a formatted reference.
144 docstring const data = getEscapedLabel(rp);
147 docstring const fcmd = getFormattedCmd(data, label, prefix);
148 os << fcmd << '{' << label << '}';
153 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
155 docstring const str = getParam("reference");
156 os << '[' << str << ']';
157 return 2 + str.size();
161 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
163 docstring const & name = getParam("name");
165 if (runparams.flavor == OutputParams::XML) {
166 os << "<xref linkend=\""
167 << sgml::cleanID(buffer(), runparams, getParam("reference"))
170 os << "<xref linkend=\""
171 << sgml::cleanID(buffer(), runparams, getParam("reference"))
175 os << "<link linkend=\""
176 << sgml::cleanID(buffer(), runparams, getParam("reference"))
186 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
188 docstring const & ref = getParam("reference");
189 InsetLabel const * il = buffer().insetLabel(ref);
190 string const & cmd = params().getCmdName();
191 docstring display_string;
193 if (il && !il->counterValue().empty()) {
194 // Try to construct a label from the InsetLabel we reference.
195 docstring const & value = il->counterValue();
197 display_string = value;
198 else if (cmd == "vref")
199 // normally, would be "ref on page #", but we have no pages
200 display_string = value;
201 else if (cmd == "pageref" || cmd == "vpageref")
202 // normally would be "on page #", but we have no pages
203 display_string = _("elsewhere");
204 else if (cmd == "eqref")
205 display_string = bformat(from_ascii("equation (%1$s)"), value);
206 else if (cmd == "prettyref"
207 // we don't really have the ability to handle these
208 // properly in XHTML output
210 display_string = il->prettyCounter();
212 display_string = ref;
214 // FIXME What we'd really like to do is to be able to output some
215 // appropriate sort of text here. But to do that, we need to associate
216 // some sort of counter with the label, and we don't have that yet.
217 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
218 xs << html::StartTag("a", attr);
219 xs << display_string;
220 xs << html::EndTag("a");
225 void InsetRef::tocString(odocstream & os) const
227 plaintext(os, OutputParams(0));
231 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
233 docstring const & ref = getParam("reference");
234 // register this inset into the buffer reference cache.
235 buffer().references(ref).push_back(make_pair(this, it));
238 for (int i = 0; !types[i].latex_name.empty(); ++i) {
239 if (getCmdName() == types[i].latex_name) {
240 label = _(types[i].short_gui_name);
246 if (!buffer().isLatex() && !getParam("name").empty()) {
248 label += getParam("name");
251 screen_label_ = label;
252 bool shortened = false;
253 unsigned int const maxLabelChars = 24;
254 if (screen_label_.size() > maxLabelChars) {
255 screen_label_.erase(maxLabelChars - 3);
256 screen_label_ += "...";
262 tooltip_ = from_ascii("");
266 void InsetRef::addToToc(DocIterator const & cpit)
268 docstring const & label = getParam("reference");
269 if (buffer().insetLabel(label))
270 // This InsetRef has already been taken care of in InsetLabel::addToToc().
273 // It seems that this reference does not point to any valid label.
274 screen_label_ = _("BROKEN: ") + screen_label_;
275 Toc & toc = buffer().tocBackend().toc("label");
276 toc.push_back(TocItem(cpit, 0, screen_label_));
280 void InsetRef::validate(LaTeXFeatures & features) const
282 string const cmd = getCmdName();
283 if (cmd == "vref" || cmd == "vpageref")
284 features.require("varioref");
285 else if (getCmdName() == "formatted") {
286 if (buffer().params().use_refstyle) {
287 features.require("refstyle");
288 docstring const data = getEscapedLabel(features.runparams());
291 string const fcmd = to_utf8(getFormattedCmd(data, label, prefix));
292 if (!prefix.empty()) {
293 string lcmd = "\\AtBeginDocument{\\providecommand" +
294 fcmd + "[1]{\\ref{" + to_utf8(prefix) + ":#1}}}";
295 features.addPreambleSnippet(lcmd);
298 features.require("prettyref");
299 } else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
300 // refstyle defines its own version
301 features.require("amsmath");
302 else if (cmd == "nameref")
303 features.require("nameref");
307 InsetRef::type_info InsetRef::types[] = {
308 { "ref", N_("Standard"), N_("Ref: ")},
309 { "eqref", N_("Equation"), N_("EqRef: ")},
310 { "pageref", N_("Page Number"), N_("Page: ")},
311 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
312 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
313 { "formatted", N_("Formatted"), N_("Format: ")},
314 { "nameref", N_("Reference to Name"), N_("NameRef:")},
319 int InsetRef::getType(string const & name)
321 for (int i = 0; !types[i].latex_name.empty(); ++i)
322 if (name == types[i].latex_name)
328 string const & InsetRef::getName(int type)
330 return types[type].latex_name;