unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#50228: [PATCH] 'command-completion-using-modes-p' doesn't work with multiple modes
@ 2021-08-27 10:38 Johannes Maier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2021-08-27 15:31 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 2+ messages in thread
From: Johannes Maier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2021-08-27 10:38 UTC (permalink / raw)
  To: 50228

[-- Attachment #1: Type: text/plain, Size: 1745 bytes --]

Hello everyone,

I'm using an up-to-date build of Emacs on NixOS and on Ubuntu (M-x
emacs-version yields "GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu,
GTK+ Version 3.24.30, cairo version 1.16.0").

I tried writing a command that would only show up in M-x TAB in certain
modes, using the new MODES argument to 'interactive', like so:

(setq read-extended-command-predicate
      #'command-completion-default-include-p)

(defun my-foo ()
  (interactive nil haskell-mode emacs-lisp-mode)
  (message "foo"))

I would have expected the command 'my-foo' to be shown in an Elisp
buffer, but hidden in fundamental-mode.  But 'my-foo' never shows up (it
works in the case where the 'interactive' call only specifies one mode,
though).

I checked the code that is called and found that
'command-completion-using-modes-p' seems to be missing an 'or' around
the predicates in the alternative case.  Digging a little deeper I found
the predicate function 'command-completion-with-modes-p' that already
seems to do what's needed for the other predicate, but with slightly
different arguments.  In particular the single-mode case seems to be
handled correctly by the code in 'command-completion-with-modes-p' as
well.

I've attached a patch that removes the duplicated checks in these two
functions and simply calls 'command-completion-with-modes-p' from
'command-completion-using-modes-p'.  Now it works for me with one or
more (major or minor) modes in 'interactive'.

I'm very much looking forward to feedback on whether that's a "good"
solution and hope I gave all the information that's needed.

(I didn't see any tests for those functions, but I'm not sure how to go
about those yet.  Could probably have look into those, too.)

Thank you,
Johannes


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: command-completion-using-modes-p.patch --]
[-- Type: text/x-patch, Size: 1922 bytes --]

From 08d6f48b4f7a295d3fd590ad6e5723beb3f0eadf Mon Sep 17 00:00:00 2001
From: Johannes Maier <johannes.maier@mailbox.org>
Date: Fri, 27 Aug 2021 09:40:08 +0200
Subject: [PATCH] Fix 'command-completion-using-modes-p' in case of multiple
 modes

* lisp/simple.el: Use `command-completion-with-modes-p' inside of
`command-completion-using-modes-p' instead of performing a faulty
check (`or' was missing) in the case where `command-modes' returns
multiple modes.
---
 lisp/simple.el | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)

diff --git a/lisp/simple.el b/lisp/simple.el
index db083cfc25..82ab8927a7 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -2067,24 +2067,9 @@ read-extended-command
        t nil 'extended-command-history))))
 
 (defun command-completion-using-modes-p (symbol buffer)
-  "Say whether SYMBOL has been marked as a mode-specific command in BUFFER."
-  ;; Check the modes.
-  (let ((modes (command-modes symbol)))
-    ;; Common case: Just a single mode.
-    (if (null (cdr modes))
-        (or (provided-mode-derived-p
-             (buffer-local-value 'major-mode buffer) (car modes))
-            (memq (car modes)
-                  (buffer-local-value 'local-minor-modes buffer))
-            (memq (car modes) global-minor-modes))
-      ;; Uncommon case: Multiple modes.
-      (apply #'provided-mode-derived-p
-             (buffer-local-value 'major-mode buffer)
-             modes)
-      (seq-intersection modes
-                        (buffer-local-value 'local-minor-modes buffer)
-                        #'eq)
-      (seq-intersection modes global-minor-modes #'eq))))
+  "Say whether SYMBOL has been marked as a mode-specific command in
+BUFFER."
+  (command-completion-with-modes-p (command-modes symbol) buffer))
 
 (defun command-completion-default-include-p (symbol buffer)
   "Say whether SYMBOL should be offered as a completion.
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#50228: [PATCH] 'command-completion-using-modes-p' doesn't work with multiple modes
  2021-08-27 10:38 bug#50228: [PATCH] 'command-completion-using-modes-p' doesn't work with multiple modes Johannes Maier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2021-08-27 15:31 ` Lars Ingebrigtsen
  0 siblings, 0 replies; 2+ messages in thread
From: Lars Ingebrigtsen @ 2021-08-27 15:31 UTC (permalink / raw)
  To: Johannes Maier; +Cc: 50228

Johannes Maier <johannes.maier@mailbox.org> writes:

> I've attached a patch that removes the duplicated checks in these two
> functions and simply calls 'command-completion-with-modes-p' from
> 'command-completion-using-modes-p'.  Now it works for me with one or
> more (major or minor) modes in 'interactive'.

Thanks, that does fix the problem.

However, the reason `command-completion-using-modes-p' is coded that way
is that `command-completion-with-modes-p' is slower in the common case,
and completion has to be fast.  But in the multiple-mode case, we can
indeed just call `command-completion-with-modes-p', so I've kept the
common case, but punted to `command-completion-with-modes-p' in the
multiple-mode case, and that seems to fix the issue.

> (I didn't see any tests for those functions, but I'm not sure how to go
> about those yet.  Could probably have look into those, too.)

Yes, there really should be tests in this area...

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-27 15:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-27 10:38 bug#50228: [PATCH] 'command-completion-using-modes-p' doesn't work with multiple modes Johannes Maier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-27 15:31 ` Lars Ingebrigtsen

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).