; Merge from origin/emacs-24
[emacs.git] / lisp / net / browse-url.el
blob3f8cb841c6f50571fb318ac2b51c7e46822812a0
1 ;;; browse-url.el --- pass a URL to a WWW browser
3 ;; Copyright (C) 1995-2015 Free Software Foundation, Inc.
5 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: 03 Apr 1995
8 ;; Keywords: hypertext, hypermedia, mouse
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package provides functions which read a URL (Uniform Resource
28 ;; Locator) from the minibuffer, defaulting to the URL around point,
29 ;; and ask a World-Wide Web browser to load it. It can also load the
30 ;; URL associated with the current buffer. Different browsers use
31 ;; different methods of remote control so there is one function for
32 ;; each supported browser. If the chosen browser is not running, it
33 ;; is started. Currently there is support for the following browsers,
34 ;; as well as some other obsolete ones:
36 ;; Function Browser Earliest version
37 ;; browse-url-mozilla Mozilla Don't know
38 ;; browse-url-firefox Firefox Don't know (tried with 1.0.1)
39 ;; browse-url-chromium Chromium 3.0
40 ;; browse-url-epiphany Epiphany Don't know
41 ;; browse-url-conkeror Conkeror Don't know
42 ;; browse-url-w3 w3 0
43 ;; browse-url-text-* Any text browser 0
44 ;; browse-url-generic arbitrary
45 ;; browse-url-default-windows-browser MS-Windows browser
46 ;; browse-url-default-macosx-browser Mac OS X browser
47 ;; browse-url-xdg-open Free Desktop xdg-open on Gnome, KDE, Xfce4, LXDE
48 ;; browse-url-kde KDE konqueror (kfm)
49 ;; browse-url-elinks Elinks Don't know (tried with 0.12.GIT)
51 ;; Browsers can cache Web pages so it may be necessary to tell them to
52 ;; reload the current page if it has changed (e.g., if you have edited
53 ;; it). There is currently no perfect automatic solution to this.
55 ;; This package generalizes function html-previewer-process in Marc
56 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
57 ;; ffap.el package. The huge hyperbole package also contains similar
58 ;; functions.
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;; Usage
63 ;; To display the URL at or before point:
64 ;; M-x browse-url-at-point RET
65 ;; or, similarly but with the opportunity to edit the URL extracted from
66 ;; the buffer, use:
67 ;; M-x browse-url
69 ;; To display a URL by shift-clicking on it, put this in your init file:
70 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
71 ;; (Note that using Shift-mouse-1 is not desirable because
72 ;; that event has a standard meaning in Emacs.)
74 ;; To display the current buffer in a web browser:
75 ;; M-x browse-url-of-buffer RET
77 ;; To display the current region in a web browser:
78 ;; M-x browse-url-of-region RET
80 ;; In Dired, to display the file named on the current line:
81 ;; M-x browse-url-of-dired-file RET
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 ;; Customization (~/.emacs)
86 ;; To see what variables are available for customization, type
87 ;; `M-x set-variable browse-url TAB'. Better, use
88 ;; `M-x customize-group browse-url'.
90 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
91 ;; (as used by html-helper-mode):
92 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
93 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
94 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
95 ;; (global-set-key "\C-c\C-zu" 'browse-url)
96 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
97 ;; (add-hook 'dired-mode-hook
98 ;; (lambda ()
99 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
101 ;; Browse URLs in mail messages under RMAIL by clicking mouse-2:
102 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
103 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
104 ;; Alternatively, add `goto-address' to `rmail-show-message-hook'.
106 ;; Gnus provides a standard feature to activate URLs in article
107 ;; buffers for invocation of browse-url.
109 ;; Use the Emacs w3 browser when not running under X11:
110 ;; (or (eq window-system 'x)
111 ;; (setq browse-url-browser-function 'browse-url-w3))
113 ;; To always save modified buffers before displaying the file in a browser:
114 ;; (setq browse-url-save-file t)
116 ;; To invoke different browsers for different URLs:
117 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
118 ;; ("." . browse-url-firefox)))
120 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121 ;;; Code:
123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 ;; Variables
126 (defgroup browse-url nil
127 "Use a web browser to look at a URL."
128 :prefix "browse-url-"
129 :link '(emacs-commentary-link "browse-url")
130 :group 'external
131 :group 'comm)
133 ;;;###autoload
134 (defcustom browse-url-browser-function
135 'browse-url-default-browser
136 "Function to display the current buffer in a WWW browser.
137 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
138 `browse-url-of-file' commands.
140 If the value is not a function it should be a list of pairs
141 \(REGEXP . FUNCTION). In this case the function called will be the one
142 associated with the first REGEXP which matches the current URL. The
143 function is passed the URL and any other args of `browse-url'. The last
144 regexp should probably be \".\" to specify a default browser."
145 :type '(choice
146 (function-item :tag "Emacs W3" :value browse-url-w3)
147 (function-item :tag "eww" :value eww-browse-url)
148 (function-item :tag "Mozilla" :value browse-url-mozilla)
149 (function-item :tag "Firefox" :value browse-url-firefox)
150 (function-item :tag "Chromium" :value browse-url-chromium)
151 (function-item :tag "Epiphany" :value browse-url-epiphany)
152 (function-item :tag "Conkeror" :value browse-url-conkeror)
153 (function-item :tag "Text browser in an xterm window"
154 :value browse-url-text-xterm)
155 (function-item :tag "Text browser in an Emacs window"
156 :value browse-url-text-emacs)
157 (function-item :tag "KDE" :value browse-url-kde)
158 (function-item :tag "Elinks" :value browse-url-elinks)
159 (function-item :tag "Specified by `Browse Url Generic Program'"
160 :value browse-url-generic)
161 (function-item :tag "Default Windows browser"
162 :value browse-url-default-windows-browser)
163 (function-item :tag "Default Mac OS X browser"
164 :value browse-url-default-macosx-browser)
165 (function-item :tag "Default browser"
166 :value browse-url-default-browser)
167 (function :tag "Your own function")
168 (alist :tag "Regexp/function association list"
169 :key-type regexp :value-type function))
170 :version "24.1"
171 :group 'browse-url)
173 (defcustom browse-url-mailto-function 'browse-url-mail
174 "Function to display mailto: links.
175 This variable uses the same syntax as the
176 `browse-url-browser-function' variable. If the
177 `browse-url-mailto-function' variable is nil, that variable will
178 be used instead."
179 :type '(choice
180 (function-item :tag "Emacs Mail" :value browse-url-mail)
181 (function-item :tag "None" nil))
182 :version "24.1"
183 :group 'browse-url)
185 (defcustom browse-url-netscape-program "netscape"
186 ;; Info about netscape-remote from Karl Berry.
187 "The name by which to invoke Netscape.
189 The free program `netscape-remote' from
190 <URL:http://home.netscape.com/newsref/std/remote.c> is said to start
191 up very much quicker than `netscape'. Reported to compile on a GNU
192 system, given vroot.h from the same directory, with cc flags
193 -DSTANDALONE -L/usr/X11R6/lib -lXmu -lX11."
194 :type 'string
195 :group 'browse-url)
197 (make-obsolete-variable 'browse-url-netscape-program nil "25.1")
199 (defcustom browse-url-netscape-arguments nil
200 "A list of strings to pass to Netscape as arguments."
201 :type '(repeat (string :tag "Argument"))
202 :group 'browse-url)
204 (make-obsolete-variable 'browse-url-netscape-arguments nil "25.1")
206 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
207 "A list of strings to pass to Netscape when it starts up.
208 Defaults to the value of `browse-url-netscape-arguments' at the time
209 `browse-url' is loaded."
210 :type '(repeat (string :tag "Argument"))
212 :group 'browse-url)
214 (make-obsolete-variable 'browse-url-netscape-startup-arguments nil "25.1")
216 (defcustom browse-url-browser-display nil
217 "The X display for running the browser, if not same as Emacs's."
218 :type '(choice string (const :tag "Default" nil))
219 :group 'browse-url)
221 (defcustom browse-url-mozilla-program "mozilla"
222 "The name by which to invoke Mozilla."
223 :type 'string
224 :group 'browse-url)
226 (defcustom browse-url-mozilla-arguments nil
227 "A list of strings to pass to Mozilla as arguments."
228 :type '(repeat (string :tag "Argument"))
229 :group 'browse-url)
231 (defcustom browse-url-mozilla-startup-arguments browse-url-mozilla-arguments
232 "A list of strings to pass to Mozilla when it starts up.
233 Defaults to the value of `browse-url-mozilla-arguments' at the time
234 `browse-url' is loaded."
235 :type '(repeat (string :tag "Argument"))
236 :group 'browse-url)
238 (defcustom browse-url-firefox-program
239 (let ((candidates '("icecat" "iceweasel" "firefox")))
240 (while (and candidates (not (executable-find (car candidates))))
241 (setq candidates (cdr candidates)))
242 (or (car candidates) "firefox"))
243 "The name by which to invoke Firefox or a variant of it."
244 :type 'string
245 :group 'browse-url)
247 (defcustom browse-url-firefox-arguments nil
248 "A list of strings to pass to Firefox (or variant) as arguments."
249 :type '(repeat (string :tag "Argument"))
250 :group 'browse-url)
252 (defcustom browse-url-firefox-startup-arguments browse-url-firefox-arguments
253 "A list of strings to pass to Firefox (or variant) when it starts up.
254 Defaults to the value of `browse-url-firefox-arguments' at the time
255 `browse-url' is loaded."
256 :type '(repeat (string :tag "Argument"))
257 :group 'browse-url)
259 (make-obsolete-variable 'browse-url-firefox-startup-arguments
260 "it no longer has any effect." "24.5")
262 (defcustom browse-url-chromium-program
263 (let ((candidates '("chromium" "chromium-browser")))
264 (while (and candidates (not (executable-find (car candidates))))
265 (setq candidates (cdr candidates)))
266 (or (car candidates) "chromium"))
267 "The name by which to invoke Chromium."
268 :type 'string
269 :version "24.1"
270 :group 'browse-url)
272 (defcustom browse-url-chromium-arguments nil
273 "A list of strings to pass to Chromium as arguments."
274 :type '(repeat (string :tag "Argument"))
275 :version "24.1"
276 :group 'browse-url)
278 (defcustom browse-url-galeon-program "galeon"
279 "The name by which to invoke Galeon."
280 :type 'string
281 :group 'browse-url)
283 (make-obsolete-variable 'browse-url-galeon-program nil "25.1")
285 (defcustom browse-url-galeon-arguments nil
286 "A list of strings to pass to Galeon as arguments."
287 :type '(repeat (string :tag "Argument"))
288 :group 'browse-url)
290 (make-obsolete-variable 'browse-url-galeon-arguments nil "25.1")
292 (defcustom browse-url-galeon-startup-arguments browse-url-galeon-arguments
293 "A list of strings to pass to Galeon when it starts up.
294 Defaults to the value of `browse-url-galeon-arguments' at the time
295 `browse-url' is loaded."
296 :type '(repeat (string :tag "Argument"))
297 :group 'browse-url)
299 (make-obsolete-variable 'browse-url-galeon-startup-arguments nil "25.1")
301 (defcustom browse-url-epiphany-program "epiphany"
302 "The name by which to invoke Epiphany."
303 :type 'string
304 :group 'browse-url)
306 (defcustom browse-url-epiphany-arguments nil
307 "A list of strings to pass to Epiphany as arguments."
308 :type '(repeat (string :tag "Argument"))
309 :group 'browse-url)
311 (defcustom browse-url-epiphany-startup-arguments browse-url-epiphany-arguments
312 "A list of strings to pass to Epiphany when it starts up.
313 Defaults to the value of `browse-url-epiphany-arguments' at the time
314 `browse-url' is loaded."
315 :type '(repeat (string :tag "Argument"))
316 :group 'browse-url)
318 ;; GNOME means of invoking either Mozilla or Netscape.
319 (defvar browse-url-gnome-moz-program "gnome-moz-remote")
321 (make-obsolete-variable 'browse-url-gnome-moz-program nil "25.1")
323 (defcustom browse-url-gnome-moz-arguments '()
324 "A list of strings passed to the GNOME mozilla viewer as arguments."
325 :version "21.1"
326 :type '(repeat (string :tag "Argument"))
327 :group 'browse-url)
329 (make-obsolete-variable 'browse-url-gnome-moz-arguments nil "25.1")
331 (defcustom browse-url-mozilla-new-window-is-tab nil
332 "Whether to open up new windows in a tab or a new window.
333 If non-nil, then open the URL in a new tab rather than a new window if
334 `browse-url-mozilla' is asked to open it in a new window."
335 :type 'boolean
336 :group 'browse-url)
338 (defcustom browse-url-firefox-new-window-is-tab nil
339 "Whether to open up new windows in a tab or a new window.
340 If non-nil, then open the URL in a new tab rather than a new window if
341 `browse-url-firefox' is asked to open it in a new window.
343 This option is currently ignored on MS-Windows, since the necessary
344 functionality is not available there."
345 :type 'boolean
346 :group 'browse-url)
348 (defcustom browse-url-conkeror-new-window-is-buffer nil
349 "Whether to open up new windows in a buffer or a new window.
350 If non-nil, then open the URL in a new buffer rather than a new window if
351 `browse-url-conkeror' is asked to open it in a new window."
352 :type 'boolean
353 :group 'browse-url)
355 (defcustom browse-url-galeon-new-window-is-tab nil
356 "Whether to open up new windows in a tab or a new window.
357 If non-nil, then open the URL in a new tab rather than a new window if
358 `browse-url-galeon' is asked to open it in a new window."
359 :type 'boolean
360 :group 'browse-url)
362 (make-obsolete-variable 'browse-url-galeon-new-window-is-tab nil "25.1")
364 (defcustom browse-url-epiphany-new-window-is-tab nil
365 "Whether to open up new windows in a tab or a new window.
366 If non-nil, then open the URL in a new tab rather than a new window if
367 `browse-url-epiphany' is asked to open it in a new window."
368 :type 'boolean
369 :group 'browse-url)
371 (defcustom browse-url-netscape-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
374 window if `browse-url-netscape' is asked to open it in a new
375 window."
376 :type 'boolean
377 :group 'browse-url)
379 (make-obsolete-variable 'browse-url-netscape-new-window-is-tab nil "25.1")
381 (defcustom browse-url-new-window-flag nil
382 "Non-nil means always open a new browser window with appropriate browsers.
383 Passing an interactive argument to \\[browse-url], or specific browser
384 commands reverses the effect of this variable."
385 :type 'boolean
386 :group 'browse-url)
388 (defcustom browse-url-mosaic-program "xmosaic"
389 "The name by which to invoke Mosaic (or mMosaic)."
390 :type 'string
391 :version "20.3"
392 :group 'browse-url)
394 (make-obsolete-variable 'browse-url-mosaic-program nil "25.1")
396 (defcustom browse-url-mosaic-arguments nil
397 "A list of strings to pass to Mosaic as arguments."
398 :type '(repeat (string :tag "Argument"))
399 :group 'browse-url)
401 (make-obsolete-variable 'browse-url-mosaic-arguments nil "25.1")
403 (defcustom browse-url-mosaic-pidfile "~/.mosaicpid"
404 "The name of the pidfile created by Mosaic."
405 :type 'string
406 :group 'browse-url)
408 (make-obsolete-variable 'browse-url-mosaic-pidfile nil "25.1")
410 (defcustom browse-url-conkeror-program "conkeror"
411 "The name by which to invoke Conkeror."
412 :type 'string
413 :version "25.1"
414 :group 'browse-url)
416 (defcustom browse-url-conkeror-arguments nil
417 "A list of strings to pass to Conkeror as arguments."
418 :type '(repeat (string :tag "Argument"))
419 :group 'browse-url)
421 (defcustom browse-url-filename-alist
422 `(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
423 ;; The above loses the username to avoid the browser prompting for
424 ;; it in anonymous cases. If it's not anonymous the next regexp
425 ;; applies.
426 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
427 ,@(if (memq system-type '(windows-nt ms-dos))
428 '(("^\\([a-zA-Z]:\\)[\\/]" . "file:///\\1/")
429 ("^[\\/][\\/]+" . "file://")))
430 ("^/+" . "file:///"))
431 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
432 Any substring of a filename matching one of the REGEXPs is replaced by
433 the corresponding STRING using `replace-match', not treating STRING
434 literally. All pairs are applied in the order given. The default
435 value converts ange-ftp/EFS-style file names into ftp URLs and prepends
436 `file:' to any file name beginning with `/'.
438 For example, adding to the default a specific translation of an ange-ftp
439 address to an HTTP URL:
441 (setq browse-url-filename-alist
442 '((\"/webmaster@webserver:/home/www/html/\" .
443 \"http://www.acme.co.uk/\")
444 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
445 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
446 (\"^/+\" . \"file:/\")))"
447 :type '(repeat (cons :format "%v"
448 (regexp :tag "Regexp")
449 (string :tag "Replacement")))
450 :version "23.1"
451 :group 'browse-url)
453 (defcustom browse-url-save-file nil
454 "If non-nil, save the buffer before displaying its file.
455 Used by the `browse-url-of-file' command."
456 :type 'boolean
457 :group 'browse-url)
459 (defcustom browse-url-of-file-hook nil
460 "Hook run after `browse-url-of-file' has asked a browser to load a file."
461 :type 'hook
462 :group 'browse-url)
464 (defcustom browse-url-CCI-port 3003
465 "Port to access XMosaic via CCI.
466 This can be any number between 1024 and 65535 but must correspond to
467 the value set in the browser."
468 :type 'integer
469 :group 'browse-url)
471 (make-obsolete-variable 'browse-url-CCI-port nil "25.1")
473 (defcustom browse-url-CCI-host "localhost"
474 "Host to access XMosaic via CCI.
475 This should be the host name of the machine running XMosaic with CCI
476 enabled. The port number should be set in `browse-url-CCI-port'."
477 :type 'string
478 :group 'browse-url)
480 (make-obsolete-variable 'browse-url-CCI-host nil "25.1")
482 (defvar browse-url-temp-file-name nil)
483 (make-variable-buffer-local 'browse-url-temp-file-name)
485 (defcustom browse-url-xterm-program "xterm"
486 "The name of the terminal emulator used by `browse-url-text-xterm'.
487 This might, for instance, be a separate color version of xterm."
488 :type 'string
489 :group 'browse-url)
491 (defcustom browse-url-xterm-args nil
492 "A list of strings defining options for `browse-url-xterm-program'.
493 These might set its size, for instance."
494 :type '(repeat (string :tag "Argument"))
495 :group 'browse-url)
497 (defcustom browse-url-gnudoit-program "gnudoit"
498 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
499 :type 'string
500 :group 'browse-url)
502 (defcustom browse-url-gnudoit-args '("-q")
503 "A list of strings defining options for `browse-url-gnudoit-program'.
504 These might set the port, for instance."
505 :type '(repeat (string :tag "Argument"))
506 :group 'browse-url)
508 (defcustom browse-url-generic-program nil
509 "The name of the browser program used by `browse-url-generic'."
510 :type '(choice string (const :tag "None" nil))
511 :group 'browse-url)
513 (defcustom browse-url-generic-args nil
514 "A list of strings defining options for `browse-url-generic-program'."
515 :type '(repeat (string :tag "Argument"))
516 :group 'browse-url)
518 (defcustom browse-url-temp-dir temporary-file-directory
519 "The name of a directory for browse-url's temporary files.
520 Such files are generated by functions like `browse-url-of-region'.
521 You might want to set this to somewhere with restricted read permissions
522 for privacy's sake."
523 :type 'string
524 :group 'browse-url)
526 (defcustom browse-url-netscape-version 3
527 "The version of Netscape you are using.
528 This affects how URL reloading is done; the mechanism changed
529 incompatibly at version 4."
530 :type 'number
531 :group 'browse-url)
533 (make-obsolete-variable 'browse-url-netscape-version nil "25.1")
535 (defcustom browse-url-text-browser "lynx"
536 "The name of the text browser to invoke."
537 :type 'string
538 :group 'browse-url
539 :version "23.1")
541 (defcustom browse-url-text-emacs-args (and (not window-system)
542 '("-show_cursor"))
543 "A list of strings defining options for a text browser in an Emacs buffer.
545 The default is none in a window system, otherwise `-show_cursor' to
546 indicate the position of the current link in the absence of
547 highlighting, assuming the normal default for showing the cursor."
548 :type '(repeat (string :tag "Argument"))
549 :version "23.1"
550 :group 'browse-url)
552 (defcustom browse-url-text-input-field 'avoid
553 "Action on selecting an existing text browser buffer at an input field.
554 What to do when sending a new URL to an existing text browser buffer in Emacs
555 if the browser cursor is on an input field (in which case the `g' command
556 would be entered as data). Such fields are recognized by the
557 underlines ____. Allowed values: nil: disregard it, `warn': warn the
558 user and don't emit the URL, `avoid': try to avoid the field by moving
559 down (this *won't* always work)."
560 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
561 (const :tag "Disregard" :value nil)
562 (const :tag "Warn, don't emit URL" :value warn))
563 :version "23.1"
564 :group 'browse-url)
566 (defcustom browse-url-text-input-attempts 10
567 "How many times to try to move down from a series of text browser input fields."
568 :type 'integer
569 :version "23.1"
570 :group 'browse-url)
572 (defcustom browse-url-text-input-delay 0.2
573 "Seconds to wait for a text browser between moves down from an input field."
574 :type 'number
575 :version "23.1"
576 :group 'browse-url)
578 (defcustom browse-url-kde-program "kfmclient"
579 "The name by which to invoke the KDE web browser."
580 :type 'string
581 :version "21.1"
582 :group 'browse-url)
584 (defcustom browse-url-kde-args '("openURL")
585 "A list of strings defining options for `browse-url-kde-program'."
586 :type '(repeat (string :tag "Argument"))
587 :group 'browse-url)
589 (defcustom browse-url-elinks-wrapper '("xterm" "-e")
590 "Wrapper command prepended to the Elinks command-line."
591 :type '(repeat (string :tag "Wrapper"))
592 :group 'browse-url)
594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
595 ;; URL encoding
597 (defun browse-url-url-encode-chars (text chars)
598 "URL-encode the chars in TEXT that match CHARS.
599 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
600 (let ((encoded-text (copy-sequence text))
601 (s 0))
602 (while (setq s (string-match chars encoded-text s))
603 (setq encoded-text
604 (replace-match (format "%%%X"
605 (string-to-char (match-string 0 encoded-text)))
606 t t encoded-text)
607 s (1+ s)))
608 encoded-text))
610 (defun browse-url-encode-url (url)
611 "Escape annoying characters in URL.
612 The annoying characters are those that can mislead a web browser
613 regarding its parameter treatment."
614 ;; FIXME: Is there an actual example of a web browser getting
615 ;; confused? (This used to encode commas, but at least Firefox
616 ;; handles commas correctly and doesn't accept encoded commas.)
617 (browse-url-url-encode-chars url "[\")$] "))
619 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620 ;; URL input
622 (defun browse-url-url-at-point ()
623 (or (thing-at-point 'url t)
624 ;; assume that the user is pointing at something like gnu.org/gnu
625 (let ((f (thing-at-point 'filename t)))
626 (and f (concat "http://" f)))))
628 ;; Having this as a separate function called by the browser-specific
629 ;; functions allows them to be stand-alone commands, making it easier
630 ;; to switch between browsers.
632 (defun browse-url-interactive-arg (prompt)
633 "Read a URL from the minibuffer, prompting with PROMPT.
634 If `transient-mark-mode' is non-nil and the mark is active,
635 it defaults to the current region, else to the URL at or before
636 point. If invoked with a mouse button, it moves point to the
637 position clicked before acting.
639 This function returns a list (URL NEW-WINDOW-FLAG)
640 for use in `interactive'."
641 (let ((event (elt (this-command-keys) 0)))
642 (and (listp event) (mouse-set-point event)))
643 (list (read-string prompt (or (and transient-mark-mode mark-active
644 ;; rfc2396 Appendix E.
645 (replace-regexp-in-string
646 "[\t\r\f\n ]+" ""
647 (buffer-substring-no-properties
648 (region-beginning) (region-end))))
649 (browse-url-url-at-point)))
650 (not (eq (null browse-url-new-window-flag)
651 (null current-prefix-arg)))))
653 ;; called-interactive-p needs to be called at a function's top-level, hence
654 ;; this macro. We use that rather than interactive-p because
655 ;; use in a keyboard macro should not change this behavior.
656 (defmacro browse-url-maybe-new-window (arg)
657 `(if (or noninteractive (not (called-interactively-p 'any)))
658 ,arg
659 browse-url-new-window-flag))
661 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
662 ;; Browse current buffer
664 ;;;###autoload
665 (defun browse-url-of-file (&optional file)
666 "Ask a WWW browser to display FILE.
667 Display the current buffer's file if FILE is nil or if called
668 interactively. Turn the filename into a URL with function
669 `browse-url-file-url'. Pass the URL to a browser using the
670 `browse-url' function then run `browse-url-of-file-hook'."
671 (interactive)
672 (or file
673 (setq file (buffer-file-name))
674 (error "Current buffer has no file"))
675 (let ((buf (get-file-buffer file)))
676 (if buf
677 (with-current-buffer buf
678 (cond ((not (buffer-modified-p)))
679 (browse-url-save-file (save-buffer))
680 (t (message "%s modified since last save" file))))))
681 (browse-url (browse-url-file-url file))
682 (run-hooks 'browse-url-of-file-hook))
684 (defun browse-url-file-url (file)
685 "Return the URL corresponding to FILE.
686 Use variable `browse-url-filename-alist' to map filenames to URLs."
687 (let ((coding (if (equal system-type 'windows-nt)
688 ;; W32 pretends that file names are UTF-8 encoded.
689 'utf-8
690 (and (default-value 'enable-multibyte-characters)
691 (or file-name-coding-system
692 default-file-name-coding-system)))))
693 (if coding (setq file (encode-coding-string file coding))))
694 (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
695 (dolist (map browse-url-filename-alist)
696 (when (and map (string-match (car map) file))
697 (setq file (replace-match (cdr map) t nil file))))
698 file)
700 ;;;###autoload
701 (defun browse-url-of-buffer (&optional buffer)
702 "Ask a WWW browser to display BUFFER.
703 Display the current buffer if BUFFER is nil. Display only the
704 currently visible part of BUFFER (from a temporary file) if buffer is
705 narrowed."
706 (interactive)
707 (save-excursion
708 (and buffer (set-buffer buffer))
709 (let ((file-name
710 ;; Ignore real name if restricted
711 (and (not (buffer-narrowed-p))
712 (or buffer-file-name
713 (and (boundp 'dired-directory) dired-directory)))))
714 (or file-name
715 (progn
716 (or browse-url-temp-file-name
717 (setq browse-url-temp-file-name
718 (convert-standard-filename
719 (make-temp-file
720 (expand-file-name "burl" browse-url-temp-dir)
721 nil ".html"))))
722 (setq file-name browse-url-temp-file-name)
723 (write-region (point-min) (point-max) file-name nil 'no-message)))
724 (browse-url-of-file file-name))))
726 (defun browse-url-delete-temp-file (&optional temp-file-name)
727 ;; Delete browse-url-temp-file-name from the file system
728 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
729 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
730 (if (and file-name (file-exists-p file-name))
731 (delete-file file-name))))
733 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
735 (declare-function dired-get-filename "dired"
736 (&optional localp no-error-if-not-filep))
738 ;;;###autoload
739 (defun browse-url-of-dired-file ()
740 "In Dired, ask a WWW browser to display the file named on this line."
741 (interactive)
742 (let ((tem (dired-get-filename t t)))
743 (if tem
744 (browse-url-of-file (expand-file-name tem))
745 (error "No file on this line"))))
747 ;;;###autoload
748 (defun browse-url-of-region (min max)
749 "Ask a WWW browser to display the current region."
750 (interactive "r")
751 (save-excursion
752 (save-restriction
753 (narrow-to-region min max)
754 (browse-url-of-buffer))))
756 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
757 ;; Browser-independent commands
759 ;; A generic command to call the current browse-url-browser-function
761 ;;;###autoload
762 (defun browse-url (url &rest args)
763 "Ask a WWW browser to load URL.
764 Prompt for a URL, defaulting to the URL at or before point.
765 The variable `browse-url-browser-function' says which browser to use.
766 If the URL is a mailto: URL, consult `browse-url-mailto-function'
767 first, if that exists.
769 Passes any ARGS to the browser function.
770 The default is to pass `browse-url-new-window-flag'."
771 (interactive (browse-url-interactive-arg "URL: "))
772 (unless (called-interactively-p 'interactive)
773 (setq args (or args (list browse-url-new-window-flag))))
774 (when (and url-handler-mode (not (file-name-absolute-p url)))
775 (setq url (expand-file-name url)))
776 (let ((process-environment (copy-sequence process-environment))
777 (function (or (and (string-match "\\`mailto:" url)
778 browse-url-mailto-function)
779 browse-url-browser-function))
780 ;; Ensure that `default-directory' exists and is readable (b#6077).
781 (default-directory (or (unhandled-file-name-directory default-directory)
782 (expand-file-name "~/"))))
783 ;; When connected to various displays, be careful to use the display of
784 ;; the currently selected frame, rather than the original start display,
785 ;; which may not even exist any more.
786 (if (stringp (frame-parameter nil 'display))
787 (setenv "DISPLAY" (frame-parameter nil 'display)))
788 (if (and (consp function)
789 (not (functionp function)))
790 ;; The `function' can be an alist; look down it for first match
791 ;; and apply the function (which might be a lambda).
792 (catch 'done
793 (dolist (bf function)
794 (when (string-match (car bf) url)
795 (apply (cdr bf) url args)
796 (throw 'done t)))
797 (error "No browse-url-browser-function matching URL %s"
798 url))
799 ;; Unbound symbols go down this leg, since void-function from
800 ;; apply is clearer than wrong-type-argument from dolist.
801 (apply function url args))))
803 ;;;###autoload
804 (defun browse-url-at-point (&optional arg)
805 "Ask a WWW browser to load the URL at or before point.
806 Variable `browse-url-browser-function' says which browser to use.
807 Optional prefix argument ARG non-nil inverts the value of the option
808 `browse-url-new-window-flag'."
809 (interactive "P")
810 (let ((url (browse-url-url-at-point)))
811 (if url
812 (browse-url url (if arg
813 (not browse-url-new-window-flag)
814 browse-url-new-window-flag))
815 (error "No URL found"))))
817 ;;;###autoload
818 (defun browse-url-at-mouse (event)
819 "Ask a WWW browser to load a URL clicked with the mouse.
820 The URL is the one around or before the position of the mouse click
821 but point is not changed. Variable `browse-url-browser-function'
822 says which browser to use."
823 (interactive "e")
824 (save-excursion
825 (mouse-set-point event)
826 ;; This handles browse-url-new-window-flag properly
827 ;; when it gets no arg.
828 (browse-url-at-point)))
830 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
831 ;; Browser-specific commands
833 ;; --- Default MS-Windows browser ---
835 (defvar dos-windows-version)
836 (declare-function w32-shell-execute "w32fns.c") ;; Defined in C.
838 (defun browse-url-default-windows-browser (url &optional _new-window)
839 (interactive (browse-url-interactive-arg "URL: "))
840 (cond ((eq system-type 'ms-dos)
841 (if dos-windows-version
842 (shell-command (concat "start " (shell-quote-argument url)))
843 (error "Browsing URLs is not supported on this system")))
844 ((eq system-type 'cygwin)
845 (call-process "cygstart" nil nil nil url))
846 (t (w32-shell-execute "open" url))))
848 (defun browse-url-default-macosx-browser (url &optional _new-window)
849 (interactive (browse-url-interactive-arg "URL: "))
850 (start-process (concat "open " url) nil "open" url))
852 ;; --- Netscape ---
854 (defun browse-url-process-environment ()
855 "Set DISPLAY in the environment to the X display the browser will use.
856 This is either the value of variable `browse-url-browser-display' if
857 non-nil, or the same display as Emacs if different from the current
858 environment, otherwise just use the current environment."
859 (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
860 (if display
861 (cons (concat "DISPLAY=" display) process-environment)
862 process-environment)))
864 (defun browse-url-emacs-display ()
865 "Return the X display Emacs is running on.
866 This is nil if the display is the same as the DISPLAY environment variable.
868 Actually Emacs could be using several displays; this just returns the
869 one showing the selected frame."
870 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
871 (and (not (equal display (getenv "DISPLAY")))
872 display)))
874 (defun browse-url-default-browser (url &rest args)
875 "Find a suitable browser and ask it to load URL.
876 Default to the URL around or before point.
878 When called interactively, if variable `browse-url-new-window-flag' is
879 non-nil, load the document in a new window, if possible, otherwise use
880 a random existing one. A non-nil interactive prefix argument reverses
881 the effect of `browse-url-new-window-flag'.
883 When called non-interactively, optional second argument NEW-WINDOW is
884 used instead of `browse-url-new-window-flag'."
885 (apply
886 (cond
887 ((memq system-type '(windows-nt ms-dos cygwin))
888 'browse-url-default-windows-browser)
889 ((memq system-type '(darwin))
890 'browse-url-default-macosx-browser)
891 ((browse-url-can-use-xdg-open) 'browse-url-xdg-open)
892 ;;; ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
893 ((executable-find browse-url-mozilla-program) 'browse-url-mozilla)
894 ((executable-find browse-url-firefox-program) 'browse-url-firefox)
895 ((executable-find browse-url-chromium-program) 'browse-url-chromium)
896 ;;; ((executable-find browse-url-galeon-program) 'browse-url-galeon)
897 ((executable-find browse-url-kde-program) 'browse-url-kde)
898 ;;; ((executable-find browse-url-netscape-program) 'browse-url-netscape)
899 ;;; ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
900 ((executable-find browse-url-conkeror-program) 'browse-url-conkeror)
901 ((executable-find browse-url-xterm-program) 'browse-url-text-xterm)
902 ((locate-library "w3") 'browse-url-w3)
904 (lambda (&rest _ignore) (error "No usable browser found"))))
905 url args))
907 (defun browse-url-can-use-xdg-open ()
908 "Return non-nil if the \"xdg-open\" program can be used.
909 xdg-open is a desktop utility that calls your preferred web browser.
910 This requires you to be running either Gnome, KDE, Xfce4 or LXDE."
911 (and (getenv "DISPLAY")
912 (executable-find "xdg-open")
913 ;; xdg-open may call gnome-open and that does not wait for its child
914 ;; to finish. This child may then be killed when the parent dies.
915 ;; Use nohup to work around. See bug#7166, bug#8917, bug#9779 and
916 ;; http://lists.gnu.org/archive/html/emacs-devel/2009-07/msg00279.html
917 (executable-find "nohup")
918 (or (getenv "GNOME_DESKTOP_SESSION_ID")
919 ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
920 (condition-case nil
921 (eq 0 (call-process
922 "dbus-send" nil nil nil
923 "--dest=org.gnome.SessionManager"
924 "--print-reply"
925 "/org/gnome/SessionManager"
926 "org.gnome.SessionManager.CanShutdown"))
927 (error nil))
928 (equal (getenv "KDE_FULL_SESSION") "true")
929 (condition-case nil
930 (eq 0 (call-process
931 "/bin/sh" nil nil nil
932 "-c"
933 ;; FIXME use string-match rather than grep.
934 "xprop -root _DT_SAVE_MODE|grep xfce4"))
935 (error nil))
936 (member (getenv "DESKTOP_SESSION") '("LXDE" "Lubuntu"))
937 (equal (getenv "XDG_CURRENT_DESKTOP") "LXDE"))))
940 ;;;###autoload
941 (defun browse-url-xdg-open (url &optional ignored)
942 "Pass the specified URL to the \"xdg-open\" command.
943 xdg-open is a desktop utility that calls your preferred web browser.
944 The optional argument IGNORED is not used."
945 (interactive (browse-url-interactive-arg "URL: "))
946 (call-process "xdg-open" nil 0 nil url))
948 ;;;###autoload
949 (defun browse-url-netscape (url &optional new-window)
950 "Ask the Netscape WWW browser to load URL.
951 Default to the URL around or before point. The strings in variable
952 `browse-url-netscape-arguments' are also passed to Netscape.
954 When called interactively, if variable `browse-url-new-window-flag' is
955 non-nil, load the document in a new Netscape window, otherwise use a
956 random existing one. A non-nil interactive prefix argument reverses
957 the effect of `browse-url-new-window-flag'.
959 If `browse-url-netscape-new-window-is-tab' is non-nil, then
960 whenever a document would otherwise be loaded in a new window, it
961 is loaded in a new tab in an existing window instead.
963 When called non-interactively, optional second argument NEW-WINDOW is
964 used instead of `browse-url-new-window-flag'."
965 (declare (obsolete nil "25.1"))
966 (interactive (browse-url-interactive-arg "URL: "))
967 (setq url (browse-url-encode-url url))
968 (let* ((process-environment (browse-url-process-environment))
969 (process
970 (apply 'start-process
971 (concat "netscape " url) nil
972 browse-url-netscape-program
973 (append
974 browse-url-netscape-arguments
975 (if (eq window-system 'w32)
976 (list url)
977 (append
978 (if new-window '("-noraise"))
979 (list "-remote"
980 (concat "openURL(" url
981 (if (browse-url-maybe-new-window
982 new-window)
983 (if browse-url-netscape-new-window-is-tab
984 ",new-tab"
985 ",new-window"))
986 ")"))))))))
987 (set-process-sentinel process
988 `(lambda (process change)
989 (browse-url-netscape-sentinel process ,url)))))
991 (defun browse-url-netscape-sentinel (process url)
992 "Handle a change to the process communicating with Netscape."
993 (declare (obsolete nil "25.1"))
994 (or (eq (process-exit-status process) 0)
995 (let* ((process-environment (browse-url-process-environment)))
996 ;; Netscape not running - start it
997 (message "Starting %s..." browse-url-netscape-program)
998 (apply 'start-process (concat "netscape" url) nil
999 browse-url-netscape-program
1000 (append browse-url-netscape-startup-arguments (list url))))))
1002 (defun browse-url-netscape-reload ()
1003 "Ask Netscape to reload its current document.
1004 How depends on `browse-url-netscape-version'."
1005 (declare (obsolete nil "25.1"))
1006 (interactive)
1007 ;; Backwards incompatibility reported by
1008 ;; <peter.kruse@psychologie.uni-regensburg.de>.
1009 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
1010 "xfeDoCommand(reload)"
1011 "reload")))
1013 (defun browse-url-netscape-send (command)
1014 "Send a remote control command to Netscape."
1015 (declare (obsolete nil "25.1"))
1016 (let* ((process-environment (browse-url-process-environment)))
1017 (apply 'start-process "netscape" nil
1018 browse-url-netscape-program
1019 (append browse-url-netscape-arguments
1020 (list "-remote" command)))))
1022 ;;;###autoload
1023 (defun browse-url-mozilla (url &optional new-window)
1024 "Ask the Mozilla WWW browser to load URL.
1025 Default to the URL around or before point. The strings in variable
1026 `browse-url-mozilla-arguments' are also passed to Mozilla.
1028 When called interactively, if variable `browse-url-new-window-flag' is
1029 non-nil, load the document in a new Mozilla window, otherwise use a
1030 random existing one. A non-nil interactive prefix argument reverses
1031 the effect of `browse-url-new-window-flag'.
1033 If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
1034 document would otherwise be loaded in a new window, it is loaded in a
1035 new tab in an existing window instead.
1037 When called non-interactively, optional second argument NEW-WINDOW is
1038 used instead of `browse-url-new-window-flag'."
1039 (interactive (browse-url-interactive-arg "URL: "))
1040 (setq url (browse-url-encode-url url))
1041 (let* ((process-environment (browse-url-process-environment))
1042 (process
1043 (apply 'start-process
1044 (concat "mozilla " url) nil
1045 browse-url-mozilla-program
1046 (append
1047 browse-url-mozilla-arguments
1048 (list "-remote"
1049 (concat "openURL("
1051 (if (browse-url-maybe-new-window
1052 new-window)
1053 (if browse-url-mozilla-new-window-is-tab
1054 ",new-tab"
1055 ",new-window"))
1056 ")"))))))
1057 (set-process-sentinel process
1058 `(lambda (process change)
1059 (browse-url-mozilla-sentinel process ,url)))))
1061 (defun browse-url-mozilla-sentinel (process url)
1062 "Handle a change to the process communicating with Mozilla."
1063 (or (eq (process-exit-status process) 0)
1064 (let* ((process-environment (browse-url-process-environment)))
1065 ;; Mozilla is not running - start it
1066 (message "Starting %s..." browse-url-mozilla-program)
1067 (apply 'start-process (concat "mozilla " url) nil
1068 browse-url-mozilla-program
1069 (append browse-url-mozilla-startup-arguments (list url))))))
1071 ;;;###autoload
1072 (defun browse-url-firefox (url &optional new-window)
1073 "Ask the Firefox WWW browser to load URL.
1074 Defaults to the URL around or before point. Passes the strings
1075 in the variable `browse-url-firefox-arguments' to Firefox.
1077 Interactively, if the variable `browse-url-new-window-flag' is non-nil,
1078 loads the document in a new Firefox window. A non-nil prefix argument
1079 reverses the effect of `browse-url-new-window-flag'.
1081 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1082 whenever a document would otherwise be loaded in a new window, it
1083 is loaded in a new tab in an existing window instead.
1085 Non-interactively, this uses the optional second argument NEW-WINDOW
1086 instead of `browse-url-new-window-flag'."
1087 (interactive (browse-url-interactive-arg "URL: "))
1088 (setq url (browse-url-encode-url url))
1089 (let* ((process-environment (browse-url-process-environment)))
1090 (apply 'start-process
1091 (concat "firefox " url) nil
1092 browse-url-firefox-program
1093 (append
1094 browse-url-firefox-arguments
1095 (if (browse-url-maybe-new-window new-window)
1096 (if browse-url-firefox-new-window-is-tab
1097 '("-new-tab")
1098 '("-new-window")))
1099 (list url)))))
1101 ;;;###autoload
1102 (defun browse-url-chromium (url &optional _new-window)
1103 "Ask the Chromium WWW browser to load URL.
1104 Default to the URL around or before point. The strings in
1105 variable `browse-url-chromium-arguments' are also passed to
1106 Chromium."
1107 (interactive (browse-url-interactive-arg "URL: "))
1108 (setq url (browse-url-encode-url url))
1109 (let* ((process-environment (browse-url-process-environment)))
1110 (apply 'start-process
1111 (concat "chromium " url) nil
1112 browse-url-chromium-program
1113 (append
1114 browse-url-chromium-arguments
1115 (list url)))))
1117 ;;;###autoload
1118 (defun browse-url-galeon (url &optional new-window)
1119 "Ask the Galeon WWW browser to load URL.
1120 Default to the URL around or before point. The strings in variable
1121 `browse-url-galeon-arguments' are also passed to Galeon.
1123 When called interactively, if variable `browse-url-new-window-flag' is
1124 non-nil, load the document in a new Galeon window, otherwise use a
1125 random existing one. A non-nil interactive prefix argument reverses
1126 the effect of `browse-url-new-window-flag'.
1128 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1129 document would otherwise be loaded in a new window, it is loaded in a
1130 new tab in an existing window instead.
1132 When called non-interactively, optional second argument NEW-WINDOW is
1133 used instead of `browse-url-new-window-flag'."
1134 (declare (obsolete nil "25.1"))
1135 (interactive (browse-url-interactive-arg "URL: "))
1136 (setq url (browse-url-encode-url url))
1137 (let* ((process-environment (browse-url-process-environment))
1138 (process (apply 'start-process
1139 (concat "galeon " url)
1141 browse-url-galeon-program
1142 (append
1143 browse-url-galeon-arguments
1144 (if (browse-url-maybe-new-window new-window)
1145 (if browse-url-galeon-new-window-is-tab
1146 '("--new-tab")
1147 '("--new-window" "--noraise"))
1148 '("--existing"))
1149 (list url)))))
1150 (set-process-sentinel process
1151 `(lambda (process change)
1152 (browse-url-galeon-sentinel process ,url)))))
1154 (defun browse-url-galeon-sentinel (process url)
1155 "Handle a change to the process communicating with Galeon."
1156 (declare (obsolete nil "25.1"))
1157 (or (eq (process-exit-status process) 0)
1158 (let* ((process-environment (browse-url-process-environment)))
1159 ;; Galeon is not running - start it
1160 (message "Starting %s..." browse-url-galeon-program)
1161 (apply 'start-process (concat "galeon " url) nil
1162 browse-url-galeon-program
1163 (append browse-url-galeon-startup-arguments (list url))))))
1165 (defun browse-url-epiphany (url &optional new-window)
1166 "Ask the Epiphany WWW browser to load URL.
1167 Default to the URL around or before point. The strings in variable
1168 `browse-url-galeon-arguments' are also passed to Epiphany.
1170 When called interactively, if variable `browse-url-new-window-flag' is
1171 non-nil, load the document in a new Epiphany window, otherwise use a
1172 random existing one. A non-nil interactive prefix argument reverses
1173 the effect of `browse-url-new-window-flag'.
1175 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1176 document would otherwise be loaded in a new window, it is loaded in a
1177 new tab in an existing window instead.
1179 When called non-interactively, optional second argument NEW-WINDOW is
1180 used instead of `browse-url-new-window-flag'."
1181 (interactive (browse-url-interactive-arg "URL: "))
1182 (setq url (browse-url-encode-url url))
1183 (let* ((process-environment (browse-url-process-environment))
1184 (process (apply 'start-process
1185 (concat "epiphany " url)
1187 browse-url-epiphany-program
1188 (append
1189 browse-url-epiphany-arguments
1190 (if (browse-url-maybe-new-window new-window)
1191 (if browse-url-epiphany-new-window-is-tab
1192 '("--new-tab")
1193 '("--new-window" "--noraise"))
1194 '("--existing"))
1195 (list url)))))
1196 (set-process-sentinel process
1197 `(lambda (process change)
1198 (browse-url-epiphany-sentinel process ,url)))))
1200 (defun browse-url-epiphany-sentinel (process url)
1201 "Handle a change to the process communicating with Epiphany."
1202 (or (eq (process-exit-status process) 0)
1203 (let* ((process-environment (browse-url-process-environment)))
1204 ;; Epiphany is not running - start it
1205 (message "Starting %s..." browse-url-epiphany-program)
1206 (apply 'start-process (concat "epiphany " url) nil
1207 browse-url-epiphany-program
1208 (append browse-url-epiphany-startup-arguments (list url))))))
1210 (defvar url-handler-regexp)
1212 ;;;###autoload
1213 (defun browse-url-emacs (url &optional _new-window)
1214 "Ask Emacs to load URL into a buffer and show it in another window."
1215 (interactive (browse-url-interactive-arg "URL: "))
1216 (require 'url-handlers)
1217 (let ((file-name-handler-alist
1218 (cons (cons url-handler-regexp 'url-file-handler)
1219 file-name-handler-alist)))
1220 ;; Ignore `new-window': with all other browsers the URL is always shown
1221 ;; in another window than the current Emacs one since it's shown in
1222 ;; another application's window.
1223 ;; (if new-window (find-file-other-window url) (find-file url))
1224 (find-file-other-window url)))
1226 ;;;###autoload
1227 (defun browse-url-gnome-moz (url &optional new-window)
1228 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1229 Default to the URL around or before point. The strings in variable
1230 `browse-url-gnome-moz-arguments' are also passed.
1232 When called interactively, if variable `browse-url-new-window-flag' is
1233 non-nil, load the document in a new browser window, otherwise use an
1234 existing one. A non-nil interactive prefix argument reverses the
1235 effect of `browse-url-new-window-flag'.
1237 When called non-interactively, optional second argument NEW-WINDOW is
1238 used instead of `browse-url-new-window-flag'."
1239 (declare (obsolete nil "25.1"))
1240 (interactive (browse-url-interactive-arg "URL: "))
1241 (apply 'start-process (concat "gnome-moz-remote " url)
1243 browse-url-gnome-moz-program
1244 (append
1245 browse-url-gnome-moz-arguments
1246 (if (browse-url-maybe-new-window new-window)
1247 '("--newwin"))
1248 (list "--raise" url))))
1250 ;; --- Mosaic ---
1252 ;;;###autoload
1253 (defun browse-url-mosaic (url &optional new-window)
1254 "Ask the XMosaic WWW browser to load URL.
1256 Default to the URL around or before point. The strings in variable
1257 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1258 program is invoked according to the variable
1259 `browse-url-mosaic-program'.
1261 When called interactively, if variable `browse-url-new-window-flag' is
1262 non-nil, load the document in a new Mosaic window, otherwise use a
1263 random existing one. A non-nil interactive prefix argument reverses
1264 the effect of `browse-url-new-window-flag'.
1266 When called non-interactively, optional second argument NEW-WINDOW is
1267 used instead of `browse-url-new-window-flag'."
1268 (declare (obsolete nil "25.1"))
1269 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1270 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
1271 pid)
1272 (if (file-readable-p pidfile)
1273 (with-temp-buffer
1274 (insert-file-contents pidfile)
1275 (setq pid (read (current-buffer)))))
1276 (if (and (integerp pid) (zerop (signal-process pid 0))) ; Mosaic running
1277 (progn
1278 (with-temp-buffer
1279 (insert (if (browse-url-maybe-new-window new-window)
1280 "newwin\n"
1281 "goto\n")
1282 url "\n")
1283 (with-file-modes ?\700
1284 (if (file-exists-p
1285 (setq pidfile (format "/tmp/Mosaic.%d" pid)))
1286 (delete-file pidfile))
1287 ;; http://debbugs.gnu.org/17428. Use O_EXCL.
1288 (write-region nil nil pidfile nil 'silent nil 'excl)))
1289 ;; Send signal SIGUSR to Mosaic
1290 (message "Signaling Mosaic...")
1291 (signal-process pid 'SIGUSR1)
1292 ;; Or you could try:
1293 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1294 (message "Signaling Mosaic...done"))
1295 ;; Mosaic not running - start it
1296 (message "Starting %s..." browse-url-mosaic-program)
1297 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1298 (append browse-url-mosaic-arguments (list url)))
1299 (message "Starting %s...done" browse-url-mosaic-program))))
1301 ;; --- Mosaic using CCI ---
1303 ;;;###autoload
1304 (defun browse-url-cci (url &optional new-window)
1305 "Ask the XMosaic WWW browser to load URL.
1306 Default to the URL around or before point.
1308 This function only works for XMosaic version 2.5 or later. You must
1309 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1310 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1312 When called interactively, if variable `browse-url-new-window-flag' is
1313 non-nil, load the document in a new browser window, otherwise use a
1314 random existing one. A non-nil interactive prefix argument reverses
1315 the effect of `browse-url-new-window-flag'.
1317 When called non-interactively, optional second argument NEW-WINDOW is
1318 used instead of `browse-url-new-window-flag'."
1319 (declare (obsolete nil "25.1"))
1320 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1321 (open-network-stream "browse-url" " *browse-url*"
1322 browse-url-CCI-host browse-url-CCI-port)
1323 ;; Todo: start browser if fails
1324 (process-send-string "browse-url"
1325 (concat "get url (" url ") output "
1326 (if (browse-url-maybe-new-window new-window)
1327 "new"
1328 "current")
1329 "\r\n"))
1330 (process-send-string "browse-url" "disconnect\r\n")
1331 (delete-process "browse-url"))
1333 ;; --- Conkeror ---
1334 ;;;###autoload
1335 (defun browse-url-conkeror (url &optional new-window)
1336 "Ask the Conkeror WWW browser to load URL.
1337 Default to the URL around or before point. Also pass the strings
1338 in the variable `browse-url-conkeror-arguments' to Conkeror.
1340 When called interactively, if variable
1341 `browse-url-new-window-flag' is non-nil, load the document in a
1342 new Conkeror window, otherwise use a random existing one. A
1343 non-nil interactive prefix argument reverses the effect of
1344 `browse-url-new-window-flag'.
1346 If variable `browse-url-conkeror-new-window-is-buffer' is
1347 non-nil, then whenever a document would otherwise be loaded in a
1348 new window, load it in a new buffer in an existing window instead.
1350 When called non-interactively, use optional second argument
1351 NEW-WINDOW instead of `browse-url-new-window-flag'."
1352 (interactive (browse-url-interactive-arg "URL: "))
1353 (setq url (browse-url-encode-url url))
1354 (let* ((process-environment (browse-url-process-environment)))
1355 (apply 'start-process (format "conkeror %s" url)
1357 browse-url-conkeror-program
1358 (append
1359 browse-url-conkeror-arguments
1360 (list
1361 "-e"
1362 (format "load_url_in_new_%s('%s')"
1363 (if (browse-url-maybe-new-window new-window)
1364 (if browse-url-conkeror-new-window-is-buffer
1365 "buffer"
1366 "window")
1367 "buffer")
1368 url))))))
1369 ;; --- W3 ---
1371 ;; External.
1372 (declare-function w3-fetch-other-window "ext:w3m" (&optional url))
1373 (declare-function w3-fetch "ext:w3m" (&optional url target))
1375 ;;;###autoload
1376 (defun browse-url-w3 (url &optional new-window)
1377 "Ask the w3 WWW browser to load URL.
1378 Default to the URL around or before point.
1380 When called interactively, if variable `browse-url-new-window-flag' is
1381 non-nil, load the document in a new window. A non-nil interactive
1382 prefix argument reverses the effect of `browse-url-new-window-flag'.
1384 When called non-interactively, optional second argument NEW-WINDOW is
1385 used instead of `browse-url-new-window-flag'."
1386 (interactive (browse-url-interactive-arg "W3 URL: "))
1387 (require 'w3) ; w3-fetch-other-window not autoloaded
1388 (if (browse-url-maybe-new-window new-window)
1389 (w3-fetch-other-window url)
1390 (w3-fetch url)))
1392 ;;;###autoload
1393 (defun browse-url-w3-gnudoit (url &optional _new-window)
1394 ;; new-window ignored
1395 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1396 The `browse-url-gnudoit-program' program is used with options given by
1397 `browse-url-gnudoit-args'. Default to the URL around or before point."
1398 (declare (obsolete nil "25.1"))
1399 (interactive (browse-url-interactive-arg "W3 URL: "))
1400 (apply 'start-process (concat "gnudoit:" url) nil
1401 browse-url-gnudoit-program
1402 (append browse-url-gnudoit-args
1403 (list (concat "(w3-fetch \"" url "\")")
1404 "(raise-frame)"))))
1406 ;; --- Lynx in an xterm ---
1408 ;;;###autoload
1409 (defun browse-url-text-xterm (url &optional _new-window)
1410 ;; new-window ignored
1411 "Ask a text browser to load URL.
1412 URL defaults to the URL around or before point.
1413 This runs the text browser specified by `browse-url-text-browser'.
1414 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1415 with possible additional arguments `browse-url-xterm-args'."
1416 (interactive (browse-url-interactive-arg "Text browser URL: "))
1417 (apply #'start-process `(,(concat browse-url-text-browser url)
1418 nil ,browse-url-xterm-program
1419 ,@browse-url-xterm-args "-e" ,browse-url-text-browser
1420 ,url)))
1422 ;; --- Lynx in an Emacs "term" window ---
1424 (declare-function term-char-mode "term" ())
1425 (declare-function term-send-down "term" ())
1426 (declare-function term-send-string "term" (proc str))
1428 ;;;###autoload
1429 (defun browse-url-text-emacs (url &optional new-buffer)
1430 "Ask a text browser to load URL.
1431 URL defaults to the URL around or before point.
1432 This runs the text browser specified by `browse-url-text-browser'.
1433 With a prefix argument, it runs a new browser process in a new buffer.
1435 When called interactively, if variable `browse-url-new-window-flag' is
1436 non-nil, load the document in a new browser process in a new term window,
1437 otherwise use any existing one. A non-nil interactive prefix argument
1438 reverses the effect of `browse-url-new-window-flag'.
1440 When called non-interactively, optional second argument NEW-WINDOW is
1441 used instead of `browse-url-new-window-flag'."
1442 (interactive (browse-url-interactive-arg "Text browser URL: "))
1443 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
1444 ;; (term-term-name "vt100") ; ??
1445 (buf (get-buffer "*text browser*"))
1446 (proc (and buf (get-buffer-process buf)))
1447 (n browse-url-text-input-attempts))
1448 (require 'term)
1449 (if (and (browse-url-maybe-new-window new-buffer) buf)
1450 ;; Rename away the OLD buffer. This isn't very polite, but
1451 ;; term insists on working in a buffer named *lynx* and would
1452 ;; choke on *lynx*<1>
1453 (progn (set-buffer buf)
1454 (rename-uniquely)))
1455 (if (or (browse-url-maybe-new-window new-buffer)
1456 (not buf)
1457 (not proc)
1458 (not (memq (process-status proc) '(run stop))))
1459 ;; start a new text browser
1460 (progn
1461 (setq buf
1462 (apply #'make-term
1463 `(,browse-url-text-browser
1464 ,browse-url-text-browser
1465 nil ,@browse-url-text-emacs-args
1466 ,url)))
1467 (switch-to-buffer buf)
1468 (term-char-mode)
1469 (set-process-sentinel
1470 (get-buffer-process buf)
1471 ;; Don't leave around a dead one (especially because of its
1472 ;; munged keymap.)
1473 (lambda (process _event)
1474 (if (not (memq (process-status process) '(run stop)))
1475 (let ((buf (process-buffer process)))
1476 (if buf (kill-buffer buf)))))))
1477 ;; Send the url to the text browser in the old buffer
1478 (let ((win (get-buffer-window buf t)))
1479 (if win
1480 (select-window win)
1481 (switch-to-buffer buf)))
1482 (if (eq (following-char) ?_)
1483 (cond ((eq browse-url-text-input-field 'warn)
1484 (error "Please move out of the input field first"))
1485 ((eq browse-url-text-input-field 'avoid)
1486 (while (and (eq (following-char) ?_) (> n 0))
1487 (term-send-down) ; down arrow
1488 (sit-for browse-url-text-input-delay))
1489 (if (eq (following-char) ?_)
1490 (error "Cannot move out of the input field, sorry")))))
1491 (term-send-string proc (concat "g" ; goto
1492 "\C-u" ; kill default url
1494 "\r")))))
1496 ;; --- mailto ---
1498 (autoload 'rfc2368-parse-mailto-url "rfc2368")
1500 ;;;###autoload
1501 (defun browse-url-mail (url &optional new-window)
1502 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1503 Default to using the mailto: URL around or before point as the
1504 recipient's address. Supplying a non-nil interactive prefix argument
1505 will cause the mail to be composed in another window rather than the
1506 current one.
1508 When called interactively, if variable `browse-url-new-window-flag' is
1509 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1510 non-nil interactive prefix argument reverses the effect of
1511 `browse-url-new-window-flag'.
1513 When called non-interactively, optional second argument NEW-WINDOW is
1514 used instead of `browse-url-new-window-flag'."
1515 (interactive (browse-url-interactive-arg "Mailto URL: "))
1516 (save-excursion
1517 (let* ((alist (rfc2368-parse-mailto-url url))
1518 (to (assoc "To" alist))
1519 (subject (assoc "Subject" alist))
1520 (body (assoc "Body" alist))
1521 (rest (delq to (delq subject (delq body alist))))
1522 (to (cdr to))
1523 (subject (cdr subject))
1524 (body (cdr body))
1525 (mail-citation-hook (unless body mail-citation-hook)))
1526 (if (browse-url-maybe-new-window new-window)
1527 (compose-mail-other-window to subject rest nil
1528 (list 'insert-buffer (current-buffer)))
1529 (compose-mail to subject rest nil nil
1530 (list 'insert-buffer (current-buffer))))
1531 (when body
1532 (goto-char (point-min))
1533 (unless (or (search-forward (concat "\n" mail-header-separator "\n")
1534 nil 'move)
1535 (bolp))
1536 (insert "\n"))
1537 (goto-char (prog1
1538 (point)
1539 (insert (replace-regexp-in-string "\r\n" "\n" body))
1540 (unless (bolp)
1541 (insert "\n"))))))))
1543 ;; --- Random browser ---
1545 ;;;###autoload
1546 (defun browse-url-generic (url &optional _new-window)
1547 ;; new-window ignored
1548 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1549 Default to the URL around or before point. A fresh copy of the
1550 browser is started up in a new process with possible additional arguments
1551 `browse-url-generic-args'. This is appropriate for browsers which
1552 don't offer a form of remote control."
1553 (interactive (browse-url-interactive-arg "URL: "))
1554 (if (not browse-url-generic-program)
1555 (error "No browser defined (`browse-url-generic-program')"))
1556 (apply 'call-process browse-url-generic-program nil
1557 0 nil
1558 (append browse-url-generic-args (list url))))
1560 ;;;###autoload
1561 (defun browse-url-kde (url &optional _new-window)
1562 "Ask the KDE WWW browser to load URL.
1563 Default to the URL around or before point."
1564 (interactive (browse-url-interactive-arg "KDE URL: "))
1565 (message "Sending URL to KDE...")
1566 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
1567 (append browse-url-kde-args (list url))))
1569 (defun browse-url-elinks-new-window (url)
1570 "Ask the Elinks WWW browser to load URL in a new window."
1571 (let ((process-environment (browse-url-process-environment)))
1572 (apply #'start-process
1573 (append (list (concat "elinks:" url)
1574 nil)
1575 browse-url-elinks-wrapper
1576 (list "elinks" url)))))
1578 ;;;###autoload
1579 (defun browse-url-elinks (url &optional new-window)
1580 "Ask the Elinks WWW browser to load URL.
1581 Default to the URL around the point.
1583 The document is loaded in a new tab of a running Elinks or, if
1584 none yet running, a newly started instance.
1586 The Elinks command will be prepended by the program+arguments
1587 from `browse-url-elinks-wrapper'."
1588 (interactive (browse-url-interactive-arg "URL: "))
1589 (setq url (browse-url-encode-url url))
1590 (if new-window
1591 (browse-url-elinks-new-window url)
1592 (let ((process-environment (browse-url-process-environment))
1593 (elinks-ping-process (start-process "elinks-ping" nil
1594 "elinks" "-remote" "ping()")))
1595 (set-process-sentinel elinks-ping-process
1596 `(lambda (process change)
1597 (browse-url-elinks-sentinel process ,url))))))
1599 (defun browse-url-elinks-sentinel (process url)
1600 "Determines if Elinks is running or a new one has to be started."
1601 ;; Try to determine if an instance is running or if we have to
1602 ;; create a new one.
1603 (pcase (process-exit-status process)
1605 ;; No instance, start a new one.
1606 (browse-url-elinks-new-window url))
1608 ;; Found an instance, open URL in new tab.
1609 (let ((process-environment (browse-url-process-environment)))
1610 (start-process (concat "elinks:" url) nil
1611 "elinks" "-remote"
1612 (concat "openURL(\"" url "\",new-tab)"))))
1613 (exit-status
1614 (error "Unrecognized exit-code %d of process `elinks'"
1615 exit-status))))
1617 (provide 'browse-url)
1619 ;;; browse-url.el ends here