Skip to content

Commit

Permalink
hash-lang: Add region behavior to automatic pairs
Browse files Browse the repository at this point in the history
Similar to electric-pairs-mode, when a region is active, wrap it with
the pair.

Although I don't want to go too far down the road on reinvented
wheels, this seems like a basic nice thing to add without too much
effort.
  • Loading branch information
greghendershott committed Nov 28, 2023
1 parent fc3f83c commit 58c12a9
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions racket-hash-lang.el
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ can contribute more colors; see the customization variable
(append (list (cons 'racket-token t))
text-property-default-nonsticky))
(add-hook 'post-self-insert-hook #'racket-hash-lang-post-self-insert nil t)
(add-hook 'self-insert-uses-region-functions #'racket-hash-lang-will-use-region nil t)
(electric-pair-mode -1)
(electric-indent-local-mode -1)
(setq-local electric-indent-inhibit t)
Expand Down Expand Up @@ -609,18 +610,39 @@ You may customize this default initialization in
vs)))
(setq-local racket-hash-lang-pairs (reverse vs))))

(defun racket-hash-lang-will-use-region ()
"A value for hook `self-insert-uses-region-functions'."
(and (use-region-p)
(assq last-command-event racket-hash-lang-pairs)))

(defun racket-hash-lang-post-self-insert ()
"A value for `post-self-insert-hook'."
(cl-flet ((self-insert (char)
(let ((racket-hash-lang-pairs nil) ;don't recur!
(last-command-event char)
(blink-matching-paren nil))
(self-insert-command 1)
(forward-char -1))))
"A value for hook `post-self-insert-hook'."
(cl-flet* ((self-insert
(char)
(let ((racket-hash-lang-pairs nil) ;don't recur!
(blink-matching-paren nil)
(last-command-event char))
(self-insert-command 1)))
(add-close
(open-char close-char)
(if (use-region-p)
(if (<= (point) (mark))
(save-excursion
(goto-char (mark))
(self-insert close-char))
(save-excursion
(let ((end (point)))
(delete-char -1) ;delete open at end
(goto-char (mark))
(self-insert open-char)
(goto-char end)
(self-insert close-char))))
(save-excursion
(self-insert close-char)))))
(pcase (assq last-command-event racket-hash-lang-pairs)
(`(,_open ,close)
(self-insert close))
(`(,_open ,close . ,except-kinds)
(`(,open ,close)
(add-close open close))
(`(,open ,close . ,except-kinds)
(pcase-let ((`(,_beg ,_end (,kind . ,_))
(racket--cmd/await
nil
Expand All @@ -630,7 +652,7 @@ You may customize this default initialization in
,racket--hash-lang-generation
,(1- (point))))))
(unless (memq kind except-kinds)
(self-insert close)))))))
(add-close open close)))))))

(defun racket-hash-lang-delete-backward-char ()
"Delete previous character, and when between a pair, following character."
Expand Down

0 comments on commit 58c12a9

Please sign in to comment.