Setup auto-fill-chars.
[bpt/emacs.git] / lisp / browse-url.el
1 ;;; browse-url.el --- Pass a URL to a WWW browser
2
3 ;; Copyright 1995, 1996, 1997, 1998 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 ;; [The Netscape browser is now free software
50 ;; <URL:http://www.mozilla.org/>, albeit not GPLed, so it is
51 ;; reasonable to keep it 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 the freely available WWW browser implemented in Objective
89 ;; Caml, 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> and Amaya
100 ;; <URL:ftp://ftp.w3.org/pub/amaya>. mMosaic
101 ;; <URL:ftp://sig.enst.fr/pub/multicast/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
227 (defgroup browse-url nil
228 "Use a web browser to look at a URL."
229 :prefix "browse-url-"
230 :group 'hypermedia)
231
232 ;;;###autoload
233 (defcustom browse-url-browser-function
234 'browse-url-netscape
235 "*Function to display the current buffer in a WWW browser.
236 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
237 `browse-url-of-file' commands.
238
239 If the value is not a function it should be a list of pairs
240 (REGEXP . FUNCTION). In this case the function called will be the one
241 associated with the first REGEXP which matches the current URL. The
242 function is passed the URL and any other args of `browse-url'. The last
243 regexp should probably be \".\" to specify a default browser."
244 :type '(choice
245 (function-item :tag "Emacs W3" :value browse-url-w3)
246 (function-item :tag "W3 in another Emacs via `gnudoit'"
247 :value browse-url-w3-gnudoit)
248 (function-item :tag "Netscape" :value browse-url-netscape)
249 (function-item :tag "Mosaic" :value browse-url-mosaic)
250 (function-item :tag "Mosaic using CCI" :value browse-url-cci)
251 (function-item :tag "IXI Mosaic" :value browse-url-iximosaic)
252 (function-item :tag "Lynx in an xterm window"
253 :value browse-url-lynx-xterm)
254 (function-item :tag "Lynx in an Emacs window"
255 :value browse-url-lynx-emacs)
256 (function-item :tag "Grail" :value browse-url-grail)
257 (function-item :tag "MMM" :value browse-url-mmm)
258 (function-item :tag "Specified by `Browse Url Generic Program'"
259 :value browse-url-generic)
260 (function :tag "Your own function"))
261 :group 'browse-url)
262
263 (defcustom browse-url-netscape-program "netscape"
264 ;; Info about netscape-remote from Kurt Swanson in gnu.emacs.gnus
265 "The name by which to invoke Netscape.
266
267 It is said that source is available for a program `netscape-remote'
268 which starts up very much quicker than `netscape' and that it is
269 useful to set this variable to the name of a script which invokes that
270 program like:
271 #!/bin/sh
272 /usr/local/bin/netscape-remote \"$@\" > /dev/null 2>&1
273 "
274 :type 'string
275 :group 'browse-url)
276
277 (defcustom browse-url-netscape-arguments nil
278 "A list of strings to pass to Netscape as arguments."
279 :type '(repeat (string :tag "Argument"))
280 :group 'browse-url)
281
282 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
283 "A list of strings to pass to Netscape when it starts up.
284 Defaults to the value of `browse-url-netscape-arguments' at the time
285 `browse-url' is loaded."
286 :type '(repeat (string :tag "Argument"))
287 :group 'browse-url)
288
289 ;;;###autoload
290 (defcustom browse-url-new-window-p nil
291 "*If non-nil, always open a new browser window with appropriate browsers.
292 Passing an interactive argument to \\[browse-url], or specific browser
293 commands reverses the effect of this variable. Requires Netscape version
294 1.1N or later or XMosaic version 2.5 or later if using those browsers."
295 :type 'boolean
296 :group 'browse-url)
297
298 ;;;###autoload
299 (defcustom browse-url-netscape-display nil
300 "*The X display for running Netscape, if not same as Emacs'."
301 :type '(choice string (const :tag "Default" nil))
302 :group 'browse-url)
303
304 (defcustom browse-url-mosaic-program "xmosaic"
305 "The name by which to invoke Mosaic (or mMosaic)."
306 :type 'string
307 :version "20.3"
308 :group 'browse-url)
309
310 (defcustom browse-url-mosaic-arguments nil
311 "A list of strings to pass to Mosaic as arguments."
312 :type '(repeat (string :tag "Argument"))
313 :group 'browse-url)
314
315 (defcustom browse-url-filename-alist
316 '(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
317 ;; The above loses the username to avoid the browser prompting for
318 ;; it in anonymous cases. If it's not anonymous the next regexp
319 ;; applies.
320 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
321 ("^/+" . "file:/"))
322 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
323 Any substring of a filename matching one of the REGEXPs is replaced by
324 the corresponding STRING using `replace-match', not treating STRING
325 literally. All pairs are applied in the order given. The default
326 value converts ange-ftp/EFS-style paths into ftp URLs and prepends
327 `file:' to any path beginning with `/'.
328
329 For example, adding to the default a specific translation of an ange-ftp
330 address to an HTTP URL:
331
332 (setq browse-url-filename-alist
333 '((\"/webmaster@webserver:/home/www/html/\" .
334 \"http://www.acme.co.uk/\")
335 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
336 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
337 (\"^/+\" . \"file:/\")))
338 "
339 :type '(repeat (cons :format "%v"
340 (regexp :tag "Regexp")
341 (string :tag "Replacement")))
342 :version "20.3"
343 :group 'browse-url)
344
345 ;;;###autoload
346 (defcustom browse-url-save-file nil
347 "*If non-nil, save the buffer before displaying its file.
348 Used by the `browse-url-of-file' command."
349 :type 'boolean
350 :group 'browse-url)
351
352 (defcustom browse-url-of-file-hook nil
353 "Run after `browse-url-of-file' has asked a browser to load a file.
354
355 Set this to `browse-url-netscape-reload' to force Netscape to load the
356 file rather than displaying a cached copy."
357 :type 'hook
358 :options '(browse-url-netscape-reload)
359 :group 'browse-url)
360
361 (defvar browse-url-usr1-signal
362 (if (and (boundp 'emacs-major-version)
363 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
364 'SIGUSR1 ; Why did I think this was in lower case before?
365 30) ; Check /usr/include/signal.h.
366 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
367 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
368 which is 30 on SunOS and 16 on HP-UX and Solaris.")
369
370 (defcustom browse-url-CCI-port 3003
371 "Port to access XMosaic via CCI.
372 This can be any number between 1024 and 65535 but must correspond to
373 the value set in the browser."
374 :type 'integer
375 :group 'browse-url)
376
377 (defcustom browse-url-CCI-host "localhost"
378 "*Host to access XMosaic via CCI.
379 This should be the host name of the machine running XMosaic with CCI
380 enabled. The port number should be set in `browse-url-CCI-port'."
381 :type 'string
382 :group 'browse-url)
383
384 (defvar browse-url-temp-file-name nil)
385 (make-variable-buffer-local 'browse-url-temp-file-name)
386
387 (defcustom browse-url-xterm-program "xterm"
388 "The name of the terminal emulator used by `browse-url-lynx-xterm'.
389 This might, for instance, be a separate colour version of xterm."
390 :type 'string
391 :group 'browse-url)
392
393 (defcustom browse-url-xterm-args nil
394 "*A list of strings defining options for `browse-url-xterm-program'.
395 These might set its size, for instance."
396 :type '(repeat (string :tag "Argument"))
397 :group 'browse-url)
398
399 (defcustom browse-url-lynx-emacs-args (and (not window-system)
400 '("-show_cursor"))
401 "A list of strings defining options for Lynx in an Emacs buffer.
402
403 The default is none in a window system, otherwise `-show_cursor' to
404 indicate the position of the current link in the absence of
405 highlighting, assuming the normal default for showing the cursor."
406 :type '(repeat (string :tag "Argument"))
407 :version "20.3"
408 :group 'browse-url)
409
410 (defcustom browse-url-gnudoit-program "gnudoit"
411 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
412 :type 'string
413 :group 'browse-url)
414
415 (defcustom browse-url-gnudoit-args '("-q")
416 "*A list of strings defining options for `browse-url-gnudoit-program'.
417 These might set the port, for instance."
418 :type '(repeat (string :tag "Argument"))
419 :group 'browse-url)
420
421 ;;;###autoload
422 (defcustom browse-url-generic-program nil
423 "*The name of the browser program used by `browse-url-generic'."
424 :type '(choice string (const :tag "None" nil))
425 :group 'browse-url)
426
427 (defcustom browse-url-generic-args nil
428 "*A list of strings defining options for `browse-url-generic-program'."
429 :type '(repeat (string :tag "Argument"))
430 :group 'browse-url)
431
432 (defcustom browse-url-temp-dir temporary-file-directory
433 "The name of a directory for browse-url's temporary files.
434 Such files are generated by functions like `browse-url-of-region'.
435 You might want to set this to somewhere with restricted read permissions
436 for privacy's sake."
437 :type 'string
438 :group 'browse-url)
439
440 (defcustom browse-url-netscape-version
441 3
442 "The version of Netscape you are using.
443 This affects how URL reloading is done; the mechanism changed
444 incompatibly at version 4."
445 :type 'number
446 :group 'browse-url)
447
448 (defcustom browse-url-lynx-input-field 'avoid
449 "*Action on selecting an existing Lynx buffer at an input field.
450 What to do when sending a new URL to an existing Lynx buffer in Emacs
451 if the Lynx cursor is on an input field (in which case the `g' command
452 would be entered as data). Such fields are recognized by the
453 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
454 user and don't emit the URL, 'avoid: try to avoid the field by moving
455 down (this *won't* always work)."
456 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
457 (const :tag "Disregard" :value nil)
458 (const :tag "Warn, don't emit URL" :value warn))
459 :version "20.3"
460 :group 'browse-url)
461
462 (defvar browse-url-lynx-input-attempts 10
463 "*How many times to try to move down from a series of lynx input fields.")
464
465 (defcustom browse-url-lynx-input-delay 0.2
466 "How many seconds to wait for lynx between moves down from an input field.")
467
468 (defvar browse-url-temp-file-list '())
469
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 ;; URL input
472
473 (defun browse-url-url-at-point ()
474 (let ((url (thing-at-point 'url)))
475 (set-text-properties 0 (length url) nil url)
476 url))
477
478 ;; Having this as a separate function called by the browser-specific
479 ;; functions allows them to be stand-alone commands, making it easier
480 ;; to switch between browsers.
481
482 (defun browse-url-interactive-arg (prompt)
483 "Read a URL from the minibuffer, prompting with PROMPT.
484 Default to the URL at or before point. If invoked with a mouse button,
485 set point to the position clicked first. Return a list for use in
486 `interactive' containing the URL and `browse-url-new-window-p' or its
487 negation if a prefix argument was given."
488 (let ((event (elt (this-command-keys) 0)))
489 (and (listp event) (mouse-set-point event)))
490 (list (read-string prompt (browse-url-url-at-point))
491 (not (eq (null browse-url-new-window-p)
492 (null current-prefix-arg)))))
493
494 (defun browse-url-maybe-new-window (arg)
495 (if (interactive-p)
496 arg
497 browse-url-new-window-p))
498
499 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
500 ;; Browse current buffer
501
502 ;;;###autoload
503 (defun browse-url-of-file (&optional file)
504 "Ask a WWW browser to display FILE.
505 Display the current buffer's file if FILE is nil or if called
506 interactively. Turn the filename into a URL with function
507 `browse-url-file-url'. Pass the URL to a browser using the
508 `browse-url' function then run `browse-url-of-file-hook'."
509 (interactive)
510 (or file
511 (setq file (buffer-file-name))
512 (error "Current buffer has no file"))
513 (let ((buf (get-file-buffer file)))
514 (if buf
515 (save-excursion
516 (set-buffer buf)
517 (cond ((not (buffer-modified-p)))
518 (browse-url-save-file (save-buffer))
519 (t (message "%s modified since last save" file))))))
520 (browse-url (browse-url-file-url file))
521 (run-hooks 'browse-url-of-file-hook))
522
523 (defun browse-url-file-url (file)
524 "Return the URL corresponding to FILE.
525 Use variable `browse-url-filename-alist' to map filenames to URLs."
526 ;; URL-encode special chars, do % first
527 (let ((s 0))
528 (while (setq s (string-match "%" file s))
529 (setq file (replace-match "%25" t t file)
530 s (1+ s))))
531 (while (string-match "[*\"()',=;? ]" file)
532 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
533 (setq file (replace-match enc t t file))))
534 (let ((maps browse-url-filename-alist))
535 (while maps
536 (let* ((map (car maps))
537 (from-re (car map))
538 (to-string (cdr map)))
539 (setq maps (cdr maps))
540 (and (string-match from-re file)
541 (setq file (replace-match to-string t nil file))))))
542 file)
543
544 ;;;###autoload
545 (defun browse-url-of-buffer (&optional buffer)
546 "Ask a WWW browser to display BUFFER.
547 Display the current buffer if BUFFER is nil. Display only the
548 currently visible part of BUFFER (from a temporary file) if buffer is
549 narrowed."
550 (interactive)
551 (save-excursion
552 (and buffer (set-buffer buffer))
553 (let ((file-name
554 ;; Ignore real name if restricted
555 (and (= (- (point-max) (point-min)) (buffer-size))
556 (or buffer-file-name
557 (and (boundp 'dired-directory) dired-directory)))))
558 (or file-name
559 (progn
560 (or browse-url-temp-file-name
561 (setq browse-url-temp-file-name
562 (convert-standard-filename
563 (make-temp-name
564 (expand-file-name "burl" browse-url-temp-dir)))))
565 (setq file-name browse-url-temp-file-name)
566 (write-region (point-min) (point-max) file-name nil 'no-message)))
567 (browse-url-of-file file-name))))
568
569 (defun browse-url-delete-temp-file (&optional temp-file-name)
570 ;; Delete browse-url-temp-file-name from the file system and from
571 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
572 ;; non-nil, delete it instead, but only from the file system --
573 ;; browse-url-temp-file-list is not affected.
574 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
575 (if (and file-name (file-exists-p file-name))
576 (progn
577 (delete-file file-name)
578 (if (null temp-file-name)
579 (setq browse-url-temp-file-list
580 (delete browse-url-temp-file-name
581 browse-url-temp-file-list)))))))
582
583 (defun browse-url-delete-temp-file-list ()
584 ;; Delete all elements of browse-url-temp-file-list.
585 (while browse-url-temp-file-list
586 (browse-url-delete-temp-file (car browse-url-temp-file-list))
587 (setq browse-url-temp-file-list
588 (cdr browse-url-temp-file-list))))
589
590 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
591 (add-hook 'kill-emacs-hook 'browse-url-delete-temp-file-list)
592
593 ;;;###autoload
594 (defun browse-url-of-dired-file ()
595 "In Dired, ask a WWW browser to display the file named on this line."
596 (interactive)
597 (browse-url-of-file (dired-get-filename)))
598
599 ;;;###autoload
600 (defun browse-url-of-region (min max)
601 "Ask a WWW browser to display the current region."
602 (interactive "r")
603 (save-excursion
604 (save-restriction
605 (narrow-to-region min max)
606 (browse-url-of-buffer))))
607
608 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
609 ;; Browser-independent commands
610
611 ;; A generic command to call the current browse-url-browser-function
612
613 ;;;###autoload
614 (defun browse-url (url &rest args)
615 "Ask a WWW browser to load URL.
616 Prompts for a URL, defaulting to the URL at or before point. Variable
617 `browse-url-browser-function' says which browser to use."
618 (interactive (browse-url-interactive-arg "URL: "))
619 (let ((bf browse-url-browser-function) re)
620 (while (consp bf)
621 (setq re (car (car bf))
622 bf (if (string-match re url)
623 (cdr (car bf)) ; The function
624 (cdr bf)))) ; More pairs
625 (or bf (error "No browser in browse-url-browser-function matching URL %s"
626 url))
627 (apply bf url args)))
628
629 ;;;###autoload
630 (defun browse-url-at-point ()
631 "Ask a WWW browser to load the URL at or before point.
632 Doesn't let you edit the URL like `browse-url'. Variable
633 `browse-url-browser-function' says which browser to use."
634 (interactive)
635 (browse-url (browse-url-url-at-point)))
636
637 (defun browse-url-event-buffer (event)
638 (window-buffer (posn-window (event-start event))))
639
640 (defun browse-url-event-point (event)
641 (posn-point (event-start event)))
642
643 ;;;###autoload
644 (defun browse-url-at-mouse (event)
645 "Ask a WWW browser to load a URL clicked with the mouse.
646 The URL is the one around or before the position of the mouse click
647 but point is not changed. Doesn't let you edit the URL like
648 `browse-url'. Variable `browse-url-browser-function' says which browser
649 to use."
650 (interactive "e")
651 (save-excursion
652 (set-buffer (browse-url-event-buffer event))
653 (goto-char (browse-url-event-point event))
654 (let ((url (browse-url-url-at-point)))
655 (if (string-equal url "")
656 (error "No URL found"))
657 (browse-url url browse-url-new-window-p))))
658
659 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
660 ;; Browser-specific commands
661
662 ;; --- Netscape ---
663
664 (defun browse-url-process-environment ()
665 "Set DISPLAY in the environment to the X display Netscape is running on.
666 This is either the value of variable `browse-url-netscape-display' if
667 non-nil, or the same display as Emacs if different from the current
668 environment, otherwise just use the current environment."
669 (let ((display (or browse-url-netscape-display (browse-url-emacs-display))))
670 (if display
671 (cons (concat "DISPLAY=" display) process-environment)
672 process-environment)))
673
674 (defun browse-url-emacs-display ()
675 "Return the X display Emacs is running on.
676 This is nil if the display is the same as the DISPLAY environment variable.
677
678 Actually Emacs could be using several displays; this just returns the
679 one showing the selected frame."
680 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
681 (and (not (equal display (getenv "DISPLAY")))
682 display)))
683
684 ;;;###autoload
685 (defun browse-url-netscape (url &optional new-window)
686 "Ask the Netscape WWW browser to load URL.
687
688 Default to the URL around or before point. The strings in variable
689 `browse-url-netscape-arguments' are also passed to Netscape.
690
691 When called interactively, if variable `browse-url-new-window-p' is
692 non-nil, load the document in a new Netscape window, otherwise use a
693 random existing one. A non-nil interactive prefix argument reverses
694 the effect of `browse-url-new-window-p'.
695
696 When called non-interactively, optional second argument NEW-WINDOW is
697 used instead of `browse-url-new-window-p'."
698 (interactive (browse-url-interactive-arg "Netscape URL: "))
699 ;; URL encode any `confusing' characters in the URL. This needs to
700 ;; include at least commas; presumably also close parens.
701 (while (string-match "[,)]" url)
702 (setq url (replace-match
703 (format "%%%x" (string-to-char (match-string 0 url))) t t url)))
704 (let* ((process-environment (browse-url-process-environment))
705 (process (apply 'start-process
706 (concat "netscape " url) nil
707 browse-url-netscape-program
708 (append browse-url-netscape-arguments
709 (if (eq window-system 'w32)
710 (list url)
711 (if new-window '("-noraise"))
712 (list "-remote"
713 (concat "openURL(" url
714 (if (browse-url-maybe-new-window
715 new-window)
716 ",new-window")
717 ")")))))))
718 (set-process-sentinel process
719 (list 'lambda '(process change)
720 (list 'browse-url-netscape-sentinel 'process url)))))
721
722 (defun browse-url-netscape-sentinel (process url)
723 "Handle a change to the process communicating with Netscape."
724 (or (eq (process-exit-status process) 0)
725 (let* ((process-environment (browse-url-process-environment)))
726 ;; Netscape not running - start it
727 (message "Starting Netscape...")
728 (apply 'start-process (concat "netscape" url) nil
729 browse-url-netscape-program
730 (append browse-url-netscape-startup-arguments (list url))))))
731
732 (defun browse-url-netscape-reload ()
733 "Ask Netscape to reload its current document.
734 How depends on `browse-url-netscape-version'."
735 (interactive)
736 ;; Backwards incompatibility reported by
737 ;; <peter.kruse@psychologie.uni-regensburg.de>.
738 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
739 "xfeDoCommand(reload)"
740 "reload")))
741
742 (defun browse-url-netscape-send (command)
743 "Send a remote control command to Netscape."
744 (let* ((process-environment (browse-url-process-environment)))
745 (apply 'start-process "netscape" nil
746 browse-url-netscape-program
747 (append browse-url-netscape-arguments
748 (list "-remote" command)))))
749
750 ;; --- Mosaic ---
751
752 ;;;###autoload
753 (defun browse-url-mosaic (url &optional new-window)
754 ;; new-window ignored
755 "Ask the XMosaic WWW browser to load URL.
756
757 Default to the URL around or before point. The strings in variable
758 `browse-url-mosaic-arguments' are also passed to Mosaic and the
759 program is invoked according to the variable
760 `browse-url-mosaic-program'.
761
762 When called interactively, if variable `browse-url-new-window-p' is
763 non-nil, load the document in a new Mosaic window, otherwise use a
764 random existing one. A non-nil interactive prefix argument reverses
765 the effect of `browse-url-new-window-p'.
766
767 When called non-interactively, optional second argument NEW-WINDOW is
768 used instead of `browse-url-new-window-p'."
769 (interactive (browse-url-interactive-arg "Mosaic URL: "))
770 (let ((pidfile (expand-file-name "~/.mosaicpid"))
771 pid)
772 (if (file-readable-p pidfile)
773 (save-excursion
774 (find-file pidfile)
775 (goto-char (point-min))
776 (setq pid (read (current-buffer)))
777 (kill-buffer nil)))
778 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
779 (save-excursion
780 (find-file (format "/tmp/Mosaic.%d" pid))
781 (erase-buffer)
782 (insert (if (browse-url-maybe-new-window new-window)
783 "newwin\n"
784 "goto\n")
785 url "\n")
786 (save-buffer)
787 (kill-buffer nil)
788 ;; Send signal SIGUSR to Mosaic
789 (message "Signalling Mosaic...")
790 (signal-process pid browse-url-usr1-signal)
791 ;; Or you could try:
792 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
793 (message "Signalling Mosaic...done")
794 )
795 ;; Mosaic not running - start it
796 (message "Starting Mosaic...")
797 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
798 (append browse-url-mosaic-arguments (list url)))
799 (message "Starting Mosaic...done"))))
800
801 ;; --- Grail ---
802
803 ;;;###autoload
804 (defvar browse-url-grail
805 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
806 "Location of Grail remote control client script `rcgrail.py'.
807 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
808
809 ;;;###autoload
810 (defun browse-url-grail (url &optional new-window)
811 "Ask the Grail WWW browser to load URL.
812 Default to the URL around or before point. Runs the program in the
813 variable `browse-url-grail'."
814 (interactive (browse-url-interactive-arg "Grail URL: "))
815 (message "Sending URL to Grail...")
816 (save-excursion
817 (set-buffer (get-buffer-create " *Shell Command Output*"))
818 (erase-buffer)
819 ;; don't worry about this failing.
820 (if new-window
821 (call-process browse-url-grail nil 0 nil "-b" url)
822 (call-process browse-url-grail nil 0 nil url))
823 (message "Sending URL to Grail... done")))
824
825 ;; --- Mosaic using CCI ---
826
827 ;;;###autoload
828 (defun browse-url-cci (url &optional new-window)
829 "Ask the XMosaic WWW browser to load URL.
830 Default to the URL around or before point.
831
832 This function only works for XMosaic version 2.5 or later. You must
833 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
834 value of variable `browse-url-CCI-port', and enable `Accept requests'.
835
836 When called interactively, if variable `browse-url-new-window-p' is
837 non-nil, load the document in a new browser window, otherwise use a
838 random existing one. A non-nil interactive prefix argument reverses
839 the effect of `browse-url-new-window-p'.
840
841 When called non-interactively, optional second argument NEW-WINDOW is
842 used instead of `browse-url-new-window-p'."
843 (interactive (browse-url-interactive-arg "Mosaic URL: "))
844 (open-network-stream "browse-url" " *browse-url*"
845 browse-url-CCI-host browse-url-CCI-port)
846 ;; Todo: start browser if fails
847 (process-send-string "browse-url"
848 (concat "get url (" url ") output "
849 (if (browse-url-maybe-new-window
850 new-window)
851 "new"
852 "current")
853 "\r\n"))
854 (process-send-string "browse-url" "disconnect\r\n")
855 (delete-process "browse-url"))
856
857 ;; --- IXI Mosaic ---
858
859 ;;;###autoload
860 (defun browse-url-iximosaic (url &optional new-window)
861 ;; new-window ignored
862 "Ask the IXIMosaic WWW browser to load URL.
863 Default to the URL around or before point."
864 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
865 (start-process "tellw3b" nil "tellw3b"
866 "-service WWW_BROWSER ixi_showurl " url))
867
868 ;; --- W3 ---
869
870 ;;;###autoload
871 (defun browse-url-w3 (url &optional new-window)
872 "Ask the w3 WWW browser to load URL.
873 Default to the URL around or before point.
874
875 When called interactively, if variable `browse-url-new-window-p' is
876 non-nil, load the document in a new window. A non-nil interactive
877 prefix argument reverses the effect of `browse-url-new-window-p'.
878
879 When called non-interactively, optional second argument NEW-WINDOW is
880 used instead of `browse-url-new-window-p'."
881 (interactive (browse-url-interactive-arg "W3 URL: "))
882 (if (browse-url-maybe-new-window new-window)
883 (w3-fetch-other-window)
884 (w3-fetch url)))
885
886 ;;;###autoload
887 (defun browse-url-w3-gnudoit (url &optional new-window)
888 ;; new-window ignored
889 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
890 The `browse-url-gnudoit-program' program is used with options given by
891 `browse-url-gnudoit-args'. Default to the URL around or before point."
892 (interactive (browse-url-interactive-arg "W3 URL: "))
893 (apply 'start-process (concat "gnudoit:" url) nil
894 browse-url-gnudoit-program
895 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
896
897 ;; --- Lynx in an xterm ---
898
899 ;;;###autoload
900 (defun browse-url-lynx-xterm (url &optional new-window)
901 ;; new-window ignored
902 "Ask the Lynx WWW browser to load URL.
903 Default to the URL around or before point. A new Lynx process is run
904 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
905 with possible additional arguments `browse-url-xterm-args'."
906 (interactive (browse-url-interactive-arg "Lynx URL: "))
907 (apply #'start-process `(,(concat "lynx" url) nil ,browse-url-xterm-program
908 ,@browse-url-xterm-args "-e" "lynx" ,url)))
909
910 ;; --- Lynx in an Emacs "term" window ---
911
912 ;;;###autoload
913 (defun browse-url-lynx-emacs (url &optional new-buffer)
914 "Ask the Lynx WWW browser to load URL.
915 Default to the URL around or before point. With a prefix argument, run
916 a new Lynx process in a new buffer.
917
918 When called interactively, if variable `browse-url-new-window-p' is
919 non-nil, load the document in a new lynx in a new term window,
920 otherwise use any existing one. A non-nil interactive prefix argument
921 reverses the effect of `browse-url-new-window-p'.
922
923 When called non-interactively, optional second argument NEW-WINDOW is
924 used instead of `browse-url-new-window-p'."
925 (interactive (browse-url-interactive-arg "Lynx URL: "))
926 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
927 ;; (term-term-name "vt100") ; ??
928 (buf (get-buffer "*lynx*"))
929 (proc (and buf (get-buffer-process buf)))
930 (n browse-url-lynx-input-attempts))
931 (if (and (browse-url-maybe-new-window new-buffer) buf)
932 ;; Rename away the OLD buffer. This isn't very polite, but
933 ;; term insists on working in a buffer named *lynx* and would
934 ;; choke on *lynx*<1>
935 (progn (set-buffer buf)
936 (rename-uniquely)))
937 (if (or (browse-url-maybe-new-window new-buffer)
938 (not buf)
939 (not proc)
940 (not (memq (process-status proc) '(run stop))))
941 ;; start a new lynx
942 (progn
943 (setq buf
944 (apply #'make-term
945 `("lynx" "lynx" nil ,@browse-url-lynx-emacs-args ,url)))
946 (switch-to-buffer buf)
947 (term-char-mode)
948 (set-process-sentinel
949 (get-buffer-process buf)
950 ;; Don't leave around a dead one (especially because of its
951 ;; munged keymap.)
952 (lambda (process event)
953 (if (not (memq (process-status process) '(run stop)))
954 (let ((buf (process-buffer process)))
955 (if buf (kill-buffer buf)))))))
956 ;; send the url to lynx in the old buffer
957 (let ((win (get-buffer-window buf t)))
958 (if win
959 (select-window win)
960 (switch-to-buffer buf)))
961 (if (eq (following-char) ?_)
962 (cond ((eq browse-url-lynx-input-field 'warn)
963 (error "Please move out of the input field first."))
964 ((eq browse-url-lynx-input-field 'avoid)
965 (while (and (eq (following-char) ?_) (> n 0))
966 (term-send-down) ; down arrow
967 (sit-for browse-url-lynx-input-delay))
968 (if (eq (following-char) ?_)
969 (error "Cannot move out of the input field, sorry.")))))
970 (term-send-string proc (concat "g" ; goto
971 "\C-u" ; kill default url
972 url
973 "\r")))))
974
975 ;; --- MMM ---
976
977 ;;;###autoload
978 (defun browse-url-mmm (url &optional new-window)
979 "Ask the MMM WWW browser to load URL.
980 Default to the URL around or before point."
981 (interactive (browse-url-interactive-arg "MMM URL: "))
982 (message "Sending URL to MMM...")
983 (save-excursion
984 (set-buffer (get-buffer-create " *Shell Command Output*"))
985 (erase-buffer)
986 ;; mmm_remote just SEGVs if the file isn't there...
987 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
988 ;; location in v 0.4:
989 (file-exists-p (expand-file-name "~/.mmm/remote")))
990 (call-process "mmm_remote" nil 0 nil url)
991 (call-process "mmm" nil 0 nil "-external" url))
992 (message "Sending URL to MMM... done")))
993
994 ;; --- mailto ---
995
996 ;;;###autoload
997 (defun browse-url-mail (url &optional new-window)
998 "Open a new mail message buffer within Emacs.
999 Default to using the mailto: URL around or before point as the
1000 recipient's address. Supplying a non-nil interactive prefix argument
1001 will cause the mail to be composed in another window rather than the
1002 current one.
1003
1004 When called interactively, if variable `browse-url-new-window-p' is
1005 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1006 non-nil interactive prefix argument reverses the effect of
1007 `browse-url-new-window-p'.
1008
1009 When called non-interactively, optional second argument NEW-WINDOW is
1010 used instead of `browse-url-new-window-p'."
1011 (interactive (browse-url-interactive-arg "Mailto URL: "))
1012 (save-excursion
1013 (let ((func (if (browse-url-maybe-new-window new-window)
1014 'compose-mail-other-window
1015 'compose-mail))
1016 (to (if (string-match "^mailto:" url)
1017 (substring url 7)
1018 url)))
1019 (apply func
1020 (list to nil nil nil nil nil (cons 'insert-buffer
1021 (current-buffer)))))))
1022
1023 ;; --- Random browser ---
1024
1025 ;;;###autoload
1026 (defun browse-url-generic (url &optional new-window)
1027 ;; new-window ignored
1028 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1029 Default to the URL around or before point. A fresh copy of the
1030 browser is started up in a new process with possible additional arguments
1031 `browse-url-generic-args'. This is appropriate for browsers which
1032 don't offer a form of remote control."
1033 (interactive (browse-url-interactive-arg "URL: "))
1034 (if (not browse-url-generic-program)
1035 (error "No browser defined (`browse-url-generic-program')"))
1036 (apply 'start-process (concat browse-url-generic-program url) nil
1037 browse-url-generic-program
1038 (append browse-url-generic-args (list url))))
1039
1040 (provide 'browse-url)
1041
1042 ;;; browse-url.el ends here