browse-url.el 60 KB
Newer Older
1
;;; browse-url.el --- pass a URL to a WWW browser
Gerd Moellmann's avatar
Gerd Moellmann committed
2

3
;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
Gerd Moellmann's avatar
Gerd Moellmann committed
5 6

;; Author: Denis Howe <dbh@doc.ic.ac.uk>
7
;; Maintainer: FSF
Gerd Moellmann's avatar
Gerd Moellmann committed
8 9 10 11 12
;; Created: 03 Apr 1995
;; Keywords: hypertext, hypermedia, mouse

;; This file is part of GNU Emacs.

13
;; GNU Emacs is free software: you can redistribute it and/or modify
Gerd Moellmann's avatar
Gerd Moellmann committed
14
;; it under the terms of the GNU General Public License as published by
15 16
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Gerd Moellmann's avatar
Gerd Moellmann committed
17 18 19 20 21 22 23

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
24
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Gerd Moellmann's avatar
Gerd Moellmann committed
25 26 27 28 29 30 31 32 33

;;; Commentary:

;; This package provides functions which read a URL (Uniform Resource
;; Locator) from the minibuffer, defaulting to the URL around point,
;; and ask a World-Wide Web browser to load it.  It can also load the
;; URL associated with the current buffer.  Different browsers use
;; different methods of remote control so there is one function for
;; each supported browser.  If the chosen browser is not running, it
34 35 36 37
;; is started.  Currently there is support for the following browsers,
;; some of them probably now obsolete:

;; Function                           Browser     Earliest version
38
;; browse-url-mozilla                 Mozilla     Don't know
Eli Zaretskii's avatar
Eli Zaretskii committed
39
;; browse-url-firefox                 Firefox     Don't know (tried with 1.0.1)
40
;; browse-url-galeon                  Galeon      Don't know
41
;; browse-url-epiphany                Epiphany    Don't know
42 43 44 45 46
;; browse-url-netscape                Netscape    1.1b1
;; browse-url-mosaic                  XMosaic/mMosaic <= 2.4
;; browse-url-cci                     XMosaic     2.5
;; browse-url-w3                      w3          0
;; browse-url-w3-gnudoit              w3 remotely
47
;; browse-url-text-*	              Any text browser     0
48 49
;; browse-url-generic                 arbitrary
;; browse-url-default-windows-browser MS-Windows browser
50
;; browse-url-default-macosx-browser  Mac OS X browser
51
;; browse-url-gnome-moz               GNOME interface to Mozilla
52
;; browse-url-kde                     KDE konqueror (kfm)
53
;; browse-url-elinks                  Elinks      Don't know (tried with 0.12.GIT)
Gerd Moellmann's avatar
Gerd Moellmann committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

;; [A version of the Netscape browser is now free software
;; <URL:http://www.mozilla.org/>, albeit not GPLed, so it is
;; reasonable to have that as the default.]

;; Note that versions of Netscape before 1.1b1 did not have remote
;; control.  <URL:http://www.netscape.com/newsref/std/x-remote.html>.

;; Browsers can cache Web pages so it may be necessary to tell them to
;; reload the current page if it has changed (e.g. if you have edited
;; it).  There is currently no perfect automatic solution to this.

;; Netscape allows you to specify the id of the window you want to
;; control but which window DO you want to control and how do you
;; discover its id?

