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)
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.
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
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 ;; [The Netscape browser is now free software
50 ;; <URL:http://www.mozilla.org/>, albeit not GPLed, so it is
51 ;; reasonable to keep it as the default.]
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
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
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
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
138 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
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
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
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
196 ;; (add-hook 'local-write-file-hooks
198 ;; (let ((local-write-file-hooks))
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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 (eval-when-compile (require 'thingatpt)
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-"
234 (defcustom browse-url-browser-function
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."
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"))
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.
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
273 /usr/local/bin/netscape-remote \"$@\" > /dev/null 2>&1
278 (defcustom browse-url-netscape-arguments nil
279 "A list of strings to pass to Netscape as arguments."
280 :type '(repeat (string :tag "Argument"))
283 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
284 "A list of strings to pass to Netscape when it starts up.
285 Defaults to the value of `browse-url-netscape-arguments' at the time
286 `browse-url' is loaded."
287 :type '(repeat (string :tag "Argument"))
291 (defcustom browse-url-new-window-p nil
292 "*If non-nil, always open a new browser window with appropriate browsers.
293 Passing an interactive argument to \\[browse-url], or specific browser
294 commands reverses the effect of this variable. Requires Netscape version
295 1.1N or later or XMosaic version 2.5 or later if using those browsers."
300 (defcustom browse-url-netscape-display nil
301 "*The X display for running Netscape, if not same as Emacs'."
302 :type '(choice string (const :tag "Default" nil))
305 (defcustom browse-url-mosaic-program "xmosaic"
306 "The name by which to invoke Mosaic (or mMosaic)."
311 (defcustom browse-url-mosaic-arguments nil
312 "A list of strings to pass to Mosaic as arguments."
313 :type '(repeat (string :tag "Argument"))
316 (defcustom browse-url-filename-alist
317 '(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
318 ;; The above loses the username to avoid the browser prompting for
319 ;; it in anonymous cases. If it's not anonymous the next regexp
321 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
323 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
324 Any substring of a filename matching one of the REGEXPs is replaced by
325 the corresponding STRING using `replace-match', not treating STRING
326 literally. All pairs are applied in the order given. The default
327 value converts ange-ftp/EFS-style paths into ftp URLs and prepends
328 `file:' to any path beginning with `/'.
330 For example, adding to the default a specific translation of an ange-ftp
331 address to an HTTP URL:
333 (setq browse-url-filename-alist
334 '((\"/webmaster@webserver:/home/www/html/\" .
335 \"http://www.acme.co.uk/\")
336 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
337 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
338 (\"^/+\" . \"file:/\")))
340 :type '(repeat (cons :format "%v"
341 (regexp :tag "Regexp")
342 (string :tag "Replacement")))
347 (defcustom browse-url-save-file nil
348 "*If non-nil, save the buffer before displaying its file.
349 Used by the `browse-url-of-file' command."
353 (defcustom browse-url-of-file-hook nil
354 "Run after `browse-url-of-file' has asked a browser to load a file.
356 Set this to `browse-url-netscape-reload' to force Netscape to load the
357 file rather than displaying a cached copy."
359 :options '(browse-url-netscape-reload)
362 (defvar browse-url-usr1-signal
363 (if (and (boundp 'emacs-major-version)
364 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
365 'SIGUSR1 ; Why did I think this was in lower case before?
366 30) ; Check /usr/include/signal.h.
367 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
368 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
369 which is 30 on SunOS and 16 on HP-UX and Solaris.")
371 (defcustom browse-url-CCI-port 3003
372 "Port to access XMosaic via CCI.
373 This can be any number between 1024 and 65535 but must correspond to
374 the value set in the browser."
378 (defcustom browse-url-CCI-host "localhost"
379 "*Host to access XMosaic via CCI.
380 This should be the host name of the machine running XMosaic with CCI
381 enabled. The port number should be set in `browse-url-CCI-port'."
385 (defvar browse-url-temp-file-name nil)
386 (make-variable-buffer-local 'browse-url-temp-file-name)
388 (defcustom browse-url-xterm-program "xterm"
389 "The name of the terminal emulator used by `browse-url-lynx-xterm'.
390 This might, for instance, be a separate colour version of xterm."
394 (defcustom browse-url-xterm-args nil
395 "*A list of strings defining options for `browse-url-xterm-program'.
396 These might set its size, for instance."
397 :type '(repeat (string :tag "Argument"))
400 (defcustom browse-url-lynx-emacs-args (and (not window-system)
402 "A list of strings defining options for Lynx in an Emacs buffer.
404 The default is none in a window system, otherwise `-show_cursor' to
405 indicate the position of the current link in the absence of
406 highlighting, assuming the normal default for showing the cursor."
407 :type '(repeat (string :tag "Argument"))
411 (defcustom browse-url-gnudoit-program "gnudoit"
412 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
416 (defcustom browse-url-gnudoit-args '("-q")
417 "*A list of strings defining options for `browse-url-gnudoit-program'.
418 These might set the port, for instance."
419 :type '(repeat (string :tag "Argument"))
423 (defcustom browse-url-generic-program nil
424 "*The name of the browser program used by `browse-url-generic'."
425 :type '(choice string (const :tag "None" nil))
428 (defcustom browse-url-generic-args nil
429 "*A list of strings defining options for `browse-url-generic-program'."
430 :type '(repeat (string :tag "Argument"))
433 (defcustom browse-url-temp-dir temporary-file-directory
434 "The name of a directory for browse-url's temporary files.
435 Such files are generated by functions like `browse-url-of-region'.
436 You might want to set this to somewhere with restricted read permissions
441 (defcustom browse-url-netscape-version
443 "The version of Netscape you are using.
444 This affects how URL reloading is done; the mechanism changed
445 incompatibly at version 4."
449 (defcustom browse-url-lynx-input-field 'avoid
450 "*Action on selecting an existing Lynx buffer at an input field.
451 What to do when sending a new URL to an existing Lynx buffer in Emacs
452 if the Lynx cursor is on an input field (in which case the `g' command
453 would be entered as data). Such fields are recognized by the
454 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
455 user and don't emit the URL, 'avoid: try to avoid the field by moving
456 down (this *won't* always work)."
457 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
458 (const :tag "Disregard" :value nil)
459 (const :tag "Warn, don't emit URL" :value warn))
463 (defvar browse-url-lynx-input-attempts 10
464 "*How many times to try to move down from a series of lynx input fields.")
466 (defcustom browse-url-lynx-input-delay 0.2
467 "How many seconds to wait for lynx between moves down from an input field.")
469 (defvar browse-url-temp-file-list '())
471 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 (defun browse-url-url-at-point ()
475 (let ((url (thing-at-point 'url)))
476 (set-text-properties 0 (length url) nil url)
479 ;; Having this as a separate function called by the browser-specific
480 ;; functions allows them to be stand-alone commands, making it easier
481 ;; to switch between browsers.
483 (defun browse-url-interactive-arg (prompt)
484 "Read a URL from the minibuffer, prompting with PROMPT.
485 Default to the URL at or before point. If invoked with a mouse button,
486 set point to the position clicked first. Return a list for use in
487 `interactive' containing the URL and `browse-url-new-window-p' or its
488 negation if a prefix argument was given."
489 (let ((event (elt (this-command-keys) 0)))
490 (and (listp event) (mouse-set-point event)))
491 (list (read-string prompt (browse-url-url-at-point))
492 (not (eq (null browse-url-new-window-p)
493 (null current-prefix-arg)))))
495 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
496 ;; Browse current buffer
499 (defun browse-url-of-file (&optional file)
500 "Ask a WWW browser to display FILE.
501 Display the current buffer's file if FILE is nil or if called
502 interactively. Turn the filename into a URL with function
503 `browse-url-file-url'. Pass the URL to a browser using the
504 `browse-url' function then run `browse-url-of-file-hook'."
507 (setq file (buffer-file-name))
508 (error "Current buffer has no file"))
509 (let ((buf (get-file-buffer file)))
513 (cond ((not (buffer-modified-p)))
514 (browse-url-save-file (save-buffer))
515 (t (message "%s modified since last save" file))))))
516 (browse-url (browse-url-file-url file))
517 (run-hooks 'browse-url-of-file-hook))
519 (defun browse-url-file-url (file)
520 "Return the URL corresponding to FILE.
521 Use variable `browse-url-filename-alist' to map filenames to URLs."
522 ;; URL-encode special chars, do % first
524 (while (setq s (string-match "%" file s))
525 (setq file (replace-match "%25" t t file)
527 (while (string-match "[*\"()',=;? ]" file)
528 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
529 (setq file (replace-match enc t t file))))
530 (let ((maps browse-url-filename-alist))
532 (let* ((map (car maps))
534 (to-string (cdr map)))
535 (setq maps (cdr maps))
536 (and (string-match from-re file)
537 (setq file (replace-match to-string t nil file))))))
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
548 (and buffer (set-buffer buffer))
550 ;; Ignore real name if restricted
551 (and (= (- (point-max) (point-min)) (buffer-size))
553 (and (boundp 'dired-directory) dired-directory)))))
556 (or browse-url-temp-file-name
557 (setq browse-url-temp-file-name
558 (convert-standard-filename
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))
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)
590 (defun browse-url-of-dired-file ()
591 "In Dired, ask a WWW browser to display the file named on this line."
593 (browse-url-of-file (dired-get-filename)))
596 (defun browse-url-of-region (min max)
597 "Ask a WWW browser to display the current region."
601 (narrow-to-region min max)
602 (browse-url-of-buffer))))
604 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
605 ;; Browser-independent commands
607 ;; A generic command to call the current browse-url-browser-function
610 (defun browse-url (url &rest args)
611 "Ask a WWW browser to load URL.
612 Prompts for a URL, defaulting to the URL at or before point. Variable
613 `browse-url-browser-function' says which browser to use."
614 (interactive (browse-url-interactive-arg "URL: "))
615 (let ((bf browse-url-browser-function) re)
616 (unless (functionp bf)
618 (setq re (car (car bf))
619 bf (if (string-match re url)
620 (cdr (car bf)) ; The function
621 (cdr bf))))) ; More pairs
622 (or bf (error "No browser in browse-url-browser-function matching URL %s"
624 (apply bf url args)))
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."
632 (browse-url (browse-url-url-at-point)))
634 (defun browse-url-event-buffer (event)
635 (window-buffer (posn-window (event-start event))))
637 (defun browse-url-event-point (event)
638 (posn-point (event-start event)))
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
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))))
656 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
657 ;; Browser-specific commands
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))))
668 (cons (concat "DISPLAY=" display) process-environment)
669 process-environment)))
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.
675 Actually Emacs could be using several displays; this just returns the
676 one showing the selected frame."
677 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
678 (and (not (equal display (getenv "DISPLAY")))
682 (defun browse-url-netscape (url &optional new-window)
683 "Ask the Netscape WWW browser to load URL.
685 Default to the URL around or before point. The strings in variable
686 `browse-url-netscape-arguments' are also passed to Netscape.
688 When called interactively, if variable `browse-url-new-window-p' is
689 non-nil, load the document in a new Netscape window, otherwise use a
690 random existing one. A non-nil interactive prefix argument reverses
691 the effect of `browse-url-new-window-p'.
693 When called non-interactively, optional second argument NEW-WINDOW is
694 used instead of `browse-url-new-window-p'."
695 (interactive (browse-url-interactive-arg "Netscape URL: "))
696 ;; URL encode any `confusing' characters in the URL. This needs to
697 ;; include at least commas; presumably also close parens.
698 (while (string-match "[,)]" url)
699 (setq url (replace-match
700 (format "%%%x" (string-to-char (match-string 0 url))) t t url)))
701 (let* ((process-environment (browse-url-process-environment))
702 (process (apply 'start-process
703 (concat "netscape " url) nil
704 browse-url-netscape-program
706 browse-url-netscape-arguments
707 (if (eq window-system 'w32)
710 (if new-window '("-noraise"))
712 (concat "openURL(" url
713 (if new-window ",new-window")
715 (set-process-sentinel process
716 (list 'lambda '(process change)
717 (list 'browse-url-netscape-sentinel 'process url)))))
719 (defun browse-url-netscape-sentinel (process url)
720 "Handle a change to the process communicating with Netscape."
721 (or (eq (process-exit-status process) 0)
722 (let* ((process-environment (browse-url-process-environment)))
723 ;; Netscape not running - start it
724 (message "Starting Netscape...")
725 (apply 'start-process (concat "netscape" url) nil
726 browse-url-netscape-program
727 (append browse-url-netscape-startup-arguments (list url))))))
729 (defun browse-url-netscape-reload ()
730 "Ask Netscape to reload its current document.
731 How depends on `browse-url-netscape-version'."
733 ;; Backwards incompatibility reported by
734 ;; <peter.kruse@psychologie.uni-regensburg.de>.
735 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
736 "xfeDoCommand(reload)"
739 (defun browse-url-netscape-send (command)
740 "Send a remote control command to Netscape."
741 (let* ((process-environment (browse-url-process-environment)))
742 (apply 'start-process "netscape" nil
743 browse-url-netscape-program
744 (append browse-url-netscape-arguments
745 (list "-remote" command)))))
750 (defun browse-url-mosaic (url &optional new-window)
751 "Ask the XMosaic WWW browser to load URL.
753 Default to the URL around or before point. The strings in variable
754 `browse-url-mosaic-arguments' are also passed to Mosaic and the
755 program is invoked according to the variable
756 `browse-url-mosaic-program'.
758 When called interactively, if variable `browse-url-new-window-p' is
759 non-nil, load the document in a new Mosaic window, otherwise use a
760 random existing one. A non-nil interactive prefix argument reverses
761 the effect of `browse-url-new-window-p'.
763 When called non-interactively, optional second argument NEW-WINDOW is
764 used instead of `browse-url-new-window-p'."
765 (interactive (browse-url-interactive-arg "Mosaic URL: "))
766 (let ((pidfile (expand-file-name "~/.mosaicpid"))
768 (if (file-readable-p pidfile)
771 (goto-char (point-min))
772 (setq pid (read (current-buffer)))
774 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
776 (find-file (format "/tmp/Mosaic.%d" pid))
778 (insert (if new-window
784 ;; Send signal SIGUSR to Mosaic
785 (message "Signalling Mosaic...")
786 (signal-process pid browse-url-usr1-signal)
788 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
789 (message "Signalling Mosaic...done")
791 ;; Mosaic not running - start it
792 (message "Starting Mosaic...")
793 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
794 (append browse-url-mosaic-arguments (list url)))
795 (message "Starting Mosaic...done"))))
800 (defvar browse-url-grail
801 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
802 "Location of Grail remote control client script `rcgrail.py'.
803 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
806 (defun browse-url-grail (url &optional new-window)
807 "Ask the Grail WWW browser to load URL.
808 Default to the URL around or before point. Runs the program in the
809 variable `browse-url-grail'."
810 (interactive (browse-url-interactive-arg "Grail URL: "))
811 (message "Sending URL to Grail...")
813 (set-buffer (get-buffer-create " *Shell Command Output*"))
815 ;; don't worry about this failing.
817 (call-process browse-url-grail nil 0 nil "-b" url)
818 (call-process browse-url-grail nil 0 nil url))
819 (message "Sending URL to Grail... done")))
821 ;; --- Mosaic using CCI ---
824 (defun browse-url-cci (url &optional new-window)
825 "Ask the XMosaic WWW browser to load URL.
826 Default to the URL around or before point.
828 This function only works for XMosaic version 2.5 or later. You must
829 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
830 value of variable `browse-url-CCI-port', and enable `Accept requests'.
832 When called interactively, if variable `browse-url-new-window-p' is
833 non-nil, load the document in a new browser window, otherwise use a
834 random existing one. A non-nil interactive prefix argument reverses
835 the effect of `browse-url-new-window-p'.
837 When called non-interactively, optional second argument NEW-WINDOW is
838 used instead of `browse-url-new-window-p'."
839 (interactive (browse-url-interactive-arg "Mosaic URL: "))
840 (open-network-stream "browse-url" " *browse-url*"
841 browse-url-CCI-host browse-url-CCI-port)
842 ;; Todo: start browser if fails
843 (process-send-string "browse-url"
844 (concat "get url (" url ") output "
849 (process-send-string "browse-url" "disconnect\r\n")
850 (delete-process "browse-url"))
852 ;; --- IXI Mosaic ---
855 (defun browse-url-iximosaic (url &optional new-window)
856 ;; new-window ignored
857 "Ask the IXIMosaic WWW browser to load URL.
858 Default to the URL around or before point."
859 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
860 (start-process "tellw3b" nil "tellw3b"
861 "-service WWW_BROWSER ixi_showurl " url))
866 (defun browse-url-w3 (url &optional new-window)
867 "Ask the w3 WWW browser to load URL.
868 Default to the URL around or before point.
870 When called interactively, if variable `browse-url-new-window-p' is
871 non-nil, load the document in a new window. A non-nil interactive
872 prefix argument reverses the effect of `browse-url-new-window-p'.
874 When called non-interactively, optional second argument NEW-WINDOW is
875 used instead of `browse-url-new-window-p'."
876 (interactive (browse-url-interactive-arg "W3 URL: "))
877 (require 'w3) ; w3-fetch-other-window not autoloaded
879 (w3-fetch-other-window url)
883 (defun browse-url-w3-gnudoit (url &optional new-window)
884 ;; new-window ignored
885 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
886 The `browse-url-gnudoit-program' program is used with options given by
887 `browse-url-gnudoit-args'. Default to the URL around or before point."
888 (interactive (browse-url-interactive-arg "W3 URL: "))
889 (apply 'start-process (concat "gnudoit:" url) nil
890 browse-url-gnudoit-program
891 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
893 ;; --- Lynx in an xterm ---
896 (defun browse-url-lynx-xterm (url &optional new-window)
897 ;; new-window ignored
898 "Ask the Lynx WWW browser to load URL.
899 Default to the URL around or before point. A new Lynx process is run
900 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
901 with possible additional arguments `browse-url-xterm-args'."
902 (interactive (browse-url-interactive-arg "Lynx URL: "))
903 (apply #'start-process `(,(concat "lynx" url) nil ,browse-url-xterm-program
904 ,@browse-url-xterm-args "-e" "lynx" ,url)))
906 ;; --- Lynx in an Emacs "term" window ---
909 (defun browse-url-lynx-emacs (url &optional new-buffer)
910 "Ask the Lynx WWW browser to load URL.
911 Default to the URL around or before point. With a prefix argument, run
912 a new Lynx process in a new buffer.
914 When called interactively, if variable `browse-url-new-window-p' is
915 non-nil, load the document in a new lynx in a new term window,
916 otherwise use any existing one. A non-nil interactive prefix argument
917 reverses the effect of `browse-url-new-window-p'.
919 When called non-interactively, optional second argument NEW-WINDOW is
920 used instead of `browse-url-new-window-p'."
921 (interactive (browse-url-interactive-arg "Lynx URL: "))
922 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
923 ;; (term-term-name "vt100") ; ??
924 (buf (get-buffer "*lynx*"))
925 (proc (and buf (get-buffer-process buf)))
926 (n browse-url-lynx-input-attempts))
927 (if (and new-buffer buf)
928 ;; Rename away the OLD buffer. This isn't very polite, but
929 ;; term insists on working in a buffer named *lynx* and would
930 ;; choke on *lynx*<1>
931 (progn (set-buffer buf)
936 (not (memq (process-status proc) '(run stop))))
941 `("lynx" "lynx" nil ,@browse-url-lynx-emacs-args ,url)))
942 (switch-to-buffer buf)
944 (set-process-sentinel
945 (get-buffer-process buf)
946 ;; Don't leave around a dead one (especially because of its
948 (lambda (process event)
949 (if (not (memq (process-status process) '(run stop)))
950 (let ((buf (process-buffer process)))
951 (if buf (kill-buffer buf)))))))
952 ;; send the url to lynx in the old buffer
953 (let ((win (get-buffer-window buf t)))
956 (switch-to-buffer buf)))
957 (if (eq (following-char) ?_)
958 (cond ((eq browse-url-lynx-input-field 'warn)
959 (error "Please move out of the input field first."))
960 ((eq browse-url-lynx-input-field 'avoid)
961 (while (and (eq (following-char) ?_) (> n 0))
962 (term-send-down) ; down arrow
963 (sit-for browse-url-lynx-input-delay))
964 (if (eq (following-char) ?_)
965 (error "Cannot move out of the input field, sorry.")))))
966 (term-send-string proc (concat "g" ; goto
967 "\C-u" ; kill default url
974 (defun browse-url-mmm (url &optional new-window)
975 "Ask the MMM WWW browser to load URL.
976 Default to the URL around or before point."
977 (interactive (browse-url-interactive-arg "MMM URL: "))
978 (message "Sending URL to MMM...")
980 (set-buffer (get-buffer-create " *Shell Command Output*"))
982 ;; mmm_remote just SEGVs if the file isn't there...
983 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
984 ;; location in v 0.4:
985 (file-exists-p (expand-file-name "~/.mmm/remote")))
986 (call-process "mmm_remote" nil 0 nil url)
987 (call-process "mmm" nil 0 nil "-external" url))
988 (message "Sending URL to MMM... done")))
993 (defun browse-url-mail (url &optional new-window)
994 "Open a new mail message buffer within Emacs.
995 Default to using the mailto: URL around or before point as the
996 recipient's address. Supplying a non-nil interactive prefix argument
997 will cause the mail to be composed in another window rather than the
1000 When called interactively, if variable `browse-url-new-window-p' is
1001 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1002 non-nil interactive prefix argument reverses the effect of
1003 `browse-url-new-window-p'.
1005 When called non-interactively, optional second argument NEW-WINDOW is
1006 used instead of `browse-url-new-window-p'."
1007 (interactive (browse-url-interactive-arg "Mailto URL: "))
1009 (let ((to (if (string-match "^mailto:" url)
1013 (compose-mail-other-window to nil nil nil
1014 (list 'insert-buffer (current-buffer)))
1015 (compose-mail to nil nil nil nil
1016 (list 'insert-buffer (current-buffer)))))))
1018 ;; --- Random browser ---
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))))
1035 (provide 'browse-url)
1037 ;;; browse-url.el ends here