Fix a comment whitespace typo.
[emacs.git] / lisp / net / browse-url.el
blob20ae072f6527b19b9cccfe81dcb40e5b197ec2af
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 freedesktop.org xdg-open
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-unhex-string 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 ;; The exact set of situations where xdg-open works is complicated,
949 ;; and it would be a pain to duplicate xdg-open's situation-specific
950 ;; code here, as the code is a moving target. So assume that
951 ;; xdg-open will work if there is a graphical display; this should
952 ;; be good enough for platforms Emacs is likely to be running on.
953 (and (or (getenv "DISPLAY") (getenv "WAYLAND_DISPLAY"))
954 (executable-find "xdg-open")))
956 ;;;###autoload
957 (defun browse-url-xdg-open (url &optional ignored)
958 "Pass the specified URL to the \"xdg-open\" command.
959 xdg-open is a desktop utility that calls your preferred web browser.
960 The optional argument IGNORED is not used."
961 (interactive (browse-url-interactive-arg "URL: "))
962 (call-process "xdg-open" nil 0 nil url))
964 ;;;###autoload
965 (defun browse-url-netscape (url &optional new-window)
966 "Ask the Netscape WWW browser to load URL.
967 Default to the URL around or before point. The strings in variable
968 `browse-url-netscape-arguments' are also passed to Netscape.
970 When called interactively, if variable `browse-url-new-window-flag' is
971 non-nil, load the document in a new Netscape window, otherwise use a
972 random existing one. A non-nil interactive prefix argument reverses
973 the effect of `browse-url-new-window-flag'.
975 If `browse-url-netscape-new-window-is-tab' is non-nil, then
976 whenever a document would otherwise be loaded in a new window, it
977 is loaded in a new tab in an existing window instead.
979 When called non-interactively, optional second argument NEW-WINDOW is
980 used instead of `browse-url-new-window-flag'."
981 (declare (obsolete nil "25.1"))
982 (interactive (browse-url-interactive-arg "URL: "))
983 (setq url (browse-url-encode-url url))
984 (let* ((process-environment (browse-url-process-environment))
985 (process
986 (apply 'start-process
987 (concat "netscape " url) nil
988 browse-url-netscape-program
989 (append
990 browse-url-netscape-arguments
991 (if (eq window-system 'w32)
992 (list url)
993 (append
994 (if new-window '("-noraise"))
995 (list "-remote"
996 (concat "openURL(" url
997 (if (browse-url-maybe-new-window
998 new-window)
999 (if browse-url-netscape-new-window-is-tab
1000 ",new-tab"
1001 ",new-window"))
1002 ")"))))))))
1003 (set-process-sentinel process
1004 `(lambda (process change)
1005 (browse-url-netscape-sentinel process ,url)))))
1007 (defun browse-url-netscape-sentinel (process url)
1008 "Handle a change to the process communicating with Netscape."
1009 (declare (obsolete nil "25.1"))
1010 (or (eq (process-exit-status process) 0)
1011 (let* ((process-environment (browse-url-process-environment)))
1012 ;; Netscape not running - start it
1013 (message "Starting %s..." browse-url-netscape-program)
1014 (apply 'start-process (concat "netscape" url) nil
1015 browse-url-netscape-program
1016 (append browse-url-netscape-startup-arguments (list url))))))
1018 (defun browse-url-netscape-reload ()
1019 "Ask Netscape to reload its current document.
1020 How depends on `browse-url-netscape-version'."
1021 (declare (obsolete nil "25.1"))
1022 (interactive)
1023 ;; Backwards incompatibility reported by
1024 ;; <peter.kruse@psychologie.uni-regensburg.de>.
1025 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
1026 "xfeDoCommand(reload)"
1027 "reload")))
1029 (defun browse-url-netscape-send (command)
1030 "Send a remote control command to Netscape."
1031 (declare (obsolete nil "25.1"))
1032 (let* ((process-environment (browse-url-process-environment)))
1033 (apply 'start-process "netscape" nil
1034 browse-url-netscape-program
1035 (append browse-url-netscape-arguments
1036 (list "-remote" command)))))
1038 ;;;###autoload
1039 (defun browse-url-mozilla (url &optional new-window)
1040 "Ask the Mozilla WWW browser to load URL.
1041 Default to the URL around or before point. The strings in variable
1042 `browse-url-mozilla-arguments' are also passed to Mozilla.
1044 When called interactively, if variable `browse-url-new-window-flag' is
1045 non-nil, load the document in a new Mozilla window, otherwise use a
1046 random existing one. A non-nil interactive prefix argument reverses
1047 the effect of `browse-url-new-window-flag'.
1049 If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
1050 document would otherwise be loaded in a new window, it is loaded in a
1051 new tab in an existing window instead.
1053 When called non-interactively, optional second argument NEW-WINDOW is
1054 used instead of `browse-url-new-window-flag'."
1055 (interactive (browse-url-interactive-arg "URL: "))
1056 (setq url (browse-url-encode-url url))
1057 (let* ((process-environment (browse-url-process-environment))
1058 (process
1059 (apply 'start-process
1060 (concat "mozilla " url) nil
1061 browse-url-mozilla-program
1062 (append
1063 browse-url-mozilla-arguments
1064 (list "-remote"
1065 (concat "openURL("
1067 (if (browse-url-maybe-new-window
1068 new-window)
1069 (if browse-url-mozilla-new-window-is-tab
1070 ",new-tab"
1071 ",new-window"))
1072 ")"))))))
1073 (set-process-sentinel process
1074 `(lambda (process change)
1075 (browse-url-mozilla-sentinel process ,url)))))
1077 (defun browse-url-mozilla-sentinel (process url)
1078 "Handle a change to the process communicating with Mozilla."
1079 (or (eq (process-exit-status process) 0)
1080 (let* ((process-environment (browse-url-process-environment)))
1081 ;; Mozilla is not running - start it
1082 (message "Starting %s..." browse-url-mozilla-program)
1083 (apply 'start-process (concat "mozilla " url) nil
1084 browse-url-mozilla-program
1085 (append browse-url-mozilla-startup-arguments (list url))))))
1087 ;;;###autoload
1088 (defun browse-url-firefox (url &optional new-window)
1089 "Ask the Firefox WWW browser to load URL.
1090 Defaults to the URL around or before point. Passes the strings
1091 in the variable `browse-url-firefox-arguments' to Firefox.
1093 Interactively, if the variable `browse-url-new-window-flag' is non-nil,
1094 loads the document in a new Firefox window. A non-nil prefix argument
1095 reverses the effect of `browse-url-new-window-flag'.
1097 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1098 whenever a document would otherwise be loaded in a new window, it
1099 is loaded in a new tab in an existing window instead.
1101 Non-interactively, this uses the optional second argument NEW-WINDOW
1102 instead of `browse-url-new-window-flag'."
1103 (interactive (browse-url-interactive-arg "URL: "))
1104 (setq url (browse-url-encode-url url))
1105 (let* ((process-environment (browse-url-process-environment)))
1106 (apply 'start-process
1107 (concat "firefox " url) nil
1108 browse-url-firefox-program
1109 (append
1110 browse-url-firefox-arguments
1111 (if (browse-url-maybe-new-window new-window)
1112 (if browse-url-firefox-new-window-is-tab
1113 '("-new-tab")
1114 '("-new-window")))
1115 (list url)))))
1117 ;;;###autoload
1118 (defun browse-url-chromium (url &optional _new-window)
1119 "Ask the Chromium WWW browser to load URL.
1120 Default to the URL around or before point. The strings in
1121 variable `browse-url-chromium-arguments' are also passed to
1122 Chromium.
1123 The optional argument NEW-WINDOW is not used."
1124 (interactive (browse-url-interactive-arg "URL: "))
1125 (setq url (browse-url-encode-url url))
1126 (let* ((process-environment (browse-url-process-environment)))
1127 (apply 'start-process
1128 (concat "chromium " url) nil
1129 browse-url-chromium-program
1130 (append
1131 browse-url-chromium-arguments
1132 (list url)))))
1134 (defun browse-url-chrome (url &optional _new-window)
1135 "Ask the Google Chrome WWW browser to load URL.
1136 Default to the URL around or before point. The strings in
1137 variable `browse-url-chrome-arguments' are also passed to
1138 Google Chrome.
1139 The optional argument NEW-WINDOW is not used."
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 "google-chrome " url) nil
1145 browse-url-chrome-program
1146 (append
1147 browse-url-chrome-arguments
1148 (list url)))))
1150 ;;;###autoload
1151 (defun browse-url-galeon (url &optional new-window)
1152 "Ask the Galeon WWW browser to load URL.
1153 Default to the URL around or before point. The strings in variable
1154 `browse-url-galeon-arguments' are also passed to Galeon.
1156 When called interactively, if variable `browse-url-new-window-flag' is
1157 non-nil, load the document in a new Galeon window, otherwise use a
1158 random existing one. A non-nil interactive prefix argument reverses
1159 the effect of `browse-url-new-window-flag'.
1161 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1162 document would otherwise be loaded in a new window, it is loaded in a
1163 new tab in an existing window instead.
1165 When called non-interactively, optional second argument NEW-WINDOW is
1166 used instead of `browse-url-new-window-flag'."
1167 (declare (obsolete nil "25.1"))
1168 (interactive (browse-url-interactive-arg "URL: "))
1169 (setq url (browse-url-encode-url url))
1170 (let* ((process-environment (browse-url-process-environment))
1171 (process (apply 'start-process
1172 (concat "galeon " url)
1174 browse-url-galeon-program
1175 (append
1176 browse-url-galeon-arguments
1177 (if (browse-url-maybe-new-window new-window)
1178 (if browse-url-galeon-new-window-is-tab
1179 '("--new-tab")
1180 '("--new-window" "--noraise"))
1181 '("--existing"))
1182 (list url)))))
1183 (set-process-sentinel process
1184 `(lambda (process change)
1185 (browse-url-galeon-sentinel process ,url)))))
1187 (defun browse-url-galeon-sentinel (process url)
1188 "Handle a change to the process communicating with Galeon."
1189 (declare (obsolete nil "25.1"))
1190 (or (eq (process-exit-status process) 0)
1191 (let* ((process-environment (browse-url-process-environment)))
1192 ;; Galeon is not running - start it
1193 (message "Starting %s..." browse-url-galeon-program)
1194 (apply 'start-process (concat "galeon " url) nil
1195 browse-url-galeon-program
1196 (append browse-url-galeon-startup-arguments (list url))))))
1198 (defun browse-url-epiphany (url &optional new-window)
1199 "Ask the Epiphany WWW browser to load URL.
1200 Default to the URL around or before point. The strings in variable
1201 `browse-url-galeon-arguments' are also passed to Epiphany.
1203 When called interactively, if variable `browse-url-new-window-flag' is
1204 non-nil, load the document in a new Epiphany window, otherwise use a
1205 random existing one. A non-nil interactive prefix argument reverses
1206 the effect of `browse-url-new-window-flag'.
1208 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1209 document would otherwise be loaded in a new window, it is loaded in a
1210 new tab in an existing window instead.
1212 When called non-interactively, optional second argument NEW-WINDOW is
1213 used instead of `browse-url-new-window-flag'."
1214 (interactive (browse-url-interactive-arg "URL: "))
1215 (setq url (browse-url-encode-url url))
1216 (let* ((process-environment (browse-url-process-environment))
1217 (process (apply 'start-process
1218 (concat "epiphany " url)
1220 browse-url-epiphany-program
1221 (append
1222 browse-url-epiphany-arguments
1223 (if (browse-url-maybe-new-window new-window)
1224 (if browse-url-epiphany-new-window-is-tab
1225 '("--new-tab")
1226 '("--new-window" "--noraise"))
1227 '("--existing"))
1228 (list url)))))
1229 (set-process-sentinel process
1230 `(lambda (process change)
1231 (browse-url-epiphany-sentinel process ,url)))))
1233 (defun browse-url-epiphany-sentinel (process url)
1234 "Handle a change to the process communicating with Epiphany."
1235 (or (eq (process-exit-status process) 0)
1236 (let* ((process-environment (browse-url-process-environment)))
1237 ;; Epiphany is not running - start it
1238 (message "Starting %s..." browse-url-epiphany-program)
1239 (apply 'start-process (concat "epiphany " url) nil
1240 browse-url-epiphany-program
1241 (append browse-url-epiphany-startup-arguments (list url))))))
1243 (defvar url-handler-regexp)
1245 ;;;###autoload
1246 (defun browse-url-emacs (url &optional _new-window)
1247 "Ask Emacs to load URL into a buffer and show it in another window."
1248 (interactive (browse-url-interactive-arg "URL: "))
1249 (require 'url-handlers)
1250 (let ((file-name-handler-alist
1251 (cons (cons url-handler-regexp 'url-file-handler)
1252 file-name-handler-alist)))
1253 ;; Ignore `new-window': with all other browsers the URL is always shown
1254 ;; in another window than the current Emacs one since it's shown in
1255 ;; another application's window.
1256 ;; (if new-window (find-file-other-window url) (find-file url))
1257 (find-file-other-window url)))
1259 ;;;###autoload
1260 (defun browse-url-gnome-moz (url &optional new-window)
1261 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1262 Default to the URL around or before point. The strings in variable
1263 `browse-url-gnome-moz-arguments' are also passed.
1265 When called interactively, if variable `browse-url-new-window-flag' is
1266 non-nil, load the document in a new browser window, otherwise use an
1267 existing one. A non-nil interactive prefix argument reverses the
1268 effect of `browse-url-new-window-flag'.
1270 When called non-interactively, optional second argument NEW-WINDOW is
1271 used instead of `browse-url-new-window-flag'."
1272 (declare (obsolete nil "25.1"))
1273 (interactive (browse-url-interactive-arg "URL: "))
1274 (apply 'start-process (concat "gnome-moz-remote " url)
1276 browse-url-gnome-moz-program
1277 (append
1278 browse-url-gnome-moz-arguments
1279 (if (browse-url-maybe-new-window new-window)
1280 '("--newwin"))
1281 (list "--raise" url))))
1283 ;; --- Mosaic ---
1285 ;;;###autoload
1286 (defun browse-url-mosaic (url &optional new-window)
1287 "Ask the XMosaic WWW browser to load URL.
1289 Default to the URL around or before point. The strings in variable
1290 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1291 program is invoked according to the variable
1292 `browse-url-mosaic-program'.
1294 When called interactively, if variable `browse-url-new-window-flag' is
1295 non-nil, load the document in a new Mosaic window, otherwise use a
1296 random existing one. A non-nil interactive prefix argument reverses
1297 the effect of `browse-url-new-window-flag'.
1299 When called non-interactively, optional second argument NEW-WINDOW is
1300 used instead of `browse-url-new-window-flag'."
1301 (declare (obsolete nil "25.1"))
1302 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1303 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
1304 pid)
1305 (if (file-readable-p pidfile)
1306 (with-temp-buffer
1307 (insert-file-contents pidfile)
1308 (setq pid (read (current-buffer)))))
1309 (if (and (integerp pid) (zerop (signal-process pid 0))) ; Mosaic running
1310 (progn
1311 (with-temp-buffer
1312 (insert (if (browse-url-maybe-new-window new-window)
1313 "newwin\n"
1314 "goto\n")
1315 url "\n")
1316 (with-file-modes ?\700
1317 (if (file-exists-p
1318 (setq pidfile (format "/tmp/Mosaic.%d" pid)))
1319 (delete-file pidfile))
1320 ;; http://debbugs.gnu.org/17428. Use O_EXCL.
1321 (write-region nil nil pidfile nil 'silent nil 'excl)))
1322 ;; Send signal SIGUSR to Mosaic
1323 (message "Signaling Mosaic...")
1324 (signal-process pid 'SIGUSR1)
1325 ;; Or you could try:
1326 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1327 (message "Signaling Mosaic...done"))
1328 ;; Mosaic not running - start it
1329 (message "Starting %s..." browse-url-mosaic-program)
1330 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1331 (append browse-url-mosaic-arguments (list url)))
1332 (message "Starting %s...done" browse-url-mosaic-program))))
1334 ;; --- Mosaic using CCI ---
1336 ;;;###autoload
1337 (defun browse-url-cci (url &optional new-window)
1338 "Ask the XMosaic WWW browser to load URL.
1339 Default to the URL around or before point.
1341 This function only works for XMosaic version 2.5 or later. You must
1342 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1343 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1345 When called interactively, if variable `browse-url-new-window-flag' is
1346 non-nil, load the document in a new browser window, otherwise use a
1347 random existing one. A non-nil interactive prefix argument reverses
1348 the effect of `browse-url-new-window-flag'.
1350 When called non-interactively, optional second argument NEW-WINDOW is
1351 used instead of `browse-url-new-window-flag'."
1352 (declare (obsolete nil "25.1"))
1353 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1354 (open-network-stream "browse-url" " *browse-url*"
1355 browse-url-CCI-host browse-url-CCI-port)
1356 ;; Todo: start browser if fails
1357 (process-send-string "browse-url"
1358 (concat "get url (" url ") output "
1359 (if (browse-url-maybe-new-window new-window)
1360 "new"
1361 "current")
1362 "\r\n"))
1363 (process-send-string "browse-url" "disconnect\r\n")
1364 (delete-process "browse-url"))
1366 ;; --- Conkeror ---
1367 ;;;###autoload
1368 (defun browse-url-conkeror (url &optional new-window)
1369 "Ask the Conkeror WWW browser to load URL.
1370 Default to the URL around or before point. Also pass the strings
1371 in the variable `browse-url-conkeror-arguments' to Conkeror.
1373 When called interactively, if variable
1374 `browse-url-new-window-flag' is non-nil, load the document in a
1375 new Conkeror window, otherwise use a random existing one. A
1376 non-nil interactive prefix argument reverses the effect of
1377 `browse-url-new-window-flag'.
1379 If variable `browse-url-conkeror-new-window-is-buffer' is
1380 non-nil, then whenever a document would otherwise be loaded in a
1381 new window, load it in a new buffer in an existing window instead.
1383 When called non-interactively, use optional second argument
1384 NEW-WINDOW instead of `browse-url-new-window-flag'."
1385 (interactive (browse-url-interactive-arg "URL: "))
1386 (setq url (browse-url-encode-url url))
1387 (let* ((process-environment (browse-url-process-environment)))
1388 (apply 'start-process (format "conkeror %s" url)
1390 browse-url-conkeror-program
1391 (append
1392 browse-url-conkeror-arguments
1393 (list
1394 "-e"
1395 (format "load_url_in_new_%s('%s')"
1396 (if (browse-url-maybe-new-window new-window)
1397 (if browse-url-conkeror-new-window-is-buffer
1398 "buffer"
1399 "window")
1400 "buffer")
1401 url))))))
1402 ;; --- W3 ---
1404 ;; External.
1405 (declare-function w3-fetch-other-window "ext:w3m" (&optional url))
1406 (declare-function w3-fetch "ext:w3m" (&optional url target))
1408 ;;;###autoload
1409 (defun browse-url-w3 (url &optional new-window)
1410 "Ask the w3 WWW browser to load URL.
1411 Default to the URL around or before point.
1413 When called interactively, if variable `browse-url-new-window-flag' is
1414 non-nil, load the document in a new window. A non-nil interactive
1415 prefix argument reverses the effect of `browse-url-new-window-flag'.
1417 When called non-interactively, optional second argument NEW-WINDOW is
1418 used instead of `browse-url-new-window-flag'."
1419 (interactive (browse-url-interactive-arg "W3 URL: "))
1420 (require 'w3) ; w3-fetch-other-window not autoloaded
1421 (if (browse-url-maybe-new-window new-window)
1422 (w3-fetch-other-window url)
1423 (w3-fetch url)))
1425 ;;;###autoload
1426 (defun browse-url-w3-gnudoit (url &optional _new-window)
1427 ;; new-window ignored
1428 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1429 The `browse-url-gnudoit-program' program is used with options given by
1430 `browse-url-gnudoit-args'. Default to the URL around or before point."
1431 (declare (obsolete nil "25.1"))
1432 (interactive (browse-url-interactive-arg "W3 URL: "))
1433 (apply 'start-process (concat "gnudoit:" url) nil
1434 browse-url-gnudoit-program
1435 (append browse-url-gnudoit-args
1436 (list (concat "(w3-fetch \"" url "\")")
1437 "(raise-frame)"))))
1439 ;; --- Lynx in an xterm ---
1441 ;;;###autoload
1442 (defun browse-url-text-xterm (url &optional _new-window)
1443 ;; new-window ignored
1444 "Ask a text browser to load URL.
1445 URL defaults to the URL around or before point.
1446 This runs the text browser specified by `browse-url-text-browser'.
1447 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1448 with possible additional arguments `browse-url-xterm-args'.
1449 The optional argument NEW-WINDOW is not used."
1450 (interactive (browse-url-interactive-arg "Text browser URL: "))
1451 (apply #'start-process `(,(concat browse-url-text-browser url)
1452 nil ,browse-url-xterm-program
1453 ,@browse-url-xterm-args "-e" ,browse-url-text-browser
1454 ,url)))
1456 ;; --- Lynx in an Emacs "term" window ---
1458 (declare-function term-char-mode "term" ())
1459 (declare-function term-send-down "term" ())
1460 (declare-function term-send-string "term" (proc str))
1462 ;;;###autoload
1463 (defun browse-url-text-emacs (url &optional new-buffer)
1464 "Ask a text browser to load URL.
1465 URL defaults to the URL around or before point.
1466 This runs the text browser specified by `browse-url-text-browser'.
1467 With a prefix argument, it runs a new browser process in a new buffer.
1469 When called interactively, if variable `browse-url-new-window-flag' is
1470 non-nil, load the document in a new browser process in a new term window,
1471 otherwise use any existing one. A non-nil interactive prefix argument
1472 reverses the effect of `browse-url-new-window-flag'.
1474 When called non-interactively, optional second argument NEW-WINDOW is
1475 used instead of `browse-url-new-window-flag'."
1476 (interactive (browse-url-interactive-arg "Text browser URL: "))
1477 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
1478 ;; (term-term-name "vt100") ; ??
1479 (buf (get-buffer "*text browser*"))
1480 (proc (and buf (get-buffer-process buf)))
1481 (n browse-url-text-input-attempts))
1482 (require 'term)
1483 (if (and (browse-url-maybe-new-window new-buffer) buf)
1484 ;; Rename away the OLD buffer. This isn't very polite, but
1485 ;; term insists on working in a buffer named *lynx* and would
1486 ;; choke on *lynx*<1>
1487 (progn (set-buffer buf)
1488 (rename-uniquely)))
1489 (if (or (browse-url-maybe-new-window new-buffer)
1490 (not buf)
1491 (not proc)
1492 (not (memq (process-status proc) '(run stop))))
1493 ;; start a new text browser
1494 (progn
1495 (setq buf
1496 (apply #'make-term
1497 `(,browse-url-text-browser
1498 ,browse-url-text-browser
1499 nil ,@browse-url-text-emacs-args
1500 ,url)))
1501 (switch-to-buffer buf)
1502 (term-char-mode)
1503 (set-process-sentinel
1504 (get-buffer-process buf)
1505 ;; Don't leave around a dead one (especially because of its
1506 ;; munged keymap.)
1507 (lambda (process _event)
1508 (if (not (memq (process-status process) '(run stop)))
1509 (let ((buf (process-buffer process)))
1510 (if buf (kill-buffer buf)))))))
1511 ;; Send the url to the text browser in the old buffer
1512 (let ((win (get-buffer-window buf t)))
1513 (if win
1514 (select-window win)
1515 (switch-to-buffer buf)))
1516 (if (eq (following-char) ?_)
1517 (cond ((eq browse-url-text-input-field 'warn)
1518 (error "Please move out of the input field first"))
1519 ((eq browse-url-text-input-field 'avoid)
1520 (while (and (eq (following-char) ?_) (> n 0))
1521 (term-send-down) ; down arrow
1522 (sit-for browse-url-text-input-delay))
1523 (if (eq (following-char) ?_)
1524 (error "Cannot move out of the input field, sorry")))))
1525 (term-send-string proc (concat "g" ; goto
1526 "\C-u" ; kill default url
1528 "\r")))))
1530 ;; --- mailto ---
1532 (autoload 'rfc2368-parse-mailto-url "rfc2368")
1534 ;;;###autoload
1535 (defun browse-url-mail (url &optional new-window)
1536 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1537 Default to using the mailto: URL around or before point as the
1538 recipient's address. Supplying a non-nil interactive prefix argument
1539 will cause the mail to be composed in another window rather than the
1540 current one.
1542 When called interactively, if variable `browse-url-new-window-flag' is
1543 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1544 non-nil interactive prefix argument reverses the effect of
1545 `browse-url-new-window-flag'.
1547 When called non-interactively, optional second argument NEW-WINDOW is
1548 used instead of `browse-url-new-window-flag'."
1549 (interactive (browse-url-interactive-arg "Mailto URL: "))
1550 (save-excursion
1551 (let* ((alist (rfc2368-parse-mailto-url url))
1552 (to (assoc "To" alist))
1553 (subject (assoc "Subject" alist))
1554 (body (assoc "Body" alist))
1555 (rest (delq to (delq subject (delq body alist))))
1556 (to (cdr to))
1557 (subject (cdr subject))
1558 (body (cdr body))
1559 (mail-citation-hook (unless body mail-citation-hook)))
1560 (if (browse-url-maybe-new-window new-window)
1561 (compose-mail-other-window to subject rest nil
1562 (list 'insert-buffer (current-buffer)))
1563 (compose-mail to subject rest nil nil
1564 (list 'insert-buffer (current-buffer))))
1565 (when body
1566 (goto-char (point-min))
1567 (unless (or (search-forward (concat "\n" mail-header-separator "\n")
1568 nil 'move)
1569 (bolp))
1570 (insert "\n"))
1571 (goto-char (prog1
1572 (point)
1573 (insert (replace-regexp-in-string "\r\n" "\n" body))
1574 (unless (bolp)
1575 (insert "\n"))))))))
1577 ;; --- man ---
1579 (defvar manual-program)
1581 (defun browse-url-man (url &optional _new-window)
1582 "Open a man page."
1583 (interactive (browse-url-interactive-arg "Man page URL: "))
1584 (require 'man)
1585 (setq url (replace-regexp-in-string "\\`man:" "" url))
1586 (cond
1587 ((executable-find manual-program) (man url))
1588 (t (woman (replace-regexp-in-string "([[:alnum:]]+)" "" url)))))
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 The optional argument NEW-WINDOW is not used."
1612 (interactive (browse-url-interactive-arg "KDE URL: "))
1613 (message "Sending URL to KDE...")
1614 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
1615 (append browse-url-kde-args (list url))))
1617 (defun browse-url-elinks-new-window (url)
1618 "Ask the Elinks WWW browser to load URL in a new window."
1619 (let ((process-environment (browse-url-process-environment)))
1620 (apply #'start-process
1621 (append (list (concat "elinks:" url)
1622 nil)
1623 browse-url-elinks-wrapper
1624 (list "elinks" url)))))
1626 ;;;###autoload
1627 (defun browse-url-elinks (url &optional new-window)
1628 "Ask the Elinks WWW browser to load URL.
1629 Default to the URL around the point.
1631 The document is loaded in a new tab of a running Elinks or, if
1632 none yet running, a newly started instance.
1634 The Elinks command will be prepended by the program+arguments
1635 from `browse-url-elinks-wrapper'."
1636 (interactive (browse-url-interactive-arg "URL: "))
1637 (setq url (browse-url-encode-url url))
1638 (if new-window
1639 (browse-url-elinks-new-window url)
1640 (let ((process-environment (browse-url-process-environment))
1641 (elinks-ping-process (start-process "elinks-ping" nil
1642 "elinks" "-remote" "ping()")))
1643 (set-process-sentinel elinks-ping-process
1644 `(lambda (process change)
1645 (browse-url-elinks-sentinel process ,url))))))
1647 (defun browse-url-elinks-sentinel (process url)
1648 "Determines if Elinks is running or a new one has to be started."
1649 ;; Try to determine if an instance is running or if we have to
1650 ;; create a new one.
1651 (pcase (process-exit-status process)
1653 ;; No instance, start a new one.
1654 (browse-url-elinks-new-window url))
1656 ;; Found an instance, open URL in new tab.
1657 (let ((process-environment (browse-url-process-environment)))
1658 (start-process (concat "elinks:" url) nil
1659 "elinks" "-remote"
1660 (concat "openURL(\"" url "\",new-tab)"))))
1661 (exit-status
1662 (error "Unrecognized exit-code %d of process `elinks'"
1663 exit-status))))
1665 (provide 'browse-url)
1667 ;;; browse-url.el ends here