diff --git a/racket-hash-lang.el b/racket-hash-lang.el index 620399cb..82e187ac 100644 --- a/racket-hash-lang.el +++ b/racket-hash-lang.el @@ -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-local-mode -1) (electric-indent-local-mode -1) (setq-local electric-indent-inhibit t) @@ -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 @@ -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."