Here's an attempt at defining a custom kill-sentence-dwim
command that will either kill the entire sentence or kill up to the sentence-ending punctuation.
(defun my/forward-to-sentence-end ()
"Move point to just before the end of the current sentence."
(forward-sentence)
(backward-char)
(unless (looking-back "[[:alnum:]]")
(backward-char)))
(defun my/beginning-of-sentence-p ()
"Return t if point is at the beginning of a sentence."
(let ((start (point))
(beg (save-excursion (forward-sentence) (forward-sentence -1))))
(eq start beg)))
(defun my/kill-sentence-dwim ()
"Kill the current sentence up to and possibly including the punctuation.
When point is at the beginning of a sentence, kill the entire
sentence. Otherwise kill forward but preserve any punctuation at the sentence end."
(interactive)
(if (my/beginning-of-sentence-p)
(progn
(kill-sentence)
(just-one-space)
(when (looking-back "^[[:space:]]+") (delete-horizontal-space)))
(kill-region (point) (progn (my/forward-to-sentence-end) (point)))
(just-one-space 0)))
You can bind my/kill-sentence-dwim
to a key binding of your choice. If you want to replace the existing kill-sentence
binding you could use this:
(define-key (current-global-map) [remap kill-sentence] 'my/kill-sentence-dwim)
A couple notes:
Instead of just assuming a period, my/forward-to-sentence-end
moves to the end of the sentence and then backs up to just after the last alphanumeric character. That should preserve any closing punctuation including any quotes or parens.
I'm checking whether point is at the beginning of the sentence by calling forward-sentence
to jump to the end and then back to the beginning, then seeing if point changed. Not sure how reliable this will be but it seems correct in the simple cases I tried.
As requested in the comments, this kill command also attempts to fix up whitespace. When deleting a partial sentence it removes whitespace before the ending punctuation. When deleting an entire sentence it leaves one space between sentences or no spaces at the beginning of the line. There will likely still be edge cases...