-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcc-re-builder.el
88 lines (67 loc) · 2.65 KB
/
cc-re-builder.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
;;; cc-re-builder.el --- Re-Builder Customization -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Charles Choi
;; Author: Charles Choi <[email protected]>
;; Keywords: tools
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 're-builder)
(require 'rx)
(require 'transient)
(require 'casual-lib)
(defun cc/reb-copy ()
"Reformat `reb-copy' result for interactive use.
The implementation of `reb-copy' presumes that its result will be
used in Elisp code and as such escapes certain characters.
Often it is desired to instead use the regexp in an interactive
function such as `query-replace-regexp'. Such functions require
that the regexp not be escaped, which motivates the need for this
function."
(interactive)
(reb-copy)
(let* ((buf (pop kill-ring))
(buf (string-trim-left buf "\""))
(buf (string-trim-right buf "\""))
(buf (replace-regexp-in-string (rx "\\" (group anything)) "\\1" buf)))
(kill-new buf)))
(transient-define-prefix cc/reb-tmenu ()
"Transient menu for Re-Builder commands.
Menu for Re-Builder (`re-builder'), a tool to construct a
regexp interactively.
* References
- Info node `(elisp) Regular Expressions'"
["Re-Builder"
["Copy Regexp"
("c" "For interactive" cc/reb-copy)
("C" "For code" reb-copy)]
["Match"
("p" "Previous" reb-prev-match :transient t)
("n" "Next" reb-next-match :transient t)]
["Settings"
("i" "Change syntax" reb-change-syntax)
("b" "Change target buffer" reb-change-target-buffer)
("t" "Toggle case" reb-toggle-case)]
[""
("S" "Subexp mode" reb-enter-subexp-mode)
("f" "Force update" reb-force-update)]]
[:class transient-row
("s" "ℹ️ Special Characters" cc/reb-info-regexp-special)
("Q" "Quit Re-Builder" reb-quit)
(casual-lib-quit-all)])
(defun cc/reb-info-regexp-special ()
"Get Info for special characters in regular expressions."
(interactive)
(info "(elisp) Regexp Special"))
(keymap-set reb-mode-map "C-o" #'cc/reb-tmenu)
(provide 'cc-re-builder)
;;; cc-re-builder.el ends here