New stuff in setup: bindings.el, flyspell.el, mywin.el
[emacs-init.git] / auto-install.el
1 ;;; auto-install.el --- Auto install elisp file
2 ;; $Id: auto-install.el,v 1.53 2011/04/12 06:28:20 rubikitch Exp $
3
4 ;; Filename: auto-install.el
5 ;; Description: Auto install elisp file
6 ;; Author: Andy Stewart <lazycat.manatee@gmail.com>
7 ;;         rubikitch <rubikitch@ruby-lang.org>
8 ;; Maintainer: rubikitch <rubikitch@ruby-lang.org>
9 ;; Copyright (C) 2008, 2009, Andy Stewart, all rights reserved.
10 ;; Copyright (C) 2009, rubikitch, all rights reserved.
11 ;; Created: 2008-12-11 13:56:50
12 ;; Version: $Revision: 1.53 $
13 ;; URL: http://www.emacswiki.org/emacs/download/auto-install.el
14 ;; Keywords: auto-install
15 ;; Compatibility: GNU Emacs 22 ~ 23
16 ;;
17 ;; Features that might be required by this library:
18 ;;
19 ;;   `backquote', `bytecomp', `dired', `find-func', `ietf-drums',
20 ;;   `loadhist', `mail-parse', `mail-prsvr', `mailcap', `mm-util',
21 ;;   `qp', `rfc2045', `rfc2047', `rfc2231', `thingatpt', `time-date',
22 ;;   `timezone', `url', `url-cookie', `url-expand', `url-history',
23 ;;   `url-methods', `url-parse', `url-privacy', `url-proxy',
24 ;;   `url-util', `url-vars'.
25 ;;
26
27 (defvar auto-install-version "$Id: auto-install.el,v 1.53 2011/04/12 06:28:20 rubikitch Exp $")
28 ;;; This file is NOT part of GNU Emacs
29
30 ;;; License
31 ;;
32 ;; This program is free software; you can redistribute it and/or modify
33 ;; it under the terms of the GNU General Public License as published by
34 ;; the Free Software Foundation; either version 3, or (at your option)
35 ;; any later version.
36
37 ;; This program is distributed in the hope that it will be useful,
38 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
39 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40 ;; GNU General Public License for more details.
41
42 ;; You should have received a copy of the GNU General Public License
43 ;; along with this program; see the file COPYING.  If not, write to
44 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
45 ;; Floor, Boston, MA 02110-1301, USA.
46
47 ;;; Commentary:
48 ;;
49 ;; Automates the installation of Emacs Lisp files and packages.
50 ;;
51 ;; `auto-install' provides an automated way to:
52 ;;
53 ;; (1) Download Emacs Lisp files and packages from common sources
54 ;; (2) View them (diff) and save them to your repository
55 ;; (3) Compile and Load them
56 ;;
57
58 ;;; Commands:
59 ;;
60 ;; Below are complete command list:
61 ;;
62 ;;  `auto-install-minor-mode'
63 ;;    Auto Install minor mode.
64 ;;  `auto-install-from-buffer'
65 ;;    Install the elisp file in the current buffer.
66 ;;  `auto-install-from-url'
67 ;;    Install an elisp file from a given url.
68 ;;  `auto-install-from-emacswiki'
69 ;;    Install an elisp file from EmacsWiki.org.
70 ;;  `auto-install-from-gist'
71 ;;    Install an elisp file from gist.github.com.
72 ;;  `auto-install-from-library'
73 ;;    Update an elisp LIBRARY.
74 ;;  `auto-install-from-directory'
75 ;;    Update elisp files under DIRECTORY from EmacsWiki.
76 ;;  `auto-install-from-dired'
77 ;;    Update dired marked elisp files from EmacsWiki.org.
78 ;;  `auto-install-update-emacswiki-package-name'
79 ;;    Update the list of elisp package names from `EmacsWiki'.
80 ;;  `auto-install-dired-mark-files'
81 ;;    Mark dired files that contain at `EmacsWiki.org'.
82 ;;  `auto-install-mode'
83 ;;    Major mode for auto-installing elisp code.
84 ;;  `auto-install-buffer-quit'
85 ;;    Quit from `auto-install' temporary buffer.
86 ;;  `auto-install-compatibility-setup'
87 ;;    Install Compatibility commands for install-elisp.el users.
88 ;;  `auto-install-batch'
89 ;;    Batch install many files (libraries and non-elisp files) in some extension.
90 ;;  `auto-install-batch-edit'
91 ;;    Edit auto-install-batch-list.el
92 ;;  `auto-install-buffer-diff'
93 ;;    View different between old version.
94 ;;  `auto-install-buffer-save'
95 ;;    Save downloaded content to file FILENAME.
96 ;;
97 ;;; Customizable Options:
98 ;;
99 ;; Below are customizable option list:
100 ;;
101 ;;  `auto-install-directory'
102 ;;    The directory for saving elisp files.
103 ;;    default = "~/.emacs.d/auto-install/"
104 ;;  `auto-install-buffer-name'
105 ;;    The temporary buffer for storing download content.
106 ;;    default = "auto-install"
107 ;;  `auto-install-emacswiki-base-url'
108 ;;    The base emacswiki.org url from which to download elisp files.
109 ;;    default = "http://www.emacswiki.org/cgi-bin/wiki/download/"
110 ;;  `auto-install-gist-base-url'
111 ;;    The base gist.github.com url from which to download elisp files.
112 ;;    default = "http://gist.github.com/"
113 ;;  `auto-install-filter-url'
114 ;;    Alist mapping filter url for library.
115 ;;    default = (quote (("color-grep" "http://www.bookshelf.jp/elc/")))
116 ;;  `auto-install-save-confirm'
117 ;;    Whether confirmation is needed to save downloaded content.
118 ;;    default = t
119 ;;  `auto-install-replace-confirm'
120 ;;    Whether confirmation is needed to replace an existing elisp file.
121 ;;    default = nil
122 ;;  `auto-install-install-confirm'
123 ;;    Whether confirmation is needed to install a downloaded elisp file.
124 ;;    default = nil
125 ;;  `auto-install-from-dired-confirm'
126 ;;    Whether confirmation is needed to download marked files from Dired.
127 ;;    default = t
128 ;;  `auto-install-wget-command'
129 ;;    *Wget command. Use only if `auto-install-use-wget' is non-nil.
130 ;;    default = "wget"
131 ;;  `auto-install-use-wget'
132 ;;    *Use wget instead of `url-retrieve'.
133 ;;    default = t
134 ;;  `auto-install-batch-list'
135 ;;    This list contain packages information for batch install.
136 ;;    default = nil
137
138 ;;; Tips:
139 ;;
140 ;;      Downloading is asynchronous: you can do your work and download
141 ;;      files at the same time.  The download process won't hang
142 ;;      Emacs.
143 ;;
144 ;;      `auto-install-from-url' remembers previous installations.  So if
145 ;;      your search is the same as the previous search, you don't need
146 ;;      to type it in, just hit RETURN.
147 ;;
148 ;;      `auto-install-from-emacswiki' will complete then names of
149 ;;      packages from those in the Elisp area in `EmacsWiki'.
150 ;;
151 ;;      `auto-install-from-library' will prompt you library name in
152 ;;      you load-path, then it try to download from EmacsWiki if it
153 ;;      can't find match in `auto-install-filter-url'.
154 ;;
155 ;;      `auto-install-from-directory' can install elisp file
156 ;;      under specify directory.
157 ;;
158 ;;      `auto-install-from-dired' can install marked files using dired.
159 ;;      You can mark the files you want in dired and then use
160 ;;      `auto-install-from-dired' to download those files
161 ;;      asynchronously.
162 ;;
163 ;;      `auto-install-from-buffer' can save and install the contents of
164 ;;      the current buffer as a file.  You need a valid elisp file name.
165 ;;      The default name is the buffer name.
166 ;;
167 ;;      `auto-install-from-emacswiki' and `auto-install-from-library'
168 ;;      will try to pick up file around point, you can move
169 ;;      cursor to file name, and just hit RET for install.
170 ;;
171 ;;      Some extension (such as icicles) have many libraries to need install,
172 ;;      and install one by one is painful, you can use command
173 ;;      `auto-install-batch' install all icicles libraries.
174 ;;      And `auto-install-batch' handle max connect limit with some website
175 ;;      (such as EmacsWiki) to avoid download failed.
176 ;;
177 ;;      All of the above functions support a filename filter.  You can
178 ;;      input any url to download an elisp file, if the file name suffix is
179 ;;      `.el', it will download and install the file automatically.
180 ;;      Otherwise, it won't install it unless you input a valid elisp
181 ;;      file name.
182 ;;
183 ;;      By default, if a file that you download does not exist on your
184 ;;      system the file is downloaded to `auto-install-directory'.  If
185 ;;      you already have a file with the same name in your load
186 ;;      directory, `auto-install' will try to replace that file.
187 ;;
188 ;;      You can use command `auto-install-dired-mark-files' to mark files
189 ;;      that contain at `EmacsWiki.org' for fast update.
190 ;;
191 ;;      By default, command `auto-install-from-emacswiki' will initialization
192 ;;      current symbol as default value, if default value is you want,
193 ;;      just hit RET, so lazy!
194 ;;
195
196 ;;; Installation:
197 ;;
198 ;; (1) Put auto-install.el somewhere in your load-path.
199 ;;
200 ;;     For example, put it into  ~/elisp/.
201 ;;     Then add the following to your ~/.emacs:
202 ;;
203 ;;       (add-to-list 'load-path (expand-file-name "~/elisp"))
204 ;;
205 ;; (2) And put the following in your ~/.emacs startup file:
206 ;;
207 ;;       (require 'auto-install)
208 ;;
209 ;; (3) Add this to your ~/.emacs to optionally specify a download directory:
210 ;;
211 ;;       (setq auto-install-directory "~/.emacs.d/auto-install/")
212 ;;
213 ;;     If you don't set this, "~/.emacs.d/auto-install/" will be used as the default,
214 ;;     and will be created as needed.
215 ;;
216 ;; (4) Optionally, if your computer is always connected Internet when Emacs start up,
217 ;;     I recommend you add below to your ~/.emacs, to update package name when start up:
218 ;;
219 ;;       (auto-install-update-emacswiki-package-name t)
220 ;;
221 ;;     And above setup is not necessary, because AutoInstall will automatically update
222 ;;     package name when you just first call `auto-install-from-emacswiki',
223 ;;     above setup just avoid *delay* when you first call `auto-install-from-emacswiki'.
224 ;;
225 ;; (5) I recommend you add below to your ~/.emacs for install-elisp users:
226 ;;
227 ;;       (auto-install-compatibility-setup)
228 ;;
229 ;;     This command `defalias'es `install-elisp',
230 ;;     `install-elisp-from-emacswiki' and `install-elisp-from-gist' to
231 ;;     `auto-install' ones.
232 ;;
233 ;; (6) If you want to use proxy server, set `url-proxy-services'. For example:
234 ;;
235 ;;       (setq url-proxy-services '(("http" . "localhost:8339")))
236
237 ;;; Customize:
238 ;;
239 ;; `auto-install-directory'
240 ;; The default directory for keeping auto-downloaded elisp files.
241 ;;
242 ;; `auto-install-buffer-name'
243 ;; The base buffer name for temporarily storing downloaded download content.
244 ;;
245 ;; `auto-install-emacswiki-base-url'
246 ;; The base url for downloading from EmacsWiki.org.
247 ;;
248 ;; `auto-install-gist-base-url'
249 ;; The base url for downloading from gist.github.com
250 ;;
251 ;; `auto-install-filter-url'
252 ;; Filter url for downloading a special library.
253 ;;
254 ;; `auto-install-save-confirm'
255 ;; Whether to require confirmation when saving downloaded content.
256 ;;
257 ;; `auto-install-replace-confirm'
258 ;; Whether to require confirmation when replacing an already-installed
259 ;; file.
260 ;;
261 ;; `auto-install-install-confirm'
262 ;; Whether to require confirmation when installing a file.
263 ;;
264 ;; `auto-install-from-dired-confirm'
265 ;; Whether to require confirmation when downloading files marked in dired.
266 ;;
267 ;; `auto-install-batch-list'
268 ;; This list contain packages information for batch install.
269 ;; Anyone can add packages information in this list for batch install.
270 ;;
271 ;; And all above option can customize easy through:
272 ;;      M-x RET customize-group RET auto-install RET
273 ;;
274
275
276 ;;; Bug Report:
277 ;;
278 ;; If you have problem, send a bug report via M-x auto-install-send-bug-report.
279 ;; The step is:
280 ;;  0) Setup mail in Emacs, the easiest way is:
281 ;;       (setq user-mail-address "your@mail.address")
282 ;;       (setq user-full-name "Your Full Name")
283 ;;       (setq smtpmail-smtp-server "your.smtp.server.jp")
284 ;;       (setq mail-user-agent 'message-user-agent)
285 ;;       (setq message-send-mail-function 'message-smtpmail-send-it)
286 ;;  1) Be sure to use the LATEST version of auto-install.el.
287 ;;  2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
288 ;;  3) Use Lisp version instead of compiled one: (load "auto-install.el")
289 ;;  4) Do it!
290 ;;  5) If you got an error, please do not close *Backtrace* buffer.
291 ;;  6) M-x auto-install-send-bug-report and M-x insert-buffer *Backtrace*
292 ;;  7) Describe the bug using a precise recipe.
293 ;;  8) Type C-c C-c to send.
294 ;;  # If you are a Japanese, please write in Japanese:-)
295
296 ;;; Change log:
297 ;;
298 ;; $Log: auto-install.el,v $
299 ;; Revision 1.53  2011/04/12 06:28:20  rubikitch
300 ;; fix for proxy
301 ;;
302 ;; Revision 1.52  2011/01/29 11:11:47  rubikitch
303 ;; bugfix: auto-install-buffer-save cannot treat auto-install-directory properly if it doesn't end with `/'
304 ;;
305 ;; patched by MaskRay thanks!
306 ;;
307 ;; Revision 1.51  2010/12/10 10:30:58  rubikitch
308 ;; Bugfix when wget is not installed
309 ;;
310 ;; replace auto-install-use-wget with (auto-install-use-wget-p)
311 ;;
312 ;; Revision 1.50  2010/11/29 15:52:57  rubikitch
313 ;; compatibility code for emacs21.1
314 ;;
315 ;; Revision 1.49  2010/11/10 13:32:37  rubikitch
316 ;; Use `wget -q -O- --no-check-certificate' if wget is available.
317 ;; Change default value: `auto-install-use-wget' = t
318 ;;
319 ;; Revision 1.48  2010/05/20 23:29:10  rubikitch
320 ;; `auto-install-update-emacswiki-package-name': Check whether network is reachable
321 ;;
322 ;; Revision 1.47  2010/05/14 00:09:08  rubikitch
323 ;; Fixed a bug of `auto-install-batch' with argument.
324 ;;
325 ;; Pass extension-name argument to `auto-install-batch-real'.
326 ;;
327 ;; Revision 1.46  2010/05/04 08:46:21  rubikitch
328 ;; Added bug report command
329 ;;
330 ;; Revision 1.45  2010/05/01 01:24:25  rubikitch
331 ;; auto-install-batch: Dependency support
332 ;;
333 ;; Revision 1.44  2010/05/01 00:53:48  rubikitch
334 ;; auto-install-batch-real: refactoring
335 ;;
336 ;; Revision 1.43  2010/04/30 23:41:48  rubikitch
337 ;; Changed default value of `auto-install-use-wget' to nil.
338 ;;
339 ;; Revision 1.42  2010/04/25 02:30:27  rubikitch
340 ;; Avoid `auto-async-byte-compile'
341 ;;
342 ;; Revision 1.41  2010/04/25 01:59:20  rubikitch
343 ;; *** empty log message ***
344 ;;
345 ;; Revision 1.40  2010/04/25 01:58:17  rubikitch
346 ;; Do not save/compile up-to-date files from `auto-install-batch'.
347 ;;
348 ;; Revision 1.39  2010/04/25 01:11:33  rubikitch
349 ;; If downloaded file is not updated, kill download buffer.
350 ;; (auto-install-batch support is not yet)
351 ;;
352 ;; Revision 1.38  2010/04/25 00:10:49  rubikitch
353 ;; code cleanup: remove unneeded `format'
354 ;;
355 ;; Revision 1.37  2010/04/24 23:59:11  rubikitch
356 ;; comment change (no code change)
357 ;;
358 ;; Revision 1.36  2010/04/19 07:10:36  rubikitch
359 ;; Avoid updating time-stamp
360 ;;
361 ;; Revision 1.35  2010/04/07 20:16:10  rubikitch
362 ;; Fixed a typo
363 ;;
364 ;; Revision 1.34  2010/04/05 21:34:07  rubikitch
365 ;; `auto-install-from-gist' can accept gist URL.
366 ;;
367 ;; Revision 1.33  2010/04/05 21:27:30  rubikitch
368 ;; `auto-install-use-wget' is enabled by default when wget command (`auto-install-wget-command') is found.
369 ;;
370 ;; Revision 1.32  2010/04/05 21:24:30  rubikitch
371 ;; * `auto-install-batch' can install non-elisp files because some elisp requires external scripts.
372 ;; * New option: `auto-install-add-exec-path-flag'
373 ;;
374 ;; Revision 1.31  2010/04/01 03:43:17  rubikitch
375 ;; added RCS Id: tag
376 ;;
377 ;; Revision 1.30  2010/04/01 03:42:34  rubikitch
378 ;; document typo
379 ;;
380 ;; Revision 1.29  2010/04/01 03:28:39  rubikitch
381 ;; New command: `auto-install-batch-edit'
382 ;;
383 ;; Revision 1.28  2010/04/01 03:10:38  rubikitch
384 ;; The real value of `auto-install-batch-list' is moved to
385 ;; auto-install-batch-list.el to split program and data.
386 ;;
387 ;; Revision 1.27  2010/03/29 07:36:39  rubikitch
388 ;; Stupid bug fix in auto-install-use-wget
389 ;;
390 ;; Revision 1.26  2010/03/29 02:38:46  rubikitch
391 ;; New option: `auto-install-use-wget', `auto-install-wget-command'
392 ;;
393 ;; Revision 1.25  2010/03/26 00:07:27  rubikitch
394 ;; `url-http-end-of-headers' workaround
395 ;;
396 ;; Revision 1.24  2010/01/05 09:40:04  rubikitch
397 ;; fixed error of auto-complete development version in `auto-install-batch-list'
398 ;;
399 ;; Revision 1.23  2009/12/29 09:31:23  rubikitch
400 ;; add Text Translator to auto-install-batch-list
401 ;;
402 ;; Revision 1.22  2009/12/21 12:51:56  rubikitch
403 ;; Update auto-install-batch anything
404 ;;
405 ;; Revision 1.21  2009/12/21 12:26:54  rubikitch
406 ;; New URL for auto-complete development version
407 ;;
408 ;; Revision 1.20  2009/05/22 20:17:24  rubikitch
409 ;; Merged from dradams' change
410 ;;
411 ;; Revision 1.19  2009/05/22 13:04:56  dadams
412 ;; Split icicles-cmd.el into icicles-cmd[12].el.
413 ;;
414 ;; Revision 1.18  2009/05/20 15:42:54  rubikitch
415 ;; Add php-completion / perl-completion to auto-install-batch-list
416 ;;
417 ;; Revision 1.17  2009/05/20 01:19:15  rubikitch
418 ;; Add document for proxy server
419 ;;
420 ;; Revision 1.16  2009/05/15 20:28:18  rubikitch
421 ;; More readable temporary buffer name.
422 ;;
423 ;; Revision 1.15  2009/05/15 20:12:49  rubikitch
424 ;; Added missing require
425 ;;
426 ;; Revision 1.14  2009/05/15 20:11:44  rubikitch
427 ;; How to save
428 ;;
429 ;; Revision 1.13  2009/05/15 20:09:07  rubikitch
430 ;; Code cleanup
431 ;;
432 ;; Revision 1.12  2009/05/15 19:59:30  rubikitch
433 ;; Fixed a bug of single file installation
434 ;;
435 ;; Revision 1.11  2009/05/15 19:44:32  rubikitch
436 ;; Ordering `auto-install-batch'
437 ;;
438 ;; Revision 1.10  2009/05/15 17:48:09  rubikitch
439 ;; Replace `message' with `error' for error messages.
440 ;;
441 ;; Revision 1.9  2009/05/15 17:40:37  rubikitch
442 ;; refactoring
443 ;;
444 ;; Revision 1.8  2009/05/15 17:19:47  rubikitch
445 ;; refactoring
446 ;;
447 ;; Revision 1.7  2009/05/15 17:17:03  rubikitch
448 ;; Use `view-mode' if `view-read-only'.
449 ;;
450 ;; Revision 1.6  2009/05/15 17:10:22  rubikitch
451 ;; Adjust docstrings of commands to auto-document.
452 ;; Delete `It provides the following commands:' section because of duplication.
453 ;;
454 ;; Revision 1.5  2009/05/15 17:03:13  rubikitch
455 ;; Show downloaded URL in header-line.
456 ;;
457 ;; Revision 1.4  2009/05/15 16:59:32  rubikitch
458 ;; New internal variable: `auto-install-add-load-path-flag'
459 ;;
460 ;; Revision 1.3  2009/05/09 02:41:32  rubikitch
461 ;; Add `auto-install-directory' automatically.
462 ;;
463 ;; Revision 1.2  2009/05/09 02:37:14  rubikitch
464 ;; Changed `auto-install-get-buffer' format (including URL)
465 ;;
466 ;; Revision 1.1  2009/05/09 02:33:09  rubikitch
467 ;; Initial revision
468 ;;
469 ;; 2009/05/01
470 ;;  * Andy Stewart:
471 ;;      * Take over by rubikitch.
472 ;;
473 ;; 2009/04/15
474 ;;  * rubikitch:
475 ;;      * Encoding detection support.
476 ;;
477 ;; 2009/04/07
478 ;;  * Andy Stewart:
479 ;;      * Fix bug of `auto-install-batch'.
480 ;;      * Add more sources to `auto-install-batch-list'.
481 ;;
482 ;; 2009/03/30
483 ;;  * Andy Stewart:
484 ;;      * Add new command: `auto-install-batch'.
485 ;;      * Add new option: `auto-install-batch-list'.
486 ;;
487 ;; 2009/03/29
488 ;;  * Andy Stewart:
489 ;;      * Add new function: `auto-install-from-url-list'.
490 ;;
491 ;; 2009/03/11
492 ;;  * Andy Stewart:
493 ;;      * Fix bug of `auto-install-download'.
494 ;;
495 ;; 2009/03/03
496 ;;  * rubikitch
497 ;;      * Add new command `auto-install-compatibility-setup'
498 ;;        for install-elisp users.
499 ;;  * Andy Stewart:
500 ;;      * `auto-install-region-or-thing' return region string
501 ;;        just when `transient-mark-mode' is on.
502 ;;      * Fix doc.
503 ;;
504 ;; 2009/02/17
505 ;;  * Andy Stewart:
506 ;;      * Modified keybindings, make it more easy to remember.
507 ;;      * Make `auto-install-save-confirm' default with `t'
508 ;;        for security problem.
509 ;;      * Pick up current symbol when use `auto-install-from-library'.
510 ;;      * Remove unnecessary completion name from `auto-install-from-library'.
511 ;;      * Refactory code.
512 ;;      * Fix doc.
513 ;;
514 ;; 2009/02/12
515 ;;  * Andy Stewart:
516 ;;      * Remove option `auto-install-update-emacswiki-package-name-when-startup'.
517 ;;      * Make current symbol as initialization of `auto-install-from-emacswiki'.
518 ;;      * Add option `unforced' to function `auto-install-update-emacswiki-package-name'.
519 ;;      * Fix doc.
520 ;;      * Fix bug of `auto-install-from-library'.
521 ;;
522 ;; 2009/02/10
523 ;;  * Andy Stewart:
524 ;;      * Automatically download package name list when
525 ;;        variable `auto-install-package-name-list' is nil.
526 ;;      * Reverse `auto-install-package-name-list' for `anything' interface.
527 ;;      * New command `auto-install-dired-mark-files',
528 ;;        mark files that contain at `EmacsWiki.org'.
529 ;;      * New command `auto-install-buffer-diff',
530 ;;        view different between current version and old version.
531 ;;
532 ;; 2009/02/06
533 ;;  * Andy Stewart:
534 ;;      * Add new command `auto-install-from-directory'.
535 ;;      * Remove option `auto-install-create-directory', not necessary.
536 ;;      * Documentation improvements (thanks Scot Becker)
537 ;;
538 ;; 2009/02/01
539 ;;  * Andy Stewart:
540 ;;      * Make command `auto-install-from-emacswiki' can
541 ;;        completing package name for input.
542 ;;      * Add new command `auto-install-update-emacswiki-package-name'.
543 ;;      * Add new option `auto-install-update-emacswiki-package-name-when-startup'
544 ;;
545 ;; 2009/01/30
546 ;;  * Andy Stewart:
547 ;;      * Compatibility with GNU Emacs 22.
548 ;;
549 ;; 2009/01/26
550 ;;  * Andy Stewart:
551 ;;      * Add new command `auto-install-from-gist'.
552 ;;
553 ;; 2009/01/21
554 ;;  * Andy Stewart:
555 ;;      * Add emacs-lisp syntax highlight for download buffer.
556 ;;      * Make notify message display at mode-line instead echo-area.
557 ;;
558 ;; 2009/01/10
559 ;;  * Andy Stewart:
560 ;;      * Add new option `auto-install-filter-url' and new function
561 ;;        `auto-install-from-library', try to use it. ;)
562 ;;
563 ;; 2009/01/08
564 ;;  * Andy Stewart:
565 ;;      * Fix coding bug.
566 ;;
567 ;; 2009/01/07
568 ;;  * Andy Stewart:
569 ;;      * Move `w3m' code to file `auto-install-extension.el' to make all
570 ;;        user can use this package with standard emacs.
571 ;;
572 ;; 2009/01/06
573 ;;  * Andy Stewart:
574 ;;      * Clean code.
575 ;;
576 ;; 2009/01/02
577 ;;  * Andy Stewart:
578 ;;      * Add new option `auto-install-create-directory' for create install directory
579 ;;        automatically if it doesn't exist.
580 ;;      * Improve many document make it more clear.
581 ;;      * Thanks document improve and create directory advice of 'Drew Adams'!
582 ;;
583 ;; 2008/12/24
584 ;;  * Andy Stewart:
585 ;;      * Remove `auto-install-window-configuration-before-download', `auto-install-init-window-layout'
586 ;;        and `auto-install-revert-window-layout'.
587 ;;        It's not necessary to revert window layout, `winner-mode' can revert window layout more better,
588 ;;        just type `winner-undo'.
589 ;;
590 ;; 2008/12/15
591 ;;  * Andy Stewart:
592 ;;      * Fix a little bug of `auto-install-window-configuration-before-download'.
593 ;;
594 ;; 2008/12/11
595 ;;  * Andy Stewart:
596 ;;      * Add new function `auto-install-from-buffer', to install elisp file from current buffer.
597 ;;        Modified `auto-install-buffer-save' to use `auto-install-from-buffer'.
598 ;;
599 ;;      * First released.
600 ;;
601
602 ;;; Acknowledgements:
603 ;;
604 ;;      rubikitch       <rubikitch@ruby-lang.org>
605 ;;              For install-elisp.el
606 ;;      Drew Adams      <drew.adams@oracle.com>
607 ;;      Scot Becker     <scot.becker@gmail.com>
608 ;;      Richard Riley   <rileyrgdev@gmail.com>
609 ;;              For documentation improvements and advices.
610 ;;
611
612 ;;; TODO
613 ;;
614 ;;      Fix the problem parallel install process with recursive prompt.
615 ;;      Redesign and give more friendly user interface.
616 ;;      Scan RSS track package update and notify.
617 ;;
618
619 ;;; Require
620 (require 'url)
621 (require 'dired)
622 (require 'find-func)
623 (require 'bytecomp)
624 (require 'thingatpt)
625 (require 'ffap)
626 (eval-when-compile (require 'cl))
627 (when (<= emacs-major-version 22)       ;Compatibility with 22.
628   (autoload 'ignore-errors "cl-macs")
629   (unless (fboundp 'url-file-nondirectory)
630     (defun url-file-nondirectory (file)
631       "Return the nondirectory part of FILE, for a URL."
632       (cond
633        ((null file) "")
634        ((string-match (eval-when-compile (regexp-quote "?")) file)
635         (file-name-nondirectory (substring file 0 (match-beginning 0))))
636        (t (file-name-nondirectory file))))))
637
638 ;;; Code:
639
640 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Customize ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
641 (defgroup auto-install nil
642   "Auto install elisp files."
643   :group 'external)
644
645 (defcustom auto-install-directory "~/.emacs.d/auto-install/"
646   "The directory for saving elisp files.
647 This directory is used when a downloaded
648 elisp file does not already exist in other directory.
649 Otherwise, the existing file of the same name is replaced."
650   :type 'string
651   :group 'auto-install)
652
653 (defcustom auto-install-buffer-name "auto-install"
654   "The temporary buffer for storing download content."
655   :type 'string
656   :group 'auto-install)
657
658 (defcustom auto-install-emacswiki-base-url "http://www.emacswiki.org/cgi-bin/wiki/download/"
659   "The base emacswiki.org url from which to download elisp files."
660   :type 'string
661   :group 'auto-install)
662
663 (defcustom auto-install-gist-base-url "http://gist.github.com/"
664   "The base gist.github.com url from which to download elisp files."
665   :type 'string
666   :group 'auto-install)
667
668 (defcustom auto-install-filter-url
669   '(("color-grep" "http://www.bookshelf.jp/elc/"))
670   "Alist mapping filter url for library.
671 Default command `auto-install-from-library' will install from EmacsWiki,
672 if it can't find match in this alist."
673   :type '(repeat (list (string :tag "Library")
674                        (string :tag "Download URL")))
675   :group 'auto-install)
676
677 (defcustom auto-install-save-confirm t
678   "Whether confirmation is needed to save downloaded content.
679 Nil means no confirmation is needed.
680 If non-nil, the downloaded content is shown in a buffer and you are
681 prompted to confirm saving it to a file."
682   :type 'boolean
683   :group 'auto-install)
684
685 (defcustom auto-install-replace-confirm nil
686   "Whether confirmation is needed to replace an existing elisp file.
687 Nil means no confirmation is needed."
688   :type 'boolean
689   :group 'auto-install)
690
691 (defcustom auto-install-install-confirm nil
692   "Whether confirmation is needed to install a downloaded elisp file.
693 Nil means no confirmation is needed."
694   :type 'boolean
695   :group 'auto-install)
696
697 (defcustom auto-install-from-dired-confirm t
698   "Whether confirmation is needed to download marked files from Dired.
699 Nil means no confirmation is needed."
700   :type 'boolean
701   :group 'auto-install)
702
703 (defcustom auto-install-wget-command "wget"
704   "*Wget command. Use only if `auto-install-use-wget' is non-nil."
705   :type 'string  
706   :group 'auto-install)
707
708 (defcustom auto-install-use-wget t
709   "*Use wget instead of `url-retrieve'.
710
711 It is enabled by default when wget is found."
712   :type 'boolean  
713   :group 'auto-install)
714
715 (defcustom auto-install-batch-list
716   nil
717   "This list contain packages information for batch install.
718
719 Have four arguments per list:
720 First argument is extension name.
721 Second argument is delay time for batch install.
722 Third argument is libraries number limit in delay time.
723 Fourth argument is list of libraries url or extension name.
724
725 If you want to add files, please edit auto-install-batch-list.el in EmacsWiki.
726 Use M-x `auto-install-batch-edit'. "
727   :group 'auto-install)
728
729 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Variable ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
730 (defvar auto-install-batch-list-internal nil
731   "The real value of `auto-install-batch-list'. ")
732
733 (defvar auto-install-batch-list-el-url
734   "http://www.rubyist.net/~rubikitch/archive/auto-install-batch-list.el"
735   "The url of auto-install-batch-list.el.
736 It is downloaded and evaluated just after M-x `auto-install-batch'. ")
737
738 (defvar auto-install-download-buffer nil
739   "The download buffer used by `url-retrieve'.
740 This variable is always buffer-local.")
741 (make-variable-buffer-local 'auto-install-download-buffer)
742
743 (defvar auto-install-download-url nil
744   "The url from which to download files.
745 This variable is always buffer-local.")
746 (make-variable-buffer-local 'auto-install-download-url)
747
748 (defvar auto-install-last-url nil
749   "The last url used in `auto-install-from-url'.")
750
751 (defvar auto-install-last-gist-id nil
752   "The last gist id you visit in `auto-install-from-gist'.")
753
754 (defvar auto-install-package-name-list nil
755   "The package name list for completion input.")
756
757 (defvar auto-install-minor-mode-map
758   (let ((map (make-sparse-keymap)))
759     (define-key map (kbd "C-c C-d") 'auto-install-buffer-diff) ;diff
760     (define-key map (kbd "C-c C-c") 'auto-install-buffer-save) ;save
761     (define-key map (kbd "C-c C-q") 'auto-install-buffer-quit) ;quit
762     map)
763   "Keymap used by variable `auto-install-minor-mode'.")
764
765 (defvar auto-install-add-load-path-flag t
766   "If non-nil, add `auto-install-directory' to `load-path'.
767 This variable is intended to be used in test.")
768
769 (defvar auto-install-add-exec-path-flag t
770   "If non-nil, add `auto-install-directory' to `exec-path'.
771 This variable is intended to be used in test.
772
773 It is needed because `auto-install-batch' can install non-elisp files.")
774
775 (defvar auto-install-waiting-url-list nil
776   "URLs in downloading.")
777 (defvar auto-install-url-queue nil
778   "Installation order.")
779 (defvar auto-install-download-buffer-alist nil
780   "Pairs of URL and downloaded buffer.")
781 (defvar auto-install-batch-using nil)
782
783 (define-minor-mode auto-install-minor-mode
784   "Auto Install minor mode."
785   :init-value nil
786   :lighter " Auto-Install"
787   :keymap auto-install-minor-mode-map
788   :group 'auto-install)
789
790 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Interactive Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
791 (defun auto-install-from-buffer ()
792   "Install the elisp file in the current buffer."
793   (interactive)
794   (let (filename)
795     (setq filename (read-string (format "Filename (%s): " (buffer-name)) nil nil (buffer-name)))
796     (auto-install-mode)
797     (auto-install-buffer-save filename)))
798
799 (defun auto-install-from-url (&optional url)
800   "Install an elisp file from a given url."
801   (interactive)
802   (or url (setq url (read-string (format "URL (%s): " (or auto-install-last-url "")) nil nil auto-install-last-url)))
803   (setq auto-install-last-url url)
804   (auto-install-download url))
805
806 (defun auto-install-from-emacswiki (&optional file)
807   "Install an elisp file from EmacsWiki.org."
808   (interactive)
809   (cond (auto-install-package-name-list
810          ;; Install package if `auto-install-package-name-list' is non-nil.
811          (or file (setq file (auto-install-get-candidate "Package" auto-install-package-name-list)))
812          (auto-install-download (concat auto-install-emacswiki-base-url file)))
813         (t
814          ;; Otherwise update package name and install.
815          (auto-install-download "http://www.emacswiki.org/cgi-bin/emacs?action=index;raw=1"
816                                 'auto-install-handle-emacswiki-package-install))))
817
818 (defun auto-install-from-gist (&optional gistid-or-url)
819   "Install an elisp file from gist.github.com.
820
821 Optional argument GISTID-OR-URL is gist ID or URL for download
822 elisp file from gist.github.com."
823   (interactive)
824   (or gistid-or-url
825       (setq gistid-or-url (read-string (format "Gist ID or URL (%s): " (or auto-install-last-gist-id ""))
826                                        nil nil
827                                        auto-install-last-gist-id)))
828   (let ((gistid (file-name-sans-extension (file-name-nondirectory gistid-or-url))))
829     (setq auto-install-last-gist-id gistid)
830     (auto-install-download (format "%s%s.txt" auto-install-gist-base-url gistid))))
831
832 (defun auto-install-from-library (&optional library)
833   "Update an elisp LIBRARY.
834 Default this function will found 'download url' from `auto-install-filter-url',
835 if not found, try to download from EmacsWiki."
836   (interactive
837    (let* ((dirs load-path)
838           (suffixes (find-library-suffixes)))
839      (list (auto-install-get-candidate "Library name" (auto-install-get-library-list)))))
840   (let ((filename (file-name-nondirectory (find-library-name library)))
841         (base-url auto-install-emacswiki-base-url)
842         (library-name (replace-regexp-in-string "\\(\\.el.*$\\)" "" library)))
843     (if (assoc library-name auto-install-filter-url)
844         (setq base-url (cadr (assoc library-name auto-install-filter-url))))
845     (auto-install-download (concat base-url filename))))
846
847 (defun auto-install-from-directory (directory)
848   "Update elisp files under DIRECTORY from EmacsWiki.
849 You can use this command to update elisp file under DIRECTORY."
850   (interactive "DDirectory: ")
851   (let (filename)
852     (dolist (file (directory-files directory t))
853       (if (file-directory-p file)
854           ;; Don't match . or .. directory.
855           (unless (string-match "^\\.\\.?$" (file-name-nondirectory file))
856             ;; Find files in sub-directory.
857             (auto-install-from-directory file))
858         ;; Get file name.
859         (setq filename (file-name-nondirectory file))
860         ;; Not backup file.
861         (unless (string-match "^\\.?#" filename)
862           ;; Match elisp file.
863           (if (string-match "^.*\\.el" filename)
864               (auto-install-download (concat auto-install-emacswiki-base-url filename))))))))
865
866 (defun auto-install-from-dired ()
867   "Update dired marked elisp files from EmacsWiki.org.
868 You can use this to download marked files in Dired asynchronously."
869   (interactive)
870   (if (eq major-mode 'dired-mode)
871       (if (or (not auto-install-from-dired-confirm)
872               (yes-or-no-p "Do you want install marked files from EmacsWiki.org?"))
873           (dolist (file (dired-get-marked-files))
874             (auto-install-download (concat auto-install-emacswiki-base-url (file-name-nondirectory file)))))
875     (error "This command is only for `dired-mode'.")))
876
877 (defun auto-install-network-available-p (host)
878   (if auto-install-use-wget
879       (eq (call-process auto-install-wget-command nil nil nil "-q" "--spider" host) 0)
880     (with-current-buffer (url-retrieve-synchronously (concat "http://" host))
881       (prog1 (not (zerop (buffer-size)))
882         (kill-buffer (current-buffer))))))
883 ;; (auto-install-network-available-p "www.emacswiki.org")
884
885 (defun auto-install-update-emacswiki-package-name (&optional unforced)
886   "Update the list of elisp package names from `EmacsWiki'.
887 By default, this function will update package name forcibly.
888 If UNFORCED is non-nil, just update package name when `auto-install-package-name-list' is nil."
889   (interactive)
890   (unless (and unforced
891                auto-install-package-name-list)
892     (if (auto-install-network-available-p "www.emacswiki.org")
893         (auto-install-download "http://www.emacswiki.org/cgi-bin/emacs?action=index;raw=1"
894                                'auto-install-handle-emacswiki-package-name)
895       (message
896        (concat "Network unreachable!\n"
897                "Try M-x auto-install-handle-emacswiki-package-name afterward."))
898       (sit-for 2))))
899
900 (defun auto-install-dired-mark-files ()
901   "Mark dired files that contain at `EmacsWiki.org'."
902   (interactive)
903   (if (eq major-mode 'dired-mode)
904       (if auto-install-package-name-list
905           ;; Mark files that exist at `EmacsWiki'.
906           (auto-install-dired-mark-files-internal)
907         ;; Or get package name list and match files.
908         (auto-install-download "http://www.emacswiki.org/cgi-bin/emacs?action=index;raw=1"
909                                'auto-install-handle-dired-mark-files))
910     (error "This command just use in `dired-mode'.")))
911
912 (defun auto-install-mode ()
913   "Major mode for auto-installing elisp code."
914   (interactive)
915   ;; Load emacs-lisp syntax highlight.
916   (set-syntax-table emacs-lisp-mode-syntax-table)
917   (lisp-mode-variables)
918   (setq font-lock-mode t)
919   (font-lock-fontify-buffer)
920   ;; Read only.
921   (setq buffer-read-only t)
922   (and view-read-only (view-mode 1))
923   ;; Load `auto-install' mode.
924   (auto-install-minor-mode t)
925   (setq major-mode 'auto-install-minor-mode))
926
927 (defun auto-install-buffer-quit ()
928   "Quit from `auto-install' temporary buffer."
929   (interactive)
930   ;; Quit buffer.
931   (if (eq major-mode 'auto-install-minor-mode)
932       (auto-install-quit)
933     (error "This command just use in `auto-install-minor-mode'.")))
934
935 (defun auto-install-compatibility-setup ()
936   "Install Compatibility commands for install-elisp.el users."
937   (interactive)
938   (defalias 'install-elisp 'auto-install-from-url)
939   (if (require 'anything-auto-install nil t)
940       (defalias 'install-elisp-from-emacswiki 'anything-auto-install-from-emacswiki)
941     (defalias 'install-elisp-from-emacswiki 'auto-install-from-emacswiki))
942   (defalias 'install-elisp-from-gist 'auto-install-from-gist)
943   (message "Install-elisp compatibility installed.
944 install-elisp                = %s
945 install-elisp-from-emacswiki = %s
946 install-elisp-from-gist      = %s"
947            (symbol-function 'install-elisp)
948            (symbol-function 'install-elisp-from-emacswiki)
949            (symbol-function 'install-elisp-from-gist)))
950
951 (defun auto-install-batch (&optional extension-name)
952   "Batch install many files (libraries and non-elisp files) in some extension.
953 EXTENSION-NAME is extension name for batch install.
954
955 Note that non-elisp can be installed only via `auto-install-batch'"
956   (interactive)
957   (if (and auto-install-batch-list-internal extension-name)
958       (auto-install-batch-real extension-name)
959     (auto-install-download
960      auto-install-batch-list-el-url
961      (lexical-let ((extension-name extension-name))
962        (lambda (buf)
963          (with-current-buffer buf
964            (eval-buffer)
965            (run-at-time 0 nil 'auto-install-batch-real extension-name)))))))
966
967 (defun auto-install-batch-edit ()
968   "Edit auto-install-batch-list.el"
969   (interactive)
970   (cond ((fboundp 'yaoddmuse-edit)
971          (yaoddmuse-edit "EmacsWiki" "auto-install-batch-list.el"))
972         ((fboundp 'oddmuse-edit)
973          (oddmuse-edit "EmacsWiki" "auto-install-batch-list.el"))
974         (t
975          (browse-url "http://www.emacswiki.org/emacs/?action=edit;id=auto-install-batch-list.el"))))
976
977 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Utilities Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
978 (defun auto-install-batch-real (&optional extension-name)
979   (setq auto-install-batch-using t)
980   (when auto-install-add-exec-path-flag
981       (add-to-list 'exec-path auto-install-directory))
982   (destructuring-bind (name delay-time limit-number (&rest urls))
983       (auto-install-batch-get-info
984        (or
985         ;; Get information list from give extension name.
986         extension-name
987         ;; Otherwise completion from user select.
988         (completing-read "Extension name: " (mapcar 'car auto-install-batch-list-internal)))
989        auto-install-batch-list-internal)
990     (or name (error "Haven't install information for `%s'." extension-name))
991     (setq auto-install-waiting-url-list urls
992           auto-install-url-queue urls)
993     (if (not (and
994               ;; Delay time is above 0.
995               delay-time
996               (> delay-time 0)
997               ;; Limit number is above 0.
998               limit-number
999               (> limit-number 0)))
1000         (auto-install-from-url-list urls)
1001       (let ((delay-counter 0)
1002             install-list)
1003         (while urls
1004           (if (> (length urls) limit-number)
1005               ;; Install apart libraries list under `limit-number'
1006               (progn
1007                 (setq install-list (nthcar limit-number urls))
1008                 (run-with-timer
1009                  (* delay-counter delay-time)
1010                  nil
1011                  'auto-install-from-url-list install-list)
1012                 (setq urls (nthcdr+ limit-number urls))
1013                 (incf delay-counter))
1014             ;; Install remain libraries list.
1015             (setq install-list urls)
1016             (run-with-timer
1017              (* delay-counter delay-time)
1018              nil
1019              'auto-install-from-url-list install-list)
1020             (setq urls nil)))))))
1021
1022 ;;; borrowed from eev.el
1023 (defun auto-install-flatten (obj &rest rest)
1024   (cond (rest (append (auto-install-flatten obj) (auto-install-flatten rest)))
1025         ((null obj) nil)
1026         ((listp obj) (append (auto-install-flatten (car obj)) (auto-install-flatten (cdr obj))))
1027         (t (list obj))))
1028
1029 (defun auto-install-batch-get-info (extension batch-list)
1030   (let* ((it (assoc extension batch-list))
1031          (urls (car (last it)))
1032          (urlp (lambda (url) (string-match "^https?://" url))))
1033     (cond ((not it)
1034            '(nil nil nil (nil)))
1035           ((loop for url in urls always (funcall urlp url))
1036            it)
1037           (t
1038            (append
1039             (butlast it)
1040             (list (auto-install-flatten
1041                    (loop for url in urls collect
1042                          (if (funcall urlp url)
1043                              url
1044                            (car (last (auto-install-batch-get-info url batch-list))))))))))))
1045
1046 ;;;; unit test
1047 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
1048 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
1049 (dont-compile
1050   (when (fboundp 'expectations)
1051     (expectations
1052       (desc "auto-install-flatten")
1053       (expect '(1 2 3 4 5 6 7 8 9)
1054         (auto-install-flatten '((1 2 3) (4 5) (((6)) 7) nil nil 8 9)))
1055       (expect '(1 2 3 4 5 6 7 8 9)
1056         (auto-install-flatten '(1 2 3) '(4 5) '(((6)) 7) nil nil 8 9))
1057       (desc "auto-install-batch-get-info")
1058       (expect '(nil nil nil (nil))
1059         (auto-install-batch-get-info
1060          "not-found"
1061          '(("foo" nil nil ("https://example.com/1.el")))))
1062       (expect '("foo" nil nil ("http://example.com/1.el"))
1063         (auto-install-batch-get-info
1064          "foo"
1065          '(("foo" nil nil ("http://example.com/1.el")))))
1066       (expect '("withdep" nil nil ("http://example.com/1.el"
1067                                    "http://example.com/2.el"))
1068         (auto-install-batch-get-info
1069          "withdep"
1070          '(("foo" nil nil ("http://example.com/1.el"))
1071            ("withdep" nil nil ("foo" "http://example.com/2.el")))))
1072       (expect '("withdep-recursive" nil nil ("http://example.com/1.el"
1073                                              "http://example.com/2.el"
1074                                              "http://example.com/3.el"))
1075         (auto-install-batch-get-info
1076          "withdep-recursive"
1077          '(("foo" nil nil ("http://example.com/1.el"))
1078            ("withdep" nil nil ("foo" "http://example.com/2.el"))
1079            ("withdep-recursive" nil nil ("withdep" "http://example.com/3.el")))))
1080       )))
1081
1082
1083 (defun auto-install-use-wget-p ()
1084   (and auto-install-use-wget
1085        (executable-find auto-install-wget-command)))
1086 (defun auto-install-download (url &optional handle-function)
1087   "Download elisp file from URL.
1088 HANDLE-FUNCTION for handle download content,
1089 default is `auto-install-handle-download-content'."
1090   ;; Check and create install directory.
1091   (unless (file-exists-p auto-install-directory)
1092     (make-directory auto-install-directory)
1093     (when auto-install-add-load-path-flag
1094       (add-to-list 'load-path auto-install-directory)) 
1095     (message "Create directory %s for install elisp file." auto-install-directory))
1096   ;; Download.
1097   (funcall
1098    (if (auto-install-use-wget-p)
1099        'auto-install-download-by-wget
1100      'auto-install-download-by-url-retrieve)
1101    url handle-function (auto-install-get-buffer url)))
1102
1103 (defun auto-install-download-by-wget (url handle-function download-buffer)
1104   (with-current-buffer download-buffer
1105     (setq auto-install-download-buffer (get-buffer-create (concat (buffer-name download-buffer)
1106                                                 "-wget")))
1107     (setq auto-install-download-url url)
1108     (set-process-sentinel
1109      (start-process "auto-install-wget" (current-buffer)
1110                     auto-install-wget-command "-q" "-O-" "--no-check-certificate" url)
1111      (lexical-let ((handle-function handle-function))
1112        (lambda (proc stat)
1113          (auto-install-download-callback-continue (buffer-name (process-buffer proc))
1114                                                   handle-function))))))
1115
1116 (defun auto-install-download-by-url-retrieve (url handle-function download-buffer)
1117   (let* ((url-request-method "GET")
1118          (url-request-extra-headers nil)
1119          (url-mime-accept-string "*/*")
1120          (parsed-url (url-generic-parse-url url))
1121          (download-buffer-name (buffer-name download-buffer)))
1122     (with-current-buffer download-buffer
1123       ;; Bind download url with local buffer.
1124       (setq auto-install-download-url url)
1125       ;; Bind download buffer with local buffer.
1126       ;;
1127       ;; Use buffer-local variable receive
1128       ;; data from `url-retrieve' to make asynchronously
1129       ;; download file with special buffer.
1130       ;;
1131       ;; Because the buffer name is unique that generate
1132       ;; through `current-time', so can download many elisp file
1133       ;; asynchronously and won't conflict each other.
1134       (setq auto-install-download-buffer
1135             (url-retrieve parsed-url
1136                           'auto-install-download-callback
1137                           (list download-buffer-name handle-function))))))
1138
1139 (defun auto-install-download-callback (&optional redirect download-buffer-name handle-function)
1140   "The callback for `auto-install-download'.
1141 With `auto-install-download', this downloads elisp files asynchronously.
1142 REDIRECT is the argument for check download status.
1143 DOWNLOAD-BUFFER-NAME is the name of download buffer.
1144 HANDLE-FUNCTION is function for handle download content."
1145   (if (eq (car redirect) ':error)
1146       ;; Notify user and kill buffer when occur error.
1147       (with-current-buffer (get-buffer download-buffer-name)
1148         (message "Download from '%s' failed." auto-install-download-url)
1149         (kill-buffer download-buffer-name))
1150     ;; Otherwise continue install process.
1151     (auto-install-download-callback-continue download-buffer-name handle-function)))
1152
1153 (defun auto-install-download-callback-continue (download-buffer-name handle-function)
1154   (auto-install-retrieve-decode download-buffer-name) ;decode retrieve information.
1155   (with-current-buffer (get-buffer download-buffer-name)
1156     ;; Show successful message
1157     (message "Download from '%s' successful." auto-install-download-url)
1158     ;; Handle download content.
1159     (funcall (or handle-function 'auto-install-handle-download-content)
1160              (current-buffer))))
1161
1162 (defun auto-install-retrieve-decode (retrieve-buffer-name)
1163   "Decode the RETRIEVE-BUFFER-NAME with coding detection."
1164   (declare (special url-http-end-of-headers))
1165   (with-current-buffer (get-buffer retrieve-buffer-name)
1166     (insert
1167      (with-current-buffer auto-install-download-buffer
1168        (set-buffer-multibyte t)
1169        ;; I do not know why the case url-http-end-of-headers is nil exists!!
1170        ;; I HATE url-retrieve.
1171        (if (and (boundp 'url-http-end-of-headers)
1172                 (numberp url-http-end-of-headers))
1173            (goto-char (1+ url-http-end-of-headers))
1174          ;; workaround
1175          (if (auto-install-use-wget-p)
1176              (goto-char (point-min))
1177            (search-forward "\n\n" nil t)))
1178        (decode-coding-region
1179         (point) (point-max)
1180         (coding-system-change-eol-conversion
1181          ;; rubikitch: encoding detection is better because of
1182          ;; non-utf8 Japanese encodings.
1183          (detect-coding-region (point-min) (point-max) t) 'dos))
1184        (buffer-substring (point) (point-max))))
1185     (goto-char (point-min))))
1186
1187 (defun auto-install-handle-download-content (download-buffer)
1188   "Handle the content downloaded to buffer DOWNLOAD-BUFFER."
1189   (with-current-buffer download-buffer
1190     ;; Load mode.
1191     (auto-install-mode)
1192     ;; Display help information in mode-line.
1193     (setq mode-line-format (list "Type C-c C-c to continue; Type C-c C-d for view diff; Type C-c C-q to quit."))
1194
1195     (setq header-line-format (list auto-install-download-url))
1196     (setq auto-install-download-buffer-alist
1197           (cons (cons auto-install-download-url download-buffer)
1198                 auto-install-download-buffer-alist))
1199     (setq auto-install-waiting-url-list
1200           (remove auto-install-download-url auto-install-waiting-url-list))
1201     ;; When all files are downloaded
1202     (unless auto-install-waiting-url-list
1203       ;; Select first file
1204       (switch-to-buffer (or (assoc-default (car auto-install-url-queue)
1205                                            auto-install-download-buffer-alist)
1206                             ;; if single file
1207                             download-buffer))
1208       (if (not (and (not auto-install-batch-using) (auto-install-check-update)))
1209           (unless auto-install-save-confirm
1210             (auto-install-buffer-save))
1211         (message "%s is up-to-date" (url-file-nondirectory auto-install-download-url))
1212         (kill-buffer))
1213       ;; (unless auto-install-save-confirm
1214       ;;   (auto-install-buffer-save))
1215       )))
1216
1217 (defun auto-install-check-update ()
1218   (let* ((new-file (url-file-nondirectory auto-install-download-url))
1219          (old-file (auto-install-get-path new-file))
1220          (old-content (and old-file
1221                            (with-temp-buffer
1222                              (insert-file-contents-literally old-file)
1223                              (buffer-string)))))
1224     (equal old-content (buffer-string))))
1225
1226 (defun auto-install-handle-emacswiki-package-name (download-buffer &optional prompt-install)
1227   "Handle elisp package name from `EmacsWiki'.
1228 DOWNLOAD-BUFFER is the name of download buffer.
1229 PROMPT-INSTALL is non-nil, will prompt package name for install."
1230   ;; Update package name list.
1231   (auto-install-update-emacswiki-package-list download-buffer)
1232   ;; Prompt package name for install.
1233   (when prompt-install
1234     (auto-install-download
1235      (concat auto-install-emacswiki-base-url
1236              (auto-install-get-candidate "Package" auto-install-package-name-list)))))
1237
1238 (defun auto-install-handle-dired-mark-files (download-buffer)
1239   "Handle dired mark files that exist at `EmacsWiki'.
1240 DOWNLOAD-BUFFER is the name of download buffer."
1241   ;; Update package name list.
1242   (auto-install-update-emacswiki-package-list download-buffer)
1243   ;; Mark dired files.
1244   (auto-install-dired-mark-files-internal))
1245
1246 (defun auto-install-handle-emacswiki-package-install (download-buffer)
1247   "Handle elisp package install from `EmacsWiki'.
1248 DOWNLOAD-BUFFER is the name of download buffer."
1249   (auto-install-handle-emacswiki-package-name download-buffer t))
1250
1251 (defun auto-install-update-emacswiki-package-list (download-buffer)
1252   "Filter and update package name list from `EmacsWiki'.
1253 DOWNLOAD-BUFFER is the name of download buffer."
1254   (goto-char (point-min))
1255   (setq auto-install-package-name-list
1256         (loop while (re-search-forward "^.*\\.el$" nil t)
1257               collect (match-string 0)))
1258   ;; Kill buffer.
1259   (kill-buffer download-buffer)
1260   ;; Display successful message.
1261   (message "Update package name from `EmacsWiki' successful."))
1262
1263 (defun auto-install-buffer-diff ()
1264   "View different between old version.
1265 This command just run when have exist old version."
1266   (interactive)
1267   (let* ((new-file (url-file-nondirectory auto-install-download-url))
1268          (old-file (auto-install-get-path new-file)))
1269     (if old-file
1270         ;; View different when have old version exist.
1271         (ediff-buffers (current-buffer) (find-file-noselect old-file))
1272       ;; Otherwise notify user.
1273       (message "Haven't old version exist."))))
1274
1275 (defun auto-install-buffer-save (&optional filename)
1276   "Save downloaded content to file FILENAME."
1277   (interactive)
1278   (if (eq major-mode 'auto-install-minor-mode)
1279       (let (file-path)
1280         ;; Get filename
1281         (unless filename
1282           (setq filename (url-file-nondirectory auto-install-download-url)))
1283         (unless auto-install-batch-using
1284           ;; Make sure file suffix with `.el'.
1285           (while (not (string-match ".*\.el$" filename))
1286            (setq filename (read-string "Please input file name suffix with `.el': "))))
1287         ;; Get file path.
1288         (setq file-path
1289               (or
1290                ;; Replace file if have exist.
1291                (auto-install-get-path filename)
1292                ;; Otherwise, install in directory `auto-install-directory'.
1293                (expand-file-name filename auto-install-directory)))
1294         ;; Save file.
1295         (if (and (file-exists-p file-path)
1296                  (file-writable-p file-path)
1297                  auto-install-replace-confirm
1298                  (not (yes-or-no-p (format "Do you want replace file: '%s' ?" file-path))))
1299             (auto-install-quit)
1300           (if (auto-install-check-update)
1301               (auto-install-install-next-file)
1302             (let ((before-save-hook before-save-hook)
1303                   (auto-async-byte-compile-exclude-files-regexp
1304                    (regexp-quote file-path)))
1305               (remove-hook 'before-save-hook 'time-stamp)
1306               (write-file file-path)
1307               (auto-install-install file-path)))))
1308     (error "This command just use in `auto-install-minor-mode'.")))
1309
1310 (defun auto-install-install (file-path)
1311   "Install elisp file FILE-PATH."
1312   (if (and auto-install-install-confirm
1313            (not (yes-or-no-p (format "Do you want install file: '%s' ?" file-path))))
1314       (auto-install-quit)
1315     (let (byte-compile-warnings) ;; suppress compile warnings
1316       ;; Compile and load file.
1317       (when (and (string= "el" (file-name-extension file-path))
1318                  (not (ignore-errors (byte-compile-file file-path t))))
1319         ;; Show `ERROR' message if compile failed.
1320         (message "Auto-Install ERROR: Compiled file '%s' failed." file-path))
1321       (auto-install-install-next-file))))
1322
1323 (defun auto-install-install-next-file ()
1324   (setq auto-install-url-queue (cdr auto-install-url-queue))
1325   (let (byte-compile-warnings)
1326     (cond ((car auto-install-url-queue)
1327            (switch-to-buffer (assoc-default (car auto-install-url-queue)
1328                                             auto-install-download-buffer-alist))
1329            (unless auto-install-save-confirm
1330              (auto-install-buffer-save)))
1331           (t                        ;completed
1332            (auto-install-cleanup)
1333            (message "Installation is completed.")))))
1334
1335 (defun auto-install-cleanup ()
1336   (while auto-install-minor-mode
1337     (kill-buffer))
1338   (setq auto-install-url-queue nil)
1339   (setq auto-install-download-buffer-alist nil)
1340   (setq auto-install-batch-using nil))
1341
1342 (defun auto-install-quit ()
1343   "Quit auto-install."
1344   ;; Kill buffer
1345   (kill-buffer (current-buffer))
1346   ;; Show quit message.
1347   (message "Quit auto-install process."))
1348
1349 (defun auto-install-get-path (library)
1350   "Return the absolute file path of the Lisp source of LIBRARY."
1351   ;; If the library is byte-compiled, try to find a source library by
1352   ;; the same name.
1353   (if (string-match "\\.el\\(c\\(\\..*\\)?\\)\\'" library)
1354       (setq library (replace-match "" t t library)))
1355   (or
1356    (locate-file library
1357                 (or find-function-source-path load-path)
1358                 (find-library-suffixes))
1359    (locate-file library
1360                 (or find-function-source-path load-path)
1361                 load-file-rep-suffixes)))
1362
1363 (defun auto-install-get-buffer (url)
1364   "Get a buffer for temporary storage of downloaded content.
1365 Uses `current-time' to make buffer name unique."
1366   (get-buffer-create (format "*%s %s <%s>*"
1367                              auto-install-buffer-name url
1368                              (format-time-string "%m/%d %H:%M:%S"))))
1369
1370 (defun auto-install-dired-mark-files-internal ()
1371   "Mark files that match `auto-install-package-name-list'."
1372   ;; Set buffer visible in select window.
1373   (set-buffer (window-buffer))
1374   ;; Get mark files.
1375   (save-excursion
1376     (let (filename)
1377       ;; Unmark all markes.
1378       (dired-unmark-all-marks)
1379       ;; Try to mark files.
1380       (goto-char (point-min))
1381       (while (not (eobp))
1382         (setq filename (dired-get-filename nil t))
1383         (if (and filename
1384                  (not (file-directory-p filename))
1385                  (member (file-name-nondirectory filename) auto-install-package-name-list))
1386             (dired-mark 1))
1387         (dired-next-line 1)))))
1388
1389 (defun auto-install-region-or-thing (&optional thing)
1390   "Return region or thing around point.
1391 If `mark-active' and variable `transient-mark-mode', return region.
1392 If THING is non-nil, return THING around point;
1393 otherwise return symbol around point."
1394   ;; Return string.
1395   (if (and mark-active
1396            transient-mark-mode)
1397       ;; Return region string just when
1398       ;; `mark-active' and `transient-mark-mode' is on.
1399       (buffer-substring-no-properties (region-beginning)
1400                                       (region-end))
1401     ;; Otherwise try to pick-up THING around point.
1402     (setq thing (or thing 'symbol))
1403     (ignore-errors
1404       (save-excursion
1405         (buffer-substring-no-properties (beginning-of-thing thing)
1406                                         (end-of-thing thing))))))
1407
1408 (defun auto-install-get-library-list (&optional dirs string)
1409   "Do completion for file names passed to `locate-file'.
1410 DIRS is directory to search path.
1411 STRING is string to match."
1412   ;; Use `load-path' as path when ignore `dirs'.
1413   (or dirs (setq dirs load-path))
1414   ;; Init with blank when ignore `string'.
1415   (or string (setq string ""))
1416   ;; Get library list.
1417   (let ((string-dir (file-name-directory string))
1418         name
1419         names)
1420     (dolist (dir dirs)
1421       (unless dir
1422         (setq dir default-directory))
1423       (if string-dir
1424           (setq dir (expand-file-name string-dir dir)))
1425       (when (file-directory-p dir)
1426         (dolist (file (file-name-all-completions
1427                        (file-name-nondirectory string) dir))
1428           ;; Suffixes match `load-file-rep-suffixes'.
1429           (setq name (if string-dir (concat string-dir file) file))
1430           (if (string-match (format "^.*\\.el%s$" (regexp-opt load-file-rep-suffixes)) name)
1431               (add-to-list 'names name)))))
1432     names))
1433
1434 (defun auto-install-get-candidate (prompt collection)
1435   "Get candidate from completing list.
1436 PROMPT is string for prompt.
1437 COLLECTION is list for completing candidates."
1438   (completing-read (format "%s (%s): " prompt (or (auto-install-region-or-thing) ""))
1439                    collection
1440                    nil nil nil nil
1441                    (auto-install-region-or-thing)))
1442
1443 (defun auto-install-from-url-list (&optional url-list)
1444   "Batch install many packages form URL-LIST."
1445   (if (listp url-list)
1446       (dolist (url url-list)
1447         (auto-install-from-url url))
1448     (error "Invalid url list for install.")))
1449
1450 (defun nthcdr+ (n list)
1451   "Take cdr N times on LIST, return the result.
1452 If LIST length below N, return entire list.
1453 If LIST is nil, return nil."
1454   (if (or (null list)
1455           (> n (length list)))
1456       list
1457     (nthcdr n list)))
1458
1459 (defun nthcar (n list)
1460   "Return first N elements of LIST.
1461 If LIST length below N, return entire list.
1462 If LIST is nil, return nil."
1463   (reverse (nthcdr (- (length list) n) (reverse list))))
1464
1465 ;;;; Bug report
1466 (defvar auto-install-maintainer-mail-address
1467   (concat "rubiki" "tch@ru" "by-lang.org"))
1468 (defvar auto-install-bug-report-salutation
1469   "Describe bug below, using a precise recipe.
1470
1471 When I executed M-x ...
1472
1473 How to send a bug report:
1474   1) Be sure to use the LATEST version of auto-install.el.
1475   2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
1476   3) Use Lisp version instead of compiled one: (load \"auto-install.el\")
1477   4) If you got an error, please paste *Backtrace* buffer.
1478   5) Type C-c C-c to send.
1479 # If you are a Japanese, please write in Japanese:-)")
1480 (defun auto-install-send-bug-report ()
1481   (interactive)
1482   (reporter-submit-bug-report
1483    auto-install-maintainer-mail-address
1484    "auto-install.el"
1485    (apropos-internal "^auto-install-" 'boundp)
1486    nil nil
1487    auto-install-bug-report-salutation))
1488
1489
1490 (provide 'auto-install)
1491
1492 ;; TestCase
1493 ;; (find-file-other-window "~/memo/junk/2010-04-25-094621.auto-install-check-update.test.el")
1494
1495 ;; How to save (DO NOT REMOVE!!)
1496 ;; (emacswiki-post "auto-install.el")
1497 ;;; auto-install.el ends here
1498
1499 ;;; LocalWords:  el eol dirs fontify gistid txt func bytecomp DDirectory ediff
1500 ;;; LocalWords:  noselect Unmark unmark AutoInstall keybindings defalias'es
1501