33.7 用户级删除命令

本节描述用于删除文本的更高级别命令,这类命令主要面向用户,但在 Lisp 程序中同样有用。

Command: delete-horizontal-space &optional backward-only

此函数删除光标周围所有空格和制表符,返回 nil

backward-onlynil,函数仅删除光标前的空格与制表符,不删除光标后的。

在下面的示例中,我们四次调用 delete-horizontal-space,每次都在一行的第二个与第三个字符之间放置光标。

---------- Buffer: foo ----------
I ∗thought
I ∗     thought
We∗ thought
Yo∗u thought
---------- Buffer: foo ----------

(delete-horizontal-space)   ; Four times.
     ⇒ nil

---------- Buffer: foo ----------
Ithought
Ithought
Wethought
You thought
---------- Buffer: foo ----------
Command: delete-indentation &optional join-following-p beg end

此函数将光标所在行与上一行合并,删除连接处的所有空白,并在某些情况下替换为一个空格。 若 join-following-pnildelete-indentation 会将本行与下一行合并。 除此之外,若 begend 均非 nil,此函数会合并它们所指定区域内的所有行。

在交互式调用时,join-following-p 为前缀参数;若区域处于激活状态,begend 分别为区域的起点和终点,否则为 nil。函数返回 nil

若存在填充前缀,且待合并的第二行以该前缀开头,则 delete-indentation 会在合并前先删除该填充前缀。See 填充边距

在下面的示例中,光标位于以 ‘events’ 开头的行,上一行末尾是否有空格不影响结果。

---------- Buffer: foo ----------
When in the course of human
∗    events, it becomes necessary
---------- Buffer: foo ----------

(delete-indentation)
     ⇒ nil

---------- Buffer: foo ----------
When in the course of human∗ events, it becomes necessary
---------- Buffer: foo ----------

行合并后,由 fixup-whitespace 函数决定在连接处保留一个空格还是不留空格。

Command: fixup-whitespace

此函数根据上下文,将光标周围所有水平空白替换为一个空格或直接清空。返回 nil

在行首或行尾时,合适的处理是清空空白。 在右括号语法字符之前,或左括号、表达式前缀语法字符之后,同样适合不留空格。 其他情况下保留一个空格。See 语法类别表

在下面的示例中,第一次调用 fixup-whitespace 时光标位于第一行单词 ‘spaces’ 之前。第二次调用时光标紧跟在 ‘(’ 之后。

---------- Buffer: foo ----------
This has too many     ∗spaces
This has too many spaces at the start of (∗   this list)
---------- Buffer: foo ----------

(fixup-whitespace)
     ⇒ nil
(fixup-whitespace)
     ⇒ nil

---------- Buffer: foo ----------
This has too many spaces
This has too many spaces at the start of (this list)
---------- Buffer: foo ----------
Command: just-one-space &optional n

此命令将光标周围的所有空格和制表符替换为单个空格;若指定了 n,则替换为 n 个空格。返回 nil

Command: delete-blank-lines

此函数删除光标周围的空行。 若光标位于一个空行上,且其前后有一个或多个空行,则只保留一行,其余删除。 若光标位于单独一行空行上,则删除该行。 若光标位于非空行上,则删除紧跟在该行之后的所有空行。

空行的定义是只包含制表符和空格的行。

delete-blank-lines 返回 nil

Command: delete-trailing-whitespace &optional start end

删除 startend 所指定区域内的行尾空白。

此命令删除区域内每一行最后一个非空白字符之后的所有空白字符。

若此命令作用于整个缓冲区(即交互式调用时标记未激活,或从 Lisp 中调用时 endnil),且变量 delete-trailing-linesnil,它还会删除缓冲区末尾的所有多余空行。