(lisp-imenu-generic-expression): Recognize`defcustom' and `defgroup".
[emacs.git] / lisp / browse-url.el
blob03a8d4888b7cfd585747251bf139e71083bbd6b5
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 (defgroup browse-url nil
228 "Use a web browser to look at a URL."
229 :group 'hypermedia)
231 ;;;###autoload
232 (defcustom browse-url-browser-function
233 'browse-url-netscape
234 "*Function to display the current buffer in a WWW browser.
235 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
236 `browse-url-of-file' commands.
238 If the value is not a function it should be a list of pairs
239 (REGEXP.FUNCTION). In this case the function called will be the one
240 associated with the first REGEXP which matches the current URL. The
241 function is passed the URL and any other args of `browse-url'. The last
242 regexp should probably be \".\" to specify a default browser."
243 :type 'function
244 :group 'browse-url)
246 (defcustom browse-url-netscape-program "netscape"
247 "*The name by which to invoke Netscape."
248 :type 'string
249 :group 'browse-url)
251 (defcustom browse-url-netscape-arguments nil
252 "*A list of strings to pass to Netscape as arguments."
253 :type '(repeat (string :tag "Argument"))
254 :group 'browse-url)
256 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
257 "*A list of strings to pass to Netscape when it starts up.
258 Defaults to the value of `browse-url-netscape-arguments' at the time
259 `browse-url' is loaded."
260 :type '(repeat (string :tag "Argument"))
261 :group 'browse-url)
263 (defcustom browse-url-new-window-p nil
264 "*If non-nil, always open a new browser window.
265 Passing an interactive argument to \\[browse-url-netscape] or
266 \\[browse-url-cci] reverses the effect of this variable. Requires
267 Netscape version 1.1N or later or XMosaic version 2.5 or later."
268 :type 'boolean
269 :group 'browse-url)
271 (defcustom browse-url-netscape-display nil
272 "*The X display for running Netscape, if not same as Emacs's."
273 :type 'string
274 :group 'browse-url)
276 (defcustom browse-url-mosaic-arguments nil
277 "*A list of strings to pass to Mosaic as arguments."
278 :type '(repeat (string :tag "Argument"))
279 :group 'browse-url)
281 (defvar browse-url-path-regexp
282 "[^]\t\n \"'()<>[^`{}]*[^]\t\n \"'()<>[^`{}.,;]+"
283 "A regular expression to match the host, path or e-mail part of a URL.")
285 (defvar browse-url-short-regexp
286 (concat "[-A-Za-z0-9.]+" browse-url-path-regexp)
287 "A regular expression probably matching a URL without an access scheme.
288 Hostname matching is stricter in this case than for
289 ``browse-url-regexp''.")
291 (defvar browse-url-regexp
292 (concat
293 "\\(https?://\\|ftp://\\|gopher://\\|telnet://\\|wais://\\|file:/\\|s?news:\\|mailto:\\)"
294 browse-url-path-regexp)
295 "A regular expression probably matching a complete URL.")
297 (defvar browse-url-markedup-regexp
298 "<URL:[^>]+>"
299 "A regular expression matching a URL marked up per RFC1738.
300 This may be broken across lines.")
302 (defvar browse-url-filename-alist
303 '(("^/+" . "file:/"))
304 "An alist of (REGEXP . STRING) pairs.
305 Any substring of a filename matching one of the REGEXPs is replaced by
306 the corresponding STRING. All pairs are applied in the order given.
307 The default value prepends `file:' to any path beginning with `/'.
308 Used by the `browse-url-of-file' command.
310 For example, to map EFS filenames to URLs:
312 (setq browse-url-filename-alist
313 '((\"/webmaster@webserver:/home/www/html/\" .
314 \"http://www.acme.co.uk/\")
315 (\"^/+\" . \"file:/\")))
318 (defvar browse-url-save-file nil
319 "If non-nil, save the buffer before displaying its file.
320 Used by the `browse-url-of-file' command.")
322 (defvar browse-url-of-file-hook nil
323 "A hook to be run with run-hook after `browse-url-of-file' has asked
324 a browser to load a file.
326 Set this to `browse-url-netscape-reload' to force Netscape to load the
327 file rather than displaying a cached copy.")
329 (defvar browse-url-usr1-signal
330 (if (and (boundp 'emacs-major-version)
331 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
332 'SIGUSR1 ; Why did I think this was in lower case before?
333 30) ; Check /usr/include/signal.h.
334 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
335 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
336 which is 30 on SunOS and 16 on HP-UX and Solaris.")
338 (defvar browse-url-CCI-port 3003
339 "Port to access XMosaic via CCI.
340 This can be any number between 1024 and 65535 but must correspond to
341 the value set in the browser.")
343 (defvar browse-url-CCI-host "localhost"
344 "*Host to access XMosaic via CCI.
345 This should be the host name of the machine running XMosaic with CCI
346 enabled. The port number should be set in `browse-url-CCI-port'.")
348 (defvar browse-url-temp-file-name nil)
349 (make-variable-buffer-local 'browse-url-temp-file-name)
351 (defcustom browse-url-xterm-program "xterm"
352 "*The name of the terminal emulator used by `browse-url-lynx-xterm'.
353 This might, for instance, be a separate colour version of xterm."
354 :type 'string
355 :group 'browse-url)
357 (defcustom browse-url-xterm-args nil
358 "*A list of strings defining options for `browse-url-xterm-program'.
359 These might set its size, for instance."
360 :type '(repeat (string :tag "Argument"))
361 :group 'browse-url)
363 (defcustom browse-url-gnudoit-program "gnudoit"
364 "*The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
365 :type 'string
366 :group 'browse-url)
368 (defcustom browse-url-gnudoit-args '("-q")
369 "*A list of strings defining options for `browse-url-gnudoit-program'.
370 These might set the port, for instance."
371 :type '(repeat (string :tag "Argument"))
372 :group 'browse-url)
374 (defcustom browse-url-generic-program nil
375 "*The name of the browser program used by `browse-url-generic'."
376 :type 'string
377 :group 'browse-url)
379 (defcustom browse-url-generic-args nil
380 "*A list of strings defining options for `browse-url-generic-program'."
381 :type '(repeat (string :tag "Argument"))
382 :group 'browse-url)
384 (defcustom browse-url-temp-dir
385 (or (getenv "TMPDIR") "/tmp")
386 "*The name of a directory in which to store temporary files
387 generated by functions like `browse-url-of-region'. You might want to
388 set this to somewhere with restricted read permissions for privacy's sake."
389 :type 'string
390 :group 'browse-url)
392 (defcustom browse-url-netscape-version
394 "*The version of Netscape you are using.
395 This affects how URL reloading is done; the mechanism changed
396 incompatibly at version 4."
397 :type 'number
398 :group 'browse-url)
400 (defvar browse-url-temp-file-list '())
402 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
403 ;; URL input
405 (defun browse-url-url-at-point ()
406 (let ((url (thing-at-point 'url)))
407 (set-text-properties 0 (length url) nil url)
408 url))
410 ;; Having this as a separate function called by the browser-specific
411 ;; functions allows them to be stand-alone commands, making it easier
412 ;; to switch between browsers.
414 (defun browse-url-interactive-arg (prompt)
415 "Read a URL from the minibuffer, prompting with PROMPT.
416 Default to the URL at or before point. If invoked with a mouse button,
417 set point to the position clicked first. Return a list for use in
418 `interactive' containing the URL and `browse-url-new-window-p' or its
419 negation if a prefix argument was given."
420 (let ((event (elt (this-command-keys) 0)))
421 (and (listp event) (mouse-set-point event)))
422 (list (read-string prompt (browse-url-url-at-point))
423 (not (eq (null browse-url-new-window-p)
424 (null current-prefix-arg)))))
426 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
427 ;; Browse current buffer
429 ;;;###autoload
430 (defun browse-url-of-file (&optional file)
431 "Ask a WWW browser to display FILE.
432 Display the current buffer's file if FILE is nil or if called
433 interactively. Turn the filename into a URL with function
434 `browse-url-file-url'. Pass the URL to a browser using the
435 `browse-url' function then run `browse-url-of-file-hook'."
436 (interactive)
437 (or file
438 (setq file (buffer-file-name))
439 (error "Current buffer has no file"))
440 (let ((buf (get-file-buffer file)))
441 (if buf
442 (save-excursion
443 (set-buffer buf)
444 (cond ((not (buffer-modified-p)))
445 (browse-url-save-file (save-buffer))
446 (t (message "%s modified since last save" file))))))
447 (browse-url (browse-url-file-url file))
448 (run-hooks 'browse-url-of-file-hook))
450 (defun browse-url-file-url (file)
451 "Return the URL corresponding to FILE.
452 Use variable `browse-url-filename-alist' to map filenames to URLs.
453 Convert EFS file names of the form /USER@HOST:PATH to ftp://HOST/PATH."
454 ;; URL-encode special chars, do % first
455 (let ((s 0))
456 (while (setq s (string-match "%" file s))
457 (setq file (replace-match "%25" t t file)
458 s (1+ s))))
459 (while (string-match "[*\"()',=;? ]" file)
460 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
461 (setq file (replace-match enc t t file))))
462 (let ((maps browse-url-filename-alist))
463 (while maps
464 (let* ((map (car maps))
465 (from-re (car map))
466 (to-string (cdr map)))
467 (setq maps (cdr maps))
468 (and (string-match from-re file)
469 (setq file (replace-match to-string t t file))))))
470 ;; Check for EFS path
471 (and (string-match "^/\\([^:@]+@\\)?\\([^:]+\\):/*" file)
472 (setq file (concat "ftp://"
473 (substring file (match-beginning 2) (match-end 2))
474 "/" (substring file (match-end 0)))))
475 file)
477 ;;;###autoload
478 (defun browse-url-of-buffer (&optional buffer)
479 "Ask a WWW browser to display BUFFER.
480 Display the current buffer if BUFFER is nil. Display only the
481 currently visible part of BUFFER (from a temporary file) if buffer is
482 narrowed."
483 (interactive)
484 (save-excursion
485 (and buffer (set-buffer buffer))
486 (let ((file-name
487 ;; Ignore real name if restricted
488 (and (= (- (point-max) (point-min)) (buffer-size))
489 (or buffer-file-name
490 (and (boundp 'dired-directory) dired-directory)))))
491 (or file-name
492 (progn
493 (or browse-url-temp-file-name
494 (setq browse-url-temp-file-name
495 (convert-standard-filename
496 (make-temp-name
497 (expand-file-name "burl" browse-url-temp-dir)))))
498 (setq file-name browse-url-temp-file-name)
499 (write-region (point-min) (point-max) file-name nil 'no-message)))
500 (browse-url-of-file file-name))))
502 (defun browse-url-delete-temp-file (&optional temp-file-name)
503 ;; Delete browse-url-temp-file-name from the file system and from
504 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
505 ;; non-nil, delete it instead, but only from the file system --
506 ;; browse-url-temp-file-list is not affected.
507 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
508 (if (and file-name (file-exists-p file-name))
509 (progn
510 (delete-file file-name)
511 (if (null temp-file-name)
512 (setq browse-url-temp-file-list
513 (delete browse-url-temp-file-name
514 browse-url-temp-file-list)))))))
516 (defun browse-url-delete-temp-file-list ()
517 ;; Delete all elements of browse-url-temp-file-list.
518 (while browse-url-temp-file-list
519 (browse-url-delete-temp-file (car browse-url-temp-file-list))
520 (setq browse-url-temp-file-list
521 (cdr browse-url-temp-file-list))))
523 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
524 (add-hook 'kill-emacs-hook 'browse-url-delete-temp-file-list)
526 ;;;###autoload
527 (defun browse-url-of-dired-file ()
528 "In Dired, ask a WWW browser to display the file named on this line."
529 (interactive)
530 (browse-url-of-file (dired-get-filename)))
532 ;;;###autoload
533 (defun browse-url-of-region (min max)
534 "Ask a WWW browser to display the current region."
535 (interactive "r")
536 (save-excursion
537 (save-restriction
538 (narrow-to-region (mark) (point))
539 (browse-url-of-buffer))))
541 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
542 ;; Browser-independent commands
544 ;; A generic command to call the current browse-url-browser-function
546 ;;;###autoload
547 (defun browse-url (&rest args)
548 "Ask a WWW browser to load URL.
549 Prompts for a URL, defaulting to the URL at or before point. Variable
550 `browse-url-browser-function' says which browser to use."
551 (interactive (browse-url-interactive-arg "URL: "))
552 (if (consp browse-url-browser-function)
553 (browse-url-choose-browser args)
554 (apply browse-url-browser-function args)))
556 (defun browse-url-choose-browser (url &rest args)
557 "Pass URL to a browser function chosen.
558 This is done according to the association list in variable
559 `browse-url-browser-function'."
560 (let ((blist browse-url-browser-function)
561 re bf)
562 (while (consp blist)
563 (setq re (car (car blist))
564 bf (cdr (car blist))
565 blist (cdr blist))
566 (if (string-match re url)
567 (progn (apply bf url args) (setq blist t))))
568 (or blist
569 (error "No browser in browse-url-browser-function matching URL %s" url))))
571 ;;;###autoload
572 (defun browse-url-at-point ()
573 "Ask a WWW browser to load the URL at or before point.
574 Doesn't let you edit the URL like browse-url. Variable
575 `browse-url-browser-function' says which browser to use."
576 (interactive)
577 (browse-url (browse-url-url-at-point)))
579 (defun browse-url-event-buffer (event)
580 (window-buffer (posn-window (event-start event))))
582 (defun browse-url-event-point (event)
583 (posn-point (event-start event)))
585 ;;;###autoload
586 (defun browse-url-at-mouse (event)
587 "Ask a WWW browser to load a URL clicked with the mouse.
588 The URL is the one around or before the position of the mouse click
589 but point is not changed. Doesn't let you edit the URL like
590 browse-url. Variable `browse-url-browser-function' says which browser
591 to use."
592 (interactive "e")
593 (save-excursion
594 (set-buffer (browse-url-event-buffer event))
595 (goto-char (browse-url-event-point event))
596 (let ((url (browse-url-url-at-point)))
597 (if (string-equal url "")
598 (error "No URL found"))
599 (browse-url url))))
601 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
602 ;; Browser-specific commands
604 ;; --- Netscape ---
606 (defun browse-url-process-environment ()
607 "Set DISPLAY in the environment to the X display Netscape is running on.
608 This is either the value of variable `browse-url-netscape-display' if
609 non-nil, or the same display as Emacs if different from the current
610 environment, otherwise just use the current environment."
611 (let ((display (or browse-url-netscape-display (browse-url-emacs-display))))
612 (if display
613 (cons (concat "DISPLAY=" display) process-environment)
614 process-environment)))
616 (defun browse-url-emacs-display ()
617 "Return the X display Emacs is running on.
618 This nil if the display is the same as the DISPLAY environment variable.
620 Actually Emacs could be using several screens on several displays, as
621 listed by (emacs-display-list) and (x-display-screens DISPLAY), this
622 just returns the display showing the selected frame. You got a
623 problem with that?"
624 (let (device display)
625 (and (fboundp 'selected-device) (fboundp 'device-type) (fboundp 'device-connection)
626 (setq device (selected-device))
627 (eq (device-type device) 'x)
628 (setq display (device-connection device))
629 (not (equal display (getenv "DISPLAY")))
630 display)))
632 ;;;###autoload
633 (defun browse-url-netscape (url &optional new-window)
634 "Ask the Netscape WWW browser to load URL.
636 Default to the URL around or before point. The strings in variable
637 `browse-url-netscape-arguments' are also passed to Netscape.
639 When called interactively, if variable `browse-url-new-window-p' is
640 non-nil, load the document in a new Netscape window, otherwise use a
641 random existing one. A non-nil interactive prefix argument reverses
642 the effect of `browse-url-new-window-p'.
644 When called non-interactively, optional second argument NEW-WINDOW is
645 used instead of `browse-url-new-window-p'."
646 (interactive (browse-url-interactive-arg "Netscape URL: "))
647 ;; URL encode any commas in the URL
648 (while (string-match "," url)
649 (setq url (replace-match "%2C" t t url)))
650 (let* ((process-environment (browse-url-process-environment))
651 (process (apply 'start-process
652 (concat "netscape " url) nil
653 browse-url-netscape-program
654 (append browse-url-netscape-arguments
655 (if (eq window-system 'w32)
656 (list url)
657 (if new-window '("-noraise"))
658 (list "-remote"
659 (concat "openURL(" url
660 (if new-window ",new-window")
661 ")")))))))
662 (set-process-sentinel process
663 (list 'lambda '(process change)
664 (list 'browse-url-netscape-sentinel 'process url)))))
666 (defun browse-url-netscape-sentinel (process url)
667 "Handle a change to the process communicating with Netscape."
668 (or (eq (process-exit-status process) 0)
669 (let* ((process-environment (browse-url-process-environment)))
670 ;; Netscape not running - start it
671 (message "Starting Netscape...")
672 (apply 'start-process (concat "netscape" url) nil
673 browse-url-netscape-program
674 (append browse-url-netscape-startup-arguments (list url))))))
676 (defun browse-url-netscape-reload ()
677 "Ask Netscape to reload its current document."
678 (interactive)
679 ;; Backwards incompatibility reported by
680 ;; <peter.kruse@psychologie.uni-regensburg.de>.
681 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
682 "xfeDoCommand(reload)"
683 "reload")))
685 (defun browse-url-netscape-send (command)
686 "Send a remote control command to Netscape."
687 (let* ((process-environment (browse-url-process-environment)))
688 (apply 'start-process "netscape" nil
689 browse-url-netscape-program
690 (append browse-url-netscape-arguments
691 (list "-remote" command)))))
693 ;; --- Mosaic ---
695 ;;;###autoload
696 (defun browse-url-mosaic (url &optional new-window)
697 ;; new-window ignored
698 "Ask the XMosaic WWW browser to load URL.
699 Default to the URL around or before point."
700 (interactive (browse-url-interactive-arg "Mosaic URL: "))
701 (let ((pidfile (expand-file-name "~/.mosaicpid"))
702 pid pidbuf)
703 (if (file-readable-p pidfile)
704 (save-excursion
705 (find-file pidfile)
706 (goto-char (point-min))
707 (setq pid (read (current-buffer)))
708 (kill-buffer nil)))
709 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
710 (save-excursion
711 (find-file (format "/tmp/Mosaic.%d" pid))
712 (erase-buffer)
713 (insert "goto\n" url "\n")
714 (save-buffer)
715 (kill-buffer nil)
716 ;; Send signal SIGUSR to Mosaic
717 (message "Signalling Mosaic...")
718 (signal-process pid browse-url-usr1-signal)
719 ;; Or you could try:
720 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
721 (message "Signalling Mosaic...done")
723 ;; Mosaic not running - start it
724 (message "Starting Mosaic...")
725 (apply 'start-process "xmosaic" nil "xmosaic"
726 (append browse-url-mosaic-arguments (list url)))
727 (message "Starting Mosaic...done"))))
729 ;; --- Grail ---
731 ;;;###autoload
732 (defvar browse-url-grail
733 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
734 "*Location of Grail remote control client script `rcgrail.py'.
735 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
737 ;;;###autoload
738 (defun browse-url-grail (url &optional new-window)
739 "Ask the Grail WWW browser to load URL.
740 Default to the URL around or before point. Runs the program in the
741 variable `browse-url-grail'."
742 (interactive (browse-url-interactive-arg "Grail URL: "))
743 (message "Sending URL to Grail...")
744 (save-excursion
745 (set-buffer (get-buffer-create " *Shell Command Output*"))
746 (erase-buffer)
747 ;; don't worry about this failing.
748 (call-process browse-url-grail nil 0 nil url)
749 (message "Sending URL to Grail... done")))
751 ;; --- Mosaic using CCI ---
753 (defun browse-url-cci (url &optional new-window)
754 "Ask the XMosaic WWW browser to load URL.
755 Default to the URL around or before point.
757 This function only works for XMosaic version 2.5 or later. You must
758 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
759 value of variable `browse-url-CCI-port', and enable `Accept requests'.
761 When called interactively, if variable `browse-url-new-window-p' is
762 non-nil, load the document in a new browser window, otherwise use a
763 random existing one. A non-nil interactive prefix argument reverses
764 the effect of `browse-url-new-window-p'.
766 When called non-interactively, optional second argument NEW-WINDOW is
767 used instead of `browse-url-new-window-p'."
768 (interactive (browse-url-interactive-arg "Mosaic URL: "))
769 (open-network-stream "browse-url" " *browse-url*"
770 browse-url-CCI-host browse-url-CCI-port)
771 ;; Todo: start browser if fails
772 (process-send-string "browse-url"
773 (concat "get url (" url ") output "
774 (if new-window "new" "current") "\r\n"))
775 (process-send-string "browse-url" "disconnect\r\n")
776 (delete-process "browse-url"))
778 ;; --- IXI Mosaic ---
780 ;;;###autoload
781 (defun browse-url-iximosaic (url &optional new-window)
782 ;; new-window ignored
783 "Ask the IXIMosaic WWW browser to load URL.
784 Default to the URL around or before point."
785 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
786 (start-process "tellw3b" nil "tellw3b"
787 "-service WWW_BROWSER ixi_showurl " url))
789 ;; --- W3 ---
791 ;;;###autoload
792 (defun browse-url-w3 (url &optional new-window)
793 ;; new-window ignored
794 "Ask the w3 WWW browser to load URL.
795 Default to the URL around or before point."
796 (interactive (browse-url-interactive-arg "W3 URL: "))
797 (w3-fetch url))
799 ;;;###autoload
800 (defun browse-url-w3-gnudoit (url &optional new-window)
801 ;; new-window ignored
802 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
803 The `browse-url-gnudoit-program' program is used with options given by
804 `browse-url-gnudoit-args'. Default to the URL around or before point."
805 (interactive (browse-url-interactive-arg "W3 URL: "))
806 (apply 'start-process (concat "gnudoit:" url) nil
807 browse-url-gnudoit-program
808 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
810 ;; --- Lynx in an xterm ---
812 ;;;###autoload
813 (defun browse-url-lynx-xterm (url &optional new-window)
814 ;; new-window ignored
815 "Ask the Lynx WWW browser to load URL.
816 Default to the URL around or before point. A new Lynx process is run
817 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
818 with possible additional arguments `browse-url-xterm-args'."
819 (interactive (browse-url-interactive-arg "Lynx URL: "))
820 (apply 'start-process (concat "lynx" url) nil browse-url-xterm-program
821 (append browse-url-xterm-args (list "-e" "lynx" url))))
823 ;; --- Lynx in an Emacs "term" window ---
825 ;;;###autoload
826 (defun browse-url-lynx-emacs (url &optional new-window)
827 ;; new-window ignored
828 "Ask the Lynx WWW browser to load URL.
829 Default to the URL around or before point. Run a new Lynx process in
830 an Emacs buffer."
831 (interactive (browse-url-interactive-arg "Lynx URL: "))
832 (let ((system-uses-terminfo t)) ; Lynx uses terminfo
833 (if (fboundp 'make-term)
834 (let ((term-term-name "vt100"))
835 (set-buffer (make-term "browse-url" "lynx" nil url))
836 (term-mode)
837 (term-char-mode)
838 (switch-to-buffer "*browse-url*"))
839 (terminal-emulator "*browse-url*" "lynx" (list url)))))
841 ;; --- MMM ---
843 ;;;###autoload
844 (defun browse-url-mmm (url &optional new-window)
845 "Ask the MMM WWW browser to load URL.
846 Default to the URL around or before point."
847 (interactive (browse-url-interactive-arg "MMM URL: "))
848 (message "Sending URL to MMM...")
849 (save-excursion
850 (set-buffer (get-buffer-create " *Shell Command Output*"))
851 (erase-buffer)
852 ;; mmm_remote just SEGVs if the file isn't there...
853 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
854 ;; location in v 0.4:
855 (file-exists-p (expand-file-name "~/.mmm/remote")))
856 (call-process "mmm_remote" nil 0 nil url)
857 (call-process "mmm" nil 0 nil "-external" url))
858 (message "Sending URL to MMM... done")))
860 ;; --- mailto ---
862 ;;;###autoload
863 (defun browse-url-mail (url &optional new-window)
864 "Open a new mail message buffer within Emacs.
865 Default to using the mailto: URL around or before point as the
866 recipient's address. Supplying a non-nil interactive prefix argument
867 will cause the mail to be composed in another window rather than the
868 current one."
869 (interactive (browse-url-interactive-arg "Mailto URL: "))
870 (save-excursion
871 (let ((func (if new-window
872 'compose-mail-other-window
873 'compose-mail))
874 (to (if (string-match "^mailto:" url)
875 (substring url 7)
876 url)))
877 (apply func
878 (list to nil nil nil nil nil (cons 'insert-buffer
879 (current-buffer)))))))
881 ;; --- Random browser ---
883 ;;;###autoload
884 (defun browse-url-generic (url &optional new-window)
885 ;; new-window ignored
886 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
887 Default to the URL around or before point. A fresh copy of the
888 browser is started up in a new process with possible additional arguments
889 `browse-url-generic-args'. This is appropriate for browsers which
890 don't offer a form of remote control."
891 (interactive (browse-url-interactive-arg "URL: "))
892 (if (not browse-url-generic-program)
893 (error "No browser defined (`browse-url-generic-program')"))
894 (apply 'start-process (concat browse-url-generic-program url) nil
895 browse-url-generic-program
896 (append browse-url-generic-args (list url))))
898 (provide 'browse-url)
900 ;;; browse-url.el ends here