GNU bug report logs - #72210
31.0.50; Feature request: multi-category support in `icomplete-fido-kill'.

Previous Next

Package: emacs;

Reported by: Fernando de Morais <fernandodemorais.jf <at> gmail.com>

Date: Sat, 20 Jul 2024 14:26:01 UTC

Severity: wishlist

Found in version 31.0.50

Full log


View this message in rfc822 format

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Fernando de Morais <fernandodemorais.jf <at> gmail.com>
Cc: Daniel Mendler <mail <at> daniel-mendler.de>, 72210 <at> debbugs.gnu.org
Subject: bug#72210: 31.0.50; Feature request: multi-category support in `icomplete-fido-kill'
Date: Tue, 24 Sep 2024 13:50:31 -0400
[Message part 1 (text/plain, inline)]
>> Maybe a better option would be something like the patch below,
>> so `consult` could define its own method for its own category, which
>> could even extend the semantics to do thing like delete bookmarks when
>> applied to bookmarks, etc...
>
> This is indeed a better idea!  I'm not very familiar with `cl-*' code,
> but the way you suggest makes `icomplete-fido-kill' very flexible.  As I
> understand it, even if `consult' doesn't define its methods, we can
> still do it ourselves as end users, without needing to advise or
> override the original function.

Indeed, tho it would rely on "internal knowledge" of consult's
`multi-category`.

I'm not 100% happy with my suggestion, tho.  One of the problems is the
name (should it include "fido"?   Currently, `icomplete-fido-kill` and
`icomplete-fido-ret` can misbehave in non-fido-mode, because of an
assumption they make about the completion-style).  The other is the
confirmation prompt, which feels like it should be implemented once and
for all outside of the generic function.

For reference here's my current code.


        Stefan
[icomplete.patch (text/x-diff, inline)]
diff --git a/lisp/icomplete.el b/lisp/icomplete.el
index 2ea5e36fa88..c37275587b6 100644
--- a/lisp/icomplete.el
+++ b/lisp/icomplete.el
@@ -317,6 +317,30 @@ icomplete-vertical-goto-last
 
 ;;;_* Helpers for `fido-mode' (or `ido-mode' emulation)
 
+(cl-defgeneric icomplete-kill-candidate (category _candidate)
+  "\"Kill\" CANDIDATE, assuming it is of kind CATEGORY.
+CANDIDATE is a string denoting a completion candidate,
+CATEGORY should be a completion category, as specified
+in `completion-metadata'.
+\"Kill\" here means to actually delete the underlying object, such
+as a file, buffer, ..."
+  (error "Don't know how to kill things for category `%s'" category))
+
+(cl-defmethod icomplete-kill-candidate ((_ (eq 'buffer)) thing)
+  ;; FIXME: Shouldn't the confirmation prompting be done by the caller?
+  (when (yes-or-no-p (concat "Kill buffer " thing "? "))
+    (kill-buffer thing)))
+
+(cl-defmethod icomplete-kill-candidate ((_ (eq 'file)) thing)
+  ;; FIXME: This makes assumptions about completion style: e.g. with
+  ;; partial-completion, `/usr/s/d/ema' can result in DIR being
+  ;; `/usr/s/d/' and THING being `share/doc/emacs', in which case DIR
+  ;; isn't the right base directory to pass to `expand-file-name'!
+  (let* ((dir (file-name-directory (icomplete--field-string)))
+         (file (expand-file-name thing dir)))
+    (when (yes-or-no-p (concat "Delete file " file "? "))
+      (delete-file file) t)))
+
 (defun icomplete-fido-kill ()
   "Kill line or current completion, like `ido-mode'.
 If killing to the end of line make sense, call `kill-line',
@@ -331,26 +355,12 @@ icomplete-fido-kill
         (call-interactively 'kill-line)
       (let* ((all (completion-all-sorted-completions))
              (thing (car all))
-             (cat (icomplete--category))
-             (action
-              (cl-case cat
-                (buffer
-                 (lambda ()
-                   (when (yes-or-no-p (concat "Kill buffer " thing "? "))
-                     (kill-buffer thing))))
-                ((project-file file)
-                 (lambda ()
-                   (let* ((dir (file-name-directory (icomplete--field-string)))
-                          (path (expand-file-name thing dir)))
-                     (when (yes-or-no-p (concat "Delete file " path "? "))
-                       (delete-file path) t))))
-                (t
-                 (error "Sorry, don't know how to kill things for `%s'" cat)))))
+             (cat (icomplete--category)))
         (when (let (;; Allow `yes-or-no-p' to work and don't let it
                     ;; `icomplete-exhibit' anything.
                     (enable-recursive-minibuffers t)
                     (icomplete-mode nil))
-                (funcall action))
+                (icomplete-kill-candidate cat thing))
           (completion--cache-all-sorted-completions
            (icomplete--field-beg)
            (icomplete--field-end)
@@ -373,6 +383,8 @@ icomplete-fido-ret
                    (file-name-directory (icomplete--field-string))))
          (current (car completion-all-sorted-completions))
          (probe (and dir current
+                     ;; FIXME: Same problem as in
+                     ;; `icomplete-kill-candidate<file>' above.
                      (expand-file-name (directory-file-name current)
                                        (substitute-env-vars dir)))))
     (cond ((and probe (file-directory-p probe) (not (string= current "./")))

This bug report was last modified 65 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.