Move RET & backspace fixes out of +smartparens feature

Neither of these are tied to smartparens, and should be available to
folks that have disabled +smartparens.
This commit is contained in:
Henrik Lissner 2020-05-12 19:38:56 -04:00
parent bc5bbb1770
commit 82ddc86335
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 54 additions and 52 deletions

View file

@ -50,8 +50,8 @@ If `buffer-file-name' isn't set, uses `default-directory'."
(abbreviate-file-name path) (abbreviate-file-name path)
(file-name-nondirectory path))))) (file-name-nondirectory path)))))
;;;###autoload
(defun doom--backward-delete-whitespace-to-column () (defun doom/backward-delete-whitespace-to-column ()
"Delete back to the previous column of whitespace, or as much whitespace as "Delete back to the previous column of whitespace, or as much whitespace as
possible, or just one char if that's not possible." possible, or just one char if that's not possible."
(interactive) (interactive)
@ -74,7 +74,7 @@ possible, or just one char if that's not possible."
;; point and bol. ;; point and bol.
((and (not indent-tabs-mode) ((and (not indent-tabs-mode)
(not (bolp)) (not (bolp))
(not (sp-point-in-string)) (not (doom-point-in-string-p))
(save-excursion (>= (- (skip-chars-backward " \t")) tab-width))) (save-excursion (>= (- (skip-chars-backward " \t")) tab-width)))
(let ((movement (% (current-column) tab-width))) (let ((movement (% (current-column) tab-width)))
(when (= movement 0) (when (= movement 0)
@ -97,7 +97,7 @@ possible, or just one char if that's not possible."
{ {
| |
} => {|} } => {|}
+ Otherwise, resort to `doom--backward-delete-whitespace-to-column'. + Otherwise, resort to `doom/backward-delete-whitespace-to-column'.
+ Resorts to `delete-char' if n > 1" + Resorts to `delete-char' if n > 1"
(interactive "p\nP") (interactive "p\nP")
(or (integerp n) (or (integerp n)
@ -120,12 +120,14 @@ possible, or just one char if that's not possible."
(save-excursion (save-excursion
(insert-char ?\s (- ocol (current-column)) nil)))) (insert-char ?\s (- ocol (current-column)) nil))))
;; ;;
((and (= n 1) (bound-and-true-p smartparens-mode)) ((= n 1)
(cond ((and (memq (char-before) (list ?\ ?\t)) (cond ((or (not (featurep! +smartparens))
(not (bound-and-true-p smartparens-mode))
(and (memq (char-before) (list ?\ ?\t))
(save-excursion (save-excursion
(and (/= (skip-chars-backward " \t" (line-beginning-position)) 0) (and (/= (skip-chars-backward " \t" (line-beginning-position)) 0)
(bolp)))) (bolp)))))
(doom--backward-delete-whitespace-to-column)) (doom/backward-delete-whitespace-to-column))
((let* ((pair (ignore-errors (sp-get-thing))) ((let* ((pair (ignore-errors (sp-get-thing)))
(op (plist-get pair :op)) (op (plist-get pair :op))
(cl (plist-get pair :cl)) (cl (plist-get pair :cl))
@ -144,6 +146,6 @@ possible, or just one char if that's not possible."
(sp-insert-pair op) (sp-insert-pair op)
t) t)
((run-hook-with-args-until-success 'doom-delete-backward-functions)) ((run-hook-with-args-until-success 'doom-delete-backward-functions))
((doom--backward-delete-whitespace-to-column))))))) ((doom/backward-delete-whitespace-to-column)))))))
;; Otherwise, do simple deletion. ;; Otherwise, do simple deletion.
((delete-char (- n) killflag)))) ((delete-char (- n) killflag))))

View file

@ -204,35 +204,39 @@
;; This keybind allows * to skip over **. ;; This keybind allows * to skip over **.
(map! :map markdown-mode-map (map! :map markdown-mode-map
:ig "*" (λ! (if (looking-at-p "\\*\\* *$") :ig "*" (general-predicate-dispatch nil
(forward-char 2) (looking-at-p "\\*\\* *")
(call-interactively 'self-insert-command))))) (λ! (forward-char 2)))))))
;; Highjacks backspace to:
;;
;;; Keybinding fixes
;; Highjacks backspace to delete up to nearest column multiple of `tab-width' at
;; a time. If you have smartparens enabled, it will also:
;; a) balance spaces inside brackets/parentheses ( | ) -> (|) ;; a) balance spaces inside brackets/parentheses ( | ) -> (|)
;; b) delete up to nearest column multiple of `tab-width' at a time ;; b) close empty multiline brace blocks in one step:
;; c) close empty multiline brace blocks in one step:
;; { ;; {
;; | ;; |
;; } ;; }
;; becomes {|} ;; becomes {|}
;; d) refresh smartparens' :post-handlers, so SPC and RET expansions work ;; c) refresh smartparens' :post-handlers, so SPC and RET expansions work even
;; even after a backspace. ;; after a backspace.
;; e) properly delete smartparen pairs when they are encountered, without ;; d) properly delete smartparen pairs when they are encountered, without the
;; the need for strict mode. ;; need for strict mode.
;; f) do none of this when inside a string ;; e) do none of this when inside a string
(advice-add #'delete-backward-char :override #'+default--delete-backward-char-a)) (advice-add #'delete-backward-char :override #'doom/backward-delete-whitespace-to-column)
;; HACK Makes `newline-and-indent' continue comments (and more reliably). ;; HACK Makes `newline-and-indent' continue comments (and more reliably).
;; Consults `doom-point-in-comment-functions' to detect a commented ;; Consults `doom-point-in-comment-functions' to detect a commented region
;; region and uses that mode's `comment-line-break-function' to continue ;; and uses that mode's `comment-line-break-function' to continue comments.
;; comments. If neither exists, it will fall back to the normal behavior ;; If neither exists, it will fall back to the normal behavior of
;; of `newline-and-indent'. ;; `newline-and-indent'.
;; ;;
;; We use an advice here instead of a remapping because many modes define ;; We use an advice here instead of a remapping because many modes define
;; and remap to their own newline-and-indent commands, and tackling all ;; and remap to their own newline-and-indent commands, and tackling all
;; those cases was judged to be more work than dealing with the edge ;; those cases was judged to be more work than dealing with the edge cases
;; cases on a case by case basis. ;; on a case by case basis.
(defadvice! +default--newline-indent-and-continue-comments-a (&rest _) (defadvice! +default--newline-indent-and-continue-comments-a (&rest _)
"A replacement for `newline-and-indent'. "A replacement for `newline-and-indent'.
@ -244,11 +248,7 @@ Continues comments if executed from a commented line. Consults
(doom-point-in-comment-p) (doom-point-in-comment-p)
(fboundp comment-line-break-function)) (fboundp comment-line-break-function))
(funcall comment-line-break-function nil) (funcall comment-line-break-function nil)
t))) t))
;;
;;; Keybinding fixes
;; This section is dedicated to "fixing" certain keys so that they behave ;; This section is dedicated to "fixing" certain keys so that they behave
;; sensibly (and consistently with similar contexts). ;; sensibly (and consistently with similar contexts).