-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecb-util.el
2375 lines (2102 loc) · 98.5 KB
/
ecb-util.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
;;; ecb-util.el --- utility functions for ECB
;; Copyright (C) 2000 - 2005 Jesper Nordenberg,
;; Klaus Berndl,
;; Kevin A. Burton,
;; Free Software Foundation, Inc.
;; Author: Jesper Nordenberg <[email protected]>
;; Klaus Berndl <[email protected]>
;; Kevin A. Burton <[email protected]>
;; Maintainer: Klaus Berndl <[email protected]>
;; Keywords: browser, code, programming, tools
;; Created: 2000
;; 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
;; GNU Emacs; see the file COPYING. If not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;; $Id: ecb-util.el,v 1.165 2010/03/02 12:02:59 berndl Exp $
;;; Commentary:
;;
;; Contains misc utility functions for ECB.
;;
;; This file is part of the ECB package which can be found at:
;; http://ecb.sourceforge.net
;;; History
;;
;; For the ChangeLog of this file see the CVS-repository. For a complete
;; history of the ECB-package see the file NEWS.
;;; Code:
(eval-when-compile
(require 'silentcomp))
(eval-when-compile (require 'cl))
;;; ----- Silentcomp-Defs ----------------------------------
;; XEmacs
(silentcomp-defun symbol-value-in-buffer)
(silentcomp-defun button-release-event-p)
(silentcomp-defun button-press-event-p)
(silentcomp-defun event-key)
(silentcomp-defun frame-property)
(silentcomp-defun point-at-bol)
(silentcomp-defun point-at-eol)
(silentcomp-defun frame-parameter)
(silentcomp-defun line-beginning-position)
(silentcomp-defun line-end-position)
(silentcomp-defun window-pixel-edges)
(silentcomp-defun noninteractive)
;; Emacs
(silentcomp-defun event-basic-type)
(silentcomp-defvar noninteractive)
(silentcomp-defun window-edges)
(silentcomp-defun buffer-local-value)
(silentcomp-defun posn-point)
(silentcomp-defun posn-window)
(silentcomp-defun event-start)
(silentcomp-defun set-window-vscroll)
;; XEmacs
(silentcomp-defun make-dialog-box)
(silentcomp-defun display-message)
(silentcomp-defun clear-message)
(silentcomp-defun make-event)
;; Emacs
(silentcomp-defvar message-log-max)
(silentcomp-defvar message-truncate-lines)
(silentcomp-defun x-popup-dialog)
(silentcomp-defun display-images-p)
(silentcomp-defvar tar-subfile-mode)
(silentcomp-defvar archive-subfile-mode)
(silentcomp-defun count-screen-lines)
(silentcomp-defvar header-line-format)
;; timer stuff for Xemacs
(silentcomp-defun delete-itimer)
(silentcomp-defun start-itimer)
;; thing stuff for XEmacs
(silentcomp-defun thing-boundaries)
(silentcomp-defun thing-symbol)
(silentcomp-defun custom-file)
;;; ----- Some constants -----------------------------------
;;;###autoload
(defconst ecb-running-xemacs (featurep 'xemacs))
(defconst ecb-running-gnu-emacs (not ecb-running-xemacs))
(defconst ecb-running-unsupported-emacs (condition-case nil
(<= emacs-major-version 20)
(error t))
"True if running XEmacs or Gnu Emacs < 21.")
(defconst ecb-running-gnu-emacs-version-22 (and ecb-running-gnu-emacs
(>= emacs-major-version 22))
"True if running Gnu Emacs >= version 22")
(defconst ecb-running-gnu-emacs-version-23 (and ecb-running-gnu-emacs
(>= emacs-major-version 23))
"True if running Gnu Emacs >= version 23")
(defconst ecb-temp-dir
(file-name-as-directory
(or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP")
(if (eq system-type 'windows-nt) "c:/temp/" "/tmp/")))
"A directory where ECB can store temporary files.")
(defconst ecb-ecb-dir
(expand-file-name (file-name-directory (locate-library "ecb"))))
(defconst ecb-semantic-dir
(if (locate-library "semantic")
(expand-file-name (file-name-directory (locate-library "semantic")))))
(defconst ecb-ecb-parent-dir (expand-file-name (concat ecb-ecb-dir "../")))
;; we assume that current loaded ECB is a regular XEmacs-package if and only
;; if `ecb-ecb-dir' contains the files "_pkg.el" and "auto-autoloads.el" and
;; we are running XEmacs
(defconst ecb-regular-xemacs-package-p
(and ecb-running-xemacs
(file-exists-p (expand-file-name (concat ecb-ecb-dir "_pkg.el")))
(file-exists-p (expand-file-name (concat ecb-ecb-dir "auto-autoloads.el")))))
;; image support possible with current Emacs setup?
;; This will first checked at activation-time of ECB because otherwise usage
;; of emacs --deamon could fail...
(defvar ecb-images-can-be-used nil
"INTERNAL - DO NOT USE AND CHANGE!")
(defvar ecb-images-can-be-used-init-p nil
"INTERNAL - DO NOT USE AND CHANGE!")
(defun ecb-images-can-be-used ()
"Not nil if images can be used with current Emacs setup."
(if ecb-images-can-be-used-init-p
ecb-images-can-be-used
(setq ecb-images-can-be-used-init-p t)
(setq ecb-images-can-be-used
(and (or (fboundp 'defimage)
(fboundp 'make-image-specifier))
(if (fboundp 'display-images-p)
(display-images-p)
window-system)))))
;;; ----- Tracing ------------------------------------------
;; we use the trace.el library!
;;; ----- Compatibility between GNU Emacs and XEmacs -------
;; miscellaneous differences
(defmacro when-ecb-running-xemacs (&rest body)
"Evaluates BODY when `ecb-running-xemacs' is true. Use this macro when you
want the BODY being parsed by semantic!. If not use the variable
`ecb-running-xemacs'."
`(when ecb-running-xemacs
,@body))
(defmacro when-ecb-running-emacs (&rest body)
"Evaluates BODY when `ecb-running-gnu-emacs' is false. Use this
macro when you want the BODY being parsed by semantic!. If not
use the form \(unless ecb-running-xemacs)."
`(when ecb-running-gnu-emacs
,@body))
(defmacro when-ecb-running-emacs-22 (&rest body)
"Evaluates BODY when `ecb-running-gnu-emacs-version-22' is
true. Use this macro when you want the BODY being parsed by
semantic!. If not use the form \(when ecb-running-gnu-emacs-version-22)."
`(when ecb-running-gnu-emacs-version-22
,@body))
(defmacro when-ecb-running-emacs-23 (&rest body)
"Evaluates BODY when `ecb-running-gnu-emacs-version-23' is
true. Use this macro when you want the BODY being parsed by
semantic!. If not use the form \(when ecb-running-gnu-emacs-version-23)."
`(when ecb-running-gnu-emacs-version-23
,@body))
;; I do not want all this compatibitly stuff being parsed by semantic,
;; therefore i do not use the macro `when-ecb-running-xemacs'!
(when ecb-running-xemacs
(defun ecb-event-to-key (event)
(typecase event
(button-release-event 'mouse-release)
(button-press-event 'mouse-press)
(otherwise
;; the ignore-errors is a little hack because i don't know all
;; events of XEmacs so sometimes event-key produces a
;; wrong-type-argument error.
(ignore-errors (event-key event)))))
(defun ecb-facep (face)
(memq face (face-list)))
(defun ecb-noninteractive ()
"Return non-nil if running non-interactively, i.e. in batch mode."
(noninteractive))
(defun ecb-subst-char-in-string (fromchar tochar string &optional inplace)
"Replace FROMCHAR with TOCHAR in STRING each time it occurs.
Unless optional argument INPLACE is non-nil, return a new string."
(let ((i (length string))
(newstr (if inplace string (copy-sequence string))))
(while (> i 0)
(setq i (1- i))
(if (eq (aref newstr i) fromchar)
(aset newstr i tochar)))
newstr))
(defun ecb-substring-no-properties (string &optional start end)
(let* ((start (or start 0))
(end (or end (length string)))
(string (substring string start end)))
(set-text-properties start end nil string)
string))
(defun ecb-derived-mode-p (&rest modes)
"Non-nil if the current major mode is derived from one of MODES.
Uses the `derived-mode-parent' property of the symbol to trace backwards."
(let ((parent major-mode))
(while (and (not (memq parent modes))
(setq parent (get parent 'derived-mode-parent))))
parent))
(defsubst ecb-count-screen-lines (&optional beg end)
(let ((b (or beg (point-min)))
(e (or end (point-max))))
(count-lines b e)))
(defalias 'ecb-frame-parameter 'frame-property)
(defalias 'ecb-line-beginning-pos 'point-at-bol)
(defalias 'ecb-bolp 'bolp)
(defalias 'ecb-eolp 'eolp)
(defalias 'ecb-bobp 'bobp)
(defalias 'ecb-eobp 'eobp)
(defalias 'ecb-line-end-pos 'point-at-eol)
(defalias 'ecb-event-window 'event-window)
(defalias 'ecb-event-point 'event-point)
(defalias 'ecb-event-buffer 'event-buffer)
(defalias 'ecb-window-full-width 'window-full-width)
(defalias 'ecb-window-full-height 'window-height)
(defalias 'ecb-window-display-height 'window-displayed-height)
(defun ecb-frame-char-width (&optional frame)
(/ (frame-pixel-width frame) (frame-width frame)))
(defun ecb-frame-char-height (&optional frame)
(/ (frame-pixel-height frame) (frame-height frame)))
(defun ecb-window-edges (&optional window)
(let ((pix-edges (window-pixel-edges window)))
(list (/ (nth 0 pix-edges) (ecb-frame-char-width))
(/ (nth 1 pix-edges) (ecb-frame-char-height))
(/ (nth 2 pix-edges) (ecb-frame-char-width))
(/ (nth 3 pix-edges) (ecb-frame-char-height))))))
(unless ecb-running-xemacs
(defun ecb-event-to-key (event)
(let ((type (event-basic-type event)))
(case type
((mouse-1 mouse-2 mouse-3) 'mouse-release)
((down-mouse-1 down-mouse-2 down-mouse-3) 'mouse-press)
(otherwise (event-basic-type event)))))
(defalias 'ecb-facep 'facep)
(defun ecb-noninteractive ()
"Return non-nil if running non-interactively, i.e. in batch mode."
noninteractive)
(defalias 'ecb-subst-char-in-string 'subst-char-in-string)
(defalias 'ecb-substring-no-properties 'substring-no-properties)
(defalias 'ecb-derived-mode-p 'derived-mode-p)
(defsubst ecb-count-screen-lines (&optional beg end)
(count-screen-lines beg end))
(defalias 'ecb-frame-parameter 'frame-parameter)
(defalias 'ecb-line-beginning-pos 'line-beginning-position)
(defalias 'ecb-line-end-pos 'line-end-position)
(defalias 'ecb-bolp 'bolp)
(defalias 'ecb-eolp 'eolp)
(defalias 'ecb-bobp 'bobp)
(defalias 'ecb-eobp 'eobp)
(defun ecb-event-window (event)
(posn-window (event-start event)))
(defun ecb-event-point (event)
(posn-point (event-start event)))
(defun ecb-event-buffer (event)
(window-buffer (ecb-event-window event)))
(defun ecb-window-full-width (&optional window)
(let ((edges (window-edges window)))
(- (nth 2 edges) (nth 0 edges))))
(defalias 'ecb-window-display-height 'window-text-height)
(defalias 'ecb-window-full-height 'window-height)
(defalias 'ecb-frame-char-width 'frame-char-width)
(defalias 'ecb-frame-char-height 'frame-char-height)
(defalias 'ecb-window-edges 'window-edges))
;; thing at point stuff
(if (not ecb-running-xemacs)
(progn
(require 'thingatpt)
(defalias 'ecb-thing-at-point 'thing-at-point)
(defalias 'ecb-end-of-thing 'end-of-thing)
(defalias 'ecb-beginning-of-thing 'beginning-of-thing))
;; Xemacs
(require 'thing)
(defun ecb-thing-at-point (thing)
(let ((bounds (if (eq 'symbol thing)
(thing-symbol (point))
(thing-boundaries (point)))))
(buffer-substring (car bounds) (cdr bounds))))
(defun ecb-end-of-thing (thing)
(goto-char (cdr (if (eq 'symbol thing)
(thing-symbol (point))
(thing-boundaries (point))))))
(defun ecb-beginning-of-thing (thing)
(goto-char (car (if (eq 'symbol thing)
(thing-symbol (point))
(thing-boundaries (point)))))))
;; overlay- and extend-stuff
(if (not ecb-running-xemacs)
(progn
(defalias 'ecb-make-overlay 'make-overlay)
(defalias 'ecb-overlay-p 'overlayp)
(defalias 'ecb-overlay-put 'overlay-put)
(defalias 'ecb-overlay-get 'overlay-get)
(defalias 'ecb-overlay-move 'move-overlay)
(defalias 'ecb-overlay-delete 'delete-overlay)
(defalias 'ecb-overlay-kill 'delete-overlay))
;; XEmacs
(defalias 'ecb-make-overlay 'make-extent)
(defalias 'ecb-overlay-p 'extentp)
(defalias 'ecb-overlay-put 'set-extent-property)
(defalias 'ecb-overlay-get 'extent-property)
(defalias 'ecb-overlay-move 'set-extent-endpoints)
(defalias 'ecb-overlay-delete 'detach-extent)
(defalias 'ecb-overlay-kill 'delete-extent))
;; timer stuff
(if (not ecb-running-xemacs)
(progn
(defalias 'ecb-run-with-timer 'run-with-timer)
(defalias 'ecb-run-with-idle-timer 'run-with-idle-timer)
(defalias 'ecb-cancel-timer 'cancel-timer))
;; XEmacs
(defun ecb-run-with-timer (secs repeat function &rest args)
(start-itimer "ecb-timer" function secs repeat
nil (if args t nil) args))
(defun ecb-run-with-idle-timer (secs repeat function &rest args)
(start-itimer "ecb-idle-timer"
function secs (if repeat secs nil)
t (if args t nil) args))
(defun ecb-cancel-timer (timer)
(delete-itimer timer))
)
;;; ----- Customize stuff ----------------------------------
(defun ecb-custom-file ()
"Filename of that file which is used by \(X)Emacs to store the
customize-options. If no custom-file can be computed or if Emacs reports an
error \(e.g. GNU Emacs complains when calling `custom-file' and Emacs has been
started with -q) nil is returned."
(if ecb-running-xemacs
custom-file
(require 'cus-edit)
(ignore-errors (custom-file))))
(defun ecb-option-get-value (option &optional type)
"Return the value of a customizable ECB-option OPTION with TYPE, where TYPE
can either be 'standard-value \(the default-value of the defcustom) or
'saved-value \(the value stored persistent by the user via customize) or
'customized-value \(the value set but not saved in the customize buffer).
If TYPE is nil then the most recent set value is returned, means it
tries the customized-value, then the saved-value and then the standard-value
in exactly this sequence."
(let ((val (car (if type
(get option type)
(or (get option 'customized-value)
(get option 'saved-value)
(get option 'standard-value))))))
(cond ((not (listp val)) val)
((equal 'quote (car val)) (car (cdr val)))
;; (t (car val)))))
(t (eval val)))))
;;; ----- Assoc helpers ------------------------------------
(defun ecb-remove-assoc (key list)
(delete nil
(mapcar (function (lambda (elem)
(if (equal (car elem) key)
nil
elem)))
list)))
(defun ecb-add-assoc (key-value list)
(cons key-value list))
(defun ecb-find-assoc-value (key list)
(cdr (assoc key list)))
(defun ecb-find-assoc (key list)
(assoc key list))
;;; ----- Some function from cl ----------------------------
(defun ecb-filter (seq pred)
"Filter out those elements of SEQUENCE for which PREDICATE returns nil."
(let ((res))
(dolist (elem seq)
(if (if pred (funcall pred elem) elem)
(setq res (append res (list elem)))))
res))
(defun ecb-some (cl-pred cl-seq &rest cl-rest)
"Return true if PREDICATE is true of any element of SEQ or SEQs.
If so, return the true (non-nil) value returned by PREDICATE."
(if (or cl-rest (nlistp cl-seq))
(catch 'cl-some
(apply 'map nil
(function (lambda (&rest cl-x)
(let ((cl-res (apply cl-pred cl-x)))
(if cl-res (throw 'cl-some cl-res)))))
cl-seq cl-rest) nil)
(let ((cl-x nil))
(while (and cl-seq (not (setq cl-x (funcall cl-pred (pop cl-seq))))))
cl-x)))
(defun ecb-copy-list (list)
"Return a copy of a LIST, which may be a dotted list.
The elements of the list are not copied, just the list structure itself."
(if (fboundp 'copy-sequence)
(copy-sequence list)
(if (consp list)
(let ((res nil))
(while (consp list) (push (pop list) res))
(prog1 (nreverse res) (setcdr res list)))
(car list))))
(defun ecb-set-difference (list1 list2 &optional test-fcn)
"Combine LIST1 and LIST2 using a set-difference operation.
The result list contains all items that appear in LIST1 but not LIST2.
This is a non-destructive function; it makes a copy of the data if necessary
to avoid corrupting the original LIST1 and LIST2.
If TEST-FCN is not nil then it must be a function which is used to check if an
item of LIST1 is an element of LIST2. If TEST-FCN is nil then `memq' is used."
(if (or (null list1) (null list2)) list1
(let ((res nil))
(while list1
(or (if test-fcn
(funcall test-fcn (car list1) list2)
(memq (car list1) list2))
(push (car list1) res))
(pop list1))
res)))
(defun ecb-member (item list &optional test-fcn)
"Find the first occurrence of ITEM in LIST.
Return the sublist of LIST whose car is ITEM. Comparison is done via `equal'
unless TEST-FCN is not nil: In this case TEST-FCN will be used to compare ITEM
with the elements of LIST. If TEST-FCN is `eq' then `memq' is called for
optimization."
(if test-fcn
(if (eq test-fcn 'eq)
;; some optimization
(memq item list)
(progn
(while (and list (not (funcall test-fcn item (car list))))
(setq list (cdr list)))
list))
(member item list)))
;; stolen and adapted from cl-seq.el
(defun ecb-delete-duplicates (cl-seq &optional
cl-test-fcn cl-start cl-end cl-from-end cl-copy)
"Deletes duplicate elements from CL-SEQ.
Comparison is done with `equal' unless CL-TEST-FCN is not nil: In
this case TEST-FCN will be used to compare CL-ITEM with the
elements of CL-SEQ. Specifically, if two elements from the
sequence match according to the test-function \(s.a.) only the
rightmost one is retained. If CL-FROM-END is true, the leftmost
one is retained instead. If CL-START or CL-END is specified, only
elements within that subsequence are examined or removed. If
CL-COPY is nil then it destructively modifies CL-SEQ otherwise a
copy of CL-SEQ with removed duplicates is returned."
(if (listp cl-seq)
(let ((cl-start (or cl-start 0)))
(if cl-from-end
(let ((cl-p (nthcdr cl-start cl-seq))
cl-i)
(setq cl-end (- (or cl-end (length cl-seq)) cl-start))
(while (> cl-end 1)
(setq cl-i 0)
(while (setq cl-i (ecb-position (car cl-p)
(cdr cl-p)
cl-test-fcn
cl-i
(1- cl-end)))
(if cl-copy (setq cl-seq (ecb-copy-list cl-seq)
cl-p (nthcdr cl-start cl-seq) cl-copy nil))
(let ((cl-tail (nthcdr cl-i cl-p)))
(setcdr cl-tail (cdr (cdr cl-tail))))
(setq cl-end (1- cl-end)))
(setq cl-p (cdr cl-p) cl-end (1- cl-end)
cl-start (1+ cl-start)))
cl-seq)
(setq cl-end (- (or cl-end (length cl-seq)) cl-start))
(while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1)
(ecb-position (car cl-seq)
(cdr cl-seq)
cl-test-fcn
0
(1- cl-end)))
(setq cl-seq (cdr cl-seq) cl-end (1- cl-end)))
(let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq)
(setq cl-end (1- cl-end) cl-start 1) cl-seq)))
(while (and (cdr (cdr cl-p)) (> cl-end 1))
(if (ecb-position (car (cdr cl-p))
(cdr (cdr cl-p))
cl-test-fcn
0
(1- cl-end))
(progn
(if cl-copy (setq cl-seq (ecb-copy-list cl-seq)
cl-p (nthcdr (1- cl-start) cl-seq)
cl-copy nil))
(setcdr cl-p (cdr (cdr cl-p))))
(setq cl-p (cdr cl-p)))
(setq cl-end (1- cl-end) cl-start (1+ cl-start)))
cl-seq)))
(let ((cl-res (ecb-delete-duplicates (append cl-seq nil)
cl-test-fcn
cl-start
cl-end
cl-from-end
nil)))
(if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))))
;; (ecb-delete-duplicates (vector 'a 'b 'c 'd 'A 'a 'c 'e) nil nil nil t t)
;; (ecb-delete-duplicates '("a" "b" "c" "d" "A" ("a" . 0) "a" ("c" . "ewfwrew") "e")
;; (function (lambda (l r)
;; (ecb-string= (if (consp l) (car l) l)
;; (if (consp r) (car r) r))
;; ))
;; nil nil t t)
;; (remove-duplicates '("a" "b" "c" "d" "A" ("a" . 0) "a" ("c" . "ewfwrew") "e")
;; :test (function (lambda (l r)
;; (ecb-string= (if (consp l) (car l) l)
;; (if (consp r) (car r) r))
;; ))
;; :from-end nil)
;; stolen and adapted from cl-seq.el
(defun ecb-position (cl-item cl-seq &optional cl-test-fcn cl-start cl-end cl-from-end)
"Return the position of first occurence of CL-ITEM in CL-SEQ.
Comparison is done with `equal' unless CL-TEST-FCN is not nil: In
this case TEST-FCN will be used to compare CL-ITEM with the elements
of CL-SEQ.
Return the 0-based index of the matching item, or nil if not found."
(let ((cl-test (or cl-test-fcn 'equal)))
(or cl-start (setq cl-start 0))
(if (listp cl-seq)
(let ((cl-p (nthcdr cl-start cl-seq)))
(or cl-end (setq cl-end 8000000))
(let ((cl-res nil))
(while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end))
(if (funcall cl-test cl-item (car cl-p))
(setq cl-res cl-start))
(setq cl-p (cdr cl-p) cl-start (1+ cl-start)))
cl-res))
(or cl-end (setq cl-end (length cl-seq)))
(if cl-from-end
(progn
(while (and (>= (setq cl-end (1- cl-end)) cl-start)
(not (funcall cl-test cl-item (aref cl-seq cl-end)))))
(and (>= cl-end cl-start) cl-end))
(while (and (< cl-start cl-end)
(not (funcall cl-test cl-item (aref cl-seq cl-start))))
(setq cl-start (1+ cl-start)))
(and (< cl-start cl-end) cl-start)))))
;; (ecb-position "v" '("a" "b" "c" "d" "A" ("a" . 0) "w" "a" ("c" . "ewfwrew") "e")
;; (function (lambda (l r)
;; (ecb-string= (if (consp l) (car l) l)
;; (if (consp r) (car r) r))
;; ))
;; 0)
;; (ecb-position "d" '("a" "b" "c" "d" "e") 'ecb-string= 1 4)
;; (position "d" '("a" "b" "c" "d" "e") :test 'ecb-string= :start 1 :end 3)
;; (defun ecb-position (seq elem &optional test-fcn)
;; "Return the position of ELEM within SEQ counting from 0. Comparison is done
;; with `equal' unless TEST-FCN is not nil: In this case TEST-FCN will be used to
;; compare ITEM with the elements of SEQ."
;; (if (listp seq)
;; (let ((pos (- (length seq) (length (ecb-member elem seq test-fcn)))))
;; (if (= pos (length seq))
;; nil
;; pos))
;; (catch 'found
;; (dotimes (i (length seq))
;; (if (funcall (or test-fcn 'equal) elem (aref seq i))
;; (throw 'found i)))
;; nil)))
(defun ecb-set-elt (seq n val)
"Set VAL as new N-th element of SEQ. SEQ can be any sequence. SEQ will be
changed because this is desctructive function. SEQ is returned."
(if (listp seq)
(setcar (nthcdr n seq) val)
(aset seq n val))
seq)
(defun ecb-remove-elt (seq n)
"Remove N-th element from SEQ. SEQ can be any sequence. SEQ will be
changed because this is desctructive function. SEQ is returned."
(delq 'ecb-util-remove-marker (ecb-set-elt seq n 'ecb-util-remove-marker)))
(defun ecb-replace-first-occurence (seq old-elem new-elem)
"Replace in SEQ the first occurence of OLD-ELEM with NEW-ELEM. Comparison is
done by `equal'. This is desctructive function. SEQ is returned."
(let ((pos (ecb-position old-elem seq)))
(if pos
(ecb-set-elt seq pos new-elem)))
seq)
(defun ecb-replace-all-occurences (seq old-elem new-elem)
"Replace in SEQ all occurences of OLD-ELEM with NEW-ELEM. Comparison is
done by `equal'. This is desctructive function. SEQ is returned."
(while (ecb-position old-elem seq)
(setq seq (ecb-replace-first-occurence seq old-elem new-elem)))
seq)
(defun ecb-delete-first-occurence-from-list (list elem)
"Replace first occurence of ELEM from LIST. Comparison is done by `equal'.
This is desctructive function. LIST is returned."
(delq 'ecb-util-remove-marker
(ecb-replace-first-occurence list elem 'ecb-util-remove-marker)))
(defun ecb-delete-all-occurences-from-list (list elem)
"Replace all occurences of ELEM from LIST. Comparison is done by `equal'.
This is desctructive function. LIST is returned."
(delq 'ecb-util-remove-marker
(progn
(while (ecb-position elem list)
(setq list (ecb-replace-first-occurence list elem
'ecb-util-remove-marker)))
list)))
(defun ecb-subseq (seq start &optional end)
"Return the subsequence of SEQ from START to END.
If END is omitted, it defaults to the length of the sequence.
If START or END is negative, it counts from the end."
(if (stringp seq) (substring seq start end)
(let (len)
(and end (< end 0) (setq end (+ end (setq len (length seq)))))
(if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
(typecase seq
(list (if (> start 0) (setq seq (nthcdr start seq)))
(if end
(let ((res nil))
(while (>= (setq end (1- end)) start)
(push (pop seq) res))
(nreverse res))
(copy-sequence seq)))
(otherwise (or end (setq end (or len (length seq))))
(let ((res (make-vector (max (- end start) 0) nil))
(i 0))
(while (< start end)
(aset res i (aref seq start))
(setq i (1+ i) start (1+ start)))
res))))))
(defun ecb-concatenate (type &rest seqs)
"Concatenate, into a sequence of type TYPE, the argument SEQUENCES.
TYPE can be 'string, 'vector or 'list."
(case type
(vector (apply 'vconcat seqs))
(string (apply 'concat seqs))
(list (apply 'append (append seqs '(nil))))
(otherwise (ecb-error "Not a sequence type name: %s" type))))
(defun ecb-rotate (seq start-elem)
"Rotate SEQ so START-ELEM is the new first element of SEQ. SEQ is an
arbitrary sequence. Example: \(ecb-rotate '\(a b c d e f) 'c) results in \(c d
e f a b). If START-ELEM is not contained in SEQ then nil is returned."
(let ((start-pos (ecb-position start-elem seq)))
(when start-pos
(ecb-concatenate (typecase seq
(list 'list)
(string 'string)
(vector 'vector))
(ecb-subseq seq start-pos)
(ecb-subseq seq 0 start-pos)))))
(defun ecb-last (seq)
"Return the last elem of the sequence SEQ."
(if (listp seq)
(car (last seq))
(if (> (length seq) 0)
(aref seq (1- (length seq)))
nil)))
(defun ecb-first (seq)
"Return the first elem of the sequence SEQ."
(if (listp seq)
(car seq)
(if (> (length seq) 0)
(aref seq 0)
nil)))
(defun ecb-next-listelem (list elem &optional nth-next)
"Return that element of LIST which follows directly ELEM when ELEM is an
element of LIST. If ELEM is the last element of LIST then return the first
element of LIST. If ELEM is not an element of LIST nil is returned. Elements
are compared with `equal'.
If NTH-NEXT is an integer then the NTH-NEXT element of LIST in the meaning
described above is returned, i.e. the algorithm above is applied NTH-NEXT
times. Example: Suppose LIST = '\(a b c d), ELEM is 'c and NTH-NEXT = 3 then
'b is returned - same result for NTH-NEXT = 7, 11... It works also for
negative integers, so when NTH-NEXT is -1 in the example above then 'b is
returned."
(let ((elem-pos (ecb-position elem list))
(next (or nth-next 1)))
(and elem-pos
(nth (mod (+ elem-pos next)
(length list))
list))))
(defun ecb-aggregate-alist (alist same-predicate sort-predicate)
"Return ALIST as a sorted, aggregated alist.
In the result all items with the same car element (according to
SAME-PREDICATE) are aggregated together. The alist is first sorted by
SORT-PREDICATE which is called with two items of the alist and has to return
not nil if item1 should be precede item2.
Please note: SAME-PREDICATE gets the car of an item as argument, whereas
SORT-PREDICATE gets two complete items as arguments!
Example:
\(ecb-aggregate-alist
'((a . a1) (a . a2) (b . b1) (c . c3) (a . a4) (a . a3) (b . b3) (b . b2))
(function string=)
(lambda (item1 item2)
(string< (symbol-name (car item1)) (symbol-name (car item2)))))
results in
\((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))"
(when (not (null alist))
(let (same
tmp-old-car
tmp-same
(first-time-p t)
old-car)
(nconc
(apply #'nconc
(mapcar
(lambda (item)
(cond
(first-time-p
(push (cdr item) same)
(setq first-time-p nil)
(setq old-car (car item))
nil)
((funcall same-predicate (car item) old-car)
(push (cdr item) same)
nil)
(t
(setq tmp-same same
tmp-old-car old-car)
(setq same (list (cdr item))
old-car (car item))
(list (cons tmp-old-car (nreverse tmp-same))))))
(sort alist (lambda (item1 item2)
(funcall sort-predicate
item1 item2)))))
(list (cons old-car (nreverse same)))))))
;; test
;; (ecb-aggregate-alist
;; '((a . a1) (a . a2) (b . b1) (c . c3) (a . a4) (a . a3) (b . b3) (b . b2))
;; 'string=
;; (lambda (item1 item2)
;; (if (string= (car item1) (car item2))
;; (string< (symbol-name (cdr item1)) (symbol-name (cdr item2)))
;; (string< (car item1) (car item2)))))
(defun ecb-values-of-symbol/value-list (list &optional elem-accessor)
"Return a list of values build from the members of LIST.
The result-list is a list which is build from LIST by using the
symbol-value if a list-member is a symbol and otherwise the
list-member itself.
If ELEM-ACCESSOR is a function then it is used to get that part of an elem
of LIST for which the rule above should be applied."
(let ((elem-acc (or elem-accessor 'identity)))
(mapcar (function (lambda (elem)
(let ((e (funcall elem-acc elem)))
(if (symbolp e)
(symbol-value e)
e))))
list)))
;; Maybe we should enhance this docstring ;-)
(defun ecb-member-of-symbol/value-list (value list &optional elem-accessor
return-accessor compare-fcn)
"Returns not nil when VALUE is a member of that list which is build from
LIST by using the symbol-value if a list-member is a symbol and otherwise the
list-member itself. If a member then the matching elem of LIST is returned.
Per default comparison between VALUE and such a list-elem is done by `equal'
unless third optional argument COMPARE-FCN is not nil: Then this function is
used.
If ELEM-ACCESSOR is a function then it is used to get that part of an elem
of LIST for which the rule above should be applied. If RETURN-ACCESSOR is a
function then it is used to get that part of that list-elem which is equal
according to the rules above."
(let ((elem-acc (or elem-accessor 'identity))
(return-acc (or return-accessor 'identity))
(cmp-fcn (or compare-fcn 'equal)))
(catch 'exit
(dolist (elem list)
(let ((case-fold-search t)
(e (funcall elem-acc elem)))
(if (funcall cmp-fcn value (if (symbolp e)
(symbol-value e)
e))
(throw 'exit (funcall return-acc elem)))
nil)))))
;; tests
;; (ecb-member-of-symbol/value-list ecb-directories-buffer-name
;; ecb-tree-do-not-leave-window-after-select--internal)
;; (ecb-member-of-symbol/value-list ecb-directories-buffer-name
;; (cdr '("adgasgd" .
;; ((ecb-directories-buffer-name . "dir")
;; (ecb-sources-buffer-name . "sou")
;; (ecb-history-buffer-name . "hist"))))
;; 'car
;; 'cdr)
;;; ----- Some regexp stuff -------------------------------
(defsubst ecb-match-regexp-list (str regexp-list &optional elem-accessor
return-accessor)
"Return not nil if STR matches one of the regexps in REGEXP-LIST. If
ELEM-ACCESSOR is a function then it is used to get the regexp from the
processed elem of REGEXP-LIST. If nil the elem itself is used. If
RETURN-ACCESSOR is a function then it is used to get the object to return from
the matching elem. If nil then the matching elem itself is returned."
(let ((elem-acc (or elem-accessor 'identity))
(return-acc (or return-accessor 'identity)))
(catch 'exit
(dolist (elem regexp-list)
(let ((case-fold-search t))
(save-match-data
(if (string-match (funcall elem-acc elem) str)
(throw 'exit (funcall return-acc elem))))
nil)))))
;;; ----- Multicache ---------------------------------------
;; internal functions
(defsubst ecb-multicache-init (cache-var)
"Initialize the ecb-multicache of CACHE-VAR. If CACHE-VAR contains already
a valid cache then nothing is done otherwise a new cache is created."
(or (ecb-multicache-p cache-var)
(set cache-var (make-hash-table :size (get cache-var 'ecb-multicache-size)
:test (get cache-var 'ecb-multicache-test)))))
(defun ecb-multicache-add-empty-key (cache-var key)
"Checks if KEY is already cached in the cache of CACHE-VAR. If yes nothing
is done otherwise a new cache-element with empty subcaches is added to the
cache. All subcaches defined via `defecb-multicache' are created with a
value nil. CACHE-VAR has to be a symbol for which an assoc cache has been
defined with `defecb-multicache'!"
(ecb-multicache-init cache-var)
(or (gethash key (symbol-value cache-var))
;; now we add as value an assoc-list with an element for each registered
;; subcache-element
(puthash key (mapcar (function (lambda (sc)
(cons sc nil)))
(get cache-var
'ecb-multicache-subcache-list))
(symbol-value cache-var))))
(defun ecb-multicache-get-subcache (cache-var key subcache)
"Return that cons-cell which is associated with KEY in the cache of
CACHE-VAR and which has the symbol SUBCACHE as its car. The cdr of this
cons-cell is the currently stored SUBCACHE-value for KEY. If KEY is not cached
then nil is returned."
(ecb-multicache-init cache-var)
(let ((hash-val (gethash key (symbol-value cache-var))))
(and hash-val
(assoc subcache hash-val))))
;; public interface for the multi-cache
(defmacro defecb-multicache (name size test subcache docstring)
"Defines NAME as variable and makes it an ecb-multicache.
This means that for each cache-item of the cache NAME informations can be
associated to different subcaches. SUBCACHE is either a symbol or a list of
symbols. For each symbol in SUBCACHE a subcache is reserved in the cache NAME.
Such a cache is especially senseful if different informations should be
associated to one key.
SIZE is a hint as to how many elements will be put in the cache. If SIZE is
nil then the default is 100. If the cache exceeds SIZE it will be increased
automatically.
TEST must be a symbol that specifies how to compare keys. If TEST is nil then
the default is `equal'.
After defining the cache with this macro the cache can be used immediately\;
there is no need for special initialization. The following functions are
available for setting and accessing values in such a cache:
`ecb-multicache-put-value'
`ecb-multicache-apply-to-value'
`ecb-multicache-get-value'
`ecb-multicache-mapsubcache'
`ecb-multicache-clear-value'
`ecb-multicache-clear-subcache'
`ecb-multicache-remove'
`ecb-multicache-clear'
`ecb-multicache-print-subcache'
`ecb-multicache-p'
The lookup in this multi-cache is really fast because the time required is
essentially _independent_ of how many elements are stored in the cache."
`(progn
(eval-and-compile
(defvar ,name nil ,docstring))
(unless (get ',name 'ecb-multicache-p)
(setq ,name nil)
(put ',name 'ecb-multicache-subcache-list
(if (listp ,subcache)
,subcache
(list ,subcache)))
(put ',name 'ecb-multicache-p t)
(put ',name 'ecb-multicache-size ,(or size 100))
(put ',name 'ecb-multicache-test ,(or test (quote 'equal)))
)))
(put 'defecb-multicache 'lisp-indent-function 4)
;; (insert (pp (macroexpand '(defecb-multicache klaus nil 'equal '(A B C) "docstring"))))
(defun ecb-multicache-p (cache-var)
"Return not nil if the value of CACHE-VAR is a cache defined with
`defecb-multicache'."
(and (hash-table-p (symbol-value cache-var))
(get cache-var 'ecb-multicache-p)))
(defun ecb-multicache-get-value (cache-var key subcache)
"Return the currently associated value for KEY in the subcache SUBCACHE of
the cache of CACHE-VAR. CACHE-VAR has to be a symbol for which an assoc cache
has been defined with `defecb-multicache'!
Be aware that the semantic of nil is not unique because nil can have the
following meanings:
- There is no cached item with KEY at all
- There is an item with KEY in the cache but there is no assigned value for
SUBCACHE.
- nil has been set as value for KEY and SUBCACHE \(via
`ecb-multicache-put-value' or `ecb-multicache-apply-to-value') - but this
is not recommended, see `ecb-multicache-apply-to-value'."
(cdr (ecb-multicache-get-subcache cache-var key subcache)))
(defun ecb-multicache-get-values (cache-var key &optional subcache-list)
"Return an assoc-list with the subcaches listed in SUBCACHE-LIST. If
SUBCACHE-LIST is nil then all currently registered subcaches of CACHE-VAR are
returned. The result is an assoc-list where each element is a cons-cell:
- car: subcache-symbol.