Add `auth-source-pick-first-password'.
authorTed Zlatanov <tzz@lifelogs.com>
Wed, 9 Mar 2011 17:36:28 +0000 (11:36 -0600)
committerTed Zlatanov <tzz@lifelogs.com>
Wed, 9 Mar 2011 17:36:28 +0000 (11:36 -0600)
* auth-source.el (auth-source-pick-first-password): New convenience
function.

lisp/ChangeLog
lisp/auth-source.el

index 3cf50e5..96d521b 100644 (file)
@@ -6,6 +6,7 @@
        '(?a ?b ?c).  The `dropdown-list' support is disabled for now.  Use
        `eval-when-compile' to load `dropdown-list'.
        (auth-source-netrc-saver): Use it.
+       (auth-source-pick-first-password): New convenience function.
 
 2011-03-08  Teodor Zlatanov  <tzz@lifelogs.com>
 
index 1088719..e2a3ce5 100644 (file)
@@ -769,7 +769,26 @@ while \(:host t) would find all host entries."
               (return 'no)))
           'no))))
 
-;;; Backend specific parsing: netrc/authinfo backend
+;;; (auth-source-pick-first-password :host "z.lifelogs.com")
+;;; (auth-source-pick-first-password :port "imap")
+(defun auth-source-pick-first-password (&rest spec)
+  "Pick the first secret found from applying SPEC to `auth-source-search'."
+  (let* ((result (nth 0 (apply 'auth-source-search (plist-put spec :max 1))))
+         (secret (plist-get result :secret)))
+
+    (if (functionp secret)
+        (funcall secret)
+      secret)))
+
+;; (auth-source-format-prompt "test %u %h %p" '((?u "user") (?h "host")))
+(defun auth-source-format-prompt (prompt alist)
+  "Format PROMPT using %x (for any character x) specifiers in ALIST."
+  (dolist (cell alist)
+    (let ((c (nth 0 cell))
+          (v (nth 1 cell)))
+      (when (and c v)
+        (setq prompt (replace-regexp-in-string (format "%%%c" c) v prompt)))))
+  prompt)
 
 (defun auth-source-ensure-strings (values)
   (unless (listp values)
@@ -780,6 +799,8 @@ while \(:host t) would find all host entries."
              value))
          values))
 
+;;; Backend specific parsing: netrc/authinfo backend
+
 (defvar auth-source-netrc-cache nil)
 
 ;;; (auth-source-netrc-parse "~/.authinfo.gpg")
@@ -998,17 +1019,6 @@ See `auth-source-search' for details on SPEC."
       (nth 0 v)
     v))
 
-;; (auth-source-format-prompt "test %u %h %p" '((?u "user") (?h "host")))
-
-(defun auth-source-format-prompt (prompt alist)
-  "Format PROMPT using %x (for any character x) specifiers in ALIST."
-  (dolist (cell alist)
-    (let ((c (nth 0 cell))
-          (v (nth 1 cell)))
-      (when (and c v)
-        (setq prompt (replace-regexp-in-string (format "%%%c" c) v prompt)))))
-  prompt)
-
 ;;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t)
 ;;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t :create-extra-keys '((A "default A") (B)))