gpt4 book ai didi

emacs - 存储在文件中的elisp 代码的结果值?

转载 作者:行者123 更新时间:2023-12-04 19:58:42 25 4
gpt4 key购买 nike

寻找一种如何评估存储在外部文件中的 elisp 代码并将其结果作为函数参数传递的方法。演示我想要实现的示例如下:

;; content of my_template.el
'(this is a list)

;; content of .emacs where result of my_template.el has to be used
(define-auto-insert "\.ext$"
;; bellow is my attempt to retrieve resulting list object
;; but getting nil instead
(with-temp-buffer
(insert-file-contents ("my_template.el"))
(eval-buffer))))

可能正在寻找一个类似于 eval 的函数,除了副作用之外,它还返回最后一个表达式的结果。

任何的想法 ?

最佳答案

使用变量来共享数据更容易也更常见,例如:

;; content of ~/my_template.el
(defvar my-template '(this is a list))

;; content of .emacs where result of my_template.el has to be used
(load-file "~/my_template.el")
(define-auto-insert "\.ext$"
my-template)

更新 函数 eval-file应该做你想做的:
;; content of ~/my_template.el
'(this is a list)

(defun eval-file (file)
"Execute FILE and return the result of the last expression."
(load-file file)
(with-temp-buffer
(insert-file-contents file)
(emacs-lisp-mode)
(goto-char (point-max))
(backward-sexp)
(eval (sexp-at-point))))

(eval-file "~/my_template.el")
=> (this is a list)

更新二 : 不计算最后一个表达式两次
(defun eval-file (file)
"Execute FILE and return the result of the last expression."
(eval
(ignore-errors
(read-from-whole-string
(with-temp-buffer
(insert-file-contents file)
(buffer-string))))))

(eval-file "~/my_template.el")
=> (this is a list)

关于emacs - 存储在文件中的elisp 代码的结果值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30568113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com