您的位置:首页 > 其它

emacs lisp(elisp)程序像脚本一样运行

2014-01-22 19:03 211 查看
elisp程序的运行被称为取值,一般的过程是打开一个emacs编辑器,然后用快捷键C-x C-e 调用函数eval-current-buffer运行当前buffer里的elisp程序。

emacs 22以后支持#!/usr/bin/emacs --script像脚本一样执行elisp程序,这样就没必要打开一个emacs编辑器后再执行了。

下面是一个程序实例,见我的GitHub链接

#!/usr/bin/emacs --script
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NAME
;;    kit.el   ---- an example elisp script, which is
;;             used to calculate intern salary.
;;
;; USAGE
;;   emacs -Q --script kit.el iday
;;   ./kit.el iday
;;
;; NOTE
;; #!/  shebang works for emacs22 and later.
;;
;; AUTHOR
;;   Aborn Jiang (aborn.jiang@gmail.com)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;(print argv)
(message "input argument are %s" argv)
(message "system-type:%s system-name:%s" system-type system-name)

(defun cal-salary (iday)
"Calculate the salary for intern in shanghai"
(interactive "p")
(setq total (* iday 180))
(if (> total 800)
(setq value (+ 800 (* 0.8 (- total 800)))
tvalue  (- total value))      ;; if true
(setq value total                     ;; else part
tvalue 0))
(message "You have worked %d day(s), and salary is %d, tax is %d."  iday value tvalue)
)

(cal-salary (string-to-number (elt argv 0)))


在终端里运行:

emacs -Q --script kit.el 10


或者直接运行

./kit.el 10


结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: