发现 ELPA 镜像又没有更新了。不想每次都打开浏览器查看镜像网站同步状态,于是把自己用的 find-fastest-mirror-for-me
函数改了一下,输出结果加上最后更新时间戳(遗憾的是 gitlab/github 的 http headers 不包含更新时间戳):
(defun find-fastest-mirror-for-me ()
"Find the fatest mirror for me.
Inspied by @xuchunyang https://emacs-china.org/t/elpa/11192/9"
(interactive)
(require 'request)
(with-output-to-temp-buffer "*Elpa mirror test*"
(princ " score (s) mirror last updated\n")
(princ "----------- ---------------------------- ------------------\n"))
(dolist (mirror package-mirror-alist)
(let ((url (cdr (assoc "melpa" mirror)))
(begin-time (float-time (current-time)))
(request-backend (car '(curl url-retrieve))))
(request (concat url "archive-contents")
:timeout 30
:complete
(cl-function
(lambda (&key response symbol-status &allow-other-keys)
(with-current-buffer "*Elpa mirror test*"
(goto-char (point-max))
(let ((inhibit-read-only t))
(insert (format "%11s %-29s [%s]\n"
(if (eq symbol-status 'success)
(format
"%6fs"
(- (float-time (current-time)) begin-time))
symbol-status)
(url-host (url-generic-parse-url url))
(if (eq symbol-status 'success)
(request-response-header response "Last-Modified"))))))))))))
代码基于 @xuchunyang 的《 本地测试 ELPA 镜像的速度 - #9,来自 xuchunyang 》,并改为异步请求(依赖 request 包),齐发并进节省时间。结果也不排序,先返回先打印。
使用时需预先定一个 package-mirror-alist
变量,其各式为:
(defconst package-mirror-alist
'((default
("gnu" . "https://elpa.gnu.org/packages/")
("melpa" . "https://melpa.org/packages/")
("org" . "https://orgmode.org/elpa/"))
(emacs-china
("gnu" . "https://elpa.emacs-china.org/gnu/")
("melpa" . "https://elpa.emacs-china.org/melpa/")
("org" . "https://elpa.emacs-china.org/org/"))
...))
有这个变量之后,切换镜像也很方便:(setq package-archives (assoc-default 'emacs-china package-mirror-alist))
。