diff options
Diffstat (limited to 'lisp/acdw-re.el')
-rw-r--r-- | lisp/acdw-re.el | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lisp/acdw-re.el b/lisp/acdw-re.el new file mode 100644 index 0000000..ea1c42a --- /dev/null +++ b/lisp/acdw-re.el | |||
@@ -0,0 +1,66 @@ | |||
1 | ;;; acdw-re.el -*- lexical-binding: t; coding: utf-8-unix -*- | ||
2 | ;; Author: Case Duckworth <acdw@acdw.net> | ||
3 | ;; Created: 2021-04-29 | ||
4 | ;; Keywords: configuration | ||
5 | ;; URL: https://tildegit.org/acdw/emacs | ||
6 | |||
7 | ;; This file is NOT part of GNU Emacs. | ||
8 | |||
9 | ;;; License: | ||
10 | ;; Everyone is permitted to do whatever with this software, without | ||
11 | ;; limitation. This software comes without any warranty whatsoever, | ||
12 | ;; but with two pieces of advice: | ||
13 | ;; - Don't hurt yourself. | ||
14 | ;; - Make good choices. | ||
15 | |||
16 | ;;; Commentary: | ||
17 | ;; Pulled mostly from karthinks: | ||
18 | ;; https://karthinks.com/software/bridging-islands-in-emacs-1/ | ||
19 | |||
20 | ;;; Code: | ||
21 | |||
22 | (defvar acdw/re-builder-positions nil | ||
23 | "Store point and region bounds before calling re-builder") | ||
24 | |||
25 | (defun acdw/re-builder-save-state (&rest _) | ||
26 | "Save into `acdw/re-builder-positions' the point and region | ||
27 | positions before calling `re-builder'." | ||
28 | (setq acdw/re-builder-positions | ||
29 | (cons (point) | ||
30 | (when (region-active-p) | ||
31 | (list (region-beginning) | ||
32 | (region-end)))))) | ||
33 | |||
34 | (defun reb-replace-regexp (&optional delimited) | ||
35 | "Run `query-replace-regexp' with the contents of re-builder. With | ||
36 | non-nil optional argument DELIMITED, only replace matches | ||
37 | surrounded by word boundaries." | ||
38 | (interactive "P") | ||
39 | (reb-update-regexp) | ||
40 | (let* ((re (reb-target-binding reb-regexp)) | ||
41 | (replacement (query-replace-read-to | ||
42 | re | ||
43 | (concat "Query replace" | ||
44 | (if current-prefix-arg | ||
45 | (if (eq current-prefix-arg '-) | ||
46 | " backward" | ||
47 | " word") | ||
48 | "") | ||
49 | " regexp" | ||
50 | (if (with-selected-window reb-target-window | ||
51 | (region-active-p)) " in region" "")) | ||
52 | t)) | ||
53 | (pnt (car acdw/re-builder-positions)) | ||
54 | (beg (cadr acdw/re-builder-positions)) | ||
55 | (end (caddr acdw/re-builder-positions))) | ||
56 | (with-selected-window reb-target-window | ||
57 | ;; replace with (goto-char (match-beginning 0)) if you want to control | ||
58 | ;; where in the buffer the replacement starts with re-builder | ||
59 | (goto-char pnt) | ||
60 | (setq acdw/re-builder-positions nil) | ||
61 | (reb-quit) | ||
62 | (query-replace-regexp re replacement delimited beg end)))) | ||
63 | |||
64 | (provide 'acdw-re) | ||
65 | |||
66 | ;;; acdw-re.el ends here | ||