编程网站资源

700-二叉搜索树中的搜索

描述

  给定二叉搜索树(BST)的根节点和一个值。你需要在BST中找到节点值等于给定值的节点。返回以该节点为根的子树。如果节点不存在,则返回 NULL。

例如

给定二叉搜索树:

1
2
3
4
5
    4  
/ \
2 7
/ \
1 3

和值: 2

你应该返回如下子树:

1
2
3
  2 
/ \
1 3

  在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。

解答:

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null)
return null;
if (root.val == val)
return root;
if (val > root.val)
return searchBST(root.right, val);
return searchBST(root.left, val);
}
}

693-交替位二进制数

给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等。

示例 1:

输入: 5
输出: True
解释: 5的二进制数是: 101

示例 2:

输入: 7
输出: False
解释: 7的二进制数是: 111

示例 3:

输入: 11
输出: False
解释: 11的二进制数是: 1011

示例 4:

输入: 10
输出: True
解释: 10的二进制数是: 1010 输出: True 解释: 5的二进制数是: 101

示例 2:

输入: 7 输出: Flse 解释: 7的二进制数是: 111

示例 3:

输入: 11 输出: False 解释: 11的二进制数是: 1011

示例 4:

输入: 10 输出: True 解释: 10的二进制数是: 1010

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public boolean hasAlternatingBits(int n) {
int last = n & 0b1;
n >>= 1;
while (n > 0) {
int tmp = n & 0b1;
if ((last ^ tmp) == 0)
return false;
n >>= 1;
last = tmp;
}
return true;
}
}

696-计数二进制子串

给定一个字符串 s,计算具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是组合在一起的。
重复出现的子串要计算它们出现的次数。

示例 1 :

输入: “00110011”
输出: 6
解释: 有6个子串具有相同数量的连续1和0:“0011”,“01”,“1100”,“10”,“0011” 和 “01”。 请注意,一些重复出现的子串要计算它们出现的次数。 另外,“00110011”不是有效的子串,因为所有的0(和1)没有组合在一起。

示例 2 :

输入: “10101”
输出: 4
解释: 有4个子串:“10”,“01”,“10”,“01”,它们具有相同数量的连续1和0。

注意:

s.length 在1到50,000之间。
s 只包含“0”或“1”字符。

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public int countBinarySubstrings(String s) {
if (s.length() < 2)
return 0;
int ret = 0;
int last = s.charAt(0);
int zero = last == '0' ? 1 : 0;
int one = last == '1' ? 1 : 0;

for (int i = 1; i < s.length(); i++) {
char c = s.charAt(i);
if (c != last) {
if (c == '1')
one = 0;
else if (c == '0')
zero = 0;
}
if (c == '0') {
zero++;
if (one > 0) {
one--;
ret++;
}
}
if (c == '1') {
one++;
if (zero > 0) {
zero--;
ret++;
}
}
last = c;
}
return ret;
}
}

其他

主要是发现下面这个规律。
000111,连续的3个3个,结果+3.
00111,结果+2.
00011,结果也+2。
所以只要数变换字符以后两边哪边连续字符多一些。

697-数组的度

给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值。

你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。

示例 1:

输入: [1, 2, 2, 3, 1]
输出: 2
解释: 输入数组的度是2,因为元素1和2的出现频数最大,均为2. 连续子数组里面拥有相同度的有如下所示: [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2] 最短连续子数组[2, 2]的长度为2,所以返回2.

示例 2:

输入: [1,2,2,3,1,4,2]
输出: 6

注意:

nums.length 在1到50,000区间范围内。
nums[i] 是一个在0到49,999范围内的整数。

解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public int findShortestSubArray(int[] nums) {
HashMap<Integer, Integer> intStartIndex = new HashMap<>(),
intLastIndex = new HashMap<>(),
intCnt = new HashMap<>();
List<Integer> list = new ArrayList<>();
int max = 0;
for (int i = 0; i < nums.length; i++) {
Integer n = nums[i];
if (!intStartIndex.containsKey(n))
intStartIndex.put(n, i);
intLastIndex.put(n, i);
intCnt.put(n, intCnt.getOrDefault(n, 0) + 1);
int cnt = intCnt.get(n);
if (cnt > max) {
max = cnt;
list.clear();
}
if (cnt == max)
list.add(n);

}
int ret = 0;
for (int i = 0; i < list.size(); i++) {
int tmp = list.get(i);
int candidate = intLastIndex.get(tmp) - intStartIndex.get(tmp) + 1;
if (candidate < ret || ret == 0)
ret = candidate;
}
return ret;
}
}

