--- /dev/null
+;;; refactor-elisp.el --- Refactoring backend for Emacs Lisp -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2024 Eshel Yaron
+
+;; Author: Eshel Yaron <me@eshelyaron.com>
+;; Keywords: tools
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;
+
+;;; Code:
+
+(require 'refactor)
+
+;;;###autoload
+(defun elisp-refactor-backend () '(elisp rename))
+
+(cl-defmethod refactor-backend-read-scoped-identifier ((_backend (eql elisp)))
+ (let ((all (scope (save-excursion
+ (beginning-of-defun)
+ (read-positioning-symbols (current-buffer))))))
+ (seq-some
+ (pcase-lambda (`(,beg ,len ,bin))
+ (and bin (<= beg (point) (+ beg len))
+ (list (buffer-substring-no-properties beg (+ beg len)))))
+ all)))
+
+(cl-defmethod refactor-backend-rename-edits ((_backend (eql elisp)) _old new (_scope (eql nil)))
+ (let* ((all (scope (save-excursion
+ (beginning-of-defun)
+ (read-positioning-symbols (current-buffer)))))
+ (dec (seq-some
+ (pcase-lambda (`(,beg ,len ,bin))
+ (when (<= beg (point) (+ beg len)) bin))
+ all)))
+ (list
+ (cons (current-buffer)
+ (seq-keep
+ (pcase-lambda (`(,beg ,len ,bin))
+ (when (equal bin dec)
+ (list beg (+ beg len) new)))
+ all)))))
+
+(provide 'refactor-elisp)
+;;; refactor-elisp.el ends here
"Special hook for choosing a refactor backend to use in the current context.
Each function on this hook is called in turn with no arguments, and
-should return either nil to mean that it is not applicable, or a cons
-cell (BACKEND . OPS) where BACKEND refactor backend, a value used for
-dispatching the generic functions, and OPS is a list of refactoring
+should return either nil to say that it is not applicable, or a cons
+cell (BACKEND . OPS) where BACKEND is a refactor backend, a value used
+for dispatching the generic functions, and OPS is a list of refactoring
operations that BACKEND supports.")
(defun refactor-backends ()
(dolist (buffer-reps edits)
(with-current-buffer (car buffer-reps)
(atomic-change-group
- (let* ((change-group (prepare-change-group)))
+ (let ((change-group (prepare-change-group)))
(refactor--apply-replacements (cdr buffer-reps))
(undo-amalgamate-change-group change-group))))))