Git Product home page Git Product logo

emacs-smart-hungry-delete's Introduction

Smart-hungry-delete

Delete whitespace between words, parenthesis and other delimiters in a smart (dumb) way.

Suppose you have this text in the buffer:

./doc/demo-00-01.png

With smart-hungry-delete/backward-char, it will delete all but one (white-)space between word and paren:

./doc/demo-00-02.png

If you delete here:

./doc/demo-00-03.png

it deletes all whitespace, because it’s on the inside of a paren:

./doc/demo-00-04.png

Inside of right paren:

./doc/demo-00-05.png

./doc/demo-00-06.png

This is configured for C-like languages (e.g. Python), where : usually has no space to the left:

./doc/demo-00-07.png

./doc/demo-00-08.png

It has space to the right though:

./doc/demo-00-09.png

./doc/demo-00-10.png

This also works for (nested) parentheses:

./doc/demo-00-11.png

./doc/demo-00-12.png

./doc/demo-00-13.png

Between words it will leave a space:

./doc/demo-00-14.png

./doc/demo-00-15.png

./doc/demo-00-16.png

Here’s a small demonstration video:

Demonstration Video

Installing

MELPA

It’s in MELPA, so if you have that in your package lists, just M-x package-install smart-hungry-delete, and bind smart-hungry-delete-backward-char and smart-hungry-delete-forward-char:
(require 'smart-hungry-delete)
(smart-hungry-delete-add-default-hooks)
(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)

use-package

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

Only for some modes:

(use-package smart-hungry-delete
  :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))

el-get

Here’s the basic el-get recipe:
(:name smart-hungry-delete
 :type github
 :pkgname "hrehfeld/emacs-smart-hungry-delete")

Configuration

smart-hungry-delete deletes hungrily if three regexps match:

<left><kill>(point)<right>

or with smart-hungry-delete-forward-char:

<left>(point)<kill><right>

You can configure these on a per buffer basis:

smart-hungry-delete-char-kill-regexp is a buffer-local variable holding a regex that defines what will be hungrily deleted (<kill>).

smart-hungry-delete-char-trigger-killall-regexps is a list of pairs:

'(("\\[" . ".") ;; delete when <left> is "[" and <right> is any of "."
  ("." . "\\]") ;; delete when <left> is any of "." and <right> is "]"
  ...)

smart-hungry-delete-add-regexps-left-right makes it easy to add left-right combinations of chars like parentheses:

(smart-hungry-delete-add-regexps-left-right "\\{" "\\}") ;;as above, but with "{" and "}"

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

If you have good suggestions for more defaults, make sure to suggest the recipes!

emacs-smart-hungry-delete's People

Contributors

black7375 avatar hrehfeld avatar jotare avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

emacs-smart-hungry-delete's Issues

Smart backspace for Python

Not sure if there is a better/suggested way of doing this, but I wrote a simple function for backspacing in Python. Normally smart-hungry-delete-backward-char will delete all indentation on a line if called with the point directly after the indentation, but I usually just want it to decrease the indentation by one level. This function does that:

(defun python-smart-backspace (&optional arg)
  "Call `smart-hungry-delete-backward-char' unless point is directly after the indentation, in which case call `python-indent-dedent-line-backspace'."
  (interactive)
  (if (looking-back "^[\t ]+")
      (python-indent-dedent-line-backspace arg)
    (smart-hungry-delete-backward-char arg)))

I just bind it to <backspace> in python-mode-map.

Deleted region is added to kill ring

Question

When I select region and run smart-hungry-delete-backward-char, the region is added to my kill ring.
Is this expected behavior?

Suggestion

I suggest: we can choose whether the region should be added to kill ring or not as follows.

;; adding to kill ring
(setq smart-hungry-delete-add-kill-ring t)
;; not adding to kill ring
(setq smart-hungry-delete-add-kill-ring nil)

I guess these two commits occur this behavior.

Symbol void: prefix-command-preserve-state

after installing from melpa and putting following lines in my init.el:

(require 'smart-hungry-delete)
(smart-hungry-delete-add-default-hooks)
(global-set-key (kbd "<backspace>") 'smart-hungry-delete-backward-char)
(global-set-key (kbd "C-d") 'smart-hungry-delete-forward-char)

I get just the following error messages at pressing backspace or C-d:

smart-hungry-delete-backward-char: Symbol's function definition is void: prefix-command-preserve-state

I'm using: GNU Emacs 24.5.1 on Linux

delete-selection-mode compatiblity

tried binding to smart-hungry-delete-forward-char as

(global-set-key (kbd "<delete>") 'smart-hungry-delete-forward-char)

it works but kinda makes (delete-selection-mode 1) useless, one that replace the text in keystroke,
any suggestion how bind it to <delete>

bug: error when invoke delete when cursor is before `end-of-indentation` in python buffer

im using emacs 29: GNU Emacs 29.2 (build 2, x86_64-w64-mingw32) of 2024-02-02

  • error image:

image

  • cause error when cursor is before end-of-indentation, including cursor on the beginning-of-line.

for example, open a new python buffer, enter some new lines, and invoke delete key.

i think its because the code below:

(defcustom smart-hungry-delete-major-mode-dedent-function-alist '((python-mode . (lambda () (interactive) (python-indent-dedent-line-backspace nil))))
  "Number of characters to search back in the most."
  :safe t)

we shouldnt use nil as parameter for function python-indent-dedent-line-backspace, as it wants a numeric value.

  • a quickfix could be this:
(setq smart-hungry-delete-major-mode-dedent-function-alist
      '((python-mode . (lambda ()
                         (interactive)
                         (if (bolp)
                             (python-indent-dedent-line-backspace 1)
                           (python-indent-dedent-line-backspace (current-column)))))))

Extra characters deleted with a region

Hi,

When calling the smart commands from keybindings with a region selected, smart-hungry-delete-forward-char will also delete the char after point, and smart-hungry-delete-backward-char also deletes the char before point. If the char is whitespace, it will hungrily delete it.

For example, with ([] being point and | mark):

ab[]cde|fg

pressing backspace results in

a[]fg

instead of

ab[]fg

The weird thing is that it only happens if the functions are called from a bound key. Using M-x smart-hungry-delete-XXX-char works fine in both cases, deleting only the region.

It also appears to be two actions for undo: one undo will undo the character deletion, the second will undo the region deletion.

I don't know if it is related to #3 (regarding delete-selection-mode).

Cheers!

Is there any reason to use killing?

I love the smart-hungry-delete's action.
It is not excessive and is natural.

However, one problem occurred after use.
The deleted spaces appear when pasting.

I looked into the code for a while and was able to find the cause.

(kill-region (min kill-start (point)) (max kill-start (point))))

If I change it to delete-region, the above does not happen.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.