p4-chapter-2-Guiding-Philosoph-LISP

Emacs is powered by its own LISP implementation called
Emacs Lisp or just elisp. Many are put off or intimidated by
this esoteric language; that’s a shame, because it’s a practical
and fun way to learn LISP in an editor built up around the
idea of LISP. Every part of Emacs can be inspected, evaluated
or modified because the editor is approximately 95 percent
elisp and 5 percent C code. It’s also a practical way to learn
a radical paradigm: that code and data are interchangeable
and malleable; that the language, owing to its simple syntax,
is trivially extensible with macros.

啥是LISP
Emacs是靠它自己的LISP解释器支持的。很多人被劝退,或者被这门神秘的
语言吓到。这样就太可惜了,因为在围绕着LISP方法的编辑器中学LISP
这才是有效和有趣的学习LISP的方式。Emacs的每一部分都可以被审查,评价,
和修改,因为这个编辑器由95%的elisp代码和5%的c代码组成。这也是学习本质
范例的一个方法,数据交换,代码适应性,这语言还拥有简单的语法,和琐细
的宏扩展。

Unfortunately, there’s no getting around learning elisp at
some point. In this book, I will talk about the Customize in-
terface: a dynamically generated interface of customizable
options in Emacs. However, something as simple as rebind-
ing a key means you’ll have to interact with elisp. But it’s
not all bad. Most of the problems you’re likely to encounter
have already been solved by someone else a long time ago;
it’s a simple matter of searching the Internet for a solution
to your problems.

不幸的是,我们不谈学习Emacs。在这书里,我将讨论自定义接口:Emacs中
一个动态创建自定义接口的选项。然而,像重新绑定快捷键则不得不需要你
与elisp交互。这也不全是坏消息。大部分你可能遇到的问题已经在很久以前
就被人解决了。到互联网上搜索你要解决的问题是件很容易的事情。

Despite the relative unpopularity of elisp versus more “mod-
ern” languages like Python, Ruby and JavaScript, I doubt
Emacs would have had the same power of extensibility if
a more traditional imperative/object-oriented language had
been used. What makes LISP such a fantastic language is that
source code and data structures are intrinsically one and the
same: the LISP source code you read as a human is almost
identical to how the code is manipulated as a data structure
by LISP — the distinction between the questions “What is
data?” and “What is code?” are nil.

尽管elisp相对与大多数主流语言例如Python,Ruby,js,来说不太流行。
我怀疑Emacs跟传统命令式/面向对象语言相比是否具有一样的扩展能力。
数据结构和代码本质上是一样的这点让LISP很令人惊讶。LISP源码读起来让人
感觉在通过LISP修改数据结构。关于什么是数据什么是代码这个问题,没有答案。

The data-as-code, the macro system and the ability to “advise”
arbitrary functions – meaning you can modify the be-
havior of existing code without copying and modifying the
original – give you an unprecedented ability to alter Emacs
to suit your needs. What would in most software projects
be considered code smells or poor architecture is actually a
major benefit in Emacs: you can hook, replace or alter existing
routines in Emacs to suit your needs without rewriting
large swathes of someone else’s source code.

数据也是代码,这个宏系统“推荐”函数作为参数,这意味着你不需要修改复制
源码就能修改代码的表现。这给了你前所未有的能力去修改Emacs来适应你的需求。
绝大部分项目中会在乎代码味道和少的结构,而这两点正是Emacs的优势.你可以
挂钩子,替换,修改现存的常规Emacs代码区适应你的需求,而不需要为此重写捆绑
在一起的大量别人的源码.

This book will not teach elisp in any great detail: Emacs has
a built-in elisp introduction3 and I highly recommend it if
you are curious — and honestly you should be. LISP is fun
and this is a great way to learn and use a powerful language
in a practical environment. Don’t let the parentheses scare
you; they are actually its greatest strength.

