]> code.delx.au - gnu-emacs/blob - lisp/net/browse-url.el
* net/tramp.el (tramp-root-regexp): Simplify.
[gnu-emacs] / lisp / net / browse-url.el
1 ;;; browse-url.el --- pass a URL to a WWW browser
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Denis Howe <dbh@doc.ic.ac.uk>
7 ;; Maintainer: FSF
8 ;; Created: 03 Apr 1995
9 ;; Keywords: hypertext, hypermedia, mouse
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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.
27
28 ;;; Commentary:
29
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:
38
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-text-* Any text browser 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)
56
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.]
60
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>.
63
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.
67
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?
71
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/>.
79
80 ;; Lynx is now distributed by the FSF. See also
81 ;; <URL:http://lynx.browser.org/>.
82
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.
92
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/>
97
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.
103
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.
108
109 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 ;; Help!
111
112 ;; Can you write and test some code for the Macintrash and Windoze
113 ;; Netscape remote control APIs? (See the URL above).
114
115 ;; Do any other browsers have remote control?
116
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;; Usage
119
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
125
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.)
131
132 ;; To display the current buffer in a web browser:
133 ;; M-x browse-url-of-buffer RET
134
135 ;; To display the current region in a web browser:
136 ;; M-x browse-url-of-region RET
137
138 ;; In Dired, to display the file named on the current line:
139 ;; M-x browse-url-of-dired-file RET
140
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; Customisation (~/.emacs)
143
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'.
147
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)))
158
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'.
163
164 ;; Gnus provides a standard feature to activate URLs in article
165 ;; buffers for invocation of browse-url.
166
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))
170
171 ;; To always save modified buffers before displaying the file in a browser:
172 ;; (setq browse-url-save-file t)
173
174 ;; To get round the Netscape caching problem, you could EITHER have
175 ;; write-file in html-helper-mode make Netscape reload the document:
176 ;;
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
188 ;;
189 ;; OR have browse-url-of-file ask Netscape to load and then reload the
190 ;; file:
191 ;;
192 ;; (add-hook 'browse-url-of-file-hook 'browse-url-netscape-reload)
193
194 ;; You may also want to customise browse-url-netscape-arguments, e.g.
195 ;; (setq browse-url-netscape-arguments '("-install"))
196 ;;
197 ;; or similarly for the other browsers.
198
199 ;; To invoke different browsers for different URLs:
200 ;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
201 ;; ("." . browse-url-netscape)))
202
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 ;;; Code:
205
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207 ;; Variables
208
209 (eval-when-compile (require 'cl)
210 (require 'thingatpt)
211 (require 'term)
212 (require 'dired)
213 (require 'executable)
214 (require 'w3-auto nil t))
215
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)
221
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.
232
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 "Text browser in an xterm window"
250 :value browse-url-text-xterm)
251 (function-item :tag "Text browser in an Emacs window"
252 :value browse-url-text-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)
270
271 (defcustom browse-url-netscape-program "netscape"
272 ;; Info about netscape-remote from Karl Berry.
273 "The name by which to invoke Netscape.
274
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)
282
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)
287
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)
294
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)
299
300 (defcustom browse-url-mozilla-program "mozilla"
301 "The name by which to invoke Mozilla."
302 :type 'string
303 :group 'browse-url)
304
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)
309
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)
316
317 ;;;###autoload
318 (defcustom browse-url-firefox-program "firefox"
319 "The name by which to invoke Firefox."
320 :type 'string
321 :group 'browse-url)
322
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)
327
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)
334
335 ;;;###autoload
336 (defcustom browse-url-galeon-program "galeon"
337 "The name by which to invoke Galeon."
338 :type 'string
339 :group 'browse-url)
340
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)
345
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)
352
353 (defcustom browse-url-epiphany-program "epiphany"
354 "The name by which to invoke Epiphany."
355 :type 'string
356 :group 'browse-url)
357
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)
362
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)
369
370 ;; GNOME means of invoking either Mozilla or Netrape.
371 (defvar browse-url-gnome-moz-program "gnome-moz-remote")
372
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)
378
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)
385
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.
390
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)
395
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)
402
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)
409
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)
417
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)
425
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)
431
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)
436
437 (defcustom browse-url-mosaic-pidfile "~/.mosaicpid"
438 "The name of the pidfile created by Mosaic."
439 :type 'string
440 :group 'browse-url)
441
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 `/'.
458
459 For example, adding to the default a specific translation of an ange-ftp
460 address to an HTTP URL:
461
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)
473
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)
479
480 (defcustom browse-url-of-file-hook nil
481 "Run after `browse-url-of-file' has asked a browser to load a file.
482
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)
488
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)
495
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)
502
503 (defvar browse-url-temp-file-name nil)
504 (make-variable-buffer-local 'browse-url-temp-file-name)
505
506 (defcustom browse-url-xterm-program "xterm"
507 "The name of the terminal emulator used by `browse-url-text-xterm'.
508 This might, for instance, be a separate color version of xterm."
509 :type 'string
510 :group 'browse-url)
511
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)
517
518 (defcustom browse-url-gnudoit-program "gnudoit"
519 "The name of the `gnudoit' program used by `browse-url-w3-gnudoit'."
520 :type 'string
521 :group 'browse-url)
522
523 (defcustom browse-url-gnudoit-args '("-q")
524 "A list of strings defining options for `browse-url-gnudoit-program'.
525 These might set the port, for instance."
526 :type '(repeat (string :tag "Argument"))
527 :group 'browse-url)
528
529 (defcustom browse-url-generic-program nil
530 "The name of the browser program used by `browse-url-generic'."
531 :type '(choice string (const :tag "None" nil))
532 :group 'browse-url)
533
534 (defcustom browse-url-generic-args nil
535 "A list of strings defining options for `browse-url-generic-program'."
536 :type '(repeat (string :tag "Argument"))
537 :group 'browse-url)
538
539 (defcustom browse-url-temp-dir temporary-file-directory
540 "The name of a directory for browse-url's temporary files.
541 Such files are generated by functions like `browse-url-of-region'.
542 You might want to set this to somewhere with restricted read permissions
543 for privacy's sake."
544 :type 'string
545 :group 'browse-url)
546
547 (defcustom browse-url-netscape-version 3
548 "The version of Netscape you are using.
549 This affects how URL reloading is done; the mechanism changed
550 incompatibly at version 4."
551 :type 'number
552 :group 'browse-url)
553
554 (defcustom browse-url-text-browser "lynx"
555 "The name of the text browser to invoke."
556 :type 'string
557 :group 'browse-url
558 :version "23.1")
559
560 (defcustom browse-url-text-emacs-args (and (not window-system)
561 '("-show_cursor"))
562 "A list of strings defining options for a text browser in an Emacs buffer.
563
564 The default is none in a window system, otherwise `-show_cursor' to
565 indicate the position of the current link in the absence of
566 highlighting, assuming the normal default for showing the cursor."
567 :type '(repeat (string :tag "Argument"))
568 :version "23.1"
569 :group 'browse-url)
570
571 (defcustom browse-url-text-input-field 'avoid
572 "Action on selecting an existing text browser buffer at an input field.
573 What to do when sending a new URL to an existing text browser buffer in Emacs
574 if the browser cursor is on an input field (in which case the `g' command
575 would be entered as data). Such fields are recognized by the
576 underlines ____. Allowed values: nil: disregard it, `warn': warn the
577 user and don't emit the URL, `avoid': try to avoid the field by moving
578 down (this *won't* always work)."
579 :type '(choice (const :tag "Move to try to avoid field" :value avoid)
580 (const :tag "Disregard" :value nil)
581 (const :tag "Warn, don't emit URL" :value warn))
582 :version "23.1"
583 :group 'browse-url)
584
585 (defcustom browse-url-text-input-attempts 10
586 "How many times to try to move down from a series of text browser input fields."
587 :type 'integer
588 :version "23.1"
589 :group 'browse-url)
590
591 (defcustom browse-url-text-input-delay 0.2
592 "Seconds to wait for a text browser between moves down from an input field."
593 :type 'number
594 :version "23.1"
595 :group 'browse-url)
596
597 (defcustom browse-url-kde-program "kfmclient"
598 "The name by which to invoke the KDE web browser."
599 :type 'string
600 :version "21.1"
601 :group 'browse-url)
602
603 (defcustom browse-url-kde-args '("openURL")
604 "A list of strings defining options for `browse-url-kde-program'."
605 :type '(repeat (string :tag "Argument"))
606 :group 'browse-url)
607
608 (defcustom browse-url-elinks-wrapper '("xterm" "-e")
609 "*Wrapper command prepended to the Elinks command-line."
610 :type '(repeat (string :tag "Wrapper"))
611 :group 'browse-url)
612
613 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
614 ;; URL encoding
615
616 (defun browse-url-url-encode-chars (text chars)
617 "URL-encode the chars in TEXT that match CHARS.
618 CHARS is a regexp-like character alternative (e.g., \"[,)$]\")."
619 (let ((encoded-text (copy-sequence text))
620 (s 0))
621 (while (setq s (string-match chars encoded-text s))
622 (setq encoded-text
623 (replace-match (format "%%%x"
624 (string-to-char (match-string 0 encoded-text)))
625 t t encoded-text)
626 s (1+ s)))
627 encoded-text))
628
629 (defun browse-url-encode-url (url)
630 "Escape annoying characters in URL.
631 The annoying characters are those that can mislead a webbrowser
632 regarding its parameter treatment. For instance, `,' can
633 be misleading because it could be used to separate URLs."
634 (browse-url-url-encode-chars url "[,)$]"))
635
636 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
637 ;; URL input
638
639 ;;;###autoload
640 (defun browse-url-url-at-point ()
641 (let ((url (thing-at-point 'url)))
642 (set-text-properties 0 (length url) nil url)
643 url))
644
645 ;; Having this as a separate function called by the browser-specific
646 ;; functions allows them to be stand-alone commands, making it easier
647 ;; to switch between browsers.
648
649 (defun browse-url-interactive-arg (prompt)
650 "Read a URL from the minibuffer, prompting with PROMPT.
651 If `transient-mark-mode' is non-nil and the mark is active,
652 it defaults to the current region, else to the URL at or before
653 point. If invoked with a mouse button, it moves point to the
654 position clicked before acting.
655
656 This function returns a list (URL NEW-WINDOW-FLAG)
657 for use in `interactive'."
658 (let ((event (elt (this-command-keys) 0)))
659 (and (listp event) (mouse-set-point event)))
660 (list (read-string prompt (or (and transient-mark-mode mark-active
661 ;; rfc2396 Appendix E.
662 (replace-regexp-in-string
663 "[\t\r\f\n ]+" ""
664 (buffer-substring-no-properties
665 (region-beginning) (region-end))))
666 (browse-url-url-at-point)))
667 (not (eq (null browse-url-new-window-flag)
668 (null current-prefix-arg)))))
669
670 ;; called-interactive-p needs to be called at a function's top-level, hence
671 ;; this macro. We use that rather than interactive-p because
672 ;; use in a keyboard macro should not change this behavior.
673 (defmacro browse-url-maybe-new-window (arg)
674 `(if (or noninteractive (not (called-interactively-p)))
675 ,arg
676 browse-url-new-window-flag))
677
678 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
679 ;; Browse current buffer
680
681 ;;;###autoload
682 (defun browse-url-of-file (&optional file)
683 "Ask a WWW browser to display FILE.
684 Display the current buffer's file if FILE is nil or if called
685 interactively. Turn the filename into a URL with function
686 `browse-url-file-url'. Pass the URL to a browser using the
687 `browse-url' function then run `browse-url-of-file-hook'."
688 (interactive)
689 (or file
690 (setq file (buffer-file-name))
691 (error "Current buffer has no file"))
692 (let ((buf (get-file-buffer file)))
693 (if buf
694 (with-current-buffer buf
695 (cond ((not (buffer-modified-p)))
696 (browse-url-save-file (save-buffer))
697 (t (message "%s modified since last save" file))))))
698 (browse-url (browse-url-file-url file))
699 (run-hooks 'browse-url-of-file-hook))
700
701 (defun browse-url-file-url (file)
702 "Return the URL corresponding to FILE.
703 Use variable `browse-url-filename-alist' to map filenames to URLs."
704 (let ((coding (and default-enable-multibyte-characters
705 (or file-name-coding-system
706 default-file-name-coding-system))))
707 (if coding (setq file (encode-coding-string file coding))))
708 (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
709 (dolist (map browse-url-filename-alist)
710 (when (and map (string-match (car map) file))
711 (setq file (replace-match (cdr map) t nil file))))
712 file)
713
714 ;;;###autoload
715 (defun browse-url-of-buffer (&optional buffer)
716 "Ask a WWW browser to display BUFFER.
717 Display the current buffer if BUFFER is nil. Display only the
718 currently visible part of BUFFER (from a temporary file) if buffer is
719 narrowed."
720 (interactive)
721 (save-excursion
722 (and buffer (set-buffer buffer))
723 (let ((file-name
724 ;; Ignore real name if restricted
725 (and (= (- (point-max) (point-min)) (buffer-size))
726 (or buffer-file-name
727 (and (boundp 'dired-directory) dired-directory)))))
728 (or file-name
729 (progn
730 (or browse-url-temp-file-name
731 (setq browse-url-temp-file-name
732 (convert-standard-filename
733 (make-temp-file
734 (expand-file-name "burl" browse-url-temp-dir)
735 nil ".html"))))
736 (setq file-name browse-url-temp-file-name)
737 (write-region (point-min) (point-max) file-name nil 'no-message)))
738 (browse-url-of-file file-name))))
739
740 (defun browse-url-delete-temp-file (&optional temp-file-name)
741 ;; Delete browse-url-temp-file-name from the file system
742 ;; If optional arg TEMP-FILE-NAME is non-nil, delete it instead
743 (let ((file-name (or temp-file-name browse-url-temp-file-name)))
744 (if (and file-name (file-exists-p file-name))
745 (delete-file file-name))))
746
747 (add-hook 'kill-buffer-hook 'browse-url-delete-temp-file)
748
749 ;;;###autoload
750 (defun browse-url-of-dired-file ()
751 "In Dired, ask a WWW browser to display the file named on this line."
752 (interactive)
753 (browse-url-of-file (dired-get-filename)))
754
755 ;;;###autoload
756 (defun browse-url-of-region (min max)
757 "Ask a WWW browser to display the current region."
758 (interactive "r")
759 (save-excursion
760 (save-restriction
761 (narrow-to-region min max)
762 (browse-url-of-buffer))))
763
764 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
765 ;; Browser-independent commands
766
767 ;; A generic command to call the current browse-url-browser-function
768
769 ;;;###autoload
770 (defun browse-url (url &rest args)
771 "Ask a WWW browser to load URL.
772 Prompts for a URL, defaulting to the URL at or before point. Variable
773 `browse-url-browser-function' says which browser to use."
774 (interactive (browse-url-interactive-arg "URL: "))
775 (unless (interactive-p)
776 (setq args (or args (list browse-url-new-window-flag))))
777 (let ((process-environment (copy-sequence process-environment)))
778 ;; When connected to various displays, be careful to use the display of
779 ;; the currently selected frame, rather than the original start display,
780 ;; which may not even exist any more.
781 (if (stringp (frame-parameter (selected-frame) 'display))
782 (setenv "DISPLAY" (frame-parameter (selected-frame) 'display)))
783 (if (functionp browse-url-browser-function)
784 (apply browse-url-browser-function url args)
785 ;; The `function' can be an alist; look down it for first match
786 ;; and apply the function (which might be a lambda).
787 (catch 'done
788 (dolist (bf browse-url-browser-function)
789 (when (string-match (car bf) url)
790 (apply (cdr bf) url args)
791 (throw 'done t)))
792 (error "No browse-url-browser-function matching URL %s"
793 url)))))
794
795 ;;;###autoload
796 (defun browse-url-at-point (&optional arg)
797 "Ask a WWW browser to load the URL at or before point.
798 Doesn't let you edit the URL like `browse-url'. Variable
799 `browse-url-browser-function' says which browser to use."
800 (interactive "P")
801 (let ((url (browse-url-url-at-point)))
802 (if url
803 (browse-url url (if arg
804 (not browse-url-new-window-flag)
805 browse-url-new-window-flag))
806 (error "No URL found"))))
807
808 ;;;###autoload
809 (defun browse-url-at-mouse (event)
810 "Ask a WWW browser to load a URL clicked with the mouse.
811 The URL is the one around or before the position of the mouse click
812 but point is not changed. Doesn't let you edit the URL like
813 `browse-url'. Variable `browse-url-browser-function' says which browser
814 to use."
815 (interactive "e")
816 (save-excursion
817 (mouse-set-point event)
818 ;; This handles browse-url-new-window-flag properly
819 ;; when it gets no arg.
820 (browse-url-at-point)))
821
822 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
823 ;; Browser-specific commands
824
825 ;; --- Default MS-Windows browser ---
826
827 (defvar dos-windows-version)
828 (declare-function w32-shell-execute "w32fns.c") ;; Defined in C.
829
830 (defun browse-url-default-windows-browser (url &optional new-window)
831 (interactive (browse-url-interactive-arg "URL: "))
832 (if (eq system-type 'ms-dos)
833 (if dos-windows-version
834 (shell-command (concat "start " (shell-quote-argument url)))
835 (error "Browsing URLs is not supported on this system"))
836 (w32-shell-execute "open" url)))
837
838 (defun browse-url-default-macosx-browser (url &optional new-window)
839 (interactive (browse-url-interactive-arg "URL: "))
840 (start-process (concat "open " url) nil "open" url))
841
842 ;; --- Netscape ---
843
844 (defun browse-url-process-environment ()
845 "Set DISPLAY in the environment to the X display the browser will use.
846 This is either the value of variable `browse-url-browser-display' if
847 non-nil, or the same display as Emacs if different from the current
848 environment, otherwise just use the current environment."
849 (let ((display (or browse-url-browser-display (browse-url-emacs-display))))
850 (if display
851 (cons (concat "DISPLAY=" display) process-environment)
852 process-environment)))
853
854 (defun browse-url-emacs-display ()
855 "Return the X display Emacs is running on.
856 This is nil if the display is the same as the DISPLAY environment variable.
857
858 Actually Emacs could be using several displays; this just returns the
859 one showing the selected frame."
860 (let ((display (cdr-safe (assq 'display (frame-parameters)))))
861 (and (not (equal display (getenv "DISPLAY")))
862 display)))
863
864 ;;;###autoload
865 (defun browse-url-default-browser (url &rest args)
866 "Find a suitable browser and ask it to load URL.
867 Default to the URL around or before point.
868
869 When called interactively, if variable `browse-url-new-window-flag' is
870 non-nil, load the document in a new window, if possible, otherwise use
871 a random existing one. A non-nil interactive prefix argument reverses
872 the effect of `browse-url-new-window-flag'.
873
874 When called non-interactively, optional second argument NEW-WINDOW is
875 used instead of `browse-url-new-window-flag'.
876
877 The order attempted is gnome-moz-remote, Mozilla, Firefox,
878 Galeon, Konqueror, Netscape, Mosaic, Lynx in an xterm, and then W3."
879 (apply
880 (cond
881 ((executable-find browse-url-gnome-moz-program) 'browse-url-gnome-moz)
882 ((executable-find browse-url-mozilla-program) 'browse-url-mozilla)
883 ((executable-find browse-url-firefox-program) 'browse-url-firefox)
884 ((executable-find browse-url-galeon-program) 'browse-url-galeon)
885 ((executable-find browse-url-kde-program) 'browse-url-kde)
886 ((executable-find browse-url-netscape-program) 'browse-url-netscape)
887 ((executable-find browse-url-mosaic-program) 'browse-url-mosaic)
888 ((executable-find browse-url-xterm-program) 'browse-url-text-xterm)
889 ((locate-library "w3") 'browse-url-w3)
890 (t
891 (lambda (&ignore args) (error "No usable browser found"))))
892 url args))
893
894 ;;;###autoload
895 (defun browse-url-netscape (url &optional new-window)
896 "Ask the Netscape WWW browser to load URL.
897 Default to the URL around or before point. The strings in variable
898 `browse-url-netscape-arguments' are also passed to Netscape.
899
900 When called interactively, if variable `browse-url-new-window-flag' is
901 non-nil, load the document in a new Netscape window, otherwise use a
902 random existing one. A non-nil interactive prefix argument reverses
903 the effect of `browse-url-new-window-flag'.
904
905 If `browse-url-netscape-new-window-is-tab' is non-nil, then
906 whenever a document would otherwise be loaded in a new window, it
907 is loaded in a new tab in an existing window instead.
908
909 When called non-interactively, optional second argument NEW-WINDOW is
910 used instead of `browse-url-new-window-flag'."
911 (interactive (browse-url-interactive-arg "URL: "))
912 (setq url (browse-url-encode-url url))
913 (let* ((process-environment (browse-url-process-environment))
914 (process
915 (apply 'start-process
916 (concat "netscape " url) nil
917 browse-url-netscape-program
918 (append
919 browse-url-netscape-arguments
920 (if (eq window-system 'w32)
921 (list url)
922 (append
923 (if new-window '("-noraise"))
924 (list "-remote"
925 (concat "openURL(" url
926 (if (browse-url-maybe-new-window
927 new-window)
928 (if browse-url-netscape-new-window-is-tab
929 ",new-tab"
930 ",new-window"))
931 ")"))))))))
932 (set-process-sentinel process
933 `(lambda (process change)
934 (browse-url-netscape-sentinel process ,url)))))
935
936 (defun browse-url-netscape-sentinel (process url)
937 "Handle a change to the process communicating with Netscape."
938 (or (eq (process-exit-status process) 0)
939 (let* ((process-environment (browse-url-process-environment)))
940 ;; Netscape not running - start it
941 (message "Starting %s..." browse-url-netscape-program)
942 (apply 'start-process (concat "netscape" url) nil
943 browse-url-netscape-program
944 (append browse-url-netscape-startup-arguments (list url))))))
945
946 (defun browse-url-netscape-reload ()
947 "Ask Netscape to reload its current document.
948 How depends on `browse-url-netscape-version'."
949 (interactive)
950 ;; Backwards incompatibility reported by
951 ;; <peter.kruse@psychologie.uni-regensburg.de>.
952 (browse-url-netscape-send (if (>= browse-url-netscape-version 4)
953 "xfeDoCommand(reload)"
954 "reload")))
955
956 (defun browse-url-netscape-send (command)
957 "Send a remote control command to Netscape."
958 (let* ((process-environment (browse-url-process-environment)))
959 (apply 'start-process "netscape" nil
960 browse-url-netscape-program
961 (append browse-url-netscape-arguments
962 (list "-remote" command)))))
963
964 ;;;###autoload
965 (defun browse-url-mozilla (url &optional new-window)
966 "Ask the Mozilla WWW browser to load URL.
967 Default to the URL around or before point. The strings in variable
968 `browse-url-mozilla-arguments' are also passed to Mozilla.
969
970 When called interactively, if variable `browse-url-new-window-flag' is
971 non-nil, load the document in a new Mozilla window, otherwise use a
972 random existing one. A non-nil interactive prefix argument reverses
973 the effect of `browse-url-new-window-flag'.
974
975 If `browse-url-mozilla-new-window-is-tab' is non-nil, then whenever a
976 document would otherwise be loaded in a new window, it is loaded in a
977 new tab in an existing window instead.
978
979 When called non-interactively, optional second argument NEW-WINDOW is
980 used instead of `browse-url-new-window-flag'."
981 (interactive (browse-url-interactive-arg "URL: "))
982 (setq url (browse-url-encode-url url))
983 (let* ((process-environment (browse-url-process-environment))
984 (process
985 (apply 'start-process
986 (concat "mozilla " url) nil
987 browse-url-mozilla-program
988 (append
989 browse-url-mozilla-arguments
990 (list "-remote"
991 (concat "openURL("
992 url
993 (if (browse-url-maybe-new-window
994 new-window)
995 (if browse-url-mozilla-new-window-is-tab
996 ",new-tab"
997 ",new-window"))
998 ")"))))))
999 (set-process-sentinel process
1000 `(lambda (process change)
1001 (browse-url-mozilla-sentinel process ,url)))))
1002
1003 (defun browse-url-mozilla-sentinel (process url)
1004 "Handle a change to the process communicating with Mozilla."
1005 (or (eq (process-exit-status process) 0)
1006 (let* ((process-environment (browse-url-process-environment)))
1007 ;; Mozilla is not running - start it
1008 (message "Starting %s..." browse-url-mozilla-program)
1009 (apply 'start-process (concat "mozilla " url) nil
1010 browse-url-mozilla-program
1011 (append browse-url-mozilla-startup-arguments (list url))))))
1012
1013 ;;;###autoload
1014 (defun browse-url-firefox (url &optional new-window)
1015 "Ask the Firefox WWW browser to load URL.
1016 Default to the URL around or before point. The strings in
1017 variable `browse-url-firefox-arguments' are also passed to
1018 Firefox.
1019
1020 When called interactively, if variable
1021 `browse-url-new-window-flag' is non-nil, load the document in a
1022 new Firefox window, otherwise use a random existing one. A
1023 non-nil interactive prefix argument reverses the effect of
1024 `browse-url-new-window-flag'.
1025
1026 If `browse-url-firefox-new-window-is-tab' is non-nil, then
1027 whenever a document would otherwise be loaded in a new window, it
1028 is loaded in a new tab in an existing window instead.
1029
1030 When called non-interactively, optional second argument
1031 NEW-WINDOW is used instead of `browse-url-new-window-flag'.
1032
1033 On MS-Windows systems the optional `new-window' parameter is
1034 ignored. Firefox for Windows does not support the \"-remote\"
1035 command line parameter. Therefore, the
1036 `browse-url-new-window-flag' and `browse-url-firefox-new-window-is-tab'
1037 are ignored as well. Firefox on Windows will always open the requested
1038 URL in a new window."
1039 (interactive (browse-url-interactive-arg "URL: "))
1040 (setq url (browse-url-encode-url url))
1041 (let* ((process-environment (browse-url-process-environment))
1042 (process
1043 (apply 'start-process
1044 (concat "firefox " url) nil
1045 browse-url-firefox-program
1046 (append
1047 browse-url-firefox-arguments
1048 (if (or (featurep 'dos-w32)
1049 (string-match "win32" system-configuration))
1050 (list url)
1051 (list "-remote"
1052 (concat "openURL("
1053 url
1054 (if (browse-url-maybe-new-window
1055 new-window)
1056 (if browse-url-firefox-new-window-is-tab
1057 ",new-tab"
1058 ",new-window"))
1059 ")")))))))
1060 (set-process-sentinel process
1061 `(lambda (process change)
1062 (browse-url-firefox-sentinel process ,url)))))
1063
1064 (defun browse-url-firefox-sentinel (process url)
1065 "Handle a change to the process communicating with Firefox."
1066 (or (eq (process-exit-status process) 0)
1067 (let* ((process-environment (browse-url-process-environment)))
1068 ;; Firefox is not running - start it
1069 (message "Starting Firefox...")
1070 (apply 'start-process (concat "firefox " url) nil
1071 browse-url-firefox-program
1072 (append browse-url-firefox-startup-arguments (list url))))))
1073
1074 ;;;###autoload
1075 (defun browse-url-galeon (url &optional new-window)
1076 "Ask the Galeon WWW browser to load URL.
1077 Default to the URL around or before point. The strings in variable
1078 `browse-url-galeon-arguments' are also passed to Galeon.
1079
1080 When called interactively, if variable `browse-url-new-window-flag' is
1081 non-nil, load the document in a new Galeon window, otherwise use a
1082 random existing one. A non-nil interactive prefix argument reverses
1083 the effect of `browse-url-new-window-flag'.
1084
1085 If `browse-url-galeon-new-window-is-tab' is non-nil, then whenever a
1086 document would otherwise be loaded in a new window, it is loaded in a
1087 new tab in an existing window instead.
1088
1089 When called non-interactively, optional second argument NEW-WINDOW is
1090 used instead of `browse-url-new-window-flag'."
1091 (interactive (browse-url-interactive-arg "URL: "))
1092 (setq url (browse-url-encode-url url))
1093 (let* ((process-environment (browse-url-process-environment))
1094 (process (apply 'start-process
1095 (concat "galeon " url)
1096 nil
1097 browse-url-galeon-program
1098 (append
1099 browse-url-galeon-arguments
1100 (if (browse-url-maybe-new-window new-window)
1101 (if browse-url-galeon-new-window-is-tab
1102 '("--new-tab")
1103 '("--new-window" "--noraise"))
1104 '("--existing"))
1105 (list url)))))
1106 (set-process-sentinel process
1107 `(lambda (process change)
1108 (browse-url-galeon-sentinel process ,url)))))
1109
1110 (defun browse-url-galeon-sentinel (process url)
1111 "Handle a change to the process communicating with Galeon."
1112 (or (eq (process-exit-status process) 0)
1113 (let* ((process-environment (browse-url-process-environment)))
1114 ;; Galeon is not running - start it
1115 (message "Starting %s..." browse-url-galeon-program)
1116 (apply 'start-process (concat "galeon " url) nil
1117 browse-url-galeon-program
1118 (append browse-url-galeon-startup-arguments (list url))))))
1119
1120 (defun browse-url-epiphany (url &optional new-window)
1121 "Ask the Epiphany WWW browser to load URL.
1122 Default to the URL around or before point. The strings in variable
1123 `browse-url-galeon-arguments' are also passed to Epiphany.
1124
1125 When called interactively, if variable `browse-url-new-window-flag' is
1126 non-nil, load the document in a new Epiphany window, otherwise use a
1127 random existing one. A non-nil interactive prefix argument reverses
1128 the effect of `browse-url-new-window-flag'.
1129
1130 If `browse-url-epiphany-new-window-is-tab' is non-nil, then whenever a
1131 document would otherwise be loaded in a new window, it is loaded in a
1132 new tab in an existing window instead.
1133
1134 When called non-interactively, optional second argument NEW-WINDOW is
1135 used instead of `browse-url-new-window-flag'."
1136 (interactive (browse-url-interactive-arg "URL: "))
1137 (setq url (browse-url-encode-url url))
1138 (let* ((process-environment (browse-url-process-environment))
1139 (process (apply 'start-process
1140 (concat "epiphany " url)
1141 nil
1142 browse-url-epiphany-program
1143 (append
1144 browse-url-epiphany-arguments
1145 (if (browse-url-maybe-new-window new-window)
1146 (if browse-url-epiphany-new-window-is-tab
1147 '("--new-tab")
1148 '("--new-window" "--noraise"))
1149 '("--existing"))
1150 (list url)))))
1151 (set-process-sentinel process
1152 `(lambda (process change)
1153 (browse-url-epiphany-sentinel process ,url)))))
1154
1155 (defun browse-url-epiphany-sentinel (process url)
1156 "Handle a change to the process communicating with Epiphany."
1157 (or (eq (process-exit-status process) 0)
1158 (let* ((process-environment (browse-url-process-environment)))
1159 ;; Epiphany is not running - start it
1160 (message "Starting %s..." browse-url-epiphany-program)
1161 (apply 'start-process (concat "epiphany " url) nil
1162 browse-url-epiphany-program
1163 (append browse-url-epiphany-startup-arguments (list url))))))
1164
1165 (defvar url-handler-regexp)
1166
1167 ;;;###autoload
1168 (defun browse-url-emacs (url &optional new-window)
1169 "Ask Emacs to load URL into a buffer and show it in another window."
1170 (interactive (browse-url-interactive-arg "URL: "))
1171 (require 'url-handlers)
1172 (let ((file-name-handler-alist
1173 (cons (cons url-handler-regexp 'url-file-handler)
1174 file-name-handler-alist)))
1175 ;; Ignore `new-window': with all other browsers the URL is always shown
1176 ;; in another window than the current Emacs one since it's shown in
1177 ;; another application's window.
1178 ;; (if new-window (find-file-other-window url) (find-file url))
1179 (find-file-other-window url)))
1180
1181 ;;;###autoload
1182 (defun browse-url-gnome-moz (url &optional new-window)
1183 "Ask Mozilla/Netscape to load URL via the GNOME program `gnome-moz-remote'.
1184 Default to the URL around or before point. The strings in variable
1185 `browse-url-gnome-moz-arguments' are also passed.
1186
1187 When called interactively, if variable `browse-url-new-window-flag' is
1188 non-nil, load the document in a new browser window, otherwise use an
1189 existing one. A non-nil interactive prefix argument reverses the
1190 effect of `browse-url-new-window-flag'.
1191
1192 When called non-interactively, optional second argument NEW-WINDOW is
1193 used instead of `browse-url-new-window-flag'."
1194 (interactive (browse-url-interactive-arg "URL: "))
1195 (apply 'start-process (concat "gnome-moz-remote " url)
1196 nil
1197 browse-url-gnome-moz-program
1198 (append
1199 browse-url-gnome-moz-arguments
1200 (if (browse-url-maybe-new-window new-window)
1201 '("--newwin"))
1202 (list "--raise" url))))
1203
1204 ;; --- Mosaic ---
1205
1206 ;;;###autoload
1207 (defun browse-url-mosaic (url &optional new-window)
1208 "Ask the XMosaic WWW browser to load URL.
1209
1210 Default to the URL around or before point. The strings in variable
1211 `browse-url-mosaic-arguments' are also passed to Mosaic and the
1212 program is invoked according to the variable
1213 `browse-url-mosaic-program'.
1214
1215 When called interactively, if variable `browse-url-new-window-flag' is
1216 non-nil, load the document in a new Mosaic window, otherwise use a
1217 random existing one. A non-nil interactive prefix argument reverses
1218 the effect of `browse-url-new-window-flag'.
1219
1220 When called non-interactively, optional second argument NEW-WINDOW is
1221 used instead of `browse-url-new-window-flag'."
1222 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1223 (let ((pidfile (expand-file-name browse-url-mosaic-pidfile))
1224 pid)
1225 (if (file-readable-p pidfile)
1226 (save-excursion
1227 (find-file pidfile)
1228 (goto-char (point-min))
1229 (setq pid (read (current-buffer)))
1230 (kill-buffer nil)))
1231 (if (and pid (zerop (signal-process pid 0))) ; Mosaic running
1232 (save-excursion
1233 (find-file (format "/tmp/Mosaic.%d" pid))
1234 (erase-buffer)
1235 (insert (if (browse-url-maybe-new-window new-window)
1236 "newwin\n"
1237 "goto\n")
1238 url "\n")
1239 (save-buffer)
1240 (kill-buffer nil)
1241 ;; Send signal SIGUSR to Mosaic
1242 (message "Signaling Mosaic...")
1243 (signal-process pid 'SIGUSR1)
1244 ;; Or you could try:
1245 ;; (call-process "kill" nil 0 nil "-USR1" (int-to-string pid))
1246 (message "Signaling Mosaic...done")
1247 )
1248 ;; Mosaic not running - start it
1249 (message "Starting %s..." browse-url-mosaic-program)
1250 (apply 'start-process "xmosaic" nil browse-url-mosaic-program
1251 (append browse-url-mosaic-arguments (list url)))
1252 (message "Starting %s...done" browse-url-mosaic-program))))
1253
1254 ;; --- Mosaic using CCI ---
1255
1256 ;;;###autoload
1257 (defun browse-url-cci (url &optional new-window)
1258 "Ask the XMosaic WWW browser to load URL.
1259 Default to the URL around or before point.
1260
1261 This function only works for XMosaic version 2.5 or later. You must
1262 select `CCI' from XMosaic's File menu, set the CCI Port Address to the
1263 value of variable `browse-url-CCI-port', and enable `Accept requests'.
1264
1265 When called interactively, if variable `browse-url-new-window-flag' is
1266 non-nil, load the document in a new browser window, otherwise use a
1267 random existing one. A non-nil interactive prefix argument reverses
1268 the effect of `browse-url-new-window-flag'.
1269
1270 When called non-interactively, optional second argument NEW-WINDOW is
1271 used instead of `browse-url-new-window-flag'."
1272 (interactive (browse-url-interactive-arg "Mosaic URL: "))
1273 (open-network-stream "browse-url" " *browse-url*"
1274 browse-url-CCI-host browse-url-CCI-port)
1275 ;; Todo: start browser if fails
1276 (process-send-string "browse-url"
1277 (concat "get url (" url ") output "
1278 (if (browse-url-maybe-new-window new-window)
1279 "new"
1280 "current")
1281 "\r\n"))
1282 (process-send-string "browse-url" "disconnect\r\n")
1283 (delete-process "browse-url"))
1284
1285 ;; --- W3 ---
1286
1287 ;; External.
1288 (declare-function w3-fetch-other-window "ext:w3m" (&optional url))
1289 (declare-function w3-fetch "ext:w3m" (&optional url target))
1290
1291 ;;;###autoload
1292 (defun browse-url-w3 (url &optional new-window)
1293 "Ask the w3 WWW browser to load URL.
1294 Default to the URL around or before point.
1295
1296 When called interactively, if variable `browse-url-new-window-flag' is
1297 non-nil, load the document in a new window. A non-nil interactive
1298 prefix argument reverses the effect of `browse-url-new-window-flag'.
1299
1300 When called non-interactively, optional second argument NEW-WINDOW is
1301 used instead of `browse-url-new-window-flag'."
1302 (interactive (browse-url-interactive-arg "W3 URL: "))
1303 (require 'w3) ; w3-fetch-other-window not autoloaded
1304 (if (browse-url-maybe-new-window new-window)
1305 (w3-fetch-other-window url)
1306 (w3-fetch url)))
1307
1308 ;;;###autoload
1309 (defun browse-url-w3-gnudoit (url &optional new-window)
1310 ;; new-window ignored
1311 "Ask another Emacs running gnuserv to load the URL using the W3 browser.
1312 The `browse-url-gnudoit-program' program is used with options given by
1313 `browse-url-gnudoit-args'. Default to the URL around or before point."
1314 (interactive (browse-url-interactive-arg "W3 URL: "))
1315 (apply 'start-process (concat "gnudoit:" url) nil
1316 browse-url-gnudoit-program
1317 (append browse-url-gnudoit-args
1318 (list (concat "(w3-fetch \"" url "\")")
1319 "(raise-frame)"))))
1320
1321 ;; --- Lynx in an xterm ---
1322
1323 ;;;###autoload
1324 (defun browse-url-text-xterm (url &optional new-window)
1325 ;; new-window ignored
1326 "Ask a text browser to load URL.
1327 URL defaults to the URL around or before point.
1328 This runs the text browser specified by `browse-url-text-browser'.
1329 in an Xterm window using the Xterm program named by `browse-url-xterm-program'
1330 with possible additional arguments `browse-url-xterm-args'."
1331 (interactive (browse-url-interactive-arg "Text browser URL: "))
1332 (apply #'start-process `(,(concat browse-url-text-browser url)
1333 nil ,browse-url-xterm-program
1334 ,@browse-url-xterm-args "-e" ,browse-url-text-browser
1335 ,url)))
1336
1337 ;; --- Lynx in an Emacs "term" window ---
1338
1339 ;;;###autoload
1340 (defun browse-url-text-emacs (url &optional new-buffer)
1341 "Ask a text browser to load URL.
1342 URL defaults to the URL around or before point.
1343 This runs the text browser specified by `browse-url-text-browser'.
1344 With a prefix argument, it runs a new browser process in a new buffer.
1345
1346 When called interactively, if variable `browse-url-new-window-flag' is
1347 non-nil, load the document in a new browser process in a new term window,
1348 otherwise use any existing one. A non-nil interactive prefix argument
1349 reverses the effect of `browse-url-new-window-flag'.
1350
1351 When called non-interactively, optional second argument NEW-WINDOW is
1352 used instead of `browse-url-new-window-flag'."
1353 (interactive (browse-url-interactive-arg "Text browser URL: "))
1354 (let* ((system-uses-terminfo t) ; Lynx uses terminfo
1355 ;; (term-term-name "vt100") ; ??
1356 (buf (get-buffer "*text browser*"))
1357 (proc (and buf (get-buffer-process buf)))
1358 (n browse-url-text-input-attempts))
1359 (if (and (browse-url-maybe-new-window new-buffer) buf)
1360 ;; Rename away the OLD buffer. This isn't very polite, but
1361 ;; term insists on working in a buffer named *lynx* and would
1362 ;; choke on *lynx*<1>
1363 (progn (set-buffer buf)
1364 (rename-uniquely)))
1365 (if (or (browse-url-maybe-new-window new-buffer)
1366 (not buf)
1367 (not proc)
1368 (not (memq (process-status proc) '(run stop))))
1369 ;; start a new text browser
1370 (progn
1371 (setq buf
1372 (apply #'make-term
1373 `(,browse-url-text-browser
1374 ,browse-url-text-browser
1375 nil ,@browse-url-text-emacs-args
1376 ,url)))
1377 (switch-to-buffer buf)
1378 (term-char-mode)
1379 (set-process-sentinel
1380 (get-buffer-process buf)
1381 ;; Don't leave around a dead one (especially because of its
1382 ;; munged keymap.)
1383 (lambda (process event)
1384 (if (not (memq (process-status process) '(run stop)))
1385 (let ((buf (process-buffer process)))
1386 (if buf (kill-buffer buf)))))))
1387 ;; Send the url to the text browser in the old buffer
1388 (let ((win (get-buffer-window buf t)))
1389 (if win
1390 (select-window win)
1391 (switch-to-buffer buf)))
1392 (if (eq (following-char) ?_)
1393 (cond ((eq browse-url-text-input-field 'warn)
1394 (error "Please move out of the input field first"))
1395 ((eq browse-url-text-input-field 'avoid)
1396 (while (and (eq (following-char) ?_) (> n 0))
1397 (term-send-down) ; down arrow
1398 (sit-for browse-url-text-input-delay))
1399 (if (eq (following-char) ?_)
1400 (error "Cannot move out of the input field, sorry")))))
1401 (term-send-string proc (concat "g" ; goto
1402 "\C-u" ; kill default url
1403 url
1404 "\r")))))
1405
1406 ;; --- mailto ---
1407
1408 (autoload 'rfc2368-parse-mailto-url "rfc2368")
1409
1410 ;;;###autoload
1411 (defun browse-url-mail (url &optional new-window)
1412 "Open a new mail message buffer within Emacs for the RFC 2368 URL.
1413 Default to using the mailto: URL around or before point as the
1414 recipient's address. Supplying a non-nil interactive prefix argument
1415 will cause the mail to be composed in another window rather than the
1416 current one.
1417
1418 When called interactively, if variable `browse-url-new-window-flag' is
1419 non-nil use `compose-mail-other-window', otherwise `compose-mail'. A
1420 non-nil interactive prefix argument reverses the effect of
1421 `browse-url-new-window-flag'.
1422
1423 When called non-interactively, optional second argument NEW-WINDOW is
1424 used instead of `browse-url-new-window-flag'."
1425 (interactive (browse-url-interactive-arg "Mailto URL: "))
1426 (save-excursion
1427 (let* ((alist (rfc2368-parse-mailto-url url))
1428 (to (assoc "To" alist))
1429 (subject (assoc "Subject" alist))
1430 (body (assoc "Body" alist))
1431 (rest (delete to (delete subject (delete body alist))))
1432 (to (cdr to))
1433 (subject (cdr subject))
1434 (body (cdr body))
1435 (mail-citation-hook (unless body mail-citation-hook)))
1436 (if (browse-url-maybe-new-window new-window)
1437 (compose-mail-other-window to subject rest nil
1438 (if body
1439 (list 'insert body)
1440 (list 'insert-buffer (current-buffer))))
1441 (compose-mail to subject rest nil nil
1442 (if body
1443 (list 'insert body)
1444 (list 'insert-buffer (current-buffer))))))))
1445
1446 ;; --- Random browser ---
1447
1448 ;;;###autoload
1449 (defun browse-url-generic (url &optional new-window)
1450 ;; new-window ignored
1451 "Ask the WWW browser defined by `browse-url-generic-program' to load URL.
1452 Default to the URL around or before point. A fresh copy of the
1453 browser is started up in a new process with possible additional arguments
1454 `browse-url-generic-args'. This is appropriate for browsers which
1455 don't offer a form of remote control."
1456 (interactive (browse-url-interactive-arg "URL: "))
1457 (if (not browse-url-generic-program)
1458 (error "No browser defined (`browse-url-generic-program')"))
1459 (apply 'call-process browse-url-generic-program nil
1460 0 nil
1461 (append browse-url-generic-args (list url))))
1462
1463 ;;;###autoload
1464 (defun browse-url-kde (url &optional new-window)
1465 "Ask the KDE WWW browser to load URL.
1466 Default to the URL around or before point."
1467 (interactive (browse-url-interactive-arg "KDE URL: "))
1468 (message "Sending URL to KDE...")
1469 (apply #'start-process (concat "KDE " url) nil browse-url-kde-program
1470 (append browse-url-kde-args (list url))))
1471
1472 (defun browse-url-elinks-new-window (url)
1473 "Ask the Elinks WWW browser to load URL in a new window."
1474 (let ((process-environment (browse-url-process-environment)))
1475 (apply #'start-process
1476 (append (list (concat "elinks:" url)
1477 nil)
1478 browse-url-elinks-wrapper
1479 (list "elinks" url)))))
1480
1481 ;;;###autoload
1482 (defun browse-url-elinks (url &optional new-window)
1483 "Ask the Elinks WWW browser to load URL.
1484 Default to the URL around the point.
1485
1486 The document is loaded in a new tab of a running Elinks or, if
1487 none yet running, a newly started instance.
1488
1489 The Elinks command will be prepended by the program+arguments
1490 from `browse-url-elinks-wrapper'."
1491 (interactive (browse-url-interactive-arg "URL: "))
1492 (setq url (browse-url-encode-url url))
1493 (if new-window
1494 (browse-url-elinks-new-window url)
1495 (let ((process-environment (browse-url-process-environment))
1496 (elinks-ping-process (start-process "elinks-ping" nil
1497 "elinks" "-remote" "ping()")))
1498 (set-process-sentinel elinks-ping-process
1499 `(lambda (process change)
1500 (browse-url-elinks-sentinel process ,url))))))
1501
1502 (defun browse-url-elinks-sentinel (process url)
1503 "Determines if Elinks is running or a new one has to be started."
1504 (let ((exit-status (process-exit-status process)))
1505 ;; Try to determine if an instance is running or if we have to
1506 ;; create a new one.
1507 (case exit-status
1508 (5
1509 ;; No instance, start a new one.
1510 (browse-url-elinks-new-window url))
1511 (0
1512 ;; Found an instance, open URL in new tab.
1513 (let ((process-environment (browse-url-process-environment)))
1514 (start-process (concat "elinks:" url) nil
1515 "elinks" "-remote"
1516 (concat "openURL(\"" url "\",new-tab)"))))
1517 (otherwise
1518 (error "Unrecognized exit-code %d of process `elinks'"
1519 exit-status)))))
1520
1521 (provide 'browse-url)
1522
1523 ;; arch-tag: d2079573-5c06-4097-9598-f550fba19430
1524 ;;; browse-url.el ends here