1 ;;; browse-url.el --- pass a URL to a WWW browser
3 ;; Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
7 ;; Maintainer: Dave Love <fx@gnu.org>
8 ;; Created: 03 Apr 1995
9 ;; Keywords: hypertext, hypermedia, mouse
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; This package provides functions which read a URL (Uniform Resource
31 ;; Locator) from the minibuffer, defaulting to the URL around point,
32 ;; and ask a World-Wide Web browser to load it. It can also load the
33 ;; URL associated with the current buffer. Different browsers use
34 ;; different methods of remote control so there is one function for
35 ;; each supported browser. If the chosen browser is not running, it
36 ;; is started. Currently there is support for the following browsers,
37 ;; some of them probably now obsolete:
39 ;; Function Browser Earliest version
40 ;; browse-url-netscape Netscape 1.1b1
41 ;; browse-url-mosaic XMosaic/mMosaic <= 2.4
42 ;; browse-url-cci XMosaic 2.5
44 ;; browse-url-w3-gnudoit w3 remotely
45 ;; browse-url-iximosaic IXI Mosaic ?
46 ;; browse-url-lynx-* Lynx 0
47 ;; browse-url-grail Grail 0.3b1
48 ;; browse-url-mmm MMM ?
49 ;; browse-url-generic arbitrary
50 ;; browse-url-default-windows-browser MS-Windows browser
51 ;; browse-url-gnome-moz GNOME interface to Mozilla
52 ;; browse-url-kde KDE konqueror (kfm)
54 ;; [A version of the Netscape browser is now free software
55 ;; <URL:http://www.mozilla.org/>, albeit not GPLed, so it is
56 ;; reasonable to have that as the default.]
58 ;; Note that versions of Netscape before 1.1b1 did not have remote
59 ;; control. <URL:http://www.netscape.com/newsref/std/x-remote.html>.
61 ;; Browsers can cache Web pages so it may be necessary to tell them to
62 ;; reload the current page if it has changed (e.g. if you have edited
63 ;; it). There is currently no perfect automatic solution to this.
65 ;; Netscape allows you to specify the id of the window you want to
66 ;; control but which window DO you want to control and how do you
69 ;; If using XMosaic before version 2.5, check the definition of
70 ;; browse-url-usr1-signal below.
71 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/remote-control.html>
73 ;; XMosaic version 2.5 introduced Common Client Interface allowing you
74 ;; to control mosaic through Unix sockets.
75 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/CCI/cci-spec.html>
77 ;; William M. Perry's excellent "w3" WWW browser for
78 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
79 ;; has a function w3-follow-url-at-point, but that
80 ;; doesn't let you edit the URL like browse-url.
81 ;; The `gnuserv' package that can be used to control it in another
82 ;; Emacs process is available from
83 ;; <URL:ftp://ftp.splode.com/pub/users/friedman/packages/>.
85 ;; Grail is the freely available WWW browser implemented in Python, a
86 ;; cool object-oriented freely available interpreted language. Grail
87 ;; 0.3b1 was the first version to have remote control as distributed.
88 ;; For more information on Grail see
89 ;; <URL:http://grail.cnri.reston.va.us/> and for more information on
90 ;; Python see <url:http://www.python.org/>. Grail support in
91 ;; browse-url.el written by Barry Warsaw <bwarsaw@python.org>.
93 ;; MMM is a semi-free WWW browser implemented in Objective Caml, an
94 ;; interesting impure functional programming language. See
95 ;; <URL:http://pauillac.inria.fr/%7Erouaix/mmm/>.
97 ;; Lynx is now distributed by the FSF. See also
98 ;; <URL:http://lynx.browser.org/>.
100 ;; Free graphical browsers that could be used by `browse-url-generic'
101 ;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
102 ;; <URL:http://www.unlv.edu/chimera/>, Arena
103 ;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena> and Amaya
104 ;; <URL:ftp://ftp.w3.org/pub/amaya>. mMosaic
105 ;; <URL:ftp://sig.enst.fr/pub/multicast/mMosaic/>,
106 ;; <URL:http://sig.enst.fr/~dauphin/mMosaic/> (with development
107 ;; support for Java applets and multicast) can be used like Mosaic by
108 ;; setting `browse-url-mosaic-program' appropriately.
110 ;; I [Denis Howe, not Dave Love] recommend Nelson Minar
111 ;; <nelson@santafe.edu>'s excellent html-helper-mode.el for editing
112 ;; HTML and thank Nelson for his many useful comments on this code.
113 ;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>
115 ;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
116 ;; software/own/hm--html-menus/>. For composing correct HTML see also
117 ;; PSGML the general SGML structure editor package
118 ;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
121 ;; This package generalises function html-previewer-process in Marc
122 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
123 ;; ffap.el package. The huge hyperbole package also contains similar
126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 ;; Can you write and test some code for the Macintrash and Windoze
130 ;; Netscape remote control APIs? (See the URL above).
132 ;; Do any other browsers have remote control?
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
137 ;; To display the URL at or before point:
138 ;; M-x browse-url-at-point RET
139 ;; or, similarly but with the opportunity to edit the URL extracted from
143 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
145 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
146 ;; (Note that using Shift-mouse-1 is not desirable because
147 ;; that event has a standard meaning in Emacs.)
149 ;; To display the current buffer in a web browser:
150 ;; M-x browse-url-of-buffer RET
152 ;; To display the current region in a web browser:
153 ;; M-x browse-url-of-region RET
155 ;; In Dired, to display the file named on the current line:
156 ;; M-x browse-url-of-dired-file RET
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;; Customisation (~/.emacs)
161 ;; To see what variables are available for customization, type
162 ;; `M-x set-variable browse-url TAB'. Better, use
163 ;; `M-x customize-group browse-url'.
165 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
166 ;; (as used by html-helper-mode):
167 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
168 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
169 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
170 ;; (global-set-key "\C-c\C-zu" 'browse-url)
171 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
172 ;; (add-hook 'dired-mode-hook
174 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
176 ;; Browse URLs in mail messages under RMAIL by clicking mouse-2:
177 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
178 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
179 ;; Alternatively, add `goto-address' to `rmail-show-message-hook'.
181 ;; Gnus provides a standard feature to activate URLs in article
182 ;; buffers for invocation of browse-url.
184 ;; Use the Emacs w3 browser when not running under X11:
185 ;; (or (eq window-system 'x)
186 ;; (setq browse-url-browser-function 'browse-url-w3))
188 ;; To always save modified buffers before displaying the file in a browser:
189 ;; (setq browse-url-save-file t)
191 ;; To get round the Netscape caching problem, you could EITHER have
192 ;; write-file in html-helper-mode make Netscape reload the document:
194 ;; (autoload 'browse-url-netscape-reload "browse-url"
195 ;; "Ask a WWW browser to redisplay the current file." t)
196 ;; (add-hook 'html-helper-mode-hook
198 ;; (add-hook 'local-write-file-hooks
200 ;; (let ((local-write-file-hooks))
202 ;; (browse-url-netscape-reload)
203 ;; t) ; => file written by hook
204 ;; t))) ; append to l-w-f-hooks
206 ;; OR have browse-url-of-file ask Netscape to load and then reload the
209 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
211 ;; You may also want to customise browse-url-netscape-arguments, e.g.
212 ;; (setq browse-url-netscape-arguments '("-install"))
214 ;; or similarly for the other browsers.
216 ;; To invoke different browsers for different URLs:
217 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
218 ;; ("." . browse-url-netscape)))
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 (eval-when-compile (require 'thingatpt)
229 (require 'w3-auto nil t))
231 (defgroup browse-url nil
232 "Use a web browser to look at a URL."
233 :prefix "browse-url-"
234 :link '(emacs-commentary-link "browse-url")
238 (defcustom browse-url-browser-function
239 (if (memq system-type '(windows-nt ms-dos))
240 'browse-url-default-windows-browser
241 'browse-url-netscape)
242 "*Function to display the current buffer in a WWW browser.
243 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
244 `browse-url-of-file' commands.
246 If the value is not a function it should be a list of pairs
247 \(REGEXP . FUNCTION). In this case the function called will be the one
248 associated with the first REGEXP which matches the current URL. The
249 function is passed the URL and any other args of `browse-url'. The last
250 regexp should probably be \".\" to specify a default browser."
252 (function-item :tag "Emacs W3" :value browse-url-w3)
253 (function-item :tag "W3 in another Emacs via `gnudoit'"
254 :value browse-url-w3-gnudoit)
255 (function-item :tag "Netscape" :value browse-url-netscape)
256 (function-item :tag "Mosaic" :value browse-url-mosaic)
257 (function-item :tag "Mosaic using CCI" :value browse-url-cci)
258 (function-item :tag "IXI Mosaic" :value browse-url-iximosaic)
259 (function-item :tag "Lynx in an xterm window"
260 :value browse-url-lynx-xterm)
261 (function-item :tag "Lynx in an Emacs window"
262 :value browse-url-lynx-emacs)
263 (function-item :tag "Grail" :value browse-url-grail)
264 (function-item :tag "MMM" :value browse-url-mmm)
265 (function-item :tag "KDE" :value browse-url-kde)
266 (function-item :tag "Specified by `Browse Url Generic Program'"
267 :value browse-url-generic)
268 (function-item :tag "Default Windows browser"
269 :value browse-url-default-windows-browser)
270 (function-item :tag "GNOME invoking Mozilla"
271 :value browse-url-gnome-moz)
272 (function :tag "Your own function")
273 (alist :tag "Regexp/function association list"
274 :key-type regexp :value-type function))
278 (defcustom browse-url-netscape-program "netscape"
279 ;; Info about netscape-remote from Karl Berry.
280 "The name by which to invoke Netscape.
282 The free program `netscape-remote' from
283 <URL:http://home.netscape.com/newsref/std/remote.c> is said to start
284 up very much quicker than `netscape'. Reported to compile on a GNU
285 system, given vroot.h from the same directory, with cc flags
286 -DSTANDALONE -L/usr/X11R6/lib -lXmu -lX11."
290 (defcustom browse-url-netscape-arguments nil
291 "A list of strings to pass to Netscape as arguments."
292 :type '(repeat (string :tag "Argument"))
295 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
296 "A list of strings to pass to Netscape when it starts up.
297 Defaults to the value of `browse-url-netscape-arguments' at the time
298 `browse-url' is loaded."
299 :type '(repeat (string :tag "Argument"))
303 (defcustom browse-url-new-window-flag nil
304 "*If non-nil, always open a new browser window with appropriate browsers.
305 Passing an interactive argument to \\[browse-url], or specific browser
306 commands reverses the effect of this variable. Requires Netscape version
307 1.1N or later or XMosaic version 2.5 or later if using those browsers."
312 (defcustom browse-url-netscape-display nil
313 "*The X display for running Netscape, if not same as Emacs'."
314 :type '(choice string (const :tag "Default" nil))
317 (defcustom browse-url-mosaic-program "xmosaic"
318 "The name by which to invoke Mosaic (or mMosaic)."
323 (defcustom browse-url-mosaic-arguments nil
324 "A list of strings to pass to Mosaic as arguments."
325 :type '(repeat (string :tag "Argument"))
328 (defcustom browse-url-filename-alist
329 (\` ; Backquote syntax won't work.
330 (("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
331 ;; The above loses the username to avoid the browser prompting for
332 ;; it in anonymous cases. If it's not anonymous the next regexp
334 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
335 (,@ (if (memq system-type '(windows-nt ms-dos))
336 '(("^\\([a-zA-Z]:\\)[\\/]" . "file:\\1/")
337 ("^[\\/][\\/]+" . "file://"))))
339 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
340 Any substring of a filename matching one of the REGEXPs is replaced by
341 the corresponding STRING using `replace-match', not treating STRING
342 literally. All pairs are applied in the order given. The default
343 value converts ange-ftp/EFS-style paths into ftp URLs and prepends
344 `file:' to any path beginning with `/'.
346 For example, adding to the default a specific translation of an ange-ftp
347 address to an HTTP URL:
349 (setq browse-url-filename-alist
350 '((\"/webmaster@webserver:/home/www/html/\" .
351 \"http://www.acme.co.uk/\")
352 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
353 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
354 (\"^/+\" . \"file:/\")))
356 :type '(repeat (cons :format "%v"
357 (regexp :tag "Regexp")
358 (string :tag "Replacement")))
363 (defcustom browse-url-save-file nil
364 "*If non-nil, save the buffer before displaying its file.
365 Used by the `browse-url-of-file' command."
369 (defcustom browse-url-of-file-hook nil
370 "Run after `browse-url-of-file' has asked a browser to load a file.
372 Set this to `browse-url-netscape-reload' to force Netscape to load the
373 file rather than displaying a cached copy."
375 :options '(browse-url-netscape-reload)
378 (defvar browse-url-usr1-signal
379 (if (and (boundp 'emacs-major-version)
380 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
381 'SIGUSR1 ; Why did I think this was in lower case before?
382 30) ; Check /usr/include/signal.h.
383 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
384 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
385 which is 30 on SunOS and 16 on HP-UX and Solaris.")
387 (defcustom browse-url-CCI-port 3003
388 "Port to access XMosaic via CCI.
389 This can be any number between 1024 and 65535 but must correspond to
390 the value set in the browser."
394 (defcustom browse-url-CCI-host "localhost"
395 "*Host to access XMosaic via CCI.
396 This should be the host name of the machine running XMosaic with CCI
397 enabled. The port number should be set in `browse-url-CCI-port'."
401 (defvar browse-url-temp-file-name nil)
402 (make-variable-buffer-local 'browse-url-temp-file-name)
404 (defcustom browse-url-xterm-program "xterm"
405 "The name of the terminal emulator used by `browse-url-lynx-xterm'.
406 This might, for instance, be a separate colour version of xterm."
410 (defcustom browse-url-xterm-args nil
411 "*A list of strings defining options for `browse-url-xterm-program'.
412 These might set its size, for instance."
413 :type '(repeat (string :tag "Argument"))
416 (defcustom browse-url-lynx-emacs-args (and (not window-system)
418 "A list of strings defining options for Lynx in an Emacs buffer.
420 The default is none in a window system, otherwise `-show_cursor' to
421 indicate the position of the current link in the absence of
422 highlighting, assuming the normal default for showing the cursor."
423 :type '(repeat (string :tag "Argument"))
427 (defcustom browse-url-gnudoit-program "gnudoit"
428 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
432 (defcustom browse-url-gnudoit-args '("-q")
433 "*A list of strings defining options for `browse-url-gnudoit-program'.
434 These might set the port, for instance."
435 :type '(repeat (string :tag "Argument"))
439 (defcustom browse-url-generic-program nil
440 "*The name of the browser program used by `browse-url-generic'."
441 :type '(choice string (const :tag "None" nil))
444 (defcustom browse-url-generic-args nil
445 "*A list of strings defining options for `browse-url-generic-program'."
446 :type '(repeat (string :tag "Argument"))
449 (defcustom browse-url-temp-dir temporary-file-directory
450 "The name of a directory for browse-url's temporary files.
451 Such files are generated by functions like `browse-url-of-region'.
452 You might want to set this to somewhere with restricted read permissions
457 (defcustom browse-url-netscape-version
459 "The version of Netscape you are using.
460 This affects how URL reloading is done; the mechanism changed
461 incompatibly at version 4."
465 (defcustom browse-url-lynx-input-field 'avoid
466 "*Action on selecting an existing Lynx buffer at an input field.
467 What to do when sending a new URL to an existing Lynx buffer in Emacs
468 if the Lynx cursor is on an input field (in which case the `g' command
469 would be entered as data). Such fields are recognized by the
470 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
471 user and don't emit the URL, 'avoid: try to avoid the field by moving
472 down (this *won't* always work)."
473 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
474 (const :tag "Disregard" :value nil)
475 (const :tag "Warn, don't emit URL" :value warn))
479 (defvar browse-url-lynx-input-attempts 10
480 "*How many times to try to move down from a series of lynx input fields.")
482 (defcustom browse-url-lynx-input-delay 0.2
483 "How many seconds to wait for lynx between moves down from an input field.")
485 (defcustom browse-url-kde-program "kfmclient"
486 "The name by which to invoke the KDE web browser."
491 (defcustom browse-url-kde-args '("openURL")
492 "*A list of strings defining options for `browse-url-kde-program'."
493 :type '(repeat (string :tag "Argument"))
496 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
499 (defun browse-url-url-at-point ()
500 (let ((url (thing-at-point 'url)))
501 (set-text-properties 0 (length url) nil url)
504 ;; Having this as a separate function called by the browser-specific
505 ;; functions allows them to be stand-alone commands, making it easier
506 ;; to switch between browsers.
508 (defun browse-url-interactive-arg (prompt)
509 "Read a URL from the minibuffer, prompting with PROMPT.
510 Default to the URL at or before point. If invoked with a mouse button,
511 set point to the position clicked first. Return a list for use in
512 `interactive' containing the URL and `browse-url-new-window-flag' or its
513 negation if a prefix argument was given."
514 (let ((event (elt (this-command-keys) 0)))
515 (and (listp event) (mouse-set-point event)))
516 (list (read-string prompt (browse-url-url-at-point))
517 (not (eq (null browse-url-new-window-flag)
518 (null current-prefix-arg)))))
520 ;; interactive-p needs to be called at a function's top-level, hence
522 (defmacro browse-url-maybe-new-window (arg)
525 browse-url-new-window-flag))
527 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
528 ;; Browse current buffer
531 (defun browse-url-of-file (&optional file)
532 "Ask a WWW browser to display FILE.
533 Display the current buffer's file if FILE is nil or if called
534 interactively. Turn the filename into a URL with function
535 `browse-url-file-url'. Pass the URL to a browser using the
536 `browse-url' function then run `browse-url-of-file-hook'."
539 (setq file (buffer-file-name))
540 (error "Current buffer has no file"))
541 (let ((buf (get-file-buffer file)))
545 (cond ((not (buffer-modified-p)))
546 (browse-url-save-file (save-buffer))
547 (t (message "%s modified since last save" file))))))
548 (browse-url (browse-url-file-url file))
549 (run-hooks 'browse-url-of-file-hook))
551 (defun browse-url-file-url (file)
552 "Return the URL corresponding to FILE.
553 Use variable `browse-url-filename-alist' to map filenames to URLs."
554 ;; URL-encode special chars, do % first
556 (while (setq s (string-match "%" file s))
557 (setq file (replace-match "%25" t t file)
559 (while (string-match "[*\"()',=;? ]" file)
560 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
561 (setq file (replace-match enc t t file))))
562 (dolist (map browse-url-filename-alist)
563 (when (and map (string-match (car map) file))
564 (setq file (replace-match (cdr map) t nil file))))
568 (defun browse-url-of-buffer (&optional buffer)
569 "Ask a WWW browser to display BUFFER.
570 Display the current buffer if BUFFER is nil. Display only the
571 currently visible part of BUFFER (from a temporary file) if buffer is
575 (and buffer (set-buffer buffer))
577 ;; Ignore real name if restricted
578 (and (= (- (point-max) (point-min)) (buffer-size))
580 (and (boundp 'dired-directory) dired-directory)))))
583 (or browse-url-temp-file-name
584 (setq browse-url-temp-file-name
585 (convert-standard-filename
587 (expand-file-name "burl" browse-url-temp-dir)))))
588 (setq file-name browse-url-temp-file-name)
589 (write-region (point-min) (point-max) file-name nil 'no-message)))
590 (browse-url-of-file file-name))))
592 (defun browse-url-delete-temp-file (&optional temp-file-name)
593 ;; Delete browse-url-temp-file-name from the file system
594 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
595 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
596 (if (and file-name (file-exists-p file-name))
597 (delete-file file-name))))
599 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
602 (defun browse-url-of-dired-file ()
603 "In Dired, ask a WWW browser to display the file named on this line."
605 (browse-url-of-file (dired-get-filename)))
608 (defun browse-url-of-region (min max)
609 "Ask a WWW browser to display the current region."
613 (narrow-to-region min max)
614 (browse-url-of-buffer))))
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
617 ;; Browser-independent commands
619 ;; A generic command to call the current browse-url-browser-function
622 (defun browse-url (url &rest args)
623 "Ask a WWW browser to load URL.
624 Prompts for a URL, defaulting to the URL at or before point. Variable
625 `browse-url-browser-function' says which browser to use."
626 (interactive (browse-url-interactive-arg "URL: "))
627 (unless (interactive-p)
628 (setq args (or args (list browse-url-new-window-flag))))
629 (if (functionp browse-url-browser-function)
630 (apply browse-url-browser-function url args)
631 ;; The `function' can be an alist; look down it for first match
632 ;; and apply the function (which might be a lambda).
634 (dolist (bf browse-url-browser-function)
635 (when (string-match (car bf) url)
636 (apply (cdr bf) url args)
638 (error "No browse-url-browser-function matching URL %s"
642 (defun browse-url-at-point (&optional arg)
643 "Ask a WWW browser to load the URL at or before point.
644 Doesn't let you edit the URL like `browse-url'. Variable
645 `browse-url-browser-function' says which browser to use."
647 (let ((url (browse-url-url-at-point)))
649 (browse-url url (if arg
650 (not browse-url-new-window-flag)
651 browse-url-new-window-flag))
652 (error "No URL found"))))
655 (defun browse-url-at-mouse (event)
656 "Ask a WWW browser to load a URL clicked with the mouse.
657 The URL is the one around or before the position of the mouse click
658 but point is not changed. Doesn't let you edit the URL like
659 `browse-url'. Variable `browse-url-browser-function' says which browser
663 (mouse-set-point event)
664 (browse-url-at-point browse-url-new-window-flag)))
666 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
667 ;; Browser-specific commands
669 ;; --- Default MS-Windows browser ---
671 (defun browse-url-default-windows-browser (url &optional new-window)
672 (interactive (browse-url-interactive-arg "URL: "))
673 (if (eq system-type 'ms-dos)
674 (if dos-windows-version
675 (shell-command (concat "start " (shell-quote-argument url)))
676 (error "Browsing URLs is not supported on this system"))
677 (w32-shell-execute "open" url)))
681 (defun browse-url-process-environment ()
682 "Set DISPLAY in the environment to the X display Netscape is running on.
683 This is either the value of variable `browse-url-netscape-display' if
684 non-nil, or the same display as Emacs if different from the current
685 environment, otherwise just use the current environment."
686 (let ((display (or browse-url-netscape-display (browse-url-emacs-display))))
688 (cons (concat "DISPLAY=" display) process-environment)
689 process-environment)))
691 (defun browse-url-emacs-display ()
692 "Return the X display Emacs is running on.
693 This is nil if the display is the same as the DISPLAY environment variable.
695 Actually Emacs could be using several displays; this just returns the
696 one showing the selected frame."
697 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
698 (and (not (equal display (getenv "DISPLAY")))
702 (defun browse-url-netscape (url &optional new-window)
703 "Ask the Netscape WWW browser to load URL.
704 Default to the URL around or before point. The strings in variable
705 `browse-url-netscape-arguments' are also passed to Netscape.
707 When called interactively, if variable `browse-url-new-window-flag' is
708 non-nil, load the document in a new Netscape window, otherwise use a
709 random existing one. A non-nil interactive prefix argument reverses
710 the effect of `browse-url-new-window-flag'.
712 When called non-interactively, optional second argument NEW-WINDOW is
713 used instead of `browse-url-new-window-flag'."
714 (interactive (browse-url-interactive-arg "URL: "))
715 ;; URL encode any `confusing' characters in the URL. This needs to
716 ;; include at least commas; presumably also close parens.
717 (while (string-match "[,)]" url)
718 (setq url (replace-match
719 (format "%%%x" (string-to-char (match-string 0 url))) t t url)))
720 (let* ((process-environment (browse-url-process-environment))
721 (process (apply 'start-process
722 (concat "netscape " url) nil
723 browse-url-netscape-program
725 browse-url-netscape-arguments
726 (if (eq window-system 'w32)
729 (if new-window '("-noraise"))
731 (concat "openURL(" url
732 (if (browse-url-maybe-new-window
736 (set-process-sentinel process
737 `(lambda (process change)
738 (browse-url-netscape-sentinel process ,url)))))
740 (defun browse-url-netscape-sentinel (process url)
741 "Handle a change to the process communicating with Netscape."
742 (or (eq (process-exit-status process) 0)
743 (let* ((process-environment (browse-url-process-environment)))
744 ;; Netscape not running - start it
745 (message "Starting Netscape...")
746 (apply 'start-process (concat "netscape" url) nil
747 browse-url-netscape-program
748 (append browse-url-netscape-startup-arguments (list url))))))
750 (defun browse-url-netscape-reload ()
751 "Ask Netscape to reload its current document.
752 How depends on `browse-url-netscape-version'."
754 ;; Backwards incompatibility reported by
755 ;; <peter.kruse@psychologie.uni-regensburg.de>.
756 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
757 "xfeDoCommand(reload)"
760 (defun browse-url-netscape-send (command)
761 "Send a remote control command to Netscape."
762 (let* ((process-environment (browse-url-process-environment)))
763 (apply 'start-process "netscape" nil
764 browse-url-netscape-program
765 (append browse-url-netscape-arguments
766 (list "-remote" command)))))
768 ;; GNOME means of invoking either Mozilla or Netrape.
770 (defcustom browse-url-gnome-moz-arguments '()
771 "A list of strings passed to the GNOME mozilla viewer as arguments."
773 :type '(repeat (string :tag "Argument"))
777 (defun browse-url-gnome-moz (url &optional new-window)
778 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
779 Default to the URL around or before point. The strings in variable
780 `browse-url-gnome-moz-arguments' are also passed.
782 When called interactively, if variable `browse-url-new-window-flag' is
783 non-nil, load the document in a new browser window, otherwise use an
784 existing one. A non-nil interactive prefix argument reverses the
785 effect of `browse-url-new-window-flag'.
787 When called non-interactively, optional second argument NEW-WINDOW is
788 used instead of `browse-url-new-window-flag'."
789 (interactive (browse-url-interactive-arg "URL: "))
790 (apply 'start-process (concat "gnome-moz-remote " url)
794 browse-url-gnome-moz-arguments
795 (if (browse-url-maybe-new-window new-window)
797 (list "--raise" url))))
802 (defun browse-url-mosaic (url &optional new-window)
803 "Ask the XMosaic WWW browser to load URL.
805 Default to the URL around or before point. The strings in variable
806 `browse-url-mosaic-arguments' are also passed to Mosaic and the
807 program is invoked according to the variable
808 `browse-url-mosaic-program'.
810 When called interactively, if variable `browse-url-new-window-flag' is
811 non-nil, load the document in a new Mosaic window, otherwise use a
812 random existing one. A non-nil interactive prefix argument reverses
813 the effect of `browse-url-new-window-flag'.
815 When called non-interactively, optional second argument NEW-WINDOW is
816 used instead of `browse-url-new-window-flag'."
817 (interactive (browse-url-interactive-arg "Mosaic URL: "))
818 (let ((pidfile (expand-file-name "~/.mosaicpid"))
820 (if (file-readable-p pidfile)
823 (goto-char (point-min))
824 (setq pid (read (current-buffer)))
826 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
828 (find-file (format "/tmp/Mosaic.%d" pid))
830 (insert (if (browse-url-maybe-new-window new-window)
836 ;; Send signal SIGUSR to Mosaic
837 (message "Signalling Mosaic...")
838 (signal-process pid browse-url-usr1-signal)
840 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
841 (message "Signalling Mosaic...done")
843 ;; Mosaic not running - start it
844 (message "Starting Mosaic...")
845 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
846 (append browse-url-mosaic-arguments (list url)))
847 (message "Starting Mosaic...done"))))
852 (defvar browse-url-grail
853 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
854 "Location of Grail remote control client script `rcgrail.py'.
855 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
858 (defun browse-url-grail (url &optional new-window)
859 "Ask the Grail WWW browser to load URL.
860 Default to the URL around or before point. Runs the program in the
861 variable `browse-url-grail'."
862 (interactive (browse-url-interactive-arg "Grail URL: "))
863 (message "Sending URL to Grail...")
865 (set-buffer (get-buffer-create " *Shell Command Output*"))
867 ;; don't worry about this failing.
869 (call-process browse-url-grail nil 0 nil "-b" url)
870 (call-process browse-url-grail nil 0 nil url))
871 (message "Sending URL to Grail... done")))
873 ;; --- Mosaic using CCI ---
876 (defun browse-url-cci (url &optional new-window)
877 "Ask the XMosaic WWW browser to load URL.
878 Default to the URL around or before point.
880 This function only works for XMosaic version 2.5 or later. You must
881 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
882 value of variable `browse-url-CCI-port', and enable `Accept requests'.
884 When called interactively, if variable `browse-url-new-window-flag' is
885 non-nil, load the document in a new browser window, otherwise use a
886 random existing one. A non-nil interactive prefix argument reverses
887 the effect of `browse-url-new-window-flag'.
889 When called non-interactively, optional second argument NEW-WINDOW is
890 used instead of `browse-url-new-window-flag'."
891 (interactive (browse-url-interactive-arg "Mosaic URL: "))
892 (open-network-stream "browse-url" " *browse-url*"
893 browse-url-CCI-host browse-url-CCI-port)
894 ;; Todo: start browser if fails
895 (process-send-string "browse-url"
896 (concat "get url (" url ") output "
897 (if (browse-url-maybe-new-window new-window)
901 (process-send-string "browse-url" "disconnect\r\n")
902 (delete-process "browse-url"))
904 ;; --- IXI Mosaic ---
907 (defun browse-url-iximosaic (url &optional new-window)
908 ;; new-window ignored
909 "Ask the IXIMosaic WWW browser to load URL.
910 Default to the URL around or before point."
911 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
912 (start-process "tellw3b" nil "tellw3b"
913 "-service WWW_BROWSER ixi_showurl " url))
918 (defun browse-url-w3 (url &optional new-window)
919 "Ask the w3 WWW browser to load URL.
920 Default to the URL around or before point.
922 When called interactively, if variable `browse-url-new-window-flag' is
923 non-nil, load the document in a new window. A non-nil interactive
924 prefix argument reverses the effect of `browse-url-new-window-flag'.
926 When called non-interactively, optional second argument NEW-WINDOW is
927 used instead of `browse-url-new-window-flag'."
928 (interactive (browse-url-interactive-arg "W3 URL: "))
929 (require 'w3) ; w3-fetch-other-window not autoloaded
930 (if (browse-url-maybe-new-window new-window)
931 (w3-fetch-other-window url)
935 (defun browse-url-w3-gnudoit (url &optional new-window)
936 ;; new-window ignored
937 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
938 The `browse-url-gnudoit-program' program is used with options given by
939 `browse-url-gnudoit-args'. Default to the URL around or before point."
940 (interactive (browse-url-interactive-arg "W3 URL: "))
941 (apply 'start-process (concat "gnudoit:" url) nil
942 browse-url-gnudoit-program
943 (append browse-url-gnudoit-args
944 (list (concat "(w3-fetch \"" url "\")")
947 ;; --- Lynx in an xterm ---
950 (defun browse-url-lynx-xterm (url &optional new-window)
951 ;; new-window ignored
952 "Ask the Lynx WWW browser to load URL.
953 Default to the URL around or before point. A new Lynx process is run
954 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
955 with possible additional arguments `browse-url-xterm-args'."
956 (interactive (browse-url-interactive-arg "Lynx URL: "))
957 (apply #'start-process `(,(concat "lynx" url) nil ,browse-url-xterm-program
958 ,@browse-url-xterm-args "-e" "lynx"
961 ;; --- Lynx in an Emacs "term" window ---
964 (defun browse-url-lynx-emacs (url &optional new-buffer)
965 "Ask the Lynx WWW browser to load URL.
966 Default to the URL around or before point. With a prefix argument, run
967 a new Lynx process in a new buffer.
969 When called interactively, if variable `browse-url-new-window-flag' is
970 non-nil, load the document in a new lynx in a new term window,
971 otherwise use any existing one. A non-nil interactive prefix argument
972 reverses the effect of `browse-url-new-window-flag'.
974 When called non-interactively, optional second argument NEW-WINDOW is
975 used instead of `browse-url-new-window-flag'."
976 (interactive (browse-url-interactive-arg "Lynx URL: "))
977 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
978 ;; (term-term-name "vt100") ; ??
979 (buf (get-buffer "*lynx*"))
980 (proc (and buf (get-buffer-process buf)))
981 (n browse-url-lynx-input-attempts))
982 (if (and (browse-url-maybe-new-window new-buffer) buf)
983 ;; Rename away the OLD buffer. This isn't very polite, but
984 ;; term insists on working in a buffer named *lynx* and would
985 ;; choke on *lynx*<1>
986 (progn (set-buffer buf)
988 (if (or (browse-url-maybe-new-window new-buffer)
991 (not (memq (process-status proc) '(run stop))))
996 `("lynx" "lynx" nil ,@browse-url-lynx-emacs-args
998 (switch-to-buffer buf)
1000 (set-process-sentinel
1001 (get-buffer-process buf)
1002 ;; Don't leave around a dead one (especially because of its
1004 (lambda (process event)
1005 (if (not (memq (process-status process) '(run stop)))
1006 (let ((buf (process-buffer process)))
1007 (if buf (kill-buffer buf)))))))
1008 ;; send the url to lynx in the old buffer
1009 (let ((win (get-buffer-window buf t)))
1012 (switch-to-buffer buf)))
1013 (if (eq (following-char) ?_)
1014 (cond ((eq browse-url-lynx-input-field 'warn)
1015 (error "Please move out of the input field first"))
1016 ((eq browse-url-lynx-input-field 'avoid)
1017 (while (and (eq (following-char) ?_) (> n 0))
1018 (term-send-down) ; down arrow
1019 (sit-for browse-url-lynx-input-delay))
1020 (if (eq (following-char) ?_)
1021 (error "Cannot move out of the input field, sorry")))))
1022 (term-send-string proc (concat "g" ; goto
1023 "\C-u" ; kill default url
1030 (defun browse-url-mmm (url &optional new-window)
1031 "Ask the MMM WWW browser to load URL.
1032 Default to the URL around or before point."
1033 (interactive (browse-url-interactive-arg "MMM URL: "))
1034 (message "Sending URL to MMM...")
1036 (set-buffer (get-buffer-create " *Shell Command Output*"))
1038 ;; mmm_remote just SEGVs if the file isn't there...
1039 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
1040 ;; location in v 0.4:
1041 (file-exists-p (expand-file-name "~/.mmm/remote")))
1042 (call-process "mmm_remote" nil 0 nil url)
1043 (call-process "mmm" nil 0 nil "-external" url))
1044 (message "Sending URL to MMM... done")))
1049 (defun browse-url-mail (url &optional new-window)
1050 "Open a new mail message buffer within Emacs.
1051 Default to using the mailto: URL around or before point as the
1052 recipient's address. Supplying a non-nil interactive prefix argument
1053 will cause the mail to be composed in another window rather than the
1056 When called interactively, if variable `browse-url-new-window-flag' is
1057 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1058 non-nil interactive prefix argument reverses the effect of
1059 `browse-url-new-window-flag'.
1061 When called non-interactively, optional second argument NEW-WINDOW is
1062 used instead of `browse-url-new-window-flag'."
1063 (interactive (browse-url-interactive-arg "Mailto URL: "))
1065 (let ((to (if (string-match "^mailto:" url)
1068 (if (browse-url-maybe-new-window new-window)
1069 (compose-mail-other-window to nil nil nil
1070 (list 'insert-buffer (current-buffer)))
1071 (compose-mail to nil nil nil nil
1072 (list 'insert-buffer (current-buffer)))))))
1074 ;; --- Random browser ---
1077 (defun browse-url-generic (url &optional new-window)
1078 ;; new-window ignored
1079 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1080 Default to the URL around or before point. A fresh copy of the
1081 browser is started up in a new process with possible additional arguments
1082 `browse-url-generic-args'. This is appropriate for browsers which
1083 don't offer a form of remote control."
1084 (interactive (browse-url-interactive-arg "URL: "))
1085 (if (not browse-url-generic-program)
1086 (error "No browser defined (`browse-url-generic-program')"))
1087 (apply 'start-process (concat browse-url-generic-program url) nil
1088 browse-url-generic-program
1089 (append browse-url-generic-args (list url))))
1092 (defun browse-url-kde (url &optional new-window)
1093 "Ask the KDE WWW browser to load URL.
1094 Default to the URL around or before point."
1095 (interactive (browse-url-interactive-arg "KDE URL: "))
1096 (message "Sending URL to KDE...")
1097 (apply #'start-process `(,(concat "KDE" url) nil ,browse-url-kde-program
1098 ,@browse-url-kde-args ,url)))
1100 (provide 'browse-url)
1102 ;;; browse-url.el ends here