From 905abd0b42b081762c2df14015b5be49b206f53c Mon Sep 17 00:00:00 2001 From: David Morgan Date: Wed, 24 Aug 2022 19:01:57 +0100 Subject: [PATCH] Set add-log-current-defun-function (#629) --- CHANGELOG.md | 4 ++++ clojure-mode.el | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac6e3d13..6057f9f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Changes + +* [#629](https://github.com/clojure-emacs/clojure-mode/pull/629): Set `add-log-current-defun-function` to new function `clojure-current-defun-name` (this is used by which-function-mode and easy-kill). + ### Bugs fixed * [#581](https://github.com/clojure-emacs/clojure-mode/issues/581): Fix font locking not working for keywords starting with a number diff --git a/clojure-mode.el b/clojure-mode.el index 33ebc6d0..414d1ed9 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -524,6 +524,37 @@ replacement for `cljr-expand-let`." #'clojure-space-for-delimiter-p) (advice-add 'paredit-convolute-sexp :after #'clojure--replace-let-bindings-and-indent))) +(defun clojure-current-defun-name () + "Return the name of the defun at point, or nil. + +`add-log-current-defun-function' is set to this, for use by `which-func'." + (save-excursion + (let ((location (point))) + ;; If we are now precisely at the beginning of a defun, make sure + ;; beginning-of-defun finds that one rather than the previous one. + (or (eobp) (forward-char 1)) + (beginning-of-defun) + ;; Make sure we are really inside the defun found, not after it. + (when (and (looking-at "\\s(") + (progn (end-of-defun) + (< location (point))) + (progn (forward-sexp -1) + (>= location (point)))) + (if (looking-at "\\s(") + (forward-char 1)) + ;; Skip the defining construct name, e.g. "defn" or "def". + (forward-sexp 1) + ;; The second element is usually a symbol being defined. If it + ;; is not, use the first symbol in it. + (skip-chars-forward " \t\n'(") + ;; Skip metadata + (while (looking-at "\\^") + (forward-sexp 1) + (skip-chars-forward " \t\n'(")) + (buffer-substring-no-properties (point) + (progn (forward-sexp 1) + (point))))))) + (defun clojure-mode-variables () "Set up initial buffer-local variables for Clojure mode." (add-to-list 'imenu-generic-expression '(nil clojure-match-next-def 0)) @@ -552,6 +583,7 @@ replacement for `cljr-expand-let`." (setq-local parse-sexp-ignore-comments t) (setq-local prettify-symbols-alist clojure--prettify-symbols-alist) (setq-local open-paren-in-column-0-is-defun-start nil) + (setq-local add-log-current-defun-function #'clojure-current-defun-name) (setq-local beginning-of-defun-function #'clojure-beginning-of-defun-function)) (defsubst clojure-in-docstring-p ()