如何通过指定包的名称列表自动安装Emacs的包?(How to automatically inst

2019-06-23 12:36发布

我使用package来管理我的Emacs的扩展。 为了同步不同计算机上的我的Emacs的设置,我想办法在指定包名称列表.emacs文件,然后package可以自动搜索并安装包,这样我就不需要手动安装通过调用Mx package-list-packages 。 怎么做?

Answer 1:

; 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)))


Answer 2:

基于由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)


Answer 3:

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



Answer 4:

下面是我使用的代码Emacs的前奏 :

(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是每次不更新(因为这将启动显著放缓) - 只有在不存在卸载的软件包。



Answer 5:

没有人提到木桶还,但它还是比较适合这个任务。

基本上你创建~/.emacs.d/Cask ,列出将要安装的软件包。 例如:

(source melpa)
(depends-on "expand-region")
(depends-on "goto-last-change")
; ... etc

运行cask从命令行将安装这些包,为你,任何依赖他们的需要。

此外,您还可以自动更新使用安装包cask update



Answer 6:

呼叫package-install与包名称为一个符号。 你可以通过调用找到适合您的包包名package-install交互和完成的名称。 功能package-installed-p将让你知道,如果包括已经安装了它。

例如:

(mapc
 (lambda (package)
   (or (package-installed-p package)
       (package-install package)))
 '(package1 package2 package3))


Answer 7:

我喜欢检查,如果用户想先安装包在做这个答案 。 另外我安装任何东西之前刷新我的包内容一次。 我不知道这是否是最好的方式,但我不认为顶部的答案在做对我来说。

(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)))


Answer 8:

(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)


Answer 9:

我遇到了一个问题,没有什么增加发生后(package-install 'org)进入.emacs 。 我想安装的先进的最新版本org-mode和内置的org-mode是很老。

我翻出的源代码package-install的Emacs的25.3.1。 如果安装程序包或不并拒绝安装,如果已经安装了软件包的功能自已检查。 所以检查(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对象。



Answer 10:

这里是我的,这是短:)

(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))


文章来源: How to automatically install Emacs packages by specifying a list of package names?