Subject: bug#26457: 25.2; Cannot pass a function to imenu-generic-expression

From: Damien Cassou <[email-masked]>
Date: Wed, 12 Apr 2017 09:32:08 +0200

Damien Cassou <[email-masked]> writes:
>  sort: Wrong type argument: number-or-marker-p, (#<marker at 72 
>  in 
> *scratch*> (lambda (item pos) (message "%s" item)))

here is a patch 

-- 
Damien Cassou
http://damiencassou.seasidehosting.st

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill
>From ae61937fd0ed634c3f63645f76c4e6b681d499be Mon Sep 17 00:00:00 2001
From: Damien Cassou <[email-masked]>
Date: Wed, 12 Apr 2017 07:35:56 +0200
Subject: [PATCH] Fix imenu--sort-by-position for non-pairs parameters
 (bug#26457)

* lisp/imenu.el (imenu--sort-by-position): Fix to accept lists beyond
  pairs.
* test/lisp/imenu-tests.el: Add 2 tests for `imenu--sort-by-position`.
---
 lisp/imenu.el            |  7 ++++++-
 test/lisp/imenu-tests.el | 10 ++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/lisp/imenu.el b/lisp/imenu.el
index 0f47a92..7606593 100644
--- a/lisp/imenu.el
+++ b/lisp/imenu.el
@@ -499,7 +499,12 @@ imenu--sort-by-name
   (string-lessp (car item1) (car item2)))
 
 (defun imenu--sort-by-position (item1 item2)
-  (< (cdr item1) (cdr item2)))
+  "Comparison function to sort items depending on their position.
+Return t if and only if ITEM1's position is lower than ITEM2's
+position."
+  (if (listp (cdr item1))
+      (< (cadr item1) (cadr item2))
+    (< (cdr item1) (cdr item2))))
 
 (defun imenu--relative-position (&optional reverse)
   "Support function to calculate relative position in buffer.
diff --git a/test/lisp/imenu-tests.el b/test/lisp/imenu-tests.el
index 480368f..9309094 100644
--- a/test/lisp/imenu-tests.el
+++ b/test/lisp/imenu-tests.el
@@ -83,6 +83,16 @@ imenu-simple-scan-deftest
 }
 " '("a" "b" "c" "ABC_D"))
 
+(ert-deftest imenu--sort-by-position-pairs ()
+  (should (imenu--sort-by-position '("a" . 2) '("a" . 3)))
+  (should-not (imenu--sort-by-position '("a" . 3) '("a" . 2))))
+
+;; Regression test for bug#26457: 25.2; Cannot pass a function to
+;; imenu-generic-expression
+(ert-deftest imenu--sort-by-position-list ()
+  (should (imenu--sort-by-position '("a" 2 nil) '("a" 3 nil)))
+  (should-not (imenu--sort-by-position '("a" 3 nil) '("a" 2 nil))))
+
 (provide 'imenu-tests)
 
 ;;; imenu-tests.el ends here
-- 
2.9.3