【问题标题】:List all Emacs variables containing regexp列出所有包含 regexp 的 Emacs 变量
【发布时间】:2013-03-06 02:19:17
【问题描述】:

如何在 Emacs 中找到名称中包含“目录”的所有变量?

【问题讨论】:

    标签: variables emacs elisp


    【解决方案1】:

    M-x apropos-variable RET directory

    【讨论】:

    • 但是要注意前缀参数对这个函数的影响。请参阅C-h f apropos-variableC-h f user-variable-p(后者是一个变量需要匹配才能默认显示的谓词)。
    【解决方案2】:

    如果您只想查找包含字符串的所有变量,请查看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)))
    

    参考文献

    【讨论】:

      猜你喜欢
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 2020-08-17
      • 2013-08-12
      • 2022-09-23
      • 1970-01-01
      • 2021-12-26
      相关资源
      最近更新 更多