我使用下面的代码并绑定yf/replace-or-delete-pair
到M-D
。
用法示例:指向on时(
,我命中M-D [
,这()
对成为一[]
对。如果您M-D RET
改为点击,则该对将被删除。
该代码使用语法表,这意味着对于某些对,您必须自己指定结束括号。例如,在HTML模式下,()
可以通过更换<>
击中M-D <
。但是,在许多模式<>
下都不是公认的对,M-D <
而是会说“不知道如何关闭<”。然后就可以键入了>
。
(defun yf/replace-or-delete-pair (open)
"Replace pair at point by OPEN and its corresponding closing character.
The closing character is lookup in the syntax table or asked to
the user if not found."
(interactive
(list
(read-char
(format "Replacing pair %c%c by (or hit RET to delete pair):"
(char-after)
(save-excursion
(forward-sexp 1)
(char-before))))))
(if (memq open '(?\n ?\r))
(delete-pair)
(let ((close (cdr (aref (syntax-table) open))))
(when (not close)
(setq close
(read-char
(format "Don't know how to close character %s (#%d) ; please provide a closing character: "
(single-key-description open 'no-angles)
open))))
(yf/replace-pair open close))))
(defun yf/replace-pair (open close)
"Replace pair at point by respective chars OPEN and CLOSE.
If CLOSE is nil, lookup the syntax table. If that fails, signal
an error."
(let ((close (or close
(cdr-safe (aref (syntax-table) open))
(error "No matching closing char for character %s (#%d)"
(single-key-description open t)
open)))
(parens-require-spaces))
(insert-pair 1 open close))
(delete-pair)
(backward-char 1))
\bigl(...\bigr)
对\Bigl(...\Bigr)
等