1 ;;; browse-url.el --- pass a URL to a WWW browser
3 ;; Copyright (C) 1995-2016 Free Software Foundation, Inc.
5 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: 03 Apr 1995
8 ;; Keywords: hypertext, hypermedia, mouse
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 Free Desktop xdg-open on Gnome, KDE, Xfce4, LXDE
49 ;; browse-url-kde KDE konqueror (kfm)
50 ;; browse-url-elinks Elinks Don't know (tried with 0.12.GIT)
52 ;; Browsers can cache Web pages so it may be necessary to tell them to
53 ;; reload the current page if it has changed (e.g., if you have edited
54 ;; it). There is currently no perfect automatic solution to this.
56 ;; This package generalizes function html-previewer-process in Marc
57 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
58 ;; ffap.el package. The huge hyperbole package also contains similar
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-netscape-program
"netscape"
188 ;; Info about netscape-remote from Karl Berry.
189 "The name by which to invoke Netscape.
191 The free program `netscape-remote' from
192 <URL:http://home.netscape.com/newsref/std/remote.c> is said to start
193 up very much quicker than `netscape'. Reported to compile on a GNU
194 system, given vroot.h from the same directory, with cc flags
195 -DSTANDALONE -L/usr/X11R6/lib -lXmu -lX11."
199 (make-obsolete-variable 'browse-url-netscape-program nil
"25.1")
201 (defcustom browse-url-netscape-arguments nil
202 "A list of strings to pass to Netscape as arguments."
203 :type
'(repeat (string :tag
"Argument"))
206 (make-obsolete-variable 'browse-url-netscape-arguments nil
"25.1")
208 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
209 "A list of strings to pass to Netscape when it starts up.
210 Defaults to the value of `browse-url-netscape-arguments' at the time
211 `browse-url' is loaded."
212 :type
'(repeat (string :tag
"Argument"))
216 (make-obsolete-variable 'browse-url-netscape-startup-arguments nil
"25.1")
218 (defcustom browse-url-browser-display nil
219 "The X display for running the browser, if not same as Emacs's."
220 :type
'(choice string
(const :tag
"Default" nil
))
223 (defcustom browse-url-mozilla-program
"mozilla"
224 "The name by which to invoke Mozilla."
228 (defcustom browse-url-mozilla-arguments nil
229 "A list of strings to pass to Mozilla as arguments."
230 :type
'(repeat (string :tag
"Argument"))
233 (defcustom browse-url-mozilla-startup-arguments browse-url-mozilla-arguments
234 "A list of strings to pass to Mozilla when it starts up.
235 Defaults to the value of `browse-url-mozilla-arguments' at the time
236 `browse-url' is loaded."
237 :type
'(repeat (string :tag
"Argument"))
240 (defcustom browse-url-firefox-program
241 (let ((candidates '("icecat" "iceweasel" "firefox")))
242 (while (and candidates
(not (executable-find (car candidates
))))
243 (setq candidates
(cdr candidates
)))
244 (or (car candidates
) "firefox"))
245 "The name by which to invoke Firefox or a variant of it."
249 (defcustom browse-url-firefox-arguments nil
250 "A list of strings to pass to Firefox (or variant) as arguments."
251 :type
'(repeat (string :tag
"Argument"))
254 (defcustom browse-url-firefox-startup-arguments browse-url-firefox-arguments
255 "A list of strings to pass to Firefox (or variant) when it starts up.
256 Defaults to the value of `browse-url-firefox-arguments' at the time
257 `browse-url' is loaded."
258 :type
'(repeat (string :tag
"Argument"))
261 (make-obsolete-variable 'browse-url-firefox-startup-arguments
262 "it no longer has any effect." "24.5")
264 (defcustom browse-url-chrome-program
265 (let ((candidates '("google-chrome-stable" "google-chrome")))
266 (while (and candidates
(not (executable-find (car candidates
))))
267 (setq candidates
(cdr candidates
)))
268 (or (car candidates
) "chromium"))
269 "The name by which to invoke the Chrome browser."
274 (defcustom browse-url-chrome-arguments nil
275 "A list of strings to pass to Google Chrome as arguments."
276 :type
'(repeat (string :tag
"Argument"))
280 (defcustom browse-url-chromium-program
281 (let ((candidates '("chromium" "chromium-browser")))
282 (while (and candidates
(not (executable-find (car candidates
))))
283 (setq candidates
(cdr candidates
)))
284 (or (car candidates
) "chromium"))
285 "The name by which to invoke Chromium."
290 (defcustom browse-url-chromium-arguments nil
291 "A list of strings to pass to Chromium as arguments."
292 :type
'(repeat (string :tag
"Argument"))
296 (defcustom browse-url-galeon-program
"galeon"
297 "The name by which to invoke Galeon."
301 (make-obsolete-variable 'browse-url-galeon-program nil
"25.1")
303 (defcustom browse-url-galeon-arguments nil
304 "A list of strings to pass to Galeon as arguments."
305 :type
'(repeat (string :tag
"Argument"))
308 (make-obsolete-variable 'browse-url-galeon-arguments nil
"25.1")
310 (defcustom browse-url-galeon-startup-arguments browse-url-galeon-arguments
311 "A list of strings to pass to Galeon when it starts up.
312 Defaults to the value of `browse-url-galeon-arguments' at the time
313 `browse-url' is loaded."
314 :type
'(repeat (string :tag
"Argument"))
317 (make-obsolete-variable 'browse-url-galeon-startup-arguments nil
"25.1")
319 (defcustom browse-url-epiphany-program
"epiphany"
320 "The name by which to invoke Epiphany."
324 (defcustom browse-url-epiphany-arguments nil
325 "A list of strings to pass to Epiphany as arguments."
326 :type
'(repeat (string :tag
"Argument"))
329 (defcustom browse-url-epiphany-startup-arguments browse-url-epiphany-arguments
330 "A list of strings to pass to Epiphany when it starts up.
331 Defaults to the value of `browse-url-epiphany-arguments' at the time
332 `browse-url' is loaded."
333 :type
'(repeat (string :tag
"Argument"))
336 ;; GNOME means of invoking either Mozilla or Netscape.
337 (defvar browse-url-gnome-moz-program
"gnome-moz-remote")
339 (make-obsolete-variable 'browse-url-gnome-moz-program nil
"25.1")
341 (defcustom browse-url-gnome-moz-arguments
'()
342 "A list of strings passed to the GNOME mozilla viewer as arguments."
344 :type
'(repeat (string :tag
"Argument"))
347 (make-obsolete-variable 'browse-url-gnome-moz-arguments nil
"25.1")
349 (defcustom browse-url-mozilla-new-window-is-tab nil
350 "Whether to open up new windows in a tab or a new window.
351 If non-nil, then open the URL in a new tab rather than a new window if
352 `browse-url-mozilla' is asked to open it in a new window."
356 (defcustom browse-url-firefox-new-window-is-tab nil
357 "Whether to open up new windows in a tab or a new window.
358 If non-nil, then open the URL in a new tab rather than a new window if
359 `browse-url-firefox' is asked to open it in a new window."
363 (defcustom browse-url-conkeror-new-window-is-buffer nil
364 "Whether to open up new windows in a buffer or a new window.
365 If non-nil, then open the URL in a new buffer rather than a new window if
366 `browse-url-conkeror' is asked to open it in a new window."
371 (defcustom browse-url-galeon-new-window-is-tab nil
372 "Whether to open up new windows in a tab or a new window.
373 If non-nil, then open the URL in a new tab rather than a new window if
374 `browse-url-galeon' is asked to open it in a new window."
378 (make-obsolete-variable 'browse-url-galeon-new-window-is-tab nil
"25.1")
380 (defcustom browse-url-epiphany-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-epiphany' is asked to open it in a new window."
387 (defcustom browse-url-netscape-new-window-is-tab nil
388 "Whether to open up new windows in a tab or a new window.
389 If non-nil, then open the URL in a new tab rather than a new
390 window if `browse-url-netscape' is asked to open it in a new
395 (make-obsolete-variable 'browse-url-netscape-new-window-is-tab nil
"25.1")
397 (defcustom browse-url-new-window-flag nil
398 "Non-nil means always open a new browser window with appropriate browsers.
399 Passing an interactive argument to \\[browse-url], or specific browser
400 commands reverses the effect of this variable."
404 (defcustom browse-url-mosaic-program
"xmosaic"
405 "The name by which to invoke Mosaic (or mMosaic)."
410 (make-obsolete-variable 'browse-url-mosaic-program nil
"25.1")
412 (defcustom browse-url-mosaic-arguments nil
413 "A list of strings to pass to Mosaic as arguments."
414 :type
'(repeat (string :tag
"Argument"))
417 (make-obsolete-variable 'browse-url-mosaic-arguments nil
"25.1")
419 (defcustom browse-url-mosaic-pidfile
"~/.mosaicpid"
420 "The name of the pidfile created by Mosaic."
424 (make-obsolete-variable 'browse-url-mosaic-pidfile nil
"25.1")
426 (defcustom browse-url-conkeror-program
"conkeror"
427 "The name by which to invoke Conkeror."
432 (defcustom browse-url-conkeror-arguments nil
433 "A list of strings to pass to Conkeror as arguments."
435 :type
'(repeat (string :tag
"Argument"))
438 (defcustom browse-url-filename-alist
439 `(("^/\\(ftp@\\|anonymous@\\)?\\([^:/]+\\):/*" .
"ftp://\\2/")
440 ;; The above loses the username to avoid the browser prompting for
441 ;; it in anonymous cases. If it's not anonymous the next regexp
443 ("^/\\([^:@/]+@\\)?\\([^:/]+\\):/*" .
"ftp://\\1\\2/")
444 ,@(if (memq system-type
'(windows-nt ms-dos
))
445 '(("^\\([a-zA-Z]:\\)[\\/]" .
"file:///\\1/")
446 ("^[\\/][\\/]+" .
"file://")))
447 ("^/+" .
"file:///"))
448 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
449 Any substring of a filename matching one of the REGEXPs is replaced by
450 the corresponding STRING using `replace-match', not treating STRING
451 literally. All pairs are applied in the order given. The default
452 value converts ange-ftp/EFS-style file names into ftp URLs and prepends
453 `file:' to any file name beginning with `/'.
455 For example, adding to the default a specific translation of an ange-ftp
456 address to an HTTP URL:
458 (setq browse-url-filename-alist
459 \\='((\"/webmaster@webserver:/home/www/html/\" .
460 \"http://www.acme.co.uk/\")
461 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:/]+\\):/*\" . \"ftp://\\2/\")
462 (\"^/\\([^:@/]+@\\)?\\([^:/]+\\):/*\" . \"ftp://\\1\\2/\")
463 (\"^/+\" . \"file:/\")))"
464 :type
'(repeat (cons :format
"%v"
465 (regexp :tag
"Regexp")
466 (string :tag
"Replacement")))
470 (defcustom browse-url-save-file nil
471 "If non-nil, save the buffer before displaying its file.
472 Used by the `browse-url-of-file' command."
476 (defcustom browse-url-of-file-hook nil
477 "Hook run after `browse-url-of-file' has asked a browser to load a file."
481 (defcustom browse-url-CCI-port
3003
482 "Port to access XMosaic via CCI.
483 This can be any number between 1024 and 65535 but must correspond to
484 the value set in the browser."
488 (make-obsolete-variable 'browse-url-CCI-port nil
"25.1")
490 (defcustom browse-url-CCI-host
"localhost"
491 "Host to access XMosaic via CCI.
492 This should be the host name of the machine running XMosaic with CCI
493 enabled. The port number should be set in `browse-url-CCI-port'."
497 (make-obsolete-variable 'browse-url-CCI-host nil
"25.1")
499 (defvar browse-url-temp-file-name nil
)
500 (make-variable-buffer-local 'browse-url-temp-file-name
)
502 (defcustom browse-url-xterm-program
"xterm"
503 "The name of the terminal emulator used by `browse-url-text-xterm'.
504 This might, for instance, be a separate color version of xterm."
508 (defcustom browse-url-xterm-args nil
509 "A list of strings defining options for `browse-url-xterm-program'.
510 These might set its size, for instance."
511 :type
'(repeat (string :tag
"Argument"))
514 (defcustom browse-url-gnudoit-program
"gnudoit"
515 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
519 (defcustom browse-url-gnudoit-args
'("-q")
520 "A list of strings defining options for `browse-url-gnudoit-program'.
521 These might set the port, for instance."
522 :type
'(repeat (string :tag
"Argument"))
525 (defcustom browse-url-generic-program nil
526 "The name of the browser program used by `browse-url-generic'."
527 :type
'(choice string
(const :tag
"None" nil
))
530 (defcustom browse-url-generic-args nil
531 "A list of strings defining options for `browse-url-generic-program'."
532 :type
'(repeat (string :tag
"Argument"))
535 (defcustom browse-url-temp-dir temporary-file-directory
536 "The name of a directory for browse-url's temporary files.
537 Such files are generated by functions like `browse-url-of-region'.
538 You might want to set this to somewhere with restricted read permissions
543 (defcustom browse-url-netscape-version
3
544 "The version of Netscape you are using.
545 This affects how URL reloading is done; the mechanism changed
546 incompatibly at version 4."
550 (make-obsolete-variable 'browse-url-netscape-version nil
"25.1")
552 (defcustom browse-url-text-browser
"lynx"
553 "The name of the text browser to invoke."
558 (defcustom browse-url-text-emacs-args
(and (not window-system
)
560 "A list of strings defining options for a text browser in an Emacs buffer.
562 The default is none in a window system, otherwise `-show_cursor' to
563 indicate the position of the current link in the absence of
564 highlighting, assuming the normal default for showing the cursor."
565 :type
'(repeat (string :tag
"Argument"))
569 (defcustom browse-url-text-input-field
'avoid
570 "Action on selecting an existing text browser buffer at an input field.
571 What to do when sending a new URL to an existing text browser buffer in Emacs
572 if the browser cursor is on an input field (in which case the `g' command
573 would be entered as data). Such fields are recognized by the
574 underlines ____. Allowed values: nil: disregard it, `warn': warn the
575 user and don't emit the URL, `avoid': try to avoid the field by moving
576 down (this *won't* always work)."
577 :type
'(choice (const :tag
"Move to try to avoid field" :value avoid
)
578 (const :tag
"Disregard" :value nil
)
579 (const :tag
"Warn, don't emit URL" :value warn
))
583 (defcustom browse-url-text-input-attempts
10
584 "How many times to try to move down from a series of text browser input fields."
589 (defcustom browse-url-text-input-delay
0.2
590 "Seconds to wait for a text browser between moves down from an input field."
595 (defcustom browse-url-kde-program
"kfmclient"
596 "The name by which to invoke the KDE web browser."
601 (defcustom browse-url-kde-args
'("openURL")
602 "A list of strings defining options for `browse-url-kde-program'."
603 :type
'(repeat (string :tag
"Argument"))
606 (defcustom browse-url-elinks-wrapper
'("xterm" "-e")
607 "Wrapper command prepended to the Elinks command-line."
608 :type
'(repeat (string :tag
"Wrapper"))
611 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
614 (defun browse-url-url-encode-chars (text chars
)
615 "URL-encode the chars in TEXT that match CHARS.
616 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
617 (let ((encoded-text (copy-sequence text
))
619 (while (setq s
(string-match chars encoded-text s
))
621 (replace-match (format "%%%X"
622 (string-to-char (match-string 0 encoded-text
)))
627 (defun browse-url-encode-url (url)
628 "Escape annoying characters in URL.
629 The annoying characters are those that can mislead a web browser
630 regarding its parameter treatment."
631 ;; FIXME: Is there an actual example of a web browser getting
632 ;; confused? (This used to encode commas, but at least Firefox
633 ;; handles commas correctly and doesn't accept encoded commas.)
634 (browse-url-url-encode-chars url
"[\")$] "))
636 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
639 (defun browse-url-url-at-point ()
640 (or (thing-at-point 'url t
)
641 ;; assume that the user is pointing at something like gnu.org/gnu
642 (let ((f (thing-at-point 'filename t
)))
643 (and f
(concat "http://" f
)))))
645 ;; Having this as a separate function called by the browser-specific
646 ;; functions allows them to be stand-alone commands, making it easier
647 ;; to switch between browsers.
649 (defun browse-url-interactive-arg (prompt)
650 "Read a URL from the minibuffer, prompting with PROMPT.
651 If `transient-mark-mode' is non-nil and the mark is active,
652 it defaults to the current region, else to the URL at or before
653 point. If invoked with a mouse button, it moves point to the
654 position clicked before acting.
656 This function returns a list (URL NEW-WINDOW-FLAG)
657 for use in `interactive'."
658 (let ((event (elt (this-command-keys) 0)))
659 (and (listp event
) (mouse-set-point event
)))
660 (list (read-string prompt
(or (and transient-mark-mode mark-active
661 ;; rfc2396 Appendix E.
662 (replace-regexp-in-string
664 (buffer-substring-no-properties
665 (region-beginning) (region-end))))
666 (browse-url-url-at-point)))
667 (not (eq (null browse-url-new-window-flag
)
668 (null current-prefix-arg
)))))
670 ;; called-interactive-p needs to be called at a function's top-level, hence
671 ;; this macro. We use that rather than interactive-p because
672 ;; use in a keyboard macro should not change this behavior.
673 (defmacro browse-url-maybe-new-window
(arg)
674 `(if (or noninteractive
(not (called-interactively-p 'any
)))
676 browse-url-new-window-flag
))
678 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
679 ;; Browse current buffer
682 (defun browse-url-of-file (&optional file
)
683 "Ask a WWW browser to display FILE.
684 Display the current buffer's file if FILE is nil or if called
685 interactively. Turn the filename into a URL with function
686 `browse-url-file-url'. Pass the URL to a browser using the
687 `browse-url' function then run `browse-url-of-file-hook'."
690 (setq file
(buffer-file-name))
691 (error "Current buffer has no file"))
692 (let ((buf (get-file-buffer file
)))
694 (with-current-buffer buf
695 (cond ((not (buffer-modified-p)))
696 (browse-url-save-file (save-buffer))
697 (t (message "%s modified since last save" file
))))))
698 (browse-url (browse-url-file-url file
))
699 (run-hooks 'browse-url-of-file-hook
))
701 (defun browse-url-file-url (file)
702 "Return the URL corresponding to FILE.
703 Use variable `browse-url-filename-alist' to map filenames to URLs."
704 (let ((coding (if (equal system-type
'windows-nt
)
705 ;; W32 pretends that file names are UTF-8 encoded.
707 (and (default-value 'enable-multibyte-characters
)
708 (or file-name-coding-system
709 default-file-name-coding-system
)))))
710 (if coding
(setq file
(encode-coding-string file coding
))))
711 (setq file
(browse-url-url-encode-chars file
"[*\"()',=;?% ]"))
712 (dolist (map browse-url-filename-alist
)
713 (when (and map
(string-match (car map
) file
))
714 (setq file
(replace-match (cdr map
) t nil file
))))
718 (defun browse-url-of-buffer (&optional buffer
)
719 "Ask a WWW browser to display BUFFER.
720 Display the current buffer if BUFFER is nil. Display only the
721 currently visible part of BUFFER (from a temporary file) if buffer is
725 (and buffer
(set-buffer buffer
))
727 ;; Ignore real name if restricted
728 (and (not (buffer-narrowed-p))
730 (and (boundp 'dired-directory
) dired-directory
)))))
733 (or browse-url-temp-file-name
734 (setq browse-url-temp-file-name
735 (convert-standard-filename
737 (expand-file-name "burl" browse-url-temp-dir
)
739 (setq file-name browse-url-temp-file-name
)
740 (write-region (point-min) (point-max) file-name nil
'no-message
)))
741 (browse-url-of-file file-name
))))
743 (defun browse-url-delete-temp-file (&optional temp-file-name
)
744 ;; Delete browse-url-temp-file-name from the file system
745 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
746 (let ((file-name (or temp-file-name browse-url-temp-file-name
)))
747 (if (and file-name
(file-exists-p file-name
))
748 (delete-file file-name
))))
750 (add-hook 'kill-buffer-hook
'browse-url-delete-temp-file
)
752 (declare-function dired-get-filename
"dired"
753 (&optional localp no-error-if-not-filep
))
756 (defun browse-url-of-dired-file ()
757 "In Dired, ask a WWW browser to display the file named on this line."
759 (let ((tem (dired-get-filename t t
)))
761 (browse-url-of-file (expand-file-name tem
))
762 (error "No file on this line"))))
765 (defun browse-url-of-region (min max
)
766 "Ask a WWW browser to display the current region."
770 (narrow-to-region min max
)
771 (browse-url-of-buffer))))
773 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
774 ;; Browser-independent commands
776 ;; A generic command to call the current browse-url-browser-function
779 (defun browse-url (url &rest args
)
780 "Ask a WWW browser to load URL.
781 Prompt for a URL, defaulting to the URL at or before point.
782 Invokes a suitable browser function which does the actual job.
783 The variable `browse-url-browser-function' says which browser function to
784 use. If the URL is a mailto: URL, consult `browse-url-mailto-function'
785 first, if that exists.
787 The additional ARGS are passed to the browser function. See the doc
788 strings of the actual functions, starting with `browse-url-browser-function',
789 for information about the significance of ARGS (most of the functions
791 If ARGS are omitted, the default is to pass `browse-url-new-window-flag'
793 (interactive (browse-url-interactive-arg "URL: "))
794 (unless (called-interactively-p 'interactive
)
795 (setq args
(or args
(list browse-url-new-window-flag
))))
796 (when (and url-handler-mode
(not (file-name-absolute-p url
)))
797 (setq url
(expand-file-name url
)))
798 (let ((process-environment (copy-sequence process-environment
))
799 (function (or (and (string-match "\\`mailto:" url
)
800 browse-url-mailto-function
)
801 browse-url-browser-function
))
802 ;; Ensure that `default-directory' exists and is readable (b#6077).
803 (default-directory (or (unhandled-file-name-directory default-directory
)
804 (expand-file-name "~/"))))
805 ;; When connected to various displays, be careful to use the display of
806 ;; the currently selected frame, rather than the original start display,
807 ;; which may not even exist any more.
808 (if (stringp (frame-parameter nil
'display
))
809 (setenv "DISPLAY" (frame-parameter nil
'display
)))
810 (if (and (consp function
)
811 (not (functionp function
)))
812 ;; The `function' can be an alist; look down it for first match
813 ;; and apply the function (which might be a lambda).
815 (dolist (bf function
)
816 (when (string-match (car bf
) url
)
817 (apply (cdr bf
) url args
)
819 (error "No browse-url-browser-function matching URL %s"
821 ;; Unbound symbols go down this leg, since void-function from
822 ;; apply is clearer than wrong-type-argument from dolist.
823 (apply function url args
))))
826 (defun browse-url-at-point (&optional arg
)
827 "Ask a WWW browser to load the URL at or before point.
828 Variable `browse-url-browser-function' says which browser to use.
829 Optional prefix argument ARG non-nil inverts the value of the option
830 `browse-url-new-window-flag'."
832 (let ((url (browse-url-url-at-point)))
834 (browse-url url
(if arg
835 (not browse-url-new-window-flag
)
836 browse-url-new-window-flag
))
837 (error "No URL found"))))
840 (defun browse-url-at-mouse (event)
841 "Ask a WWW browser to load a URL clicked with the mouse.
842 The URL is the one around or before the position of the mouse click
843 but point is not changed. Variable `browse-url-browser-function'
844 says which browser to use."
847 (mouse-set-point event
)
848 ;; This handles browse-url-new-window-flag properly
849 ;; when it gets no arg.
850 (browse-url-at-point)))
852 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
853 ;; Browser-specific commands
855 ;; --- Default MS-Windows browser ---
857 (defvar dos-windows-version
)
858 (declare-function w32-shell-execute
"w32fns.c") ;; Defined in C.
860 (defun browse-url-default-windows-browser (url &optional _new-window
)
861 "Invoke the MS-Windows system's default Web browser.
862 The optional NEW-WINDOW argument is not used."
863 (interactive (browse-url-interactive-arg "URL: "))
864 (cond ((eq system-type
'ms-dos
)
865 (if dos-windows-version
866 (shell-command (concat "start " (shell-quote-argument url
)))
867 (error "Browsing URLs is not supported on this system")))
868 ((eq system-type
'cygwin
)
869 (call-process "cygstart" nil nil nil url
))
870 (t (w32-shell-execute "open" url
))))
872 (defun browse-url-default-macosx-browser (url &optional _new-window
)
873 "Invoke the macOS system's default Web browser.
874 The optional NEW-WINDOW argument is not used"
875 (interactive (browse-url-interactive-arg "URL: "))
876 (start-process (concat "open " url
) nil
"open" url
))
880 (defun browse-url-process-environment ()
881 "Set DISPLAY in the environment to the X display the browser will use.
882 This is either the value of variable `browse-url-browser-display' if
883 non-nil, or the same display as Emacs if different from the current
884 environment, otherwise just use the current environment."
885 (let ((display (or browse-url-browser-display
(browse-url-emacs-display))))
887 (cons (concat "DISPLAY=" display
) process-environment
)
888 process-environment
)))
890 (defun browse-url-emacs-display ()
891 "Return the X display Emacs is running on.
892 This is nil if the display is the same as the DISPLAY environment variable.
894 Actually Emacs could be using several displays; this just returns the
895 one showing the selected frame."
896 (let ((display (cdr-safe (assq 'display
(frame-parameters)))))
897 (and (not (equal display
(getenv "DISPLAY")))
900 (defun browse-url-default-browser (url &rest args
)
901 "Find a suitable browser and ask it to load URL.
902 Default to the URL around or before point.
904 When called interactively, if variable `browse-url-new-window-flag' is
905 non-nil, load the document in a new window, if possible, otherwise use
906 a random existing one. A non-nil interactive prefix argument reverses
907 the effect of `browse-url-new-window-flag'.
909 When called non-interactively, optional second argument ARGS is used
910 instead of `browse-url-new-window-flag'."
913 ((memq system-type
'(windows-nt ms-dos cygwin
))
914 'browse-url-default-windows-browser
)
915 ((memq system-type
'(darwin))
916 'browse-url-default-macosx-browser
)
917 ((browse-url-can-use-xdg-open) 'browse-url-xdg-open
)
918 ;;; ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
919 ((executable-find browse-url-mozilla-program
) 'browse-url-mozilla
)
920 ((executable-find browse-url-firefox-program
) 'browse-url-firefox
)
921 ((executable-find browse-url-chromium-program
) 'browse-url-chromium
)
922 ;;; ((executable-find browse-url-galeon-program) 'browse-url-galeon)
923 ((executable-find browse-url-kde-program
) 'browse-url-kde
)
924 ;;; ((executable-find browse-url-netscape-program) 'browse-url-netscape)
925 ;;; ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
926 ((executable-find browse-url-conkeror-program
) 'browse-url-conkeror
)
927 ((executable-find browse-url-chrome-program
) 'browse-url-chrome
)
928 ((executable-find browse-url-xterm-program
) 'browse-url-text-xterm
)
929 ((locate-library "w3") 'browse-url-w3
)
931 (lambda (&rest _ignore
) (error "No usable browser found"))))
934 (defun browse-url-can-use-xdg-open ()
935 "Return non-nil if the \"xdg-open\" program can be used.
936 xdg-open is a desktop utility that calls your preferred web browser.
937 This requires you to be running either Gnome, KDE, Xfce4 or LXDE."
938 (and (getenv "DISPLAY")
939 (executable-find "xdg-open")
940 ;; xdg-open may call gnome-open and that does not wait for its child
941 ;; to finish. This child may then be killed when the parent dies.
942 ;; Use nohup to work around. See bug#7166, bug#8917, bug#9779 and
943 ;; http://lists.gnu.org/archive/html/emacs-devel/2009-07/msg00279.html
944 (executable-find "nohup")
945 (or (getenv "GNOME_DESKTOP_SESSION_ID")
946 ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
949 "dbus-send" nil nil nil
950 "--dest=org.gnome.SessionManager"
952 "/org/gnome/SessionManager"
953 "org.gnome.SessionManager.CanShutdown"))
955 (equal (getenv "KDE_FULL_SESSION") "true")
958 "/bin/sh" nil nil nil
960 ;; FIXME use string-match rather than grep.
961 "xprop -root _DT_SAVE_MODE|grep xfce4"))
963 (member (getenv "DESKTOP_SESSION") '("LXDE" "Lubuntu"))
964 (equal (getenv "XDG_CURRENT_DESKTOP") "LXDE"))))
968 (defun browse-url-xdg-open (url &optional ignored
)
969 "Pass the specified URL to the \"xdg-open\" command.
970 xdg-open is a desktop utility that calls your preferred web browser.
971 The optional argument IGNORED is not used."
972 (interactive (browse-url-interactive-arg "URL: "))
973 (call-process "xdg-open" nil
0 nil url
))
976 (defun browse-url-netscape (url &optional new-window
)
977 "Ask the Netscape WWW browser to load URL.
978 Default to the URL around or before point. The strings in variable
979 `browse-url-netscape-arguments' are also passed to Netscape.
981 When called interactively, if variable `browse-url-new-window-flag' is
982 non-nil, load the document in a new Netscape window, otherwise use a
983 random existing one. A non-nil interactive prefix argument reverses
984 the effect of `browse-url-new-window-flag'.
986 If `browse-url-netscape-new-window-is-tab' is non-nil, then
987 whenever a document would otherwise be loaded in a new window, it
988 is loaded in a new tab in an existing window instead.
990 When called non-interactively, optional second argument NEW-WINDOW is
991 used instead of `browse-url-new-window-flag'."
992 (declare (obsolete nil
"25.1"))
993 (interactive (browse-url-interactive-arg "URL: "))
994 (setq url
(browse-url-encode-url url
))
995 (let* ((process-environment (browse-url-process-environment))
997 (apply 'start-process
998 (concat "netscape " url
) nil
999 browse-url-netscape-program
1001 browse-url-netscape-arguments
1002 (if (eq window-system
'w32
)
1005 (if new-window
'("-noraise"))
1007 (concat "openURL(" url
1008 (if (browse-url-maybe-new-window
1010 (if browse-url-netscape-new-window-is-tab
1014 (set-process-sentinel process
1015 `(lambda (process change
)
1016 (browse-url-netscape-sentinel process
,url
)))))
1018 (defun browse-url-netscape-sentinel (process url
)
1019 "Handle a change to the process communicating with Netscape."
1020 (declare (obsolete nil
"25.1"))
1021 (or (eq (process-exit-status process
) 0)
1022 (let* ((process-environment (browse-url-process-environment)))
1023 ;; Netscape not running - start it
1024 (message "Starting %s..." browse-url-netscape-program
)
1025 (apply 'start-process
(concat "netscape" url
) nil
1026 browse-url-netscape-program
1027 (append browse-url-netscape-startup-arguments
(list url
))))))
1029 (defun browse-url-netscape-reload ()
1030 "Ask Netscape to reload its current document.
1031 How depends on `browse-url-netscape-version'."
1032 (declare (obsolete nil
"25.1"))
1034 ;; Backwards incompatibility reported by
1035 ;; <peter.kruse@psychologie.uni-regensburg.de>.
1036 (browse-url-netscape-send (if (>= browse-url-netscape-version
4)
1037 "xfeDoCommand(reload)"
1040 (defun browse-url-netscape-send (command)
1041 "Send a remote control command to Netscape."
1042 (declare (obsolete nil
"25.1"))
1043 (let* ((process-environment (browse-url-process-environment)))
1044 (apply 'start-process
"netscape" nil
1045 browse-url-netscape-program
1046 (append browse-url-netscape-arguments
1047 (list "-remote" command
)))))
1050 (defun browse-url-mozilla (url &optional new-window
)
1051 "Ask the Mozilla WWW browser to load URL.
1052 Default to the URL around or before point. The strings in variable
1053 `browse-url-mozilla-arguments' are also passed to Mozilla.
1055 When called interactively, if variable `browse-url-new-window-flag' is
1056 non-nil, load the document in a new Mozilla window, otherwise use a
1057 random existing one. A non-nil interactive prefix argument reverses
1058 the effect of `browse-url-new-window-flag'.
1060 If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
1061 document would otherwise be loaded in a new window, it is loaded in a
1062 new tab in an existing window instead.
1064 When called non-interactively, optional second argument NEW-WINDOW is
1065 used instead of `browse-url-new-window-flag'."
1066 (interactive (browse-url-interactive-arg "URL: "))
1067 (setq url
(browse-url-encode-url url
))
1068 (let* ((process-environment (browse-url-process-environment))
1070 (apply 'start-process
1071 (concat "mozilla " url
) nil
1072 browse-url-mozilla-program
1074 browse-url-mozilla-arguments
1078 (if (browse-url-maybe-new-window
1080 (if browse-url-mozilla-new-window-is-tab
1084 (set-process-sentinel process
1085 `(lambda (process change
)
1086 (browse-url-mozilla-sentinel process
,url
)))))
1088 (defun browse-url-mozilla-sentinel (process url
)
1089 "Handle a change to the process communicating with Mozilla."
1090 (or (eq (process-exit-status process
) 0)
1091 (let* ((process-environment (browse-url-process-environment)))
1092 ;; Mozilla is not running - start it
1093 (message "Starting %s..." browse-url-mozilla-program
)
1094 (apply 'start-process
(concat "mozilla " url
) nil
1095 browse-url-mozilla-program
1096 (append browse-url-mozilla-startup-arguments
(list url
))))))
1099 (defun browse-url-firefox (url &optional new-window
)
1100 "Ask the Firefox WWW browser to load URL.
1101 Defaults to the URL around or before point. Passes the strings
1102 in the variable `browse-url-firefox-arguments' to Firefox.
1104 Interactively, if the variable `browse-url-new-window-flag' is non-nil,
1105 loads the document in a new Firefox window. A non-nil prefix argument
1106 reverses the effect of `browse-url-new-window-flag'.
1108 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1109 whenever a document would otherwise be loaded in a new window, it
1110 is loaded in a new tab in an existing window instead.
1112 Non-interactively, this uses the optional second argument NEW-WINDOW
1113 instead of `browse-url-new-window-flag'."
1114 (interactive (browse-url-interactive-arg "URL: "))
1115 (setq url
(browse-url-encode-url url
))
1116 (let* ((process-environment (browse-url-process-environment)))
1117 (apply 'start-process
1118 (concat "firefox " url
) nil
1119 browse-url-firefox-program
1121 browse-url-firefox-arguments
1122 (if (browse-url-maybe-new-window new-window
)
1123 (if browse-url-firefox-new-window-is-tab
1129 (defun browse-url-chromium (url &optional _new-window
)
1130 "Ask the Chromium WWW browser to load URL.
1131 Default to the URL around or before point. The strings in
1132 variable `browse-url-chromium-arguments' are also passed to
1134 The optional argument NEW-WINDOW is not used."
1135 (interactive (browse-url-interactive-arg "URL: "))
1136 (setq url
(browse-url-encode-url url
))
1137 (let* ((process-environment (browse-url-process-environment)))
1138 (apply 'start-process
1139 (concat "chromium " url
) nil
1140 browse-url-chromium-program
1142 browse-url-chromium-arguments
1145 (defun browse-url-chrome (url &optional _new-window
)
1146 "Ask the Google Chrome WWW browser to load URL.
1147 Default to the URL around or before point. The strings in
1148 variable `browse-url-chrome-arguments' are also passed to
1150 The optional argument NEW-WINDOW is not used."
1151 (interactive (browse-url-interactive-arg "URL: "))
1152 (setq url
(browse-url-encode-url url
))
1153 (let* ((process-environment (browse-url-process-environment)))
1154 (apply 'start-process
1155 (concat "google-chrome " url
) nil
1156 browse-url-chrome-program
1158 browse-url-chrome-arguments
1162 (defun browse-url-galeon (url &optional new-window
)
1163 "Ask the Galeon WWW browser to load URL.
1164 Default to the URL around or before point. The strings in variable
1165 `browse-url-galeon-arguments' are also passed to Galeon.
1167 When called interactively, if variable `browse-url-new-window-flag' is
1168 non-nil, load the document in a new Galeon window, otherwise use a
1169 random existing one. A non-nil interactive prefix argument reverses
1170 the effect of `browse-url-new-window-flag'.
1172 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1173 document would otherwise be loaded in a new window, it is loaded in a
1174 new tab in an existing window instead.
1176 When called non-interactively, optional second argument NEW-WINDOW is
1177 used instead of `browse-url-new-window-flag'."
1178 (declare (obsolete nil
"25.1"))
1179 (interactive (browse-url-interactive-arg "URL: "))
1180 (setq url
(browse-url-encode-url url
))
1181 (let* ((process-environment (browse-url-process-environment))
1182 (process (apply 'start-process
1183 (concat "galeon " url
)
1185 browse-url-galeon-program
1187 browse-url-galeon-arguments
1188 (if (browse-url-maybe-new-window new-window
)
1189 (if browse-url-galeon-new-window-is-tab
1191 '("--new-window" "--noraise"))
1194 (set-process-sentinel process
1195 `(lambda (process change
)
1196 (browse-url-galeon-sentinel process
,url
)))))
1198 (defun browse-url-galeon-sentinel (process url
)
1199 "Handle a change to the process communicating with Galeon."
1200 (declare (obsolete nil
"25.1"))
1201 (or (eq (process-exit-status process
) 0)
1202 (let* ((process-environment (browse-url-process-environment)))
1203 ;; Galeon is not running - start it
1204 (message "Starting %s..." browse-url-galeon-program
)
1205 (apply 'start-process
(concat "galeon " url
) nil
1206 browse-url-galeon-program
1207 (append browse-url-galeon-startup-arguments
(list url
))))))
1209 (defun browse-url-epiphany (url &optional new-window
)
1210 "Ask the Epiphany WWW browser to load URL.
1211 Default to the URL around or before point. The strings in variable
1212 `browse-url-galeon-arguments' are also passed to Epiphany.
1214 When called interactively, if variable `browse-url-new-window-flag' is
1215 non-nil, load the document in a new Epiphany window, otherwise use a
1216 random existing one. A non-nil interactive prefix argument reverses
1217 the effect of `browse-url-new-window-flag'.
1219 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1220 document would otherwise be loaded in a new window, it is loaded in a
1221 new tab in an existing window instead.
1223 When called non-interactively, optional second argument NEW-WINDOW is
1224 used instead of `browse-url-new-window-flag'."
1225 (interactive (browse-url-interactive-arg "URL: "))
1226 (setq url
(browse-url-encode-url url
))
1227 (let* ((process-environment (browse-url-process-environment))
1228 (process (apply 'start-process
1229 (concat "epiphany " url
)
1231 browse-url-epiphany-program
1233 browse-url-epiphany-arguments
1234 (if (browse-url-maybe-new-window new-window
)
1235 (if browse-url-epiphany-new-window-is-tab
1237 '("--new-window" "--noraise"))
1240 (set-process-sentinel process
1241 `(lambda (process change
)
1242 (browse-url-epiphany-sentinel process
,url
)))))
1244 (defun browse-url-epiphany-sentinel (process url
)
1245 "Handle a change to the process communicating with Epiphany."
1246 (or (eq (process-exit-status process
) 0)
1247 (let* ((process-environment (browse-url-process-environment)))
1248 ;; Epiphany is not running - start it
1249 (message "Starting %s..." browse-url-epiphany-program
)
1250 (apply 'start-process
(concat "epiphany " url
) nil
1251 browse-url-epiphany-program
1252 (append browse-url-epiphany-startup-arguments
(list url
))))))
1254 (defvar url-handler-regexp
)
1257 (defun browse-url-emacs (url &optional _new-window
)
1258 "Ask Emacs to load URL into a buffer and show it in another window."
1259 (interactive (browse-url-interactive-arg "URL: "))
1260 (require 'url-handlers
)
1261 (let ((file-name-handler-alist
1262 (cons (cons url-handler-regexp
'url-file-handler
)
1263 file-name-handler-alist
)))
1264 ;; Ignore `new-window': with all other browsers the URL is always shown
1265 ;; in another window than the current Emacs one since it's shown in
1266 ;; another application's window.
1267 ;; (if new-window (find-file-other-window url) (find-file url))
1268 (find-file-other-window url
)))
1271 (defun browse-url-gnome-moz (url &optional new-window
)
1272 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1273 Default to the URL around or before point. The strings in variable
1274 `browse-url-gnome-moz-arguments' are also passed.
1276 When called interactively, if variable `browse-url-new-window-flag' is
1277 non-nil, load the document in a new browser window, otherwise use an
1278 existing one. A non-nil interactive prefix argument reverses the
1279 effect of `browse-url-new-window-flag'.
1281 When called non-interactively, optional second argument NEW-WINDOW is
1282 used instead of `browse-url-new-window-flag'."
1283 (declare (obsolete nil
"25.1"))
1284 (interactive (browse-url-interactive-arg "URL: "))
1285 (apply 'start-process
(concat "gnome-moz-remote " url
)
1287 browse-url-gnome-moz-program
1289 browse-url-gnome-moz-arguments
1290 (if (browse-url-maybe-new-window new-window
)
1292 (list "--raise" url
))))
1297 (defun browse-url-mosaic (url &optional new-window
)
1298 "Ask the XMosaic WWW browser to load URL.
1300 Default to the URL around or before point. The strings in variable
1301 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1302 program is invoked according to the variable
1303 `browse-url-mosaic-program'.
1305 When called interactively, if variable `browse-url-new-window-flag' is
1306 non-nil, load the document in a new Mosaic window, otherwise use a
1307 random existing one. A non-nil interactive prefix argument reverses
1308 the effect of `browse-url-new-window-flag'.
1310 When called non-interactively, optional second argument NEW-WINDOW is
1311 used instead of `browse-url-new-window-flag'."
1312 (declare (obsolete nil
"25.1"))
1313 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1314 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile
))
1316 (if (file-readable-p pidfile
)
1318 (insert-file-contents pidfile
)
1319 (setq pid
(read (current-buffer)))))
1320 (if (and (integerp pid
) (zerop (signal-process pid
0))) ; Mosaic running
1323 (insert (if (browse-url-maybe-new-window new-window
)
1327 (with-file-modes ?
\700
1329 (setq pidfile
(format "/tmp/Mosaic.%d" pid
)))
1330 (delete-file pidfile
))
1331 ;; http://debbugs.gnu.org/17428. Use O_EXCL.
1332 (write-region nil nil pidfile nil
'silent nil
'excl
)))
1333 ;; Send signal SIGUSR to Mosaic
1334 (message "Signaling Mosaic...")
1335 (signal-process pid
'SIGUSR1
)
1336 ;; Or you could try:
1337 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1338 (message "Signaling Mosaic...done"))
1339 ;; Mosaic not running - start it
1340 (message "Starting %s..." browse-url-mosaic-program
)
1341 (apply 'start-process
"xmosaic" nil browse-url-mosaic-program
1342 (append browse-url-mosaic-arguments
(list url
)))
1343 (message "Starting %s...done" browse-url-mosaic-program
))))
1345 ;; --- Mosaic using CCI ---
1348 (defun browse-url-cci (url &optional new-window
)
1349 "Ask the XMosaic WWW browser to load URL.
1350 Default to the URL around or before point.
1352 This function only works for XMosaic version 2.5 or later. You must
1353 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1354 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1356 When called interactively, if variable `browse-url-new-window-flag' is
1357 non-nil, load the document in a new browser window, otherwise use a
1358 random existing one. A non-nil interactive prefix argument reverses
1359 the effect of `browse-url-new-window-flag'.
1361 When called non-interactively, optional second argument NEW-WINDOW is
1362 used instead of `browse-url-new-window-flag'."
1363 (declare (obsolete nil
"25.1"))
1364 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1365 (open-network-stream "browse-url" " *browse-url*"
1366 browse-url-CCI-host browse-url-CCI-port
)
1367 ;; Todo: start browser if fails
1368 (process-send-string "browse-url"
1369 (concat "get url (" url
") output "
1370 (if (browse-url-maybe-new-window new-window
)
1374 (process-send-string "browse-url" "disconnect\r\n")
1375 (delete-process "browse-url"))
1379 (defun browse-url-conkeror (url &optional new-window
)
1380 "Ask the Conkeror WWW browser to load URL.
1381 Default to the URL around or before point. Also pass the strings
1382 in the variable `browse-url-conkeror-arguments' to Conkeror.
1384 When called interactively, if variable
1385 `browse-url-new-window-flag' is non-nil, load the document in a
1386 new Conkeror window, otherwise use a random existing one. A
1387 non-nil interactive prefix argument reverses the effect of
1388 `browse-url-new-window-flag'.
1390 If variable `browse-url-conkeror-new-window-is-buffer' is
1391 non-nil, then whenever a document would otherwise be loaded in a
1392 new window, load it in a new buffer in an existing window instead.
1394 When called non-interactively, use optional second argument
1395 NEW-WINDOW instead of `browse-url-new-window-flag'."
1396 (interactive (browse-url-interactive-arg "URL: "))
1397 (setq url
(browse-url-encode-url url
))
1398 (let* ((process-environment (browse-url-process-environment)))
1399 (apply 'start-process
(format "conkeror %s" url
)
1401 browse-url-conkeror-program
1403 browse-url-conkeror-arguments
1406 (format "load_url_in_new_%s('%s')"
1407 (if (browse-url-maybe-new-window new-window
)
1408 (if browse-url-conkeror-new-window-is-buffer
1416 (declare-function w3-fetch-other-window
"ext:w3m" (&optional url
))
1417 (declare-function w3-fetch
"ext:w3m" (&optional url target
))
1420 (defun browse-url-w3 (url &optional new-window
)
1421 "Ask the w3 WWW browser to load URL.
1422 Default to the URL around or before point.
1424 When called interactively, if variable `browse-url-new-window-flag' is
1425 non-nil, load the document in a new window. A non-nil interactive
1426 prefix argument reverses the effect of `browse-url-new-window-flag'.
1428 When called non-interactively, optional second argument NEW-WINDOW is
1429 used instead of `browse-url-new-window-flag'."
1430 (interactive (browse-url-interactive-arg "W3 URL: "))
1431 (require 'w3
) ; w3-fetch-other-window not autoloaded
1432 (if (browse-url-maybe-new-window new-window
)
1433 (w3-fetch-other-window url
)
1437 (defun browse-url-w3-gnudoit (url &optional _new-window
)
1438 ;; new-window ignored
1439 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1440 The `browse-url-gnudoit-program' program is used with options given by
1441 `browse-url-gnudoit-args'. Default to the URL around or before point."
1442 (declare (obsolete nil
"25.1"))
1443 (interactive (browse-url-interactive-arg "W3 URL: "))
1444 (apply 'start-process
(concat "gnudoit:" url
) nil
1445 browse-url-gnudoit-program
1446 (append browse-url-gnudoit-args
1447 (list (concat "(w3-fetch \"" url
"\")")
1450 ;; --- Lynx in an xterm ---
1453 (defun browse-url-text-xterm (url &optional _new-window
)
1454 ;; new-window ignored
1455 "Ask a text browser to load URL.
1456 URL defaults to the URL around or before point.
1457 This runs the text browser specified by `browse-url-text-browser'.
1458 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1459 with possible additional arguments `browse-url-xterm-args'.
1460 The optional argument NEW-WINDOW is not used."
1461 (interactive (browse-url-interactive-arg "Text browser URL: "))
1462 (apply #'start-process
`(,(concat browse-url-text-browser url
)
1463 nil
,browse-url-xterm-program
1464 ,@browse-url-xterm-args
"-e" ,browse-url-text-browser
1467 ;; --- Lynx in an Emacs "term" window ---
1469 (declare-function term-char-mode
"term" ())
1470 (declare-function term-send-down
"term" ())
1471 (declare-function term-send-string
"term" (proc str
))
1474 (defun browse-url-text-emacs (url &optional new-buffer
)
1475 "Ask a text browser to load URL.
1476 URL defaults to the URL around or before point.
1477 This runs the text browser specified by `browse-url-text-browser'.
1478 With a prefix argument, it runs a new browser process in a new buffer.
1480 When called interactively, if variable `browse-url-new-window-flag' is
1481 non-nil, load the document in a new browser process in a new term window,
1482 otherwise use any existing one. A non-nil interactive prefix argument
1483 reverses the effect of `browse-url-new-window-flag'.
1485 When called non-interactively, optional second argument NEW-WINDOW is
1486 used instead of `browse-url-new-window-flag'."
1487 (interactive (browse-url-interactive-arg "Text browser URL: "))
1488 (let* ((system-uses-terminfo t
) ; Lynx uses terminfo
1489 ;; (term-term-name "vt100") ; ??
1490 (buf (get-buffer "*text browser*"))
1491 (proc (and buf
(get-buffer-process buf
)))
1492 (n browse-url-text-input-attempts
))
1494 (if (and (browse-url-maybe-new-window new-buffer
) buf
)
1495 ;; Rename away the OLD buffer. This isn't very polite, but
1496 ;; term insists on working in a buffer named *lynx* and would
1497 ;; choke on *lynx*<1>
1498 (progn (set-buffer buf
)
1500 (if (or (browse-url-maybe-new-window new-buffer
)
1503 (not (memq (process-status proc
) '(run stop
))))
1504 ;; start a new text browser
1508 `(,browse-url-text-browser
1509 ,browse-url-text-browser
1510 nil
,@browse-url-text-emacs-args
1512 (switch-to-buffer buf
)
1514 (set-process-sentinel
1515 (get-buffer-process buf
)
1516 ;; Don't leave around a dead one (especially because of its
1518 (lambda (process _event
)
1519 (if (not (memq (process-status process
) '(run stop
)))
1520 (let ((buf (process-buffer process
)))
1521 (if buf
(kill-buffer buf
)))))))
1522 ;; Send the url to the text browser in the old buffer
1523 (let ((win (get-buffer-window buf t
)))
1526 (switch-to-buffer buf
)))
1527 (if (eq (following-char) ?_
)
1528 (cond ((eq browse-url-text-input-field
'warn
)
1529 (error "Please move out of the input field first"))
1530 ((eq browse-url-text-input-field
'avoid
)
1531 (while (and (eq (following-char) ?_
) (> n
0))
1532 (term-send-down) ; down arrow
1533 (sit-for browse-url-text-input-delay
))
1534 (if (eq (following-char) ?_
)
1535 (error "Cannot move out of the input field, sorry")))))
1536 (term-send-string proc
(concat "g" ; goto
1537 "\C-u" ; kill default url
1543 (autoload 'rfc2368-parse-mailto-url
"rfc2368")
1546 (defun browse-url-mail (url &optional new-window
)
1547 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1548 Default to using the mailto: URL around or before point as the
1549 recipient's address. Supplying a non-nil interactive prefix argument
1550 will cause the mail to be composed in another window rather than the
1553 When called interactively, if variable `browse-url-new-window-flag' is
1554 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1555 non-nil interactive prefix argument reverses the effect of
1556 `browse-url-new-window-flag'.
1558 When called non-interactively, optional second argument NEW-WINDOW is
1559 used instead of `browse-url-new-window-flag'."
1560 (interactive (browse-url-interactive-arg "Mailto URL: "))
1562 (let* ((alist (rfc2368-parse-mailto-url url
))
1563 (to (assoc "To" alist
))
1564 (subject (assoc "Subject" alist
))
1565 (body (assoc "Body" alist
))
1566 (rest (delq to
(delq subject
(delq body alist
))))
1568 (subject (cdr subject
))
1570 (mail-citation-hook (unless body mail-citation-hook
)))
1571 (if (browse-url-maybe-new-window new-window
)
1572 (compose-mail-other-window to subject rest nil
1573 (list 'insert-buffer
(current-buffer)))
1574 (compose-mail to subject rest nil nil
1575 (list 'insert-buffer
(current-buffer))))
1577 (goto-char (point-min))
1578 (unless (or (search-forward (concat "\n" mail-header-separator
"\n")
1584 (insert (replace-regexp-in-string "\r\n" "\n" body
))
1586 (insert "\n"))))))))
1588 ;; --- Random browser ---
1591 (defun browse-url-generic (url &optional _new-window
)
1592 ;; new-window ignored
1593 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1594 Default to the URL around or before point. A fresh copy of the
1595 browser is started up in a new process with possible additional arguments
1596 `browse-url-generic-args'. This is appropriate for browsers which
1597 don't offer a form of remote control."
1598 (interactive (browse-url-interactive-arg "URL: "))
1599 (if (not browse-url-generic-program
)
1600 (error "No browser defined (`browse-url-generic-program')"))
1601 (apply 'call-process browse-url-generic-program nil
1603 (append browse-url-generic-args
(list url
))))
1606 (defun browse-url-kde (url &optional _new-window
)
1607 "Ask the KDE WWW browser to load URL.
1608 Default to the URL around or before point.
1609 The optional argument NEW-WINDOW is not used."
1610 (interactive (browse-url-interactive-arg "KDE URL: "))
1611 (message "Sending URL to KDE...")
1612 (apply #'start-process
(concat "KDE " url
) nil browse-url-kde-program
1613 (append browse-url-kde-args
(list url
))))
1615 (defun browse-url-elinks-new-window (url)
1616 "Ask the Elinks WWW browser to load URL in a new window."
1617 (let ((process-environment (browse-url-process-environment)))
1618 (apply #'start-process
1619 (append (list (concat "elinks:" url
)
1621 browse-url-elinks-wrapper
1622 (list "elinks" url
)))))
1625 (defun browse-url-elinks (url &optional new-window
)
1626 "Ask the Elinks WWW browser to load URL.
1627 Default to the URL around the point.
1629 The document is loaded in a new tab of a running Elinks or, if
1630 none yet running, a newly started instance.
1632 The Elinks command will be prepended by the program+arguments
1633 from `browse-url-elinks-wrapper'."
1634 (interactive (browse-url-interactive-arg "URL: "))
1635 (setq url
(browse-url-encode-url url
))
1637 (browse-url-elinks-new-window url
)
1638 (let ((process-environment (browse-url-process-environment))
1639 (elinks-ping-process (start-process "elinks-ping" nil
1640 "elinks" "-remote" "ping()")))
1641 (set-process-sentinel elinks-ping-process
1642 `(lambda (process change
)
1643 (browse-url-elinks-sentinel process
,url
))))))
1645 (defun browse-url-elinks-sentinel (process url
)
1646 "Determines if Elinks is running or a new one has to be started."
1647 ;; Try to determine if an instance is running or if we have to
1648 ;; create a new one.
1649 (pcase (process-exit-status process
)
1651 ;; No instance, start a new one.
1652 (browse-url-elinks-new-window url
))
1654 ;; Found an instance, open URL in new tab.
1655 (let ((process-environment (browse-url-process-environment)))
1656 (start-process (concat "elinks:" url
) nil
1658 (concat "openURL(\"" url
"\",new-tab)"))))
1660 (error "Unrecognized exit-code %d of process `elinks'"
1663 (provide 'browse-url
)
1665 ;;; browse-url.el ends here