(browse-url-of-region): Use its args. From <mit@nines.nec.co.jp>
[emacs.git] / lisp / browse-url.el
blobe719ecab3d2a5ebd1c96f01b467980c354207547
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)
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 ;; Note that versions of Netscape before 1.1b1 did not have remote
50 ;; control. <URL:http://www.netscape.com/newsref/std/x-remote.html>.
52 ;; Netscape can cache Web pages so it may be necessary to tell it to
53 ;; reload the current page if it has changed (e.g. if you have edited
54 ;; it). There is currently no perfect automatic solution to this.
56 ;; Netscape allows you to specify the id of the window you want to
57 ;; control but which window DO you want to control and how do you
58 ;; discover its id?
60 ;; If using XMosaic before version 2.5, check the definition of
61 ;; browse-url-usr1-signal below.
62 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/remote-control.html>
64 ;; XMosaic version 2.5 introduced Common Client Interface allowing you
65 ;; to control mosaic through Unix sockets.
66 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/CCI/cci-spec.html>
68 ;; William M. Perry's excellent "w3" WWW browser for
69 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
70 ;; has a function w3-follow-url-at-point, but that
71 ;; doesn't let you edit the URL like browse-url.
72 ;; The `gnuserv' package that can be used to control it in another
73 ;; Emacs process is available from
74 ;; <URL:ftp://ftp.splode.com/pub/users/friedman/packages/>.
76 ;; Grail is the freely available WWW browser implemented in Python, a
77 ;; cool object-oriented freely available interpreted language. Grail
78 ;; 0.3b1 was the first version to have remote control as distributed.
79 ;; For more information on Grail see
80 ;; <URL:http://grail.cnri.reston.va.us/> and for more information on
81 ;; Python see <url:http://www.python.org/>. Grail support in
82 ;; browse-url.el written by Barry Warsaw <bwarsaw@python.org>.
84 ;; MMM is the freely available WWW browser implemented in Objective
85 ;; Caml, a cool impure functional programming language, by Francois
86 ;; Rouaix. See the MMM home page
87 ;; <URL:http://pauillac.inria.fr/%7Erouaix/mmm/>.
89 ;; Lynx is now distributed by the FSF. See also
90 ;; <URL:http://lynx.browser.org/>.
92 ;; Free graphical browsers that could be used by `browse-url-generic'
93 ;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
94 ;; <URL:http://www.unlv.edu/chimera/>, Arena
95 ;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena> and Amaya
96 ;; <URL:ftp://ftp.w3.org/pub/amaya>. mMosaic
97 ;; <URL:ftp://sig.enst.fr/pub/multicast/mMosaic/> (with development
98 ;; support for Java applets and multicast) can be used like Mosaic by
99 ;; setting `browse-url-mosaic-program' appropriately.
101 ;; I [Denis Howe] recommend Nelson Minar <nelson@santafe.edu>'s excellent
102 ;; html-helper-mode.el for editing HTML and thank Nelson for
103 ;; his many useful comments on this code.
104 ;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>
106 ;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
107 ;; software/own/hm--html-menus/>. For composing correct HTML see also
108 ;; PSGML the general SGML structure editor package
109 ;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
110 ;; with this.
112 ;; This package generalises function html-previewer-process in Marc
113 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
114 ;; ffap.el package. The huge hyperbole package also contains similar
115 ;; functions.
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;; Help!
120 ;; Can you write and test some code for the Macintrash and Windoze
121 ;; Netscape remote control APIs? (See the URL above).
123 ;; Do any other browsers have remote control?
125 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 ;; Usage
128 ;; To display the URL at or before point:
129 ;; M-x browse-url-at-point RET
130 ;; or, similarly but with the opportunity to edit the URL extracted from
131 ;; the buffer, use:
132 ;; M-x browse-url
134 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
135 ;; file:
136 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
137 ;; (Note that using Shift-mouse-1 is not desirable because
138 ;; that event has a standard meaning in Emacs.)
140 ;; To display the current buffer in a web browser:
141 ;; M-x browse-url-of-buffer RET
143 ;; To display the current region in a web browser:
144 ;; M-x browse-url-of-region RET
146 ;; In Dired, to display the file named on the current line:
147 ;; M-x browse-url-of-dired-file RET
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;; Customisation (~/.emacs)
152 ;; To see what variables are available for customization, type
153 ;; `M-x set-variable browse-url TAB'. Better, use
154 ;; `M-x customize-group browse-url'.
156 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
157 ;; (as used by html-helper-mode):
158 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
159 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
160 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
161 ;; (global-set-key "\C-c\C-zu" 'browse-url)
162 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
163 ;; (add-hook 'dired-mode-hook
164 ;; (lambda ()
165 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
167 ;; Browse URLs in mail messages by clicking mouse-2:
168 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
169 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
171 ;; Browse URLs in Usenet messages by clicking mouse-2:
172 ;; (eval-after-load "gnus"
173 ;; '(define-key gnus-article-mode-map [mouse-2] 'browse-url-at-mouse))
174 ;; [The current version of Gnus provides a standard feature to
175 ;; activate URLs in article buffers for invocation of browse-url with
176 ;; mouse-2.]
178 ;; Use the Emacs w3 browser when not running under X11:
179 ;; (or (eq window-system 'x)
180 ;; (setq browse-url-browser-function 'browse-url-w3))
182 ;; To always save modified buffers before displaying the file in a browser:
183 ;; (setq browse-url-save-file t)
185 ;; To get round the Netscape caching problem, you could EITHER have
186 ;; write-file in html-helper-mode make Netscape reload the document:
188 ;; (autoload 'browse-url-netscape-reload "browse-url"
189 ;; "Ask a WWW browser to redisplay the current file." t)
190 ;; (add-hook 'html-helper-mode-hook
191 ;; (lambda ()
192 ;; (add-hook 'local-write-file-hooks
193 ;; (lambda ()
194 ;; (let ((local-write-file-hooks))
195 ;; (save-buffer))
196 ;; (browse-url-netscape-reload)
197 ;; t) ; => file written by hook
198 ;; t))) ; append to l-w-f-hooks
200 ;; OR have browse-url-of-file ask Netscape to load and then reload the
201 ;; file:
203 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
205 ;; You may also want to customise browse-url-netscape-arguments, e.g.
206 ;; (setq browse-url-netscape-arguments '("-install"))
208 ;; or similarly for the other browsers.
210 ;; To invoke different browsers for different URLs:
211 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
212 ;; ("." . browse-url-netscape)))
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 ;;; Code:
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 ;; Variables
220 (eval-when-compile (require 'dired)
221 (require 'thingatpt)
222 (require 'term))
224 (defgroup browse-url nil
225 "Use a web browser to look at a URL."
226 :prefix "browse-url-"
227 :group 'hypermedia)
229 ;;;###autoload
230 (defcustom browse-url-browser-function
231 'browse-url-netscape
232 "*Function to display the current buffer in a WWW browser.
233 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
234 `browse-url-of-file' commands.
236 If the value is not a function it should be a list of pairs
237 (REGEXP . FUNCTION). In this case the function called will be the one
238 associated with the first REGEXP which matches the current URL. The
239 function is passed the URL and any other args of `browse-url'. The last
240 regexp should probably be \".\" to specify a default browser."
241 :type '(choice
242 (function-item :tag "Emacs W3" :value browse-url-w3)
243 (function-item :tag "W3 in another Emacs via `gnudoit'"
244 :value browse-url-w3-gnudoit)
245 (function-item :tag "Netscape" :value browse-url-netscape)
246 (function-item :tag "Mosaic" :value browse-url-mosaic)
247 (function-item :tag "Mosaic using CCI" :value browse-url-cci)
248 (function-item :tag "IXI Mosaic" :value browse-url-iximosaic)
249 (function-item :tag "Lynx in an xterm window"
250 :value browse-url-lynx-xterm)
251 (function-item :tag "Lynx in an Emacs window"
252 :value browse-url-lynx-emacs)
253 (function-item :tag "Grail" :value browse-url-grail)
254 (function-item :tag "MMM" :value browse-url-mmm)
255 (function-item :tag "Specified by `Browse Url Generic Program'"
256 :value browse-url-generic)
257 (function :tag "Your own function"))
258 :group 'browse-url)
260 (defcustom browse-url-netscape-program "netscape"
261 ;; Info about netscape-remote from Kurt Swanson in gnu.emacs.gnus
262 "The name by which to invoke Netscape.
264 It is said that source is available for a program `netscape-remote'
265 which starts up very much quicker than `netscape' and that it is
266 useful to set this variable to the name of a script which invokes that
267 program like:
268 #!/bin/sh
269 /usr/local/bin/netscape-remote \"$@\" > /dev/null 2>&1
271 :type 'string
272 :group 'browse-url)
274 (defcustom browse-url-netscape-arguments nil
275 "A list of strings to pass to Netscape as arguments."
276 :type '(repeat (string :tag "Argument"))
277 :group 'browse-url)
279 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
280 "A list of strings to pass to Netscape when it starts up.
281 Defaults to the value of `browse-url-netscape-arguments' at the time
282 `browse-url' is loaded."
283 :type '(repeat (string :tag "Argument"))
284 :group 'browse-url)
286 ;;;###autoload
287 (defcustom browse-url-new-window-p nil
288 "*If non-nil, always open a new browser window with appropriate browsers.
289 Passing an interactive argument to \\[browse-url], or specific browser
290 commands reverses the effect of this variable. Requires Netscape version
291 1.1N or later or XMosaic version 2.5 or later if using those browsers."
292 :type 'boolean
293 :group 'browse-url)
295 ;;;###autoload
296 (defcustom browse-url-netscape-display nil
297 "*The X display for running Netscape, if not same as Emacs'."
298 :type '(choice string (const :tag "Default" nil))
299 :group 'browse-url)
301 (defcustom browse-url-mosaic-program "xmosaic"
302 "The name by which to invoke Mosaic (or mMosaic)."
303 :type 'string
304 :version "20.3"
305 :group 'browse-url)
307 (defcustom browse-url-mosaic-arguments nil
308 "A list of strings to pass to Mosaic as arguments."
309 :type '(repeat (string :tag "Argument"))
310 :group 'browse-url)
312 (defcustom browse-url-filename-alist
313 '(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
314 ;; The above loses the username to avoid the browser prompting for
315 ;; it in anonymous cases. If it's not anonymous the next regexp
316 ;; applies.
317 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
318 ("^/+" . "file:/"))
319 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
320 Any substring of a filename matching one of the REGEXPs is replaced by
321 the corresponding STRING using `replace-match', not treating STRING
322 literally. All pairs are applied in the order given. The default
323 value converts ange-ftp/EFS-style paths into ftp URLs and prepends
324 `file:' to any path beginning with `/'.
326 For example, adding to the default a specific translation of an ange-ftp
327 address to an HTTP URL:
329 (setq browse-url-filename-alist
330 '((\"/webmaster@webserver:/home/www/html/\" .
331 \"http://www.acme.co.uk/\")
332 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
333 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
334 (\"^/+\" . \"file:/\")))
336 :type '(repeat (cons :format "%v"
337 (regexp :tag "Regexp")
338 (string :tag "Replacement")))
339 :version "20.3"
340 :group 'browse-url)
342 ;;;###autoload
343 (defcustom browse-url-save-file nil
344 "*If non-nil, save the buffer before displaying its file.
345 Used by the `browse-url-of-file' command."
346 :type 'boolean
347 :group 'browse-url)
349 (defcustom browse-url-of-file-hook nil
350 "Run after `browse-url-of-file' has asked a browser to load a file.
352 Set this to `browse-url-netscape-reload' to force Netscape to load the
353 file rather than displaying a cached copy."
354 :type 'hook
355 :options '(browse-url-netscape-reload)
356 :group 'browse-url)
358 (defvar browse-url-usr1-signal
359 (if (and (boundp 'emacs-major-version)
360 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
361 'SIGUSR1 ; Why did I think this was in lower case before?
362 30) ; Check /usr/include/signal.h.
363 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
364 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
365 which is 30 on SunOS and 16 on HP-UX and Solaris.")
367 (defcustom browse-url-CCI-port 3003
368 "Port to access XMosaic via CCI.
369 This can be any number between 1024 and 65535 but must correspond to
370 the value set in the browser."
371 :type 'integer
372 :group 'browse-url)
374 (defcustom browse-url-CCI-host "localhost"
375 "*Host to access XMosaic via CCI.
376 This should be the host name of the machine running XMosaic with CCI
377 enabled. The port number should be set in `browse-url-CCI-port'."
378 :type 'string
379 :group 'browse-url)
381 (defvar browse-url-temp-file-name nil)
382 (make-variable-buffer-local 'browse-url-temp-file-name)
384 (defcustom browse-url-xterm-program "xterm"
385 "The name of the terminal emulator used by `browse-url-lynx-xterm'.
386 This might, for instance, be a separate colour version of xterm."
387 :type 'string
388 :group 'browse-url)
390 (defcustom browse-url-xterm-args nil
391 "*A list of strings defining options for `browse-url-xterm-program'.
392 These might set its size, for instance."
393 :type '(repeat (string :tag "Argument"))
394 :group 'browse-url)
396 (defcustom browse-url-lynx-emacs-args (and (not window-system)
397 '("-show_cursor"))
398 "A list of strings defining options for Lynx in an Emacs buffer.
400 The default is none in a window system, otherwise `-show_cursor' to
401 indicate the position of the current link in the absence of
402 highlighting, assuming the normal default for showing the cursor."
403 :type '(repeat (string :tag "Argument"))
404 :version "20.3"
405 :group 'browse-url)
407 (defcustom browse-url-gnudoit-program "gnudoit"
408 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
409 :type 'string
410 :group 'browse-url)
412 (defcustom browse-url-gnudoit-args '("-q")
413 "*A list of strings defining options for `browse-url-gnudoit-program'.
414 These might set the port, for instance."
415 :type '(repeat (string :tag "Argument"))
416 :group 'browse-url)
418 ;;;###autoload
419 (defcustom browse-url-generic-program nil
420 "*The name of the browser program used by `browse-url-generic'."
421 :type '(choice string (const :tag "None" nil))
422 :group 'browse-url)
424 (defcustom browse-url-generic-args nil
425 "*A list of strings defining options for `browse-url-generic-program'."
426 :type '(repeat (string :tag "Argument"))
427 :group 'browse-url)
429 (defcustom browse-url-temp-dir
430 (or (getenv "TMPDIR") "/tmp")
431 "The name of a directory for browse-url's temporary files.
432 Such files are generated by functions like `browse-url-of-region'.
433 You might want to set this to somewhere with restricted read permissions
434 for privacy's sake."
435 :type 'string
436 :group 'browse-url)
438 (defcustom browse-url-netscape-version
440 "The version of Netscape you are using.
441 This affects how URL reloading is done; the mechanism changed
442 incompatibly at version 4."
443 :type 'number
444 :group 'browse-url)
446 (defcustom browse-url-lynx-input-field 'avoid
447 "*Action on selecting an existing Lynx buffer at an input field.
448 What to do when sending a new URL to an existing Lynx buffer in Emacs
449 if the Lynx cursor is on an input field (in which case the `g' command
450 would be entered as data). Such fields are recognized by the
451 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
452 user and don't emit the URL, 'avoid: try to avoid the field by moving
453 down (this *won't* always work)."
454 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
455 (const :tag "Disregard" :value nil)
456 (const :tag "Warn, don't emit URL" :value warn))
457 :version "20.3"
458 :group 'browse-url)
460 (defcustom browse-url-lynx-input-attempts 10
461 "*How many times to try to move down from a series of lynx input fields."
462 :version "20.3"
463 :type 'integer
464 :group 'browse-url)
466 (defcustom browse-url-lynx-input-delay 0.2
467 "How many seconds to wait for lynx between moves down from an input field."
468 :version "20.3"
469 :type 'number
470 :group 'browse-url)
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 (defun browse-url-maybe-new-window (arg)
499 (if (interactive-p)
501 browse-url-new-window-p))
503 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
504 ;; Browse current buffer
506 ;;;###autoload
507 (defun browse-url-of-file (&optional file)
508 "Ask a WWW browser to display FILE.
509 Display the current buffer's file if FILE is nil or if called
510 interactively. Turn the filename into a URL with function
511 `browse-url-file-url'. Pass the URL to a browser using the
512 `browse-url' function then run `browse-url-of-file-hook'."
513 (interactive)
514 (or file
515 (setq file (buffer-file-name))
516 (error "Current buffer has no file"))
517 (let ((buf (get-file-buffer file)))
518 (if buf
519 (save-excursion
520 (set-buffer buf)
521 (cond ((not (buffer-modified-p)))
522 (browse-url-save-file (save-buffer))
523 (t (message "%s modified since last save" file))))))
524 (browse-url (browse-url-file-url file))
525 (run-hooks 'browse-url-of-file-hook))
527 (defun browse-url-file-url (file)
528 "Return the URL corresponding to FILE.
529 Use variable `browse-url-filename-alist' to map filenames to URLs."
530 ;; URL-encode special chars, do % first
531 (let ((s 0))
532 (while (setq s (string-match "%" file s))
533 (setq file (replace-match "%25" t t file)
534 s (1+ s))))
535 (while (string-match "[*\"()',=;? ]" file)
536 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
537 (setq file (replace-match enc t t file))))
538 (let ((maps browse-url-filename-alist))
539 (while maps
540 (let* ((map (car maps))
541 (from-re (car map))
542 (to-string (cdr map)))
543 (setq maps (cdr maps))
544 (and (string-match from-re file)
545 (setq file (replace-match to-string t nil file))))))
546 file)
548 ;;;###autoload
549 (defun browse-url-of-buffer (&optional buffer)
550 "Ask a WWW browser to display BUFFER.
551 Display the current buffer if BUFFER is nil. Display only the
552 currently visible part of BUFFER (from a temporary file) if buffer is
553 narrowed."
554 (interactive)
555 (save-excursion
556 (and buffer (set-buffer buffer))
557 (let ((file-name
558 ;; Ignore real name if restricted
559 (and (= (- (point-max) (point-min)) (buffer-size))
560 (or buffer-file-name
561 (and (boundp 'dired-directory) dired-directory)))))
562 (or file-name
563 (progn
564 (or browse-url-temp-file-name
565 (setq browse-url-temp-file-name
566 (convert-standard-filename
567 (make-temp-name
568 (expand-file-name "burl" browse-url-temp-dir)))))
569 (setq file-name browse-url-temp-file-name)
570 (write-region (point-min) (point-max) file-name nil 'no-message)))
571 (browse-url-of-file file-name))))
573 (defun browse-url-delete-temp-file (&optional temp-file-name)
574 ;; Delete browse-url-temp-file-name from the file system and from
575 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
576 ;; non-nil, delete it instead, but only from the file system --
577 ;; browse-url-temp-file-list is not affected.
578 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
579 (if (and file-name (file-exists-p file-name))
580 (progn
581 (delete-file file-name)
582 (if (null temp-file-name)
583 (setq browse-url-temp-file-list
584 (delete browse-url-temp-file-name
585 browse-url-temp-file-list)))))))
587 (defun browse-url-delete-temp-file-list ()
588 ;; Delete all elements of browse-url-temp-file-list.
589 (while browse-url-temp-file-list
590 (browse-url-delete-temp-file (car browse-url-temp-file-list))
591 (setq browse-url-temp-file-list
592 (cdr browse-url-temp-file-list))))
594 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
595 (add-hook 'kill-emacs-hook 'browse-url-delete-temp-file-list)
597 ;;;###autoload
598 (defun browse-url-of-dired-file ()
599 "In Dired, ask a WWW browser to display the file named on this line."
600 (interactive)
601 (browse-url-of-file (dired-get-filename)))
603 ;;;###autoload
604 (defun browse-url-of-region (min max)
605 "Ask a WWW browser to display the current region."
606 (interactive "r")
607 (save-excursion
608 (save-restriction
609 (narrow-to-region min max)
610 (browse-url-of-buffer))))
612 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
613 ;; Browser-independent commands
615 ;; A generic command to call the current browse-url-browser-function
617 ;;;###autoload
618 (defun browse-url (url &rest args)
619 "Ask a WWW browser to load URL.
620 Prompts for a URL, defaulting to the URL at or before point. Variable
621 `browse-url-browser-function' says which browser to use."
622 (interactive (browse-url-interactive-arg "URL: "))
623 (let ((bf browse-url-browser-function) re)
624 (while (consp bf)
625 (setq re (car (car bf))
626 bf (if (string-match re url)
627 (cdr (car bf)) ; The function
628 (cdr bf)))) ; More pairs
629 (or bf (error "No browser in browse-url-browser-function matching URL %s"
630 url))
631 (apply bf url args)))
633 ;;;###autoload
634 (defun browse-url-at-point ()
635 "Ask a WWW browser to load the URL at or before point.
636 Doesn't let you edit the URL like `browse-url'. Variable
637 `browse-url-browser-function' says which browser to use."
638 (interactive)
639 (browse-url (browse-url-url-at-point)))
641 (defun browse-url-event-buffer (event)
642 (window-buffer (posn-window (event-start event))))
644 (defun browse-url-event-point (event)
645 (posn-point (event-start event)))
647 ;;;###autoload
648 (defun browse-url-at-mouse (event)
649 "Ask a WWW browser to load a URL clicked with the mouse.
650 The URL is the one around or before the position of the mouse click
651 but point is not changed. Doesn't let you edit the URL like
652 `browse-url'. Variable `browse-url-browser-function' says which browser
653 to use."
654 (interactive "e")
655 (save-excursion
656 (set-buffer (browse-url-event-buffer event))
657 (goto-char (browse-url-event-point event))
658 (let ((url (browse-url-url-at-point)))
659 (if (string-equal url "")
660 (error "No URL found"))
661 (browse-url url browse-url-new-window-p))))
663 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
664 ;; Browser-specific commands
666 ;; --- Netscape ---
668 (defun browse-url-process-environment ()
669 "Set DISPLAY in the environment to the X display Netscape is running on.
670 This is either the value of variable `browse-url-netscape-display' if
671 non-nil, or the same display as Emacs if different from the current
672 environment, otherwise just use the current environment."
673 (let ((display (or browse-url-netscape-display (browse-url-emacs-display))))
674 (if display
675 (cons (concat "DISPLAY=" display) process-environment)
676 process-environment)))
678 (defun browse-url-emacs-display ()
679 "Return the X display Emacs is running on.
680 This is nil if the display is the same as the DISPLAY environment variable.
682 Actually Emacs could be using several displays; this just returns the
683 one showing the selected frame."
684 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
685 (and (not (equal display (getenv "DISPLAY")))
686 display)))
688 ;;;###autoload
689 (defun browse-url-netscape (url &optional new-window)
690 "Ask the Netscape WWW browser to load URL.
692 Default to the URL around or before point. The strings in variable
693 `browse-url-netscape-arguments' are also passed to Netscape.
695 When called interactively, if variable `browse-url-new-window-p' is
696 non-nil, load the document in a new Netscape window, otherwise use a
697 random existing one. A non-nil interactive prefix argument reverses
698 the effect of `browse-url-new-window-p'.
700 When called non-interactively, optional second argument NEW-WINDOW is
701 used instead of `browse-url-new-window-p'."
702 (interactive (browse-url-interactive-arg "Netscape URL: "))
703 ;; URL encode any commas in the URL
704 (while (string-match "," url)
705 (setq url (replace-match "%2C" t t url)))
706 (let* ((process-environment (browse-url-process-environment))
707 (process (apply 'start-process
708 (concat "netscape " url) nil
709 browse-url-netscape-program
710 (append browse-url-netscape-arguments
711 (if (eq window-system 'w32)
712 (list url)
713 (if new-window '("-noraise"))
714 (list "-remote"
715 (concat "openURL(" url
716 (if (browse-url-maybe-new-window
717 new-window)
718 ",new-window")
719 ")")))))))
720 (set-process-sentinel process
721 (list 'lambda '(process change)
722 (list 'browse-url-netscape-sentinel 'process url)))))
724 (defun browse-url-netscape-sentinel (process url)
725 "Handle a change to the process communicating with Netscape."
726 (or (eq (process-exit-status process) 0)
727 (let* ((process-environment (browse-url-process-environment)))
728 ;; Netscape not running - start it
729 (message "Starting Netscape...")
730 (apply 'start-process (concat "netscape" url) nil
731 browse-url-netscape-program
732 (append browse-url-netscape-startup-arguments (list url))))))
734 (defun browse-url-netscape-reload ()
735 "Ask Netscape to reload its current document.
736 How depends on `browse-url-netscape-version'."
737 (interactive)
738 ;; Backwards incompatibility reported by
739 ;; <peter.kruse@psychologie.uni-regensburg.de>.
740 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
741 "xfeDoCommand(reload)"
742 "reload")))
744 (defun browse-url-netscape-send (command)
745 "Send a remote control command to Netscape."
746 (let* ((process-environment (browse-url-process-environment)))
747 (apply 'start-process "netscape" nil
748 browse-url-netscape-program
749 (append browse-url-netscape-arguments
750 (list "-remote" command)))))
752 ;; --- Mosaic ---
754 ;;;###autoload
755 (defun browse-url-mosaic (url &optional new-window)
756 ;; new-window ignored
757 "Ask the XMosaic WWW browser to load URL.
759 Default to the URL around or before point. The strings in variable
760 `browse-url-mosaic-arguments' are also passed to Mosaic and the
761 program is invoked according to the variable
762 `browse-url-mosaic-program'.
764 When called interactively, if variable `browse-url-new-window-p' is
765 non-nil, load the document in a new Mosaic window, otherwise use a
766 random existing one. A non-nil interactive prefix argument reverses
767 the effect of `browse-url-new-window-p'.
769 When called non-interactively, optional second argument NEW-WINDOW is
770 used instead of `browse-url-new-window-p'."
771 (interactive (browse-url-interactive-arg "Mosaic URL: "))
772 (let ((pidfile (expand-file-name "~/.mosaicpid"))
773 pid pidbuf)
774 (if (file-readable-p pidfile)
775 (save-excursion
776 (find-file pidfile)
777 (goto-char (point-min))
778 (setq pid (read (current-buffer)))
779 (kill-buffer nil)))
780 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
781 (save-excursion
782 (find-file (format "/tmp/Mosaic.%d" pid))
783 (erase-buffer)
784 (insert (if (browse-url-maybe-new-window new-window)
785 "newwin\n"
786 "goto\n")
787 url "\n")
788 (save-buffer)
789 (kill-buffer nil)
790 ;; Send signal SIGUSR to Mosaic
791 (message "Signalling Mosaic...")
792 (signal-process pid browse-url-usr1-signal)
793 ;; Or you could try:
794 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
795 (message "Signalling Mosaic...done")
797 ;; Mosaic not running - start it
798 (message "Starting Mosaic...")
799 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
800 (append browse-url-mosaic-arguments (list url)))
801 (message "Starting Mosaic...done"))))
803 ;; --- Grail ---
805 ;;;###autoload
806 (defvar browse-url-grail
807 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
808 "Location of Grail remote control client script `rcgrail.py'.
809 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
811 ;;;###autoload
812 (defun browse-url-grail (url &optional new-window)
813 "Ask the Grail WWW browser to load URL.
814 Default to the URL around or before point. Runs the program in the
815 variable `browse-url-grail'."
816 (interactive (browse-url-interactive-arg "Grail URL: "))
817 (message "Sending URL to Grail...")
818 (save-excursion
819 (set-buffer (get-buffer-create " *Shell Command Output*"))
820 (erase-buffer)
821 ;; don't worry about this failing.
822 (call-process browse-url-grail nil 0 nil url)
823 (message "Sending URL to Grail... done")))
825 ;; --- Mosaic using CCI ---
827 (defun browse-url-cci (url &optional new-window)
828 "Ask the XMosaic WWW browser to load URL.
829 Default to the URL around or before point.
831 This function only works for XMosaic version 2.5 or later. You must
832 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
833 value of variable `browse-url-CCI-port', and enable `Accept requests'.
835 When called interactively, if variable `browse-url-new-window-p' is
836 non-nil, load the document in a new browser window, otherwise use a
837 random existing one. A non-nil interactive prefix argument reverses
838 the effect of `browse-url-new-window-p'.
840 When called non-interactively, optional second argument NEW-WINDOW is
841 used instead of `browse-url-new-window-p'."
842 (interactive (browse-url-interactive-arg "Mosaic URL: "))
843 (open-network-stream "browse-url" " *browse-url*"
844 browse-url-CCI-host browse-url-CCI-port)
845 ;; Todo: start browser if fails
846 (process-send-string "browse-url"
847 (concat "get url (" url ") output "
848 (if (browse-url-maybe-new-window
849 new-window)
850 "new"
851 "current")
852 "\r\n"))
853 (process-send-string "browse-url" "disconnect\r\n")
854 (delete-process "browse-url"))
856 ;; --- IXI Mosaic ---
858 ;;;###autoload
859 (defun browse-url-iximosaic (url &optional new-window)
860 ;; new-window ignored
861 "Ask the IXIMosaic WWW browser to load URL.
862 Default to the URL around or before point."
863 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
864 (start-process "tellw3b" nil "tellw3b"
865 "-service WWW_BROWSER ixi_showurl " url))
867 ;; --- W3 ---
869 ;;;###autoload
870 (defun browse-url-w3 (url &optional new-window)
871 "Ask the w3 WWW browser to load URL.
872 Default to the URL around or before point.
874 When called interactively, if variable `browse-url-new-window-p' is
875 non-nil, load the document in a new window. A non-nil interactive
876 prefix argument reverses the effect of `browse-url-new-window-p'.
878 When called non-interactively, optional second argument NEW-WINDOW is
879 used instead of `browse-url-new-window-p'."
880 (interactive (browse-url-interactive-arg "W3 URL: "))
881 (if (browse-url-maybe-new-window new-window)
882 (w3-fetch-other-window)
883 (w3-fetch url)))
885 ;;;###autoload
886 (defun browse-url-w3-gnudoit (url &optional new-window)
887 ;; new-window ignored
888 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
889 The `browse-url-gnudoit-program' program is used with options given by
890 `browse-url-gnudoit-args'. Default to the URL around or before point."
891 (interactive (browse-url-interactive-arg "W3 URL: "))
892 (apply 'start-process (concat "gnudoit:" url) nil
893 browse-url-gnudoit-program
894 (append browse-url-gnudoit-args (list (concat "(w3-fetch \"" url "\")") "(raise-frame)"))))
896 ;; --- Lynx in an xterm ---
898 ;;;###autoload
899 (defun browse-url-lynx-xterm (url &optional new-window)
900 ;; new-window ignored
901 "Ask the Lynx WWW browser to load URL.
902 Default to the URL around or before point. A new Lynx process is run
903 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
904 with possible additional arguments `browse-url-xterm-args'."
905 (interactive (browse-url-interactive-arg "Lynx URL: "))
906 (apply #'start-process `(,(concat "lynx" url) nil ,browse-url-xterm-program
907 ,@browse-url-xterm-args "-e" "lynx" ,url)))
909 ;; --- Lynx in an Emacs "term" window ---
911 ;;;###autoload
912 (defun browse-url-lynx-emacs (url &optional new-buffer)
913 "Ask the Lynx WWW browser to load URL.
914 Default to the URL around or before point. With a prefix argument, run
915 a new Lynx process in a new buffer.
917 When called interactively, if variable `browse-url-new-window-p' is
918 non-nil, load the document in a new lynx in a new term window,
919 otherwise use any existing one. A non-nil interactive prefix argument
920 reverses the effect of `browse-url-new-window-p'.
922 When called non-interactively, optional second argument NEW-WINDOW is
923 used instead of `browse-url-new-window-p'."
924 (interactive (browse-url-interactive-arg "Lynx URL: "))
925 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
926 ;; (term-term-name "vt100") ; ??
927 (buf (get-buffer "*lynx*"))
928 (proc (and buf (get-buffer-process buf)))
929 (n browse-url-lynx-input-attempts))
930 (if (and (browse-url-maybe-new-window new-buffer) buf)
931 ;; Rename away the OLD buffer. This isn't very polite, but
932 ;; term insists on working in a buffer named *lynx* and would
933 ;; choke on *lynx*<1>
934 (progn (set-buffer buf)
935 (rename-uniquely)))
936 (if (or (browse-url-maybe-new-window new-buffer)
937 (not buf)
938 (not proc)
939 (not (memq (process-status proc) '(run stop))))
940 ;; start a new lynx
941 (progn
942 (setq buf
943 (apply #'make-term
944 `("lynx" "lynx" nil ,@browse-url-lynx-emacs-args ,url)))
945 (switch-to-buffer buf)
946 (term-char-mode)
947 (set-process-sentinel
948 (get-buffer-process buf)
949 ;; Don't leave around a dead one (especially because of its
950 ;; munged keymap.)
951 (lambda (process event)
952 (if (not (memq (process-status process) '(run stop)))
953 (let ((buf (process-buffer process)))
954 (if buf (kill-buffer buf)))))))
955 ;; send the url to lynx in the old buffer
956 (let ((win (get-buffer-window buf t)))
957 (if win
958 (select-window win)
959 (switch-to-buffer buf)))
960 (if (eq (following-char) ?_)
961 (cond ((eq browse-url-lynx-input-field 'warn)
962 (error "Please move out of the input field first."))
963 ((eq browse-url-lynx-input-field 'avoid)
964 (while (and (eq (following-char) ?_) (> n 0))
965 (term-send-down) ; down arrow
966 (sit-for browse-url-lynx-input-delay))
967 (if (eq (following-char) ?_)
968 (error "Cannot move out of the input field, sorry.")))))
969 (term-send-string proc (concat "g" ; goto
970 "\C-u" ; kill default url
972 "\r")))))
974 ;; --- MMM ---
976 ;;;###autoload
977 (defun browse-url-mmm (url &optional new-window)
978 "Ask the MMM WWW browser to load URL.
979 Default to the URL around or before point."
980 (interactive (browse-url-interactive-arg "MMM URL: "))
981 (message "Sending URL to MMM...")
982 (save-excursion
983 (set-buffer (get-buffer-create " *Shell Command Output*"))
984 (erase-buffer)
985 ;; mmm_remote just SEGVs if the file isn't there...
986 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
987 ;; location in v 0.4:
988 (file-exists-p (expand-file-name "~/.mmm/remote")))
989 (call-process "mmm_remote" nil 0 nil url)
990 (call-process "mmm" nil 0 nil "-external" url))
991 (message "Sending URL to MMM... done")))
993 ;; --- mailto ---
995 ;;;###autoload
996 (defun browse-url-mail (url &optional new-window)
997 "Open a new mail message buffer within Emacs.
998 Default to using the mailto: URL around or before point as the
999 recipient's address. Supplying a non-nil interactive prefix argument
1000 will cause the mail to be composed in another window rather than the
1001 current one.
1003 When called interactively, if variable `browse-url-new-window-p' is
1004 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1005 non-nil interactive prefix argument reverses the effect of
1006 `browse-url-new-window-p'.
1008 When called non-interactively, optional second argument NEW-WINDOW is
1009 used instead of `browse-url-new-window-p'."
1010 (interactive (browse-url-interactive-arg "Mailto URL: "))
1011 (save-excursion
1012 (let ((func (if (browse-url-maybe-new-window new-window)
1013 'compose-mail-other-window
1014 'compose-mail))
1015 (to (if (string-match "^mailto:" url)
1016 (substring url 7)
1017 url)))
1018 (apply func
1019 (list to nil nil nil nil nil (cons 'insert-buffer
1020 (current-buffer)))))))
1022 ;; --- Random browser ---
1024 ;;;###autoload
1025 (defun browse-url-generic (url &optional new-window)
1026 ;; new-window ignored
1027 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1028 Default to the URL around or before point. A fresh copy of the
1029 browser is started up in a new process with possible additional arguments
1030 `browse-url-generic-args'. This is appropriate for browsers which
1031 don't offer a form of remote control."
1032 (interactive (browse-url-interactive-arg "URL: "))
1033 (if (not browse-url-generic-program)
1034 (error "No browser defined (`browse-url-generic-program')"))
1035 (apply 'start-process (concat browse-url-generic-program url) nil
1036 browse-url-generic-program
1037 (append browse-url-generic-args (list url))))
1039 (provide 'browse-url)
1041 ;;; browse-url.el ends here