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