all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: 33595@debbugs.gnu.org
Subject: bug#33595: [PATCH] RE: bug#33595: 26; Have `try-completion' or `completion--done' run abnormal hook if sole completion
Date: Mon, 3 Dec 2018 10:52:55 -0800 (PST)	[thread overview]
Message-ID: <5de4751e-8389-4400-aafe-9225e9470f01@default> (raw)
In-Reply-To: <fb6c5be5-ef79-484e-a150-a62c7fe26ac1@default>

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

The attached patch implements this enhancement.

Simple example use cases:

(defun my-find-file-other-window (filename &optional wildcards)
  "`find-file-other-window', but show file info if only one completion matches."
  (interactive
   (unwind-protect
       (progn
         (add-hook 'completion-sole-match-functions 'describe-file)
         (find-file-read-args "Find file in other window: "
                              (confirm-nonexistent-file-or-buffer)))
     (remove-hook 'completion-sole-match-functions 'describe-file)))
  (find-file-other-window filename wildcards))

(defun my-describe-function (function)
  "`describe-function', but show output if only one completion matches."
  (interactive
   (unwind-protect
       (progn
         (add-hook 'completion-sole-match-functions
                   (lambda (fn) (describe-function (intern fn))))
         (let* ((fn (function-called-at-point))
                (enable-recursive-minibuffers t)
                (val (completing-read
                      (if fn
                          (format "Describe function (default %s): " fn)
                        "Describe function: ")
                      #'help--symbol-completion-table
                      (lambda (f)
                        (or (fboundp f) (get f 'function-documentation)))
                      t nil nil
                      (and fn (symbol-name fn)))))
           (unless (equal val "")
             (setq fn (intern val)))
           (unless (and fn (symbolp fn))
             (user-error "You didn't specify a function symbol"))
           (unless (or (fboundp fn) (get fn 'function-documentation))
             (user-error "Symbol's function definition is void: %s" fn))
           (list fn))))
   (remove-hook 'completion-sole-match-functions
                (lambda (fn) (describe-function (intern fn)))))
  (describe-function function))

----

Or, using macro `with-hook-added' (see bug #33601):

(defun my-find-file-other-window (filename &optional wildcards)
  "`find-file-other-window', but show file info if only one completion matches."
  (interactive
   (with-hook-added completion-sole-match-functions
     describe-file
     (find-file-read-args "Find file in other window: "
                          (confirm-nonexistent-file-or-buffer))))
  (find-file-other-window filename wildcards))

(defun my-describe-function (function)
  "`describe-function', but show output if only one completion matches."
  (interactive
   (with-hook-added completion-sole-match-functions
     (lambda (fn) (describe-function (intern fn)))
     (let* ((fn (function-called-at-point))
            (enable-recursive-minibuffers t)
            (val (completing-read
                  (if fn
                      (format "Describe function (default %s): " fn)
                    "Describe function: ")
                  #'help--symbol-completion-table
                  (lambda (f)
                    (or (fboundp f) (get f 'function-documentation)))
                  t nil nil
                  (and fn (symbol-name fn)))))
       (unless (equal val "")
         (setq fn (intern val)))
       (unless (and fn (symbolp fn))
         (user-error "You didn't specify a function symbol"))
       (unless (or (fboundp fn) (get fn 'function-documentation))
         (user-error "Symbol's function definition is void: %s" fn))
       (list fn))))
  (describe-function function))

[-- Attachment #2: minibuffer-2018-12-03a.patch --]
[-- Type: application/octet-stream, Size: 1003 bytes --]

diff -u minibuffer.el minibuffer-2018-12-03a-patched.el
--- minibuffer.el	2018-12-03 09:23:45.858608200 -0800
+++ minibuffer-2018-12-03a-patched.el	2018-12-03 09:27:17.862397300 -0800
@@ -753,6 +753,10 @@
 the second failed attempt to complete."
   :type '(choice (const nil) (const t) (const lazy)))
 
+(defvar completion-sole-match-functions ()
+  "Functions to be run when completion results in only one match.
+Each function must accept that completion as its first arg.")
+
 (defconst completion-styles-alist
   '((emacs21
      completion-emacs21-try-completion completion-emacs21-all-completions
@@ -1769,6 +1773,8 @@
   (let* ((exit-fun (plist-get completion-extra-properties :exit-function))
          (pre-msg (and exit-fun (current-message))))
     (cl-assert (memq finished '(exact sole finished unknown)))
+    (when (eq finished 'finished)
+      (run-hook-with-args 'completion-sole-match-functions string))
     (when exit-fun
       (when (eq finished 'unknown)
         (setq finished

  reply	other threads:[~2018-12-03 18:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-03  4:07 bug#33595: 26; Have `try-completion' or `completion--done' run abnormal hook if sole completion Drew Adams
2018-12-03 18:52 ` Drew Adams [this message]
2018-12-28 18:56   ` bug#33595: [PATCH] " Drew Adams
2018-12-29  8:08     ` Eli Zaretskii
2019-01-02  1:23       ` Stefan Monnier
2019-01-02  4:41 ` Stefan Monnier
2019-01-02  7:11   ` Drew Adams
2019-01-02 15:27     ` Stefan Monnier
2019-01-02 16:20       ` Drew Adams
2019-01-03  3:15         ` Stefan Monnier

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5de4751e-8389-4400-aafe-9225e9470f01@default \
    --to=drew.adams@oracle.com \
    --cc=33595@debbugs.gnu.org \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.