前面我们介绍过, emacs 的 package 包只能下载这种由 archive-contents 管理的标准软件仓库中的包。对于没有提交到 melpa、elpa 的包来说,例如在github、或者gitlab中的内容我们需要通过 package-vc-install 函数下载更新。在使用 use-package 的情况下,想要下载 github 中的包,需要使用 :vc 关键字
背景知识
针对GitHub上的包仓库,我们需要使用 :vc 关键字来下载。但是需要说明一下,emacs 29 以前使用这个关键字需要额外安装软件包 vc-use-package。从Emacs 30 以后,我们可以直接在 use-package 中使用 :vc
我们以 modus-themes 主题为例,看看怎么使用:
(use-package modus-themes
:vc "https://github.com/protesilaos/modus-themes")展开之后得到宏内容如下:
(progn
(use-package-vc-install
'(modus-themes "https://github.com/protesilaos/modus-themes") nil)
(defvar use-package--warning0
#'(lambda (keyword err)
(let
((msg
(format "%s/%s: %s" 'modus-themes keyword
(error-message-string err))))
(display-warning 'use-package msg :error))))
(condition-case-unless-debug err
(if (not (require 'modus-themes nil t))
(display-warning 'use-package
(format "Cannot load %s" 'modus-themes)
:error))
(error (funcall use-package--warning0 :catch err))))我们发现原来下载安装包的函数变成了 use-package-vc-install。我们查看它的函数定义:
(defun use-package-vc-install (arg &optional local-path)
"Install a package with `package-vc.el'.
ARG is a list of the form (NAME OPTIONS REVISION), as returned by
`use-package-normalize--vc-arg'. If LOCAL-PATH is non-nil, call
`package-vc-install-from-checkout'; otherwise, indicating a
remote host, call `package-vc-install' instead."
(pcase-let* ((`(,name ,opts ,rev) arg)
(spec (if opts (cons name opts) name)))
(unless (package-installed-p name)
(if local-path
(package-vc-install-from-checkout local-path (symbol-name name))
(package-vc-install spec rev)))))它的代码相对来说比较简单,核心就是判断当前是否存在对应的包,不存在时通过调用 package-vc-install 下载安装我们的软件包。
我们按照之前介绍 package-vc-install 函数的用法来看看对应着 :vc 关键字的哪些用法
首先我们可以通过 :vc 列表中的 :url 属性指定远程仓库的地址:
(use-package modus-themes
:vc (:url "https://github.com/protesilaos/modus-themes"))可以通过 :rev 控制拉取的版本,利用 :newest 可以拉取最新提交,而不用等提交合并到对应分支
(use-package modus-themes
:vc (:url "https://github.com/protesilaos/modus-themes"
:rev :newest))我们也可以通过 :branch 指定下载某一个分支的内容,例如
(use-package modus-themes
:vc (:url "https://github.com/protesilaos/modus-themes"
:branch "main"))还有一些其他的操作,请读者自行阅读对应的文档
总结
本文内容比较简单,我们介绍了 use-package 中使用 :vc 关键字从 github 中下载软件。它内部只要依赖 package-vc-install 安装包。
我们可以通过传入 plist 列表实现下载特定版本的包,我们介绍了三个属性:
:url: 指定包地址:rev: 指定commit id:branch: 指定分支
评论 (0)