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

标签 emacs elisp emacs24 dot-emacs

寻找一种如何评估存储在外部文件中的 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/

相关文章:

emacs24 - 如何删除 Emacs(24 版以上)中已安装的软件包?

emacs - 禁用有关加载路径中的 emacs.d 的警告

emacs - Emacs/Swank/Paredit for Clojure 的温和教程

emacs - 将 emacs 与 paredit 一起使用时,如何将结束括号添加到 clojure 表单?

emacs - 让 Emacs 对缩进不那么咄咄逼人

lisp - 符号和名称不同吗?

emacs evil-mode 命令模式的简单绑定(bind)

emacs - 如何使用clojure doc功能?

emacs - 在 emacs 中为文件系统以外的东西实现一个 dired 接口(interface)?

c - 让 gdb 与 emacs 24 一起工作