comparison lisp/imenu.el @ 31560:5dd0eccb46c5

(imenu--truncate-items, imenu--cleanup) (imenu--generic-function): Avoid mapcar. (imenu--replace-spaces): Function removed. (imenu--completion-buffer): Use subst-char-in-string. (imenu-add-to-menubar): Use keymap inheritance.
author Dave Love <fx@gnu.org>
date Tue, 12 Sep 2000 12:49:20 +0000
parents 4c89037af077
children 869a035f39fa
comparison
equal deleted inserted replaced
31559:5066b4e03c62 31560:5dd0eccb46c5
555 elt))) 555 elt)))
556 alist)) 556 alist))
557 557
558 ;;; Truncate all strings in MENULIST to imenu-max-item-length 558 ;;; Truncate all strings in MENULIST to imenu-max-item-length
559 (defun imenu--truncate-items (menulist) 559 (defun imenu--truncate-items (menulist)
560 (mapcar (function 560 (dolist (item menulist)
561 (lambda (item) 561 (cond
562 (cond 562 ((consp (cdr item))
563 ((consp (cdr item)) 563 (imenu--truncate-items (cdr item)))
564 (imenu--truncate-items (cdr item))) 564 (t
565 (t 565 ;; truncate if necessary
566 ;; truncate if necessary 566 (if (and (numberp imenu-max-item-length)
567 (if (and (numberp imenu-max-item-length) 567 (> (length (car item)) imenu-max-item-length))
568 (> (length (car item)) imenu-max-item-length)) 568 (setcar item (substring (car item) 0 imenu-max-item-length)))))))
569 (setcar item (substring (car item) 0 imenu-max-item-length)))))))
570 menulist))
571 569
572 570
573 (defun imenu--make-index-alist (&optional noerror) 571 (defun imenu--make-index-alist (&optional noerror)
574 "Create an index-alist for the definitions in the current buffer. 572 "Create an index-alist for the definitions in the current buffer.
575 573
616 (if alist 614 (if alist
617 (setq imenu--cleanup-seen (cons alist imenu--cleanup-seen)) 615 (setq imenu--cleanup-seen (cons alist imenu--cleanup-seen))
618 (setq alist imenu--index-alist imenu--cleanup-seen (list alist))) 616 (setq alist imenu--index-alist imenu--cleanup-seen (list alist)))
619 617
620 (and alist 618 (and alist
621 (mapcar 619 (mapc
622 (function 620 (lambda (item)
623 (lambda (item) 621 (cond
624 (cond 622 ((markerp (cdr item))
625 ((markerp (cdr item)) 623 (set-marker (cdr item) nil))
626 (set-marker (cdr item) nil)) 624 ;; Don't process one alist twice.
627 ;; Don't process one alist twice. 625 ((memq (cdr item) imenu--cleanup-seen))
628 ((memq (cdr item) imenu--cleanup-seen)) 626 ((imenu--subalist-p item)
629 ((imenu--subalist-p item) 627 (imenu--cleanup (cdr item)))))
630 (imenu--cleanup (cdr item))))))
631 alist) 628 alist)
632 t)) 629 t))
633 630
634 (defun imenu--create-keymap-2 (alist counter &optional commands) 631 (defun imenu--create-keymap-2 (alist counter &optional commands)
635 (let ((map nil)) 632 (let ((map nil))
720 ((and imenu-generic-expression) 717 ((and imenu-generic-expression)
721 (imenu--generic-function imenu-generic-expression)) 718 (imenu--generic-function imenu-generic-expression))
722 (t 719 (t
723 (error "This buffer cannot use `imenu-default-create-index-function'")))) 720 (error "This buffer cannot use `imenu-default-create-index-function'"))))
724 721
725 (defun imenu--replace-spaces (name replacement)
726 ;; Replace all spaces in NAME with REPLACEMENT.
727 ;; That second argument should be a string.
728 (mapconcat
729 (function
730 (lambda (ch)
731 (if (char-equal ch ?\ )
732 replacement
733 (char-to-string ch))))
734 name
735 ""))
736
737 ;; Not used and would require cl at run time 722 ;; Not used and would require cl at run time
738 ;;; (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix) 723 ;;; (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
739 ;;; ;; Takes a nested INDEX-ALIST and returns a flat index alist. 724 ;;; ;; Takes a nested INDEX-ALIST and returns a flat index alist.
740 ;;; ;; If optional CONCAT-NAMES is non-nil, then a nested index has its 725 ;;; ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
741 ;;; ;; name and a space concatenated to the names of the children. 726 ;;; ;; name and a space concatenated to the names of the children.
804 ;; Modify the syntax table used while matching regexps. 789 ;; Modify the syntax table used while matching regexps.
805 (while slist 790 (while slist
806 ;; The character(s) to modify may be a single char or a string. 791 ;; The character(s) to modify may be a single char or a string.
807 (if (numberp (caar slist)) 792 (if (numberp (caar slist))
808 (modify-syntax-entry (caar slist) (cdar slist) table) 793 (modify-syntax-entry (caar slist) (cdar slist) table)
809 (mapcar (function 794 (dolist (c (caar slist))
810 (lambda (c) 795 (modify-syntax-entry c (cdar slist) table)))
811 (modify-syntax-entry c (cdar slist) table)))
812 (caar slist)))
813 (setq slist (cdr slist))) 796 (setq slist (cdr slist)))
814 (goto-char (point-max)) 797 (goto-char (point-max))
815 (imenu-progress-message prev-pos 0 t) 798 (imenu-progress-message prev-pos 0 t)
816 (unwind-protect ; for syntax table 799 (unwind-protect ; for syntax table
817 (save-match-data 800 (save-match-data
818 (set-syntax-table table) 801 (set-syntax-table table)
819 ;; map over the elements of imenu-generic-expression 802 ;; map over the elements of imenu-generic-expression
820 ;; (typically functions, variables ...) 803 ;; (typically functions, variables ...)
821 (mapcar 804 (mapc
822 (function 805 (lambda (pat)
823 (lambda (pat) 806 (let ((menu-title (car pat))
824 (let ((menu-title (car pat)) 807 (regexp (nth 1 pat))
825 (regexp (nth 1 pat)) 808 (index (nth 2 pat))
826 (index (nth 2 pat)) 809 (function (nth 3 pat))
827 (function (nth 3 pat)) 810 (rest (nthcdr 4 pat)))
828 (rest (nthcdr 4 pat))) 811 ;; Go backwards for convenience of adding items in order.
829 ;; Go backwards for convenience of adding items in order. 812 (goto-char (point-max))
830 (goto-char (point-max)) 813 (while (re-search-backward regexp nil t)
831 (while (re-search-backward regexp nil t) 814 (imenu-progress-message prev-pos nil t)
832 (imenu-progress-message prev-pos nil t) 815 (setq beg (match-beginning index))
833 (setq beg (match-beginning index)) 816 ;; Add this sort of submenu only when we've found an
834 ;; Add this sort of submenu only when we've found an 817 ;; item for it, avoiding empty, duff menus.
835 ;; item for it, avoiding empty, duff menus. 818 (unless (assoc menu-title index-alist)
836 (unless (assoc menu-title index-alist) 819 (push (list menu-title) index-alist))
837 (push (list menu-title) index-alist)) 820 (if imenu-use-markers
838 (if imenu-use-markers 821 (setq beg (copy-marker beg)))
839 (setq beg (set-marker (make-marker) beg))) 822 (let ((item
840 (let ((item 823 (if function
841 (if function 824 (nconc (list (match-string-no-properties index)
842 (nconc (list (match-string-no-properties index) 825 beg function)
843 beg function) 826 rest)
844 rest) 827 (cons (match-string-no-properties index)
845 (cons (match-string-no-properties index) 828 beg)))
846 beg))) 829 ;; This is the desired submenu,
847 ;; This is the desired submenu, 830 ;; starting with its title (or nil).
848 ;; starting with its title (or nil). 831 (menu (assoc menu-title index-alist)))
849 (menu (assoc menu-title index-alist))) 832 ;; Insert the item unless it is already present.
850 ;; Insert the item unless it is already present. 833 (unless (member item (cdr menu))
851 (unless (member item (cdr menu)) 834 (setcdr menu
852 (setcdr menu 835 (cons item (cdr menu))))))))
853 (cons item (cdr menu)))))))))
854 patterns) 836 patterns)
855 (set-syntax-table old-table))) 837 (set-syntax-table old-table)))
856 (imenu-progress-message prev-pos 100 t) 838 (imenu-progress-message prev-pos 100 t)
857 ;; Sort each submenu by position. 839 ;; Sort each submenu by position.
858 ;; This is in case one submenu gets items from two different regexps. 840 ;; This is in case one submenu gets items from two different regexps.
879 ;; Create a list for this buffer only when needed. 861 ;; Create a list for this buffer only when needed.
880 (let ((name (thing-at-point 'symbol)) 862 (let ((name (thing-at-point 'symbol))
881 choice 863 choice
882 (prepared-index-alist 864 (prepared-index-alist
883 (mapcar 865 (mapcar
884 (function 866 (lambda (item)
885 (lambda (item) 867 (cons (subst-char-in-string ?\ (aref imenu-space-replacement 0)
886 (cons (imenu--replace-spaces (car item) imenu-space-replacement) 868 (car item))
887 (cdr item)))) 869 (cdr item)))
888 index-alist))) 870 index-alist)))
889 (cond (prompt) 871 (cond (prompt)
890 ((and name (imenu--in-alist name prepared-index-alist)) 872 ((and name (imenu--in-alist name prepared-index-alist))
891 (setq prompt (format "Index item (default %s): " name))) 873 (setq prompt (format "Index item (default %s): " name)))
892 (t (setq prompt "Index item: "))) 874 (t (setq prompt "Index item: ")))
1014 (if (or (and imenu-prev-index-position-function 996 (if (or (and imenu-prev-index-position-function
1015 imenu-extract-index-name-function) 997 imenu-extract-index-name-function)
1016 imenu-generic-expression 998 imenu-generic-expression
1017 (not (eq imenu-create-index-function 999 (not (eq imenu-create-index-function
1018 'imenu-default-create-index-function))) 1000 'imenu-default-create-index-function)))
1019 (let ((newmap (make-sparse-keymap)) 1001 (let ((newmap (make-sparse-keymap)))
1020 (menu-bar (lookup-key (current-local-map) [menu-bar]))) 1002 (set-keymap-parent newmap (current-local-map))
1021 (setq imenu--last-menubar-index-alist nil) 1003 (setq imenu--last-menubar-index-alist nil)
1022 (define-key newmap [menu-bar] 1004 (define-key newmap [menu-bar]
1023 (append (make-sparse-keymap) menu-bar)) 1005 (let ((map (make-sparse-keymap)))
1024 (define-key newmap [menu-bar index] 1006 (define-key map [index]
1025 (cons name (nconc (make-sparse-keymap "Imenu") 1007 `(menu-item ,name ,(make-sparse-keymap "Imenu")))
1026 (make-sparse-keymap)))) 1008 map))
1027 (use-local-map (append newmap (current-local-map))) 1009 (use-local-map newmap)
1028 (add-hook 'menu-bar-update-hook 'imenu-update-menubar)) 1010 (add-hook 'menu-bar-update-hook 'imenu-update-menubar))
1029 (error "The mode `%s' does not support Imenu" mode-name))) 1011 (error "The mode `%s' does not support Imenu" mode-name)))
1030 1012
1031 ;;;###autoload 1013 ;;;###autoload
1032 (defun imenu-add-menubar-index () 1014 (defun imenu-add-menubar-index ()