]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
InsetTabular.cpp: fix #6585 also for wrapped floats - thanks Vincent
[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 "InsetLabel.h"
19 #include "LaTeXFeatures.h"
20 #include "LyX.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "ParIterator.h"
24 #include "sgml.h"
25 #include "TocBackend.h"
26
27 #include "support/debug.h"
28 #include "support/docstream.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31
32 using namespace lyx::support;
33 using namespace std;
34
35 namespace lyx {
36
37
38 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
39         : InsetCommand(buf, p)
40 {}
41
42
43 InsetRef::InsetRef(InsetRef const & ir)
44         : InsetCommand(ir)
45 {}
46
47
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.
51         return s == "ref" 
52                 || s == "pageref"
53                 || s == "vref" 
54                 || s == "vpageref"
55                 || s == "formatted"
56                 || s == "eqref"
57                 || s == "nameref";
58 }
59
60
61 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
62 {
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);
68         }
69         return param_info_;
70 }
71
72
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
77 {
78         static docstring const defcmd = from_ascii("\\ref");
79         if (!buffer().params().use_refstyle) 
80                 return from_ascii("\\prettyref");
81
82         docstring prefix;
83         label = split(ref, prefix, ':');
84         if (prefix.empty()) {
85                 LYXERR0("Label `" << label << "' contains no prefix.");
86                 return defcmd;
87         }
88         
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.");
94                         // restore the label
95                         label = ref;
96                         return defcmd;
97                 }
98         }
99         return from_ascii("\\") + prefix + from_ascii("ref");
100 }
101
102
103 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
104 {
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());              
109 }
110
111
112 int InsetRef::latex(odocstream & os, OutputParams const & rp) const
113 {
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);
122                 return 0;
123         } 
124         
125         // so we're doing a formatted reference.
126         docstring const data = getEscapedLabel(rp);
127         docstring label;
128         docstring const fcmd = getFormattedCmd(data, label);
129         os << fcmd << '{' << label << '}';
130         return 0;
131 }
132
133
134 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
135 {
136         docstring const str = getParam("reference");
137         os << '[' << str << ']';
138         return 2 + str.size();
139 }
140
141
142 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
143 {
144         docstring const & name = getParam("name");
145         if (name.empty()) {
146                 if (runparams.flavor == OutputParams::XML) {
147                         os << "<xref linkend=\""
148                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
149                            << "\" />";
150                 } else {
151                         os << "<xref linkend=\""
152                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
153                            << "\">";
154                 }
155         } else {
156                 os << "<link linkend=\""
157                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
158                    << "\">"
159                    << getParam("name")
160                    << "</link>";
161         }
162
163         return 0;
164 }
165
166
167 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
168 {
169         docstring const & ref = getParam("reference");
170         InsetLabel const * il = buffer().insetLabel(ref);
171         string const & cmd = params().getCmdName();
172         docstring display_string;
173
174         if (il && !il->counterValue().empty()) {
175                 // Try to construct a label from the InsetLabel we reference.
176                 docstring const & value = il->counterValue();
177                 if (cmd == "ref")
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
190                          || cmd == "nameref")
191                         display_string = il->prettyCounter();
192         } else 
193                         display_string = ref;
194
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");
202         return docstring();
203 }
204
205
206 void InsetRef::tocString(odocstream & os) const
207 {
208         plaintext(os, OutputParams(0));
209 }
210
211
212 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
213 {
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));
217
218         docstring label;
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);
222                         break;
223                 }
224         }
225         label += ref;
226         
227         if (!buffer().isLatex() && !getParam("name").empty()) {
228                 label += "||";
229                 label += getParam("name");
230         }
231         
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_ += "...";
238                 shortened = true;
239         }
240         if (shortened)
241                 tooltip_ = label;
242         else 
243                 tooltip_ = from_ascii("");
244 }
245
246
247 void InsetRef::addToToc(DocIterator const & cpit)
248 {
249         docstring const & label = getParam("reference");
250         if (buffer().insetLabel(label))
251                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
252                 return;
253
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_));
258 }
259
260
261 void InsetRef::validate(LaTeXFeatures & features) const
262 {
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());
270                         docstring label;
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);
275                         }
276                 } else
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");
283 }
284
285
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:")},
294         { "", "", "" }
295 };
296
297
298 int InsetRef::getType(string const & name)
299 {
300         for (int i = 0; !types[i].latex_name.empty(); ++i)
301                 if (name == types[i].latex_name)
302                         return i;
303         return 0;
304 }
305
306
307 string const & InsetRef::getName(int type)
308 {
309         return types[type].latex_name;
310 }
311
312
313 } // namespace lyx