From 27ae0a2ab8d9aca7757ea2deb2f4ec102c6168d0 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 29 Apr 2021 10:01:32 -0500 Subject: Replace anzu with karthink's re-builder glue --- init.el | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/init.el b/init.el index 4205222..fcbfd09 100644 --- a/init.el +++ b/init.el @@ -392,6 +392,60 @@ nil) t))) +(setup re-builder + ;; https://karthinks.com/software/bridging-islands-in-emacs-1/ + + (defvar acdw/re-builder-positions nil + "Store point and region bounds before calling re-builder") + + (advice-add 're-builder + :before + (defun acdw/re-builder-save-state (&rest _) + "Save into `acdw/re-builder-positions' the point and region +positions before calling `re-builder'." + (setq acdw/re-builder-positions + (cons (point) + (when (region-active-p) + (list (region-beginning) + (region-end))))))) + + (defun reb-replace-regexp (&optional delimited) + "Run `query-replace-regexp' with the contents of re-builder. With +non-nil optional argument DELIMITED, only replace matches +surrounded by word boundaries." + (interactive "P") + (reb-update-regexp) + (let* ((re (reb-target-binding reb-regexp)) + (replacement (query-replace-read-to + re + (concat "Query replace" + (if current-prefix-arg + (if (eq current-prefix-arg '-) " backward" " word") + "") + " regexp" + (if (with-selected-window reb-target-window + (region-active-p)) " in region" "")) + t)) + (pnt (car acdw/re-builder-positions)) + (beg (cadr acdw/re-builder-positions)) + (end (caddr acdw/re-builder-positions))) + (with-selected-window reb-target-window + (goto-char pnt) ; replace with (goto-char (match-beginning 0)) if you want + ; to control where in the buffer the replacement starts + ; with re-builder + (setq acdw/re-builder-positions nil) + (reb-quit) + (query-replace-regexp re replacement delimited beg end)))) + + (:global "C-M-%" re-builder) + + (dolist (map '(reb-mode-map reb-lisp-mode-map)) + (let ((setup-map map)) + (:bind "RET" reb-replace-regexp + "M-n" reb-next-match + "M-p" reb-prev-match + "M-q" reb-quit)))) + ;;; Applications @@ -621,20 +675,6 @@ call `zzz-to-char'." (:global "M-z" acdw/zzz-up-to-char)) -(setup (:straight anzu) - (:option anzu-replace-to-string-separator " → " - anzu-cons-mode-line-p nil) - (add-to-list 'mode-line-misc-info '(:eval (anzu--update-mode-line))) - - (:global [remap query-replace] anzu-query-replace - [remap query-replace-regexp] anzu-query-replace-regexp) - (:with-map isearch-mode-map - (:bind [remap isearch-query-replace] anzu-isearch-query-replace - [remap isearch-query-replace-regexp] - anzu-isearch-query-replace-regexp)) - - (global-anzu-mode +1)) - (setup (:straight async) (autoload 'dired-async-mode "dired-async.el" nil t) (dired-async-mode +1)) -- cgit 1.4.1-21-gabe81