Emacs init.el and Elisp and equivalent of common lisp every-p function

205 views Asked by At

I am working on my init.el and would like to setup so that it checks if my defined packages are installed and if not then refresh package contents and install them.

I can automate the install fine, but I only want to refresh the package list if there are packages not installed.

I came up with something that works with common lisp but seems as if elisp does not have every-p which is the simplest method.

(defparameter *my-packages* '(evil evil-leader helm))
(defparmeter *installed-pkgs* '())

;fake package-installed-p
(defun package-installed-p(pkg)
  (member pkg *installed-pkgs*))

;fake package-install
(defun package-install(pkg)
  (format t "Installing package: ~a~%" pkg))

(defun check-installed-p()
  (every #'package-installed-p *my-packages*))

(defun mytest()
  (unless (check-installed-p)
    (package-refresh-contents)
    (dolist (pkg *my-packages*)
       (unless (package-installed-p pkg)
          (package-install pkg)))))

The other method I came up with was the following using return-from but it seems this is not part of elisp. With some searching it looks like I could emulate this with catch and throw.

(defun check-installed-p()
    (dolist (pkg *my-packages*)
       (unless (package-installed-p(pkg)
          (return-from check-installed-p NIL)))
    (return-from check-installed-p T))

What is the best way to do this?

EDIT #1 Using cl-lib and cl-extra

(require 'package)
(require 'cl-lib)
(require 'cl-extra)

(push '("melpa" . "http://melpa.org/packages/") package-archives )

(package-initialize)

(defconst *my-packages* '(evil evil-leader helm))

(defun my-package-check()
  (unless (cl-every #'package-installed-p *my-packages)
    (package-refresh-contents)
    (dolist (pkg *my-packages*)
      (unless (package-installed-p pkg)
        (package-install pkg)))))

(my-package-check)

(require 'evil)
(require 'evil-leader)
(require 'helm-config)

EDIT #2

To do without require common lisp libraries, replace cl-every with my-every below followint Drew's suggested answer.

Thanks for help!

(defun my-every (pred list)
  (while (and list (funcall pred (car list)))
    (setq list (cdr list)))
  (null list))
2

There are 2 answers

2
Drew On BEST ANSWER

I use this in Bookmark+:

(defun bmkp-every (predicate list)
  "Return t if PREDICATE is true for all elements of LIST; else nil."
  (while (and list  (funcall predicate (car list)))  (setq list  (cdr list)))
  (null list))
2
xuchunyang On

seq-every-p comes from seq.el which is available from GNU ELPA and also will become a built-in package from 25.1.

(seq-every-p #'package-installed-p '(helm helm-ls-git helm-ag))
=> t