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