-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmic-deffilter.el
236 lines (216 loc) · 8.31 KB
/
mic-deffilter.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
;;; mic-deffilter.el --- Definer of filter for mic -*- lexical-binding: t; -*-
;; Copyright (C) 2022 ROCKTAKEY
;; Author: ROCKTAKEY <rocktakey@gmail.com>
;; 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:
;; Definer of filter for mic
;;; Code:
(require 'cl-lib)
(require 'mic-utils)
;;;###autoload
(defmacro mic-deffilter-alias (name alias target &optional docstring)
"Define filter function named NAME with document DOCSTRING.
The filter recieves plist and returns plist.
It make alias named ALIAS to TARGET."
(declare (indent defun))
(let ((key (make-symbol "key"))
(value (make-symbol "value"))
(result (make-symbol "result")))
`(defun ,name (plist)
,(or docstring
(format "Filter for `mic'.
It return PLIST but value on %s is replaced with %s."
alias target))
(let (,result)
(while plist
(let ((,key (pop plist))
(,value (pop plist)))
(when (eq ,key ,alias)
(setq ,key ,target))
(push ,key ,result)
(push ,value ,result)))
(nreverse ,result)))))
;;;###autoload
(defmacro mic-deffilter-const (name &optional docstring &rest plist)
"Define filter function named NAME with document DOCSTRING.
The filter recieves plist and returns plist.
It replace value on each property in PLIST with each value in PLIST."
(declare (indent defun))
(unless (stringp docstring)
(push docstring plist)
(setq docstring nil))
`(defun ,name (plist)
,(or docstring
(format "Filter for `mic'.
It return PLIST but each value on some property below is replaced:
%s" (pp-to-string plist)))
,@(let (result)
(while plist
(let ((key (pop plist))
(value (pop plist)))
(push `(mic-plist-put plist ,key ,value) result)))
(nreverse result))
plist))
;;;###autoload
(defmacro mic-deffilter-const-append (name &optional docstring &rest plist)
"Define filter function named NAME with document DOCSTRING.
The filter recieves plist and returns plist.
It append each value in PLIST on each property to recieved plist."
(declare (indent defun))
(unless (stringp docstring)
(push docstring plist)
(setq docstring nil))
`(defun ,name (plist)
,(or docstring
(format "Filter for `mic'.
It return PLIST but each value on some property below is appended:
%s" (pp-to-string plist)))
,@(let (result)
(while plist
(let ((key (pop plist))
(value (pop plist)))
(push `(mic-plist-put-append plist ,key ,value) result)))
(nreverse result))
plist))
;;;###autoload
(defmacro mic-deffilter-ignore (name keyword &optional docstring)
"Define filter function named NAME with document DOCSTRING.
The filter recieves plist and returns plist.
If the value on KEYWORD in PLIST exists, remove it."
(declare (indent defun))
`(defun ,name (plist)
,(or docstring
(format "Filter for `mic'.
If the value on %s in PLIST exists, remove it."
keyword))
(cl-remf plist ,keyword)
plist))
;;;###autoload
(defmacro mic-deffilter-nonlist-to-list (name keyword &optional docstring)
"Define filter function named NAME with document DOCSTRING.
The filter recieves plist and returns plist.
If the value on KEYWORD in PLIST is not list NON-LIST,
replace it with `(NON-LIST)'."
(declare (indent defun))
(let ((value (make-symbol "value")))
`(defun ,name (plist)
,(or docstring
(format "Filter for `mic'.
If the value on %s in PLIST is symbol SYMBOL, replace to '(SYMBOL)."
keyword))
(let ((,value (plist-get plist ,keyword)))
(unless (listp ,value)
(mic-plist-put
plist
,keyword
(list ,value)))
plist))))
;;;###autoload
(defmacro mic-deffilter-replace-keyword-append
(name filter new-keyword old-keyword replacement-alist &optional docstring)
"Define filter function named NAME with document DOCSTRING.
The filter recieves PLIST and returns plist.
The filter conduct same procedure as FILTER, but input and output keyword is
altered.
1. NEW-KEYWORD is used as an input keyword instead of OLD-KEYWORD.
2. Output keywords are replaced according to REPLACEMENT-ALIST.
Each `cdr' keyword is replaced with `car' keyword in REPLACEMENT-ALIST."
(declare (indent defun))
`(defun ,name (plist)
,(or docstring
(format "Filter for `mic'.
Almost same as `%s' but input keyword is `%s' instead of `%s',
and output is replaced according to a alist:
%s
where each `cdr' keyword is replaced with `car'."
filter new-keyword old-keyword replacement-alist))
(let ((inner-output-plist (,filter (list ,old-keyword (plist-get plist ,new-keyword)))))
(mic-plist-replace-keywords inner-output-plist ,replacement-alist)
(while inner-output-plist
(let ((key (pop inner-output-plist))
(value (pop inner-output-plist)))
(mic-plist-put-append plist
key value))))
(mic-plist-delete plist ,new-keyword)))
;;;###autoload
(defmacro mic-deffilter-convert-after-load
(name filter new-keyword old-keyword &optional docstring)
"Define filter function named NAME with document DOCSTRING.
This macro makes FILTER `after-load'-ized.
The filter recieves PLIST and returns plist.
The filter conduct same procedure as FILTER, but input and output keyword is
altered.
1. NEW-KEYWORD is used as an input keyword instead of OLD-KEYWORD.
2. `:eval' is replaced with `:eval-after-load' in output."
(declare (indent defun))
`(mic-deffilter-replace-keyword-append
,name ,filter ,new-keyword ,old-keyword
'((:eval . :eval-after-load))
,docstring))
;;;###autoload
(defmacro mic-deffilter-t-to-name (name keyword &optional docstring)
"Define filter function named NAME with document DOCSTRING.
The filter recieves plist and returns plist.
It replaces element t of the list value on KEYWORD in PLIST
to the value on `:name'."
(declare (indent defun))
`(defun ,name (plist)
,(or docstring
(format "Filter for `mic'.
It return PLIST but element t of the list value on %s
is replaced to the value on `:name'."
keyword))
(mic-plist-put
plist
,keyword
(mapcar
(lambda (arg)
(if (eq arg t)
(plist-get plist :name)
arg))
(plist-get plist ,keyword)))))
;;;###autoload
(defmacro mic-deffilter-validate (name &optional docstring &rest keywords)
"Define filter function named NAME with document DOCSTRING.
The filter recieves PLIST and return PLIST.
It validates PLIST properties and warn if PLIST has invalid properties.
KEYWORDS is a list of valid properties."
(declare (indent defun))
(unless (stringp docstring)
(push docstring keywords)
(setq docstring nil))
(let ((temp-plist (make-symbol "temp-plist"))
(result (make-symbol "result"))
(key (make-symbol "key"))
(value (make-symbol "value")))
`(defun ,name (plist)
,(or docstring
(format "Filter for `mic'.
It validates PLIST properties and warn if PLIST has invalid properties.
The valid properties are:
%s" (mapconcat
(lambda (arg)
(concat "`" (symbol-name arg) "'"))
keywords
"\n")))
(let ((,temp-plist plist)
,result)
(while ,temp-plist
(let ((,key (pop ,temp-plist))
(,value (pop ,temp-plist)))
(if (not (memq ,key (append ',keywords '(:name))))
(warn "`mic' %s: The keyword %s is not allowed by filter `%s'" (plist-get plist :name) ,key ',name)
(push ,key ,result)
(push ,value ,result))))
(nreverse ,result)))))
(provide 'mic-deffilter)
;;; mic-deffilter.el ends here