1 ;;; browse-url.el --- Pass a URL to a WWW browser
3 ;; Copyright 1995, 1996, 1997, 1998 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
)
227 (defgroup browse-url nil
228 "Use a web browser to look at a URL."
229 :prefix
"browse-url-"
233 (defcustom browse-url-browser-function
235 "*Function to display the current buffer in a WWW browser.
236 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
237 `browse-url-of-file' commands.
239 If the value is not a function it should be a list of pairs
240 (REGEXP . FUNCTION). In this case the function called will be the one
241 associated with the first REGEXP which matches the current URL. The
242 function is passed the URL and any other args of `browse-url'. The last
243 regexp should probably be \".\" to specify a default browser."
245 (function-item :tag
"Emacs W3" :value browse-url-w3
)
246 (function-item :tag
"W3 in another Emacs via `gnudoit'"
247 :value browse-url-w3-gnudoit
)
248 (function-item :tag
"Netscape" :value browse-url-netscape
)
249 (function-item :tag
"Mosaic" :value browse-url-mosaic
)
250 (function-item :tag
"Mosaic using CCI" :value browse-url-cci
)
251 (function-item :tag
"IXI Mosaic" :value browse-url-iximosaic
)
252 (function-item :tag
"Lynx in an xterm window"
253 :value browse-url-lynx-xterm
)
254 (function-item :tag
"Lynx in an Emacs window"
255 :value browse-url-lynx-emacs
)
256 (function-item :tag
"Grail" :value browse-url-grail
)
257 (function-item :tag
"MMM" :value browse-url-mmm
)
258 (function-item :tag
"Specified by `Browse Url Generic Program'"
259 :value browse-url-generic
)
260 (function :tag
"Your own function"))
263 (defcustom browse-url-netscape-program
"netscape"
264 ;; Info about netscape-remote from Kurt Swanson in gnu.emacs.gnus
265 "The name by which to invoke Netscape.
267 It is said that source is available for a program `netscape-remote'
268 which starts up very much quicker than `netscape' and that it is
269 useful to set this variable to the name of a script which invokes that
272 /usr/local/bin/netscape-remote \"$@\" > /dev/null 2>&1
277 (defcustom browse-url-netscape-arguments nil
278 "A list of strings to pass to Netscape as arguments."
279 :type
'(repeat (string :tag
"Argument"))
282 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
283 "A list of strings to pass to Netscape when it starts up.
284 Defaults to the value of `browse-url-netscape-arguments' at the time
285 `browse-url' is loaded."
286 :type
'(repeat (string :tag
"Argument"))
290 (defcustom browse-url-new-window-p nil
291 "*If non-nil, always open a new browser window with appropriate browsers.
292 Passing an interactive argument to \\[browse-url], or specific browser
293 commands reverses the effect of this variable. Requires Netscape version
294 1.1N or later or XMosaic version 2.5 or later if using those browsers."
299 (defcustom browse-url-netscape-display nil
300 "*The X display for running Netscape, if not same as Emacs'."
301 :type
'(choice string
(const :tag
"Default" nil
))
304 (defcustom browse-url-mosaic-program
"xmosaic"
305 "The name by which to invoke Mosaic (or mMosaic)."
310 (defcustom browse-url-mosaic-arguments nil
311 "A list of strings to pass to Mosaic as arguments."
312 :type
'(repeat (string :tag
"Argument"))
315 (defcustom browse-url-filename-alist
316 '(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" .
"ftp://\\2/")
317 ;; The above loses the username to avoid the browser prompting for
318 ;; it in anonymous cases. If it's not anonymous the next regexp
320 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" .
"ftp://\\1\\2/")
322 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
323 Any substring of a filename matching one of the REGEXPs is replaced by
324 the corresponding STRING using `replace-match', not treating STRING
325 literally. All pairs are applied in the order given. The default
326 value converts ange-ftp/EFS-style paths into ftp URLs and prepends
327 `file:' to any path beginning with `/'.
329 For example, adding to the default a specific translation of an ange-ftp
330 address to an HTTP URL:
332 (setq browse-url-filename-alist
333 '((\"/webmaster@webserver:/home/www/html/\" .
334 \"http://www.acme.co.uk/\")
335 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
336 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
337 (\"^/+\" . \"file:/\")))
339 :type
'(repeat (cons :format
"%v"
340 (regexp :tag
"Regexp")
341 (string :tag
"Replacement")))
346 (defcustom browse-url-save-file nil
347 "*If non-nil, save the buffer before displaying its file.
348 Used by the `browse-url-of-file' command."
352 (defcustom browse-url-of-file-hook nil
353 "Run after `browse-url-of-file' has asked a browser to load a file.
355 Set this to `browse-url-netscape-reload' to force Netscape to load the
356 file rather than displaying a cached copy."
358 :options
'(browse-url-netscape-reload)
361 (defvar browse-url-usr1-signal
362 (if (and (boundp 'emacs-major-version
)
363 (or (> emacs-major-version
19) (>= emacs-minor-version
29)))
364 'SIGUSR1
; Why did I think this was in lower case before?
365 30) ; Check /usr/include/signal.h.
366 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
367 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
368 which is 30 on SunOS and 16 on HP-UX and Solaris.")
370 (defcustom browse-url-CCI-port
3003
371 "Port to access XMosaic via CCI.
372 This can be any number between 1024 and 65535 but must correspond to
373 the value set in the browser."
377 (defcustom browse-url-CCI-host
"localhost"
378 "*Host to access XMosaic via CCI.
379 This should be the host name of the machine running XMosaic with CCI
380 enabled. The port number should be set in `browse-url-CCI-port'."
384 (defvar browse-url-temp-file-name nil
)
385 (make-variable-buffer-local 'browse-url-temp-file-name
)
387 (defcustom browse-url-xterm-program
"xterm"
388 "The name of the terminal emulator used by `browse-url-lynx-xterm'.
389 This might, for instance, be a separate colour version of xterm."
393 (defcustom browse-url-xterm-args nil
394 "*A list of strings defining options for `browse-url-xterm-program'.
395 These might set its size, for instance."
396 :type
'(repeat (string :tag
"Argument"))
399 (defcustom browse-url-lynx-emacs-args
(and (not window-system
)
401 "A list of strings defining options for Lynx in an Emacs buffer.
403 The default is none in a window system, otherwise `-show_cursor' to
404 indicate the position of the current link in the absence of
405 highlighting, assuming the normal default for showing the cursor."
406 :type
'(repeat (string :tag
"Argument"))
410 (defcustom browse-url-gnudoit-program
"gnudoit"
411 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
415 (defcustom browse-url-gnudoit-args
'("-q")
416 "*A list of strings defining options for `browse-url-gnudoit-program'.
417 These might set the port, for instance."
418 :type
'(repeat (string :tag
"Argument"))
422 (defcustom browse-url-generic-program nil
423 "*The name of the browser program used by `browse-url-generic'."
424 :type
'(choice string
(const :tag
"None" nil
))
427 (defcustom browse-url-generic-args nil
428 "*A list of strings defining options for `browse-url-generic-program'."
429 :type
'(repeat (string :tag
"Argument"))
432 (defcustom browse-url-temp-dir temporary-file-directory
433 "The name of a directory for browse-url's temporary files.
434 Such files are generated by functions like `browse-url-of-region'.
435 You might want to set this to somewhere with restricted read permissions
440 (defcustom browse-url-netscape-version
442 "The version of Netscape you are using.
443 This affects how URL reloading is done; the mechanism changed
444 incompatibly at version 4."
448 (defcustom browse-url-lynx-input-field
'avoid
449 "*Action on selecting an existing Lynx buffer at an input field.
450 What to do when sending a new URL to an existing Lynx buffer in Emacs
451 if the Lynx cursor is on an input field (in which case the `g' command
452 would be entered as data). Such fields are recognized by the
453 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
454 user and don't emit the URL, 'avoid: try to avoid the field by moving
455 down (this *won't* always work)."
456 :type
'(choice (const :tag
"Move to try to avoid field" :value avoid
)
457 (const :tag
"Disregard" :value nil
)
458 (const :tag
"Warn, don't emit URL" :value warn
))
462 (defvar browse-url-lynx-input-attempts
10
463 "*How many times to try to move down from a series of lynx input fields.")
465 (defcustom browse-url-lynx-input-delay
0.2
466 "How many seconds to wait for lynx between moves down from an input field.")
468 (defvar browse-url-temp-file-list
'())
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
473 (defun browse-url-url-at-point ()
474 (let ((url (thing-at-point 'url
)))
475 (set-text-properties 0 (length url
) nil url
)
478 ;; Having this as a separate function called by the browser-specific
479 ;; functions allows them to be stand-alone commands, making it easier
480 ;; to switch between browsers.
482 (defun browse-url-interactive-arg (prompt)
483 "Read a URL from the minibuffer, prompting with PROMPT.
484 Default to the URL at or before point. If invoked with a mouse button,
485 set point to the position clicked first. Return a list for use in
486 `interactive' containing the URL and `browse-url-new-window-p' or its
487 negation if a prefix argument was given."
488 (let ((event (elt (this-command-keys) 0)))
489 (and (listp event
) (mouse-set-point event
)))
490 (list (read-string prompt
(browse-url-url-at-point))
491 (not (eq (null browse-url-new-window-p
)
492 (null current-prefix-arg
)))))
494 (defun browse-url-maybe-new-window (arg)
497 browse-url-new-window-p
))
499 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
500 ;; Browse current buffer
503 (defun browse-url-of-file (&optional file
)
504 "Ask a WWW browser to display FILE.
505 Display the current buffer's file if FILE is nil or if called
506 interactively. Turn the filename into a URL with function
507 `browse-url-file-url'. Pass the URL to a browser using the
508 `browse-url' function then run `browse-url-of-file-hook'."
511 (setq file
(buffer-file-name))
512 (error "Current buffer has no file"))
513 (let ((buf (get-file-buffer file
)))
517 (cond ((not (buffer-modified-p)))
518 (browse-url-save-file (save-buffer))
519 (t (message "%s modified since last save" file
))))))
520 (browse-url (browse-url-file-url file
))
521 (run-hooks 'browse-url-of-file-hook
))
523 (defun browse-url-file-url (file)
524 "Return the URL corresponding to FILE.
525 Use variable `browse-url-filename-alist' to map filenames to URLs."
526 ;; URL-encode special chars, do % first
528 (while (setq s
(string-match "%" file s
))
529 (setq file
(replace-match "%25" t t file
)
531 (while (string-match "[*\"()',=;? ]" file
)
532 (let ((enc (format "%%%x" (aref file
(match-beginning 0)))))
533 (setq file
(replace-match enc t t file
))))
534 (let ((maps browse-url-filename-alist
))
536 (let* ((map (car maps
))
538 (to-string (cdr map
)))
539 (setq maps
(cdr maps
))
540 (and (string-match from-re file
)
541 (setq file
(replace-match to-string t nil file
))))))
545 (defun browse-url-of-buffer (&optional buffer
)
546 "Ask a WWW browser to display BUFFER.
547 Display the current buffer if BUFFER is nil. Display only the
548 currently visible part of BUFFER (from a temporary file) if buffer is
552 (and buffer
(set-buffer buffer
))
554 ;; Ignore real name if restricted
555 (and (= (- (point-max) (point-min)) (buffer-size))
557 (and (boundp 'dired-directory
) dired-directory
)))))
560 (or browse-url-temp-file-name
561 (setq browse-url-temp-file-name
562 (convert-standard-filename
564 (expand-file-name "burl" browse-url-temp-dir
)))))
565 (setq file-name browse-url-temp-file-name
)
566 (write-region (point-min) (point-max) file-name nil
'no-message
)))
567 (browse-url-of-file file-name
))))
569 (defun browse-url-delete-temp-file (&optional temp-file-name
)
570 ;; Delete browse-url-temp-file-name from the file system and from
571 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
572 ;; non-nil, delete it instead, but only from the file system --
573 ;; browse-url-temp-file-list is not affected.
574 (let ((file-name (or temp-file-name browse-url-temp-file-name
)))
575 (if (and file-name
(file-exists-p file-name
))
577 (delete-file file-name
)
578 (if (null temp-file-name
)
579 (setq browse-url-temp-file-list
580 (delete browse-url-temp-file-name
581 browse-url-temp-file-list
)))))))
583 (defun browse-url-delete-temp-file-list ()
584 ;; Delete all elements of browse-url-temp-file-list.
585 (while browse-url-temp-file-list
586 (browse-url-delete-temp-file (car browse-url-temp-file-list
))
587 (setq browse-url-temp-file-list
588 (cdr browse-url-temp-file-list
))))
590 (add-hook 'kill-buffer-hook
'browse-url-delete-temp-file
)
591 (add-hook 'kill-emacs-hook
'browse-url-delete-temp-file-list
)
594 (defun browse-url-of-dired-file ()
595 "In Dired, ask a WWW browser to display the file named on this line."
597 (browse-url-of-file (dired-get-filename)))
600 (defun browse-url-of-region (min max
)
601 "Ask a WWW browser to display the current region."
605 (narrow-to-region min max
)
606 (browse-url-of-buffer))))
608 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
609 ;; Browser-independent commands
611 ;; A generic command to call the current browse-url-browser-function
614 (defun browse-url (url &rest args
)
615 "Ask a WWW browser to load URL.
616 Prompts for a URL, defaulting to the URL at or before point. Variable
617 `browse-url-browser-function' says which browser to use."
618 (interactive (browse-url-interactive-arg "URL: "))
619 (let ((bf browse-url-browser-function
) re
)
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"
627 (apply bf url args
)))
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."
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
)))
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
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
664 (defun browse-url-process-environment ()
665 "Set DISPLAY in the environment to the X display Netscape is running on.
666 This is either the value of variable `browse-url-netscape-display' if
667 non-nil, or the same display as Emacs if different from the current
668 environment, otherwise just use the current environment."
669 (let ((display (or browse-url-netscape-display
(browse-url-emacs-display))))
671 (cons (concat "DISPLAY=" display
) process-environment
)
672 process-environment
)))
674 (defun browse-url-emacs-display ()
675 "Return the X display Emacs is running on.
676 This is nil if the display is the same as the DISPLAY environment variable.
678 Actually Emacs could be using several displays; this just returns the
679 one showing the selected frame."
680 (let ((display (cdr-safe (assq 'display
(frame-parameters)))))
681 (and (not (equal display
(getenv "DISPLAY")))
685 (defun browse-url-netscape (url &optional new-window
)
686 "Ask the Netscape WWW browser to load URL.
688 Default to the URL around or before point. The strings in variable
689 `browse-url-netscape-arguments' are also passed to Netscape.
691 When called interactively, if variable `browse-url-new-window-p' is
692 non-nil, load the document in a new Netscape window, otherwise use a
693 random existing one. A non-nil interactive prefix argument reverses
694 the effect of `browse-url-new-window-p'.
696 When called non-interactively, optional second argument NEW-WINDOW is
697 used instead of `browse-url-new-window-p'."
698 (interactive (browse-url-interactive-arg "Netscape URL: "))
699 ;; URL encode any `confusing' characters in the URL. This needs to
700 ;; include at least commas; presumably also close parens.
701 (while (string-match "[,)]" url
)
702 (setq url
(replace-match
703 (format "%%%x" (string-to-char (match-string 0 url
))) t t url
)))
704 (let* ((process-environment (browse-url-process-environment))
705 (process (apply 'start-process
706 (concat "netscape " url
) nil
707 browse-url-netscape-program
708 (append browse-url-netscape-arguments
709 (if (eq window-system
'w32
)
711 (if new-window
'("-noraise"))
713 (concat "openURL(" url
714 (if (browse-url-maybe-new-window
718 (set-process-sentinel process
719 (list 'lambda
'(process change
)
720 (list 'browse-url-netscape-sentinel
'process url
)))))
722 (defun browse-url-netscape-sentinel (process url
)
723 "Handle a change to the process communicating with Netscape."
724 (or (eq (process-exit-status process
) 0)
725 (let* ((process-environment (browse-url-process-environment)))
726 ;; Netscape not running - start it
727 (message "Starting Netscape...")
728 (apply 'start-process
(concat "netscape" url
) nil
729 browse-url-netscape-program
730 (append browse-url-netscape-startup-arguments
(list url
))))))
732 (defun browse-url-netscape-reload ()
733 "Ask Netscape to reload its current document.
734 How depends on `browse-url-netscape-version'."
736 ;; Backwards incompatibility reported by
737 ;; <peter.kruse@psychologie.uni-regensburg.de>.
738 (browse-url-netscape-send (if (>= browse-url-netscape-version
4)
739 "xfeDoCommand(reload)"
742 (defun browse-url-netscape-send (command)
743 "Send a remote control command to Netscape."
744 (let* ((process-environment (browse-url-process-environment)))
745 (apply 'start-process
"netscape" nil
746 browse-url-netscape-program
747 (append browse-url-netscape-arguments
748 (list "-remote" command
)))))
753 (defun browse-url-mosaic (url &optional new-window
)
754 ;; new-window ignored
755 "Ask the XMosaic WWW browser to load URL.
757 Default to the URL around or before point. The strings in variable
758 `browse-url-mosaic-arguments' are also passed to Mosaic and the
759 program is invoked according to the variable
760 `browse-url-mosaic-program'.
762 When called interactively, if variable `browse-url-new-window-p' is
763 non-nil, load the document in a new Mosaic window, otherwise use a
764 random existing one. A non-nil interactive prefix argument reverses
765 the effect of `browse-url-new-window-p'.
767 When called non-interactively, optional second argument NEW-WINDOW is
768 used instead of `browse-url-new-window-p'."
769 (interactive (browse-url-interactive-arg "Mosaic URL: "))
770 (let ((pidfile (expand-file-name "~/.mosaicpid"))
772 (if (file-readable-p pidfile
)
775 (goto-char (point-min))
776 (setq pid
(read (current-buffer)))
778 (if (and pid
(zerop (signal-process pid
0))) ; Mosaic running
780 (find-file (format "/tmp/Mosaic.%d" pid
))
782 (insert (if (browse-url-maybe-new-window new-window
)
788 ;; Send signal SIGUSR to Mosaic
789 (message "Signalling Mosaic...")
790 (signal-process pid browse-url-usr1-signal
)
792 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
793 (message "Signalling Mosaic...done")
795 ;; Mosaic not running - start it
796 (message "Starting Mosaic...")
797 (apply 'start-process
"xmosaic" nil browse-url-mosaic-program
798 (append browse-url-mosaic-arguments
(list url
)))
799 (message "Starting Mosaic...done"))))
804 (defvar browse-url-grail
805 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
806 "Location of Grail remote control client script `rcgrail.py'.
807 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
810 (defun browse-url-grail (url &optional new-window
)
811 "Ask the Grail WWW browser to load URL.
812 Default to the URL around or before point. Runs the program in the
813 variable `browse-url-grail'."
814 (interactive (browse-url-interactive-arg "Grail URL: "))
815 (message "Sending URL to Grail...")
817 (set-buffer (get-buffer-create " *Shell Command Output*"))
819 ;; don't worry about this failing.
821 (call-process browse-url-grail nil
0 nil
"-b" url
)
822 (call-process browse-url-grail nil
0 nil url
))
823 (message "Sending URL to Grail... done")))
825 ;; --- Mosaic using CCI ---
828 (defun browse-url-cci (url &optional new-window
)
829 "Ask the XMosaic WWW browser to load URL.
830 Default to the URL around or before point.
832 This function only works for XMosaic version 2.5 or later. You must
833 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
834 value of variable `browse-url-CCI-port', and enable `Accept requests'.
836 When called interactively, if variable `browse-url-new-window-p' is
837 non-nil, load the document in a new browser window, otherwise use a
838 random existing one. A non-nil interactive prefix argument reverses
839 the effect of `browse-url-new-window-p'.
841 When called non-interactively, optional second argument NEW-WINDOW is
842 used instead of `browse-url-new-window-p'."
843 (interactive (browse-url-interactive-arg "Mosaic URL: "))
844 (open-network-stream "browse-url" " *browse-url*"
845 browse-url-CCI-host browse-url-CCI-port
)
846 ;; Todo: start browser if fails
847 (process-send-string "browse-url"
848 (concat "get url (" url
") output "
849 (if (browse-url-maybe-new-window
854 (process-send-string "browse-url" "disconnect\r\n")
855 (delete-process "browse-url"))
857 ;; --- IXI Mosaic ---
860 (defun browse-url-iximosaic (url &optional new-window
)
861 ;; new-window ignored
862 "Ask the IXIMosaic WWW browser to load URL.
863 Default to the URL around or before point."
864 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
865 (start-process "tellw3b" nil
"tellw3b"
866 "-service WWW_BROWSER ixi_showurl " url
))
871 (defun browse-url-w3 (url &optional new-window
)
872 "Ask the w3 WWW browser to load URL.
873 Default to the URL around or before point.
875 When called interactively, if variable `browse-url-new-window-p' is
876 non-nil, load the document in a new window. A non-nil interactive
877 prefix argument reverses the effect of `browse-url-new-window-p'.
879 When called non-interactively, optional second argument NEW-WINDOW is
880 used instead of `browse-url-new-window-p'."
881 (interactive (browse-url-interactive-arg "W3 URL: "))
882 (if (browse-url-maybe-new-window new-window
)
883 (w3-fetch-other-window)
887 (defun browse-url-w3-gnudoit (url &optional new-window
)
888 ;; new-window ignored
889 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
890 The `browse-url-gnudoit-program' program is used with options given by
891 `browse-url-gnudoit-args'. Default to the URL around or before point."
892 (interactive (browse-url-interactive-arg "W3 URL: "))
893 (apply 'start-process
(concat "gnudoit:" url
) nil
894 browse-url-gnudoit-program
895 (append browse-url-gnudoit-args
(list (concat "(w3-fetch \"" url
"\")") "(raise-frame)"))))
897 ;; --- Lynx in an xterm ---
900 (defun browse-url-lynx-xterm (url &optional new-window
)
901 ;; new-window ignored
902 "Ask the Lynx WWW browser to load URL.
903 Default to the URL around or before point. A new Lynx process is run
904 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
905 with possible additional arguments `browse-url-xterm-args'."
906 (interactive (browse-url-interactive-arg "Lynx URL: "))
907 (apply #'start-process
`(,(concat "lynx" url
) nil
,browse-url-xterm-program
908 ,@browse-url-xterm-args
"-e" "lynx" ,url
)))
910 ;; --- Lynx in an Emacs "term" window ---
913 (defun browse-url-lynx-emacs (url &optional new-buffer
)
914 "Ask the Lynx WWW browser to load URL.
915 Default to the URL around or before point. With a prefix argument, run
916 a new Lynx process in a new buffer.
918 When called interactively, if variable `browse-url-new-window-p' is
919 non-nil, load the document in a new lynx in a new term window,
920 otherwise use any existing one. A non-nil interactive prefix argument
921 reverses the effect of `browse-url-new-window-p'.
923 When called non-interactively, optional second argument NEW-WINDOW is
924 used instead of `browse-url-new-window-p'."
925 (interactive (browse-url-interactive-arg "Lynx URL: "))
926 (let* ((system-uses-terminfo t
) ; Lynx uses terminfo
927 ;; (term-term-name "vt100") ; ??
928 (buf (get-buffer "*lynx*"))
929 (proc (and buf
(get-buffer-process buf
)))
930 (n browse-url-lynx-input-attempts
))
931 (if (and (browse-url-maybe-new-window new-buffer
) buf
)
932 ;; Rename away the OLD buffer. This isn't very polite, but
933 ;; term insists on working in a buffer named *lynx* and would
934 ;; choke on *lynx*<1>
935 (progn (set-buffer buf
)
937 (if (or (browse-url-maybe-new-window new-buffer
)
940 (not (memq (process-status proc
) '(run stop
))))
945 `("lynx" "lynx" nil
,@browse-url-lynx-emacs-args
,url
)))
946 (switch-to-buffer buf
)
948 (set-process-sentinel
949 (get-buffer-process buf
)
950 ;; Don't leave around a dead one (especially because of its
952 (lambda (process event
)
953 (if (not (memq (process-status process
) '(run stop
)))
954 (let ((buf (process-buffer process
)))
955 (if buf
(kill-buffer buf
)))))))
956 ;; send the url to lynx in the old buffer
957 (let ((win (get-buffer-window buf t
)))
960 (switch-to-buffer buf
)))
961 (if (eq (following-char) ?_
)
962 (cond ((eq browse-url-lynx-input-field
'warn
)
963 (error "Please move out of the input field first."))
964 ((eq browse-url-lynx-input-field
'avoid
)
965 (while (and (eq (following-char) ?_
) (> n
0))
966 (term-send-down) ; down arrow
967 (sit-for browse-url-lynx-input-delay
))
968 (if (eq (following-char) ?_
)
969 (error "Cannot move out of the input field, sorry.")))))
970 (term-send-string proc
(concat "g" ; goto
971 "\C-u" ; kill default url
978 (defun browse-url-mmm (url &optional new-window
)
979 "Ask the MMM WWW browser to load URL.
980 Default to the URL around or before point."
981 (interactive (browse-url-interactive-arg "MMM URL: "))
982 (message "Sending URL to MMM...")
984 (set-buffer (get-buffer-create " *Shell Command Output*"))
986 ;; mmm_remote just SEGVs if the file isn't there...
987 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
988 ;; location in v 0.4:
989 (file-exists-p (expand-file-name "~/.mmm/remote")))
990 (call-process "mmm_remote" nil
0 nil url
)
991 (call-process "mmm" nil
0 nil
"-external" url
))
992 (message "Sending URL to MMM... done")))
997 (defun browse-url-mail (url &optional new-window
)
998 "Open a new mail message buffer within Emacs.
999 Default to using the mailto: URL around or before point as the
1000 recipient's address. Supplying a non-nil interactive prefix argument
1001 will cause the mail to be composed in another window rather than the
1004 When called interactively, if variable `browse-url-new-window-p' is
1005 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1006 non-nil interactive prefix argument reverses the effect of
1007 `browse-url-new-window-p'.
1009 When called non-interactively, optional second argument NEW-WINDOW is
1010 used instead of `browse-url-new-window-p'."
1011 (interactive (browse-url-interactive-arg "Mailto URL: "))
1013 (let ((func (if (browse-url-maybe-new-window new-window
)
1014 'compose-mail-other-window
1016 (to (if (string-match "^mailto:" url
)
1020 (list to nil nil nil nil nil
(cons 'insert-buffer
1021 (current-buffer)))))))
1023 ;; --- Random browser ---
1026 (defun browse-url-generic (url &optional new-window
)
1027 ;; new-window ignored
1028 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1029 Default to the URL around or before point. A fresh copy of the
1030 browser is started up in a new process with possible additional arguments
1031 `browse-url-generic-args'. This is appropriate for browsers which
1032 don't offer a form of remote control."
1033 (interactive (browse-url-interactive-arg "URL: "))
1034 (if (not browse-url-generic-program
)
1035 (error "No browser defined (`browse-url-generic-program')"))
1036 (apply 'start-process
(concat browse-url-generic-program url
) nil
1037 browse-url-generic-program
1038 (append browse-url-generic-args
(list url
))))
1040 (provide 'browse-url
)
1042 ;;; browse-url.el ends here