升级了一下,支持 3 种规则定义:
- 字符
?\{ ;; 禁止输入 { 时候补全
- 字符-字符串
(?\{ . ":{") ;; 禁止输入 :{ 时候补全,但不禁止其他情况下的 { 补全
- 字符-函数
(?\{ . (lambda (_c) ;; 函数可以做更多的事,以下实现的是 2. 的效果
(eq ?: (char-before (1- (point))))))
完整代码:
;;; pair.el --- Electric Pair -*- lexical-binding: t; -*-
(defvar electric-pair-inhibit-predicate-mode-chars-alist
'((t . nil))
"A list of major-mode and inhibit chars.
Each element is in the form of (MODE . (CHAR/CHAR-STRING/CHAR-FUNCTION ...)).
MODE
A mode, or t for all modes.
CHAR
A character to match the input. for example:
?\{
CHAR-STRING
A pair of character and string, the character to match the input,
the string for ‘looking-back’. for example:
(?\{ . \":{\")
CHAR-FUNCTION
A pair of character and function, the character to match the input,
the function accept the input character as parameter. for example:
(?\{ . (lambda (_c)
(eq ?: (char-before (1- (point))))))")
(defun electric-pair-inhibit-predicate-function (c)
(let ((alist
(append
(assoc-default major-mode electric-pair-inhibit-predicate-mode-chars-alist)
(assoc-default t electric-pair-inhibit-predicate-mode-chars-alist))))
(or (cl-member c
alist
:test
(lambda (c it)
(cond
((characterp it) (equal c it))
((and (consp it) (equal c (car it)))
(cond ((stringp (cdr it)) (looking-back (cdr it) 1))
((functionp (cdr it)) (funcall (cdr it) c)))))))
(electric-pair-default-inhibit c))))
(with-eval-after-load 'elec-pair
(setq electric-pair-inhibit-predicate
#'electric-pair-inhibit-predicate-function))
;;; pair.el ends here