如何在Emacs中找到名称中包含“directory”的所有变量?
答案 0 :(得分:4)
M-x apropos-variable RET directory
答案 1 :(得分:1)
如果您只想查找包含字符串的所有变量,请查看correct答案。在这里,我以(<variable> . <value>)
形式创建了对列表。
mapatoms
是一个地图式函数,用于对obarray
进行操作,该变量包含Emacs使用的所有符号。prin1-to-string
返回一个字符串,其中包含对象的打印表示。string-match
在字符串中找到正则表达式,如果找不到则返回index或nil。push
将元素插入到列表的头部。remove-if
相当于倒置filter mapcar
是一个普通的map函数boundp
返回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)))