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