1 ;;; browse-url.el --- ask a WWW browser to load a URL
2 ;; Copyright 1995 Free Software Foundation, Inc.
4 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
5 ;; Maintainer: Denis Howe <dbh@doc.ic.ac.uk>
6 ;; Created: 03 Apr 1995
8 ;; X-Home page: http://wombat.doc.ic.ac.uk/
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
14 ;; by the Free Software Foundation; either version 2, or (at your
15 ;; option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; 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, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; The latest version of this package should be available from
30 ;; <URL:http://wombat.doc.ic.ac.uk/emacs/browse-url.el>.
32 ;; This package provides functions which read a URL (Uniform Resource
33 ;; Locator) from the minibuffer, defaulting to the URL around point,
34 ;; and ask a World-Wide Web browser to load it. It can also load the
35 ;; URL associated with the current buffer. Different browsers use
36 ;; different methods of remote control so there is one function for
37 ;; each supported browser. If the chosen browser is not running, it
38 ;; is started. Currently there is support for:
40 ;; Function Browser Earliest version
41 ;; browse-url-netscape Netscape 1.1b1
42 ;; browse-url-mosaic XMosaic <= 2.4
43 ;; browse-url-cci XMosaic 2.5
45 ;; browse-url-iximosaic IXI Mosaic ?
47 ;; Note that versions of Netscape before 1.1b1 did not have remote
48 ;; control. <URL:http://home.netscape.com/newsref/std/x-remote.html>
49 ;; and <URL:http://home.netscape.com/info/APIs/>.
51 ;; Netscape can cache Web pages so it may be necessary to tell it to
52 ;; reload the current page if it has changed (eg. if you have edited
53 ;; it). There is currently no perfect automatic solution to this.
55 ;; Netscape allows you to specify the id of the window you want to
56 ;; control but which window DO you want to control and how do you
59 ;; If using XMosaic before version 2.5, check the definition of
60 ;; browse-url-usr1-signal below.
61 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/remote-control.html>
63 ;; XMosaic version 2.5 introduced Common Client Interface allowing you
64 ;; to control mosaic through Unix sockets.
65 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/CCI/cci-spec.html>
67 ;; William M. Perry's excellent "w3" WWW browser for
68 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
69 ;; has a function w3-follow-url-at-point, but that
70 ;; doesn't let you edit the URL like browse-url.
72 ;; I recommend Nelson Minar <nelson@santafe.edu>'s excellent
73 ;; html-helper-mode.el for editing HTML and thank Nelson for
74 ;; his many useful comments on this code.
75 ;; <URL:http://www.santafe.edu/~nelson/hhm-beta/>
77 ;; This package generalises function html-previewer-process in Marc
78 ;; Andreessen <marca@ncsa.uiuc.edu>'s html-mode (LCD
79 ;; modes/html-mode.el.Z) and provides better versions of the URL
80 ;; functions in Michelangelo Grigni <mic@cs.ucsd.edu>'s ffap.el
81 ;; (find-file-at-point) <URL:ftp://cs.ucsd.edu:/pub/mic/>. The huge
82 ;; hyperbole package also contains similar functions.
84 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87 ;; Can you write and test some code for the Macintrash and Windoze
88 ;; Netscape remote control APIs? (See the URL above).
90 ;; Do any other browsers have remote control?
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95 ;; Put the following in your ~/.emacs file:
97 ;; (autoload 'browse-url-at-point "browse-url"
98 ;; "Ask a WWW browser to load the URL at or before point." t)
99 ;; (autoload 'browse-url-at-mouse "browse-url"
100 ;; "Ask a WWW browser to load a URL clicked with the mouse." t)
101 ;; (autoload 'browse-url-of-buffer "browse-url"
102 ;; "Ask a WWW browser to display BUFFER." t)
103 ;; (autoload 'browse-url-of-file "browse-url"
104 ;; "Ask a WWW browser to display FILE." t)
105 ;; (autoload 'browse-url-of-dired-file "browse-url"
106 ;; "In Dired, ask a WWW browser to display the file named on this line." t)
108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
111 ;; To display the URL at or before point:
112 ;; M-x browse-url-at-point RET
114 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
116 ;; (global-set-key [S-mouse-1] 'browse-url-at-mouse)
118 ;; To display the current buffer in a web browser:
119 ;; M-x browse-url-of-buffer RET
121 ;; In Dired, to display the file named on the current line:
122 ;; M-x browse-url-of-dired-file RET
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ;; Customisation (~/.emacs)
127 ;; To see what variables are available for customization, type `M-x
128 ;; set-variable browse-url TAB'.
130 ;; To bind the browse-url commands to keys with the `C-c u' prefix:
131 ;; (global-set-key "\C-cu." 'browse-url-at-point)
132 ;; (global-set-key "\C-cub" 'browse-url-of-buffer)
133 ;; (global-set-key "\C-cuf" 'browse-url-of-file)
134 ;; (add-hook 'dired-mode-hook
136 ;; (local-set-key "\C-cuf" 'browse-url-of-dired-file))))
137 ;; (if (boundp 'browse-url-browser-function)
138 ;; (global-set-key "\C-cuu" browse-url-browser-function)
141 ;; '(global-set-key "\C-cuu" browse-url-browser-function)))
143 ;; To use the Emacs w3 browser when not running under X11:
144 ;; (if (not (eq window-system 'x))
145 ;; (setq browse-url-browser-function 'browse-url-w3))
147 ;; To always save modified buffers before displaying the file in a browser:
148 ;; (setq browse-url-save-file t)
150 ;; To get round the Netscape caching problem, you could try either of
151 ;; the following (but not both). EITHER write-file in
152 ;; html-helper-mode makes Netscape reload document:
154 ;; (autoload 'browse-url-netscape-reload "browse-url"
155 ;; "Ask a WWW browser to redisplay the current file." t)
156 ;; (add-hook 'html-helper-mode-hook
157 ;; (function (lambda ()
158 ;; (add-hook 'local-write-file-hooks
159 ;; (function (lambda ()
160 ;; (let ((local-write-file-hooks))
162 ;; (browse-url-netscape-reload)
163 ;; t)) ; => file written by hook
164 ;; t)))) ; append to l-w-f-hooks
166 ;; [Does this work for html-mode too?]
168 ;; OR browse-url-of-file ask Netscape to load and then reload the
171 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
173 ;; You may also want to customise browse-url-netscape-arguments, eg.
175 ;; (setq browse-url-netscape-arguments '("-install"))
177 ;; or similarly for the other browsers.
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182 ;; 0.00 03 Apr 1995 Denis Howe <dbh@doc.ic.ac.uk>
186 ;; All names start with "browse-url-". Added provide.
189 ;; Save file at start of browse-url-of-file.
190 ;; Use start-process instead of start-process-shell-command.
193 ;; Add browse-url-netscape-reload, browse-url-netscape-command.
194 ;; browse-url-of-file save file option.
197 ;; b-u-file-url separate function. Change b-u-filename-alist
201 ;; Added b-u-of-file-hook.
204 ;; Improved .emacs suggestions and documentation.
207 ;; Added browse-url-interactive-arg optional prompt.
210 ;; Exclude final "." from browse-url-regexp.
213 ;; Added mouse-set-point to browse-url-interactive-arg.
216 ;; Added Mosaic signal sending variations.
217 ;; Thanks Brian K Servis <servis@ecn.purdue.edu>.
218 ;; Don't use xprop for Netscape.
221 ;; Fix reading of ~/.mosaicpid. Thanks Dag.H.Wanvik@kvatro.no.
224 ;; Interactive prefix arg => URL *after* point.
225 ;; Thanks Michelangelo Grigni <mic@cs.ucsd.edu>.
226 ;; Added IXI Mosaic support.
227 ;; Thanks David Karr <dkarr@nmo.gtegsc.com>.
230 ;; Exclude final [,;] from browse-url-regexp.
233 ;; Provide browser argument variables.
236 ;; More Netscape options. Thanks Peter Arius
237 ;; <arius@immd2.informatik.uni-erlangen.de>.
240 ;; Added browse-url-at-mouse.
241 ;; Thanks Wayne Mesard <wmesard@sgi.com>
244 ;; Renamed browse-url-at-point to browse-url-url-at-point.
245 ;; Added browse-url-at-point.
246 ;; Thanks Jonathan Cano <cano@patch.tandem.com>.
249 ;; Fixed call to browse-url-url-at-point in browse-url-at-point.
250 ;; Thanks Eric Ding <ericding@San-Jose.ate.slb.com>.
253 ;; Improved documentation.
254 ;; Thanks Kevin Rodgers <kevin.rodgers@ihs.com>.
257 ;; browse-url-of-buffer to handle file-less buffers.
258 ;; browse-url-of-dired-file browses current file in dired.
259 ;; Thanks Kevin Rodgers <kevin.rodgers@ihs.com>.
262 ;; XMosaic CCI functions.
263 ;; Thanks Marc Furrer <Marc.Furrer@di.epfl.ch>.
266 ;; Fixed new-window documentation and added to browse-url-cci.
267 ;; Thanks Dilip Sequeira <djs@dcs.ed.ac.uk>.
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272 (defvar browse-url-regexp
273 "\\(https?://\\|ftp://\\|gopher://\\|telnet://\\|wais://\\|file:/\\|s?news:\\|mailto:\\)[^]\t\n \"'()<>[^`{}]*[^]\t\n \"'()<>[^`{}.,;]+"
274 "A regular expression probably matching a URL.")
276 (defvar browse-url-browser-function
278 "*Function to display the current buffer in a WWW browser.
279 Used by the `browse-url-at-point', `browse-url-at-mouse', and
280 `browse-url-of-file' commands.")
282 (defvar browse-url-netscape-arguments nil
283 "*A list of strings to pass to Netscape as arguments.")
285 (defvar browse-url-new-window-p nil
286 "*If non-nil, always open a new browser window.
287 Passing an interactive argument to \\[browse-url-netscape] or
288 \\[browse-url-cci] reverses the effect of this variable. Requires
289 Netscape version 1.1N or later or XMosaic version 2.5 or later.")
291 (defvar browse-url-mosaic-arguments nil
292 "*A list of strings to pass to Mosaic as arguments.")
294 (defvar browse-url-filename-alist
295 '(("^/+" .
"file:/"))
296 "An alist of (REGEXP . STRING) pairs.
297 Any substring of a filename matching one of the REGEXPs is replaced by
298 the corresponding STRING. All pairs are applied in the order given.
299 Used by the `browse-url-of-file' command.")
301 (defvar browse-url-save-file nil
302 "If non-nil, save the buffer before displaying its file.
303 Used by the `browse-url-of-file' command.")
305 (defvar browse-url-of-file-hook nil
306 "A hook to be run with run-hook after `browse-url-of-file' has asked
307 a browser to load a file.
309 Set this to `browse-url-netscape-reload' to force Netscape to load the
310 file rather than displaying a cached copy.")
312 (defvar browse-url-usr1-signal
313 (if (and (boundp 'emacs-major-version
)
314 (or (> emacs-major-version
19) (>= emacs-minor-version
29)))
316 30) ; Check /usr/include/signal.h.
317 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
318 Emacs 19.29 accepts 'sigusr1, earlier versions require an integer
319 which is 30 on SunOS and 16 on HP-UX and Solaris.")
321 (defvar browse-url-CCI-port
3003
322 "Port to access XMosaic via CCI.
323 This can be any number between 1024 and 65535 but must correspond to
324 the value set in the browser.")
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 ;; thingatpt.el doesn't work for complex regexps.
331 (defun browse-url-url-at-point ()
332 "Return the URL around or before point.
333 Then search backwards for the start of a URL. If no URL found, return
335 (if (or (looking-at browse-url-regexp
) ; Already at start
336 (let ((eol (save-excursion (end-of-line) (point))))
337 ;; Search forwards for the next URL or end of line in case
338 ;; we're in the middle of one.
339 (and (re-search-forward browse-url-regexp eol
'lim
)
340 (goto-char (match-beginning 0)))
341 ;; Now back to where we started or earlier.
342 (re-search-backward browse-url-regexp nil t
)))
343 (buffer-substring (match-beginning 0) (match-end 0))
346 ;; Todo: restrict to around or immediately before point. Expand bare
349 (defun browse-url-interactive-arg (&optional prompt
)
350 "Read a URL from the minibuffer, optionally prompting with PROMPT.
351 Default to the URL at or before point. If bound to a mouse button,
352 set point to the position clicked. Return the result as a list for
353 use in `interactive'."
354 (let ((event (elt (this-command-keys) 0)))
355 (and (listp event
) (mouse-set-point event
)))
356 (list (read-string (or prompt
"URL: ") (browse-url-url-at-point))))
358 (defun browse-url-at-point ()
359 "Ask a WWW browser to load the URL at or before point.
360 The URL is loaded according to the value of `browse-url-browser-function'."
362 (funcall browse-url-browser-function
(browse-url-url-at-point)))
364 (defun browse-url-at-mouse (event)
365 "Ask a WWW browser to load a URL clicked with the mouse.
366 The URL is the one around or before the position of the mouse click
367 but point is not changed. The URL is loaded according to the value of
368 `browse-url-browser-function'."
371 (let ((posn (event-start event
)))
372 (set-buffer (window-buffer (posn-window posn
)))
373 (goto-char (posn-point posn
))
374 (let ((url (browse-url-url-at-point)))
375 (if (string-equal url
"")
376 (error "No URL found"))
377 (funcall browse-url-browser-function url
)))))
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
380 ;; Browse current buffer
382 (defun browse-url-of-file (&optional file
)
383 "Ask a WWW browser to display FILE.
384 Display the current buffer's file if FILE is nil or if called
385 interactively. Turn the filename into a URL by performing
386 replacements given in variable `browse-url-filename-alist'. Pass the
387 URL to a browser using variable `browse-url-browser-function' then run
388 `browse-url-of-file-hook'."
393 (error "Current buffer has no file")))
394 (let ((buf (get-file-buffer file
)))
398 (cond ((not (buffer-modified-p)))
399 (browse-url-save-file (save-buffer))
400 (t (message "%s modified since last save" file
))))))
401 (funcall browse-url-browser-function
(browse-url-file-url file
))
402 (run-hooks 'browse-url-of-file-hook
))
404 (defun browse-url-file-url (file)
405 "Return the URL corresponding to FILE.
406 Uses variable `browse-url-filename-alist' to map filenames to URLs."
407 (let ((maps browse-url-filename-alist
))
409 (let* ((map (car maps
))
411 (to-string (cdr map
)))
412 (setq maps
(cdr maps
))
413 (if (string-match from-re file
)
414 (setq file
(concat (substring file
0 (match-beginning 0))
416 (substring file
(match-end 0))))))))
419 (defvar browse-url-temp-file-name nil
)
420 (make-variable-buffer-local 'browse-url-temp-file-name
)
422 (defvar browse-url-temp-file-list
'())
424 (defun browse-url-of-buffer (&optional buffer
)
425 "Ask a WWW browser to display BUFFER.
426 Display the current buffer if BUFFER is nil."
429 (set-buffer (or buffer
(current-buffer)))
432 (and (boundp 'dired-directory
) dired-directory
))))
435 (if (null browse-url-temp-file-name
)
437 (setq browse-url-temp-file-name
439 (expand-file-name (buffer-name)
440 (or (getenv "TMPDIR") "/tmp"))))
441 (setq browse-url-temp-file-list
442 (cons browse-url-temp-file-name
443 browse-url-temp-file-list
))))
444 (write-region (point-min) (point-max) browse-url-temp-file-name
446 (browse-url-of-file (or file-name browse-url-temp-file-name
)))))
448 (defun browse-url-delete-temp-file (&optional temp-file-name
)
449 ;; Delete browse-url-temp-file-name from the file system and from
450 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
451 ;; non-nil, delete it instead, but only from the file system --
452 ;; browse-url-temp-file-list is not affected.
453 (let ((file-name (or temp-file-name browse-url-temp-file-name
)))
454 (if (and file-name
(file-exists-p file-name
))
456 (delete-file file-name
)
457 (if (null temp-file-name
)
458 (setq browse-url-temp-file-list
459 (delete browse-url-temp-file-name
460 browse-url-temp-file-list
)))))))
462 (defun browse-url-delete-temp-file-list ()
463 ;; Delete all elements of browse-url-temp-file-list.
464 (while browse-url-temp-file-list
465 (browse-url-delete-temp-file (car browse-url-temp-file-list
))
466 (setq browse-url-temp-file-list
467 (cdr browse-url-temp-file-list
))))
469 (add-hook 'kill-buffer-hook
'browse-url-delete-temp-file
)
470 (add-hook 'kill-emacs-hook
'browse-url-delete-temp-file-list
)
472 (defun browse-url-of-dired-file ()
473 "In Dired, ask a WWW browser to display the file named on this line."
475 (browse-url-of-file (dired-get-filename)))
477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
478 ;; Browser-specific functions
480 (defun browse-url-netscape (url &optional new-window
)
481 "Ask the Netscape WWW browser to load URL.
483 Default to the URL around or before point. The strings in variable
484 `browse-url-netscape-arguments' are also passed to Netscape.
486 When called interactively, if variable `browse-url-new-window-p' is
487 non-nil, load the document in a new Netscape window, otherwise use a
488 random existing one. A non-nil interactive prefix argument reverses
489 the effect of browse-url-new-window-p.
491 When called non-interactively, optional second argument NEW-WINDOW is
492 used instead of browse-url-new-window-p."
494 (interactive (append (browse-url-interactive-arg "Netscape URL: ")
495 (list (not (eq (null browse-url-new-window-p
)
496 (null current-prefix-arg
))))))
498 (apply 'call-process
"netscape" nil nil nil
499 (append browse-url-netscape-arguments
500 (if new-window
'("-noraise"))
502 (concat "openURL(" url
503 (if new-window
",new-window")
505 (progn ; Netscape not running - start it
506 (message "Starting Netscape...")
507 (apply 'start-process
"netscape" nil
"netscape"
508 (append browse-url-netscape-arguments
(list url
))))))
510 (defun browse-url-netscape-reload ()
511 "Ask Netscape to reload its current document."
513 (browse-url-netscape-command "reload"))
515 (defun browse-url-netscape-command (command)
516 "Send a remote control command to Netscape."
517 (apply 'start-process
"netscape" nil
"netscape"
518 (append browse-url-netscape-arguments
519 (list "-remote" command
))))
521 (defun browse-url-mosaic (url)
522 "Ask the XMosaic WWW browser to load URL.
523 Default to the URL around or before point."
524 (interactive (browse-url-interactive-arg "Mosaic URL: "))
525 (let ((pidfile (expand-file-name "~/.mosaicpid"))
527 (if (file-readable-p pidfile
)
530 (goto-char (point-min))
531 (setq pid
(read (current-buffer)))
533 (if (and pid
(zerop (signal-process pid
0))) ; Mosaic running
535 (find-file (format "/tmp/Mosaic.%d" pid
))
537 (insert "goto\n" url
"\n")
540 ;; Send signal SIGUSR to Mosaic
541 (message "Signalling Mosaic...")
542 (signal-process pid browse-url-usr1-signal
)
544 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
546 ;; Mosaic not running - start it
547 (message "Starting Mosaic...")
548 (apply 'start-process
"xmosaic" nil
"xmosaic"
549 (append browse-url-mosaic-arguments
(list url
))))))
551 (defun browse-url-cci (url &optional new-window
)
552 "Ask the XMosaic WWW browser to load URL.
553 Default to the URL around or before point.
555 This function only works for XMosaic version 2.5 or later. You must
556 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
557 value of variable `browse-url-CCI-port', and enable `Accept requests'.
559 When called interactively, if variable `browse-url-new-window-p' is
560 non-nil, load the document in a new browser window, otherwise use a
561 random existing one. A non-nil interactive prefix argument reverses
562 the effect of browse-url-new-window-p.
564 When called non-interactively, optional second argument NEW-WINDOW is
565 used instead of browse-url-new-window-p."
566 (interactive (append (browse-url-interactive-arg "Mosaic URL: ")
567 (list (not (eq (null browse-url-new-window-p
)
568 (null current-prefix-arg
))))))
569 (open-network-stream "browse-url" " *browse-url*"
570 "localhost" browse-url-CCI-port
)
571 ;; Todo: start browser if fails
572 (process-send-string "browse-url"
573 (concat "get url (" url
") output "
574 (if new-window
"new" "current") "\r\n"))
575 (process-send-string "browse-url" "disconnect\r\n")
576 (delete-process "browse-url"))
578 (defun browse-url-iximosaic (url)
579 "Ask the IXIMosaic WWW browser to load URL.
580 Default to the URL around or before point."
581 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
582 (start-process "tellw3b" nil
"tellw3b"
583 "-service WWW_BROWSER ixi_showurl " url
))
585 (defun browse-url-w3 (url)
586 "Ask the w3 WWW browser to load URL.
587 Default to the URL around or before point."
588 (interactive (browse-url-interactive-arg "W3 URL: "))
591 (provide 'browse-url
)
593 ;;; browse-url.el ends here