]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
Fix text direction of references with XeTeX/bidi
[lyx.git] / src / insets / InsetRef.cpp
1 /**
2  * \file InsetRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include <config.h>
11
12 #include "InsetRef.h"
13
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "Cursor.h"
17 #include "DispatchResult.h"
18 #include "FuncStatus.h"
19 #include "InsetLabel.h"
20 #include "Language.h"
21 #include "LaTeXFeatures.h"
22 #include "LyX.h"
23 #include "OutputParams.h"
24 #include "output_xhtml.h"
25 #include "ParIterator.h"
26 #include "sgml.h"
27 #include "texstream.h"
28 #include "TocBackend.h"
29
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34 #include "support/textutils.h"
35
36 using namespace lyx::support;
37 using namespace std;
38
39 namespace lyx {
40
41
42 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
43         : InsetCommand(buf, p), broken_(false)
44 {}
45
46
47 InsetRef::InsetRef(InsetRef const & ir)
48         : InsetCommand(ir), broken_(false)
49 {}
50
51
52 bool InsetRef::isCompatibleCommand(string const & s) {
53         //FIXME This is likely not the best way to handle this.
54         //But this stuff is hardcoded elsewhere already.
55         return s == "ref"
56                 || s == "pageref"
57                 || s == "vref"
58                 || s == "vpageref"
59                 || s == "formatted"
60                 || s == "prettyref" // for InsetMathRef FIXME
61                 || s == "eqref"
62                 || s == "nameref"
63                 || s == "labelonly";
64 }
65
66
67 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
68 {
69         static ParamInfo param_info_;
70         if (param_info_.empty()) {
71                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
72                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
73                                 ParamInfo::HANDLING_ESCAPE);
74                 param_info_.add("plural", ParamInfo::LYX_INTERNAL);
75                 param_info_.add("caps", ParamInfo::LYX_INTERNAL);
76                 param_info_.add("noprefix", ParamInfo::LYX_INTERNAL);
77         }
78         return param_info_;
79 }
80
81
82 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
83 {
84         string const inset = cmd.getArg(0);
85         string const arg   = cmd.getArg(1);
86         string pstring;
87         if (cmd.action() == LFUN_INSET_MODIFY && inset == "ref") {
88                 if (arg == "toggle-plural")
89                         pstring = "plural";
90                 else if (arg == "toggle-caps")
91                         pstring = "caps";
92                 else if (arg == "toggle-noprefix")
93                         pstring = "noprefix";
94         }
95         // otherwise not for us
96         if (pstring.empty())
97                 return InsetCommand::doDispatch(cur, cmd);
98
99         bool const isSet = (getParam(pstring) == "true");
100         setParam(pstring, from_ascii(isSet ? "false"  : "true"));
101 }
102
103
104 bool InsetRef::getStatus(Cursor & cur, FuncRequest const & cmd,
105         FuncStatus & status) const
106 {
107         if (cmd.action() != LFUN_INSET_MODIFY)
108                 return InsetCommand::getStatus(cur, cmd, status);
109         if (cmd.getArg(0) != "ref")
110                 return InsetCommand::getStatus(cur, cmd, status);
111
112         string const arg = cmd.getArg(1);
113         string pstring;
114         if (arg == "toggle-plural")
115                 pstring = "plural";
116         else if (arg == "toggle-caps")
117                 pstring = "caps";
118         if (!pstring.empty()) {
119                 status.setEnabled(buffer().params().use_refstyle &&
120                         params().getCmdName() == "formatted");
121                 bool const isSet = (getParam(pstring) == "true");
122                 status.setOnOff(isSet);
123                 return true;
124         }
125         if (arg == "toggle-noprefix") {
126                 status.setEnabled(params().getCmdName() == "labelonly");
127                 bool const isSet = (getParam("noprefix") == "true");
128                 status.setOnOff(isSet);
129                 return true;
130         }
131         // otherwise not for us
132         return InsetCommand::getStatus(cur, cmd, status);
133 }
134
135
136 namespace {
137
138 void capitalize(docstring & s) {
139         char_type t = uppercase(s[0]);
140         s[0] = t;
141 }
142
143 } // namespace
144
145
146 // the ref argument is the label name we are referencing.
147 // we expect ref to be in the form: pfx:suffix.
148 //
149 // if it isn't, then we can't produce a formatted reference,
150 // so we return "\ref" and put ref into label.
151 //
152 // for refstyle, we return "\pfxcmd", and put suffix into
153 // label and pfx into prefix. this is because refstyle expects
154 // the command: \pfxcmd{suffix}.
155 //
156 // for prettyref, we return "\prettyref" and put ref into label
157 // and pfx into prefix. this is because prettyref uses the whole
158 // label, thus: \prettyref{pfx:suffix}.
159 //
160 docstring InsetRef::getFormattedCmd(docstring const & ref,
161         docstring & label, docstring & prefix, docstring const & caps) const
162 {
163         static docstring const defcmd = from_ascii("\\ref");
164         static docstring const prtcmd = from_ascii("\\prettyref");
165
166         label = split(ref, prefix, ':');
167
168         // we have to have xxx:xxxxx...
169         if (label.empty()) {
170                 LYXERR0("Label `" << ref << "' contains no `:' separator.");
171                 label = ref;
172                 prefix = from_ascii("");
173                 return defcmd;
174         }
175
176         if (prefix.empty()) {
177                 // we have ":xxxx"
178                 label = ref;
179                 return defcmd;
180         }
181
182         if (!buffer().params().use_refstyle) {
183                 // \prettyref uses the whole label
184                 label = ref;
185                 return prtcmd;
186         }
187
188         // make sure the prefix is legal for a latex command
189         int const len = prefix.size();
190         for (int i = 0; i < len; i++) {
191                 char_type const c = prefix[i];
192                 if (!isAlphaASCII(c)) {
193                         LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
194                         // restore the label
195                         label = ref;
196                         return defcmd;
197                 }
198         }
199         if (caps == "true") {
200                 capitalize(prefix);
201         }
202         return from_ascii("\\") + prefix + from_ascii("ref");
203 }
204
205
206 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
207 {
208         InsetCommandParams const & p = params();
209         ParamInfo const & pi = p.info();
210         ParamInfo::ParamData const & pd = pi["reference"];
211         return p.prepareCommand(rp, getParam("reference"), pd.handling());
212 }
213
214
215 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
216 {
217         string const & cmd = getCmdName();
218         docstring const & data = getEscapedLabel(rp);
219
220         if (rp.inulemcmd > 0)
221                 os << "\\mbox{";
222
223         if (cmd == "eqref" && buffer().params().use_refstyle) {
224                 // we advertise this as printing "(n)", so we'll do that, at least
225                 // for refstyle, since refstlye's own \eqref prints, by default,
226                 // "equation n". if one wants \eqref, one can get it by using a
227                 // formatted label in this case.
228                 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
229         }
230         else if (cmd == "formatted") {
231                 docstring label;
232                 docstring prefix;
233                 docstring const fcmd =
234                         getFormattedCmd(data, label, prefix, getParam("caps"));
235                 os << fcmd;
236                 if (buffer().params().use_refstyle && getParam("plural") == "true")
237                     os << "[s]";
238                 os << '{' << label << '}';
239         }
240         else if (cmd == "labelonly") {
241                 docstring const & ref = getParam("reference");
242                 if (getParam("noprefix") != "true")
243                         os << ref;
244                 else {
245                         docstring prefix;
246                         docstring suffix = split(ref, prefix, ':');
247                         if (suffix.empty()) {
248                     LYXERR0("Label `" << ref << "' contains no `:' separator.");
249                                 os << ref;
250                         } else {
251                                 os << suffix;
252                         }
253                 }
254         }
255         else {
256                 // We don't want to output p_["name"], since that is only used
257                 // in docbook. So we construct new params, without it, and use that.
258                 InsetCommandParams p(REF_CODE, cmd);
259                 docstring const ref = getParam("reference");
260                 p["reference"] = ref;
261                 os << p.getCommand(rp);
262         }
263
264         if (rp.inulemcmd > 0)
265                 os << "}";
266 }
267
268
269 int InsetRef::plaintext(odocstringstream & os,
270         OutputParams const &, size_t) const
271 {
272         docstring const str = getParam("reference");
273         os << '[' << str << ']';
274         return 2 + str.size();
275 }
276
277
278 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
279 {
280         docstring const & name = getParam("name");
281         if (name.empty()) {
282                 if (runparams.flavor == OutputParams::XML) {
283                         os << "<xref linkend=\""
284                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
285                            << "\" />";
286                 } else {
287                         os << "<xref linkend=\""
288                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
289                            << "\">";
290                 }
291         } else {
292                 os << "<link linkend=\""
293                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
294                    << "\">"
295                    << getParam("name")
296                    << "</link>";
297         }
298
299         return 0;
300 }
301
302
303 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
304 {
305         docstring const & ref = getParam("reference");
306         InsetLabel const * il = buffer().insetLabel(ref, true);
307         string const & cmd = params().getCmdName();
308         docstring display_string;
309
310         if (il && !il->counterValue().empty()) {
311                 // Try to construct a label from the InsetLabel we reference.
312                 docstring const & value = il->counterValue();
313                 if (cmd == "ref")
314                         display_string = value;
315                 else if (cmd == "vref")
316                         // normally, would be "ref on page #", but we have no pages
317                         display_string = value;
318                 else if (cmd == "pageref" || cmd == "vpageref")
319                         // normally would be "on page #", but we have no pages.
320                         display_string = translateIfPossible(from_ascii("elsewhere"),
321                                 op.local_font->language()->lang());
322                 else if (cmd == "eqref")
323                         display_string = '(' + value + ')';
324                 else if (cmd == "formatted") {
325                         display_string = il->prettyCounter();
326                         if (buffer().params().use_refstyle && getParam("caps") == "true")
327                                 capitalize(display_string);
328                         // it is hard to see what to do about plurals...
329                 }
330                 else if (cmd == "nameref")
331                         // FIXME We don't really have the ability to handle these
332                         // properly in XHTML output yet (bug #8599).
333                         // It might not be that hard to do. We have the InsetLabel,
334                         // and we can presumably find its paragraph using the TOC.
335                         // But the label might be referencing a section, yet not be
336                         // in that section. So this is not trivial.
337                         display_string = il->prettyCounter();
338         } else
339                         display_string = ref;
340
341         // FIXME What we'd really like to do is to be able to output some
342         // appropriate sort of text here. But to do that, we need to associate
343         // some sort of counter with the label, and we don't have that yet.
344         docstring const attr = "href=\"#" + html::cleanAttr(ref) + '"';
345         xs << html::StartTag("a", to_utf8(attr));
346         xs << display_string;
347         xs << html::EndTag("a");
348         return docstring();
349 }
350
351
352 void InsetRef::toString(odocstream & os) const
353 {
354         odocstringstream ods;
355         plaintext(ods, OutputParams(0));
356         os << ods.str();
357 }
358
359
360 void InsetRef::forOutliner(docstring & os, size_t const, bool const) const
361 {
362         // There's no need for details in the TOC, and a long label
363         // will just get in the way.
364         os += '#';
365 }
366
367
368 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
369 {
370         docstring const & ref = getParam("reference");
371         // register this inset into the buffer reference cache.
372         buffer().addReference(ref, this, it);
373
374         docstring label;
375         string const & cmd = getCmdName();
376         for (int i = 0; !types[i].latex_name.empty(); ++i) {
377                 if (cmd == types[i].latex_name) {
378                         label = _(types[i].short_gui_name);
379                         break;
380                 }
381         }
382
383         if (cmd != "labelonly")
384                 label += ref;
385         else {
386                 if (getParam("noprefix") != "true")
387                         label += ref;
388                 else {
389                         docstring prefix;
390                         docstring suffix = split(ref, prefix, ':');
391                         if (suffix.empty()) {
392                                 label += ref;
393                         } else {
394                                 label += suffix;
395                         }
396                 }
397         }
398
399         if (!buffer().params().isLatex() && !getParam("name").empty()) {
400                 label += "||";
401                 label += getParam("name");
402         }
403
404         unsigned int const maxLabelChars = 24;
405         if (label.size() > maxLabelChars) {
406                 tooltip_ = label;
407                 support::truncateWithEllipsis(label, maxLabelChars);
408         } else
409                 tooltip_ = from_ascii("");
410
411         screen_label_ = label;
412         broken_ = false;
413 }
414
415
416 docstring InsetRef::screenLabel() const
417 {
418         return (broken_ ? _("BROKEN: ") : docstring()) + screen_label_;
419 }
420
421
422 void InsetRef::addToToc(DocIterator const & cpit, bool output_active,
423                                                 UpdateType, TocBackend & backend) const
424 {
425         docstring const & label = getParam("reference");
426         if (buffer().insetLabel(label)) {
427                 broken_ = !buffer().activeLabel(label);
428                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
429                 return;
430         }
431
432         // It seems that this reference does not point to any valid label.
433
434         broken_ = true;
435         shared_ptr<Toc> toc = backend.toc("label");
436         toc->push_back(TocItem(cpit, 0, screenLabel(), output_active));
437 }
438
439
440 void InsetRef::validate(LaTeXFeatures & features) const
441 {
442         string const cmd = getCmdName();
443         if (cmd == "vref" || cmd == "vpageref")
444                 features.require("varioref");
445         else if (cmd == "formatted") {
446                 docstring const data = getEscapedLabel(features.runparams());
447                 docstring label;
448                 docstring prefix;
449                 docstring const fcmd =
450                         getFormattedCmd(data, label, prefix, getParam("caps"));
451                 if (buffer().params().use_refstyle) {
452                         features.require("refstyle");
453                         if (prefix == "cha")
454                                 features.addPreambleSnippet(from_ascii("\\let\\charef=\\chapref"));
455                         else if (!prefix.empty()) {
456                                 docstring lcmd = "\\AtBeginDocument{\\providecommand" +
457                                                 fcmd + "[1]{\\ref{" + prefix + ":#1}}}";
458                                 features.addPreambleSnippet(lcmd);
459                         }
460                 } else {
461                         features.require("prettyref");
462                         // prettyref uses "cha" for chapters, so we provide a kind of
463                         // translation.
464                         if (prefix == "chap")
465                                 features.addPreambleSnippet(from_ascii("\\let\\pr@chap=\\pr@cha"));
466                 }
467         } else if (cmd == "eqref" && !buffer().params().use_refstyle)
468                 // with refstyle, we simply output "(\ref{label})"
469                 features.require("amsmath");
470         else if (cmd == "nameref")
471                 features.require("nameref");
472 }
473
474 bool InsetRef::forceLTR(OutputParams const & rp) const
475 {
476         // We force LTR for references. However,
477         // * Namerefs are output in the scripts direction
478         //   at least with fontspec/bidi and luabidi, though (see #11518).
479         // * Parentheses are automatically swapped with XeTeX/bidi 
480         //   [not with LuaTeX/luabidi] (see #11626).
481         // FIXME: Re-Audit all other RTL cases.
482         if (rp.useBidiPackage())
483                 return false;
484         return (getCmdName() != "nameref" || !buffer().masterParams().useNonTeXFonts);
485 }
486
487
488 InsetRef::type_info const InsetRef::types[] = {
489         { "ref",       N_("Standard"),              N_("Ref: ")},
490         { "eqref",     N_("Equation"),              N_("EqRef: ")},
491         { "pageref",   N_("Page Number"),           N_("Page: ")},
492         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
493         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
494         { "nameref",   N_("Reference to Name"),     N_("NameRef: ")},
495         { "formatted", N_("Formatted"),             N_("Format: ")},
496         { "labelonly", N_("Label Only"),            N_("Label: ")},
497         { "", "", "" }
498 };
499
500
501 docstring InsetRef::getTOCString() const
502 {
503         return tooltip_.empty() ? screenLabel() : tooltip_;
504 }
505
506 } // namespace lyx