]> code.delx.au - gnu-emacs/blob - lisp/net/browse-url.el
Moved here from lisp.
[gnu-emacs] / lisp / net / browse-url.el
1 ;;; browse-url.el --- Pass a URL to a WWW browser
2
3 ;; Copyright (C) 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4
5 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
6 ;; Maintainer: Dave Love <fx@gnu.org>
7 ;; Created: 03 Apr 1995
8 ;; Keywords: hypertext, hypermedia, mouse
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This package provides functions which read a URL (Uniform Resource
30 ;; Locator) from the minibuffer, defaulting to the URL around point,
31 ;; and ask a World-Wide Web browser to load it. It can also load the
32 ;; URL associated with the current buffer. Different browsers use
33 ;; different methods of remote control so there is one function for
34 ;; each supported browser. If the chosen browser is not running, it
35 ;; is started. Currently there is support for:
36
37 ;; Function Browser Earliest version
38 ;; browse-url-netscape Netscape 1.1b1
39 ;; browse-url-mosaic XMosaic/mMosaic <= 2.4
40 ;; browse-url-cci XMosaic 2.5
41 ;; browse-url-w3 w3 0
42 ;; browse-url-w3-gnudoit w3 remotely
43 ;; browse-url-iximosaic IXI Mosaic ?
44 ;; browse-url-lynx-* Lynx 0
45 ;; browse-url-grail Grail 0.3b1
46 ;; browse-url-mmm MMM ?
47 ;; browse-url-generic arbitrary
48
49 ;; [A version of the Netscape browser is now free software
50 ;; <URL:http://www.mozilla.org/>, albeit not GPLed, so it is
51 ;; reasonable to have that as the default.]
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 ;; Browsers can cache Web pages so it may be necessary to tell them 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:ftp://ftp.splode.com/pub/users/friedman/packages/>.
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 a semi-free WWW browser implemented in Objective Caml, an
89 ;; interesting impure functional programming language. See
90 ;; <URL:http://pauillac.inria.fr/%7Erouaix/mmm/>.
91
92 ;; Lynx is now distributed by the FSF. See also
93 ;; <URL:http://lynx.browser.org/>.
94
95 ;; Free graphical browsers that could be used by `browse-url-generic'
96 ;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
97 ;; <URL:http://www.unlv.edu/chimera/>, Arena
98 ;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena> and Amaya
99 ;; <URL:ftp://ftp.w3.org/pub/amaya>. mMosaic
100 ;; <URL:ftp://sig.enst.fr/pub/multicast/mMosaic/>,
101 ;; <URL:http://sig.enst.fr/~dauphin/mMosaic/> (with development
102 ;; support for Java applets and multicast) can be used like Mosaic by
103 ;; setting `browse-url-mosaic-program' appropriately.
104
105 ;; I [Denis Howe, not Dave Love] recommend Nelson Minar
106 ;; <nelson@santafe.edu>'s excellent html-helper-mode.el for editing
107 ;; HTML and thank Nelson for his many useful comments on this code.
108 ;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>
109
110 ;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
111 ;; software/own/hm--html-menus/>. For composing correct HTML see also
112 ;; PSGML the general SGML structure editor package
113 ;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
114 ;; with this.
115
116 ;; This package generalises function html-previewer-process in Marc
117 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
118 ;; ffap.el package. The huge hyperbole package also contains similar
119 ;; functions.
120
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;; Help!
123
124 ;; Can you write and test some code for the Macintrash and Windoze
125 ;; Netscape remote control APIs? (See the URL above).
126
127 ;; Do any other browsers have remote control?
128
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130 ;; Usage
131
132 ;; To display the URL at or before point:
133 ;; M-x browse-url-at-point RET
134 ;; or, similarly but with the opportunity to edit the URL extracted from
135 ;; the buffer, use:
136 ;; M-x browse-url
137
138 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
139 ;; file:
140 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
141 ;; (Note that using Shift-mouse-1 is not desirable because
142 ;; that event has a standard meaning in Emacs.)
143
144 ;; To display the current buffer in a web browser:
145 ;; M-x browse-url-of-buffer RET
146
147 ;; To display the current region in a web browser:
148 ;; M-x browse-url-of-region RET
149
150 ;; In Dired, to display the file named on the current line:
151 ;; M-x browse-url-of-dired-file RET
152
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 ;; Customisation (~/.emacs)
155
156 ;; To see what variables are available for customization, type
157 ;; `M-x set-variable browse-url TAB'. Better, use
158 ;; `M-x customize-group browse-url'.
159
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 ;; (lambda ()
169 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
170
171 ;; Browse URLs in mail messages by clicking mouse-2:
172 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
173 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
174
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.]
181
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))
185
186 ;; To always save modified buffers before displaying the file in a browser:
187 ;; (setq browse-url-save-file t)
188
189 ;; To get round the Netscape caching problem, you could EITHER have
190 ;; write-file in html-helper-mode make Netscape reload the document:
191 ;;
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 ;; (lambda ()
196 ;; (add-hook 'local-write-file-hooks
197 ;; (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
203 ;;
204 ;; OR have browse-url-of-file ask Netscape to load and then reload the
205 ;; file:
206 ;;
207 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
208
209 ;; You may also want to customise browse-url-netscape-arguments, e.g.
210 ;; (setq browse-url-netscape-arguments '("-install"))
211 ;;
212 ;; or similarly for the other browsers.
213
214 ;; To invoke different browsers for different URLs:
215 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
216 ;; ("." . browse-url-netscape)))
217
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;;; Code:
220
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 ;; Variables
223
224 (eval-when-compile (require 'thingatpt)
225 (require 'term)
226 (require 'dired)
227 (require 'w3-auto nil t))
228
229 (defgroup browse-url nil
230 "Use a web browser to look at a URL."
231 :prefix "browse-url-"
232 :group 'hypermedia)
233
234 ;;;###autoload
235 (defcustom browse-url-browser-function
236 (if (eq system-type 'windows-nt)
237 'browse-url-default-windows-browser
238 'browse-url-netscape)
239 "*Function to display the current buffer in a WWW browser.
240 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
241 `browse-url-of-file' commands.
242
243 If the value is not a function it should be a list of pairs
244 (REGEXP . FUNCTION). In this case the function called will be the one
245 associated with the first REGEXP which matches the current URL. The
246 function is passed the URL and any other args of `browse-url'. The last
247 regexp should probably be \".\" to specify a default browser."
248 :type '(choice
249 (function-item :tag "Emacs W3" :value browse-url-w3)
250 (function-item :tag "W3 in another Emacs via `gnudoit'"
251 :value browse-url-w3-gnudoit)
252 (function-item :tag "Netscape" :value browse-url-netscape)
253 (function-item :tag "Mosaic" :value browse-url-mosaic)
254 (function-item :tag "Mosaic using CCI" :value browse-url-cci)
255 (function-item :tag "IXI Mosaic" :value browse-url-iximosaic)
256 (function-item :tag "Lynx in an xterm window"
257 :value browse-url-lynx-xterm)
258 (function-item :tag "Lynx in an Emacs window"
259 :value browse-url-lynx-emacs)
260 (function-item :tag "Grail" :value browse-url-grail)
261 (function-item :tag "MMM" :value browse-url-mmm)
262 (function-item :tag "Specified by `Browse Url Generic Program'"
263 :value browse-url-generic)
264 (function-item :tag "Default Windows browser"
265 :value browse-url-default-windows-browser)
266 (function :tag "Your own function"))
267 :version "20.4"
268 :group 'browse-url)
269
270 (defcustom browse-url-netscape-program "netscape"
271 ;; Info about netscape-remote from Karl Berry.
272 "The name by which to invoke Netscape.
273
274 The free program `netscape-remote' from
275 <URL:http://home.netscape.com/newsref/std/remote.c> is said to start
276 up very much quicker than `netscape'. Reported to compile on a GNU
277 system, given vroot.h from the same directory, with cc flags
278 -DSTANDALONE -L/usr/X11R6/lib -lXmu -lX11."
279 :type 'string
280 :group 'browse-url)
281
282 (defcustom browse-url-netscape-arguments nil
283 "A list of strings to pass to Netscape as arguments."
284 :type '(repeat (string :tag "Argument"))
285 :group 'browse-url)
286
287 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
288 "A list of strings to pass to Netscape when it starts up.
289 Defaults to the value of `browse-url-netscape-arguments' at the time
290 `browse-url' is loaded."
291 :type '(repeat (string :tag "Argument"))
292 :group 'browse-url)
293
294 ;;;###autoload
295 (defcustom browse-url-new-window-p nil
296 "*If non-nil, always open a new browser window with appropriate browsers.
297 Passing an interactive argument to \\[browse-url], or specific browser
298 commands reverses the effect of this variable. Requires Netscape version
299 1.1N or later or XMosaic version 2.5 or later if using those browsers."
300 :type 'boolean
301 :group 'browse-url)
302
303 ;;;###autoload
304 (defcustom browse-url-netscape-display nil
305 "*The X display for running Netscape, if not same as Emacs'."
306 :type '(choice string (const :tag "Default" nil))
307 :group 'browse-url)
308
309 (defcustom browse-url-mosaic-program "xmosaic"
310 "The name by which to invoke Mosaic (or mMosaic)."
311 :type 'string
312 :version "20.3"
313 :group 'browse-url)
314
315 (defcustom browse-url-mosaic-arguments nil
316 "A list of strings to pass to Mosaic as arguments."
317 :type '(repeat (string :tag "Argument"))
318 :group 'browse-url)
319
320 (defcustom browse-url-filename-alist
321 '(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
322 ;; The above loses the username to avoid the browser prompting for
323 ;; it in anonymous cases. If it's not anonymous the next regexp
324 ;; applies.
325 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
326 ("^/+" . "file:/"))
327 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
328 Any substring of a filename matching one of the REGEXPs is replaced by
329 the corresponding STRING using `replace-match', not treating STRING
330 literally. All pairs are applied in the order given. The default
331 value converts ange-ftp/EFS-style paths into ftp URLs and prepends
332 `file:' to any path beginning with `/'.
333
334 For example, adding to the default a specific translation of an ange-ftp
335 address to an HTTP URL:
336
337 (setq browse-url-filename-alist
338 '((\"/webmaster@webserver:/home/www/html/\" .
339 \"http://www.acme.co.uk/\")
340 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
341 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
342 (\"^/+\" . \"file:/\")))
343 "
344 :type '(repeat (cons :format "%v"
345 (regexp :tag "Regexp")
346 (string :tag "Replacement")))
347 :version "20.3"
348 :group 'browse-url)
349
350 ;;;###autoload
351 (defcustom browse-url-save-file nil
352 "*If non-nil, save the buffer before displaying its file.
353 Used by the `browse-url-of-file' command."
354 :type 'boolean
355 :group 'browse-url)
356
357 (defcustom browse-url-of-file-hook nil
358 "Run after `browse-url-of-file' has asked a browser to load a file.
359
360 Set this to `browse-url-netscape-reload' to force Netscape to load the
361 file rather than displaying a cached copy."
362 :type 'hook
363 :options '(browse-url-netscape-reload)
364 :group 'browse-url)
365
366 (defvar browse-url-usr1-signal
367 (if (and (boundp 'emacs-major-version)
368 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
369 'SIGUSR1 ; Why did I think this was in lower case before?
370 30) ; Check /usr/include/signal.h.
371 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
372 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
373 which is 30 on SunOS and 16 on HP-UX and Solaris.")
374
375 (defcustom browse-url-CCI-port 3003
376 "Port to access XMosaic via CCI.
377 This can be any number between 1024 and 65535 but must correspond to
378 the value set in the browser."
379 :type 'integer
380 :group 'browse-url)
381
382 (defcustom browse-url-CCI-host "localhost"
383 "*Host to access XMosaic via CCI.
384 This should be the host name of the machine running XMosaic with CCI
385 enabled. The port number should be set in `browse-url-CCI-port'."
386 :type 'string
387 :group 'browse-url)
388
389 (defvar browse-url-temp-file-name nil)
390 (make-variable-buffer-local 'browse-url-temp-file-name)
391
392 (defcustom browse-url-xterm-program "xterm"
393 "The name of the terminal emulator used by `browse-url-lynx-xterm'.
394 This might, for instance, be a separate colour version of xterm."
395 :type 'string
396 :group 'browse-url)
397
398 (defcustom browse-url-xterm-args nil
399 "*A list of strings defining options for `browse-url-xterm-program'.
400 These might set its size, for instance."
401 :type '(repeat (string :tag "Argument"))
402 :group 'browse-url)
403
404 (defcustom browse-url-lynx-emacs-args (and (not window-system)
405 '("-show_cursor"))
406 "A list of strings defining options for Lynx in an Emacs buffer.
407
408 The default is none in a window system, otherwise `-show_cursor' to
409 indicate the position of the current link in the absence of
410 highlighting, assuming the normal default for showing the cursor."
411 :type '(repeat (string :tag "Argument"))
412 :version "20.3"
413 :group 'browse-url)
414
415 (defcustom browse-url-gnudoit-program "gnudoit"
416 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
417 :type 'string
418 :group 'browse-url)
419
420 (defcustom browse-url-gnudoit-args '("-q")
421 "*A list of strings defining options for `browse-url-gnudoit-program'.
422 These might set the port, for instance."
423 :type '(repeat (string :tag "Argument"))
424 :group 'browse-url)
425
426 ;;;###autoload
427 (defcustom browse-url-generic-program nil
428 "*The name of the browser program used by `browse-url-generic'."
429 :type '(choice string (const :tag "None" nil))
430 :group 'browse-url)
431
432 (defcustom browse-url-generic-args nil
433 "*A list of strings defining options for `browse-url-generic-program'."
434 :type '(repeat (string :tag "Argument"))
435 :group 'browse-url)
436
437 (defcustom browse-url-temp-dir temporary-file-directory
438 "The name of a directory for browse-url's temporary files.
439 Such files are generated by functions like `browse-url-of-region'.
440 You might want to set this to somewhere with restricted read permissions
441 for privacy's sake."
442 :type 'string
443 :group 'browse-url)
444
445 (defcustom browse-url-netscape-version
446 3
447 "The version of Netscape you are using.
448 This affects how URL reloading is done; the mechanism changed
449 incompatibly at version 4."
450 :type 'number
451 :group 'browse-url)
452
453 (defcustom browse-url-lynx-input-field 'avoid
454 "*Action on selecting an existing Lynx buffer at an input field.
455 What to do when sending a new URL to an existing Lynx buffer in Emacs
456 if the Lynx cursor is on an input field (in which case the `g' command
457 would be entered as data). Such fields are recognized by the
458 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
459 user and don't emit the URL, 'avoid: try to avoid the field by moving
460 down (this *won't* always work)."
461 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
462 (const :tag "Disregard" :value nil)
463 (const :tag "Warn, don't emit URL" :value warn))
464 :version "20.3"
465 :group 'browse-url)
466
467 (defvar browse-url-lynx-input-attempts 10
468 "*How many times to try to move down from a series of lynx input fields.")
469
470 (defcustom browse-url-lynx-input-delay 0.2
471 "How many seconds to wait for lynx between moves down from an input field.")
472
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 ;; URL input
475
476 (defun browse-url-url-at-point ()
477 (let ((url (thing-at-point 'url)))
478 (set-text-properties 0 (length url) nil url)
479 url))
480
481 ;; Having this as a separate function called by the browser-specific
482 ;; functions allows them to be stand-alone commands, making it easier
483 ;; to switch between browsers.
484
485 (defun browse-url-interactive-arg (prompt)
486 "Read a URL from the minibuffer, prompting with PROMPT.
487 Default to the URL at or before point. If invoked with a mouse button,
488 set point to the position clicked first. Return a list for use in
489 `interactive' containing the URL and `browse-url-new-window-p' or its
490 negation if a prefix argument was given."
491 (let ((event (elt (this-command-keys) 0)))
492 (and (listp event) (mouse-set-point event)))
493 (list (read-string prompt (browse-url-url-at-point))
494 (not (eq (null browse-url-new-window-p)
495 (null current-prefix-arg)))))
496
497 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
498 ;; Browse current buffer
499
500 ;;;###autoload
501 (defun browse-url-of-file (&optional file)
502 "Ask a WWW browser to display FILE.
503 Display the current buffer's file if FILE is nil or if called
504 interactively. Turn the filename into a URL with function
505 `browse-url-file-url'. Pass the URL to a browser using the
506 `browse-url' function then run `browse-url-of-file-hook'."
507 (interactive)
508 (or file
509 (setq file (buffer-file-name))
510 (error "Current buffer has no file"))
511 (let ((buf (get-file-buffer file)))
512 (if buf
513 (save-excursion
514 (set-buffer buf)
515 (cond ((not (buffer-modified-p)))
516 (browse-url-save-file (save-buffer))
517 (t (message "%s modified since last save" file))))))
518 (browse-url (browse-url-file-url file))
519 (run-hooks 'browse-url-of-file-hook))
520
521 (defun browse-url-file-url (file)
522 "Return the URL corresponding to FILE.
523 Use variable `browse-url-filename-alist' to map filenames to URLs."
524 ;; URL-encode special chars, do % first
525 (let ((s 0))
526 (while (setq s (string-match "%" file s))
527 (setq file (replace-match "%25" t t file)
528 s (1+ s))))
529 (while (string-match "[*\"()',=;? ]" file)
530 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
531 (setq file (replace-match enc t t file))))
532 (let ((maps browse-url-filename-alist))
533 (while maps
534 (let* ((map (car maps))
535 (from-re (car map))
536 (to-string (cdr map)))
537 (setq maps (cdr maps))
538 (and (string-match from-re file)
539 (setq file (replace-match to-string t nil file))))))
540 file)
541
542 ;;;###autoload
543 (defun browse-url-of-buffer (&optional buffer)
544 "Ask a WWW browser to display BUFFER.
545 Display the current buffer if BUFFER is nil. Display only the
546 currently visible part of BUFFER (from a temporary file) if buffer is
547 narrowed."
548 (interactive)
549 (save-excursion
550 (and buffer (set-buffer buffer))
551 (let ((file-name
552 ;; Ignore real name if restricted
553 (and (= (- (point-max) (point-min)) (buffer-size))
554 (or buffer-file-name
555 (and (boundp 'dired-directory) dired-directory)))))
556 (or file-name
557 (progn
558 (or browse-url-temp-file-name
559 (setq browse-url-temp-file-name
560 (convert-standard-filename
561 (make-temp-file
562 (expand-file-name "burl" browse-url-temp-dir)))))
563 (setq file-name browse-url-temp-file-name)
564 (write-region (point-min) (point-max) file-name nil 'no-message)))
565 (browse-url-of-file file-name))))
566
567 (defun browse-url-delete-temp-file (&optional temp-file-name)
568 ;; Delete browse-url-temp-file-name from the file system
569 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
570 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
571 (if (and file-name (file-exists-p file-name))
572 (delete-file file-name))))
573
574 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
575
576 ;;;###autoload
577 (defun browse-url-of-dired-file ()
578 "In Dired, ask a WWW browser to display the file named on this line."
579 (interactive)
580 (browse-url-of-file (dired-get-filename)))
581
582 ;;;###autoload
583 (defun browse-url-of-region (min max)
584 "Ask a WWW browser to display the current region."
585 (interactive "r")
586 (save-excursion
587 (save-restriction
588 (narrow-to-region min max)
589 (browse-url-of-buffer))))
590
591 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
592 ;; Browser-independent commands
593
594 ;; A generic command to call the current browse-url-browser-function
595
596 ;;;###autoload
597 (defun browse-url (url &rest args)
598 "Ask a WWW browser to load URL.
599 Prompts for a URL, defaulting to the URL at or before point. Variable
600 `browse-url-browser-function' says which browser to use."
601 (interactive (browse-url-interactive-arg "URL: "))
602 (if (functionp browse-url-browser-function)
603 (apply browse-url-browser-function url args)
604 ;; The `function' can be an alist; look down it for first match
605 ;; and apply the function (which might be a lambda).
606 (catch 'done
607 (mapcar
608 (lambda (bf)
609 (when (string-match (car bf) url)
610 (apply (cdr bf) url args)
611 (throw 'done t)))
612 browse-url-browser-function)
613 (error "No browser in browse-url-browser-function matching URL %s"
614 url))))
615
616 ;;;###autoload
617 (defun browse-url-at-point ()
618 "Ask a WWW browser to load the URL at or before point.
619 Doesn't let you edit the URL like `browse-url'. Variable
620 `browse-url-browser-function' says which browser to use."
621 (interactive)
622 (browse-url (browse-url-url-at-point)))
623
624 (defun browse-url-event-buffer (event)
625 (window-buffer (posn-window (event-start event))))
626
627 (defun browse-url-event-point (event)
628 (posn-point (event-start event)))
629
630 ;;;###autoload
631 (defun browse-url-at-mouse (event)
632 "Ask a WWW browser to load a URL clicked with the mouse.
633 The URL is the one around or before the position of the mouse click
634 but point is not changed. Doesn't let you edit the URL like
635 `browse-url'. Variable `browse-url-browser-function' says which browser
636 to use."
637 (interactive "e")
638 (save-excursion
639 (set-buffer (browse-url-event-buffer event))
640 (goto-char (browse-url-event-point event))
641 (let ((url (browse-url-url-at-point)))
642 (if (string-equal url "")
643 (error "No URL found"))
644 (browse-url url browse-url-new-window-p))))
645
646 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
647 ;; Browser-specific commands
648
649 ;; --- Default MS-Windows browser ---
650
651 (defun browse-url-default-windows-browser (url &optional new-window)
652 (interactive (browse-url-interactive-arg "URL: "))
653 (w32-shell-execute "open" url))
654
655 ;; --- Netscape ---
656
657 (defun browse-url-process-environment ()
658 "Set DISPLAY in the environment to the X display Netscape is running on.
659 This is either the value of variable `browse-url-netscape-display' if
660 non-nil, or the same display as Emacs if different from the current
661 environment, otherwise just use the current environment."
662 (let ((display (or browse-url-netscape-display (browse-url-emacs-display))))
663 (if display
664 (cons (concat "DISPLAY=" display) process-environment)
665 process-environment)))
666
667 (defun browse-url-emacs-display ()
668 "Return the X display Emacs is running on.
669 This is nil if the display is the same as the DISPLAY environment variable.
670
671 Actually Emacs could be using several displays; this just returns the
672 one showing the selected frame."
673 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
674 (and (not (equal display (getenv "DISPLAY")))
675 display)))
676
677 ;;;###autoload
678 (defun browse-url-netscape (url &optional new-window)
679 "Ask the Netscape WWW browser to load URL.
680
681 Default to the URL around or before point. The strings in variable
682 `browse-url-netscape-arguments' are also passed to Netscape.
683
684 When called interactively, if variable `browse-url-new-window-p' is
685 non-nil, load the document in a new Netscape window, otherwise use a
686 random existing one. A non-nil interactive prefix argument reverses
687 the effect of `browse-url-new-window-p'.
688
689 When called non-interactively, optional second argument NEW-WINDOW is
690 used instead of `browse-url-new-window-p'."
691 (interactive (browse-url-interactive-arg "Netscape URL: "))
692 ;; URL encode any `confusing' characters in the URL. This needs to
693 ;; include at least commas; presumably also close parens.
694 (while (string-match "[,)]" url)
695 (setq url (replace-match
696 (format "%%%x" (string-to-char (match-string 0 url))) t t url)))
697 (let* ((process-environment (browse-url-process-environment))
698 (process (apply 'start-process
699 (concat "netscape " url) nil
700 browse-url-netscape-program
701 (append
702 browse-url-netscape-arguments
703 (if (eq window-system 'w32)
704 (list url)
705 (append
706 (if new-window '("-noraise"))
707 (list "-remote"
708 (concat "openURL(" url
709 (if new-window ",new-window")
710 ")"))))))))
711 (set-process-sentinel process
712 (list 'lambda '(process change)
713 (list 'browse-url-netscape-sentinel 'process url)))))
714
715 (defun browse-url-netscape-sentinel (process url)
716 "Handle a change to the process communicating with Netscape."
717 (or (eq (process-exit-status process) 0)
718 (let* ((process-environment (browse-url-process-environment)))
719 ;; Netscape not running - start it
720 (message "Starting Netscape...")
721 (apply 'start-process (concat "netscape" url) nil
722 browse-url-netscape-program
723 (append browse-url-netscape-startup-arguments (list url))))))
724
725 (defun browse-url-netscape-reload ()
726 "Ask Netscape to reload its current document.
727 How depends on `browse-url-netscape-version'."
728 (interactive)
729 ;; Backwards incompatibility reported by
730 ;; <peter.kruse@psychologie.uni-regensburg.de>.
731 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
732 "xfeDoCommand(reload)"
733 "reload")))
734
735 (defun browse-url-netscape-send (command)
736 "Send a remote control command to Netscape."
737 (let* ((process-environment (browse-url-process-environment)))
738 (apply 'start-process "netscape" nil
739 browse-url-netscape-program
740 (append browse-url-netscape-arguments
741 (list "-remote" command)))))
742
743 ;; --- Mosaic ---
744
745 ;;;###autoload
746 (defun browse-url-mosaic (url &optional new-window)
747 "Ask the XMosaic WWW browser to load URL.
748
749 Default to the URL around or before point. The strings in variable
750 `browse-url-mosaic-arguments' are also passed to Mosaic and the
751 program is invoked according to the variable
752 `browse-url-mosaic-program'.
753
754 When called interactively, if variable `browse-url-new-window-p' is
755 non-nil, load the document in a new Mosaic window, otherwise use a
756 random existing one. A non-nil interactive prefix argument reverses
757 the effect of `browse-url-new-window-p'.
758
759 When called non-interactively, optional second argument NEW-WINDOW is
760 used instead of `browse-url-new-window-p'."
761 (interactive (browse-url-interactive-arg "Mosaic URL: "))
762 (let ((pidfile (expand-file-name "~/.mosaicpid"))
763 pid)
764 (if (file-readable-p pidfile)
765 (save-excursion
766 (find-file pidfile)
767 (goto-char (point-min))
768 (setq pid (read (current-buffer)))
769 (kill-buffer nil)))
770 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
771 (save-excursion
772 (find-file (format "/tmp/Mosaic.%d" pid))
773 (erase-buffer)
774 (insert (if new-window
775 "newwin\n"
776 "goto\n")
777 url "\n")
778 (save-buffer)
779 (kill-buffer nil)
780 ;; Send signal SIGUSR to Mosaic
781 (message "Signalling Mosaic...")
782 (signal-process pid browse-url-usr1-signal)
783 ;; Or you could try:
784 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
785 (message "Signalling Mosaic...done")
786 )
787 ;; Mosaic not running - start it
788 (message "Starting Mosaic...")
789 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
790 (append browse-url-mosaic-arguments (list url)))
791 (message "Starting Mosaic...done"))))
792
793 ;; --- Grail ---
794
795 ;;;###autoload
796 (defvar browse-url-grail
797 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
798 "Location of Grail remote control client script `rcgrail.py'.
799 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
800
801 ;;;###autoload
802 (defun browse-url-grail (url &optional new-window)
803 "Ask the Grail WWW browser to load URL.
804 Default to the URL around or before point. Runs the program in the
805 variable `browse-url-grail'."
806 (interactive (browse-url-interactive-arg "Grail URL: "))
807 (message "Sending URL to Grail...")
808 (save-excursion
809 (set-buffer (get-buffer-create " *Shell Command Output*"))
810 (erase-buffer)
811 ;; don't worry about this failing.
812 (if new-window
813 (call-process browse-url-grail nil 0 nil "-b" url)
814 (call-process browse-url-grail nil 0 nil url))
815 (message "Sending URL to Grail... done")))
816
817 ;; --- Mosaic using CCI ---
818
819 ;;;###autoload
820 (defun browse-url-cci (url &optional new-window)
821 "Ask the XMosaic WWW browser to load URL.
822 Default to the URL around or before point.
823
824 This function only works for XMosaic version 2.5 or later. You must
825 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
826 value of variable `browse-url-CCI-port', and enable `Accept requests'.
827
828 When called interactively, if variable `browse-url-new-window-p' is
829 non-nil, load the document in a new browser window, otherwise use a
830 random existing one. A non-nil interactive prefix argument reverses
831 the effect of `browse-url-new-window-p'.
832
833 When called non-interactively, optional second argument NEW-WINDOW is
834 used instead of `browse-url-new-window-p'."
835 (interactive (browse-url-interactive-arg "Mosaic URL: "))
836 (open-network-stream "browse-url" " *browse-url*"
837 browse-url-CCI-host browse-url-CCI-port)
838 ;; Todo: start browser if fails
839 (process-send-string "browse-url"
840 (concat "get url (" url ") output "
841 (if new-window
842 "new"
843 "current")
844 "\r\n"))
845 (process-send-string "browse-url" "disconnect\r\n")
846 (delete-process "browse-url"))
847
848 ;; --- IXI Mosaic ---
849
850 ;;;###autoload
851 (defun browse-url-iximosaic (url &optional new-window)
852 ;; new-window ignored
853 "Ask the IXIMosaic WWW browser to load URL.
854 Default to the URL around or before point."
855 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
856 (start-process "tellw3b" nil "tellw3b"
857 "-service WWW_BROWSER ixi_showurl " url))
858
859 ;; --- W3 ---
860
861 ;;;###autoload
862 (defun browse-url-w3 (url &optional new-window)
863 "Ask the w3 WWW browser to load URL.
864 Default to the URL around or before point.
865
866 When called interactively, if variable `browse-url-new-window-p' is
867 non-nil, load the document in a new window. A non-nil interactive
868 prefix argument reverses the effect of `browse-url-new-window-p'.
869
870 When called non-interactively, optional second argument NEW-WINDOW is
871 used instead of `browse-url-new-window-p'."
872 (interactive (browse-url-interactive-arg "W3 URL: "))
873 (require 'w3) ; w3-fetch-other-window not autoloaded
874 (if new-window
875 (w3-fetch-other-window url)
876 (w3-fetch url)))
877
878 ;;;###autoload
879 (defun browse-url-w3-gnudoit (url &optional new-window)
880 ;; new-window ignored
881 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
882 The `browse-url-gnudoit-program' program is used with options given by
883 `browse-url-gnudoit-args'. Default to the URL around or before point."
884 (interactive (browse-url-interactive-arg "W3 URL: "))
885 (apply 'start-process (concat "gnudoit:" url) nil
886 browse-url-gnudoit-program
887 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
888
889 ;; --- Lynx in an xterm ---
890
891 ;;;###autoload
892 (defun browse-url-lynx-xterm (url &optional new-window)
893 ;; new-window ignored
894 "Ask the Lynx WWW browser to load URL.
895 Default to the URL around or before point. A new Lynx process is run
896 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
897 with possible additional arguments `browse-url-xterm-args'."
898 (interactive (browse-url-interactive-arg "Lynx URL: "))
899 (apply #'start-process `(,(concat "lynx" url) nil ,browse-url-xterm-program
900 ,@browse-url-xterm-args "-e" "lynx" ,url)))
901
902 ;; --- Lynx in an Emacs "term" window ---
903
904 ;;;###autoload
905 (defun browse-url-lynx-emacs (url &optional new-buffer)
906 "Ask the Lynx WWW browser to load URL.
907 Default to the URL around or before point. With a prefix argument, run
908 a new Lynx process in a new buffer.
909
910 When called interactively, if variable `browse-url-new-window-p' is
911 non-nil, load the document in a new lynx in a new term window,
912 otherwise use any existing one. A non-nil interactive prefix argument
913 reverses the effect of `browse-url-new-window-p'.
914
915 When called non-interactively, optional second argument NEW-WINDOW is
916 used instead of `browse-url-new-window-p'."
917 (interactive (browse-url-interactive-arg "Lynx URL: "))
918 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
919 ;; (term-term-name "vt100") ; ??
920 (buf (get-buffer "*lynx*"))
921 (proc (and buf (get-buffer-process buf)))
922 (n browse-url-lynx-input-attempts))
923 (if (and new-buffer buf)
924 ;; Rename away the OLD buffer. This isn't very polite, but
925 ;; term insists on working in a buffer named *lynx* and would
926 ;; choke on *lynx*<1>
927 (progn (set-buffer buf)
928 (rename-uniquely)))
929 (if (or new-buffer
930 (not buf)
931 (not proc)
932 (not (memq (process-status proc) '(run stop))))
933 ;; start a new lynx
934 (progn
935 (setq buf
936 (apply #'make-term
937 `("lynx" "lynx" nil ,@browse-url-lynx-emacs-args ,url)))
938 (switch-to-buffer buf)
939 (term-char-mode)
940 (set-process-sentinel
941 (get-buffer-process buf)
942 ;; Don't leave around a dead one (especially because of its
943 ;; munged keymap.)
944 (lambda (process event)
945 (if (not (memq (process-status process) '(run stop)))
946 (let ((buf (process-buffer process)))
947 (if buf (kill-buffer buf)))))))
948 ;; send the url to lynx in the old buffer
949 (let ((win (get-buffer-window buf t)))
950 (if win
951 (select-window win)
952 (switch-to-buffer buf)))
953 (if (eq (following-char) ?_)
954 (cond ((eq browse-url-lynx-input-field 'warn)
955 (error "Please move out of the input field first."))
956 ((eq browse-url-lynx-input-field 'avoid)
957 (while (and (eq (following-char) ?_) (> n 0))
958 (term-send-down) ; down arrow
959 (sit-for browse-url-lynx-input-delay))
960 (if (eq (following-char) ?_)
961 (error "Cannot move out of the input field, sorry.")))))
962 (term-send-string proc (concat "g" ; goto
963 "\C-u" ; kill default url
964 url
965 "\r")))))
966
967 ;; --- MMM ---
968
969 ;;;###autoload
970 (defun browse-url-mmm (url &optional new-window)
971 "Ask the MMM WWW browser to load URL.
972 Default to the URL around or before point."
973 (interactive (browse-url-interactive-arg "MMM URL: "))
974 (message "Sending URL to MMM...")
975 (save-excursion
976 (set-buffer (get-buffer-create " *Shell Command Output*"))
977 (erase-buffer)
978 ;; mmm_remote just SEGVs if the file isn't there...
979 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
980 ;; location in v 0.4:
981 (file-exists-p (expand-file-name "~/.mmm/remote")))
982 (call-process "mmm_remote" nil 0 nil url)
983 (call-process "mmm" nil 0 nil "-external" url))
984 (message "Sending URL to MMM... done")))
985
986 ;; --- mailto ---
987
988 ;;;###autoload
989 (defun browse-url-mail (url &optional new-window)
990 "Open a new mail message buffer within Emacs.
991 Default to using the mailto: URL around or before point as the
992 recipient's address. Supplying a non-nil interactive prefix argument
993 will cause the mail to be composed in another window rather than the
994 current one.
995
996 When called interactively, if variable `browse-url-new-window-p' is
997 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
998 non-nil interactive prefix argument reverses the effect of
999 `browse-url-new-window-p'.
1000
1001 When called non-interactively, optional second argument NEW-WINDOW is
1002 used instead of `browse-url-new-window-p'."
1003 (interactive (browse-url-interactive-arg "Mailto URL: "))
1004 (save-excursion
1005 (let ((to (if (string-match "^mailto:" url)
1006 (substring url 7)
1007 url)))
1008 (if new-window
1009 (compose-mail-other-window to nil nil nil
1010 (list 'insert-buffer (current-buffer)))
1011 (compose-mail to nil nil nil nil
1012 (list 'insert-buffer (current-buffer)))))))
1013
1014 ;; --- Random browser ---
1015
1016 ;;;###autoload
1017 (defun browse-url-generic (url &optional new-window)
1018 ;; new-window ignored
1019 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1020 Default to the URL around or before point. A fresh copy of the
1021 browser is started up in a new process with possible additional arguments
1022 `browse-url-generic-args'. This is appropriate for browsers which
1023 don't offer a form of remote control."
1024 (interactive (browse-url-interactive-arg "URL: "))
1025 (if (not browse-url-generic-program)
1026 (error "No browser defined (`browse-url-generic-program')"))
1027 (apply 'start-process (concat browse-url-generic-program url) nil
1028 browse-url-generic-program
1029 (append browse-url-generic-args (list url))))
1030
1031 (provide 'browse-url)
1032
1033 ;;; browse-url.el ends here