forked from editorconfig/editorconfig-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editorconfig-fnmatch.el
307 lines (256 loc) · 11.1 KB
/
editorconfig-fnmatch.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
;;; editorconfig-fnmatch.el --- Glob pattern matching in Emacs lisp
;; Copyright (C) 2011-2016 EditorConfig Team
;; Author: EditorConfig Team <editorconfig@googlegroups.com>
;; Version: 0.7.2
;; Package-Requires: ((cl-lib "0.5"))
;; URL: https://github.com/editorconfig/editorconfig-emacs#readme
;; See
;; https://github.com/editorconfig/editorconfig-emacs/graphs/contributors
;; or the CONTRIBUTORS file for the list of contributors.
;; This file is part of EditorConfig Emacs Plugin.
;; EditorConfig Emacs Plugin 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.
;; EditorConfig Emacs Plugin 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
;; EditorConfig Emacs Plugin. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; editorconfig-fnmatch.el provides a fnmatch implementation with a few
;; extensions.
;; The main usage of this library is glob pattern matching for EditorConfig, but
;; it can also act solely.
;; editorconfig-fnmatch-p (name pattern)
;; Test whether NAME match PATTERN.
;; PATTERN should be a shell glob pattern, and some zsh-like wildcard matchings
;; can be used:
;; * Matches any string of characters, except path separators (/)
;; ** Matches any string of characters
;; ? Matches any single character
;; [name] Matches any single character in name
;; [^name] Matches any single character not in name
;; {s1,s2,s3} Matches any of the strings given (separated by commas)
;; {min..max} Matches any number between min and max
;; This library is a port from editorconfig-core-py library.
;; https://github.com/editorconfig/editorconfig-core-py/blob/master/editorconfig/fnmatch.py
;;; Code:
(require 'cl-lib)
(defvar editorconfig-fnmatch--cache-hash
(make-hash-table :test 'equal)
"Cache of shell pattern and its translation.")
(defconst editorconfig-fnmatch--left-brace-regexp
"\\(^\\|[^\\]\\){"
"Regular expression for left brace ({).")
(defconst editorconfig-fnmatch--right-brace-regexp
"\\(^\\|[^\\]\\)}"
"Regular expression for right brace (}).")
(defconst editorconfig-fnmatch--numeric-range-regexp
"\\([+-]?[0-9]+\\)\\.\\.\\([+-]?[0-9]+\\)"
"Regular expression for numaric range (like {-3..+3}).")
(defun editorconfig-fnmatch--match-num (regexp string)
"Return how many times REGEXP is found in STRING."
(let ((num 0))
;; START arg does not work as expected in this case
(while (string-match regexp string)
(setq num (1+ num)
string (substring string (match-end 0))))
num))
;;;###autoload
(defun editorconfig-fnmatch-p (name pattern)
"Test whether NAME match PATTERN.
Matching ignores case if `case-fold-search' is non-nil.
PATTERN should be a shell glob pattern, and some zsh-like wildcard matchings can
be used:
* Matches any string of characters, except path separators (/)
** Matches any string of characters
? Matches any single character
[name] Matches any single character in name
[^name] Matches any single character not in name
{s1,s2,s3} Matches any of the strings given (separated by commas)
{min..max} Matches any number between min and max"
(let* ((translated (editorconfig-fnmatch-translate pattern))
(re (car translated))
(num-groups (nth 1 translated))
(match (string-match re name))
(num-groups-len (length num-groups))
(pattern-matched t))
(when match
(let (num-group matched-num-str matched-num min-num max-num)
(dotimes (index num-groups-len)
(setq num-group (nth index num-groups))
(setq matched-num-str (match-string (1+ index)
name)
min-num (car num-group)
max-num (nth 1 num-group))
(setq matched-num (string-to-number matched-num-str))
(when (or (= (aref matched-num-str 0)
?0)
(< matched-num min-num)
(< max-num matched-num))
(setq pattern-matched nil))))
pattern-matched)))
;;(editorconfig-fnmatch-translate "{a,{-3..3}}.js")
;;(editorconfig-fnmatch-p "1.js" "{a,{-3..3}}.js")
(defun editorconfig-fnmatch-translate (pattern)
"Translate a shell PATTERN to a regular expression.
Translation result will be cached, so same translation will not be done twice."
(let ((cached (gethash pattern
editorconfig-fnmatch--cache-hash)))
(or cached
(puthash pattern
(editorconfig-fnmatch--do-translate pattern)
editorconfig-fnmatch--cache-hash))))
(defun editorconfig-fnmatch--do-translate (pattern &optional nested)
"Translate a shell PATTERN to a regular expression.
Set NESTED to t when this function is called from itself.
This function is called from `editorconfig-fnmatch-translate', when no cached
translation is found for PATTERN."
(let ((index 0)
(length (length pattern))
(brace-level 0)
(in-brackets nil)
;; List of strings of resulting regexp
(result ())
(is-escaped nil)
(matching-braces (= (editorconfig-fnmatch--match-num
editorconfig-fnmatch--left-brace-regexp
pattern)
(editorconfig-fnmatch--match-num
editorconfig-fnmatch--right-brace-regexp
pattern)))
(numeric-groups ())
current-char
pos
has-slash
has-comma
num-range)
(while (< index length)
(if (and (not is-escaped)
(string-match "[^]\\*?[{},/\\-]+"
;;(string-match "[^]\\*?[{},/\\-]+" "?.a")
pattern
index)
(eq index (match-beginning 0)))
(setq result `(,@result ,(regexp-quote (match-string 0 pattern)))
index (match-end 0)
is-escaped nil)
(setq current-char (aref pattern index)
index (1+ index))
(cl-case current-char
(?*
(setq pos index)
(if (and (< pos length)
(= (aref pattern pos) ?*))
(setq result `(,@result ".*"))
(setq result `(,@result "[^/]*"))))
(??
(setq result `(,@result ".")))
(?\[
(if in-brackets
(setq result `(,@result "\\["))
(setq pos index
has-slash nil)
(while (and (< pos length)
(not (= (aref pattern pos) ?\]))
(not has-slash))
(if (and (= (aref pattern pos) ?/)
(not (= (aref pattern (- pos 1)) ?\\)))
(setq has-slash t)
(setq pos (1+ pos))))
(if has-slash
(setq result `(,@result ,(concat "\\["
(substring pattern
index
(1+ pos))
"\\]"))
index (+ pos 2))
(if (and (< index length)
(memq (aref pattern index)
'(?! ?^)))
(setq index (1+ index)
result `(,@result "[^"))
(setq result `(,@result "[")))
(setq in-brackets t))))
(?-
(if in-brackets
(setq result `(,@result "-"))
(setq result `(,@result "\\-"))))
(?\]
(setq result `(,@result "]")
in-brackets nil))
(?{
(setq pos index
has-comma nil)
(while (and (or (and (< pos length)
(not (= (aref pattern pos)
?})))
is-escaped)
(not has-comma))
(if (and (eq (aref pattern pos)
?,)
(not is-escaped))
(setq has-comma t)
(setq is-escaped (and (eq (aref pattern pos)
?\\)
(not is-escaped))
pos (1+ pos))))
(if (and (not has-comma)
(< pos length))
(let ((pattern-sub (substring pattern index pos)))
(setq num-range (string-match editorconfig-fnmatch--numeric-range-regexp
pattern-sub))
(if num-range
(setq numeric-groups `(,@numeric-groups ,(mapcar 'string-to-number
(list (match-string 1
pattern-sub)
(match-string 2
pattern-sub))))
result `(,@result "\\([+-]?[0-9]+\\)"))
(let ((inner (editorconfig-fnmatch--do-translate pattern-sub t)))
(setq result `(,@result ,(format "{%s}"
(car inner)))
numeric-groups `(,@numeric-groups ,@(nth 1 inner)))))
(setq index (1+ pos)))
(if matching-braces
(setq result `(,@result "\\(?:")
brace-level (1+ brace-level))
(setq result `(,@result "{")))))
(?,
(if (and (> brace-level 0)
(not is-escaped))
(setq result `(,@result "\\|"))
(setq result `(,@result "\\,"))))
(?}
(if (and (> brace-level 0)
(not is-escaped))
(setq result `(,@result "\\)")
brace-level (- brace-level 1))
(setq result `(,@result "}"))))
(?/
(if (and (<= (+ index 3)
(length pattern))
(string= (substring pattern index (+ index 3))
"**/"))
(setq result `(,@result "\\(?:/\\|/.*/\\)")
index (+ index 3))
(setq result `(,@result "/"))))
(t
(unless (= current-char
?\\)
(setq result `(,@result ,(regexp-quote (char-to-string current-char)))))))
(if (= current-char ?\\)
(progn (when is-escaped
(setq result `(,@result "\\\\")))
(setq is-escaped (not is-escaped)))
(setq is-escaped nil))))
(unless nested
(setq result `("^" ,@result "\\'")))
(list (mapconcat 'identity
result
"")
numeric-groups)))
(provide 'editorconfig-fnmatch)
;;; editorconfig-fnmatch.el ends here