Linux 版 (精华区)

发信人: netiscpu (说不如做), 信区: Linux
标  题: EMACSulation
发信站: 哈工大紫丁香 (Mon May 17 13:23:54 1999), 转信

"Linux公报...让Linux更富魅力!" 

---------------------------------------------------------------------------

-----

EMACSulation

作者 Eric Marsden, emarsden@mail.dotcom.fr 译者 sams 

此栏专注于如何最好地使用Emacs.我计划在每篇文章中提交一个Emacs扩展方案, 以提

高你的生产力,让生活变得更美好. 

---------------------------------------------------------------------------

-----

定制Emacs

一般应用程序都有一个配置文件让你设置一些变量值.而Emacs走得更远, 用户能定制系

统的几乎所有特性.这种弹性源于其大部分系统以其扩展语言写成. 第一版Emacs使用了

一种神秘的串处理语言 TECO (觉得 Perl 神秘么? 看看 TECO), 集成了 PDP-10 支持

代码; 之后的版本使用了Lisp作扩展语言,其中某些部件(Lisp解释器和显示代码)以C写

成. 

这种扩展性对Emacs十分重要.用户可试验改进系统,如果觉得不错, 还能传给其他朋友

;大受欢迎的还可以集成到核心分发版本中. MIT 的现已不存在的 Lisp机 将这种开放

性扩展到操作系统级, 它用Lisp写成(并且运行在 定制的让 Lisp 高效运行的硬件上)

. Lisp机的用户对其机器的控制程度比如今的Linux用户高的多. Xerox 研究中心的 S

malltalk 系统的定制深度则与其相似. 

实际上,开发Emacs(MIT流派)和Unix及C(新泽西流派)的理念有着深刻差异. Richard G

abriel 曾写过一篇精彩的文章 更差即更好 对此作出比较,摘录如下: 

分别来自MIT和Berkeley(致力于Unix)的两位名人曾在一起探讨操作系统. 来自MIT的这

位因ITS(MIT智能实验室操作系统)而闻名,在阅读了Unix源码后, 他对Unix如何解决指

令指针丢失(PC loser-ing)问题颇感兴趣. 所谓PC loser-ing问题当一个用户程序调用

系统函数进行冗长的操作而有拥有重要状态时发生, 比如IO缓冲.当操作时有中断出现

,必须保存用户程序的状态. 由于系统例程的调用往往是一个指令完成,用户程序的程序

计数器不能完整获得进程状态. 此时系统例程要么退回要么继续向前. 正确的做法是退

回并恢复用户程序计数器 (PC) 至调用系统例程的指令以便于中断后用户程序的恢复并

重进入系统例程. 由于PC被强制进入loser模式(MIT的用户钟爱于称其loser),因此称为

PC loser-ing问题. 

MIT的这位没看见任何处理这种情况的代码,于是询问新泽西的这位问题是如何处理的.

 新泽西的这位说Unix人明白这个问题,不过解决方案是系统例程总是会完成, 而有时会

返回错误代码表明系统例程未完成其动作. 正确的用户程序就应该检查此错误代码来决

定是否再次使用此系统例程. MIT的这位不喜欢这种方法,因为这不是'正确的行动'. 

新泽西的这位说Unix方案是对的,因为Unix的设计思想就是简朴,而'正确的行动'过于复

杂. 此外,程序员可以轻易插入额外的测试和循环. MIT的这位指出虽然其实现简单但其

功能的接口却复杂化了. 新泽西这位说Unix已经做了正确的选择:实现的简单性重要于

接口的简单性. 

让我们回到更具体的问题.经典的[X]Emacs定制方法是在你主目录中的 .emacs 文件中

