From 0c844c3e9a8e72bd83e370c12eda5cb315ac5d41 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 19 Aug 2023 22:39:27 +1000 Subject: improve delete-pair related functions --- emacs/.emacs.d/init/ycp-editing.el | 5 ++- emacs/.emacs.d/lisp/my/my-editing.el | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/emacs/.emacs.d/init/ycp-editing.el b/emacs/.emacs.d/init/ycp-editing.el index c53cf74..5634f86 100644 --- a/emacs/.emacs.d/init/ycp-editing.el +++ b/emacs/.emacs.d/init/ycp-editing.el @@ -55,10 +55,13 @@ ;; call to `kill-visual-line' etc. when `visual-line-mode' is on. (advice-add 'kill-visual-line :override 'kill-line) (advice-add 'beginning-of-visual-line :override 'beginning-of-line) - (advice-add 'end-of-visual-line :override 'end-of-line)) + (advice-add 'end-of-visual-line :override 'end-of-line) + (setq delete-pair-blink-delay 0) + ) (my-package my-editing (:delay 5) + (my-override delete-pair) (my-keybind global-map "C-k" #'my-kill-line "M-k" #'my-kill-line-backward diff --git a/emacs/.emacs.d/lisp/my/my-editing.el b/emacs/.emacs.d/lisp/my/my-editing.el index 6bbde62..f8e405e 100644 --- a/emacs/.emacs.d/lisp/my/my-editing.el +++ b/emacs/.emacs.d/lisp/my/my-editing.el @@ -302,6 +302,71 @@ controlled by `delete-pair-blink-delay'." (delete-pair -1) (delete-pair 1))) +(defun my-delete-line-if-space-only () + "Delete the current line, if it has only space and nothing else." + (interactive) + (save-excursion + (beginning-of-line) + (when (looking-at-p "^\\ *$") + (delete-line)))) + +;;; To override delete-pair +(defun my-delete-pair (&optional arg) + "Delete a pair of characters enclosing ARG sexps that follow point. + +A negative ARG deletes a pair around the preceding ARG sexps +instead. The option `delete-pair-blink-delay' can disable +blinking. + +Delete space-only-lines caused by the deletion, as well as +re-indent the affected region." + (interactive "P") + (if arg + (setq arg (prefix-numeric-value arg)) + (setq arg 1)) + (let ((beg) (end)) + (if (< arg 0) + (save-excursion + (skip-chars-backward " \t") + (save-excursion + (let ((close-char (char-before))) + (forward-sexp arg) + (unless (member (list (char-after) close-char) + (mapcar (lambda (p) + (if (= (length p) 3) (cdr p) p)) + insert-pair-alist)) + (error "Not after matching pair")) + (when (and (numberp delete-pair-blink-delay) + (> delete-pair-blink-delay 0)) + (sit-for delete-pair-blink-delay)) + (delete-char 1) + (my-delete-line-if-space-only) + (setq beg (point)))) + (delete-char -1) + (my-delete-line-if-space-only) + (setq end (point))) + (save-excursion + (skip-chars-forward " \t") + (save-excursion + (let ((open-char (char-after))) + (forward-sexp arg) + (unless (member (list open-char (char-before)) + (mapcar (lambda (p) + (if (= (length p) 3) (cdr p) p)) + insert-pair-alist)) + (error "Not before matching pair")) + (when (and (numberp delete-pair-blink-delay) + (> delete-pair-blink-delay 0)) + (sit-for delete-pair-blink-delay)) + (delete-char -1) + (my-delete-line-if-space-only) + (setq end (point)))) + (delete-char 1) + (my-delete-line-if-space-only) + (setq beg (point)))) + (when (and beg end) + (indent-region beg end)))) + ;;;###autoload (defun my-zap-back-to-char (char &optional arg) "Backward `zap-to-char' for CHAR. -- cgit v1.2.3