这本书不会详细的教elisp,Emacs有个内置的elisp介绍.我高度推荐它,
如果你有任何困惑的话–老实说如果你有的话. LISP 时有趣的,用Emacs
是个绝佳的学习实践这门语言的机会.别让括号吓到你,这其实是它的优点.

Emacs as an Operating System

When you run Emacs you are in fact launching a tiny C
core responsible for the low-level interactions with your operating
system’s ABI. That includes mundane things like filesystem
And network access; drawing things to the screen or
printing control codes to the terminal.

Emacs当作一个操作系统

当你运行Emacs时,你其实是登录了一个微型C内核,这个内核可以跟你系统的ABI
接口进行底层交互.包括通常的文件系统和网络使用权,在屏幕上绘制东西,或者
打印控制命令到命令行.

The cornerstone of Emacs though is the elisp interpreter
without it, there is no Emacs. The interpreter is creaky and
old; it’s struggling. Modern Emacs users expect a lot from
their humble interpreter: speed and asynchrony are the two
main issues. The interpreter runs in a single thread and intensive
tasks will lock the UI thread. But there are workarounds;
the issues, manifold though they are, do not deter people
from writing ever-more sophisticated packages.

Emacs的基石是elisp解释器,如果没有它,就没有Emacs.这个解释器是
破旧的.苦苦挣扎.现代Emacs用户对这个简陋的解释器有所期待:主要
有快速异步.这个解释器跑在单线程上,密集的任务将会锁住ui线程.
但是有解决方法了,虽然这些问题多种多样,但是仍然挡不住人们继续
写越来越复杂的包

When you write elisp you are not just writing snippets of
code run in a sandbox, isolated from everything — you are
altering a living system; an operating system running on an
operating system. Every variable you alter and every func-
tion you call is carried out by the very same interpreter you
use when you edit text.

当你开始写elisp,你并不是在修改沙盒中的一小段代码,而是在修改一个
时时的系统.一个运行在操作系统之上的操作系统.你可以修改每个变量
每个你调用的函数,他们被搭载在你编辑文本用的这个编辑器上.

Emacs is a hacker’s dream because it is one giant, mutable
state. Its simplicity is both a blessing and a curse. You can
re-define live functions; change variables left and right; and
you can query the system for its state at any time — state that
changes with every key stroke as Emacs responds to events
from your keyboard to your network stack. Emacs is selfdocumenting
because it is the document. There are no other
editors that can do that. No editor comes close.

Emacs是一个极客的梦幻,因为它是一个伟大的可变的状态.它的简单特点,
既是祝福也是诅咒.你可以重新定义时时函数,彻底改变变量.可以在任何时间
查询系统的状态.每一帧的状态改变都是Emacs在响应你的键盘事件或网络事件.
Emacs又是自文档的编辑器.没有其它编辑器能做到这个,甚至接近.

And yet Emacs never crashes — not really, anyway. Emacs
has an uptime counter to prove that it doesn’t (M-x emacsuptime)
— multi-month uptimes are not uncommon.

Emacs从来没有崩溃过,无论真假.Emacs有个运行时间计数器来证明这不是虚的
比如几个月的运行时间并不奇怪.

So when you ask Emacs a question – as I will show you how
to do later – you are asking your Emacs what its state is. Because
of this, Emacs has an excellent elisp debugger and unlimited
access to every facet of Emacs’s own interpreter and
state — so it has excellent code completion too. Any time
you encounter a LISP expression you can tell Emacs to evaluate
it, and it will: from adding numbers to setting variables
to downloading packages.

当你问Emacs问题时,你是向Emacs询问现在的state是什么,这正如我演示的.
因为这样,Emacs有一个聪明的elisp调试器和无限制的访问Emacs自己的解释器
的所有方面,也有一个杰出的编码自动完成功能.任何时候你遇到一个LISP表达式,
你都能让Emacs解析它,无论是从数字加法到设置变量到下载扩展包.

p3-chapter-2-Guiding Philosophy

