Answers:
; list the packages you want
(setq package-list '(package1 package2))
; list the repositories containing them
(setq package-archives '(("elpa" . "http://tromey.com/elpa/")
("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")))
; activate all the packages (in particular autoloads)
(package-initialize)
; fetch the list of packages available
(unless package-archive-contents
(package-refresh-contents))
; install the missing packages
(dolist (package package-list)
(unless (package-installed-p package)
(package-install package)))
package
。
package-refresh-contents
只有在未安装软件包的情况下才能运行?效果如何(or (file-exists-p package-user-dir))
/如何检查软件包是否已安装?
package-refresh-contents
。这可能只会在您第一次在新计算机上打开emacs时运行,我对此表示满意。如果软件包需要更新,可以手动完成。
use-package
,则可以使用:ensure
关键字自动安装软件包。package-selected-packages
如果您需要通过自定义或编程方式访问软件包列表,也会进行设置。
根据Profpatsch的评论和以下答案:
(defun ensure-package-installed (&rest packages)
"Assure every package is installed, ask for installation if it’s not.
Return a list of installed packages or nil for every skipped package."
(mapcar
(lambda (package)
;; (package-installed-p 'evil)
(if (package-installed-p package)
nil
(if (y-or-n-p (format "Package %s is missing. Install it? " package))
(package-install package)
package)))
packages))
;; make sure to have downloaded archive description.
;; Or use package-archive-contents as suggested by Nicolas Dudebout
(or (file-exists-p package-user-dir)
(package-refresh-contents))
(ensure-package-installed 'iedit 'magit) ; --> (nil nil) if iedit and magit are already installed
;; activate installed packages
(package-initialize)
or
?哦,哇
mapc
是为了副作用。但是为什么不使用unless
呢?
(package-initialize)
在提到package-user-dir
Emacs 25.1+将在customizable package-selected-packages
变量中自动跟踪用户安装的软件包。package-install
将更新定制变量,您可以使用该package-install-selected-packages
功能安装所有选定的软件包。
这种方法的另一个便利优势是,您可以package-autoremove
用来自动删除不包含在其中的软件包package-selected-packages
(尽管它将保留相关性)。
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
(package-install-selected-packages)
资料来源:http : //endlessparentheses.com/new-in-package-el-in-emacs-25-1-user-selected-packages.html
这是我用于Emacs Prelude的代码:
(require 'package)
(require 'melpa)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
(setq url-http-attempt-keepalives nil)
(defvar prelude-packages
'(ack-and-a-half auctex clojure-mode coffee-mode deft expand-region
gist haml-mode haskell-mode helm helm-projectile inf-ruby
magit magithub markdown-mode paredit projectile
python sass-mode rainbow-mode scss-mode solarized-theme
volatile-highlights yaml-mode yari yasnippet zenburn-theme)
"A list of packages to ensure are installed at launch.")
(defun prelude-packages-installed-p ()
(loop for p in prelude-packages
when (not (package-installed-p p)) do (return nil)
finally (return t)))
(unless (prelude-packages-installed-p)
;; check for new packages (package versions)
(message "%s" "Emacs Prelude is now refreshing its package database...")
(package-refresh-contents)
(message "%s" " done.")
;; install the missing packages
(dolist (p prelude-packages)
(when (not (package-installed-p p))
(package-install p))))
(provide 'prelude-packages)
如果你不使用MELPA你不必要求它(如果你这样做melpa.el
一定是你的load-path
(或通过MELPA安装)。包DB是每次不更新(因为这将启动显著放缓)-仅在存在已卸载软件包的地方。
还没有人提到Cask,但是它非常适合完成这项任务。
基本上,您会~/.emacs.d/Cask
列出要安装的软件包。例如:
(source melpa)
(depends-on "expand-region")
(depends-on "goto-last-change")
; ... etc
cask
从命令行运行将为您安装这些软件包及其所需的任何依赖项。
另外,您可以使用来自动更新已安装的软件包cask update
。
/path/to/emacs -Q --script /path/to/cask/cask-cli.el -- [args]
package-install
以包裹名称为标志进行呼叫。您可以通过package-install
交互式调用并完成名称来查找软件包的软件包名称。该功能package-installed-p
将通知您是否已安装。
例如:
(mapc
(lambda (package)
(or (package-installed-p package)
(package-install package)))
'(package1 package2 package3))
error: Package
指示,dird +'无法用于安装`。dired +是我尝试使用您的代码的软件包。
dired+
跑步时是否显示package-list-packages
?我相信您需要在您的饼干中添加果酱或蜜饯package-archives
。如果是这样,您可以跑步(package-install 'dired+)
吗?
(package-installed-p 'dired+)
应返回t
并且将在上面的代码中将其跳过。
package-installed-p
单独工作正常,但整个代码段没有。我试过几个包。
(require 'cl)
(require 'package)
(setq cfg-var:packages '(
emmet-mode
ergoemacs-mode
flycheck
flycheck-pyflakes
monokai-theme
py-autopep8
py-isort
rainbow-mode
yafolding
yasnippet))
(defun cfg:install-packages ()
(let ((pkgs (remove-if #'package-installed-p cfg-var:packages)))
(when pkgs
(message "%s" "Emacs refresh packages database...")
(package-refresh-contents)
(message "%s" " done.")
(dolist (p cfg-var:packages)
(package-install p)))))
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/") t)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)
(add-to-list 'package-archives '("melpa-stable" . "http://stable.melpa.org/packages/") t)
(add-to-list 'package-archives '("org" . "http://orgmode.org/elpa/") t)
(package-initialize)
(cfg:install-packages)
我喜欢检查用户是否要按照此答案的要求先安装软件包。另外,在安装任何东西之前,我都会刷新一次包装内容。我不确定这是否是最好的方法,但我认为最重要的答案不是为我做的。
(setq required-pkgs '(jedi flycheck cider clojure-mode paredit markdown-mode jsx-mode company))
(require 'cl)
(setq pkgs-to-install
(let ((uninstalled-pkgs (remove-if 'package-installed-p required-pkgs)))
(remove-if-not '(lambda (pkg) (y-or-n-p (format "Package %s is missing. Install it? " pkg))) uninstalled-pkgs)))
(when (> (length pkgs-to-install) 0)
(package-refresh-contents)
(dolist (pkg pkgs-to-install)
(package-install pkg)))
我遇到一个问题,添加(package-install 'org)
进去后什么也没发生.emacs
。我想安装的最新版本,org-mode
并且内置的版本org-mode
很旧。
我package-install
从Emacs 25.3.1中挖出了源代码。函数self已经检查软件包是否已安装,如果软件包已经安装,则拒绝安装。因此,实际上是不需要(unless (package-installed-p package) ...)
答案10093312的检查的。
(defun package-install (pkg &optional dont-select)
"Install the package PKG.
PKG can be a package-desc or a symbol naming one of the available packages
in an archive in `package-archives'. Interactively, prompt for its name.
If called interactively or if DONT-SELECT nil, add PKG to
`package-selected-packages'.
If PKG is a package-desc and it is already installed, don't try
to install it but still mark it as selected."
(interactive
(progn
;; Initialize the package system to get the list of package
;; symbols for completion.
(unless package--initialized
(package-initialize t))
(unless package-archive-contents
(package-refresh-contents))
(list (intern (completing-read
"Install package: "
(delq nil
(mapcar (lambda (elt)
(unless (package-installed-p (car elt))
(symbol-name (car elt))))
package-archive-contents))
nil t))
nil)))
(add-hook 'post-command-hook #'package-menu--post-refresh)
(let ((name (if (package-desc-p pkg)
(package-desc-name pkg)
pkg)))
(unless (or dont-select (package--user-selected-p name))
(package--save-selected-packages
(cons name package-selected-packages)))
(if-let ((transaction
(if (package-desc-p pkg)
(unless (package-installed-p pkg)
(package-compute-transaction (list pkg)
(package-desc-reqs pkg)))
(package-compute-transaction () (list (list pkg))))))
(package-download-transaction transaction)
(message "`%s' is already installed" name))))
内置org-mode
也算作已安装,并package-install
拒绝从ELPA安装较新版本。花一些时间阅读package.el之后,我想到了以下解决方案。
(dolist (package (package-compute-transaction
() (list (list 'python '(0 25 1))
(list 'org '(20171211)))))
;; package-download-transaction may be more suitable here and
;; I don't have time to check it
(package-install package))
它起作用的原因是,package-*
族函数根据其是符号还是package-desc
对象来不同地处理参数。您只能package-install
通过package-desc
对象指定版本信息。
这是我的,它更短:)
(mapc
(lambda (package)
(unless (package-installed-p package)
(progn (message "installing %s" package)
(package-refresh-contents)
(package-install package))))
'(browse-kill-ring flycheck less-css-mode tabbar org auto-complete undo-tree clojure-mode markdown-mode yasnippet paredit paredit-menu php-mode haml-mode rainbow-mode fontawesome))
这是另一种方式。
;; assure every package is installed
(defun ensure-package-installed (&rest packages)
(let ((user-required-packages
(seq-remove
(lambda (package) (package-installed-p package))
packages)))
(when user-required-packages
(package-refresh-contents)
(dolist (package user-required-packages)
(package-install package)))))
;; list of packages to install
(ensure-package-installed
'try
'which-key)