You are on page 1of 2

Emacs配置系统 | Emacs中文网 http://emacser.com/emacs-conf-system.

htm

作者: shell909090

emacs是个超级复杂的程序,尤其在配置问题上。贝壳的emacs要跨越三个环境。环境一,WindowsXP+Emacs23。环境
二,Debian Testing + Xfce4。环境三,CentOS + Ssh。而整个的操作方式,个性设定需要保持一致。因此,引出
一个问题。配置如何设置,跨平台,同步。
首先解决配置的同步问题,贝壳建立了一个svn仓库,用于存储该配置系统。然后在各个系统中co出这个仓库,当有
需要调整时ci就可以保持同步了。 Linux下可以使用ln连接文件,Windows下比较麻烦点,NTFS格式(大多都是NTFS
格式了吧)可以去sysinternals下一个叫做 junction的工具,以建立目录的工具链接,当然,.emacs文件只能手工
拷贝了。
然后是配置的切分问题,如果只有一个文件,即使使用了版本控制,意义也不大。同时,将配置切割成不同的部
分,控制载入过程,也可以跨平台和加速。以下是贝壳的.emacs文件。
?
View Code LISP

1 ;; .emacs profile, written by shell.xu


2
3 ;; load other set
4 (add-to-list 'load-path "~/.emacs.d/")
5 (add-to-list 'load-path "~/.emacs.d/auto-complete/")
6 (add-to-list 'load-path "~/.emacs.d/plugins/")
7 (load "emacs-setup")
8 (load "emacs-redef")
9 (load "emacs-plugin")
10 (cond
11 ((not (boundp 'initial-window-system)) (load "emacs-console"))
12 ((memq initial-window-system '(x w32))
13 (cond
14 ((memq system-type '(windows-nt cygwin)) (load "emacs-win"))
15 ((memq system-type '(gnu/linux)) (load "emacs-linux"))
16 )
17 )
18 )
19 (load "emacs-keymap")

从上可以看出,我们先设定了.emacs.d作为默认加载路径——大多数文件都是放在这里。plugins是各种第三方程序
的安装路径,这样这些程序就无需在各个平台上各自安装一次。而auto-complete单独拆出来纯粹是因为文件太多
了。而后,我们加载了setup,这个文件内定义了emacs的基本配置,redef文件内定义了各种自定义函数和变
量,plugin内控制了需要加载的各个插件和配置。
下面就有点复杂,简单来说,设定无Windows系统的时候加载emacs-console文件,有Windows的情况下,在windows
下加载emacs-win,在linux下加载emacs-linux。这是实现跨平台设置的核心。
最后是keymap,经过上面复杂的设定,按键设置是统一的。
setup文件就不细说了,大家按照自己的习惯设定就好。下面我说几个redef中定义的函数。
?
View Code LISP

1 (defun switch-windows-buffer ()
2 (interactive)
3 (let ((this-buffer (window-buffer)))
4 (switch-to-buffer (window-buffer (next-window (selected-window))))
5 (switch-to-buffer-other-window this-buffer)
6 (other-window 1)
7 )
8)

这个函数的目标是用热键交换两个窗口的位置。如果你经常用C-x 3分栏,并且在两者间跳来跳去的话,有的时候往
往希望两者的位置换一下。通常都是C-x b切换当前的窗口,然后C-x o切到隔壁去再换。这个太繁琐了。
?
View Code LISP

1 (defun popup-term ()
这个函数是用于在当前文件所在路径弹出一个term的。也许有人说了,emacs有term啊。问题是,那个term只能开一
2 (interactive)
个,而且有些东西操作不了。例如你如果想在这个term里面跑aptitude…
3 (apply 'start-process "terminal" nil popup-terminal-command)
注意这个函数里面的popup-terminal-command,这个需要跨平台的,因此在windows和linux下设定各自不同。以下
4 )
是两个典型设定,至于哪个是哪个我想都看得懂。注意console下面没必要搞这个。

1 of 2 2010/6/8 16:56
Emacs配置系统 | Emacs中文网 http://emacser.com/emacs-conf-system.htm

?
View Code LISP

1 (setq popup-terminal-command '("xfce4-terminal"))


2 (setq popup-terminal-command '("cmd" "/c" "start"))

然后我们说plugin,这个文件其实很简单,加载插件,然后设定就好。下面是一部分范例。
?
View Code LISP

1 ;; load template
2 (require 'template)
3 ;;here set the templates directory
4 (setq template-subdirectories '("./" "Templates/" "~/.emacs.d/templates/"))
5 (template-initialize)
6 (setq template-auto-insert t)

这部分范例说明了如何载入template,并且进行设定。
下面是一点细节问题,tool-bar-mode这个设定相信多数人都有做。问题是,针对某些console系统,如果你设定了
(tool-bar-mode -1),立刻会报错。具体的条件是,编译的时候没有加入X的支持。因此,为了兼容性起见,针对
console下就忽略这个设定。
最后,这些设定,推荐进行编译加速,包括所有第三方插件。不编译也是可以载入的,然而记得删除el文件对应的
elc。否则emacs默认载入elc,从而导致你对el文件的修改无效——emacs是不认文件时间的。而显然,每个平台编
译一遍是个脑残的事情。因此,我们需要写一个Makefile。
?
View Code MAKE

1 DEPENDS=auto-complete plugins
2 SOURCES=emacs-console.el emacs-keymap.el emacs-linux.el emacs-redef.el emacs-setup.el emacs-win.el
3
4 build: $(SOURCES) emacs-plugin.el
5 for i in $(DEPENDS); do $(MAKE) -C $$i
$$ build || exit 1; done
6 emacs --batch -f batch-byte-compile $(SOURCES)
7 emacs --batch -l ~/.emacs -f batch-byte-compile emacs-plugin.el
8
9 clean:
10 rm -rf *.elc
11 for i in $(DEPENDS); do $(MAKE) -C $$i
$$ clean || exit 1; done

注意,在每个目录中也要分别写Makefile,不过内容简单多了,基本就是emacs –batch -f batch-byte-compile


*.el而已。而plugin这个文件比较特殊——他必须依赖于所有插件才能工作,所以最后才进行编译。
以上工作,基本完成了一个跨平台的emacs配置系统的组建。当然,如果你高兴向其中加入更多的内容,可以遵循以
上规范进行。

标签:buffer, ede, Emacs, keymap, linux, lisp, mode, plugin, profile, screenshot, se, shell909090,
ssh, term, windows, 安装, 按键, 插件, 编译, 配置, 配色, 配色

相关日志

2 of 2 2010/6/8 16:56

You might also like