ELisp编程一:运行elisp的各种方式
来源:互联网 发布:新闻视频下载软件 编辑:程序博客网 时间:2024/06/02 16:58
使用Emacs这么多年,越用功能越多,开发C++,Java,HTML,JavaScript,访问MySQL,CMake编辑。上新闻组,收Gmail,Emacs Lisp语言是其中的灵魂。
自己开发或者修改emacs的扩展模块就需要掌握ELisp,而且Lisp既然是人工智能语言,学会它会让自己的思维上一个层次。有这么多好处,还等什么呢?立刻行动起来吧。
执行最后一个S表达式
从最简单的如何在Emacs上执行加法开始:
输入M-x:shell进入shell终端,然后输入
(+ 2 3)
将光标放在最后一个括号后面,按下C-x C-e ,可以看到Mini-buffer上面显示结果5。函数实际上是这个
M-x:
eval-last-sexp
如果有多个元素的话,写法可以换行,写成如下形式:
'(rose violet daisy buttercup)
注意前面的' 符号是告诉Emacs不要对这个list求值,仅仅是原样输出'符号后面的list,因此C-x C-e的结果就是
(rose violet daisy buttercup)
运行buffer中所有的lisp
eval-buffer函数
可以通过M-x运行无参的eval-buffer函数,但是很遗憾,什么也不显示,因为这个函数的可选参数如果不设置,就不会输出任何东西。
所以可以这么改
运行指定buffer的lisp,输出到指定buffer
在*shell*中用C-x C-e执行这个语句
(eval-buffer "add.el" (get-buffer-create "output"))
(defun add2 (x) (+ 2 x))(print (add2 8))
c-h f查看eval-buffer函数, 可以看到,这里PRINTFLAG我们传递是期望输出到的buffer名称,当然这个output buffer是创建的。
eval-buffer is an interactive built-in function in `C source code'.(eval-buffer &optional BUFFER PRINTFLAG FILENAME UNIBYTEDO-ALLOW-PRINT)Execute the current buffer as Lisp code.When called from a Lisp program (i.e., not interactively), thisfunction accepts up to five optional arguments:BUFFER is the buffer to evaluate (nil means use current buffer).PRINTFLAG controls printing of output: A value of nil means discard it; anything else is stream for print.FILENAME specifies the file name to use for `load-history'.UNIBYTE, if non-nil, specifies `load-convert-to-unibyte' for this invocation.DO-ALLOW-PRINT, if non-nil, specifies that `print' and related functions should work normally even if PRINTFLAG is nil.This function preserves the position of point.[back][forward]
运行当前buffer的lisp代码,并输出到output buffer
上面的方式还是用起来不方面,在init.el中添加一个函数eval-buffer2,就可以执行当前buffer的elisp,并输出到output buffer中
;; elisp(defun eval-buffer2 () (interactive) (eval-buffer nil (get-buffer-create "output")))
在eshell中运行
在eshell中执行elip更加方便。比如
(+ 1 2)直接回车就可以运行了。
故命思意,这是elisp的shell。
运行el文件
如何像执行shell脚本一样运行el文件呢?
这是一个很实用的功能,实际上也是我的一个主要目的,这么强大的语言,用好了,比shell脚本方便多了,而且性能也搞。甚至可以代替Java编写程序。
现在我的test.el文件内容如下:
(defun add2 (x) (+ 2 x))(print (add2 8))
然后执行下面的命令:
emacs --no-site-file --script /home/chenshu/test.el
结果显示10
注意上面的命令在服务器上运行没有问题,
在Ubuntu Desktop上需要安装并使用emacs24-nox
apt-get install emacs24-nox
在ArchLinux上如下安装
sudo pacman -Syu emacs-nox
- ELisp编程一:运行elisp的各种方式
- ELisp编程一:运行elisp的各种方式
- Elisp 编程
- ELisp编程十三:宏一
- ELisp编程十一:正则表达式一
- ELisp编程九:循环
- ELisp编程四:文件操作
- ELisp编程六:定义变量
- ELisp编程七:创建函数
- ELisp编程八:条件语句
- ELisp编程十:求最大值
- ELisp编程十三:宏二
- ELisp编程十四:宏三
- elisp的MSN通信程序
- elisp 基础
- eLisp Basics
- elisp教程
- ELisp编程二:Atom,Symbol,空格,注释
- 【Jstl】java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
- jquery自学系列三:选择器(下)
- VS2010常见问题: 'back_inserter': identifier not found
- ARM基础知识 -- ELF映像文件
- PHP安全模式详解(PHP5.4安全模式将消失)
- ELisp编程一:运行elisp的各种方式
- 怎么实现一个线程安全的单例模式呢?
- BMP与IplImage相互转换
- 发布Web Service 到 Win7 的 IIS 7.0
- 有关字符集的解释
- /proc/sys/vm参数(转)
- jbpm3.2.2 transition 多路选择
- configure integration Hibernate3,Spring3,Struts2,JPA
- 网络的七层模型简介