]> code.delx.au - gnu-emacs/blob - lisp/browse-url.el
Doc fixes.
[gnu-emacs] / lisp / browse-url.el
1 ;;; browse-url.el --- Pass a URL to a WWW browser
2
3 ;; Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
4
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/
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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.
27
28 ;;; Commentary:
29
30 ;; The latest version of this package should be available from
31 ;; <URL:http://wombat.doc.ic.ac.uk/emacs/browse-url.el>.
32
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:
40
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
52
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>.
55
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.
59
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?
63
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>
67
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>
71
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/>.
79
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>.
87
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/>.
92
93 ;; Lynx is now distributed by the FSF. See also
94 ;; <URL:http://lynx.browser.org/>.
95
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).
103
104 ;; I 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/>
108
109 ;; This package generalises function html-previewer-process in Marc
110 ;; Andreessen <marca@ncsa.uiuc.edu>'s html-mode (LCD
111 ;; modes/html-mode.el.Z) and provides better versions of the URL
112 ;; functions in Michelangelo Grigni <mic@cs.ucsd.edu>'s ffap.el
113 ;; (find-file-at-point) <URL:ftp://cs.ucsd.edu:/pub/mic/>. The huge
114 ;; hyperbole package also contains similar functions.
115
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117 ;; Help!
118
119 ;; Can you write and test some code for the Macintrash and Windoze
120 ;; Netscape remote control APIs? (See the URL above).
121
122 ;; Do any other browsers have remote control?
123
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ;; Usage
126
127 ;; To display the URL at or before point:
128 ;; M-x browse-url-at-point RET
129 ;; or, similarly but with the opportunity to edit the URL extracted from
130 ;; the buffer, use:
131 ;; M-x browse-url
132
133 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
134 ;; file:
135 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
136 ;; (Note that using Shift-mouse-1 is not desirable because
137 ;; that event has a standard meaning in Emacs.)
138
139 ;; To display the current buffer in a web browser:
140 ;; M-x browse-url-of-buffer RET
141
142 ;; To display the current region in a web browser:
143 ;; M-x browse-url-of-region RET
144
145 ;; In Dired, to display the file named on the current line:
146 ;; M-x browse-url-of-dired-file RET
147
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;; Customisation (~/.emacs)
150
151 ;; To see what variables are available for customization, type
152 ;; `M-x set-variable browse-url TAB'.
153
154 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
155 ;; (as used by html-helper-mode):
156 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
157 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
158 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
159 ;; (global-set-key "\C-c\C-zu" 'browse-url)
160 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
161 ;; (add-hook 'dired-mode-hook
162 ;; (function (lambda ()
163 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file))))
164
165 ;; Browse URLs in mail messages by clicking mouse-2:
166 ;; (add-hook 'rmail-mode-hook (function (lambda () ; rmail-mode startup
167 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse))))
168
169 ;; Browse URLs in Usenet messages by clicking mouse-2:
170 ;; (eval-after-load "gnus"
171 ;; '(define-key gnus-article-mode-map [mouse-2] 'browse-url-at-mouse))
172
173 ;; Use the Emacs w3 browser when not running under X11:
174 ;; (or (eq window-system 'x)
175 ;; (setq browse-url-browser-function 'browse-url-w3))
176
177 ;; To always save modified buffers before displaying the file in a browser:
178 ;; (setq browse-url-save-file t)
179
180 ;; To get round the Netscape caching problem, you could EITHER have
181 ;; write-file in html-helper-mode make Netscape reload the document:
182 ;;
183 ;; (autoload 'browse-url-netscape-reload "browse-url"
184 ;; "Ask a WWW browser to redisplay the current file." t)
185 ;; (add-hook 'html-helper-mode-hook
186 ;; (function (lambda ()
187 ;; (add-hook 'local-write-file-hooks
188 ;; (function (lambda ()
189 ;; (let ((local-write-file-hooks))
190 ;; (save-buffer))
191 ;; (browse-url-netscape-reload)
192 ;; t)) ; => file written by hook
193 ;; t)))) ; append to l-w-f-hooks
194 ;;
195 ;; OR have browse-url-of-file ask Netscape to load and then reload the
196 ;; file:
197 ;;
198 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
199
200 ;; You may also want to customise browse-url-netscape-arguments, e.g.
201 ;; (setq browse-url-netscape-arguments '("-install"))
202 ;;
203 ;; or similarly for the other browsers.
204
205 ;; To invoke different browsers for different URLs:
206 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
207 ;; ("." . browse-url-netscape)))
208
209 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210 ;;; Code:
211
212 (eval-when-compile (require 'dired)
213 (require 'thingatpt))
214
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;; Variables
217
218 ;;;###autoload
219 (defgroup browse-url nil
220 "Use a web browser to look at a URL."
221 :group 'applications)
222
223 ;;;###autoload
224 (defcustom browse-url-browser-function
225 'browse-url-netscape
226 "*Function to display the current buffer in a WWW browser.
227 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
228 `browse-url-of-file' commands.
229
230 If the value is not a function it should be a list of pairs
231 (REGEXP.FUNCTION). In this case the function called will be the one
232 associated with the first REGEXP which matches the current URL. The
233 function is passed the URL and any other args of `browse-url'. The last
234 regexp should probably be \".\" to specify a default browser."
235 :type 'function
236 :group 'browse-url)
237
238 (defcustom browse-url-netscape-program "netscape"
239 "*The name by which to invoke Netscape."
240 :type 'string
241 :group 'browse-url)
242
243 (defcustom browse-url-netscape-arguments nil
244 "*A list of strings to pass to Netscape as arguments."
245 :type '(repeat (string :tag "Argument"))
246 :group 'browse-url)
247
248 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
249 "*A list of strings to pass to Netscape when it starts up.
250 Defaults to the value of `browse-url-netscape-arguments' at the time
251 `browse-url' is loaded."
252 :type '(repeat (string :tag "Argument"))
253 :group 'browse-url)
254
255 (defcustom browse-url-new-window-p nil
256 "*If non-nil, always open a new browser window.
257 Passing an interactive argument to \\[browse-url-netscape] or
258 \\[browse-url-cci] reverses the effect of this variable. Requires
259 Netscape version 1.1N or later or XMosaic version 2.5 or later."
260 :type 'boolean
261 :group 'browse-url)
262
263 (defcustom browse-url-netscape-display nil
264 "*The X display for running Netscape, if not same as Emacs's."
265 :type 'string
266 :group 'browse-url)
267
268 (defcustom browse-url-mosaic-arguments nil
269 "*A list of strings to pass to Mosaic as arguments."
270 :type '(repeat (string :tag "Argument"))
271 :group 'browse-url)
272
273 (defvar browse-url-path-regexp
274 "[^]\t\n \"'()<>[^`{}]*[^]\t\n \"'()<>[^`{}.,;]+"
275 "A regular expression to match the host, path or e-mail part of a URL.")
276
277 (defvar browse-url-short-regexp
278 (concat "[-A-Za-z0-9.]+" browse-url-path-regexp)
279 "A regular expression probably matching a URL without an access scheme.
280 Hostname matching is stricter in this case than for
281 ``browse-url-regexp''.")
282
283 (defvar browse-url-regexp
284 (concat
285 "\\(https?://\\|ftp://\\|gopher://\\|telnet://\\|wais://\\|file:/\\|s?news:\\|mailto:\\)"
286 browse-url-path-regexp)
287 "A regular expression probably matching a complete URL.")
288
289 (defvar browse-url-markedup-regexp
290 "<URL:[^>]+>"
291 "A regular expression matching a URL marked up per RFC1738.
292 This may be broken across lines.")
293
294 (defvar browse-url-filename-alist
295 '(("^/+" . "file:/"))
296 "An alist of (REGEXP . STRING) pairs.
297 Any substring of a filename matching one of the REGEXPs is replaced by
298 the corresponding STRING. All pairs are applied in the order given.
299 The default value prepends `file:' to any path beginning with `/'.
300 Used by the `browse-url-of-file' command.
301
302 For example, to map EFS filenames to URLs:
303
304 (setq browse-url-filename-alist
305 '((\"/webmaster@webserver:/home/www/html/\" .
306 \"http://www.acme.co.uk/\")
307 (\"^/+\" . \"file:/\")))
308 ")
309
310 (defvar browse-url-save-file nil
311 "If non-nil, save the buffer before displaying its file.
312 Used by the `browse-url-of-file' command.")
313
314 (defvar browse-url-of-file-hook nil
315 "A hook to be run with run-hook after `browse-url-of-file' has asked
316 a browser to load a file.
317
318 Set this to `browse-url-netscape-reload' to force Netscape to load the
319 file rather than displaying a cached copy.")
320
321 (defvar browse-url-usr1-signal
322 (if (and (boundp 'emacs-major-version)
323 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
324 'SIGUSR1 ; Why did I think this was in lower case before?
325 30) ; Check /usr/include/signal.h.
326 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
327 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
328 which is 30 on SunOS and 16 on HP-UX and Solaris.")
329
330 (defvar browse-url-CCI-port 3003
331 "Port to access XMosaic via CCI.
332 This can be any number between 1024 and 65535 but must correspond to
333 the value set in the browser.")
334
335 (defvar browse-url-CCI-host "localhost"
336 "*Host to access XMosaic via CCI.
337 This should be the host name of the machine running XMosaic with CCI
338 enabled. The port number should be set in `browse-url-CCI-port'.")
339
340 (defvar browse-url-temp-file-name nil)
341 (make-variable-buffer-local 'browse-url-temp-file-name)
342
343 (defcustom browse-url-xterm-program "xterm"
344 "*The name of the terminal emulator used by `browse-url-lynx-xterm'.
345 This might, for instance, be a separate colour version of xterm."
346 :type 'string
347 :group 'browse-url)
348
349 (defcustom browse-url-xterm-args nil
350 "*A list of strings defining options for `browse-url-xterm-program'.
351 These might set its size, for instance."
352 :type '(repeat (string :tag "Argument"))
353 :group 'browse-url)
354
355 (defcustom browse-url-gnudoit-program "gnudoit"
356 "*The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
357 :type 'string
358 :group 'browse-url)
359
360 (defcustom browse-url-gnudoit-args '("-q")
361 "*A list of strings defining options for `browse-url-gnudoit-program'.
362 These might set the port, for instance."
363 :type '(repeat (string :tag "Argument"))
364 :group 'browse-url)
365
366 (defcustom browse-url-generic-program nil
367 "*The name of the browser program used by `browse-url-generic'."
368 :type 'string
369 :group 'browse-url)
370
371 (defcustom browse-url-generic-args nil
372 "*A list of strings defining options for `browse-url-generic-program'."
373 :type '(repeat (string :tag "Argument"))
374 :group 'browse-url)
375
376 (defcustom browse-url-temp-dir
377 (or (getenv "TMPDIR") "/tmp")
378 "*The name of a directory in which to store temporary files
379 generated by functions like `browse-url-of-region'. You might want to
380 set this to somewhere with restricted read permissions for privacy's sake."
381 :type 'string
382 :group 'browse-url)
383
384 (defvar browse-url-temp-file-list '())
385
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ;; URL input
388
389 (defun browse-url-url-at-point ()
390 (let ((url (thing-at-point 'url)))
391 (set-text-properties 0 (length url) nil url)
392 url))
393
394 ;; Having this as a separate function called by the browser-specific
395 ;; functions allows them to be stand-alone commands, making it easier
396 ;; to switch between browsers.
397
398 (defun browse-url-interactive-arg (prompt)
399 "Read a URL from the minibuffer, prompting with PROMPT.
400 Default to the URL at or before point. If invoked with a mouse button,
401 set point to the position clicked first. Return a list for use in
402 `interactive' containing the URL and `browse-url-new-window-p' or its
403 negation if a prefix argument was given."
404 (let ((event (elt (this-command-keys) 0)))
405 (and (listp event) (mouse-set-point event)))
406 (list (read-string prompt (browse-url-url-at-point))
407 (not (eq (null browse-url-new-window-p)
408 (null current-prefix-arg)))))
409
410 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
411 ;; Browse current buffer
412
413 ;;;###autoload
414 (defun browse-url-of-file (&optional file)
415 "Ask a WWW browser to display FILE.
416 Display the current buffer's file if FILE is nil or if called
417 interactively. Turn the filename into a URL with function
418 `browse-url-file-url'. Pass the URL to a browser using the
419 `browse-url' function then run `browse-url-of-file-hook'."
420 (interactive)
421 (or file
422 (setq file (buffer-file-name))
423 (error "Current buffer has no file"))
424 (let ((buf (get-file-buffer file)))
425 (if buf
426 (save-excursion
427 (set-buffer buf)
428 (cond ((not (buffer-modified-p)))
429 (browse-url-save-file (save-buffer))
430 (t (message "%s modified since last save" file))))))
431 (browse-url (browse-url-file-url file))
432 (run-hooks 'browse-url-of-file-hook))
433
434 (defun browse-url-file-url (file)
435 "Return the URL corresponding to FILE.
436 Use variable `browse-url-filename-alist' to map filenames to URLs.
437 Convert EFS file names of the form /USER@HOST:PATH to ftp://HOST/PATH."
438 ;; URL-encode special chars, do % first
439 (let ((s 0))
440 (while (setq s (string-match "%" file s))
441 (setq file (replace-match "%25" t t file)
442 s (1+ s))))
443 (while (string-match "[*\"()',=;? ]" file)
444 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
445 (setq file (replace-match enc t t file))))
446 (let ((maps browse-url-filename-alist))
447 (while maps
448 (let* ((map (car maps))
449 (from-re (car map))
450 (to-string (cdr map)))
451 (setq maps (cdr maps))
452 (and (string-match from-re file)
453 (setq file (replace-match to-string t t file))))))
454 ;; Check for EFS path
455 (and (string-match "^/\\([^:@]+@\\)?\\([^:]+\\):/*" file)
456 (setq file (concat "ftp://"
457 (substring file (match-beginning 2) (match-end 2))
458 "/" (substring file (match-end 0)))))
459 file)
460
461 ;;;###autoload
462 (defun browse-url-of-buffer (&optional buffer)
463 "Ask a WWW browser to display BUFFER.
464 Display the current buffer if BUFFER is nil. Display only the
465 currently visible part of BUFFER (from a temporary file) if buffer is
466 narrowed."
467 (interactive)
468 (save-excursion
469 (and buffer (set-buffer buffer))
470 (let ((file-name
471 ;; Ignore real name if restricted
472 (and (= (- (point-max) (point-min)) (buffer-size))
473 (or buffer-file-name
474 (and (boundp 'dired-directory) dired-directory)))))
475 (or file-name
476 (progn
477 (or browse-url-temp-file-name
478 (setq browse-url-temp-file-name
479 (convert-standard-filename
480 (make-temp-name
481 (expand-file-name "burl" browse-url-temp-dir)))))
482 (setq file-name browse-url-temp-file-name)
483 (write-region (point-min) (point-max) file-name nil 'no-message)))
484 (browse-url-of-file file-name))))
485
486 (defun browse-url-delete-temp-file (&optional temp-file-name)
487 ;; Delete browse-url-temp-file-name from the file system and from
488 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
489 ;; non-nil, delete it instead, but only from the file system --
490 ;; browse-url-temp-file-list is not affected.
491 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
492 (if (and file-name (file-exists-p file-name))
493 (progn
494 (delete-file file-name)
495 (if (null temp-file-name)
496 (setq browse-url-temp-file-list
497 (delete browse-url-temp-file-name
498 browse-url-temp-file-list)))))))
499
500 (defun browse-url-delete-temp-file-list ()
501 ;; Delete all elements of browse-url-temp-file-list.
502 (while browse-url-temp-file-list
503 (browse-url-delete-temp-file (car browse-url-temp-file-list))
504 (setq browse-url-temp-file-list
505 (cdr browse-url-temp-file-list))))
506
507 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
508 (add-hook 'kill-emacs-hook 'browse-url-delete-temp-file-list)
509
510 ;;;###autoload
511 (defun browse-url-of-dired-file ()
512 "In Dired, ask a WWW browser to display the file named on this line."
513 (interactive)
514 (browse-url-of-file (dired-get-filename)))
515
516 ;;;###autoload
517 (defun browse-url-of-region (min max)
518 "Ask a WWW browser to display the current region."
519 (interactive "r")
520 (save-excursion
521 (save-restriction
522 (narrow-to-region (mark) (point))
523 (browse-url-of-buffer))))
524
525 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
526 ;; Browser-independent commands
527
528 ;; A generic command to call the current browse-url-browser-function
529
530 ;;;###autoload
531 (defun browse-url (&rest args)
532 "Ask a WWW browser to load URL.
533 Prompts for a URL, defaulting to the URL at or before point. Variable
534 `browse-url-browser-function' says which browser to use."
535 (interactive (browse-url-interactive-arg "URL: "))
536 (if (consp browse-url-browser-function)
537 (browse-url-choose-browser args)
538 (apply browse-url-browser-function args)))
539
540 (defun browse-url-choose-browser (url &rest args)
541 "Pass URL to a browser function chosen.
542 This is done according to the association list in variable
543 `browse-url-browser-function'."
544 (let ((blist browse-url-browser-function)
545 re bf)
546 (while (consp blist)
547 (setq re (car (car blist))
548 bf (cdr (car blist))
549 blist (cdr blist))
550 (if (string-match re url)
551 (progn (apply bf url args) (setq blist t))))
552 (or blist
553 (error "No browser in browse-url-browser-function matching URL %s" url))))
554
555 ;;;###autoload
556 (defun browse-url-at-point ()
557 "Ask a WWW browser to load the URL at or before point.
558 Doesn't let you edit the URL like browse-url. Variable
559 `browse-url-browser-function' says which browser to use."
560 (interactive)
561 (browse-url (browse-url-url-at-point)))
562
563 (defun browse-url-event-buffer (event)
564 (window-buffer (posn-window (event-start event))))
565
566 (defun browse-url-event-point (event)
567 (posn-point (event-start event)))
568
569 ;;;###autoload
570 (defun browse-url-at-mouse (event)
571 "Ask a WWW browser to load a URL clicked with the mouse.
572 The URL is the one around or before the position of the mouse click
573 but point is not changed. Doesn't let you edit the URL like
574 browse-url. Variable `browse-url-browser-function' says which browser
575 to use."
576 (interactive "e")
577 (save-excursion
578 (set-buffer (browse-url-event-buffer event))
579 (goto-char (browse-url-event-point event))
580 (let ((url (browse-url-url-at-point)))
581 (if (string-equal url "")
582 (error "No URL found"))
583 (browse-url url))))
584
585 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
586 ;; Browser-specific commands
587
588 ;; --- Netscape ---
589
590 (defun browse-url-process-environment ()
591 "Set DISPLAY in the environment to the X display Netscape is running on.
592 This is either the value of variable `browse-url-netscape-display' if
593 non-nil, or the same display as Emacs if different from the current
594 environment, otherwise just use the current environment."
595 (let ((display (or browse-url-netscape-display (browse-url-emacs-display))))
596 (if display
597 (cons (concat "DISPLAY=" display) process-environment)
598 process-environment)))
599
600 (defun browse-url-emacs-display ()
601 "Return the X display Emacs is running on.
602 This nil if the display is the same as the DISPLAY environment variable.
603
604 Actually Emacs could be using several screens on several displays, as
605 listed by (emacs-display-list) and (x-display-screens DISPLAY), this
606 just returns the display showing the selected frame. You got a
607 problem with that?"
608 (let (device display)
609 (and (fboundp 'selected-device) (fboundp 'device-type) (fboundp 'device-connection)
610 (setq device (selected-device))
611 (eq (device-type device) 'x)
612 (setq display (device-connection device))
613 (not (equal display (getenv "DISPLAY")))
614 display)))
615
616 ;;;###autoload
617 (defun browse-url-netscape (url &optional new-window)
618 "Ask the Netscape WWW browser to load URL.
619
620 Default to the URL around or before point. The strings in variable
621 `browse-url-netscape-arguments' are also passed to Netscape.
622
623 When called interactively, if variable `browse-url-new-window-p' is
624 non-nil, load the document in a new Netscape window, otherwise use a
625 random existing one. A non-nil interactive prefix argument reverses
626 the effect of `browse-url-new-window-p'.
627
628 When called non-interactively, optional second argument NEW-WINDOW is
629 used instead of `browse-url-new-window-p'."
630 (interactive (browse-url-interactive-arg "Netscape URL: "))
631 ;; URL encode any commas in the URL
632 (while (string-match "," url)
633 (setq url (replace-match "%2C" t t url)))
634 (let* ((process-environment (browse-url-process-environment))
635 (process (apply 'start-process
636 (concat "netscape " url) nil
637 browse-url-netscape-program
638 (append browse-url-netscape-arguments
639 (if (string-equal "win32" window-system)
640 (list url)
641 (if new-window '("-noraise"))
642 (list "-remote"
643 (concat "openURL(" url
644 (if new-window ",new-window")
645 ")")))))))
646 (set-process-sentinel process
647 (list 'lambda '(process change)
648 (list 'browse-url-netscape-sentinel 'process url)))))
649
650 (defun browse-url-netscape-sentinel (process url)
651 "Handle a change to the process communicating with Netscape."
652 (or (eq (process-exit-status process) 0)
653 (let* ((process-environment (browse-url-process-environment)))
654 ;; Netscape not running - start it
655 (message "Starting Netscape...")
656 (apply 'start-process (concat "netscape" url) nil
657 browse-url-netscape-program
658 (append browse-url-netscape-startup-arguments (list url))))))
659
660 (defun browse-url-netscape-reload ()
661 "Ask Netscape to reload its current document."
662 (interactive)
663 (browse-url-netscape-send "reload"))
664
665 (defun browse-url-netscape-send (command)
666 "Send a remote control command to Netscape."
667 (let* ((process-environment (browse-url-process-environment)))
668 (apply 'start-process "netscape" nil
669 browse-url-netscape-program
670 (append browse-url-netscape-arguments
671 (list "-remote" command)))))
672
673 ;; --- Mosaic ---
674
675 ;;;###autoload
676 (defun browse-url-mosaic (url &optional new-window)
677 ;; new-window ignored
678 "Ask the XMosaic WWW browser to load URL.
679 Default to the URL around or before point."
680 (interactive (browse-url-interactive-arg "Mosaic URL: "))
681 (let ((pidfile (expand-file-name "~/.mosaicpid"))
682 pid pidbuf)
683 (if (file-readable-p pidfile)
684 (save-excursion
685 (find-file pidfile)
686 (goto-char (point-min))
687 (setq pid (read (current-buffer)))
688 (kill-buffer nil)))
689 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
690 (save-excursion
691 (find-file (format "/tmp/Mosaic.%d" pid))
692 (erase-buffer)
693 (insert "goto\n" url "\n")
694 (save-buffer)
695 (kill-buffer nil)
696 ;; Send signal SIGUSR to Mosaic
697 (message "Signalling Mosaic...")
698 (signal-process pid browse-url-usr1-signal)
699 ;; Or you could try:
700 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
701 (message "Signalling Mosaic...done")
702 )
703 ;; Mosaic not running - start it
704 (message "Starting Mosaic...")
705 (apply 'start-process "xmosaic" nil "xmosaic"
706 (append browse-url-mosaic-arguments (list url)))
707 (message "Starting Mosaic...done"))))
708
709 ;; --- Grail ---
710
711 ;;;###autoload
712 (defvar browse-url-grail
713 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
714 "*Location of Grail remote control client script `rcgrail.py'.
715 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
716
717 ;;;###autoload
718 (defun browse-url-grail (url &optional new-window)
719 "Ask the Grail WWW browser to load URL.
720 Default to the URL around or before point. Runs the program in the
721 variable `browse-url-grail'."
722 (interactive (browse-url-interactive-arg "Grail URL: "))
723 (message "Sending URL to Grail...")
724 (save-excursion
725 (set-buffer (get-buffer-create " *Shell Command Output*"))
726 (erase-buffer)
727 ;; don't worry about this failing.
728 (call-process browse-url-grail nil 0 nil url)
729 (message "Sending URL to Grail... done")))
730
731 ;; --- Mosaic using CCI ---
732
733 (defun browse-url-cci (url &optional new-window)
734 "Ask the XMosaic WWW browser to load URL.
735 Default to the URL around or before point.
736
737 This function only works for XMosaic version 2.5 or later. You must
738 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
739 value of variable `browse-url-CCI-port', and enable `Accept requests'.
740
741 When called interactively, if variable `browse-url-new-window-p' is
742 non-nil, load the document in a new browser window, otherwise use a
743 random existing one. A non-nil interactive prefix argument reverses
744 the effect of `browse-url-new-window-p'.
745
746 When called non-interactively, optional second argument NEW-WINDOW is
747 used instead of `browse-url-new-window-p'."
748 (interactive (browse-url-interactive-arg "Mosaic URL: "))
749 (open-network-stream "browse-url" " *browse-url*"
750 browse-url-CCI-host browse-url-CCI-port)
751 ;; Todo: start browser if fails
752 (process-send-string "browse-url"
753 (concat "get url (" url ") output "
754 (if new-window "new" "current") "\r\n"))
755 (process-send-string "browse-url" "disconnect\r\n")
756 (delete-process "browse-url"))
757
758 ;; --- IXI Mosaic ---
759
760 ;;;###autoload
761 (defun browse-url-iximosaic (url &optional new-window)
762 ;; new-window ignored
763 "Ask the IXIMosaic WWW browser to load URL.
764 Default to the URL around or before point."
765 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
766 (start-process "tellw3b" nil "tellw3b"
767 "-service WWW_BROWSER ixi_showurl " url))
768
769 ;; --- W3 ---
770
771 ;;;###autoload
772 (defun browse-url-w3 (url &optional new-window)
773 ;; new-window ignored
774 "Ask the w3 WWW browser to load URL.
775 Default to the URL around or before point."
776 (interactive (browse-url-interactive-arg "W3 URL: "))
777 (w3-fetch url))
778
779 ;;;###autoload
780 (defun browse-url-w3-gnudoit (url &optional new-window)
781 ;; new-window ignored
782 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
783 The `browse-url-gnudoit-program' program is used with options given by
784 `browse-url-gnudoit-args'. Default to the URL around or before point."
785 (interactive (browse-url-interactive-arg "W3 URL: "))
786 (apply 'start-process (concat "gnudoit:" url) nil
787 browse-url-gnudoit-program
788 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
789
790 ;; --- Lynx in an xterm ---
791
792 ;;;###autoload
793 (defun browse-url-lynx-xterm (url &optional new-window)
794 ;; new-window ignored
795 "Ask the Lynx WWW browser to load URL.
796 Default to the URL around or before point. A new Lynx process is run
797 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
798 with possible additional arguments `browse-url-xterm-args'."
799 (interactive (browse-url-interactive-arg "Lynx URL: "))
800 (apply 'start-process (concat "lynx" url) nil browse-url-xterm-program
801 (append browse-url-xterm-args (list "-e" "lynx" url))))
802
803 ;; --- Lynx in an Emacs "term" window ---
804
805 ;;;###autoload
806 (defun browse-url-lynx-emacs (url &optional new-window)
807 ;; new-window ignored
808 "Ask the Lynx WWW browser to load URL.
809 Default to the URL around or before point. Run a new Lynx process in
810 an Emacs buffer."
811 (interactive (browse-url-interactive-arg "Lynx URL: "))
812 (let ((system-uses-terminfo t)) ; Lynx uses terminfo
813 (if (fboundp 'make-term)
814 (let ((term-term-name "vt100"))
815 (set-buffer (make-term "browse-url" "lynx" nil url))
816 (term-mode)
817 (term-char-mode)
818 (switch-to-buffer "*browse-url*"))
819 (terminal-emulator "*browse-url*" "lynx" (list url)))))
820
821 ;; --- MMM ---
822
823 ;;;###autoload
824 (defun browse-url-mmm (url &optional new-window)
825 "Ask the MMM WWW browser to load URL.
826 Default to the URL around or before point."
827 (interactive (browse-url-interactive-arg "MMM URL: "))
828 (message "Sending URL to MMM...")
829 (save-excursion
830 (set-buffer (get-buffer-create " *Shell Command Output*"))
831 (erase-buffer)
832 ;; mmm_remote just SEGVs if the file isn't there...
833 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
834 ;; location in v 0.4:
835 (file-exists-p (expand-file-name "~/.mmm/remote")))
836 (call-process "mmm_remote" nil 0 nil url)
837 (call-process "mmm" nil 0 nil "-external" url))
838 (message "Sending URL to MMM... done")))
839
840 ;; --- mailto ---
841
842 ;;;###autoload
843 (defun browse-url-mail (url)
844 "Open a new mail message buffer within Emacs.
845 Default to the mailto URL around or before point."
846 (interactive (browse-url-interactive-arg "Mailto URL: "))
847 (save-excursion
848 ;; open mail buffer, specifying TO and REPLYBUFFER
849 (mail nil (if (string-match "^mailto:" url)
850 (substring url 7)
851 url)
852 nil nil nil
853 (current-buffer))))
854
855 ;; --- Random browser ---
856
857 ;;;###autoload
858 (defun browse-url-generic (url &optional new-window)
859 ;; new-window ignored
860 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
861 Default to the URL around or before point. A fresh copy of the
862 browser is started up in a new process with possible additional arguments
863 `browse-url-generic-args'. This is appropriate for browsers which
864 don't offer a form of remote control."
865 (interactive (browse-url-interactive-arg "URL: "))
866 (if (not browse-url-generic-program)
867 (error "No browser defined (`browse-url-generic-program')"))
868 (apply 'start-process (concat browse-url-generic-program url) nil
869 browse-url-generic-program
870 (append browse-url-generic-args (list url))))
871
872 (provide 'browse-url)
873
874 ;;; browse-url.el ends here