1 ;;; browse-url.el --- pass a URL to a WWW browser
3 ;; Copyright (C) 1995-2011 Free Software Foundation, Inc.
5 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
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/>.
27 ;; This package provides functions which read a URL (Uniform Resource
28 ;; Locator) from the minibuffer, defaulting to the URL around point,
29 ;; and ask a World-Wide Web browser to load it. It can also load the
30 ;; URL associated with the current buffer. Different browsers use
31 ;; different methods of remote control so there is one function for
32 ;; each supported browser. If the chosen browser is not running, it
33 ;; is started. Currently there is support for the following browsers,
34 ;; some of them probably now obsolete:
36 ;; Function Browser Earliest version
37 ;; browse-url-mozilla Mozilla Don't know
38 ;; browse-url-firefox Firefox Don't know (tried with 1.0.1)
39 ;; browse-url-galeon Galeon Don't know
40 ;; browse-url-epiphany Epiphany Don't know
41 ;; browse-url-netscape Netscape 1.1b1
42 ;; browse-url-mosaic XMosaic/mMosaic <= 2.4
43 ;; browse-url-cci XMosaic 2.5
45 ;; browse-url-w3-gnudoit w3 remotely
46 ;; browse-url-text-* Any text browser 0
47 ;; browse-url-generic arbitrary
48 ;; browse-url-default-windows-browser MS-Windows browser
49 ;; browse-url-default-macosx-browser Mac OS X browser
50 ;; browse-url-gnome-moz GNOME interface to Mozilla
51 ;; browse-url-kde KDE konqueror (kfm)
52 ;; browse-url-elinks Elinks Don't know (tried with 0.12.GIT)
54 ;; [A version of the Netscape browser is now free software
55 ;; <URL:http://www.mozilla.org/>, albeit not GPLed, so it is
56 ;; reasonable to have that as the default.]
58 ;; Note that versions of Netscape before 1.1b1 did not have remote
59 ;; control. <URL:http://www.netscape.com/newsref/std/x-remote.html>.
61 ;; Browsers can cache Web pages so it may be necessary to tell them to
62 ;; reload the current page if it has changed (e.g. if you have edited
63 ;; it). There is currently no perfect automatic solution to this.
65 ;; Netscape allows you to specify the id of the window you want to
66 ;; control but which window DO you want to control and how do you
69 ;; William M. Perry's excellent "w3" WWW browser for
70 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
71 ;; has a function w3-follow-url-at-point, but that
72 ;; doesn't let you edit the URL like browse-url.
73 ;; The `gnuserv' package that can be used to control it in another
74 ;; Emacs process is available from
75 ;; <URL:ftp://ftp.splode.com/pub/users/friedman/packages/>.
77 ;; Lynx is now distributed by the FSF. See also
78 ;; <URL:http://lynx.browser.org/>.
80 ;; Free graphical browsers that could be used by `browse-url-generic'
81 ;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
82 ;; <URL:http://www.unlv.edu/chimera/>, Arena
83 ;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena> and Amaya
84 ;; <URL:ftp://ftp.w3.org/pub/amaya>. mMosaic
85 ;; <URL:ftp://ftp.enst.fr/pub/mbone/mMosaic/>,
86 ;; <URL:http://www.enst.fr/~dauphin/mMosaic/> (with development
87 ;; support for Java applets and multicast) can be used like Mosaic by
88 ;; setting `browse-url-mosaic-program' appropriately.
90 ;; I [Denis Howe, not Dave Love] recommend Nelson Minar
91 ;; <nelson@santafe.edu>'s excellent html-helper-mode.el for editing
92 ;; HTML and thank Nelson for his many useful comments on this code.
93 ;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>
95 ;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
96 ;; software/own/hm--html-menus/>. For composing correct HTML see also
97 ;; PSGML the general SGML structure editor package
98 ;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
101 ;; This package generalises function html-previewer-process in Marc
102 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
103 ;; ffap.el package. The huge hyperbole package also contains similar
106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
109 ;; Can you write and test some code for the Macintrash and Windoze
110 ;; Netscape remote control APIs? (See the URL above).
112 ;; Do any other browsers have remote control?
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117 ;; To display the URL at or before point:
118 ;; M-x browse-url-at-point RET
119 ;; or, similarly but with the opportunity to edit the URL extracted from
123 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
125 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
126 ;; (Note that using Shift-mouse-1 is not desirable because
127 ;; that event has a standard meaning in Emacs.)
129 ;; To display the current buffer in a web browser:
130 ;; M-x browse-url-of-buffer RET
132 ;; To display the current region in a web browser:
133 ;; M-x browse-url-of-region RET
135 ;; In Dired, to display the file named on the current line:
136 ;; M-x browse-url-of-dired-file RET
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
139 ;; Customisation (~/.emacs)
141 ;; To see what variables are available for customization, type
142 ;; `M-x set-variable browse-url TAB'. Better, use
143 ;; `M-x customize-group browse-url'.
145 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
146 ;; (as used by html-helper-mode):
147 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
148 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
149 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
150 ;; (global-set-key "\C-c\C-zu" 'browse-url)
151 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
152 ;; (add-hook 'dired-mode-hook
154 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
156 ;; Browse URLs in mail messages under RMAIL by clicking mouse-2:
157 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
158 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
159 ;; Alternatively, add `goto-address' to `rmail-show-message-hook'.
161 ;; Gnus provides a standard feature to activate URLs in article
162 ;; buffers for invocation of browse-url.
164 ;; Use the Emacs w3 browser when not running under X11:
165 ;; (or (eq window-system 'x)
166 ;; (setq browse-url-browser-function 'browse-url-w3))
168 ;; To always save modified buffers before displaying the file in a browser:
169 ;; (setq browse-url-save-file t)
171 ;; To get round the Netscape caching problem, you could EITHER have
172 ;; write-file in html-helper-mode make Netscape reload the document:
174 ;; (autoload 'browse-url-netscape-reload "browse-url"
175 ;; "Ask a WWW browser to redisplay the current file." t)
176 ;; (add-hook 'html-helper-mode-hook
178 ;; (add-hook 'local-write-file-hooks
180 ;; (let ((local-write-file-hooks))
182 ;; (browse-url-netscape-reload)
183 ;; t) ; => file written by hook
184 ;; t))) ; append to l-w-f-hooks
186 ;; OR have browse-url-of-file ask Netscape to load and then reload the
189 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
191 ;; You may also want to customise browse-url-netscape-arguments, e.g.
192 ;; (setq browse-url-netscape-arguments '("-install"))
194 ;; or similarly for the other browsers.
196 ;; To invoke different browsers for different URLs:
197 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
198 ;; ("." . browse-url-netscape)))
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
206 (eval-when-compile (require 'cl))
208 (defgroup browse-url nil
209 "Use a web browser to look at a URL."
210 :prefix "browse-url-"
211 :link '(emacs-commentary-link "browse-url")
216 (defcustom browse-url-browser-function
218 ((memq system-type '(windows-nt ms-dos cygwin))
219 'browse-url-default-windows-browser)
220 ((memq system-type '(darwin))
221 'browse-url-default-macosx-browser)
223 'browse-url-default-browser))
224 "Function to display the current buffer in a WWW browser.
225 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
226 `browse-url-of-file' commands.
228 If the value is not a function it should be a list of pairs
229 \(REGEXP . FUNCTION). In this case the function called will be the one
230 associated with the first REGEXP which matches the current URL. The
231 function is passed the URL and any other args of `browse-url'. The last
232 regexp should probably be \".\" to specify a default browser."
234 (function-item :tag "Emacs W3" :value browse-url-w3)
235 (function-item :tag "W3 in another Emacs via `gnudoit'"
236 :value browse-url-w3-gnudoit)
237 (function-item :tag "Mozilla" :value browse-url-mozilla)
238 (function-item :tag "Firefox" :value browse-url-firefox)
239 (function-item :tag "Galeon" :value browse-url-galeon)
240 (function-item :tag "Epiphany" :value browse-url-epiphany)
241 (function-item :tag "Netscape" :value browse-url-netscape)
242 (function-item :tag "Mosaic" :value browse-url-mosaic)
243 (function-item :tag "Mosaic using CCI" :value browse-url-cci)
244 (function-item :tag "Text browser in an xterm window"
245 :value browse-url-text-xterm)
246 (function-item :tag "Text browser in an Emacs window"
247 :value browse-url-text-emacs)
248 (function-item :tag "KDE" :value browse-url-kde)
249 (function-item :tag "Elinks" :value browse-url-elinks)
250 (function-item :tag "Specified by `Browse Url Generic Program'"
251 :value browse-url-generic)
252 (function-item :tag "Default Windows browser"
253 :value browse-url-default-windows-browser)
254 (function-item :tag "Default Mac OS X browser"
255 :value browse-url-default-macosx-browser)
256 (function-item :tag "GNOME invoking Mozilla"
257 :value browse-url-gnome-moz)
258 (function-item :tag "Default browser"
259 :value browse-url-default-browser)
260 (function :tag "Your own function")
261 (alist :tag "Regexp/function association list"
262 :key-type regexp :value-type function))
266 (defcustom browse-url-mailto-function 'browse-url-mail
267 "Function to display mailto: links.
268 This variable uses the same syntax as the
269 `browse-url-browser-function' variable. If the
270 `browse-url-mailto-function' variable is nil, that variable will
273 (function-item :tag "Emacs Mail" :value browse-url-mail)
274 (function-item :tag "None" nil))
278 (defcustom browse-url-netscape-program "netscape"
279 ;; Info about netscape-remote from Karl Berry.
280 "The name by which to invoke Netscape.
282 The free program `netscape-remote' from
283 <URL:http://home.netscape.com/newsref/std/remote.c> is said to start
284 up very much quicker than `netscape'. Reported to compile on a GNU
285 system, given vroot.h from the same directory, with cc flags
286 -DSTANDALONE -L/usr/X11R6/lib -lXmu -lX11."
290 (defcustom browse-url-netscape-arguments nil
291 "A list of strings to pass to Netscape as arguments."
292 :type '(repeat (string :tag "Argument"))
295 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
296 "A list of strings to pass to Netscape when it starts up.
297 Defaults to the value of `browse-url-netscape-arguments' at the time
298 `browse-url' is loaded."
299 :type '(repeat (string :tag "Argument"))
302 (defcustom browse-url-browser-display nil
303 "The X display for running the browser, if not same as Emacs'."
304 :type '(choice string (const :tag "Default" nil))
307 (defcustom browse-url-mozilla-program "mozilla"
308 "The name by which to invoke Mozilla."
312 (defcustom browse-url-mozilla-arguments nil
313 "A list of strings to pass to Mozilla as arguments."
314 :type '(repeat (string :tag "Argument"))
317 (defcustom browse-url-mozilla-startup-arguments browse-url-mozilla-arguments
318 "A list of strings to pass to Mozilla when it starts up.
319 Defaults to the value of `browse-url-mozilla-arguments' at the time
320 `browse-url' is loaded."
321 :type '(repeat (string :tag "Argument"))
324 (defcustom browse-url-firefox-program
325 (let ((candidates '("firefox" "iceweasel")))
326 (while (and candidates (not (executable-find (car candidates))))
327 (setq candidates (cdr candidates)))
328 (or (car candidates) "firefox"))
329 "The name by which to invoke Firefox."
333 (defcustom browse-url-firefox-arguments nil
334 "A list of strings to pass to Firefox as arguments."
335 :type '(repeat (string :tag "Argument"))
338 (defcustom browse-url-firefox-startup-arguments browse-url-firefox-arguments
339 "A list of strings to pass to Firefox when it starts up.
340 Defaults to the value of `browse-url-firefox-arguments' at the time
341 `browse-url' is loaded."
342 :type '(repeat (string :tag "Argument"))
345 (defcustom browse-url-galeon-program "galeon"
346 "The name by which to invoke Galeon."
350 (defcustom browse-url-galeon-arguments nil
351 "A list of strings to pass to Galeon as arguments."
352 :type '(repeat (string :tag "Argument"))
355 (defcustom browse-url-galeon-startup-arguments browse-url-galeon-arguments
356 "A list of strings to pass to Galeon when it starts up.
357 Defaults to the value of `browse-url-galeon-arguments' at the time
358 `browse-url' is loaded."
359 :type '(repeat (string :tag "Argument"))
362 (defcustom browse-url-epiphany-program "epiphany"
363 "The name by which to invoke Epiphany."
367 (defcustom browse-url-epiphany-arguments nil
368 "A list of strings to pass to Epiphany as arguments."
369 :type '(repeat (string :tag "Argument"))
372 (defcustom browse-url-epiphany-startup-arguments browse-url-epiphany-arguments
373 "A list of strings to pass to Epiphany when it starts up.
374 Defaults to the value of `browse-url-epiphany-arguments' at the time
375 `browse-url' is loaded."
376 :type '(repeat (string :tag "Argument"))
379 ;; GNOME means of invoking either Mozilla or Netrape.
380 (defvar browse-url-gnome-moz-program "gnome-moz-remote")
382 (defcustom browse-url-gnome-moz-arguments '()
383 "A list of strings passed to the GNOME mozilla viewer as arguments."
385 :type '(repeat (string :tag "Argument"))
388 (defcustom browse-url-mozilla-new-window-is-tab nil
389 "Whether to open up new windows in a tab or a new window.
390 If non-nil, then open the URL in a new tab rather than a new window if
391 `browse-url-mozilla' is asked to open it in a new window."
395 (defcustom browse-url-firefox-new-window-is-tab nil
396 "Whether to open up new windows in a tab or a new window.
397 If non-nil, then open the URL in a new tab rather than a new window if
398 `browse-url-firefox' is asked to open it in a new window.
400 This option is currently ignored on MS-Windows, since the necessary
401 functionality is not available there."
405 (defcustom browse-url-galeon-new-window-is-tab nil
406 "Whether to open up new windows in a tab or a new window.
407 If non-nil, then open the URL in a new tab rather than a new window if
408 `browse-url-galeon' is asked to open it in a new window."
412 (defcustom browse-url-epiphany-new-window-is-tab nil
413 "Whether to open up new windows in a tab or a new window.
414 If non-nil, then open the URL in a new tab rather than a new window if
415 `browse-url-epiphany' is asked to open it in a new window."
419 (defcustom browse-url-netscape-new-window-is-tab nil
420 "Whether to open up new windows in a tab or a new window.
421 If non-nil, then open the URL in a new tab rather than a new
422 window if `browse-url-netscape' is asked to open it in a new
427 (defcustom browse-url-new-window-flag nil
428 "Non-nil means always open a new browser window with appropriate browsers.
429 Passing an interactive argument to \\[browse-url], or specific browser
430 commands reverses the effect of this variable. Requires Netscape version
431 1.1N or later or XMosaic version 2.5 or later if using those browsers."
435 (defcustom browse-url-mosaic-program "xmosaic"
436 "The name by which to invoke Mosaic (or mMosaic)."
441 (defcustom browse-url-mosaic-arguments nil
442 "A list of strings to pass to Mosaic as arguments."
443 :type '(repeat (string :tag "Argument"))
446 (defcustom browse-url-mosaic-pidfile "~/.mosaicpid"
447 "The name of the pidfile created by Mosaic."
451 (defcustom browse-url-filename-alist
452 `(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
453 ;; The above loses the username to avoid the browser prompting for
454 ;; it in anonymous cases. If it's not anonymous the next regexp
456 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
457 ,@(if (memq system-type '(windows-nt ms-dos cygwin))
458 '(("^\\([a-zA-Z]:\\)[\\/]" . "file:///\\1/")
459 ("^[\\/][\\/]+" . "file://")))
460 ("^/+" . "file:///"))
461 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
462 Any substring of a filename matching one of the REGEXPs is replaced by
463 the corresponding STRING using `replace-match', not treating STRING
464 literally. All pairs are applied in the order given. The default
465 value converts ange-ftp/EFS-style file names into ftp URLs and prepends
466 `file:' to any file name beginning with `/'.
468 For example, adding to the default a specific translation of an ange-ftp
469 address to an HTTP URL:
471 (setq browse-url-filename-alist
472 '((\"/webmaster@webserver:/home/www/html/\" .
473 \"http://www.acme.co.uk/\")
474 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
475 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
476 (\"^/+\" . \"file:/\")))"
477 :type '(repeat (cons :format "%v"
478 (regexp :tag "Regexp")
479 (string :tag "Replacement")))
483 (defcustom browse-url-save-file nil
484 "If non-nil, save the buffer before displaying its file.
485 Used by the `browse-url-of-file' command."
489 (defcustom browse-url-of-file-hook nil
490 "Run after `browse-url-of-file' has asked a browser to load a file.
492 Set this to `browse-url-netscape-reload' to force Netscape to load the
493 file rather than displaying a cached copy."
495 :options '(browse-url-netscape-reload)
498 (defcustom browse-url-CCI-port 3003
499 "Port to access XMosaic via CCI.
500 This can be any number between 1024 and 65535 but must correspond to
501 the value set in the browser."
505 (defcustom browse-url-CCI-host "localhost"
506 "Host to access XMosaic via CCI.
507 This should be the host name of the machine running XMosaic with CCI
508 enabled. The port number should be set in `browse-url-CCI-port'."
512 (defvar browse-url-temp-file-name nil)
513 (make-variable-buffer-local 'browse-url-temp-file-name)
515 (defcustom browse-url-xterm-program "xterm"
516 "The name of the terminal emulator used by `browse-url-text-xterm'.
517 This might, for instance, be a separate color version of xterm."
521 (defcustom browse-url-xterm-args nil
522 "A list of strings defining options for `browse-url-xterm-program'.
523 These might set its size, for instance."
524 :type '(repeat (string :tag "Argument"))
527 (defcustom browse-url-gnudoit-program "gnudoit"
528 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
532 (defcustom browse-url-gnudoit-args '("-q")
533 "A list of strings defining options for `browse-url-gnudoit-program'.
534 These might set the port, for instance."
535 :type '(repeat (string :tag "Argument"))
538 (defcustom browse-url-generic-program nil
539 "The name of the browser program used by `browse-url-generic'."
540 :type '(choice string (const :tag "None" nil))
543 (defcustom browse-url-generic-args nil
544 "A list of strings defining options for `browse-url-generic-program'."
545 :type '(repeat (string :tag "Argument"))
548 (defcustom browse-url-temp-dir temporary-file-directory
549 "The name of a directory for browse-url's temporary files.
550 Such files are generated by functions like `browse-url-of-region'.
551 You might want to set this to somewhere with restricted read permissions
556 (defcustom browse-url-netscape-version 3
557 "The version of Netscape you are using.
558 This affects how URL reloading is done; the mechanism changed
559 incompatibly at version 4."
563 (defcustom browse-url-text-browser "lynx"
564 "The name of the text browser to invoke."
569 (defcustom browse-url-text-emacs-args (and (not window-system)
571 "A list of strings defining options for a text browser in an Emacs buffer.
573 The default is none in a window system, otherwise `-show_cursor' to
574 indicate the position of the current link in the absence of
575 highlighting, assuming the normal default for showing the cursor."
576 :type '(repeat (string :tag "Argument"))
580 (defcustom browse-url-text-input-field 'avoid
581 "Action on selecting an existing text browser buffer at an input field.
582 What to do when sending a new URL to an existing text browser buffer in Emacs
583 if the browser cursor is on an input field (in which case the `g' command
584 would be entered as data). Such fields are recognized by the
585 underlines ____. Allowed values: nil: disregard it, `warn': warn the
586 user and don't emit the URL, `avoid': try to avoid the field by moving
587 down (this *won't* always work)."
588 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
589 (const :tag "Disregard" :value nil)
590 (const :tag "Warn, don't emit URL" :value warn))
594 (defcustom browse-url-text-input-attempts 10
595 "How many times to try to move down from a series of text browser input fields."
600 (defcustom browse-url-text-input-delay 0.2
601 "Seconds to wait for a text browser between moves down from an input field."
606 (defcustom browse-url-kde-program "kfmclient"
607 "The name by which to invoke the KDE web browser."
612 (defcustom browse-url-kde-args '("openURL")
613 "A list of strings defining options for `browse-url-kde-program'."
614 :type '(repeat (string :tag "Argument"))
617 (defcustom browse-url-elinks-wrapper '("xterm" "-e")
618 "Wrapper command prepended to the Elinks command-line."
619 :type '(repeat (string :tag "Wrapper"))
622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
625 (defun browse-url-url-encode-chars (text chars)
626 "URL-encode the chars in TEXT that match CHARS.
627 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
628 (let ((encoded-text (copy-sequence text))
630 (while (setq s (string-match chars encoded-text s))
632 (replace-match (format "%%%x"
633 (string-to-char (match-string 0 encoded-text)))
638 (defun browse-url-encode-url (url)
639 "Escape annoying characters in URL.
640 The annoying characters are those that can mislead a web browser
641 regarding its parameter treatment."
642 ;; FIXME: Is there an actual example of a web browser getting
643 ;; confused? (This used to encode commas, but at least Firefox
644 ;; handles commas correctly and doesn't accept encoded commas.)
645 (browse-url-url-encode-chars url "[)$]"))
647 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
650 (defun browse-url-url-at-point ()
651 (let ((url (thing-at-point 'url)))
652 (set-text-properties 0 (length url) nil url)
655 ;; Having this as a separate function called by the browser-specific
656 ;; functions allows them to be stand-alone commands, making it easier
657 ;; to switch between browsers.
659 (defun browse-url-interactive-arg (prompt)
660 "Read a URL from the minibuffer, prompting with PROMPT.
661 If `transient-mark-mode' is non-nil and the mark is active,
662 it defaults to the current region, else to the URL at or before
663 point. If invoked with a mouse button, it moves point to the
664 position clicked before acting.
666 This function returns a list (URL NEW-WINDOW-FLAG)
667 for use in `interactive'."
668 (let ((event (elt (this-command-keys) 0)))
669 (and (listp event) (mouse-set-point event)))
670 (list (read-string prompt (or (and transient-mark-mode mark-active
671 ;; rfc2396 Appendix E.
672 (replace-regexp-in-string
674 (buffer-substring-no-properties
675 (region-beginning) (region-end))))
676 (browse-url-url-at-point)))
677 (not (eq (null browse-url-new-window-flag)
678 (null current-prefix-arg)))))
680 ;; called-interactive-p needs to be called at a function's top-level, hence
681 ;; this macro. We use that rather than interactive-p because
682 ;; use in a keyboard macro should not change this behavior.
683 (defmacro browse-url-maybe-new-window (arg)
684 `(if (or noninteractive (not (called-interactively-p 'any)))
686 browse-url-new-window-flag))
688 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
689 ;; Browse current buffer
692 (defun browse-url-of-file (&optional file)
693 "Ask a WWW browser to display FILE.
694 Display the current buffer's file if FILE is nil or if called
695 interactively. Turn the filename into a URL with function
696 `browse-url-file-url'. Pass the URL to a browser using the
697 `browse-url' function then run `browse-url-of-file-hook'."
700 (setq file (buffer-file-name))
701 (error "Current buffer has no file"))
702 (let ((buf (get-file-buffer file)))
704 (with-current-buffer buf
705 (cond ((not (buffer-modified-p)))
706 (browse-url-save-file (save-buffer))
707 (t (message "%s modified since last save" file))))))
708 (browse-url (browse-url-file-url file))
709 (run-hooks 'browse-url-of-file-hook))
711 (defun browse-url-file-url (file)
712 "Return the URL corresponding to FILE.
713 Use variable `browse-url-filename-alist' to map filenames to URLs."
714 ;; De-munge Cygwin filenames before passing them to Windows browser.
715 (if (eq system-type 'cygwin)
716 (let ((winfile (with-output-to-string
717 (call-process "cygpath" nil standard-output
719 (setq file (substring winfile 0 -1))))
720 (let ((coding (and (default-value 'enable-multibyte-characters)
721 (or file-name-coding-system
722 default-file-name-coding-system))))
723 (if coding (setq file (encode-coding-string file coding))))
724 (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
725 (dolist (map browse-url-filename-alist)
726 (when (and map (string-match (car map) file))
727 (setq file (replace-match (cdr map) t nil file))))
731 (defun browse-url-of-buffer (&optional buffer)
732 "Ask a WWW browser to display BUFFER.
733 Display the current buffer if BUFFER is nil. Display only the
734 currently visible part of BUFFER (from a temporary file) if buffer is
738 (and buffer (set-buffer buffer))
740 ;; Ignore real name if restricted
741 (and (= (- (point-max) (point-min)) (buffer-size))
743 (and (boundp 'dired-directory) dired-directory)))))
746 (or browse-url-temp-file-name
747 (setq browse-url-temp-file-name
748 (convert-standard-filename
750 (expand-file-name "burl" browse-url-temp-dir)
752 (setq file-name browse-url-temp-file-name)
753 (write-region (point-min) (point-max) file-name nil 'no-message)))
754 (browse-url-of-file file-name))))
756 (defun browse-url-delete-temp-file (&optional temp-file-name)
757 ;; Delete browse-url-temp-file-name from the file system
758 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
759 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
760 (if (and file-name (file-exists-p file-name))
761 (delete-file file-name))))
763 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
765 (declare-function dired-get-filename "dired"
766 (&optional localp no-error-if-not-filep))
769 (defun browse-url-of-dired-file ()
770 "In Dired, ask a WWW browser to display the file named on this line."
772 (browse-url-of-file (dired-get-filename)))
775 (defun browse-url-of-region (min max)
776 "Ask a WWW browser to display the current region."
780 (narrow-to-region min max)
781 (browse-url-of-buffer))))
783 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
784 ;; Browser-independent commands
786 ;; A generic command to call the current browse-url-browser-function
789 (defun browse-url (url &rest args)
790 "Ask a WWW browser to load URL.
791 Prompts for a URL, defaulting to the URL at or before point. Variable
792 `browse-url-browser-function' says which browser to use.
793 If the URL is a mailto: URL, consult `browse-url-mailto-function'
794 first, if that exists."
795 (interactive (browse-url-interactive-arg "URL: "))
796 (unless (called-interactively-p 'interactive)
797 (setq args (or args (list browse-url-new-window-flag))))
798 (let ((process-environment (copy-sequence process-environment))
799 (function (or (and (string-match "\\`mailto:" url)
800 browse-url-mailto-function)
801 browse-url-browser-function)))
802 ;; When connected to various displays, be careful to use the display of
803 ;; the currently selected frame, rather than the original start display,
804 ;; which may not even exist any more.
805 (if (stringp (frame-parameter (selected-frame) 'display))
806 (setenv "DISPLAY" (frame-parameter (selected-frame) 'display)))
807 (if (and (consp function)
808 (not (functionp function)))
809 ;; The `function' can be an alist; look down it for first match
810 ;; and apply the function (which might be a lambda).
812 (dolist (bf function)
813 (when (string-match (car bf) url)
814 (apply (cdr bf) url args)
816 (error "No browse-url-browser-function matching URL %s"
818 ;; Unbound symbols go down this leg, since void-function from
819 ;; apply is clearer than wrong-type-argument from dolist.
820 (apply function url args))))
823 (defun browse-url-at-point (&optional arg)
824 "Ask a WWW browser to load the URL at or before point.
825 Doesn't let you edit the URL like `browse-url'. Variable
826 `browse-url-browser-function' says which browser to use."
828 (let ((url (browse-url-url-at-point)))
830 (browse-url url (if arg
831 (not browse-url-new-window-flag)
832 browse-url-new-window-flag))
833 (error "No URL found"))))
836 (defun browse-url-at-mouse (event)
837 "Ask a WWW browser to load a URL clicked with the mouse.
838 The URL is the one around or before the position of the mouse click
839 but point is not changed. Doesn't let you edit the URL like
840 `browse-url'. Variable `browse-url-browser-function' says which browser
844 (mouse-set-point event)
845 ;; This handles browse-url-new-window-flag properly
846 ;; when it gets no arg.
847 (browse-url-at-point)))
849 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
850 ;; Browser-specific commands
852 ;; --- Default MS-Windows browser ---
854 (defvar dos-windows-version)
855 (declare-function w32-shell-execute "w32fns.c") ;; Defined in C.
857 (defun browse-url-default-windows-browser (url &optional new-window)
858 (interactive (browse-url-interactive-arg "URL: "))
859 (cond ((eq system-type 'ms-dos)
860 (if dos-windows-version
861 (shell-command (concat "start " (shell-quote-argument url)))
862 (error "Browsing URLs is not supported on this system")))
863 ((eq system-type 'cygwin)
864 (call-process "cygstart" nil nil nil url))
865 (t (w32-shell-execute "open" url))))
867 (defun browse-url-default-macosx-browser (url &optional new-window)
868 (interactive (browse-url-interactive-arg "URL: "))
869 (start-process (concat "open " url) nil "open" url))
873 (defun browse-url-process-environment ()
874 "Set DISPLAY in the environment to the X display the browser will use.
875 This is either the value of variable `browse-url-browser-display' if
876 non-nil, or the same display as Emacs if different from the current
877 environment, otherwise just use the current environment."
878 (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
880 (cons (concat "DISPLAY=" display) process-environment)
881 process-environment)))
883 (defun browse-url-emacs-display ()
884 "Return the X display Emacs is running on.
885 This is nil if the display is the same as the DISPLAY environment variable.
887 Actually Emacs could be using several displays; this just returns the
888 one showing the selected frame."
889 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
890 (and (not (equal display (getenv "DISPLAY")))
893 (defun browse-url-default-browser (url &rest args)
894 "Find a suitable browser and ask it to load URL.
895 Default to the URL around or before point.
897 When called interactively, if variable `browse-url-new-window-flag' is
898 non-nil, load the document in a new window, if possible, otherwise use
899 a random existing one. A non-nil interactive prefix argument reverses
900 the effect of `browse-url-new-window-flag'.
902 When called non-interactively, optional second argument NEW-WINDOW is
903 used instead of `browse-url-new-window-flag'.
905 The order attempted is gnome-moz-remote, Mozilla, Firefox,
906 Galeon, Konqueror, Netscape, Mosaic, Lynx in an xterm, and then W3."
909 ((browse-url-can-use-xdg-open) 'browse-url-xdg-open)
910 ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
911 ((executable-find browse-url-mozilla-program) 'browse-url-mozilla)
912 ((executable-find browse-url-firefox-program) 'browse-url-firefox)
913 ((executable-find browse-url-galeon-program) 'browse-url-galeon)
914 ((executable-find browse-url-kde-program) 'browse-url-kde)
915 ((executable-find browse-url-netscape-program) 'browse-url-netscape)
916 ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
917 ((executable-find browse-url-xterm-program) 'browse-url-text-xterm)
918 ((locate-library "w3") 'browse-url-w3)
920 (lambda (&rest ignore) (error "No usable browser found"))))
923 (defun browse-url-can-use-xdg-open ()
924 "Check if xdg-open can be used, i.e. we are on Gnome, KDE or xfce4."
925 (and (getenv "DISPLAY")
926 (executable-find "xdg-open")
927 ;; xdg-open may call gnome-open and that does not wait for its child
928 ;; to finish. This child may then be killed when the parent dies.
929 ;; Use nohup to work around.
930 (executable-find "nohup")
931 (or (getenv "GNOME_DESKTOP_SESSION_ID")
932 ;; GNOME_DESKTOP_SESSION_ID is deprecated, check on Dbus also.
935 "dbus-send" nil nil nil
936 "--dest=org.gnome.SessionManager"
938 "/org/gnome/SessionManager"
939 "org.gnome.SessionManager.CanShutdown"))
941 (equal (getenv "KDE_FULL_SESSION") "true")
944 "/bin/sh" nil nil nil
946 "xprop -root _DT_SAVE_MODE|grep xfce4"))
951 (defun browse-url-xdg-open (url &optional new-window)
952 (interactive (browse-url-interactive-arg "URL: "))
953 (call-process "nohup" nil nil nil "xdg-open" url))
956 (defun browse-url-netscape (url &optional new-window)
957 "Ask the Netscape WWW browser to load URL.
958 Default to the URL around or before point. The strings in variable
959 `browse-url-netscape-arguments' are also passed to Netscape.
961 When called interactively, if variable `browse-url-new-window-flag' is
962 non-nil, load the document in a new Netscape window, otherwise use a
963 random existing one. A non-nil interactive prefix argument reverses
964 the effect of `browse-url-new-window-flag'.
966 If `browse-url-netscape-new-window-is-tab' is non-nil, then
967 whenever a document would otherwise be loaded in a new window, it
968 is loaded in a new tab in an existing window instead.
970 When called non-interactively, optional second argument NEW-WINDOW is
971 used instead of `browse-url-new-window-flag'."
972 (interactive (browse-url-interactive-arg "URL: "))
973 (setq url (browse-url-encode-url url))
974 (let* ((process-environment (browse-url-process-environment))
976 (apply 'start-process
977 (concat "netscape " url) nil
978 browse-url-netscape-program
980 browse-url-netscape-arguments
981 (if (eq window-system 'w32)
984 (if new-window '("-noraise"))
986 (concat "openURL(" url
987 (if (browse-url-maybe-new-window
989 (if browse-url-netscape-new-window-is-tab
993 (set-process-sentinel process
994 `(lambda (process change)
995 (browse-url-netscape-sentinel process ,url)))))
997 (defun browse-url-netscape-sentinel (process url)
998 "Handle a change to the process communicating with Netscape."
999 (or (eq (process-exit-status process) 0)
1000 (let* ((process-environment (browse-url-process-environment)))
1001 ;; Netscape not running - start it
1002 (message "Starting %s..." browse-url-netscape-program)
1003 (apply 'start-process (concat "netscape" url) nil
1004 browse-url-netscape-program
1005 (append browse-url-netscape-startup-arguments (list url))))))
1007 (defun browse-url-netscape-reload ()
1008 "Ask Netscape to reload its current document.
1009 How depends on `browse-url-netscape-version'."
1011 ;; Backwards incompatibility reported by
1012 ;; <peter.kruse@psychologie.uni-regensburg.de>.
1013 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
1014 "xfeDoCommand(reload)"
1017 (defun browse-url-netscape-send (command)
1018 "Send a remote control command to Netscape."
1019 (let* ((process-environment (browse-url-process-environment)))
1020 (apply 'start-process "netscape" nil
1021 browse-url-netscape-program
1022 (append browse-url-netscape-arguments
1023 (list "-remote" command)))))
1026 (defun browse-url-mozilla (url &optional new-window)
1027 "Ask the Mozilla WWW browser to load URL.
1028 Default to the URL around or before point. The strings in variable
1029 `browse-url-mozilla-arguments' are also passed to Mozilla.
1031 When called interactively, if variable `browse-url-new-window-flag' is
1032 non-nil, load the document in a new Mozilla window, otherwise use a
1033 random existing one. A non-nil interactive prefix argument reverses
1034 the effect of `browse-url-new-window-flag'.
1036 If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
1037 document would otherwise be loaded in a new window, it is loaded in a
1038 new tab in an existing window instead.
1040 When called non-interactively, optional second argument NEW-WINDOW is
1041 used instead of `browse-url-new-window-flag'."
1042 (interactive (browse-url-interactive-arg "URL: "))
1043 (setq url (browse-url-encode-url url))
1044 (let* ((process-environment (browse-url-process-environment))
1046 (apply 'start-process
1047 (concat "mozilla " url) nil
1048 browse-url-mozilla-program
1050 browse-url-mozilla-arguments
1054 (if (browse-url-maybe-new-window
1056 (if browse-url-mozilla-new-window-is-tab
1060 (set-process-sentinel process
1061 `(lambda (process change)
1062 (browse-url-mozilla-sentinel process ,url)))))
1064 (defun browse-url-mozilla-sentinel (process url)
1065 "Handle a change to the process communicating with Mozilla."
1066 (or (eq (process-exit-status process) 0)
1067 (let* ((process-environment (browse-url-process-environment)))
1068 ;; Mozilla is not running - start it
1069 (message "Starting %s..." browse-url-mozilla-program)
1070 (apply 'start-process (concat "mozilla " url) nil
1071 browse-url-mozilla-program
1072 (append browse-url-mozilla-startup-arguments (list url))))))
1075 (defun browse-url-firefox (url &optional new-window)
1076 "Ask the Firefox WWW browser to load URL.
1077 Default to the URL around or before point. The strings in
1078 variable `browse-url-firefox-arguments' are also passed to
1081 When called interactively, if variable
1082 `browse-url-new-window-flag' is non-nil, load the document in a
1083 new Firefox window, otherwise use a random existing one. A
1084 non-nil interactive prefix argument reverses the effect of
1085 `browse-url-new-window-flag'.
1087 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1088 whenever a document would otherwise be loaded in a new window, it
1089 is loaded in a new tab in an existing window instead.
1091 When called non-interactively, optional second argument
1092 NEW-WINDOW is used instead of `browse-url-new-window-flag'.
1094 On MS-Windows systems the optional `new-window' parameter is
1095 ignored. Firefox for Windows does not support the \"-remote\"
1096 command line parameter. Therefore, the
1097 `browse-url-new-window-flag' and `browse-url-firefox-new-window-is-tab'
1098 are ignored as well. Firefox on Windows will always open the requested
1099 URL in a new window."
1100 (interactive (browse-url-interactive-arg "URL: "))
1101 (setq url (browse-url-encode-url url))
1102 (let* ((process-environment (browse-url-process-environment))
1104 (apply 'start-process
1105 (concat "firefox " url) nil
1106 browse-url-firefox-program
1108 browse-url-firefox-arguments
1109 (if (or (featurep 'dos-w32)
1110 (string-match "win32" system-configuration))
1115 (if (browse-url-maybe-new-window
1117 (if browse-url-firefox-new-window-is-tab
1121 (set-process-sentinel process
1122 `(lambda (process change)
1123 (browse-url-firefox-sentinel process ,url)))))
1125 (defun browse-url-firefox-sentinel (process url)
1126 "Handle a change to the process communicating with Firefox."
1127 (or (eq (process-exit-status process) 0)
1128 (let* ((process-environment (browse-url-process-environment)))
1129 ;; Firefox is not running - start it
1130 (message "Starting Firefox...")
1131 (apply 'start-process (concat "firefox " url) nil
1132 browse-url-firefox-program
1133 (append browse-url-firefox-startup-arguments (list url))))))
1136 (defun browse-url-galeon (url &optional new-window)
1137 "Ask the Galeon WWW browser to load URL.
1138 Default to the URL around or before point. The strings in variable
1139 `browse-url-galeon-arguments' are also passed to Galeon.
1141 When called interactively, if variable `browse-url-new-window-flag' is
1142 non-nil, load the document in a new Galeon window, otherwise use a
1143 random existing one. A non-nil interactive prefix argument reverses
1144 the effect of `browse-url-new-window-flag'.
1146 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1147 document would otherwise be loaded in a new window, it is loaded in a
1148 new tab in an existing window instead.
1150 When called non-interactively, optional second argument NEW-WINDOW is
1151 used instead of `browse-url-new-window-flag'."
1152 (interactive (browse-url-interactive-arg "URL: "))
1153 (setq url (browse-url-encode-url url))
1154 (let* ((process-environment (browse-url-process-environment))
1155 (process (apply 'start-process
1156 (concat "galeon " url)
1158 browse-url-galeon-program
1160 browse-url-galeon-arguments
1161 (if (browse-url-maybe-new-window new-window)
1162 (if browse-url-galeon-new-window-is-tab
1164 '("--new-window" "--noraise"))
1167 (set-process-sentinel process
1168 `(lambda (process change)
1169 (browse-url-galeon-sentinel process ,url)))))
1171 (defun browse-url-galeon-sentinel (process url)
1172 "Handle a change to the process communicating with Galeon."
1173 (or (eq (process-exit-status process) 0)
1174 (let* ((process-environment (browse-url-process-environment)))
1175 ;; Galeon is not running - start it
1176 (message "Starting %s..." browse-url-galeon-program)
1177 (apply 'start-process (concat "galeon " url) nil
1178 browse-url-galeon-program
1179 (append browse-url-galeon-startup-arguments (list url))))))
1181 (defun browse-url-epiphany (url &optional new-window)
1182 "Ask the Epiphany WWW browser to load URL.
1183 Default to the URL around or before point. The strings in variable
1184 `browse-url-galeon-arguments' are also passed to Epiphany.
1186 When called interactively, if variable `browse-url-new-window-flag' is
1187 non-nil, load the document in a new Epiphany window, otherwise use a
1188 random existing one. A non-nil interactive prefix argument reverses
1189 the effect of `browse-url-new-window-flag'.
1191 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1192 document would otherwise be loaded in a new window, it is loaded in a
1193 new tab in an existing window instead.
1195 When called non-interactively, optional second argument NEW-WINDOW is
1196 used instead of `browse-url-new-window-flag'."
1197 (interactive (browse-url-interactive-arg "URL: "))
1198 (setq url (browse-url-encode-url url))
1199 (let* ((process-environment (browse-url-process-environment))
1200 (process (apply 'start-process
1201 (concat "epiphany " url)
1203 browse-url-epiphany-program
1205 browse-url-epiphany-arguments
1206 (if (browse-url-maybe-new-window new-window)
1207 (if browse-url-epiphany-new-window-is-tab
1209 '("--new-window" "--noraise"))
1212 (set-process-sentinel process
1213 `(lambda (process change)
1214 (browse-url-epiphany-sentinel process ,url)))))
1216 (defun browse-url-epiphany-sentinel (process url)
1217 "Handle a change to the process communicating with Epiphany."
1218 (or (eq (process-exit-status process) 0)
1219 (let* ((process-environment (browse-url-process-environment)))
1220 ;; Epiphany is not running - start it
1221 (message "Starting %s..." browse-url-epiphany-program)
1222 (apply 'start-process (concat "epiphany " url) nil
1223 browse-url-epiphany-program
1224 (append browse-url-epiphany-startup-arguments (list url))))))
1226 (defvar url-handler-regexp)
1229 (defun browse-url-emacs (url &optional new-window)
1230 "Ask Emacs to load URL into a buffer and show it in another window."
1231 (interactive (browse-url-interactive-arg "URL: "))
1232 (require 'url-handlers)
1233 (let ((file-name-handler-alist
1234 (cons (cons url-handler-regexp 'url-file-handler)
1235 file-name-handler-alist)))
1236 ;; Ignore `new-window': with all other browsers the URL is always shown
1237 ;; in another window than the current Emacs one since it's shown in
1238 ;; another application's window.
1239 ;; (if new-window (find-file-other-window url) (find-file url))
1240 (find-file-other-window url)))
1243 (defun browse-url-gnome-moz (url &optional new-window)
1244 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1245 Default to the URL around or before point. The strings in variable
1246 `browse-url-gnome-moz-arguments' are also passed.
1248 When called interactively, if variable `browse-url-new-window-flag' is
1249 non-nil, load the document in a new browser window, otherwise use an
1250 existing one. A non-nil interactive prefix argument reverses the
1251 effect of `browse-url-new-window-flag'.
1253 When called non-interactively, optional second argument NEW-WINDOW is
1254 used instead of `browse-url-new-window-flag'."
1255 (interactive (browse-url-interactive-arg "URL: "))
1256 (apply 'start-process (concat "gnome-moz-remote " url)
1258 browse-url-gnome-moz-program
1260 browse-url-gnome-moz-arguments
1261 (if (browse-url-maybe-new-window new-window)
1263 (list "--raise" url))))
1268 (defun browse-url-mosaic (url &optional new-window)
1269 "Ask the XMosaic WWW browser to load URL.
1271 Default to the URL around or before point. The strings in variable
1272 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1273 program is invoked according to the variable
1274 `browse-url-mosaic-program'.
1276 When called interactively, if variable `browse-url-new-window-flag' is
1277 non-nil, load the document in a new Mosaic window, otherwise use a
1278 random existing one. A non-nil interactive prefix argument reverses
1279 the effect of `browse-url-new-window-flag'.
1281 When called non-interactively, optional second argument NEW-WINDOW is
1282 used instead of `browse-url-new-window-flag'."
1283 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1284 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
1286 (if (file-readable-p pidfile)
1289 (goto-char (point-min))
1290 (setq pid (read (current-buffer)))
1292 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
1294 (find-file (format "/tmp/Mosaic.%d" pid))
1296 (insert (if (browse-url-maybe-new-window new-window)
1302 ;; Send signal SIGUSR to Mosaic
1303 (message "Signaling Mosaic...")
1304 (signal-process pid 'SIGUSR1)
1305 ;; Or you could try:
1306 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1307 (message "Signaling Mosaic...done")
1309 ;; Mosaic not running - start it
1310 (message "Starting %s..." browse-url-mosaic-program)
1311 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1312 (append browse-url-mosaic-arguments (list url)))
1313 (message "Starting %s...done" browse-url-mosaic-program))))
1315 ;; --- Mosaic using CCI ---
1318 (defun browse-url-cci (url &optional new-window)
1319 "Ask the XMosaic WWW browser to load URL.
1320 Default to the URL around or before point.
1322 This function only works for XMosaic version 2.5 or later. You must
1323 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1324 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1326 When called interactively, if variable `browse-url-new-window-flag' is
1327 non-nil, load the document in a new browser window, otherwise use a
1328 random existing one. A non-nil interactive prefix argument reverses
1329 the effect of `browse-url-new-window-flag'.
1331 When called non-interactively, optional second argument NEW-WINDOW is
1332 used instead of `browse-url-new-window-flag'."
1333 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1334 (open-network-stream "browse-url" " *browse-url*"
1335 browse-url-CCI-host browse-url-CCI-port)
1336 ;; Todo: start browser if fails
1337 (process-send-string "browse-url"
1338 (concat "get url (" url ") output "
1339 (if (browse-url-maybe-new-window new-window)
1343 (process-send-string "browse-url" "disconnect\r\n")
1344 (delete-process "browse-url"))
1349 (declare-function w3-fetch-other-window "ext:w3m" (&optional url))
1350 (declare-function w3-fetch "ext:w3m" (&optional url target))
1353 (defun browse-url-w3 (url &optional new-window)
1354 "Ask the w3 WWW browser to load URL.
1355 Default to the URL around or before point.
1357 When called interactively, if variable `browse-url-new-window-flag' is
1358 non-nil, load the document in a new window. A non-nil interactive
1359 prefix argument reverses the effect of `browse-url-new-window-flag'.
1361 When called non-interactively, optional second argument NEW-WINDOW is
1362 used instead of `browse-url-new-window-flag'."
1363 (interactive (browse-url-interactive-arg "W3 URL: "))
1364 (require 'w3) ; w3-fetch-other-window not autoloaded
1365 (if (browse-url-maybe-new-window new-window)
1366 (w3-fetch-other-window url)
1370 (defun browse-url-w3-gnudoit (url &optional new-window)
1371 ;; new-window ignored
1372 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1373 The `browse-url-gnudoit-program' program is used with options given by
1374 `browse-url-gnudoit-args'. Default to the URL around or before point."
1375 (interactive (browse-url-interactive-arg "W3 URL: "))
1376 (apply 'start-process (concat "gnudoit:" url) nil
1377 browse-url-gnudoit-program
1378 (append browse-url-gnudoit-args
1379 (list (concat "(w3-fetch \"" url "\")")
1382 ;; --- Lynx in an xterm ---
1385 (defun browse-url-text-xterm (url &optional new-window)
1386 ;; new-window ignored
1387 "Ask a text browser to load URL.
1388 URL defaults to the URL around or before point.
1389 This runs the text browser specified by `browse-url-text-browser'.
1390 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1391 with possible additional arguments `browse-url-xterm-args'."
1392 (interactive (browse-url-interactive-arg "Text browser URL: "))
1393 (apply #'start-process `(,(concat browse-url-text-browser url)
1394 nil ,browse-url-xterm-program
1395 ,@browse-url-xterm-args "-e" ,browse-url-text-browser
1398 ;; --- Lynx in an Emacs "term" window ---
1400 (declare-function term-char-mode "term" ())
1401 (declare-function term-send-down "term" ())
1402 (declare-function term-send-string "term" (proc str))
1405 (defun browse-url-text-emacs (url &optional new-buffer)
1406 "Ask a text browser to load URL.
1407 URL defaults to the URL around or before point.
1408 This runs the text browser specified by `browse-url-text-browser'.
1409 With a prefix argument, it runs a new browser process in a new buffer.
1411 When called interactively, if variable `browse-url-new-window-flag' is
1412 non-nil, load the document in a new browser process in a new term window,
1413 otherwise use any existing one. A non-nil interactive prefix argument
1414 reverses the effect of `browse-url-new-window-flag'.
1416 When called non-interactively, optional second argument NEW-WINDOW is
1417 used instead of `browse-url-new-window-flag'."
1418 (interactive (browse-url-interactive-arg "Text browser URL: "))
1419 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
1420 ;; (term-term-name "vt100") ; ??
1421 (buf (get-buffer "*text browser*"))
1422 (proc (and buf (get-buffer-process buf)))
1423 (n browse-url-text-input-attempts))
1425 (if (and (browse-url-maybe-new-window new-buffer) buf)
1426 ;; Rename away the OLD buffer. This isn't very polite, but
1427 ;; term insists on working in a buffer named *lynx* and would
1428 ;; choke on *lynx*<1>
1429 (progn (set-buffer buf)
1431 (if (or (browse-url-maybe-new-window new-buffer)
1434 (not (memq (process-status proc) '(run stop))))
1435 ;; start a new text browser
1439 `(,browse-url-text-browser
1440 ,browse-url-text-browser
1441 nil ,@browse-url-text-emacs-args
1443 (switch-to-buffer buf)
1445 (set-process-sentinel
1446 (get-buffer-process buf)
1447 ;; Don't leave around a dead one (especially because of its
1449 (lambda (process event)
1450 (if (not (memq (process-status process) '(run stop)))
1451 (let ((buf (process-buffer process)))
1452 (if buf (kill-buffer buf)))))))
1453 ;; Send the url to the text browser in the old buffer
1454 (let ((win (get-buffer-window buf t)))
1457 (switch-to-buffer buf)))
1458 (if (eq (following-char) ?_)
1459 (cond ((eq browse-url-text-input-field 'warn)
1460 (error "Please move out of the input field first"))
1461 ((eq browse-url-text-input-field 'avoid)
1462 (while (and (eq (following-char) ?_) (> n 0))
1463 (term-send-down) ; down arrow
1464 (sit-for browse-url-text-input-delay))
1465 (if (eq (following-char) ?_)
1466 (error "Cannot move out of the input field, sorry")))))
1467 (term-send-string proc (concat "g" ; goto
1468 "\C-u" ; kill default url
1474 (autoload 'rfc2368-parse-mailto-url "rfc2368")
1477 (defun browse-url-mail (url &optional new-window)
1478 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1479 Default to using the mailto: URL around or before point as the
1480 recipient's address. Supplying a non-nil interactive prefix argument
1481 will cause the mail to be composed in another window rather than the
1484 When called interactively, if variable `browse-url-new-window-flag' is
1485 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1486 non-nil interactive prefix argument reverses the effect of
1487 `browse-url-new-window-flag'.
1489 When called non-interactively, optional second argument NEW-WINDOW is
1490 used instead of `browse-url-new-window-flag'."
1491 (interactive (browse-url-interactive-arg "Mailto URL: "))
1493 (let* ((alist (rfc2368-parse-mailto-url url))
1494 (to (assoc "To" alist))
1495 (subject (assoc "Subject" alist))
1496 (body (assoc "Body" alist))
1497 (rest (delq to (delq subject (delq body alist))))
1499 (subject (cdr subject))
1501 (mail-citation-hook (unless body mail-citation-hook)))
1502 (if (browse-url-maybe-new-window new-window)
1503 (compose-mail-other-window to subject rest nil
1504 (list 'insert-buffer (current-buffer)))
1505 (compose-mail to subject rest nil nil
1506 (list 'insert-buffer (current-buffer))))
1508 (goto-char (point-min))
1509 (unless (or (search-forward (concat "\n" mail-header-separator "\n")
1515 (insert (replace-regexp-in-string "\r\n" "\n" body))
1517 (insert "\n"))))))))
1519 ;; --- Random browser ---
1522 (defun browse-url-generic (url &optional new-window)
1523 ;; new-window ignored
1524 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1525 Default to the URL around or before point. A fresh copy of the
1526 browser is started up in a new process with possible additional arguments
1527 `browse-url-generic-args'. This is appropriate for browsers which
1528 don't offer a form of remote control."
1529 (interactive (browse-url-interactive-arg "URL: "))
1530 (if (not browse-url-generic-program)
1531 (error "No browser defined (`browse-url-generic-program')"))
1532 (apply 'call-process browse-url-generic-program nil
1534 (append browse-url-generic-args (list url))))
1537 (defun browse-url-kde (url &optional new-window)
1538 "Ask the KDE WWW browser to load URL.
1539 Default to the URL around or before point."
1540 (interactive (browse-url-interactive-arg "KDE URL: "))
1541 (message "Sending URL to KDE...")
1542 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
1543 (append browse-url-kde-args (list url))))
1545 (defun browse-url-elinks-new-window (url)
1546 "Ask the Elinks WWW browser to load URL in a new window."
1547 (let ((process-environment (browse-url-process-environment)))
1548 (apply #'start-process
1549 (append (list (concat "elinks:" url)
1551 browse-url-elinks-wrapper
1552 (list "elinks" url)))))
1555 (defun browse-url-elinks (url &optional new-window)
1556 "Ask the Elinks WWW browser to load URL.
1557 Default to the URL around the point.
1559 The document is loaded in a new tab of a running Elinks or, if
1560 none yet running, a newly started instance.
1562 The Elinks command will be prepended by the program+arguments
1563 from `browse-url-elinks-wrapper'."
1564 (interactive (browse-url-interactive-arg "URL: "))
1565 (setq url (browse-url-encode-url url))
1567 (browse-url-elinks-new-window url)
1568 (let ((process-environment (browse-url-process-environment))
1569 (elinks-ping-process (start-process "elinks-ping" nil
1570 "elinks" "-remote" "ping()")))
1571 (set-process-sentinel elinks-ping-process
1572 `(lambda (process change)
1573 (browse-url-elinks-sentinel process ,url))))))
1575 (defun browse-url-elinks-sentinel (process url)
1576 "Determines if Elinks is running or a new one has to be started."
1577 (let ((exit-status (process-exit-status process)))
1578 ;; Try to determine if an instance is running or if we have to
1579 ;; create a new one.
1582 ;; No instance, start a new one.
1583 (browse-url-elinks-new-window url))
1585 ;; Found an instance, open URL in new tab.
1586 (let ((process-environment (browse-url-process-environment)))
1587 (start-process (concat "elinks:" url) nil
1589 (concat "openURL(\"" url "\",new-tab)"))))
1591 (error "Unrecognized exit-code %d of process `elinks'"
1594 (provide 'browse-url)
1596 ;;; browse-url.el ends here