Subject: bug#73499: 31.0.50; sql-interactive-mode problem with package caps-lock-mode

From: Stephen Berman <[email-masked]>
Date: Sat, 05 Oct 2024 22:03:59 +0200

On Sat, 05 Oct 2024 13:22:02 +0300 Eli Zaretskii <[email-masked]> wrote:

>> From: Andrea Corallo <[email-masked]>
>> Cc: Eli Zaretskii <[email-masked]>,  Stephen Berman <[email-masked]>,
>>   Stefan Kangas <[email-masked]>,  [email-masked]
>> Date: Tue, 01 Oct 2024 14:44:13 -0400
>>
>> Stefan Monnier <[email-masked]> writes:
>>
>> >> Stefan, Andrea, Stefan: any opinions on which way is better?
>> >
>> > My vote is clearly for `post-self-insert-hook`.
>>
>> Same
>
> OK, so Stephen, please do it that way, and thanks.

Sure; however, my testing of the patch before posting it was inadequate,
and the patch needs to be amended.  As is it now, if you customize
sql-electric-stuff to use sql-magic-go and then type "go" at the prompt
in a comint-derived mode other than sql-interactive-mode, that wrongly
calls comint-send-input.  E.g. in shell-mode, this results in the shell
output "bash: go: command not found".  And in an arbitary buffer not
derived from comint-mode, typing "go" at BOB causes a ding and shows the
message "Current buffer has no process".  These problems are because the
patch adds sql-magic-go to post-self-insert-hook globally; since this is
done when customizing sql-electric-stuff, the current buffer need not
(and probably won't) be in sql-interactive-mode, so the hook can't be
added locally.  AFAICS the simplest fix is to check (eq major-mode
'sql-interactive-mode) in sql-magic-go, as in the attached patch.  If
there's no objection to this, then I'll go ahead and commit the amended
patch to master.

Steve Berman

diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el
index a0b350ce54f..13bf5e874b0 100644
--- a/lisp/progmodes/sql.el
+++ b/lisp/progmodes/sql.el
@@ -869,7 +869,13 @@ sql-electric-stuff
   :type '(choice (const :tag "Nothing" nil)
 		 (const :tag "The semicolon `;'" semicolon)
 		 (const :tag "The string `go' by itself" go))
-  :version "20.8")
+  :initialize #'custom-initialize-default
+  :set (lambda (symbol value)
+         (custom-set-default symbol value)
+         (if (eq value 'go)
+             (add-hook 'post-self-insert-hook 'sql-magic-go)
+           (remove-hook 'post-self-insert-hook 'sql-magic-go)))
+  :version "31.1")

 (defcustom sql-send-terminator nil
   "When non-nil, add a terminator to text sent to the SQL interpreter.
@@ -1359,8 +1365,6 @@ sql-interactive-mode-map
   :parent comint-mode-map
   "C-j"       #'sql-accumulate-and-indent
   "C-c C-w"   #'sql-copy-column
-  "O"         #'sql-magic-go
-  "o"         #'sql-magic-go
   ";"         #'sql-magic-semicolon
   "C-c C-l a" #'sql-list-all
   "C-c C-l t" #'sql-list-table)
@@ -3067,16 +3071,16 @@ sql-end-of-statement

 ;;; Small functions

-(defun sql-magic-go (arg)
+(defun sql-magic-go ()
   "Insert \"o\" and call `comint-send-input'.
 `sql-electric-stuff' must be the symbol `go'."
-  (interactive "P")
-  (self-insert-command (prefix-numeric-value arg))
-  (if (and (equal sql-electric-stuff 'go)
-	   (save-excursion
-	     (comint-bol nil)
-	     (looking-at "go\\b")))
-      (comint-send-input)))
+  (and (eq major-mode 'sql-interactive-mode)
+       (equal sql-electric-stuff 'go)
+       (or (eq last-command-event ?o) (eq last-command-event ?O))
+       (save-excursion
+	 (comint-bol nil)
+	 (looking-at "go\\b"))
+       (comint-send-input)))
 (put 'sql-magic-go 'delete-selection t)

 (defun sql-magic-semicolon (arg)