[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Require or Install

From: Olivier Ramonat
Subject: Require or Install
Date: Sun, 16 Sep 2007 15:09:35 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.0 (gnu/linux)


I've written a small tool to automatically download and install
library from EmacsWiki or URL using install-elisp.el

Use (require-or-install 'my-package) to install my-package.el from
EmacsWiki if require fails.

You'll need to patch install-elisp.el :
> (defcustom install-elisp-simple-confirm nil
>   "If non-nil, ask y-or-n-p to confirm installation"
>   :group 'install-elisp)
<       (install-elisp-confirmation-minor-mode)
<       (message "Type C-c C-c to do installation!"))))
>       (if (cond (install-elisp-simple-confirm
>                  (y-or-n-p "Confirm installation ? ")
>                  ))
>           (install-elisp-proceed)
>         (install-elisp-confirmation-minor-mode)
>         (message "Type C-c C-c to do installation!")))))


;;; require-or-install.el --- Download and automagically install Emacs Lisp

;; Copyright (C) 2007  Olivier Ramonat

;; Author: Olivier Ramonat <address@hidden>
;; Created: 8 Aug 2007

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; Use (require-or-install 'my-package) to automatically download and install
;; my-package.el if require fails.
;; require-or-install takes two others optionals arguments :
;;    - PAGENAME (the EmacsWiki PAGENAME where to download my-package)
;;    - URL (the download URL)
;; To install, to add to .emacs:
;;    (require 'require-or-install)

;;; Code:

(eval-when-compile (require 'install-elisp))

(defgroup require-or-install nil
  "Download and automagically install lib"
  :group 'hypermedia
  :prefix "require-or-install-"

(defcustom require-or-install-confirm-flag t
  "Non-nil means do download confirmation.
Note that install-elisp.el ask for install confirmation.
  :type 'boolean
  :require 'install-elisp
  :group 'require-or-install)

(defcustom require-or-install-is-enabled t
  "If nil, disable installation.  Do require only."
  :type 'boolean
  :require 'install-elisp
  :group 'require-or-install)

(defvar require-or-install-map
  (let ((map (make-sparse-keymap)))
    (define-key map " " 'act)
    (define-key map "\d" 'skip)
    (define-key map [delete] 'skip)
    (define-key map [backspace] 'skip)
    (define-key map "y" 'act)
    (define-key map "n" 'skip)
    (define-key map "Y" 'act)
    (define-key map "N" 'skip)
    (define-key map "!" 'automatic)
    (define-key map "\C-h" 'help)
    (define-key map [f1] 'help)
    (define-key map [help] 'help)
    (define-key map "?" 'help)
    (define-key map "\C-g" 'quit)
    (define-key map "\C-]" 'quit)
    (define-key map "q" 'exit)
    (define-key map "\r" 'exit)
    (define-key map [return] 'exit)
  "Key map that defines the responses to questions in `require-or-install'.
The valid answers include `act', `skip', `automatic', `exit' and `help'.")

(defconst require-or-install-help
  "Type Space or `y' to install the library, Delete or `n' to skip to next,
RET or `q' to exit, ! to install all remaining libraries with no
more questions."
  "Help message while in `require-or-install'.")

(defun require-or-install-ask (library)
  "Ask for confirmation to install LIBRARY."
  (let (key def done do-install)
    (message (format "Install Emacs Lisp code %s? " library))
    (while (not done)
      (setq key (read-event))
      (setq key (vector key))
      (setq def (lookup-key require-or-install-map key))
      (cond ((eq def 'help)
             (with-output-to-temp-buffer "*Help*"
                (concat "Require Or Install.\n\n"
               (with-current-buffer standard-output
            ((eq def 'exit)
             (setq done t)
             (setq require-or-install-is-enabled nil))
            ((eq def 'skip)
             (setq done t))
            ((eq def 'automatic)
             (setq do-install t)
             (setq done t)
             (setq require-or-install-confirm-flag nil))
            ((eq def 'act)
             (setq do-install t)
             (setq done t))))

(defun require-or-install (feature &optional pagename &optional url)
  "Try to require FEATURE.  Ask for install if `require' fails.
PAGENAME is the name of the EmacsWiki page when FEATURE can be downloaded
If PAGENAME is nil, FEATURE.el is used.
When URL is not null then install from URL."
  (if (locate-library (format "%s" feature))
      (require feature)
    (when require-or-install-is-enabled
      (setq install-elisp-simple-confirm t)
      (if url
          (when (or (not require-or-install-confirm-flag)
                    (require-or-install-ask url))
            (install-elisp url))
        (let ((emacswiki-pagename
               ((lambda (a &optional b)
                  (if b b (format "%s.el" a)))
                feature pagename)))
          (when (or (not require-or-install-confirm-flag)
                    (require-or-install-ask emacswiki-pagename))
            (install-elisp-from-emacswiki emacswiki-pagename))))
      (setq install-elisp-simple-confirm nil))))

(defun require-or-install-add-font-lock ()
  "Add keyword face for `require-or-install'."
  (font-lock-add-keywords 'emacs-lisp-mode
                          '(("require-or-install" . font-lock-keyword-face))))

(provide 'require-or-install)

;;; require-or-install.el ends here

reply via email to

[Prev in Thread] Current Thread [Next in Thread]