Merge changes from emacs-23
[bpt/emacs.git] / lisp / net / browse-url.el
CommitLineData
e8af40ee 1;;; browse-url.el --- pass a URL to a WWW browser
8749abea 2
95df8112 3;; Copyright (C) 1995-2011 Free Software Foundation, Inc.
8749abea
GM
4
5;; Author: Denis Howe <dbh@doc.ic.ac.uk>
7f61a9ee 6;; Maintainer: FSF
8749abea
GM
7;; Created: 03 Apr 1995
8;; Keywords: hypertext, hypermedia, mouse
9
10;; This file is part of GNU Emacs.
11
874a927a 12;; GNU Emacs is free software: you can redistribute it and/or modify
8749abea 13;; it under the terms of the GNU General Public License as published by
874a927a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
8749abea
GM
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
874a927a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
8749abea
GM
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
3abc9fa1
DL
33;; is started. Currently there is support for the following browsers,
34;; some of them probably now obsolete:
35
36;; Function Browser Earliest version
7f61a9ee 37;; browse-url-mozilla Mozilla Don't know
49866ff8 38;; browse-url-firefox Firefox Don't know (tried with 1.0.1)
7f61a9ee 39;; browse-url-galeon Galeon Don't know
654805e3 40;; browse-url-epiphany Epiphany Don't know
3abc9fa1
DL
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
d9774611 46;; browse-url-text-* Any text browser 0
3abc9fa1
DL
47;; browse-url-generic arbitrary
48;; browse-url-default-windows-browser MS-Windows browser
662871dd 49;; browse-url-default-macosx-browser Mac OS X browser
b12e6de3 50;; browse-url-gnome-moz GNOME interface to Mozilla
af06f459 51;; browse-url-kde KDE konqueror (kfm)
8c22c51a 52;; browse-url-elinks Elinks Don't know (tried with 0.12.GIT)
8749abea
GM
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
6eddc3bb 69;; William M. Perry's excellent "w3" WWW browser for
8749abea
GM
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
8749abea
GM
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
85d30d29
PJ
85;; <URL:ftp://ftp.enst.fr/pub/mbone/mMosaic/>,
86;; <URL:http://www.enst.fr/~dauphin/mMosaic/> (with development
8749abea
GM
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
3abc9fa1 156;; Browse URLs in mail messages under RMAIL by clicking mouse-2:
8749abea
GM
157;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
158;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
3abc9fa1 159;; Alternatively, add `goto-address' to `rmail-show-message-hook'.
8749abea 160
3abc9fa1
DL
161;; Gnus provides a standard feature to activate URLs in article
162;; buffers for invocation of browse-url.
8749abea
GM
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
a1ab97d0 206(eval-when-compile (require 'cl))
8749abea
GM
207
208(defgroup browse-url nil
209 "Use a web browser to look at a URL."
210 :prefix "browse-url-"
082527fe 211 :link '(emacs-commentary-link "browse-url")
eba5b4dd 212 :group 'external
26f4b8ab 213 :group 'comm)
8749abea
GM
214
215;;;###autoload
216(defcustom browse-url-browser-function
2a517d45 217 'browse-url-default-browser
5e835c9f 218 "Function to display the current buffer in a WWW browser.
8749abea
GM
219This is used by the `browse-url-at-point', `browse-url-at-mouse', and
220`browse-url-of-file' commands.
221
222If the value is not a function it should be a list of pairs
082527fe 223\(REGEXP . FUNCTION). In this case the function called will be the one
8749abea
GM
224associated with the first REGEXP which matches the current URL. The
225function is passed the URL and any other args of `browse-url'. The last
226regexp should probably be \".\" to specify a default browser."
227 :type '(choice
082527fe
DL
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)
7f61a9ee 231 (function-item :tag "Mozilla" :value browse-url-mozilla)
49866ff8 232 (function-item :tag "Firefox" :value browse-url-firefox)
7f61a9ee 233 (function-item :tag "Galeon" :value browse-url-galeon)
654805e3 234 (function-item :tag "Epiphany" :value browse-url-epiphany)
082527fe
DL
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)
d9774611
RS
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)
af06f459 242 (function-item :tag "KDE" :value browse-url-kde)
8c22c51a 243 (function-item :tag "Elinks" :value browse-url-elinks)
082527fe
DL
244 (function-item :tag "Specified by `Browse Url Generic Program'"
245 :value browse-url-generic)
246 (function-item :tag "Default Windows browser"
b12e6de3 247 :value browse-url-default-windows-browser)
662871dd
SM
248 (function-item :tag "Default Mac OS X browser"
249 :value browse-url-default-macosx-browser)
b12e6de3
DL
250 (function-item :tag "GNOME invoking Mozilla"
251 :value browse-url-gnome-moz)
7f61a9ee
RS
252 (function-item :tag "Default browser"
253 :value browse-url-default-browser)
082527fe
DL
254 (function :tag "Your own function")
255 (alist :tag "Regexp/function association list"
256 :key-type regexp :value-type function))
afe2870b 257 :version "24.1"
8749abea
GM
258 :group 'browse-url)
259
876d1684
LMI
260(defcustom browse-url-mailto-function 'browse-url-mail
261 "Function to display mailto: links.
262This 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
265be 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
8749abea
GM
272(defcustom browse-url-netscape-program "netscape"
273 ;; Info about netscape-remote from Karl Berry.
5e835c9f 274 "The name by which to invoke Netscape.
8749abea
GM
275
276The free program `netscape-remote' from
277<URL:http://home.netscape.com/newsref/std/remote.c> is said to start
278up very much quicker than `netscape'. Reported to compile on a GNU
279system, 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
5e835c9f 285 "A list of strings to pass to Netscape as arguments."
8749abea
GM
286 :type '(repeat (string :tag "Argument"))
287 :group 'browse-url)
288
289(defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
5e835c9f 290 "A list of strings to pass to Netscape when it starts up.
8749abea
GM
291Defaults 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
7f61a9ee 296(defcustom browse-url-browser-display nil
5e835c9f 297 "The X display for running the browser, if not same as Emacs'."
7f61a9ee
RS
298 :type '(choice string (const :tag "Default" nil))
299 :group 'browse-url)
300
301(defcustom browse-url-mozilla-program "mozilla"
5e835c9f 302 "The name by which to invoke Mozilla."
7f61a9ee
RS
303 :type 'string
304 :group 'browse-url)
305
306(defcustom browse-url-mozilla-arguments nil
5e835c9f 307 "A list of strings to pass to Mozilla as arguments."
7f61a9ee
RS
308 :type '(repeat (string :tag "Argument"))
309 :group 'browse-url)
310
311(defcustom browse-url-mozilla-startup-arguments browse-url-mozilla-arguments
5e835c9f 312 "A list of strings to pass to Mozilla when it starts up.
7f61a9ee
RS
313Defaults 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
7d353d11 318(defcustom browse-url-firefox-program
f9ad64f3 319 (let ((candidates '("firefox" "iceweasel" "icecat")))
7d353d11
SM
320 (while (and candidates (not (executable-find (car candidates))))
321 (setq candidates (cdr candidates)))
322 (or (car candidates) "firefox"))
5e835c9f 323 "The name by which to invoke Firefox."
49866ff8
EZ
324 :type 'string
325 :group 'browse-url)
326
327(defcustom browse-url-firefox-arguments nil
5e835c9f 328 "A list of strings to pass to Firefox as arguments."
49866ff8
EZ
329 :type '(repeat (string :tag "Argument"))
330 :group 'browse-url)
331
332(defcustom browse-url-firefox-startup-arguments browse-url-firefox-arguments
5e835c9f 333 "A list of strings to pass to Firefox when it starts up.
49866ff8
EZ
334Defaults 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
7d353d11 339(defcustom browse-url-galeon-program "galeon"
5e835c9f 340 "The name by which to invoke Galeon."
7f61a9ee
RS
341 :type 'string
342 :group 'browse-url)
343
344(defcustom browse-url-galeon-arguments nil
5e835c9f 345 "A list of strings to pass to Galeon as arguments."
7f61a9ee
RS
346 :type '(repeat (string :tag "Argument"))
347 :group 'browse-url)
348
349(defcustom browse-url-galeon-startup-arguments browse-url-galeon-arguments
5e835c9f 350 "A list of strings to pass to Galeon when it starts up.
7f61a9ee
RS
351Defaults 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
654805e3 356(defcustom browse-url-epiphany-program "epiphany"
5e835c9f 357 "The name by which to invoke Epiphany."
654805e3
RS
358 :type 'string
359 :group 'browse-url)
360
361(defcustom browse-url-epiphany-arguments nil
5e835c9f 362 "A list of strings to pass to Epiphany as arguments."
654805e3
RS
363 :type '(repeat (string :tag "Argument"))
364 :group 'browse-url)
365
366(defcustom browse-url-epiphany-startup-arguments browse-url-epiphany-arguments
5e835c9f 367 "A list of strings to pass to Epiphany when it starts up.
654805e3
RS
368Defaults 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
db324706
RS
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 '()
5e835c9f 377 "A list of strings passed to the GNOME mozilla viewer as arguments."
db324706
RS
378 :version "21.1"
379 :type '(repeat (string :tag "Argument"))
380 :group 'browse-url)
381
d6da15ec 382(defcustom browse-url-mozilla-new-window-is-tab nil
5e835c9f 383 "Whether to open up new windows in a tab or a new window.
d6da15ec
SJ
384If 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
49866ff8 389(defcustom browse-url-firefox-new-window-is-tab nil
5e835c9f 390 "Whether to open up new windows in a tab or a new window.
49866ff8
EZ
391If 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
394This option is currently ignored on MS-Windows, since the necessary
395functionality is not available there."
396 :type 'boolean
397 :group 'browse-url)
398
1d5d4123 399(defcustom browse-url-galeon-new-window-is-tab nil
5e835c9f 400 "Whether to open up new windows in a tab or a new window.
1d5d4123
RS
401If 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
654805e3 406(defcustom browse-url-epiphany-new-window-is-tab nil
5e835c9f 407 "Whether to open up new windows in a tab or a new window.
654805e3
RS
408If 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
fe77c061 413(defcustom browse-url-netscape-new-window-is-tab nil
5e835c9f 414 "Whether to open up new windows in a tab or a new window.
fe77c061
SJ
415If non-nil, then open the URL in a new tab rather than a new
416window if `browse-url-netscape' is asked to open it in a new
417window."
418 :type 'boolean
419 :group 'browse-url)
420
408d5219 421(defcustom browse-url-new-window-flag nil
6eddc3bb 422 "Non-nil means always open a new browser window with appropriate browsers.
8749abea
GM
423Passing an interactive argument to \\[browse-url], or specific browser
424commands reverses the effect of this variable. Requires Netscape version
4251.1N or later or XMosaic version 2.5 or later if using those browsers."
426 :type 'boolean
427 :group 'browse-url)
428
8749abea 429(defcustom browse-url-mosaic-program "xmosaic"
5e835c9f 430 "The name by which to invoke Mosaic (or mMosaic)."
8749abea
GM
431 :type 'string
432 :version "20.3"
433 :group 'browse-url)
434
435(defcustom browse-url-mosaic-arguments nil
5e835c9f 436 "A list of strings to pass to Mosaic as arguments."
8749abea
GM
437 :type '(repeat (string :tag "Argument"))
438 :group 'browse-url)
439
1665be47 440(defcustom browse-url-mosaic-pidfile "~/.mosaicpid"
5e835c9f 441 "The name of the pidfile created by Mosaic."
1665be47
RS
442 :type 'string
443 :group 'browse-url)
444
8749abea 445(defcustom browse-url-filename-alist
5e835c9f 446 `(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
8749abea
GM
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/")
5e835c9f 451 ,@(if (memq system-type '(windows-nt ms-dos cygwin))
5241b291 452 '(("^\\([a-zA-Z]:\\)[\\/]" . "file:///\\1/")
5e835c9f 453 ("^[\\/][\\/]+" . "file://")))
dee8ac10 454 ("^/+" . "file:///"))
5e835c9f 455 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
8749abea
GM
456Any substring of a filename matching one of the REGEXPs is replaced by
457the corresponding STRING using `replace-match', not treating STRING
458literally. All pairs are applied in the order given. The default
4dd14ef0
KS
459value converts ange-ftp/EFS-style file names into ftp URLs and prepends
460`file:' to any file name beginning with `/'.
8749abea
GM
461
462For example, adding to the default a specific translation of an ange-ftp
463address 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/\")
5e835c9f 470 (\"^/+\" . \"file:/\")))"
8749abea
GM
471 :type '(repeat (cons :format "%v"
472 (regexp :tag "Regexp")
473 (string :tag "Replacement")))
3257a734 474 :version "23.1"
8749abea
GM
475 :group 'browse-url)
476
8749abea 477(defcustom browse-url-save-file nil
5e835c9f 478 "If non-nil, save the buffer before displaying its file.
8749abea
GM
479Used by the `browse-url-of-file' command."
480 :type 'boolean
481 :group 'browse-url)
482
483(defcustom browse-url-of-file-hook nil
5e835c9f 484 "Run after `browse-url-of-file' has asked a browser to load a file.
8749abea
GM
485
486Set this to `browse-url-netscape-reload' to force Netscape to load the
487file rather than displaying a cached copy."
488 :type 'hook
489 :options '(browse-url-netscape-reload)
490 :group 'browse-url)
491
8749abea 492(defcustom browse-url-CCI-port 3003
5e835c9f 493 "Port to access XMosaic via CCI.
8749abea
GM
494This can be any number between 1024 and 65535 but must correspond to
495the value set in the browser."
496 :type 'integer
497 :group 'browse-url)
498
499(defcustom browse-url-CCI-host "localhost"
5e835c9f 500 "Host to access XMosaic via CCI.
8749abea
GM
501This should be the host name of the machine running XMosaic with CCI
502enabled. 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)
db6003fb 508
8749abea 509(defcustom browse-url-xterm-program "xterm"
d9774611 510 "The name of the terminal emulator used by `browse-url-text-xterm'.
028f45d2 511This might, for instance, be a separate color version of xterm."
8749abea
GM
512 :type 'string
513 :group 'browse-url)
514
515(defcustom browse-url-xterm-args nil
5e835c9f 516 "A list of strings defining options for `browse-url-xterm-program'.
8749abea
GM
517These might set its size, for instance."
518 :type '(repeat (string :tag "Argument"))
519 :group 'browse-url)
520
8749abea 521(defcustom browse-url-gnudoit-program "gnudoit"
5e835c9f 522 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
8749abea
GM
523 :type 'string
524 :group 'browse-url)
525
526(defcustom browse-url-gnudoit-args '("-q")
5e835c9f 527 "A list of strings defining options for `browse-url-gnudoit-program'.
8749abea
GM
528These might set the port, for instance."
529 :type '(repeat (string :tag "Argument"))
530 :group 'browse-url)
531
8749abea 532(defcustom browse-url-generic-program nil
5e835c9f 533 "The name of the browser program used by `browse-url-generic'."
8749abea
GM
534 :type '(choice string (const :tag "None" nil))
535 :group 'browse-url)
536
537(defcustom browse-url-generic-args nil
5e835c9f 538 "A list of strings defining options for `browse-url-generic-program'."
8749abea
GM
539 :type '(repeat (string :tag "Argument"))
540 :group 'browse-url)
541
542(defcustom browse-url-temp-dir temporary-file-directory
5e835c9f 543 "The name of a directory for browse-url's temporary files.
8749abea
GM
544Such files are generated by functions like `browse-url-of-region'.
545You might want to set this to somewhere with restricted read permissions
546for privacy's sake."
547 :type 'string
548 :group 'browse-url)
549
5e835c9f
SM
550(defcustom browse-url-netscape-version 3
551 "The version of Netscape you are using.
8749abea
GM
552This affects how URL reloading is done; the mechanism changed
553incompatibly at version 4."
554 :type 'number
555 :group 'browse-url)
556
d9774611
RS
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
567The default is none in a window system, otherwise `-show_cursor' to
568indicate the position of the current link in the absence of
569highlighting, 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.
576What to do when sending a new URL to an existing text browser buffer in Emacs
577if the browser cursor is on an input field (in which case the `g' command
8749abea 578would be entered as data). Such fields are recognized by the
d9774611
RS
579underlines ____. Allowed values: nil: disregard it, `warn': warn the
580user and don't emit the URL, `avoid': try to avoid the field by moving
8749abea
GM
581down (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))
d9774611 585 :version "23.1"
8749abea
GM
586 :group 'browse-url)
587
d9774611
RS
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."
64f6f486 590 :type 'integer
d9774611 591 :version "23.1"
64f6f486 592 :group 'browse-url)
8749abea 593
d9774611
RS
594(defcustom browse-url-text-input-delay 0.2
595 "Seconds to wait for a text browser between moves down from an input field."
64f6f486 596 :type 'number
d9774611 597 :version "23.1"
64f6f486 598 :group 'browse-url)
8749abea 599
af06f459 600(defcustom browse-url-kde-program "kfmclient"
5e835c9f 601 "The name by which to invoke the KDE web browser."
af06f459
GM
602 :type 'string
603 :version "21.1"
604 :group 'browse-url)
605
606(defcustom browse-url-kde-args '("openURL")
5e835c9f 607 "A list of strings defining options for `browse-url-kde-program'."
af06f459
GM
608 :type '(repeat (string :tag "Argument"))
609 :group 'browse-url)
610
8c22c51a 611(defcustom browse-url-elinks-wrapper '("xterm" "-e")
1fc7dabf 612 "Wrapper command prepended to the Elinks command-line."
8c22c51a
GM
613 :type '(repeat (string :tag "Wrapper"))
614 :group 'browse-url)
615
616;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
617;; URL encoding
618
6eddc3bb
MC
619(defun browse-url-url-encode-chars (text chars)
620 "URL-encode the chars in TEXT that match CHARS.
da76998b 621CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
59c4e1da 622 (let ((encoded-text (copy-sequence text))
ad56e18b 623 (s 0))
59c4e1da
MC
624 (while (setq s (string-match chars encoded-text s))
625 (setq encoded-text
8c22c51a 626 (replace-match (format "%%%x"
59c4e1da
MC
627 (string-to-char (match-string 0 encoded-text)))
628 t t encoded-text)
ad56e18b 629 s (1+ s)))
59c4e1da 630 encoded-text))
8c22c51a 631
6eddc3bb
MC
632(defun browse-url-encode-url (url)
633 "Escape annoying characters in URL.
da76998b
CY
634The annoying characters are those that can mislead a web browser
635regarding 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 "[)$]"))
6eddc3bb 640
8749abea
GM
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.
4c493999 655If `transient-mark-mode' is non-nil and the mark is active,
657962fc
RS
656it defaults to the current region, else to the URL at or before
657point. If invoked with a mouse button, it moves point to the
658position clicked before acting.
659
660This function returns a list (URL NEW-WINDOW-FLAG)
661for use in `interactive'."
8749abea
GM
662 (let ((event (elt (this-command-keys) 0)))
663 (and (listp event) (mouse-set-point event)))
4c493999
JB
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)))
408d5219 671 (not (eq (null browse-url-new-window-flag)
8749abea
GM
672 (null current-prefix-arg)))))
673
2de9d0c3
RS
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.
01f97560 677(defmacro browse-url-maybe-new-window (arg)
12a3c28c 678 `(if (or noninteractive (not (called-interactively-p 'any)))
01f97560 679 ,arg
408d5219 680 browse-url-new-window-flag))
01f97560 681
8749abea
GM
682;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
683;; Browse current buffer
684
685;;;###autoload
686(defun browse-url-of-file (&optional file)
687 "Ask a WWW browser to display FILE.
688Display the current buffer's file if FILE is nil or if called
689interactively. 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
378f9937 698 (with-current-buffer buf
8749abea
GM
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.
707Use variable `browse-url-filename-alist' to map filenames to URLs."
5241b291
CY
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))))
95e55d20 714 (let ((coding (and (default-value 'enable-multibyte-characters)
59b71501
YM
715 (or file-name-coding-system
716 default-file-name-coding-system))))
717 (if coding (setq file (encode-coding-string file coding))))
6eddc3bb 718 (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
63900fcf
SS
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))))
8749abea
GM
722 file)
723
724;;;###autoload
725(defun browse-url-of-buffer (&optional buffer)
726 "Ask a WWW browser to display BUFFER.
727Display the current buffer if BUFFER is nil. Display only the
728currently visible part of BUFFER (from a temporary file) if buffer is
729narrowed."
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
2e167dc4
RS
744 (expand-file-name "burl" browse-url-temp-dir)
745 nil ".html"))))
8749abea
GM
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
a1ab97d0
GM
759(declare-function dired-get-filename "dired"
760 (&optional localp no-error-if-not-filep))
761
8749abea
GM
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)
2a86a00c
RS
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"))))
8749abea
GM
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.
788Prompts for a URL, defaulting to the URL at or before point. Variable
876d1684
LMI
789`browse-url-browser-function' says which browser to use.
790If the URL is a mailto: URL, consult `browse-url-mailto-function'
791first, if that exists."
8749abea 792 (interactive (browse-url-interactive-arg "URL: "))
32226619 793 (unless (called-interactively-p 'interactive)
408d5219 794 (setq args (or args (list browse-url-new-window-flag))))
876d1684
LMI
795 (let ((process-environment (copy-sequence process-environment))
796 (function (or (and (string-match "\\`mailto:" url)
797 browse-url-mailto-function)
9d982739
GM
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 "~/"))))
addc252e
SM
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)))
876d1684
LMI
809 (if (and (consp function)
810 (not (functionp function)))
9c16fc95
KR
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
876d1684 814 (dolist (bf function)
9c16fc95
KR
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.
876d1684 822 (apply function url args))))
8749abea
GM
823
824;;;###autoload
01f97560 825(defun browse-url-at-point (&optional arg)
8749abea
GM
826 "Ask a WWW browser to load the URL at or before point.
827Doesn't let you edit the URL like `browse-url'. Variable
828`browse-url-browser-function' says which browser to use."
01f97560 829 (interactive "P")
3abc9fa1
DL
830 (let ((url (browse-url-url-at-point)))
831 (if url
832 (browse-url url (if arg
408d5219
GM
833 (not browse-url-new-window-flag)
834 browse-url-new-window-flag))
3abc9fa1 835 (error "No URL found"))))
8749abea
GM
836
837;;;###autoload
838(defun browse-url-at-mouse (event)
839 "Ask a WWW browser to load a URL clicked with the mouse.
840The URL is the one around or before the position of the mouse click
841but point is not changed. Doesn't let you edit the URL like
842`browse-url'. Variable `browse-url-browser-function' says which browser
843to use."
844 (interactive "e")
845 (save-excursion
3abc9fa1 846 (mouse-set-point event)
857356cb
RS
847 ;; This handles browse-url-new-window-flag properly
848 ;; when it gets no arg.
849 (browse-url-at-point)))
8749abea
GM
850
851;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
852;; Browser-specific commands
853
854;; --- Default MS-Windows browser ---
855
14a06a2d 856(defvar dos-windows-version)
73e6adaa 857(declare-function w32-shell-execute "w32fns.c") ;; Defined in C.
14a06a2d 858
8749abea
GM
859(defun browse-url-default-windows-browser (url &optional new-window)
860 (interactive (browse-url-interactive-arg "URL: "))
e572025f
CY
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)
5241b291 866 (call-process "cygstart" nil nil nil url))
e572025f 867 (t (w32-shell-execute "open" url))))
8749abea 868
662871dd
SM
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
8749abea
GM
873;; --- Netscape ---
874
875(defun browse-url-process-environment ()
7f61a9ee
RS
876 "Set DISPLAY in the environment to the X display the browser will use.
877This is either the value of variable `browse-url-browser-display' if
8749abea
GM
878non-nil, or the same display as Emacs if different from the current
879environment, otherwise just use the current environment."
7f61a9ee 880 (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
8749abea
GM
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.
887This is nil if the display is the same as the DISPLAY environment variable.
888
889Actually Emacs could be using several displays; this just returns the
890one showing the selected frame."
891 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
892 (and (not (equal display (getenv "DISPLAY")))
893 display)))
894
7f61a9ee
RS
895(defun browse-url-default-browser (url &rest args)
896 "Find a suitable browser and ask it to load URL.
897Default to the URL around or before point.
898
899When called interactively, if variable `browse-url-new-window-flag' is
900non-nil, load the document in a new window, if possible, otherwise use
901a random existing one. A non-nil interactive prefix argument reverses
902the effect of `browse-url-new-window-flag'.
903
904When called non-interactively, optional second argument NEW-WINDOW is
2a517d45 905used instead of `browse-url-new-window-flag'."
7f61a9ee 906 (apply
6eddc3bb 907 (cond
2a517d45
LM
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)
2269b349 912 ((browse-url-can-use-xdg-open) 'browse-url-xdg-open)
6eddc3bb
MC
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)
d9774611 920 ((executable-find browse-url-xterm-program) 'browse-url-text-xterm)
6eddc3bb
MC
921 ((locate-library "w3") 'browse-url-w3)
922 (t
db6003fb 923 (lambda (&rest ignore) (error "No usable browser found"))))
6eddc3bb 924 url args))
7f61a9ee 925
2269b349
JD
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: "))
1c0f1a19 956 (call-process "xdg-open" nil 0 nil url))
2269b349 957
8749abea
GM
958;;;###autoload
959(defun browse-url-netscape (url &optional new-window)
960 "Ask the Netscape WWW browser to load URL.
8749abea
GM
961Default to the URL around or before point. The strings in variable
962`browse-url-netscape-arguments' are also passed to Netscape.
963
408d5219 964When called interactively, if variable `browse-url-new-window-flag' is
8749abea
GM
965non-nil, load the document in a new Netscape window, otherwise use a
966random existing one. A non-nil interactive prefix argument reverses
408d5219 967the effect of `browse-url-new-window-flag'.
8749abea 968
fe77c061
SJ
969If `browse-url-netscape-new-window-is-tab' is non-nil, then
970whenever a document would otherwise be loaded in a new window, it
971is loaded in a new tab in an existing window instead.
972
8749abea 973When called non-interactively, optional second argument NEW-WINDOW is
408d5219 974used instead of `browse-url-new-window-flag'."
de5552d5 975 (interactive (browse-url-interactive-arg "URL: "))
8c22c51a 976 (setq url (browse-url-encode-url url))
8749abea 977 (let* ((process-environment (browse-url-process-environment))
fe77c061
SJ
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 ")"))))))))
8749abea 996 (set-process-sentinel process
3abc9fa1
DL
997 `(lambda (process change)
998 (browse-url-netscape-sentinel process ,url)))))
8749abea
GM
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
7844257c 1005 (message "Starting %s..." browse-url-netscape-program)
8749abea
GM
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.
1012How 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)"
6eddc3bb 1018 "reload")))
8749abea
GM
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
7f61a9ee
RS
1028;;;###autoload
1029(defun browse-url-mozilla (url &optional new-window)
1030 "Ask the Mozilla WWW browser to load URL.
1031Default to the URL around or before point. The strings in variable
1032`browse-url-mozilla-arguments' are also passed to Mozilla.
1033
1034When called interactively, if variable `browse-url-new-window-flag' is
1035non-nil, load the document in a new Mozilla window, otherwise use a
1036random existing one. A non-nil interactive prefix argument reverses
1037the effect of `browse-url-new-window-flag'.
1038
d6da15ec
SJ
1039If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
1040document would otherwise be loaded in a new window, it is loaded in a
1041new tab in an existing window instead.
1042
7f61a9ee
RS
1043When called non-interactively, optional second argument NEW-WINDOW is
1044used instead of `browse-url-new-window-flag'."
1045 (interactive (browse-url-interactive-arg "URL: "))
8c22c51a 1046 (setq url (browse-url-encode-url url))
7f61a9ee 1047 (let* ((process-environment (browse-url-process-environment))
d6da15ec
SJ
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
c29cf9a4
RS
1060 ",new-tab"
1061 ",new-window"))
d6da15ec 1062 ")"))))))
7f61a9ee
RS
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
7844257c 1072 (message "Starting %s..." browse-url-mozilla-program)
7f61a9ee
RS
1073 (apply 'start-process (concat "mozilla " url) nil
1074 browse-url-mozilla-program
1075 (append browse-url-mozilla-startup-arguments (list url))))))
49866ff8
EZ
1076
1077;;;###autoload
1078(defun browse-url-firefox (url &optional new-window)
1079 "Ask the Firefox WWW browser to load URL.
1080Default to the URL around or before point. The strings in
1081variable `browse-url-firefox-arguments' are also passed to
1082Firefox.
1083
1084When called interactively, if variable
1085`browse-url-new-window-flag' is non-nil, load the document in a
1086new Firefox window, otherwise use a random existing one. A
1087non-nil interactive prefix argument reverses the effect of
1088`browse-url-new-window-flag'.
1089
1090If `browse-url-firefox-new-window-is-tab' is non-nil, then
1091whenever a document would otherwise be loaded in a new window, it
1092is loaded in a new tab in an existing window instead.
1093
1094When called non-interactively, optional second argument
1095NEW-WINDOW is used instead of `browse-url-new-window-flag'.
1096
1097On MS-Windows systems the optional `new-window' parameter is
1098ignored. Firefox for Windows does not support the \"-remote\"
1099command line parameter. Therefore, the
1100`browse-url-new-window-flag' and `browse-url-firefox-new-window-is-tab'
1101are ignored as well. Firefox on Windows will always open the requested
1102URL in a new window."
1103 (interactive (browse-url-interactive-arg "URL: "))
8c22c51a 1104 (setq url (browse-url-encode-url url))
49866ff8 1105 (let* ((process-environment (browse-url-process-environment))
a3f2468a
CY
1106 (use-remote
1107 (not (memq system-type '(windows-nt ms-dos))))
49866ff8
EZ
1108 (process
1109 (apply 'start-process
1110 (concat "firefox " url) nil
1111 browse-url-firefox-program
1112 (append
1113 browse-url-firefox-arguments
a3f2468a
CY
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))))))
49866ff8
EZ
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))))))
7f61a9ee
RS
1142
1143;;;###autoload
1144(defun browse-url-galeon (url &optional new-window)
1145 "Ask the Galeon WWW browser to load URL.
1146Default to the URL around or before point. The strings in variable
1147`browse-url-galeon-arguments' are also passed to Galeon.
1148
1149When called interactively, if variable `browse-url-new-window-flag' is
1150non-nil, load the document in a new Galeon window, otherwise use a
1151random existing one. A non-nil interactive prefix argument reverses
1152the effect of `browse-url-new-window-flag'.
1153
b77784f7
RS
1154If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1155document would otherwise be loaded in a new window, it is loaded in a
1156new tab in an existing window instead.
1157
7f61a9ee
RS
1158When called non-interactively, optional second argument NEW-WINDOW is
1159used instead of `browse-url-new-window-flag'."
1160 (interactive (browse-url-interactive-arg "URL: "))
5edcabcb 1161 (setq url (browse-url-encode-url url))
7f61a9ee
RS
1162 (let* ((process-environment (browse-url-process-environment))
1163 (process (apply 'start-process
654805e3
RS
1164 (concat "galeon " url)
1165 nil
7f61a9ee
RS
1166 browse-url-galeon-program
1167 (append
1168 browse-url-galeon-arguments
1d5d4123
RS
1169 (if (browse-url-maybe-new-window new-window)
1170 (if browse-url-galeon-new-window-is-tab
1171 '("--new-tab")
b77784f7
RS
1172 '("--new-window" "--noraise"))
1173 '("--existing"))
1174 (list url)))))
7f61a9ee
RS
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
7844257c 1184 (message "Starting %s..." browse-url-galeon-program)
7f61a9ee
RS
1185 (apply 'start-process (concat "galeon " url) nil
1186 browse-url-galeon-program
1187 (append browse-url-galeon-startup-arguments (list url))))))
1188
654805e3
RS
1189(defun browse-url-epiphany (url &optional new-window)
1190 "Ask the Epiphany WWW browser to load URL.
1191Default to the URL around or before point. The strings in variable
1192`browse-url-galeon-arguments' are also passed to Epiphany.
1193
1194When called interactively, if variable `browse-url-new-window-flag' is
1195non-nil, load the document in a new Epiphany window, otherwise use a
1196random existing one. A non-nil interactive prefix argument reverses
1197the effect of `browse-url-new-window-flag'.
1198
674dd916 1199If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
654805e3
RS
1200document would otherwise be loaded in a new window, it is loaded in a
1201new tab in an existing window instead.
1202
1203When called non-interactively, optional second argument NEW-WINDOW is
1204used instead of `browse-url-new-window-flag'."
1205 (interactive (browse-url-interactive-arg "URL: "))
8c22c51a 1206 (setq url (browse-url-encode-url url))
654805e3
RS
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
7844257c 1229 (message "Starting %s..." browse-url-epiphany-program)
654805e3
RS
1230 (apply 'start-process (concat "epiphany " url) nil
1231 browse-url-epiphany-program
1232 (append browse-url-epiphany-startup-arguments (list url))))))
1233
3fe5c37a
DN
1234(defvar url-handler-regexp)
1235
378f9937
SM
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
b12e6de3 1250;;;###autoload
de5552d5
DL
1251(defun browse-url-gnome-moz (url &optional new-window)
1252 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1253Default to the URL around or before point. The strings in variable
1254`browse-url-gnome-moz-arguments' are also passed.
1255
408d5219 1256When called interactively, if variable `browse-url-new-window-flag' is
de5552d5
DL
1257non-nil, load the document in a new browser window, otherwise use an
1258existing one. A non-nil interactive prefix argument reverses the
408d5219 1259effect of `browse-url-new-window-flag'.
de5552d5
DL
1260
1261When called non-interactively, optional second argument NEW-WINDOW is
408d5219 1262used instead of `browse-url-new-window-flag'."
c60ee5e7 1263 (interactive (browse-url-interactive-arg "URL: "))
de5552d5
DL
1264 (apply 'start-process (concat "gnome-moz-remote " url)
1265 nil
662871dd 1266 browse-url-gnome-moz-program
de5552d5
DL
1267 (append
1268 browse-url-gnome-moz-arguments
1269 (if (browse-url-maybe-new-window new-window)
6eddc3bb 1270 '("--newwin"))
de5552d5
DL
1271 (list "--raise" url))))
1272
8749abea
GM
1273;; --- Mosaic ---
1274
1275;;;###autoload
1276(defun browse-url-mosaic (url &optional new-window)
1277 "Ask the XMosaic WWW browser to load URL.
1278
1279Default to the URL around or before point. The strings in variable
1280`browse-url-mosaic-arguments' are also passed to Mosaic and the
1281program is invoked according to the variable
1282`browse-url-mosaic-program'.
1283
408d5219 1284When called interactively, if variable `browse-url-new-window-flag' is
8749abea
GM
1285non-nil, load the document in a new Mosaic window, otherwise use a
1286random existing one. A non-nil interactive prefix argument reverses
408d5219 1287the effect of `browse-url-new-window-flag'.
8749abea
GM
1288
1289When called non-interactively, optional second argument NEW-WINDOW is
408d5219 1290used instead of `browse-url-new-window-flag'."
8749abea 1291 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1665be47 1292 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
8749abea
GM
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)
01f97560 1304 (insert (if (browse-url-maybe-new-window new-window)
8749abea
GM
1305 "newwin\n"
1306 "goto\n")
1307 url "\n")
1308 (save-buffer)
1309 (kill-buffer nil)
1310 ;; Send signal SIGUSR to Mosaic
84c2714c 1311 (message "Signaling Mosaic...")
662871dd 1312 (signal-process pid 'SIGUSR1)
8749abea
GM
1313 ;; Or you could try:
1314 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
84c2714c 1315 (message "Signaling Mosaic...done")
8749abea
GM
1316 )
1317 ;; Mosaic not running - start it
7844257c 1318 (message "Starting %s..." browse-url-mosaic-program)
8749abea
GM
1319 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1320 (append browse-url-mosaic-arguments (list url)))
7844257c 1321 (message "Starting %s...done" browse-url-mosaic-program))))
8749abea 1322
8749abea
GM
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.
1328Default to the URL around or before point.
1329
1330This function only works for XMosaic version 2.5 or later. You must
1331select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1332value of variable `browse-url-CCI-port', and enable `Accept requests'.
1333
408d5219 1334When called interactively, if variable `browse-url-new-window-flag' is
8749abea
GM
1335non-nil, load the document in a new browser window, otherwise use a
1336random existing one. A non-nil interactive prefix argument reverses
408d5219 1337the effect of `browse-url-new-window-flag'.
8749abea
GM
1338
1339When called non-interactively, optional second argument NEW-WINDOW is
408d5219 1340used instead of `browse-url-new-window-flag'."
8749abea
GM
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 "
01f97560 1347 (if (browse-url-maybe-new-window new-window)
8749abea
GM
1348 "new"
1349 "current")
1350 "\r\n"))
1351 (process-send-string "browse-url" "disconnect\r\n")
1352 (delete-process "browse-url"))
1353
8749abea
GM
1354;; --- W3 ---
1355
352d2dea
GM
1356;; External.
1357(declare-function w3-fetch-other-window "ext:w3m" (&optional url))
1358(declare-function w3-fetch "ext:w3m" (&optional url target))
1359
8749abea
GM
1360;;;###autoload
1361(defun browse-url-w3 (url &optional new-window)
1362 "Ask the w3 WWW browser to load URL.
1363Default to the URL around or before point.
1364
408d5219 1365When called interactively, if variable `browse-url-new-window-flag' is
8749abea 1366non-nil, load the document in a new window. A non-nil interactive
408d5219 1367prefix argument reverses the effect of `browse-url-new-window-flag'.
8749abea
GM
1368
1369When called non-interactively, optional second argument NEW-WINDOW is
408d5219 1370used instead of `browse-url-new-window-flag'."
8749abea 1371 (interactive (browse-url-interactive-arg "W3 URL: "))
6eddc3bb 1372 (require 'w3) ; w3-fetch-other-window not autoloaded
01f97560 1373 (if (browse-url-maybe-new-window new-window)
8749abea
GM
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.
1381The `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: "))
6eddc3bb
MC
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)"))))
8749abea
GM
1389
1390;; --- Lynx in an xterm ---
1391
1392;;;###autoload
d9774611 1393(defun browse-url-text-xterm (url &optional new-window)
8749abea 1394 ;; new-window ignored
d9774611 1395 "Ask a text browser to load URL.
db6003fb 1396URL defaults to the URL around or before point.
d9774611 1397This runs the text browser specified by `browse-url-text-browser'.
8749abea
GM
1398in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1399with possible additional arguments `browse-url-xterm-args'."
d9774611
RS
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
03e49c06 1403 ,@browse-url-xterm-args "-e" ,browse-url-text-browser
6eddc3bb 1404 ,url)))
8749abea
GM
1405
1406;; --- Lynx in an Emacs "term" window ---
1407
a1ab97d0
GM
1408(declare-function term-char-mode "term" ())
1409(declare-function term-send-down "term" ())
1410(declare-function term-send-string "term" (proc str))
1411
8749abea 1412;;;###autoload
d9774611
RS
1413(defun browse-url-text-emacs (url &optional new-buffer)
1414 "Ask a text browser to load URL.
db6003fb 1415URL defaults to the URL around or before point.
d9774611
RS
1416This runs the text browser specified by `browse-url-text-browser'.
1417With a prefix argument, it runs a new browser process in a new buffer.
8749abea 1418
408d5219 1419When called interactively, if variable `browse-url-new-window-flag' is
d9774611 1420non-nil, load the document in a new browser process in a new term window,
8749abea 1421otherwise use any existing one. A non-nil interactive prefix argument
408d5219 1422reverses the effect of `browse-url-new-window-flag'.
8749abea
GM
1423
1424When called non-interactively, optional second argument NEW-WINDOW is
408d5219 1425used instead of `browse-url-new-window-flag'."
d9774611 1426 (interactive (browse-url-interactive-arg "Text browser URL: "))
6eddc3bb 1427 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
8749abea 1428 ;; (term-term-name "vt100") ; ??
d9774611 1429 (buf (get-buffer "*text browser*"))
8749abea 1430 (proc (and buf (get-buffer-process buf)))
d9774611 1431 (n browse-url-text-input-attempts))
a1ab97d0 1432 (require 'term)
01f97560 1433 (if (and (browse-url-maybe-new-window new-buffer) buf)
8749abea
GM
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)))
01f97560 1439 (if (or (browse-url-maybe-new-window new-buffer)
8749abea
GM
1440 (not buf)
1441 (not proc)
1442 (not (memq (process-status proc) '(run stop))))
d9774611 1443 ;; start a new text browser
8749abea
GM
1444 (progn
1445 (setq buf
1446 (apply #'make-term
d9774611
RS
1447 `(,browse-url-text-browser
1448 ,browse-url-text-browser
1449 nil ,@browse-url-text-emacs-args
01f97560 1450 ,url)))
8749abea
GM
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)))))))
d9774611 1461 ;; Send the url to the text browser in the old buffer
8749abea
GM
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) ?_)
d9774611 1467 (cond ((eq browse-url-text-input-field 'warn)
e8af40ee 1468 (error "Please move out of the input field first"))
d9774611 1469 ((eq browse-url-text-input-field 'avoid)
8749abea 1470 (while (and (eq (following-char) ?_) (> n 0))
6eddc3bb 1471 (term-send-down) ; down arrow
d9774611 1472 (sit-for browse-url-text-input-delay))
8749abea 1473 (if (eq (following-char) ?_)
e8af40ee 1474 (error "Cannot move out of the input field, sorry")))))
6eddc3bb 1475 (term-send-string proc (concat "g" ; goto
8749abea
GM
1476 "\C-u" ; kill default url
1477 url
1478 "\r")))))
1479
8749abea
GM
1480;; --- mailto ---
1481
1abcd088
EZ
1482(autoload 'rfc2368-parse-mailto-url "rfc2368")
1483
8749abea
GM
1484;;;###autoload
1485(defun browse-url-mail (url &optional new-window)
1abcd088 1486 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
8749abea
GM
1487Default to using the mailto: URL around or before point as the
1488recipient's address. Supplying a non-nil interactive prefix argument
1489will cause the mail to be composed in another window rather than the
1490current one.
1491
408d5219 1492When called interactively, if variable `browse-url-new-window-flag' is
8749abea
GM
1493non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1494non-nil interactive prefix argument reverses the effect of
408d5219 1495`browse-url-new-window-flag'.
8749abea
GM
1496
1497When called non-interactively, optional second argument NEW-WINDOW is
408d5219 1498used instead of `browse-url-new-window-flag'."
8749abea
GM
1499 (interactive (browse-url-interactive-arg "Mailto URL: "))
1500 (save-excursion
1abcd088
EZ
1501 (let* ((alist (rfc2368-parse-mailto-url url))
1502 (to (assoc "To" alist))
1503 (subject (assoc "Subject" alist))
1504 (body (assoc "Body" alist))
5ad3e885 1505 (rest (delq to (delq subject (delq body alist))))
1abcd088
EZ
1506 (to (cdr to))
1507 (subject (cdr subject))
1508 (body (cdr body))
1509 (mail-citation-hook (unless body mail-citation-hook)))
01f97560 1510 (if (browse-url-maybe-new-window new-window)
1abcd088 1511 (compose-mail-other-window to subject rest nil
5ad3e885 1512 (list 'insert-buffer (current-buffer)))
1abcd088 1513 (compose-mail to subject rest nil nil
5ad3e885
KY
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"))))))))
8749abea
GM
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.
1533Default to the URL around or before point. A fresh copy of the
1534browser is started up in a new process with possible additional arguments
1535`browse-url-generic-args'. This is appropriate for browsers which
1536don't offer a form of remote control."
1537 (interactive (browse-url-interactive-arg "URL: "))
1538 (if (not browse-url-generic-program)
6eddc3bb 1539 (error "No browser defined (`browse-url-generic-program')"))
78cfc6ad
DK
1540 (apply 'call-process browse-url-generic-program nil
1541 0 nil
8749abea
GM
1542 (append browse-url-generic-args (list url))))
1543
af06f459
GM
1544;;;###autoload
1545(defun browse-url-kde (url &optional new-window)
1546 "Ask the KDE WWW browser to load URL.
1547Default to the URL around or before point."
1548 (interactive (browse-url-interactive-arg "KDE URL: "))
1549 (message "Sending URL to KDE...")
662871dd 1550 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
6eddc3bb 1551 (append browse-url-kde-args (list url))))
af06f459 1552
a38de16e
MC
1553(defun browse-url-elinks-new-window (url)
1554 "Ask the Elinks WWW browser to load URL in a new window."
db6003fb 1555 (let ((process-environment (browse-url-process-environment)))
a38de16e
MC
1556 (apply #'start-process
1557 (append (list (concat "elinks:" url)
1558 nil)
1559 browse-url-elinks-wrapper
1560 (list "elinks" url)))))
1561
8c22c51a 1562;;;###autoload
a38de16e 1563(defun browse-url-elinks (url &optional new-window)
8c22c51a
GM
1564 "Ask the Elinks WWW browser to load URL.
1565Default to the URL around the point.
1566
1567The document is loaded in a new tab of a running Elinks or, if
1568none yet running, a newly started instance.
1569
1570The Elinks command will be prepended by the program+arguments
a38de16e 1571from `browse-url-elinks-wrapper'."
8c22c51a
GM
1572 (interactive (browse-url-interactive-arg "URL: "))
1573 (setq url (browse-url-encode-url url))
a38de16e
MC
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))))))
8c22c51a
GM
1582
1583(defun browse-url-elinks-sentinel (process url)
1584 "Determines if Elinks is running or a new one has to be started."
a38de16e 1585 (let ((exit-status (process-exit-status process)))
8c22c51a
GM
1586 ;; Try to determine if an instance is running or if we have to
1587 ;; create a new one.
1588 (case exit-status
6eddc3bb
MC
1589 (5
1590 ;; No instance, start a new one.
a38de16e 1591 (browse-url-elinks-new-window url))
6eddc3bb
MC
1592 (0
1593 ;; Found an instance, open URL in new tab.
a38de16e
MC
1594 (let ((process-environment (browse-url-process-environment)))
1595 (start-process (concat "elinks:" url) nil
1596 "elinks" "-remote"
1597 (concat "openURL(\"" url "\",new-tab)"))))
6eddc3bb 1598 (otherwise
a38de16e
MC
1599 (error "Unrecognized exit-code %d of process `elinks'"
1600 exit-status)))))
8c22c51a 1601
8749abea
GM
1602(provide 'browse-url)
1603
1604;;; browse-url.el ends here