Emacs is a tinkerer’s editor. Plain and simple. People who
hack on Emacs do it because almost every facet of it is
extensible. It is the original extensible, customizable, selfdocumenting
editor. If you come from other text editors,
the idea of being able to change anything may seem like an
unnecessary distraction from your work – and indeed, a
lot of Emacs hacking does happen at the expense of one’s
real job – but once you realize that you can shape your
editor to do what you want it to do, it opens up a world of
possibilities.

Emacs是一个多面手的编辑器.平文本并且简单.之所以Emacs被用来hack编程,
是因为它几乎所有层面上的可扩展性.它是一个本身可扩展,可定制,自文档的
编辑器.如果你从别的文本编辑器过来,这种改变所有事的想法看起来没有必要,
只是分散你对工作的注意力.事实上,很多的Emacs神操作确实很消耗工作时间,
但是只要你认识到你可以把你的编辑器改造成任何你想要的样子的时候,新世界
大门就向你敞开了。

That means you can truly rebind all of Emacs’s keys to your
liking; you are not hidebound by your IDE’s undocumented
and buggy API nor the limitations that would follow if you
did change things — such as your custom navigation keys
not working in, say, the search & replace window or in the
internal help files. Truly, in Emacs, you can change every-
thing — and people do. Vim users are migrating to Emacs
because, well, Emacs is often a better Vim than Vim.

你可以重新绑定所有快捷键。你不用被包裹在ide的无文档或者一车车的api中,
例如你设置的快捷键不生效,如说明,或搜索替换窗口,或内部帮助文件。
确实,你在Emacs中你能改变任何事情,正如人们做的那样。Vim用户会想要
迁移到Emacs中,毕竟,Emacs是一个更加Vim的Vim。

Emacs pulls you in. Once you start using Emacs for the edit-
ing, you realize that using Emacs for IRC, email, database ac-
cess, command-line shells, compiling your code or surfing the
Internet is just as easy as editing text – and you get to
keep your key bindings, theme and all the power of Emacs
and elisp to configure or alter the behavior of everything.

Emacs把你拉入进来。只要你开始使用Emacs去编辑,你会意识到使用Emacs进行,
IRC,email,数据库访问,命令行,编译你的代码,或者在Internet上冲浪,
都会像编辑文本一样简单。你可以保持你的快捷键,主题,所有Emacs能量和使用
elisp来配置和修改任何事的表现。

And when everything is seamlessly tied together you avoid
the usual context switches of going from application to ap-
plication: most Emacs users use little more than the editor,
a browser and maybe a dedicated terminal application.

当任何事都无缝的绑定在一起,你可以避免通常的从一个应用到另外一个应用的
文本切换,大部分Emacs使用者使用编辑器,浏览器,或许还有一个命令行。

 Emacs’s history
 Emacs’s source code repository (now in Git)
 stretches back over 30 years and has more than
 130,000 commits and nearly 600 committers.

 Emacs的历史。
 Emacs的源码仓库(现在在github)可以追溯到30年前,并且有超过
 130,000次提交,以及将近600个贡献者。

If you want to modify Emacs, or any of the myriad pack-
ages available to you, Emacs Lisp (also known informally as
elisp) is what you will have to write. There have been a few
attempts to graft other languages onto elisp and Emacs but
with no lasting effect. As it turns out, LISP is actually a per-
fect abstraction for a very advanced tool like Emacs. And
most modern languages wouldn’t necessarily stand the test
of time: TCL was briefly considered in the 90s as it was popu-
lar at the time — but that has the distinction of being even
more obscure than LISP, nowadays.

假如你想修改Emacs,或者大量提供给你的包,你需要写elisp语言。
有些人尝试把其他语言移植到elisp和Emacs中,但没有效果。事实证明,
LISP是对高级工具Emacs的完美抽象。其他大多数语言没法经过时间的考验,
TCL曾短暂的被认为考虑在它流行的1990年代,但是它跟Emacs相比有些区别,
从今天看来它甚至更难以理解。

The only downside is that fiddling with your Emacs config-
uration is something you will have to learn to live with (and
in LISP no less, but as I explain in the next part that’s actually
a good thing.) That’s why I reinforced the point that it’s a
tinkerer’s editor. If you hate the idea of tweaking anything
and want everything out of the box, you have two options
left:

