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, "ref")
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 // for refstyle, given pfx:suffix, we want to return "\\pfxcmd"
74 // and put "suffix" into label
75 docstring InsetRef::getFormattedCmd(
76 docstring const & ref, docstring & label) const
78 static docstring const defcmd = from_ascii("\\ref");
79 if (!buffer().params().use_refstyle)
80 return from_ascii("\\prettyref");
83 label = split(ref, prefix, ':');
85 LYXERR0("Label `" << label << "' contains no prefix.");
89 // make sure the prefix is legal for a latex command
90 int const len = prefix.size();
91 for (int i = 0; i < len; i++) {
92 if (!isalpha(prefix[i])) {
93 LYXERR0("Prefix `" << prefix << "' contains non-letters.");
99 return from_ascii("\\") + prefix + from_ascii("ref");
103 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
105 InsetCommandParams const & p = params();
106 ParamInfo const & pi = p.info();
107 ParamInfo::ParamData const & pd = pi["reference"];
108 return p.prepareCommand(rp, getParam("reference"), pd.handling());
112 int InsetRef::latex(odocstream & os, OutputParams const & rp) const
114 string const cmd = getCmdName();
115 if (cmd != "formatted") {
116 // We don't want to output p_["name"], since that is only used
117 // in docbook. So we construct new params, without it, and use that.
118 InsetCommandParams p(REF_CODE, cmd);
119 docstring const ref = getParam("reference");
120 p["reference"] = ref;
121 os << p.getCommand(rp);
125 // so we're doing a formatted reference.
126 docstring const data = getEscapedLabel(rp);
128 docstring const fcmd = getFormattedCmd(data, label);
129 os << fcmd << '{' << label << '}';
134 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
136 docstring const str = getParam("reference");
137 os << '[' << str << ']';
138 return 2 + str.size();
142 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
144 docstring const & name = getParam("name");
146 if (runparams.flavor == OutputParams::XML) {
147 os << "<xref linkend=\""
148 << sgml::cleanID(buffer(), runparams, getParam("reference"))
151 os << "<xref linkend=\""
152 << sgml::cleanID(buffer(), runparams, getParam("reference"))
156 os << "<link linkend=\""
157 << sgml::cleanID(buffer(), runparams, getParam("reference"))
167 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
169 docstring const & ref = getParam("reference");
170 InsetLabel const * il = buffer().insetLabel(ref);
171 string const & cmd = params().getCmdName();
172 docstring display_string;
174 if (il && !il->counterValue().empty()) {
175 // Try to construct a label from the InsetLabel we reference.
176 docstring const & value = il->counterValue();
178 display_string = value;
179 else if (cmd == "vref")
180 // normally, would be "ref on page #", but we have no pages
181 display_string = value;
182 else if (cmd == "pageref" || cmd == "vpageref")
183 // normally would be "on page #", but we have no pages
184 display_string = _("elsewhere");
185 else if (cmd == "eqref")
186 display_string = bformat(from_ascii("equation (%1$s)"), value);
187 else if (cmd == "prettyref"
188 // we don't really have the ability to handle these
189 // properly in XHTML output
191 display_string = il->prettyCounter();
193 display_string = ref;
195 // FIXME What we'd really like to do is to be able to output some
196 // appropriate sort of text here. But to do that, we need to associate
197 // some sort of counter with the label, and we don't have that yet.
198 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
199 xs << html::StartTag("a", attr);
200 xs << display_string;
201 xs << html::EndTag("a");
206 void InsetRef::tocString(odocstream & os) const
208 plaintext(os, OutputParams(0));
212 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
214 docstring const & ref = getParam("reference");
215 // register this inset into the buffer reference cache.
216 buffer().references(ref).push_back(make_pair(this, it));
219 for (int i = 0; !types[i].latex_name.empty(); ++i) {
220 if (getCmdName() == types[i].latex_name) {
221 label = _(types[i].short_gui_name);
227 if (!buffer().isLatex() && !getParam("name").empty()) {
229 label += getParam("name");
232 screen_label_ = label;
233 bool shortened = false;
234 unsigned int const maxLabelChars = 24;
235 if (screen_label_.size() > maxLabelChars) {
236 screen_label_.erase(maxLabelChars - 3);
237 screen_label_ += "...";
243 tooltip_ = from_ascii("");
247 void InsetRef::addToToc(DocIterator const & cpit)
249 docstring const & label = getParam("reference");
250 if (buffer().insetLabel(label))
251 // This InsetRef has already been taken care of in InsetLabel::addToToc().
254 // It seems that this reference does not point to any valid label.
255 screen_label_ = _("BROKEN: ") + screen_label_;
256 Toc & toc = buffer().tocBackend().toc("label");
257 toc.push_back(TocItem(cpit, 0, screen_label_));
261 void InsetRef::validate(LaTeXFeatures & features) const
263 string const cmd = getCmdName();
264 if (cmd == "vref" || cmd == "vpageref")
265 features.require("varioref");
266 else if (getCmdName() == "formatted") {
267 if (buffer().params().use_refstyle) {
268 features.require("refstyle");
269 docstring const data = getEscapedLabel(features.runparams());
271 string const fcmd = to_utf8(getFormattedCmd(data, label));
272 if (fcmd != "\\ref") {
273 string lcmd = "\\providecommand" + fcmd + "[1]{\\ref{#1}}";
274 features.addPreambleSnippet(lcmd);
277 features.require("prettyref");
278 } else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
279 // refstyle defines its own version
280 features.require("amsmath");
281 else if (cmd == "nameref")
282 features.require("nameref");
286 InsetRef::type_info InsetRef::types[] = {
287 { "ref", N_("Standard"), N_("Ref: ")},
288 { "eqref", N_("Equation"), N_("EqRef: ")},
289 { "pageref", N_("Page Number"), N_("Page: ")},
290 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
291 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
292 { "formatted", N_("Formatted"), N_("Format: ")},
293 { "nameref", N_("Reference to Name"), N_("NameRef:")},
298 int InsetRef::getType(string const & name)
300 for (int i = 0; !types[i].latex_name.empty(); ++i)
301 if (name == types[i].latex_name)
307 string const & InsetRef::getName(int type)
309 return types[type].latex_name;