]> code.delx.au - gnu-emacs/blob - lisp/net/browse-url.el
Add rx.el support for numbered groups (Bug#8776).
[gnu-emacs] / lisp / net / browse-url.el
1 ;;; browse-url.el --- pass a URL to a WWW browser
2
3 ;; Copyright (C) 1995-2011 Free Software Foundation, Inc.
4
5 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
6 ;; Maintainer: FSF
7 ;; Created: 03 Apr 1995
8 ;; Keywords: hypertext, hypermedia, mouse
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This package provides functions which read a URL (Uniform Resource
28 ;; Locator) from the minibuffer, defaulting to the URL around point,
29 ;; and ask a World-Wide Web browser to load it. It can also load the
30 ;; URL associated with the current buffer. Different browsers use
31 ;; different methods of remote control so there is one function for
32 ;; each supported browser. If the chosen browser is not running, it
33 ;; is started. Currently there is support for the following browsers,
34 ;; some of them probably now obsolete:
35
36 ;; Function Browser Earliest version
37 ;; browse-url-mozilla Mozilla Don't know
38 ;; browse-url-firefox Firefox Don't know (tried with 1.0.1)
39 ;; browse-url-galeon Galeon Don't know
40 ;; browse-url-epiphany Epiphany Don't know
41 ;; browse-url-netscape Netscape 1.1b1
42 ;; browse-url-mosaic XMosaic/mMosaic <= 2.4
43 ;; browse-url-cci XMosaic 2.5
44 ;; browse-url-w3 w3 0
45 ;; browse-url-w3-gnudoit w3 remotely
46 ;; browse-url-text-* Any text browser 0
47 ;; browse-url-generic arbitrary
48 ;; browse-url-default-windows-browser MS-Windows browser
49 ;; browse-url-default-macosx-browser Mac OS X browser
50 ;; browse-url-gnome-moz GNOME interface to Mozilla
51 ;; browse-url-kde KDE konqueror (kfm)
52 ;; browse-url-elinks Elinks Don't know (tried with 0.12.GIT)
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 ;; William M. Perry's excellent "w3" WWW browser for
70 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
71 ;; has a function w3-follow-url-at-point, but that
72 ;; doesn't let you edit the URL like browse-url.
73 ;; The `gnuserv' package that can be used to control it in another
74 ;; Emacs process is available from
75 ;; <URL:ftp://ftp.splode.com/pub/users/friedman/packages/>.
76
77 ;; Lynx is now distributed by the FSF. See also
78 ;; <URL:http://lynx.browser.org/>.
79
80 ;; Free graphical browsers that could be used by `browse-url-generic'
81 ;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
82 ;; <URL:http://www.unlv.edu/chimera/>, Arena
83 ;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena> and Amaya
84 ;; <URL:ftp://ftp.w3.org/pub/amaya>. mMosaic
85 ;; <URL:ftp://ftp.enst.fr/pub/mbone/mMosaic/>,
86 ;; <URL:http://www.enst.fr/~dauphin/mMosaic/> (with development
87 ;; support for Java applets and multicast) can be used like Mosaic by
88 ;; setting `browse-url-mosaic-program' appropriately.
89
90 ;; I [Denis Howe, not Dave Love] recommend Nelson Minar
91 ;; <nelson@santafe.edu>'s excellent html-helper-mode.el for editing
92 ;; HTML and thank Nelson for his many useful comments on this code.
93 ;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>
94
95 ;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
96 ;; software/own/hm--html-menus/>. For composing correct HTML see also
97 ;; PSGML the general SGML structure editor package
98 ;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
99 ;; with this.
100
101 ;; This package generalises function html-previewer-process in Marc
102 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
103 ;; ffap.el package. The huge hyperbole package also contains similar
104 ;; functions.
105
106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107 ;; Help!
108
109 ;; Can you write and test some code for the Macintrash and Windoze
110 ;; Netscape remote control APIs? (See the URL above).
111
112 ;; Do any other browsers have remote control?
113
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
115 ;; Usage
116
117 ;; To display the URL at or before point:
118 ;; M-x browse-url-at-point RET
119 ;; or, similarly but with the opportunity to edit the URL extracted from
120 ;; the buffer, use:
121 ;; M-x browse-url
122
123 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
124 ;; file:
125 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
126 ;; (Note that using Shift-mouse-1 is not desirable because
127 ;; that event has a standard meaning in Emacs.)
128
129 ;; To display the current buffer in a web browser:
130 ;; M-x browse-url-of-buffer RET
131
132 ;; To display the current region in a web browser:
133 ;; M-x browse-url-of-region RET
134
135 ;; In Dired, to display the file named on the current line:
136 ;; M-x browse-url-of-dired-file RET
137
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
139 ;; Customisation (~/.emacs)
140
141 ;; To see what variables are available for customization, type
142 ;; `M-x set-variable browse-url TAB'. Better, use
143 ;; `M-x customize-group browse-url'.
144
145 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
146 ;; (as used by html-helper-mode):
147 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
148 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
149 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
150 ;; (global-set-key "\C-c\C-zu" 'browse-url)
151 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
152 ;; (add-hook 'dired-mode-hook
153 ;; (lambda ()
154 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
155
156 ;; Browse URLs in mail messages under RMAIL by clicking mouse-2:
157 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
158 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
159 ;; Alternatively, add `goto-address' to `rmail-show-message-hook'.
160
161 ;; Gnus provides a standard feature to activate URLs in article
162 ;; buffers for invocation of browse-url.
163
164 ;; Use the Emacs w3 browser when not running under X11:
165 ;; (or (eq window-system 'x)
166 ;; (setq browse-url-browser-function 'browse-url-w3))
167
168 ;; To always save modified buffers before displaying the file in a browser:
169 ;; (setq browse-url-save-file t)
170
171 ;; To get round the Netscape caching problem, you could EITHER have
172 ;; write-file in html-helper-mode make Netscape reload the document:
173 ;;
174 ;; (autoload 'browse-url-netscape-reload "browse-url"
175 ;; "Ask a WWW browser to redisplay the current file." t)
176 ;; (add-hook 'html-helper-mode-hook
177 ;; (lambda ()
178 ;; (add-hook 'local-write-file-hooks
179 ;; (lambda ()
180 ;; (let ((local-write-file-hooks))
181 ;; (save-buffer))
182 ;; (browse-url-netscape-reload)
183 ;; t) ; => file written by hook
184 ;; t))) ; append to l-w-f-hooks
185 ;;
186 ;; OR have browse-url-of-file ask Netscape to load and then reload the
187 ;; file:
188 ;;
189 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
190
191 ;; You may also want to customise browse-url-netscape-arguments, e.g.
192 ;; (setq browse-url-netscape-arguments '("-install"))
193 ;;
194 ;; or similarly for the other browsers.
195
196 ;; To invoke different browsers for different URLs:
197 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
198 ;; ("." . browse-url-netscape)))
199
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 ;;; Code:
202
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 ;; Variables
205
206 (eval-when-compile (require 'cl))
207
208 (defgroup browse-url nil
209 "Use a web browser to look at a URL."
210 :prefix "browse-url-"
211 :link '(emacs-commentary-link "browse-url")
212 :group 'external
213 :group 'comm)
214
215 ;;;###autoload
216 (defcustom browse-url-browser-function
217 (cond
218 ((memq system-type '(windows-nt ms-dos cygwin))
219 'browse-url-default-windows-browser)
220 ((memq system-type '(darwin))
221 'browse-url-default-macosx-browser)
222 (t
223 'browse-url-default-browser))
224 "Function to display the current buffer in a WWW browser.
225 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
226 `browse-url-of-file' commands.
227
228 If the value is not a function it should be a list of pairs
229 \(REGEXP . FUNCTION). In this case the function called will be the one
230 associated with the first REGEXP which matches the current URL. The
231 function is passed the URL and any other args of `browse-url'. The last
232 regexp should probably be \".\" to specify a default browser."
233 :type '(choice
234 (function-item :tag "Emacs W3" :value browse-url-w3)
235 (function-item :tag "W3 in another Emacs via `gnudoit'"
236 :value browse-url-w3-gnudoit)
237 (function-item :tag "Mozilla" :value browse-url-mozilla)
238 (function-item :tag "Firefox" :value browse-url-firefox)
239 (function-item :tag "Galeon" :value browse-url-galeon)
240 (function-item :tag "Epiphany" :value browse-url-epiphany)
241 (function-item :tag "Netscape" :value browse-url-netscape)
242 (function-item :tag "Mosaic" :value browse-url-mosaic)
243 (function-item :tag "Mosaic using CCI" :value browse-url-cci)
244 (function-item :tag "Text browser in an xterm window"
245 :value browse-url-text-xterm)
246 (function-item :tag "Text browser in an Emacs window"
247 :value browse-url-text-emacs)
248 (function-item :tag "KDE" :value browse-url-kde)
249 (function-item :tag "Elinks" :value browse-url-elinks)
250 (function-item :tag "Specified by `Browse Url Generic Program'"
251 :value browse-url-generic)
252 (function-item :tag "Default Windows browser"
253 :value browse-url-default-windows-browser)
254 (function-item :tag "Default Mac OS X browser"
255 :value browse-url-default-macosx-browser)
256 (function-item :tag "GNOME invoking Mozilla"
257 :value browse-url-gnome-moz)
258 (function-item :tag "Default browser"
259 :value browse-url-default-browser)
260 (function :tag "Your own function")
261 (alist :tag "Regexp/function association list"
262 :key-type regexp :value-type function))
263 :version "24.1"
264 :group 'browse-url)
265
266 (defcustom browse-url-mailto-function 'browse-url-mail
267 "Function to display mailto: links.
268 This variable uses the same syntax as the
269 `browse-url-browser-function' variable. If the
270 `browse-url-mailto-function' variable is nil, that variable will
271 be used instead."
272 :type '(choice
273 (function-item :tag "Emacs Mail" :value browse-url-mail)
274 (function-item :tag "None" nil))
275 :version "24.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 (defcustom browse-url-browser-display nil
303 "The X display for running the browser, if not same as Emacs'."
304 :type '(choice string (const :tag "Default" nil))
305 :group 'browse-url)
306
307 (defcustom browse-url-mozilla-program "mozilla"
308 "The name by which to invoke Mozilla."
309 :type 'string
310 :group 'browse-url)
311
312 (defcustom browse-url-mozilla-arguments nil
313 "A list of strings to pass to Mozilla as arguments."
314 :type '(repeat (string :tag "Argument"))
315 :group 'browse-url)
316
317 (defcustom browse-url-mozilla-startup-arguments browse-url-mozilla-arguments
318 "A list of strings to pass to Mozilla when it starts up.
319 Defaults to the value of `browse-url-mozilla-arguments' at the time
320 `browse-url' is loaded."
321 :type '(repeat (string :tag "Argument"))
322 :group 'browse-url)
323
324 (defcustom browse-url-firefox-program
325 (let ((candidates '("firefox" "iceweasel")))
326 (while (and candidates (not (executable-find (car candidates))))
327 (setq candidates (cdr candidates)))
328 (or (car candidates) "firefox"))
329 "The name by which to invoke Firefox."
330 :type 'string
331 :group 'browse-url)
332
333 (defcustom browse-url-firefox-arguments nil
334 "A list of strings to pass to Firefox as arguments."
335 :type '(repeat (string :tag "Argument"))
336 :group 'browse-url)
337
338 (defcustom browse-url-firefox-startup-arguments browse-url-firefox-arguments
339 "A list of strings to pass to Firefox when it starts up.
340 Defaults to the value of `browse-url-firefox-arguments' at the time
341 `browse-url' is loaded."
342 :type '(repeat (string :tag "Argument"))
343 :group 'browse-url)
344
345 (defcustom browse-url-galeon-program "galeon"
346 "The name by which to invoke Galeon."
347 :type 'string
348 :group 'browse-url)
349
350 (defcustom browse-url-galeon-arguments nil
351 "A list of strings to pass to Galeon as arguments."
352 :type '(repeat (string :tag "Argument"))
353 :group 'browse-url)
354
355 (defcustom browse-url-galeon-startup-arguments browse-url-galeon-arguments
356 "A list of strings to pass to Galeon when it starts up.
357 Defaults to the value of `browse-url-galeon-arguments' at the time
358 `browse-url' is loaded."
359 :type '(repeat (string :tag "Argument"))
360 :group 'browse-url)
361
362 (defcustom browse-url-epiphany-program "epiphany"
363 "The name by which to invoke Epiphany."
364 :type 'string
365 :group 'browse-url)
366
367 (defcustom browse-url-epiphany-arguments nil
368 "A list of strings to pass to Epiphany as arguments."
369 :type '(repeat (string :tag "Argument"))
370 :group 'browse-url)
371
372 (defcustom browse-url-epiphany-startup-arguments browse-url-epiphany-arguments
373 "A list of strings to pass to Epiphany when it starts up.
374 Defaults to the value of `browse-url-epiphany-arguments' at the time
375 `browse-url' is loaded."
376 :type '(repeat (string :tag "Argument"))
377 :group 'browse-url)
378
379 ;; GNOME means of invoking either Mozilla or Netrape.
380 (defvar browse-url-gnome-moz-program "gnome-moz-remote")
381
382 (defcustom browse-url-gnome-moz-arguments '()
383 "A list of strings passed to the GNOME mozilla viewer as arguments."
384 :version "21.1"
385 :type '(repeat (string :tag "Argument"))
386 :group 'browse-url)
387
388 (defcustom browse-url-mozilla-new-window-is-tab nil
389 "Whether to open up new windows in a tab or a new window.
390 If non-nil, then open the URL in a new tab rather than a new window if
391 `browse-url-mozilla' is asked to open it in a new window."
392 :type 'boolean
393 :group 'browse-url)
394
395 (defcustom browse-url-firefox-new-window-is-tab nil
396 "Whether to open up new windows in a tab or a new window.
397 If non-nil, then open the URL in a new tab rather than a new window if
398 `browse-url-firefox' is asked to open it in a new window.
399
400 This option is currently ignored on MS-Windows, since the necessary
401 functionality is not available there."
402 :type 'boolean
403 :group 'browse-url)
404
405 (defcustom browse-url-galeon-new-window-is-tab nil
406 "Whether to open up new windows in a tab or a new window.
407 If non-nil, then open the URL in a new tab rather than a new window if
408 `browse-url-galeon' is asked to open it in a new window."
409 :type 'boolean
410 :group 'browse-url)
411
412 (defcustom browse-url-epiphany-new-window-is-tab nil
413 "Whether to open up new windows in a tab or a new window.
414 If non-nil, then open the URL in a new tab rather than a new window if
415 `browse-url-epiphany' is asked to open it in a new window."
416 :type 'boolean
417 :group 'browse-url)
418
419 (defcustom browse-url-netscape-new-window-is-tab nil
420 "Whether to open up new windows in a tab or a new window.
421 If non-nil, then open the URL in a new tab rather than a new
422 window if `browse-url-netscape' is asked to open it in a new
423 window."
424 :type 'boolean
425 :group 'browse-url)
426
427 (defcustom browse-url-new-window-flag nil
428 "Non-nil means always open a new browser window with appropriate browsers.
429 Passing an interactive argument to \\[browse-url], or specific browser
430 commands reverses the effect of this variable. Requires Netscape version
431 1.1N or later or XMosaic version 2.5 or later if using those browsers."
432 :type 'boolean
433 :group 'browse-url)
434
435 (defcustom browse-url-mosaic-program "xmosaic"
436 "The name by which to invoke Mosaic (or mMosaic)."
437 :type 'string
438 :version "20.3"
439 :group 'browse-url)
440
441 (defcustom browse-url-mosaic-arguments nil
442 "A list of strings to pass to Mosaic as arguments."
443 :type '(repeat (string :tag "Argument"))
444 :group 'browse-url)
445
446 (defcustom browse-url-mosaic-pidfile "~/.mosaicpid"
447 "The name of the pidfile created by Mosaic."
448 :type 'string
449 :group 'browse-url)
450
451 (defcustom browse-url-filename-alist
452 `(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
453 ;; The above loses the username to avoid the browser prompting for
454 ;; it in anonymous cases. If it's not anonymous the next regexp
455 ;; applies.
456 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
457 ,@(if (memq system-type '(windows-nt ms-dos cygwin))
458 '(("^\\([a-zA-Z]:\\)[\\/]" . "file:///\\1/")
459 ("^[\\/][\\/]+" . "file://")))
460 ("^/+" . "file:///"))
461 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
462 Any substring of a filename matching one of the REGEXPs is replaced by
463 the corresponding STRING using `replace-match', not treating STRING
464 literally. All pairs are applied in the order given. The default
465 value converts ange-ftp/EFS-style file names into ftp URLs and prepends
466 `file:' to any file name beginning with `/'.
467
468 For example, adding to the default a specific translation of an ange-ftp
469 address to an HTTP URL:
470
471 (setq browse-url-filename-alist
472 '((\"/webmaster@webserver:/home/www/html/\" .
473 \"http://www.acme.co.uk/\")
474 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
475 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
476 (\"^/+\" . \"file:/\")))"
477 :type '(repeat (cons :format "%v"
478 (regexp :tag "Regexp")
479 (string :tag "Replacement")))
480 :version "23.1"
481 :group 'browse-url)
482
483 (defcustom browse-url-save-file nil
484 "If non-nil, save the buffer before displaying its file.
485 Used by the `browse-url-of-file' command."
486 :type 'boolean
487 :group 'browse-url)
488
489 (defcustom browse-url-of-file-hook nil
490 "Run after `browse-url-of-file' has asked a browser to load a file.
491
492 Set this to `browse-url-netscape-reload' to force Netscape to load the
493 file rather than displaying a cached copy."
494 :type 'hook
495 :options '(browse-url-netscape-reload)
496 :group 'browse-url)
497
498 (defcustom browse-url-CCI-port 3003
499 "Port to access XMosaic via CCI.
500 This can be any number between 1024 and 65535 but must correspond to
501 the value set in the browser."
502 :type 'integer
503 :group 'browse-url)
504
505 (defcustom browse-url-CCI-host "localhost"
506 "Host to access XMosaic via CCI.
507 This should be the host name of the machine running XMosaic with CCI
508 enabled. The port number should be set in `browse-url-CCI-port'."
509 :type 'string
510 :group 'browse-url)
511
512 (defvar browse-url-temp-file-name nil)
513 (make-variable-buffer-local 'browse-url-temp-file-name)
514
515 (defcustom browse-url-xterm-program "xterm"
516 "The name of the terminal emulator used by `browse-url-text-xterm'.
517 This might, for instance, be a separate color version of xterm."
518 :type 'string
519 :group 'browse-url)
520
521 (defcustom browse-url-xterm-args nil
522 "A list of strings defining options for `browse-url-xterm-program'.
523 These might set its size, for instance."
524 :type '(repeat (string :tag "Argument"))
525 :group 'browse-url)
526
527 (defcustom browse-url-gnudoit-program "gnudoit"
528 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
529 :type 'string
530 :group 'browse-url)
531
532 (defcustom browse-url-gnudoit-args '("-q")
533 "A list of strings defining options for `browse-url-gnudoit-program'.
534 These might set the port, for instance."
535 :type '(repeat (string :tag "Argument"))
536 :group 'browse-url)
537
538 (defcustom browse-url-generic-program nil
539 "The name of the browser program used by `browse-url-generic'."
540 :type '(choice string (const :tag "None" nil))
541 :group 'browse-url)
542
543 (defcustom browse-url-generic-args nil
544 "A list of strings defining options for `browse-url-generic-program'."
545 :type '(repeat (string :tag "Argument"))
546 :group 'browse-url)
547
548 (defcustom browse-url-temp-dir temporary-file-directory
549 "The name of a directory for browse-url's temporary files.
550 Such files are generated by functions like `browse-url-of-region'.
551 You might want to set this to somewhere with restricted read permissions
552 for privacy's sake."
553 :type 'string
554 :group 'browse-url)
555
556 (defcustom browse-url-netscape-version 3
557 "The version of Netscape you are using.
558 This affects how URL reloading is done; the mechanism changed
559 incompatibly at version 4."
560 :type 'number
561 :group 'browse-url)
562
563 (defcustom browse-url-text-browser "lynx"
564 "The name of the text browser to invoke."
565 :type 'string
566 :group 'browse-url
567 :version "23.1")
568
569 (defcustom browse-url-text-emacs-args (and (not window-system)
570 '("-show_cursor"))
571 "A list of strings defining options for a text browser in an Emacs buffer.
572
573 The default is none in a window system, otherwise `-show_cursor' to
574 indicate the position of the current link in the absence of
575 highlighting, assuming the normal default for showing the cursor."
576 :type '(repeat (string :tag "Argument"))
577 :version "23.1"
578 :group 'browse-url)
579
580 (defcustom browse-url-text-input-field 'avoid
581 "Action on selecting an existing text browser buffer at an input field.
582 What to do when sending a new URL to an existing text browser buffer in Emacs
583 if the browser cursor is on an input field (in which case the `g' command
584 would be entered as data). Such fields are recognized by the
585 underlines ____. Allowed values: nil: disregard it, `warn': warn the
586 user and don't emit the URL, `avoid': try to avoid the field by moving
587 down (this *won't* always work)."
588 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
589 (const :tag "Disregard" :value nil)
590 (const :tag "Warn, don't emit URL" :value warn))
591 :version "23.1"
592 :group 'browse-url)
593
594 (defcustom browse-url-text-input-attempts 10
595 "How many times to try to move down from a series of text browser input fields."
596 :type 'integer
597 :version "23.1"
598 :group 'browse-url)
599
600 (defcustom browse-url-text-input-delay 0.2
601 "Seconds to wait for a text browser between moves down from an input field."
602 :type 'number
603 :version "23.1"
604 :group 'browse-url)
605
606 (defcustom browse-url-kde-program "kfmclient"
607 "The name by which to invoke the KDE web browser."
608 :type 'string
609 :version "21.1"
610 :group 'browse-url)
611
612 (defcustom browse-url-kde-args '("openURL")
613 "A list of strings defining options for `browse-url-kde-program'."
614 :type '(repeat (string :tag "Argument"))
615 :group 'browse-url)
616
617 (defcustom browse-url-elinks-wrapper '("xterm" "-e")
618 "Wrapper command prepended to the Elinks command-line."
619 :type '(repeat (string :tag "Wrapper"))
620 :group 'browse-url)
621
622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
623 ;; URL encoding
624
625 (defun browse-url-url-encode-chars (text chars)
626 "URL-encode the chars in TEXT that match CHARS.
627 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
628 (let ((encoded-text (copy-sequence text))
629 (s 0))
630 (while (setq s (string-match chars encoded-text s))
631 (setq encoded-text
632 (replace-match (format "%%%x"
633 (string-to-char (match-string 0 encoded-text)))
634 t t encoded-text)
635 s (1+ s)))
636 encoded-text))
637
638 (defun browse-url-encode-url (url)
639 "Escape annoying characters in URL.
640 The annoying characters are those that can mislead a web browser
641 regarding its parameter treatment."
642 ;; FIXME: Is there an actual example of a web browser getting
643 ;; confused? (This used to encode commas, but at least Firefox
644 ;; handles commas correctly and doesn't accept encoded commas.)
645 (browse-url-url-encode-chars url "[)$]"))
646
647 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
648 ;; URL input
649
650 (defun browse-url-url-at-point ()
651 (let ((url (thing-at-point 'url)))
652 (set-text-properties 0 (length url) nil url)
653 url))
654
655 ;; Having this as a separate function called by the browser-specific
656 ;; functions allows them to be stand-alone commands, making it easier
657 ;; to switch between browsers.
658
659 (defun browse-url-interactive-arg (prompt)
660 "Read a URL from the minibuffer, prompting with PROMPT.
661 If `transient-mark-mode' is non-nil and the mark is active,
662 it defaults to the current region, else to the URL at or before
663 point. If invoked with a mouse button, it moves point to the
664 position clicked before acting.
665
666 This function returns a list (URL NEW-WINDOW-FLAG)
667 for use in `interactive'."
668 (let ((event (elt (this-command-keys) 0)))
669 (and (listp event) (mouse-set-point event)))
670 (list (read-string prompt (or (and transient-mark-mode mark-active
671 ;; rfc2396 Appendix E.
672 (replace-regexp-in-string
673 "[\t\r\f\n ]+" ""
674 (buffer-substring-no-properties
675 (region-beginning) (region-end))))
676 (browse-url-url-at-point)))
677 (not (eq (null browse-url-new-window-flag)
678 (null current-prefix-arg)))))
679
680 ;; called-interactive-p needs to be called at a function's top-level, hence
681 ;; this macro. We use that rather than interactive-p because
682 ;; use in a keyboard macro should not change this behavior.
683 (defmacro browse-url-maybe-new-window (arg)
684 `(if (or noninteractive (not (called-interactively-p 'any)))
685 ,arg
686 browse-url-new-window-flag))
687
688 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
689 ;; Browse current buffer
690
691 ;;;###autoload
692 (defun browse-url-of-file (&optional file)
693 "Ask a WWW browser to display FILE.
694 Display the current buffer's file if FILE is nil or if called
695 interactively. Turn the filename into a URL with function
696 `browse-url-file-url'. Pass the URL to a browser using the
697 `browse-url' function then run `browse-url-of-file-hook'."
698 (interactive)
699 (or file
700 (setq file (buffer-file-name))
701 (error "Current buffer has no file"))
702 (let ((buf (get-file-buffer file)))
703 (if buf
704 (with-current-buffer buf
705 (cond ((not (buffer-modified-p)))
706 (browse-url-save-file (save-buffer))
707 (t (message "%s modified since last save" file))))))
708 (browse-url (browse-url-file-url file))
709 (run-hooks 'browse-url-of-file-hook))
710
711 (defun browse-url-file-url (file)
712 "Return the URL corresponding to FILE.
713 Use variable `browse-url-filename-alist' to map filenames to URLs."
714 ;; De-munge Cygwin filenames before passing them to Windows browser.
715 (if (eq system-type 'cygwin)
716 (let ((winfile (with-output-to-string
717 (call-process "cygpath" nil standard-output
718 nil "-m" file))))
719 (setq file (substring winfile 0 -1))))
720 (let ((coding (and (default-value 'enable-multibyte-characters)
721 (or file-name-coding-system
722 default-file-name-coding-system))))
723 (if coding (setq file (encode-coding-string file coding))))
724 (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
725 (dolist (map browse-url-filename-alist)
726 (when (and map (string-match (car map) file))
727 (setq file (replace-match (cdr map) t nil file))))
728 file)
729
730 ;;;###autoload
731 (defun browse-url-of-buffer (&optional buffer)
732 "Ask a WWW browser to display BUFFER.
733 Display the current buffer if BUFFER is nil. Display only the
734 currently visible part of BUFFER (from a temporary file) if buffer is
735 narrowed."
736 (interactive)
737 (save-excursion
738 (and buffer (set-buffer buffer))
739 (let ((file-name
740 ;; Ignore real name if restricted
741 (and (= (- (point-max) (point-min)) (buffer-size))
742 (or buffer-file-name
743 (and (boundp 'dired-directory) dired-directory)))))
744 (or file-name
745 (progn
746 (or browse-url-temp-file-name
747 (setq browse-url-temp-file-name
748 (convert-standard-filename
749 (make-temp-file
750 (expand-file-name "burl" browse-url-temp-dir)
751 nil ".html"))))
752 (setq file-name browse-url-temp-file-name)
753 (write-region (point-min) (point-max) file-name nil 'no-message)))
754 (browse-url-of-file file-name))))
755
756 (defun browse-url-delete-temp-file (&optional temp-file-name)
757 ;; Delete browse-url-temp-file-name from the file system
758 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
759 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
760 (if (and file-name (file-exists-p file-name))
761 (delete-file file-name))))
762
763 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
764
765 (declare-function dired-get-filename "dired"
766 (&optional localp no-error-if-not-filep))
767
768 ;;;###autoload
769 (defun browse-url-of-dired-file ()
770 "In Dired, ask a WWW browser to display the file named on this line."
771 (interactive)
772 (let ((tem (dired-get-filename t t)))
773 (if tem
774 (browse-url-of-file (expand-file-name tem))
775 (error "No file on this line"))))
776
777 ;;;###autoload
778 (defun browse-url-of-region (min max)
779 "Ask a WWW browser to display the current region."
780 (interactive "r")
781 (save-excursion
782 (save-restriction
783 (narrow-to-region min max)
784 (browse-url-of-buffer))))
785
786 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
787 ;; Browser-independent commands
788
789 ;; A generic command to call the current browse-url-browser-function
790
791 ;;;###autoload
792 (defun browse-url (url &rest args)
793 "Ask a WWW browser to load URL.
794 Prompts for a URL, defaulting to the URL at or before point. Variable
795 `browse-url-browser-function' says which browser to use.
796 If the URL is a mailto: URL, consult `browse-url-mailto-function'
797 first, if that exists."
798 (interactive (browse-url-interactive-arg "URL: "))
799 (unless (called-interactively-p 'interactive)
800 (setq args (or args (list browse-url-new-window-flag))))
801 (let ((process-environment (copy-sequence process-environment))
802 (function (or (and (string-match "\\`mailto:" url)
803 browse-url-mailto-function)
804 browse-url-browser-function))
805 ;; Ensure that `default-directory' exists and is readable (b#6077).
806 (default-directory (if (and (file-directory-p default-directory)
807 (file-readable-p default-directory))
808 default-directory
809 (expand-file-name "~/"))))
810 ;; When connected to various displays, be careful to use the display of
811 ;; the currently selected frame, rather than the original start display,
812 ;; which may not even exist any more.
813 (if (stringp (frame-parameter (selected-frame) 'display))
814 (setenv "DISPLAY" (frame-parameter (selected-frame) 'display)))
815 (if (and (consp function)
816 (not (functionp function)))
817 ;; The `function' can be an alist; look down it for first match
818 ;; and apply the function (which might be a lambda).
819 (catch 'done
820 (dolist (bf function)
821 (when (string-match (car bf) url)
822 (apply (cdr bf) url args)
823 (throw 'done t)))
824 (error "No browse-url-browser-function matching URL %s"
825 url))
826 ;; Unbound symbols go down this leg, since void-function from
827 ;; apply is clearer than wrong-type-argument from dolist.
828 (apply function url args))))
829
830 ;;;###autoload
831 (defun browse-url-at-point (&optional arg)
832 "Ask a WWW browser to load the URL at or before point.
833 Doesn't let you edit the URL like `browse-url'. Variable
834 `browse-url-browser-function' says which browser to use."
835 (interactive "P")
836 (let ((url (browse-url-url-at-point)))
837 (if url
838 (browse-url url (if arg
839 (not browse-url-new-window-flag)
840 browse-url-new-window-flag))
841 (error "No URL found"))))
842
843 ;;;###autoload
844 (defun browse-url-at-mouse (event)
845 "Ask a WWW browser to load a URL clicked with the mouse.
846 The URL is the one around or before the position of the mouse click
847 but point is not changed. Doesn't let you edit the URL like
848 `browse-url'. Variable `browse-url-browser-function' says which browser
849 to use."
850 (interactive "e")
851 (save-excursion
852 (mouse-set-point event)
853 ;; This handles browse-url-new-window-flag properly
854 ;; when it gets no arg.
855 (browse-url-at-point)))
856
857 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
858 ;; Browser-specific commands
859
860 ;; --- Default MS-Windows browser ---
861
862 (defvar dos-windows-version)
863 (declare-function w32-shell-execute "w32fns.c") ;; Defined in C.
864
865 (defun browse-url-default-windows-browser (url &optional new-window)
866 (interactive (browse-url-interactive-arg "URL: "))
867 (cond ((eq system-type 'ms-dos)
868 (if dos-windows-version
869 (shell-command (concat "start " (shell-quote-argument url)))
870 (error "Browsing URLs is not supported on this system")))
871 ((eq system-type 'cygwin)
872 (call-process "cygstart" nil nil nil url))
873 (t (w32-shell-execute "open" url))))
874
875 (defun browse-url-default-macosx-browser (url &optional new-window)
876 (interactive (browse-url-interactive-arg "URL: "))
877 (start-process (concat "open " url) nil "open" url))
878
879 ;; --- Netscape ---
880
881 (defun browse-url-process-environment ()
882 "Set DISPLAY in the environment to the X display the browser will use.
883 This is either the value of variable `browse-url-browser-display' if
884 non-nil, or the same display as Emacs if different from the current
885 environment, otherwise just use the current environment."
886 (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
887 (if display
888 (cons (concat "DISPLAY=" display) process-environment)
889 process-environment)))
890
891 (defun browse-url-emacs-display ()
892 "Return the X display Emacs is running on.
893 This is nil if the display is the same as the DISPLAY environment variable.
894
895 Actually Emacs could be using several displays; this just returns the
896 one showing the selected frame."
897 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
898 (and (not (equal display (getenv "DISPLAY")))
899 display)))
900
901 (defun browse-url-default-browser (url &rest args)
902 "Find a suitable browser and ask it to load URL.
903 Default to the URL around or before point.
904
905 When called interactively, if variable `browse-url-new-window-flag' is
906 non-nil, load the document in a new window, if possible, otherwise use
907 a random existing one. A non-nil interactive prefix argument reverses
908 the effect of `browse-url-new-window-flag'.
909
910 When called non-interactively, optional second argument NEW-WINDOW is
911 used instead of `browse-url-new-window-flag'.
912
913 The order attempted is gnome-moz-remote, Mozilla, Firefox,
914 Galeon, Konqueror, Netscape, Mosaic, Lynx in an xterm, and then W3."
915 (apply
916 (cond
917 ((browse-url-can-use-xdg-open) 'browse-url-xdg-open)
918 ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
919 ((executable-find browse-url-mozilla-program) 'browse-url-mozilla)
920 ((executable-find browse-url-firefox-program) 'browse-url-firefox)
921 ((executable-find browse-url-galeon-program) 'browse-url-galeon)
922 ((executable-find browse-url-kde-program) 'browse-url-kde)
923 ((executable-find browse-url-netscape-program) 'browse-url-netscape)
924 ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
925 ((executable-find browse-url-xterm-program) 'browse-url-text-xterm)
926 ((locate-library "w3") 'browse-url-w3)
927 (t
928 (lambda (&rest ignore) (error "No usable browser found"))))
929 url args))
930
931 (defun browse-url-can-use-xdg-open ()
932 "Check if xdg-open can be used, i.e. we are on Gnome, KDE or xfce4."
933 (and (getenv "DISPLAY")
934 (executable-find "xdg-open")
935 ;; xdg-open may call gnome-open and that does not wait for its child
936 ;; to finish. This child may then be killed when the parent dies.
937 ;; Use nohup to work around.
938 (executable-find "nohup")
939 (or (getenv "GNOME_DESKTOP_SESSION_ID")
940 ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
941 (condition-case nil
942 (eq 0 (call-process
943 "dbus-send" nil nil nil
944 "--dest=org.gnome.SessionManager"
945 "--print-reply"
946 "/org/gnome/SessionManager"
947 "org.gnome.SessionManager.CanShutdown"))
948 (error nil))
949 (equal (getenv "KDE_FULL_SESSION") "true")
950 (condition-case nil
951 (eq 0 (call-process
952 "/bin/sh" nil nil nil
953 "-c"
954 "xprop -root _DT_SAVE_MODE|grep xfce4"))
955 (error nil)))))
956
957
958 ;;;###autoload
959 (defun browse-url-xdg-open (url &optional new-window)
960 (interactive (browse-url-interactive-arg "URL: "))
961 (call-process "nohup" nil nil nil "xdg-open" url))
962
963 ;;;###autoload
964 (defun browse-url-netscape (url &optional new-window)
965 "Ask the Netscape WWW browser to load URL.
966 Default to the URL around or before point. The strings in variable
967 `browse-url-netscape-arguments' are also passed to Netscape.
968
969 When called interactively, if variable `browse-url-new-window-flag' is
970 non-nil, load the document in a new Netscape window, otherwise use a
971 random existing one. A non-nil interactive prefix argument reverses
972 the effect of `browse-url-new-window-flag'.
973
974 If `browse-url-netscape-new-window-is-tab' is non-nil, then
975 whenever a document would otherwise be loaded in a new window, it
976 is loaded in a new tab in an existing window instead.
977
978 When called non-interactively, optional second argument NEW-WINDOW is
979 used instead of `browse-url-new-window-flag'."
980 (interactive (browse-url-interactive-arg "URL: "))
981 (setq url (browse-url-encode-url url))
982 (let* ((process-environment (browse-url-process-environment))
983 (process
984 (apply 'start-process
985 (concat "netscape " url) nil
986 browse-url-netscape-program
987 (append
988 browse-url-netscape-arguments
989 (if (eq window-system 'w32)
990 (list url)
991 (append
992 (if new-window '("-noraise"))
993 (list "-remote"
994 (concat "openURL(" url
995 (if (browse-url-maybe-new-window
996 new-window)
997 (if browse-url-netscape-new-window-is-tab
998 ",new-tab"
999 ",new-window"))
1000 ")"))))))))
1001 (set-process-sentinel process
1002 `(lambda (process change)
1003 (browse-url-netscape-sentinel process ,url)))))
1004
1005 (defun browse-url-netscape-sentinel (process url)
1006 "Handle a change to the process communicating with Netscape."
1007 (or (eq (process-exit-status process) 0)
1008 (let* ((process-environment (browse-url-process-environment)))
1009 ;; Netscape not running - start it
1010 (message "Starting %s..." browse-url-netscape-program)
1011 (apply 'start-process (concat "netscape" url) nil
1012 browse-url-netscape-program
1013 (append browse-url-netscape-startup-arguments (list url))))))
1014
1015 (defun browse-url-netscape-reload ()
1016 "Ask Netscape to reload its current document.
1017 How depends on `browse-url-netscape-version'."
1018 (interactive)
1019 ;; Backwards incompatibility reported by
1020 ;; <peter.kruse@psychologie.uni-regensburg.de>.
1021 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
1022 "xfeDoCommand(reload)"
1023 "reload")))
1024
1025 (defun browse-url-netscape-send (command)
1026 "Send a remote control command to Netscape."
1027 (let* ((process-environment (browse-url-process-environment)))
1028 (apply 'start-process "netscape" nil
1029 browse-url-netscape-program
1030 (append browse-url-netscape-arguments
1031 (list "-remote" command)))))
1032
1033 ;;;###autoload
1034 (defun browse-url-mozilla (url &optional new-window)
1035 "Ask the Mozilla WWW browser to load URL.
1036 Default to the URL around or before point. The strings in variable
1037 `browse-url-mozilla-arguments' are also passed to Mozilla.
1038
1039 When called interactively, if variable `browse-url-new-window-flag' is
1040 non-nil, load the document in a new Mozilla window, otherwise use a
1041 random existing one. A non-nil interactive prefix argument reverses
1042 the effect of `browse-url-new-window-flag'.
1043
1044 If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
1045 document would otherwise be loaded in a new window, it is loaded in a
1046 new tab in an existing window instead.
1047
1048 When called non-interactively, optional second argument NEW-WINDOW is
1049 used instead of `browse-url-new-window-flag'."
1050 (interactive (browse-url-interactive-arg "URL: "))
1051 (setq url (browse-url-encode-url url))
1052 (let* ((process-environment (browse-url-process-environment))
1053 (process
1054 (apply 'start-process
1055 (concat "mozilla " url) nil
1056 browse-url-mozilla-program
1057 (append
1058 browse-url-mozilla-arguments
1059 (list "-remote"
1060 (concat "openURL("
1061 url
1062 (if (browse-url-maybe-new-window
1063 new-window)
1064 (if browse-url-mozilla-new-window-is-tab
1065 ",new-tab"
1066 ",new-window"))
1067 ")"))))))
1068 (set-process-sentinel process
1069 `(lambda (process change)
1070 (browse-url-mozilla-sentinel process ,url)))))
1071
1072 (defun browse-url-mozilla-sentinel (process url)
1073 "Handle a change to the process communicating with Mozilla."
1074 (or (eq (process-exit-status process) 0)
1075 (let* ((process-environment (browse-url-process-environment)))
1076 ;; Mozilla is not running - start it
1077 (message "Starting %s..." browse-url-mozilla-program)
1078 (apply 'start-process (concat "mozilla " url) nil
1079 browse-url-mozilla-program
1080 (append browse-url-mozilla-startup-arguments (list url))))))
1081
1082 ;;;###autoload
1083 (defun browse-url-firefox (url &optional new-window)
1084 "Ask the Firefox WWW browser to load URL.
1085 Default to the URL around or before point. The strings in
1086 variable `browse-url-firefox-arguments' are also passed to
1087 Firefox.
1088
1089 When called interactively, if variable
1090 `browse-url-new-window-flag' is non-nil, load the document in a
1091 new Firefox window, otherwise use a random existing one. A
1092 non-nil interactive prefix argument reverses the effect of
1093 `browse-url-new-window-flag'.
1094
1095 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1096 whenever a document would otherwise be loaded in a new window, it
1097 is loaded in a new tab in an existing window instead.
1098
1099 When called non-interactively, optional second argument
1100 NEW-WINDOW is used instead of `browse-url-new-window-flag'.
1101
1102 On MS-Windows systems the optional `new-window' parameter is
1103 ignored. Firefox for Windows does not support the \"-remote\"
1104 command line parameter. Therefore, the
1105 `browse-url-new-window-flag' and `browse-url-firefox-new-window-is-tab'
1106 are ignored as well. Firefox on Windows will always open the requested
1107 URL in a new window."
1108 (interactive (browse-url-interactive-arg "URL: "))
1109 (setq url (browse-url-encode-url url))
1110 (let* ((process-environment (browse-url-process-environment))
1111 (process
1112 (apply 'start-process
1113 (concat "firefox " url) nil
1114 browse-url-firefox-program
1115 (append
1116 browse-url-firefox-arguments
1117 (if (memq system-type '(windows-nt ms-dos))
1118 (list url)
1119 (list "-remote"
1120 (concat "openURL("
1121 url
1122 (if (browse-url-maybe-new-window
1123 new-window)
1124 (if browse-url-firefox-new-window-is-tab
1125 ",new-tab"
1126 ",new-window"))
1127 ")")))))))
1128 (set-process-sentinel process
1129 `(lambda (process change)
1130 (browse-url-firefox-sentinel process ,url)))))
1131
1132 (defun browse-url-firefox-sentinel (process url)
1133 "Handle a change to the process communicating with Firefox."
1134 (or (eq (process-exit-status process) 0)
1135 (let* ((process-environment (browse-url-process-environment)))
1136 ;; Firefox is not running - start it
1137 (message "Starting Firefox...")
1138 (apply 'start-process (concat "firefox " url) nil
1139 browse-url-firefox-program
1140 (append browse-url-firefox-startup-arguments (list url))))))
1141
1142 ;;;###autoload
1143 (defun browse-url-galeon (url &optional new-window)
1144 "Ask the Galeon WWW browser to load URL.
1145 Default to the URL around or before point. The strings in variable
1146 `browse-url-galeon-arguments' are also passed to Galeon.
1147
1148 When called interactively, if variable `browse-url-new-window-flag' is
1149 non-nil, load the document in a new Galeon window, otherwise use a
1150 random existing one. A non-nil interactive prefix argument reverses
1151 the effect of `browse-url-new-window-flag'.
1152
1153 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1154 document would otherwise be loaded in a new window, it is loaded in a
1155 new tab in an existing window instead.
1156
1157 When called non-interactively, optional second argument NEW-WINDOW is
1158 used instead of `browse-url-new-window-flag'."
1159 (interactive (browse-url-interactive-arg "URL: "))
1160 (setq url (browse-url-encode-url url))
1161 (let* ((process-environment (browse-url-process-environment))
1162 (process (apply 'start-process
1163 (concat "galeon " url)
1164 nil
1165 browse-url-galeon-program
1166 (append
1167 browse-url-galeon-arguments
1168 (if (browse-url-maybe-new-window new-window)
1169 (if browse-url-galeon-new-window-is-tab
1170 '("--new-tab")
1171 '("--new-window" "--noraise"))
1172 '("--existing"))
1173 (list url)))))
1174 (set-process-sentinel process
1175 `(lambda (process change)
1176 (browse-url-galeon-sentinel process ,url)))))
1177
1178 (defun browse-url-galeon-sentinel (process url)
1179 "Handle a change to the process communicating with Galeon."
1180 (or (eq (process-exit-status process) 0)
1181 (let* ((process-environment (browse-url-process-environment)))
1182 ;; Galeon is not running - start it
1183 (message "Starting %s..." browse-url-galeon-program)
1184 (apply 'start-process (concat "galeon " url) nil
1185 browse-url-galeon-program
1186 (append browse-url-galeon-startup-arguments (list url))))))
1187
1188 (defun browse-url-epiphany (url &optional new-window)
1189 "Ask the Epiphany WWW browser to load URL.
1190 Default to the URL around or before point. The strings in variable
1191 `browse-url-galeon-arguments' are also passed to Epiphany.
1192
1193 When called interactively, if variable `browse-url-new-window-flag' is
1194 non-nil, load the document in a new Epiphany window, otherwise use a
1195 random existing one. A non-nil interactive prefix argument reverses
1196 the effect of `browse-url-new-window-flag'.
1197
1198 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1199 document would otherwise be loaded in a new window, it is loaded in a
1200 new tab in an existing window instead.
1201
1202 When called non-interactively, optional second argument NEW-WINDOW is
1203 used instead of `browse-url-new-window-flag'."
1204 (interactive (browse-url-interactive-arg "URL: "))
1205 (setq url (browse-url-encode-url url))
1206 (let* ((process-environment (browse-url-process-environment))
1207 (process (apply 'start-process
1208 (concat "epiphany " url)
1209 nil
1210 browse-url-epiphany-program
1211 (append
1212 browse-url-epiphany-arguments
1213 (if (browse-url-maybe-new-window new-window)
1214 (if browse-url-epiphany-new-window-is-tab
1215 '("--new-tab")
1216 '("--new-window" "--noraise"))
1217 '("--existing"))
1218 (list url)))))
1219 (set-process-sentinel process
1220 `(lambda (process change)
1221 (browse-url-epiphany-sentinel process ,url)))))
1222
1223 (defun browse-url-epiphany-sentinel (process url)
1224 "Handle a change to the process communicating with Epiphany."
1225 (or (eq (process-exit-status process) 0)
1226 (let* ((process-environment (browse-url-process-environment)))
1227 ;; Epiphany is not running - start it
1228 (message "Starting %s..." browse-url-epiphany-program)
1229 (apply 'start-process (concat "epiphany " url) nil
1230 browse-url-epiphany-program
1231 (append browse-url-epiphany-startup-arguments (list url))))))
1232
1233 (defvar url-handler-regexp)
1234
1235 ;;;###autoload
1236 (defun browse-url-emacs (url &optional new-window)
1237 "Ask Emacs to load URL into a buffer and show it in another window."
1238 (interactive (browse-url-interactive-arg "URL: "))
1239 (require 'url-handlers)
1240 (let ((file-name-handler-alist
1241 (cons (cons url-handler-regexp 'url-file-handler)
1242 file-name-handler-alist)))
1243 ;; Ignore `new-window': with all other browsers the URL is always shown
1244 ;; in another window than the current Emacs one since it's shown in
1245 ;; another application's window.
1246 ;; (if new-window (find-file-other-window url) (find-file url))
1247 (find-file-other-window url)))
1248
1249 ;;;###autoload
1250 (defun browse-url-gnome-moz (url &optional new-window)
1251 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1252 Default to the URL around or before point. The strings in variable
1253 `browse-url-gnome-moz-arguments' are also passed.
1254
1255 When called interactively, if variable `browse-url-new-window-flag' is
1256 non-nil, load the document in a new browser window, otherwise use an
1257 existing one. A non-nil interactive prefix argument reverses the
1258 effect of `browse-url-new-window-flag'.
1259
1260 When called non-interactively, optional second argument NEW-WINDOW is
1261 used instead of `browse-url-new-window-flag'."
1262 (interactive (browse-url-interactive-arg "URL: "))
1263 (apply 'start-process (concat "gnome-moz-remote " url)
1264 nil
1265 browse-url-gnome-moz-program
1266 (append
1267 browse-url-gnome-moz-arguments
1268 (if (browse-url-maybe-new-window new-window)
1269 '("--newwin"))
1270 (list "--raise" url))))
1271
1272 ;; --- Mosaic ---
1273
1274 ;;;###autoload
1275 (defun browse-url-mosaic (url &optional new-window)
1276 "Ask the XMosaic WWW browser to load URL.
1277
1278 Default to the URL around or before point. The strings in variable
1279 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1280 program is invoked according to the variable
1281 `browse-url-mosaic-program'.
1282
1283 When called interactively, if variable `browse-url-new-window-flag' is
1284 non-nil, load the document in a new Mosaic window, otherwise use a
1285 random existing one. A non-nil interactive prefix argument reverses
1286 the effect of `browse-url-new-window-flag'.
1287
1288 When called non-interactively, optional second argument NEW-WINDOW is
1289 used instead of `browse-url-new-window-flag'."
1290 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1291 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
1292 pid)
1293 (if (file-readable-p pidfile)
1294 (save-excursion
1295 (find-file pidfile)
1296 (goto-char (point-min))
1297 (setq pid (read (current-buffer)))
1298 (kill-buffer nil)))
1299 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
1300 (save-excursion
1301 (find-file (format "/tmp/Mosaic.%d" pid))
1302 (erase-buffer)
1303 (insert (if (browse-url-maybe-new-window new-window)
1304 "newwin\n"
1305 "goto\n")
1306 url "\n")
1307 (save-buffer)
1308 (kill-buffer nil)
1309 ;; Send signal SIGUSR to Mosaic
1310 (message "Signaling Mosaic...")
1311 (signal-process pid 'SIGUSR1)
1312 ;; Or you could try:
1313 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1314 (message "Signaling Mosaic...done")
1315 )
1316 ;; Mosaic not running - start it
1317 (message "Starting %s..." browse-url-mosaic-program)
1318 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1319 (append browse-url-mosaic-arguments (list url)))
1320 (message "Starting %s...done" browse-url-mosaic-program))))
1321
1322 ;; --- Mosaic using CCI ---
1323
1324 ;;;###autoload
1325 (defun browse-url-cci (url &optional new-window)
1326 "Ask the XMosaic WWW browser to load URL.
1327 Default to the URL around or before point.
1328
1329 This function only works for XMosaic version 2.5 or later. You must
1330 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1331 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1332
1333 When called interactively, if variable `browse-url-new-window-flag' is
1334 non-nil, load the document in a new browser window, otherwise use a
1335 random existing one. A non-nil interactive prefix argument reverses
1336 the effect of `browse-url-new-window-flag'.
1337
1338 When called non-interactively, optional second argument NEW-WINDOW is
1339 used instead of `browse-url-new-window-flag'."
1340 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1341 (open-network-stream "browse-url" " *browse-url*"
1342 browse-url-CCI-host browse-url-CCI-port)
1343 ;; Todo: start browser if fails
1344 (process-send-string "browse-url"
1345 (concat "get url (" url ") output "
1346 (if (browse-url-maybe-new-window new-window)
1347 "new"
1348 "current")
1349 "\r\n"))
1350 (process-send-string "browse-url" "disconnect\r\n")
1351 (delete-process "browse-url"))
1352
1353 ;; --- W3 ---
1354
1355 ;; External.
1356 (declare-function w3-fetch-other-window "ext:w3m" (&optional url))
1357 (declare-function w3-fetch "ext:w3m" (&optional url target))
1358
1359 ;;;###autoload
1360 (defun browse-url-w3 (url &optional new-window)
1361 "Ask the w3 WWW browser to load URL.
1362 Default to the URL around or before point.
1363
1364 When called interactively, if variable `browse-url-new-window-flag' is
1365 non-nil, load the document in a new window. A non-nil interactive
1366 prefix argument reverses the effect of `browse-url-new-window-flag'.
1367
1368 When called non-interactively, optional second argument NEW-WINDOW is
1369 used instead of `browse-url-new-window-flag'."
1370 (interactive (browse-url-interactive-arg "W3 URL: "))
1371 (require 'w3) ; w3-fetch-other-window not autoloaded
1372 (if (browse-url-maybe-new-window new-window)
1373 (w3-fetch-other-window url)
1374 (w3-fetch url)))
1375
1376 ;;;###autoload
1377 (defun browse-url-w3-gnudoit (url &optional new-window)
1378 ;; new-window ignored
1379 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1380 The `browse-url-gnudoit-program' program is used with options given by
1381 `browse-url-gnudoit-args'. Default to the URL around or before point."
1382 (interactive (browse-url-interactive-arg "W3 URL: "))
1383 (apply 'start-process (concat "gnudoit:" url) nil
1384 browse-url-gnudoit-program
1385 (append browse-url-gnudoit-args
1386 (list (concat "(w3-fetch \"" url "\")")
1387 "(raise-frame)"))))
1388
1389 ;; --- Lynx in an xterm ---
1390
1391 ;;;###autoload
1392 (defun browse-url-text-xterm (url &optional new-window)
1393 ;; new-window ignored
1394 "Ask a text browser to load URL.
1395 URL defaults to the URL around or before point.
1396 This runs the text browser specified by `browse-url-text-browser'.
1397 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1398 with possible additional arguments `browse-url-xterm-args'."
1399 (interactive (browse-url-interactive-arg "Text browser URL: "))
1400 (apply #'start-process `(,(concat browse-url-text-browser url)
1401 nil ,browse-url-xterm-program
1402 ,@browse-url-xterm-args "-e" ,browse-url-text-browser
1403 ,url)))
1404
1405 ;; --- Lynx in an Emacs "term" window ---
1406
1407 (declare-function term-char-mode "term" ())
1408 (declare-function term-send-down "term" ())
1409 (declare-function term-send-string "term" (proc str))
1410
1411 ;;;###autoload
1412 (defun browse-url-text-emacs (url &optional new-buffer)
1413 "Ask a text browser to load URL.
1414 URL defaults to the URL around or before point.
1415 This runs the text browser specified by `browse-url-text-browser'.
1416 With a prefix argument, it runs a new browser process in a new buffer.
1417
1418 When called interactively, if variable `browse-url-new-window-flag' is
1419 non-nil, load the document in a new browser process in a new term window,
1420 otherwise use any existing one. A non-nil interactive prefix argument
1421 reverses the effect of `browse-url-new-window-flag'.
1422
1423 When called non-interactively, optional second argument NEW-WINDOW is
1424 used instead of `browse-url-new-window-flag'."
1425 (interactive (browse-url-interactive-arg "Text browser URL: "))
1426 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
1427 ;; (term-term-name "vt100") ; ??
1428 (buf (get-buffer "*text browser*"))
1429 (proc (and buf (get-buffer-process buf)))
1430 (n browse-url-text-input-attempts))
1431 (require 'term)
1432 (if (and (browse-url-maybe-new-window new-buffer) buf)
1433 ;; Rename away the OLD buffer. This isn't very polite, but
1434 ;; term insists on working in a buffer named *lynx* and would
1435 ;; choke on *lynx*<1>
1436 (progn (set-buffer buf)
1437 (rename-uniquely)))
1438 (if (or (browse-url-maybe-new-window new-buffer)
1439 (not buf)
1440 (not proc)
1441 (not (memq (process-status proc) '(run stop))))
1442 ;; start a new text browser
1443 (progn
1444 (setq buf
1445 (apply #'make-term
1446 `(,browse-url-text-browser
1447 ,browse-url-text-browser
1448 nil ,@browse-url-text-emacs-args
1449 ,url)))
1450 (switch-to-buffer buf)
1451 (term-char-mode)
1452 (set-process-sentinel
1453 (get-buffer-process buf)
1454 ;; Don't leave around a dead one (especially because of its
1455 ;; munged keymap.)
1456 (lambda (process event)
1457 (if (not (memq (process-status process) '(run stop)))
1458 (let ((buf (process-buffer process)))
1459 (if buf (kill-buffer buf)))))))
1460 ;; Send the url to the text browser in the old buffer
1461 (let ((win (get-buffer-window buf t)))
1462 (if win
1463 (select-window win)
1464 (switch-to-buffer buf)))
1465 (if (eq (following-char) ?_)
1466 (cond ((eq browse-url-text-input-field 'warn)
1467 (error "Please move out of the input field first"))
1468 ((eq browse-url-text-input-field 'avoid)
1469 (while (and (eq (following-char) ?_) (> n 0))
1470 (term-send-down) ; down arrow
1471 (sit-for browse-url-text-input-delay))
1472 (if (eq (following-char) ?_)
1473 (error "Cannot move out of the input field, sorry")))))
1474 (term-send-string proc (concat "g" ; goto
1475 "\C-u" ; kill default url
1476 url
1477 "\r")))))
1478
1479 ;; --- mailto ---
1480
1481 (autoload 'rfc2368-parse-mailto-url "rfc2368")
1482
1483 ;;;###autoload
1484 (defun browse-url-mail (url &optional new-window)
1485 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1486 Default to using the mailto: URL around or before point as the
1487 recipient's address. Supplying a non-nil interactive prefix argument
1488 will cause the mail to be composed in another window rather than the
1489 current one.
1490
1491 When called interactively, if variable `browse-url-new-window-flag' is
1492 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1493 non-nil interactive prefix argument reverses the effect of
1494 `browse-url-new-window-flag'.
1495
1496 When called non-interactively, optional second argument NEW-WINDOW is
1497 used instead of `browse-url-new-window-flag'."
1498 (interactive (browse-url-interactive-arg "Mailto URL: "))
1499 (save-excursion
1500 (let* ((alist (rfc2368-parse-mailto-url url))
1501 (to (assoc "To" alist))
1502 (subject (assoc "Subject" alist))
1503 (body (assoc "Body" alist))
1504 (rest (delq to (delq subject (delq body alist))))
1505 (to (cdr to))
1506 (subject (cdr subject))
1507 (body (cdr body))
1508 (mail-citation-hook (unless body mail-citation-hook)))
1509 (if (browse-url-maybe-new-window new-window)
1510 (compose-mail-other-window to subject rest nil
1511 (list 'insert-buffer (current-buffer)))
1512 (compose-mail to subject rest nil nil
1513 (list 'insert-buffer (current-buffer))))
1514 (when body
1515 (goto-char (point-min))
1516 (unless (or (search-forward (concat "\n" mail-header-separator "\n")
1517 nil 'move)
1518 (bolp))
1519 (insert "\n"))
1520 (goto-char (prog1
1521 (point)
1522 (insert (replace-regexp-in-string "\r\n" "\n" body))
1523 (unless (bolp)
1524 (insert "\n"))))))))
1525
1526 ;; --- Random browser ---
1527
1528 ;;;###autoload
1529 (defun browse-url-generic (url &optional new-window)
1530 ;; new-window ignored
1531 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1532 Default to the URL around or before point. A fresh copy of the
1533 browser is started up in a new process with possible additional arguments
1534 `browse-url-generic-args'. This is appropriate for browsers which
1535 don't offer a form of remote control."
1536 (interactive (browse-url-interactive-arg "URL: "))
1537 (if (not browse-url-generic-program)
1538 (error "No browser defined (`browse-url-generic-program')"))
1539 (apply 'call-process browse-url-generic-program nil
1540 0 nil
1541 (append browse-url-generic-args (list url))))
1542
1543 ;;;###autoload
1544 (defun browse-url-kde (url &optional new-window)
1545 "Ask the KDE WWW browser to load URL.
1546 Default to the URL around or before point."
1547 (interactive (browse-url-interactive-arg "KDE URL: "))
1548 (message "Sending URL to KDE...")
1549 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
1550 (append browse-url-kde-args (list url))))
1551
1552 (defun browse-url-elinks-new-window (url)
1553 "Ask the Elinks WWW browser to load URL in a new window."
1554 (let ((process-environment (browse-url-process-environment)))
1555 (apply #'start-process
1556 (append (list (concat "elinks:" url)
1557 nil)
1558 browse-url-elinks-wrapper
1559 (list "elinks" url)))))
1560
1561 ;;;###autoload
1562 (defun browse-url-elinks (url &optional new-window)
1563 "Ask the Elinks WWW browser to load URL.
1564 Default to the URL around the point.
1565
1566 The document is loaded in a new tab of a running Elinks or, if
1567 none yet running, a newly started instance.
1568
1569 The Elinks command will be prepended by the program+arguments
1570 from `browse-url-elinks-wrapper'."
1571 (interactive (browse-url-interactive-arg "URL: "))
1572 (setq url (browse-url-encode-url url))
1573 (if new-window
1574 (browse-url-elinks-new-window url)
1575 (let ((process-environment (browse-url-process-environment))
1576 (elinks-ping-process (start-process "elinks-ping" nil
1577 "elinks" "-remote" "ping()")))
1578 (set-process-sentinel elinks-ping-process
1579 `(lambda (process change)
1580 (browse-url-elinks-sentinel process ,url))))))
1581
1582 (defun browse-url-elinks-sentinel (process url)
1583 "Determines if Elinks is running or a new one has to be started."
1584 (let ((exit-status (process-exit-status process)))
1585 ;; Try to determine if an instance is running or if we have to
1586 ;; create a new one.
1587 (case exit-status
1588 (5
1589 ;; No instance, start a new one.
1590 (browse-url-elinks-new-window url))
1591 (0
1592 ;; Found an instance, open URL in new tab.
1593 (let ((process-environment (browse-url-process-environment)))
1594 (start-process (concat "elinks:" url) nil
1595 "elinks" "-remote"
1596 (concat "openURL(\"" url "\",new-tab)"))))
1597 (otherwise
1598 (error "Unrecognized exit-code %d of process `elinks'"
1599 exit-status)))))
1600
1601 (provide 'browse-url)
1602
1603 ;;; browse-url.el ends here