]> code.delx.au - gnu-emacs-elpa/commitdiff
Convert ivy formatting functions to dotted pairs.
authorStephen Whipple <shw@wicdmedia.org>
Thu, 26 Nov 2015 07:07:16 +0000 (00:07 -0700)
committerStephen Whipple <shw@wicdmedia.org>
Thu, 26 Nov 2015 07:19:12 +0000 (00:19 -0700)
`ivy-format-function' now expects to operate on dotted pairs
representing (stub . extra), where `stub' is the original
candidate and `extra' is any extra information that has been
added by counsel or other libraries.

The format function can differentiate between the original stub
and extra information and choose how to display the result to
the user.

counsel.el
ivy-test.el
ivy.el

index 5354d39c5e5e77c34d1bd1382d2b63b4740a0979..549149572a519a39726f4000aefd59e8b749d259 100644 (file)
@@ -822,15 +822,19 @@ When NO-ASYNC is non-nil, do it synchronously."
                (when (= 0 (cl-incf counsel-gg-state))
                  (ivy--exhibit)))))))))
 
-(defun counsel--M-x-transformer (cmd)
-  "Add a binding to CMD if it's bound in the current window.
-CMD is a command name."
-  (let ((binding (substitute-command-keys (format "\\[%s]" cmd))))
+(defun counsel--M-x-transformer (cand-pair)
+  "Add a binding to CAND-PAIR cdr if the car is bound in the current window.
+CAND-PAIR is (command-name . extra-info)."
+  (let* ((command-name (car cand-pair))
+         (extra-info (cdr cand-pair))
+         (binding (substitute-command-keys (format "\\[%s]" command-name))))
     (setq binding (replace-regexp-in-string "C-x 6" "<f2>" binding))
     (if (string-match "^M-x" binding)
-        cmd
-      (format "%s (%s)" cmd
-              (propertize binding 'face 'font-lock-keyword-face)))))
+        cand-pair
+      (cons command-name
+            (if extra-info
+                (format "%s (%s)" extra-info (propertize binding 'face 'font-lock-keyword-face))
+              (format "(%s)" (propertize binding 'face 'font-lock-keyword-face)))))))
 
 (defvar smex-initialized-p)
 (defvar smex-ido-cache)
@@ -863,11 +867,11 @@ Optional INITIAL-INPUT is the initial input in the minibuffer."
                                     ivy-initial-inputs-alist))))
   (let* ((store ivy-format-function)
          (ivy-format-function
-          (lambda (cands)
+          (lambda (cand-pairs)
             (funcall
              store
              (with-ivy-window
-               (mapcar #'counsel--M-x-transformer cands)))))
+               (mapcar #'counsel--M-x-transformer cand-pairs)))))
          (cands obarray)
          (pred 'commandp)
          (sort t))
index ddfef347ea3350fb248555666065942c7bc0012b..ec5857d8581b59bb549b45c701f1ba766963f3d5 100644 (file)
 
 (ert-deftest ivy--format ()
   (should (string= (let ((ivy--index 10)
-                         (ivy-format-function (lambda (x) (mapconcat #'identity x "\n")))
+                         (ivy-format-function (lambda (x) (mapconcat (lambda (y) (car y)) x "\n")))
                          (cands '("NAME"
                                   "SYNOPSIS"
                                   "DESCRIPTION"
diff --git a/ivy.el b/ivy.el
index c365d4706ac2529b9ca2ea5d137bcecb4c6b783b..5578acd672074624a9651d8912650028fc9245d8 100644 (file)
--- a/ivy.el
+++ b/ivy.el
@@ -1895,7 +1895,7 @@ Prefix matches to NAME are put ahead of the list."
      cands)))
 
 (defcustom ivy-format-function 'ivy-format-function-default
-  "Function to transform the list of candidates into a string.
+  "Function to transform the list of candidate pairs into a string.
 This string will be inserted into the minibuffer."
   :type '(choice
           (const :tag "Default" ivy-format-function-default)
@@ -1909,37 +1909,48 @@ This string will be inserted into the minibuffer."
                                     (- (length str) 3))) "...")
     str))
 
-(defun ivy-format-function-default (cands)
-  "Transform CANDS into a string for minibuffer."
+(defun ivy-format-function-default (cand-pairs)
+  "Transform CAND-PAIRS into a string for minibuffer."
   (let ((i -1))
     (mapconcat
-     (lambda (s)
-       (when (eq (cl-incf i) ivy--index)
-         (ivy--add-face s 'ivy-current-match))
-       s)
-     cands "\n")))
-
-(defun ivy-format-function-arrow (cands)
-  "Transform CANDS into a string for minibuffer."
+     (lambda (pair)
+       (let ((stub (car pair))
+             (extra (cdr pair))
+             (curr (eq (cl-incf i) ivy--index)))
+         (when curr
+           (ivy--add-face stub 'ivy-current-match))
+         (if extra (format "%s %s" stub extra) stub)))
+     cand-pairs "\n")))
+
+(defun ivy-format-function-arrow (cand-pairs)
+  "Transform CAND-PAIRS into a string for minibuffer."
   (let ((i -1))
     (mapconcat
-     (lambda (s)
-       (let ((curr (eq (cl-incf i) ivy--index)))
+     (lambda (pair)
+       (let ((stub (car pair))
+             (extra (cdr pair))
+             (curr (eq (cl-incf i) ivy--index)))
          (when curr
-           (ivy--add-face s 'ivy-current-match))
-         (concat (if curr "> " "  ") s)))
-     cands "\n")))
+           (ivy--add-face stub 'ivy-current-match))
+         (concat (if curr "> " "  ")
+                 (if extra (format "%s %s" stub extra) stub))))
+     cand-pairs "\n")))
 
-(defun ivy-format-function-line (cands)
-  "Transform CANDS into a string for minibuffer."
+(defun ivy-format-function-line (cand-pairs)
+  "Transform CAND-PAIRS into a string for minibuffer."
   (let ((i -1))
     (mapconcat
-     (lambda (s)
-       (let ((line (concat s "\n")))
-         (when (eq (cl-incf i) ivy--index)
+     (lambda (pair)
+       (let* ((stub (car pair))
+              (extra (cdr pair))
+              (curr (eq (cl-incf i) ivy--index))
+              (line (if extra
+                        (format "%s %s\n" stub extra)
+                      (concat stub "\n"))))
+         (when curr
            (ivy--add-face line 'ivy-current-match))
          line))
-     cands "")))
+     cand-pairs "")))
 
 (defface ivy-minibuffer-match-face-1
   '((((class color) (background light))
@@ -2041,11 +2052,11 @@ CANDS is a list of strings."
                                        x)))
                                  cands))))
       (setq ivy--current (copy-sequence (nth index cands)))
-      (setq cands (mapcar
-                   #'ivy--format-minibuffer-line
-                   cands))
       (let* ((ivy--index index)
-             (res (concat "\n" (funcall ivy-format-function cands))))
+             (cand-pairs (mapcar
+                          (lambda (cand)
+                            (cons (ivy--format-minibuffer-line cand) nil)) cands))
+             (res (concat "\n" (funcall ivy-format-function cand-pairs))))
         (put-text-property 0 (length res) 'read-only nil res)
         res))))