all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Fernando de Morais <fernandodemorais.jf@gmail.com>
Cc: Daniel Mendler <mail@daniel-mendler.de>, 72210@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	[thread overview]
Message-ID: <jwvfrppj68r.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87r09fr4zt.fsf@gmail.com> (Fernando de Morais's message of "Thu,  19 Sep 2024 13:19:02 -0300")

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

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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: icomplete.patch --]
[-- Type: text/x-diff, Size: 3473 bytes --]

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

  reply	other threads:[~2024-09-24 17:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-20 14:22 bug#72210: 31.0.50; Feature request: multi-category support in `icomplete-fido-kill' Fernando de Morais
2024-07-25  7:42 ` Eli Zaretskii
2024-08-17  8:08   ` Eli Zaretskii
2024-08-31  7:57     ` Eli Zaretskii
2024-08-31 22:14       ` João Távora
2024-09-01  4:48         ` Eli Zaretskii
2024-09-01 14:46 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-02 16:23   ` Fernando de Morais
2024-09-14  7:42     ` Eli Zaretskii
2024-09-17 21:15     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-19 16:19       ` Fernando de Morais
2024-09-24 17:50         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-10-14  1:57           ` Fernando de Morais
2024-10-14 12:59             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

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=jwvfrppj68r.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=72210@debbugs.gnu.org \
    --cc=fernandodemorais.jf@gmail.com \
    --cc=mail@daniel-mendler.de \
    --cc=monnier@iro.umontreal.ca \
    /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.