;;; acdw-re.el -*- lexical-binding: t; coding: utf-8-unix -*- ;; Author: Case Duckworth ;; Created: 2021-04-29 ;; Keywords: configuration ;; URL: https://tildegit.org/acdw/emacs ;; This file is NOT part of GNU Emacs. ;;; License: ;; Everyone is permitted to do whatever with this software, without ;; limitation. This software comes without any warranty whatsoever, ;; but with two pieces of advice: ;; - Don't hurt yourself. ;; - Make good choices. ;;; Commentary: ;; Pulled mostly from karthinks: ;; https://karthinks.com/software/bridging-islands-in-emacs-1/ ;;; Code: (defvar acdw/re-builder-positions nil "Store point and region bounds before calling re-builder") (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 ;; replace with (goto-char (match-beginning 0)) if you want to control ;; where in the buffer the replacement starts with re-builder (goto-char pnt) (setq acdw/re-builder-positions nil) (reb-quit) (query-replace-regexp re replacement delimited beg end)))) (provide 'acdw-re) ;;; acdw-re.el ends here