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 // for refstyle, given pfx:suffix, we want to return "\\pfxcmd"
74 // and put "suffix" into label.
75 // otherwise, we put the reference into label.
76 docstring InsetRef::getFormattedCmd(
77 docstring const & ref, docstring & label) const
79 static docstring const defcmd = from_ascii("\\ref");
80 static docstring const prtcmd = from_ascii("\\prettyref");
83 label = split(ref, prefix, ':');
85 // we have to have xxx:xxxxx...
87 LYXERR0("Label `" << label << "' contains no prefix.");
92 if (!buffer().params().use_refstyle) {
93 // \prettyref uses the whole label
98 // make sure the prefix is legal for a latex command
99 int const len = prefix.size();
100 for (int i = 0; i < len; i++) {
101 if (!isalpha(prefix[i])) {
102 LYXERR0("Prefix `" << prefix << "' contains non-letters.");
108 return from_ascii("\\") + prefix + from_ascii("ref");
112 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
114 InsetCommandParams const & p = params();
115 ParamInfo const & pi = p.info();
116 ParamInfo::ParamData const & pd = pi["reference"];
117 return p.prepareCommand(rp, getParam("reference"), pd.handling());
121 int InsetRef::latex(odocstream & os, OutputParams const & rp) const
123 string const cmd = getCmdName();
124 if (cmd != "formatted") {
125 // We don't want to output p_["name"], since that is only used
126 // in docbook. So we construct new params, without it, and use that.
127 InsetCommandParams p(REF_CODE, cmd);
128 docstring const ref = getParam("reference");
129 p["reference"] = ref;
130 os << p.getCommand(rp);
134 // so we're doing a formatted reference.
135 docstring const data = getEscapedLabel(rp);
137 docstring const fcmd = getFormattedCmd(data, label);
138 os << fcmd << '{' << label << '}';
143 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
145 docstring const str = getParam("reference");
146 os << '[' << str << ']';
147 return 2 + str.size();
151 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
153 docstring const & name = getParam("name");
155 if (runparams.flavor == OutputParams::XML) {
156 os << "<xref linkend=\""
157 << sgml::cleanID(buffer(), runparams, getParam("reference"))
160 os << "<xref linkend=\""
161 << sgml::cleanID(buffer(), runparams, getParam("reference"))
165 os << "<link linkend=\""
166 << sgml::cleanID(buffer(), runparams, getParam("reference"))
176 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
178 docstring const & ref = getParam("reference");
179 InsetLabel const * il = buffer().insetLabel(ref);
180 string const & cmd = params().getCmdName();
181 docstring display_string;
183 if (il && !il->counterValue().empty()) {
184 // Try to construct a label from the InsetLabel we reference.
185 docstring const & value = il->counterValue();
187 display_string = value;
188 else if (cmd == "vref")
189 // normally, would be "ref on page #", but we have no pages
190 display_string = value;
191 else if (cmd == "pageref" || cmd == "vpageref")
192 // normally would be "on page #", but we have no pages
193 display_string = _("elsewhere");
194 else if (cmd == "eqref")
195 display_string = bformat(from_ascii("equation (%1$s)"), value);
196 else if (cmd == "prettyref"
197 // we don't really have the ability to handle these
198 // properly in XHTML output
200 display_string = il->prettyCounter();
202 display_string = ref;
204 // FIXME What we'd really like to do is to be able to output some
205 // appropriate sort of text here. But to do that, we need to associate
206 // some sort of counter with the label, and we don't have that yet.
207 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
208 xs << html::StartTag("a", attr);
209 xs << display_string;
210 xs << html::EndTag("a");
215 void InsetRef::tocString(odocstream & os) const
217 plaintext(os, OutputParams(0));
221 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
223 docstring const & ref = getParam("reference");
224 // register this inset into the buffer reference cache.
225 buffer().references(ref).push_back(make_pair(this, it));
228 for (int i = 0; !types[i].latex_name.empty(); ++i) {
229 if (getCmdName() == types[i].latex_name) {
230 label = _(types[i].short_gui_name);
236 if (!buffer().isLatex() && !getParam("name").empty()) {
238 label += getParam("name");
241 screen_label_ = label;
242 bool shortened = false;
243 unsigned int const maxLabelChars = 24;
244 if (screen_label_.size() > maxLabelChars) {
245 screen_label_.erase(maxLabelChars - 3);
246 screen_label_ += "...";
252 tooltip_ = from_ascii("");
256 void InsetRef::addToToc(DocIterator const & cpit)
258 docstring const & label = getParam("reference");
259 if (buffer().insetLabel(label))
260 // This InsetRef has already been taken care of in InsetLabel::addToToc().
263 // It seems that this reference does not point to any valid label.
264 screen_label_ = _("BROKEN: ") + screen_label_;
265 Toc & toc = buffer().tocBackend().toc("label");
266 toc.push_back(TocItem(cpit, 0, screen_label_));
270 void InsetRef::validate(LaTeXFeatures & features) const
272 string const cmd = getCmdName();
273 if (cmd == "vref" || cmd == "vpageref")
274 features.require("varioref");
275 else if (getCmdName() == "formatted") {
276 if (buffer().params().use_refstyle) {
277 features.require("refstyle");
278 docstring const data = getEscapedLabel(features.runparams());
280 string const fcmd = to_utf8(getFormattedCmd(data, label));
281 if (fcmd != "\\ref") {
282 string lcmd = "\\AtBeginDocument{\\providecommand" + fcmd + "[1]{\\ref{#1}}}";
283 features.addPreambleSnippet(lcmd);
286 features.require("prettyref");
287 } else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
288 // refstyle defines its own version
289 features.require("amsmath");
290 else if (cmd == "nameref")
291 features.require("nameref");
295 InsetRef::type_info InsetRef::types[] = {
296 { "ref", N_("Standard"), N_("Ref: ")},
297 { "eqref", N_("Equation"), N_("EqRef: ")},
298 { "pageref", N_("Page Number"), N_("Page: ")},
299 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
300 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
301 { "formatted", N_("Formatted"), N_("Format: ")},
302 { "nameref", N_("Reference to Name"), N_("NameRef:")},
307 int InsetRef::getType(string const & name)
309 for (int i = 0; !types[i].latex_name.empty(); ++i)
310 if (name == types[i].latex_name)
316 string const & InsetRef::getName(int type)
318 return types[type].latex_name;