在编写 Clojure 代码时,我经常在最后一个表达式和右括号之间添加空格。就像是
(defn myfunction
[arg]
(do
(expr1)
(expr2)|
))
在哪里 | 是光标的位置。Emacs 中是否有删除 (expr2) 和最后括号之间空格的快捷方式?目标是结束
(defn myfunction
[arg]
(do
(expr1)
(expr2)))
在编写 Clojure 代码时,我经常在最后一个表达式和右括号之间添加空格。就像是
(defn myfunction
[arg]
(do
(expr1)
(expr2)|
))
在哪里 | 是光标的位置。Emacs 中是否有删除 (expr2) 和最后括号之间空格的快捷方式?目标是结束
(defn myfunction
[arg]
(do
(expr1)
(expr2)))
M-^
(command delete-indentation
) 已经完成了您的要求,至少在您给出的示例中(以及类似的示例)。见(省略)User-Level Deletion
。
将前缀参数发送到 M-^:
C-u M-^
没有前缀,M-^ 将当前行与前一行连接起来。
使用前缀(Cu),M-^ 将下一行与当前行连接起来。
这是我对这个问题的解决方案的看法:在您的示例中,您当时可能想要做的是退出列表。那么为什么不将空白清理附加到列表退出呢?
(global-set-key (kbd "C-M-e") 'up-list-robust)
(defun up-list-robust ()
(interactive)
(remove-gaps)
(let ((s (syntax-ppss)))
(when (nth 3 s)
(goto-char (nth 8 s)))
(ignore-errors (up-list))
(remove-gaps)))
(defun remove-gaps ()
(when (looking-back ")\\([ \t\n]+\\))")
(delete-region (match-beginning 1)
(match-end 1))))
所以现在,每当你退出一个列表时,被困在两个括号之间的最近的空白就会被删除。
我刚刚写了它,欢迎提出改进建议,但我已经使用了几分钟,我喜欢它。您可能还希望将此绑定到更好的快捷方式,C-M-e这是up-list
.
改进上述@wvxvw 的评论,您可以将以下内容添加到您的.emacs
文件中。然后,C-z m
(或您选择的任何其他组合键)将执行您想要的操作。事实上,如果你在包含(expr1)
.
(global-set-key "\C-zm" 'join-lines-removing-spaces)
(defun join-lines-removing-spaces ()
"Join the current line with the next, removing all whitespace at this point."
(move-end-of-line nil)
(kill-line)
(delete-horizontal-space))
这是我为自己解决这个特定问题而编写的一个函数。
(defun delete-surrounding-whitespace ()
(interactive)
(let ((skip-chars "\t\n\r "))
(skip-chars-backward skip-chars)
(let* ((start (point))
(end (progn
(skip-chars-forward skip-chars)
(point))))
(delete-region start end))))
本着教钓鱼胜过提供鱼的精神,我将分享如何从 Emacs 中发现这一点。
如果您知道想要更多信息的函数或变量的名称,那么您可以使用 apropos 进行更深入的挖掘。但是,如果您不知道该命令的名称怎么办?
例如,我可能会使用 apropos 并搜索 del.*white、zap.*space、del.*space 等……但永远不会遇到像 just-one-space 这样有用的空白函数。
为了扩大搜索范围,您可以在 Emacs 内部搜索 Emacs 的文档,方法是按 Ch i 进入 Texinfo 文档。按 mEmacs 进入文档中特定于 Emacs 的部分(也有一些包的部分)。进入 Emacs 部分后,按 s 搜索并执行对 delete.*white 之类的搜索,然后您将被带到文档的删除部分,在那里您将看到各种有用的删除方法。
12.1.1 Deletion
---------------
Deletion means erasing text and not saving it in the kill ring. For the
most part, the Emacs commands that delete text are those that erase just
one character or only whitespace.
‘<DEL>’
‘<BACKSPACE>’
Delete the previous character, or the text in the region if it is
active (‘delete-backward-char’).
‘<Delete>’
Delete the next character, or the text in the region if it is
active (‘delete-forward-char’).
‘C-d’
Delete the next character (‘delete-char’).
‘M-\’
Delete spaces and tabs around point (‘delete-horizontal-space’).
‘M-<SPC>’
Delete spaces and tabs around point, leaving one space
(‘just-one-space’).
‘C-x C-o’
Delete blank lines around the current line (‘delete-blank-lines’).
‘M-^’
Join two lines by deleting the intervening newline, along with any
indentation following it (‘delete-indentation’).
我没有看到任何完全符合我要求的东西。但是,通过使用 apropos 搜索和提取某些函数的帮助缓冲区,我可以看到它们是如何实现的,并使用这些相同的技术来编写我需要的确切函数。
在 simple.el.gz 中查看函数 just-one-space,我看到附近有一个名为 cycle-spacing 的函数,看起来它已经接近了我需要的功能。
(defun cycle-spacing (&optional n preserve-nl-back mode)
"Manipulate whitespace around point in a smart way.
In interactive use, this function behaves differently in successive
consecutive calls.
The first call in a sequence acts like `just-one-space'.
It deletes all spaces and tabs around point, leaving one space
\(or N spaces). N is the prefix argument. If N is negative,
it deletes newlines as well, leaving -N spaces.
\(If PRESERVE-NL-BACK is non-nil, it does not delete newlines before point.)
The second call in a sequence deletes all spaces.
The third call in a sequence restores the original whitespace (and point).
If MODE is `single-shot', it only performs the first step in the sequence.
If MODE is `fast' and the first step would not result in any change
\(i.e., there are exactly (abs N) spaces around point),
the function goes straight to the second step.
Repeatedly calling the function with different values of N starts a
new sequence each time."
(interactive "*p")
(let ((orig-pos (point))
(skip-characters (if (and n (< n 0)) " \t\n\r" " \t"))
(num (abs (or n 1))))
(skip-chars-backward (if preserve-nl-back " \t" skip-characters))
(constrain-to-field nil orig-pos)
(cond
;; Command run for the first time, single-shot mode or different argument
((or (eq 'single-shot mode)
(not (equal last-command this-command))
(not cycle-spacing--context)
(not (eq (car cycle-spacing--context) n)))
(let* ((start (point))
(num (- num (skip-chars-forward " " (+ num (point)))))
(mid (point))
(end (progn
(skip-chars-forward skip-characters)
(constrain-to-field nil orig-pos t))))
(setq cycle-spacing--context ;; Save for later.
;; Special handling for case where there was no space at all.
(unless (= start end)
(cons n (cons orig-pos (buffer-substring start (point))))))
;; If this run causes no change in buffer content, delete all spaces,
;; otherwise delete all excess spaces.
(delete-region (if (and (eq mode 'fast) (zerop num) (= mid end))
start mid) end)
(insert (make-string num ?\s))))
;; Command run for the second time.
((not (equal orig-pos (point)))
(delete-region (point) orig-pos))
;; Command run for the third time.
(t
(insert (cddr cycle-spacing--context))
(goto-char (cadr cycle-spacing--context))
(setq cycle-spacing--context nil)))))
我可以稍微简化一下,因为我不需要有条件地删除换行符并留下 n 个剩余空格。