You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Common Lisp中获取定义某函数或宏的文件名称?

Common Lisp 获取函数/宏定义所属文件最后写入时间方案

核心逻辑

所有方案均遵循两个通用步骤:

  1. 查询目标函数/宏的定义所在源文件路径
  2. 调用ANSI Common Lisp标准内置函数file-write-date获取该文件的最后写入时间,返回自1900年1月1日0时起的秒级时间戳

无SWANK依赖的可移植实现方案

该方案通过适配各主流实现的原生接口实现,无需引入第三方依赖:

(defun get-function-def-file-write-time (func-designator)
  "返回函数/宏定义所属文件的最后写入时间,无有效路径时返回nil"
  (let ((source-path 
          #+sbcl (sb-introspect:definition-source-pathname
                   (sb-introspect:find-definition-source func-designator))
          #+ccl (car (ccl:find-definition-location func-designator))
          #+clisp (sys::function-source-file func-designator)
          #+ecl (ext:function-source-file func-designator)
          #-(or sbcl ccl clisp ecl) nil))
    (when (and source-path (probe-file source-path))
      (file-write-date source-path))))

商业IDE专属实现方案

LispWorks

LispWorks提供dspec模块原生支持查询定义位置,实现代码如下:

(defun lw-get-def-file-mtime (func)
  (let ((loc (dspec:definition-source-location func)))
    (when loc
      (file-write-date (dspec:location-pathname loc)))))

Allegro CL (ACL)

ACL内置excl:find-source-location接口直接返回定义文件路径:

(defun acl-get-def-file-mtime (func)
  (let ((path (excl:find-source-location func)))
    (when path
      (file-write-date path))))

SWANK环境简化方案

如果运行环境已加载SWANK(Slime、Sly等编辑器集成环境默认自带),可直接调用SWANK通用接口实现全支持SWANK的实现适配:

(defun swank-get-def-file-mtime (func)
  (let* ((def-info (swank:find-definition-for-thing func))
         (file-path (when (eq (car def-info) :location)
                      (getf (cdr def-info) :file))))
    (when file-path
      (file-write-date file-path))))

内容的提问来源于stack exchange,提问作者Ashok Khanna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 09:54:05