emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

scratch/command 0bb2d27 1/2: Add support for marking autoloads with mode


From: Lars Ingebrigtsen
Subject: scratch/command 0bb2d27 1/2: Add support for marking autoloads with modes and also declare completion
Date: Fri, 12 Feb 2021 06:13:49 -0500 (EST)

branch: scratch/command
commit 0bb2d277272142465e3af266e6847359111cbcee
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Add support for marking autoloads with modes and also declare completion
---
 doc/lispref/functions.texi  |  7 +++++++
 doc/lispref/loading.texi    |  3 +++
 lisp/emacs-lisp/byte-run.el |  8 +++++++-
 lisp/simple.el              | 31 +++++++++++++++++++------------
 src/data.c                  |  8 +++++++-
 src/eval.c                  |  9 ++++++++-
 6 files changed, 51 insertions(+), 15 deletions(-)

diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index 414035f..9929739 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -2309,6 +2309,13 @@ form @code{(lambda (@var{arg}) @var{body})} in which 
case that function will
 additionally have access to the macro (or function)'s arguments and it will
 be passed to @code{gv-define-setter}.
 
+@item (completion @var{completion-predicate})
+Declare @var{completion-predicate} as a function to determine whether
+to include the symbol in the list of functions when asking for
+completions in @kbd{M-x}.  @var{completion-predicate} is called with
+two parameters: The first parameter is the symbol, and the second is
+the current buffer.
+
 @end table
 
 @end defmac
diff --git a/doc/lispref/loading.texi b/doc/lispref/loading.texi
index 22f0dde..90262df 100644
--- a/doc/lispref/loading.texi
+++ b/doc/lispref/loading.texi
@@ -510,6 +510,9 @@ specification is not given here; it's not needed unless the 
user
 actually calls @var{function}, and when that happens, it's time to load
 the real definition.
 
+If @var{interactive} is a list, it is interpreted as a list of modes
+this command is applicable for.
+
 You can autoload macros and keymaps as well as ordinary functions.
 Specify @var{type} as @code{macro} if @var{function} is really a macro.
 Specify @var{type} as @code{keymap} if @var{function} is really a
diff --git a/lisp/emacs-lisp/byte-run.el b/lisp/emacs-lisp/byte-run.el
index 88f362d..4603b7c 100644
--- a/lisp/emacs-lisp/byte-run.el
+++ b/lisp/emacs-lisp/byte-run.el
@@ -143,6 +143,11 @@ The return value of this function is not used."
       (list 'function-put (list 'quote f)
             ''lisp-indent-function (list 'quote val))))
 
+(defalias 'byte-run--set-completion
+  #'(lambda (f _args val)
+      (list 'function-put (list 'quote f)
+            ''completion-predicate (list 'quote val))))
+
 ;; Add any new entries to info node `(elisp)Declare Form'.
 (defvar defun-declarations-alist
   (list
@@ -159,7 +164,8 @@ This may shift errors from run-time to compile-time.")
 If `error-free', drop calls even if `byte-compile-delete-errors' is nil.")
    (list 'compiler-macro #'byte-run--set-compiler-macro)
    (list 'doc-string #'byte-run--set-doc-string)
-   (list 'indent #'byte-run--set-indent))
+   (list 'indent #'byte-run--set-indent)
+   (list 'completion #'byte-run--set-completion))
   "List associating function properties to their macro expansion.
 Each element of the list takes the form (PROP FUN) where FUN is
 a function.  For each (PROP . VALUES) in a function's declaration,
diff --git a/lisp/simple.el b/lisp/simple.el
index 5307f5e..f9d9558 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1900,17 +1900,17 @@ to get different commands to edit and resubmit."
 (defvar extended-command-history nil)
 (defvar execute-extended-command--last-typed nil)
 
-(defcustom read-extended-command-predicate #'command-for-mode-p
+(defcustom read-extended-command-predicate #'completion-major-mode-p
   "Predicate to use to determine which commands to include when completing."
   :version "28.1"
   :type '(choice (const :tag "Exclude commands not relevant to this mode"
-                        #'command-for-mode-p)
-                 (const :tag "All commands" #'commandp)
+                        #'completion-major-mode-p)
+                 (const :tag "All commands" (lambda (_ _) t))
                  (function :tag "Other function")))
 
 (defun read-extended-command ()
   "Read command name to invoke in `execute-extended-command'."
-  (let ((current-buffer (current-buffer)))
+  (let ((buffer (current-buffer)))
     (minibuffer-with-setup-hook
         (lambda ()
           (add-hook 'post-self-insert-hook
@@ -1960,17 +1960,24 @@ to get different commands to edit and resubmit."
               (category . command))
            (complete-with-action action obarray string pred)))
        (lambda (sym)
-         (with-current-buffer current-buffer
-           (funcall read-extended-command-predicate sym)))
+         (and (commandp sym)
+              (funcall read-extended-command-predicate sym buffer)))
        t nil 'extended-command-history))))
 
-(defun command-for-mode-p (symbol)
+(defun completion-major-mode-p (symbol buffer)
+  "Say whether SYMBOL should be offered as a completion.
+This is true if the command is applicable to the major mode in
+BUFFER."
+  (or (null (command-modes symbol))
+      (apply #'provided-mode-derived-p
+             (buffer-local-value 'major-mode buffer)
+             (command-modes symbol))))
+
+(defun completion-on-button-p (symbol buffer)
   "Say whether SYMBOL should be offered as a completion.
-This is true if it's a command and the command is applicable to
-the current major mode."
-  (and (commandp symbol)
-       (or (null (command-modes symbol))
-           (apply #'derived-mode-p (command-modes symbol)))))
+This is true if SYMBOL is a command that's in the local map at
+the current point in BUFFER."
+  )
 
 (defun read-extended-command--affixation (command-names)
   (with-selected-window (or (minibuffer-selected-window) (selected-window))
diff --git a/src/data.c b/src/data.c
index 4b2ecc1..e9d209e 100644
--- a/src/data.c
+++ b/src/data.c
@@ -986,7 +986,13 @@ The value, if non-nil, is a list of mode name symbols.  */)
     }
 #endif
   else if (AUTOLOADP (fun))
-    return Qnil;
+    {
+      Lisp_Object modes = Fnth (make_int (3), fun);
+      if (CONSP (modes))
+       return modes;
+      else
+       return Qnil;
+    }
   else if (CONSP (fun))
     {
       Lisp_Object funcar = XCAR (fun);
diff --git a/src/eval.c b/src/eval.c
index 869a283..c862128 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -2087,14 +2087,21 @@ then strings and vectors are not accepted.  */)
 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
        doc: /* Define FUNCTION to autoload from FILE.
 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
+
 Third arg DOCSTRING is documentation for the function.
-Fourth arg INTERACTIVE if non-nil says function can be called interactively.
+
+Fourth arg INTERACTIVE if non-nil says function can be called
+interactively.  If INTERACTIVE is a list, it is interpreted as a list
+of modes the function is applicable for.
+
 Fifth arg TYPE indicates the type of the object:
    nil or omitted says FUNCTION is a function,
    `keymap' says FUNCTION is really a keymap, and
    `macro' or t says FUNCTION is really a macro.
+
 Third through fifth args give info about the real definition.
 They default to nil.
+
 If FUNCTION is already defined other than as an autoload,
 this does nothing and returns nil.  */)
   (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object 
interactive, Lisp_Object type)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]