(universal-coding-system-argument): Improve prompt strings.
[emacs.git] / lisp / browse-url.el
blob344769ad8f637cd52dcd1d4766a4557dfb9557af
1 ;;; browse-url.el --- Pass a URL to a WWW browser
3 ;; Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
6 ;; Maintainer: Dave Love <d.love@dl.ac.uk>
7 ;; Created: 03 Apr 1995
8 ;; Keywords: hypertext, hypermedia, mouse
9 ;; X-Home page: http://wombat.doc.ic.ac.uk/
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
30 ;; The latest version of this package should be available from
31 ;; <URL:http://wombat.doc.ic.ac.uk/emacs/browse-url.el>.
33 ;; This package provides functions which read a URL (Uniform Resource
34 ;; Locator) from the minibuffer, defaulting to the URL around point,
35 ;; and ask a World-Wide Web browser to load it. It can also load the
36 ;; URL associated with the current buffer. Different browsers use
37 ;; different methods of remote control so there is one function for
38 ;; each supported browser. If the chosen browser is not running, it
39 ;; is started. Currently there is support for:
41 ;; Function Browser Earliest version
42 ;; browse-url-netscape Netscape 1.1b1
43 ;; browse-url-mosaic XMosaic <= 2.4
44 ;; browse-url-cci XMosaic 2.5
45 ;; browse-url-w3 w3 0
46 ;; browse-url-w3-gnudoit w3 remotely
47 ;; browse-url-iximosaic IXI Mosaic ?
48 ;; browse-url-lynx-* Lynx 0
49 ;; browse-url-grail Grail 0.3b1
50 ;; browse-url-mmm MMM ?
51 ;; browse-url-generic arbitrary
53 ;; Note that versions of Netscape before 1.1b1 did not have remote
54 ;; control. <URL:http://www.netscape.com/newsref/std/x-remote.html>.
56 ;; Netscape can cache Web pages so it may be necessary to tell it to
57 ;; reload the current page if it has changed (e.g. if you have edited
58 ;; it). There is currently no perfect automatic solution to this.
60 ;; Netscape allows you to specify the id of the window you want to
61 ;; control but which window DO you want to control and how do you
62 ;; discover its id?
64 ;; If using XMosaic before version 2.5, check the definition of
65 ;; browse-url-usr1-signal below.
66 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/remote-control.html>
68 ;; XMosaic version 2.5 introduced Common Client Interface allowing you
69 ;; to control mosaic through Unix sockets.
70 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/CCI/cci-spec.html>
72 ;; William M. Perry's excellent "w3" WWW browser for
73 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
74 ;; has a function w3-follow-url-at-point, but that
75 ;; doesn't let you edit the URL like browse-url.
76 ;; The `gnuserv' package that can be used to control it in another
77 ;; Emacs process is available from
78 ;; <URL:http://hplbwww.hpl.hp.com/people/ange/gnuserv/>.
80 ;; Grail is the freely available WWW browser implemented in Python, a
81 ;; cool object-oriented freely available interpreted language. Grail
82 ;; 0.3b1 was the first version to have remote control as distributed.
83 ;; For more information on Grail see
84 ;; <URL:http://grail.cnri.reston.va.us/> and for more information on
85 ;; Python see <url:http://www.python.org/>. Grail support in
86 ;; browse-url.el written by Barry Warsaw <bwarsaw@python.org>.
88 ;; MMM is the freely available WWW browser implemented in Caml Special
89 ;; Light, a cool impure functional programming language, by Francois
90 ;; Rouaix. See the MMM home page
91 ;; <URL:http://pauillac.inria.fr/%7Erouaix/mmm/>.
93 ;; Lynx is now distributed by the FSF. See also
94 ;; <URL:http://lynx.browser.org/>.
96 ;; Free graphical browsers that could be used by `browse-url-generic'
97 ;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
98 ;; <URL:http://www.unlv.edu/chimera/>, Arena
99 ;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena>, Amaya
100 ;; <URL:ftp://ftp.w3.org/pub/amaya>, mMosaic
101 ;; <URL:ftp://sig.enst.fr/pub/multicast/mMosaic/> (the latter with
102 ;; development support for Java applets).
104 ;; I [Denis Howe] recommend Nelson Minar <nelson@santafe.edu>'s excellent
105 ;; html-helper-mode.el for editing HTML and thank Nelson for
106 ;; his many useful comments on this code.
107 ;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>
109 ;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
110 ;; software/own/hm--html-menus/>. For composing correct HTML see also
111 ;; PSGML the general SGML structure editor package
112 ;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
113 ;; with this.
115 ;; This package generalises function html-previewer-process in Marc
116 ;; Andreessen <marca@ncsa.uiuc.edu>'s html-mode (LCD
117 ;; modes/html-mode.el.Z) and provides better versions of the URL
118 ;; functions in Michelangelo Grigni <mic@cs.ucsd.edu>'s ffap.el
119 ;; (find-file-at-point) <URL:ftp://cs.ucsd.edu:/pub/mic/>. The huge
120 ;; hyperbole package also contains similar functions.
122 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 ;; Help!
125 ;; Can you write and test some code for the Macintrash and Windoze
126 ;; Netscape remote control APIs? (See the URL above).
128 ;; Do any other browsers have remote control?
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;; Usage
133 ;; To display the URL at or before point:
134 ;; M-x browse-url-at-point RET
135 ;; or, similarly but with the opportunity to edit the URL extracted from
136 ;; the buffer, use:
137 ;; M-x browse-url
139 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
140 ;; file:
141 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
142 ;; (Note that using Shift-mouse-1 is not desirable because
143 ;; that event has a standard meaning in Emacs.)
145 ;; To display the current buffer in a web browser:
146 ;; M-x browse-url-of-buffer RET
148 ;; To display the current region in a web browser:
149 ;; M-x browse-url-of-region RET
151 ;; In Dired, to display the file named on the current line:
152 ;; M-x browse-url-of-dired-file RET
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;; Customisation (~/.emacs)
157 ;; To see what variables are available for customization, type
158 ;; `M-x set-variable browse-url TAB'.
160 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
161 ;; (as used by html-helper-mode):
162 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
163 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
164 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
165 ;; (global-set-key "\C-c\C-zu" 'browse-url)
166 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
167 ;; (add-hook 'dired-mode-hook
168 ;; (function (lambda ()
169 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file))))
171 ;; Browse URLs in mail messages by clicking mouse-2:
172 ;; (add-hook 'rmail-mode-hook (function (lambda () ; rmail-mode startup
173 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse))))
175 ;; Browse URLs in Usenet messages by clicking mouse-2:
176 ;; (eval-after-load "gnus"
177 ;; '(define-key gnus-article-mode-map [mouse-2] 'browse-url-at-mouse))
178 ;; [The current version of Gnus provides a standard feature to
179 ;; activate URLs in article buffers for invocation of browse-url with
180 ;; mouse-2.]
182 ;; Use the Emacs w3 browser when not running under X11:
183 ;; (or (eq window-system 'x)
184 ;; (setq browse-url-browser-function 'browse-url-w3))
186 ;; To always save modified buffers before displaying the file in a browser:
187 ;; (setq browse-url-save-file t)
189 ;; To get round the Netscape caching problem, you could EITHER have
190 ;; write-file in html-helper-mode make Netscape reload the document:
192 ;; (autoload 'browse-url-netscape-reload "browse-url"
193 ;; "Ask a WWW browser to redisplay the current file." t)
194 ;; (add-hook 'html-helper-mode-hook
195 ;; (function (lambda ()
196 ;; (add-hook 'local-write-file-hooks
197 ;; (function (lambda ()
198 ;; (let ((local-write-file-hooks))
199 ;; (save-buffer))
200 ;; (browse-url-netscape-reload)
201 ;; t)) ; => file written by hook
202 ;; t)))) ; append to l-w-f-hooks
204 ;; OR have browse-url-of-file ask Netscape to load and then reload the
205 ;; file:
207 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
209 ;; You may also want to customise browse-url-netscape-arguments, e.g.
210 ;; (setq browse-url-netscape-arguments '("-install"))
212 ;; or similarly for the other browsers.
214 ;; To invoke different browsers for different URLs:
215 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
216 ;; ("." . browse-url-netscape)))
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;;; Code:
221 (eval-when-compile (require 'dired)
222 (require 'thingatpt))
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
225 ;; Variables
227 ;;;###autoload
228 (defgroup browse-url nil
229 "Use a web browser to look at a URL."
230 :group 'hypermedia)
232 ;;;###autoload
233 (defcustom browse-url-browser-function
234 'browse-url-netscape
235 "*Function to display the current buffer in a WWW browser.
236 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
237 `browse-url-of-file' commands.
239 If the value is not a function it should be a list of pairs
240 (REGEXP.FUNCTION). In this case the function called will be the one
241 associated with the first REGEXP which matches the current URL. The
242 function is passed the URL and any other args of `browse-url'. The last
243 regexp should probably be \".\" to specify a default browser."
244 :type 'function
245 :group 'browse-url)
247 (defcustom browse-url-netscape-program "netscape"
248 "*The name by which to invoke Netscape."
249 :type 'string
250 :group 'browse-url)
252 (defcustom browse-url-netscape-arguments nil
253 "*A list of strings to pass to Netscape as arguments."
254 :type '(repeat (string :tag "Argument"))
255 :group 'browse-url)
257 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
258 "*A list of strings to pass to Netscape when it starts up.
259 Defaults to the value of `browse-url-netscape-arguments' at the time
260 `browse-url' is loaded."
261 :type '(repeat (string :tag "Argument"))
262 :group 'browse-url)
264 (defcustom browse-url-new-window-p nil
265 "*If non-nil, always open a new browser window.
266 Passing an interactive argument to \\[browse-url-netscape] or
267 \\[browse-url-cci] reverses the effect of this variable. Requires
268 Netscape version 1.1N or later or XMosaic version 2.5 or later."
269 :type 'boolean
270 :group 'browse-url)
272 (defcustom browse-url-netscape-display nil
273 "*The X display for running Netscape, if not same as Emacs's."
274 :type 'string
275 :group 'browse-url)
277 (defcustom browse-url-mosaic-arguments nil
278 "*A list of strings to pass to Mosaic as arguments."
279 :type '(repeat (string :tag "Argument"))
280 :group 'browse-url)
282 (defvar browse-url-path-regexp
283 "[^]\t\n \"'()<>[^`{}]*[^]\t\n \"'()<>[^`{}.,;]+"
284 "A regular expression to match the host, path or e-mail part of a URL.")
286 (defvar browse-url-short-regexp
287 (concat "[-A-Za-z0-9.]+" browse-url-path-regexp)
288 "A regular expression probably matching a URL without an access scheme.
289 Hostname matching is stricter in this case than for
290 ``browse-url-regexp''.")
292 (defvar browse-url-regexp
293 (concat
294 "\\(https?://\\|ftp://\\|gopher://\\|telnet://\\|wais://\\|file:/\\|s?news:\\|mailto:\\)"
295 browse-url-path-regexp)
296 "A regular expression probably matching a complete URL.")
298 (defvar browse-url-markedup-regexp
299 "<URL:[^>]+>"
300 "A regular expression matching a URL marked up per RFC1738.
301 This may be broken across lines.")
303 (defvar browse-url-filename-alist
304 '(("^/+" . "file:/"))
305 "An alist of (REGEXP . STRING) pairs.
306 Any substring of a filename matching one of the REGEXPs is replaced by
307 the corresponding STRING. All pairs are applied in the order given.
308 The default value prepends `file:' to any path beginning with `/'.
309 Used by the `browse-url-of-file' command.
311 For example, to map EFS filenames to URLs:
313 (setq browse-url-filename-alist
314 '((\"/webmaster@webserver:/home/www/html/\" .
315 \"http://www.acme.co.uk/\")
316 (\"^/+\" . \"file:/\")))
319 (defvar browse-url-save-file nil
320 "If non-nil, save the buffer before displaying its file.
321 Used by the `browse-url-of-file' command.")
323 (defvar browse-url-of-file-hook nil
324 "A hook to be run with run-hook after `browse-url-of-file' has asked
325 a browser to load a file.
327 Set this to `browse-url-netscape-reload' to force Netscape to load the
328 file rather than displaying a cached copy.")
330 (defvar browse-url-usr1-signal
331 (if (and (boundp 'emacs-major-version)
332 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
333 'SIGUSR1 ; Why did I think this was in lower case before?
334 30) ; Check /usr/include/signal.h.
335 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
336 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
337 which is 30 on SunOS and 16 on HP-UX and Solaris.")
339 (defvar browse-url-CCI-port 3003
340 "Port to access XMosaic via CCI.
341 This can be any number between 1024 and 65535 but must correspond to
342 the value set in the browser.")
344 (defvar browse-url-CCI-host "localhost"
345 "*Host to access XMosaic via CCI.
346 This should be the host name of the machine running XMosaic with CCI
347 enabled. The port number should be set in `browse-url-CCI-port'.")
349 (defvar browse-url-temp-file-name nil)
350 (make-variable-buffer-local 'browse-url-temp-file-name)
352 (defcustom browse-url-xterm-program "xterm"
353 "*The name of the terminal emulator used by `browse-url-lynx-xterm'.
354 This might, for instance, be a separate colour version of xterm."
355 :type 'string
356 :group 'browse-url)
358 (defcustom browse-url-xterm-args nil
359 "*A list of strings defining options for `browse-url-xterm-program'.
360 These might set its size, for instance."
361 :type '(repeat (string :tag "Argument"))
362 :group 'browse-url)
364 (defcustom browse-url-gnudoit-program "gnudoit"
365 "*The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
366 :type 'string
367 :group 'browse-url)
369 (defcustom browse-url-gnudoit-args '("-q")
370 "*A list of strings defining options for `browse-url-gnudoit-program'.
371 These might set the port, for instance."
372 :type '(repeat (string :tag "Argument"))
373 :group 'browse-url)
375 (defcustom browse-url-generic-program nil
376 "*The name of the browser program used by `browse-url-generic'."
377 :type 'string
378 :group 'browse-url)
380 (defcustom browse-url-generic-args nil
381 "*A list of strings defining options for `browse-url-generic-program'."
382 :type '(repeat (string :tag "Argument"))
383 :group 'browse-url)
385 (defcustom browse-url-temp-dir
386 (or (getenv "TMPDIR") "/tmp")
387 "*The name of a directory in which to store temporary files
388 generated by functions like `browse-url-of-region'. You might want to
389 set this to somewhere with restricted read permissions for privacy's sake."
390 :type 'string
391 :group 'browse-url)
393 (defcustom browse-url-netscape-version
395 "*The version of Netscape you are using.
396 This affects how URL reloading is done; the mechanism changed
397 incompatibly at version 4."
398 :type 'number
399 :group 'browse-url)
401 (defvar browse-url-temp-file-list '())
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
404 ;; URL input
406 (defun browse-url-url-at-point ()
407 (let ((url (thing-at-point 'url)))
408 (set-text-properties 0 (length url) nil url)
409 url))
411 ;; Having this as a separate function called by the browser-specific
412 ;; functions allows them to be stand-alone commands, making it easier
413 ;; to switch between browsers.
415 (defun browse-url-interactive-arg (prompt)
416 "Read a URL from the minibuffer, prompting with PROMPT.
417 Default to the URL at or before point. If invoked with a mouse button,
418 set point to the position clicked first. Return a list for use in
419 `interactive' containing the URL and `browse-url-new-window-p' or its
420 negation if a prefix argument was given."
421 (let ((event (elt (this-command-keys) 0)))
422 (and (listp event) (mouse-set-point event)))
423 (list (read-string prompt (browse-url-url-at-point))
424 (not (eq (null browse-url-new-window-p)
425 (null current-prefix-arg)))))
427 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
428 ;; Browse current buffer
430 ;;;###autoload
431 (defun browse-url-of-file (&optional file)
432 "Ask a WWW browser to display FILE.
433 Display the current buffer's file if FILE is nil or if called
434 interactively. Turn the filename into a URL with function
435 `browse-url-file-url'. Pass the URL to a browser using the
436 `browse-url' function then run `browse-url-of-file-hook'."
437 (interactive)
438 (or file
439 (setq file (buffer-file-name))
440 (error "Current buffer has no file"))
441 (let ((buf (get-file-buffer file)))
442 (if buf
443 (save-excursion
444 (set-buffer buf)
445 (cond ((not (buffer-modified-p)))
446 (browse-url-save-file (save-buffer))
447 (t (message "%s modified since last save" file))))))
448 (browse-url (browse-url-file-url file))
449 (run-hooks 'browse-url-of-file-hook))
451 (defun browse-url-file-url (file)
452 "Return the URL corresponding to FILE.
453 Use variable `browse-url-filename-alist' to map filenames to URLs.
454 Convert EFS file names of the form /USER@HOST:PATH to ftp://HOST/PATH."
455 ;; URL-encode special chars, do % first
456 (let ((s 0))
457 (while (setq s (string-match "%" file s))
458 (setq file (replace-match "%25" t t file)
459 s (1+ s))))
460 (while (string-match "[*\"()',=;? ]" file)
461 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
462 (setq file (replace-match enc t t file))))
463 (let ((maps browse-url-filename-alist))
464 (while maps
465 (let* ((map (car maps))
466 (from-re (car map))
467 (to-string (cdr map)))
468 (setq maps (cdr maps))
469 (and (string-match from-re file)
470 (setq file (replace-match to-string t t file))))))
471 ;; Check for EFS path
472 (and (string-match "^/\\([^:@]+@\\)?\\([^:]+\\):/*" file)
473 (setq file (concat "ftp://"
474 (substring file (match-beginning 2) (match-end 2))
475 "/" (substring file (match-end 0)))))
476 file)
478 ;;;###autoload
479 (defun browse-url-of-buffer (&optional buffer)
480 "Ask a WWW browser to display BUFFER.
481 Display the current buffer if BUFFER is nil. Display only the
482 currently visible part of BUFFER (from a temporary file) if buffer is
483 narrowed."
484 (interactive)
485 (save-excursion
486 (and buffer (set-buffer buffer))
487 (let ((file-name
488 ;; Ignore real name if restricted
489 (and (= (- (point-max) (point-min)) (buffer-size))
490 (or buffer-file-name
491 (and (boundp 'dired-directory) dired-directory)))))
492 (or file-name
493 (progn
494 (or browse-url-temp-file-name
495 (setq browse-url-temp-file-name
496 (convert-standard-filename
497 (make-temp-name
498 (expand-file-name "burl" browse-url-temp-dir)))))
499 (setq file-name browse-url-temp-file-name)
500 (write-region (point-min) (point-max) file-name nil 'no-message)))
501 (browse-url-of-file file-name))))
503 (defun browse-url-delete-temp-file (&optional temp-file-name)
504 ;; Delete browse-url-temp-file-name from the file system and from
505 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
506 ;; non-nil, delete it instead, but only from the file system --
507 ;; browse-url-temp-file-list is not affected.
508 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
509 (if (and file-name (file-exists-p file-name))
510 (progn
511 (delete-file file-name)
512 (if (null temp-file-name)
513 (setq browse-url-temp-file-list
514 (delete browse-url-temp-file-name
515 browse-url-temp-file-list)))))))
517 (defun browse-url-delete-temp-file-list ()
518 ;; Delete all elements of browse-url-temp-file-list.
519 (while browse-url-temp-file-list
520 (browse-url-delete-temp-file (car browse-url-temp-file-list))
521 (setq browse-url-temp-file-list
522 (cdr browse-url-temp-file-list))))
524 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
525 (add-hook 'kill-emacs-hook 'browse-url-delete-temp-file-list)
527 ;;;###autoload
528 (defun browse-url-of-dired-file ()
529 "In Dired, ask a WWW browser to display the file named on this line."
530 (interactive)
531 (browse-url-of-file (dired-get-filename)))
533 ;;;###autoload
534 (defun browse-url-of-region (min max)
535 "Ask a WWW browser to display the current region."
536 (interactive "r")
537 (save-excursion
538 (save-restriction
539 (narrow-to-region (mark) (point))
540 (browse-url-of-buffer))))
542 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
543 ;; Browser-independent commands
545 ;; A generic command to call the current browse-url-browser-function
547 ;;;###autoload
548 (defun browse-url (&rest args)
549 "Ask a WWW browser to load URL.
550 Prompts for a URL, defaulting to the URL at or before point. Variable
551 `browse-url-browser-function' says which browser to use."
552 (interactive (browse-url-interactive-arg "URL: "))
553 (if (consp browse-url-browser-function)
554 (browse-url-choose-browser args)
555 (apply browse-url-browser-function args)))
557 (defun browse-url-choose-browser (url &rest args)
558 "Pass URL to a browser function chosen.
559 This is done according to the association list in variable
560 `browse-url-browser-function'."
561 (let ((blist browse-url-browser-function)
562 re bf)
563 (while (consp blist)
564 (setq re (car (car blist))
565 bf (cdr (car blist))
566 blist (cdr blist))
567 (if (string-match re url)
568 (progn (apply bf url args) (setq blist t))))
569 (or blist
570 (error "No browser in browse-url-browser-function matching URL %s" url))))
572 ;;;###autoload
573 (defun browse-url-at-point ()
574 "Ask a WWW browser to load the URL at or before point.
575 Doesn't let you edit the URL like browse-url. Variable
576 `browse-url-browser-function' says which browser to use."
577 (interactive)
578 (browse-url (browse-url-url-at-point)))
580 (defun browse-url-event-buffer (event)
581 (window-buffer (posn-window (event-start event))))
583 (defun browse-url-event-point (event)
584 (posn-point (event-start event)))
586 ;;;###autoload
587 (defun browse-url-at-mouse (event)
588 "Ask a WWW browser to load a URL clicked with the mouse.
589 The URL is the one around or before the position of the mouse click
590 but point is not changed. Doesn't let you edit the URL like
591 browse-url. Variable `browse-url-browser-function' says which browser
592 to use."
593 (interactive "e")
594 (save-excursion
595 (set-buffer (browse-url-event-buffer event))
596 (goto-char (browse-url-event-point event))
597 (let ((url (browse-url-url-at-point)))
598 (if (string-equal url "")
599 (error "No URL found"))
600 (browse-url url))))
602 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
603 ;; Browser-specific commands
605 ;; --- Netscape ---
607 (defun browse-url-process-environment ()
608 "Set DISPLAY in the environment to the X display Netscape is running on.
609 This is either the value of variable `browse-url-netscape-display' if
610 non-nil, or the same display as Emacs if different from the current
611 environment, otherwise just use the current environment."
612 (let ((display (or browse-url-netscape-display (browse-url-emacs-display))))
613 (if display
614 (cons (concat "DISPLAY=" display) process-environment)
615 process-environment)))
617 (defun browse-url-emacs-display ()
618 "Return the X display Emacs is running on.
619 This nil if the display is the same as the DISPLAY environment variable.
621 Actually Emacs could be using several screens on several displays, as
622 listed by (emacs-display-list) and (x-display-screens DISPLAY), this
623 just returns the display showing the selected frame. You got a
624 problem with that?"
625 (let (device display)
626 (and (fboundp 'selected-device) (fboundp 'device-type) (fboundp 'device-connection)
627 (setq device (selected-device))
628 (eq (device-type device) 'x)
629 (setq display (device-connection device))
630 (not (equal display (getenv "DISPLAY")))
631 display)))
633 ;;;###autoload
634 (defun browse-url-netscape (url &optional new-window)
635 "Ask the Netscape WWW browser to load URL.
637 Default to the URL around or before point. The strings in variable
638 `browse-url-netscape-arguments' are also passed to Netscape.
640 When called interactively, if variable `browse-url-new-window-p' is
641 non-nil, load the document in a new Netscape window, otherwise use a
642 random existing one. A non-nil interactive prefix argument reverses
643 the effect of `browse-url-new-window-p'.
645 When called non-interactively, optional second argument NEW-WINDOW is
646 used instead of `browse-url-new-window-p'."
647 (interactive (browse-url-interactive-arg "Netscape URL: "))
648 ;; URL encode any commas in the URL
649 (while (string-match "," url)
650 (setq url (replace-match "%2C" t t url)))
651 (let* ((process-environment (browse-url-process-environment))
652 (process (apply 'start-process
653 (concat "netscape " url) nil
654 browse-url-netscape-program
655 (append browse-url-netscape-arguments
656 (if (eq window-system 'w32)
657 (list url)
658 (if new-window '("-noraise"))
659 (list "-remote"
660 (concat "openURL(" url
661 (if new-window ",new-window")
662 ")")))))))
663 (set-process-sentinel process
664 (list 'lambda '(process change)
665 (list 'browse-url-netscape-sentinel 'process url)))))
667 (defun browse-url-netscape-sentinel (process url)
668 "Handle a change to the process communicating with Netscape."
669 (or (eq (process-exit-status process) 0)
670 (let* ((process-environment (browse-url-process-environment)))
671 ;; Netscape not running - start it
672 (message "Starting Netscape...")
673 (apply 'start-process (concat "netscape" url) nil
674 browse-url-netscape-program
675 (append browse-url-netscape-startup-arguments (list url))))))
677 (defun browse-url-netscape-reload ()
678 "Ask Netscape to reload its current document."
679 (interactive)
680 ;; Backwards incompatibility reported by
681 ;; <peter.kruse@psychologie.uni-regensburg.de>.
682 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
683 "xfeDoCommand(reload)"
684 "reload")))
686 (defun browse-url-netscape-send (command)
687 "Send a remote control command to Netscape."
688 (let* ((process-environment (browse-url-process-environment)))
689 (apply 'start-process "netscape" nil
690 browse-url-netscape-program
691 (append browse-url-netscape-arguments
692 (list "-remote" command)))))
694 ;; --- Mosaic ---
696 ;;;###autoload
697 (defun browse-url-mosaic (url &optional new-window)
698 ;; new-window ignored
699 "Ask the XMosaic WWW browser to load URL.
700 Default to the URL around or before point."
701 (interactive (browse-url-interactive-arg "Mosaic URL: "))
702 (let ((pidfile (expand-file-name "~/.mosaicpid"))
703 pid pidbuf)
704 (if (file-readable-p pidfile)
705 (save-excursion
706 (find-file pidfile)
707 (goto-char (point-min))
708 (setq pid (read (current-buffer)))
709 (kill-buffer nil)))
710 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
711 (save-excursion
712 (find-file (format "/tmp/Mosaic.%d" pid))
713 (erase-buffer)
714 (insert "goto\n" url "\n")
715 (save-buffer)
716 (kill-buffer nil)
717 ;; Send signal SIGUSR to Mosaic
718 (message "Signalling Mosaic...")
719 (signal-process pid browse-url-usr1-signal)
720 ;; Or you could try:
721 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
722 (message "Signalling Mosaic...done")
724 ;; Mosaic not running - start it
725 (message "Starting Mosaic...")
726 (apply 'start-process "xmosaic" nil "xmosaic"
727 (append browse-url-mosaic-arguments (list url)))
728 (message "Starting Mosaic...done"))))
730 ;; --- Grail ---
732 ;;;###autoload
733 (defvar browse-url-grail
734 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
735 "*Location of Grail remote control client script `rcgrail.py'.
736 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
738 ;;;###autoload
739 (defun browse-url-grail (url &optional new-window)
740 "Ask the Grail WWW browser to load URL.
741 Default to the URL around or before point. Runs the program in the
742 variable `browse-url-grail'."
743 (interactive (browse-url-interactive-arg "Grail URL: "))
744 (message "Sending URL to Grail...")
745 (save-excursion
746 (set-buffer (get-buffer-create " *Shell Command Output*"))
747 (erase-buffer)
748 ;; don't worry about this failing.
749 (call-process browse-url-grail nil 0 nil url)
750 (message "Sending URL to Grail... done")))
752 ;; --- Mosaic using CCI ---
754 (defun browse-url-cci (url &optional new-window)
755 "Ask the XMosaic WWW browser to load URL.
756 Default to the URL around or before point.
758 This function only works for XMosaic version 2.5 or later. You must
759 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
760 value of variable `browse-url-CCI-port', and enable `Accept requests'.
762 When called interactively, if variable `browse-url-new-window-p' is
763 non-nil, load the document in a new browser window, otherwise use a
764 random existing one. A non-nil interactive prefix argument reverses
765 the effect of `browse-url-new-window-p'.
767 When called non-interactively, optional second argument NEW-WINDOW is
768 used instead of `browse-url-new-window-p'."
769 (interactive (browse-url-interactive-arg "Mosaic URL: "))
770 (open-network-stream "browse-url" " *browse-url*"
771 browse-url-CCI-host browse-url-CCI-port)
772 ;; Todo: start browser if fails
773 (process-send-string "browse-url"
774 (concat "get url (" url ") output "
775 (if new-window "new" "current") "\r\n"))
776 (process-send-string "browse-url" "disconnect\r\n")
777 (delete-process "browse-url"))
779 ;; --- IXI Mosaic ---
781 ;;;###autoload
782 (defun browse-url-iximosaic (url &optional new-window)
783 ;; new-window ignored
784 "Ask the IXIMosaic WWW browser to load URL.
785 Default to the URL around or before point."
786 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
787 (start-process "tellw3b" nil "tellw3b"
788 "-service WWW_BROWSER ixi_showurl " url))
790 ;; --- W3 ---
792 ;;;###autoload
793 (defun browse-url-w3 (url &optional new-window)
794 ;; new-window ignored
795 "Ask the w3 WWW browser to load URL.
796 Default to the URL around or before point."
797 (interactive (browse-url-interactive-arg "W3 URL: "))
798 (w3-fetch url))
800 ;;;###autoload
801 (defun browse-url-w3-gnudoit (url &optional new-window)
802 ;; new-window ignored
803 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
804 The `browse-url-gnudoit-program' program is used with options given by
805 `browse-url-gnudoit-args'. Default to the URL around or before point."
806 (interactive (browse-url-interactive-arg "W3 URL: "))
807 (apply 'start-process (concat "gnudoit:" url) nil
808 browse-url-gnudoit-program
809 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
811 ;; --- Lynx in an xterm ---
813 ;;;###autoload
814 (defun browse-url-lynx-xterm (url &optional new-window)
815 ;; new-window ignored
816 "Ask the Lynx WWW browser to load URL.
817 Default to the URL around or before point. A new Lynx process is run
818 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
819 with possible additional arguments `browse-url-xterm-args'."
820 (interactive (browse-url-interactive-arg "Lynx URL: "))
821 (apply 'start-process (concat "lynx" url) nil browse-url-xterm-program
822 (append browse-url-xterm-args (list "-e" "lynx" url))))
824 ;; --- Lynx in an Emacs "term" window ---
826 ;;;###autoload
827 (defun browse-url-lynx-emacs (url &optional new-window)
828 ;; new-window ignored
829 "Ask the Lynx WWW browser to load URL.
830 Default to the URL around or before point. Run a new Lynx process in
831 an Emacs buffer."
832 (interactive (browse-url-interactive-arg "Lynx URL: "))
833 (let ((system-uses-terminfo t)) ; Lynx uses terminfo
834 (if (fboundp 'make-term)
835 (let ((term-term-name "vt100"))
836 (set-buffer (make-term "browse-url" "lynx" nil url))
837 (term-mode)
838 (term-char-mode)
839 (switch-to-buffer "*browse-url*"))
840 (terminal-emulator "*browse-url*" "lynx" (list url)))))
842 ;; --- MMM ---
844 ;;;###autoload
845 (defun browse-url-mmm (url &optional new-window)
846 "Ask the MMM WWW browser to load URL.
847 Default to the URL around or before point."
848 (interactive (browse-url-interactive-arg "MMM URL: "))
849 (message "Sending URL to MMM...")
850 (save-excursion
851 (set-buffer (get-buffer-create " *Shell Command Output*"))
852 (erase-buffer)
853 ;; mmm_remote just SEGVs if the file isn't there...
854 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
855 ;; location in v 0.4:
856 (file-exists-p (expand-file-name "~/.mmm/remote")))
857 (call-process "mmm_remote" nil 0 nil url)
858 (call-process "mmm" nil 0 nil "-external" url))
859 (message "Sending URL to MMM... done")))
861 ;; --- mailto ---
863 ;;;###autoload
864 (defun browse-url-mail (url &optional new-window)
865 "Open a new mail message buffer within Emacs.
866 Default to using the mailto: URL around or before point as the
867 recipient's address. Supplying a non-nil interactive prefix argument
868 will cause the mail to be composed in another window rather than the
869 current one."
870 (interactive (browse-url-interactive-arg "Mailto URL: "))
871 (save-excursion
872 (let ((func (if new-window
873 'compose-mail-other-window
874 'compose-mail))
875 (to (if (string-match "^mailto:" url)
876 (substring url 7)
877 url)))
878 (apply func
879 (list to nil nil nil nil nil (cons 'insert-buffer
880 (current-buffer)))))))
882 ;; --- Random browser ---
884 ;;;###autoload
885 (defun browse-url-generic (url &optional new-window)
886 ;; new-window ignored
887 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
888 Default to the URL around or before point. A fresh copy of the
889 browser is started up in a new process with possible additional arguments
890 `browse-url-generic-args'. This is appropriate for browsers which
891 don't offer a form of remote control."
892 (interactive (browse-url-interactive-arg "URL: "))
893 (if (not browse-url-generic-program)
894 (error "No browser defined (`browse-url-generic-program')"))
895 (apply 'start-process (concat browse-url-generic-program url) nil
896 browse-url-generic-program
897 (append browse-url-generic-args (list url))))
899 (provide 'browse-url)
901 ;;; browse-url.el ends here