我正在处理我的init.el并且想要设置它以检查我的已定义的软件包是否已安装,如果没有,则刷新软件包内容并安装它们。
我可以自动完成安装,但是如果没有安装软件包,我只想刷新软件包列表。
我提出了一些适用于普通lisp的东西,但好像elisp没有every-p这是最简单的方法。
(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)))))
我想出的另一种方法是使用return-from,但似乎这不是elisp的一部分。通过一些搜索,看起来我可以用catch和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))
这样做的最佳方式是什么?
编辑#1 使用cl-lib和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)
编辑#2
要做到不需要常见的lisp库,请将my-every替换为以下followint Drew的建议答案。
感谢您的帮助!
(defun my-every (pred list)
(while (and list (funcall pred (car list)))
(setq list (cdr list)))
(null list))
答案 0 :(得分:2)
seq-every-p
来自seq.el,可从GNU ELPA获得,也将成为25.1的内置包。
(seq-every-p #'package-installed-p '(helm helm-ls-git helm-ag))
=> t
答案 1 :(得分:1)
我在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))