Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Emacs 文件中存储的elisp代码的结果值?_Emacs_Elisp_Emacs24_Dot Emacs - Fatal编程技术网

Emacs 文件中存储的elisp代码的结果值?

Emacs 文件中存储的elisp代码的结果值?,emacs,elisp,emacs24,dot-emacs,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

寻找如何计算存储在外部文件中的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)
更新函数
评估文件
应执行您想要的操作:

;; 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)

不要从字符串中读取。从缓冲区读取

(卸载和返回(文件和可选msgp)
“加载文件。返回上次sexp读取的值。”
(交互式“fFile:\np”)
(let*((sexp)(使用当前缓冲区(查找文件noselect文件)
(转到字符(最小点))
(读取(当前缓冲区)))
(val(忽略错误(eval sexp)))
(prog1 val(当msgp(消息“值:%S”val())))时)

谢谢。我已经有了一个通过变量来解决这个问题的想法,只是好奇它是否可以用一种更优雅的函数方式来解决。@DavidUnric这是我第一次听说文件的结果,最后一个表达式的结果不是很有用或可实现的,所以我想没有现成的函数来做你想做的事情,虽然我知道一种很尴尬的方法,我没有使用(e)lisp,但在ruby/python中,它有时可以方便地评估文件,其中返回值是最后一个表达式/显式返回的结果。我认为它的可靠性不会比加载该文件差很多。谢谢你的更新回答,尽管运行整个文件时发现有点奇怪,然后重新运行最后一个表达式。如果最后一个表达式会产生一些副作用,则可能会导致意外的问题。如果没有其他人会提出更好的解决方案,我会将您的答案标记为已接受。我已更新了我的答案,
从整个字符串读取
用于将字符串转换为sexp并对其进行
求值
求值,现在无需重新计算最后一个表达式。(a)您应该阅读
求值缓冲区
的文档,如果没有额外的参数,输出将被丢弃(b)
define auto insert
的第二个参数需要是一个函数,您可以对匿名函数使用lambda表单。您对eval buffer的看法是正确的,我正在寻找一个返回上一个表达式结果的替代版本。顺便说一句,在这种情况下,如何帮助lambda作为第二个参数-即评估外部elisp文件?我的意思是您可能需要将调用代码包装在
(lambda()…)
中,以便它是一个匿名函数。