写个简单的Emacs Lisp表达式.此表达式可以设置变量,调用函数,或装载库:

    

    ;; 设置变量值.'t'表示为真,'nil'表示为假

    (setq dired-listing-switches "-alF")

    (setq tab-width 4)

    (setq line-number-mode t)

    (setq global-font-lock-mode t)

    (setq next-line-add-newlines nil)

    

    ;; 调用函数在模式行显示时间

    (display-time)

    

    ;; 装载Emacs Lisp库并调用其初始化函数

    (require 'jka-compr)

    (auto-compression-mode 1)

 

这种语法将会激怒那些人们,他们嘲笑存在 "Lots of irritating spurious parenthe

ses",但在配置文件中使用完整编程语言有着真正重要的优点. 它允许你测试局部设置

的特性,例如:

    (if (file-exists-p "/bin/bash")

       (setq explicit-shell-file-name "/bin/bash"))

 

这使Emacs在 *shell* 缓冲区使用bash,如果它存在的话. 在配置中使用真正语言的另

一原因是它避免了各有自己混乱语法的各类配置文件的无限增长 (想一想~/.Xdefault

s,窗口管理设置文件,~/.inputrc,~/.procmailrc,等等). 希望扩展使用内嵌描述语言

 Guile 将解决问题. 最后的原因是真正的编程语言给予了用户充分权利. 实际上,在对

Emacs的 最初的技术报告 中, Richard Stallman 将其作为了一个重要的目标: 

当大量非技术工作人员使用可编程的编辑器, 他们将一日复一日的不断被吸引开始编程

.这将大大有助于计算机能力,特别对那些作秘书的, 往往被教导他们没有数学能力,甚

至一刻都不敢想还能编程. 但是如果他们并不知道正在学的就是编程,他们就会敢于学

下去! 定制

最近版本的[X]Emacs包括了叫做Customize的包, 它使你不用写Lisp程序就能调整Emac

s到你喜欢的那样.定制包由 Per Abrahamsen开发, 他也是流行的 Auc-TeX 包的作者.

 它让用户能看见Emacs中所有用户可配置变量的列表并按自己的意愿修改. 可以在Hel

p -> Customize 菜单找到它.XEmacs中它看起来如下: 

 

定制包需要Emacs Lisp库以声明它输出的用户可修改变量. 变量类型是布尔型或整型或

字符串或几个选项中的一个, 几个变量可以设成一组,使信息具有层次. 一接到请求则

修改屏幕自动生成.这是个优雅的设计, 因为手工对成千的对话框编程的工作量是相当

大的,并且需要大量存储器. 用户的修改存在文件中,[X]Emacs初始化时将读取. 

定制包不好的一个方面是它给出了作同一件事的两种方法. 想要超越使用图形界面作简

单定制的用户不得不面对全新的途径,甚至知难而退. 要么使简单的事更简单,要么使复

杂的成为可能, 在两者之间跳来跳去是困难的, 而且古谚说: 如果建立一个傻子都能用

的系统,那么也只有傻子将会去用. 

键捆绑

键捆绑是定制中格外不可靠的部分, 主要问题在于X11和控制台操作的差别(尤其是 退

格键), 以及Emacs和XEmacs的语法差别.你想做的第一件事可能就是将Delete, Home和

EOL键设置成PC世界中的标准含义: 

    (pc-selection-mode)         ; pc-select.el

 

这同时也让你能够用Shift和光标键选择文本, 用Shift-Delete,Contrl-Insert和Shif

t_Insert进行剪切/拷贝/粘贴. 你可以象下面那样捆绑功能键(这些语法在Emacs和XEm

acs中相同): 

    (define-key global-map [(f2)]  'save-buffer)

    (define-key global-map [(f4)]  (lambda () (interactive) (kill-buffer ni

l))) 

第一行使 F2 存储当前缓冲(捆绑到内建函数 save-buffer 上), 第二行展示了如何捆

绑到你自己的函数: lambda引入一Lisp未知的函数; (interactive) 意思是能够交互地

存取此函数, 剩余部分则不经询问证实而清除当前缓冲. global-map 意味着这些捆绑

在整个Emacs中生效,不管你在看email或些HTML. 你也可以定义局部的键捆绑,只对在特

定模式下的缓冲生效: 

    (define-key emacs-lisp-mode-map [(control c) (control e)] 'eval-buffer)

    (define-key c-mode-map [(f5)] 'compile)

 

Emacs的FAQ(可用 C-h F 在线激活)提出了捆绑键的另一种方法,可能不能在所有版本使

用. Jari Aalto写了 一篇长长的捆绑键指引, 描述了多种重捆绑键的方法. 

如果你够运有个Windows 95键盘,你可以用小小的 xmodmap 使那些迷人的键在X11中使

用. X Window系统有五个特殊键,前三个是Shift,Control和Alt. 你可以将win95键设成

Super和Hyper,如下(这里使用了法国键盘;你可以用 xev 算出你的键盘的键值):

    (shell-command (concat  "xmodmap "

                            "-e 'keycode 115 = Hyper_L' "

                            "-e 'keycode 116 = Hyper_R' "

                            "-e 'keycode 117 = Super_L' "

                            "-e 'add mod4 = Hyper_L' "

                            "-e 'add mod4 = Hyper_R' "

                            "-e 'add mod3 = Super_L' "))

    (message "Setting up Hyper and Super keys")

    (define-key global-map [(hyper tab)] 'complete-tag)

    (define-key global-map [(super !)]   'speedbar-get-focus)

 

备份

缺省配置下,Emacs在原文件同一目录下,在文件名后加上符号~作为备份文件. 如果你宁

愿将所有备份集中在同一地点,可以试试下面的程序.Emacs也能象 à la VMS一样保持

一系列编号的备份;参见变量 version-control.

    

    ;; make backup files in ~/.backups/ rather than scattered around all

    ;; over the filesystem. 

    (defun make-backup-file-name (file-name)                     

      "Create the non-numeric backup file name for `file-name'." 

      (require 'dired)

      (if (file-exists-p "~/.backups")

          (concat (expand-file-name "~/.backups/")

                  (dired-replace-in-string "/" "|" file-name))

        (concat file-name "~")))

    

    ;; disable backups for files in /tmp or in my Mail or News directories.

 

    (defun ecm-backup-enable-predicate (filename)

      (and (not (string= "/tmp/" (substring filename 0 5)))

           (not (string-match "/Mail/" filename))

           (not (string-match "/News/" filename))))

    

    (setq backup-enable-predicate 'ecm-backup-enable-predicate)

 

更多的信息

这些只是[X]Emacs可以定制的一点儿例子. 下面指出的的资源会为你带来更多灵感: 

在线手册包含了广泛的清晰易懂的说明,可以在[X]Emacs中键入'C-h i, 来打开浏览,并

且包含了一页可放入你的 ~/.emacs 的例子.. Emacs Lisp 手册 详细描述了Emacs的扩

展语言. Emacs FAQ. 

XEmacs FAQ. 

新闻组 gnu.emacs.help 针对GNU Emacs的特定问题, comp.emacs.xemacs 针对 XEmac

s 用户, comp.emacs 针对一般问题, alt.religion.emacs 则是关于 Emacs 圣殿 的.

 一般你得到的新的包会在源代码靠近开始的地方包括对哪些变量进行了有用的修改的

说明. XEmacs 给出了 样板 ~/.emacs 文件. 

Erik Sundermann的 XEmacs定制包 在 XEmacs FAQ 中提及. 

反馈

Vincent Zweije wrote to me regarding my gnuserv column, where I suggested u

sing cat /etc/passwd | md5sum as a means of generating a cookie for xauth. 

---------------------------------------------------------------------------

-----In <URL:http://www.linuxgazette.com/issue29/marsden.html>, you wrote: 

[...] 

While allowing access to your X display is bad enough (someone could captur

e all your keystrokes, for example), giving remote access to your Emacs pro

cess is much more worrying, since Emacs can execute arbitrary commands unde

r your id, delete files, send insulting email to the President of the Unite

d States, etc. 

Or maybe more dangerous, send insulting email to the president of an arbitr

ary banana republic. :-) 

Since release 2.1, gnuserv is able to use MIT-MAGIC-COOKIE-1 authentication

 for remote requests. This protocol uses the contents of your ~/.Xauthority

 file, as described in the xauth(1) man page. Gnuserv requires a cookie for

 display number 999, which you can create as follows (blade is the name of 

the machine) : 

     

      ~$ xauth add blade:999 . `cat /etc/passwd | md5sum`

      ~$ xauth list

      blade/unix:0  MIT-MAGIC-COOKIE-1  bc1d627babdbabe9d1f288d2b57c348f

      blade:999  MIT-MAGIC-COOKIE-1  d89570b20925d401c05a79be67159cae

You have picked an unlucky example. There is a real danger that /etc/passwd

 is stable over a long period. This means that it will generate the same ma

gic cookie for many times. 

This is a problem when you have given a cookie away (either voluntarily or 

involuntarily), and intend to revoke permissions by generating a new cookie



Best is to select a source of data that is volatile such as the output of p

s -al or /proc/interrupts, or use mcookie, as you indicated later.  

---------------------------------------------------------------------------

-----

These remarks are particularly important if the site is using shadow passwo

rds. Vincent noted that security issues are too important to be left to ord

inary users : 

---------------------------------------------------------------------------

-----Come to think of it, it's probably just a bad idea to let ordinary use

rs arrange their own security, as with magic cookies. They're just not enou

gh concerned with security - they want to get their job done. 

You'd be amazed at how many times on usenet I've seen suggested to do "xhos

t +" to allow X connections from anywhere. Such people often don't even rea

lise that they're dealing with security. They see disallowing X (gnuserv) c

onnections as a hindrance, and just want a magic incantation to remove it. 

It's a perception thing. 

Someone who deals with security simply has to know what he's doing. However

, first you have to realise that it is security that you're playing with.  

---------------------------------------------------------------------------

-----

下一次 ...

下个月我将讨论Emacs的各种缩写机制.如果有任何说明,建议和改正,别犹豫, 写信到 

<emarsden@mail.dotcom.fr> 和我联系(什么是 你 最喜爱的没它不行的Emacs扩展包?

). C-u 1000 M-x hail-emacs ! 

PS : Emacs并不限于Linux,很多系统上都有其实现(一些系统只有部分操作). 实际上,

作为自由软件的代表之一,作为最强大的复杂的可定制的软件之一,我认为它在 Linux 

公报 中应有其位置.. 

---------------------------------------------------------------------------

-----

Copyright ? 1998, Eric Marsden 

Published in Issue 31 of Linux Gazette, August 1998

---------------------------------------------------------------------------

-----

--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: bin@mtlab.hit.edu.cn]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:213.318毫秒