(browse-url-lynx-emacs): Add sentinel to kill the buffer when lynx
[emacs.git] / lisp / browse-url.el
blobeeec6232951fa4edf3e1a57293d17284c9cee440
1 ;;; browse-url.el --- Pass a URL to a WWW browser
3 ;; Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
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
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
29 ;; The latest version of this package should be available from
30 ;; <URL:http://wombat.doc.ic.ac.uk/emacs/browse-url.el>.
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:
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
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>.
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.
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?
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>
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>
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/>.
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>.
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/>.
92 ;; Lynx is now distributed by the FSF. See also
93 ;; <URL:http://lynx.browser.org/>.
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.
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/>
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.
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.
122 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 ;; Help!
125 ;; Can you write and test some code for the Macintrash and Windoze
126 ;; Netscape remote control APIs? (See the URL above).
128 ;; Do any other browsers have remote control?
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;; Usage
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
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.)
145 ;; To display the current buffer in a web browser:
146 ;; M-x browse-url-of-buffer RET
148 ;; To display the current region in a web browser:
149 ;; M-x browse-url-of-region RET
151 ;; In Dired, to display the file named on the current line:
152 ;; M-x browse-url-of-dired-file RET
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;; Customisation (~/.emacs)
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'.
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)))
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)))
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.]
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))
187 ;; To always save modified buffers before displaying the file in a browser:
188 ;; (setq browse-url-save-file t)
190 ;; To get round the Netscape caching problem, you could EITHER have
191 ;; write-file in html-helper-mode make Netscape reload the document:
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
205 ;; OR have browse-url-of-file ask Netscape to load and then reload the
206 ;; file:
208 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
210 ;; You may also want to customise browse-url-netscape-arguments, e.g.
211 ;; (setq browse-url-netscape-arguments '("-install"))
213 ;; or similarly for the other browsers.
215 ;; To invoke different browsers for different URLs:
216 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
217 ;; ("." . browse-url-netscape)))
219 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
220 ;;; Code:
222 (eval-when-compile (require 'dired)
223 (require 'thingatpt))
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 ;; Variables
228 (defgroup browse-url nil
229 "Use a web browser to look at a URL."
230 :prefix "browse-url-"
231 :group 'hypermedia)
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.
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)
264 (defcustom browse-url-netscape-program "netscape"
265 "*The name by which to invoke Netscape."
266 :type 'string
267 :group 'browse-url)
269 (defcustom browse-url-netscape-arguments nil
270 "*A list of strings to pass to Netscape as arguments."
271 :type '(repeat (string :tag "Argument"))
272 :group 'browse-url)
274 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
275 "*A list of strings to pass to Netscape when it start up.
276 Defaults to the value of `browse-url-netscape-arguments' at the time
277 `browse-url' is loaded."
278 :type '(repeat (string :tag "Argument"))
279 :group 'browse-url)
281 (defcustom browse-url-new-window-p nil
282 "*If non-nil, always open a new browser window with appropriate browsers.
283 Passing an interactive argument to \\[browse-url], or specific browser
284 commands reverses the effect of this variable. Requires Netscape version
285 1.1N or later or XMosaic version 2.5 or later if using those browsers."
286 :type 'boolean
287 :group 'browse-url)
289 (defcustom browse-url-netscape-display nil
290 "*The X display for running Netscape, if not same as Emacs'."
291 :type '(choice string (const :tag "Default" nil))
292 :group 'browse-url)
294 (defcustom browse-url-mosaic-program "xmosaic"
295 "*The name by which to invoke Mosaic (or mMosaic)."
296 :type 'string
297 :group 'browse-url)
299 (defcustom browse-url-mosaic-arguments nil
300 "*A list of strings to pass to Mosaic as arguments."
301 :type '(repeat (string :tag "Argument"))
302 :group 'browse-url)
304 (defvar browse-url-path-regexp
305 "[^]\t\n \"'()<>[^`{}]*[^]\t\n \"'()<>[^`{}.,;]+"
306 "A regular expression to match the host, path or e-mail part of a URL.")
308 (defvar browse-url-short-regexp
309 (concat "[-A-Za-z0-9.]+" browse-url-path-regexp)
310 "A regular expression probably matching a URL without an access scheme.
311 Hostname matching is stricter in this case than for
312 ``browse-url-regexp''.")
314 (defvar browse-url-regexp
315 (concat
316 "\\(https?://\\|ftp://\\|gopher://\\|telnet://\\|wais://\\|file:/\\|s?news:\\|mailto:\\)"
317 browse-url-path-regexp)
318 "A regular expression probably matching a complete URL.")
320 (defvar browse-url-markedup-regexp
321 "<URL:[^>]+>"
322 "A regular expression matching a URL marked up per RFC1738.
323 This may be broken across lines.")
325 (defcustom browse-url-filename-alist
326 '(("^/+" . "file:/"))
327 "*An alist of (REGEXP . STRING) pairs.
328 Any substring of a filename matching one of the REGEXPs is replaced by
329 the corresponding STRING. All pairs are applied in the order given.
330 The default value prepends `file:' to any path beginning with `/'.
331 Used by the `browse-url-of-file' command.
333 For example, to map EFS filenames to URLs:
335 (setq browse-url-filename-alist
336 '((\"/webmaster@webserver:/home/www/html/\" .
337 \"http://www.acme.co.uk/\")
338 (\"^/+\" . \"file:/\")))
340 :type '(repeat (cons :format "%v"
341 (regexp :tag "Regexp")
342 (string :tag "Replacement")))
343 :group 'browse-url)
345 (defcustom browse-url-save-file nil
346 "*If non-nil, save the buffer before displaying its file.
347 Used by the `browse-url-of-file' command."
348 :type 'boolean
349 :group 'browse-url)
351 (defcustom browse-url-of-file-hook nil
352 "*Run after `browse-url-of-file' has asked a browser to load a file.
354 Set this to `browse-url-netscape-reload' to force Netscape to load the
355 file rather than displaying a cached copy."
356 :type 'hook
357 :group 'browse-url)
359 (defvar browse-url-usr1-signal
360 (if (and (boundp 'emacs-major-version)
361 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
362 'SIGUSR1 ; Why did I think this was in lower case before?
363 30) ; Check /usr/include/signal.h.
364 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
365 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
366 which is 30 on SunOS and 16 on HP-UX and Solaris.")
368 (defcustom browse-url-CCI-port 3003
369 "*Port to access XMosaic via CCI.
370 This can be any number between 1024 and 65535 but must correspond to
371 the value set in the browser."
372 :type 'integer
373 :group 'browse-url)
375 (defcustom browse-url-CCI-host "localhost"
376 "*Host to access XMosaic via CCI.
377 This should be the host name of the machine running XMosaic with CCI
378 enabled. The port number should be set in `browse-url-CCI-port'."
379 :type 'string
380 :group 'browse-url)
382 (defvar browse-url-temp-file-name nil)
383 (make-variable-buffer-local 'browse-url-temp-file-name)
385 (defcustom browse-url-xterm-program "xterm"
386 "*The name of the terminal emulator used by `browse-url-lynx-xterm'.
387 This might, for instance, be a separate colour version of xterm."
388 :type 'string
389 :group 'browse-url)
391 (defcustom browse-url-xterm-args nil
392 "*A list of strings defining options for `browse-url-xterm-program'.
393 These might set its size, for instance."
394 :type '(repeat (string :tag "Argument"))
395 :group 'browse-url)
397 (defcustom browse-url-gnudoit-program "gnudoit"
398 "*The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
399 :type 'string
400 :group 'browse-url)
402 (defcustom browse-url-gnudoit-args '("-q")
403 "*A list of strings defining options for `browse-url-gnudoit-program'.
404 These might set the port, for instance."
405 :type '(repeat (string :tag "Argument"))
406 :group 'browse-url)
408 (defcustom browse-url-generic-program nil
409 "*The name of the browser program used by `browse-url-generic'."
410 :type '(choice string (const :tag "None" nil))
411 :group 'browse-url)
413 (defcustom browse-url-generic-args nil
414 "*A list of strings defining options for `browse-url-generic-program'."
415 :type '(repeat (string :tag "Argument"))
416 :group 'browse-url)
418 (defcustom browse-url-temp-dir
419 (or (getenv "TMPDIR") "/tmp")
420 "*The name of a directory for browse-url's temporary files.
421 Such files are generated by functions like `browse-url-of-region'.
422 You might want to set this to somewhere with restricted read permissions
423 for privacy's sake."
424 :type 'string
425 :group 'browse-url)
427 (defcustom browse-url-netscape-version
429 "*The version of Netscape you are using.
430 This affects how URL reloading is done; the mechanism changed
431 incompatibly at version 4."
432 :type 'number
433 :group 'browse-url)
435 (defcustom browse-url-lynx-input-field 'avoid
436 "*Action on selecting an existing Lynx buffer at an input field.
437 What to do when sending a new URL to an existing Lynx buffer in Emacs
438 if the Lynx cursor is on an input field (in which case the `g' command
439 would be entered as data). Such fields are recognized by the
440 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
441 user and don't emit the URL, 'avoid: try to avoid the field by moving
442 down (this *won't* always work)."
443 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
444 (const :tag "Disregard" :value nil)
445 (const :tag "Warn, don't emit URL" :value warn))
446 :group 'browse-url)
448 (defcustom browse-url-lynx-input-attempts 10
449 "*How many times to try to move down from a series of lynx input fields."
450 :type 'integer
451 :group 'browse-url)
453 (defcustom browse-url-lynx-input-delay 0.2
454 "*How many seconds to wait for lynx between moves down from an input field."
455 :type 'number
456 :group 'browse-url)
458 (defvar browse-url-temp-file-list '())
460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
461 ;; URL input
463 (defun browse-url-url-at-point ()
464 (let ((url (thing-at-point 'url)))
465 (set-text-properties 0 (length url) nil url)
466 url))
468 ;; Having this as a separate function called by the browser-specific
469 ;; functions allows them to be stand-alone commands, making it easier
470 ;; to switch between browsers.
472 (defun browse-url-interactive-arg (prompt)
473 "Read a URL from the minibuffer, prompting with PROMPT.
474 Default to the URL at or before point. If invoked with a mouse button,
475 set point to the position clicked first. Return a list for use in
476 `interactive' containing the URL and `browse-url-new-window-p' or its
477 negation if a prefix argument was given."
478 (let ((event (elt (this-command-keys) 0)))
479 (and (listp event) (mouse-set-point event)))
480 (list (read-string prompt (browse-url-url-at-point))
481 (not (eq (null browse-url-new-window-p)
482 (null current-prefix-arg)))))
484 (defun browse-url-maybe-new-window (arg)
485 (if (interactive-p)
487 browse-url-new-window-p))
489 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
490 ;; Browse current buffer
492 ;;;###autoload
493 (defun browse-url-of-file (&optional file)
494 "Ask a WWW browser to display FILE.
495 Display the current buffer's file if FILE is nil or if called
496 interactively. Turn the filename into a URL with function
497 `browse-url-file-url'. Pass the URL to a browser using the
498 `browse-url' function then run `browse-url-of-file-hook'."
499 (interactive)
500 (or file
501 (setq file (buffer-file-name))
502 (error "Current buffer has no file"))
503 (let ((buf (get-file-buffer file)))
504 (if buf
505 (save-excursion
506 (set-buffer buf)
507 (cond ((not (buffer-modified-p)))
508 (browse-url-save-file (save-buffer))
509 (t (message "%s modified since last save" file))))))
510 (browse-url (browse-url-file-url file))
511 (run-hooks 'browse-url-of-file-hook))
513 (defun browse-url-file-url (file)
514 "Return the URL corresponding to FILE.
515 Use variable `browse-url-filename-alist' to map filenames to URLs.
516 Convert EFS file names of the form /USER@HOST:PATH to ftp://HOST/PATH."
517 ;; URL-encode special chars, do % first
518 (let ((s 0))
519 (while (setq s (string-match "%" file s))
520 (setq file (replace-match "%25" t t file)
521 s (1+ s))))
522 (while (string-match "[*\"()',=;? ]" file)
523 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
524 (setq file (replace-match enc t t file))))
525 (let ((maps browse-url-filename-alist))
526 (while maps
527 (let* ((map (car maps))
528 (from-re (car map))
529 (to-string (cdr map)))
530 (setq maps (cdr maps))
531 (and (string-match from-re file)
532 (setq file (replace-match to-string t t file))))))
533 ;; Check for EFS path
534 (and (string-match "^/\\([^:@]+@\\)?\\([^:]+\\):/*" file)
535 (setq file (concat "ftp://"
536 (substring file (match-beginning 2) (match-end 2))
537 "/" (substring file (match-end 0)))))
538 file)
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))))
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)))))))
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))))
586 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
587 (add-hook 'kill-emacs-hook 'browse-url-delete-temp-file-list)
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)))
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 (mark) (point))
602 (browse-url-of-buffer))))
604 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
605 ;; Browser-independent commands
607 ;; A generic command to call the current browse-url-browser-function
609 ;;;###autoload
610 (defun browse-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 (if (consp browse-url-browser-function)
616 (apply 'browse-url-choose-browser args)
617 (apply browse-url-browser-function args)))
619 (defun browse-url-choose-browser (url &rest args)
620 "Pass URL to a browser function chosen.
621 This is done according to the association list in variable
622 `browse-url-browser-function'."
623 (let ((blist browse-url-browser-function)
624 re bf)
625 (while (consp blist)
626 (setq re (car (car blist))
627 bf (cdr (car blist))
628 blist (cdr blist))
629 (if (string-match re url)
630 (progn (apply bf url args) (setq blist t))))
631 (or blist
632 (error "No browser in browse-url-browser-function matching URL %s" url))))
634 ;;;###autoload
635 (defun browse-url-at-point ()
636 "Ask a WWW browser to load the URL at or before point.
637 Doesn't let you edit the URL like `browse-url'. Variable
638 `browse-url-browser-function' says which browser to use."
639 (interactive)
640 (browse-url (browse-url-url-at-point)))
642 (defun browse-url-event-buffer (event)
643 (window-buffer (posn-window (event-start event))))
645 (defun browse-url-event-point (event)
646 (posn-point (event-start event)))
648 ;;;###autoload
649 (defun browse-url-at-mouse (event)
650 "Ask a WWW browser to load a URL clicked with the mouse.
651 The URL is the one around or before the position of the mouse click
652 but point is not changed. Doesn't let you edit the URL like
653 `browse-url'. Variable `browse-url-browser-function' says which browser
654 to use."
655 (interactive "e")
656 (save-excursion
657 (set-buffer (browse-url-event-buffer event))
658 (goto-char (browse-url-event-point event))
659 (let ((url (browse-url-url-at-point)))
660 (if (string-equal url "")
661 (error "No URL found"))
662 (browse-url url browse-url-new-window-p))))
664 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
665 ;; Browser-specific commands
667 ;; --- Netscape ---
669 (defun browse-url-process-environment ()
670 "Set DISPLAY in the environment to the X display Netscape is running on.
671 This is either the value of variable `browse-url-netscape-display' if
672 non-nil, or the same display as Emacs if different from the current
673 environment, otherwise just use the current environment."
674 (let ((display (or browse-url-netscape-display (browse-url-emacs-display))))
675 (if display
676 (cons (concat "DISPLAY=" display) process-environment)
677 process-environment)))
679 (defun browse-url-emacs-display ()
680 "Return the X display Emacs is running on.
681 This is nil if the display is the same as the DISPLAY environment variable.
683 Actually Emacs could be using several screens on several displays, as
684 listed by (emacs-display-list) and (x-display-screens DISPLAY), this
685 just returns the display showing the selected frame. You got a
686 problem with that?"
687 (let (device display)
688 (and (fboundp 'selected-device) (fboundp 'device-type) (fboundp 'device-connection)
689 (setq device (selected-device))
690 (eq (device-type device) 'x)
691 (setq display (device-connection device))
692 (not (equal display (getenv "DISPLAY")))
693 display)))
695 ;;;###autoload
696 (defun browse-url-netscape (url &optional new-window)
697 "Ask the Netscape WWW browser to load URL.
699 Default to the URL around or before point. The strings in variable
700 `browse-url-netscape-arguments' are also passed to Netscape.
702 When called interactively, if variable `browse-url-new-window-p' is
703 non-nil, load the document in a new Netscape window, otherwise use a
704 random existing one. A non-nil interactive prefix argument reverses
705 the effect of `browse-url-new-window-p'.
707 When called non-interactively, optional second argument NEW-WINDOW is
708 used instead of `browse-url-new-window-p'."
709 (interactive (browse-url-interactive-arg "Netscape URL: "))
710 ;; URL encode any commas in the URL
711 (while (string-match "," url)
712 (setq url (replace-match "%2C" t t url)))
713 (let* ((process-environment (browse-url-process-environment))
714 (process (apply 'start-process
715 (concat "netscape " url) nil
716 browse-url-netscape-program
717 (append browse-url-netscape-arguments
718 (if (eq window-system 'w32)
719 (list url)
720 (if new-window '("-noraise"))
721 (list "-remote"
722 (concat "openURL(" url
723 (if (browse-url-maybe-new-window
724 new-window)
725 ",new-window")
726 ")")))))))
727 (set-process-sentinel process
728 (list 'lambda '(process change)
729 (list 'browse-url-netscape-sentinel 'process url)))))
731 (defun browse-url-netscape-sentinel (process url)
732 "Handle a change to the process communicating with Netscape."
733 (or (eq (process-exit-status process) 0)
734 (let* ((process-environment (browse-url-process-environment)))
735 ;; Netscape not running - start it
736 (message "Starting Netscape...")
737 (apply 'start-process (concat "netscape" url) nil
738 browse-url-netscape-program
739 (append browse-url-netscape-startup-arguments (list url))))))
741 (defun browse-url-netscape-reload ()
742 "Ask Netscape to reload its current document.
743 How depends on `browse-url-netscape-version'."
744 (interactive)
745 ;; Backwards incompatibility reported by
746 ;; <peter.kruse@psychologie.uni-regensburg.de>.
747 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
748 "xfeDoCommand(reload)"
749 "reload")))
751 (defun browse-url-netscape-send (command)
752 "Send a remote control command to Netscape."
753 (let* ((process-environment (browse-url-process-environment)))
754 (apply 'start-process "netscape" nil
755 browse-url-netscape-program
756 (append browse-url-netscape-arguments
757 (list "-remote" command)))))
759 ;; --- Mosaic ---
761 ;;;###autoload
762 (defun browse-url-mosaic (url &optional new-window)
763 ;; new-window ignored
764 "Ask the XMosaic WWW browser to load URL.
766 Default to the URL around or before point. The strings in variable
767 `browse-url-mosaic-arguments' are also passed to Mosaic and the
768 program is invoked according to the variable
769 `browse-url-mosaic-program'.
771 When called interactively, if variable `browse-url-new-window-p' is
772 non-nil, load the document in a new Mosaic window, otherwise use a
773 random existing one. A non-nil interactive prefix argument reverses
774 the effect of `browse-url-new-window-p'.
776 When called non-interactively, optional second argument NEW-WINDOW is
777 used instead of `browse-url-new-window-p'."
778 (interactive (browse-url-interactive-arg "Mosaic URL: "))
779 (let ((pidfile (expand-file-name "~/.mosaicpid"))
780 pid pidbuf)
781 (if (file-readable-p pidfile)
782 (save-excursion
783 (find-file pidfile)
784 (goto-char (point-min))
785 (setq pid (read (current-buffer)))
786 (kill-buffer nil)))
787 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
788 (save-excursion
789 (find-file (format "/tmp/Mosaic.%d" pid))
790 (erase-buffer)
791 (insert (if (browse-url-maybe-new-window new-window)
792 "newwin\n"
793 "goto\n")
794 url "\n")
795 (save-buffer)
796 (kill-buffer nil)
797 ;; Send signal SIGUSR to Mosaic
798 (message "Signalling Mosaic...")
799 (signal-process pid browse-url-usr1-signal)
800 ;; Or you could try:
801 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
802 (message "Signalling Mosaic...done")
804 ;; Mosaic not running - start it
805 (message "Starting Mosaic...")
806 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
807 (append browse-url-mosaic-arguments (list url)))
808 (message "Starting Mosaic...done"))))
810 ;; --- Grail ---
812 ;;;###autoload
813 (defvar browse-url-grail
814 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
815 "*Location of Grail remote control client script `rcgrail.py'.
816 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
818 ;;;###autoload
819 (defun browse-url-grail (url &optional new-window)
820 "Ask the Grail WWW browser to load URL.
821 Default to the URL around or before point. Runs the program in the
822 variable `browse-url-grail'."
823 (interactive (browse-url-interactive-arg "Grail URL: "))
824 (message "Sending URL to Grail...")
825 (save-excursion
826 (set-buffer (get-buffer-create " *Shell Command Output*"))
827 (erase-buffer)
828 ;; don't worry about this failing.
829 (call-process browse-url-grail nil 0 nil url)
830 (message "Sending URL to Grail... done")))
832 ;; --- Mosaic using CCI ---
834 (defun browse-url-cci (url &optional new-window)
835 "Ask the XMosaic WWW browser to load URL.
836 Default to the URL around or before point.
838 This function only works for XMosaic version 2.5 or later. You must
839 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
840 value of variable `browse-url-CCI-port', and enable `Accept requests'.
842 When called interactively, if variable `browse-url-new-window-p' is
843 non-nil, load the document in a new browser window, otherwise use a
844 random existing one. A non-nil interactive prefix argument reverses
845 the effect of `browse-url-new-window-p'.
847 When called non-interactively, optional second argument NEW-WINDOW is
848 used instead of `browse-url-new-window-p'."
849 (interactive (browse-url-interactive-arg "Mosaic URL: "))
850 (open-network-stream "browse-url" " *browse-url*"
851 browse-url-CCI-host browse-url-CCI-port)
852 ;; Todo: start browser if fails
853 (process-send-string "browse-url"
854 (concat "get url (" url ") output "
855 (if (browse-url-maybe-new-window
856 new-window)
857 "new"
858 "current")
859 "\r\n"))
860 (process-send-string "browse-url" "disconnect\r\n")
861 (delete-process "browse-url"))
863 ;; --- IXI Mosaic ---
865 ;;;###autoload
866 (defun browse-url-iximosaic (url &optional new-window)
867 ;; new-window ignored
868 "Ask the IXIMosaic WWW browser to load URL.
869 Default to the URL around or before point."
870 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
871 (start-process "tellw3b" nil "tellw3b"
872 "-service WWW_BROWSER ixi_showurl " url))
874 ;; --- W3 ---
876 ;;;###autoload
877 (defun browse-url-w3 (url &optional new-window)
878 "Ask the w3 WWW browser to load URL.
879 Default to the URL around or before point.
881 When called interactively, if variable `browse-url-new-window-p' is
882 non-nil, load the document in a new window. A non-nil interactive
883 prefix argument reverses the effect of `browse-url-new-window-p'.
885 When called non-interactively, optional second argument NEW-WINDOW is
886 used instead of `browse-url-new-window-p'."
887 (interactive (browse-url-interactive-arg "W3 URL: "))
888 (if (browse-url-maybe-new-window new-window)
889 (w3-fetch-other-window)
890 (w3-fetch url)))
892 ;;;###autoload
893 (defun browse-url-w3-gnudoit (url &optional new-window)
894 ;; new-window ignored
895 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
896 The `browse-url-gnudoit-program' program is used with options given by
897 `browse-url-gnudoit-args'. Default to the URL around or before point."
898 (interactive (browse-url-interactive-arg "W3 URL: "))
899 (apply 'start-process (concat "gnudoit:" url) nil
900 browse-url-gnudoit-program
901 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
903 ;; --- Lynx in an xterm ---
905 ;;;###autoload
906 (defun browse-url-lynx-xterm (url &optional new-window)
907 ;; new-window ignored
908 "Ask the Lynx WWW browser to load URL.
909 Default to the URL around or before point. A new Lynx process is run
910 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
911 with possible additional arguments `browse-url-xterm-args'."
912 (interactive (browse-url-interactive-arg "Lynx URL: "))
913 (apply 'start-process (concat "lynx" url) nil browse-url-xterm-program
914 (append browse-url-xterm-args (list "-e" "lynx" url))))
916 ;; --- Lynx in an Emacs "term" window ---
918 ;;;###autoload
919 (defun browse-url-lynx-emacs (url &optional new-buffer)
920 "Ask the Lynx WWW browser to load URL.
921 Default to the URL around or before point. With a prefix argument, run
922 a new Lynx process in a new buffer.
924 When called interactively, if variable `browse-url-new-window-p' is
925 non-nil, load the document in a new lynx in a new term window,
926 otherwise use any existing one. A non-nil interactive prefix argument
927 reverses the effect of `browse-url-new-window-p'.
929 When called non-interactively, optional second argument NEW-WINDOW is
930 used instead of `browse-url-new-window-p'."
931 (interactive (browse-url-interactive-arg "Lynx URL: "))
932 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
933 ;; (term-term-name "vt100") ; ??
934 (buf (get-buffer "*lynx*"))
935 (proc (and buf (get-buffer-process buf)))
936 (n browse-url-lynx-input-attempts))
937 (if (and (browse-url-maybe-new-window new-buffer) buf)
938 ;; Rename away the OLD buffer. This isn't very polite, but
939 ;; term insists on working in a buffer named *lynx* and would
940 ;; choke on *lynx*<1>
941 (progn (set-buffer buf)
942 (rename-uniquely)))
943 (if (or (browse-url-maybe-new-window new-buffer)
944 (not buf)
945 (not proc)
946 (not (memq (process-status proc) '(run stop))))
947 ;; start a new lynx
948 (progn (setq buf (make-term "lynx" "lynx" nil url))
949 (switch-to-buffer buf)
950 (term-char-mode)
951 (set-process-sentinel
952 (get-buffer-process buf)
953 ;; Don't leave around a dead one (especially because
954 ;; of its munged keymap.)
955 (lambda (process event)
956 (if (not (memq (process-status process) '(run stop)))
957 (let ((buf (process-buffer process)))
958 (if buf (kill-buffer buf)))))))
959 ;; send the url to lynx in the old buffer
960 (let ((win (get-buffer-window buf t)))
961 (if win
962 (select-window win)
963 (switch-to-buffer buf)))
964 (if (eq (following-char) ?_)
965 (cond ((eq browse-url-lynx-input-field 'warn)
966 (error "Please move out of the input field first."))
967 ((eq browse-url-lynx-input-field 'avoid)
968 (while (and (eq (following-char) ?_) (> n 0))
969 (term-send-down) ; down arrow
970 (sit-for browse-url-lynx-input-delay))
971 (if (eq (following-char) ?_)
972 (error "Cannot move out of the input field, sorry.")))))
973 (term-send-string proc (concat "g" ; goto
974 "\C-u" ; kill default url
976 "\r")))))
978 ;; --- MMM ---
980 ;;;###autoload
981 (defun browse-url-mmm (url &optional new-window)
982 "Ask the MMM WWW browser to load URL.
983 Default to the URL around or before point."
984 (interactive (browse-url-interactive-arg "MMM URL: "))
985 (message "Sending URL to MMM...")
986 (save-excursion
987 (set-buffer (get-buffer-create " *Shell Command Output*"))
988 (erase-buffer)
989 ;; mmm_remote just SEGVs if the file isn't there...
990 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
991 ;; location in v 0.4:
992 (file-exists-p (expand-file-name "~/.mmm/remote")))
993 (call-process "mmm_remote" nil 0 nil url)
994 (call-process "mmm" nil 0 nil "-external" url))
995 (message "Sending URL to MMM... done")))
997 ;; --- mailto ---
999 ;;;###autoload
1000 (defun browse-url-mail (url &optional new-window)
1001 "Open a new mail message buffer within Emacs.
1002 Default to using the mailto: URL around or before point as the
1003 recipient's address. Supplying a non-nil interactive prefix argument
1004 will cause the mail to be composed in another window rather than the
1005 current one.
1007 When called interactively, if variable `browse-url-new-window-p' is
1008 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1009 non-nil interactive prefix argument reverses the effect of
1010 `browse-url-new-window-p'.
1012 When called non-interactively, optional second argument NEW-WINDOW is
1013 used instead of `browse-url-new-window-p'."
1014 (interactive (browse-url-interactive-arg "Mailto URL: "))
1015 (save-excursion
1016 (let ((func (if (browse-url-maybe-new-window new-window)
1017 'compose-mail-other-window
1018 'compose-mail))
1019 (to (if (string-match "^mailto:" url)
1020 (substring url 7)
1021 url)))
1022 (apply func
1023 (list to nil nil nil nil nil (cons 'insert-buffer
1024 (current-buffer)))))))
1026 ;; --- Random browser ---
1028 ;;;###autoload
1029 (defun browse-url-generic (url &optional new-window)
1030 ;; new-window ignored
1031 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1032 Default to the URL around or before point. A fresh copy of the
1033 browser is started up in a new process with possible additional arguments
1034 `browse-url-generic-args'. This is appropriate for browsers which
1035 don't offer a form of remote control."
1036 (interactive (browse-url-interactive-arg "URL: "))
1037 (if (not browse-url-generic-program)
1038 (error "No browser defined (`browse-url-generic-program')"))
1039 (apply 'start-process (concat browse-url-generic-program url) nil
1040 browse-url-generic-program
1041 (append browse-url-generic-args (list url))))
1043 (provide 'browse-url)
1045 ;;; browse-url.el ends here