* mu4e: add `mu4e~get-dirs', to speed up getting a list of maildirs

This commit is contained in:
djcb 2012-08-25 12:37:59 +03:00
parent b034936271
commit 905a84b13a
2 changed files with 27 additions and 16 deletions

View File

@ -85,7 +85,7 @@
'highlight 'highlight
'mu4e~speedbar-maildir 'mu4e~speedbar-maildir
maildir-name)) maildir-name))
(mu4e-get-maildirs mu4e-maildir))) (mu4e-get-maildirs)))
(defun mu4e~speedbar-maildir (&optional text token ident) (defun mu4e~speedbar-maildir (&optional text token ident)
"Jump to maildir TOKEN. TEXT and INDENT are not used." "Jump to maildir TOKEN. TEXT and INDENT are not used."

View File

@ -138,6 +138,20 @@ Function will return the cdr of the list element."
(unless chosen (mu4e-error "%S not found" response)) (unless chosen (mu4e-error "%S not found" response))
(cdr chosen))) (cdr chosen)))
(defun mu4e~get-dirs (dir)
"Get a list of directories under DIR."
;; is 'ls' available on this system?
(if (not (string= insert-directory-program "ls"))
;; nope; get the subdirs the slow way
(remove-if
(lambda (de)
(or
(string= (substring de 0 1) ".")
(not (file-directory-p (concat dir "/" de)))))
(directory-files dir))
;; yes; we have ls. let ls sort out the dirs for us
(let ((cmd (concat "ls -d " (shell-quote-argument dir) "/*/")))
(process-lines "sh" "-c" cmd))))
(defun mu4e~get-maildirs-1 (path &optional mdir) (defun mu4e~get-maildirs-1 (path &optional mdir)
"Get maildirs under path, recursively, as a list of relative "Get maildirs under path, recursively, as a list of relative
@ -147,13 +161,8 @@ paths."
;; don't have tmp, new sister dirs. And there we're done! ;; don't have tmp, new sister dirs. And there we're done!
;; 1. get all proper subdirs of the current dir, if it is readable ;; 1. get all proper subdirs of the current dir, if it is readable
(when (file-accessible-directory-p (concat path mdir)) (when (file-accessible-directory-p (concat path mdir))
(let* ((subdirs (let* ((subdirs (mu4e~get-dirs (concat path mdir)))
(remove-if ;; 2. get the list of dirs with a /cur leaf dir
(lambda (de)
(or (not (file-directory-p (concat path mdir "/" de)))
(string-match "\\.\\{1,2\\}$" de)))
(directory-files (concat path mdir))))
;; 2. get the list of dirs with a /cur leaf dir
(maildirs '())) (maildirs '()))
(dolist (dir subdirs) (dolist (dir subdirs)
(when (string= dir "cur") (when (string= dir "cur")
@ -167,20 +176,22 @@ paths."
(defvar mu4e~maildir-list nil "Cached list of maildirs.") (defvar mu4e~maildir-list nil "Cached list of maildirs.")
(defun mu4e-get-maildirs (path) (defun mu4e-get-maildirs ()
"Get maildirs under path, recursively, as a list of relative "Get maildirs under `mu4e-maildir', recursively, as a list of
paths (ie., /archive, /sent etc.). Most of the work is done in relative paths (ie., /archive, /sent etc.). Most of the work is
`mu4e-get-maildirs-1'. Note, these results are /cached/, so the done in `mu4e-get-maildirs-1'. Note, these results are /cached/, so
list of maildirs will not change until you restart mu4e." the list of maildirs will not change until you restart mu4e."
(unless mu4e-maildir (mu4e-error "`mu4e-maildir' is not defined"))
(unless mu4e~maildir-list (unless mu4e~maildir-list
(setq mu4e~maildir-list (setq mu4e~maildir-list
(sort (mu4e~get-maildirs-1 path) (sort (mu4e~get-maildirs-1 mu4e-maildir)
(lambda (m1 m2) (lambda (m1 m2)
(when (string= m1 "/") (when (string= m1 "/")
-1 ;; '/' comes first -1 ;; '/' comes first
(compare-strings m1 0 nil m2 0 nil t)))))) (compare-strings m1 0 nil m2 0 nil t))))))
mu4e~maildir-list) mu4e~maildir-list)
;;(setq mu4e~maildir-list nil)
(defun mu4e-ask-maildir (prompt) (defun mu4e-ask-maildir (prompt)
"Ask the user for a shortcut (using PROMPT) as defined in "Ask the user for a shortcut (using PROMPT) as defined in
@ -192,7 +203,7 @@ maildirs under `mu4e-maildir."
(let ((prompt (mu4e-format "%s" prompt))) (let ((prompt (mu4e-format "%s" prompt)))
(if (not mu4e-maildir-shortcuts) (if (not mu4e-maildir-shortcuts)
(ido-completing-read prompt (ido-completing-read prompt
(mu4e-get-maildirs mu4e-maildir)) (mu4e-get-maildirs))
(let* ((mlist (append mu4e-maildir-shortcuts '(("ther" . ?o)))) (let* ((mlist (append mu4e-maildir-shortcuts '(("ther" . ?o))))
(fnames (fnames
(mapconcat (mapconcat
@ -206,7 +217,7 @@ maildirs under `mu4e-maildir."
mlist ", ")) mlist ", "))
(kar (read-char (concat prompt fnames)))) (kar (read-char (concat prompt fnames))))
(if (= kar ?o) ;; user chose 'other'? (if (= kar ?o) ;; user chose 'other'?
(ido-completing-read prompt (mu4e-get-maildirs mu4e-maildir)) (ido-completing-read prompt (mu4e-get-maildirs))
(or (car-safe (or (car-safe
(find-if (lambda (item) (= kar (cdr item))) mu4e-maildir-shortcuts)) (find-if (lambda (item) (= kar (cdr item))) mu4e-maildir-shortcuts))
(mu4e-error "Invalid shortcut '%c'" kar))))))) (mu4e-error "Invalid shortcut '%c'" kar)))))))