Skip to content

Commit

Permalink
Refactor; use full check in racket-hash-lang-will-use-region
Browse files Browse the repository at this point in the history
  • Loading branch information
greghendershott committed Nov 30, 2023
1 parent db2bc19 commit 8de142d
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions racket-hash-lang.el
Original file line number Diff line number Diff line change
Expand Up @@ -610,49 +610,53 @@ You may customize this default initialization in
vs)))
(setq-local racket-hash-lang-pairs (reverse vs))))

(defun racket--hash-lang-lookup-pair ()
(pcase (assq last-command-event racket-hash-lang-pairs)
(`(,open ,close)
(cons open close))
(`(,open ,close . ,except-kinds)
(pcase-let ((`(,_beg ,_end (,kind . ,_))
(racket--cmd/await
nil
`(hash-lang
classify
,racket--hash-lang-id
,racket--hash-lang-generation
,(1- (point))))))
(unless (memq kind except-kinds)
(cons open close))))))

(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)))
(racket--hash-lang-lookup-pair)
t))

(defun racket--hash-lang-plain-self-insert (char)
"Use `self-insert-command' to insert CHAR without more pair checking."
(let ((racket-hash-lang-pairs nil) ;don't recur!
(blink-matching-paren nil)
(last-command-event char))
(self-insert-command 1)))

(defun racket-hash-lang-post-self-insert ()
"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)
(add-close open close))
(`(,open ,close . ,except-kinds)
(pcase-let ((`(,_beg ,_end (,kind . ,_))
(racket--cmd/await
nil
`(hash-lang
classify
,racket--hash-lang-id
,racket--hash-lang-generation
,(1- (point))))))
(unless (memq kind except-kinds)
(add-close open close)))))))
(pcase (racket--hash-lang-lookup-pair)
(`(,open-char . ,close-char)
(if (use-region-p)
(if (<= (point) (mark))
(save-excursion
(goto-char (mark))
(racket--hash-lang-plain-self-insert close-char))
(save-excursion
(let ((end (point)))
(delete-char -1) ;delete open-char at end
(goto-char (mark))
(racket--hash-lang-plain-self-insert open-char)
(goto-char end)
(racket--hash-lang-plain-self-insert close-char))))
(save-excursion
(racket--hash-lang-plain-self-insert close-char))))))

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

0 comments on commit 8de142d

Please sign in to comment.