]> code.delx.au - gnu-emacs/blob - lisp/net/browse-url.el
d232c8add139d48ffa642988b1406b4f3e7d0691
[gnu-emacs] / lisp / net / browse-url.el
1 ;;; browse-url.el --- pass a URL to a WWW browser
2
3 ;; Copyright (C) 1995-2015 Free Software Foundation, Inc.
4
5 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: 03 Apr 1995
8 ;; Keywords: hypertext, hypermedia, mouse
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This package provides functions which read a URL (Uniform Resource
28 ;; Locator) from the minibuffer, defaulting to the URL around point,
29 ;; and ask a World-Wide Web browser to load it. It can also load the
30 ;; URL associated with the current buffer. Different browsers use
31 ;; different methods of remote control so there is one function for
32 ;; each supported browser. If the chosen browser is not running, it
33 ;; is started. Currently there is support for the following browsers,
34 ;; as well as some other obsolete ones:
35
36 ;; Function Browser Earliest version
37 ;; browse-url-mozilla Mozilla Don't know
38 ;; browse-url-firefox Firefox Don't know (tried with 1.0.1)
39 ;; browse-url-chromium Chromium 3.0
40 ;; browse-url-epiphany Epiphany Don't know
41 ;; browse-url-conkeror Conkeror Don't know
42 ;; browse-url-w3 w3 0
43 ;; browse-url-text-* Any text browser 0
44 ;; browse-url-generic arbitrary
45 ;; browse-url-default-windows-browser MS-Windows browser
46 ;; browse-url-default-macosx-browser Mac OS X browser
47 ;; browse-url-xdg-open Free Desktop xdg-open on Gnome, KDE, Xfce4, LXDE
48 ;; browse-url-kde KDE konqueror (kfm)
49 ;; browse-url-elinks Elinks Don't know (tried with 0.12.GIT)
50
51 ;; Browsers can cache Web pages so it may be necessary to tell them to
52 ;; reload the current page if it has changed (e.g., if you have edited
53 ;; it). There is currently no perfect automatic solution to this.
54
55 ;; This package generalizes function html-previewer-process in Marc
56 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
57 ;; ffap.el package. The huge hyperbole package also contains similar
58 ;; functions.
59
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;; Usage
62
63 ;; To display the URL at or before point:
64 ;; M-x browse-url-at-point RET
65 ;; or, similarly but with the opportunity to edit the URL extracted from
66 ;; the buffer, use:
67 ;; M-x browse-url
68
69 ;; To display a URL by shift-clicking on it, put this in your init file:
70 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
71 ;; (Note that using Shift-mouse-1 is not desirable because
72 ;; that event has a standard meaning in Emacs.)
73
74 ;; To display the current buffer in a web browser:
75 ;; M-x browse-url-of-buffer RET
76
77 ;; To display the current region in a web browser:
78 ;; M-x browse-url-of-region RET
79
80 ;; In Dired, to display the file named on the current line:
81 ;; M-x browse-url-of-dired-file RET
82
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 ;; Customization (~/.emacs)
85
86 ;; To see what variables are available for customization, type
87 ;; `M-x set-variable browse-url TAB'. Better, use
88 ;; `M-x customize-group browse-url'.
89
90 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
91 ;; (as used by html-helper-mode):
92 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
93 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
94 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
95 ;; (global-set-key "\C-c\C-zu" 'browse-url)
96 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
97 ;; (add-hook 'dired-mode-hook
98 ;; (lambda ()
99 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
100
101 ;; Browse URLs in mail messages under RMAIL by clicking mouse-2:
102 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
103 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
104 ;; Alternatively, add `goto-address' to `rmail-show-message-hook'.
105
106 ;; Gnus provides a standard feature to activate URLs in article
107 ;; buffers for invocation of browse-url.
108
109 ;; Use the Emacs w3 browser when not running under X11:
110 ;; (or (eq window-system 'x)
111 ;; (setq browse-url-browser-function 'browse-url-w3))
112
113 ;; To always save modified buffers before displaying the file in a browser:
114 ;; (setq browse-url-save-file t)
115
116 ;; To invoke different browsers for different URLs:
117 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
118 ;; ("." . browse-url-firefox)))
119
120 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121 ;;; Code:
122
123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 ;; Variables
125
126 (defgroup browse-url nil
127 "Use a web browser to look at a URL."
128 :prefix "browse-url-"
129 :link '(emacs-commentary-link "browse-url")
130 :group 'external
131 :group 'comm)
132
133 ;;;###autoload
134 (defcustom browse-url-browser-function
135 'browse-url-default-browser
136 "Function to display the current buffer in a WWW browser.
137 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
138 `browse-url-of-file' commands.
139
140 If the value is not a function it should be a list of pairs
141 \(REGEXP . FUNCTION). In this case the function called will be the one
142 associated with the first REGEXP which matches the current URL. The
143 function is passed the URL and any other args of `browse-url'. The last
144 regexp should probably be \".\" to specify a default browser."
145 :type '(choice
146 (function-item :tag "Emacs W3" :value browse-url-w3)
147 (function-item :tag "eww" :value eww-browse-url)
148 (function-item :tag "Mozilla" :value browse-url-mozilla)
149 (function-item :tag "Firefox" :value browse-url-firefox)
150 (function-item :tag "Chromium" :value browse-url-chromium)
151 (function-item :tag "Epiphany" :value browse-url-epiphany)
152 (function-item :tag "Conkeror" :value browse-url-conkeror)
153 (function-item :tag "Text browser in an xterm window"
154 :value browse-url-text-xterm)
155 (function-item :tag "Text browser in an Emacs window"
156 :value browse-url-text-emacs)
157 (function-item :tag "KDE" :value browse-url-kde)
158 (function-item :tag "Elinks" :value browse-url-elinks)
159 (function-item :tag "Specified by `Browse Url Generic Program'"
160 :value browse-url-generic)
161 (function-item :tag "Default Windows browser"
162 :value browse-url-default-windows-browser)
163 (function-item :tag "Default Mac OS X browser"
164 :value browse-url-default-macosx-browser)
165 (function-item :tag "Default browser"
166 :value browse-url-default-browser)
167 (function :tag "Your own function")
168 (alist :tag "Regexp/function association list"
169 :key-type regexp :value-type function))
170 :version "24.1"
171 :group 'browse-url)
172
173 (defcustom browse-url-mailto-function 'browse-url-mail
174 "Function to display mailto: links.
175 This variable uses the same syntax as the
176 `browse-url-browser-function' variable. If the
177 `browse-url-mailto-function' variable is nil, that variable will
178 be used instead."
179 :type '(choice
180 (function-item :tag "Emacs Mail" :value browse-url-mail)
181 (function-item :tag "None" nil))
182 :version "24.1"
183 :group 'browse-url)
184
185 (defcustom browse-url-netscape-program "netscape"
186 ;; Info about netscape-remote from Karl Berry.
187 "The name by which to invoke Netscape.
188
189 The free program `netscape-remote' from
190 <URL:http://home.netscape.com/newsref/std/remote.c> is said to start
191 up very much quicker than `netscape'. Reported to compile on a GNU
192 system, given vroot.h from the same directory, with cc flags
193 -DSTANDALONE -L/usr/X11R6/lib -lXmu -lX11."
194 :type 'string
195 :group 'browse-url)
196
197 (make-obsolete-variable 'browse-url-netscape-program nil "25.1")
198
199 (defcustom browse-url-netscape-arguments nil
200 "A list of strings to pass to Netscape as arguments."
201 :type '(repeat (string :tag "Argument"))
202 :group 'browse-url)
203
204 (make-obsolete-variable 'browse-url-netscape-arguments nil "25.1")
205
206 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
207 "A list of strings to pass to Netscape when it starts up.
208 Defaults to the value of `browse-url-netscape-arguments' at the time
209 `browse-url' is loaded."
210 :type '(repeat (string :tag "Argument"))
211
212 :group 'browse-url)
213
214 (make-obsolete-variable 'browse-url-netscape-startup-arguments nil "25.1")
215
216 (defcustom browse-url-browser-display nil
217 "The X display for running the browser, if not same as Emacs's."
218 :type '(choice string (const :tag "Default" nil))
219 :group 'browse-url)
220
221 (defcustom browse-url-mozilla-program "mozilla"
222 "The name by which to invoke Mozilla."
223 :type 'string
224 :group 'browse-url)
225
226 (defcustom browse-url-mozilla-arguments nil
227 "A list of strings to pass to Mozilla as arguments."
228 :type '(repeat (string :tag "Argument"))
229 :group 'browse-url)
230
231 (defcustom browse-url-mozilla-startup-arguments browse-url-mozilla-arguments
232 "A list of strings to pass to Mozilla when it starts up.
233 Defaults to the value of `browse-url-mozilla-arguments' at the time
234 `browse-url' is loaded."
235 :type '(repeat (string :tag "Argument"))
236 :group 'browse-url)
237
238 (defcustom browse-url-firefox-program
239 (let ((candidates '("icecat" "iceweasel" "firefox")))
240 (while (and candidates (not (executable-find (car candidates))))
241 (setq candidates (cdr candidates)))
242 (or (car candidates) "firefox"))
243 "The name by which to invoke Firefox or a variant of it."
244 :type 'string
245 :group 'browse-url)
246
247 (defcustom browse-url-firefox-arguments nil
248 "A list of strings to pass to Firefox (or variant) as arguments."
249 :type '(repeat (string :tag "Argument"))
250 :group 'browse-url)
251
252 (defcustom browse-url-firefox-startup-arguments browse-url-firefox-arguments
253 "A list of strings to pass to Firefox (or variant) when it starts up.
254 Defaults to the value of `browse-url-firefox-arguments' at the time
255 `browse-url' is loaded."
256 :type '(repeat (string :tag "Argument"))
257 :group 'browse-url)
258
259 (make-obsolete-variable 'browse-url-firefox-startup-arguments
260 "it no longer has any effect." "24.5")
261
262 (defcustom browse-url-chromium-program
263 (let ((candidates '("chromium" "chromium-browser")))
264 (while (and candidates (not (executable-find (car candidates))))
265 (setq candidates (cdr candidates)))
266 (or (car candidates) "chromium"))
267 "The name by which to invoke Chromium."
268 :type 'string
269 :version "24.1"
270 :group 'browse-url)
271
272 (defcustom browse-url-chromium-arguments nil
273 "A list of strings to pass to Chromium as arguments."
274 :type '(repeat (string :tag "Argument"))
275 :version "24.1"
276 :group 'browse-url)
277
278 (defcustom browse-url-galeon-program "galeon"
279 "The name by which to invoke Galeon."
280 :type 'string
281 :group 'browse-url)
282
283 (make-obsolete-variable 'browse-url-galeon-program nil "25.1")
284
285 (defcustom browse-url-galeon-arguments nil
286 "A list of strings to pass to Galeon as arguments."
287 :type '(repeat (string :tag "Argument"))
288 :group 'browse-url)
289
290 (make-obsolete-variable 'browse-url-galeon-arguments nil "25.1")
291
292 (defcustom browse-url-galeon-startup-arguments browse-url-galeon-arguments
293 "A list of strings to pass to Galeon when it starts up.
294 Defaults to the value of `browse-url-galeon-arguments' at the time
295 `browse-url' is loaded."
296 :type '(repeat (string :tag "Argument"))
297 :group 'browse-url)
298
299 (make-obsolete-variable 'browse-url-galeon-startup-arguments nil "25.1")
300
301 (defcustom browse-url-epiphany-program "epiphany"
302 "The name by which to invoke Epiphany."
303 :type 'string
304 :group 'browse-url)
305
306 (defcustom browse-url-epiphany-arguments nil
307 "A list of strings to pass to Epiphany as arguments."
308 :type '(repeat (string :tag "Argument"))
309 :group 'browse-url)
310
311 (defcustom browse-url-epiphany-startup-arguments browse-url-epiphany-arguments
312 "A list of strings to pass to Epiphany when it starts up.
313 Defaults to the value of `browse-url-epiphany-arguments' at the time
314 `browse-url' is loaded."
315 :type '(repeat (string :tag "Argument"))
316 :group 'browse-url)
317
318 ;; GNOME means of invoking either Mozilla or Netscape.
319 (defvar browse-url-gnome-moz-program "gnome-moz-remote")
320
321 (make-obsolete-variable 'browse-url-gnome-moz-program nil "25.1")
322
323 (defcustom browse-url-gnome-moz-arguments '()
324 "A list of strings passed to the GNOME mozilla viewer as arguments."
325 :version "21.1"
326 :type '(repeat (string :tag "Argument"))
327 :group 'browse-url)
328
329 (make-obsolete-variable 'browse-url-gnome-moz-arguments nil "25.1")
330
331 (defcustom browse-url-mozilla-new-window-is-tab nil
332 "Whether to open up new windows in a tab or a new window.
333 If non-nil, then open the URL in a new tab rather than a new window if
334 `browse-url-mozilla' is asked to open it in a new window."
335 :type 'boolean
336 :group 'browse-url)
337
338 (defcustom browse-url-firefox-new-window-is-tab nil
339 "Whether to open up new windows in a tab or a new window.
340 If non-nil, then open the URL in a new tab rather than a new window if
341 `browse-url-firefox' is asked to open it in a new window.
342
343 This option is currently ignored on MS-Windows, since the necessary
344 functionality is not available there."
345 :type 'boolean
346 :group 'browse-url)
347
348 (defcustom browse-url-conkeror-new-window-is-buffer nil
349 "Whether to open up new windows in a buffer or a new window.
350 If non-nil, then open the URL in a new buffer rather than a new window if
351 `browse-url-conkeror' is asked to open it in a new window."
352 :type 'boolean
353 :group 'browse-url)
354
355 (defcustom browse-url-galeon-new-window-is-tab nil
356 "Whether to open up new windows in a tab or a new window.
357 If non-nil, then open the URL in a new tab rather than a new window if
358 `browse-url-galeon' is asked to open it in a new window."
359 :type 'boolean
360 :group 'browse-url)
361
362 (make-obsolete-variable 'browse-url-galeon-new-window-is-tab nil "25.1")
363
364 (defcustom browse-url-epiphany-new-window-is-tab nil
365 "Whether to open up new windows in a tab or a new window.
366 If non-nil, then open the URL in a new tab rather than a new window if
367 `browse-url-epiphany' is asked to open it in a new window."
368 :type 'boolean
369 :group 'browse-url)
370
371 (defcustom browse-url-netscape-new-window-is-tab nil
372 "Whether to open up new windows in a tab or a new window.
373 If non-nil, then open the URL in a new tab rather than a new
374 window if `browse-url-netscape' is asked to open it in a new
375 window."
376 :type 'boolean
377 :group 'browse-url)
378
379 (make-obsolete-variable 'browse-url-netscape-new-window-is-tab nil "25.1")
380
381 (defcustom browse-url-new-window-flag nil
382 "Non-nil means always open a new browser window with appropriate browsers.
383 Passing an interactive argument to \\[browse-url], or specific browser
384 commands reverses the effect of this variable."
385 :type 'boolean
386 :group 'browse-url)
387
388 (defcustom browse-url-mosaic-program "xmosaic"
389 "The name by which to invoke Mosaic (or mMosaic)."
390 :type 'string
391 :version "20.3"
392 :group 'browse-url)
393
394 (make-obsolete-variable 'browse-url-mosaic-program nil "25.1")
395
396 (defcustom browse-url-mosaic-arguments nil
397 "A list of strings to pass to Mosaic as arguments."
398 :type '(repeat (string :tag "Argument"))
399 :group 'browse-url)
400
401 (make-obsolete-variable 'browse-url-mosaic-arguments nil "25.1")
402
403 (defcustom browse-url-mosaic-pidfile "~/.mosaicpid"
404 "The name of the pidfile created by Mosaic."
405 :type 'string
406 :group 'browse-url)
407
408 (make-obsolete-variable 'browse-url-mosaic-pidfile nil "25.1")
409
410 (defcustom browse-url-conkeror-program "conkeror"
411 "The name by which to invoke Conkeror."
412 :type 'string
413 :version "25.1"
414 :group 'browse-url)
415
416 (defcustom browse-url-conkeror-arguments nil
417 "A list of strings to pass to Conkeror as arguments."
418 :type '(repeat (string :tag "Argument"))
419 :group 'browse-url)
420
421 (defcustom browse-url-filename-alist
422 `(("^/\\(ftp@\\|anonymous@\\)?\\([^:/]+\\):/*" . "ftp://\\2/")
423 ;; The above loses the username to avoid the browser prompting for
424 ;; it in anonymous cases. If it's not anonymous the next regexp
425 ;; applies.
426 ("^/\\([^:@/]+@\\)?\\([^:/]+\\):/*" . "ftp://\\1\\2/")
427 ,@(if (memq system-type '(windows-nt ms-dos))
428 '(("^\\([a-zA-Z]:\\)[\\/]" . "file:///\\1/")
429 ("^[\\/][\\/]+" . "file://")))
430 ("^/+" . "file:///"))
431 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
432 Any substring of a filename matching one of the REGEXPs is replaced by
433 the corresponding STRING using `replace-match', not treating STRING
434 literally. All pairs are applied in the order given. The default
435 value converts ange-ftp/EFS-style file names into ftp URLs and prepends
436 `file:' to any file name beginning with `/'.
437
438 For example, adding to the default a specific translation of an ange-ftp
439 address to an HTTP URL:
440
441 (setq browse-url-filename-alist
442 \\='((\"/webmaster@webserver:/home/www/html/\" .
443 \"http://www.acme.co.uk/\")
444 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:/]+\\):/*\" . \"ftp://\\2/\")
445 (\"^/\\([^:@/]+@\\)?\\([^:/]+\\):/*\" . \"ftp://\\1\\2/\")
446 (\"^/+\" . \"file:/\")))"
447 :type '(repeat (cons :format "%v"
448 (regexp :tag "Regexp")
449 (string :tag "Replacement")))
450 :version "25.1"
451 :group 'browse-url)
452
453 (defcustom browse-url-save-file nil
454 "If non-nil, save the buffer before displaying its file.
455 Used by the `browse-url-of-file' command."
456 :type 'boolean
457 :group 'browse-url)
458
459 (defcustom browse-url-of-file-hook nil
460 "Hook run after `browse-url-of-file' has asked a browser to load a file."
461 :type 'hook
462 :group 'browse-url)
463
464 (defcustom browse-url-CCI-port 3003
465 "Port to access XMosaic via CCI.
466 This can be any number between 1024 and 65535 but must correspond to
467 the value set in the browser."
468 :type 'integer
469 :group 'browse-url)
470
471 (make-obsolete-variable 'browse-url-CCI-port nil "25.1")
472
473 (defcustom browse-url-CCI-host "localhost"
474 "Host to access XMosaic via CCI.
475 This should be the host name of the machine running XMosaic with CCI
476 enabled. The port number should be set in `browse-url-CCI-port'."
477 :type 'string
478 :group 'browse-url)
479
480 (make-obsolete-variable 'browse-url-CCI-host nil "25.1")
481
482 (defvar browse-url-temp-file-name nil)
483 (make-variable-buffer-local 'browse-url-temp-file-name)
484
485 (defcustom browse-url-xterm-program "xterm"
486 "The name of the terminal emulator used by `browse-url-text-xterm'.
487 This might, for instance, be a separate color version of xterm."
488 :type 'string
489 :group 'browse-url)
490
491 (defcustom browse-url-xterm-args nil
492 "A list of strings defining options for `browse-url-xterm-program'.
493 These might set its size, for instance."
494 :type '(repeat (string :tag "Argument"))
495 :group 'browse-url)
496
497 (defcustom browse-url-gnudoit-program "gnudoit"
498 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
499 :type 'string
500 :group 'browse-url)
501
502 (defcustom browse-url-gnudoit-args '("-q")
503 "A list of strings defining options for `browse-url-gnudoit-program'.
504 These might set the port, for instance."
505 :type '(repeat (string :tag "Argument"))
506 :group 'browse-url)
507
508 (defcustom browse-url-generic-program nil
509 "The name of the browser program used by `browse-url-generic'."
510 :type '(choice string (const :tag "None" nil))
511 :group 'browse-url)
512
513 (defcustom browse-url-generic-args nil
514 "A list of strings defining options for `browse-url-generic-program'."
515 :type '(repeat (string :tag "Argument"))
516 :group 'browse-url)
517
518 (defcustom browse-url-temp-dir temporary-file-directory
519 "The name of a directory for browse-url's temporary files.
520 Such files are generated by functions like `browse-url-of-region'.
521 You might want to set this to somewhere with restricted read permissions
522 for privacy's sake."
523 :type 'string
524 :group 'browse-url)
525
526 (defcustom browse-url-netscape-version 3
527 "The version of Netscape you are using.
528 This affects how URL reloading is done; the mechanism changed
529 incompatibly at version 4."
530 :type 'number
531 :group 'browse-url)
532
533 (make-obsolete-variable 'browse-url-netscape-version nil "25.1")
534
535 (defcustom browse-url-text-browser "lynx"
536 "The name of the text browser to invoke."
537 :type 'string
538 :group 'browse-url
539 :version "23.1")
540
541 (defcustom browse-url-text-emacs-args (and (not window-system)
542 '("-show_cursor"))
543 "A list of strings defining options for a text browser in an Emacs buffer.
544
545 The default is none in a window system, otherwise `-show_cursor' to
546 indicate the position of the current link in the absence of
547 highlighting, assuming the normal default for showing the cursor."
548 :type '(repeat (string :tag "Argument"))
549 :version "23.1"
550 :group 'browse-url)
551
552 (defcustom browse-url-text-input-field 'avoid
553 "Action on selecting an existing text browser buffer at an input field.
554 What to do when sending a new URL to an existing text browser buffer in Emacs
555 if the browser cursor is on an input field (in which case the `g' command
556 would be entered as data). Such fields are recognized by the
557 underlines ____. Allowed values: nil: disregard it, `warn': warn the
558 user and don't emit the URL, `avoid': try to avoid the field by moving
559 down (this *won't* always work)."
560 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
561 (const :tag "Disregard" :value nil)
562 (const :tag "Warn, don't emit URL" :value warn))
563 :version "23.1"
564 :group 'browse-url)
565
566 (defcustom browse-url-text-input-attempts 10
567 "How many times to try to move down from a series of text browser input fields."
568 :type 'integer
569 :version "23.1"
570 :group 'browse-url)
571
572 (defcustom browse-url-text-input-delay 0.2
573 "Seconds to wait for a text browser between moves down from an input field."
574 :type 'number
575 :version "23.1"
576 :group 'browse-url)
577
578 (defcustom browse-url-kde-program "kfmclient"
579 "The name by which to invoke the KDE web browser."
580 :type 'string
581 :version "21.1"
582 :group 'browse-url)
583
584 (defcustom browse-url-kde-args '("openURL")
585 "A list of strings defining options for `browse-url-kde-program'."
586 :type '(repeat (string :tag "Argument"))
587 :group 'browse-url)
588
589 (defcustom browse-url-elinks-wrapper '("xterm" "-e")
590 "Wrapper command prepended to the Elinks command-line."
591 :type '(repeat (string :tag "Wrapper"))
592 :group 'browse-url)
593
594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
595 ;; URL encoding
596
597 (defun browse-url-url-encode-chars (text chars)
598 "URL-encode the chars in TEXT that match CHARS.
599 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
600 (let ((encoded-text (copy-sequence text))
601 (s 0))
602 (while (setq s (string-match chars encoded-text s))
603 (setq encoded-text
604 (replace-match (format "%%%X"
605 (string-to-char (match-string 0 encoded-text)))
606 t t encoded-text)
607 s (1+ s)))
608 encoded-text))
609
610 (defun browse-url-encode-url (url)
611 "Escape annoying characters in URL.
612 The annoying characters are those that can mislead a web browser
613 regarding its parameter treatment."
614 ;; FIXME: Is there an actual example of a web browser getting
615 ;; confused? (This used to encode commas, but at least Firefox
616 ;; handles commas correctly and doesn't accept encoded commas.)
617 (browse-url-url-encode-chars url "[\")$] "))
618
619 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620 ;; URL input
621
622 (defun browse-url-url-at-point ()
623 (or (thing-at-point 'url t)
624 ;; assume that the user is pointing at something like gnu.org/gnu
625 (let ((f (thing-at-point 'filename t)))
626 (and f (concat "http://" f)))))
627
628 ;; Having this as a separate function called by the browser-specific
629 ;; functions allows them to be stand-alone commands, making it easier
630 ;; to switch between browsers.
631
632 (defun browse-url-interactive-arg (prompt)
633 "Read a URL from the minibuffer, prompting with PROMPT.
634 If `transient-mark-mode' is non-nil and the mark is active,
635 it defaults to the current region, else to the URL at or before
636 point. If invoked with a mouse button, it moves point to the
637 position clicked before acting.
638
639 This function returns a list (URL NEW-WINDOW-FLAG)
640 for use in `interactive'."
641 (let ((event (elt (this-command-keys) 0)))
642 (and (listp event) (mouse-set-point event)))
643 (list (read-string prompt (or (and transient-mark-mode mark-active
644 ;; rfc2396 Appendix E.
645 (replace-regexp-in-string
646 "[\t\r\f\n ]+" ""
647 (buffer-substring-no-properties
648 (region-beginning) (region-end))))
649 (browse-url-url-at-point)))
650 (not (eq (null browse-url-new-window-flag)
651 (null current-prefix-arg)))))
652
653 ;; called-interactive-p needs to be called at a function's top-level, hence
654 ;; this macro. We use that rather than interactive-p because
655 ;; use in a keyboard macro should not change this behavior.
656 (defmacro browse-url-maybe-new-window (arg)
657 `(if (or noninteractive (not (called-interactively-p 'any)))
658 ,arg
659 browse-url-new-window-flag))
660
661 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
662 ;; Browse current buffer
663
664 ;;;###autoload
665 (defun browse-url-of-file (&optional file)
666 "Ask a WWW browser to display FILE.
667 Display the current buffer's file if FILE is nil or if called
668 interactively. Turn the filename into a URL with function
669 `browse-url-file-url'. Pass the URL to a browser using the
670 `browse-url' function then run `browse-url-of-file-hook'."
671 (interactive)
672 (or file
673 (setq file (buffer-file-name))
674 (error "Current buffer has no file"))
675 (let ((buf (get-file-buffer file)))
676 (if buf
677 (with-current-buffer buf
678 (cond ((not (buffer-modified-p)))
679 (browse-url-save-file (save-buffer))
680 (t (message "%s modified since last save" file))))))
681 (browse-url (browse-url-file-url file))
682 (run-hooks 'browse-url-of-file-hook))
683
684 (defun browse-url-file-url (file)
685 "Return the URL corresponding to FILE.
686 Use variable `browse-url-filename-alist' to map filenames to URLs."
687 (let ((coding (if (equal system-type 'windows-nt)
688 ;; W32 pretends that file names are UTF-8 encoded.
689 'utf-8
690 (and (default-value 'enable-multibyte-characters)
691 (or file-name-coding-system
692 default-file-name-coding-system)))))
693 (if coding (setq file (encode-coding-string file coding))))
694 (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
695 (dolist (map browse-url-filename-alist)
696 (when (and map (string-match (car map) file))
697 (setq file (replace-match (cdr map) t nil file))))
698 file)
699
700 ;;;###autoload
701 (defun browse-url-of-buffer (&optional buffer)
702 "Ask a WWW browser to display BUFFER.
703 Display the current buffer if BUFFER is nil. Display only the
704 currently visible part of BUFFER (from a temporary file) if buffer is
705 narrowed."
706 (interactive)
707 (save-excursion
708 (and buffer (set-buffer buffer))
709 (let ((file-name
710 ;; Ignore real name if restricted
711 (and (not (buffer-narrowed-p))
712 (or buffer-file-name
713 (and (boundp 'dired-directory) dired-directory)))))
714 (or file-name
715 (progn
716 (or browse-url-temp-file-name
717 (setq browse-url-temp-file-name
718 (convert-standard-filename
719 (make-temp-file
720 (expand-file-name "burl" browse-url-temp-dir)
721 nil ".html"))))
722 (setq file-name browse-url-temp-file-name)
723 (write-region (point-min) (point-max) file-name nil 'no-message)))
724 (browse-url-of-file file-name))))
725
726 (defun browse-url-delete-temp-file (&optional temp-file-name)
727 ;; Delete browse-url-temp-file-name from the file system
728 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
729 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
730 (if (and file-name (file-exists-p file-name))
731 (delete-file file-name))))
732
733 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
734
735 (declare-function dired-get-filename "dired"
736 (&optional localp no-error-if-not-filep))
737
738 ;;;###autoload
739 (defun browse-url-of-dired-file ()
740 "In Dired, ask a WWW browser to display the file named on this line."
741 (interactive)
742 (let ((tem (dired-get-filename t t)))
743 (if tem
744 (browse-url-of-file (expand-file-name tem))
745 (error "No file on this line"))))
746
747 ;;;###autoload
748 (defun browse-url-of-region (min max)
749 "Ask a WWW browser to display the current region."
750 (interactive "r")
751 (save-excursion
752 (save-restriction
753 (narrow-to-region min max)
754 (browse-url-of-buffer))))
755
756 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
757 ;; Browser-independent commands
758
759 ;; A generic command to call the current browse-url-browser-function
760
761 ;;;###autoload
762 (defun browse-url (url &rest args)
763 "Ask a WWW browser to load URL.
764 Prompt for a URL, defaulting to the URL at or before point.
765 Invokes a suitable browser function which does the actual job.
766 The variable `browse-url-browser-function' says which browser function to
767 use. If the URL is a mailto: URL, consult `browse-url-mailto-function'
768 first, if that exists.
769
770 The additional ARGS are passed to the browser function. See the doc
771 strings of the actual functions, starting with `browse-url-browser-function',
772 for information about the significance of ARGS (most of the functions
773 ignore it).
774 If ARGS are omitted, the default is to pass `browse-url-new-window-flag'
775 as ARGS."
776 (interactive (browse-url-interactive-arg "URL: "))
777 (unless (called-interactively-p 'interactive)
778 (setq args (or args (list browse-url-new-window-flag))))
779 (when (and url-handler-mode (not (file-name-absolute-p url)))
780 (setq url (expand-file-name url)))
781 (let ((process-environment (copy-sequence process-environment))
782 (function (or (and (string-match "\\`mailto:" url)
783 browse-url-mailto-function)
784 browse-url-browser-function))
785 ;; Ensure that `default-directory' exists and is readable (b#6077).
786 (default-directory (or (unhandled-file-name-directory default-directory)
787 (expand-file-name "~/"))))
788 ;; When connected to various displays, be careful to use the display of
789 ;; the currently selected frame, rather than the original start display,
790 ;; which may not even exist any more.
791 (if (stringp (frame-parameter nil 'display))
792 (setenv "DISPLAY" (frame-parameter nil 'display)))
793 (if (and (consp function)
794 (not (functionp function)))
795 ;; The `function' can be an alist; look down it for first match
796 ;; and apply the function (which might be a lambda).
797 (catch 'done
798 (dolist (bf function)
799 (when (string-match (car bf) url)
800 (apply (cdr bf) url args)
801 (throw 'done t)))
802 (error "No browse-url-browser-function matching URL %s"
803 url))
804 ;; Unbound symbols go down this leg, since void-function from
805 ;; apply is clearer than wrong-type-argument from dolist.
806 (apply function url args))))
807
808 ;;;###autoload
809 (defun browse-url-at-point (&optional arg)
810 "Ask a WWW browser to load the URL at or before point.
811 Variable `browse-url-browser-function' says which browser to use.
812 Optional prefix argument ARG non-nil inverts the value of the option
813 `browse-url-new-window-flag'."
814 (interactive "P")
815 (let ((url (browse-url-url-at-point)))
816 (if url
817 (browse-url url (if arg
818 (not browse-url-new-window-flag)
819 browse-url-new-window-flag))
820 (error "No URL found"))))
821
822 ;;;###autoload
823 (defun browse-url-at-mouse (event)
824 "Ask a WWW browser to load a URL clicked with the mouse.
825 The URL is the one around or before the position of the mouse click
826 but point is not changed. Variable `browse-url-browser-function'
827 says which browser to use."
828 (interactive "e")
829 (save-excursion
830 (mouse-set-point event)
831 ;; This handles browse-url-new-window-flag properly
832 ;; when it gets no arg.
833 (browse-url-at-point)))
834
835 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
836 ;; Browser-specific commands
837
838 ;; --- Default MS-Windows browser ---
839
840 (defvar dos-windows-version)
841 (declare-function w32-shell-execute "w32fns.c") ;; Defined in C.
842
843 (defun browse-url-default-windows-browser (url &optional _new-window)
844 "Invoke the MS-Windows system's default Web browser.
845 The optional NEW-WINDOW argument is not used."
846 (interactive (browse-url-interactive-arg "URL: "))
847 (cond ((eq system-type 'ms-dos)
848 (if dos-windows-version
849 (shell-command (concat "start " (shell-quote-argument url)))
850 (error "Browsing URLs is not supported on this system")))
851 ((eq system-type 'cygwin)
852 (call-process "cygstart" nil nil nil url))
853 (t (w32-shell-execute "open" url))))
854
855 (defun browse-url-default-macosx-browser (url &optional _new-window)
856 "Invoke the MacOS X system's default Web browser.
857 The optional NEW-WINDOW argument is not used"
858 (interactive (browse-url-interactive-arg "URL: "))
859 (start-process (concat "open " url) nil "open" url))
860
861 ;; --- Netscape ---
862
863 (defun browse-url-process-environment ()
864 "Set DISPLAY in the environment to the X display the browser will use.
865 This is either the value of variable `browse-url-browser-display' if
866 non-nil, or the same display as Emacs if different from the current
867 environment, otherwise just use the current environment."
868 (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
869 (if display
870 (cons (concat "DISPLAY=" display) process-environment)
871 process-environment)))
872
873 (defun browse-url-emacs-display ()
874 "Return the X display Emacs is running on.
875 This is nil if the display is the same as the DISPLAY environment variable.
876
877 Actually Emacs could be using several displays; this just returns the
878 one showing the selected frame."
879 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
880 (and (not (equal display (getenv "DISPLAY")))
881 display)))
882
883 (defun browse-url-default-browser (url &rest args)
884 "Find a suitable browser and ask it to load URL.
885 Default to the URL around or before point.
886
887 When called interactively, if variable `browse-url-new-window-flag' is
888 non-nil, load the document in a new window, if possible, otherwise use
889 a random existing one. A non-nil interactive prefix argument reverses
890 the effect of `browse-url-new-window-flag'.
891
892 When called non-interactively, optional second argument ARGS is used
893 instead of `browse-url-new-window-flag'."
894 (apply
895 (cond
896 ((memq system-type '(windows-nt ms-dos cygwin))
897 'browse-url-default-windows-browser)
898 ((memq system-type '(darwin))
899 'browse-url-default-macosx-browser)
900 ((browse-url-can-use-xdg-open) 'browse-url-xdg-open)
901 ;;; ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
902 ((executable-find browse-url-mozilla-program) 'browse-url-mozilla)
903 ((executable-find browse-url-firefox-program) 'browse-url-firefox)
904 ((executable-find browse-url-chromium-program) 'browse-url-chromium)
905 ;;; ((executable-find browse-url-galeon-program) 'browse-url-galeon)
906 ((executable-find browse-url-kde-program) 'browse-url-kde)
907 ;;; ((executable-find browse-url-netscape-program) 'browse-url-netscape)
908 ;;; ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
909 ((executable-find browse-url-conkeror-program) 'browse-url-conkeror)
910 ((executable-find browse-url-xterm-program) 'browse-url-text-xterm)
911 ((locate-library "w3") 'browse-url-w3)
912 (t
913 (lambda (&rest _ignore) (error "No usable browser found"))))
914 url args))
915
916 (defun browse-url-can-use-xdg-open ()
917 "Return non-nil if the \"xdg-open\" program can be used.
918 xdg-open is a desktop utility that calls your preferred web browser.
919 This requires you to be running either Gnome, KDE, Xfce4 or LXDE."
920 (and (getenv "DISPLAY")
921 (executable-find "xdg-open")
922 ;; xdg-open may call gnome-open and that does not wait for its child
923 ;; to finish. This child may then be killed when the parent dies.
924 ;; Use nohup to work around. See bug#7166, bug#8917, bug#9779 and
925 ;; http://lists.gnu.org/archive/html/emacs-devel/2009-07/msg00279.html
926 (executable-find "nohup")
927 (or (getenv "GNOME_DESKTOP_SESSION_ID")
928 ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
929 (condition-case nil
930 (eq 0 (call-process
931 "dbus-send" nil nil nil
932 "--dest=org.gnome.SessionManager"
933 "--print-reply"
934 "/org/gnome/SessionManager"
935 "org.gnome.SessionManager.CanShutdown"))
936 (error nil))
937 (equal (getenv "KDE_FULL_SESSION") "true")
938 (condition-case nil
939 (eq 0 (call-process
940 "/bin/sh" nil nil nil
941 "-c"
942 ;; FIXME use string-match rather than grep.
943 "xprop -root _DT_SAVE_MODE|grep xfce4"))
944 (error nil))
945 (member (getenv "DESKTOP_SESSION") '("LXDE" "Lubuntu"))
946 (equal (getenv "XDG_CURRENT_DESKTOP") "LXDE"))))
947
948
949 ;;;###autoload
950 (defun browse-url-xdg-open (url &optional ignored)
951 "Pass the specified URL to the \"xdg-open\" command.
952 xdg-open is a desktop utility that calls your preferred web browser.
953 The optional argument IGNORED is not used."
954 (interactive (browse-url-interactive-arg "URL: "))
955 (call-process "xdg-open" nil 0 nil url))
956
957 ;;;###autoload
958 (defun browse-url-netscape (url &optional new-window)
959 "Ask the Netscape WWW browser to load URL.
960 Default to the URL around or before point. The strings in variable
961 `browse-url-netscape-arguments' are also passed to Netscape.
962
963 When called interactively, if variable `browse-url-new-window-flag' is
964 non-nil, load the document in a new Netscape window, otherwise use a
965 random existing one. A non-nil interactive prefix argument reverses
966 the effect of `browse-url-new-window-flag'.
967
968 If `browse-url-netscape-new-window-is-tab' is non-nil, then
969 whenever a document would otherwise be loaded in a new window, it
970 is loaded in a new tab in an existing window instead.
971
972 When called non-interactively, optional second argument NEW-WINDOW is
973 used instead of `browse-url-new-window-flag'."
974 (declare (obsolete nil "25.1"))
975 (interactive (browse-url-interactive-arg "URL: "))
976 (setq url (browse-url-encode-url url))
977 (let* ((process-environment (browse-url-process-environment))
978 (process
979 (apply 'start-process
980 (concat "netscape " url) nil
981 browse-url-netscape-program
982 (append
983 browse-url-netscape-arguments
984 (if (eq window-system 'w32)
985 (list url)
986 (append
987 (if new-window '("-noraise"))
988 (list "-remote"
989 (concat "openURL(" url
990 (if (browse-url-maybe-new-window
991 new-window)
992 (if browse-url-netscape-new-window-is-tab
993 ",new-tab"
994 ",new-window"))
995 ")"))))))))
996 (set-process-sentinel process
997 `(lambda (process change)
998 (browse-url-netscape-sentinel process ,url)))))
999
1000 (defun browse-url-netscape-sentinel (process url)
1001 "Handle a change to the process communicating with Netscape."
1002 (declare (obsolete nil "25.1"))
1003 (or (eq (process-exit-status process) 0)
1004 (let* ((process-environment (browse-url-process-environment)))
1005 ;; Netscape not running - start it
1006 (message "Starting %s..." browse-url-netscape-program)
1007 (apply 'start-process (concat "netscape" url) nil
1008 browse-url-netscape-program
1009 (append browse-url-netscape-startup-arguments (list url))))))
1010
1011 (defun browse-url-netscape-reload ()
1012 "Ask Netscape to reload its current document.
1013 How depends on `browse-url-netscape-version'."
1014 (declare (obsolete nil "25.1"))
1015 (interactive)
1016 ;; Backwards incompatibility reported by
1017 ;; <peter.kruse@psychologie.uni-regensburg.de>.
1018 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
1019 "xfeDoCommand(reload)"
1020 "reload")))
1021
1022 (defun browse-url-netscape-send (command)
1023 "Send a remote control command to Netscape."
1024 (declare (obsolete nil "25.1"))
1025 (let* ((process-environment (browse-url-process-environment)))
1026 (apply 'start-process "netscape" nil
1027 browse-url-netscape-program
1028 (append browse-url-netscape-arguments
1029 (list "-remote" command)))))
1030
1031 ;;;###autoload
1032 (defun browse-url-mozilla (url &optional new-window)
1033 "Ask the Mozilla WWW browser to load URL.
1034 Default to the URL around or before point. The strings in variable
1035 `browse-url-mozilla-arguments' are also passed to Mozilla.
1036
1037 When called interactively, if variable `browse-url-new-window-flag' is
1038 non-nil, load the document in a new Mozilla window, otherwise use a
1039 random existing one. A non-nil interactive prefix argument reverses
1040 the effect of `browse-url-new-window-flag'.
1041
1042 If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
1043 document would otherwise be loaded in a new window, it is loaded in a
1044 new tab in an existing window instead.
1045
1046 When called non-interactively, optional second argument NEW-WINDOW is
1047 used instead of `browse-url-new-window-flag'."
1048 (interactive (browse-url-interactive-arg "URL: "))
1049 (setq url (browse-url-encode-url url))
1050 (let* ((process-environment (browse-url-process-environment))
1051 (process
1052 (apply 'start-process
1053 (concat "mozilla " url) nil
1054 browse-url-mozilla-program
1055 (append
1056 browse-url-mozilla-arguments
1057 (list "-remote"
1058 (concat "openURL("
1059 url
1060 (if (browse-url-maybe-new-window
1061 new-window)
1062 (if browse-url-mozilla-new-window-is-tab
1063 ",new-tab"
1064 ",new-window"))
1065 ")"))))))
1066 (set-process-sentinel process
1067 `(lambda (process change)
1068 (browse-url-mozilla-sentinel process ,url)))))
1069
1070 (defun browse-url-mozilla-sentinel (process url)
1071 "Handle a change to the process communicating with Mozilla."
1072 (or (eq (process-exit-status process) 0)
1073 (let* ((process-environment (browse-url-process-environment)))
1074 ;; Mozilla is not running - start it
1075 (message "Starting %s..." browse-url-mozilla-program)
1076 (apply 'start-process (concat "mozilla " url) nil
1077 browse-url-mozilla-program
1078 (append browse-url-mozilla-startup-arguments (list url))))))
1079
1080 ;;;###autoload
1081 (defun browse-url-firefox (url &optional new-window)
1082 "Ask the Firefox WWW browser to load URL.
1083 Defaults to the URL around or before point. Passes the strings
1084 in the variable `browse-url-firefox-arguments' to Firefox.
1085
1086 Interactively, if the variable `browse-url-new-window-flag' is non-nil,
1087 loads the document in a new Firefox window. A non-nil prefix argument
1088 reverses the effect of `browse-url-new-window-flag'.
1089
1090 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1091 whenever a document would otherwise be loaded in a new window, it
1092 is loaded in a new tab in an existing window instead.
1093
1094 Non-interactively, this uses the optional second argument NEW-WINDOW
1095 instead of `browse-url-new-window-flag'."
1096 (interactive (browse-url-interactive-arg "URL: "))
1097 (setq url (browse-url-encode-url url))
1098 (let* ((process-environment (browse-url-process-environment)))
1099 (apply 'start-process
1100 (concat "firefox " url) nil
1101 browse-url-firefox-program
1102 (append
1103 browse-url-firefox-arguments
1104 (if (browse-url-maybe-new-window new-window)
1105 (if browse-url-firefox-new-window-is-tab
1106 '("-new-tab")
1107 '("-new-window")))
1108 (list url)))))
1109
1110 ;;;###autoload
1111 (defun browse-url-chromium (url &optional _new-window)
1112 "Ask the Chromium WWW browser to load URL.
1113 Default to the URL around or before point. The strings in
1114 variable `browse-url-chromium-arguments' are also passed to
1115 Chromium.
1116 The optional argument NEW-WINDOW is not used."
1117 (interactive (browse-url-interactive-arg "URL: "))
1118 (setq url (browse-url-encode-url url))
1119 (let* ((process-environment (browse-url-process-environment)))
1120 (apply 'start-process
1121 (concat "chromium " url) nil
1122 browse-url-chromium-program
1123 (append
1124 browse-url-chromium-arguments
1125 (list url)))))
1126
1127 ;;;###autoload
1128 (defun browse-url-galeon (url &optional new-window)
1129 "Ask the Galeon WWW browser to load URL.
1130 Default to the URL around or before point. The strings in variable
1131 `browse-url-galeon-arguments' are also passed to Galeon.
1132
1133 When called interactively, if variable `browse-url-new-window-flag' is
1134 non-nil, load the document in a new Galeon window, otherwise use a
1135 random existing one. A non-nil interactive prefix argument reverses
1136 the effect of `browse-url-new-window-flag'.
1137
1138 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1139 document would otherwise be loaded in a new window, it is loaded in a
1140 new tab in an existing window instead.
1141
1142 When called non-interactively, optional second argument NEW-WINDOW is
1143 used instead of `browse-url-new-window-flag'."
1144 (declare (obsolete nil "25.1"))
1145 (interactive (browse-url-interactive-arg "URL: "))
1146 (setq url (browse-url-encode-url url))
1147 (let* ((process-environment (browse-url-process-environment))
1148 (process (apply 'start-process
1149 (concat "galeon " url)
1150 nil
1151 browse-url-galeon-program
1152 (append
1153 browse-url-galeon-arguments
1154 (if (browse-url-maybe-new-window new-window)
1155 (if browse-url-galeon-new-window-is-tab
1156 '("--new-tab")
1157 '("--new-window" "--noraise"))
1158 '("--existing"))
1159 (list url)))))
1160 (set-process-sentinel process
1161 `(lambda (process change)
1162 (browse-url-galeon-sentinel process ,url)))))
1163
1164 (defun browse-url-galeon-sentinel (process url)
1165 "Handle a change to the process communicating with Galeon."
1166 (declare (obsolete nil "25.1"))
1167 (or (eq (process-exit-status process) 0)
1168 (let* ((process-environment (browse-url-process-environment)))
1169 ;; Galeon is not running - start it
1170 (message "Starting %s..." browse-url-galeon-program)
1171 (apply 'start-process (concat "galeon " url) nil
1172 browse-url-galeon-program
1173 (append browse-url-galeon-startup-arguments (list url))))))
1174
1175 (defun browse-url-epiphany (url &optional new-window)
1176 "Ask the Epiphany WWW browser to load URL.
1177 Default to the URL around or before point. The strings in variable
1178 `browse-url-galeon-arguments' are also passed to Epiphany.
1179
1180 When called interactively, if variable `browse-url-new-window-flag' is
1181 non-nil, load the document in a new Epiphany window, otherwise use a
1182 random existing one. A non-nil interactive prefix argument reverses
1183 the effect of `browse-url-new-window-flag'.
1184
1185 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1186 document would otherwise be loaded in a new window, it is loaded in a
1187 new tab in an existing window instead.
1188
1189 When called non-interactively, optional second argument NEW-WINDOW is
1190 used instead of `browse-url-new-window-flag'."
1191 (interactive (browse-url-interactive-arg "URL: "))
1192 (setq url (browse-url-encode-url url))
1193 (let* ((process-environment (browse-url-process-environment))
1194 (process (apply 'start-process
1195 (concat "epiphany " url)
1196 nil
1197 browse-url-epiphany-program
1198 (append
1199 browse-url-epiphany-arguments
1200 (if (browse-url-maybe-new-window new-window)
1201 (if browse-url-epiphany-new-window-is-tab
1202 '("--new-tab")
1203 '("--new-window" "--noraise"))
1204 '("--existing"))
1205 (list url)))))
1206 (set-process-sentinel process
1207 `(lambda (process change)
1208 (browse-url-epiphany-sentinel process ,url)))))
1209
1210 (defun browse-url-epiphany-sentinel (process url)
1211 "Handle a change to the process communicating with Epiphany."
1212 (or (eq (process-exit-status process) 0)
1213 (let* ((process-environment (browse-url-process-environment)))
1214 ;; Epiphany is not running - start it
1215 (message "Starting %s..." browse-url-epiphany-program)
1216 (apply 'start-process (concat "epiphany " url) nil
1217 browse-url-epiphany-program
1218 (append browse-url-epiphany-startup-arguments (list url))))))
1219
1220 (defvar url-handler-regexp)
1221
1222 ;;;###autoload
1223 (defun browse-url-emacs (url &optional _new-window)
1224 "Ask Emacs to load URL into a buffer and show it in another window."
1225 (interactive (browse-url-interactive-arg "URL: "))
1226 (require 'url-handlers)
1227 (let ((file-name-handler-alist
1228 (cons (cons url-handler-regexp 'url-file-handler)
1229 file-name-handler-alist)))
1230 ;; Ignore `new-window': with all other browsers the URL is always shown
1231 ;; in another window than the current Emacs one since it's shown in
1232 ;; another application's window.
1233 ;; (if new-window (find-file-other-window url) (find-file url))
1234 (find-file-other-window url)))
1235
1236 ;;;###autoload
1237 (defun browse-url-gnome-moz (url &optional new-window)
1238 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1239 Default to the URL around or before point. The strings in variable
1240 `browse-url-gnome-moz-arguments' are also passed.
1241
1242 When called interactively, if variable `browse-url-new-window-flag' is
1243 non-nil, load the document in a new browser window, otherwise use an
1244 existing one. A non-nil interactive prefix argument reverses the
1245 effect of `browse-url-new-window-flag'.
1246
1247 When called non-interactively, optional second argument NEW-WINDOW is
1248 used instead of `browse-url-new-window-flag'."
1249 (declare (obsolete nil "25.1"))
1250 (interactive (browse-url-interactive-arg "URL: "))
1251 (apply 'start-process (concat "gnome-moz-remote " url)
1252 nil
1253 browse-url-gnome-moz-program
1254 (append
1255 browse-url-gnome-moz-arguments
1256 (if (browse-url-maybe-new-window new-window)
1257 '("--newwin"))
1258 (list "--raise" url))))
1259
1260 ;; --- Mosaic ---
1261
1262 ;;;###autoload
1263 (defun browse-url-mosaic (url &optional new-window)
1264 "Ask the XMosaic WWW browser to load URL.
1265
1266 Default to the URL around or before point. The strings in variable
1267 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1268 program is invoked according to the variable
1269 `browse-url-mosaic-program'.
1270
1271 When called interactively, if variable `browse-url-new-window-flag' is
1272 non-nil, load the document in a new Mosaic window, otherwise use a
1273 random existing one. A non-nil interactive prefix argument reverses
1274 the effect of `browse-url-new-window-flag'.
1275
1276 When called non-interactively, optional second argument NEW-WINDOW is
1277 used instead of `browse-url-new-window-flag'."
1278 (declare (obsolete nil "25.1"))
1279 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1280 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
1281 pid)
1282 (if (file-readable-p pidfile)
1283 (with-temp-buffer
1284 (insert-file-contents pidfile)
1285 (setq pid (read (current-buffer)))))
1286 (if (and (integerp pid) (zerop (signal-process pid 0))) ; Mosaic running
1287 (progn
1288 (with-temp-buffer
1289 (insert (if (browse-url-maybe-new-window new-window)
1290 "newwin\n"
1291 "goto\n")
1292 url "\n")
1293 (with-file-modes ?\700
1294 (if (file-exists-p
1295 (setq pidfile (format "/tmp/Mosaic.%d" pid)))
1296 (delete-file pidfile))
1297 ;; http://debbugs.gnu.org/17428. Use O_EXCL.
1298 (write-region nil nil pidfile nil 'silent nil 'excl)))
1299 ;; Send signal SIGUSR to Mosaic
1300 (message "Signaling Mosaic...")
1301 (signal-process pid 'SIGUSR1)
1302 ;; Or you could try:
1303 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1304 (message "Signaling Mosaic...done"))
1305 ;; Mosaic not running - start it
1306 (message "Starting %s..." browse-url-mosaic-program)
1307 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1308 (append browse-url-mosaic-arguments (list url)))
1309 (message "Starting %s...done" browse-url-mosaic-program))))
1310
1311 ;; --- Mosaic using CCI ---
1312
1313 ;;;###autoload
1314 (defun browse-url-cci (url &optional new-window)
1315 "Ask the XMosaic WWW browser to load URL.
1316 Default to the URL around or before point.
1317
1318 This function only works for XMosaic version 2.5 or later. You must
1319 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1320 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1321
1322 When called interactively, if variable `browse-url-new-window-flag' is
1323 non-nil, load the document in a new browser window, otherwise use a
1324 random existing one. A non-nil interactive prefix argument reverses
1325 the effect of `browse-url-new-window-flag'.
1326
1327 When called non-interactively, optional second argument NEW-WINDOW is
1328 used instead of `browse-url-new-window-flag'."
1329 (declare (obsolete nil "25.1"))
1330 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1331 (open-network-stream "browse-url" " *browse-url*"
1332 browse-url-CCI-host browse-url-CCI-port)
1333 ;; Todo: start browser if fails
1334 (process-send-string "browse-url"
1335 (concat "get url (" url ") output "
1336 (if (browse-url-maybe-new-window new-window)
1337 "new"
1338 "current")
1339 "\r\n"))
1340 (process-send-string "browse-url" "disconnect\r\n")
1341 (delete-process "browse-url"))
1342
1343 ;; --- Conkeror ---
1344 ;;;###autoload
1345 (defun browse-url-conkeror (url &optional new-window)
1346 "Ask the Conkeror WWW browser to load URL.
1347 Default to the URL around or before point. Also pass the strings
1348 in the variable `browse-url-conkeror-arguments' to Conkeror.
1349
1350 When called interactively, if variable
1351 `browse-url-new-window-flag' is non-nil, load the document in a
1352 new Conkeror window, otherwise use a random existing one. A
1353 non-nil interactive prefix argument reverses the effect of
1354 `browse-url-new-window-flag'.
1355
1356 If variable `browse-url-conkeror-new-window-is-buffer' is
1357 non-nil, then whenever a document would otherwise be loaded in a
1358 new window, load it in a new buffer in an existing window instead.
1359
1360 When called non-interactively, use optional second argument
1361 NEW-WINDOW instead of `browse-url-new-window-flag'."
1362 (interactive (browse-url-interactive-arg "URL: "))
1363 (setq url (browse-url-encode-url url))
1364 (let* ((process-environment (browse-url-process-environment)))
1365 (apply 'start-process (format "conkeror %s" url)
1366 nil
1367 browse-url-conkeror-program
1368 (append
1369 browse-url-conkeror-arguments
1370 (list
1371 "-e"
1372 (format "load_url_in_new_%s('%s')"
1373 (if (browse-url-maybe-new-window new-window)
1374 (if browse-url-conkeror-new-window-is-buffer
1375 "buffer"
1376 "window")
1377 "buffer")
1378 url))))))
1379 ;; --- W3 ---
1380
1381 ;; External.
1382 (declare-function w3-fetch-other-window "ext:w3m" (&optional url))
1383 (declare-function w3-fetch "ext:w3m" (&optional url target))
1384
1385 ;;;###autoload
1386 (defun browse-url-w3 (url &optional new-window)
1387 "Ask the w3 WWW browser to load URL.
1388 Default to the URL around or before point.
1389
1390 When called interactively, if variable `browse-url-new-window-flag' is
1391 non-nil, load the document in a new window. A non-nil interactive
1392 prefix argument reverses the effect of `browse-url-new-window-flag'.
1393
1394 When called non-interactively, optional second argument NEW-WINDOW is
1395 used instead of `browse-url-new-window-flag'."
1396 (interactive (browse-url-interactive-arg "W3 URL: "))
1397 (require 'w3) ; w3-fetch-other-window not autoloaded
1398 (if (browse-url-maybe-new-window new-window)
1399 (w3-fetch-other-window url)
1400 (w3-fetch url)))
1401
1402 ;;;###autoload
1403 (defun browse-url-w3-gnudoit (url &optional _new-window)
1404 ;; new-window ignored
1405 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1406 The `browse-url-gnudoit-program' program is used with options given by
1407 `browse-url-gnudoit-args'. Default to the URL around or before point."
1408 (declare (obsolete nil "25.1"))
1409 (interactive (browse-url-interactive-arg "W3 URL: "))
1410 (apply 'start-process (concat "gnudoit:" url) nil
1411 browse-url-gnudoit-program
1412 (append browse-url-gnudoit-args
1413 (list (concat "(w3-fetch \"" url "\")")
1414 "(raise-frame)"))))
1415
1416 ;; --- Lynx in an xterm ---
1417
1418 ;;;###autoload
1419 (defun browse-url-text-xterm (url &optional _new-window)
1420 ;; new-window ignored
1421 "Ask a text browser to load URL.
1422 URL defaults to the URL around or before point.
1423 This runs the text browser specified by `browse-url-text-browser'.
1424 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1425 with possible additional arguments `browse-url-xterm-args'.
1426 The optional argument NEW-WINDOW is not used."
1427 (interactive (browse-url-interactive-arg "Text browser URL: "))
1428 (apply #'start-process `(,(concat browse-url-text-browser url)
1429 nil ,browse-url-xterm-program
1430 ,@browse-url-xterm-args "-e" ,browse-url-text-browser
1431 ,url)))
1432
1433 ;; --- Lynx in an Emacs "term" window ---
1434
1435 (declare-function term-char-mode "term" ())
1436 (declare-function term-send-down "term" ())
1437 (declare-function term-send-string "term" (proc str))
1438
1439 ;;;###autoload
1440 (defun browse-url-text-emacs (url &optional new-buffer)
1441 "Ask a text browser to load URL.
1442 URL defaults to the URL around or before point.
1443 This runs the text browser specified by `browse-url-text-browser'.
1444 With a prefix argument, it runs a new browser process in a new buffer.
1445
1446 When called interactively, if variable `browse-url-new-window-flag' is
1447 non-nil, load the document in a new browser process in a new term window,
1448 otherwise use any existing one. A non-nil interactive prefix argument
1449 reverses the effect of `browse-url-new-window-flag'.
1450
1451 When called non-interactively, optional second argument NEW-WINDOW is
1452 used instead of `browse-url-new-window-flag'."
1453 (interactive (browse-url-interactive-arg "Text browser URL: "))
1454 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
1455 ;; (term-term-name "vt100") ; ??
1456 (buf (get-buffer "*text browser*"))
1457 (proc (and buf (get-buffer-process buf)))
1458 (n browse-url-text-input-attempts))
1459 (require 'term)
1460 (if (and (browse-url-maybe-new-window new-buffer) buf)
1461 ;; Rename away the OLD buffer. This isn't very polite, but
1462 ;; term insists on working in a buffer named *lynx* and would
1463 ;; choke on *lynx*<1>
1464 (progn (set-buffer buf)
1465 (rename-uniquely)))
1466 (if (or (browse-url-maybe-new-window new-buffer)
1467 (not buf)
1468 (not proc)
1469 (not (memq (process-status proc) '(run stop))))
1470 ;; start a new text browser
1471 (progn
1472 (setq buf
1473 (apply #'make-term
1474 `(,browse-url-text-browser
1475 ,browse-url-text-browser
1476 nil ,@browse-url-text-emacs-args
1477 ,url)))
1478 (switch-to-buffer buf)
1479 (term-char-mode)
1480 (set-process-sentinel
1481 (get-buffer-process buf)
1482 ;; Don't leave around a dead one (especially because of its
1483 ;; munged keymap.)
1484 (lambda (process _event)
1485 (if (not (memq (process-status process) '(run stop)))
1486 (let ((buf (process-buffer process)))
1487 (if buf (kill-buffer buf)))))))
1488 ;; Send the url to the text browser in the old buffer
1489 (let ((win (get-buffer-window buf t)))
1490 (if win
1491 (select-window win)
1492 (switch-to-buffer buf)))
1493 (if (eq (following-char) ?_)
1494 (cond ((eq browse-url-text-input-field 'warn)
1495 (error "Please move out of the input field first"))
1496 ((eq browse-url-text-input-field 'avoid)
1497 (while (and (eq (following-char) ?_) (> n 0))
1498 (term-send-down) ; down arrow
1499 (sit-for browse-url-text-input-delay))
1500 (if (eq (following-char) ?_)
1501 (error "Cannot move out of the input field, sorry")))))
1502 (term-send-string proc (concat "g" ; goto
1503 "\C-u" ; kill default url
1504 url
1505 "\r")))))
1506
1507 ;; --- mailto ---
1508
1509 (autoload 'rfc2368-parse-mailto-url "rfc2368")
1510
1511 ;;;###autoload
1512 (defun browse-url-mail (url &optional new-window)
1513 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1514 Default to using the mailto: URL around or before point as the
1515 recipient's address. Supplying a non-nil interactive prefix argument
1516 will cause the mail to be composed in another window rather than the
1517 current one.
1518
1519 When called interactively, if variable `browse-url-new-window-flag' is
1520 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1521 non-nil interactive prefix argument reverses the effect of
1522 `browse-url-new-window-flag'.
1523
1524 When called non-interactively, optional second argument NEW-WINDOW is
1525 used instead of `browse-url-new-window-flag'."
1526 (interactive (browse-url-interactive-arg "Mailto URL: "))
1527 (save-excursion
1528 (let* ((alist (rfc2368-parse-mailto-url url))
1529 (to (assoc "To" alist))
1530 (subject (assoc "Subject" alist))
1531 (body (assoc "Body" alist))
1532 (rest (delq to (delq subject (delq body alist))))
1533 (to (cdr to))
1534 (subject (cdr subject))
1535 (body (cdr body))
1536 (mail-citation-hook (unless body mail-citation-hook)))
1537 (if (browse-url-maybe-new-window new-window)
1538 (compose-mail-other-window to subject rest nil
1539 (list 'insert-buffer (current-buffer)))
1540 (compose-mail to subject rest nil nil
1541 (list 'insert-buffer (current-buffer))))
1542 (when body
1543 (goto-char (point-min))
1544 (unless (or (search-forward (concat "\n" mail-header-separator "\n")
1545 nil 'move)
1546 (bolp))
1547 (insert "\n"))
1548 (goto-char (prog1
1549 (point)
1550 (insert (replace-regexp-in-string "\r\n" "\n" body))
1551 (unless (bolp)
1552 (insert "\n"))))))))
1553
1554 ;; --- Random browser ---
1555
1556 ;;;###autoload
1557 (defun browse-url-generic (url &optional _new-window)
1558 ;; new-window ignored
1559 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1560 Default to the URL around or before point. A fresh copy of the
1561 browser is started up in a new process with possible additional arguments
1562 `browse-url-generic-args'. This is appropriate for browsers which
1563 don't offer a form of remote control."
1564 (interactive (browse-url-interactive-arg "URL: "))
1565 (if (not browse-url-generic-program)
1566 (error "No browser defined (`browse-url-generic-program')"))
1567 (apply 'call-process browse-url-generic-program nil
1568 0 nil
1569 (append browse-url-generic-args (list url))))
1570
1571 ;;;###autoload
1572 (defun browse-url-kde (url &optional _new-window)
1573 "Ask the KDE WWW browser to load URL.
1574 Default to the URL around or before point.
1575 The optional argument NEW-WINDOW is not used."
1576 (interactive (browse-url-interactive-arg "KDE URL: "))
1577 (message "Sending URL to KDE...")
1578 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
1579 (append browse-url-kde-args (list url))))
1580
1581 (defun browse-url-elinks-new-window (url)
1582 "Ask the Elinks WWW browser to load URL in a new window."
1583 (let ((process-environment (browse-url-process-environment)))
1584 (apply #'start-process
1585 (append (list (concat "elinks:" url)
1586 nil)
1587 browse-url-elinks-wrapper
1588 (list "elinks" url)))))
1589
1590 ;;;###autoload
1591 (defun browse-url-elinks (url &optional new-window)
1592 "Ask the Elinks WWW browser to load URL.
1593 Default to the URL around the point.
1594
1595 The document is loaded in a new tab of a running Elinks or, if
1596 none yet running, a newly started instance.
1597
1598 The Elinks command will be prepended by the program+arguments
1599 from `browse-url-elinks-wrapper'."
1600 (interactive (browse-url-interactive-arg "URL: "))
1601 (setq url (browse-url-encode-url url))
1602 (if new-window
1603 (browse-url-elinks-new-window url)
1604 (let ((process-environment (browse-url-process-environment))
1605 (elinks-ping-process (start-process "elinks-ping" nil
1606 "elinks" "-remote" "ping()")))
1607 (set-process-sentinel elinks-ping-process
1608 `(lambda (process change)
1609 (browse-url-elinks-sentinel process ,url))))))
1610
1611 (defun browse-url-elinks-sentinel (process url)
1612 "Determines if Elinks is running or a new one has to be started."
1613 ;; Try to determine if an instance is running or if we have to
1614 ;; create a new one.
1615 (pcase (process-exit-status process)
1616 (5
1617 ;; No instance, start a new one.
1618 (browse-url-elinks-new-window url))
1619 (0
1620 ;; Found an instance, open URL in new tab.
1621 (let ((process-environment (browse-url-process-environment)))
1622 (start-process (concat "elinks:" url) nil
1623 "elinks" "-remote"
1624 (concat "openURL(\"" url "\",new-tab)"))))
1625 (exit-status
1626 (error "Unrecognized exit-code %d of process `elinks'"
1627 exit-status))))
1628
1629 (provide 'browse-url)
1630
1631 ;;; browse-url.el ends here