0
votes

Current learning elisp. I saw the following snippet:

(defun abedra/packages-installed-p ()
  (loop for pkg in abedra/packages
        when (not (package-installed-p pkg)) do (return nil)
        finally (return t)))

(unless (abedra/packages-installed-p)
  (message "%s" "Refreshing package database...")
  (package-refresh-contents)
  (dolist (pkg abedra/packages)
    (when (not (package-installed-p pkg))
      (package-install pkg))))

What this snippet does is test to see if all packages are installed. If not, it will refresh the package database and then install the missing packages. I'd like to eliminate the need for abedra/packages-installed-p combine it into one statement. It would check each package, and if one is missing, it would update the database and then install the missing packages. The updating of the database would only be done if at least one package is missing.

Can someone show me how this would be done?

2

2 Answers

0
votes

If I am understanding you correctly, you want to Factor In (as opposed to factoring out) abedra/packages-installed-p.

All that is needed to do that is to replace the call to abedra/packages-installed-p with its body like so:

(unless
    (loop for pkg in abedra/packages
      when (not (package-installed-p pkg)) do (return nil)
      finally (return t))
  (message "%s" "Refreshing package database...")
  (package-refresh-contents)
  (dolist (pkg abedra/packages)
    (when (not (package-installed-p pkg))
      (package-install pkg))))

Why you would want to do this is a different story, as it is usually better to factor out small functions to make code easier to maintain.

0
votes

This is not exactly the answer to my own question but I ended up with this which I liked more than alternatives, and did not require me to (require 'cl). I had no idea what the cl library was and there did not seem to be a reason why I needed it.

(defun abc/install-packages (refreshed pkgs)
  (when pkgs
    (when (not (package-installed-p (car pkgs)))
      (if (not refreshed) (package-refresh-contents))
      (setq refreshed t)
      (package-install (car pkgs)))
    (abc/install-packages refreshed (cdr pkgs))))

(abc/install-packages nil '(yaml-mode rainbow-delimiters))