做了一个模仿Clion <backspace>行为的小程序

(defun clion-backspace (arg)
  (interactive "*P")
  (if (<= (current-column) (current-indentation))
      (if (= (current-indentation) (c-get-syntactic-indentation (c-guess-basic-syntax)))
	  (if (previous-line-blank-p) (moveline-up) (delete-indentation))
	(if (= (current-column) 0)
	    (if (previous-line-blank-p) (moveline-up) (delete-indentation)) (c-indent-line)))
    (backward-delete-char-untabify(prefix-numeric-value arg))))

(defun moveline-up ()
  (delete-region (line-beginning-position 0) (line-beginning-position))
  (c-indent-line))

(defun previous-line-blank-p ()
  (save-excursion
    (previous-line)
    (beginning-of-line)
    (looking-at "[[:space:]]*$")))

4赞

(c-get-syntactic-indentation (c-guess-basic-syntax)) 只适用于 c-mode 及其派生模式。

只删除上一行空行,再上一行空行不删除?

对的,最多只删除一行,只在c中适用,和Clion差不多,挺趁手的,刚学elisp一周,写这么个破玩意,以后再改

上次问缩进状态函数就是为了干这事

改成这样好用了

(defun clion-backspace (arg)
  "Simulate clion's backspace key behavior"
  (interactive "*P")
  (if (and (<= (current-column) (current-indentation)) (not (use-region-p)))
      (if (bolp)
	  (progn (c-indent-line) (plb))
	(when (= (c-indent-line) 0) (plb)))
    (c-electric-backspace arg)))

(defun plb ()
  (if (save-excursion
	(previous-line)
	(beginning-of-line)
	(looking-at "[[:space:]]*$"))
      (delete-region (line-beginning-position 0) (line-beginning-position))
    (delete-indentation)))

(define-key c-mode-map (kbd "DEL") 'clion-backspace)

(provide 'clion-backspace)