Emacs Lisp:如何避免插入重复的列表项?

Emacs Lisp:如何避免插入重复的列表项?,emacs,elisp,Emacs,Elisp,如何在Emacs Lisp中检查字符串是否已在列表中?我需要检查某个路径字符串是否已在exec path中,如果没有,则将其添加到该列表中。在添加之前,该函数将自动进行检查 (setq a '(1 2 3)) (add-to-list 'a 4) (add-to-list 'a 3) 将导致a等于(4 1 2 3) 从Emacs 26 C-h f添加到列表中: (add-to-list LIST-VAR ELEMENT &optional APPEND COMPARE-FN) Add ELE

如何在Emacs Lisp中检查字符串是否已在列表中?我需要检查某个路径字符串是否已在
exec path
中,如果没有,则将其添加到该列表中。

在添加之前,该函数将自动进行检查

(setq a '(1 2 3))
(add-to-list 'a 4)
(add-to-list 'a 3)
将导致
a
等于(4 1 2 3)

从Emacs 26 C-h f
添加到列表中

(add-to-list LIST-VAR ELEMENT &optional APPEND COMPARE-FN) Add ELEMENT to the value of LIST-VAR if it isn’t there yet. The test for presence of ELEMENT is done with ‘equal’, or with COMPARE-FN if that’s non-nil. If ELEMENT is added, it is added at the beginning of the list, unless the optional argument APPEND is non-nil, in which case ELEMENT is added at the end. The return value is the new value of LIST-VAR. This is handy to add some elements to configuration variables, but please do not abuse it in Elisp code, where you are usually better off using ‘push’ or ‘cl-pushnew’. If you want to use ‘add-to-list’ on a variable that is not defined until a certain package is loaded, you should put the call to ‘add-to-list’ into a hook function that will be run only after loading the package. ‘eval-after-load’ provides one way to do this. In some cases other hooks, such as major mode hooks, can do the job. (添加到列表list-VAR元素和可选的追加比较-FN) 如果LIST-VAR的值还不存在,则将元素添加到该值中。 使用“equal”或 比较-FN,如果它不是零。 如果添加了元素,则会将其添加到列表的开头, 除非可选参数APPEND为非nil,在这种情况下 元素添加到末尾。 返回值是LIST-VAR的新值。 这便于向配置变量添加一些元素, 但请不要在Elisp代码中滥用它,通常情况下 最好使用“push”或“cl pushnew”。 如果您想对不可用的变量使用“添加到列表” 在加载某个包之前,您应该将 将“添加到列表”调用到仅运行的挂钩函数中 装完包裹后。”“加载后评估”提供了一种 这样做。在某些情况下,其他挂钩,如主模式挂钩, 我能胜任这项工作。 除了大约,还有:

(需要“cl-lib”)
(setq字符串列表“(“一”“二”“三”))
(新的字符串“两个”列表:test#'string=)
⇒ (“一”“二”“三”)
(新的字符串“零”列表:test#'string=)
⇒ (“零”“一”“二”“三”)
在您的案例中需要该参数,因为默认情况下,
cl-pushnew
用于比较,并且它不会将具有相同内容的两个字符串视为相等。()也行

(eql“一些字符串”“一些字符串”)
⇒ 无
(string=“some string”“some string”)
⇒ T
(等于“某字符串”“某字符串”)
⇒ T
从Emacs 26 C-h f
cl推送新的

cl-pushnew is a Lisp macro in ‘cl-lib.el’. (cl-pushnew X PLACE [KEYWORD VALUE]...) (cl-pushnew X PLACE): insert X at the head of the list if not already there. Like (push X PLACE), except that the list is unmodified if X is ‘eql’ to an element already on the list. Keywords supported: :test :test-not :key cl pushnew是“cl lib.el”中的Lisp宏。 (新X位置[关键字值]…) (新X位置):如果列表的开头没有X,则在列表的开头插入X。 Like(push X PLACE),但如果X是'eql'to,则列表不会被修改 列表中已存在一个元素。 支持的关键字::测试:测试不:键