唯一的缺陷是,摆弄你的Emacs配置是你不得不学会的(只有Lisp,但是我在
下一部分会解释,这或许是一件好事)这也是我强调的那一点,这是一个思考者
的编辑器。如果你讨厌配置任何东西,并且想要开箱即用,你有两个选项:

Use a starter kit There are many free starter kits that come
equipped with additional packages and what the
author thinks are sensible default settings. They can
be a good way to start out but with the caveat that
you don’t know where Emacs ends and the starter
kits’ added functionality begins.
I recommend you look at one of the following starter
kits widely used:
• Steve Purcell’s .emacs.d
https://github.com/purcell/emacs.d
• Bozhidar Batzov’s Prelude
https://github.com/bbatsov/prelude

一、使用启动工具箱 有很多启动工具箱已经装备了很多额外的包和作者
认为明智的默认配置。但是请小心,你不知道Emacs的来源和启动工具箱在
哪里添加了启动函数。我建议你查看这下面的两个被广泛使用的工具包。

Use the defaults Certainly an option but Emacs, I would
say, is rather lacking out of the box. You are expected
to configure Emacs to your liking or use a starter kit.
For an editor that is so radically different from main-
stream editors, the maintainers are surprisingly conservative
about changing the defaults for fear of upsetting
the old guard (who, of all people, should know how to
configure Emacs.)

二、使用默认配置 这确实是一个选择,但我必须说这缺少很多开箱即用
的东西。你需要配置Emacs或用工具箱。从编辑器的角度,它跟主流的编辑器
有本质区别,维护者是保守主义者,不愿意修改默认配置,因为害怕令那些
老同志失望。(可这群老同志都知道怎么配置Emacs)

p2-chapter-2-The Way of Emacs

The Way of Emacs

Emacs的套路

“The purpose of a windowing system is to put some amusing fluff
around your one almighty emacs window.”– Mark, gnu.emacs.help.

一个emacs窗口的目的是把一些可笑的杂毛放在窗口的周围。

If you imagine the span of the modern computing era be-
ginninginth 1960s,then Emacs has been there longer than
just about everything else. It was first written by Richard
Stallman as a set of macros on top of another editor, called
TECO, back in 1976.1 TECO is now mostly remembered for be-
ing even more obtuse and hard to understand than Emacs
and DOS-era WordPerfect combined. Since then, there have
been many competing implementations of Emacs but today
you’re only likely to encounter XEmacs and GNU Emacs.

你可以想象一下现代计算机时代开始于1960年,然鹅,Emacs比所有已有的东西
更加的古老。Richard Stallman撰写了Emacs的第一版,这是另一个顶级
编辑器TECO的宏的集合。TECO最为人所记住的是它比Emacs加DOS时代的WordPrefect
加一块还更让人难以理解和困惑.自那时起,出现了很多互相竞争的Emacs实现,但是
你现在只要世道XEmacs和GNU Emacs就行.

This book will only concern iteself with GNU Emacs. Once upon a time
XEmacs was the more advanced and feature rich editor, but this is no
longer the case: from Emacs 22 onwards GNU Emacs is the best Emacs
out there.The history of XEmacs and GNU Emacs is an interesting one.
It was one of the first major forks2 in a free software project and
both XEmacs and GNU Emacs are developed in parallel to this day.

这本书只涉及GNU Emacs.曾经在一段时间上,XEmacs层是最高级和特性丰富的编辑器,
但是这已经过去了,从Emacs 22 开始GNU Emacs就是最好的Emacs了.XEmacs和GNU Emacs
的历史也是一件趣事.他俩曾是一个开源项目的第一个主要两条分支,他俩并行开发到
现在.

Note
To almost everyone, the word Emacs refers specifically
to GNU Emacs. I will only spell out the full
name when I am distinguishing between different
implementations. When I mention Emacs, I
always talk about GNU Emacs.

Note
对大多数人来说,Emacs这个词就是指GNU Emacs. 在我要区分两个不同的Emacs实现时,
我会完整的拼出这个名字.但当我提到Emacs,我一直是在说GNU Emacs.

