Skip to content

Commit

Permalink
Respect end user function key bindings; fixes #714
Browse files Browse the repository at this point in the history
  • Loading branch information
greghendershott committed Aug 25, 2024
1 parent 50249f1 commit edc3d4a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
7 changes: 3 additions & 4 deletions racket-hash-lang.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
`((("C-c C-c"
"C-c C-k") ,#'racket-run-module-at-point)
("C-c C-z" ,#'racket-edit-switch-to-repl)
("<f5>" ,#'racket-run-and-switch-to-repl)
("M-C-<f5>" ,#'racket-racket)
("C-<f5>" ,#'racket-test)
("C-c C-t" ,#'racket-test)
("C-c C-l" ,#'racket-logger)
("C-c C-o" ,#'racket-profile)
Expand All @@ -45,7 +42,8 @@
("C-M-f" ,#'racket-hash-lang-forward)
("C-M-u" ,#'racket-hash-lang-up)
("C-M-d" ,#'racket-hash-lang-down)
("C-M-q" ,#'racket-hash-lang-C-M-q-dwim))))
("C-M-q" ,#'racket-hash-lang-C-M-q-dwim)
,@racket--f5-bindings)))

(easy-menu-define racket-hash-lang-mode-menu racket-hash-lang-mode-map
"Menu for `racket-hash-lang-mode'."
Expand Down Expand Up @@ -222,6 +220,7 @@ A discussion of the information provided by a Racket language:
\\{racket-hash-lang-mode-map}
"
(racket--polite-user-f-keys racket-hash-lang-mode-map racket--f5-bindings)
(racket-call-racket-repl-buffer-name-function)
(add-hook 'kill-buffer-hook
#'racket-mode-maybe-offer-to-kill-repl-buffer
Expand Down
25 changes: 12 additions & 13 deletions racket-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,31 @@

(defvar racket-mode-map
(racket--easy-keymap-define
'((("C-c C-c"
`((("C-c C-c"
"C-c C-k") racket-run-module-at-point)
("C-c C-z" racket-edit-switch-to-repl)
("<f5>" racket-run-and-switch-to-repl)
("M-C-<f5>" racket-racket)
("C-<f5>" racket-test)
("C-c C-t" racket-test)
("C-c C-l" racket-logger)
("C-c C-o" racket-profile)
("C-c C-z" ,#'racket-edit-switch-to-repl)
("C-c C-t" ,#'racket-test)
("C-c C-l" ,#'racket-logger)
("C-c C-o" ,#'racket-profile)
("M-C-x" racket-send-definition)
("C-x C-e" racket-send-last-sexp)
("C-c C-r" racket-send-region)
("C-c C-e f" racket-expand-file)
("C-c C-e x" racket-expand-definition)
("C-c C-e x" ,#'racket-expand-definition)
("C-c C-e e" racket-expand-last-sexp)
("C-c C-e r" racket-expand-region)
("C-c C-x C-f" racket-open-require-path)
("TAB" indent-for-tab-command)
("C-c C-x C-f" ,#'racket-open-require-path)
("TAB" ,#'indent-for-tab-command)
("M-C-u" racket-backward-up-list)
("C-c C-p" racket-cycle-paren-shapes)
("C-c C-p" ,#'racket-cycle-paren-shapes)
("M-C-y" racket-insert-lambda)
("C-c C-d" racket-documentation-search)
(("C-c C-s"
"C-c C-.") racket-describe-search)
("C-c C-f" racket-fold-all-tests)
("C-c C-u" racket-unfold-all-tests)
((")" "]" "}") racket-insert-closing)))
((")" "]" "}") racket-insert-closing)
,@racket--f5-bindings))
"Keymap for Racket mode.")

(easy-menu-define racket-mode-menu racket-mode-map
Expand Down Expand Up @@ -135,6 +133,7 @@
"Major mode for editing Racket source files.
\\{racket-mode-map}"
(racket--polite-user-f-keys racket-mode-map racket--f5-bindings)
;;; Syntax
(set-syntax-table racket-mode-syntax-table)
(setq-local multibyte-syntax-as-symbol t)
Expand Down
33 changes: 33 additions & 0 deletions racket-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,39 @@ as if the user had C-g to quit."
s))
sap)))

(defconst racket--f5-bindings
'(("<f5>" racket-run-and-switch-to-repl)
("M-C-<f5>" racket-racket)
("C-<f5>" racket-test))
"On the one hand, we want to allow `racket-mode-map' and
`racket-hash-lang-mode-map' to bind <f5> as a convenience for
users coming from DrRacket.
On the other hand, Emacs convention reserves <f5> for user
bindings. See issue #714.
On the third hand, we want to initialize the major mode's keymaps
with these, for use by doc/generate.el, to document the default
bindings.
Solution: Append these in the keymap initialization, and also
call `racket--polite-user-f-keys' in the major mode
initialization function. That adds/remove the binding based on
whether it would shadow an end user binding in the global map.")

(defun racket--polite-user-f-keys (major-mode-keymap keys+cmds)
"Politely bind/unbind KEYS+CMDS in MAJOR-MODE-KEYMAP."
(dolist (k+c keys+cmds)
(let ((key (kbd (car k+c)))
(cmd (cadr k+c)))
;; Avoid shadowing a binding user has made in the global map.
(if (lookup-key (current-global-map) key)
(define-key major-mode-keymap key nil)
;; Unless user has modified binding in major-mode-keymap,
;; restore our binding there.
(unless (lookup-key major-mode-keymap key)
(define-key major-mode-keymap key cmd))))))

(provide 'racket-util)

;; racket-util.el ends here

0 comments on commit edc3d4a

Please sign in to comment.