Run elisp program without Emacs?

匿名 (未验证) 提交于 2019-12-03 09:52:54

问题:

elisp is a good language, I find it can handle all kind of jobs, but can I use it like a shell script?

i.e. execute some *.el files from the console, without launching Emacs. Or launch Emacs, but don't enter interactive mode.

回答1:

You can most definitely run elisp scripts in Emacs without starting the editor interface.

Here are the notes I've made/copied from a few extremely useful Q&As on the subject here at S.O. (and the following two in particular).

;;;; Elisp executable scripts  ;; --batch vs --script ;; M-: (info "(emacs) Initial Options") RET ;; M-: (info "(elisp) Batch Mode") RET  ;; Passing additional command-line arguments to Emacs: ;; https://stackoverflow.com/questions/6238331/#6259330 ;; ;; 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 set "argv" to nil at the end of the script ;; (to prevent Emacs from visiting the non-option arguments as files). ;; ;; #!/bin/sh ;; ":"; exec emacs --no-site-file --script "$0" -- "$@" # -*-emacs-lisp-*- ;; (print (+ 2 2)) ;; (setq argv nil) ;; always end with this  ;; Processing with STDIN and STDOUT via --script: ;; https://stackoverflow.com/questions/2879746/#2906967 ;; ;; #!/usr/local/bin/emacs --script ;; ;;-*- mode: emacs-lisp;-*- ;; ;; (defun process (string) ;;   "just reverse the string" ;;   (concat (nreverse (string-to-list string)))) ;; ;; (condition-case nil ;;     (let (line) ;;       ;; commented out b/c not relevant for `cat`, but potentially useful ;;       ;; (princ "argv is ") ;;       ;; (princ argv) ;;       ;; (princ "\n") ;;       ;; (princ "command-line-args is" ) ;;       ;; (princ command-line-args) ;;       ;; (princ "\n") ;; ;;       (while (setq line (read-from-minibuffer "")) ;;         (princ (process line)) ;;         (princ "\n"))) ;;   (error nil)) 

Emacs aside, the only other elisp interpreter/compiler I'm aware of is Guile. If you're keen on general coding in elisp, that should be worth a look (especially if performance is a concern).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!