如何通过指定包名列表自动安装Emacs包?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过指定包名列表自动安装Emacs包?相关的知识,希望对你有一定的参考价值。

我正在使用package来管理我的Emacs扩展。为了在不同的计算机上同步我的Emacs设置,我想在.emacs文件中指定包名列表,然后package可以自动搜索和安装包,这样我就不需要手动安装它们了。叫M-x package-list-packages。怎么做?

答案
; 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)))
另一答案

这是我的,它更短:)

(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))
另一答案

根据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)
另一答案

Emacs 25.1+将自动跟踪可自定义的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自动更新已安装的软件包。

另一答案

使用包名称作为符号调用package-install。您可以通过交互方式调用package-install并填写名称来查找软件包的软件包名称。功能package-installed-p会告诉您它是否已经安装。

例如:

(mapc
 (lambda (package)
   (or (package-installed-p package)
       (package-install package)))
 '(package1 package2 package3))
另一答案

我喜欢检查用户是否想要首先安装包,就像在this answer中一样。在安装任何东西之前,我还要刷新包装内容。我不确定这是不是最好的方法,但我不认为最好的答案是为我做的。

(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)))
另一答案
(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)
另一答案

我遇到了一个问题,即在将(package-install 'org)添加到.emacs后没有任何反应。我想安装最新版本的org-mode,内置的org-mode已经很老了。

我从Emacs 25.3.1中挖出了package-install的源代码。函数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-conte

以上是关于如何通过指定包名列表自动安装Emacs包?的主要内容,如果未能解决你的问题,请参考以下文章

helm安装:包emacs '24 .4'不可用

如何让 Emacs/Slime/SBCL 识别 quicklisp 包

Emacs 24 包系统初始化问题

Go-18-自动下载所有依赖包

Go-18-自动下载所有依赖包

SQLAlchemy学习-3.(懒人专用)sqlacodegen自动同步数据库中表生成model 代码