Fix maintainer address.
[emacs.git] / lisp / browse-url.el
blob660383ab04458efbf412ddff4fb3e903b4ba412d
1 ;;; browse-url.el --- Pass a URL to a WWW browser
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
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
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 ;; 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:
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
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.]
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>.
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.
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?
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>
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>
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/>.
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>.
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/>.
93 ;; Lynx is now distributed by the FSF. See also
94 ;; <URL:http://lynx.browser.org/>.
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.
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/>
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.
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.
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;; Help!
124 ;; Can you write and test some code for the Macintrash and Windoze
125 ;; Netscape remote control APIs? (See the URL above).
127 ;; Do any other browsers have remote control?
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130 ;; Usage
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
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.)
144 ;; To display the current buffer in a web browser:
145 ;; M-x browse-url-of-buffer RET
147 ;; To display the current region in a web browser:
148 ;; M-x browse-url-of-region RET
150 ;; In Dired, to display the file named on the current line:
151 ;; M-x browse-url-of-dired-file RET
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 ;; Customisation (~/.emacs)
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'.
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)))
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)))
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.]
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))
186 ;; To always save modified buffers before displaying the file in a browser:
187 ;; (setq browse-url-save-file t)
189 ;; To get round the Netscape caching problem, you could EITHER have
190 ;; write-file in html-helper-mode make Netscape reload the document:
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
204 ;; OR have browse-url-of-file ask Netscape to load and then reload the
205 ;; file:
207 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
209 ;; You may also want to customise browse-url-netscape-arguments, e.g.
210 ;; (setq browse-url-netscape-arguments '("-install"))
212 ;; or similarly for the other browsers.
214 ;; To invoke different browsers for different URLs:
215 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
216 ;; ("." . browse-url-netscape)))
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;;; Code:
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 ;; Variables
224 (eval-when-compile (require 'thingatpt)
225 (require 'term)
226 (require 'w3-auto nil t))
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 (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.
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)
269 (defcustom browse-url-netscape-program "netscape"
270 ;; Info about netscape-remote from Karl Berry.
271 "The name by which to invoke Netscape.
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)
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)
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)
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)
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)
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)
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)
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 `/'.
333 For example, adding to the default a specific translation of an ange-ftp
334 address to an HTTP URL:
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:/\")))
343 :type '(repeat (cons :format "%v"
344 (regexp :tag "Regexp")
345 (string :tag "Replacement")))
346 :version "20.3"
347 :group 'browse-url)
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)
356 (defcustom browse-url-of-file-hook nil
357 "Run after `browse-url-of-file' has asked a browser to load a file.
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)
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.")
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)
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)
388 (defvar browse-url-temp-file-name nil)
389 (make-variable-buffer-local 'browse-url-temp-file-name)
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)
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)
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.
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)
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)
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)
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)
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)
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)
444 (defcustom browse-url-netscape-version
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)
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)
466 (defvar browse-url-lynx-input-attempts 10
467 "*How many times to try to move down from a series of lynx input fields.")
469 (defcustom browse-url-lynx-input-delay 0.2
470 "How many seconds to wait for lynx between moves down from an input field.")
472 (defvar browse-url-temp-file-list '())
474 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
475 ;; URL input
477 (defun browse-url-url-at-point ()
478 (let ((url (thing-at-point 'url)))
479 (set-text-properties 0 (length url) nil url)
480 url))
482 ;; Having this as a separate function called by the browser-specific
483 ;; functions allows them to be stand-alone commands, making it easier
484 ;; to switch between browsers.
486 (defun browse-url-interactive-arg (prompt)
487 "Read a URL from the minibuffer, prompting with PROMPT.
488 Default to the URL at or before point. If invoked with a mouse button,
489 set point to the position clicked first. Return a list for use in
490 `interactive' containing the URL and `browse-url-new-window-p' or its
491 negation if a prefix argument was given."
492 (let ((event (elt (this-command-keys) 0)))
493 (and (listp event) (mouse-set-point event)))
494 (list (read-string prompt (browse-url-url-at-point))
495 (not (eq (null browse-url-new-window-p)
496 (null current-prefix-arg)))))
498 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
499 ;; Browse current buffer
501 ;;;###autoload
502 (defun browse-url-of-file (&optional file)
503 "Ask a WWW browser to display FILE.
504 Display the current buffer's file if FILE is nil or if called
505 interactively. Turn the filename into a URL with function
506 `browse-url-file-url'. Pass the URL to a browser using the
507 `browse-url' function then run `browse-url-of-file-hook'."
508 (interactive)
509 (or file
510 (setq file (buffer-file-name))
511 (error "Current buffer has no file"))
512 (let ((buf (get-file-buffer file)))
513 (if buf
514 (save-excursion
515 (set-buffer buf)
516 (cond ((not (buffer-modified-p)))
517 (browse-url-save-file (save-buffer))
518 (t (message "%s modified since last save" file))))))
519 (browse-url (browse-url-file-url file))
520 (run-hooks 'browse-url-of-file-hook))
522 (defun browse-url-file-url (file)
523 "Return the URL corresponding to FILE.
524 Use variable `browse-url-filename-alist' to map filenames to URLs."
525 ;; URL-encode special chars, do % first
526 (let ((s 0))
527 (while (setq s (string-match "%" file s))
528 (setq file (replace-match "%25" t t file)
529 s (1+ s))))
530 (while (string-match "[*\"()',=;? ]" file)
531 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
532 (setq file (replace-match enc t t file))))
533 (let ((maps browse-url-filename-alist))
534 (while maps
535 (let* ((map (car maps))
536 (from-re (car map))
537 (to-string (cdr map)))
538 (setq maps (cdr maps))
539 (and (string-match from-re file)
540 (setq file (replace-match to-string t nil file))))))
541 file)
543 ;;;###autoload
544 (defun browse-url-of-buffer (&optional buffer)
545 "Ask a WWW browser to display BUFFER.
546 Display the current buffer if BUFFER is nil. Display only the
547 currently visible part of BUFFER (from a temporary file) if buffer is
548 narrowed."
549 (interactive)
550 (save-excursion
551 (and buffer (set-buffer buffer))
552 (let ((file-name
553 ;; Ignore real name if restricted
554 (and (= (- (point-max) (point-min)) (buffer-size))
555 (or buffer-file-name
556 (and (boundp 'dired-directory) dired-directory)))))
557 (or file-name
558 (progn
559 (or browse-url-temp-file-name
560 (setq browse-url-temp-file-name
561 (convert-standard-filename
562 (make-temp-name
563 (expand-file-name "burl" browse-url-temp-dir)))))
564 (setq file-name browse-url-temp-file-name)
565 (write-region (point-min) (point-max) file-name nil 'no-message)))
566 (browse-url-of-file file-name))))
568 (defun browse-url-delete-temp-file (&optional temp-file-name)
569 ;; Delete browse-url-temp-file-name from the file system and from
570 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
571 ;; non-nil, delete it instead, but only from the file system --
572 ;; browse-url-temp-file-list is not affected.
573 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
574 (if (and file-name (file-exists-p file-name))
575 (progn
576 (delete-file file-name)
577 (if (null temp-file-name)
578 (setq browse-url-temp-file-list
579 (delete browse-url-temp-file-name
580 browse-url-temp-file-list)))))))
582 (defun browse-url-delete-temp-file-list ()
583 ;; Delete all elements of browse-url-temp-file-list.
584 (while browse-url-temp-file-list
585 (browse-url-delete-temp-file (car browse-url-temp-file-list))
586 (setq browse-url-temp-file-list
587 (cdr browse-url-temp-file-list))))
589 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
590 (add-hook 'kill-emacs-hook 'browse-url-delete-temp-file-list)
592 ;;;###autoload
593 (defun browse-url-of-dired-file ()
594 "In Dired, ask a WWW browser to display the file named on this line."
595 (interactive)
596 (browse-url-of-file (dired-get-filename)))
598 ;;;###autoload
599 (defun browse-url-of-region (min max)
600 "Ask a WWW browser to display the current region."
601 (interactive "r")
602 (save-excursion
603 (save-restriction
604 (narrow-to-region min max)
605 (browse-url-of-buffer))))
607 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
608 ;; Browser-independent commands
610 ;; A generic command to call the current browse-url-browser-function
612 ;;;###autoload
613 (defun browse-url (url &rest args)
614 "Ask a WWW browser to load URL.
615 Prompts for a URL, defaulting to the URL at or before point. Variable
616 `browse-url-browser-function' says which browser to use."
617 (interactive (browse-url-interactive-arg "URL: "))
618 (let ((bf browse-url-browser-function) re)
619 (unless (functionp bf)
620 (while (consp bf)
621 (setq re (car (car bf))
622 bf (if (string-match re url)
623 (cdr (car bf)) ; The function
624 (cdr bf))))) ; More pairs
625 (or bf (error "No browser in browse-url-browser-function matching URL %s"
626 url))
627 (apply bf url args)))
629 ;;;###autoload
630 (defun browse-url-at-point ()
631 "Ask a WWW browser to load the URL at or before point.
632 Doesn't let you edit the URL like `browse-url'. Variable
633 `browse-url-browser-function' says which browser to use."
634 (interactive)
635 (browse-url (browse-url-url-at-point)))
637 (defun browse-url-event-buffer (event)
638 (window-buffer (posn-window (event-start event))))
640 (defun browse-url-event-point (event)
641 (posn-point (event-start event)))
643 ;;;###autoload
644 (defun browse-url-at-mouse (event)
645 "Ask a WWW browser to load a URL clicked with the mouse.
646 The URL is the one around or before the position of the mouse click
647 but point is not changed. Doesn't let you edit the URL like
648 `browse-url'. Variable `browse-url-browser-function' says which browser
649 to use."
650 (interactive "e")
651 (save-excursion
652 (set-buffer (browse-url-event-buffer event))
653 (goto-char (browse-url-event-point event))
654 (let ((url (browse-url-url-at-point)))
655 (if (string-equal url "")
656 (error "No URL found"))
657 (browse-url url browse-url-new-window-p))))
659 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
660 ;; Browser-specific commands
662 ;; --- Default MS-Windows browser ---
664 (defun browse-url-default-windows-browser (url &optional new-window)
665 (interactive (browse-url-interactive-arg "URL: "))
666 (w32-shell-execute "open" url))
668 ;; --- Netscape ---
670 (defun browse-url-process-environment ()
671 "Set DISPLAY in the environment to the X display Netscape is running on.
672 This is either the value of variable `browse-url-netscape-display' if
673 non-nil, or the same display as Emacs if different from the current
674 environment, otherwise just use the current environment."
675 (let ((display (or browse-url-netscape-display (browse-url-emacs-display))))
676 (if display
677 (cons (concat "DISPLAY=" display) process-environment)
678 process-environment)))
680 (defun browse-url-emacs-display ()
681 "Return the X display Emacs is running on.
682 This is nil if the display is the same as the DISPLAY environment variable.
684 Actually Emacs could be using several displays; this just returns the
685 one showing the selected frame."
686 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
687 (and (not (equal display (getenv "DISPLAY")))
688 display)))
690 ;;;###autoload
691 (defun browse-url-netscape (url &optional new-window)
692 "Ask the Netscape WWW browser to load URL.
694 Default to the URL around or before point. The strings in variable
695 `browse-url-netscape-arguments' are also passed to Netscape.
697 When called interactively, if variable `browse-url-new-window-p' is
698 non-nil, load the document in a new Netscape window, otherwise use a
699 random existing one. A non-nil interactive prefix argument reverses
700 the effect of `browse-url-new-window-p'.
702 When called non-interactively, optional second argument NEW-WINDOW is
703 used instead of `browse-url-new-window-p'."
704 (interactive (browse-url-interactive-arg "Netscape URL: "))
705 ;; URL encode any `confusing' characters in the URL. This needs to
706 ;; include at least commas; presumably also close parens.
707 (while (string-match "[,)]" url)
708 (setq url (replace-match
709 (format "%%%x" (string-to-char (match-string 0 url))) t t url)))
710 (let* ((process-environment (browse-url-process-environment))
711 (process (apply 'start-process
712 (concat "netscape " url) nil
713 browse-url-netscape-program
714 (append
715 browse-url-netscape-arguments
716 (if (eq window-system 'w32)
717 (list url)
718 (append
719 (if new-window '("-noraise"))
720 (list "-remote"
721 (concat "openURL(" url
722 (if new-window ",new-window")
723 ")"))))))))
724 (set-process-sentinel process
725 (list 'lambda '(process change)
726 (list 'browse-url-netscape-sentinel 'process url)))))
728 (defun browse-url-netscape-sentinel (process url)
729 "Handle a change to the process communicating with Netscape."
730 (or (eq (process-exit-status process) 0)
731 (let* ((process-environment (browse-url-process-environment)))
732 ;; Netscape not running - start it
733 (message "Starting Netscape...")
734 (apply 'start-process (concat "netscape" url) nil
735 browse-url-netscape-program
736 (append browse-url-netscape-startup-arguments (list url))))))
738 (defun browse-url-netscape-reload ()
739 "Ask Netscape to reload its current document.
740 How depends on `browse-url-netscape-version'."
741 (interactive)
742 ;; Backwards incompatibility reported by
743 ;; <peter.kruse@psychologie.uni-regensburg.de>.
744 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
745 "xfeDoCommand(reload)"
746 "reload")))
748 (defun browse-url-netscape-send (command)
749 "Send a remote control command to Netscape."
750 (let* ((process-environment (browse-url-process-environment)))
751 (apply 'start-process "netscape" nil
752 browse-url-netscape-program
753 (append browse-url-netscape-arguments
754 (list "-remote" command)))))
756 ;; --- Mosaic ---
758 ;;;###autoload
759 (defun browse-url-mosaic (url &optional new-window)
760 "Ask the XMosaic WWW browser to load URL.
762 Default to the URL around or before point. The strings in variable
763 `browse-url-mosaic-arguments' are also passed to Mosaic and the
764 program is invoked according to the variable
765 `browse-url-mosaic-program'.
767 When called interactively, if variable `browse-url-new-window-p' is
768 non-nil, load the document in a new Mosaic window, otherwise use a
769 random existing one. A non-nil interactive prefix argument reverses
770 the effect of `browse-url-new-window-p'.
772 When called non-interactively, optional second argument NEW-WINDOW is
773 used instead of `browse-url-new-window-p'."
774 (interactive (browse-url-interactive-arg "Mosaic URL: "))
775 (let ((pidfile (expand-file-name "~/.mosaicpid"))
776 pid)
777 (if (file-readable-p pidfile)
778 (save-excursion
779 (find-file pidfile)
780 (goto-char (point-min))
781 (setq pid (read (current-buffer)))
782 (kill-buffer nil)))
783 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
784 (save-excursion
785 (find-file (format "/tmp/Mosaic.%d" pid))
786 (erase-buffer)
787 (insert (if new-window
788 "newwin\n"
789 "goto\n")
790 url "\n")
791 (save-buffer)
792 (kill-buffer nil)
793 ;; Send signal SIGUSR to Mosaic
794 (message "Signalling Mosaic...")
795 (signal-process pid browse-url-usr1-signal)
796 ;; Or you could try:
797 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
798 (message "Signalling Mosaic...done")
800 ;; Mosaic not running - start it
801 (message "Starting Mosaic...")
802 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
803 (append browse-url-mosaic-arguments (list url)))
804 (message "Starting Mosaic...done"))))
806 ;; --- Grail ---
808 ;;;###autoload
809 (defvar browse-url-grail
810 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
811 "Location of Grail remote control client script `rcgrail.py'.
812 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
814 ;;;###autoload
815 (defun browse-url-grail (url &optional new-window)
816 "Ask the Grail WWW browser to load URL.
817 Default to the URL around or before point. Runs the program in the
818 variable `browse-url-grail'."
819 (interactive (browse-url-interactive-arg "Grail URL: "))
820 (message "Sending URL to Grail...")
821 (save-excursion
822 (set-buffer (get-buffer-create " *Shell Command Output*"))
823 (erase-buffer)
824 ;; don't worry about this failing.
825 (if new-window
826 (call-process browse-url-grail nil 0 nil "-b" url)
827 (call-process browse-url-grail nil 0 nil url))
828 (message "Sending URL to Grail... done")))
830 ;; --- Mosaic using CCI ---
832 ;;;###autoload
833 (defun browse-url-cci (url &optional new-window)
834 "Ask the XMosaic WWW browser to load URL.
835 Default to the URL around or before point.
837 This function only works for XMosaic version 2.5 or later. You must
838 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
839 value of variable `browse-url-CCI-port', and enable `Accept requests'.
841 When called interactively, if variable `browse-url-new-window-p' is
842 non-nil, load the document in a new browser window, otherwise use a
843 random existing one. A non-nil interactive prefix argument reverses
844 the effect of `browse-url-new-window-p'.
846 When called non-interactively, optional second argument NEW-WINDOW is
847 used instead of `browse-url-new-window-p'."
848 (interactive (browse-url-interactive-arg "Mosaic URL: "))
849 (open-network-stream "browse-url" " *browse-url*"
850 browse-url-CCI-host browse-url-CCI-port)
851 ;; Todo: start browser if fails
852 (process-send-string "browse-url"
853 (concat "get url (" url ") output "
854 (if new-window
855 "new"
856 "current")
857 "\r\n"))
858 (process-send-string "browse-url" "disconnect\r\n")
859 (delete-process "browse-url"))
861 ;; --- IXI Mosaic ---
863 ;;;###autoload
864 (defun browse-url-iximosaic (url &optional new-window)
865 ;; new-window ignored
866 "Ask the IXIMosaic WWW browser to load URL.
867 Default to the URL around or before point."
868 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
869 (start-process "tellw3b" nil "tellw3b"
870 "-service WWW_BROWSER ixi_showurl " url))
872 ;; --- W3 ---
874 ;;;###autoload
875 (defun browse-url-w3 (url &optional new-window)
876 "Ask the w3 WWW browser to load URL.
877 Default to the URL around or before point.
879 When called interactively, if variable `browse-url-new-window-p' is
880 non-nil, load the document in a new window. A non-nil interactive
881 prefix argument reverses the effect of `browse-url-new-window-p'.
883 When called non-interactively, optional second argument NEW-WINDOW is
884 used instead of `browse-url-new-window-p'."
885 (interactive (browse-url-interactive-arg "W3 URL: "))
886 (require 'w3) ; w3-fetch-other-window not autoloaded
887 (if new-window
888 (w3-fetch-other-window url)
889 (w3-fetch url)))
891 ;;;###autoload
892 (defun browse-url-w3-gnudoit (url &optional new-window)
893 ;; new-window ignored
894 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
895 The `browse-url-gnudoit-program' program is used with options given by
896 `browse-url-gnudoit-args'. Default to the URL around or before point."
897 (interactive (browse-url-interactive-arg "W3 URL: "))
898 (apply 'start-process (concat "gnudoit:" url) nil
899 browse-url-gnudoit-program
900 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
902 ;; --- Lynx in an xterm ---
904 ;;;###autoload
905 (defun browse-url-lynx-xterm (url &optional new-window)
906 ;; new-window ignored
907 "Ask the Lynx WWW browser to load URL.
908 Default to the URL around or before point. A new Lynx process is run
909 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
910 with possible additional arguments `browse-url-xterm-args'."
911 (interactive (browse-url-interactive-arg "Lynx URL: "))
912 (apply #'start-process `(,(concat "lynx" url) nil ,browse-url-xterm-program
913 ,@browse-url-xterm-args "-e" "lynx" ,url)))
915 ;; --- Lynx in an Emacs "term" window ---
917 ;;;###autoload
918 (defun browse-url-lynx-emacs (url &optional new-buffer)
919 "Ask the Lynx WWW browser to load URL.
920 Default to the URL around or before point. With a prefix argument, run
921 a new Lynx process in a new buffer.
923 When called interactively, if variable `browse-url-new-window-p' is
924 non-nil, load the document in a new lynx in a new term window,
925 otherwise use any existing one. A non-nil interactive prefix argument
926 reverses the effect of `browse-url-new-window-p'.
928 When called non-interactively, optional second argument NEW-WINDOW is
929 used instead of `browse-url-new-window-p'."
930 (interactive (browse-url-interactive-arg "Lynx URL: "))
931 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
932 ;; (term-term-name "vt100") ; ??
933 (buf (get-buffer "*lynx*"))
934 (proc (and buf (get-buffer-process buf)))
935 (n browse-url-lynx-input-attempts))
936 (if (and new-buffer buf)
937 ;; Rename away the OLD buffer. This isn't very polite, but
938 ;; term insists on working in a buffer named *lynx* and would
939 ;; choke on *lynx*<1>
940 (progn (set-buffer buf)
941 (rename-uniquely)))
942 (if (or new-buffer
943 (not buf)
944 (not proc)
945 (not (memq (process-status proc) '(run stop))))
946 ;; start a new lynx
947 (progn
948 (setq buf
949 (apply #'make-term
950 `("lynx" "lynx" nil ,@browse-url-lynx-emacs-args ,url)))
951 (switch-to-buffer buf)
952 (term-char-mode)
953 (set-process-sentinel
954 (get-buffer-process buf)
955 ;; Don't leave around a dead one (especially because of its
956 ;; munged keymap.)
957 (lambda (process event)
958 (if (not (memq (process-status process) '(run stop)))
959 (let ((buf (process-buffer process)))
960 (if buf (kill-buffer buf)))))))
961 ;; send the url to lynx in the old buffer
962 (let ((win (get-buffer-window buf t)))
963 (if win
964 (select-window win)
965 (switch-to-buffer buf)))
966 (if (eq (following-char) ?_)
967 (cond ((eq browse-url-lynx-input-field 'warn)
968 (error "Please move out of the input field first."))
969 ((eq browse-url-lynx-input-field 'avoid)
970 (while (and (eq (following-char) ?_) (> n 0))
971 (term-send-down) ; down arrow
972 (sit-for browse-url-lynx-input-delay))
973 (if (eq (following-char) ?_)
974 (error "Cannot move out of the input field, sorry.")))))
975 (term-send-string proc (concat "g" ; goto
976 "\C-u" ; kill default url
978 "\r")))))
980 ;; --- MMM ---
982 ;;;###autoload
983 (defun browse-url-mmm (url &optional new-window)
984 "Ask the MMM WWW browser to load URL.
985 Default to the URL around or before point."
986 (interactive (browse-url-interactive-arg "MMM URL: "))
987 (message "Sending URL to MMM...")
988 (save-excursion
989 (set-buffer (get-buffer-create " *Shell Command Output*"))
990 (erase-buffer)
991 ;; mmm_remote just SEGVs if the file isn't there...
992 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
993 ;; location in v 0.4:
994 (file-exists-p (expand-file-name "~/.mmm/remote")))
995 (call-process "mmm_remote" nil 0 nil url)
996 (call-process "mmm" nil 0 nil "-external" url))
997 (message "Sending URL to MMM... done")))
999 ;; --- mailto ---
1001 ;;;###autoload
1002 (defun browse-url-mail (url &optional new-window)
1003 "Open a new mail message buffer within Emacs.
1004 Default to using the mailto: URL around or before point as the
1005 recipient's address. Supplying a non-nil interactive prefix argument
1006 will cause the mail to be composed in another window rather than the
1007 current one.
1009 When called interactively, if variable `browse-url-new-window-p' is
1010 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1011 non-nil interactive prefix argument reverses the effect of
1012 `browse-url-new-window-p'.
1014 When called non-interactively, optional second argument NEW-WINDOW is
1015 used instead of `browse-url-new-window-p'."
1016 (interactive (browse-url-interactive-arg "Mailto URL: "))
1017 (save-excursion
1018 (let ((to (if (string-match "^mailto:" url)
1019 (substring url 7)
1020 url)))
1021 (if new-window
1022 (compose-mail-other-window to nil nil nil
1023 (list 'insert-buffer (current-buffer)))
1024 (compose-mail to nil nil nil nil
1025 (list 'insert-buffer (current-buffer)))))))
1027 ;; --- Random browser ---
1029 ;;;###autoload
1030 (defun browse-url-generic (url &optional new-window)
1031 ;; new-window ignored
1032 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1033 Default to the URL around or before point. A fresh copy of the
1034 browser is started up in a new process with possible additional arguments
1035 `browse-url-generic-args'. This is appropriate for browsers which
1036 don't offer a form of remote control."
1037 (interactive (browse-url-interactive-arg "URL: "))
1038 (if (not browse-url-generic-program)
1039 (error "No browser defined (`browse-url-generic-program')"))
1040 (apply 'start-process (concat browse-url-generic-program url) nil
1041 browse-url-generic-program
1042 (append browse-url-generic-args (list url))))
1044 (provide 'browse-url)
1046 ;;; browse-url.el ends here