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