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