Because of Emacs’s age there are a number of… oddities.
Weird choices of terminology and historical anachronisms
persist because in most cases Emacs was ahead of the editor-
IDE curve for many decades and thus had to invent its own
terminology for things. There are talks of replacing Emacs’s
own vernacular with words familiar to everyone, but that
is still a long way off.

因为Emacs的出现时代,它带有很多怪异的东西.历史原因导致的意识和术语上的
怪异选择仍然存在,因为Emacs在一定的时间里在IDE这条线上是领先的,所以不得不为
很多事创建术语.有讨论说要用更让人熟悉的词语替换Emacs的术语方言,但这条
路还很长.

Despite the lack of marketing, a small core of Emacs developers,
the anachronisms and terminology that predqates the
modern Personal Computing-era, there are many people
out there who just love using Emacs. When Sublime Text
showed off its mini-map feature (a miniature display of the
source code) someone immediately coded up a minimap
package doing the same thing in Emacs. In fact, it is this
extensibility that attracts some to – and repels others from
– Emacs.

除了市场缺失,一小部分核心Emacs开发者,早于现代个人PC时代的历史错误和术语外,
还是有很多人无视这些而只是单纯的喜欢使用Emacs.当Sublime展示了它对源码的
缩略图功能后,有人可以开发了一个缩略图的包给Emacs用.事实上,就是这种扩展性
吸引了哪些排斥其他死板编辑器的人.
-来自Emacs

This chapter will talk about the Way of Emacs: the terminology
and what Emacs means to a lot of people, and why understanding
where Emacs comes from will make it easier to
adopt it.

这一章将讨论Way of Emacs这个标题:
通过讨论术语和对大多数人来说Emacs意味着什么,和为什么知道Emacs的来源
会让你更容易采用它.

p1-chapter-1-Introduction

Chapter 1
Introduction
“I’m using Linux. A library that emacs uses to
communicate with Intel hardware.”
– Erwin, #emacs,Freenode.

我在用linux,一个emacs用来连接互联网的的库

Thank You
Thank you for purchasing Mastering Emacs. This book has
been a long time coming. When I started my blog, Master-
ing Emacs, in 2010, it was at the recommendation of a good
friend, Lee, who suggested that I share my thoughts on
Emacs and work flow in Emacs. At the time I had accrued
in an org mode file titled blogideas.org a large but random
assortment of ideas and concepts that I’d learned about and
wished someone had taught me. The end result of that file
is the blog and now this book.

感谢你买了这本Mastering Emacs.这本书的到来可以追溯到很久之前。
在我开始写博客那时候,有个朋友建议我分享自己关于emacs的思考,
和在emacs中工作的流程。当时我积累了一个名为blogideas.org的文件,
里面随机分类了大量的想法和我学会的概念,或者希望别人教我的东西。
这个org文件最后变成了这个博客和这本书。

Special Thanks
I would like to thank the following people for
their encouragement, advice, suggestions and
critiques:
Akira Kitada, Alvaro Ramirez, Arialdo Mar-
tini, Bob Koss, Catherine Mongrain, Chandan
Rajendra, Christopher Lee, Daniel Hannaske,
Edwin Ong, Evan Misshula, Friedrich Paetzke,
Gabriela Hajduk, Gabriele Lana, Greg Sieranski,
Holger Pirk, John Mastro, John Kitchin, Jonas
Enlund, Konstantin Nazarenko, Lee Cullip,
Luis Gerhorst, Lukas Pukenis, Manuel Uberti,
Marcin Borkowski, Mark Kocera, Matt Wilbur,
Matthew Daly, Michael Reid, Nanci Bonfim,
Oliver Martell, Patrick Mosby, Patrick Martin,
Sebastian Garcia Anderman, Stephen Nelson-
Smith, Steve Mayer, Tariq Master, Travis
Jefferson, Travis Hartwell.

我要感谢以下这些人的鼓励,建议,见解,评论。

Like a lot of people, I was thrust into the world of Emacs
without knowing anything about it; in my case it was in
my first year of University where the local computer soci-
ety was made up primarily of Vim users. It was explained
to me, in no uncertain terms, that “you use Vim — that’s
it.” Not wanting to be told what to do, I picked the polar
opposite of Vim and went with Emacs.

