From: Alan Mackenzie Date: Mon, 8 Jul 2019 09:24:29 +0000 (+0000) Subject: Fix bug #36474, such that CC Mode quotes work properly in electric-pair-mode X-Git-Tag: emacs-27.0.90~2059 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=8d5cdd688f5dd39c8e667f9c6c2c4876c7fdc564;p=emacs.git Fix bug #36474, such that CC Mode quotes work properly in electric-pair-mode Also finishes the fix for bug #36423. * lisp/progmodes/cc-mode.el (c-initialize-cc-mode): Add an `eval-after-load' to set electric-pair-inhibit-predicate for existing CC Mode buffers when elec-pair.elc gets loaded. (c-basic-common-init): Set electric-pair-inhibit-predicate when a CC Mode mode gets initialized. (c-electric-pair-inhibit-predicate): New function. --- diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el index b45c2e5fd38..98b8385fccb 100644 --- a/lisp/progmodes/cc-mode.el +++ b/lisp/progmodes/cc-mode.el @@ -226,6 +226,15 @@ control). See \"cc-mode.el\" for more info." (if (boundp 'c-comment-continuation-stars) (setq c-block-comment-prefix c-comment-continuation-stars)) (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode) + ;; Connect up with Emacs's electric-pair-mode + (eval-after-load "elec-pair" + '(when (boundp 'electric-pair-inhibit-predicate) + (dolist (buf (buffer-list)) + (with-current-buffer buf + (when c-buffer-is-cc-mode + (make-local-variable 'electric-pair-inhibit-predicate) + (setq electric-pair-inhibit-predicate + #'c-electric-pair-inhibit-predicate)))))) (setq c-initialization-ok t) ;; Connect up with Emacs's electric-indent-mode, for >= Emacs 24.4 (when (fboundp 'electric-indent-local-mode) @@ -553,6 +562,17 @@ that requires a literal mode spec at compile time." (make-local-variable 'adaptive-fill-regexp) (make-local-variable 'fill-paragraph-handle-comment) + (setq c-buffer-is-cc-mode mode) + + ;; Prepare for the use of `electric-pair-mode'. Note: if this mode is not + ;; yet loaded, `electric-pair-inhibit-predicate' will get set from an + ;; `eval-after-load' form in `c-initialize-cc-mode' when elec-pair.elc is + ;; loaded. + (when (boundp 'electric-pair-inhibit-predicate) + (make-local-variable 'electric-pair-inhibit-predicate) + (setq electric-pair-inhibit-predicate + #'c-electric-pair-inhibit-predicate)) + ;; now set their values (set (make-local-variable 'parse-sexp-ignore-comments) t) (set (make-local-variable 'indent-line-function) 'c-indent-line) @@ -2254,6 +2274,26 @@ This function is called from `c-common-init', once per mode initialization." (setq c-electric-flag electric-indent-mode) (c-update-modeline))) + +;; Connection with Emacs's electric-pair-mode +(defun c-electric-pair-inhibit-predicate (char) + "Return t to inhibit the insertion of a second copy of CHAR. + +At the time of call, point is just after the newly inserted CHAR. + +When CHAR is \", t will be returned unless the \" is marked with +a string fence syntax-table text property. For other characters, +the default value of `electric-pair-inhibit-predicate' is called +and its value returned. + +This function is the appropriate value of +`electric-pair-inhibit-predicate' for CC Mode modes, which mark +invalid strings with such a syntax table text property on the +opening \" and the next unescaped end of line." + (if (eq char ?\") + (not (equal (get-text-property (1- (point)) 'syntax-table) '(15))) + (funcall (default-value 'electric-pair-inhibit-predicate) char))) + ;; Support for C