Skip to content

Commit

Permalink
Simplify use of paredit-space-for-delimiter-predicates
Browse files Browse the repository at this point in the history
Define a single predicate function, and, set it only buffer-local.

(Previously we set the hook globally, which in hindsight seems too
"heavy", even though the functions checked that they were running in a
Racket Mode buffer.)
  • Loading branch information
greghendershott committed Dec 17, 2024
1 parent dd40f62 commit 3a9675d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 62 deletions.
4 changes: 4 additions & 0 deletions racket-hash-lang.el
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ A discussion of the information provided by a Racket language:
(setq-local imenu-create-index-function nil)
(setq-local completion-at-point-functions nil) ;rely on racket-xp-mode
(setq racket-submodules-at-point-function nil) ;might change in on-new-lang
(when (boundp 'paredit-space-for-delimiter-predicates)
(add-hook 'paredit-space-for-delimiter-predicates
#'racket--paredit-space-for-delimiter-predicate
nil t))
;; Create back end hash-lang object.
;;
;; On the one hand, `racket--cmd/await' would be simpler to use
Expand Down
6 changes: 5 additions & 1 deletion racket-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@
(add-hook 'xref-backend-functions
#'racket-mode-xref-backend-function
nil t)
(setq racket-submodules-at-point-function #'racket-submodules-at-point-text-sexp))
(setq racket-submodules-at-point-function #'racket-submodules-at-point-text-sexp)
(when (boundp 'paredit-space-for-delimiter-predicates)
(add-hook 'paredit-space-for-delimiter-predicates
#'racket--paredit-space-for-delimiter-predicate
nil t)))

;;;###autoload
(progn
Expand Down
98 changes: 38 additions & 60 deletions racket-parens.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; racket-parens.el -*- lexical-binding: t; -*-

;; Copyright (c) 2013-2021 by Greg Hendershott.
;; Copyright (c) 2013-2024 by Greg Hendershott.
;; Portions Copyright (C) 1985-1986, 1999-2013 Free Software Foundation, Inc.

;; Author: Greg Hendershott
Expand Down Expand Up @@ -70,61 +70,49 @@ This is handy if you're not yet using something like
(put 'racket-insert-closing 'delete-selection
#'racket--electric-pair-mode-not-active)

;;; paredit and reader literals

(defun racket--reader-literal-paredit-space-for-delimiter-predicate (endp _delimiter)
"`paredit-mode' shouldn't insert space beteween # and open delimiters.
Examples: #() #2() #fl() #hasheq etc.
This function is a suitable element for the list variable
`paredit-space-for-delimiter-predicates'."
(if (and (racket--mode-edits-racket-p)
(not endp))
(not (looking-back (rx ?# (* (or (syntax word)
(syntax symbol)
(syntax punctuation))))
nil))
t))

(eval-after-load 'paredit
'(add-hook 'paredit-space-for-delimiter-predicates
#'racket--reader-literal-paredit-space-for-delimiter-predicate))

;;; paredit and at-expressions
(defun racket--open-paren (back-func)
"Use BACK-FUNC to find an opening ( [ or { if any.
BACK-FUNC should be something like #\\='backward-sexp or #\\='backward-up-list."
(save-excursion
(ignore-errors
(funcall back-func)
(let ((ch (char-after)))
(and (eq ?\( (char-syntax ch))
ch)))))

(defun racket--at-expression-paredit-space-for-delimiter-predicate (endp delimiter)
"`paredit-mode' shouldn't insert space before [ or { in Racket at-expressions.
;;; paredit spaces in reader literals and at-expressions

This function is a suitable element for the list variable
`paredit-space-for-delimiter-predicates'."
(defun racket--paredit-space-for-delimiter-predicate (endp delimiter)
"A value for hook `paredit-space-for-delimiter-predicates'."
(if (and (racket--mode-edits-racket-p)
(not endp))
(not (or
;; @foo[ @foo{
(and (memq delimiter '(?\[ ?\{))
(looking-back (rx ?@ (* (or (syntax word)
(syntax symbol)
(syntax punctuation))))
nil))
;; @foo[]{
(and (eq delimiter ?\{)
(looking-back (rx ?@ (* (or (syntax word)
(syntax symbol)
(syntax punctuation)))
?\[
(* (or (syntax word)
(syntax symbol)
(syntax punctuation)))
?\])
nil))))
(not
(or
;; reader literal: e.g. #(), #hasheq(), #"bstr", #px".*"
(looking-back (rx ?# (* (or (syntax word)
(syntax symbol)
(syntax punctuation))))
nil)
;; at-expression: @foo[ @foo{
(and (memq delimiter '(?\[ ?\{))
(looking-back (rx ?@ (* (or (syntax word)
(syntax symbol)
(syntax punctuation))))
nil))
;; at-expression: @foo[]{
(and (eq delimiter ?\{)
(looking-back (rx ?@ (* (or (syntax word)
(syntax symbol)
(syntax punctuation)))
?\[
(* (or (syntax word)
(syntax symbol)
(syntax punctuation)))
?\])
nil))
))
t))

(eval-after-load 'paredit
'(add-hook 'paredit-space-for-delimiter-predicates
#'racket--at-expression-paredit-space-for-delimiter-predicate))


;;; Cycle paren shapes

(defconst racket--paren-shapes
Expand Down Expand Up @@ -152,16 +140,6 @@ This function is a suitable element for the list variable
(_
(user-error "Don't know that paren shape")))))

(defun racket--open-paren (back-func)
"Use BACK-FUNC to find an opening ( [ or { if any.
BACK-FUNC should be something like #\\='backward-sexp or #\\='backward-up-list."
(save-excursion
(ignore-errors
(funcall back-func)
(let ((ch (char-after)))
(and (eq ?\( (char-syntax ch))
ch)))))

(provide 'racket-parens)

;; racket-parens.el ends here
6 changes: 5 additions & 1 deletion racket-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,11 @@ identifier bindings and modules from the REPL's namespace.
(add-hook 'kill-emacs-hook #'racket-repl-write-all-histories nil t)
(add-hook 'xref-backend-functions #'racket-repl-xref-backend-function nil t)
(add-to-list 'semantic-symref-filepattern-alist
'(racket-repl-mode "*.rkt" "*.rktd" "*.rktl")))
'(racket-repl-mode "*.rkt" "*.rktd" "*.rktl"))
(when (boundp 'paredit-space-for-delimiter-predicates)
(add-hook 'paredit-space-for-delimiter-predicates
#'racket--paredit-space-for-delimiter-predicate
nil t)))

(defun racket-repl-write-all-histories ()
"Call `racket-repl-write-history' for all `racket-repl-mode' buffers.
Expand Down

0 comments on commit 3a9675d

Please sign in to comment.