-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathduo-referen.el
1584 lines (1486 loc) · 54.5 KB
/
duo-referen.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; duo-referen.el --- Reference part of duo -*- lexical-binding: t; -*-
;;; Commentary:
;; Use reference to alter list
;; Copyright (C) 2019 Chimay
;;; License:
;;; ----------------------------------------------------------------------
;; This file is not part of 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 2, 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Code:
;;; ----------------------------------------------------------------------
;;; Require
;;; ------------------------------------------------------------
(eval-when-compile
(require 'duo-common))
(declare-function duo-< "duo-common")
(declare-function duo-type-of "duo-common")
(declare-function duo-last "duo-common")
(declare-function duo-inside "duo-common")
(declare-function duo-member "duo-common")
(declare-function duo-truncate "duo-common")
(declare-function duo-previous "duo-common")
(declare-function duo-before "duo-common")
(declare-function duo-circ-previous "duo-common")
(declare-function duo-circ-next "duo-common")
(declare-function duo-circ-before "duo-common")
(declare-function duo-assoc "duo-common")
(declare-function duo-partition "duo-common")
;;; Pointers
;;; ------------------------------------------------------------
(defun duo-deref (reflist)
"Return list referenced by REFLIST.
REFLIST must be one of the forms :
- (cons my-list nil) = (list my-list)
- (cons my-list whatever-you-want)
- (cons not-a-cons . my-list)
That’s all folks."
(let ((one (car reflist))
(two (cdr reflist)))
(cond ((and (not (consp one))
(not (consp two)))
nil)
((and (consp two)
(not (consp one)))
two)
(t one))))
(defun duo-ref-set (reflist list)
"Change the reference of REFLIST to LIST.
See `duo-deref' for the format of REFLIST.
LIST must be a cons."
(when (or (consp list) (null list))
(let ((oldlist (duo-deref reflist)))
(cond ((eq oldlist (car reflist)) (setcar reflist list))
((eq oldlist (cdr reflist)) (setcdr reflist list))))
reflist))
(defun duo-ref-frame (reflist &optional list last)
"Set car of REFLIST to LIST and cdr of REFLIST to LAST in LIST."
(when list
(setcar reflist list))
(when last
(setcdr reflist last)))
;;; Stack & Queue
;;; ------------------------------------------------------------
(defun duo-ref-push-cons (cons reflist)
"Add CONS at the beginning of the list referenced by REFLIST.
Return list referenced by REFLIST.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-push-cons cons reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(setcdr cons (duo-deref reflist))
(duo-ref-set reflist cons)
(duo-deref reflist))
(defun duo-ref-add-cons (cons reflist &optional last)
"Store CONS at the end of list referenced by REFLIST. Return CONS.
If non nil, LAST is used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-add-cons cons reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(last (if last
last
(duo-last list))))
(when last
(setcdr last cons))
(setcdr cons nil)
(unless list
(duo-ref-set reflist cons))
cons))
(defun duo-ref-push (elem reflist)
"Add ELEM at the beginning of the list referenced by REFLIST.
Return list referenced by REFLIST.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-push elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((duo (cons elem (duo-deref reflist))))
(duo-ref-set reflist duo)
(duo-deref reflist)))
(defun duo-ref-add (elem reflist &optional last)
"Add ELEM at the end of list referenced by REFLIST. Return the new LAST.
If non nil, LAST is used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-add elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(last (if last
last
(duo-last list)))
(duo (cons elem nil)))
(when last
(setcdr last duo))
(unless list
(duo-ref-set reflist duo))
duo))
(defun duo-ref-push-new-cons (cons reflist)
"Add CONS at the beginning of list referenced by REFLIST if not already there.
Return list referenced by REFLIST.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-push-new-cons cons reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((list (duo-deref reflist)))
(if (duo-inside cons list)
list
(duo-ref-push-cons cons reflist))))
(defun duo-ref-add-new-cons (cons reflist &optional last)
"Add CONS at the end of list referenced by REFLIST if not already there.
Return the new LAST.
If non nil, LAST is used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-add-new-cons cons reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(unless (duo-inside cons (duo-deref reflist))
(duo-ref-add-cons cons reflist last)))
(defun duo-ref-push-new (elem reflist &optional fn-equal)
"Add ELEM at the beginning of list referenced by REFLIST if not already there.
Return list referenced by REFLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-push-new elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((list (duo-deref reflist)))
(if (duo-member elem list fn-equal)
list
(duo-ref-push elem reflist))))
(defun duo-ref-add-new (elem reflist &optional last fn-equal)
"Add ELEM at the end of the list referenced by REFLIST.
Do nothing if ELEM is already present.
Return the new LAST.
If non nil, LAST is used to speed up the process.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-add-new elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(unless (duo-member elem (duo-deref reflist) fn-equal)
(duo-ref-add elem reflist last)))
(defun duo-ref-pop (reflist)
"Remove first element in the list referenced by REFLIST. Return popped cons.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-pop' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(setq popped (duo-ref-pop reflist))
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(popped list))
(duo-ref-set reflist (cdr list))
(setcdr popped nil)
popped))
(defun duo-ref-drop (reflist)
"Remove last element of list referenced by REFLIST.
Return cons of removed element.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-drop reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(before-last (duo-last list 2))
(last (cdr before-last)))
(if last
(setcdr before-last nil)
;; One element list
(setq last list)
(duo-ref-set reflist nil))
last))
(defun duo-ref-push-and-truncate (elem reflist &optional num)
"Add ELEM at the beginning of list referenced by REFLIST.
Truncate list referenced by REFLIST to its first NUM elements.
If NUM is nil, do nothing.
Return list referenced by REFLIST.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-push-and-truncate elem reflist num)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(duo-ref-push elem reflist)
(duo-truncate (duo-deref reflist) num)
(duo-deref reflist))
(defun duo-ref-add-and-clip (elem reflist &optional num length last)
"Add ELEM at the end of list referenced by REFLIST.
Truncate list referenced by REFLIST to its last NUM elements.
If NUM is nil, do nothing.
Return the new LAST.
If non nil, LENGTH is used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-add-and-clip elem reflist num)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(length (if length
length
(length list)))
(added (duo-ref-add elem reflist last)))
(when added
(setq length (1+ length)))
(while (> length num)
(duo-ref-pop reflist)
(setq length (1- length)))
added))
(defun duo-ref-push-new-and-truncate (elem reflist &optional num fn-equal)
"Push ELEM to list referenced by REFLIST if not there and truncate.
The first NUM elements are kept.
If NUM is nil, do nothing.
Return list referenced by REFLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-push-new-and-truncate elem reflist num)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((list (duo-deref reflist)))
(if (duo-member elem list fn-equal)
list
(duo-ref-push-and-truncate elem reflist num))))
(defun duo-ref-add-new-and-clip (elem reflist &optional num length last fn-equal)
"Add ELEM at the end of list referenced by REFLIST if not there.
If NUM is nil, do nothing.
Return the new LAST.
If non nil, LENGTH and LAST are used to speed up the process.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-add-new-and-clip elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(unless (duo-member elem (duo-deref reflist) fn-equal)
(duo-ref-add-and-clip elem reflist num length last)))
;;; Rotate <- ->
;;; ------------------------------
(defun duo-ref-rotate-left (reflist)
"Rotate list referenced by REFLIST to the left.
Return list referenced by REFLIST.
Equivalent to pop first element and add it to the end.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-pop' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-rotate-left reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
;; Length list > 1
(when (cdr (duo-deref reflist))
(let ((popped (duo-ref-pop reflist)))
(duo-ref-add-cons popped reflist)))
(duo-deref reflist))
(defun duo-ref-rotate-right (reflist)
"Rotate list referenced by REFLIST to the right.
Return list referenced by REFLIST.
Equivalent to drop last element and push it at the beginning.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself as argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-rotate-right reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
;; Length list > 1
(when (cdr (duo-deref reflist))
(let ((dropped (duo-ref-drop reflist)))
(duo-ref-push-cons dropped reflist)))
(duo-deref reflist))
;;; Roll
;;; ------------------------------
(defun duo-ref-roll-cons-to-beg (cons reflist &optional previous)
"Roll list referenced by REFLIST to the left until CONS is at the beginning.
Return list referenced by REFLIST.
CONS must be a cons in list referenced by REFLIST.
If non nil, PREVIOUS is used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-roll-cons-to-beg cons reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(previous (if (and previous (eq cons (cdr previous)))
previous
(duo-previous cons list)))
(last previous))
(when (and cons previous (cdr list))
(while (cdr last)
(setq last (cdr last)))
(setcdr previous nil)
(setcdr last list)
(duo-ref-set reflist cons)))
(duo-deref reflist))
(defun duo-ref-roll-cons-to-end (cons reflist)
"Roll list referenced by REFLIST to the right until CONS is at the end.
Return list referenced by REFLIST.
CONS must be a cons in list referenced by REFLIST.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-roll-cons-to-end cons reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(next (cdr cons))
(last next))
(when (and cons next (cdr list))
(while (cdr last)
(setq last (cdr last)))
(setcdr cons nil)
(setcdr last list)
(duo-ref-set reflist next)))
(duo-deref reflist))
(defun duo-ref-roll-to-beg (elem reflist &rest restargs)
"Roll list referenced by REFLIST to the left until ELEM is at the beginning.
Return list referenced by REFLIST.
ELEM must be present in list referenced by REFLIST.
If non nil in RESTARGS :
- PREVIOUS is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-roll-to-beg elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(previous (or (car (cdr (car (duo-assoc "cons" argassoc))))
(duo-before elem list 1 fn-equal)))
(duo (if previous
(cdr previous)
list)))
(duo-ref-roll-cons-to-beg duo reflist previous)))
(defun duo-ref-roll-to-end (elem reflist &optional fn-equal)
"Roll list referenced by REFLIST to the right until ELEM is at the end.
Return list referenced by REFLIST.
ELEM must be present in list referenced by REFLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-roll-to-end elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(duo (duo-member elem list fn-equal)))
(duo-ref-roll-cons-to-end duo reflist)))
;;; Reverse
;;; ------------------------------
(defun duo-ref-reverse (reflist)
"Reverse list referenced by REFLIST. Return list referenced by REFLIST.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-reverse reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(newlist (duo-last list))
(current newlist)
(previous (duo-previous current list)))
(while previous
(setcdr current previous)
(setq current previous)
(setq previous (duo-previous current list)))
(setcdr current nil)
(duo-ref-set reflist newlist)
newlist))
(defun duo-ref-reverse-previous (cons reflist)
"Reverse first part of list referenced by REFLIST.
The fist part goes from beginning to CONS included.
Return list referenced by REFLIST.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-reverse-previous cons reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((next (cdr cons)))
(setcdr cons nil)
(duo-ref-reverse reflist)
(setcdr (duo-last (duo-deref reflist)) next)
(duo-deref reflist)))
(defun duo-ref-reverse-next (cons reflist)
"Reverse second part of list referenced by REFLIST, from after CONS to end.
Return list referenced by REFLIST.
Destructive."
(let ((refnext (list (cdr cons))))
(setcdr cons nil)
(duo-ref-reverse refnext)
(setcdr cons (duo-deref refnext))
(duo-deref reflist)))
(defun duo-ref-reverse-before (elem reflist &optional fn-equal)
"Reverse first part of list referenced by REFLIST.
The first part goes from beginning to ELEM included.
Return list referenced by REFLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-reverse-before elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((duo (duo-member elem (duo-deref reflist) fn-equal)))
(duo-ref-reverse-previous duo reflist)))
(defun duo-ref-reverse-after (elem reflist &optional fn-equal)
"Reverse second part of list referenced by REFLIST, from after ELEM to end.
Return list referenced by REFLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
Destructive."
(let ((duo (duo-member elem (duo-deref reflist) fn-equal)))
(duo-ref-reverse-next duo reflist)))
;;; Insert
;;; ------------------------------------------------------------
;;; Cons Cons
;;; ------------------------------
(defun duo-ref-insert-cons-previous (cons new reflist &optional previous)
"Insert NEW before CONS in list referenced by REFLIST. Return NEW.
CONS must be a cons in list referenced by REFLIST.
NEW is the cons inserted.
If non nil, PREVIOUS inserted is used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-insert-cons-previous cons new reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((list (duo-deref reflist)))
(if (eq cons list)
(duo-ref-push-cons new reflist)
(let ((previous (if (and previous
(eq cons (cdr previous))
(not (eq previous new)))
previous
(duo-previous cons list))))
(when (and cons new previous)
(setcdr new (cdr previous))
(setcdr previous new))
new))))
(defun duo-ref-insert-cons-next (cons new)
"Insert NEW after CONS in list. Return NEW.
CONS must be a cons in list.
NEW is the cons inserted.
Destructive."
(when (and cons new)
(setcdr new (cdr cons))
(setcdr cons new))
new)
;;; Cons Elem
;;; ------------------------------
(defun duo-ref-insert-previous (cons new reflist &optional previous)
"Insert NEW before CONS in list referenced by REFLIST. Return cons of NEW.
CONS must be a cons in list referenced by REFLIST.
NEW is the value of the element inserted.
If non nil, PREVIOUS inserted is used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-insert-previous cons new reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((duo (list new)))
(duo-ref-insert-cons-previous cons duo reflist previous)))
(defun duo-ref-insert-next (cons new)
"Insert NEW after CONS in list. Return cons of NEW.
CONS must be a cons in list.
NEW is the value of the element inserted.
Destructive."
(let ((duo (list new)))
(duo-ref-insert-cons-next cons duo)))
;;; Elem Cons
;;; ------------------------------
(defun duo-ref-insert-cons-before (elem new reflist &rest restargs)
"Insert NEW before ELEM in list referenced by REFLIST. Return NEW.
ELEM must be present in list.
NEW is the cons inserted.
If non nil in RESTARGS :
- PREVIOUS inserted is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-insert-cons-before elem new reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre (car (cdr (car (duo-assoc "cons" argassoc)))))
(previous (if (and arg-pre
(funcall fn-equal elem (car (cdr arg-pre))))
arg-pre
(duo-before elem list 1 fn-equal)))
(duo (if (funcall fn-equal elem (car list))
list
(cdr previous))))
(duo-ref-insert-cons-previous duo new reflist previous)))
(defun duo-ref-insert-cons-after (elem new reflist &optional fn-equal)
"Insert NEW after ELEM in list referenced in REFLIST. Return NEW.
ELEM must be present in list.
NEW is the cons inserted.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
Destructive."
(let ((duo (duo-member elem (duo-deref reflist) fn-equal)))
(duo-ref-insert-cons-next duo new)))
;;; Elem Elem
;;; ------------------------------
(defun duo-ref-insert-before (elem new reflist &rest restargs)
"Insert NEW before ELEM in list referenced by REFLIST. Return cons of NEW.
ELEM must be present in list.
NEW is the value of the element inserted.
If non nil in RESTARGS :
- PREVIOUS inserted is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-insert-before elem new reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre (car (cdr (car (duo-assoc "cons" argassoc)))))
(previous (if (and arg-pre
(funcall fn-equal elem (car (cdr arg-pre))))
arg-pre
(duo-before elem list 1 fn-equal)))
(cons-elem (if (funcall fn-equal elem (car list))
list
(cdr previous)))
(cons-new (list new)))
(duo-ref-insert-cons-previous cons-elem cons-new reflist previous)))
(defun duo-ref-insert-after (elem new reflist &optional fn-equal)
"Insert NEW after ELEM referenced in REFLIST. Return cons of NEW.
ELEM must be present in list.
NEW is the value of the element inserted.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
Destructive."
(let* ((list (duo-deref reflist))
(cons-elem (duo-member elem list fn-equal))
(cons-new (list new)))
(duo-ref-insert-cons-next cons-elem cons-new)))
;;; Remove
;;; ------------------------------------------------------------
(defun duo-ref-remove (cons reflist &optional previous)
"Remove CONS from list referenced by REFLIST. Return CONS.
CONS must be a cons in list referenced by REFLIST.
If non nil, PREVIOUS removed is used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-pop' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-remove cons reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((list (duo-deref reflist)))
(if (eq cons list)
(duo-ref-pop reflist)
(let ((previous (if (and previous
(eq cons (cdr previous))
(not (eq previous cons)))
previous
(duo-previous cons list))))
(when (and cons previous)
(setcdr previous (cdr cons))
(setcdr cons nil))
cons))))
(defun duo-ref-delete (elem reflist &rest restargs)
"Delete ELEM from list referenced by REFLIST. Return removed cons.
If non nil in RESTARGS :
- PREVIOUS deleted is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-pop' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-delete elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre (car (cdr (car (duo-assoc "cons" argassoc)))))
(previous (if (and arg-pre
(funcall fn-equal elem (car (cdr arg-pre))))
arg-pre
(duo-before elem list 1 fn-equal)))
(duo (if (funcall fn-equal elem (car list))
list
(cdr previous))))
(if (and duo list)
(duo-ref-remove duo reflist previous)
nil)))
(defun duo-ref-delete-all (elem reflist &optional fn-equal)
"Delete all elements equals to ELEM from list referenced by REFLIST.
Return list of removed cons.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-pop' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-delete-all elem reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let ((removed)
(removed-list (list nil))
(last)
(list)
(duo)
(pre)
(next)
(fn-equal (or fn-equal #'equal)))
(while (funcall fn-equal elem (car (duo-deref reflist)))
(setq removed (duo-ref-pop reflist))
(setq last (duo-ref-add-cons removed removed-list last)))
(setq list (duo-deref reflist))
(setq duo list)
(setq pre nil)
(while duo
(setq next (cdr duo))
(if (funcall fn-equal elem (car duo))
(progn
(duo-ref-remove duo reflist pre)
(setq removed duo)
(setq last (duo-ref-add-cons removed removed-list last))
(setq pre nil))
(setq pre duo))
(setq duo next))
(duo-deref removed-list)))
;;; Teleport
;;; ------------------------------------------------------------
;;; Cons Cons
;;; ------------------------------
(defun duo-ref-teleport-cons-previous (cons moved reflist
&optional pre-removed pre-inserted)
"Move MOVED before CONS in list referenced by REFLIST. Return MOVED.
CONS must be a cons in list referenced by REFLIST.
MOVED is the cons of the moved element.
If non nil, PRE-REMOVED and PRE-INSERTED
are used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-teleport-cons-previous cons moved reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(when (and cons moved (not (eq cons moved)))
(duo-ref-remove moved reflist pre-removed)
(duo-ref-insert-cons-previous cons moved reflist pre-inserted))
moved)
(defun duo-ref-teleport-cons-next (cons moved reflist &optional previous)
"Move MOVED after CONS in list referenced by REFLIST. Return MOVED.
CONS must be a cons in list referenced by REFLIST.
MOVED is the cons of the moved element.
If non nil, PREVIOUS removed is used to speed up the process.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-teleport-cons-next cons moved reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(when (and cons moved (not (eq cons moved)))
(duo-ref-remove moved reflist previous)
(duo-ref-insert-cons-next cons moved))
moved)
;;; Cons Elem
;;; ------------------------------
(defun duo-ref-teleport-previous (cons moved reflist &rest restargs)
"Move MOVED before CONS in list referenced by REFLIST. Return cons of MOVED.
CONS must be a cons in list referenced by REFLIST.
MOVED is the value of the moved element.
If non nil in RESTARGS :
- PRE-REMOVED and PRE-INSERTED are used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
See `duo-deref' for the format of REFLIST.
See the docstring of `duo-naive-push' to know why it doesn’t
use the list itself in argument.
Common usage :
;; Create reflist
\(setq reflist (list mylist))
;; Modify
\(duo-ref-teleport-previous cons moved reflist)
;; Update list
\(setq mylist (duo-deref reflist))
Destructive."
(let* ((list (duo-deref reflist))
(argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre-removed (car (cdr (car (duo-assoc "cons" argassoc)))))
(pre-removed (if (and
arg-pre-removed
(funcall fn-equal moved (car (cdr arg-pre-removed))))
arg-pre-removed
(duo-before moved list 1 fn-equal)))
(pre-inserted (car (nthcdr 2 (car (duo-assoc "cons" argassoc)))))
(duo (if pre-removed
(cdr pre-removed)
(duo-member moved list fn-equal))))
(duo-ref-teleport-cons-previous cons duo reflist pre-removed pre-inserted)))
(defun duo-ref-teleport-next (cons moved reflist &rest restargs)
"Move MOVED after CONS in list referenced by REFLIST. Return cons of MOVED.