加载路径 ivy-* 匹配任意系列号

Load path ivy-* match any series number

我使用 -0.13.1 的包后缀为 Ivy 自定义 load-path:

(add-to-list 'load-path "~/.zeroemacs/elpa/ivy-0.13.1/")

但是ivy包升级到0.14.1后,我不得不手动修改load-path

(add-to-list 'load-path "~/.zeroemacs/elpa/ivy-0.14.1/")

是否可以用 ivy-* 之类的匹配任何序列号的东西替换它?

版本号有多种形状和大小。下面的 latest-file-version 函数没有尝试处理任何和所有版本格式,而是依赖于比较 version-to-list 函数接受的版本字符串。 version-to-list 的文档包括对它接受的内容的描述:

The version syntax is given by the following EBNF:

VERSION ::= NUMBER ( SEPARATOR NUMBER )*.

NUMBER ::= (0|1|2|3|4|5|6|7|8|9)+.

SEPARATOR ::= ‘version-separator’ (which see)
| ‘version-regexp-alist’ (which see).

The NUMBER part is optional if SEPARATOR is a match for an element in ‘version-regexp-alist’.

您可以像这样在 load-path 设置中使用 latest-file-version 函数:

(add-to-list 'load-path (latest-file-version "~/.zeroemacs/elpa" "ivy"))

第一个参数是要检查的目录,第二个参数是要检查的版本文件名或目录名的前缀。

(defun latest-file-version (dir prefix)
  "Get the latest version of files in DIR starting with PREFIX.
Only filenames in DIR with the form PREFIX-version are
considered, where the version portion of the filename must have
valid version syntax as specified for `version-to-list'. Raise an
error if no filenames in DIR start with PREFIX or if no valid
matching versioned filenames are found."
  (let* ((vsn-regex (concat "^" prefix "-\(.+\)$"))
         (vsn-entries
          (seq-reduce
           #'(lambda (acc s)
               (if (string-match vsn-regex s)
                   (let* ((m (match-string 1 s))
                          (vsn (condition-case nil
                                   (version-to-list m)
                                 (error nil))))
                     (if vsn
                         (cons (cons m s) acc)
                       acc))
                 acc)) 
           (directory-files dir nil nil t) nil)))
    (if vsn-entries
        (concat (file-name-as-directory dir)
          (cdar (sort vsn-entries
                      #'(lambda (v1 v2)
                          (version<= (car v2) (car v1))))))
      (error "No valid versioned filenames found in %s with prefix \"%s-\""
             dir prefix))))

使用 emacs 27.1 测试。