diff options
Diffstat (limited to 'lisp/acdw-re.el')
-rw-r--r-- | lisp/acdw-re.el | 151 |
1 files changed, 0 insertions, 151 deletions
diff --git a/lisp/acdw-re.el b/lisp/acdw-re.el deleted file mode 100644 index eff61e1..0000000 --- a/lisp/acdw-re.el +++ /dev/null | |||
@@ -1,151 +0,0 @@ | |||
1 | ;;; acdw-re.el -*- lexical-binding: t; coding: utf-8-unix -*- | ||
2 | ;; Author: Case Duckworth <(rot13-string "npqj@npqj.arg")> | ||
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 | ;; UPDATED CODE: | ||
21 | ;; https://github.com/karthink/.emacs.d/blob/master/init.el#L981 | ||
22 | ;; https://github.com/karthink/.emacs.d/blob/master/lisp/reb-fix.el | ||
23 | |||
24 | ;;; Code: | ||
25 | |||
26 | (require 're-builder) | ||
27 | |||
28 | (defvar my/re-builder-positions nil | ||
29 | "Store point and region bounds before calling `re-builder'.") | ||
30 | |||
31 | (defun my/re-builder-save-state (&rest _) | ||
32 | "Save the point and region before calling `re-builder'." | ||
33 | (setq my/re-builder-positions | ||
34 | (cons (point) | ||
35 | (when (region-active-p) | ||
36 | (list (region-beginning) | ||
37 | (region-end)))))) | ||
38 | |||
39 | (defun reb-replace-regexp (&optional delimited) | ||
40 | "Run `query-replace-regexp' with the contents of `re-builder'. | ||
41 | With non-nil optional argument DELIMITED, only replace matches | ||
42 | surrounded by word boundaries." | ||
43 | (interactive "P") | ||
44 | (reb-update-regexp) | ||
45 | (let* ((re (reb-target-binding reb-regexp)) | ||
46 | (replacement (query-replace-read-to | ||
47 | re | ||
48 | (concat "Query replace" | ||
49 | (if current-prefix-arg | ||
50 | (if (eq current-prefix-arg '-) | ||
51 | " backward" | ||
52 | " word") | ||
53 | "") | ||
54 | " regexp" | ||
55 | (if (with-selected-window reb-target-window | ||
56 | (region-active-p)) | ||
57 | " in region" | ||
58 | "")) | ||
59 | t)) | ||
60 | (pnt (car my/re-builder-positions)) | ||
61 | (beg (cadr my/re-builder-positions)) | ||
62 | (end (caddr my/re-builder-positions))) | ||
63 | (with-selected-window reb-target-window | ||
64 | (goto-char (or pnt 0)) | ||
65 | (setq my/re-builder-positions nil) | ||
66 | (reb-quit) | ||
67 | (query-replace-regexp re replacement delimited beg end)))) | ||
68 | |||
69 | ;; Restrict re-builder matches to region | ||
70 | |||
71 | (defun reb-update-overlays (&optional subexp) | ||
72 | "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'. | ||
73 | If SUBEXP is non-nil mark only the corresponding sub-expressions." | ||
74 | (let* ((re (reb-target-binding reb-regexp)) | ||
75 | (subexps (reb-count-subexps re)) | ||
76 | (matches 0) | ||
77 | (submatches 0) | ||
78 | firstmatch | ||
79 | here | ||
80 | start end | ||
81 | firstmatch-after-here) | ||
82 | (with-current-buffer reb-target-buffer | ||
83 | (setq here | ||
84 | (if reb-target-window | ||
85 | (with-selected-window reb-target-window (window-point)) | ||
86 | (point)) | ||
87 | start | ||
88 | (if (region-active-p) | ||
89 | (nth 1 my/re-builder-positions) | ||
90 | (nth 0 my/re-builder-positions)) | ||
91 | end | ||
92 | (if (region-active-p) | ||
93 | (nth 2 my/re-builder-positions) | ||
94 | (point-max))) | ||
95 | (reb-delete-overlays) | ||
96 | (goto-char (or start 0)) | ||
97 | (while (and (not (eobp)) | ||
98 | (re-search-forward re end t) | ||
99 | (or (not reb-auto-match-limit) | ||
100 | (< matches reb-auto-match-limit))) | ||
101 | (when (and (= 0 (length (match-string 0))) | ||
102 | (not (eobp))) | ||
103 | (forward-char 1)) | ||
104 | (let ((i 0) | ||
105 | suffix max-suffix) | ||
106 | (setq matches (1+ matches)) | ||
107 | (while (<= i subexps) | ||
108 | (when (and (or (not subexp) (= subexp i)) | ||
109 | (match-beginning i)) | ||
110 | (let ((overlay (make-overlay (match-beginning i) | ||
111 | (match-end i))) | ||
112 | ;; When we have exceeded the number of provided faces, | ||
113 | ;; cycle thru them where `max-suffix' denotes the maximum | ||
114 | ;; suffix for `reb-match-*' that has been defined and | ||
115 | ;; `suffix' the suffix calculated for the current match. | ||
116 | (face | ||
117 | (cond | ||
118 | (max-suffix | ||
119 | (if (= suffix max-suffix) | ||
120 | (setq suffix 1) | ||
121 | (setq suffix (1+ suffix))) | ||
122 | (intern-soft (format "reb-match-%d" suffix))) | ||
123 | ((intern-soft (format "reb-match-%d" i))) | ||
124 | ((setq max-suffix (1- i)) | ||
125 | (setq suffix 1) | ||
126 | ;; `reb-match-1' must exist. | ||
127 | 'reb-match-1)))) | ||
128 | (unless firstmatch (setq firstmatch (match-data))) | ||
129 | (unless firstmatch-after-here | ||
130 | (when (> (point) here) | ||
131 | (setq firstmatch-after-here (match-data)))) | ||
132 | (setq reb-overlays (cons overlay reb-overlays) | ||
133 | submatches (1+ submatches)) | ||
134 | (overlay-put overlay 'face face) | ||
135 | (overlay-put overlay 'priority i))) | ||
136 | (setq i (1+ i)))))) | ||
137 | (let ((count (if subexp submatches matches))) | ||
138 | (message "%s %smatch%s%s" | ||
139 | (if (= 0 count) "No" (int-to-string count)) | ||
140 | (if subexp "subexpression " "") | ||
141 | (if (= 1 count) "" "es") | ||
142 | (if (and reb-auto-match-limit | ||
143 | (= reb-auto-match-limit count)) | ||
144 | " (limit reached)" ""))) | ||
145 | (when firstmatch | ||
146 | (store-match-data (or firstmatch-after-here firstmatch)) | ||
147 | (reb-show-subexp (or subexp 0))))) | ||
148 | |||
149 | (provide 'acdw-re) | ||
150 | |||
151 | ;;; acdw-re.el ends here | ||