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