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