emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 3b67044 145/167: Convert ivy formatting functions to dotte


From: Oleh Krehel
Subject: [elpa] master 3b67044 145/167: Convert ivy formatting functions to dotted pairs.
Date: Tue, 08 Dec 2015 10:50:36 +0000

branch: master
commit 3b67044d00573b8207d0df9fa03777237e196bdf
Author: Stephen Whipple <address@hidden>
Commit: Stephen Whipple <address@hidden>

    Convert ivy formatting functions to dotted pairs.
    
    `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  |   22 ++++++++++++--------
 ivy-test.el |    2 +-
 ivy.el      |   63 ++++++++++++++++++++++++++++++++++------------------------
 3 files changed, 51 insertions(+), 36 deletions(-)

diff --git a/counsel.el b/counsel.el
index 5354d39..5491495 100644
--- a/counsel.el
+++ b/counsel.el
@@ -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))
diff --git a/ivy-test.el b/ivy-test.el
index ddfef34..ec5857d 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -115,7 +115,7 @@
 
 (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 c365d47..5578acd 100644
--- 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))))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]