Emacs Lisp:求值字符串列表

4

我虽然对elisp不熟悉,但我希望在我的.emacs文件中加入一些内容。

我正在尝试定义一些路径,但是在创建路径列表方面遇到了一些问题(更具体地说是为YaSnippet设置列表时)。

当我评估列表时,我得到一个符号名称的列表(而不是yassnippet需要的符号值)。

我已经让代码工作了,但感觉有更好的方法可以实现吗?

这是可行的代码:

;; some paths
(setq my-snippets-path "~/.emacs.d/snippets")
(setq default-snippets-path "~/.emacs.d/site-lisp/yasnippet/snippets")

;; set the yas/root-directory to a list of the paths
(setq yas/root-directory `(,my-snippets-path ,default-snippets-path))

;; load the directories
(mapc 'yas/load-directory yas/root-directory)

1
你要做的是使用两个变量构建一个新列表。可以使用list函数来实现,就像@jpkotta所描述的那样,尽管这不是列表的评估,而是列表的构建。 更有效和推荐的配置片段目录的方法是将一个字符串列表分配给变量yas-snippet-dirs。 Elisp Cookbook包含一些有关列表结构的有用信息。 - raju-bitter
2个回答

6

如果您要评估一个字符串列表,结果取决于列表项的值。测试最佳方法是启动ielm repl (M-x ielm),然后输入:

ELISP> '("abc" "def" "ghi")
("abc" "def" "ghi")

引用的字符串列表将被评估为列表值。如果您将列表的值存储在变量中,然后对变量进行评估,则ELisp会抱怨函数abc未知。
ELISP> (setq my-list '("abc" "def" "ghi"))
("abc" "def" "ghi")

ELISP> (eval my-list)
*** Eval error ***  Invalid function: "abc"

对于 yasnippet 目录配置,你只需要设置 yas-snippet-dir 即可,例如:

(add-to-list 'load-path
              "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)

(setq yas-snippet-dirs
      '("~/.emacs.d/snippets"            ;; personal snippets
        "/path/to/yasnippet/snippets"    ;; the default collection
        "/path/to/other/snippets"        ;; add any other folder with a snippet collection
        ))

(yas-global-mode 1)

编辑:
使用yas/root-directory已经被弃用。来自yasnippet.el文档的说明。

`yas-snippet-dirs'

The directory where user-created snippets are to be
stored. Can also be a list of directories. In that case,
when used for bulk (re)loading of snippets (at startup or
via `yas-reload-all'), directories appearing earlier in
the list shadow other dir's snippets. Also, the first
directory is taken as the default for storing the user's
new snippets.

The deprecated `yas/root-directory' aliases this variable
for backward-compatibility.

4

我想您需要

(setq yas/root-directory (list my-snippets-path default-snippets-path))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接