-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathar-translate-paired-delimiters.el
94 lines (76 loc) · 3.01 KB
/
ar-translate-paired-delimiters.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
89
90
91
92
93
94
;;; ar-translate-paired-delimiters.el --- Translate delimiter chars
;; Copyright (C) 2014-2024 Andreas Röhler
;; Author: Andreas Röhler <[email protected]>
;; Keywords: lisp
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Translate delimited forms in region, like parentized into bracketed
;;; Code:
(unless (functionp 'ar-escaped)
(defun ar-escaped ()
"Return t if char is preceded by an odd number of backslashes. "
(interactive "p")
(let ((orig (point))
erg)
(goto-char (match-beginning 0))
(setq erg (< 0 (abs (% (skip-chars-backward "\\\\")2))))
(goto-char orig)
erg)))
(defun ar--translate-paired-delimiters (to)
(delete-char 1)
(insert to))
(defun ar--translate-paired-delimiters-base (beg end opening-old opening-new ending-new)
(save-excursion
(save-restriction
;; (when ar-debug-p (switch-to-buffer (current-buffer)))
(narrow-to-region beg end)
(goto-char beg)
(unless (eq (char-after) opening-old)
(while (and
(search-forward (char-to-string opening-old))
(ar-escaped)))
(forward-char -1))
(save-excursion
(if (eq 123 opening-old)
(while (and
(search-forward (char-to-string 125))
(ar-escaped)))
(forward-sexp))
(forward-char -1)
(ar--translate-paired-delimiters ending-new))
(ar--translate-paired-delimiters opening-new))))
(defun ar-paren2bracket (beg end)
(interactive "r*")
"Translate delimiter chars FROM into char TO. "
(ar--translate-paired-delimiters-base beg end ?\( ?\[ ?\]))
(defun ar-paren2brace (beg end)
(interactive "r*")
"Translate delimiter chars FROM into char TO. "
(ar--translate-paired-delimiters-base beg end ?\( ?{ ?}))
(defun ar-bracket2paren (beg end)
(interactive "r*")
"Translate delimiter chars FROM into char TO. "
(ar--translate-paired-delimiters-base beg end ?\[ ?\( ?\)))
(defun ar-bracket2brace (beg end)
(interactive "r*")
"Translate delimiter chars FROM into char TO. "
(ar--translate-paired-delimiters-base beg end ?\[ ?{ ?}))
(defun ar-brace2paren (beg end)
(interactive "r*")
"Translate delimiter chars FROM into char TO. "
(ar--translate-paired-delimiters-base beg end ?{ ?\( ?\)))
(defun ar-brace2bracket (beg end)
(interactive "r*")
"Translate delimiter chars FROM into char TO. "
(ar--translate-paired-delimiters-base beg end ?{ ?\[ ?\]))
(provide 'ar-translate-paired-delimiters)
;;; ar-translate-paired-delimiters.el ends here