在没有Emacs的情况下运行elisp程序?

问题描述 投票:19回答:1

elisp是一种很好的语言,我发现它可以处理所有类型的工作,但是我可以像shell脚本一样使用它吗?

即从控制台执行一些* .el文件,而不启动Emacs。或者启动Emacs,但不要进入交互模式。

emacs elisp
1个回答
22
投票

您绝对可以在Emacs中运行elisp脚本而无需启动编辑器界面。

以下是我在S.O.的主题上从一些非常有用的问答制作/复制的笔记。 (特别是以下两个)。

大部分此类信息以及更多信息也包含在以下优秀概述中,建议阅读:

;;;; Elisp executable scripts

;; --batch vs --script
;; M-: (info "(emacs) Initial Options") RET
;; M-: (info "(elisp) Batch Mode") RET

;; Processing command-line arguments (boiler-plate)
;; http://stackoverflow.com/questions/6238331/#6259330 (and others)
;;
;; For robustness, it's important to both pass '--' as an argument
;; (to prevent Emacs from trying to process option arguments intended
;; for the script), and also to exit explicitly with `kill-emacs' at
;; the end of the script (to prevent Emacs from carrying on with other
;; processing, and/or visiting non-option arguments as files).
;;
;; #!/bin/sh
;; ":"; exec emacs -Q --script "$0" -- "$@" # -*-emacs-lisp-*-
;; (pop argv) ; Remove the "--" argument
;; ;; (setq debug-on-error t) ; if a backtrace is wanted
;; (defun stdout (msg &optional args) (princ (format msg args)))
;; (defun stderr (msg &optional args) (princ (format msg args)
;;                                           #'external-debugging-output))
;; ;; [script body here]
;; Always exit explicitly. This returns the desired exit
;; status, and also avoids the need to (setq argv nil).
;; (kill-emacs 0)

;; Processing with STDIN and STDOUT via --script:
;; https://stackoverflow.com/questions/2879746/#2906967
;;
;; #!/bin/sh
;; ":"; exec emacs -Q --script "$0" -- "$@" # -*-emacs-lisp-*-
;; (pop argv) ; Remove the "--" argument
;; (setq debug-on-error t) ; if a backtrace is wanted
;; (defun stdout (msg &optional args) (princ (format msg args)))
;; (defun stderr (msg &optional args) (princ (format msg args)
;;                                           #'external-debugging-output))
;; (defun process (string)
;;   "Reverse STRING."
;;   (concat (nreverse (string-to-list string))))
;;
;; (condition-case nil
;;     (let (line)
;;       (while (setq line (read-from-minibuffer ""))
;;         (stdout "%s\n" (process line))))
;;   (error nil))
;;
;; ;; Always exit explicitly. This returns the desired exit
;; ;; status, and also avoids the need to (setq argv nil).
;; (kill-emacs 0)

除了Emacs之外,我所知道的唯一其他elisp解释器/编译器是Guile。如果你热衷于elisp中的通用编码,那么值得一看(特别是如果考虑性能的话)。

© www.soinside.com 2019 - 2024. All rights reserved.