Delete file or kill buffer DWIM [Feature request] #164

Closed
kaushalmodi opened this Issue Jun 30, 2015 · 8 comments

Projects

None yet

2 participants

@kaushalmodi
Contributor

I have switched to using counsel and ivy instead of ido and this is one of the features I miss from ido:

  1. When doing C-x C-f, if the selection is on a file name that is not open in one of the buffers, C-k will delete (or trash, depending on delete-by-moving-to-trash) that file name.
  2. When switching buffers, C-k will kill the selected buffer if open. If the buffer is already closed (virtual buffer) and if that buffer was associated to a file name, that file will be deleted (or trashed).

It will be nice to have these functionalities in counsel-find-file and ivy-switch-buffer.

Currently, I need to switch to that file/buffer and then delete/kill it.

@abo-abo abo-abo added a commit that referenced this issue Jun 30, 2015
@abo-abo Add a custom keymap for ivy-switch-buffer
* ivy.el (ivy-switch-buffer-map): New defvar.

Re #164
d6452fb
@abo-abo abo-abo added a commit that referenced this issue Jun 30, 2015
@abo-abo Add a custom keymap for counsel-find-file
* counsel.el (counsel-find-file-map): New defvar.

Re #164
1aa7520
@abo-abo
Owner
abo-abo commented Jun 30, 2015

You could add something like this to your config:

(define-key
    ivy-switch-buffer-map
    (kbd "C-k")
  (lambda ()
    (interactive)
    (ivy-set-action 'kill-buffer)
    (ivy-done)))

I'm currently working on multiple actions for a single command. Similar to counsel-describe-variable, except the key bindings aren't hard coded. Maybe that could solve your problem as well:

  1. Call ivy-switch-buffer
  2. C-o to open up options.
  3. w / s to select the buffer kill action, instead of the default switch-to-buffer action.
  4. Use C-M-n to kill a bunch of buffers, while skipping some with C-n.
@abo-abo abo-abo added a commit that referenced this issue Jun 30, 2015
@abo-abo Fixup the last two commits
* counsel.el (counsel-find-file): Update.

* ivy.el (ivy--buffer-list): Update.

Re #164
9bbf9f9
@abo-abo abo-abo added a commit that referenced this issue Jun 30, 2015
@abo-abo ivy.el (ivy-switch-buffer): Add a multi-action interface
While in "C-o":

- Use "s" to make "C-m", "C-j", "C-M-n" and "C-M-p" kill
- Use "w" to switch back to normal.

Re #164
b465c09
@kaushalmodi
Contributor

Works great with ivy-switch-buffer. Would like to implement 'delete file' function in counsel-find-file in a similar fashion.

btw, why have the ivy- prefix for ivy-switch-buffer and counsel- for almost everything else?

@kaushalmodi
Contributor
    (defun counsel-find-file ()
      "Forward to `find-file'."
      (interactive)
      (ivy-read "Find file: " 'read-file-name-internal
                :matcher #'counsel--find-file-matcher
                :action (cons
                         1
                         `(("default"
                            (lambda (x)
                              (find-file
                               (expand-file-name x ivy--directory))))
                           (,(propertize "delete"
                                         'face 'font-lock-warning-face)
                            (lambda (x)
                              (delete-file
                               (expand-file-name x ivy--directory))))))
                :preselect (when counsel-find-file-at-point
                             (require 'ffap)
                             (ffap-guesser))
                :require-match 'confirm-after-completion
                :history 'file-name-history
                :keymap counsel-find-file-map))
@kaushalmodi kaushalmodi closed this Jul 2, 2015
@abo-abo
Owner
abo-abo commented Jul 3, 2015

btw, why have the ivy- prefix for ivy-switch-buffer and counsel- for almost everything else?

Because most of ivy-switch-buffer is in ivy.el, and it's part of ivy-mode via:

(define-key map [remap switch-to-buffer] 'ivy-switch-buffer)

I tried remapping counsel-find-file, it didn't work for some reason.

@abo-abo
Owner
abo-abo commented Jul 3, 2015

Would like to implement 'delete file' function in counsel-find-file in a similar fashion.

I'm afraid of deleting files by accident: by mashing sc you can delete a file by accident. But I don't want to make you redefine counsel-find-file if you choose to have that functionality. Maybe I could add ivy-extra-actions-alist similar to ivy-initial-inputs-alist. What do you think?

@kaushalmodi
Contributor

Maybe I could add ivy-extra-actions-alist similar to ivy-initial-inputs-alist.

I am fine redefining that function. But adding that variable would also mean easy customizability of any function using ivy; so go for it. Thanks! :)

I also feared of accidental deletion and so I gave font-lock-warning-face to delete option. I can even go further and wrap the delete-file in y-or-n-p.

@abo-abo abo-abo added a commit that referenced this issue Jul 6, 2015
@abo-abo Add a new interface to customize additional exit points
* ivy.el (ivy--actions-list): New defvar. Store the exit points per
  command.
(ivy-set-actions): New defun. Use this to set the extra exit points for
each command.
(ivy-read): Account for `ivy--actions-list'.
(ivy-switch-buffer): Set extra action to kill the buffer. Update the
call to `ivy-read'.

* counsel.el (counsel-locate): Use the single action in the function and
  customize the rest via `ivy-set-actions'.

Re #164
d289b78
@abo-abo
Owner
abo-abo commented Jul 6, 2015

Added the new interface. Give it a try for your file-deleting application.

@kaushalmodi
Contributor

Works great! Thank you.

(ivy-set-actions
 'counsel-find-file
 `((,(propertize "delete" 'face 'font-lock-warning-face)
    (lambda (x) (delete-file (expand-file-name x ivy--directory))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment