[slime-devel] renaming symbols locally

I got fed up doing this manually, and wrote the following. I'm know there are pre-existing versions and this isn't probably the best one, but it has one feature I missed: it checks for conflict potential before starting the renaming. Something like this would be nice to have in Slime, I think. Is there a contrib that would be appropriate for this? Also, this is way too deep in elisp-land for me to feel quite confident that this is a sane way to do this. (defun ns:slime-rename-symbol-in-defun (old new) "Replace `old' with `new' in the current defun." (interactive "sSymbol to replace: \nsSymbol to replace with: ") (catch 'abort (let ((count 0)) (save-excursion (destructuring-bind (start end) (slime-region-for-defun-at-point) ;; First scan to see if we have conflicts... (let ((size (length new)) (p start)) (goto-char start) ;; KLUDGE: no forward-op for slime-symbols, but this gets most ;; things right when used with SLIME-SYMBOL-AT-POINT. (forward-thing 'symbol) (loop while (and (< (point) end) (> (point) p)) do (let ((symbol (slime-symbol-at-point))) (when (equalp symbol new) (slime-beginning-of-symbol) (when (ns:slime-highlight-y-or-n-p (format "Pre-existing %s, abort? " new) (point) (+ (point) size)) (throw 'abort nil)) (slime-end-of-symbol))) (setf p (point)) (forward-thing 'symbol))) ;; ...then scan again to replace. (let ((size (length old)) (p start)) (goto-char start) (forward-thing 'symbol) (loop while (and (< (point) end) (> (point) p)) do (let ((symbol (slime-symbol-at-point))) (when (equalp symbol old) (slime-beginning-of-symbol) (when (ns:slime-highlight-y-or-n-p "Rename? " (point) (+ (point) size)) (delete-char size) (insert-string new) (incf count)) (slime-end-of-symbol))) (setf p (point)) (forward-thing 'symbol)))) (message "%d occurrances of %s replaced with %s" count old new) t)))) (defun ns:slime-highlight-y-or-n-p (string start end) "Ask a y-or-n-p question using `string' with the region delimited by `start' and `end' highlighted." (let (hilite) (unwind-protect (progn (setf hilite (make-overlay start end)) (overlay-put hilite 'face 'slime-highlight-face) (y-or-n-p string)) (when hilite (delete-overlay hilite))))) Cheers, -- Nikodemus

Hi Nikodemus, I put together a hack a little while ago to do this in almost any kind of buffer. What's the advantage of using Slime-specific functions for this? You can see my hack here: http://brian.mastenbrook.net/static/files/misc/refactor.el . It's not a query-replace, but of course that's not terribly difficult to add. Brian On Jun 27, 2009, at 7:04 AM, Nikodemus Siivola wrote:
I got fed up doing this manually, and wrote the following. I'm know there are pre-existing versions and this isn't probably the best one, but it has one feature I missed: it checks for conflict potential before starting the renaming.
Something like this would be nice to have in Slime, I think. Is there a contrib that would be appropriate for this? Also, this is way too deep in elisp-land for me to feel quite confident that this is a sane way to do this.
(defun ns:slime-rename-symbol-in-defun (old new) "Replace `old' with `new' in the current defun." (interactive "sSymbol to replace: \nsSymbol to replace with: ") (catch 'abort (let ((count 0)) (save-excursion (destructuring-bind (start end) (slime-region-for-defun-at- point) ;; First scan to see if we have conflicts... (let ((size (length new)) (p start)) (goto-char start) ;; KLUDGE: no forward-op for slime-symbols, but this gets most ;; things right when used with SLIME-SYMBOL-AT-POINT. (forward-thing 'symbol) (loop while (and (< (point) end) (> (point) p)) do (let ((symbol (slime-symbol-at-point))) (when (equalp symbol new) (slime-beginning-of-symbol) (when (ns:slime-highlight-y-or-n-p (format "Pre-existing %s, abort? " new) (point) (+ (point) size)) (throw 'abort nil)) (slime-end-of-symbol))) (setf p (point)) (forward-thing 'symbol))) ;; ...then scan again to replace. (let ((size (length old)) (p start)) (goto-char start) (forward-thing 'symbol) (loop while (and (< (point) end) (> (point) p)) do (let ((symbol (slime-symbol-at-point))) (when (equalp symbol old) (slime-beginning-of-symbol) (when (ns:slime-highlight-y-or-n-p "Rename? " (point) (+ (point) size)) (delete-char size) (insert-string new) (incf count)) (slime-end-of-symbol))) (setf p (point)) (forward-thing 'symbol)))) (message "%d occurrances of %s replaced with %s" count old new) t))))
(defun ns:slime-highlight-y-or-n-p (string start end) "Ask a y-or-n-p question using `string' with the region delimited by `start' and `end' highlighted." (let (hilite) (unwind-protect (progn (setf hilite (make-overlay start end)) (overlay-put hilite 'face 'slime-highlight-face) (y-or-n-p string)) (when hilite (delete-overlay hilite)))))
Cheers,
-- Nikodemus
_______________________________________________ slime-devel site list slime-devel@common-lisp.net http://common-lisp.net/mailman/listinfo/slime-devel
-- Brian Mastenbrook brian@mastenbrook.net http://brian.mastenbrook.net/

2009/6/27 Brian Mastenbrook <brian@mastenbrook.net>:
I put together a hack a little while ago to do this in almost any kind of buffer. What's the advantage of using Slime-specific functions for this?
You can see my hack here: http://brian.mastenbrook.net/static/files/misc/refactor.el . It's not a query-replace, but of course that's not terribly difficult to add.
I looked at yours before writing that one -- aside from the query-style I wanted to add the pre-existing-new-name check, and using slime-symbol and symbol gets things like #.foo (foo is the symbol to rename) ,@foo (foo is the symbol to rename) @foo (@foo is the symbol to rename) at least mostly right. I'm not at all convinced that the way I did this is sensible, though. Cheers, -- Nikodemus
participants (2)
-
Brian Mastenbrook
-
Nikodemus Siivola