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

问题描述

我使用包后缀 -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 的文档包括它接受的内容的描述:

版本语法由以下 EBNF 给出:

版本 ::= 编号(分隔符编号)*。

数字 ::= (0|1|2|3|4|5|6|7|8|9)+。

SEPARATOR ::= ‘version-separator’(见)
| ‘version-regexp-alist’(见)。

如果 SEPARATOR 与 ‘version-regexp-alist’ 中的元素匹配,则 NUMBER 部分是可选的。

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

(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 测试。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...