* mail/rmail.el (rmail-show-message-1): When displaying a mime message,
[emacs.git] / lisp / net / browse-url.el
blob933d292ddfde28e41b131d7a027e420ceeecd2e8
1 ;;; browse-url.el --- pass a URL to a WWW browser
3 ;; Copyright (C) 1995-2015 Free Software Foundation, Inc.
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
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
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:
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)
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.
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.
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;; Usage
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
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.)
74 ;; To display the current buffer in a web browser:
75 ;; M-x browse-url-of-buffer RET
77 ;; To display the current region in a web browser:
78 ;; M-x browse-url-of-region RET
80 ;; In Dired, to display the file named on the current line:
81 ;; M-x browse-url-of-dired-file RET
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 ;; Customization (~/.emacs)
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'.
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)))
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'.
106 ;; Gnus provides a standard feature to activate URLs in article
107 ;; buffers for invocation of browse-url.
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))
113 ;; To always save modified buffers before displaying the file in a browser:
114 ;; (setq browse-url-save-file t)
116 ;; To invoke different browsers for different URLs:
117 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
118 ;; ("." . browse-url-firefox)))
120 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121 ;;; Code:
123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 ;; Variables
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)
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.
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)
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)
185 (defcustom browse-url-netscape-program "netscape"
186 ;; Info about netscape-remote from Karl Berry.
187 "The name by which to invoke Netscape.
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)
197 (make-obsolete-variable 'browse-url-netscape-program nil "25.1")
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)
204 (make-obsolete-variable 'browse-url-netscape-arguments nil "25.1")
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"))
212 :group 'browse-url)
214 (make-obsolete-variable 'browse-url-netscape-startup-arguments nil "25.1")
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)
221 (defcustom browse-url-mozilla-program "mozilla"
222 "The name by which to invoke Mozilla."
223 :type 'string
224 :group 'browse-url)
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)
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)
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)
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)
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)
259 (make-obsolete-variable 'browse-url-firefox-startup-arguments
260 "it no longer has any effect." "24.5")
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)
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)
278 (defcustom browse-url-galeon-program "galeon"
279 "The name by which to invoke Galeon."
280 :type 'string
281 :group 'browse-url)
283 (make-obsolete-variable 'browse-url-galeon-program nil "25.1")
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)
290 (make-obsolete-variable 'browse-url-galeon-arguments nil "25.1")
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)
299 (make-obsolete-variable 'browse-url-galeon-startup-arguments nil "25.1")
301 (defcustom browse-url-epiphany-program "epiphany"
302 "The name by which to invoke Epiphany."
303 :type 'string
304 :group 'browse-url)
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)
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)
318 ;; GNOME means of invoking either Mozilla or Netscape.
319 (defvar browse-url-gnome-moz-program "gnome-moz-remote")
321 (make-obsolete-variable 'browse-url-gnome-moz-program nil "25.1")
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)
329 (make-obsolete-variable 'browse-url-gnome-moz-arguments nil "25.1")
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)
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.
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)
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)
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)
362 (make-obsolete-variable 'browse-url-galeon-new-window-is-tab nil "25.1")
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)
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)
379 (make-obsolete-variable 'browse-url-netscape-new-window-is-tab nil "25.1")
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)
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)
394 (make-obsolete-variable 'browse-url-mosaic-program nil "25.1")
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)
401 (make-obsolete-variable 'browse-url-mosaic-arguments nil "25.1")
403 (defcustom browse-url-mosaic-pidfile "~/.mosaicpid"
404 "The name of the pidfile created by Mosaic."
405 :type 'string
406 :group 'browse-url)
408 (make-obsolete-variable 'browse-url-mosaic-pidfile nil "25.1")
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)
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)
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 `/'.
438 For example, adding to the default a specific translation of an ange-ftp
439 address to an HTTP URL:
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 "23.1"
451 :group 'browse-url)
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)
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)
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)
471 (make-obsolete-variable 'browse-url-CCI-port nil "25.1")
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)
480 (make-obsolete-variable 'browse-url-CCI-host nil "25.1")
482 (defvar browse-url-temp-file-name nil)
483 (make-variable-buffer-local 'browse-url-temp-file-name)
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)
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)
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)
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)
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)
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)
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)
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)
533 (make-obsolete-variable 'browse-url-netscape-version nil "25.1")
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")
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.
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)
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)
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)
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)
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)
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)
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)
594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
595 ;; URL encoding
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))
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 "[\")$] "))
619 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620 ;; URL input
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)))))
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.
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.
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)))))
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))
661 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
662 ;; Browse current buffer
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))
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)
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))))
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))))
733 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
735 (declare-function dired-get-filename "dired"
736 (&optional localp no-error-if-not-filep))
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"))))
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))))
756 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
757 ;; Browser-independent commands
759 ;; A generic command to call the current browse-url-browser-function
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. Variable
765 `browse-url-browser-function' says which browser to use.
766 If the URL is a mailto: URL, consult `browse-url-mailto-function'
767 first, if that exists."
768 (interactive (browse-url-interactive-arg "URL: "))
769 (unless (called-interactively-p 'interactive)
770 (setq args (or args (list browse-url-new-window-flag))))
771 (when (and url-handler-mode (not (file-name-absolute-p url)))
772 (setq url (expand-file-name url)))
773 (let ((process-environment (copy-sequence process-environment))
774 (function (or (and (string-match "\\`mailto:" url)
775 browse-url-mailto-function)
776 browse-url-browser-function))
777 ;; Ensure that `default-directory' exists and is readable (b#6077).
778 (default-directory (or (unhandled-file-name-directory default-directory)
779 (expand-file-name "~/"))))
780 ;; When connected to various displays, be careful to use the display of
781 ;; the currently selected frame, rather than the original start display,
782 ;; which may not even exist any more.
783 (if (stringp (frame-parameter nil 'display))
784 (setenv "DISPLAY" (frame-parameter nil 'display)))
785 (if (and (consp function)
786 (not (functionp function)))
787 ;; The `function' can be an alist; look down it for first match
788 ;; and apply the function (which might be a lambda).
789 (catch 'done
790 (dolist (bf function)
791 (when (string-match (car bf) url)
792 (apply (cdr bf) url args)
793 (throw 'done t)))
794 (error "No browse-url-browser-function matching URL %s"
795 url))
796 ;; Unbound symbols go down this leg, since void-function from
797 ;; apply is clearer than wrong-type-argument from dolist.
798 (apply function url args))))
800 ;;;###autoload
801 (defun browse-url-at-point (&optional arg)
802 "Ask a WWW browser to load the URL at or before point.
803 Variable `browse-url-browser-function' says which browser to use."
804 (interactive "P")
805 (let ((url (browse-url-url-at-point)))
806 (if url
807 (browse-url url (if arg
808 (not browse-url-new-window-flag)
809 browse-url-new-window-flag))
810 (error "No URL found"))))
812 ;;;###autoload
813 (defun browse-url-at-mouse (event)
814 "Ask a WWW browser to load a URL clicked with the mouse.
815 The URL is the one around or before the position of the mouse click
816 but point is not changed. Variable `browse-url-browser-function'
817 says which browser to use."
818 (interactive "e")
819 (save-excursion
820 (mouse-set-point event)
821 ;; This handles browse-url-new-window-flag properly
822 ;; when it gets no arg.
823 (browse-url-at-point)))
825 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
826 ;; Browser-specific commands
828 ;; --- Default MS-Windows browser ---
830 (defvar dos-windows-version)
831 (declare-function w32-shell-execute "w32fns.c") ;; Defined in C.
833 (defun browse-url-default-windows-browser (url &optional _new-window)
834 (interactive (browse-url-interactive-arg "URL: "))
835 (cond ((eq system-type 'ms-dos)
836 (if dos-windows-version
837 (shell-command (concat "start " (shell-quote-argument url)))
838 (error "Browsing URLs is not supported on this system")))
839 ((eq system-type 'cygwin)
840 (call-process "cygstart" nil nil nil url))
841 (t (w32-shell-execute "open" url))))
843 (defun browse-url-default-macosx-browser (url &optional _new-window)
844 (interactive (browse-url-interactive-arg "URL: "))
845 (start-process (concat "open " url) nil "open" url))
847 ;; --- Netscape ---
849 (defun browse-url-process-environment ()
850 "Set DISPLAY in the environment to the X display the browser will use.
851 This is either the value of variable `browse-url-browser-display' if
852 non-nil, or the same display as Emacs if different from the current
853 environment, otherwise just use the current environment."
854 (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
855 (if display
856 (cons (concat "DISPLAY=" display) process-environment)
857 process-environment)))
859 (defun browse-url-emacs-display ()
860 "Return the X display Emacs is running on.
861 This is nil if the display is the same as the DISPLAY environment variable.
863 Actually Emacs could be using several displays; this just returns the
864 one showing the selected frame."
865 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
866 (and (not (equal display (getenv "DISPLAY")))
867 display)))
869 (defun browse-url-default-browser (url &rest args)
870 "Find a suitable browser and ask it to load URL.
871 Default to the URL around or before point.
873 When called interactively, if variable `browse-url-new-window-flag' is
874 non-nil, load the document in a new window, if possible, otherwise use
875 a random existing one. A non-nil interactive prefix argument reverses
876 the effect of `browse-url-new-window-flag'.
878 When called non-interactively, optional second argument NEW-WINDOW is
879 used instead of `browse-url-new-window-flag'."
880 (apply
881 (cond
882 ((memq system-type '(windows-nt ms-dos cygwin))
883 'browse-url-default-windows-browser)
884 ((memq system-type '(darwin))
885 'browse-url-default-macosx-browser)
886 ((browse-url-can-use-xdg-open) 'browse-url-xdg-open)
887 ;;; ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
888 ((executable-find browse-url-mozilla-program) 'browse-url-mozilla)
889 ((executable-find browse-url-firefox-program) 'browse-url-firefox)
890 ((executable-find browse-url-chromium-program) 'browse-url-chromium)
891 ;;; ((executable-find browse-url-galeon-program) 'browse-url-galeon)
892 ((executable-find browse-url-kde-program) 'browse-url-kde)
893 ;;; ((executable-find browse-url-netscape-program) 'browse-url-netscape)
894 ;;; ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
895 ((executable-find browse-url-conkeror-program) 'browse-url-conkeror)
896 ((executable-find browse-url-xterm-program) 'browse-url-text-xterm)
897 ((locate-library "w3") 'browse-url-w3)
899 (lambda (&rest _ignore) (error "No usable browser found"))))
900 url args))
902 (defun browse-url-can-use-xdg-open ()
903 "Return non-nil if the \"xdg-open\" program can be used.
904 xdg-open is a desktop utility that calls your preferred web browser.
905 This requires you to be running either Gnome, KDE, Xfce4 or LXDE."
906 (and (getenv "DISPLAY")
907 (executable-find "xdg-open")
908 ;; xdg-open may call gnome-open and that does not wait for its child
909 ;; to finish. This child may then be killed when the parent dies.
910 ;; Use nohup to work around. See bug#7166, bug#8917, bug#9779 and
911 ;; http://lists.gnu.org/archive/html/emacs-devel/2009-07/msg00279.html
912 (executable-find "nohup")
913 (or (getenv "GNOME_DESKTOP_SESSION_ID")
914 ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
915 (condition-case nil
916 (eq 0 (call-process
917 "dbus-send" nil nil nil
918 "--dest=org.gnome.SessionManager"
919 "--print-reply"
920 "/org/gnome/SessionManager"
921 "org.gnome.SessionManager.CanShutdown"))
922 (error nil))
923 (equal (getenv "KDE_FULL_SESSION") "true")
924 (condition-case nil
925 (eq 0 (call-process
926 "/bin/sh" nil nil nil
927 "-c"
928 ;; FIXME use string-match rather than grep.
929 "xprop -root _DT_SAVE_MODE|grep xfce4"))
930 (error nil))
931 (member (getenv "DESKTOP_SESSION") '("LXDE" "Lubuntu"))
932 (equal (getenv "XDG_CURRENT_DESKTOP") "LXDE"))))
935 ;;;###autoload
936 (defun browse-url-xdg-open (url &optional ignored)
937 "Pass the specified URL to the \"xdg-open\" command.
938 xdg-open is a desktop utility that calls your preferred web browser.
939 The optional argument IGNORED is not used."
940 (interactive (browse-url-interactive-arg "URL: "))
941 (call-process "xdg-open" nil 0 nil url))
943 ;;;###autoload
944 (defun browse-url-netscape (url &optional new-window)
945 "Ask the Netscape WWW browser to load URL.
946 Default to the URL around or before point. The strings in variable
947 `browse-url-netscape-arguments' are also passed to Netscape.
949 When called interactively, if variable `browse-url-new-window-flag' is
950 non-nil, load the document in a new Netscape window, otherwise use a
951 random existing one. A non-nil interactive prefix argument reverses
952 the effect of `browse-url-new-window-flag'.
954 If `browse-url-netscape-new-window-is-tab' is non-nil, then
955 whenever a document would otherwise be loaded in a new window, it
956 is loaded in a new tab in an existing window instead.
958 When called non-interactively, optional second argument NEW-WINDOW is
959 used instead of `browse-url-new-window-flag'."
960 (declare (obsolete nil "25.1"))
961 (interactive (browse-url-interactive-arg "URL: "))
962 (setq url (browse-url-encode-url url))
963 (let* ((process-environment (browse-url-process-environment))
964 (process
965 (apply 'start-process
966 (concat "netscape " url) nil
967 browse-url-netscape-program
968 (append
969 browse-url-netscape-arguments
970 (if (eq window-system 'w32)
971 (list url)
972 (append
973 (if new-window '("-noraise"))
974 (list "-remote"
975 (concat "openURL(" url
976 (if (browse-url-maybe-new-window
977 new-window)
978 (if browse-url-netscape-new-window-is-tab
979 ",new-tab"
980 ",new-window"))
981 ")"))))))))
982 (set-process-sentinel process
983 `(lambda (process change)
984 (browse-url-netscape-sentinel process ,url)))))
986 (defun browse-url-netscape-sentinel (process url)
987 "Handle a change to the process communicating with Netscape."
988 (declare (obsolete nil "25.1"))
989 (or (eq (process-exit-status process) 0)
990 (let* ((process-environment (browse-url-process-environment)))
991 ;; Netscape not running - start it
992 (message "Starting %s..." browse-url-netscape-program)
993 (apply 'start-process (concat "netscape" url) nil
994 browse-url-netscape-program
995 (append browse-url-netscape-startup-arguments (list url))))))
997 (defun browse-url-netscape-reload ()
998 "Ask Netscape to reload its current document.
999 How depends on `browse-url-netscape-version'."
1000 (declare (obsolete nil "25.1"))
1001 (interactive)
1002 ;; Backwards incompatibility reported by
1003 ;; <peter.kruse@psychologie.uni-regensburg.de>.
1004 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
1005 "xfeDoCommand(reload)"
1006 "reload")))
1008 (defun browse-url-netscape-send (command)
1009 "Send a remote control command to Netscape."
1010 (declare (obsolete nil "25.1"))
1011 (let* ((process-environment (browse-url-process-environment)))
1012 (apply 'start-process "netscape" nil
1013 browse-url-netscape-program
1014 (append browse-url-netscape-arguments
1015 (list "-remote" command)))))
1017 ;;;###autoload
1018 (defun browse-url-mozilla (url &optional new-window)
1019 "Ask the Mozilla WWW browser to load URL.
1020 Default to the URL around or before point. The strings in variable
1021 `browse-url-mozilla-arguments' are also passed to Mozilla.
1023 When called interactively, if variable `browse-url-new-window-flag' is
1024 non-nil, load the document in a new Mozilla window, otherwise use a
1025 random existing one. A non-nil interactive prefix argument reverses
1026 the effect of `browse-url-new-window-flag'.
1028 If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
1029 document would otherwise be loaded in a new window, it is loaded in a
1030 new tab in an existing window instead.
1032 When called non-interactively, optional second argument NEW-WINDOW is
1033 used instead of `browse-url-new-window-flag'."
1034 (interactive (browse-url-interactive-arg "URL: "))
1035 (setq url (browse-url-encode-url url))
1036 (let* ((process-environment (browse-url-process-environment))
1037 (process
1038 (apply 'start-process
1039 (concat "mozilla " url) nil
1040 browse-url-mozilla-program
1041 (append
1042 browse-url-mozilla-arguments
1043 (list "-remote"
1044 (concat "openURL("
1046 (if (browse-url-maybe-new-window
1047 new-window)
1048 (if browse-url-mozilla-new-window-is-tab
1049 ",new-tab"
1050 ",new-window"))
1051 ")"))))))
1052 (set-process-sentinel process
1053 `(lambda (process change)
1054 (browse-url-mozilla-sentinel process ,url)))))
1056 (defun browse-url-mozilla-sentinel (process url)
1057 "Handle a change to the process communicating with Mozilla."
1058 (or (eq (process-exit-status process) 0)
1059 (let* ((process-environment (browse-url-process-environment)))
1060 ;; Mozilla is not running - start it
1061 (message "Starting %s..." browse-url-mozilla-program)
1062 (apply 'start-process (concat "mozilla " url) nil
1063 browse-url-mozilla-program
1064 (append browse-url-mozilla-startup-arguments (list url))))))
1066 ;;;###autoload
1067 (defun browse-url-firefox (url &optional new-window)
1068 "Ask the Firefox WWW browser to load URL.
1069 Defaults to the URL around or before point. Passes the strings
1070 in the variable `browse-url-firefox-arguments' to Firefox.
1072 Interactively, if the variable `browse-url-new-window-flag' is non-nil,
1073 loads the document in a new Firefox window. A non-nil prefix argument
1074 reverses the effect of `browse-url-new-window-flag'.
1076 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1077 whenever a document would otherwise be loaded in a new window, it
1078 is loaded in a new tab in an existing window instead.
1080 Non-interactively, this uses the optional second argument NEW-WINDOW
1081 instead of `browse-url-new-window-flag'."
1082 (interactive (browse-url-interactive-arg "URL: "))
1083 (setq url (browse-url-encode-url url))
1084 (let* ((process-environment (browse-url-process-environment)))
1085 (apply 'start-process
1086 (concat "firefox " url) nil
1087 browse-url-firefox-program
1088 (append
1089 browse-url-firefox-arguments
1090 (if (browse-url-maybe-new-window new-window)
1091 (if browse-url-firefox-new-window-is-tab
1092 '("-new-tab")
1093 '("-new-window")))
1094 (list url)))))
1096 ;;;###autoload
1097 (defun browse-url-chromium (url &optional _new-window)
1098 "Ask the Chromium WWW browser to load URL.
1099 Default to the URL around or before point. The strings in
1100 variable `browse-url-chromium-arguments' are also passed to
1101 Chromium."
1102 (interactive (browse-url-interactive-arg "URL: "))
1103 (setq url (browse-url-encode-url url))
1104 (let* ((process-environment (browse-url-process-environment)))
1105 (apply 'start-process
1106 (concat "chromium " url) nil
1107 browse-url-chromium-program
1108 (append
1109 browse-url-chromium-arguments
1110 (list url)))))
1112 ;;;###autoload
1113 (defun browse-url-galeon (url &optional new-window)
1114 "Ask the Galeon WWW browser to load URL.
1115 Default to the URL around or before point. The strings in variable
1116 `browse-url-galeon-arguments' are also passed to Galeon.
1118 When called interactively, if variable `browse-url-new-window-flag' is
1119 non-nil, load the document in a new Galeon window, otherwise use a
1120 random existing one. A non-nil interactive prefix argument reverses
1121 the effect of `browse-url-new-window-flag'.
1123 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1124 document would otherwise be loaded in a new window, it is loaded in a
1125 new tab in an existing window instead.
1127 When called non-interactively, optional second argument NEW-WINDOW is
1128 used instead of `browse-url-new-window-flag'."
1129 (declare (obsolete nil "25.1"))
1130 (interactive (browse-url-interactive-arg "URL: "))
1131 (setq url (browse-url-encode-url url))
1132 (let* ((process-environment (browse-url-process-environment))
1133 (process (apply 'start-process
1134 (concat "galeon " url)
1136 browse-url-galeon-program
1137 (append
1138 browse-url-galeon-arguments
1139 (if (browse-url-maybe-new-window new-window)
1140 (if browse-url-galeon-new-window-is-tab
1141 '("--new-tab")
1142 '("--new-window" "--noraise"))
1143 '("--existing"))
1144 (list url)))))
1145 (set-process-sentinel process
1146 `(lambda (process change)
1147 (browse-url-galeon-sentinel process ,url)))))
1149 (defun browse-url-galeon-sentinel (process url)
1150 "Handle a change to the process communicating with Galeon."
1151 (declare (obsolete nil "25.1"))
1152 (or (eq (process-exit-status process) 0)
1153 (let* ((process-environment (browse-url-process-environment)))
1154 ;; Galeon is not running - start it
1155 (message "Starting %s..." browse-url-galeon-program)
1156 (apply 'start-process (concat "galeon " url) nil
1157 browse-url-galeon-program
1158 (append browse-url-galeon-startup-arguments (list url))))))
1160 (defun browse-url-epiphany (url &optional new-window)
1161 "Ask the Epiphany WWW browser to load URL.
1162 Default to the URL around or before point. The strings in variable
1163 `browse-url-galeon-arguments' are also passed to Epiphany.
1165 When called interactively, if variable `browse-url-new-window-flag' is
1166 non-nil, load the document in a new Epiphany window, otherwise use a
1167 random existing one. A non-nil interactive prefix argument reverses
1168 the effect of `browse-url-new-window-flag'.
1170 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1171 document would otherwise be loaded in a new window, it is loaded in a
1172 new tab in an existing window instead.
1174 When called non-interactively, optional second argument NEW-WINDOW is
1175 used instead of `browse-url-new-window-flag'."
1176 (interactive (browse-url-interactive-arg "URL: "))
1177 (setq url (browse-url-encode-url url))
1178 (let* ((process-environment (browse-url-process-environment))
1179 (process (apply 'start-process
1180 (concat "epiphany " url)
1182 browse-url-epiphany-program
1183 (append
1184 browse-url-epiphany-arguments
1185 (if (browse-url-maybe-new-window new-window)
1186 (if browse-url-epiphany-new-window-is-tab
1187 '("--new-tab")
1188 '("--new-window" "--noraise"))
1189 '("--existing"))
1190 (list url)))))
1191 (set-process-sentinel process
1192 `(lambda (process change)
1193 (browse-url-epiphany-sentinel process ,url)))))
1195 (defun browse-url-epiphany-sentinel (process url)
1196 "Handle a change to the process communicating with Epiphany."
1197 (or (eq (process-exit-status process) 0)
1198 (let* ((process-environment (browse-url-process-environment)))
1199 ;; Epiphany is not running - start it
1200 (message "Starting %s..." browse-url-epiphany-program)
1201 (apply 'start-process (concat "epiphany " url) nil
1202 browse-url-epiphany-program
1203 (append browse-url-epiphany-startup-arguments (list url))))))
1205 (defvar url-handler-regexp)
1207 ;;;###autoload
1208 (defun browse-url-emacs (url &optional _new-window)
1209 "Ask Emacs to load URL into a buffer and show it in another window."
1210 (interactive (browse-url-interactive-arg "URL: "))
1211 (require 'url-handlers)
1212 (let ((file-name-handler-alist
1213 (cons (cons url-handler-regexp 'url-file-handler)
1214 file-name-handler-alist)))
1215 ;; Ignore `new-window': with all other browsers the URL is always shown
1216 ;; in another window than the current Emacs one since it's shown in
1217 ;; another application's window.
1218 ;; (if new-window (find-file-other-window url) (find-file url))
1219 (find-file-other-window url)))
1221 ;;;###autoload
1222 (defun browse-url-gnome-moz (url &optional new-window)
1223 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1224 Default to the URL around or before point. The strings in variable
1225 `browse-url-gnome-moz-arguments' are also passed.
1227 When called interactively, if variable `browse-url-new-window-flag' is
1228 non-nil, load the document in a new browser window, otherwise use an
1229 existing one. A non-nil interactive prefix argument reverses the
1230 effect of `browse-url-new-window-flag'.
1232 When called non-interactively, optional second argument NEW-WINDOW is
1233 used instead of `browse-url-new-window-flag'."
1234 (declare (obsolete nil "25.1"))
1235 (interactive (browse-url-interactive-arg "URL: "))
1236 (apply 'start-process (concat "gnome-moz-remote " url)
1238 browse-url-gnome-moz-program
1239 (append
1240 browse-url-gnome-moz-arguments
1241 (if (browse-url-maybe-new-window new-window)
1242 '("--newwin"))
1243 (list "--raise" url))))
1245 ;; --- Mosaic ---
1247 ;;;###autoload
1248 (defun browse-url-mosaic (url &optional new-window)
1249 "Ask the XMosaic WWW browser to load URL.
1251 Default to the URL around or before point. The strings in variable
1252 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1253 program is invoked according to the variable
1254 `browse-url-mosaic-program'.
1256 When called interactively, if variable `browse-url-new-window-flag' is
1257 non-nil, load the document in a new Mosaic window, otherwise use a
1258 random existing one. A non-nil interactive prefix argument reverses
1259 the effect of `browse-url-new-window-flag'.
1261 When called non-interactively, optional second argument NEW-WINDOW is
1262 used instead of `browse-url-new-window-flag'."
1263 (declare (obsolete nil "25.1"))
1264 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1265 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
1266 pid)
1267 (if (file-readable-p pidfile)
1268 (with-temp-buffer
1269 (insert-file-contents pidfile)
1270 (setq pid (read (current-buffer)))))
1271 (if (and (integerp pid) (zerop (signal-process pid 0))) ; Mosaic running
1272 (progn
1273 (with-temp-buffer
1274 (insert (if (browse-url-maybe-new-window new-window)
1275 "newwin\n"
1276 "goto\n")
1277 url "\n")
1278 (with-file-modes ?\700
1279 (if (file-exists-p
1280 (setq pidfile (format "/tmp/Mosaic.%d" pid)))
1281 (delete-file pidfile))
1282 ;; http://debbugs.gnu.org/17428. Use O_EXCL.
1283 (write-region nil nil pidfile nil 'silent nil 'excl)))
1284 ;; Send signal SIGUSR to Mosaic
1285 (message "Signaling Mosaic...")
1286 (signal-process pid 'SIGUSR1)
1287 ;; Or you could try:
1288 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1289 (message "Signaling Mosaic...done"))
1290 ;; Mosaic not running - start it
1291 (message "Starting %s..." browse-url-mosaic-program)
1292 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1293 (append browse-url-mosaic-arguments (list url)))
1294 (message "Starting %s...done" browse-url-mosaic-program))))
1296 ;; --- Mosaic using CCI ---
1298 ;;;###autoload
1299 (defun browse-url-cci (url &optional new-window)
1300 "Ask the XMosaic WWW browser to load URL.
1301 Default to the URL around or before point.
1303 This function only works for XMosaic version 2.5 or later. You must
1304 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1305 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1307 When called interactively, if variable `browse-url-new-window-flag' is
1308 non-nil, load the document in a new browser window, otherwise use a
1309 random existing one. A non-nil interactive prefix argument reverses
1310 the effect of `browse-url-new-window-flag'.
1312 When called non-interactively, optional second argument NEW-WINDOW is
1313 used instead of `browse-url-new-window-flag'."
1314 (declare (obsolete nil "25.1"))
1315 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1316 (open-network-stream "browse-url" " *browse-url*"
1317 browse-url-CCI-host browse-url-CCI-port)
1318 ;; Todo: start browser if fails
1319 (process-send-string "browse-url"
1320 (concat "get url (" url ") output "
1321 (if (browse-url-maybe-new-window new-window)
1322 "new"
1323 "current")
1324 "\r\n"))
1325 (process-send-string "browse-url" "disconnect\r\n")
1326 (delete-process "browse-url"))
1328 ;; --- Conkeror ---
1329 ;;;###autoload
1330 (defun browse-url-conkeror (url &optional new-window)
1331 "Ask the Conkeror WWW browser to load URL.
1332 Default to the URL around or before point. Also pass the strings
1333 in the variable `browse-url-conkeror-arguments' to Conkeror.
1335 When called interactively, if variable
1336 `browse-url-new-window-flag' is non-nil, load the document in a
1337 new Conkeror window, otherwise use a random existing one. A
1338 non-nil interactive prefix argument reverses the effect of
1339 `browse-url-new-window-flag'.
1341 If variable `browse-url-conkeror-new-window-is-buffer' is
1342 non-nil, then whenever a document would otherwise be loaded in a
1343 new window, load it in a new buffer in an existing window instead.
1345 When called non-interactively, use optional second argument
1346 NEW-WINDOW instead of `browse-url-new-window-flag'."
1347 (interactive (browse-url-interactive-arg "URL: "))
1348 (setq url (browse-url-encode-url url))
1349 (let* ((process-environment (browse-url-process-environment)))
1350 (apply 'start-process (format "conkeror %s" url)
1352 browse-url-conkeror-program
1353 (append
1354 browse-url-conkeror-arguments
1355 (list
1356 "-e"
1357 (format "load_url_in_new_%s('%s')"
1358 (if (browse-url-maybe-new-window new-window)
1359 (if browse-url-conkeror-new-window-is-buffer
1360 "buffer"
1361 "window")
1362 "buffer")
1363 url))))))
1364 ;; --- W3 ---
1366 ;; External.
1367 (declare-function w3-fetch-other-window "ext:w3m" (&optional url))
1368 (declare-function w3-fetch "ext:w3m" (&optional url target))
1370 ;;;###autoload
1371 (defun browse-url-w3 (url &optional new-window)
1372 "Ask the w3 WWW browser to load URL.
1373 Default to the URL around or before point.
1375 When called interactively, if variable `browse-url-new-window-flag' is
1376 non-nil, load the document in a new window. A non-nil interactive
1377 prefix argument reverses the effect of `browse-url-new-window-flag'.
1379 When called non-interactively, optional second argument NEW-WINDOW is
1380 used instead of `browse-url-new-window-flag'."
1381 (interactive (browse-url-interactive-arg "W3 URL: "))
1382 (require 'w3) ; w3-fetch-other-window not autoloaded
1383 (if (browse-url-maybe-new-window new-window)
1384 (w3-fetch-other-window url)
1385 (w3-fetch url)))
1387 ;;;###autoload
1388 (defun browse-url-w3-gnudoit (url &optional _new-window)
1389 ;; new-window ignored
1390 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1391 The `browse-url-gnudoit-program' program is used with options given by
1392 `browse-url-gnudoit-args'. Default to the URL around or before point."
1393 (declare (obsolete nil "25.1"))
1394 (interactive (browse-url-interactive-arg "W3 URL: "))
1395 (apply 'start-process (concat "gnudoit:" url) nil
1396 browse-url-gnudoit-program
1397 (append browse-url-gnudoit-args
1398 (list (concat "(w3-fetch \"" url "\")")
1399 "(raise-frame)"))))
1401 ;; --- Lynx in an xterm ---
1403 ;;;###autoload
1404 (defun browse-url-text-xterm (url &optional _new-window)
1405 ;; new-window ignored
1406 "Ask a text browser to load URL.
1407 URL defaults to the URL around or before point.
1408 This runs the text browser specified by `browse-url-text-browser'.
1409 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1410 with possible additional arguments `browse-url-xterm-args'."
1411 (interactive (browse-url-interactive-arg "Text browser URL: "))
1412 (apply #'start-process `(,(concat browse-url-text-browser url)
1413 nil ,browse-url-xterm-program
1414 ,@browse-url-xterm-args "-e" ,browse-url-text-browser
1415 ,url)))
1417 ;; --- Lynx in an Emacs "term" window ---
1419 (declare-function term-char-mode "term" ())
1420 (declare-function term-send-down "term" ())
1421 (declare-function term-send-string "term" (proc str))
1423 ;;;###autoload
1424 (defun browse-url-text-emacs (url &optional new-buffer)
1425 "Ask a text browser to load URL.
1426 URL defaults to the URL around or before point.
1427 This runs the text browser specified by `browse-url-text-browser'.
1428 With a prefix argument, it runs a new browser process in a new buffer.
1430 When called interactively, if variable `browse-url-new-window-flag' is
1431 non-nil, load the document in a new browser process in a new term window,
1432 otherwise use any existing one. A non-nil interactive prefix argument
1433 reverses the effect of `browse-url-new-window-flag'.
1435 When called non-interactively, optional second argument NEW-WINDOW is
1436 used instead of `browse-url-new-window-flag'."
1437 (interactive (browse-url-interactive-arg "Text browser URL: "))
1438 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
1439 ;; (term-term-name "vt100") ; ??
1440 (buf (get-buffer "*text browser*"))
1441 (proc (and buf (get-buffer-process buf)))
1442 (n browse-url-text-input-attempts))
1443 (require 'term)
1444 (if (and (browse-url-maybe-new-window new-buffer) buf)
1445 ;; Rename away the OLD buffer. This isn't very polite, but
1446 ;; term insists on working in a buffer named *lynx* and would
1447 ;; choke on *lynx*<1>
1448 (progn (set-buffer buf)
1449 (rename-uniquely)))
1450 (if (or (browse-url-maybe-new-window new-buffer)
1451 (not buf)
1452 (not proc)
1453 (not (memq (process-status proc) '(run stop))))
1454 ;; start a new text browser
1455 (progn
1456 (setq buf
1457 (apply #'make-term
1458 `(,browse-url-text-browser
1459 ,browse-url-text-browser
1460 nil ,@browse-url-text-emacs-args
1461 ,url)))
1462 (switch-to-buffer buf)
1463 (term-char-mode)
1464 (set-process-sentinel
1465 (get-buffer-process buf)
1466 ;; Don't leave around a dead one (especially because of its
1467 ;; munged keymap.)
1468 (lambda (process _event)
1469 (if (not (memq (process-status process) '(run stop)))
1470 (let ((buf (process-buffer process)))
1471 (if buf (kill-buffer buf)))))))
1472 ;; Send the url to the text browser in the old buffer
1473 (let ((win (get-buffer-window buf t)))
1474 (if win
1475 (select-window win)
1476 (switch-to-buffer buf)))
1477 (if (eq (following-char) ?_)
1478 (cond ((eq browse-url-text-input-field 'warn)
1479 (error "Please move out of the input field first"))
1480 ((eq browse-url-text-input-field 'avoid)
1481 (while (and (eq (following-char) ?_) (> n 0))
1482 (term-send-down) ; down arrow
1483 (sit-for browse-url-text-input-delay))
1484 (if (eq (following-char) ?_)
1485 (error "Cannot move out of the input field, sorry")))))
1486 (term-send-string proc (concat "g" ; goto
1487 "\C-u" ; kill default url
1489 "\r")))))
1491 ;; --- mailto ---
1493 (autoload 'rfc2368-parse-mailto-url "rfc2368")
1495 ;;;###autoload
1496 (defun browse-url-mail (url &optional new-window)
1497 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1498 Default to using the mailto: URL around or before point as the
1499 recipient's address. Supplying a non-nil interactive prefix argument
1500 will cause the mail to be composed in another window rather than the
1501 current one.
1503 When called interactively, if variable `browse-url-new-window-flag' is
1504 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1505 non-nil interactive prefix argument reverses the effect of
1506 `browse-url-new-window-flag'.
1508 When called non-interactively, optional second argument NEW-WINDOW is
1509 used instead of `browse-url-new-window-flag'."
1510 (interactive (browse-url-interactive-arg "Mailto URL: "))
1511 (save-excursion
1512 (let* ((alist (rfc2368-parse-mailto-url url))
1513 (to (assoc "To" alist))
1514 (subject (assoc "Subject" alist))
1515 (body (assoc "Body" alist))
1516 (rest (delq to (delq subject (delq body alist))))
1517 (to (cdr to))
1518 (subject (cdr subject))
1519 (body (cdr body))
1520 (mail-citation-hook (unless body mail-citation-hook)))
1521 (if (browse-url-maybe-new-window new-window)
1522 (compose-mail-other-window to subject rest nil
1523 (list 'insert-buffer (current-buffer)))
1524 (compose-mail to subject rest nil nil
1525 (list 'insert-buffer (current-buffer))))
1526 (when body
1527 (goto-char (point-min))
1528 (unless (or (search-forward (concat "\n" mail-header-separator "\n")
1529 nil 'move)
1530 (bolp))
1531 (insert "\n"))
1532 (goto-char (prog1
1533 (point)
1534 (insert (replace-regexp-in-string "\r\n" "\n" body))
1535 (unless (bolp)
1536 (insert "\n"))))))))
1538 ;; --- Random browser ---
1540 ;;;###autoload
1541 (defun browse-url-generic (url &optional _new-window)
1542 ;; new-window ignored
1543 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1544 Default to the URL around or before point. A fresh copy of the
1545 browser is started up in a new process with possible additional arguments
1546 `browse-url-generic-args'. This is appropriate for browsers which
1547 don't offer a form of remote control."
1548 (interactive (browse-url-interactive-arg "URL: "))
1549 (if (not browse-url-generic-program)
1550 (error "No browser defined (`browse-url-generic-program')"))
1551 (apply 'call-process browse-url-generic-program nil
1552 0 nil
1553 (append browse-url-generic-args (list url))))
1555 ;;;###autoload
1556 (defun browse-url-kde (url &optional _new-window)
1557 "Ask the KDE WWW browser to load URL.
1558 Default to the URL around or before point."
1559 (interactive (browse-url-interactive-arg "KDE URL: "))
1560 (message "Sending URL to KDE...")
1561 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
1562 (append browse-url-kde-args (list url))))
1564 (defun browse-url-elinks-new-window (url)
1565 "Ask the Elinks WWW browser to load URL in a new window."
1566 (let ((process-environment (browse-url-process-environment)))
1567 (apply #'start-process
1568 (append (list (concat "elinks:" url)
1569 nil)
1570 browse-url-elinks-wrapper
1571 (list "elinks" url)))))
1573 ;;;###autoload
1574 (defun browse-url-elinks (url &optional new-window)
1575 "Ask the Elinks WWW browser to load URL.
1576 Default to the URL around the point.
1578 The document is loaded in a new tab of a running Elinks or, if
1579 none yet running, a newly started instance.
1581 The Elinks command will be prepended by the program+arguments
1582 from `browse-url-elinks-wrapper'."
1583 (interactive (browse-url-interactive-arg "URL: "))
1584 (setq url (browse-url-encode-url url))
1585 (if new-window
1586 (browse-url-elinks-new-window url)
1587 (let ((process-environment (browse-url-process-environment))
1588 (elinks-ping-process (start-process "elinks-ping" nil
1589 "elinks" "-remote" "ping()")))
1590 (set-process-sentinel elinks-ping-process
1591 `(lambda (process change)
1592 (browse-url-elinks-sentinel process ,url))))))
1594 (defun browse-url-elinks-sentinel (process url)
1595 "Determines if Elinks is running or a new one has to be started."
1596 ;; Try to determine if an instance is running or if we have to
1597 ;; create a new one.
1598 (pcase (process-exit-status process)
1600 ;; No instance, start a new one.
1601 (browse-url-elinks-new-window url))
1603 ;; Found an instance, open URL in new tab.
1604 (let ((process-environment (browse-url-process-environment)))
1605 (start-process (concat "elinks:" url) nil
1606 "elinks" "-remote"
1607 (concat "openURL(\"" url "\",new-tab)"))))
1608 (exit-status
1609 (error "Unrecognized exit-code %d of process `elinks'"
1610 exit-status))))
1612 (provide 'browse-url)
1614 ;;; browse-url.el ends here