Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / lisp / net / browse-url.el
blob3b5169d4734bfa3d24e4ba83338a404eceebc647
1 ;;; browse-url.el --- pass a URL to a WWW browser
3 ;; Copyright (C) 1995-2018 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 <https://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 (or file-name-coding-system
717 default-file-name-coding-system)))))
718 (if coding (setq file (encode-coding-string file coding))))
719 (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
720 (dolist (map browse-url-filename-alist)
721 (when (and map (string-match (car map) file))
722 (setq file (replace-match (cdr map) t nil file))))
723 file)
725 ;;;###autoload
726 (defun browse-url-of-buffer (&optional buffer)
727 "Ask a WWW browser to display BUFFER.
728 Display the current buffer if BUFFER is nil. Display only the
729 currently visible part of BUFFER (from a temporary file) if buffer is
730 narrowed."
731 (interactive)
732 (save-excursion
733 (and buffer (set-buffer buffer))
734 (let ((file-name
735 ;; Ignore real name if restricted
736 (and (not (buffer-narrowed-p))
737 (or buffer-file-name
738 (and (boundp 'dired-directory) dired-directory)))))
739 (or file-name
740 (progn
741 (or browse-url-temp-file-name
742 (setq browse-url-temp-file-name
743 (convert-standard-filename
744 (make-temp-file
745 (expand-file-name "burl" browse-url-temp-dir)
746 nil ".html"))))
747 (setq file-name browse-url-temp-file-name)
748 (write-region (point-min) (point-max) file-name nil 'no-message)))
749 (browse-url-of-file file-name))))
751 (defun browse-url-delete-temp-file (&optional temp-file-name)
752 ;; Delete browse-url-temp-file-name from the file system
753 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
754 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
755 (if (and file-name (file-exists-p file-name))
756 (delete-file file-name))))
758 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
760 (declare-function dired-get-filename "dired"
761 (&optional localp no-error-if-not-filep))
763 ;;;###autoload
764 (defun browse-url-of-dired-file ()
765 "In Dired, ask a WWW browser to display the file named on this line."
766 (interactive)
767 (let ((tem (dired-get-filename t t)))
768 (if tem
769 (browse-url-of-file (expand-file-name tem))
770 (error "No file on this line"))))
772 ;;;###autoload
773 (defun browse-url-of-region (min max)
774 "Ask a WWW browser to display the current region."
775 (interactive "r")
776 (save-excursion
777 (save-restriction
778 (narrow-to-region min max)
779 (browse-url-of-buffer))))
781 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
782 ;; Browser-independent commands
784 ;; A generic command to call the current browse-url-browser-function
786 ;;;###autoload
787 (defun browse-url (url &rest args)
788 "Ask a WWW browser to load URL.
789 Prompt for a URL, defaulting to the URL at or before point.
790 Invokes a suitable browser function which does the actual job.
791 The variable `browse-url-browser-function' says which browser function to
792 use. If the URL is a mailto: URL, consult `browse-url-mailto-function'
793 first, if that exists.
795 The additional ARGS are passed to the browser function. See the doc
796 strings of the actual functions, starting with `browse-url-browser-function',
797 for information about the significance of ARGS (most of the functions
798 ignore it).
799 If ARGS are omitted, the default is to pass `browse-url-new-window-flag'
800 as ARGS."
801 (interactive (browse-url-interactive-arg "URL: "))
802 (unless (called-interactively-p 'interactive)
803 (setq args (or args (list browse-url-new-window-flag))))
804 (when (and url-handler-mode (not (file-name-absolute-p url)))
805 (setq url (expand-file-name url)))
806 (let ((process-environment (copy-sequence process-environment))
807 (function (or (and (string-match "\\`mailto:" url)
808 browse-url-mailto-function)
809 (and (string-match "\\`man:" url)
810 browse-url-man-function)
811 browse-url-browser-function))
812 ;; Ensure that `default-directory' exists and is readable (b#6077).
813 (default-directory (or (unhandled-file-name-directory default-directory)
814 (expand-file-name "~/"))))
815 ;; When connected to various displays, be careful to use the display of
816 ;; the currently selected frame, rather than the original start display,
817 ;; which may not even exist any more.
818 (if (stringp (frame-parameter nil 'display))
819 (setenv "DISPLAY" (frame-parameter nil 'display)))
820 (if (and (consp function)
821 (not (functionp function)))
822 ;; The `function' can be an alist; look down it for first match
823 ;; and apply the function (which might be a lambda).
824 (catch 'done
825 (dolist (bf function)
826 (when (string-match (car bf) url)
827 (apply (cdr bf) url args)
828 (throw 'done t)))
829 (error "No browse-url-browser-function matching URL %s"
830 url))
831 ;; Unbound symbols go down this leg, since void-function from
832 ;; apply is clearer than wrong-type-argument from dolist.
833 (apply function url args))))
835 ;;;###autoload
836 (defun browse-url-at-point (&optional arg)
837 "Ask a WWW browser to load the URL at or before point.
838 Variable `browse-url-browser-function' says which browser to use.
839 Optional prefix argument ARG non-nil inverts the value of the option
840 `browse-url-new-window-flag'."
841 (interactive "P")
842 (let ((url (browse-url-url-at-point)))
843 (if url
844 (browse-url url (if arg
845 (not browse-url-new-window-flag)
846 browse-url-new-window-flag))
847 (error "No URL found"))))
849 ;;;###autoload
850 (defun browse-url-at-mouse (event)
851 "Ask a WWW browser to load a URL clicked with the mouse.
852 The URL is the one around or before the position of the mouse click
853 but point is not changed. Variable `browse-url-browser-function'
854 says which browser to use."
855 (interactive "e")
856 (save-excursion
857 (mouse-set-point event)
858 ;; This handles browse-url-new-window-flag properly
859 ;; when it gets no arg.
860 (browse-url-at-point)))
862 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
863 ;; Browser-specific commands
865 ;; --- Default MS-Windows browser ---
867 (defvar dos-windows-version)
868 (declare-function w32-shell-execute "w32fns.c") ;; Defined in C.
870 (defun browse-url-default-windows-browser (url &optional _new-window)
871 "Invoke the MS-Windows system's default Web browser.
872 The optional NEW-WINDOW argument is not used."
873 (interactive (browse-url-interactive-arg "URL: "))
874 (cond ((eq system-type 'ms-dos)
875 (if dos-windows-version
876 (shell-command (concat "start " (shell-quote-argument url)))
877 (error "Browsing URLs is not supported on this system")))
878 ((eq system-type 'cygwin)
879 (call-process "cygstart" nil nil nil url))
880 (t (w32-shell-execute "open" (url-unhex-string url)))))
882 (defun browse-url-default-macosx-browser (url &optional _new-window)
883 "Invoke the macOS system's default Web browser.
884 The optional NEW-WINDOW argument is not used"
885 (interactive (browse-url-interactive-arg "URL: "))
886 (start-process (concat "open " url) nil "open" url))
888 ;; --- Netscape ---
890 (defun browse-url-process-environment ()
891 "Set DISPLAY in the environment to the X display the browser will use.
892 This is either the value of variable `browse-url-browser-display' if
893 non-nil, or the same display as Emacs if different from the current
894 environment, otherwise just use the current environment."
895 (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
896 (if display
897 (cons (concat "DISPLAY=" display) process-environment)
898 process-environment)))
900 (defun browse-url-emacs-display ()
901 "Return the X display Emacs is running on.
902 This is nil if the display is the same as the DISPLAY environment variable.
904 Actually Emacs could be using several displays; this just returns the
905 one showing the selected frame."
906 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
907 (and (not (equal display (getenv "DISPLAY")))
908 display)))
910 (defun browse-url-default-browser (url &rest args)
911 "Find a suitable browser and ask it to load URL.
912 Default to the URL around or before point.
914 When called interactively, if variable `browse-url-new-window-flag' is
915 non-nil, load the document in a new window, if possible, otherwise use
916 a random existing one. A non-nil interactive prefix argument reverses
917 the effect of `browse-url-new-window-flag'.
919 When called non-interactively, optional second argument ARGS is used
920 instead of `browse-url-new-window-flag'."
921 (apply
922 (cond
923 ((memq system-type '(windows-nt ms-dos cygwin))
924 'browse-url-default-windows-browser)
925 ((memq system-type '(darwin))
926 'browse-url-default-macosx-browser)
927 ((browse-url-can-use-xdg-open) 'browse-url-xdg-open)
928 ;;; ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
929 ((executable-find browse-url-mozilla-program) 'browse-url-mozilla)
930 ((executable-find browse-url-firefox-program) 'browse-url-firefox)
931 ((executable-find browse-url-chromium-program) 'browse-url-chromium)
932 ;;; ((executable-find browse-url-galeon-program) 'browse-url-galeon)
933 ((executable-find browse-url-kde-program) 'browse-url-kde)
934 ;;; ((executable-find browse-url-netscape-program) 'browse-url-netscape)
935 ;;; ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
936 ((executable-find browse-url-conkeror-program) 'browse-url-conkeror)
937 ((executable-find browse-url-chrome-program) 'browse-url-chrome)
938 ((executable-find browse-url-xterm-program) 'browse-url-text-xterm)
939 ((locate-library "w3") 'browse-url-w3)
941 (lambda (&rest _ignore) (error "No usable browser found"))))
942 url args))
944 (defun browse-url-can-use-xdg-open ()
945 "Return non-nil if the \"xdg-open\" program can be used.
946 xdg-open is a desktop utility that calls your preferred web browser."
947 ;; The exact set of situations where xdg-open works is complicated,
948 ;; and it would be a pain to duplicate xdg-open's situation-specific
949 ;; code here, as the code is a moving target. So assume that
950 ;; xdg-open will work if there is a graphical display; this should
951 ;; be good enough for platforms Emacs is likely to be running on.
952 (and (or (getenv "DISPLAY") (getenv "WAYLAND_DISPLAY"))
953 (executable-find "xdg-open")))
955 ;;;###autoload
956 (defun browse-url-xdg-open (url &optional ignored)
957 "Pass the specified URL to the \"xdg-open\" command.
958 xdg-open is a desktop utility that calls your preferred web browser.
959 The optional argument IGNORED is not used."
960 (interactive (browse-url-interactive-arg "URL: "))
961 (call-process "xdg-open" nil 0 nil url))
963 ;;;###autoload
964 (defun browse-url-netscape (url &optional new-window)
965 "Ask the Netscape WWW browser to load URL.
966 Default to the URL around or before point. The strings in variable
967 `browse-url-netscape-arguments' are also passed to Netscape.
969 When called interactively, if variable `browse-url-new-window-flag' is
970 non-nil, load the document in a new Netscape window, otherwise use a
971 random existing one. A non-nil interactive prefix argument reverses
972 the effect of `browse-url-new-window-flag'.
974 If `browse-url-netscape-new-window-is-tab' is non-nil, then
975 whenever a document would otherwise be loaded in a new window, it
976 is loaded in a new tab in an existing window instead.
978 When called non-interactively, optional second argument NEW-WINDOW is
979 used instead of `browse-url-new-window-flag'."
980 (declare (obsolete nil "25.1"))
981 (interactive (browse-url-interactive-arg "URL: "))
982 (setq url (browse-url-encode-url url))
983 (let* ((process-environment (browse-url-process-environment))
984 (process
985 (apply 'start-process
986 (concat "netscape " url) nil
987 browse-url-netscape-program
988 (append
989 browse-url-netscape-arguments
990 (if (eq window-system 'w32)
991 (list url)
992 (append
993 (if new-window '("-noraise"))
994 (list "-remote"
995 (concat "openURL(" url
996 (if (browse-url-maybe-new-window
997 new-window)
998 (if browse-url-netscape-new-window-is-tab
999 ",new-tab"
1000 ",new-window"))
1001 ")"))))))))
1002 (set-process-sentinel process
1003 `(lambda (process change)
1004 (browse-url-netscape-sentinel process ,url)))))
1006 (defun browse-url-netscape-sentinel (process url)
1007 "Handle a change to the process communicating with Netscape."
1008 (declare (obsolete nil "25.1"))
1009 (or (eq (process-exit-status process) 0)
1010 (let* ((process-environment (browse-url-process-environment)))
1011 ;; Netscape not running - start it
1012 (message "Starting %s..." browse-url-netscape-program)
1013 (apply 'start-process (concat "netscape" url) nil
1014 browse-url-netscape-program
1015 (append browse-url-netscape-startup-arguments (list url))))))
1017 (defun browse-url-netscape-reload ()
1018 "Ask Netscape to reload its current document.
1019 How depends on `browse-url-netscape-version'."
1020 (declare (obsolete nil "25.1"))
1021 (interactive)
1022 ;; Backwards incompatibility reported by
1023 ;; <peter.kruse@psychologie.uni-regensburg.de>.
1024 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
1025 "xfeDoCommand(reload)"
1026 "reload")))
1028 (defun browse-url-netscape-send (command)
1029 "Send a remote control command to Netscape."
1030 (declare (obsolete nil "25.1"))
1031 (let* ((process-environment (browse-url-process-environment)))
1032 (apply 'start-process "netscape" nil
1033 browse-url-netscape-program
1034 (append browse-url-netscape-arguments
1035 (list "-remote" command)))))
1037 ;;;###autoload
1038 (defun browse-url-mozilla (url &optional new-window)
1039 "Ask the Mozilla WWW browser to load URL.
1040 Default to the URL around or before point. The strings in variable
1041 `browse-url-mozilla-arguments' are also passed to Mozilla.
1043 When called interactively, if variable `browse-url-new-window-flag' is
1044 non-nil, load the document in a new Mozilla window, otherwise use a
1045 random existing one. A non-nil interactive prefix argument reverses
1046 the effect of `browse-url-new-window-flag'.
1048 If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
1049 document would otherwise be loaded in a new window, it is loaded in a
1050 new tab in an existing window instead.
1052 When called non-interactively, optional second argument NEW-WINDOW is
1053 used instead of `browse-url-new-window-flag'."
1054 (interactive (browse-url-interactive-arg "URL: "))
1055 (setq url (browse-url-encode-url url))
1056 (let* ((process-environment (browse-url-process-environment))
1057 (process
1058 (apply 'start-process
1059 (concat "mozilla " url) nil
1060 browse-url-mozilla-program
1061 (append
1062 browse-url-mozilla-arguments
1063 (list "-remote"
1064 (concat "openURL("
1066 (if (browse-url-maybe-new-window
1067 new-window)
1068 (if browse-url-mozilla-new-window-is-tab
1069 ",new-tab"
1070 ",new-window"))
1071 ")"))))))
1072 (set-process-sentinel process
1073 `(lambda (process change)
1074 (browse-url-mozilla-sentinel process ,url)))))
1076 (defun browse-url-mozilla-sentinel (process url)
1077 "Handle a change to the process communicating with Mozilla."
1078 (or (eq (process-exit-status process) 0)
1079 (let* ((process-environment (browse-url-process-environment)))
1080 ;; Mozilla is not running - start it
1081 (message "Starting %s..." browse-url-mozilla-program)
1082 (apply 'start-process (concat "mozilla " url) nil
1083 browse-url-mozilla-program
1084 (append browse-url-mozilla-startup-arguments (list url))))))
1086 ;;;###autoload
1087 (defun browse-url-firefox (url &optional new-window)
1088 "Ask the Firefox WWW browser to load URL.
1089 Defaults to the URL around or before point. Passes the strings
1090 in the variable `browse-url-firefox-arguments' to Firefox.
1092 Interactively, if the variable `browse-url-new-window-flag' is non-nil,
1093 loads the document in a new Firefox window. A non-nil prefix argument
1094 reverses the effect of `browse-url-new-window-flag'.
1096 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1097 whenever a document would otherwise be loaded in a new window, it
1098 is loaded in a new tab in an existing window instead.
1100 Non-interactively, this uses the optional second argument NEW-WINDOW
1101 instead of `browse-url-new-window-flag'."
1102 (interactive (browse-url-interactive-arg "URL: "))
1103 (setq url (browse-url-encode-url url))
1104 (let* ((process-environment (browse-url-process-environment)))
1105 (apply 'start-process
1106 (concat "firefox " url) nil
1107 browse-url-firefox-program
1108 (append
1109 browse-url-firefox-arguments
1110 (if (browse-url-maybe-new-window new-window)
1111 (if browse-url-firefox-new-window-is-tab
1112 '("-new-tab")
1113 '("-new-window")))
1114 (list url)))))
1116 ;;;###autoload
1117 (defun browse-url-chromium (url &optional _new-window)
1118 "Ask the Chromium WWW browser to load URL.
1119 Default to the URL around or before point. The strings in
1120 variable `browse-url-chromium-arguments' are also passed to
1121 Chromium.
1122 The optional argument NEW-WINDOW is not used."
1123 (interactive (browse-url-interactive-arg "URL: "))
1124 (setq url (browse-url-encode-url url))
1125 (let* ((process-environment (browse-url-process-environment)))
1126 (apply 'start-process
1127 (concat "chromium " url) nil
1128 browse-url-chromium-program
1129 (append
1130 browse-url-chromium-arguments
1131 (list url)))))
1133 (defun browse-url-chrome (url &optional _new-window)
1134 "Ask the Google Chrome WWW browser to load URL.
1135 Default to the URL around or before point. The strings in
1136 variable `browse-url-chrome-arguments' are also passed to
1137 Google Chrome.
1138 The optional argument NEW-WINDOW is not used."
1139 (interactive (browse-url-interactive-arg "URL: "))
1140 (setq url (browse-url-encode-url url))
1141 (let* ((process-environment (browse-url-process-environment)))
1142 (apply 'start-process
1143 (concat "google-chrome " url) nil
1144 browse-url-chrome-program
1145 (append
1146 browse-url-chrome-arguments
1147 (list url)))))
1149 ;;;###autoload
1150 (defun browse-url-galeon (url &optional new-window)
1151 "Ask the Galeon WWW browser to load URL.
1152 Default to the URL around or before point. The strings in variable
1153 `browse-url-galeon-arguments' are also passed to Galeon.
1155 When called interactively, if variable `browse-url-new-window-flag' is
1156 non-nil, load the document in a new Galeon window, otherwise use a
1157 random existing one. A non-nil interactive prefix argument reverses
1158 the effect of `browse-url-new-window-flag'.
1160 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1161 document would otherwise be loaded in a new window, it is loaded in a
1162 new tab in an existing window instead.
1164 When called non-interactively, optional second argument NEW-WINDOW is
1165 used instead of `browse-url-new-window-flag'."
1166 (declare (obsolete nil "25.1"))
1167 (interactive (browse-url-interactive-arg "URL: "))
1168 (setq url (browse-url-encode-url url))
1169 (let* ((process-environment (browse-url-process-environment))
1170 (process (apply 'start-process
1171 (concat "galeon " url)
1173 browse-url-galeon-program
1174 (append
1175 browse-url-galeon-arguments
1176 (if (browse-url-maybe-new-window new-window)
1177 (if browse-url-galeon-new-window-is-tab
1178 '("--new-tab")
1179 '("--new-window" "--noraise"))
1180 '("--existing"))
1181 (list url)))))
1182 (set-process-sentinel process
1183 `(lambda (process change)
1184 (browse-url-galeon-sentinel process ,url)))))
1186 (defun browse-url-galeon-sentinel (process url)
1187 "Handle a change to the process communicating with Galeon."
1188 (declare (obsolete nil "25.1"))
1189 (or (eq (process-exit-status process) 0)
1190 (let* ((process-environment (browse-url-process-environment)))
1191 ;; Galeon is not running - start it
1192 (message "Starting %s..." browse-url-galeon-program)
1193 (apply 'start-process (concat "galeon " url) nil
1194 browse-url-galeon-program
1195 (append browse-url-galeon-startup-arguments (list url))))))
1197 (defun browse-url-epiphany (url &optional new-window)
1198 "Ask the Epiphany WWW browser to load URL.
1199 Default to the URL around or before point. The strings in variable
1200 `browse-url-galeon-arguments' are also passed to Epiphany.
1202 When called interactively, if variable `browse-url-new-window-flag' is
1203 non-nil, load the document in a new Epiphany window, otherwise use a
1204 random existing one. A non-nil interactive prefix argument reverses
1205 the effect of `browse-url-new-window-flag'.
1207 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1208 document would otherwise be loaded in a new window, it is loaded in a
1209 new tab in an existing window instead.
1211 When called non-interactively, optional second argument NEW-WINDOW is
1212 used instead of `browse-url-new-window-flag'."
1213 (interactive (browse-url-interactive-arg "URL: "))
1214 (setq url (browse-url-encode-url url))
1215 (let* ((process-environment (browse-url-process-environment))
1216 (process (apply 'start-process
1217 (concat "epiphany " url)
1219 browse-url-epiphany-program
1220 (append
1221 browse-url-epiphany-arguments
1222 (if (browse-url-maybe-new-window new-window)
1223 (if browse-url-epiphany-new-window-is-tab
1224 '("--new-tab")
1225 '("--new-window" "--noraise"))
1226 '("--existing"))
1227 (list url)))))
1228 (set-process-sentinel process
1229 `(lambda (process change)
1230 (browse-url-epiphany-sentinel process ,url)))))
1232 (defun browse-url-epiphany-sentinel (process url)
1233 "Handle a change to the process communicating with Epiphany."
1234 (or (eq (process-exit-status process) 0)
1235 (let* ((process-environment (browse-url-process-environment)))
1236 ;; Epiphany is not running - start it
1237 (message "Starting %s..." browse-url-epiphany-program)
1238 (apply 'start-process (concat "epiphany " url) nil
1239 browse-url-epiphany-program
1240 (append browse-url-epiphany-startup-arguments (list url))))))
1242 (defvar url-handler-regexp)
1244 ;;;###autoload
1245 (defun browse-url-emacs (url &optional same-window)
1246 "Ask Emacs to load URL into a buffer and show it in another window."
1247 (interactive (browse-url-interactive-arg "URL: "))
1248 (require 'url-handlers)
1249 (let ((file-name-handler-alist
1250 (cons (cons url-handler-regexp 'url-file-handler)
1251 file-name-handler-alist)))
1252 (if same-window (find-file url) (find-file-other-window url))))
1254 ;;;###autoload
1255 (defun browse-url-gnome-moz (url &optional new-window)
1256 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1257 Default to the URL around or before point. The strings in variable
1258 `browse-url-gnome-moz-arguments' are also passed.
1260 When called interactively, if variable `browse-url-new-window-flag' is
1261 non-nil, load the document in a new browser window, otherwise use an
1262 existing one. A non-nil interactive prefix argument reverses the
1263 effect of `browse-url-new-window-flag'.
1265 When called non-interactively, optional second argument NEW-WINDOW is
1266 used instead of `browse-url-new-window-flag'."
1267 (declare (obsolete nil "25.1"))
1268 (interactive (browse-url-interactive-arg "URL: "))
1269 (apply 'start-process (concat "gnome-moz-remote " url)
1271 browse-url-gnome-moz-program
1272 (append
1273 browse-url-gnome-moz-arguments
1274 (if (browse-url-maybe-new-window new-window)
1275 '("--newwin"))
1276 (list "--raise" url))))
1278 ;; --- Mosaic ---
1280 ;;;###autoload
1281 (defun browse-url-mosaic (url &optional new-window)
1282 "Ask the XMosaic WWW browser to load URL.
1284 Default to the URL around or before point. The strings in variable
1285 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1286 program is invoked according to the variable
1287 `browse-url-mosaic-program'.
1289 When called interactively, if variable `browse-url-new-window-flag' is
1290 non-nil, load the document in a new Mosaic window, otherwise use a
1291 random existing one. A non-nil interactive prefix argument reverses
1292 the effect of `browse-url-new-window-flag'.
1294 When called non-interactively, optional second argument NEW-WINDOW is
1295 used instead of `browse-url-new-window-flag'."
1296 (declare (obsolete nil "25.1"))
1297 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1298 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
1299 pid)
1300 (if (file-readable-p pidfile)
1301 (with-temp-buffer
1302 (insert-file-contents pidfile)
1303 (setq pid (read (current-buffer)))))
1304 (if (and (integerp pid) (zerop (signal-process pid 0))) ; Mosaic running
1305 (progn
1306 (with-temp-buffer
1307 (insert (if (browse-url-maybe-new-window new-window)
1308 "newwin\n"
1309 "goto\n")
1310 url "\n")
1311 (with-file-modes ?\700
1312 (if (file-exists-p
1313 (setq pidfile (format "/tmp/Mosaic.%d" pid)))
1314 (delete-file pidfile))
1315 ;; https://debbugs.gnu.org/17428. Use O_EXCL.
1316 (write-region nil nil pidfile nil 'silent nil 'excl)))
1317 ;; Send signal SIGUSR to Mosaic
1318 (message "Signaling Mosaic...")
1319 (signal-process pid 'SIGUSR1)
1320 ;; Or you could try:
1321 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1322 (message "Signaling Mosaic...done"))
1323 ;; Mosaic not running - start it
1324 (message "Starting %s..." browse-url-mosaic-program)
1325 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1326 (append browse-url-mosaic-arguments (list url)))
1327 (message "Starting %s...done" browse-url-mosaic-program))))
1329 ;; --- Mosaic using CCI ---
1331 ;;;###autoload
1332 (defun browse-url-cci (url &optional new-window)
1333 "Ask the XMosaic WWW browser to load URL.
1334 Default to the URL around or before point.
1336 This function only works for XMosaic version 2.5 or later. You must
1337 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1338 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1340 When called interactively, if variable `browse-url-new-window-flag' is
1341 non-nil, load the document in a new browser window, otherwise use a
1342 random existing one. A non-nil interactive prefix argument reverses
1343 the effect of `browse-url-new-window-flag'.
1345 When called non-interactively, optional second argument NEW-WINDOW is
1346 used instead of `browse-url-new-window-flag'."
1347 (declare (obsolete nil "25.1"))
1348 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1349 (open-network-stream "browse-url" " *browse-url*"
1350 browse-url-CCI-host browse-url-CCI-port)
1351 ;; Todo: start browser if fails
1352 (process-send-string "browse-url"
1353 (concat "get url (" url ") output "
1354 (if (browse-url-maybe-new-window new-window)
1355 "new"
1356 "current")
1357 "\r\n"))
1358 (process-send-string "browse-url" "disconnect\r\n")
1359 (delete-process "browse-url"))
1361 ;; --- Conkeror ---
1362 ;;;###autoload
1363 (defun browse-url-conkeror (url &optional new-window)
1364 "Ask the Conkeror WWW browser to load URL.
1365 Default to the URL around or before point. Also pass the strings
1366 in the variable `browse-url-conkeror-arguments' to Conkeror.
1368 When called interactively, if variable
1369 `browse-url-new-window-flag' is non-nil, load the document in a
1370 new Conkeror window, otherwise use a random existing one. A
1371 non-nil interactive prefix argument reverses the effect of
1372 `browse-url-new-window-flag'.
1374 If variable `browse-url-conkeror-new-window-is-buffer' is
1375 non-nil, then whenever a document would otherwise be loaded in a
1376 new window, load it in a new buffer in an existing window instead.
1378 When called non-interactively, use optional second argument
1379 NEW-WINDOW instead of `browse-url-new-window-flag'."
1380 (interactive (browse-url-interactive-arg "URL: "))
1381 (setq url (browse-url-encode-url url))
1382 (let* ((process-environment (browse-url-process-environment)))
1383 (apply 'start-process (format "conkeror %s" url)
1385 browse-url-conkeror-program
1386 (append
1387 browse-url-conkeror-arguments
1388 (list
1389 "-e"
1390 (format "load_url_in_new_%s('%s')"
1391 (if (browse-url-maybe-new-window new-window)
1392 (if browse-url-conkeror-new-window-is-buffer
1393 "buffer"
1394 "window")
1395 "buffer")
1396 url))))))
1397 ;; --- W3 ---
1399 ;; External.
1400 (declare-function w3-fetch-other-window "ext:w3m" (&optional url))
1401 (declare-function w3-fetch "ext:w3m" (&optional url target))
1403 ;;;###autoload
1404 (defun browse-url-w3 (url &optional new-window)
1405 "Ask the w3 WWW browser to load URL.
1406 Default to the URL around or before point.
1408 When called interactively, if variable `browse-url-new-window-flag' is
1409 non-nil, load the document in a new window. A non-nil interactive
1410 prefix argument reverses the effect of `browse-url-new-window-flag'.
1412 When called non-interactively, optional second argument NEW-WINDOW is
1413 used instead of `browse-url-new-window-flag'."
1414 (interactive (browse-url-interactive-arg "W3 URL: "))
1415 (require 'w3) ; w3-fetch-other-window not autoloaded
1416 (if (browse-url-maybe-new-window new-window)
1417 (w3-fetch-other-window url)
1418 (w3-fetch url)))
1420 ;;;###autoload
1421 (defun browse-url-w3-gnudoit (url &optional _new-window)
1422 ;; new-window ignored
1423 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1424 The `browse-url-gnudoit-program' program is used with options given by
1425 `browse-url-gnudoit-args'. Default to the URL around or before point."
1426 (declare (obsolete nil "25.1"))
1427 (interactive (browse-url-interactive-arg "W3 URL: "))
1428 (apply 'start-process (concat "gnudoit:" url) nil
1429 browse-url-gnudoit-program
1430 (append browse-url-gnudoit-args
1431 (list (concat "(w3-fetch \"" url "\")")
1432 "(raise-frame)"))))
1434 ;; --- Lynx in an xterm ---
1436 ;;;###autoload
1437 (defun browse-url-text-xterm (url &optional _new-window)
1438 ;; new-window ignored
1439 "Ask a text browser to load URL.
1440 URL defaults to the URL around or before point.
1441 This runs the text browser specified by `browse-url-text-browser'.
1442 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1443 with possible additional arguments `browse-url-xterm-args'.
1444 The optional argument NEW-WINDOW is not used."
1445 (interactive (browse-url-interactive-arg "Text browser URL: "))
1446 (apply #'start-process `(,(concat browse-url-text-browser url)
1447 nil ,browse-url-xterm-program
1448 ,@browse-url-xterm-args "-e" ,browse-url-text-browser
1449 ,url)))
1451 ;; --- Lynx in an Emacs "term" window ---
1453 (declare-function term-char-mode "term" ())
1454 (declare-function term-send-down "term" ())
1455 (declare-function term-send-string "term" (proc str))
1457 ;;;###autoload
1458 (defun browse-url-text-emacs (url &optional new-buffer)
1459 "Ask a text browser to load URL.
1460 URL defaults to the URL around or before point.
1461 This runs the text browser specified by `browse-url-text-browser'.
1462 With a prefix argument, it runs a new browser process in a new buffer.
1464 When called interactively, if variable `browse-url-new-window-flag' is
1465 non-nil, load the document in a new browser process in a new term window,
1466 otherwise use any existing one. A non-nil interactive prefix argument
1467 reverses the effect of `browse-url-new-window-flag'.
1469 When called non-interactively, optional second argument NEW-WINDOW is
1470 used instead of `browse-url-new-window-flag'."
1471 (interactive (browse-url-interactive-arg "Text browser URL: "))
1472 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
1473 ;; (term-term-name "vt100") ; ??
1474 (buf (get-buffer "*text browser*"))
1475 (proc (and buf (get-buffer-process buf)))
1476 (n browse-url-text-input-attempts))
1477 (require 'term)
1478 (if (and (browse-url-maybe-new-window new-buffer) buf)
1479 ;; Rename away the OLD buffer. This isn't very polite, but
1480 ;; term insists on working in a buffer named *lynx* and would
1481 ;; choke on *lynx*<1>
1482 (progn (set-buffer buf)
1483 (rename-uniquely)))
1484 (if (or (browse-url-maybe-new-window new-buffer)
1485 (not buf)
1486 (not proc)
1487 (not (memq (process-status proc) '(run stop))))
1488 ;; start a new text browser
1489 (progn
1490 (setq buf
1491 (apply #'make-term
1492 `(,browse-url-text-browser
1493 ,browse-url-text-browser
1494 nil ,@browse-url-text-emacs-args
1495 ,url)))
1496 (switch-to-buffer buf)
1497 (term-char-mode)
1498 (set-process-sentinel
1499 (get-buffer-process buf)
1500 ;; Don't leave around a dead one (especially because of its
1501 ;; munged keymap.)
1502 (lambda (process _event)
1503 (if (not (memq (process-status process) '(run stop)))
1504 (let ((buf (process-buffer process)))
1505 (if buf (kill-buffer buf)))))))
1506 ;; Send the url to the text browser in the old buffer
1507 (let ((win (get-buffer-window buf t)))
1508 (if win
1509 (select-window win)
1510 (switch-to-buffer buf)))
1511 (if (eq (following-char) ?_)
1512 (cond ((eq browse-url-text-input-field 'warn)
1513 (error "Please move out of the input field first"))
1514 ((eq browse-url-text-input-field 'avoid)
1515 (while (and (eq (following-char) ?_) (> n 0))
1516 (term-send-down) ; down arrow
1517 (sit-for browse-url-text-input-delay))
1518 (if (eq (following-char) ?_)
1519 (error "Cannot move out of the input field, sorry")))))
1520 (term-send-string proc (concat "g" ; goto
1521 "\C-u" ; kill default url
1523 "\r")))))
1525 ;; --- mailto ---
1527 (autoload 'rfc2368-parse-mailto-url "rfc2368")
1529 ;;;###autoload
1530 (defun browse-url-mail (url &optional new-window)
1531 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1532 Default to using the mailto: URL around or before point as the
1533 recipient's address. Supplying a non-nil interactive prefix argument
1534 will cause the mail to be composed in another window rather than the
1535 current one.
1537 When called interactively, if variable `browse-url-new-window-flag' is
1538 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1539 non-nil interactive prefix argument reverses the effect of
1540 `browse-url-new-window-flag'.
1542 When called non-interactively, optional second argument NEW-WINDOW is
1543 used instead of `browse-url-new-window-flag'."
1544 (interactive (browse-url-interactive-arg "Mailto URL: "))
1545 (save-excursion
1546 (let* ((alist (rfc2368-parse-mailto-url url))
1547 (to (assoc "To" alist))
1548 (subject (assoc "Subject" alist))
1549 (body (assoc "Body" alist))
1550 (rest (delq to (delq subject (delq body alist))))
1551 (to (cdr to))
1552 (subject (cdr subject))
1553 (body (cdr body))
1554 (mail-citation-hook (unless body mail-citation-hook)))
1555 (if (browse-url-maybe-new-window new-window)
1556 (compose-mail-other-window to subject rest nil
1557 (list 'insert-buffer (current-buffer)))
1558 (compose-mail to subject rest nil nil
1559 (list 'insert-buffer (current-buffer))))
1560 (when body
1561 (goto-char (point-min))
1562 (unless (or (search-forward (concat "\n" mail-header-separator "\n")
1563 nil 'move)
1564 (bolp))
1565 (insert "\n"))
1566 (goto-char (prog1
1567 (point)
1568 (insert (replace-regexp-in-string "\r\n" "\n" body))
1569 (unless (bolp)
1570 (insert "\n"))))))))
1572 ;; --- man ---
1574 (defvar manual-program)
1576 (defun browse-url-man (url &optional _new-window)
1577 "Open a man page."
1578 (interactive (browse-url-interactive-arg "Man page URL: "))
1579 (require 'man)
1580 (setq url (replace-regexp-in-string "\\`man:" "" url))
1581 (cond
1582 ((executable-find manual-program) (man url))
1583 (t (woman (replace-regexp-in-string "([[:alnum:]]+)" "" url)))))
1585 ;; --- Random browser ---
1587 ;;;###autoload
1588 (defun browse-url-generic (url &optional _new-window)
1589 ;; new-window ignored
1590 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1591 Default to the URL around or before point. A fresh copy of the
1592 browser is started up in a new process with possible additional arguments
1593 `browse-url-generic-args'. This is appropriate for browsers which
1594 don't offer a form of remote control."
1595 (interactive (browse-url-interactive-arg "URL: "))
1596 (if (not browse-url-generic-program)
1597 (error "No browser defined (`browse-url-generic-program')"))
1598 (apply 'call-process browse-url-generic-program nil
1599 0 nil
1600 (append browse-url-generic-args (list url))))
1602 ;;;###autoload
1603 (defun browse-url-kde (url &optional _new-window)
1604 "Ask the KDE WWW browser to load URL.
1605 Default to the URL around or before point.
1606 The optional argument NEW-WINDOW is not used."
1607 (interactive (browse-url-interactive-arg "KDE URL: "))
1608 (message "Sending URL to KDE...")
1609 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
1610 (append browse-url-kde-args (list url))))
1612 (defun browse-url-elinks-new-window (url)
1613 "Ask the Elinks WWW browser to load URL in a new window."
1614 (let ((process-environment (browse-url-process-environment)))
1615 (apply #'start-process
1616 (append (list (concat "elinks:" url)
1617 nil)
1618 browse-url-elinks-wrapper
1619 (list "elinks" url)))))
1621 ;;;###autoload
1622 (defun browse-url-elinks (url &optional new-window)
1623 "Ask the Elinks WWW browser to load URL.
1624 Default to the URL around the point.
1626 The document is loaded in a new tab of a running Elinks or, if
1627 none yet running, a newly started instance.
1629 The Elinks command will be prepended by the program+arguments
1630 from `browse-url-elinks-wrapper'."
1631 (interactive (browse-url-interactive-arg "URL: "))
1632 (setq url (browse-url-encode-url url))
1633 (if new-window
1634 (browse-url-elinks-new-window url)
1635 (let ((process-environment (browse-url-process-environment))
1636 (elinks-ping-process (start-process "elinks-ping" nil
1637 "elinks" "-remote" "ping()")))
1638 (set-process-sentinel elinks-ping-process
1639 `(lambda (process change)
1640 (browse-url-elinks-sentinel process ,url))))))
1642 (defun browse-url-elinks-sentinel (process url)
1643 "Determines if Elinks is running or a new one has to be started."
1644 ;; Try to determine if an instance is running or if we have to
1645 ;; create a new one.
1646 (pcase (process-exit-status process)
1648 ;; No instance, start a new one.
1649 (browse-url-elinks-new-window url))
1651 ;; Found an instance, open URL in new tab.
1652 (let ((process-environment (browse-url-process-environment)))
1653 (start-process (concat "elinks:" url) nil
1654 "elinks" "-remote"
1655 (concat "openURL(\"" url "\",new-tab)"))))
1656 (exit-status
1657 (error "Unrecognized exit-code %d of process `elinks'"
1658 exit-status))))
1660 (provide 'browse-url)
1662 ;;; browse-url.el ends here