Skip to content

Add option to choose whether the deleted region should be added to kill ring #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ It's in [[https://melpa.org][MELPA]], so if you have that in your package lists,
#+begin_src elisp
(require 'smart-hungry-delete)
(smart-hungry-delete-add-default-hooks)
(smart-hungry-delete-should-add-kill-ring t)
(global-set-key (kbd "<backspace>") 'smart-hungry-delete-backward-char)
(global-set-key (kbd "<delete>") 'smart-hungry-delete-backward-char)
(global-set-key (kbd "C-d") 'smart-hungry-delete-forward-char)
Expand All @@ -93,9 +94,11 @@ If you use =use-package=:
(use-package smart-hungry-delete
:ensure t
:bind (([remap backward-delete-char-untabify] . smart-hungry-delete-backward-char)
([remap delete-backward-char] . smart-hungry-delete-backward-char)
([remap delete-char] . smart-hungry-delete-forward-char))
:init (smart-hungry-delete-add-default-hooks))
([remap delete-backward-char] . smart-hungry-delete-backward-char)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide these in a separate PR?

([remap delete-char] . smart-hungry-delete-forward-char))
:init
(smart-hungry-delete-add-default-hooks)
(smart-hungry-delete-should-add-kill-ring t))
#+end_src

Only for some modes:
Expand All @@ -104,9 +107,11 @@ Only for some modes:
:ensure t
:bind (:map python-mode-map
([remap backward-delete-char-untabify] . smart-hungry-delete-backward-char)
([remap delete-backward-char] . smart-hungry-delete-backward-char)
([remap delete-char] . smart-hungry-delete-forward-char))
:init (smart-hungry-delete-add-default-hooks))
([remap delete-backward-char] . smart-hungry-delete-backward-char)
([remap delete-char] . smart-hungry-delete-forward-char))
:init
(smart-hungry-delete-add-default-hooks)
(smart-hungry-delete-should-add-kill-ring t))
#+end_src


Expand Down Expand Up @@ -160,4 +165,6 @@ You can configure these on a per buffer basis:

=smart-hungry-delete-add-default-hooks= will add some good defaults for (some) programming modes. Check out the =smart-hungry-delete-default-*-hook= functions.

=smart-hungry-delete-should-add-kill-ring= will change whether the deleted regions is added to `kill ring` or not. Default `t`.

If you have good suggestions for more defaults, make sure to [[https://github.com/hrehfeld/emacs-smart-hungry-delete/issues][suggest the recipes!]]
20 changes: 15 additions & 5 deletions smart-hungry-delete.el
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
:safe t
)

(defcustom smart-hungry-delete-add-kill-ring t
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't want to be annoying, but can we rename this to smart-hungry-delete-kill-ring-save just like kill-ring-save? Or were you inspired by some other existing variable/function name? There's also avy-kill-ring-save-region and org-export-copy-to-kill-ring.

"If t, deleted region will be added to `kill ring`."
:type '(boolean)
:safe t
)

(defun smart-hungry-delete-looking-back-limit ()
"LIMIT for `looking-back`."
(max 0 (- (point) smart-hungry-delete-max-lookback)))
Expand Down Expand Up @@ -124,6 +130,14 @@ completely deleted."
(add-hook 'nxml-mode-hook 'smart-hungry-delete-default-sgml-mode-common-hook)
(add-hook 'text-mode-hook 'smart-hungry-delete-default-text-mode-hook))

;;;###autoload
(defun smart-hungry-delete-should-add-kill-ring (should-kill)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common elisp convention is to postfix predicate functions with -p: e.g. -any-p, ring-p, org-url-p.

How about smart-hungry-delete-kill-ring-save-p?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, this isn't a predicate. Hmm.

"Choose deleted region should be added to kill ring or not by `SHOULD-KILL`."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Should the deleted region be added to the KILL-RING?"

(interactive)
(setq smart-hungry-delete-add-kill-ring should-kill)
(put #'smart-hungry-delete-backward-char 'delete-selection (if smart-hungry-delete-add-kill-ring 'kill nil))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you putting stuff into the fsymbol here? It's never used, is it?

(put #'smart-hungry-delete-forward-char 'delete-selection (if smart-hungry-delete-add-kill-ring 'kill nil)))

;;;###autoload
(defun smart-hungry-delete-backward-char (arg)
"If there is more than one char of whitespace between previous word and point,
Expand All @@ -136,8 +150,6 @@ With prefix argument ARG, just delete a single char."
(prefix-command-preserve-state)
(smart-hungry-delete-char arg t))

(put #'smart-hungry-delete-backward-char 'delete-selection 'kill)

;;;###autoload
(defun smart-hungry-delete-forward-char (arg)
"If there is more than one char of whitespace between point and next word,
Expand All @@ -150,8 +162,6 @@ With prefix argument ARG, just delete a single char."
(prefix-command-preserve-state)
(smart-hungry-delete-char arg))

(put #'smart-hungry-delete-forward-char 'delete-selection 'kill)

(defun smart-hungry-delete-char-trigger (to from)
"Return t if the region (TO FROM) should be killed completely."
(save-excursion
Expand Down Expand Up @@ -204,7 +214,7 @@ With PREFIX just delete one char."
fallback 'delete-char)
)
(if (use-region-p)
(kill-region nil nil t)
(if smart-hungry-delete-add-kill-ring (kill-region nil nil t) (delete-region (region-beginning) (region-end)))
(let ((kill-hungrily (funcall check smart-hungry-delete-char-kill-regexp)))
(cond ((functionp kill-hungrily)
(funcall-interactively kill-hungrily))
Expand Down