Skip to content

Commit

Permalink
Add customizable behavior allowing backward-delete of blank sexps
Browse files Browse the repository at this point in the history
This also changes the return value of `sp-point-in-blank-sexp` to
return the beginning and ending positions of the enclosing regexp.
  • Loading branch information
timor authored and Fuco1 committed Jun 29, 2024
1 parent 74d2b65 commit cf57048
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions smartparens.el
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,12 @@ kill \"subwords\" when `subword-mode' is active."
:type 'boolean
:group 'smartparens)

(defcustom sp-delete-blank-sexps nil
"If non-nil, allow deletion of enclosing pair when it only
contains whitespace."
:type 'boolean
:group 'smartparens)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Selection mode handling
Expand Down Expand Up @@ -1829,14 +1835,18 @@ If optional argument P is present test this instead of point."

If optional argument P is present test this instead of point.

Returns a cons containing the position of the beginning and end
delimiters.

Warning: it is only safe to call this when point is inside a
sexp, otherwise the call may be very slow."
(save-excursion
(when p (goto-char p))
(-when-let (enc (sp-get-enclosing-sexp))
(sp-get enc (string-match-p
"\\`[ \t\n]*\\'"
(buffer-substring-no-properties :beg-in :end-in))))))
(sp-get enc (when (string-match-p
"\\`[ \t\n]*\\'"
(buffer-substring-no-properties :beg-in :end-in))
(cons :beg :end))))))

(defun sp-char-is-escaped-p (&optional point)
"Test if the char at POINT is escaped or not.
Expand Down Expand Up @@ -9141,6 +9151,8 @@ If on a closing delimiter, move backward into balanced expression.

If on a opening delimiter, refuse to delete unless the balanced
expression is empty, in which case delete the entire expression.
If `sp-delete-blank-sexps' is non-nil, the expression is also
considered empty if it contains only white-space.

If the delimiter does not form a balanced expression, it will be
deleted normally.
Expand Down Expand Up @@ -9186,6 +9198,13 @@ Examples:
ok)
;; make this customizable
(setq n (1- n)))
((and sp-delete-blank-sexps
(sp--looking-back (sp--get-opening-regexp (sp--get-pair-list-context 'navigate)))
(-when-let ((beg . end) (sp-point-in-blank-sexp))
(goto-char beg)
(delete-char (- end beg))
t))
(setq n (1- n)))
((and (sp-point-in-string)
(save-excursion (backward-char) (not (sp-point-in-string))))
(setq n 0))
Expand Down

0 comments on commit cf57048

Please sign in to comment.