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/>.
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
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
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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")
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."
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
))
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
182 (function-item :tag
"Emacs Mail" :value browse-url-mail
)
183 (function-item :tag
"None" nil
))
187 (defcustom browse-url-man-function
'browse-url-man
188 "Function to display man: links."
190 (function-item :tag
"Emacs Man" :value browse-url-man
)
191 (const :tag
"None" nil
)
192 (function :tag
"Other function"))
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."
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"))
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"))
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
))
232 (defcustom browse-url-mozilla-program
"mozilla"
233 "The name by which to invoke Mozilla."
237 (defcustom browse-url-mozilla-arguments nil
238 "A list of strings to pass to Mozilla as arguments."
239 :type
'(repeat (string :tag
"Argument"))
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"))
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."
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"))
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"))
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."
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"))
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."
299 (defcustom browse-url-chromium-arguments nil
300 "A list of strings to pass to Chromium as arguments."
301 :type
'(repeat (string :tag
"Argument"))
305 (defcustom browse-url-galeon-program
"galeon"
306 "The name by which to invoke Galeon."
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"))
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"))
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."
333 (defcustom browse-url-epiphany-arguments nil
334 "A list of strings to pass to Epiphany as arguments."
335 :type
'(repeat (string :tag
"Argument"))
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"))
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."
353 :type
'(repeat (string :tag
"Argument"))
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."
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."
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."
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."
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."
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
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."
413 (defcustom browse-url-mosaic-program
"xmosaic"
414 "The name by which to invoke Mosaic (or mMosaic)."
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"))
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."
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."
441 (defcustom browse-url-conkeror-arguments nil
442 "A list of strings to pass to Conkeror as arguments."
444 :type
'(repeat (string :tag
"Argument"))
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
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")))
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."
485 (defcustom browse-url-of-file-hook nil
486 "Hook run after `browse-url-of-file' has asked a browser to load a file."
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."
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'."
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."
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"))
523 (defcustom browse-url-gnudoit-program
"gnudoit"
524 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
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"))
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
))
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"))
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
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."
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."
567 (defcustom browse-url-text-emacs-args
(and (not window-system
)
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"))
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
))
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."
598 (defcustom browse-url-text-input-delay
0.2
599 "Seconds to wait for a text browser between moves down from an input field."
604 (defcustom browse-url-kde-program
"kfmclient"
605 "The name by which to invoke the KDE web browser."
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"))
615 (defcustom browse-url-elinks-wrapper
'("xterm" "-e")
616 "Wrapper command prepended to the Elinks command-line."
617 :type
'(repeat (string :tag
"Wrapper"))
620 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
))
628 (while (setq s
(string-match chars encoded-text s
))
630 (replace-match (format "%%%X"
631 (string-to-char (match-string 0 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
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
)))
685 browse-url-new-window-flag
))
687 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
688 ;; Browse current buffer
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'."
699 (setq file
(buffer-file-name))
700 (error "Current buffer has no file"))
701 (let ((buf (get-file-buffer file
)))
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.
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
))))
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
733 (and buffer
(set-buffer buffer
))
735 ;; Ignore real name if restricted
736 (and (not (buffer-narrowed-p))
738 (and (boundp 'dired-directory
) dired-directory
)))))
741 (or browse-url-temp-file-name
742 (setq browse-url-temp-file-name
743 (convert-standard-filename
745 (expand-file-name "burl" browse-url-temp-dir
)
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
))
764 (defun browse-url-of-dired-file ()
765 "In Dired, ask a WWW browser to display the file named on this line."
767 (let ((tem (dired-get-filename t t
)))
769 (browse-url-of-file (expand-file-name tem
))
770 (error "No file on this line"))))
773 (defun browse-url-of-region (min max
)
774 "Ask a WWW browser to display the current region."
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
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
799 If ARGS are omitted, the default is to pass `browse-url-new-window-flag'
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).
825 (dolist (bf function
)
826 (when (string-match (car bf
) url
)
827 (apply (cdr bf
) url args
)
829 (error "No browse-url-browser-function matching URL %s"
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
))))
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'."
842 (let ((url (browse-url-url-at-point)))
844 (browse-url url
(if arg
845 (not browse-url-new-window-flag
)
846 browse-url-new-window-flag
))
847 (error "No URL found"))))
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."
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
))
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))))
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")))
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'."
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"))))
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")))
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
))
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))
985 (apply 'start-process
986 (concat "netscape " url
) nil
987 browse-url-netscape-program
989 browse-url-netscape-arguments
990 (if (eq window-system
'w32
)
993 (if new-window
'("-noraise"))
995 (concat "openURL(" url
996 (if (browse-url-maybe-new-window
998 (if browse-url-netscape-new-window-is-tab
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"))
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)"
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
)))))
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))
1058 (apply 'start-process
1059 (concat "mozilla " url
) nil
1060 browse-url-mozilla-program
1062 browse-url-mozilla-arguments
1066 (if (browse-url-maybe-new-window
1068 (if browse-url-mozilla-new-window-is-tab
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
))))))
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
1109 browse-url-firefox-arguments
1110 (if (browse-url-maybe-new-window new-window
)
1111 (if browse-url-firefox-new-window-is-tab
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
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
1130 browse-url-chromium-arguments
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
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
1146 browse-url-chrome-arguments
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
1175 browse-url-galeon-arguments
1176 (if (browse-url-maybe-new-window new-window
)
1177 (if browse-url-galeon-new-window-is-tab
1179 '("--new-window" "--noraise"))
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
1221 browse-url-epiphany-arguments
1222 (if (browse-url-maybe-new-window new-window
)
1223 (if browse-url-epiphany-new-window-is-tab
1225 '("--new-window" "--noraise"))
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
)
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 Optional argument SAME-WINDOW non-nil means show the URL in the
1248 currently selected window instead."
1249 (interactive (browse-url-interactive-arg "URL: "))
1250 (require 'url-handlers
)
1251 (let ((file-name-handler-alist
1252 (cons (cons url-handler-regexp
'url-file-handler
)
1253 file-name-handler-alist
)))
1254 (if same-window
(find-file url
) (find-file-other-window url
))))
1257 (defun browse-url-gnome-moz (url &optional new-window
)
1258 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1259 Default to the URL around or before point. The strings in variable
1260 `browse-url-gnome-moz-arguments' are also passed.
1262 When called interactively, if variable `browse-url-new-window-flag' is
1263 non-nil, load the document in a new browser window, otherwise use an
1264 existing one. A non-nil interactive prefix argument reverses the
1265 effect of `browse-url-new-window-flag'.
1267 When called non-interactively, optional second argument NEW-WINDOW is
1268 used instead of `browse-url-new-window-flag'."
1269 (declare (obsolete nil
"25.1"))
1270 (interactive (browse-url-interactive-arg "URL: "))
1271 (apply 'start-process
(concat "gnome-moz-remote " url
)
1273 browse-url-gnome-moz-program
1275 browse-url-gnome-moz-arguments
1276 (if (browse-url-maybe-new-window new-window
)
1278 (list "--raise" url
))))
1283 (defun browse-url-mosaic (url &optional new-window
)
1284 "Ask the XMosaic WWW browser to load URL.
1286 Default to the URL around or before point. The strings in variable
1287 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1288 program is invoked according to the variable
1289 `browse-url-mosaic-program'.
1291 When called interactively, if variable `browse-url-new-window-flag' is
1292 non-nil, load the document in a new Mosaic window, otherwise use a
1293 random existing one. A non-nil interactive prefix argument reverses
1294 the effect of `browse-url-new-window-flag'.
1296 When called non-interactively, optional second argument NEW-WINDOW is
1297 used instead of `browse-url-new-window-flag'."
1298 (declare (obsolete nil
"25.1"))
1299 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1300 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile
))
1302 (if (file-readable-p pidfile
)
1304 (insert-file-contents pidfile
)
1305 (setq pid
(read (current-buffer)))))
1306 (if (and (integerp pid
) (zerop (signal-process pid
0))) ; Mosaic running
1309 (insert (if (browse-url-maybe-new-window new-window
)
1313 (with-file-modes ?
\700
1315 (setq pidfile
(format "/tmp/Mosaic.%d" pid
)))
1316 (delete-file pidfile
))
1317 ;; https://debbugs.gnu.org/17428. Use O_EXCL.
1318 (write-region nil nil pidfile nil
'silent nil
'excl
)))
1319 ;; Send signal SIGUSR to Mosaic
1320 (message "Signaling Mosaic...")
1321 (signal-process pid
'SIGUSR1
)
1322 ;; Or you could try:
1323 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1324 (message "Signaling Mosaic...done"))
1325 ;; Mosaic not running - start it
1326 (message "Starting %s..." browse-url-mosaic-program
)
1327 (apply 'start-process
"xmosaic" nil browse-url-mosaic-program
1328 (append browse-url-mosaic-arguments
(list url
)))
1329 (message "Starting %s...done" browse-url-mosaic-program
))))
1331 ;; --- Mosaic using CCI ---
1334 (defun browse-url-cci (url &optional new-window
)
1335 "Ask the XMosaic WWW browser to load URL.
1336 Default to the URL around or before point.
1338 This function only works for XMosaic version 2.5 or later. You must
1339 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1340 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1342 When called interactively, if variable `browse-url-new-window-flag' is
1343 non-nil, load the document in a new browser window, otherwise use a
1344 random existing one. A non-nil interactive prefix argument reverses
1345 the effect of `browse-url-new-window-flag'.
1347 When called non-interactively, optional second argument NEW-WINDOW is
1348 used instead of `browse-url-new-window-flag'."
1349 (declare (obsolete nil
"25.1"))
1350 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1351 (open-network-stream "browse-url" " *browse-url*"
1352 browse-url-CCI-host browse-url-CCI-port
)
1353 ;; Todo: start browser if fails
1354 (process-send-string "browse-url"
1355 (concat "get url (" url
") output "
1356 (if (browse-url-maybe-new-window new-window
)
1360 (process-send-string "browse-url" "disconnect\r\n")
1361 (delete-process "browse-url"))
1365 (defun browse-url-conkeror (url &optional new-window
)
1366 "Ask the Conkeror WWW browser to load URL.
1367 Default to the URL around or before point. Also pass the strings
1368 in the variable `browse-url-conkeror-arguments' to Conkeror.
1370 When called interactively, if variable
1371 `browse-url-new-window-flag' is non-nil, load the document in a
1372 new Conkeror window, otherwise use a random existing one. A
1373 non-nil interactive prefix argument reverses the effect of
1374 `browse-url-new-window-flag'.
1376 If variable `browse-url-conkeror-new-window-is-buffer' is
1377 non-nil, then whenever a document would otherwise be loaded in a
1378 new window, load it in a new buffer in an existing window instead.
1380 When called non-interactively, use optional second argument
1381 NEW-WINDOW instead of `browse-url-new-window-flag'."
1382 (interactive (browse-url-interactive-arg "URL: "))
1383 (setq url
(browse-url-encode-url url
))
1384 (let* ((process-environment (browse-url-process-environment)))
1385 (apply 'start-process
(format "conkeror %s" url
)
1387 browse-url-conkeror-program
1389 browse-url-conkeror-arguments
1392 (format "load_url_in_new_%s('%s')"
1393 (if (browse-url-maybe-new-window new-window
)
1394 (if browse-url-conkeror-new-window-is-buffer
1402 (declare-function w3-fetch-other-window
"ext:w3m" (&optional url
))
1403 (declare-function w3-fetch
"ext:w3m" (&optional url target
))
1406 (defun browse-url-w3 (url &optional new-window
)
1407 "Ask the w3 WWW browser to load URL.
1408 Default to the URL around or before point.
1410 When called interactively, if variable `browse-url-new-window-flag' is
1411 non-nil, load the document in a new window. A non-nil interactive
1412 prefix argument reverses the effect of `browse-url-new-window-flag'.
1414 When called non-interactively, optional second argument NEW-WINDOW is
1415 used instead of `browse-url-new-window-flag'."
1416 (interactive (browse-url-interactive-arg "W3 URL: "))
1417 (require 'w3
) ; w3-fetch-other-window not autoloaded
1418 (if (browse-url-maybe-new-window new-window
)
1419 (w3-fetch-other-window url
)
1423 (defun browse-url-w3-gnudoit (url &optional _new-window
)
1424 ;; new-window ignored
1425 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1426 The `browse-url-gnudoit-program' program is used with options given by
1427 `browse-url-gnudoit-args'. Default to the URL around or before point."
1428 (declare (obsolete nil
"25.1"))
1429 (interactive (browse-url-interactive-arg "W3 URL: "))
1430 (apply 'start-process
(concat "gnudoit:" url
) nil
1431 browse-url-gnudoit-program
1432 (append browse-url-gnudoit-args
1433 (list (concat "(w3-fetch \"" url
"\")")
1436 ;; --- Lynx in an xterm ---
1439 (defun browse-url-text-xterm (url &optional _new-window
)
1440 ;; new-window ignored
1441 "Ask a text browser to load URL.
1442 URL defaults to the URL around or before point.
1443 This runs the text browser specified by `browse-url-text-browser'.
1444 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1445 with possible additional arguments `browse-url-xterm-args'.
1446 The optional argument NEW-WINDOW is not used."
1447 (interactive (browse-url-interactive-arg "Text browser URL: "))
1448 (apply #'start-process
`(,(concat browse-url-text-browser url
)
1449 nil
,browse-url-xterm-program
1450 ,@browse-url-xterm-args
"-e" ,browse-url-text-browser
1453 ;; --- Lynx in an Emacs "term" window ---
1455 (declare-function term-char-mode
"term" ())
1456 (declare-function term-send-down
"term" ())
1457 (declare-function term-send-string
"term" (proc str
))
1460 (defun browse-url-text-emacs (url &optional new-buffer
)
1461 "Ask a text browser to load URL.
1462 URL defaults to the URL around or before point.
1463 This runs the text browser specified by `browse-url-text-browser'.
1464 With a prefix argument, it runs a new browser process in a new buffer.
1466 When called interactively, if variable `browse-url-new-window-flag' is
1467 non-nil, load the document in a new browser process in a new term window,
1468 otherwise use any existing one. A non-nil interactive prefix argument
1469 reverses the effect of `browse-url-new-window-flag'.
1471 When called non-interactively, optional second argument NEW-WINDOW is
1472 used instead of `browse-url-new-window-flag'."
1473 (interactive (browse-url-interactive-arg "Text browser URL: "))
1474 (let* ((system-uses-terminfo t
) ; Lynx uses terminfo
1475 ;; (term-term-name "vt100") ; ??
1476 (buf (get-buffer "*text browser*"))
1477 (proc (and buf
(get-buffer-process buf
)))
1478 (n browse-url-text-input-attempts
))
1480 (if (and (browse-url-maybe-new-window new-buffer
) buf
)
1481 ;; Rename away the OLD buffer. This isn't very polite, but
1482 ;; term insists on working in a buffer named *lynx* and would
1483 ;; choke on *lynx*<1>
1484 (progn (set-buffer buf
)
1486 (if (or (browse-url-maybe-new-window new-buffer
)
1489 (not (memq (process-status proc
) '(run stop
))))
1490 ;; start a new text browser
1494 `(,browse-url-text-browser
1495 ,browse-url-text-browser
1496 nil
,@browse-url-text-emacs-args
1498 (switch-to-buffer buf
)
1500 (set-process-sentinel
1501 (get-buffer-process buf
)
1502 ;; Don't leave around a dead one (especially because of its
1504 (lambda (process _event
)
1505 (if (not (memq (process-status process
) '(run stop
)))
1506 (let ((buf (process-buffer process
)))
1507 (if buf
(kill-buffer buf
)))))))
1508 ;; Send the url to the text browser in the old buffer
1509 (let ((win (get-buffer-window buf t
)))
1512 (switch-to-buffer buf
)))
1513 (if (eq (following-char) ?_
)
1514 (cond ((eq browse-url-text-input-field
'warn
)
1515 (error "Please move out of the input field first"))
1516 ((eq browse-url-text-input-field
'avoid
)
1517 (while (and (eq (following-char) ?_
) (> n
0))
1518 (term-send-down) ; down arrow
1519 (sit-for browse-url-text-input-delay
))
1520 (if (eq (following-char) ?_
)
1521 (error "Cannot move out of the input field, sorry")))))
1522 (term-send-string proc
(concat "g" ; goto
1523 "\C-u" ; kill default url
1529 (autoload 'rfc2368-parse-mailto-url
"rfc2368")
1532 (defun browse-url-mail (url &optional new-window
)
1533 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1534 Default to using the mailto: URL around or before point as the
1535 recipient's address. Supplying a non-nil interactive prefix argument
1536 will cause the mail to be composed in another window rather than the
1539 When called interactively, if variable `browse-url-new-window-flag' is
1540 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1541 non-nil interactive prefix argument reverses the effect of
1542 `browse-url-new-window-flag'.
1544 When called non-interactively, optional second argument NEW-WINDOW is
1545 used instead of `browse-url-new-window-flag'."
1546 (interactive (browse-url-interactive-arg "Mailto URL: "))
1548 (let* ((alist (rfc2368-parse-mailto-url url
))
1549 (to (assoc "To" alist
))
1550 (subject (assoc "Subject" alist
))
1551 (body (assoc "Body" alist
))
1552 (rest (delq to
(delq subject
(delq body alist
))))
1554 (subject (cdr subject
))
1556 (mail-citation-hook (unless body mail-citation-hook
)))
1557 (if (browse-url-maybe-new-window new-window
)
1558 (compose-mail-other-window to subject rest nil
1559 (list 'insert-buffer
(current-buffer)))
1560 (compose-mail to subject rest nil nil
1561 (list 'insert-buffer
(current-buffer))))
1563 (goto-char (point-min))
1564 (unless (or (search-forward (concat "\n" mail-header-separator
"\n")
1570 (insert (replace-regexp-in-string "\r\n" "\n" body
))
1572 (insert "\n"))))))))
1576 (defvar manual-program
)
1578 (defun browse-url-man (url &optional _new-window
)
1580 (interactive (browse-url-interactive-arg "Man page URL: "))
1582 (setq url
(replace-regexp-in-string "\\`man:" "" url
))
1584 ((executable-find manual-program
) (man url
))
1585 (t (woman (replace-regexp-in-string "([[:alnum:]]+)" "" url
)))))
1587 ;; --- Random browser ---
1590 (defun browse-url-generic (url &optional _new-window
)
1591 ;; new-window ignored
1592 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1593 Default to the URL around or before point. A fresh copy of the
1594 browser is started up in a new process with possible additional arguments
1595 `browse-url-generic-args'. This is appropriate for browsers which
1596 don't offer a form of remote control."
1597 (interactive (browse-url-interactive-arg "URL: "))
1598 (if (not browse-url-generic-program
)
1599 (error "No browser defined (`browse-url-generic-program')"))
1600 (apply 'call-process browse-url-generic-program nil
1602 (append browse-url-generic-args
(list url
))))
1605 (defun browse-url-kde (url &optional _new-window
)
1606 "Ask the KDE WWW browser to load URL.
1607 Default to the URL around or before point.
1608 The optional argument NEW-WINDOW is not used."
1609 (interactive (browse-url-interactive-arg "KDE URL: "))
1610 (message "Sending URL to KDE...")
1611 (apply #'start-process
(concat "KDE " url
) nil browse-url-kde-program
1612 (append browse-url-kde-args
(list url
))))
1614 (defun browse-url-elinks-new-window (url)
1615 "Ask the Elinks WWW browser to load URL in a new window."
1616 (let ((process-environment (browse-url-process-environment)))
1617 (apply #'start-process
1618 (append (list (concat "elinks:" url
)
1620 browse-url-elinks-wrapper
1621 (list "elinks" url
)))))
1624 (defun browse-url-elinks (url &optional new-window
)
1625 "Ask the Elinks WWW browser to load URL.
1626 Default to the URL around the point.
1628 The document is loaded in a new tab of a running Elinks or, if
1629 none yet running, a newly started instance.
1631 The Elinks command will be prepended by the program+arguments
1632 from `browse-url-elinks-wrapper'."
1633 (interactive (browse-url-interactive-arg "URL: "))
1634 (setq url
(browse-url-encode-url url
))
1636 (browse-url-elinks-new-window url
)
1637 (let ((process-environment (browse-url-process-environment))
1638 (elinks-ping-process (start-process "elinks-ping" nil
1639 "elinks" "-remote" "ping()")))
1640 (set-process-sentinel elinks-ping-process
1641 `(lambda (process change
)
1642 (browse-url-elinks-sentinel process
,url
))))))
1644 (defun browse-url-elinks-sentinel (process url
)
1645 "Determines if Elinks is running or a new one has to be started."
1646 ;; Try to determine if an instance is running or if we have to
1647 ;; create a new one.
1648 (pcase (process-exit-status process
)
1650 ;; No instance, start a new one.
1651 (browse-url-elinks-new-window url
))
1653 ;; Found an instance, open URL in new tab.
1654 (let ((process-environment (browse-url-process-environment)))
1655 (start-process (concat "elinks:" url
) nil
1657 (concat "openURL(\"" url
"\",new-tab)"))))
1659 (error "Unrecognized exit-code %d of process `elinks'"
1662 (provide 'browse-url
)
1664 ;;; browse-url.el ends here