From: Alan Mackenzie <acm@muc.de>
To: Eli Zaretskii <eliz@gnu.org>, yyoncho@gmail.com
Cc: 38406@debbugs.gnu.org
Subject: bug#38406: 27.0.50; post-self-insert-hook does not hold its contract in cc-mode derived modes
Date: Wed, 4 Dec 2019 20:41:59 +0000 [thread overview]
Message-ID: <20191204204159.GA7587@ACM> (raw)
In-Reply-To: <83blsrn58a.fsf@gnu.org>
Hello, Eli and Ivan.
On Sun, Dec 01, 2019 at 22:47:01 +0200, Eli Zaretskii wrote:
> > Date: Sun, 1 Dec 2019 19:27:09 +0000
> > Cc: yyoncho@gmail.com, 38406@debbugs.gnu.org
> > From: Alan Mackenzie <acm@muc.de>
[ .... ]
> But breaking a hook's contract as a means to teach people not to shoot
> themselves in the foot is not right. If the uses are legitimate, they
> should be able to do them; if they aren't, let them cope with the
> consequences.
> > So to call this hook at the end of c-electric-brace would mean
> > having to filter the hook first (at the very least, to remove
> > electric-pair-post-self-insert-function), which just seems very
> > hackish and unsatisfactory.
> It doesn't seem too hackish to me, and as a nice bonus we will have
> post-self-insert-hook act as per its contract again.
> So could you please do that? TIA.
OK, here's a patch which I think does just what's wanted. Would you
please try it out, Ivan, then let me know that it works, or about any
problems which there still may be? Thanks.
It's merely necessary to apply the patch below, byte compile cc-cmds.el,
and load it. (In the highly unlikely event you want any help with the
patch or the compilation, feel free to send me private email.) The
patch should apply cleanly to the Emacs master branch.
diff -r d20020192bd6 cc-cmds.el
--- a/cc-cmds.el Sat Nov 30 21:10:11 2019 +0000
+++ b/cc-cmds.el Wed Dec 04 20:15:11 2019 +0000
@@ -493,6 +493,34 @@
(c-hungry-delete-forward)
(c-hungry-delete-backwards)))
+(defvar c--unsafe-post-self-insert-hook-functions
+ '(smie-blink-matching-open
+ electric-pair-post-self-insert-function
+ blink-paren-post-self-insert-function
+ electric-indent-post-self-insert-function
+ electric-layout-post-self-insert-function
+ electric-quote-post-self-insert-function)
+ "Known unsafe functions when members of `post-self-insert-hook' in CC Mode")
+
+(defun c--call-post-self-insert-hook-more-safely ()
+ ;; Call post-self-insert-hook, having removed from `post-self-insert-hook'
+ ;; function known not to be safe to CC Mode. The result is of no
+ ;; significance. Note that the hook call is NOT absolutely safe.
+ (let ((src post-self-insert-hook)
+ dest)
+ (while src
+ (cond
+ ((memq (car src) c--unsafe-post-self-insert-hook-functions))
+ ((eq (car src) t)
+ (let ((src (default-value 'post-self-insert-hook)))
+ (while src
+ (unless (memq (car src) c--unsafe-post-self-insert-hook-functions)
+ (add-hook dest (car src) t)) ; Preserve the order of the functions.
+ (setq src (cdr src)))))
+ (t (add-hook dest (car src) t))) ; Preserve the order of the functions.
+ (setq src (cdr src)))
+ (run-hooks dest)))
+
(defun c-electric-pound (arg)
"Insert a \"#\".
If `c-electric-flag' is set, handle it specially according to the variable
@@ -522,7 +550,8 @@
(insert (c-last-command-char))
(and (not bolp)
(goto-char (- (point-max) pos)))
- )))
+ ))
+ (c--call-post-self-insert-hook-more-safely))
(defun c-point-syntax ()
;; Return the syntactic context of the construct at point. (This is NOT
@@ -903,7 +932,8 @@
(save-excursion
(c-save-buffer-state nil
(c-backward-syntactic-ws safepos))
- (funcall old-blink-paren)))))
+ (funcall old-blink-paren)))
+ (c--call-post-self-insert-hook-more-safely)))
(defun c-electric-slash (arg)
"Insert a slash character.
@@ -955,7 +985,8 @@
(let (post-self-insert-hook) ; Disable random functionality.
(self-insert-command (prefix-numeric-value arg)))
(if indentp
- (indent-according-to-mode))))
+ (indent-according-to-mode))
+ (c--call-post-self-insert-hook-more-safely)))
(defun c-electric-star (arg)
"Insert a star character.
@@ -985,7 +1016,8 @@
(bolp))))
(let (c-echo-syntactic-information-p) ; shut this up
(indent-according-to-mode))
- ))
+ )
+ (c--call-post-self-insert-hook-more-safely))
(defun c-electric-semi&comma (arg)
"Insert a comma or semicolon.
@@ -1057,8 +1089,8 @@
(setq add-newline-p (not (eq answer 'stop)))
))
(if add-newline-p
- (c-newline-and-indent))
- )))))
+ (c-newline-and-indent)))))
+ (c--call-post-self-insert-hook-more-safely)))
(defun c-electric-colon (arg)
"Insert a colon.
@@ -1160,8 +1192,8 @@
;; does a newline go after the colon?
(if (and (memq 'after (cdr-safe newlines))
(not is-scope-op))
- (c-newline-and-indent))
- ))))
+ (c-newline-and-indent))))
+ (c--call-post-self-insert-hook-more-safely)))
(defun c-electric-lt-gt (arg)
"Insert a \"<\" or \">\" character.
@@ -1251,7 +1283,8 @@
;; From now (2016-01-01), the syntax-table text properties on < and >
;; are applied in an after-change function, not during redisplay. Hence
;; we no longer need to call (sit-for 0) for blink paren to work.
- (funcall blink-paren-function)))))
+ (funcall blink-paren-function))))
+ (c--call-post-self-insert-hook-more-safely))
(defun c-electric-paren (arg)
"Insert a parenthesis.
@@ -1370,7 +1403,8 @@
;; Apply `electric-pair-mode' stuff inside a string or comment.
(when (and (boundp 'electric-pair-mode) electric-pair-mode)
(let (post-self-insert-hook)
- (electric-pair-post-self-insert-function))))))
+ (electric-pair-post-self-insert-function))))
+ (c--call-post-self-insert-hook-more-safely)))
(defun c-electric-continued-statement ()
"Reindent the current line if appropriate.
--
Alan Mackenzie (Nuremberg, Germany).
next prev parent reply other threads:[~2019-12-04 20:41 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-27 20:00 bug#38406: 27.0.50; post-self-insert-hook does not hold its contract in cc-mode derived modes yyoncho
2019-11-30 14:36 ` Alan Mackenzie
2019-12-01 10:02 ` yyoncho
2019-12-01 15:07 ` Alan Mackenzie
2019-12-01 15:27 ` yyoncho
2019-12-01 15:58 ` Alan Mackenzie
2019-12-01 18:03 ` Eli Zaretskii
2019-12-02 18:37 ` Alan Mackenzie
2019-12-01 17:59 ` Eli Zaretskii
2019-12-01 19:27 ` Alan Mackenzie
2019-12-01 20:47 ` Eli Zaretskii
2019-12-02 18:31 ` Alan Mackenzie
2019-12-02 20:17 ` Eli Zaretskii
2019-12-04 20:41 ` Alan Mackenzie [this message]
2019-12-04 21:04 ` Dmitry Gutov
2019-12-05 19:14 ` Alan Mackenzie
2019-12-05 20:44 ` Dmitry Gutov
2019-12-05 14:45 ` Eli Zaretskii
2019-12-05 19:09 ` Alan Mackenzie
2019-12-05 19:25 ` Eli Zaretskii
2019-12-05 20:17 ` Alan Mackenzie
2019-12-06 8:06 ` Eli Zaretskii
2019-12-06 18:28 ` Alan Mackenzie
2019-12-06 18:48 ` Eli Zaretskii
2019-12-06 22:24 ` Alan Mackenzie
2019-12-07 8:21 ` Eli Zaretskii
2019-12-07 11:40 ` Alan Mackenzie
2019-12-07 13:27 ` Eli Zaretskii
2019-12-07 19:03 ` Alan Mackenzie
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191204204159.GA7587@ACM \
--to=acm@muc.de \
--cc=38406@debbugs.gnu.org \
--cc=eliz@gnu.org \
--cc=yyoncho@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).