elisp 是一门很好的语言,我发现它可以处理所有类型的工作,但我可以像使用 shell 脚本一样使用它吗?
即从控制台执行一些 *.el 文件,而不启动 Emacs。或者启动 Emacs,但不要进入交互模式。
您绝对可以在 Emacs 中运行 elisp 脚本,而无需启动编辑器界面。
以下是我在 SO(尤其是以下两个)关于该主题的一些非常有用的问答中制作/复制的笔记。
以下出色的概述也涵盖了许多此类信息以及更多信息,建议阅读:
;;;; 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 中的一般编码,那应该值得一看(尤其是在考虑性能的情况下)。