70
;; William M.  Perry's excellent "w3" WWW browser for
Gerd Moellmann's avatar
Gerd Moellmann committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
;; has a function w3-follow-url-at-point, but that
;; doesn't let you edit the URL like browse-url.
;; The `gnuserv' package that can be used to control it in another
;; Emacs process is available from
;; <URL:ftp://ftp.splode.com/pub/users/friedman/packages/>.

;; Lynx is now distributed by the FSF.  See also
;; <URL:http://lynx.browser.org/>.

;; Free graphical browsers that could be used by `browse-url-generic'
;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
;; <URL:http://www.unlv.edu/chimera/>, Arena
;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena> and Amaya
;; <URL:ftp://ftp.w3.org/pub/amaya>.  mMosaic
Pavel Janík's avatar
Pavel Janík committed
86 87
;; <URL:ftp://ftp.enst.fr/pub/mbone/mMosaic/>,
;; <URL:http://www.enst.fr/~dauphin/mMosaic/> (with development
Gerd Moellmann's avatar
Gerd Moellmann committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
;; support for Java applets and multicast) can be used like Mosaic by
;; setting `browse-url-mosaic-program' appropriately.

;; I [Denis Howe, not Dave Love] recommend Nelson Minar
;; <nelson@santafe.edu>'s excellent html-helper-mode.el for editing
;; HTML and thank Nelson for his many useful comments on this code.
;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>

;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
;; software/own/hm--html-menus/>.  For composing correct HTML see also
;; PSGML the general SGML structure editor package
;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
;; with this.

;; This package generalises function html-previewer-process in Marc
;; Andreessen's html-mode (LCD modes/html-mode.el.Z).  See also the
;; ffap.el package.  The huge hyperbole package also contains similar
;; functions.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Help!

;; Can you write and test some code for the Macintrash and Windoze
;; Netscape remote control APIs?  (See the URL above).

;; Do any other browsers have remote control?

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Usage

;; To display the URL at or before point:
;; M-x browse-url-at-point RET
;; or, similarly but with the opportunity to edit the URL extracted from
;; the buffer, use:
;; M-x browse-url

;; To display a URL by shift-clicking on it, put this in your ~/.emacs
;; file:
;;      (global-set-key [S-mouse-2] 'browse-url-at-mouse)
;; (Note that using Shift-mouse-1 is not desirable because
;; that event has a standard meaning in Emacs.)

;; To display the current buffer in a web browser:
;; M-x browse-url-of-buffer RET

;; To display the current region in a web browser:
;; M-x browse-url-of-region RET

;; In Dired, to display the file named on the current line:
;; M-x browse-url-of-dired-file RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Customisation (~/.emacs)

;; To see what variables are available for customization, type
;; `M-x set-variable browse-url TAB'.  Better, use
;; `M-x customize-group browse-url'.

;; Bind the browse-url commands to keys with the `C-c C-z' prefix
;; (as used by html-helper-mode):
;;	(global-set-key "\C-c\C-z." 'browse-url-at-point)
;;	(global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
;;	(global-set-key "\C-c\C-zr" 'browse-url-of-region)
;;	(global-set-key "\C-c\C-zu" 'browse-url)
;;	(global-set-key "\C-c\C-zv" 'browse-url-of-file)
;;	(add-hook 'dired-mode-hook
;;		  (lambda ()
;;	             (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))

157
;; Browse URLs in mail messages under RMAIL by clicking mouse-2:
Gerd Moellmann's avatar
Gerd Moellmann committed
158 159
;;	(add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
;;	  (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
160
;; Alternatively, add `goto-address' to `rmail-show-message-hook'.
Gerd Moellmann's avatar
Gerd Moellmann committed
161

162 163
;; Gnus provides a standard feature to activate URLs in article
;; buffers for invocation of browse-url.
Gerd Moellmann's avatar
Gerd Moellmann committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

;; Use the Emacs w3 browser when not running under X11:
;;	(or (eq window-system 'x)
;;	    (setq browse-url-browser-function 'browse-url-w3))

;; To always save modified buffers before displaying the file in a browser:
;;	(setq browse-url-save-file t)

;; To get round the Netscape caching problem, you could EITHER have
;; write-file in html-helper-mode make Netscape reload the document:
;;
;;	(autoload 'browse-url-netscape-reload "browse-url"
;;	  "Ask a WWW browser to redisplay the current file." t)
;;	(add-hook 'html-helper-mode-hook
;;		  (lambda ()
;;		     (add-hook 'local-write-file-hooks
;;			       (lambda ()
;;				  (let ((local-write-file-hooks))
;;				    (save-buffer))
;;				  (browse-url-netscape-reload)
;;				  t)			; => file written by hook
;;			       t)))			; append to l-w-f-hooks
;;
;; OR have browse-url-of-file ask Netscape to load and then reload the
;; file:
;;
;;	(add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)

;; You may also want to customise browse-url-netscape-arguments, e.g.
;;	(setq browse-url-netscape-arguments '("-install"))
;;
;; or similarly for the other browsers.

;; To invoke different browsers for different URLs:
;;      (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
;;      				    ("." . browse-url-netscape)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Variables

207 208
(eval-when-compile (require 'cl)
		   (require 'thingatpt)
Gerd Moellmann's avatar
Gerd Moellmann committed
209 210
                   (require 'term)
		   (require 'dired)
211
                   (require 'executable)
Gerd Moellmann's avatar
Gerd Moellmann committed
212 213 214 215 216
		   (require 'w3-auto nil t))

(defgroup browse-url nil
  "Use a web browser to look at a URL."
  :prefix "browse-url-"
Dave Love's avatar
Dave Love committed
217
  :link '(emacs-commentary-link "browse-url")
Chong Yidong's avatar
Chong Yidong committed
218
  :group 'external
219
  :group 'comm)
Gerd Moellmann's avatar
Gerd Moellmann committed
220 221 222

;;;###autoload
(defcustom browse-url-browser-function
223 224 225 226 227
  (cond
   ((memq system-type '(windows-nt ms-dos cygwin))
    'browse-url-default-windows-browser)
   ((memq system-type '(darwin)) 'browse-url-default-macosx-browser)
   (t 'browse-url-default-browser))
228
  "Function to display the current buffer in a WWW browser.
Gerd Moellmann's avatar
Gerd Moellmann committed
229 230 231 232
This is used by the `browse-url-at-point', `browse-url-at-mouse', and
`browse-url-of-file' commands.

If the value is not a function it should be a list of pairs
Dave Love's avatar
Dave Love committed
233
\(REGEXP . FUNCTION).  In this case the function called will be the one
Gerd Moellmann's avatar
Gerd Moellmann committed
234 235 236 237
associated with the first REGEXP which matches the current URL.  The
function is passed the URL and any other args of `browse-url'.  The last
regexp should probably be \".\" to specify a default browser."
  :type '(choice
Dave Love's avatar
Dave Love committed
238 239 240
	  (function-item :tag "Emacs W3" :value  browse-url-w3)
	  (function-item :tag "W3 in another Emacs via `gnudoit'"
			 :value  browse-url-w3-gnudoit)
241
	  (function-item :tag "Mozilla" :value  browse-url-mozilla)
Eli Zaretskii's avatar
Eli Zaretskii committed
242
	  (function-item :tag "Firefox" :value browse-url-firefox)
243
	  (function-item :tag "Galeon" :value  browse-url-galeon)
244
	  (function-item :tag "Epiphany" :value  browse-url-epiphany)
Dave Love's avatar
Dave Love committed
245 246 247
	  (function-item :tag "Netscape" :value  browse-url-netscape)
	  (function-item :tag "Mosaic" :value  browse-url-mosaic)
	  (function-item :tag "Mosaic using CCI" :value  browse-url-cci)
248 249 250 251
	  (function-item :tag "Text browser in an xterm window"
			 :value browse-url-text-xterm)
	  (function-item :tag "Text browser in an Emacs window"
			 :value browse-url-text-emacs)
252
	  (function-item :tag "KDE" :value browse-url-kde)
253
	  (function-item :tag "Elinks" :value browse-url-elinks)
Dave Love's avatar
Dave Love committed
254 255 256
	  (function-item :tag "Specified by `Browse Url Generic Program'"
			 :value browse-url-generic)
	  (function-item :tag "Default Windows browser"
257
			 :value browse-url-default-windows-browser)
258 259
	  (function-item :tag "Default Mac OS X browser"
			 :value browse-url-default-macosx-browser)
260 261
	  (function-item :tag "GNOME invoking Mozilla"
			 :value browse-url-gnome-moz)
262 263
	  (function-item :tag "Default browser"
			 :value browse-url-default-browser)
Dave Love's avatar
Dave Love committed
264 265 266
	  (function :tag "Your own function")
	  (alist :tag "Regexp/function association list"
		 :key-type regexp :value-type function))
267
  :version "21.1"
Gerd Moellmann's avatar
Gerd Moellmann committed
268 269 270 271
  :group 'browse-url)

(defcustom browse-url-netscape-program "netscape"
  ;; Info about netscape-remote from Karl Berry.
272
  "The name by which to invoke Netscape.
Gerd Moellmann's avatar
Gerd Moellmann committed
273 274 275 276 277 278 279 280 281 282

The free program `netscape-remote' from
<URL:http://home.netscape.com/newsref/std/remote.c> is said to start
up very much quicker than `netscape'.  Reported to compile on a GNU
system, given vroot.h from the same directory, with cc flags
 -DSTANDALONE -L/usr/X11R6/lib -lXmu -lX11."
  :type 'string
  :group 'browse-url)

(defcustom browse-url-netscape-arguments nil
283
  "A list of strings to pass to Netscape as arguments."
Gerd Moellmann's avatar
Gerd Moellmann committed
284 285 286 287
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

(defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
288
  "A list of strings to pass to Netscape when it starts up.
Gerd Moellmann's avatar
Gerd Moellmann committed
289 290 291 292 293
Defaults to the value of `browse-url-netscape-arguments' at the time
`browse-url' is loaded."
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

294
(defcustom browse-url-browser-display nil
295
  "The X display for running the browser, if not same as Emacs'."
296 297 298 299
  :type '(choice string (const :tag "Default" nil))
  :group 'browse-url)

(defcustom browse-url-mozilla-program "mozilla"
300
  "The name by which to invoke Mozilla."
301 302 303 304
  :type 'string
  :group 'browse-url)

(defcustom browse-url-mozilla-arguments nil
305
  "A list of strings to pass to Mozilla as arguments."
306 307 308 309
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

(defcustom browse-url-mozilla-startup-arguments browse-url-mozilla-arguments
310
  "A list of strings to pass to Mozilla when it starts up.
311 312 313 314 315
Defaults to the value of `browse-url-mozilla-arguments' at the time
`browse-url' is loaded."
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

316 317 318 319 320
(defcustom browse-url-firefox-program
  (let ((candidates '("firefox" "iceweasel")))
    (while (and candidates (not (executable-find (car candidates))))
      (setq candidates (cdr candidates)))
    (or (car candidates) "firefox"))
321
  "The name by which to invoke Firefox."
Eli Zaretskii's avatar
Eli Zaretskii committed
322 323 324 325
  :type 'string
  :group 'browse-url)

(defcustom browse-url-firefox-arguments nil
326
  "A list of strings to pass to Firefox as arguments."
Eli Zaretskii's avatar
Eli Zaretskii committed
327 328 329 330
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

(defcustom browse-url-firefox-startup-arguments browse-url-firefox-arguments
331
  "A list of strings to pass to Firefox when it starts up.
Eli Zaretskii's avatar
Eli Zaretskii committed
332 333 334 335 336
Defaults to the value of `browse-url-firefox-arguments' at the time
`browse-url' is loaded."
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

337
(defcustom browse-url-galeon-program "galeon"
338
  "The name by which to invoke Galeon."
339 340 341 342
  :type 'string
  :group 'browse-url)

(defcustom browse-url-galeon-arguments nil
343
  "A list of strings to pass to Galeon as arguments."
344 345 346 347
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

(defcustom browse-url-galeon-startup-arguments browse-url-galeon-arguments
348
  "A list of strings to pass to Galeon when it starts up.
349 350 351 352 353
Defaults to the value of `browse-url-galeon-arguments' at the time
`browse-url' is loaded."
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

354
(defcustom browse-url-epiphany-program "epiphany"
355
  "The name by which to invoke Epiphany."
356 357 358 359
  :type 'string
  :group 'browse-url)

(defcustom browse-url-epiphany-arguments nil
360
  "A list of strings to pass to Epiphany as arguments."
361 362 363 364
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

(defcustom browse-url-epiphany-startup-arguments browse-url-epiphany-arguments
365
  "A list of strings to pass to Epiphany when it starts up.
366 367 368 369 370
Defaults to the value of `browse-url-epiphany-arguments' at the time
`browse-url' is loaded."
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

371 372 373 374
;; GNOME means of invoking either Mozilla or Netrape.
(defvar browse-url-gnome-moz-program "gnome-moz-remote")

(defcustom browse-url-gnome-moz-arguments '()
375
  "A list of strings passed to the GNOME mozilla viewer as arguments."
376 377 378 379
  :version "21.1"
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

380
(defcustom browse-url-mozilla-new-window-is-tab nil
381
  "Whether to open up new windows in a tab or a new window.
382 383 384 385 386
If non-nil, then open the URL in a new tab rather than a new window if
`browse-url-mozilla' is asked to open it in a new window."
  :type 'boolean
  :group 'browse-url)

Eli Zaretskii's avatar
Eli Zaretskii committed
387
(defcustom browse-url-firefox-new-window-is-tab nil
388
  "Whether to open up new windows in a tab or a new window.
Eli Zaretskii's avatar
Eli Zaretskii committed
389 390 391 392 393 394 395 396
If non-nil, then open the URL in a new tab rather than a new window if
`browse-url-firefox' is asked to open it in a new window.

This option is currently ignored on MS-Windows, since the necessary
functionality is not available there."
  :type 'boolean
  :group 'browse-url)

397
(defcustom browse-url-galeon-new-window-is-tab nil
398
  "Whether to open up new windows in a tab or a new window.
399 400 401 402 403
If non-nil, then open the URL in a new tab rather than a new window if
`browse-url-galeon' is asked to open it in a new window."
  :type 'boolean
  :group 'browse-url)

404
(defcustom browse-url-epiphany-new-window-is-tab nil
405
  "Whether to open up new windows in a tab or a new window.
406 407 408 409 410
If non-nil, then open the URL in a new tab rather than a new window if
`browse-url-epiphany' is asked to open it in a new window."
  :type 'boolean
  :group 'browse-url)

411
(defcustom browse-url-netscape-new-window-is-tab nil
412
  "Whether to open up new windows in a tab or a new window.
413 414 415 416 417 418
If non-nil, then open the URL in a new tab rather than a new
window if `browse-url-netscape' is asked to open it in a new
window."
  :type 'boolean
  :group 'browse-url)

419
(defcustom browse-url-new-window-flag nil
420
  "Non-nil means always open a new browser window with appropriate browsers.
Gerd Moellmann's avatar
Gerd Moellmann committed
421 422 423 424 425 426 427
Passing an interactive argument to \\[browse-url], or specific browser
commands reverses the effect of this variable.  Requires Netscape version
1.1N or later or XMosaic version 2.5 or later if using those browsers."
  :type 'boolean
  :group 'browse-url)

(defcustom browse-url-mosaic-program "xmosaic"
428
  "The name by which to invoke Mosaic (or mMosaic)."
Gerd Moellmann's avatar
Gerd Moellmann committed
429 430 431 432 433
  :type 'string
  :version "20.3"
  :group 'browse-url)

(defcustom browse-url-mosaic-arguments nil
434
  "A list of strings to pass to Mosaic as arguments."
Gerd Moellmann's avatar
Gerd Moellmann committed
435 436 437
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

438
(defcustom browse-url-mosaic-pidfile "~/.mosaicpid"
439
  "The name of the pidfile created by Mosaic."
440 441 442
  :type 'string
  :group 'browse-url)

Gerd Moellmann's avatar
Gerd Moellmann committed
443
(defcustom browse-url-filename-alist
444
  `(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
Gerd Moellmann's avatar
Gerd Moellmann committed
445 446 447 448
    ;; The above loses the username to avoid the browser prompting for
    ;; it in anonymous cases.  If it's not anonymous the next regexp
    ;; applies.
    ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
449
    ,@(if (memq system-type '(windows-nt ms-dos cygwin))
450
          '(("^\\([a-zA-Z]:\\)[\\/]" . "file:///\\1/")
451
            ("^[\\/][\\/]+" . "file://")))
452
    ("^/+" . "file:///"))
453
  "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
Gerd Moellmann's avatar
Gerd Moellmann committed
454 455 456
Any substring of a filename matching one of the REGEXPs is replaced by
the corresponding STRING using `replace-match', not treating STRING
literally.  All pairs are applied in the order given.  The default
457 458
value converts ange-ftp/EFS-style file names into ftp URLs and prepends
`file:' to any file name beginning with `/'.
Gerd Moellmann's avatar
Gerd Moellmann committed
459 460 461 462 463 464 465 466 467

For example, adding to the default a specific translation of an ange-ftp
address to an HTTP URL:

    (setq browse-url-filename-alist
	  '((\"/webmaster@webserver:/home/www/html/\" .
	     \"http://www.acme.co.uk/\")
            (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
            (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
468
	    (\"^/+\" . \"file:/\")))"
Gerd Moellmann's avatar
Gerd Moellmann committed
469 470 471
  :type '(repeat (cons :format "%v"
                       (regexp :tag "Regexp")
                       (string :tag "Replacement")))
472
  :version "23.1"
Gerd Moellmann's avatar
Gerd Moellmann committed
473 474 475
  :group 'browse-url)

(defcustom browse-url-save-file nil
476
  "If non-nil, save the buffer before displaying its file.
Gerd Moellmann's avatar
Gerd Moellmann committed
477 478 479 480 481
Used by the `browse-url-of-file' command."
  :type 'boolean
  :group 'browse-url)

(defcustom browse-url-of-file-hook nil
482
  "Run after `browse-url-of-file' has asked a browser to load a file.
Gerd Moellmann's avatar
Gerd Moellmann committed
483 484 485 486 487 488 489 490

Set this to `browse-url-netscape-reload' to force Netscape to load the
file rather than displaying a cached copy."
  :type 'hook
  :options '(browse-url-netscape-reload)
  :group 'browse-url)

(defcustom browse-url-CCI-port 3003
491
  "Port to access XMosaic via CCI.
Gerd Moellmann's avatar
Gerd Moellmann committed
492 493 494 495 496 497
This can be any number between 1024 and 65535 but must correspond to
the value set in the browser."
  :type 'integer
  :group 'browse-url)

(defcustom browse-url-CCI-host "localhost"
498
  "Host to access XMosaic via CCI.
Gerd Moellmann's avatar
Gerd Moellmann committed
499 500 501 502 503 504 505
This should be the host name of the machine running XMosaic with CCI
enabled.  The port number should be set in `browse-url-CCI-port'."
  :type 'string
  :group 'browse-url)

(defvar browse-url-temp-file-name nil)
(make-variable-buffer-local 'browse-url-temp-file-name)
506

Gerd Moellmann's avatar
Gerd Moellmann committed
507
(defcustom browse-url-xterm-program "xterm"
508
  "The name of the terminal emulator used by `browse-url-text-xterm'.
509
This might, for instance, be a separate color version of xterm."
Gerd Moellmann's avatar
Gerd Moellmann committed
510 511 512 513
  :type 'string
  :group 'browse-url)

(defcustom browse-url-xterm-args nil
514
  "A list of strings defining options for `browse-url-xterm-program'.
Gerd Moellmann's avatar
Gerd Moellmann committed
515 516 517 518 519
These might set its size, for instance."
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

(defcustom browse-url-gnudoit-program "gnudoit"
520
  "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
Gerd Moellmann's avatar
Gerd Moellmann committed
521 522 523 524
  :type 'string
  :group 'browse-url)

(defcustom browse-url-gnudoit-args '("-q")
525
  "A list of strings defining options for `browse-url-gnudoit-program'.
Gerd Moellmann's avatar
Gerd Moellmann committed
526 527 528 529 530
These might set the port, for instance."
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

(defcustom browse-url-generic-program nil
531
  "The name of the browser program used by `browse-url-generic'."
Gerd Moellmann's avatar
Gerd Moellmann committed
532 533 534 535
  :type '(choice string (const :tag "None" nil))
  :group 'browse-url)

(defcustom browse-url-generic-args nil
536
  "A list of strings defining options for `browse-url-generic-program'."
Gerd Moellmann's avatar
Gerd Moellmann committed
537 538 539 540
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

(defcustom browse-url-temp-dir temporary-file-directory
541
  "The name of a directory for browse-url's temporary files.
Gerd Moellmann's avatar
Gerd Moellmann committed
542 543 544 545 546 547
Such files are generated by functions like `browse-url-of-region'.
You might want to set this to somewhere with restricted read permissions
for privacy's sake."
  :type 'string
  :group 'browse-url)

548 549
(defcustom browse-url-netscape-version 3
  "The version of Netscape you are using.
Gerd Moellmann's avatar
Gerd Moellmann committed
550 551 552 553 554
This affects how URL reloading is done; the mechanism changed
incompatibly at version 4."
  :type 'number
  :group 'browse-url)

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
(defcustom browse-url-text-browser "lynx"
  "The name of the text browser to invoke."
  :type 'string
  :group 'browse-url
  :version "23.1")

(defcustom browse-url-text-emacs-args (and (not window-system)
					   '("-show_cursor"))
  "A list of strings defining options for a text browser in an Emacs buffer.

The default is none in a window system, otherwise `-show_cursor' to
indicate the position of the current link in the absence of
highlighting, assuming the normal default for showing the cursor."
  :type '(repeat (string :tag "Argument"))
  :version "23.1"
  :group 'browse-url)

(defcustom browse-url-text-input-field 'avoid
  "Action on selecting an existing text browser buffer at an input field.
What to do when sending a new URL to an existing text browser buffer in Emacs
if the browser cursor is on an input field (in which case the `g' command
Gerd Moellmann's avatar
Gerd Moellmann committed
576
would be entered as data).  Such fields are recognized by the
577 578
underlines ____.  Allowed values: nil: disregard it, `warn': warn the
user and don't emit the URL, `avoid': try to avoid the field by moving
Gerd Moellmann's avatar
Gerd Moellmann committed
579 580 581 582
down (this *won't* always work)."
  :type '(choice (const :tag "Move to try to avoid field" :value avoid)
                 (const :tag "Disregard" :value nil)
                 (const :tag "Warn, don't emit URL" :value warn))
583
  :version "23.1"
Gerd Moellmann's avatar
Gerd Moellmann committed
584 585
  :group 'browse-url)

586 587
(defcustom browse-url-text-input-attempts 10
  "How many times to try to move down from a series of text browser input fields."
588
  :type 'integer
589
  :version "23.1"
590
  :group 'browse-url)
Gerd Moellmann's avatar
Gerd Moellmann committed
591

592 593
(defcustom browse-url-text-input-delay 0.2
  "Seconds to wait for a text browser between moves down from an input field."
594
  :type 'number
595
  :version "23.1"
596
  :group 'browse-url)
Gerd Moellmann's avatar
Gerd Moellmann committed
597

598
(defcustom browse-url-kde-program "kfmclient"
599
  "The name by which to invoke the KDE web browser."
600 601 602 603 604
  :type 'string
  :version "21.1"
  :group 'browse-url)

(defcustom browse-url-kde-args '("openURL")
605
  "A list of strings defining options for `browse-url-kde-program'."
606 607 608
  :type '(repeat (string :tag "Argument"))
  :group 'browse-url)

609 610 611 612 613 614 615 616
(defcustom browse-url-elinks-wrapper '("xterm" "-e")
  "*Wrapper command prepended to the Elinks command-line."
  :type '(repeat (string :tag "Wrapper"))
  :group 'browse-url)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; URL encoding

617 618
(defun browse-url-url-encode-chars (text chars)
  "URL-encode the chars in TEXT that match CHARS.
619
CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
620
  (let ((encoded-text (copy-sequence text))
621
	(s 0))
622 623
    (while (setq s (string-match chars encoded-text s))
      (setq encoded-text
624
	    (replace-match (format "%%%x"
625 626
				   (string-to-char (match-string 0 encoded-text)))
			   t t encoded-text)
627
	    s (1+ s)))
628
    encoded-text))
629

630 631
(defun browse-url-encode-url (url)
  "Escape annoying characters in URL.
632 633 634 635 636 637
The annoying characters are those that can mislead a web browser
regarding its parameter treatment."
  ;; FIXME: Is there an actual example of a web browser getting
  ;; confused?  (This used to encode commas, but at least Firefox
  ;; handles commas correctly and doesn't accept encoded commas.)
  (browse-url-url-encode-chars url "[)$]"))
638

Gerd Moellmann's avatar
Gerd Moellmann committed
639 640 641
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; URL input

642
;;;###autoload
Gerd Moellmann's avatar
Gerd Moellmann committed
643 644 645 646 647 648 649 650 651 652 653
(defun browse-url-url-at-point ()
  (let ((url (thing-at-point 'url)))
    (set-text-properties 0 (length url) nil url)
    url))

;; Having this as a separate function called by the browser-specific
;; functions allows them to be stand-alone commands, making it easier
;; to switch between browsers.

(defun browse-url-interactive-arg (prompt)
  "Read a URL from the minibuffer, prompting with PROMPT.
654
If `transient-mark-mode' is non-nil and the mark is active,
655 656 657 658 659 660
it defaults to the current region, else to the URL at or before
point.  If invoked with a mouse button, it moves point to the
position clicked before acting.

This function returns a list (URL NEW-WINDOW-FLAG)
for use in `interactive'."
Gerd Moellmann's avatar
Gerd Moellmann committed
661 662
  (let ((event (elt (this-command-keys) 0)))
    (and (listp event) (mouse-set-point event)))
663 664 665 666 667 668 669
  (list (read-string prompt (or (and transient-mark-mode mark-active
				     ;; rfc2396 Appendix E.
				     (replace-regexp-in-string
				      "[\t\r\f\n ]+" ""
				      (buffer-substring-no-properties
				       (region-beginning) (region-end))))
				(browse-url-url-at-point)))
670
	(not (eq (null browse-url-new-window-flag)
Gerd Moellmann's avatar
Gerd Moellmann committed
671 672
		 (null current-prefix-arg)))))

673 674 675
;; called-interactive-p needs to be called at a function's top-level, hence
;; this macro.  We use that rather than interactive-p because
;; use in a keyboard macro should not change this behavior.
676
(defmacro browse-url-maybe-new-window (arg)
677
  `(if (or noninteractive (not (called-interactively-p 'any)))
678
       ,arg
679
     browse-url-new-window-flag))
680

Gerd Moellmann's avatar
Gerd Moellmann committed
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Browse current buffer

;;;###autoload
(defun browse-url-of-file (&optional file)
  "Ask a WWW browser to display FILE.
Display the current buffer's file if FILE is nil or if called
interactively.  Turn the filename into a URL with function
`browse-url-file-url'.  Pass the URL to a browser using the
`browse-url' function then run `browse-url-of-file-hook'."
  (interactive)
  (or file
      (setq file (buffer-file-name))
      (error "Current buffer has no file"))
  (let ((buf (get-file-buffer file)))
    (if buf
697
	(with-current-buffer buf
Gerd Moellmann's avatar
Gerd Moellmann committed
698 699 700 701 702 703 704 705 706
	  (cond ((not (buffer-modified-p)))
		(browse-url-save-file (save-buffer))
		(t (message "%s modified since last save" file))))))
  (browse-url (browse-url-file-url file))
  (run-hooks 'browse-url-of-file-hook))

(defun browse-url-file-url (file)
  "Return the URL corresponding to FILE.
Use variable `browse-url-filename-alist' to map filenames to URLs."
707 708 709 710 711 712
  ;; De-munge Cygwin filenames before passing them to Windows browser.
  (if (eq system-type 'cygwin)
      (let ((winfile (with-output-to-string
		       (call-process "cygpath" nil standard-output
				     nil "-m" file))))
	(setq file (substring winfile 0 -1))))
713
  (let ((coding (and (default-value 'enable-multibyte-characters)
714 715 716
		     (or file-name-coding-system
			 default-file-name-coding-system))))
    (if coding (setq file (encode-coding-string file coding))))
717
  (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
718 719 720
  (dolist (map browse-url-filename-alist)
    (when (and map (string-match (car map) file))
      (setq file (replace-match (cdr map) t nil file))))
Gerd Moellmann's avatar
Gerd Moellmann committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
  file)

;;;###autoload
(defun browse-url-of-buffer (&optional buffer)
  "Ask a WWW browser to display BUFFER.
Display the current buffer if BUFFER is nil.  Display only the
currently visible part of BUFFER (from a temporary file) if buffer is
narrowed."
  (interactive)
  (save-excursion
    (and buffer (set-buffer buffer))
    (let ((file-name
	   ;; Ignore real name if restricted
	   (and (= (- (point-max) (point-min)) (buffer-size))
		(or buffer-file-name
		    (and (boundp 'dired-directory) dired-directory)))))
      (or file-name
	  (progn
	    (or browse-url-temp-file-name
		(setq browse-url-temp-file-name
		      (convert-standard-filename
		       (make-temp-file
743 744
			(expand-file-name "burl" browse-url-temp-dir)
			nil ".html"))))
Gerd Moellmann's avatar
Gerd Moellmann committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	    (setq file-name browse-url-temp-file-name)
	    (write-region (point-min) (point-max) file-name nil 'no-message)))
      (browse-url-of-file file-name))))

(defun browse-url-delete-temp-file (&optional temp-file-name)
  ;; Delete browse-url-temp-file-name from the file system
  ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
  (let ((file-name (or temp-file-name browse-url-temp-file-name)))
    (if (and file-name (file-exists-p file-name))
	(delete-file file-name))))

(add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)

;;;###autoload
(defun browse-url-of-dired-file ()
  "In Dired, ask a WWW browser to display the file named on this line."
  (interactive)
  (browse-url-of-file (dired-get-filename)))

;;;###autoload
(defun browse-url-of-region (min max)
  "Ask a WWW browser to display the current region."
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region min max)
      (browse-url-of-buffer))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Browser-independent commands

;; A generic command to call the current browse-url-browser-function

;;;###autoload
(defun browse-url (url &rest args)
  "Ask a WWW browser to load URL.
Prompts for a URL, defaulting to the URL at or before point.  Variable
`browse-url-browser-function' says which browser to use."
  (interactive (browse-url-interactive-arg "URL: "))
784
  (unless (called-interactively-p 'interactive)
785
    (setq args (or args (list browse-url-new-window-flag))))
786 787 788 789 790 791
  (let ((process-environment (copy-sequence process-environment)))
    ;; When connected to various displays, be careful to use the display of
    ;; the currently selected frame, rather than the original start display,
    ;; which may not even exist any more.
    (if (stringp (frame-parameter (selected-frame) 'display))
        (setenv "DISPLAY" (frame-parameter (selected-frame) 'display)))
792 793 794 795 796 797 798 799 800 801 802 803 804 805
    (if (and (consp browse-url-browser-function)
	     (not (functionp browse-url-browser-function)))
	;; The `function' can be an alist; look down it for first match
	;; and apply the function (which might be a lambda).
	(catch 'done
	  (dolist (bf browse-url-browser-function)
	    (when (string-match (car bf) url)
	      (apply (cdr bf) url args)
	      (throw 'done t)))
	  (error "No browse-url-browser-function matching URL %s"
		 url))
      ;; Unbound symbols go down this leg, since void-function from
      ;; apply is clearer than wrong-type-argument from dolist.
      (apply browse-url-browser-function url args))))
Gerd Moellmann's avatar
Gerd Moellmann committed
806 807

;;;###autoload
808
(defun browse-url-at-point (&optional arg)
Gerd Moellmann's avatar
Gerd Moellmann committed
809 810 811
  "Ask a WWW browser to load the URL at or before point.
Doesn't let you edit the URL like `browse-url'.  Variable
`browse-url-browser-function' says which browser to use."
812
  (interactive "P")
813 814 815
  (let ((url (browse-url-url-at-point)))
    (if url
	(browse-url url (if arg
816 817
			    (not browse-url-new-window-flag)
			  browse-url-new-window-flag))
818
      (error "No URL found"))))
Gerd Moellmann's avatar
Gerd Moellmann committed
819 820 821 822 823 824 825 826 827 828

;;;###autoload
(defun browse-url-at-mouse (event)
  "Ask a WWW browser to load a URL clicked with the mouse.
The URL is the one around or before the position of the mouse click
but point is not changed.  Doesn't let you edit the URL like
`browse-url'.  Variable `browse-url-browser-function' says which browser
to use."
  (interactive "e")
  (save-excursion
829
    (mouse-set-point event)
830 831 832
    ;; This handles browse-url-new-window-flag properly
    ;; when it gets no arg.
    (browse-url-at-point)))
Gerd Moellmann's avatar
Gerd Moellmann committed
833 834 835 836 837 838

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Browser-specific commands

;; --- Default MS-Windows browser ---

839
(defvar dos-windows-version)
840
(declare-function w32-shell-execute "w32fns.c")    ;; Defined in C.
841

Gerd Moellmann's avatar
Gerd Moellmann committed
842 843
(defun browse-url-default-windows-browser (url &optional new-window)
  (interactive (browse-url-interactive-arg "URL: "))
844 845 846 847 848
  (cond ((eq system-type 'ms-dos)
	 (if dos-windows-version
	     (shell-command (concat "start " (shell-quote-argument url)))
	   (error "Browsing URLs is not supported on this system")))
	((eq system-type 'cygwin)
849
	 (call-process "cygstart" nil nil nil url))
850
	(t (w32-shell-execute "open" url))))
Gerd Moellmann's avatar
Gerd Moellmann committed
851

852 853 854 855
(defun browse-url-default-macosx-browser (url &optional new-window)
  (interactive (browse-url-interactive-arg "URL: "))
  (start-process (concat "open " url) nil "open" url))

Gerd Moellmann's avatar
Gerd Moellmann committed
856 857 858
;; --- Netscape ---

(defun browse-url-process-environment ()
859 860
  "Set DISPLAY in the environment to the X display the browser will use.
This is either the value of variable `browse-url-browser-display' if
Gerd Moellmann's avatar
Gerd Moellmann committed
861 862
non-nil, or the same display as Emacs if different from the current
environment, otherwise just use the current environment."
863
  (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
Gerd Moellmann's avatar
Gerd Moellmann committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877
    (if display
	(cons (concat "DISPLAY=" display) process-environment)
      process-environment)))

(defun browse-url-emacs-display ()
  "Return the X display Emacs is running on.
This is nil if the display is the same as the DISPLAY environment variable.

Actually Emacs could be using several displays; this just returns the
one showing the selected frame."
  (let ((display (cdr-safe (assq 'display (frame-parameters)))))
    (and (not (equal display (getenv "DISPLAY")))
         display)))

878 879 880 881 882 883 884 885 886 887 888 889 890
;;;###autoload
(defun browse-url-default-browser (url &rest args)
  "Find a suitable browser and ask it to load URL.
Default to the URL around or before point.

When called interactively, if variable `browse-url-new-window-flag' is
non-nil, load the document in a new window, if possible, otherwise use
a random existing one.  A non-nil interactive prefix argument reverses
the effect of `browse-url-new-window-flag'.

When called non-interactively, optional second argument NEW-WINDOW is
used instead of `browse-url-new-window-flag'.

Eli Zaretskii's avatar
Eli Zaretskii committed
891
The order attempted is gnome-moz-remote, Mozilla, Firefox,
892
Galeon, Konqueror, Netscape, Mosaic, Lynx in an xterm, and then W3."
893
  (apply
894
   (cond
895
    ((browse-url-can-use-xdg-open) 'browse-url-xdg-open)
896 897 898 899 900 901 902
    ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
    ((executable-find browse-url-mozilla-program) 'browse-url-mozilla)
    ((executable-find browse-url-firefox-program) 'browse-url-firefox)
    ((executable-find browse-url-galeon-program) 'browse-url-galeon)
    ((executable-find browse-url-kde-program) 'browse-url-kde)
    ((executable-find browse-url-netscape-program) 'browse-url-netscape)
    ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
903
    ((executable-find browse-url-xterm-program) 'browse-url-text-xterm)
904 905
    ((locate-library "w3") 'browse-url-w3)
    (t
906
     (lambda (&rest ignore) (error "No usable browser found"))))
907
   url args))
908

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
(defun browse-url-can-use-xdg-open ()
  "Check if xdg-open can be used, i.e. we are on Gnome, KDE or xfce4."
  (and (getenv "DISPLAY")
       (executable-find "xdg-open")
       ;; xdg-open may call gnome-open and that does not wait for its child
       ;; to finish.  This child may then be killed when the parent dies.
       ;; Use nohup to work around.
       (executable-find "nohup")
       (or (getenv "GNOME_DESKTOP_SESSION_ID")
	   ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
	   (condition-case nil
	       (eq 0 (call-process
		      "dbus-send" nil nil nil
				  "--dest=org.gnome.SessionManager"
				  "--print-reply"
				  "/org/gnome/SessionManager"
				  "org.gnome.SessionManager.CanShutdown"))
	     (error nil))
	   (equal (getenv "KDE_FULL_SESSION") "true")
	   (condition-case nil
	       (eq 0 (call-process
		      "/bin/sh" nil nil nil
		      "-c"
		      "xprop -root _DT_SAVE_MODE|grep xfce4"))
	     (error nil)))))


;;;###autoload
(defun browse-url-xdg-open (url &optional new-window)
  (interactive (browse-url-interactive-arg "URL: "))
  (call-process "/bin/sh" nil nil nil
		"-c"
		(concat "nohup xdg-open " url
			">/dev/null 2>&1 </dev/null")))

Gerd Moellmann's avatar
Gerd Moellmann committed
944 945 946 947 948 949
;;;###autoload
(defun browse-url-netscape (url &optional new-window)
  "Ask the Netscape WWW browser to load URL.
Default to the URL around or before point.  The strings in variable
`browse-url-netscape-arguments' are also passed to Netscape.

950
When called interactively, if variable `browse-url-new-window-flag' is
Gerd Moellmann's avatar
Gerd Moellmann committed
951 952
non-nil, load the document in a new Netscape window, otherwise use a
random existing one.  A non-nil interactive prefix argument reverses
953
the effect of `browse-url-new-window-flag'.
Gerd Moellmann's avatar
Gerd Moellmann committed
954

955 956 957 958
If `browse-url-netscape-new-window-is-tab' is non-nil, then
whenever a document would otherwise be loaded in a new window, it
is loaded in a new tab in an existing window instead.

Gerd Moellmann's avatar
Gerd Moellmann committed
959
When called non-interactively, optional second argument NEW-WINDOW is
960
used instead of `browse-url-new-window-flag'."
961
  (interactive (browse-url-interactive-arg "URL: "))
962
  (setq url (browse-url-encode-url url))
Gerd Moellmann's avatar
Gerd Moellmann committed
963
  (let* ((process-environment (browse-url-process-environment))
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
	 (process
	  (apply 'start-process
		 (concat "netscape " url) nil
		 browse-url-netscape-program
		 (append
		  browse-url-netscape-arguments
		  (if (eq window-system 'w32)
		      (list url)
		    (append
		     (if new-window '("-noraise"))
		     (list "-remote"
			   (concat "openURL(" url
				   (if (browse-url-maybe-new-window
					new-window)
				       (if browse-url-netscape-new-window-is-tab
					   ",new-tab"
					 ",new-window"))
				   ")"))))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
982
    (set-process-sentinel process
983 984
			  `(lambda (process change)
			     (browse-url-netscape-sentinel process ,url)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
985 986 987 988 989 990

(defun browse-url-netscape-sentinel (process url)
  "Handle a change to the process communicating with Netscape."
  (or (eq (process-exit-status process) 0)
      (let* ((process-environment (browse-url-process-environment)))
	;; Netscape not running - start it
Eli Zaretskii's avatar
Eli Zaretskii committed
991
	(message "Starting %s..." browse-url-netscape-program)
Gerd Moellmann's avatar
Gerd Moellmann committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003
	(apply 'start-process (concat "netscape" url) nil
	       browse-url-netscape-program
	       (append browse-url-netscape-startup-arguments (list url))))))

(defun browse-url-netscape-reload ()
  "Ask Netscape to reload its current document.
How depends on `browse-url-netscape-version'."
  (interactive)
  ;; Backwards incompatibility reported by
  ;; <peter.kruse@psychologie.uni-regensburg.de>.
  (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
				"xfeDoCommand(reload)"
1004
			      "reload")))
Gerd Moellmann's avatar
Gerd Moellmann committed
1005 1006 1007 1008 1009 1010 1011 1012 1013

(defun browse-url-netscape-send (command)
  "Send a remote control command to Netscape."
  (let* ((process-environment (browse-url-process-environment)))
    (apply 'start-process "netscape" nil
           browse-url-netscape-program
           (append browse-url-netscape-arguments
                   (list "-remote" command)))))

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
;;;###autoload
(defun browse-url-mozilla (url &optional new-window)
  "Ask the Mozilla WWW browser to load URL.
Default to the URL around or before point.  The strings in variable
`browse-url-mozilla-arguments' are also passed to Mozilla.

When called interactively, if variable `browse-url-new-window-flag' is
non-nil, load the document in a new Mozilla window, otherwise use a
random existing one.  A non-nil interactive prefix argument reverses
the effect of `browse-url-new-window-flag'.

1025 1026 1027 1028
If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
document would otherwise be loaded in a new window, it is loaded in a
new tab in an existing window instead.

1029 1030 1031
When called non-interactively, optional second argument NEW-WINDOW is
used instead of `browse-url-new-window-flag'."
  (interactive (browse-url-interactive-arg "URL: "))
1032
  (setq url (browse-url-encode-url url))
1033
  (let* ((process-environment (browse-url-process-environment))
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
         (process
	  (apply 'start-process
		 (concat "mozilla " url) nil
		 browse-url-mozilla-program
		 (append
		  browse-url-mozilla-arguments
		  (list "-remote"
			(concat "openURL("
				url
				(if (browse-url-maybe-new-window
				     new-window)
				    (if browse-url-mozilla-new-window-is-tab
1046 1047
					",new-tab"
				      ",new-window"))
1048
				")"))))))
1049 1050 1051 1052 1053 1054 1055 1056 1057
    (set-process-sentinel process
			  `(lambda (process change)
			     (browse-url-mozilla-sentinel process ,url)))))

(defun browse-url-mozilla-sentinel (process url)
  "Handle a change to the process communicating with Mozilla."
  (or (eq (process-exit-status process) 0)
      (let* ((process-environment (browse-url-process-environment)))
	;; Mozilla is not running - start it
Eli Zaretskii's avatar
Eli Zaretskii committed
1058
	(message "Starting %s..." browse-url-mozilla-program)
1059 1060 1061
	(apply 'start-process (concat "mozilla " url) nil
	       browse-url-mozilla-program
	       (append browse-url-mozilla-startup-arguments (list url))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089

;;;###autoload
(defun browse-url-firefox (url &optional new-window)
  "Ask the Firefox WWW browser to load URL.
Default to the URL around or before point.  The strings in
variable `browse-url-firefox-arguments' are also passed to
Firefox.

When called interactively, if variable
`browse-url-new-window-flag' is non-nil, load the document in a
new Firefox window, otherwise use a random existing one.  A
non-nil interactive prefix argument reverses the effect of
`browse-url-new-window-flag'.

If `browse-url-firefox-new-window-is-tab' is non-nil, then
whenever a document would otherwise be loaded in a new window, it
is loaded in a new tab in an existing window instead.

When called non-interactively, optional second argument
NEW-WINDOW is used instead of `browse-url-new-window-flag'.

On MS-Windows systems the optional `new-window' parameter is
ignored.  Firefox for Windows does not support the \"-remote\"
command line parameter.  Therefore, the
`browse-url-new-window-flag' and `browse-url-firefox-new-window-is-tab'
are ignored as well.  Firefox on Windows will always open the requested
URL in a new window."
  (interactive (browse-url-interactive-arg "URL: "))
1090
  (setq url (browse-url-encode-url url))
Eli Zaretskii's avatar
Eli Zaretskii committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
  (let* ((process-environment (browse-url-process-environment))
	 (process
	  (apply 'start-process
		 (concat "firefox " url) nil
		 browse-url-firefox-program
		 (append
		  browse-url-firefox-arguments
		  (if (or (featurep 'dos-w32)
			  (string-match "win32" system-configuration))
		      (list url)
		    (list "-remote"
			  (concat "openURL("
				  url
				  (if (browse-url-maybe-new-window
				       new-window)
				      (if browse-url-firefox-new-window-is-tab
					  ",new-tab"
					",new-window"))
				  ")")))))))
    (set-process-sentinel process
			  `(lambda (process change)
			     (browse-url-firefox-sentinel process ,url)))))

(defun browse-url-firefox-sentinel (process url)
  "Handle a change to the process communicating with Firefox."
  (or (eq (process-exit-status process) 0)
      (let* ((process-environment (browse-url-process-environment)))
	;; Firefox is not running - start it
	(message "Starting Firefox...")
	(apply 'start-process (concat "firefox " url) nil
	       browse-url-firefox-program
	       (append browse-url-firefox-startup-arguments (list url))))))
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134

;;;###autoload
(defun browse-url-galeon (url &optional new-window)
  "Ask the Galeon WWW browser to load URL.
Default to the URL around or before point.  The strings in variable
`browse-url-galeon-arguments' are also passed to Galeon.

When called interactively, if variable `browse-url-new-window-flag' is
non-nil, load the document in a new Galeon window, otherwise use a
random existing one.  A non-nil interactive prefix argument reverses
the effect of `browse-url-new-window-flag'.

1135 1136 1137 1138
If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
document would otherwise be loaded in a new window, it is loaded in a
new tab in an existing window instead.

1139 1140 1141
When called non-interactively, optional second argument NEW-WINDOW is
used instead of `browse-url-new-window-flag'."
  (interactive (browse-url-interactive-arg "URL: "))
1142
  (setq url (browse-url-encode-url url))
1143 1144
  (let* ((process-environment (browse-url-process-environment))
         (process (apply 'start-process
1145 1146
			 (concat "galeon " url)
			 nil
1147 1148 1149
			 browse-url-galeon-program
			 (append
			  browse-url-galeon-arguments
1150 1151 1152
                          (if (browse-url-maybe-new-window new-window)
			      (if browse-url-galeon-new-window-is-tab
				  '("--new-tab")
1153 1154 1155
				'("--new-window" "--noraise"))
                            '("--existing"))
                          (list url)))))
1156 1157 1158 1159 1160 1161 1162 1163 1164
    (set-process-sentinel process
			  `(lambda (process change)
			     (browse-url-galeon-sentinel process ,url)))))

(defun b