(browse-url-filename-alist): Allow UNC file
[emacs.git] / lisp / net / browse-url.el
blobee579d859c1e5ccb016d449ca31cc083929a4d2a
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)
16 ;; any later version.
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.
28 ;;; Commentary:
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
43 ;; browse-url-w3 w3 0
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
53 ;; [A version of the Netscape browser is now free software
54 ;; <URL:http://www.mozilla.org/>, albeit not GPLed, so it is
55 ;; reasonable to have that as the default.]
57 ;; Note that versions of Netscape before 1.1b1 did not have remote
58 ;; control. <URL:http://www.netscape.com/newsref/std/x-remote.html>.
60 ;; Browsers can cache Web pages so it may be necessary to tell them to
61 ;; reload the current page if it has changed (e.g. if you have edited
62 ;; it). There is currently no perfect automatic solution to this.
64 ;; Netscape allows you to specify the id of the window you want to
65 ;; control but which window DO you want to control and how do you
66 ;; discover its id?
68 ;; If using XMosaic before version 2.5, check the definition of
69 ;; browse-url-usr1-signal below.
70 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/remote-control.html>
72 ;; XMosaic version 2.5 introduced Common Client Interface allowing you
73 ;; to control mosaic through Unix sockets.
74 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/CCI/cci-spec.html>
76 ;; William M. Perry's excellent "w3" WWW browser for
77 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
78 ;; has a function w3-follow-url-at-point, but that
79 ;; doesn't let you edit the URL like browse-url.
80 ;; The `gnuserv' package that can be used to control it in another
81 ;; Emacs process is available from
82 ;; <URL:ftp://ftp.splode.com/pub/users/friedman/packages/>.
84 ;; Grail is the freely available WWW browser implemented in Python, a
85 ;; cool object-oriented freely available interpreted language. Grail
86 ;; 0.3b1 was the first version to have remote control as distributed.
87 ;; For more information on Grail see
88 ;; <URL:http://grail.cnri.reston.va.us/> and for more information on
89 ;; Python see <url:http://www.python.org/>. Grail support in
90 ;; browse-url.el written by Barry Warsaw <bwarsaw@python.org>.
92 ;; MMM is a semi-free WWW browser implemented in Objective Caml, an
93 ;; interesting impure functional programming language. See
94 ;; <URL:http://pauillac.inria.fr/%7Erouaix/mmm/>.
96 ;; Lynx is now distributed by the FSF. See also
97 ;; <URL:http://lynx.browser.org/>.
99 ;; Free graphical browsers that could be used by `browse-url-generic'
100 ;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
101 ;; <URL:http://www.unlv.edu/chimera/>, Arena
102 ;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena> and Amaya
103 ;; <URL:ftp://ftp.w3.org/pub/amaya>. mMosaic
104 ;; <URL:ftp://sig.enst.fr/pub/multicast/mMosaic/>,
105 ;; <URL:http://sig.enst.fr/~dauphin/mMosaic/> (with development
106 ;; support for Java applets and multicast) can be used like Mosaic by
107 ;; setting `browse-url-mosaic-program' appropriately.
109 ;; I [Denis Howe, not Dave Love] recommend Nelson Minar
110 ;; <nelson@santafe.edu>'s excellent html-helper-mode.el for editing
111 ;; HTML and thank Nelson for his many useful comments on this code.
112 ;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>
114 ;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
115 ;; software/own/hm--html-menus/>. For composing correct HTML see also
116 ;; PSGML the general SGML structure editor package
117 ;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
118 ;; with this.
120 ;; This package generalises function html-previewer-process in Marc
121 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
122 ;; ffap.el package. The huge hyperbole package also contains similar
123 ;; functions.
125 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 ;; Help!
128 ;; Can you write and test some code for the Macintrash and Windoze
129 ;; Netscape remote control APIs? (See the URL above).
131 ;; Do any other browsers have remote control?
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ;; Usage
136 ;; To display the URL at or before point:
137 ;; M-x browse-url-at-point RET
138 ;; or, similarly but with the opportunity to edit the URL extracted from
139 ;; the buffer, use:
140 ;; M-x browse-url
142 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
143 ;; file:
144 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
145 ;; (Note that using Shift-mouse-1 is not desirable because
146 ;; that event has a standard meaning in Emacs.)
148 ;; To display the current buffer in a web browser:
149 ;; M-x browse-url-of-buffer RET
151 ;; To display the current region in a web browser:
152 ;; M-x browse-url-of-region RET
154 ;; In Dired, to display the file named on the current line:
155 ;; M-x browse-url-of-dired-file RET
157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 ;; Customisation (~/.emacs)
160 ;; To see what variables are available for customization, type
161 ;; `M-x set-variable browse-url TAB'. Better, use
162 ;; `M-x customize-group browse-url'.
164 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
165 ;; (as used by html-helper-mode):
166 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
167 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
168 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
169 ;; (global-set-key "\C-c\C-zu" 'browse-url)
170 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
171 ;; (add-hook 'dired-mode-hook
172 ;; (lambda ()
173 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
175 ;; Browse URLs in mail messages under RMAIL by clicking mouse-2:
176 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
177 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
178 ;; Alternatively, add `goto-address' to `rmail-show-message-hook'.
180 ;; Gnus provides a standard feature to activate URLs in article
181 ;; buffers for invocation of browse-url.
183 ;; Use the Emacs w3 browser when not running under X11:
184 ;; (or (eq window-system 'x)
185 ;; (setq browse-url-browser-function 'browse-url-w3))
187 ;; To always save modified buffers before displaying the file in a browser:
188 ;; (setq browse-url-save-file t)
190 ;; To get round the Netscape caching problem, you could EITHER have
191 ;; write-file in html-helper-mode make Netscape reload the document:
193 ;; (autoload 'browse-url-netscape-reload "browse-url"
194 ;; "Ask a WWW browser to redisplay the current file." t)
195 ;; (add-hook 'html-helper-mode-hook
196 ;; (lambda ()
197 ;; (add-hook 'local-write-file-hooks
198 ;; (lambda ()
199 ;; (let ((local-write-file-hooks))
200 ;; (save-buffer))
201 ;; (browse-url-netscape-reload)
202 ;; t) ; => file written by hook
203 ;; t))) ; append to l-w-f-hooks
205 ;; OR have browse-url-of-file ask Netscape to load and then reload the
206 ;; file:
208 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
210 ;; You may also want to customise browse-url-netscape-arguments, e.g.
211 ;; (setq browse-url-netscape-arguments '("-install"))
213 ;; or similarly for the other browsers.
215 ;; To invoke different browsers for different URLs:
216 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
217 ;; ("." . browse-url-netscape)))
219 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
220 ;;; Code:
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;; Variables
225 (eval-when-compile (require 'thingatpt)
226 (require 'term)
227 (require 'dired)
228 (require 'w3-auto nil t))
230 (defgroup browse-url nil
231 "Use a web browser to look at a URL."
232 :prefix "browse-url-"
233 :link '(emacs-commentary-link "browse-url")
234 :group 'hypermedia)
236 ;;;###autoload
237 (defcustom browse-url-browser-function
238 (if (eq system-type 'windows-nt)
239 'browse-url-default-windows-browser
240 'browse-url-netscape)
241 "*Function to display the current buffer in a WWW browser.
242 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
243 `browse-url-of-file' commands.
245 If the value is not a function it should be a list of pairs
246 \(REGEXP . FUNCTION). In this case the function called will be the one
247 associated with the first REGEXP which matches the current URL. The
248 function is passed the URL and any other args of `browse-url'. The last
249 regexp should probably be \".\" to specify a default browser."
250 :type '(choice
251 (function-item :tag "Emacs W3" :value browse-url-w3)
252 (function-item :tag "W3 in another Emacs via `gnudoit'"
253 :value browse-url-w3-gnudoit)
254 (function-item :tag "Netscape" :value browse-url-netscape)
255 (function-item :tag "Mosaic" :value browse-url-mosaic)
256 (function-item :tag "Mosaic using CCI" :value browse-url-cci)
257 (function-item :tag "IXI Mosaic" :value browse-url-iximosaic)
258 (function-item :tag "Lynx in an xterm window"
259 :value browse-url-lynx-xterm)
260 (function-item :tag "Lynx in an Emacs window"
261 :value browse-url-lynx-emacs)
262 (function-item :tag "Grail" :value browse-url-grail)
263 (function-item :tag "MMM" :value browse-url-mmm)
264 (function-item :tag "Specified by `Browse Url Generic Program'"
265 :value browse-url-generic)
266 (function-item :tag "Default Windows browser"
267 :value browse-url-default-windows-browser)
268 (function-item :tag "GNOME invoking Mozilla"
269 :value browse-url-gnome-moz)
270 (function :tag "Your own function")
271 (alist :tag "Regexp/function association list"
272 :key-type regexp :value-type function))
273 :version "21.1"
274 :group 'browse-url)
276 (defcustom browse-url-netscape-program "netscape"
277 ;; Info about netscape-remote from Karl Berry.
278 "The name by which to invoke Netscape.
280 The free program `netscape-remote' from
281 <URL:http://home.netscape.com/newsref/std/remote.c> is said to start
282 up very much quicker than `netscape'. Reported to compile on a GNU
283 system, given vroot.h from the same directory, with cc flags
284 -DSTANDALONE -L/usr/X11R6/lib -lXmu -lX11."
285 :type 'string
286 :group 'browse-url)
288 (defcustom browse-url-netscape-arguments nil
289 "A list of strings to pass to Netscape as arguments."
290 :type '(repeat (string :tag "Argument"))
291 :group 'browse-url)
293 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
294 "A list of strings to pass to Netscape when it starts up.
295 Defaults to the value of `browse-url-netscape-arguments' at the time
296 `browse-url' is loaded."
297 :type '(repeat (string :tag "Argument"))
298 :group 'browse-url)
300 ;;;###autoload
301 (defcustom browse-url-new-window-flag nil
302 "*If non-nil, always open a new browser window with appropriate browsers.
303 Passing an interactive argument to \\[browse-url], or specific browser
304 commands reverses the effect of this variable. Requires Netscape version
305 1.1N or later or XMosaic version 2.5 or later if using those browsers."
306 :type 'boolean
307 :group 'browse-url)
309 ;;;###autoload
310 (defcustom browse-url-netscape-display nil
311 "*The X display for running Netscape, if not same as Emacs'."
312 :type '(choice string (const :tag "Default" nil))
313 :group 'browse-url)
315 (defcustom browse-url-mosaic-program "xmosaic"
316 "The name by which to invoke Mosaic (or mMosaic)."
317 :type 'string
318 :version "20.3"
319 :group 'browse-url)
321 (defcustom browse-url-mosaic-arguments nil
322 "A list of strings to pass to Mosaic as arguments."
323 :type '(repeat (string :tag "Argument"))
324 :group 'browse-url)
326 (defcustom browse-url-filename-alist
327 (\` ; Backquote syntax won't work.
328 (("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
329 ;; The above loses the username to avoid the browser prompting for
330 ;; it in anonymous cases. If it's not anonymous the next regexp
331 ;; applies.
332 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
333 (,@ (if (memq system-type '(windows-nt ms-dos))
334 '(("^\\([a-zA-Z]:\\)[\\/]" . "file:\\1/")
335 ("^[\\/][\\/]+" . "file://"))))
336 ("^/+" . "file:/")))
337 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
338 Any substring of a filename matching one of the REGEXPs is replaced by
339 the corresponding STRING using `replace-match', not treating STRING
340 literally. All pairs are applied in the order given. The default
341 value converts ange-ftp/EFS-style paths into ftp URLs and prepends
342 `file:' to any path beginning with `/'.
344 For example, adding to the default a specific translation of an ange-ftp
345 address to an HTTP URL:
347 (setq browse-url-filename-alist
348 '((\"/webmaster@webserver:/home/www/html/\" .
349 \"http://www.acme.co.uk/\")
350 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
351 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
352 (\"^/+\" . \"file:/\")))
354 :type '(repeat (cons :format "%v"
355 (regexp :tag "Regexp")
356 (string :tag "Replacement")))
357 :version "20.3"
358 :group 'browse-url)
360 ;;;###autoload
361 (defcustom browse-url-save-file nil
362 "*If non-nil, save the buffer before displaying its file.
363 Used by the `browse-url-of-file' command."
364 :type 'boolean
365 :group 'browse-url)
367 (defcustom browse-url-of-file-hook nil
368 "Run after `browse-url-of-file' has asked a browser to load a file.
370 Set this to `browse-url-netscape-reload' to force Netscape to load the
371 file rather than displaying a cached copy."
372 :type 'hook
373 :options '(browse-url-netscape-reload)
374 :group 'browse-url)
376 (defvar browse-url-usr1-signal
377 (if (and (boundp 'emacs-major-version)
378 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
379 'SIGUSR1 ; Why did I think this was in lower case before?
380 30) ; Check /usr/include/signal.h.
381 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
382 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
383 which is 30 on SunOS and 16 on HP-UX and Solaris.")
385 (defcustom browse-url-CCI-port 3003
386 "Port to access XMosaic via CCI.
387 This can be any number between 1024 and 65535 but must correspond to
388 the value set in the browser."
389 :type 'integer
390 :group 'browse-url)
392 (defcustom browse-url-CCI-host "localhost"
393 "*Host to access XMosaic via CCI.
394 This should be the host name of the machine running XMosaic with CCI
395 enabled. The port number should be set in `browse-url-CCI-port'."
396 :type 'string
397 :group 'browse-url)
399 (defvar browse-url-temp-file-name nil)
400 (make-variable-buffer-local 'browse-url-temp-file-name)
402 (defcustom browse-url-xterm-program "xterm"
403 "The name of the terminal emulator used by `browse-url-lynx-xterm'.
404 This might, for instance, be a separate colour version of xterm."
405 :type 'string
406 :group 'browse-url)
408 (defcustom browse-url-xterm-args nil
409 "*A list of strings defining options for `browse-url-xterm-program'.
410 These might set its size, for instance."
411 :type '(repeat (string :tag "Argument"))
412 :group 'browse-url)
414 (defcustom browse-url-lynx-emacs-args (and (not window-system)
415 '("-show_cursor"))
416 "A list of strings defining options for Lynx in an Emacs buffer.
418 The default is none in a window system, otherwise `-show_cursor' to
419 indicate the position of the current link in the absence of
420 highlighting, assuming the normal default for showing the cursor."
421 :type '(repeat (string :tag "Argument"))
422 :version "20.3"
423 :group 'browse-url)
425 (defcustom browse-url-gnudoit-program "gnudoit"
426 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
427 :type 'string
428 :group 'browse-url)
430 (defcustom browse-url-gnudoit-args '("-q")
431 "*A list of strings defining options for `browse-url-gnudoit-program'.
432 These might set the port, for instance."
433 :type '(repeat (string :tag "Argument"))
434 :group 'browse-url)
436 ;;;###autoload
437 (defcustom browse-url-generic-program nil
438 "*The name of the browser program used by `browse-url-generic'."
439 :type '(choice string (const :tag "None" nil))
440 :group 'browse-url)
442 (defcustom browse-url-generic-args nil
443 "*A list of strings defining options for `browse-url-generic-program'."
444 :type '(repeat (string :tag "Argument"))
445 :group 'browse-url)
447 (defcustom browse-url-temp-dir temporary-file-directory
448 "The name of a directory for browse-url's temporary files.
449 Such files are generated by functions like `browse-url-of-region'.
450 You might want to set this to somewhere with restricted read permissions
451 for privacy's sake."
452 :type 'string
453 :group 'browse-url)
455 (defcustom browse-url-netscape-version
457 "The version of Netscape you are using.
458 This affects how URL reloading is done; the mechanism changed
459 incompatibly at version 4."
460 :type 'number
461 :group 'browse-url)
463 (defcustom browse-url-lynx-input-field 'avoid
464 "*Action on selecting an existing Lynx buffer at an input field.
465 What to do when sending a new URL to an existing Lynx buffer in Emacs
466 if the Lynx cursor is on an input field (in which case the `g' command
467 would be entered as data). Such fields are recognized by the
468 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
469 user and don't emit the URL, 'avoid: try to avoid the field by moving
470 down (this *won't* always work)."
471 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
472 (const :tag "Disregard" :value nil)
473 (const :tag "Warn, don't emit URL" :value warn))
474 :version "20.3"
475 :group 'browse-url)
477 (defvar browse-url-lynx-input-attempts 10
478 "*How many times to try to move down from a series of lynx input fields.")
480 (defcustom browse-url-lynx-input-delay 0.2
481 "How many seconds to wait for lynx between moves down from an input field.")
483 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
484 ;; URL input
486 (defun browse-url-url-at-point ()
487 (let ((url (thing-at-point 'url)))
488 (set-text-properties 0 (length url) nil url)
489 url))
491 ;; Having this as a separate function called by the browser-specific
492 ;; functions allows them to be stand-alone commands, making it easier
493 ;; to switch between browsers.
495 (defun browse-url-interactive-arg (prompt)
496 "Read a URL from the minibuffer, prompting with PROMPT.
497 Default to the URL at or before point. If invoked with a mouse button,
498 set point to the position clicked first. Return a list for use in
499 `interactive' containing the URL and `browse-url-new-window-flag' or its
500 negation if a prefix argument was given."
501 (let ((event (elt (this-command-keys) 0)))
502 (and (listp event) (mouse-set-point event)))
503 (list (read-string prompt (browse-url-url-at-point))
504 (not (eq (null browse-url-new-window-flag)
505 (null current-prefix-arg)))))
507 ;; interactive-p needs to be called at a function's top-level, hence
508 ;; the macro.
509 (defmacro browse-url-maybe-new-window (arg)
510 `(if (interactive-p)
511 ,arg
512 browse-url-new-window-flag))
514 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
515 ;; Browse current buffer
517 ;;;###autoload
518 (defun browse-url-of-file (&optional file)
519 "Ask a WWW browser to display FILE.
520 Display the current buffer's file if FILE is nil or if called
521 interactively. Turn the filename into a URL with function
522 `browse-url-file-url'. Pass the URL to a browser using the
523 `browse-url' function then run `browse-url-of-file-hook'."
524 (interactive)
525 (or file
526 (setq file (buffer-file-name))
527 (error "Current buffer has no file"))
528 (let ((buf (get-file-buffer file)))
529 (if buf
530 (save-excursion
531 (set-buffer buf)
532 (cond ((not (buffer-modified-p)))
533 (browse-url-save-file (save-buffer))
534 (t (message "%s modified since last save" file))))))
535 (browse-url (browse-url-file-url file))
536 (run-hooks 'browse-url-of-file-hook))
538 (defun browse-url-file-url (file)
539 "Return the URL corresponding to FILE.
540 Use variable `browse-url-filename-alist' to map filenames to URLs."
541 ;; URL-encode special chars, do % first
542 (let ((s 0))
543 (while (setq s (string-match "%" file s))
544 (setq file (replace-match "%25" t t file)
545 s (1+ s))))
546 (while (string-match "[*\"()',=;? ]" file)
547 (let ((enc (format "%%%x" (aref file (match-beginning 0)))))
548 (setq file (replace-match enc t t file))))
549 (dolist (map browse-url-filename-alist)
550 (when (and map (string-match (car map) file))
551 (setq file (replace-match (cdr map) t nil file))))
552 file)
554 ;;;###autoload
555 (defun browse-url-of-buffer (&optional buffer)
556 "Ask a WWW browser to display BUFFER.
557 Display the current buffer if BUFFER is nil. Display only the
558 currently visible part of BUFFER (from a temporary file) if buffer is
559 narrowed."
560 (interactive)
561 (save-excursion
562 (and buffer (set-buffer buffer))
563 (let ((file-name
564 ;; Ignore real name if restricted
565 (and (= (- (point-max) (point-min)) (buffer-size))
566 (or buffer-file-name
567 (and (boundp 'dired-directory) dired-directory)))))
568 (or file-name
569 (progn
570 (or browse-url-temp-file-name
571 (setq browse-url-temp-file-name
572 (convert-standard-filename
573 (make-temp-file
574 (expand-file-name "burl" browse-url-temp-dir)))))
575 (setq file-name browse-url-temp-file-name)
576 (write-region (point-min) (point-max) file-name nil 'no-message)))
577 (browse-url-of-file file-name))))
579 (defun browse-url-delete-temp-file (&optional temp-file-name)
580 ;; Delete browse-url-temp-file-name from the file system
581 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
582 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
583 (if (and file-name (file-exists-p file-name))
584 (delete-file file-name))))
586 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
588 ;;;###autoload
589 (defun browse-url-of-dired-file ()
590 "In Dired, ask a WWW browser to display the file named on this line."
591 (interactive)
592 (browse-url-of-file (dired-get-filename)))
594 ;;;###autoload
595 (defun browse-url-of-region (min max)
596 "Ask a WWW browser to display the current region."
597 (interactive "r")
598 (save-excursion
599 (save-restriction
600 (narrow-to-region min max)
601 (browse-url-of-buffer))))
603 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
604 ;; Browser-independent commands
606 ;; A generic command to call the current browse-url-browser-function
608 ;;;###autoload
609 (defun browse-url (url &rest args)
610 "Ask a WWW browser to load URL.
611 Prompts for a URL, defaulting to the URL at or before point. Variable
612 `browse-url-browser-function' says which browser to use."
613 (interactive (browse-url-interactive-arg "URL: "))
614 (unless (interactive-p)
615 (setq args (or args (list browse-url-new-window-flag))))
616 (if (functionp browse-url-browser-function)
617 (apply browse-url-browser-function url args)
618 ;; The `function' can be an alist; look down it for first match
619 ;; and apply the function (which might be a lambda).
620 (catch 'done
621 (dolist (bf browse-url-browser-function)
622 (when (string-match (car bf) url)
623 (apply (cdr bf) url args)
624 (throw 'done t)))
625 (error "No browse-url-browser-function matching URL %s"
626 url))))
628 ;;;###autoload
629 (defun browse-url-at-point (&optional arg)
630 "Ask a WWW browser to load the URL at or before point.
631 Doesn't let you edit the URL like `browse-url'. Variable
632 `browse-url-browser-function' says which browser to use."
633 (interactive "P")
634 (let ((url (browse-url-url-at-point)))
635 (if url
636 (browse-url url (if arg
637 (not browse-url-new-window-flag)
638 browse-url-new-window-flag))
639 (error "No URL found"))))
641 ;;;###autoload
642 (defun browse-url-at-mouse (event)
643 "Ask a WWW browser to load a URL clicked with the mouse.
644 The URL is the one around or before the position of the mouse click
645 but point is not changed. Doesn't let you edit the URL like
646 `browse-url'. Variable `browse-url-browser-function' says which browser
647 to use."
648 (interactive "e")
649 (save-excursion
650 (mouse-set-point event)
651 (browse-url-at-point browse-url-new-window-flag)))
653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
654 ;; Browser-specific commands
656 ;; --- Default MS-Windows browser ---
658 (defun browse-url-default-windows-browser (url &optional new-window)
659 (interactive (browse-url-interactive-arg "URL: "))
660 (w32-shell-execute "open" url))
662 ;; --- Netscape ---
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))))
670 (if 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")))
682 display)))
684 ;;;###autoload
685 (defun browse-url-netscape (url &optional new-window)
686 "Ask the Netscape WWW browser to load URL.
687 Default to the URL around or before point. The strings in variable
688 `browse-url-netscape-arguments' are also passed to Netscape.
690 When called interactively, if variable `browse-url-new-window-flag' is
691 non-nil, load the document in a new Netscape window, otherwise use a
692 random existing one. A non-nil interactive prefix argument reverses
693 the effect of `browse-url-new-window-flag'.
695 When called non-interactively, optional second argument NEW-WINDOW is
696 used instead of `browse-url-new-window-flag'."
697 (interactive (browse-url-interactive-arg "URL: "))
698 ;; URL encode any `confusing' characters in the URL. This needs to
699 ;; include at least commas; presumably also close parens.
700 (while (string-match "[,)]" url)
701 (setq url (replace-match
702 (format "%%%x" (string-to-char (match-string 0 url))) t t url)))
703 (let* ((process-environment (browse-url-process-environment))
704 (process (apply 'start-process
705 (concat "netscape " url) nil
706 browse-url-netscape-program
707 (append
708 browse-url-netscape-arguments
709 (if (eq window-system 'w32)
710 (list url)
711 (append
712 (if new-window '("-noraise"))
713 (list "-remote"
714 (concat "openURL(" url
715 (if (browse-url-maybe-new-window
716 new-window)
717 ",new-window")
718 ")"))))))))
719 (set-process-sentinel process
720 `(lambda (process change)
721 (browse-url-netscape-sentinel process ,url)))))
723 (defun browse-url-netscape-sentinel (process url)
724 "Handle a change to the process communicating with Netscape."
725 (or (eq (process-exit-status process) 0)
726 (let* ((process-environment (browse-url-process-environment)))
727 ;; Netscape not running - start it
728 (message "Starting Netscape...")
729 (apply 'start-process (concat "netscape" url) nil
730 browse-url-netscape-program
731 (append browse-url-netscape-startup-arguments (list url))))))
733 (defun browse-url-netscape-reload ()
734 "Ask Netscape to reload its current document.
735 How depends on `browse-url-netscape-version'."
736 (interactive)
737 ;; Backwards incompatibility reported by
738 ;; <peter.kruse@psychologie.uni-regensburg.de>.
739 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
740 "xfeDoCommand(reload)"
741 "reload")))
743 (defun browse-url-netscape-send (command)
744 "Send a remote control command to Netscape."
745 (let* ((process-environment (browse-url-process-environment)))
746 (apply 'start-process "netscape" nil
747 browse-url-netscape-program
748 (append browse-url-netscape-arguments
749 (list "-remote" command)))))
751 ;; GNOME means of invoking either Mozilla or Netrape.
753 (defcustom browse-url-gnome-moz-arguments '()
754 "A list of strings passed to the GNOME mozilla viewer as arguments."
755 :version "21.1"
756 :type '(repeat (string :tag "Argument"))
757 :group 'browse-url)
759 ;;;###autoload
760 (defun browse-url-gnome-moz (url &optional new-window)
761 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
762 Default to the URL around or before point. The strings in variable
763 `browse-url-gnome-moz-arguments' are also passed.
765 When called interactively, if variable `browse-url-new-window-flag' is
766 non-nil, load the document in a new browser window, otherwise use an
767 existing one. A non-nil interactive prefix argument reverses the
768 effect of `browse-url-new-window-flag'.
770 When called non-interactively, optional second argument NEW-WINDOW is
771 used instead of `browse-url-new-window-flag'."
772 (interactive (browse-url-interactive-arg "URL: "))
773 (apply 'start-process (concat "gnome-moz-remote " url)
775 "gnome-moz-remote"
776 (append
777 browse-url-gnome-moz-arguments
778 (if (browse-url-maybe-new-window new-window)
779 '("--newwin"))
780 (list "--raise" url))))
782 ;; --- Mosaic ---
784 ;;;###autoload
785 (defun browse-url-mosaic (url &optional new-window)
786 "Ask the XMosaic WWW browser to load URL.
788 Default to the URL around or before point. The strings in variable
789 `browse-url-mosaic-arguments' are also passed to Mosaic and the
790 program is invoked according to the variable
791 `browse-url-mosaic-program'.
793 When called interactively, if variable `browse-url-new-window-flag' is
794 non-nil, load the document in a new Mosaic window, otherwise use a
795 random existing one. A non-nil interactive prefix argument reverses
796 the effect of `browse-url-new-window-flag'.
798 When called non-interactively, optional second argument NEW-WINDOW is
799 used instead of `browse-url-new-window-flag'."
800 (interactive (browse-url-interactive-arg "Mosaic URL: "))
801 (let ((pidfile (expand-file-name "~/.mosaicpid"))
802 pid)
803 (if (file-readable-p pidfile)
804 (save-excursion
805 (find-file pidfile)
806 (goto-char (point-min))
807 (setq pid (read (current-buffer)))
808 (kill-buffer nil)))
809 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
810 (save-excursion
811 (find-file (format "/tmp/Mosaic.%d" pid))
812 (erase-buffer)
813 (insert (if (browse-url-maybe-new-window new-window)
814 "newwin\n"
815 "goto\n")
816 url "\n")
817 (save-buffer)
818 (kill-buffer nil)
819 ;; Send signal SIGUSR to Mosaic
820 (message "Signalling Mosaic...")
821 (signal-process pid browse-url-usr1-signal)
822 ;; Or you could try:
823 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
824 (message "Signalling Mosaic...done")
826 ;; Mosaic not running - start it
827 (message "Starting Mosaic...")
828 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
829 (append browse-url-mosaic-arguments (list url)))
830 (message "Starting Mosaic...done"))))
832 ;; --- Grail ---
834 ;;;###autoload
835 (defvar browse-url-grail
836 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
837 "Location of Grail remote control client script `rcgrail.py'.
838 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
840 ;;;###autoload
841 (defun browse-url-grail (url &optional new-window)
842 "Ask the Grail WWW browser to load URL.
843 Default to the URL around or before point. Runs the program in the
844 variable `browse-url-grail'."
845 (interactive (browse-url-interactive-arg "Grail URL: "))
846 (message "Sending URL to Grail...")
847 (save-excursion
848 (set-buffer (get-buffer-create " *Shell Command Output*"))
849 (erase-buffer)
850 ;; don't worry about this failing.
851 (if new-window
852 (call-process browse-url-grail nil 0 nil "-b" url)
853 (call-process browse-url-grail nil 0 nil url))
854 (message "Sending URL to Grail... done")))
856 ;; --- Mosaic using CCI ---
858 ;;;###autoload
859 (defun browse-url-cci (url &optional new-window)
860 "Ask the XMosaic WWW browser to load URL.
861 Default to the URL around or before point.
863 This function only works for XMosaic version 2.5 or later. You must
864 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
865 value of variable `browse-url-CCI-port', and enable `Accept requests'.
867 When called interactively, if variable `browse-url-new-window-flag' is
868 non-nil, load the document in a new browser window, otherwise use a
869 random existing one. A non-nil interactive prefix argument reverses
870 the effect of `browse-url-new-window-flag'.
872 When called non-interactively, optional second argument NEW-WINDOW is
873 used instead of `browse-url-new-window-flag'."
874 (interactive (browse-url-interactive-arg "Mosaic URL: "))
875 (open-network-stream "browse-url" " *browse-url*"
876 browse-url-CCI-host browse-url-CCI-port)
877 ;; Todo: start browser if fails
878 (process-send-string "browse-url"
879 (concat "get url (" url ") output "
880 (if (browse-url-maybe-new-window new-window)
881 "new"
882 "current")
883 "\r\n"))
884 (process-send-string "browse-url" "disconnect\r\n")
885 (delete-process "browse-url"))
887 ;; --- IXI Mosaic ---
889 ;;;###autoload
890 (defun browse-url-iximosaic (url &optional new-window)
891 ;; new-window ignored
892 "Ask the IXIMosaic WWW browser to load URL.
893 Default to the URL around or before point."
894 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
895 (start-process "tellw3b" nil "tellw3b"
896 "-service WWW_BROWSER ixi_showurl " url))
898 ;; --- W3 ---
900 ;;;###autoload
901 (defun browse-url-w3 (url &optional new-window)
902 "Ask the w3 WWW browser to load URL.
903 Default to the URL around or before point.
905 When called interactively, if variable `browse-url-new-window-flag' is
906 non-nil, load the document in a new window. A non-nil interactive
907 prefix argument reverses the effect of `browse-url-new-window-flag'.
909 When called non-interactively, optional second argument NEW-WINDOW is
910 used instead of `browse-url-new-window-flag'."
911 (interactive (browse-url-interactive-arg "W3 URL: "))
912 (require 'w3) ; w3-fetch-other-window not autoloaded
913 (if (browse-url-maybe-new-window new-window)
914 (w3-fetch-other-window url)
915 (w3-fetch url)))
917 ;;;###autoload
918 (defun browse-url-w3-gnudoit (url &optional new-window)
919 ;; new-window ignored
920 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
921 The `browse-url-gnudoit-program' program is used with options given by
922 `browse-url-gnudoit-args'. Default to the URL around or before point."
923 (interactive (browse-url-interactive-arg "W3 URL: "))
924 (apply 'start-process (concat "gnudoit:" url) nil
925 browse-url-gnudoit-program
926 (append browse-url-gnudoit-args
927 (list (concat "(w3-fetch \"" url "\")")
928 "(raise-frame)"))))
930 ;; --- Lynx in an xterm ---
932 ;;;###autoload
933 (defun browse-url-lynx-xterm (url &optional new-window)
934 ;; new-window ignored
935 "Ask the Lynx WWW browser to load URL.
936 Default to the URL around or before point. A new Lynx process is run
937 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
938 with possible additional arguments `browse-url-xterm-args'."
939 (interactive (browse-url-interactive-arg "Lynx URL: "))
940 (apply #'start-process `(,(concat "lynx" url) nil ,browse-url-xterm-program
941 ,@browse-url-xterm-args "-e" "lynx"
942 ,url)))
944 ;; --- Lynx in an Emacs "term" window ---
946 ;;;###autoload
947 (defun browse-url-lynx-emacs (url &optional new-buffer)
948 "Ask the Lynx WWW browser to load URL.
949 Default to the URL around or before point. With a prefix argument, run
950 a new Lynx process in a new buffer.
952 When called interactively, if variable `browse-url-new-window-flag' is
953 non-nil, load the document in a new lynx in a new term window,
954 otherwise use any existing one. A non-nil interactive prefix argument
955 reverses the effect of `browse-url-new-window-flag'.
957 When called non-interactively, optional second argument NEW-WINDOW is
958 used instead of `browse-url-new-window-flag'."
959 (interactive (browse-url-interactive-arg "Lynx URL: "))
960 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
961 ;; (term-term-name "vt100") ; ??
962 (buf (get-buffer "*lynx*"))
963 (proc (and buf (get-buffer-process buf)))
964 (n browse-url-lynx-input-attempts))
965 (if (and (browse-url-maybe-new-window new-buffer) buf)
966 ;; Rename away the OLD buffer. This isn't very polite, but
967 ;; term insists on working in a buffer named *lynx* and would
968 ;; choke on *lynx*<1>
969 (progn (set-buffer buf)
970 (rename-uniquely)))
971 (if (or (browse-url-maybe-new-window new-buffer)
972 (not buf)
973 (not proc)
974 (not (memq (process-status proc) '(run stop))))
975 ;; start a new lynx
976 (progn
977 (setq buf
978 (apply #'make-term
979 `("lynx" "lynx" nil ,@browse-url-lynx-emacs-args
980 ,url)))
981 (switch-to-buffer buf)
982 (term-char-mode)
983 (set-process-sentinel
984 (get-buffer-process buf)
985 ;; Don't leave around a dead one (especially because of its
986 ;; munged keymap.)
987 (lambda (process event)
988 (if (not (memq (process-status process) '(run stop)))
989 (let ((buf (process-buffer process)))
990 (if buf (kill-buffer buf)))))))
991 ;; send the url to lynx in the old buffer
992 (let ((win (get-buffer-window buf t)))
993 (if win
994 (select-window win)
995 (switch-to-buffer buf)))
996 (if (eq (following-char) ?_)
997 (cond ((eq browse-url-lynx-input-field 'warn)
998 (error "Please move out of the input field first."))
999 ((eq browse-url-lynx-input-field 'avoid)
1000 (while (and (eq (following-char) ?_) (> n 0))
1001 (term-send-down) ; down arrow
1002 (sit-for browse-url-lynx-input-delay))
1003 (if (eq (following-char) ?_)
1004 (error "Cannot move out of the input field, sorry.")))))
1005 (term-send-string proc (concat "g" ; goto
1006 "\C-u" ; kill default url
1008 "\r")))))
1010 ;; --- MMM ---
1012 ;;;###autoload
1013 (defun browse-url-mmm (url &optional new-window)
1014 "Ask the MMM WWW browser to load URL.
1015 Default to the URL around or before point."
1016 (interactive (browse-url-interactive-arg "MMM URL: "))
1017 (message "Sending URL to MMM...")
1018 (save-excursion
1019 (set-buffer (get-buffer-create " *Shell Command Output*"))
1020 (erase-buffer)
1021 ;; mmm_remote just SEGVs if the file isn't there...
1022 (if (or (file-exists-p (expand-file-name "~/.mmm_remote"))
1023 ;; location in v 0.4:
1024 (file-exists-p (expand-file-name "~/.mmm/remote")))
1025 (call-process "mmm_remote" nil 0 nil url)
1026 (call-process "mmm" nil 0 nil "-external" url))
1027 (message "Sending URL to MMM... done")))
1029 ;; --- mailto ---
1031 ;;;###autoload
1032 (defun browse-url-mail (url &optional new-window)
1033 "Open a new mail message buffer within Emacs.
1034 Default to using the mailto: URL around or before point as the
1035 recipient's address. Supplying a non-nil interactive prefix argument
1036 will cause the mail to be composed in another window rather than the
1037 current one.
1039 When called interactively, if variable `browse-url-new-window-flag' is
1040 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1041 non-nil interactive prefix argument reverses the effect of
1042 `browse-url-new-window-flag'.
1044 When called non-interactively, optional second argument NEW-WINDOW is
1045 used instead of `browse-url-new-window-flag'."
1046 (interactive (browse-url-interactive-arg "Mailto URL: "))
1047 (save-excursion
1048 (let ((to (if (string-match "^mailto:" url)
1049 (substring url 7)
1050 url)))
1051 (if (browse-url-maybe-new-window new-window)
1052 (compose-mail-other-window to nil nil nil
1053 (list 'insert-buffer (current-buffer)))
1054 (compose-mail to nil nil nil nil
1055 (list 'insert-buffer (current-buffer)))))))
1057 ;; --- Random browser ---
1059 ;;;###autoload
1060 (defun browse-url-generic (url &optional new-window)
1061 ;; new-window ignored
1062 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1063 Default to the URL around or before point. A fresh copy of the
1064 browser is started up in a new process with possible additional arguments
1065 `browse-url-generic-args'. This is appropriate for browsers which
1066 don't offer a form of remote control."
1067 (interactive (browse-url-interactive-arg "URL: "))
1068 (if (not browse-url-generic-program)
1069 (error "No browser defined (`browse-url-generic-program')"))
1070 (apply 'start-process (concat browse-url-generic-program url) nil
1071 browse-url-generic-program
1072 (append browse-url-generic-args (list url))))
1074 (provide 'browse-url)
1076 ;;; browse-url.el ends here