EMACS-DOCUMENT

=============>集思广益

构建基于linux内核的纯Emacs环境

这项实验的目的是为了体验一下构建在linux内核上的纯Emacs环境用起来怎么样.

唯一需要的外部工具就是 mount.

该"root_fs_emacs"可以挂载为普通分区或烧录到live CD上,但在本次试验中,我们使用User Mode Linux 来启动它

1 1- 初始化一个至少拥有150MB的根文件系统(root_fs)

cd ~/uml
dd if=/dev/zero of=root_fs_emacs bs=1k count=200k
yes y|mke2fs root_fs_emacs
mkdir /emacs 
mount -o loop root_fs_emacs /emacs
cd /emacs
ln -s .  emacs     # we create this link to simplify config --prefix of emacs 
cp -a /dev dev     # we boldly copy the whole /dev
mkdir etc sbin tmp # some other directories not installed by emacs
cat >etc/fstab <<EOF
/dev/ubd0 / ext2 defaults  0 1
EOF

2 2- 静态编译Emacs,不需要支持X

cd ~/src
tar jxvf emacs-21.3.tar.bz2
cd emacs-21.3
CFLAGS=-static LDFLAGS=-static ./configure --without-x --prefix=/emacs
make && make install

3 3- 将Emacs安装为/sbin/init

cd /emacs
ln bin/emacs sbin/init
cat >.emacs <<EOF
(message "init starting")
(setq auto-save-interval 0)
(defun shutdown ()
  (interactive)
  (when (yes-or-no-p "Really shut down the system? ")
    ;; actually, kill-emacs signals emacs ie. init, which makes linux panic.
    (kill-emacs)))
(global-set-key "\C-x\C-c" 'shutdown)
(global-set-key "^\"  'keyboard-quit) ;; strangely, C-g does not work.
(call-process "/bin/mount" nil "*log*" t "-n" "-o" "rw,remount" "/")
(if (file-exists-p "/etc/mtab") (delete-file "/etc/mtab"))
(call-process "/bin/mount" nil "*log*" t "-f" "/dev/ubd0" "/")
(message "init done")
EOF

4 4- 静态编译mount命令

cd ~/src
tar jxvf util-linux-2.12a.tar.bz2 
cd util-linux-2.12a
CFLAGS=-static LDFLAGS=-static ./configure
make && install -m 755 mount/umount mount/mount /emacs/bin/

5 5- 启动linux

cd ~/uml
umount /emacs
linux ubd0=root_fs_emacs

现在你可以通过 M-x eshell RET 运行emacs shell了. 在emacs shell中输入 ls -l RET 得到如下结果:

File Edit Options Buffers Tools Help 
Welcome to the Emacs shell

/ # ls -l
total 21
drwxr-xr-x   2 0        0            1024 Jul 26 08:42 bin
drwxr-xr-x   1 0        0               0 Jan  1  1970 dev
lrwxrwxrwx   1 0        0               1 Jul 26 08:11 emacs -> .
drwxr-xr-x   2 0        0            1024 Jul 26 09:20 etc
drwxr-xr-x   2 0        0            2048 Jul 26 08:11 info
drwxr-xr-x   3 0        0            1024 Jul 26 08:11 libexec
drwx------   2 0        0           12288 Jul 26 08:10 lost+found
drwxr-xr-x   3 0        0            1024 Jul 26 08:10 man
drwxr-xr-x   2 0        0            1024 Jul 26 08:11 sbin
drwxr-xr-x   3 0        0            1024 Jul 26 08:10 share
drwxr-xr-x   2 0        0            1024 Jul 26 09:15 tmp
/ # 

--1-:---F1  *eshell*          (EShell)--L20--All---------------------

当然,emacs缺少很多系统调用(没有实现为elisp primitives), 所以,看样子很难在Emacs中完成所有的事情, 但这个实验只是个开始而已.

另一个更可行的替代是使用一个支持FFI即可移植的HemLock的Common-Lisp实现来代替emacs.

6 Emacs as shell

Emacs可以很容易的当作shell来用:

echo    /usr/bin/emacs >> /etc/shells
chsh -s /usr/bin/emacs  GOODUSER

echo    '(setenv "SHELL" "/bin/bash")' >> ~GOODUSER/.emacs
# in case the user wants to use M-x shell
# [ I use rather: (setenv "SHELL" "/usr/bin/clisp") ]

echo    '(eshell)' >> ~GOODUSER/.emacs
# to launch eshell automatically.
# One could use: (dired default-directory) instead...

su - GOODUSER
# Hosanna!