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