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