和很多人一样,我在对EMACS毫不知情的情况下,被推入到EMACS的世界里。
在大学的第一年里,当地的电脑社团全是Vim用户。有人对我解释,
不需要理由,你给我用Vim。并且不想告诉我为什么要这么做,
于是我选择了一个一个完全相反的一极,走向EMACS。

Emacs proved to be a stable and reliable editor in all those
years, but it was a tough one to get to know. Despite the
extensive user documentation, it never helped me to learn
and understand Emacs.

Emacs这些年被证明是一个稳定可靠编辑器,但理解这一点还是很难。
尽管有着广泛的文档存在,但这并不能帮助我学习理解Emacs。

As it turns out, Emacs is a philosophy or even a religion. So,
the joke about the “Church of Emacs” is eerily accurate in
many ways, as you will find out in the next chapter.

事实证明,Emacs是一种哲学,甚至是一种宗教。所以,在很多情况下,
Emacs教会这个玩笑说的很准确,这点你可以在下一章找到例证。

Intended Audience
目标受众

It’s a bit weird talking about the intended audience when
you’ve already bought the book on the subject. But it bears
mentioning anyway so no matter your Emacs skill level you
will get something out of this book.

讲目标受众这个东西很奇怪,因为毕竟你们已经买了这本书。
但还是要提下,不管你Emacs是什么水平,你还是能从这本书里得到些东西。

The first and (most obvious) audience are people new to
Emacs. If you’ve never used Emacs before in your life, you
will hopefully find this book very useful. However, if
you’re new to Emacs and non-technical, then you’re going
to have a harder time. Emacs, despite being suitable for
much more than just programming, is squarely aimed at
computer-savvy people. Although it’s perfectly possible
to use Emacs anyway, this book will assume that you’re
technically inclined, but not necessarily a programmer.

最首要的受众是那些Emacs新手。如果你从来没用过Emacs,
你会发现这本书非常有用。然而,如果你是Emacs新手并且不是技术人员,
你会经历一段痛苦的时期。Emasc,不仅适用于编程人员,
同时也适用于喜欢捣鼓电脑的人。任何情况下都使用Emacs是可能的,
这本书假设你是有技术倾向的人,但是不是专业的程序人员。

If you’ve tried Emacs before but given up, then I hope this
book is what convinces you to stick with it. But it’s fine if
you don’t; some languages or environments don’t (contrary
to what a lot of Emacs users would claim) work well with
Emacs. If you’re primarily a Microsoft Windows developer
working with Visual Studio, using Emacs is going to be a
case of two steps forward, one step back: you gain unprece-
dented text editing and tool integration but lose all the ben-
efits a unified IDE would give you.

假如你曾经尝试过Emacs但放弃了,希望这本书能说服你坚持下去。
但是你不坚持也ok。因为一些语言和环境(很多Emacs声称)不能在
Emacs中很好的工作。如果你使用Visual Studio工作,使用Emacs将会
给你带来前所未有的编辑和工具集成体验,但会丧失集成开发工具带来
的好处。

If you’re a Vim refugee, then welcome to the dark side! If
your primary objective is to use Emacs’s Vim emulation lay-
ers, then some of this book is redundant; it concerns itself
with the default Emacs bindings and it teaches “the Emacs
way”of doing things.But not to worry:a lot of the tips and
advice herein are still applicable, and who knows — maybe
you’ll switch away from Evil mode in time.

假如你是一个Vim难民,欢迎来到黑暗的另一面。假如你的主要目的是
使用Emacs中的Vim仿真,那么这本书对你来说是多于的。这本书主要关注
Emacs绑定按键和Emacs做事的方式。这有很多提示和建议仍然很实用,
谁知道呢,也许你会半路转到Evil模式。

And finally, if you’re an existing Emacs user but struggling
to take it to the next level,or maybe you just need a refresher
course “from the ground up,” then this book is also for you.

最后,徦如你已经是一个Emacs用户,挣扎着想提高自己的Emacs水平,
或者只是想来个复习,这本书都适合你。

What You’ll Learn
你将学到什么?

Covering all of Emacs in just one book would be a Sisyphean
task.Instead,I aim to teach you what you need to be produc-
tive in Emacs, which is just a small subset of Emacs’s capabil-
ity. Hopefully, by the end of this book, and with practice,
you will know enough about Emacs to seek out and answer
questions you have about the editor.

