* emulation/pc-select.el (next-line-mark, next-line-nomark)
[emacs.git] / lisp / net / browse-url.el
blobc2a0442478c9d77f6f8d7d39f98fd57e04fb2d8a
1 ;;; browse-url.el --- pass a URL to a WWW browser
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
7 ;; Maintainer: FSF
8 ;; Created: 03 Apr 1995
9 ;; Keywords: hypertext, hypermedia, mouse
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This package provides functions which read a URL (Uniform Resource
31 ;; Locator) from the minibuffer, defaulting to the URL around point,
32 ;; and ask a World-Wide Web browser to load it. It can also load the
33 ;; URL associated with the current buffer. Different browsers use
34 ;; different methods of remote control so there is one function for
35 ;; each supported browser. If the chosen browser is not running, it
36 ;; is started. Currently there is support for the following browsers,
37 ;; some of them probably now obsolete:
39 ;; Function Browser Earliest version
40 ;; browse-url-mozilla Mozilla Don't know
41 ;; browse-url-firefox Firefox Don't know (tried with 1.0.1)
42 ;; browse-url-galeon Galeon Don't know
43 ;; browse-url-epiphany Epiphany Don't know
44 ;; browse-url-netscape Netscape 1.1b1
45 ;; browse-url-mosaic XMosaic/mMosaic <= 2.4
46 ;; browse-url-cci XMosaic 2.5
47 ;; browse-url-w3 w3 0
48 ;; browse-url-w3-gnudoit w3 remotely
49 ;; browse-url-lynx-* Lynx 0
50 ;; browse-url-generic arbitrary
51 ;; browse-url-default-windows-browser MS-Windows browser
52 ;; browse-url-default-macosx-browser Mac OS X browser
53 ;; browse-url-gnome-moz GNOME interface to Mozilla
54 ;; browse-url-kde KDE konqueror (kfm)
55 ;; browse-url-elinks Elinks Don't know (tried with 0.12.GIT)
57 ;; [A version of the Netscape browser is now free software
58 ;; <URL:http://www.mozilla.org/>, albeit not GPLed, so it is
59 ;; reasonable to have that as the default.]
61 ;; Note that versions of Netscape before 1.1b1 did not have remote
62 ;; control. <URL:http://www.netscape.com/newsref/std/x-remote.html>.
64 ;; Browsers can cache Web pages so it may be necessary to tell them to
65 ;; reload the current page if it has changed (e.g. if you have edited
66 ;; it). There is currently no perfect automatic solution to this.
68 ;; Netscape allows you to specify the id of the window you want to
69 ;; control but which window DO you want to control and how do you
70 ;; discover its id?
72 ;; William M. Perry's excellent "w3" WWW browser for
73 ;; Emacs <URL:ftp://cs.indiana.edu/pub/elisp/w3/>
74 ;; has a function w3-follow-url-at-point, but that
75 ;; doesn't let you edit the URL like browse-url.
76 ;; The `gnuserv' package that can be used to control it in another
77 ;; Emacs process is available from
78 ;; <URL:ftp://ftp.splode.com/pub/users/friedman/packages/>.
80 ;; Lynx is now distributed by the FSF. See also
81 ;; <URL:http://lynx.browser.org/>.
83 ;; Free graphical browsers that could be used by `browse-url-generic'
84 ;; include Chimera <URL:ftp://ftp.cs.unlv.edu/pub/chimera> and
85 ;; <URL:http://www.unlv.edu/chimera/>, Arena
86 ;; <URL:ftp://ftp.yggdrasil.com/pub/dist/web/arena> and Amaya
87 ;; <URL:ftp://ftp.w3.org/pub/amaya>. mMosaic
88 ;; <URL:ftp://ftp.enst.fr/pub/mbone/mMosaic/>,
89 ;; <URL:http://www.enst.fr/~dauphin/mMosaic/> (with development
90 ;; support for Java applets and multicast) can be used like Mosaic by
91 ;; setting `browse-url-mosaic-program' appropriately.
93 ;; I [Denis Howe, not Dave Love] recommend Nelson Minar
94 ;; <nelson@santafe.edu>'s excellent html-helper-mode.el for editing
95 ;; HTML and thank Nelson for his many useful comments on this code.
96 ;; <URL:http://www.santafe.edu/%7Enelson/hhm-beta/>
98 ;; See also hm--html-menus <URL:http://www.tnt.uni-hannover.de/%7Emuenkel/
99 ;; software/own/hm--html-menus/>. For composing correct HTML see also
100 ;; PSGML the general SGML structure editor package
101 ;; <URL:ftp://ftp.lysator.liu.se/pub/sgml>; hm--html-menus can be used
102 ;; with this.
104 ;; This package generalises function html-previewer-process in Marc
105 ;; Andreessen's html-mode (LCD modes/html-mode.el.Z). See also the
106 ;; ffap.el package. The huge hyperbole package also contains similar
107 ;; functions.
109 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 ;; Help!
112 ;; Can you write and test some code for the Macintrash and Windoze
113 ;; Netscape remote control APIs? (See the URL above).
115 ;; Do any other browsers have remote control?
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;; Usage
120 ;; To display the URL at or before point:
121 ;; M-x browse-url-at-point RET
122 ;; or, similarly but with the opportunity to edit the URL extracted from
123 ;; the buffer, use:
124 ;; M-x browse-url
126 ;; To display a URL by shift-clicking on it, put this in your ~/.emacs
127 ;; file:
128 ;; (global-set-key [S-mouse-2] 'browse-url-at-mouse)
129 ;; (Note that using Shift-mouse-1 is not desirable because
130 ;; that event has a standard meaning in Emacs.)
132 ;; To display the current buffer in a web browser:
133 ;; M-x browse-url-of-buffer RET
135 ;; To display the current region in a web browser:
136 ;; M-x browse-url-of-region RET
138 ;; In Dired, to display the file named on the current line:
139 ;; M-x browse-url-of-dired-file RET
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; Customisation (~/.emacs)
144 ;; To see what variables are available for customization, type
145 ;; `M-x set-variable browse-url TAB'. Better, use
146 ;; `M-x customize-group browse-url'.
148 ;; Bind the browse-url commands to keys with the `C-c C-z' prefix
149 ;; (as used by html-helper-mode):
150 ;; (global-set-key "\C-c\C-z." 'browse-url-at-point)
151 ;; (global-set-key "\C-c\C-zb" 'browse-url-of-buffer)
152 ;; (global-set-key "\C-c\C-zr" 'browse-url-of-region)
153 ;; (global-set-key "\C-c\C-zu" 'browse-url)
154 ;; (global-set-key "\C-c\C-zv" 'browse-url-of-file)
155 ;; (add-hook 'dired-mode-hook
156 ;; (lambda ()
157 ;; (local-set-key "\C-c\C-zf" 'browse-url-of-dired-file)))
159 ;; Browse URLs in mail messages under RMAIL by clicking mouse-2:
160 ;; (add-hook 'rmail-mode-hook (lambda () ; rmail-mode startup
161 ;; (define-key rmail-mode-map [mouse-2] 'browse-url-at-mouse)))
162 ;; Alternatively, add `goto-address' to `rmail-show-message-hook'.
164 ;; Gnus provides a standard feature to activate URLs in article
165 ;; buffers for invocation of browse-url.
167 ;; Use the Emacs w3 browser when not running under X11:
168 ;; (or (eq window-system 'x)
169 ;; (setq browse-url-browser-function 'browse-url-w3))
171 ;; To always save modified buffers before displaying the file in a browser:
172 ;; (setq browse-url-save-file t)
174 ;; To get round the Netscape caching problem, you could EITHER have
175 ;; write-file in html-helper-mode make Netscape reload the document:
177 ;; (autoload 'browse-url-netscape-reload "browse-url"
178 ;; "Ask a WWW browser to redisplay the current file." t)
179 ;; (add-hook 'html-helper-mode-hook
180 ;; (lambda ()
181 ;; (add-hook 'local-write-file-hooks
182 ;; (lambda ()
183 ;; (let ((local-write-file-hooks))
184 ;; (save-buffer))
185 ;; (browse-url-netscape-reload)
186 ;; t) ; => file written by hook
187 ;; t))) ; append to l-w-f-hooks
189 ;; OR have browse-url-of-file ask Netscape to load and then reload the
190 ;; file:
192 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
194 ;; You may also want to customise browse-url-netscape-arguments, e.g.
195 ;; (setq browse-url-netscape-arguments '("-install"))
197 ;; or similarly for the other browsers.
199 ;; To invoke different browsers for different URLs:
200 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
201 ;; ("." . browse-url-netscape)))
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 ;;; Code:
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207 ;; Variables
209 (eval-when-compile (require 'cl)
210 (require 'thingatpt)
211 (require 'term)
212 (require 'dired)
213 (require 'executable)
214 (require 'w3-auto nil t))
216 (defgroup browse-url nil
217 "Use a web browser to look at a URL."
218 :prefix "browse-url-"
219 :link '(emacs-commentary-link "browse-url")
220 :group 'hypermedia)
222 ;;;###autoload
223 (defcustom browse-url-browser-function
224 (cond
225 ((memq system-type '(windows-nt ms-dos cygwin))
226 'browse-url-default-windows-browser)
227 ((memq system-type '(darwin)) 'browse-url-default-macosx-browser)
228 (t 'browse-url-default-browser))
229 "Function to display the current buffer in a WWW browser.
230 This is used by the `browse-url-at-point', `browse-url-at-mouse', and
231 `browse-url-of-file' commands.
233 If the value is not a function it should be a list of pairs
234 \(REGEXP . FUNCTION). In this case the function called will be the one
235 associated with the first REGEXP which matches the current URL. The
236 function is passed the URL and any other args of `browse-url'. The last
237 regexp should probably be \".\" to specify a default browser."
238 :type '(choice
239 (function-item :tag "Emacs W3" :value browse-url-w3)
240 (function-item :tag "W3 in another Emacs via `gnudoit'"
241 :value browse-url-w3-gnudoit)
242 (function-item :tag "Mozilla" :value browse-url-mozilla)
243 (function-item :tag "Firefox" :value browse-url-firefox)
244 (function-item :tag "Galeon" :value browse-url-galeon)
245 (function-item :tag "Epiphany" :value browse-url-epiphany)
246 (function-item :tag "Netscape" :value browse-url-netscape)
247 (function-item :tag "Mosaic" :value browse-url-mosaic)
248 (function-item :tag "Mosaic using CCI" :value browse-url-cci)
249 (function-item :tag "Lynx in an xterm window"
250 :value browse-url-lynx-xterm)
251 (function-item :tag "Lynx in an Emacs window"
252 :value browse-url-lynx-emacs)
253 (function-item :tag "KDE" :value browse-url-kde)
254 (function-item :tag "Elinks" :value browse-url-elinks)
255 (function-item :tag "Specified by `Browse Url Generic Program'"
256 :value browse-url-generic)
257 (function-item :tag "Default Windows browser"
258 :value browse-url-default-windows-browser)
259 (function-item :tag "Default Mac OS X browser"
260 :value browse-url-default-macosx-browser)
261 (function-item :tag "GNOME invoking Mozilla"
262 :value browse-url-gnome-moz)
263 (function-item :tag "Default browser"
264 :value browse-url-default-browser)
265 (function :tag "Your own function")
266 (alist :tag "Regexp/function association list"
267 :key-type regexp :value-type function))
268 :version "21.1"
269 :group 'browse-url)
271 (defcustom browse-url-netscape-program "netscape"
272 ;; Info about netscape-remote from Karl Berry.
273 "The name by which to invoke Netscape.
275 The free program `netscape-remote' from
276 <URL:http://home.netscape.com/newsref/std/remote.c> is said to start
277 up very much quicker than `netscape'. Reported to compile on a GNU
278 system, given vroot.h from the same directory, with cc flags
279 -DSTANDALONE -L/usr/X11R6/lib -lXmu -lX11."
280 :type 'string
281 :group 'browse-url)
283 (defcustom browse-url-netscape-arguments nil
284 "A list of strings to pass to Netscape as arguments."
285 :type '(repeat (string :tag "Argument"))
286 :group 'browse-url)
288 (defcustom browse-url-netscape-startup-arguments browse-url-netscape-arguments
289 "A list of strings to pass to Netscape when it starts up.
290 Defaults to the value of `browse-url-netscape-arguments' at the time
291 `browse-url' is loaded."
292 :type '(repeat (string :tag "Argument"))
293 :group 'browse-url)
295 (defcustom browse-url-browser-display nil
296 "The X display for running the browser, if not same as Emacs'."
297 :type '(choice string (const :tag "Default" nil))
298 :group 'browse-url)
300 (defcustom browse-url-mozilla-program "mozilla"
301 "The name by which to invoke Mozilla."
302 :type 'string
303 :group 'browse-url)
305 (defcustom browse-url-mozilla-arguments nil
306 "A list of strings to pass to Mozilla as arguments."
307 :type '(repeat (string :tag "Argument"))
308 :group 'browse-url)
310 (defcustom browse-url-mozilla-startup-arguments browse-url-mozilla-arguments
311 "A list of strings to pass to Mozilla when it starts up.
312 Defaults to the value of `browse-url-mozilla-arguments' at the time
313 `browse-url' is loaded."
314 :type '(repeat (string :tag "Argument"))
315 :group 'browse-url)
317 ;;;###autoload
318 (defcustom browse-url-firefox-program "firefox"
319 "The name by which to invoke Firefox."
320 :type 'string
321 :group 'browse-url)
323 (defcustom browse-url-firefox-arguments nil
324 "A list of strings to pass to Firefox as arguments."
325 :type '(repeat (string :tag "Argument"))
326 :group 'browse-url)
328 (defcustom browse-url-firefox-startup-arguments browse-url-firefox-arguments
329 "A list of strings to pass to Firefox when it starts up.
330 Defaults to the value of `browse-url-firefox-arguments' at the time
331 `browse-url' is loaded."
332 :type '(repeat (string :tag "Argument"))
333 :group 'browse-url)
335 ;;;###autoload
336 (defcustom browse-url-galeon-program "galeon"
337 "The name by which to invoke Galeon."
338 :type 'string
339 :group 'browse-url)
341 (defcustom browse-url-galeon-arguments nil
342 "A list of strings to pass to Galeon as arguments."
343 :type '(repeat (string :tag "Argument"))
344 :group 'browse-url)
346 (defcustom browse-url-galeon-startup-arguments browse-url-galeon-arguments
347 "A list of strings to pass to Galeon when it starts up.
348 Defaults to the value of `browse-url-galeon-arguments' at the time
349 `browse-url' is loaded."
350 :type '(repeat (string :tag "Argument"))
351 :group 'browse-url)
353 (defcustom browse-url-epiphany-program "epiphany"
354 "The name by which to invoke Epiphany."
355 :type 'string
356 :group 'browse-url)
358 (defcustom browse-url-epiphany-arguments nil
359 "A list of strings to pass to Epiphany as arguments."
360 :type '(repeat (string :tag "Argument"))
361 :group 'browse-url)
363 (defcustom browse-url-epiphany-startup-arguments browse-url-epiphany-arguments
364 "A list of strings to pass to Epiphany when it starts up.
365 Defaults to the value of `browse-url-epiphany-arguments' at the time
366 `browse-url' is loaded."
367 :type '(repeat (string :tag "Argument"))
368 :group 'browse-url)
370 ;; GNOME means of invoking either Mozilla or Netrape.
371 (defvar browse-url-gnome-moz-program "gnome-moz-remote")
373 (defcustom browse-url-gnome-moz-arguments '()
374 "A list of strings passed to the GNOME mozilla viewer as arguments."
375 :version "21.1"
376 :type '(repeat (string :tag "Argument"))
377 :group 'browse-url)
379 (defcustom browse-url-mozilla-new-window-is-tab nil
380 "Whether to open up new windows in a tab or a new window.
381 If non-nil, then open the URL in a new tab rather than a new window if
382 `browse-url-mozilla' is asked to open it in a new window."
383 :type 'boolean
384 :group 'browse-url)
386 (defcustom browse-url-firefox-new-window-is-tab nil
387 "Whether to open up new windows in a tab or a new window.
388 If non-nil, then open the URL in a new tab rather than a new window if
389 `browse-url-firefox' is asked to open it in a new window.
391 This option is currently ignored on MS-Windows, since the necessary
392 functionality is not available there."
393 :type 'boolean
394 :group 'browse-url)
396 (defcustom browse-url-galeon-new-window-is-tab nil
397 "Whether to open up new windows in a tab or a new window.
398 If non-nil, then open the URL in a new tab rather than a new window if
399 `browse-url-galeon' is asked to open it in a new window."
400 :type 'boolean
401 :group 'browse-url)
403 (defcustom browse-url-epiphany-new-window-is-tab nil
404 "Whether to open up new windows in a tab or a new window.
405 If non-nil, then open the URL in a new tab rather than a new window if
406 `browse-url-epiphany' is asked to open it in a new window."
407 :type 'boolean
408 :group 'browse-url)
410 (defcustom browse-url-netscape-new-window-is-tab nil
411 "Whether to open up new windows in a tab or a new window.
412 If non-nil, then open the URL in a new tab rather than a new
413 window if `browse-url-netscape' is asked to open it in a new
414 window."
415 :type 'boolean
416 :group 'browse-url)
418 (defcustom browse-url-new-window-flag nil
419 "Non-nil means always open a new browser window with appropriate browsers.
420 Passing an interactive argument to \\[browse-url], or specific browser
421 commands reverses the effect of this variable. Requires Netscape version
422 1.1N or later or XMosaic version 2.5 or later if using those browsers."
423 :type 'boolean
424 :group 'browse-url)
426 (defcustom browse-url-mosaic-program "xmosaic"
427 "The name by which to invoke Mosaic (or mMosaic)."
428 :type 'string
429 :version "20.3"
430 :group 'browse-url)
432 (defcustom browse-url-mosaic-arguments nil
433 "A list of strings to pass to Mosaic as arguments."
434 :type '(repeat (string :tag "Argument"))
435 :group 'browse-url)
437 (defcustom browse-url-mosaic-pidfile "~/.mosaicpid"
438 "The name of the pidfile created by Mosaic."
439 :type 'string
440 :group 'browse-url)
442 (defcustom browse-url-filename-alist
443 `(("^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*" . "ftp://\\2/")
444 ;; The above loses the username to avoid the browser prompting for
445 ;; it in anonymous cases. If it's not anonymous the next regexp
446 ;; applies.
447 ("^/\\([^:@]+@\\)?\\([^:]+\\):/*" . "ftp://\\1\\2/")
448 ,@(if (memq system-type '(windows-nt ms-dos cygwin))
449 '(("^\\([a-zA-Z]:\\)[\\/]" . "file:\\1/")
450 ("^[\\/][\\/]+" . "file://")))
451 ("^/+" . "file:/"))
452 "An alist of (REGEXP . STRING) pairs used by `browse-url-of-file'.
453 Any substring of a filename matching one of the REGEXPs is replaced by
454 the corresponding STRING using `replace-match', not treating STRING
455 literally. All pairs are applied in the order given. The default
456 value converts ange-ftp/EFS-style file names into ftp URLs and prepends
457 `file:' to any file name beginning with `/'.
459 For example, adding to the default a specific translation of an ange-ftp
460 address to an HTTP URL:
462 (setq browse-url-filename-alist
463 '((\"/webmaster@webserver:/home/www/html/\" .
464 \"http://www.acme.co.uk/\")
465 (\"^/\\(ftp@\\|anonymous@\\)?\\([^:]+\\):/*\" . \"ftp://\\2/\")
466 (\"^/\\([^:@]+@\\)?\\([^:]+\\):/*\" . \"ftp://\\1\\2/\")
467 (\"^/+\" . \"file:/\")))"
468 :type '(repeat (cons :format "%v"
469 (regexp :tag "Regexp")
470 (string :tag "Replacement")))
471 :version "20.3"
472 :group 'browse-url)
474 (defcustom browse-url-save-file nil
475 "If non-nil, save the buffer before displaying its file.
476 Used by the `browse-url-of-file' command."
477 :type 'boolean
478 :group 'browse-url)
480 (defcustom browse-url-of-file-hook nil
481 "Run after `browse-url-of-file' has asked a browser to load a file.
483 Set this to `browse-url-netscape-reload' to force Netscape to load the
484 file rather than displaying a cached copy."
485 :type 'hook
486 :options '(browse-url-netscape-reload)
487 :group 'browse-url)
489 (defcustom browse-url-CCI-port 3003
490 "Port to access XMosaic via CCI.
491 This can be any number between 1024 and 65535 but must correspond to
492 the value set in the browser."
493 :type 'integer
494 :group 'browse-url)
496 (defcustom browse-url-CCI-host "localhost"
497 "Host to access XMosaic via CCI.
498 This should be the host name of the machine running XMosaic with CCI
499 enabled. The port number should be set in `browse-url-CCI-port'."
500 :type 'string
501 :group 'browse-url)
503 (defvar browse-url-temp-file-name nil)
504 (make-variable-buffer-local 'browse-url-temp-file-name)
506 (defcustom browse-url-xterm-program "xterm"
507 "The name of the terminal emulator used by `browse-url-lynx-xterm'.
508 This might, for instance, be a separate color version of xterm."
509 :type 'string
510 :group 'browse-url)
512 (defcustom browse-url-xterm-args nil
513 "A list of strings defining options for `browse-url-xterm-program'.
514 These might set its size, for instance."
515 :type '(repeat (string :tag "Argument"))
516 :group 'browse-url)
518 (defcustom browse-url-lynx-emacs-args (and (not window-system)
519 '("-show_cursor"))
520 "A list of strings defining options for Lynx in an Emacs buffer.
522 The default is none in a window system, otherwise `-show_cursor' to
523 indicate the position of the current link in the absence of
524 highlighting, assuming the normal default for showing the cursor."
525 :type '(repeat (string :tag "Argument"))
526 :version "20.3"
527 :group 'browse-url)
529 (defcustom browse-url-gnudoit-program "gnudoit"
530 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
531 :type 'string
532 :group 'browse-url)
534 (defcustom browse-url-gnudoit-args '("-q")
535 "A list of strings defining options for `browse-url-gnudoit-program'.
536 These might set the port, for instance."
537 :type '(repeat (string :tag "Argument"))
538 :group 'browse-url)
540 (defcustom browse-url-generic-program nil
541 "The name of the browser program used by `browse-url-generic'."
542 :type '(choice string (const :tag "None" nil))
543 :group 'browse-url)
545 (defcustom browse-url-generic-args nil
546 "A list of strings defining options for `browse-url-generic-program'."
547 :type '(repeat (string :tag "Argument"))
548 :group 'browse-url)
550 (defcustom browse-url-temp-dir temporary-file-directory
551 "The name of a directory for browse-url's temporary files.
552 Such files are generated by functions like `browse-url-of-region'.
553 You might want to set this to somewhere with restricted read permissions
554 for privacy's sake."
555 :type 'string
556 :group 'browse-url)
558 (defcustom browse-url-netscape-version 3
559 "The version of Netscape you are using.
560 This affects how URL reloading is done; the mechanism changed
561 incompatibly at version 4."
562 :type 'number
563 :group 'browse-url)
565 (defcustom browse-url-lynx-input-field 'avoid
566 "Action on selecting an existing Lynx buffer at an input field.
567 What to do when sending a new URL to an existing Lynx buffer in Emacs
568 if the Lynx cursor is on an input field (in which case the `g' command
569 would be entered as data). Such fields are recognized by the
570 underlines ____. Allowed values: nil: disregard it, 'warn: warn the
571 user and don't emit the URL, 'avoid: try to avoid the field by moving
572 down (this *won't* always work)."
573 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
574 (const :tag "Disregard" :value nil)
575 (const :tag "Warn, don't emit URL" :value warn))
576 :version "20.3"
577 :group 'browse-url)
579 (defcustom browse-url-lynx-input-attempts 10
580 "How many times to try to move down from a series of lynx input fields."
581 :type 'integer
582 :group 'browse-url)
584 (defcustom browse-url-lynx-input-delay 0.2
585 "How many seconds to wait for lynx between moves down from an input field."
586 :type 'number
587 :group 'browse-url)
589 (defcustom browse-url-kde-program "kfmclient"
590 "The name by which to invoke the KDE web browser."
591 :type 'string
592 :version "21.1"
593 :group 'browse-url)
595 (defcustom browse-url-kde-args '("openURL")
596 "A list of strings defining options for `browse-url-kde-program'."
597 :type '(repeat (string :tag "Argument"))
598 :group 'browse-url)
600 (defcustom browse-url-elinks-wrapper '("xterm" "-e")
601 "*Wrapper command prepended to the Elinks command-line."
602 :type '(repeat (string :tag "Wrapper"))
603 :group 'browse-url)
605 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
606 ;; URL encoding
608 (defun browse-url-url-encode-chars (text chars)
609 "URL-encode the chars in TEXT that match CHARS.
610 CHARS is a regexp-like character alternative (e.g., \"[,)$]\")."
611 (let ((encoded-text (copy-sequence text))
612 (s 0))
613 (while (setq s (string-match chars encoded-text s))
614 (setq encoded-text
615 (replace-match (format "%%%x"
616 (string-to-char (match-string 0 encoded-text)))
617 t t encoded-text)
618 s (1+ s)))
619 encoded-text))
621 (defun browse-url-encode-url (url)
622 "Escape annoying characters in URL.
623 The annoying characters are those that can mislead a webbrowser
624 regarding its parameter treatment. For instance, `,' can
625 be misleading because it could be used to separate URLs."
626 (browse-url-url-encode-chars url "[,)$]"))
628 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
629 ;; URL input
631 ;;;###autoload
632 (defun browse-url-url-at-point ()
633 (let ((url (thing-at-point 'url)))
634 (set-text-properties 0 (length url) nil url)
635 url))
637 ;; Having this as a separate function called by the browser-specific
638 ;; functions allows them to be stand-alone commands, making it easier
639 ;; to switch between browsers.
641 (defun browse-url-interactive-arg (prompt)
642 "Read a URL from the minibuffer, prompting with PROMPT.
643 If `transient-mark-mode' is non-nil and the mark is active,
644 it defaults to the current region, else to the URL at or before
645 point. If invoked with a mouse button, it moves point to the
646 position clicked before acting.
648 This function returns a list (URL NEW-WINDOW-FLAG)
649 for use in `interactive'."
650 (let ((event (elt (this-command-keys) 0)))
651 (and (listp event) (mouse-set-point event)))
652 (list (read-string prompt (or (and transient-mark-mode mark-active
653 ;; rfc2396 Appendix E.
654 (replace-regexp-in-string
655 "[\t\r\f\n ]+" ""
656 (buffer-substring-no-properties
657 (region-beginning) (region-end))))
658 (browse-url-url-at-point)))
659 (not (eq (null browse-url-new-window-flag)
660 (null current-prefix-arg)))))
662 ;; called-interactive-p needs to be called at a function's top-level, hence
663 ;; this macro. We use that rather than interactive-p because
664 ;; use in a keyboard macro should not change this behavior.
665 (defmacro browse-url-maybe-new-window (arg)
666 `(if (or noninteractive (not (called-interactively-p)))
667 ,arg
668 browse-url-new-window-flag))
670 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
671 ;; Browse current buffer
673 ;;;###autoload
674 (defun browse-url-of-file (&optional file)
675 "Ask a WWW browser to display FILE.
676 Display the current buffer's file if FILE is nil or if called
677 interactively. Turn the filename into a URL with function
678 `browse-url-file-url'. Pass the URL to a browser using the
679 `browse-url' function then run `browse-url-of-file-hook'."
680 (interactive)
681 (or file
682 (setq file (buffer-file-name))
683 (error "Current buffer has no file"))
684 (let ((buf (get-file-buffer file)))
685 (if buf
686 (with-current-buffer buf
687 (cond ((not (buffer-modified-p)))
688 (browse-url-save-file (save-buffer))
689 (t (message "%s modified since last save" file))))))
690 (browse-url (browse-url-file-url file))
691 (run-hooks 'browse-url-of-file-hook))
693 (defun browse-url-file-url (file)
694 "Return the URL corresponding to FILE.
695 Use variable `browse-url-filename-alist' to map filenames to URLs."
696 (let ((coding (and default-enable-multibyte-characters
697 (or file-name-coding-system
698 default-file-name-coding-system))))
699 (if coding (setq file (encode-coding-string file coding))))
700 (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
701 (dolist (map browse-url-filename-alist)
702 (when (and map (string-match (car map) file))
703 (setq file (replace-match (cdr map) t nil file))))
704 file)
706 ;;;###autoload
707 (defun browse-url-of-buffer (&optional buffer)
708 "Ask a WWW browser to display BUFFER.
709 Display the current buffer if BUFFER is nil. Display only the
710 currently visible part of BUFFER (from a temporary file) if buffer is
711 narrowed."
712 (interactive)
713 (save-excursion
714 (and buffer (set-buffer buffer))
715 (let ((file-name
716 ;; Ignore real name if restricted
717 (and (= (- (point-max) (point-min)) (buffer-size))
718 (or buffer-file-name
719 (and (boundp 'dired-directory) dired-directory)))))
720 (or file-name
721 (progn
722 (or browse-url-temp-file-name
723 (setq browse-url-temp-file-name
724 (convert-standard-filename
725 (make-temp-file
726 (expand-file-name "burl" browse-url-temp-dir)
727 nil ".html"))))
728 (setq file-name browse-url-temp-file-name)
729 (write-region (point-min) (point-max) file-name nil 'no-message)))
730 (browse-url-of-file file-name))))
732 (defun browse-url-delete-temp-file (&optional temp-file-name)
733 ;; Delete browse-url-temp-file-name from the file system
734 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
735 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
736 (if (and file-name (file-exists-p file-name))
737 (delete-file file-name))))
739 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
741 ;;;###autoload
742 (defun browse-url-of-dired-file ()
743 "In Dired, ask a WWW browser to display the file named on this line."
744 (interactive)
745 (browse-url-of-file (dired-get-filename)))
747 ;;;###autoload
748 (defun browse-url-of-region (min max)
749 "Ask a WWW browser to display the current region."
750 (interactive "r")
751 (save-excursion
752 (save-restriction
753 (narrow-to-region min max)
754 (browse-url-of-buffer))))
756 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
757 ;; Browser-independent commands
759 ;; A generic command to call the current browse-url-browser-function
761 ;;;###autoload
762 (defun browse-url (url &rest args)
763 "Ask a WWW browser to load URL.
764 Prompts for a URL, defaulting to the URL at or before point. Variable
765 `browse-url-browser-function' says which browser to use."
766 (interactive (browse-url-interactive-arg "URL: "))
767 (unless (interactive-p)
768 (setq args (or args (list browse-url-new-window-flag))))
769 (let ((process-environment (copy-sequence process-environment)))
770 ;; When connected to various displays, be careful to use the display of
771 ;; the currently selected frame, rather than the original start display,
772 ;; which may not even exist any more.
773 (if (stringp (frame-parameter (selected-frame) 'display))
774 (setenv "DISPLAY" (frame-parameter (selected-frame) 'display)))
775 (if (functionp browse-url-browser-function)
776 (apply browse-url-browser-function url args)
777 ;; The `function' can be an alist; look down it for first match
778 ;; and apply the function (which might be a lambda).
779 (catch 'done
780 (dolist (bf browse-url-browser-function)
781 (when (string-match (car bf) url)
782 (apply (cdr bf) url args)
783 (throw 'done t)))
784 (error "No browse-url-browser-function matching URL %s"
785 url)))))
787 ;;;###autoload
788 (defun browse-url-at-point (&optional arg)
789 "Ask a WWW browser to load the URL at or before point.
790 Doesn't let you edit the URL like `browse-url'. Variable
791 `browse-url-browser-function' says which browser to use."
792 (interactive "P")
793 (let ((url (browse-url-url-at-point)))
794 (if url
795 (browse-url url (if arg
796 (not browse-url-new-window-flag)
797 browse-url-new-window-flag))
798 (error "No URL found"))))
800 ;;;###autoload
801 (defun browse-url-at-mouse (event)
802 "Ask a WWW browser to load a URL clicked with the mouse.
803 The URL is the one around or before the position of the mouse click
804 but point is not changed. Doesn't let you edit the URL like
805 `browse-url'. Variable `browse-url-browser-function' says which browser
806 to use."
807 (interactive "e")
808 (save-excursion
809 (mouse-set-point event)
810 ;; This handles browse-url-new-window-flag properly
811 ;; when it gets no arg.
812 (browse-url-at-point)))
814 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
815 ;; Browser-specific commands
817 ;; --- Default MS-Windows browser ---
819 (defvar dos-windows-version)
821 (defun browse-url-default-windows-browser (url &optional new-window)
822 (interactive (browse-url-interactive-arg "URL: "))
823 (if (eq system-type 'ms-dos)
824 (if dos-windows-version
825 (shell-command (concat "start " (shell-quote-argument url)))
826 (error "Browsing URLs is not supported on this system"))
827 (w32-shell-execute "open" url)))
829 (defun browse-url-default-macosx-browser (url &optional new-window)
830 (interactive (browse-url-interactive-arg "URL: "))
831 (start-process (concat "open " url) nil "open" url))
833 ;; --- Netscape ---
835 (defun browse-url-process-environment ()
836 "Set DISPLAY in the environment to the X display the browser will use.
837 This is either the value of variable `browse-url-browser-display' if
838 non-nil, or the same display as Emacs if different from the current
839 environment, otherwise just use the current environment."
840 (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
841 (if display
842 (cons (concat "DISPLAY=" display) process-environment)
843 process-environment)))
845 (defun browse-url-emacs-display ()
846 "Return the X display Emacs is running on.
847 This is nil if the display is the same as the DISPLAY environment variable.
849 Actually Emacs could be using several displays; this just returns the
850 one showing the selected frame."
851 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
852 (and (not (equal display (getenv "DISPLAY")))
853 display)))
855 ;;;###autoload
856 (defun browse-url-default-browser (url &rest args)
857 "Find a suitable browser and ask it to load URL.
858 Default to the URL around or before point.
860 When called interactively, if variable `browse-url-new-window-flag' is
861 non-nil, load the document in a new window, if possible, otherwise use
862 a random existing one. A non-nil interactive prefix argument reverses
863 the effect of `browse-url-new-window-flag'.
865 When called non-interactively, optional second argument NEW-WINDOW is
866 used instead of `browse-url-new-window-flag'.
868 The order attempted is gnome-moz-remote, Mozilla, Firefox,
869 Galeon, Konqueror, Netscape, Mosaic, Lynx in an xterm, and then W3."
870 (apply
871 (cond
872 ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
873 ((executable-find browse-url-mozilla-program) 'browse-url-mozilla)
874 ((executable-find browse-url-firefox-program) 'browse-url-firefox)
875 ((executable-find browse-url-galeon-program) 'browse-url-galeon)
876 ((executable-find browse-url-kde-program) 'browse-url-kde)
877 ((executable-find browse-url-netscape-program) 'browse-url-netscape)
878 ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
879 ((executable-find browse-url-xterm-program) 'browse-url-lynx-xterm)
880 ((locate-library "w3") 'browse-url-w3)
882 (lambda (&ignore args) (error "No usable browser found"))))
883 url args))
885 ;;;###autoload
886 (defun browse-url-netscape (url &optional new-window)
887 "Ask the Netscape WWW browser to load URL.
888 Default to the URL around or before point. The strings in variable
889 `browse-url-netscape-arguments' are also passed to Netscape.
891 When called interactively, if variable `browse-url-new-window-flag' is
892 non-nil, load the document in a new Netscape window, otherwise use a
893 random existing one. A non-nil interactive prefix argument reverses
894 the effect of `browse-url-new-window-flag'.
896 If `browse-url-netscape-new-window-is-tab' is non-nil, then
897 whenever a document would otherwise be loaded in a new window, it
898 is loaded in a new tab in an existing window instead.
900 When called non-interactively, optional second argument NEW-WINDOW is
901 used instead of `browse-url-new-window-flag'."
902 (interactive (browse-url-interactive-arg "URL: "))
903 (setq url (browse-url-encode-url url))
904 (let* ((process-environment (browse-url-process-environment))
905 (process
906 (apply 'start-process
907 (concat "netscape " url) nil
908 browse-url-netscape-program
909 (append
910 browse-url-netscape-arguments
911 (if (eq window-system 'w32)
912 (list url)
913 (append
914 (if new-window '("-noraise"))
915 (list "-remote"
916 (concat "openURL(" url
917 (if (browse-url-maybe-new-window
918 new-window)
919 (if browse-url-netscape-new-window-is-tab
920 ",new-tab"
921 ",new-window"))
922 ")"))))))))
923 (set-process-sentinel process
924 `(lambda (process change)
925 (browse-url-netscape-sentinel process ,url)))))
927 (defun browse-url-netscape-sentinel (process url)
928 "Handle a change to the process communicating with Netscape."
929 (or (eq (process-exit-status process) 0)
930 (let* ((process-environment (browse-url-process-environment)))
931 ;; Netscape not running - start it
932 (message "Starting %s..." browse-url-netscape-program)
933 (apply 'start-process (concat "netscape" url) nil
934 browse-url-netscape-program
935 (append browse-url-netscape-startup-arguments (list url))))))
937 (defun browse-url-netscape-reload ()
938 "Ask Netscape to reload its current document.
939 How depends on `browse-url-netscape-version'."
940 (interactive)
941 ;; Backwards incompatibility reported by
942 ;; <peter.kruse@psychologie.uni-regensburg.de>.
943 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
944 "xfeDoCommand(reload)"
945 "reload")))
947 (defun browse-url-netscape-send (command)
948 "Send a remote control command to Netscape."
949 (let* ((process-environment (browse-url-process-environment)))
950 (apply 'start-process "netscape" nil
951 browse-url-netscape-program
952 (append browse-url-netscape-arguments
953 (list "-remote" command)))))
955 ;;;###autoload
956 (defun browse-url-mozilla (url &optional new-window)
957 "Ask the Mozilla WWW browser to load URL.
958 Default to the URL around or before point. The strings in variable
959 `browse-url-mozilla-arguments' are also passed to Mozilla.
961 When called interactively, if variable `browse-url-new-window-flag' is
962 non-nil, load the document in a new Mozilla 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-mozilla-new-window-is-tab' is non-nil, then whenever a
967 document would otherwise be loaded in a new window, it is loaded in a
968 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))
975 (process
976 (apply 'start-process
977 (concat "mozilla " url) nil
978 browse-url-mozilla-program
979 (append
980 browse-url-mozilla-arguments
981 (list "-remote"
982 (concat "openURL("
984 (if (browse-url-maybe-new-window
985 new-window)
986 (if browse-url-mozilla-new-window-is-tab
987 ",new-tab"
988 ",new-window"))
989 ")"))))))
990 (set-process-sentinel process
991 `(lambda (process change)
992 (browse-url-mozilla-sentinel process ,url)))))
994 (defun browse-url-mozilla-sentinel (process url)
995 "Handle a change to the process communicating with Mozilla."
996 (or (eq (process-exit-status process) 0)
997 (let* ((process-environment (browse-url-process-environment)))
998 ;; Mozilla is not running - start it
999 (message "Starting %s..." browse-url-mozilla-program)
1000 (apply 'start-process (concat "mozilla " url) nil
1001 browse-url-mozilla-program
1002 (append browse-url-mozilla-startup-arguments (list url))))))
1004 ;;;###autoload
1005 (defun browse-url-firefox (url &optional new-window)
1006 "Ask the Firefox WWW browser to load URL.
1007 Default to the URL around or before point. The strings in
1008 variable `browse-url-firefox-arguments' are also passed to
1009 Firefox.
1011 When called interactively, if variable
1012 `browse-url-new-window-flag' is non-nil, load the document in a
1013 new Firefox window, otherwise use a random existing one. A
1014 non-nil interactive prefix argument reverses the effect of
1015 `browse-url-new-window-flag'.
1017 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1018 whenever a document would otherwise be loaded in a new window, it
1019 is loaded in a new tab in an existing window instead.
1021 When called non-interactively, optional second argument
1022 NEW-WINDOW is used instead of `browse-url-new-window-flag'.
1024 On MS-Windows systems the optional `new-window' parameter is
1025 ignored. Firefox for Windows does not support the \"-remote\"
1026 command line parameter. Therefore, the
1027 `browse-url-new-window-flag' and `browse-url-firefox-new-window-is-tab'
1028 are ignored as well. Firefox on Windows will always open the requested
1029 URL in a new window."
1030 (interactive (browse-url-interactive-arg "URL: "))
1031 (setq url (browse-url-encode-url url))
1032 (let* ((process-environment (browse-url-process-environment))
1033 (process
1034 (apply 'start-process
1035 (concat "firefox " url) nil
1036 browse-url-firefox-program
1037 (append
1038 browse-url-firefox-arguments
1039 (if (or (featurep 'dos-w32)
1040 (string-match "win32" system-configuration))
1041 (list url)
1042 (list "-remote"
1043 (concat "openURL("
1045 (if (browse-url-maybe-new-window
1046 new-window)
1047 (if browse-url-firefox-new-window-is-tab
1048 ",new-tab"
1049 ",new-window"))
1050 ")")))))))
1051 (set-process-sentinel process
1052 `(lambda (process change)
1053 (browse-url-firefox-sentinel process ,url)))))
1055 (defun browse-url-firefox-sentinel (process url)
1056 "Handle a change to the process communicating with Firefox."
1057 (or (eq (process-exit-status process) 0)
1058 (let* ((process-environment (browse-url-process-environment)))
1059 ;; Firefox is not running - start it
1060 (message "Starting Firefox...")
1061 (apply 'start-process (concat "firefox " url) nil
1062 browse-url-firefox-program
1063 (append browse-url-firefox-startup-arguments (list url))))))
1065 ;;;###autoload
1066 (defun browse-url-galeon (url &optional new-window)
1067 "Ask the Galeon WWW browser to load URL.
1068 Default to the URL around or before point. The strings in variable
1069 `browse-url-galeon-arguments' are also passed to Galeon.
1071 When called interactively, if variable `browse-url-new-window-flag' is
1072 non-nil, load the document in a new Galeon window, otherwise use a
1073 random existing one. A non-nil interactive prefix argument reverses
1074 the effect of `browse-url-new-window-flag'.
1076 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1077 document would otherwise be loaded in a new window, it is loaded in a
1078 new tab in an existing window instead.
1080 When called non-interactively, optional second argument NEW-WINDOW is
1081 used instead of `browse-url-new-window-flag'."
1082 (interactive (browse-url-interactive-arg "URL: "))
1083 (setq url (browse-url-encode-url url))
1084 (let* ((process-environment (browse-url-process-environment))
1085 (process (apply 'start-process
1086 (concat "galeon " url)
1088 browse-url-galeon-program
1089 (append
1090 browse-url-galeon-arguments
1091 (if (browse-url-maybe-new-window new-window)
1092 (if browse-url-galeon-new-window-is-tab
1093 '("--new-tab")
1094 '("--new-window" "--noraise"))
1095 '("--existing"))
1096 (list url)))))
1097 (set-process-sentinel process
1098 `(lambda (process change)
1099 (browse-url-galeon-sentinel process ,url)))))
1101 (defun browse-url-galeon-sentinel (process url)
1102 "Handle a change to the process communicating with Galeon."
1103 (or (eq (process-exit-status process) 0)
1104 (let* ((process-environment (browse-url-process-environment)))
1105 ;; Galeon is not running - start it
1106 (message "Starting %s..." browse-url-galeon-program)
1107 (apply 'start-process (concat "galeon " url) nil
1108 browse-url-galeon-program
1109 (append browse-url-galeon-startup-arguments (list url))))))
1111 (defun browse-url-epiphany (url &optional new-window)
1112 "Ask the Epiphany WWW browser to load URL.
1113 Default to the URL around or before point. The strings in variable
1114 `browse-url-galeon-arguments' are also passed to Epiphany.
1116 When called interactively, if variable `browse-url-new-window-flag' is
1117 non-nil, load the document in a new Epiphany window, otherwise use a
1118 random existing one. A non-nil interactive prefix argument reverses
1119 the effect of `browse-url-new-window-flag'.
1121 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1122 document would otherwise be loaded in a new window, it is loaded in a
1123 new tab in an existing window instead.
1125 When called non-interactively, optional second argument NEW-WINDOW is
1126 used instead of `browse-url-new-window-flag'."
1127 (interactive (browse-url-interactive-arg "URL: "))
1128 (setq url (browse-url-encode-url url))
1129 (let* ((process-environment (browse-url-process-environment))
1130 (process (apply 'start-process
1131 (concat "epiphany " url)
1133 browse-url-epiphany-program
1134 (append
1135 browse-url-epiphany-arguments
1136 (if (browse-url-maybe-new-window new-window)
1137 (if browse-url-epiphany-new-window-is-tab
1138 '("--new-tab")
1139 '("--new-window" "--noraise"))
1140 '("--existing"))
1141 (list url)))))
1142 (set-process-sentinel process
1143 `(lambda (process change)
1144 (browse-url-epiphany-sentinel process ,url)))))
1146 (defun browse-url-epiphany-sentinel (process url)
1147 "Handle a change to the process communicating with Epiphany."
1148 (or (eq (process-exit-status process) 0)
1149 (let* ((process-environment (browse-url-process-environment)))
1150 ;; Epiphany is not running - start it
1151 (message "Starting %s..." browse-url-epiphany-program)
1152 (apply 'start-process (concat "epiphany " url) nil
1153 browse-url-epiphany-program
1154 (append browse-url-epiphany-startup-arguments (list url))))))
1156 (defvar url-handler-regexp)
1158 ;;;###autoload
1159 (defun browse-url-emacs (url &optional new-window)
1160 "Ask Emacs to load URL into a buffer and show it in another window."
1161 (interactive (browse-url-interactive-arg "URL: "))
1162 (require 'url-handlers)
1163 (let ((file-name-handler-alist
1164 (cons (cons url-handler-regexp 'url-file-handler)
1165 file-name-handler-alist)))
1166 ;; Ignore `new-window': with all other browsers the URL is always shown
1167 ;; in another window than the current Emacs one since it's shown in
1168 ;; another application's window.
1169 ;; (if new-window (find-file-other-window url) (find-file url))
1170 (find-file-other-window url)))
1172 ;;;###autoload
1173 (defun browse-url-gnome-moz (url &optional new-window)
1174 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1175 Default to the URL around or before point. The strings in variable
1176 `browse-url-gnome-moz-arguments' are also passed.
1178 When called interactively, if variable `browse-url-new-window-flag' is
1179 non-nil, load the document in a new browser window, otherwise use an
1180 existing one. A non-nil interactive prefix argument reverses the
1181 effect of `browse-url-new-window-flag'.
1183 When called non-interactively, optional second argument NEW-WINDOW is
1184 used instead of `browse-url-new-window-flag'."
1185 (interactive (browse-url-interactive-arg "URL: "))
1186 (apply 'start-process (concat "gnome-moz-remote " url)
1188 browse-url-gnome-moz-program
1189 (append
1190 browse-url-gnome-moz-arguments
1191 (if (browse-url-maybe-new-window new-window)
1192 '("--newwin"))
1193 (list "--raise" url))))
1195 ;; --- Mosaic ---
1197 ;;;###autoload
1198 (defun browse-url-mosaic (url &optional new-window)
1199 "Ask the XMosaic WWW browser to load URL.
1201 Default to the URL around or before point. The strings in variable
1202 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1203 program is invoked according to the variable
1204 `browse-url-mosaic-program'.
1206 When called interactively, if variable `browse-url-new-window-flag' is
1207 non-nil, load the document in a new Mosaic window, otherwise use a
1208 random existing one. A non-nil interactive prefix argument reverses
1209 the effect of `browse-url-new-window-flag'.
1211 When called non-interactively, optional second argument NEW-WINDOW is
1212 used instead of `browse-url-new-window-flag'."
1213 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1214 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
1215 pid)
1216 (if (file-readable-p pidfile)
1217 (save-excursion
1218 (find-file pidfile)
1219 (goto-char (point-min))
1220 (setq pid (read (current-buffer)))
1221 (kill-buffer nil)))
1222 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
1223 (save-excursion
1224 (find-file (format "/tmp/Mosaic.%d" pid))
1225 (erase-buffer)
1226 (insert (if (browse-url-maybe-new-window new-window)
1227 "newwin\n"
1228 "goto\n")
1229 url "\n")
1230 (save-buffer)
1231 (kill-buffer nil)
1232 ;; Send signal SIGUSR to Mosaic
1233 (message "Signaling Mosaic...")
1234 (signal-process pid 'SIGUSR1)
1235 ;; Or you could try:
1236 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1237 (message "Signaling Mosaic...done")
1239 ;; Mosaic not running - start it
1240 (message "Starting %s..." browse-url-mosaic-program)
1241 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1242 (append browse-url-mosaic-arguments (list url)))
1243 (message "Starting %s...done" browse-url-mosaic-program))))
1245 ;; --- Mosaic using CCI ---
1247 ;;;###autoload
1248 (defun browse-url-cci (url &optional new-window)
1249 "Ask the XMosaic WWW browser to load URL.
1250 Default to the URL around or before point.
1252 This function only works for XMosaic version 2.5 or later. You must
1253 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1254 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1256 When called interactively, if variable `browse-url-new-window-flag' is
1257 non-nil, load the document in a new browser window, otherwise use a
1258 random existing one. A non-nil interactive prefix argument reverses
1259 the effect of `browse-url-new-window-flag'.
1261 When called non-interactively, optional second argument NEW-WINDOW is
1262 used instead of `browse-url-new-window-flag'."
1263 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1264 (open-network-stream "browse-url" " *browse-url*"
1265 browse-url-CCI-host browse-url-CCI-port)
1266 ;; Todo: start browser if fails
1267 (process-send-string "browse-url"
1268 (concat "get url (" url ") output "
1269 (if (browse-url-maybe-new-window new-window)
1270 "new"
1271 "current")
1272 "\r\n"))
1273 (process-send-string "browse-url" "disconnect\r\n")
1274 (delete-process "browse-url"))
1276 ;; --- W3 ---
1278 ;;;###autoload
1279 (defun browse-url-w3 (url &optional new-window)
1280 "Ask the w3 WWW browser to load URL.
1281 Default to the URL around or before point.
1283 When called interactively, if variable `browse-url-new-window-flag' is
1284 non-nil, load the document in a new window. A non-nil interactive
1285 prefix argument reverses the effect of `browse-url-new-window-flag'.
1287 When called non-interactively, optional second argument NEW-WINDOW is
1288 used instead of `browse-url-new-window-flag'."
1289 (interactive (browse-url-interactive-arg "W3 URL: "))
1290 (require 'w3) ; w3-fetch-other-window not autoloaded
1291 (if (browse-url-maybe-new-window new-window)
1292 (w3-fetch-other-window url)
1293 (w3-fetch url)))
1295 ;;;###autoload
1296 (defun browse-url-w3-gnudoit (url &optional new-window)
1297 ;; new-window ignored
1298 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1299 The `browse-url-gnudoit-program' program is used with options given by
1300 `browse-url-gnudoit-args'. Default to the URL around or before point."
1301 (interactive (browse-url-interactive-arg "W3 URL: "))
1302 (apply 'start-process (concat "gnudoit:" url) nil
1303 browse-url-gnudoit-program
1304 (append browse-url-gnudoit-args
1305 (list (concat "(w3-fetch \"" url "\")")
1306 "(raise-frame)"))))
1308 ;; --- Lynx in an xterm ---
1310 ;;;###autoload
1311 (defun browse-url-lynx-xterm (url &optional new-window)
1312 ;; new-window ignored
1313 "Ask the Lynx WWW browser to load URL.
1314 Default to the URL around or before point. A new Lynx process is run
1315 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1316 with possible additional arguments `browse-url-xterm-args'."
1317 (interactive (browse-url-interactive-arg "Lynx URL: "))
1318 (apply #'start-process `(,(concat "lynx" url) nil ,browse-url-xterm-program
1319 ,@browse-url-xterm-args "-e" "lynx"
1320 ,url)))
1322 ;; --- Lynx in an Emacs "term" window ---
1324 ;;;###autoload
1325 (defun browse-url-lynx-emacs (url &optional new-buffer)
1326 "Ask the Lynx WWW browser to load URL.
1327 Default to the URL around or before point. With a prefix argument, run
1328 a new Lynx process in a new buffer.
1330 When called interactively, if variable `browse-url-new-window-flag' is
1331 non-nil, load the document in a new lynx in a new term window,
1332 otherwise use any existing one. A non-nil interactive prefix argument
1333 reverses the effect of `browse-url-new-window-flag'.
1335 When called non-interactively, optional second argument NEW-WINDOW is
1336 used instead of `browse-url-new-window-flag'."
1337 (interactive (browse-url-interactive-arg "Lynx URL: "))
1338 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
1339 ;; (term-term-name "vt100") ; ??
1340 (buf (get-buffer "*lynx*"))
1341 (proc (and buf (get-buffer-process buf)))
1342 (n browse-url-lynx-input-attempts))
1343 (if (and (browse-url-maybe-new-window new-buffer) buf)
1344 ;; Rename away the OLD buffer. This isn't very polite, but
1345 ;; term insists on working in a buffer named *lynx* and would
1346 ;; choke on *lynx*<1>
1347 (progn (set-buffer buf)
1348 (rename-uniquely)))
1349 (if (or (browse-url-maybe-new-window new-buffer)
1350 (not buf)
1351 (not proc)
1352 (not (memq (process-status proc) '(run stop))))
1353 ;; start a new lynx
1354 (progn
1355 (setq buf
1356 (apply #'make-term
1357 `("lynx" "lynx" nil ,@browse-url-lynx-emacs-args
1358 ,url)))
1359 (switch-to-buffer buf)
1360 (term-char-mode)
1361 (set-process-sentinel
1362 (get-buffer-process buf)
1363 ;; Don't leave around a dead one (especially because of its
1364 ;; munged keymap.)
1365 (lambda (process event)
1366 (if (not (memq (process-status process) '(run stop)))
1367 (let ((buf (process-buffer process)))
1368 (if buf (kill-buffer buf)))))))
1369 ;; send the url to lynx in the old buffer
1370 (let ((win (get-buffer-window buf t)))
1371 (if win
1372 (select-window win)
1373 (switch-to-buffer buf)))
1374 (if (eq (following-char) ?_)
1375 (cond ((eq browse-url-lynx-input-field 'warn)
1376 (error "Please move out of the input field first"))
1377 ((eq browse-url-lynx-input-field 'avoid)
1378 (while (and (eq (following-char) ?_) (> n 0))
1379 (term-send-down) ; down arrow
1380 (sit-for browse-url-lynx-input-delay))
1381 (if (eq (following-char) ?_)
1382 (error "Cannot move out of the input field, sorry")))))
1383 (term-send-string proc (concat "g" ; goto
1384 "\C-u" ; kill default url
1386 "\r")))))
1388 ;; --- mailto ---
1390 (autoload 'rfc2368-parse-mailto-url "rfc2368")
1392 ;;;###autoload
1393 (defun browse-url-mail (url &optional new-window)
1394 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1395 Default to using the mailto: URL around or before point as the
1396 recipient's address. Supplying a non-nil interactive prefix argument
1397 will cause the mail to be composed in another window rather than the
1398 current one.
1400 When called interactively, if variable `browse-url-new-window-flag' is
1401 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1402 non-nil interactive prefix argument reverses the effect of
1403 `browse-url-new-window-flag'.
1405 When called non-interactively, optional second argument NEW-WINDOW is
1406 used instead of `browse-url-new-window-flag'."
1407 (interactive (browse-url-interactive-arg "Mailto URL: "))
1408 (save-excursion
1409 (let* ((alist (rfc2368-parse-mailto-url url))
1410 (to (assoc "To" alist))
1411 (subject (assoc "Subject" alist))
1412 (body (assoc "Body" alist))
1413 (rest (delete to (delete subject (delete body alist))))
1414 (to (cdr to))
1415 (subject (cdr subject))
1416 (body (cdr body))
1417 (mail-citation-hook (unless body mail-citation-hook)))
1418 (if (browse-url-maybe-new-window new-window)
1419 (compose-mail-other-window to subject rest nil
1420 (if body
1421 (list 'insert body)
1422 (list 'insert-buffer (current-buffer))))
1423 (compose-mail to subject rest nil nil
1424 (if body
1425 (list 'insert body)
1426 (list 'insert-buffer (current-buffer))))))))
1428 ;; --- Random browser ---
1430 ;;;###autoload
1431 (defun browse-url-generic (url &optional new-window)
1432 ;; new-window ignored
1433 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1434 Default to the URL around or before point. A fresh copy of the
1435 browser is started up in a new process with possible additional arguments
1436 `browse-url-generic-args'. This is appropriate for browsers which
1437 don't offer a form of remote control."
1438 (interactive (browse-url-interactive-arg "URL: "))
1439 (if (not browse-url-generic-program)
1440 (error "No browser defined (`browse-url-generic-program')"))
1441 (apply 'call-process browse-url-generic-program nil
1442 0 nil
1443 (append browse-url-generic-args (list url))))
1445 ;;;###autoload
1446 (defun browse-url-kde (url &optional new-window)
1447 "Ask the KDE WWW browser to load URL.
1448 Default to the URL around or before point."
1449 (interactive (browse-url-interactive-arg "KDE URL: "))
1450 (message "Sending URL to KDE...")
1451 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
1452 (append browse-url-kde-args (list url))))
1454 (defun browse-url-elinks-new-window (url)
1455 "Ask the Elinks WWW browser to load URL in a new window."
1456 (let ((process-environment (browse-url-process-environment)))
1457 (apply #'start-process
1458 (append (list (concat "elinks:" url)
1459 nil)
1460 browse-url-elinks-wrapper
1461 (list "elinks" url)))))
1463 ;;;###autoload
1464 (defun browse-url-elinks (url &optional new-window)
1465 "Ask the Elinks WWW browser to load URL.
1466 Default to the URL around the point.
1468 The document is loaded in a new tab of a running Elinks or, if
1469 none yet running, a newly started instance.
1471 The Elinks command will be prepended by the program+arguments
1472 from `browse-url-elinks-wrapper'."
1473 (interactive (browse-url-interactive-arg "URL: "))
1474 (setq url (browse-url-encode-url url))
1475 (if new-window
1476 (browse-url-elinks-new-window url)
1477 (let ((process-environment (browse-url-process-environment))
1478 (elinks-ping-process (start-process "elinks-ping" nil
1479 "elinks" "-remote" "ping()")))
1480 (set-process-sentinel elinks-ping-process
1481 `(lambda (process change)
1482 (browse-url-elinks-sentinel process ,url))))))
1484 (defun browse-url-elinks-sentinel (process url)
1485 "Determines if Elinks is running or a new one has to be started."
1486 (let ((exit-status (process-exit-status process)))
1487 ;; Try to determine if an instance is running or if we have to
1488 ;; create a new one.
1489 (case exit-status
1491 ;; No instance, start a new one.
1492 (browse-url-elinks-new-window url))
1494 ;; Found an instance, open URL in new tab.
1495 (let ((process-environment (browse-url-process-environment)))
1496 (start-process (concat "elinks:" url) nil
1497 "elinks" "-remote"
1498 (concat "openURL(\"" url "\",new-tab)"))))
1499 (otherwise
1500 (error "Unrecognized exit-code %d of process `elinks'"
1501 exit-status)))))
1503 (provide 'browse-url)
1505 ;; arch-tag: d2079573-5c06-4097-9598-f550fba19430
1506 ;;; browse-url.el ends here