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