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