Subject: Re: bug#14029: 24.2.50; [PATCH] imenu problems with special elements

From: Andreas Politz <[email-masked]>
Date: Mon, 25 Nov 2013 02:32:33 +0100

Let's do that again.

Changelog:

* imenu.el (imenu--subalist-p): Don't error on non-conses and
allow non-lambda lists as functions.

In my opinion a predicate should not throw an error.  Anyway more
important is the `non-lambda' lists part.

(imenu--in-alist): Don't recurse into non-subalists.

Looking at the commentary, the ability to embed functions in a menu has
clearly been added after this functions was written.

(imenu): Don't pass function itself as an argument.

,----[ (info "(elisp) Imenu") ]
|      Selecting a simple element has the effect of moving to position
|      INDEX-POSITION in the buffer.  Special elements look like this:
| 
|           (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...)
| 
|      Selecting a special element performs:
| 
|           (funcall FUNCTION
|                    INDEX-NAME INDEX-POSITION ARGUMENTS...)
`----

(cddr index-item) == (FUNCTION . ARGS), is wrong as argument list, we
need one more cdr.

* doc/lispref/modes.texi: Make it clear that sub-alist is the cdr.

-ap

=== modified file 'doc/lispref/modes.texi'
*** doc/lispref/modes.texi	2013-08-17 11:14:10 +0000
--- doc/lispref/modes.texi	2013-11-25 00:39:51 +0000
***************
*** 2483,2489 ****
  A nested sub-alist element looks like this:
  
  @example
! (@var{menu-title} @var{sub-alist})
  @end example
  
  It creates the submenu @var{menu-title} specified by @var{sub-alist}.
--- 2483,2489 ----
  A nested sub-alist element looks like this:
  
  @example
! (@var{menu-title} . @var{sub-alist})
  @end example
  
  It creates the submenu @var{menu-title} specified by @var{sub-alist}.

=== modified file 'lisp/ChangeLog'
*** lisp/ChangeLog	2013-11-24 22:53:35 +0000
--- lisp/ChangeLog	2013-11-25 00:45:59 +0000
***************
*** 1,3 ****
--- 1,10 ----
+ 2013-11-22  Andreas Politz  <[email-masked]>
+ 	* imenu.el (imenu--subalist-p): Don't error on non-conses and
+ 	allow non-lambda lists as functions.
+ 	(imenu--in-alist): Don't recurse into non-subalists.
+ 	(imenu): Don't pass function itself as an argument.
+ 	* doc/lispref/modes.texi: Make it clear that sub-alist is the cdr.
+ 
  2013-11-24  Simon Schubert  <[email-masked]>  (tiny change)
  
  	* json.el (json-alist-p): Only return non-nil if the alist has

=== modified file 'lisp/imenu.el'
*** lisp/imenu.el	2013-11-24 21:23:47 +0000
--- lisp/imenu.el	2013-11-25 01:26:34 +0000
***************
*** 293,300 ****

  (defun imenu--subalist-p (item)
!   (and (consp (cdr item)) (listp (cadr item))
!        (not (eq (car (cadr item)) 'lambda))))
  
  (defmacro imenu-progress-message (_prevpos &optional _relpos _reverse)
    "Macro to display a progress message.
--- 293,302 ----

  (defun imenu--subalist-p (item)
!   (and (consp item)
!        (consp (cdr item))
!        (listp (cadr item))
!        (not (functionp (cadr item)))))
  
  (defmacro imenu-progress-message (_prevpos &optional _relpos _reverse)
    "Macro to display a progress message.
***************
*** 645,653 ****
        ;;   (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
        ;; while a bottom-level element looks like
        ;;   (INDEX-NAME . INDEX-POSITION)
        ;; We are only interested in the bottom-level elements, so we need to
!       ;; recurse if TAIL is a list.
!       (cond ((listp tail)
  	     (if (setq res (imenu--in-alist str tail))
  		 (setq alist nil)))
  	    ((if imenu-name-lookup-function
--- 647,657 ----
        ;;   (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
        ;; while a bottom-level element looks like
        ;;   (INDEX-NAME . INDEX-POSITION)
+       ;;   or
+       ;;   (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...)
        ;; We are only interested in the bottom-level elements, so we need to
!       ;; recurse if TAIL is a nested ALIST.
!       (cond ((imenu--subalist-p elt)
  	     (if (setq res (imenu--in-alist str tail))
                   (setq alist nil)))
  	    ((if imenu-name-lookup-function
***************
*** 1033,1040 ****
  		(nth 2 index-item) imenu-default-goto-function))
  	   (position (if is-special-item
  			 (cadr index-item) (cdr index-item)))
! 	   (rest (if is-special-item (cddr index-item))))
!       (apply function (car index-item) position rest))
      (run-hooks 'imenu-after-jump-hook)))
  
  (provide 'imenu)
--- 1037,1044 ----
  		(nth 2 index-item) imenu-default-goto-function))
  	   (position (if is-special-item
  			 (cadr index-item) (cdr index-item)))
! 	   (args (if is-special-item (cdr (cddr index-item)))))
!       (apply function (car index-item) position args))
      (run-hooks 'imenu-after-jump-hook)))
  
  (provide 'imenu)