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 `" << ref << "' contains no prefix.");
98 prefix = from_ascii("");
102 if (prefix.empty()) {
108 if (!buffer().params().use_refstyle) {
109 // \prettyref uses the whole label
114 // make sure the prefix is legal for a latex command
115 int const len = prefix.size();
116 for (int i = 0; i < len; i++) {
117 if (!isalpha(prefix[i])) {
118 LYXERR0("Prefix `" << prefix << "' contains non-letters.");
124 return from_ascii("\\") + prefix + from_ascii("ref");
128 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
130 InsetCommandParams const & p = params();
131 ParamInfo const & pi = p.info();
132 ParamInfo::ParamData const & pd = pi["reference"];
133 return p.prepareCommand(rp, getParam("reference"), pd.handling());
137 int InsetRef::latex(odocstream & os, OutputParams const & rp) const
139 string const cmd = getCmdName();
140 if (cmd != "formatted") {
141 // We don't want to output p_["name"], since that is only used
142 // in docbook. So we construct new params, without it, and use that.
143 InsetCommandParams p(REF_CODE, cmd);
144 docstring const ref = getParam("reference");
145 p["reference"] = ref;
146 os << p.getCommand(rp);
150 // so we're doing a formatted reference.
151 docstring const data = getEscapedLabel(rp);
154 docstring const fcmd = getFormattedCmd(data, label, prefix);
155 os << fcmd << '{' << label << '}';
160 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
162 docstring const str = getParam("reference");
163 os << '[' << str << ']';
164 return 2 + str.size();
168 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
170 docstring const & name = getParam("name");
172 if (runparams.flavor == OutputParams::XML) {
173 os << "<xref linkend=\""
174 << sgml::cleanID(buffer(), runparams, getParam("reference"))
177 os << "<xref linkend=\""
178 << sgml::cleanID(buffer(), runparams, getParam("reference"))
182 os << "<link linkend=\""
183 << sgml::cleanID(buffer(), runparams, getParam("reference"))
193 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
195 docstring const & ref = getParam("reference");
196 InsetLabel const * il = buffer().insetLabel(ref);
197 string const & cmd = params().getCmdName();
198 docstring display_string;
200 if (il && !il->counterValue().empty()) {
201 // Try to construct a label from the InsetLabel we reference.
202 docstring const & value = il->counterValue();
204 display_string = value;
205 else if (cmd == "vref")
206 // normally, would be "ref on page #", but we have no pages
207 display_string = value;
208 else if (cmd == "pageref" || cmd == "vpageref")
209 // normally would be "on page #", but we have no pages
210 display_string = _("elsewhere");
211 else if (cmd == "eqref")
212 display_string = bformat(from_ascii("equation (%1$s)"), value);
213 else if (cmd == "prettyref"
214 // we don't really have the ability to handle these
215 // properly in XHTML output
217 display_string = il->prettyCounter();
219 display_string = ref;
221 // FIXME What we'd really like to do is to be able to output some
222 // appropriate sort of text here. But to do that, we need to associate
223 // some sort of counter with the label, and we don't have that yet.
224 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
225 xs << html::StartTag("a", attr);
226 xs << display_string;
227 xs << html::EndTag("a");
232 void InsetRef::tocString(odocstream & os) const
234 plaintext(os, OutputParams(0));
238 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
240 docstring const & ref = getParam("reference");
241 // register this inset into the buffer reference cache.
242 buffer().references(ref).push_back(make_pair(this, it));
245 for (int i = 0; !types[i].latex_name.empty(); ++i) {
246 if (getCmdName() == types[i].latex_name) {
247 label = _(types[i].short_gui_name);
253 if (!buffer().isLatex() && !getParam("name").empty()) {
255 label += getParam("name");
258 screen_label_ = label;
259 bool shortened = false;
260 unsigned int const maxLabelChars = 24;
261 if (screen_label_.size() > maxLabelChars) {
262 screen_label_.erase(maxLabelChars - 3);
263 screen_label_ += "...";
269 tooltip_ = from_ascii("");
273 void InsetRef::addToToc(DocIterator const & cpit)
275 docstring const & label = getParam("reference");
276 if (buffer().insetLabel(label))
277 // This InsetRef has already been taken care of in InsetLabel::addToToc().
280 // It seems that this reference does not point to any valid label.
281 screen_label_ = _("BROKEN: ") + screen_label_;
282 Toc & toc = buffer().tocBackend().toc("label");
283 toc.push_back(TocItem(cpit, 0, screen_label_));
287 void InsetRef::validate(LaTeXFeatures & features) const
289 string const cmd = getCmdName();
290 if (cmd == "vref" || cmd == "vpageref")
291 features.require("varioref");
292 else if (getCmdName() == "formatted") {
293 docstring const data = getEscapedLabel(features.runparams());
296 if (buffer().params().use_refstyle) {
297 features.require("refstyle");
298 string const fcmd = to_utf8(getFormattedCmd(data, label, prefix));
299 if (!prefix.empty()) {
300 string lcmd = "\\AtBeginDocument{\\providecommand" +
301 fcmd + "[1]{\\ref{" + to_utf8(prefix) + ":#1}}}";
302 features.addPreambleSnippet(lcmd);
303 } else if (prefix == "cha")
304 features.addPreambleSnippet("\\let\\charef=\\chapref");
306 features.require("prettyref");
307 // prettyref uses "cha" for chapters, so we provide a kind of
309 if (prefix == "chap")
310 features.addPreambleSnippet("\\let\\pr@chap=\\pr@cha");
312 } else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
313 // refstyle defines its own version
314 features.require("amsmath");
315 else if (cmd == "nameref")
316 features.require("nameref");
320 InsetRef::type_info InsetRef::types[] = {
321 { "ref", N_("Standard"), N_("Ref: ")},
322 { "eqref", N_("Equation"), N_("EqRef: ")},
323 { "pageref", N_("Page Number"), N_("Page: ")},
324 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
325 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
326 { "formatted", N_("Formatted"), N_("Format: ")},
327 { "nameref", N_("Reference to Name"), N_("NameRef:")},
332 int InsetRef::getType(string const & name)
334 for (int i = 0; !types[i].latex_name.empty(); ++i)
335 if (name == types[i].latex_name)
341 string const & InsetRef::getName(int type)
343 return types[type].latex_name;