Last active
December 12, 2015 04:38
-
-
Save Wilfred/4715345 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun dwim-at-point () | |
"If there's an active selection, return that. Otherwise, get | |
the symbol at point." | |
(if (use-region-p) | |
(buffer-substring-no-properties (region-beginning) (region-end)) | |
(if (symbol-at-point) | |
(symbol-name (symbol-at-point))))) | |
;; todo: investigate whether we're reinventing the wheel, since query-replace-history already exists | |
(defvar query-replace/history nil) | |
(defun query-replace-at-point (from-string to-string) | |
"Replace occurrences of FROM-STRING with TO-STRING, defaulting | |
to the symbol at point." | |
(interactive (list | |
(read-from-minibuffer "Replace what? " (dwim-at-point)) | |
(read-from-minibuffer "With what? " (dwim-at-point)))) | |
;; if we currently have point on a symbol we're replacing, go back | |
(let ((current-symbol (symbol-at-point))) | |
(if current-symbol | |
(let ((current-symbol-name (symbol-name current-symbol))) | |
(if (string-equal current-symbol-name from-string) | |
(forward-symbol -1))))) | |
(add-to-list 'query-replace/history | |
(list (format "%s -> %s" from-string to-string) | |
from-string to-string)) | |
(perform-replace from-string to-string t nil nil)) | |
(eval-when-compile (require 'cl)) ; first, second | |
(defun query-replace-repeat () | |
(interactive) | |
(unless query-replace/history | |
(error "You need to have done query-replace-at-point first")) | |
(let* ((choices (mapcar 'first query-replace/history)) | |
(choice (ido-completing-read "Previous replaces: " choices)) | |
(from-with-to (cdr (assoc choice query-replace/history))) | |
(from-string (first from-with-to)) | |
(to-string (second from-with-to))) | |
(perform-replace from-string to-string | |
t nil nil))) | |
(define-key global-map (kbd "M-%") 'query-replace-at-point) | |
(define-key global-map (kbd "C-c M-%") 'query-replace-repeat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment