]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
Enable InsetQuote in verbatim and Hebrew
[lyx.git] / src / insets / InsetHyperlink.cpp
1 /**
2  * \file InsetHyperlink.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  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetHyperlink.h"
15
16 #include "Buffer.h"
17 #include "DispatchResult.h"
18 #include "Encoding.h"
19 #include "Format.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "LaTeXFeatures.h"
23 #include "OutputParams.h"
24 #include "output_xhtml.h"
25 #include "texstream.h"
26
27 #include "support/docstream.h"
28 #include "support/FileName.h"
29 #include "support/filetools.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32
33 #include "frontends/alert.h"
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39
40
41 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
42         : InsetCommand(buf, p)
43 {}
44
45
46 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
47 {
48         static ParamInfo param_info_;
49         if (param_info_.empty()) {
50                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
51                 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
52                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
53         }
54         return param_info_;
55 }
56
57
58 docstring InsetHyperlink::screenLabel() const
59 {
60         docstring const temp = _("Hyperlink: ");
61
62         docstring url;
63
64         url += getParam("name");
65         if (url.empty())
66                 url += getParam("target");
67
68         // elide if long
69         if (url.length() > 30) {
70                 docstring end = url.substr(url.length() - 17, url.length());
71                 support::truncateWithEllipsis(url, 13);
72                 url += end;
73         }
74         return temp + url;
75 }
76
77 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
78 {
79         switch (cmd.action()) {
80
81         case LFUN_INSET_EDIT:
82                 viewTarget();
83                 break;
84
85         default:
86                 InsetCommand::doDispatch(cur, cmd);
87                 break;
88         }
89 }
90
91
92 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
93                 FuncStatus & flag) const
94 {
95         switch (cmd.action()) {
96         case LFUN_INSET_EDIT:
97                 flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
98                 return true;
99
100         default:
101                 return InsetCommand::getStatus(cur, cmd, flag);
102         }
103 }
104
105
106 void InsetHyperlink::viewTarget() const
107 {
108         if (getParam("type") == "file:") {
109                 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
110                 string const format = formats.getFormatFromFile(url);
111                 formats.view(buffer(), url, format);
112         }
113 }
114
115
116 void InsetHyperlink::latex(otexstream & os,
117                            OutputParams const & runparams) const
118 {
119         docstring url = getParam("target");
120         docstring name = getParam("name");
121         static docstring const backslash = from_ascii("\\");
122         static docstring const braces = from_ascii("{}");
123         static char_type const chars_url[2] = {'%', '#'};
124         static char_type const chars_name[6] = {
125                 '&', '_', '$', '%', '#', '^'};
126
127         // For the case there is no name given, the target is set as name.
128         // Do this before !url.empty() and !name.empty() to handle characters
129         // like the "%" correctly.
130         if (name.empty())
131                 name = url;
132
133         if (!url.empty()) {
134                 // Replace the "\" character by its ASCII code according to the
135                 // URL specifications because "\" is not allowed in URLs and by
136                 // \href. Only do this when the following character is not also
137                 // a "\", because "\\" is valid code
138                 for (size_t i = 0, pos;
139                         (pos = url.find('\\', i)) != string::npos;
140                         i = pos + 2) {
141                         if (url[pos + 1] != '\\')
142                                 url.replace(pos, 1, from_ascii("%5C"));
143                 }
144
145                 // The characters in chars_url[] need to be escaped in the url
146                 // field because otherwise LaTeX will fail when the hyperlink is
147                 // within an argument of another command, e.g. in a \footnote. It
148                 // is important that they are escaped as "\#" and not as "\#{}".
149                 for (int k = 0; k < 2; k++)
150                         for (size_t i = 0, pos;
151                                 (pos = url.find(chars_url[k], i)) != string::npos;
152                                 i = pos + 2)
153                                 url.replace(pos, 1, backslash + chars_url[k]);
154                 
155                 // add "http://" when the type is web (type = empty)
156                 // and no "://" or "run:" is given
157                 docstring type = getParam("type");
158                 if (url.find(from_ascii("://")) == string::npos
159                         && url.find(from_ascii("run:")) == string::npos
160                         && type.empty())
161                         url = from_ascii("http://") + url;
162
163         } // end if (!url.empty())
164
165         // The characters in chars_name[] need to be changed to a command when
166         // they are in the name field.
167         if (!name.empty()) {
168                 // handle the "\" character, but only when the following character
169                 // is not also a "\", because "\\" is valid code
170                 docstring const textbackslash = from_ascii("\\textbackslash{}");
171                 for (size_t i = 0, pos;
172                         (pos = name.find('\\', i)) != string::npos;
173                         i = pos + 2) {
174                         if (name[pos + 1] != '\\')
175                                 name.replace(pos, 1, textbackslash);
176                 }
177                 // The characters in chars_name[] need to be changed to a command
178                 // when they are in the name field.
179                 // Therefore the treatment of "\" must be the first thing
180                 for (int k = 0; k < 6; k++)
181                         for (size_t i = 0, pos;
182                                 (pos = name.find(chars_name[k], i)) != string::npos;
183                                 i = pos + 2)
184                                 name.replace(pos, 1, backslash + chars_name[k] + braces);
185
186                 // replace the tilde by the \sim character as suggested in the
187                 // LaTeX FAQ for URLs
188                 docstring const sim = from_ascii("$\\sim$");
189                 for (size_t i = 0, pos;
190                         (pos = name.find('~', i)) != string::npos;
191                         i = pos + 1)
192                         name.replace(pos, 1, sim);
193                 pair<docstring, docstring> name_latexed =
194                         runparams.encoding->latexString(name, runparams.dryrun);
195                 name = name_latexed.first;
196                 if (!name_latexed.second.empty() && !runparams.silent) {
197                         // issue a warning about omitted characters
198                         // FIXME: should be passed to the error dialog
199                         frontend::Alert::warning(_("Uncodable characters"),
200                                 bformat(_("The following characters that are used in the href inset are not\n"
201                                           "representable in the current encoding and therefore have been omitted:\n%1$s."),
202                                         name_latexed.second));
203                 }
204         }  // end if (!name.empty())
205         
206         if (runparams.moving_arg)
207                 os << "\\protect";
208
209         // output the ready \href command
210         os << "\\href{" << getParam("type") << url << "}{" << name << '}';
211 }
212
213
214 int InsetHyperlink::plaintext(odocstringstream & os,
215         OutputParams const &, size_t) const
216 {
217         odocstringstream oss;
218
219         oss << '[' << getParam("target");
220         if (getParam("name").empty())
221                 oss << ']';
222         else
223                 oss << "||" << getParam("name") << ']';
224
225         docstring const str = oss.str();
226         os << str;
227         return str.size();
228 }
229
230
231 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
232 {
233         os << "<ulink url=\""
234            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
235            << "\">"
236            << getParam("name")
237            << "</ulink>";
238         return 0;
239 }
240
241
242 docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
243 {
244         docstring const & target = 
245                 html::htmlize(getParam("target"), XHTMLStream::ESCAPE_AND);
246         docstring const & name   = getParam("name");
247         xs << html::StartTag("a", to_utf8("href=\"" + target + "\""));
248         xs << (name.empty() ? target : name);
249         xs << html::EndTag("a");
250         return docstring();
251 }
252
253
254 void InsetHyperlink::toString(odocstream & os) const
255 {
256         odocstringstream ods;
257         plaintext(ods, OutputParams(0), INT_MAX);
258         os << ods.str();
259 }
260
261
262 void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
263 {
264         docstring const & n = getParam("name");
265         if (!n.empty()) {
266                 os += n;
267                 return;
268         }
269         os += getParam("target");
270 }
271
272
273 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
274 {
275         docstring url = getParam("target");
276         docstring type = getParam("type");
277         docstring guitype = _("www");
278         if (type == "mailto:")
279                 guitype = _("email");
280         else if (type == "file:")
281                 guitype = _("file");
282         return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
283 }
284
285
286 void InsetHyperlink::validate(LaTeXFeatures & features) const
287 {
288         features.require("hyperref");
289 }
290
291
292 string InsetHyperlink::contextMenuName() const
293 {
294         return "context-hyperlink";
295 }
296
297
298 } // namespace lyx