-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfor-sequence.el
468 lines (411 loc) · 18.7 KB
/
for-sequence.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
;;; for-sequence.el --- Sequence -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Wing Hei Chan
;; Author: Wing Hei Chan <[email protected]>
;; URL: https://github.com/usaoc/elisp-for
;; Keywords: extensions
;; This file is not part of GNU Emacs.
;; 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:
;; This file provides sequence constructors.
;;; Code:
;;;; Require
(require 'cl-lib)
(require 'generator)
(eval-when-compile
(require 'for-helper)
(require 'for-iteration)
(require 'subr-x))
;;;; Internal
(defsubst for--make-circular (&rest values)
"Make a circular list of VALUES."
(declare (pure t) (side-effect-free error-free))
(let (last)
(let ((tail values))
(while tail (cl-shiftf last tail (cdr tail))))
(setf (cdr last) values)))
(defsubst for--overlay-list (buffer)
"Return (`overlays-in' (`point-min') (`point-max')).
BUFFER is used as the current buffer."
(declare (side-effect-free t))
(if (not buffer) (overlays-in (point-min) (point-max))
(with-current-buffer buffer
(overlays-in (point-min) (point-max)))))
;;;; Interface
(defmacro define-for-sequence (name arglist &rest subforms)
"Define a sequence constructor NAME with ARGLIST and DOCSTRING.
A SUBFORM in SUBFORMS is one of the following types:
- (`declare' [DECLARATION...]), which is a `declare' form;
- (`:alias' [ALIAS...]) where ALIAS is a identifier as an aliases
of NAME;
- (`:type' [TYPE...]) where TYPE is a type specifier;
- (`:expander' (ARG) BODY...) where (`lambda' (ARG) BODY...) is
the definition of the expander or (`:expander-case' [CASE...])
equivalent to (`:expander' (ARG) (`pcase-exhaustive' ARG
[CASE...])).
BODY are the body of generator. See Info node `(for)Definers'.
\(fn NAME ARGLIST [DOCSTRING] [SUBFORM...] [BODY...])"
(declare (debug (&define name lambda-list lambda-doc
[&optional
[&or ("declare" def-declarations)
(":alias" &rest symbolp)
(":type" &rest cl-type-spec)
(&define ":expander" (arg) def-body)
(&define ":expander-case"
&rest (pcase-PAT body))]]
def-body))
(doc-string 3) (indent 2))
(pcase-let
((name-string (symbol-name name))
((or `(,(and (cl-type string)
(app (lambda (form) `(,form)) docstring))
. ,subforms)
(let docstring '()))
subforms))
(named-let parse ((declaration '()) (aliases '())
(types '()) (expander '()) (subforms subforms))
(pcase subforms
(`(,(and (let '() declaration) `(declare . ,_)
(app (lambda (form) `(,form)) more))
. ,subforms)
(parse more aliases types expander subforms))
(`(,(and (let '() aliases) `(:alias . ,more)) . ,subforms)
(parse declaration more types expander subforms))
(`(,(and (let '() types) `(:type . ,more)) . ,subforms)
(parse declaration aliases more expander subforms))
(`(,(and (let '() expander)
(or `(:expander . ,(and `((,_) ,_ . ,_) body))
`(:expander-case
. ,(app (lambda (cases)
`((#1=#:for-clause)
(pcase-exhaustive #1# . ,cases)))
body))))
. ,subforms)
(let ((id (intern (concat name-string
"--for-sequence-expander"))))
(parse declaration aliases types
`((eval-and-compile (defun ,id . ,body))
(define-symbol-prop
',name 'for--sequence-expander ',id))
subforms)))
(_ (let ((body
(pcase-exhaustive subforms
((and '() (let (and (pred (not (memq '&rest)))
(app (remq '&optional) args))
arglist))
(for--with-gensyms (value)
`((for-iter ((,value (,name . ,args))
,value)))))
(`(,_ . ,_) subforms))))
`(progn
,@expander
,@(mapcar (lambda (alias)
`(define-symbol-prop
',alias 'for--alias ',name))
aliases)
,@(mapcar (lambda (type)
`(cl-defmethod for-generator
((#1=#:datum ,type))
,(concat "Call `" name-string
"' with DATUM.")
(,name #1#)))
types)
(defun ,name ,arglist
,@docstring ,@declaration . ,body))))))))
(define-error 'for-unhandled-type "Unhandled type")
(cl-defgeneric for-generator (datum)
"Return an iterator of DATUM.
As a special case, return DATUM as is when it is a function. See
Info node `(for)Sequence Constructors'.")
(cl-defmethod for-generator :around (datum)
"Return DATUM as is when it is a function.
Otherwise, invoke `cl-call-next-method'."
(pcase datum
((cl-type function) datum) (_ (cl-call-next-method))))
(cl-defmethod for-generator (datum)
"Signal `for-unhandled-type' with DATUM."
(signal 'for-unhandled-type (list datum)))
(for--defseq for-in-array (array &optional start end step)
"Return an iterator that returns each item in ARRAY.
Indexes are iterated as in (`for-in-range' START END STEP),
except that END defaults to (`length' ARRAY) when it is nil or
omitted."
(:type array)
(:expander-case
(`(,id ,(for--funcall array-form
&optional start-form end-form step-form))
(for--with-gensyms (array start end step continuep index)
`(,id (:do-in ((,array ,array-form)
(,start ,start-form)
(,end ,end-form)
(,step ,step-form))
((,start (or ,start 0))
(,end (or ,end (length ,array)))
(,step (or ,step 1))
(,continuep (if (< ,step 0) #'> #'<)))
((,index ,start))
((funcall ,continuep ,index ,end))
((,id (aref ,array ,index)))
((+ ,index ,step))))))))
(for--defseq for-in-inclusive-range (start end &optional step)
"Return an iterator that returns each number in inclusive range.
START is the start of range, and END is the end of range. STEP
defaults to 1 when it is nil or omitted. Unlike `for-in-range',
the range is closed."
(:expander-case
(`(,id ,(for--funcall start-form end-form &optional step-form))
(for--with-gensyms (start end step continuep number)
`(,id (:do-in ((,start ,start-form)
(,end ,end-form)
(,step ,step-form))
((,step (or ,step 1))
(,continuep (if (< ,step 0) #'>= #'<=)))
((,number ,start))
((funcall ,continuep ,number ,end))
((,id ,number)) ((+ ,number ,step))))))))
(for--defseq for-in-infinite-range (start &optional step)
"Return an iterator that returns each number in infinite range.
START is the start of range. STEP defaults to 1 when it is nil
or omitted."
(:expander-case
(`(,id ,(for--funcall start-form &optional step-form))
(for--with-gensyms (start step number)
`(,id (:do-in ((,start ,start-form)
(,step ,step-form))
((,step (or ,step 1)))
((,number ,start)) () ((,id ,number))
((+ ,number ,step))))))))
(for--defseq for-in-list (list)
"Return an iterator that returns each element in LIST."
(:type list)
(:expander-case (`(,id (,_ ,list)) (for--in-list id list))))
(for--defseq for-in-producer (producer &rest args)
"Return an iterator that returns each call to PRODUCER.
When CONTINUEP is omitted, the iterator is infinite. Otherwise,
CONTINUEP is a unary predicate. PRODUCER is applied to ARGS in
each iteration, and the produced value is tested by CONTINUEP.
When CONTINUEP returns nil, the iterator stops.
...
\(fn PRODUCER [CONTINUEP [ARG...]])"
(:expander-case
(`(,id (,_ ,producer-form))
(for--with-gensyms (producer value)
(let ((value-form `(funcall ,producer)))
`(,id (:do-in ((,producer ,producer-form)) ()
((,value ,value-form)) () ((,id ,value))
(,value-form))))))
(`(,id (,_ ,producer-form ,continuep-form
. ,(or (and '() arg-ids arg-bindings)
(and `(,_ . ,_)
(app (mapcar (lambda (_form)
(gensym "arg")))
arg-ids)
(app (cl-mapcar (lambda (arg-id arg-form)
`(,arg-id ,arg-form))
arg-ids)
arg-bindings)))))
(for--with-gensyms (producer continuep value)
(let ((value-form `(funcall ,producer . ,arg-ids)))
`(,id (:do-in ((,producer ,producer-form)
(,continuep ,continuep-form) . ,arg-bindings)
() ((,value ,value-form))
((funcall ,continuep ,value))
((,id ,value)) (,value-form)))))))
(if (null args)
(iter-make (cl-loop (iter-yield (funcall producer))))
(pcase-let ((`(,continuep . ,args) args))
(if (null args)
(iter-make (let ((value (funcall producer)))
(while (funcall continuep value)
(iter-yield value)
(setq value (funcall producer)))))
(iter-make (let ((value (apply producer args)))
(while (funcall continuep value)
(iter-yield value)
(setq value (apply producer args)))))))))
(for--defseq for-in-range (start-or-end &optional end step)
"Return an iterator that returns each number in range.
When both END and STEP is nil, START-OR-END is the end of range,
and the start of range is 0. Otherwise, START-OR-END is the
start of range, and END is the end of range. STEP defaults to 1
when it is nil or omitted. Unlike `for-in-inclusive-range', the
range is right half-open when STEP is non-negative, or left
half-open when STEP is negative."
(:type integer)
(:expander-case
(`(,id ,(for--funcall start-or-end-form
&optional end-form step-form))
(for--with-gensyms (start-or-end end step start continuep number)
`(,id (:do-in ((,start-or-end ,start-or-end-form)
(,end ,end-form)
(,step ,step-form))
((,start (if ,end ,start-or-end 0))
(,end (if ,end ,end ,start-or-end))
(,step (or ,step 1))
(,continuep (if (< ,step 0) #'> #'<)))
((,number ,start))
((funcall ,continuep ,number ,end))
((,id ,number)) ((+ ,number ,step))))))))
(for--defseq for-in-iterator (iterator)
"Return the function ITERATOR as is."
(declare (pure t) (side-effect-free t))
(:expander-case
(`(,id (,_ ,iterator-form))
(for--with-gensyms (returned iterator value)
(let* ((returned `',returned)
(next `(condition-case nil (iter-next ,iterator)
(iter-end-of-sequence ,returned))))
`(,id (:do-in ((,iterator ,iterator-form)) () ((,value ,next))
((not (eq ,value ,returned))) ((,id ,value))
(,next)))))))
(cl-the function iterator))
(for--defseq for-in-repeat (value &rest values)
"Return an iterator that repeatedly returns each VALUE.
...
\(fn VALUE...)"
(:expander-case
(`(,id (,_ ,value-form))
(for--with-gensyms (value)
`(,id (:do-in ((,value ,value-form)) () () () ((,id ,value))
()))))
(`(,id (,_ . ,(and `(,_ . ,_)
(app (mapcar (lambda (_form) (gensym "value")))
value-ids)
(app (cl-mapcar (lambda (value-id value-form)
`(,value-id ,value-form))
value-ids)
value-bindings))))
(for--with-gensyms (values tail)
`(,id (:do-in ,value-bindings
((,values (for--make-circular . ,value-ids)))
((,tail ,values)) () ((,id (car ,tail)))
((cdr ,tail)))))))
(if (null values) (iter-make (cl-loop (iter-yield value)))
(iter-make (iter-yield value)
(let (last)
(let ((tail values))
(while tail
(iter-yield (car tail))
(cl-shiftf last tail (cdr tail))))
(setf (cdr last) (push value values)))
(cl-loop (iter-yield (car values))
(cl-callf cdr values)))))
(for--defseq for-in-value (value)
"Return an iterator that returns VALUE once."
(:expander-case
(`(,id (,_ ,value-form))
(for--with-gensyms (value continue)
`(,id (:do-in ((,value ,value-form)) () ((,continue t))
(,continue) ((,id ,value)) (nil))))))
(iter-make (iter-yield value)))
(for--defseq for-on-list (list)
"Return an iterator that returns each cons on LIST."
(:expander-case
(`(,id (,_ ,list-form))
(for--with-gensyms (list tail)
`(,id (:do-in ((,list ,list-form)) () ((,tail ,list))
(,tail) ((,id ,tail)) ((cdr ,tail))))))))
(for--defseq for-in-directory
(directory &optional full match nosort count)
"Return an iterator that returns each file in DIRECTORY.
Equivalent to (`for-in-list' (`directory-files' DIRECTORY FULL
MATCH NOSORT COUNT)), where FULL, NOSORT, and COUNT defaults to
nil when omitted, and MATCH defaults to
`directory-files-no-dot-files-regexp' when it is nil or omitted."
(:expander-case
(`(,id ,(for--funcall directory &optional full match nosort count))
(for--in-list id
(directory-files
directory full
(match directory-files-no-dot-files-regexp)
nosort count)))))
(for--defseq for-in-directory-recursively
(dir regexp
&optional include-directories predicate follow-symlinks)
"Return an iterator that returns each file under DIRECTORY.
Equivalent to (`for-in-list' (`directory-files-recursively' DIR
REGEXP INCLUDE-DIRECTORIES PREDICATE FOLLOW-SYMLINKS)) where
INCLUDE-DIRECTORIES, PREDICATE, and FOLLOW-SYMLINKS defaults to
nil when omitted."
(:expander-case
(`(,id ,(for--funcall
dir regexp
&optional include-directories predicate follow-symlinks))
(for--in-list id
(directory-files-recursively
dir regexp
include-directories predicate follow-symlinks)))))
(for--defseq for-the-buffers (&optional frame)
"Return an iterator that returns each buffer in FRAME.
Equivalent to (`for-in-list' (`buffer-list' FRAME)) where FRAME
defaults to nil when omitted."
(:expander-case
(`(,id ,(or (and `(,_) (let frame nil)) `(,_ ,frame)))
(for--in-list id (buffer-list frame)))))
(for--defseq for-the-frames ()
"Return an iterator that returns each frame.
Not equivalent to (`for-in-list' (`frame-list')), since the
frames are visited from the selected frame in `next-frame'
order."
(:expander-case
(`(,id (,_))
(for--with-gensyms (current original visited)
`(,id (:do-in () ((,original (selected-frame)) (,visited nil))
((,current ,original))
((or (not (eq ,current ,original))
(and (not ,visited) (setq ,visited t))))
((,id ,current)) ((next-frame ,current)))))))
(iter-make (let ((original (selected-frame)))
(iter-yield original)
(let ((current (next-frame original)))
(while (not (eq current original))
(iter-yield current)
(cl-callf next-frame current))))))
(for--defseq for-the-overlays (&optional buffer)
"Return an iterator that returns each overlay in BUFFER.
Equivalent
to (`for-in-list' (`overlays-in' (`point-min') (`point-max')))
with BUFFER as the current buffer. BUFFER defaults to the
current buffer when it is nil or omitted."
(:expander-case
(`(,id ,(or (and `(,_) (let buffer nil)) `(,_ ,buffer)))
(for--in-list id (for--overlay-list buffer)))))
(for--defseq for-the-windows (&optional frame minibuf)
"Return an iterator that returns each window in FRAME.
Not equivalent to (`for-in-list' (`window-list')), since the
frames are visited from the selected window of FRAME in
`next-window' order. FRAME defaults to the selected frame when
it is not a frame or omitted. MINIBUF and FRAME are passed to
`next-window' as the MINIBUF and ALL-FRAMES arguments where
MINIBUF defaults to nil when it is omitted."
(:expander-case
(`(,id ,(for--funcall &optional frame-form minibuf-form))
(for--with-gensyms (frame minibuf current original visited)
`(,id (:do-in ((,frame ,frame-form) (,minibuf ,minibuf-form))
((,original (frame-selected-window
(and (framep ,frame) ,frame)))
(,visited nil))
((,current ,original))
((or (not (eq ,current ,original))
(and (not ,visited) (setq ,visited t))))
((,id ,current))
((next-window ,current ,minibuf ,frame)))))))
(iter-make (let ((original (frame-selected-window
(and (framep frame) frame))))
(iter-yield original)
(let ((current (next-window original minibuf frame)))
(while (not (eq current original))
(iter-yield current)
(cl-callf next-window current minibuf frame))))))
;;;; Provide
(provide 'for-sequence)
;;; for-sequence.el ends here