【发布时间】:2013-03-06 02:19:17
【问题描述】:
如何在 Emacs 中找到名称中包含“目录”的所有变量?
【问题讨论】:
如何在 Emacs 中找到名称中包含“目录”的所有变量?
【问题讨论】:
M-x apropos-variable RET directory
【讨论】:
C-h f apropos-variable 和C-h f user-variable-p(后者是一个变量需要匹配才能默认显示的谓词)。
如果您只想查找包含字符串的所有变量,请查看correct 答案。在这里,我以(<variable> . <value>) 的形式创建了对列表。
mapatoms 是一个映射式函数,用于对 obarray 进行操作,该变量包含 Emacs 使用的所有符号。prin1-to-string 返回一个带有对象打印表示的字符串。string-match 在字符串中找到一个正则表达式,如果没有找到则返回 index 或 nil。push 将元素就地插入到列表的头部。remove-if 相当于倒置filter
mapcar 是一个普通的map 函数boundp 如果变量的值不为 void,则返回 t。symbol-value 返回变量的值。(let ((matching-variables
(let ((result '()))
;; result will contain only variables containing "directory"
(mapatoms (lambda (variable)
(let* ((variable-string (prin1-to-string variable))
(match (string-match "directory" variable-string)))
(if match
(push variable result)))))
result)))
;; returns list of pairs (variable-name . variable-value)
(remove-if #'null
(mapcar (lambda (variable)
(if (boundp variable)
(cons variable (symbol-value variable))
nil))
matching-variables)))
【讨论】: