0
|
1 ;;; browse-url.el --- ask a WWW browser to load a URL
|
|
2
|
|
3 ;; Copyright 1995 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
|
|
6 ;; Maintainer: Denis Howe <dbh@doc.ic.ac.uk>
|
|
7 ;; Created: 03 Apr 1995
|
|
8 ;; Version: 0.38 18 Jun 1996
|
|
9 ;; Keywords: hypertext
|
|
10 ;; X-Home page: http://wombat.doc.ic.ac.uk/
|
|
11
|
12
|
12 ;; This file is part of XEmacs.
|
0
|
13
|
12
|
14 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
15 ;; under the terms of the GNU General Public License as published by
|
|
16 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
17 ;; any later version.
|
0
|
18
|
12
|
19 ;; XEmacs is distributed in the hope that it will be useful, but
|
0
|
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
22 ;; General Public License for more details.
|
|
23
|
|
24 ;; You should have received a copy of the GNU General Public License
|
12
|
25 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
26 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
27 ;; 02111-1307, USA.
|
0
|
28
|
|
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
30 ;;; Commentary:
|
|
31
|
|
32 ;; The latest version of this package should be available from
|
|
33 ;; <URL:http://wombat.doc.ic.ac.uk/emacs/browse-url.el>.
|
|
34
|
|
35 ;; This package provides functions which read a URL (Uniform Resource
|
|
36 ;; Locator) from the minibuffer, defaulting to the URL around point,
|
|
37 ;; and ask a World-Wide Web browser to load it. It can also load the
|
|
38 ;; URL associated with the current buffer. Different browsers use
|
|
39 ;; different methods of remote control so there is one function for
|
|
40 ;; each supported browser. If the chosen browser is not running, it
|
|
41 ;; is started. Currently there is support for:
|
|
42
|
|
43 ;; Function Browser Earliest version
|
|
44 ;; browse-url-netscape Netscape 1.1b1
|
|
45 ;; browse-url-mosaic XMosaic <= 2.4
|
|
46 ;; browse-url-cci XMosaic 2.5
|
|
47 ;; browse-url-w3 w3 0
|
|
48 ;; browse-url-iximosaic IXI Mosaic ?
|
|
49 ;; browse-url-lynx-* Lynx 0
|
|
50 ;; browse-url-grail Grail 0.3b1
|
|
51
|
|
52 ;; Note that versions of Netscape before 1.1b1 did not have remote
|
|
53 ;; control. <URL:http://www.netscape.com/newsref/std/x-remote.html>
|
|
54 ;; and <URL:http://www.netscape.com/info/APIs/>.
|
|
55
|
|
56 ;; Netscape can cache Web pages so it may be necessary to tell it to
|
|
57 ;; reload the current page if it has changed (e.g. if you have edited
|
|
58 ;; it). There is currently no perfect automatic solution to this.
|
|
59
|
|
60 ;; Netscape allows you to specify the id of the window you want to
|
|
61 ;; control but which window DO you want to control and how do you
|
|
62 ;; discover its id?
|
|
63
|
|
64 ;; If using XMosaic before version 2.5, check the definition of
|
|
65 ;; browse-url-usr1-signal below.
|
|
66 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/remote-control.html>
|
|
67
|
|
68 ;; XMosaic version 2.5 introduced Common Client Interface allowing you
|
|
69 ;; to control mosaic through Unix sockets.
|
|
70 ;; <URL:http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/CCI/cci-spec.html>
|
|
71
|
|
72 ;; William M. Perry's excellent "w3" WWW browser for
|
|
73 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
|
|
74 ;; has a function w3-follow-url-at-point, but that
|
|
75 ;; doesn't let you edit the URL like browse-url.
|
|
76
|
|
77 ;; I recommend Nelson Minar <nelson@santafe.edu>'s excellent
|
|
78 ;; html-helper-mode.el for editing HTML and thank Nelson for
|
|
79 ;; his many useful comments on this code.
|
|
80 ;; <URL:http://www.santafe.edu/~nelson/hhm-beta/>
|
|
81
|
|
82 ;; This package generalises function html-previewer-process in Marc
|
|
83 ;; Andreessen <marca@ncsa.uiuc.edu>'s html-mode (LCD
|
|
84 ;; modes/html-mode.el.Z) and provides better versions of the URL
|
|
85 ;; functions in Michelangelo Grigni <mic@cs.ucsd.edu>'s ffap.el
|
|
86 ;; (find-file-at-point) <URL:ftp://cs.ucsd.edu:/pub/mic/>. The huge
|
|
87 ;; hyperbole package also contains similar functions.
|
|
88
|
|
89 ;; Grail is the freely available WWW browser implemented in Python, a
|
|
90 ;; cool object-oriented freely available interpreted language. Grail
|
|
91 ;; 0.3b1 was the first version to have remote control as distributed.
|
|
92 ;; For more information on Grail see
|
|
93 ;; <URL:http://monty.cnri.reston.va.us/> and for more information on
|
|
94 ;; Python see <url:http://www.python.org/>. Grail support in
|
|
95 ;; browse-url.el written by Barry Warsaw <bwarsaw@python.org>.
|
|
96
|
|
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
98 ;; Help!
|
|
99
|
|
100 ;; Can you write and test some code for the Macintrash and Windoze
|
|
101 ;; Netscape remote control APIs? (See the URL above).
|
|
102
|
|
103 ;; Do any other browsers have remote control?
|
|
104
|
|
105 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
106 ;; Installation
|
|
107
|
|
108 ;; Put the following in your ~/.emacs file:
|
|
109 ;;
|
|
110 ;; (autoload 'browse-url-at-point "browse-url"
|
|
111 ;; "Ask a WWW browser to load the URL at or before point." t)
|
|
112 ;; (autoload 'browse-url-at-mouse "browse-url"
|
|
113 ;; "Ask a WWW browser to load a URL clicked with the mouse." t)
|
|
114 ;; (autoload 'browse-url-of-buffer "browse-url"
|
|
115 ;; "Ask a WWW browser to display BUFFER." t)
|
|
116 ;; (autoload 'browse-url-of-file "browse-url"
|
|
117 ;; "Ask a WWW browser to display FILE." t)
|
|
118 ;; (autoload 'browse-url-of-dired-file "browse-url"
|
|
119 ;; "In Dired, ask a WWW browser to display the file named on this line." t)
|
|
120
|
|
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
122 ;; Usage
|
|
123
|
|
124 ;; To display the URL at or before point:
|
|
125 ;; M-x browse-url-at-point RET
|
|
126
|
|
127 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
|
|
128 ;; file:
|
|
129 ;; (global-set-key [S-mouse-1] 'browse-url-at-mouse)
|
|
130
|
|
131 ;; To display the current buffer in a web browser:
|
|
132 ;; M-x browse-url-of-buffer RET
|
|
133
|
|
134 ;; In Dired, to display the file named on the current line:
|
|
135 ;; M-x browse-url-of-dired-file RET
|
|
136
|
|
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
138 ;; Customisation (~/.emacs)
|
|
139
|
|
140 ;; To see what variables are available for customization, type
|
|
141 ;; `M-x set-variable browse-url TAB'.
|
|
142
|
|
143 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
|
|
144 ;; (as used by html-helper-mode):
|
|
145 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
|
|
146 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
|
|
147 ;; (global-set-key "\C-c\C-zu" 'browse-url)
|
|
148 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
|
|
149 ;; (add-hook 'dired-mode-hook
|
|
150 ;; (function (lambda ()
|
|
151 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file))))
|
|
152
|
|
153 ;; Browse URLs in mail messages by clicking mouse-2:
|
|
154 ;; (add-hook 'rmail-mode-hook (function (lambda () ; rmail-mode startup
|
|
155 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse))))
|
|
156
|
|
157 ;; Browse URLs in Usenet messages by clicking mouse-2:
|
|
158 ;; (eval-after-load "gnus"
|
|
159 ;; '(define-key gnus-article-mode-map [mouse-2] 'browse-url-at-mouse))
|
|
160
|
|
161 ;; Use the Emacs w3 browser when not running under X11:
|
|
162 ;; (or (eq window-system 'x)
|
|
163 ;; (setq browse-url-browser-function 'browse-url-w3))
|
|
164
|
|
165 ;; To always save modified buffers before displaying the file in a browser:
|
|
166 ;; (setq browse-url-save-file t)
|
|
167
|
|
168 ;; To get round the Netscape caching problem, you could EITHER have
|
|
169 ;; write-file in html-helper-mode make Netscape reload the document:
|
|
170 ;;
|
|
171 ;; (autoload 'browse-url-netscape-reload "browse-url"
|
|
172 ;; "Ask a WWW browser to redisplay the current file." t)
|
|
173 ;; (add-hook 'html-helper-mode-hook
|
|
174 ;; (function (lambda ()
|
|
175 ;; (add-hook 'local-write-file-hooks
|
|
176 ;; (function (lambda ()
|
|
177 ;; (let ((local-write-file-hooks))
|
|
178 ;; (save-buffer))
|
|
179 ;; (browse-url-netscape-reload)
|
|
180 ;; t)) ; => file written by hook
|
|
181 ;; t)))) ; append to l-w-f-hooks
|
|
182 ;;
|
|
183 ;; OR have browse-url-of-file ask Netscape to load and then reload the
|
|
184 ;; file:
|
|
185 ;;
|
|
186 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
|
|
187
|
|
188 ;; You may also want to customise browse-url-netscape-arguments, e.g.
|
|
189 ;; (setq browse-url-netscape-arguments '("-install"))
|
|
190 ;;
|
|
191 ;; or similarly for mosaic.
|
|
192
|
|
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
194 ;;; Change Log:
|
|
195
|
|
196 ;; 0.00 03 Apr 1995 Denis Howe <dbh@doc.ic.ac.uk>
|
|
197 ;; Created.
|
|
198
|
|
199 ;; 0.01 04 Apr 1995
|
|
200 ;; All names start with "browse-url-". Added provide.
|
|
201
|
|
202 ;; 0.02 05 Apr 1995
|
|
203 ;; Save file at start of browse-url-of-file.
|
|
204 ;; Use start-process instead of start-process-shell-command.
|
|
205
|
|
206 ;; 0.03 06 Apr 1995
|
|
207 ;; Add browse-url-netscape-reload, browse-url-netscape-send.
|
|
208 ;; browse-url-of-file save file option.
|
|
209
|
|
210 ;; 0.04 08 Apr 1995
|
|
211 ;; b-u-file-url separate function. Change b-u-filename-alist
|
|
212 ;; default.
|
|
213
|
|
214 ;; 0.05 09 Apr 1995
|
|
215 ;; Added b-u-of-file-hook.
|
|
216
|
|
217 ;; 0.06 11 Apr 1995
|
|
218 ;; Improved .emacs suggestions and documentation.
|
|
219
|
|
220 ;; 0.07 13 Apr 1995
|
|
221 ;; Added browse-url-interactive-arg optional prompt.
|
|
222
|
|
223 ;; 0.08 18 Apr 1995
|
|
224 ;; Exclude final "." from browse-url-regexp.
|
|
225
|
|
226 ;; 0.09 21 Apr 1995
|
|
227 ;; Added mouse-set-point to browse-url-interactive-arg.
|
|
228
|
|
229 ;; 0.10 24 Apr 1995
|
|
230 ;; Added Mosaic signal sending variations.
|
|
231 ;; Thanks Brian K Servis <servis@ecn.purdue.edu>.
|
|
232 ;; Don't use xprop for Netscape.
|
|
233
|
|
234 ;; 0.11 25 Apr 1995
|
|
235 ;; Fix reading of ~/.mosaicpid. Thanks Dag.H.Wanvik@kvatro.no.
|
|
236
|
|
237 ;; 0.12 27 Apr 1995
|
|
238 ;; Interactive prefix arg => URL *after* point.
|
|
239 ;; Thanks Michelangelo Grigni <mic@cs.ucsd.edu>.
|
|
240 ;; Added IXI Mosaic support.
|
|
241 ;; Thanks David Karr <dkarr@nmo.gtegsc.com>.
|
|
242
|
|
243 ;; 0.13 28 Apr 1995
|
|
244 ;; Exclude final [,;] from browse-url-regexp.
|
|
245
|
|
246 ;; 0.14 02 May 1995
|
|
247 ;; Provide browser argument variables.
|
|
248
|
|
249 ;; 0.15 07 May 1995
|
|
250 ;; More Netscape options. Thanks Peter Arius
|
|
251 ;; <arius@immd2.informatik.uni-erlangen.de>.
|
|
252
|
|
253 ;; 0.16 17 May 1995
|
|
254 ;; Added browse-url-at-mouse.
|
|
255 ;; Thanks Wayne Mesard <wmesard@sgi.com>
|
|
256
|
|
257 ;; 0.17 27 Jun 1995
|
|
258 ;; Renamed browse-url-at-point to browse-url-url-at-point.
|
|
259 ;; Added browse-url-at-point.
|
|
260 ;; Thanks Jonathan Cano <cano@patch.tandem.com>.
|
|
261
|
|
262 ;; 0.18 16 Aug 1995
|
|
263 ;; Fixed call to browse-url-url-at-point in browse-url-at-point.
|
|
264 ;; Thanks Eric Ding <ericding@San-Jose.ate.slb.com>.
|
|
265
|
|
266 ;; 0.19 24 Aug 1995
|
|
267 ;; Improved documentation.
|
|
268 ;; Thanks Kevin Rodgers <kevin.rodgers@ihs.com>.
|
|
269
|
|
270 ;; 0.20 31 Aug 1995
|
|
271 ;; browse-url-of-buffer to handle file-less buffers.
|
|
272 ;; browse-url-of-dired-file browses current file in dired.
|
|
273 ;; Thanks Kevin Rodgers <kevin.rodgers@ihs.com>.
|
|
274
|
|
275 ;; 0.21 09 Sep 1995
|
|
276 ;; XMosaic CCI functions.
|
|
277 ;; Thanks Marc Furrer <Marc.Furrer@di.epfl.ch>.
|
|
278
|
|
279 ;; 0.22 13 Sep 1995
|
|
280 ;; Fixed new-window documentation and added to browse-url-cci.
|
|
281 ;; Thanks Dilip Sequeira <djs@dcs.ed.ac.uk>.
|
|
282
|
|
283 ;; 0.23 10 Nov 1995
|
|
284 ;; Added b-u-lynx. Thanks Steven L. Baur <steve@miranova.com>.
|
|
285
|
|
286 ;; 0.24 22 Nov 1995
|
|
287 ;; Renamed b-u-netscape command to b-u-netscape-send.
|
|
288 ;; Added b-u-netscape-command variable.
|
|
289
|
|
290 ;; 0.25 03 Dec 1995
|
|
291 ;; Added event-buffer and event-point for XEmacs compatibility.
|
|
292 ;; Thanks Eric Engstrom <engstrom@src.honeywell.com>
|
|
293
|
|
294 ;; 0.26 13 Jan 1996
|
|
295 ;; Changed b-u-lynx to b-u-lynx-xterm, added b-u-lynx-emacs to
|
|
296 ;; run Lynx in an Emacs buffer under terminal-emulator.
|
|
297 ;; Thanks Jari Aalto <jaalto@tre.tele.nokia.fi>
|
|
298
|
|
299 ;; 0.27 27 Feb 1996
|
|
300 ;; Changed event-buffer and event-point from macros to functions.
|
|
301 ;; Other fixes for byte-compilation.
|
|
302
|
|
303 ;; 0.28 07 Mar 1996
|
|
304 ;; browse-url-lynx-emacs uses term.el instead of terminal.el.
|
|
305
|
|
306 ;; 0.29 13 Mar 1996
|
|
307 ;; Added browse-url-CCI-host. Thanks Greg Marr <gregm@WPI.EDU>.
|
|
308
|
|
309 ;; 0.30 23 Mar 1996
|
|
310 ;; Contact/start Netscape in the background.
|
|
311 ;; Thanks Per Abrahamsen <abraham@dina.kvl.dk>
|
|
312
|
|
313 ;; 0.31 28 Apr 1996
|
|
314 ;; Added browse-url command.
|
|
315 ;; Added new-window logic to b-u-interactive-arg.
|
|
316 ;; b-u-file-url checks for EFS path.
|
|
317
|
|
318 ;; 0.32 02 May 1996
|
|
319 ;; Improved b-u-url-at-point matching to supply missing "http://".
|
|
320
|
|
321 ;; 0.33 01 Jun 1996
|
|
322 ;; Jari Aalto <jaalto@tre.tele.nokia.fi> browse-url-lynx-emacs
|
|
323 ;; fix. Thanks Jari.
|
|
324
|
|
325 ;; 0.34 05 Jun 1996
|
|
326 ;; b-u-file-url checks for EFS after alist. Thanks
|
|
327 ;; Jens-U H Petersen <petersen@kurims.kyoto-u.ac.jp>
|
|
328
|
|
329 ;; 0.35 11 Jun 1996
|
|
330 ;; Grail support. Thanks Barry A. Warsaw
|
|
331 ;; <bwarsaw@anthem.cnri.reston.va.us>.
|
|
332
|
|
333 ;; 0.36 12 Jun 1996
|
|
334 ;; Fixed browse-url-looking-at (I hope).
|
|
335
|
|
336 ;; 0.37 15 Jun 1996
|
|
337 ;; b-u-file-url URL-encodes special chars.
|
|
338 ;; Thanks Martin Schwenke <Martin.Schwenke@cs.anu.edu.au>.
|
|
339
|
|
340 ;; 0.38 17 Jun 1996
|
|
341 ;; b-u-file-url encodes fewer chars. Multi-display support for
|
|
342 ;; Netscape. Thanks Richard Mlynarik <mly@adoc.xerox.com>
|
|
343
|
|
344 ;;; Code:
|
|
345
|
|
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
347 ;; Variables
|
|
348
|
|
349 (eval-when-compile (require 'dired))
|
|
350
|
|
351 (defvar browse-url-path-regexp
|
|
352 "[^]\t\n \"'()<>[^`{}]*[^]\t\n \"'()<>[^`{}.,;]+"
|
|
353 "A regular expression probably matching the host, path or e-mail
|
|
354 part of a URL.")
|
|
355
|
|
356 (defvar browse-url-short-regexp
|
|
357 (concat "[-A-Za-z0-9.]+" browse-url-path-regexp)
|
|
358 "A regular expression probably matching a URL without an access scheme.
|
|
359 Hostname matching is stricter in this case than for
|
|
360 ``browse-url-regexp''.")
|
|
361
|
|
362 (defvar browse-url-regexp
|
|
363 (concat
|
|
364 "\\(https?://\\|ftp://\\|gopher://\\|telnet://\\|wais://\\|file:/\\|s?news:\\|mailto:\\)"
|
|
365 browse-url-path-regexp)
|
|
366 "A regular expression probably matching a complete URL.")
|
|
367
|
|
368
|
|
369 ;;;###autoload
|
|
370 (defvar browse-url-browser-function 'browse-url-w3
|
|
371 "*Function to display the current buffer in a WWW browser.
|
|
372 Used by the `browse-url-at-point', `browse-url-at-mouse', and
|
|
373 `browse-url-of-file' commands.")
|
|
374
|
|
375 (defvar browse-url-netscape-command "netscape"
|
|
376 "*The name by which to invoke Netscape.")
|
|
377
|
|
378 (defvar browse-url-netscape-arguments nil
|
|
379 "*A list of strings to pass to Netscape as arguments.")
|
|
380
|
|
381 (defvar browse-url-new-window-p nil
|
|
382 "*If non-nil, always open a new browser window.
|
|
383 Passing an interactive argument to \\[browse-url-netscape] or
|
|
384 \\[browse-url-cci] reverses the effect of this variable. Requires
|
|
385 Netscape version 1.1N or later or XMosaic version 2.5 or later.")
|
|
386
|
|
387 (defvar browse-url-mosaic-arguments nil
|
|
388 "*A list of strings to pass to Mosaic as arguments.")
|
|
389
|
|
390 (defvar browse-url-filename-alist
|
|
391 '(("^/+" . "file:/"))
|
|
392 "An alist of (REGEXP . STRING) pairs.
|
|
393 Any substring of a filename matching one of the REGEXPs is replaced by
|
|
394 the corresponding STRING. All pairs are applied in the order given.
|
|
395 The default value prepends `file:' to any path beginning with `/'.
|
|
396 Used by the `browse-url-of-file' command.")
|
|
397
|
|
398 (defvar browse-url-save-file nil
|
|
399 "If non-nil, save the buffer before displaying its file.
|
|
400 Used by the `browse-url-of-file' command.")
|
|
401
|
|
402 (defvar browse-url-of-file-hook nil
|
|
403 "A hook to be run with run-hook after `browse-url-of-file' has asked
|
|
404 a browser to load a file.
|
|
405
|
|
406 Set this to `browse-url-netscape-reload' to force Netscape to load the
|
|
407 file rather than displaying a cached copy.")
|
|
408
|
|
409 (defvar browse-url-usr1-signal
|
|
410 (if (and (boundp 'emacs-major-version)
|
|
411 (or (> emacs-major-version 19) (>= emacs-minor-version 29)))
|
|
412 'SIGUSR1 ; Why did I think this was in lower case before?
|
|
413 30) ; Check /usr/include/signal.h.
|
|
414 "The argument to `signal-process' for sending SIGUSR1 to XMosaic.
|
|
415 Emacs 19.29 accepts 'SIGUSR1, earlier versions require an integer
|
|
416 which is 30 on SunOS and 16 on HP-UX and Solaris.")
|
|
417
|
|
418 (defvar browse-url-CCI-port 3003
|
|
419 "Port to access XMosaic via CCI.
|
|
420 This can be any number between 1024 and 65535 but must correspond to
|
|
421 the value set in the browser.")
|
|
422
|
|
423 (defvar browse-url-CCI-host "localhost"
|
|
424 "*Host to access XMosaic via CCI.
|
|
425 This should be the host name of the machine running XMosaic with CCI
|
|
426 enabled. The port number should be set in `browse-url-CCI-port'.")
|
|
427
|
|
428 (defvar browse-url-temp-file-name nil)
|
|
429 (make-variable-buffer-local 'browse-url-temp-file-name)
|
|
430
|
|
431 (defvar browse-url-temp-file-list '())
|
|
432
|
|
433 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
434 ;; URL input
|
|
435
|
|
436 ;; thingatpt.el doesn't work for complex regexps
|
|
437
|
|
438 (defun browse-url-url-at-point ()
|
|
439 "Return the URL around or before point.
|
|
440 Search backwards for the start of a URL ending at or after
|
|
441 point. If no URL found, return the empty string. The
|
|
442 access scheme, `http://' will be prepended if absent."
|
|
443 (cond ((browse-url-looking-at browse-url-regexp)
|
|
444 (buffer-substring (match-beginning 0) (match-end 0)))
|
|
445 ;; Access scheme omitted?
|
|
446 ((browse-url-looking-at browse-url-short-regexp)
|
|
447 (concat "http://"
|
|
448 (buffer-substring (match-beginning 0) (match-end 0))))
|
|
449 (t ""))) ; No match
|
|
450
|
|
451 (defun browse-url-looking-at (regexp)
|
|
452 "Return non-nil if point is in or just after a match for REGEXP.
|
|
453 Set the match data from the earliest such match in the current line
|
|
454 ending at or after point."
|
|
455 (save-excursion
|
|
456 (let ((old-point (point))
|
|
457 (eol (progn (end-of-line) (point)))
|
|
458 (hit nil))
|
|
459 (beginning-of-line)
|
|
460 (or (and (looking-at regexp)
|
|
461 (>= (match-end 0) old-point))
|
|
462 (progn
|
|
463 (while (and (re-search-forward regexp eol t)
|
|
464 (<= (match-beginning 0) old-point)
|
|
465 (not (setq hit (>= (match-end 0) old-point)))))
|
|
466 hit)))))
|
|
467
|
|
468 ;; Having this as a separate function called by the browser-specific
|
|
469 ;; functions allows them to be stand-alone commands, making it easier
|
|
470 ;; to switch between browsers.
|
|
471
|
|
472 (defun browse-url-interactive-arg (prompt)
|
|
473 "Read a URL from the minibuffer, prompting with PROMPT.
|
|
474 Default to the URL at or before point. If invoke with a mouse button,
|
|
475 set point to the position clicked first. Return a list for use in
|
|
476 `interactive' containing the URL and browse-url-new-window-p or its
|
|
477 negation if a prefix argument was given."
|
|
478 (let ((event (elt (this-command-keys) 0)))
|
|
479 (and (listp event) (mouse-set-point event)))
|
|
480 (list (read-string prompt (browse-url-url-at-point))
|
|
481 (not (eq (null browse-url-new-window-p)
|
|
482 (null current-prefix-arg)))))
|
|
483
|
|
484 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
485 ;; Browse current buffer
|
|
486
|
|
487 (defun browse-url-of-file (&optional file)
|
|
488 "Ask a WWW browser to display FILE.
|
|
489 Display the current buffer's file if FILE is nil or if called
|
|
490 interactively. Turn the filename into a URL with function
|
|
491 browse-url-file-url. Pass the URL to a browser using variable
|
|
492 `browse-url-browser-function' then run `browse-url-of-file-hook'."
|
|
493 (interactive)
|
|
494 (or file
|
|
495 (setq file (buffer-file-name))
|
|
496 (error "Current buffer has no file"))
|
|
497 (let ((buf (get-file-buffer file)))
|
|
498 (if buf
|
|
499 (save-excursion
|
|
500 (set-buffer buf)
|
|
501 (cond ((not (buffer-modified-p)))
|
|
502 (browse-url-save-file (save-buffer))
|
|
503 (t (message "%s modified since last save" file))))))
|
|
504 (funcall browse-url-browser-function (browse-url-file-url file))
|
|
505 (run-hooks 'browse-url-of-file-hook))
|
|
506
|
|
507 (defun browse-url-file-url (file)
|
|
508 "Return the URL corresponding to FILE.
|
|
509 Use variable `browse-url-filename-alist' to map filenames to URLs.
|
|
510 Convert EFS file names of the form /USER@HOST:PATH to ftp://HOST/PATH."
|
|
511 ;; URL-encode special chars, do % first
|
|
512 (let ((s 0))
|
|
513 (while (setq s (string-match "%" file s))
|
|
514 (setq file (replace-match "%25" t t file)
|
|
515 s (1+ s))))
|
|
516 (while (string-match "[*\"()',=;? ]" file)
|
|
517 (setq enc (format "%%%x" (aref file (match-beginning 0)))
|
|
518 file (replace-match enc t t file)))
|
|
519 (let ((maps browse-url-filename-alist))
|
|
520 (while maps
|
|
521 (let* ((map (car maps))
|
|
522 (from-re (car map))
|
|
523 (to-string (cdr map)))
|
|
524 (setq maps (cdr maps))
|
|
525 (and (string-match from-re file)
|
|
526 (setq file (replace-match to-string t t file))))))
|
|
527 ;; Check for EFS path
|
|
528 (and (string-match "^/\\([^:@]+@\\)?\\([^:]+\\):/*" file)
|
|
529 (setq file (concat "ftp://"
|
|
530 (substring file (match-beginning 2) (match-end 2))
|
|
531 "/" (substring file (match-end 0)))))
|
|
532 file)
|
|
533
|
|
534 (defun browse-url-of-buffer (&optional buffer)
|
|
535 "Ask a WWW browser to display BUFFER.
|
|
536 Display the current buffer if BUFFER is nil."
|
|
537 (interactive)
|
|
538 (save-excursion
|
|
539 (and buffer (set-buffer buffer))
|
|
540 (let ((file-name
|
|
541 (or buffer-file-name
|
|
542 (and (boundp 'dired-directory) dired-directory))))
|
|
543 (or file-name
|
|
544 (progn
|
|
545 (or browse-url-temp-file-name
|
|
546 (setq browse-url-temp-file-name
|
|
547 (make-temp-name
|
|
548 (expand-file-name (buffer-name)
|
|
549 (or (getenv "TMPDIR") "/tmp")))
|
|
550 browse-url-temp-file-list
|
|
551 (cons browse-url-temp-file-name
|
|
552 browse-url-temp-file-list)))
|
|
553 (setq file-name browse-url-temp-file-name)
|
|
554 (write-region (point-min) (point-max) file-name nil 'no-message)))
|
|
555 (browse-url-of-file file-name))))
|
|
556
|
|
557 (defun browse-url-delete-temp-file (&optional temp-file-name)
|
|
558 ;; Delete browse-url-temp-file-name from the file system and from
|
|
559 ;; browse-url-temp-file-list. If optional arg TEMP-FILE-NAME is
|
|
560 ;; non-nil, delete it instead, but only from the file system --
|
|
561 ;; browse-url-temp-file-list is not affected.
|
|
562 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
|
|
563 (if (and file-name (file-exists-p file-name))
|
|
564 (progn
|
|
565 (delete-file file-name)
|
|
566 (if (null temp-file-name)
|
|
567 (setq browse-url-temp-file-list
|
|
568 (delete browse-url-temp-file-name
|
|
569 browse-url-temp-file-list)))))))
|
|
570
|
|
571 (defun browse-url-delete-temp-file-list ()
|
|
572 ;; Delete all elements of browse-url-temp-file-list.
|
|
573 (while browse-url-temp-file-list
|
|
574 (browse-url-delete-temp-file (car browse-url-temp-file-list))
|
|
575 (setq browse-url-temp-file-list
|
|
576 (cdr browse-url-temp-file-list))))
|
|
577
|
|
578 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
|
|
579 (add-hook 'kill-emacs-hook 'browse-url-delete-temp-file-list)
|
|
580
|
|
581 (defun browse-url-of-dired-file ()
|
|
582 "In Dired, ask a WWW browser to display the file named on this line."
|
|
583 (interactive)
|
|
584 (browse-url-of-file (dired-get-filename)))
|
|
585
|
|
586 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
587 ;; Browser-independant commands
|
|
588
|
|
589 ;; A generic command to call the current b-u-browser-function
|
|
590
|
|
591 (defun browse-url (&rest args)
|
|
592 "Ask a WWW browser to load URL.
|
|
593 Prompts for a URL, defaulting to the URL at or before point. Variable
|
|
594 `browse-url-browser-function' says which browser to use."
|
|
595 (interactive (browse-url-interactive-arg "URL: "))
|
|
596 (apply browse-url-browser-function args))
|
|
597
|
|
598 (defun browse-url-at-point ()
|
|
599 "Ask a WWW browser to load the URL at or before point.
|
|
600 Doesn't let you edit the URL like browse-url. Variable
|
|
601 `browse-url-browser-function' says which browser to use."
|
|
602 (interactive)
|
|
603 (funcall browse-url-browser-function (browse-url-url-at-point)))
|
|
604
|
|
605 ;; Define these if not already defined (XEmacs compatibility)
|
|
606
|
|
607 (eval-and-compile
|
|
608 (or (fboundp 'event-buffer)
|
|
609 (defun event-buffer (event)
|
|
610 (window-buffer (posn-window (event-start event))))))
|
|
611
|
|
612 (eval-and-compile
|
|
613 (or (fboundp 'event-point)
|
|
614 (defun event-point (event)
|
|
615 (posn-point (event-start event)))))
|
|
616
|
|
617 (defun browse-url-at-mouse (event)
|
|
618 "Ask a WWW browser to load a URL clicked with the mouse.
|
|
619 The URL is the one around or before the position of the mouse click
|
|
620 but point is not changed. Doesn't let you edit the URL like
|
|
621 browse-url. Variable `browse-url-browser-function' says which browser
|
|
622 to use."
|
|
623 (interactive "e")
|
|
624 (save-excursion
|
|
625 (set-buffer (event-buffer event))
|
|
626 (goto-char (event-point event))
|
|
627 (let ((url (browse-url-url-at-point)))
|
|
628 (if (string-equal url "")
|
|
629 (error "No URL found"))
|
|
630 (funcall browse-url-browser-function url))))
|
|
631
|
|
632 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
633 ;; Browser-specific commands
|
|
634
|
|
635 ;; --- Netscape ---
|
|
636
|
|
637 ;; Put the correct DISPLAY value in the environment for Netscape
|
|
638 ;; launched from multi-display Emacs.
|
|
639
|
|
640 (defun browse-url-process-environment ()
|
|
641 (let* ((device (and (fboundp 'selected-device)
|
|
642 (fboundp 'device-connection)
|
|
643 (selected-device)))
|
|
644 (display (and device (fboundp 'device-type)
|
|
645 (eq (device-type device) 'x)
|
|
646 (not (equal (device-connection device)
|
|
647 (getenv "DISPLAY"))))))
|
|
648 (if display
|
|
649 ;; Attempt to run on the correct display
|
|
650 (cons (concat "DISPLAY=" (device-connection device))
|
|
651 process-environment)
|
|
652 process-environment)))
|
|
653
|
|
654
|
|
655 ;;;###autoload
|
|
656 (defun browse-url-netscape (url &optional new-window)
|
|
657 "Ask the Netscape WWW browser to load URL.
|
|
658
|
|
659 Default to the URL around or before point. The strings in variable
|
|
660 `browse-url-netscape-arguments' are also passed to Netscape.
|
|
661
|
|
662 When called interactively, if variable `browse-url-new-window-p' is
|
|
663 non-nil, load the document in a new Netscape window, otherwise use a
|
|
664 random existing one. A non-nil interactive prefix argument reverses
|
|
665 the effect of browse-url-new-window-p.
|
|
666
|
|
667 When called non-interactively, optional second argument NEW-WINDOW is
|
|
668 used instead of browse-url-new-window-p."
|
|
669 (interactive (browse-url-interactive-arg "Netscape URL: "))
|
|
670 (let* ((process-environment (browse-url-process-environment))
|
|
671 (process (apply 'start-process
|
|
672 (concat "netscape " url) nil
|
|
673 browse-url-netscape-command
|
|
674 (append browse-url-netscape-arguments
|
|
675 (if new-window '("-noraise"))
|
|
676 (list "-remote"
|
|
677 (concat "openURL(" url
|
|
678 (if new-window ",new-window")
|
|
679 ")"))))))
|
|
680 (set-process-sentinel process
|
|
681 (list 'lambda '(process change)
|
|
682 (list 'browse-url-netscape-sentinel 'process url)))))
|
|
683
|
|
684 (defun browse-url-netscape-sentinel (process url)
|
|
685 "Handle a change to the process communicating with Netscape."
|
|
686 (or (eq (process-exit-status process) 0)
|
|
687 (let* ((process-environment (browse-url-process-environment)))
|
|
688 ;; Netscape not running - start it
|
|
689 (message "Starting Netscape...")
|
|
690 (apply 'start-process (concat "netscape" url) nil
|
|
691 browse-url-netscape-command
|
|
692 (append browse-url-netscape-arguments (list url))))))
|
|
693
|
|
694 (defun browse-url-netscape-reload ()
|
|
695 "Ask Netscape to reload its current document."
|
|
696 (interactive)
|
|
697 (browse-url-netscape-send "reload"))
|
|
698
|
|
699 (defun browse-url-netscape-send (command)
|
|
700 "Send a remote control command to Netscape."
|
|
701 (let* ((process-environment (browse-url-process-environment)))
|
|
702 (apply 'start-process "netscape" nil
|
|
703 browse-url-netscape-command
|
|
704 (append browse-url-netscape-arguments
|
|
705 (list "-remote" command)))))
|
|
706
|
|
707 ;; --- Mosaic ---
|
|
708
|
|
709 ;;;###autoload
|
|
710 (defun browse-url-mosaic (url &optional new-window)
|
|
711 ;; new-window ignored
|
|
712 "Ask the XMosaic WWW browser to load URL.
|
|
713 Default to the URL around or before point."
|
|
714 (interactive (browse-url-interactive-arg "Mosaic URL: "))
|
|
715 (let ((pidfile (expand-file-name "~/.mosaicpid"))
|
|
716 pid pidbuf)
|
|
717 (if (file-readable-p pidfile)
|
|
718 (save-excursion
|
|
719 (find-file pidfile)
|
|
720 (goto-char (point-min))
|
|
721 (setq pid (read (current-buffer)))
|
|
722 (kill-buffer nil)))
|
|
723 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
|
|
724 (save-excursion
|
|
725 (find-file (format "/tmp/Mosaic.%d" pid))
|
|
726 (erase-buffer)
|
|
727 (insert "goto\n" url "\n")
|
|
728 (save-buffer)
|
|
729 (kill-buffer nil)
|
|
730 ;; Send signal SIGUSR to Mosaic
|
|
731 (message "Signalling Mosaic...")
|
|
732 (signal-process pid browse-url-usr1-signal)
|
|
733 ;; Or you could try:
|
|
734 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
|
|
735 (message "Signalling Mosaic...done")
|
|
736 )
|
|
737 ;; Mosaic not running - start it
|
|
738 (message "Starting Mosaic...")
|
|
739 (apply 'start-process "xmosaic" nil "xmosaic"
|
|
740 (append browse-url-mosaic-arguments (list url)))
|
|
741 (message "Starting Mosaic...done"))))
|
|
742
|
|
743 ;; --- Grail ---
|
|
744
|
|
745 (defvar browse-url-grail
|
|
746 (concat (or (getenv "GRAILDIR") "~/.grail") "/user/rcgrail.py")
|
|
747 "*Location of Grail remote control client script `rcgrail.py'.
|
|
748 Typically found in $GRAILDIR/rcgrail.py, or ~/.grail/user/rcgrail.py.")
|
|
749
|
|
750 ;;;###autoload
|
|
751 (defun browse-url-grail (url)
|
|
752 "Ask the Grail WWW browser to load URL.
|
|
753 Default to the URL around or before point. Runs the program in the
|
|
754 variable `browse-url-grail'."
|
|
755 (interactive (browse-url-interactive-arg "Grail URL: "))
|
|
756 (message "Sending URL to Grail...")
|
|
757 (save-excursion
|
|
758 (set-buffer (get-buffer-create " *Shell Command Output*"))
|
|
759 (erase-buffer)
|
|
760 ;; don't worry about this failing.
|
|
761 (call-process browse-url-grail nil 0 nil url)
|
|
762 (message "Sending URL to Grail... done")))
|
|
763
|
|
764 ;; --- Mosaic using CCI ---
|
|
765
|
|
766 (defun browse-url-cci (url &optional new-window)
|
|
767 "Ask the XMosaic WWW browser to load URL.
|
|
768 Default to the URL around or before point.
|
|
769
|
|
770 This function only works for XMosaic version 2.5 or later. You must
|
|
771 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
|
|
772 value of variable `browse-url-CCI-port', and enable `Accept requests'.
|
|
773
|
|
774 When called interactively, if variable `browse-url-new-window-p' is
|
|
775 non-nil, load the document in a new browser window, otherwise use a
|
|
776 random existing one. A non-nil interactive prefix argument reverses
|
|
777 the effect of browse-url-new-window-p.
|
|
778
|
|
779 When called non-interactively, optional second argument NEW-WINDOW is
|
|
780 used instead of browse-url-new-window-p."
|
|
781 (interactive (browse-url-interactive-arg "Mosaic URL: "))
|
|
782 (open-network-stream "browse-url" " *browse-url*"
|
|
783 browse-url-CCI-host browse-url-CCI-port)
|
|
784 ;; Todo: start browser if fails
|
|
785 (process-send-string "browse-url"
|
|
786 (concat "get url (" url ") output "
|
|
787 (if new-window "new" "current") "\r\n"))
|
|
788 (process-send-string "browse-url" "disconnect\r\n")
|
|
789 (delete-process "browse-url"))
|
|
790
|
|
791 ;; --- IXI Mosaic ---
|
|
792
|
|
793 ;;;###autoload
|
|
794 (defun browse-url-iximosaic (url &optional new-window)
|
|
795 ;; new-window ignored
|
|
796 "Ask the IXIMosaic WWW browser to load URL.
|
|
797 Default to the URL around or before point."
|
|
798 (interactive (browse-url-interactive-arg "IXI Mosaic URL: "))
|
|
799 (start-process "tellw3b" nil "tellw3b"
|
|
800 "-service WWW_BROWSER ixi_showurl " url))
|
|
801
|
|
802 ;; --- W3 ---
|
|
803
|
|
804 ;;;###autoload
|
|
805 (defun browse-url-w3 (url &optional new-window)
|
|
806 ;; new-window ignored
|
|
807 "Ask the w3 WWW browser to load URL.
|
|
808 Default to the URL around or before point."
|
|
809 (interactive (browse-url-interactive-arg "W3 URL: "))
|
|
810 (w3-fetch url))
|
|
811
|
|
812 ;; --- Lynx in an xterm ---
|
|
813
|
|
814 ;;;###autoload
|
|
815 (defun browse-url-lynx-xterm (url &optional new-window)
|
|
816 ;; new-window ignored
|
|
817 "Ask the Lynx WWW browser to load URL.
|
|
818 Default to the URL around or before point. A new Lynx process is run
|
|
819 in an Xterm window."
|
|
820 (interactive (browse-url-interactive-arg "Lynx URL: "))
|
|
821 (start-process (concat "lynx" url) nil "xterm" "-e" "lynx" url))
|
|
822
|
|
823 (eval-when-compile (require 'term))
|
|
824
|
|
825 ;; --- Lynx in an Emacs "term" window ---
|
|
826
|
|
827 ;;;###autoload
|
|
828 (defun browse-url-lynx-emacs (url &optional new-window)
|
|
829 ;; new-window ignored
|
|
830 "Ask the Lynx WWW browser to load URL.
|
|
831 Default to the URL around or before point. Run a new Lynx process in
|
|
832 an Emacs buffer."
|
|
833 (interactive (browse-url-interactive-arg "Lynx URL: "))
|
|
834 (let ((system-uses-terminfo t)) ; Lynx uses terminfo
|
|
835 (if (fboundp 'make-term)
|
|
836 (let ((term-term-name "vt100"))
|
|
837 (set-buffer (make-term "browse-url" "lynx" nil url))
|
|
838 (term-mode)
|
|
839 (term-char-mode)
|
|
840 (switch-to-buffer "*browse-url*"))
|
|
841 (terminal-emulator "*browse-url*" "lynx" (list url)))))
|
|
842
|
|
843 (provide 'browse-url)
|
|
844
|
|
845 ;;; browse-url.el ends here
|