commit 4dde3f27d103f662703732f477bcec7c9ec3f8f1
parent 93eb1631b6b590538eda3529ae347d161753e34f
Author: andsy10 <andsy1016@gmail.com>
Date: Tue, 20 Oct 2026 10:46:37 +0800
Update keybindings
Diffstat:
2 files changed, 49 insertions(+), 9 deletions(-)
diff --git a/lisp/init-kbd-func.el b/lisp/init-kbd-func.el
@@ -15,6 +15,39 @@
(or (find-file custom-file)
(user-error "No custom file")))
+(defun my-empty-line-p (&optional really-empty)
+ (save-excursion
+ (beginning-of-line)
+ (looking-at-p (if really-empty
+ "$"
+ "[[:blank:]]*$"))))
+
+(defun my-electric-open-line-and-indent (n)
+ "Insert N newlines and leave point before them.
+If there is a fill prefix and/or a `left-margin', insert them on the new
+line if the line would have been blank. Reindent the line following the
+inserted newlines, if that line is blank, remove its indentation."
+ (interactive "*p")
+ (let* ((do-fill-prefix (and fill-prefix (bolp)))
+ (do-left-margin (and (bolp) (> (current-left-margin) 0)))
+ (loc (point-marker))
+ (abbrev-mode nil))
+ (newline n)
+ (when (and (not do-left-margin)
+ (not do-fill-prefix))
+ (indent-according-to-mode)
+ (when (my-empty-line-p)
+ (delete-horizontal-space t)))
+ (goto-char loc)
+ (while (> n 0)
+ (cond ((bolp)
+ (if do-left-margin (indent-to (current-left-margin)))
+ (if do-fill-prefix (insert-and-inherit fill-prefix))))
+ (forward-line 1)
+ (setq n (1- n)))
+ (goto-char loc)
+ (end-of-line)))
+
(defun my-copy-function-name ()
"Copy the function name return by `which-function'."
(interactive)
@@ -68,15 +101,21 @@ With prefix ARG, report for the entire buffer rather than the region."
symbol
sexp)))
-(defun my-mark-sexp-hack (&optional arg allow-extend)
- (unless (and allow-extend
- (or (and (eq last-command this-command) (mark t))
- (and transient-mark-mode mark-active)))
- (when-let* ((bounds (or (my-bounds-of-symbol-at-point-or-next-sexp)
- (bounds-of-thing-at-point 'sexp)))
- (pos (car bounds)))
- (goto-char pos))))
-(advice-add #'mark-sexp :before #'my-mark-sexp-hack)
+(defun my-mark-sexp-hack (fun &optional arg allow-extend)
+ (if (and allow-extend
+ (or (and (eq last-command this-command) (mark t))
+ (and transient-mark-mode mark-active)))
+ (funcall fun arg allow-extend)
+ (let* ((bounds (or (my-bounds-of-symbol-at-point-or-next-sexp)
+ (bounds-of-thing-at-point 'sexp)))
+ (pos (point))
+ (beg (car bounds))
+ (end (cdr bounds))
+ (endp (equal (point) end)))
+ (goto-char beg)
+ (funcall fun arg allow-extend)
+ (if endp (exchange-point-and-mark)))))
+(advice-add #'mark-sexp :around #'my-mark-sexp-hack)
(defun my-mark-word-hack (&optional arg allow-extend)
(unless (and allow-extend
diff --git a/lisp/init-kbd.el b/lisp/init-kbd.el
@@ -25,6 +25,7 @@
"C-x C-0" #'delete-window
;; edit
+ [remap open-line] #'my-electric-open-line-and-indent
"M-j" #'join-line
"M-c" #'capitalize-dwim
"M-u" #'upcase-dwim