(compilation-error-regexp-alist): Allow initial
[bpt/emacs.git] / lisp / browse-url.el
1 ;;; browse-url.el --- Pass a URL to a WWW browser
2
3 ;; Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
6 ;; Maintainer: Dave Love <d.love@dl.ac.uk>
7 ;; Created: 03 Apr 1995
8 ;; Keywords: hypertext, hypermedia, mouse
9
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 ;; The latest version of this package should be available from
30 ;; <URL:http://wombat.doc.ic.ac.uk/emacs/browse-url.el>.
31
32 ;; This package provides functions which read a URL (Uniform Resource
33 ;; Locator) from the minibuffer, defaulting to the URL around point,
34 ;; and ask a World-Wide Web browser to load it. It can also load the
35 ;; URL associated with the current buffer. Different browsers use
36 ;; different methods of remote control so there is one function for
37 ;; each supported browser. If the chosen browser is not running, it
38 ;; is started. Currently there is support for:
39
40 ;; Function Browser Earliest version
41 ;; browse-url-netscape Netscape 1.1b1
42 ;; browse-url-mosaic XMosaic/mMosaic <= 2.4
43 ;; browse-url-cci XMosaic 2.5
44 ;; browse-url-w3 w3 0
45 ;; browse-url-w3-gnudoit w3 remotely
46 ;; browse-url-iximosaic IXI Mosaic ?
47 ;; browse-url-lynx-* Lynx 0
48 ;; browse-url-grail Grail 0.3b1
49 ;; browse-url-mmm MMM ?
50 ;; browse-url-generic arbitrary
51
52 ;; Note that versions of Netscape before 1.1b1 did not have remote
53 ;; control. <URL:http://www.netscape.com/newsref/std/x-remote.html>.
54
55 ;; Netscape can cache Web pages so it may be necessary to tell it to
56 ;; reload the current page if it has changed (e.g. if you have edited
57 ;; it). There is currently no perfect automatic solution to this.
58
59 ;; Netscape allows you to specify the id of the window you want to
60 ;; control but which window DO you want to control and how do you
61 ;; discover its id?
62
63 ;; If using XMosaic before version 2.5, check the definition of
64 ;; browse-url-usr1-signal below.
65 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/remote-control.html>
66
67 ;; XMosaic version 2.5 introduced Common Client Interface allowing you
68 ;; to control mosaic through Unix sockets.
69 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/CCI/cci-spec.html>
70
71 ;; William M. Perry's excellent "w3" WWW browser for
72 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
73 ;; has a function w3-follow-url-at-point, but that
74 ;; doesn't let you edit the URL like browse-url.
75 ;; The `gnuserv' package that can be used to control it in another
76 ;; Emacs process is available from
77 ;; <URL:http://hplbwww.hpl.hp.com/people/ange/gnuserv/>.
78
79 ;; Grail is the freely available WWW browser implemented in Python, a
80 ;; cool object-oriented freely available interpreted language. Grail
81 ;; 0.3b1 was the first version to have remote control as distributed.
82 ;; For more information on Grail see
83 ;; <URL:http://grail.cnri.reston.va.us/> and for more information on
84 ;; Python see <url:http://www.python.org/>. Grail support in
85 ;; browse-url.el written by Barry Warsaw <bwarsaw@python.org>.
86
87 ;; MMM is the freely available WWW browser implemented in Caml Special
88 ;; Light, a cool impure functional programming language, by Francois
89 ;; Rouaix. See the MMM home page
90 ;; <URL:http://pauillac.inria.fr/%7Erouaix/mmm/>.
91
92 ;; Lynx is now distributed by the FSF. See also
93 ;; <URL:http://lynx.browser.org/>.
94
95 ;; Free graphical browsers that could be used by `browse-url-generic'
96 ;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
97 ;; <URL:http://www.unlv.edu/chimera/>, Arena
98 ;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena> and Amaya
99 ;; <URL:ftp://ftp.w3.org/pub/amaya>. mMosaic
100 ;; <URL:ftp://sig.enst.fr/pub/multicast/mMosaic/> (with development
101 ;; support for Java applets and multicast) can be used like Mosaic by
102 ;; setting `browse-url-mosaic-program' appropriately.
103
104 ;; I [Denis Howe] recommend Nelson Minar <nelson@santafe.edu>'s excellent
105 ;; html-helper-mode.el for editing HTML and thank Nelson for
106 ;; his many useful comments on this code.
107 ;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>
108
109 ;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
110 ;; software/own/hm--html-menus/>. For composing correct HTML see also
111 ;; PSGML the general SGML structure editor package
112 ;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
113 ;; with this.
114
115 ;; This package generalises function html-previewer-process in Marc
116 ;; Andreessen <marca@ncsa.uiuc.edu>'s html-mode (LCD
117 ;; modes/html-mode.el.Z) and provides better versions of the URL
118 ;; functions in Michelangelo Grigni <mic@cs.ucsd.edu>'s ffap.el
119 ;; (find-file-at-point) <URL:ftp://cs.ucsd.edu:/pub/mic/>. The huge
120 ;; hyperbole package also contains similar functions.
121
122 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 ;; Help!
124
125 ;; Can you write and test some code for the Macintrash and Windoze
126 ;; Netscape remote control APIs? (See the URL above).
127
128 ;; Do any other browsers have remote control?
129
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;; Usage
132
133 ;; To display the URL at or before point:
134 ;; M-x browse-url-at-point RET
135 ;; or, similarly but with the opportunity to edit the URL extracted from
136 ;; the buffer, use:
137 ;; M-x browse-url
138
139 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
140 ;; file:
141 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
142 ;; (Note that using Shift-mouse-1 is not desirable because
143 ;; that event has a standard meaning in Emacs.)
144
145 ;; To display the current buffer in a web browser:
146 ;; M-x browse-url-of-buffer RET
147
148 ;; To display the current region in a web browser:
149 ;; M-x browse-url-of-region RET
150
151 ;; In Dired, to display the file named on the current line:
152 ;; M-x browse-url-of-dired-file RET
153
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;; Customisation (~/.emacs)
156
157 ;; To see what variables are available for customization, type
158 ;; `M-x set-variable browse-url TAB'. Better, use
159 ;; `M-x customize-group browse-url'.
160
161 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
162 ;; (as used by html-helper-mode):
163 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
164 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
165 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
166 ;; (global-set-key "\C-c\C-zu" 'browse-url)
167 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
168 ;; (add-hook 'dired-mode-hook
169 ;; (lambda ()
170 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
171
172 ;; Browse URLs in mail messages by clicking mouse-2:
173 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
174 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
175
176 ;; Browse URLs in Usenet messages by clicking mouse-2:
177 ;; (eval-after-load "gnus"
178 ;; '(define-key gnus-article-mode-map [mouse-2] 'browse-url-at-mouse))
179 ;; [The current version of Gnus provides a standard feature to
180 ;; activate URLs in article buffers for invocation of browse-url with
181 ;; mouse-2.]
182
183 ;; Use the Emacs w3 browser when not running under X11:
184 ;; (or (eq window-system 'x)
185 ;; (setq browse-url-browser-function 'browse-url-w3))
186
187 ;; To always save modified buffers before displaying the file in a browser:
188 ;; (setq browse-url-save-file t)
189
190 ;; To get round the Netscape caching problem, you could EITHER have
191 ;; write-file in html-helper-mode make Netscape reload the document:
192 ;;
193 ;; (autoload 'browse-url-netscape-reload "browse-url"
194 ;; "Ask a WWW browser to redisplay the current file." t)
195 ;; (add-hook 'html-helper-mode-hook
196 ;; (lambda ()
197 ;; (add-hook 'local-write-file-hooks
198 ;; (lambda ()
199 ;; (let ((local-write-file-hooks))
200 ;; (save-buffer))
201 ;; (browse-url-netscape-reload)
202 ;; t) ; => file written by hook
203 ;; t))) ; append to l-w-f-hooks
204 ;;
205 ;; OR have browse-url-of-file ask Netscape to load and then reload the
206 ;; file:
207 ;;
208 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
209
210 ;; You may also want to customise browse-url-netscape-arguments, e.g.
211 ;; (setq browse-url-netscape-arguments '("-install"))
212 ;;
213 ;; or similarly for the other browsers.
214
215 ;; To invoke different browsers for different URLs:
216 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
217 ;; ("." . browse-url-netscape)))
218
219 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
220 ;;; Code:
221
222 (eval-when-compile (require 'dired)
223 (require 'thingatpt))
224
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 ;; Variables
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 start 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 (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 (defcustom browse-url-netscape-display nil
299 "*The X display for running Netscape, if not same as Emacs'."
300 :type '(choice string (const :tag "Default" nil))
301 :group 'browse-url)
302
303 (defcustom browse-url-mosaic-program "xmosaic"
304 "*The name by which to invoke Mosaic (or mMosaic)."
305 :type 'string
306 :group 'browse-url)
307
308 (defcustom browse-url-mosaic-arguments nil
309 "*A list of strings to pass to Mosaic as arguments."
310 :type '(repeat (string :tag "Argument"))
311 :group 'browse-url)
312
313 (defcustom browse-url-filename-alist
314 '(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
315 ;; The above loses the username to avoid the browser prompting for
316 ;; it in anonymous cases. If it's not anonymous the next regexp
317 ;; applies.
318 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
319 ("^/+" . "file:/"))
320 "*An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
321 Any substring of a filename matching one of the REGEXPs is replaced by
322 the corresponding STRING using `replace-match', not treating STRING
323 literally. All pairs are applied in the order given. The default
324 value converts ange-ftp/EFS-style paths into ftp URLs and prepends
325 `file:' to any path beginning with `/'.
326
327 For example, adding to the default a specific translation of an ange-ftp
328 address to an HTTP URL:
329
330 (setq browse-url-filename-alist
331 '((\"/webmaster@webserver:/home/www/html/\" .
332 \"http://www.acme.co.uk/\")
333 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
334 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
335 (\"^/+\" . \"file:/\")))
336 "
337 :type '(repeat (cons :format "%v"
338 (regexp :tag "Regexp")
339 (string :tag "Replacement")))
340 :version "20.3"
341 :group 'browse-url)
342
343 (defcustom browse-url-save-file nil
344 "*If non-nil, save the buffer before displaying its file.
345 Used by the `browse-url-of-file' command."
346 :type 'boolean
347 :group 'browse-url)
348
349 (defcustom browse-url-of-file-hook nil
350 "*Run after `browse-url-of-file' has asked a browser to load a file.
351
352 Set this to `browse-url-netscape-reload' to force Netscape to load the
353 file rather than displaying a cached copy."
354 :type 'hook
355 :group 'browse-url)
356
357 (defvar browse-url-usr1-signal
358 (if (and (boundp 'emacs-major-version)
359 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
360 'SIGUSR1 ; Why did I think this was in lower case before?
361 30) ; Check /usr/include/signal.h.
362 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
363 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
364 which is 30 on SunOS and 16 on HP-UX and Solaris.")
365
366 (defcustom browse-url-CCI-port 3003
367 "*Port to access XMosaic via CCI.
368 This can be any number between 1024 and 65535 but must correspond to
369 the value set in the browser."
370 :type 'integer
371 :group 'browse-url)
372
373 (defcustom browse-url-CCI-host "localhost"
374 "*Host to access XMosaic via CCI.
375 This should be the host name of the machine running XMosaic with CCI
376 enabled. The port number should be set in `browse-url-CCI-port'."
377 :type 'string
378 :group 'browse-url)
379
380 (defvar browse-url-temp-file-name nil)
381 (make-variable-buffer-local 'browse-url-temp-file-name)
382
383 (defcustom browse-url-xterm-program "xterm"
384 "*The name of the terminal emulator used by `browse-url-lynx-xterm'.
385 This might, for instance, be a separate colour version of xterm."
386 :type 'string
387 :group 'browse-url)
388
389 (defcustom browse-url-xterm-args nil
390 "*A list of strings defining options for `browse-url-xterm-program'.
391 These might set its size, for instance."
392 :type '(repeat (string :tag "Argument"))
393 :group 'browse-url)
394
395 (defcustom browse-url-gnudoit-program "gnudoit"
396 "*The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
397 :type 'string
398 :group 'browse-url)
399
400 (defcustom browse-url-gnudoit-args '("-q")
401 "*A list of strings defining options for `browse-url-gnudoit-program'.
402 These might set the port, for instance."
403 :type '(repeat (string :tag "Argument"))
404 :group 'browse-url)
405
406 (defcustom browse-url-generic-program nil
407 "*The name of the browser program used by `browse-url-generic'."
408 :type '(choice string (const :tag "None" nil))
409 :group 'browse-url)
410
411 (defcustom browse-url-generic-args nil
412 "*A list of strings defining options for `browse-url-generic-program'."
413 :type '(repeat (string :tag "Argument"))
414 :group 'browse-url)
415
416 (defcustom browse-url-temp-dir
417 (or (getenv "TMPDIR") "/tmp")
418 "*The name of a directory for browse-url's temporary files.
419 Such files are generated by functions like `browse-url-of-region'.
420 You might want to set this to somewhere with restricted read permissions
421 for privacy's sake."
422 :type 'string
423 :group 'browse-url)
424
425 (defcustom browse-url-netscape-version
426 3
427 "*The version of Netscape you are using.
428 This affects how URL reloading is done; the mechanism changed
429 incompatibly at version 4."
430 :type 'number
431 :group 'browse-url)
432
433 (defcustom browse-url-lynx-input-field 'avoid
434 "*Action on selecting an existing Lynx buffer at an input field.
435 What to do when sending a new URL to an existing Lynx buffer in Emacs
436 if the Lynx cursor is on an input field (in which case the `g' command
437 would be entered as data). Such fields are recognized by the
438 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
439 user and don't emit the URL, 'avoid: try to avoid the field by moving
440 down (this *won't* always work)."
441 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
442 (const :tag "Disregard" :value nil)
443 (const :tag "Warn, don't emit URL" :value warn))
444 :group 'browse-url)
445
446 (defcustom browse-url-lynx-input-attempts 10
447 "*How many times to try to move down from a series of lynx input fields."
448 :type 'integer
449 :group 'browse-url)
450
451 (defcustom browse-url-lynx-input-delay 0.2
452 "*How many seconds to wait for lynx between moves down from an input field."
453 :type 'number
454 :group 'browse-url)
455
456 (defvar browse-url-temp-file-list '())
457
458 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
459 ;; URL input
460
461 (defun browse-url-url-at-point ()
462 (let ((url (thing-at-point 'url)))
463 (set-text-properties 0 (length url) nil url)
464 url))
465
466 ;; Having this as a separate function called by the browser-specific
467 ;; functions allows them to be stand-alone commands, making it easier
468 ;; to switch between browsers.
469
470 (defun browse-url-interactive-arg (prompt)
471 "Read a URL from the minibuffer, prompting with PROMPT.
472 Default to the URL at or before point. If invoked with a mouse button,
473 set point to the position clicked first. Return a list for use in
474 `interactive' containing the URL and `browse-url-new-window-p' or its
475 negation if a prefix argument was given."
476 (let ((event (elt (this-command-keys) 0)))
477 (and (listp event) (mouse-set-point event)))
478 (list (read-string prompt (browse-url-url-at-point))
479 (not (eq (null browse-url-new-window-p)
480 (null current-prefix-arg)))))
481
482 (defun browse-url-maybe-new-window (arg)
483 (if (interactive-p)
484 arg
485 browse-url-new-window-p))
486
487 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
488 ;; Browse current buffer
489
490 ;;;###autoload
491 (defun browse-url-of-file (&optional file)
492 "Ask a WWW browser to display FILE.
493 Display the current buffer's file if FILE is nil or if called
494 interactively. Turn the filename into a URL with function
495 `browse-url-file-url'. Pass the URL to a browser using the
496 `browse-url' function then run `browse-url-of-file-hook'."
497 (interactive)
498 (or file
499 (setq file (buffer-file-name))
500 (error "Current buffer has no file"))
501 (let ((buf (get-file-buffer file)))
502 (if buf
503 (save-excursion
504 (set-buffer buf)
505 (cond ((not (buffer-modified-p)))
506 (browse-url-save-file (save-buffer))
507 (t (message "%s modified since last save" file))))))
508 (browse-url (browse-url-file-url file))
509 (run-hooks 'browse-url-of-file-hook))
510
511 (defun browse-url-file-url (file)
512 "Return the URL corresponding to FILE.
513 Use variable `browse-url-filename-alist' to map filenames to URLs."
514 ;; URL-encode special chars, do % first
515 (let ((s 0))
516 (while (setq s (string-match "%" file s))
517 (setq file (replace-match "%25" t t file)
518 s (1+ s))))
519 (while (string-match "[*\"()',=;? ]" file)
520 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
521 (setq file (replace-match enc t t file))))
522 (let ((maps browse-url-filename-alist))
523 (while maps
524 (let* ((map (car maps))
525 (from-re (car map))
526 (to-string (cdr map)))
527 (setq maps (cdr maps))
528 (and (string-match from-re file)
529 (setq file (replace-match to-string t nil file))))))
530 file)
531
532 ;;;###autoload
533 (defun browse-url-of-buffer (&optional buffer)
534 "Ask a WWW browser to display BUFFER.
535 Display the current buffer if BUFFER is nil. Display only the
536 currently visible part of BUFFER (from a temporary file) if buffer is
537 narrowed."
538 (interactive)
539 (save-excursion
540 (and buffer (set-buffer buffer))
541 (let ((file-name
542 ;; Ignore real name if restricted
543 (and (= (- (point-max) (point-min)) (buffer-size))
544 (or buffer-file-name
545 (and (boundp 'dired-directory) dired-directory)))))
546 (or file-name
547 (progn
548 (or browse-url-temp-file-name
549 (setq browse-url-temp-file-name
550 (convert-standard-filename
551 (make-temp-name
552 (expand-file-name "burl" browse-url-temp-dir)))))
553 (setq file-name browse-url-temp-file-name)
554 (write-region (point-min) (point-max) file-name nil 'no-message)))
555 (browse-url-of-file file-name))))
556
557 (defun browse-url-delete-temp-file (&optional temp-file-name)
558 ;; Delete browse-url-temp-file-name from the file system and from
559 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
560 ;; non-nil, delete it instead, but only from the file system --
561 ;; browse-url-temp-file-list is not affected.
562 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
563 (if (and file-name (file-exists-p file-name))
564 (progn
565 (delete-file file-name)
566 (if (null temp-file-name)
567 (setq browse-url-temp-file-list
568 (delete browse-url-temp-file-name
569 browse-url-temp-file-list)))))))
570
571 (defun browse-url-delete-temp-file-list ()
572 ;; Delete all elements of browse-url-temp-file-list.
573 (while browse-url-temp-file-list
574 (browse-url-delete-temp-file (car browse-url-temp-file-list))
575 (setq browse-url-temp-file-list
576 (cdr browse-url-temp-file-list))))
577
578 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
579 (add-hook 'kill-emacs-hook 'browse-url-delete-temp-file-list)
580
581 ;;;###autoload
582 (defun browse-url-of-dired-file ()
583 "In Dired, ask a WWW browser to display the file named on this line."
584 (interactive)
585 (browse-url-of-file (dired-get-filename)))
586
587 ;;;###autoload
588 (defun browse-url-of-region (min max)
589 "Ask a WWW browser to display the current region."
590 (interactive "r")
591 (save-excursion
592 (save-restriction
593 (narrow-to-region (mark) (point))
594 (browse-url-of-buffer))))
595
596 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
597 ;; Browser-independent commands
598
599 ;; A generic command to call the current browse-url-browser-function
600
601 ;;;###autoload
602 (defun browse-url (&rest args)
603 "Ask a WWW browser to load URL.
604 Prompts for a URL, defaulting to the URL at or before point. Variable
605 `browse-url-browser-function' says which browser to use."
606 (interactive (browse-url-interactive-arg "URL: "))
607 (if (consp browse-url-browser-function)
608 (apply 'browse-url-choose-browser args)
609 (apply browse-url-browser-function args)))
610
611 (defun browse-url-choose-browser (url &rest args)
612 "Pass URL to a browser function chosen.
613 This is done according to the association list in variable
614 `browse-url-browser-function'."
615 (let ((blist browse-url-browser-function)
616 re bf)
617 (while (consp blist)
618 (setq re (car (car blist))
619 bf (cdr (car blist))
620 blist (cdr blist))
621 (if (string-match re url)
622 (progn (apply bf url args) (setq blist t))))
623 (or blist
624 (error "No browser in browse-url-browser-function matching URL %s" url))))
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 screens on several displays, as
676 listed by (emacs-display-list) and (x-display-screens DISPLAY), this
677 just returns the display showing the selected frame. You got a
678 problem with that?"
679 (let (device display)
680 (and (fboundp 'selected-device) (fboundp 'device-type) (fboundp 'device-connection)
681 (setq device (selected-device))
682 (eq (device-type device) 'x)
683 (setq display (device-connection device))
684 (not (equal display (getenv "DISPLAY")))
685 display)))
686
687 ;;;###autoload
688 (defun browse-url-netscape (url &optional new-window)
689 "Ask the Netscape WWW browser to load URL.
690
691 Default to the URL around or before point. The strings in variable
692 `browse-url-netscape-arguments' are also passed to Netscape.
693
694 When called interactively, if variable `browse-url-new-window-p' is
695 non-nil, load the document in a new Netscape window, otherwise use a
696 random existing one. A non-nil interactive prefix argument reverses
697 the effect of `browse-url-new-window-p'.
698
699 When called non-interactively, optional second argument NEW-WINDOW is
700 used instead of `browse-url-new-window-p'."
701 (interactive (browse-url-interactive-arg "Netscape URL: "))
702 ;; URL encode any commas in the URL
703 (while (string-match "," url)
704 (setq url (replace-match "%2C" t t url)))
705 (let* ((process-environment (browse-url-process-environment))
706 (process (apply 'start-process
707 (concat "netscape " url) nil
708 browse-url-netscape-program
709 (append browse-url-netscape-arguments
710 (if (eq window-system 'w32)
711 (list url)
712 (if new-window '("-noraise"))
713 (list "-remote"
714 (concat "openURL(" url
715 (if (browse-url-maybe-new-window
716 new-window)
717 ",new-window")
718 ")")))))))
719 (set-process-sentinel process
720 (list 'lambda '(process change)
721 (list 'browse-url-netscape-sentinel 'process url)))))
722
723 (defun browse-url-netscape-sentinel (process url)
724 "Handle a change to the process communicating with Netscape."
725 (or (eq (process-exit-status process) 0)
726 (let* ((process-environment (browse-url-process-environment)))
727 ;; Netscape not running - start it
728 (message "Starting Netscape...")
729 (apply 'start-process (concat "netscape" url) nil
730 browse-url-netscape-program
731 (append browse-url-netscape-startup-arguments (list url))))))
732
733 (defun browse-url-netscape-reload ()
734 "Ask Netscape to reload its current document.
735 How depends on `browse-url-netscape-version'."
736 (interactive)
737 ;; Backwards incompatibility reported by
738 ;; <peter.kruse@psychologie.uni-regensburg.de>.
739 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
740 "xfeDoCommand(reload)"
741 "reload")))
742
743 (defun browse-url-netscape-send (command)
744 "Send a remote control command to Netscape."
745 (let* ((process-environment (browse-url-process-environment)))
746 (apply 'start-process "netscape" nil
747 browse-url-netscape-program
748 (append browse-url-netscape-arguments
749 (list "-remote" command)))))
750
751 ;; --- Mosaic ---
752
753 ;;;###autoload
754 (defun browse-url-mosaic (url &optional new-window)
755 ;; new-window ignored
756 "Ask the XMosaic WWW browser to load URL.
757
758 Default to the URL around or before point. The strings in variable
759 `browse-url-mosaic-arguments' are also passed to Mosaic and the
760 program is invoked according to the variable
761 `browse-url-mosaic-program'.
762
763 When called interactively, if variable `browse-url-new-window-p' is
764 non-nil, load the document in a new Mosaic window, otherwise use a
765 random existing one. A non-nil interactive prefix argument reverses
766 the effect of `browse-url-new-window-p'.
767
768 When called non-interactively, optional second argument NEW-WINDOW is
769 used instead of `browse-url-new-window-p'."
770 (interactive (browse-url-interactive-arg "Mosaic URL: "))
771 (let ((pidfile (expand-file-name "~/.mosaicpid"))
772 pid pidbuf)
773 (if (file-readable-p pidfile)
774 (save-excursion
775 (find-file pidfile)
776 (goto-char (point-min))
777 (setq pid (read (current-buffer)))
778 (kill-buffer nil)))
779 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
780 (save-excursion
781 (find-file (format "/tmp/Mosaic.%d" pid))
782 (erase-buffer)
783 (insert (if (browse-url-maybe-new-window new-window)
784 "newwin\n"
785 "goto\n")
786 url "\n")
787 (save-buffer)
788 (kill-buffer nil)
789 ;; Send signal SIGUSR to Mosaic
790 (message "Signalling Mosaic...")
791 (signal-process pid browse-url-usr1-signal)
792 ;; Or you could try:
793 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
794 (message "Signalling Mosaic...done")
795 )
796 ;; Mosaic not running - start it
797 (message "Starting Mosaic...")
798 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
799 (append browse-url-mosaic-arguments (list url)))
800 (message "Starting Mosaic...done"))))
801
802 ;; --- Grail ---
803
804 ;;;###autoload
805 (defvar browse-url-grail
806 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
807 "*Location of Grail remote control client script `rcgrail.py'.
808 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
809
810 ;;;###autoload
811 (defun browse-url-grail (url &optional new-window)
812 "Ask the Grail WWW browser to load URL.
813 Default to the URL around or before point. Runs the program in the
814 variable `browse-url-grail'."
815 (interactive (browse-url-interactive-arg "Grail URL: "))
816 (message "Sending URL to Grail...")
817 (save-excursion
818 (set-buffer (get-buffer-create " *Shell Command Output*"))
819 (erase-buffer)
820 ;; don't worry about this failing.
821 (call-process browse-url-grail nil 0 nil url)
822 (message "Sending URL to Grail... done")))
823
824 ;; --- Mosaic using CCI ---
825
826 (defun browse-url-cci (url &optional new-window)
827 "Ask the XMosaic WWW browser to load URL.
828 Default to the URL around or before point.
829
830 This function only works for XMosaic version 2.5 or later. You must
831 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
832 value of variable `browse-url-CCI-port', and enable `Accept requests'.
833
834 When called interactively, if variable `browse-url-new-window-p' is
835 non-nil, load the document in a new browser window, otherwise use a
836 random existing one. A non-nil interactive prefix argument reverses
837 the effect of `browse-url-new-window-p'.
838
839 When called non-interactively, optional second argument NEW-WINDOW is
840 used instead of `browse-url-new-window-p'."
841 (interactive (browse-url-interactive-arg "Mosaic URL: "))
842 (open-network-stream "browse-url" " *browse-url*"
843 browse-url-CCI-host browse-url-CCI-port)
844 ;; Todo: start browser if fails
845 (process-send-string "browse-url"
846 (concat "get url (" url ") output "
847 (if (browse-url-maybe-new-window
848 new-window)
849 "new"
850 "current")
851 "\r\n"))
852 (process-send-string "browse-url" "disconnect\r\n")
853 (delete-process "browse-url"))
854
855 ;; --- IXI Mosaic ---
856
857 ;;;###autoload
858 (defun browse-url-iximosaic (url &optional new-window)
859 ;; new-window ignored
860 "Ask the IXIMosaic WWW browser to load URL.
861 Default to the URL around or before point."
862 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
863 (start-process "tellw3b" nil "tellw3b"
864 "-service WWW_BROWSER ixi_showurl " url))
865
866 ;; --- W3 ---
867
868 ;;;###autoload
869 (defun browse-url-w3 (url &optional new-window)
870 "Ask the w3 WWW browser to load URL.
871 Default to the URL around or before point.
872
873 When called interactively, if variable `browse-url-new-window-p' is
874 non-nil, load the document in a new window. A non-nil interactive
875 prefix argument reverses the effect of `browse-url-new-window-p'.
876
877 When called non-interactively, optional second argument NEW-WINDOW is
878 used instead of `browse-url-new-window-p'."
879 (interactive (browse-url-interactive-arg "W3 URL: "))
880 (if (browse-url-maybe-new-window new-window)
881 (w3-fetch-other-window)
882 (w3-fetch url)))
883
884 ;;;###autoload
885 (defun browse-url-w3-gnudoit (url &optional new-window)
886 ;; new-window ignored
887 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
888 The `browse-url-gnudoit-program' program is used with options given by
889 `browse-url-gnudoit-args'. Default to the URL around or before point."
890 (interactive (browse-url-interactive-arg "W3 URL: "))
891 (apply 'start-process (concat "gnudoit:" url) nil
892 browse-url-gnudoit-program
893 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
894
895 ;; --- Lynx in an xterm ---
896
897 ;;;###autoload
898 (defun browse-url-lynx-xterm (url &optional new-window)
899 ;; new-window ignored
900 "Ask the Lynx WWW browser to load URL.
901 Default to the URL around or before point. A new Lynx process is run
902 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
903 with possible additional arguments `browse-url-xterm-args'."
904 (interactive (browse-url-interactive-arg "Lynx URL: "))
905 (apply 'start-process (concat "lynx" url) nil browse-url-xterm-program
906 (append browse-url-xterm-args (list "-e" "lynx" url))))
907
908 ;; --- Lynx in an Emacs "term" window ---
909
910 ;;;###autoload
911 (defun browse-url-lynx-emacs (url &optional new-buffer)
912 "Ask the Lynx WWW browser to load URL.
913 Default to the URL around or before point. With a prefix argument, run
914 a new Lynx process in a new buffer.
915
916 When called interactively, if variable `browse-url-new-window-p' is
917 non-nil, load the document in a new lynx in a new term window,
918 otherwise use any existing one. A non-nil interactive prefix argument
919 reverses the effect of `browse-url-new-window-p'.
920
921 When called non-interactively, optional second argument NEW-WINDOW is
922 used instead of `browse-url-new-window-p'."
923 (interactive (browse-url-interactive-arg "Lynx URL: "))
924 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
925 ;; (term-term-name "vt100") ; ??
926 (buf (get-buffer "*lynx*"))
927 (proc (and buf (get-buffer-process buf)))
928 (n browse-url-lynx-input-attempts))
929 (if (and (browse-url-maybe-new-window new-buffer) buf)
930 ;; Rename away the OLD buffer. This isn't very polite, but
931 ;; term insists on working in a buffer named *lynx* and would
932 ;; choke on *lynx*<1>
933 (progn (set-buffer buf)
934 (rename-uniquely)))
935 (if (or (browse-url-maybe-new-window new-buffer)
936 (not buf)
937 (not proc)
938 (not (memq (process-status proc) '(run stop))))
939 ;; start a new lynx
940 (progn (setq buf (make-term "lynx" "lynx" nil url))
941 (switch-to-buffer buf)
942 (term-char-mode)
943 (set-process-sentinel
944 (get-buffer-process buf)
945 ;; Don't leave around a dead one (especially because
946 ;; of its munged keymap.)
947 (lambda (process event)
948 (if (not (memq (process-status process) '(run stop)))
949 (let ((buf (process-buffer process)))
950 (if buf (kill-buffer buf)))))))
951 ;; send the url to lynx in the old buffer
952 (let ((win (get-buffer-window buf t)))
953 (if win
954 (select-window win)
955 (switch-to-buffer buf)))
956 (if (eq (following-char) ?_)
957 (cond ((eq browse-url-lynx-input-field 'warn)
958 (error "Please move out of the input field first."))
959 ((eq browse-url-lynx-input-field 'avoid)
960 (while (and (eq (following-char) ?_) (> n 0))
961 (term-send-down) ; down arrow
962 (sit-for browse-url-lynx-input-delay))
963 (if (eq (following-char) ?_)
964 (error "Cannot move out of the input field, sorry.")))))
965 (term-send-string proc (concat "g" ; goto
966 "\C-u" ; kill default url
967 url
968 "\r")))))
969
970 ;; --- MMM ---
971
972 ;;;###autoload
973 (defun browse-url-mmm (url &optional new-window)
974 "Ask the MMM WWW browser to load URL.
975 Default to the URL around or before point."
976 (interactive (browse-url-interactive-arg "MMM URL: "))
977 (message "Sending URL to MMM...")
978 (save-excursion
979 (set-buffer (get-buffer-create " *Shell Command Output*"))
980 (erase-buffer)
981 ;; mmm_remote just SEGVs if the file isn't there...
982 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
983 ;; location in v 0.4:
984 (file-exists-p (expand-file-name "~/.mmm/remote")))
985 (call-process "mmm_remote" nil 0 nil url)
986 (call-process "mmm" nil 0 nil "-external" url))
987 (message "Sending URL to MMM... done")))
988
989 ;; --- mailto ---
990
991 ;;;###autoload
992 (defun browse-url-mail (url &optional new-window)
993 "Open a new mail message buffer within Emacs.
994 Default to using the mailto: URL around or before point as the
995 recipient's address. Supplying a non-nil interactive prefix argument
996 will cause the mail to be composed in another window rather than the
997 current one.
998
999 When called interactively, if variable `browse-url-new-window-p' is
1000 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1001 non-nil interactive prefix argument reverses the effect of
1002 `browse-url-new-window-p'.
1003
1004 When called non-interactively, optional second argument NEW-WINDOW is
1005 used instead of `browse-url-new-window-p'."
1006 (interactive (browse-url-interactive-arg "Mailto URL: "))
1007 (save-excursion
1008 (let ((func (if (browse-url-maybe-new-window new-window)
1009 'compose-mail-other-window
1010 'compose-mail))
1011 (to (if (string-match "^mailto:" url)
1012 (substring url 7)
1013 url)))
1014 (apply func
1015 (list to nil nil nil nil nil (cons 'insert-buffer
1016 (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