覆盖所有Emacs知识在一本书里是徒劳的任务。相反,我想教你在Emacs中
生产需要的东西,这会是Emacs能力的一部分。希望,到了这本书的最后,
并通过练习,你能对Emacs有足够多的了解来解决关于这个编辑器相关的问题。

To be more specific, I will teach you, in broad terms, six
things:
更具体的,我将教你以下六点。

What Emacs is about: A thorough explanation of impor-
tant terminology and conventions that Emacs uses
which in many cases differs greatly from other editors.
You will also learn what the philosophy of Emacs is,
and why a text editor even has a philosophy. I will
also talk about Vim briefly and the Editor Wars and
what the deal is with all those different keys.

什么是Emacs,Emacs就是一套贯穿始终的术语和在很多方面
区别于其他编辑器的约定。你还会学到什么是Emacs的哲学,
和为什么一个编辑器会有哲学。我简单的聊下vim,以及编辑器之争,
。。。

Getting started with Emacs How to install Emacs,how to
run it, and how to ensure you’re using a reasonably
new version of Emacs. I explain how to modify Emacs and
what you need to do to make your changes permanent.
I will introduce the Customize interface and how to load
a color theme. And finally,I’ll talk about the user interface
of Emacs and some handy tips in case you get stuck.

首先从怎么安装Emacs,怎么运行它,怎么确认是最新版的Emacs。
我会解释怎么修改Emacs和怎么让你的修改常驻。我会介绍个性化
界面,和怎么加载一个顔色主题。最后,我会讲讲Emacs的用户界面,
和一些便利的提示来防止你卡住。

Discovering Emacs Emacs is self-documenting; but what
does it mean and how can you leverage that aspect
to discover more about Emacs or answer questions
you have about particular features? I will show you
what I do when I have to learn how to use a new
mode or feature in Emacs, and how you can use the
self-documenting nature of Emacs to find things for
which you’re looking.

发现Emacs,Emacs是一个自文档的。但是,这意味什么以及你怎么
利用这个特点来挖掘Emacs或回答你遇到的特别问题。我会向你
展示当我使用Emacs的一个新模式或特性时会怎么做,以及当你
在查找东西时怎么利用Emacs的自文档特性。

Movement How to move around in Emacs.At first glance a
simple thing to do, but in Emacs there are many ways
of going from where you are to where you need to
go in the fewest possible keystrokes. Moving around
is probably half the battle for a developer and know-
ing how to do it quickly will make you more efficient.
Some of the things you’ll learn: moving by syntactic
units, and what exactly syntactic units are; using win-
dows and buffers; searching and indexing text; select-
ing text and using the mark.

怎么在Emacs中移动。首先,找个简单的来试,但其实Emacs中
有很多简单有效的快捷键来移动。移动方式几乎占了开发者一半
的争论点,以及知道怎么快速移动可以让你更高效。你将会学习:
通过语法单元移动、什么是语法单元、使用窗口和缓存、搜索和
索引文档、选择文本和使用标记。

Editing As in the chapter on movement, I will show you
how to edit text using a variety of tools offered to you
by Emacs. This includes things like editing text by bal-
anced expressions, words, lines, paragraphs; creating
keyboard macros to automate repetitive tasks;
search-ing and replacing; registers; multi-file editing;
abbreviations; remote file editing; and more.

。。。 我将会向你展示怎么使用Emacs提供的一系列工具进行文本编辑。
包括表达、词语、行、段落的操作。创建宏来自动化重复任务:
搜索、替换、登记;多文件编辑、缩写、远程文件编辑;其他。

Productivity Emacs can do more than just edit text and this
chapter is only a taste of what attracts so many people
to Emacs: its tight integration with hundreds of exter-
nal tools.I will whet your appetit eand show you some
of the more interesting things you can do when you
choreograph Emacs’s movement and editing.

Emacs的生产效率不仅仅包括文本编辑,这节将尝试说明Emacs吸引这么
多人的原因:与大量的扩展工具有紧密结合。我将会打磨你的兴趣,并向你
展示当你编排Emacs的移动和编辑时你可以做些什么。