* mu4e: some (micro)optimizations, code cleanups

This commit is contained in:
djcb 2012-09-15 21:10:46 +03:00
parent 5a9b867ca8
commit f5ee44fa81
2 changed files with 84 additions and 91 deletions

View File

@ -266,11 +266,16 @@ into a string."
(make-string (* (if (plist-get thread :empty-parent) 0 2) (make-string (* (if (plist-get thread :empty-parent) 0 2)
(plist-get thread :level)) ?\s) (plist-get thread :level)) ?\s)
(cond (cond
((plist-get thread :has-child) (funcall get-prefix mu4e-headers-has-child-prefix)) ((plist-get thread :has-child)
((plist-get thread :empty-parent) (funcall get-prefix mu4e-headers-empty-parent-prefix)) (funcall get-prefix mu4e-headers-has-child-prefix))
((plist-get thread :first-child) (funcall get-prefix mu4e-headers-first-child-prefix)) ((plist-get thread :empty-parent)
((plist-get thread :duplicate) (funcall get-prefix mu4e-headers-duplicate-prefix)) (funcall get-prefix mu4e-headers-empty-parent-prefix))
(t (funcall get-prefix mu4e-headers-default-prefix))) ((plist-get thread :first-child)
(funcall get-prefix mu4e-headers-first-child-prefix))
((plist-get thread :duplicate)
(funcall get-prefix mu4e-headers-duplicate-prefix))
(t
(funcall get-prefix mu4e-headers-default-prefix)))
" ")))) " "))))
(defsubst mu4e~headers-flags-str (flags) (defsubst mu4e~headers-flags-str (flags)
@ -306,66 +311,60 @@ display may be different)."
respectively, From: or To:. It's a cons cell with the car element respectively, From: or To:. It's a cons cell with the car element
being the From: prefix, the cdr element the To: prefix.") being the From: prefix, the cdr element the To: prefix.")
(defsubst mu4e~headers-from-or-to (msg)
"When the from address for message MSG matches
`mu4e-user-mail-address-regexp', show the To address; otherwise
show the from address; prefixed with the appropriate
`mu4e-headers-from-or-to-prefix'."
(let ((addr (cdr-safe (car-safe (plist-get msg :from)))))
(if (and addr (string-match mu4e-user-mail-address-regexp addr))
(concat (cdr mu4e-headers-from-or-to-prefix)
(mu4e~headers-contact-str (plist-get msg :to)))
(concat (car mu4e-headers-from-or-to-prefix)
(mu4e~headers-contact-str (plist-get msg :from))))))
;; note: this function is very performance-sensitive
(defun mu4e~headers-header-handler (msg &optional point) (defun mu4e~headers-header-handler (msg &optional point)
"Create a one line description of MSG in this buffer, at POINT, "Create a one line description of MSG in this buffer, at POINT,
if provided, or at the end of the buffer otherwise." if provided, or at the end of the buffer otherwise."
(when (buffer-live-p mu4e~headers-buffer) (let ((docid (plist-get msg :docid)) (line ""))
(let* ((docid (plist-get msg :docid)) (dolist (f-w mu4e-headers-fields)
(line (let ((field (car f-w)) (width (cdr f-w))
(mapconcat (val (plist-get msg (car f-w))) (str))
(lambda (f-w) (setq str
(let* ((field (car f-w)) (width (cdr f-w)) (case field
(val (plist-get msg field)) (:subject
(str (concat ;; prefix subject with a thread indicator
(case field (mu4e~headers-thread-prefix (plist-get msg :thread))
(:subject ;; "["(plist-get (plist-get msg :thread) :path) "] "
(concat ;; prefix subject with a thread indicator val))
(mu4e~headers-thread-prefix (plist-get msg :thread)) ((:maildir :path) val)
;; "["(plist-get (plist-get msg :thread) :path) "] " ((:to :from :cc :bcc) (mu4e~headers-contact-str val))
val)) ;; if we (ie. `user-mail-address' is the 'From', show
((:maildir :path) val) ;; 'To', otherwise show From
((:to :from :cc :bcc) (mu4e~headers-contact-str val)) (:from-or-to (mu4e~headers-from-or-to msg))
;; if we (ie. `user-mail-address' is the 'From', show (:date (format-time-string mu4e-headers-date-format val))
;; 'To', otherwise show From (:flags (propertize (mu4e~headers-flags-str val)
(:from-or-to 'help-echo (format "%S" val)))
(let* ((from-lst (plist-get msg :from)) (:size (mu4e-display-size val))
(from (and from-lst (cdar from-lst)))) (t (mu4e-error "Unsupported header field (%S)" field))))
(if (and from (when str
(string-match mu4e-user-mail-address-regexp (setq line
from)) (concat line
(concat (or (cdr-safe (if (not width)
mu4e-headers-from-or-to-prefix)) str
(mu4e~headers-contact-str (plist-get msg :to))) (truncate-string-to-width str width 0 ?\s t)) " ")))))
(concat (or (car-safe ;; now, propertize it.
mu4e-headers-from-or-to-prefix)) (setq line (propertize line 'face
(mu4e~headers-contact-str from-lst))))) (case (car-safe (plist-get msg :flags))
(:date (format-time-string mu4e-headers-date-format val)) ('draft 'mu4e-draft-face)
(:flags (propertize (mu4e~headers-flags-str val) ('trash 'mu4e-trashed-face)
'help-echo (format "%S" val))) ((unread new) 'mu4e-unread-face)
(:size (mu4e-display-size val)) ('flagged 'mu4e-flagged-face)
(t (mu4e-error "Unsupported header field (%S)" field))))) ((replied passed) 'mu4e-replied-face)
(when str (t 'mu4e-header-face))))
(if (not width)
str
(truncate-string-to-width str width 0 ?\s t)))))
mu4e-headers-fields " "))
(flags (plist-get msg :flags))
(line (cond
((member 'draft flags)
(propertize line 'face 'mu4e-draft-face))
((member 'trashed flags)
(propertize line 'face 'mu4e-trashed-face))
((or (member 'unread flags) (member 'new flags))
(propertize line 'face 'mu4e-unread-face))
((member 'flagged flags)
(propertize line 'face 'mu4e-flagged-face))
((or (member 'replied flags) (member 'passed flags))
(propertize line 'face 'mu4e-replied-face))
(t ;; else
(propertize line 'face 'mu4e-header-face)))))
;; now, append the header line ;; now, append the header line
(mu4e~headers-add-header line docid point msg)))) (mu4e~headers-add-header line docid point msg)))
(defun mu4e~headers-found-handler (count) (defun mu4e~headers-found-handler (count)
"Create a one line description of the number of headers found "Create a one line description of the number of headers found
@ -588,9 +587,7 @@ after the end of the search results."
(and obj (get-text-property 0 'field (car obj))))) (and obj (get-text-property 0 'field (car obj)))))
(if (eq field mu4e-headers-sortfield) (if (eq field mu4e-headers-sortfield)
(setq mu4e-headers-sort-revert (not mu4e-headers-sort-revert)) (setq mu4e-headers-sort-revert (not mu4e-headers-sort-revert))
(setq mu4e-headers-sortfield field)) (setq mu4e-headers-sortfield field)))
;;(message "REFRESH! %S %S" obj field)
)
(mu4e-headers-rerun-search)))) (mu4e-headers-rerun-search))))
(concat (concat
(propertize (propertize
@ -731,10 +728,10 @@ with DOCID which must be present in the headers buffer."
(goto-char oldpoint)))) (goto-char oldpoint))))
(defun mu4e~headers-add-header (str docid point &optional msg) (defsubst mu4e~headers-add-header (str docid point &optional msg)
"Add header STR with DOCID to the buffer at POINT if non-nil, or "Add header STR with DOCID to the buffer at POINT if non-nil, or
at (point-max) otherwise. If MSG is not nil, add it as the text-property `msg'." at (point-max) otherwise. If MSG is not nil, add it as the
(unless docid (mu4e-error "Invalid message")) text-property `msg'."
(when (buffer-live-p mu4e~headers-buffer) (when (buffer-live-p mu4e~headers-buffer)
(with-current-buffer mu4e~headers-buffer (with-current-buffer mu4e~headers-buffer
(let ((inhibit-read-only t) (let ((inhibit-read-only t)

View File

@ -98,39 +98,36 @@ the length (in hex).")
t)) t))
(defsubst mu4e~proc-eat-sexp-from-buf () (defsubst mu4e~proc-eat-sexp-from-buf ()
"'Eat' the next s-expression from `mu4e~proc-buf'. Note: this is a string, "'Eat' the next s-expression from `mu4e~proc-buf'. Note: this is a string,
not an emacs-buffer. `mu4e~proc-buf gets its contents from the not an emacs-buffer. `mu4e~proc-buf gets its contents from the
mu-servers in the following form: mu-servers in the following form:
<`mu4e~cookie-pre'><length-in-hex><`mu4e~cookie-post'> <`mu4e~cookie-pre'><length-in-hex><`mu4e~cookie-post'>
Function returns this sexp, or nil if there was Function returns this sexp, or nil if there was
none. `mu4e~proc-buf' is updated as well, with all processed sexp none. `mu4e~proc-buf' is updated as well, with all processed sexp
data removed." data removed."
(when mu4e~proc-buf ;; mu4e~cookie-matcher-rx:
;; mu4e~cookie-matcher-rx: ;; (concat mu4e~cookie-pre "\\([[:xdigit:]]+\\)]" mu4e~cookie-post)
;; (concat mu4e~cookie-pre "\\([[:xdigit:]]+\\)]" mu4e~cookie-post) (ignore-errors ;; an error would e.g. when proc is killed in the middel
(let* ((b (string-match mu4e~cookie-matcher-rx mu4e~proc-buf)) (let ((b (string-match mu4e~cookie-matcher-rx mu4e~proc-buf)) (sexp-len) (objcons))
(sexp-len (when b
(when b (string-to-number (match-string 1 mu4e~proc-buf) 16)))) (setq sexp-len (string-to-number (match-string 1 mu4e~proc-buf) 16))
;; does mu4e~proc-buf contain the full sexp? ;; does mu4e~proc-buf contain the full sexp?
(when (and b (>= (length mu4e~proc-buf) (+ sexp-len (match-end 0)))) (when (>= (length mu4e~proc-buf) (+ sexp-len (match-end 0)))
;; clear-up start ;; clear-up start
(setq mu4e~proc-buf (substring mu4e~proc-buf (match-end 0))) (setq mu4e~proc-buf (substring mu4e~proc-buf (match-end 0)))
;; note: we read the input in binary mode -- here, we take the part that ;; note: we read the input in binary mode -- here, we take the part that
;; is the sexp, and convert that to utf-8, before we interpret it. ;; is the sexp, and convert that to utf-8, before we interpret it.
(let ((objcons (setq objcons (read-from-string
(ignore-errors ;; note: this may fail if we killed the process (decode-coding-string (substring mu4e~proc-buf 0 sexp-len)
;; in the middle 'utf-8 t)))
(read-from-string
(decode-coding-string (substring mu4e~proc-buf 0 sexp-len)
'utf-8 t)))))
(when objcons (when objcons
(setq mu4e~proc-buf (substring mu4e~proc-buf sexp-len)) (setq mu4e~proc-buf (substring mu4e~proc-buf sexp-len))
(car objcons))))))) (car objcons)))))))
(defun mu4e~proc-filter (proc str) (defsubst mu4e~proc-filter (proc str)
"A process-filter for the 'mu server' output; it accumulates the "A process-filter for the 'mu server' output; it accumulates the
strings into valid sexps by checking of the ';;eox' end-of-sexp strings into valid sexps by checking of the ';;eox' end-of-sexp
marker, and then evaluating them. marker, and then evaluating them.
@ -290,7 +287,7 @@ terminates."
(t (t
(error "Something bad happened to the mu server process"))))) (error "Something bad happened to the mu server process")))))
(defun mu4e~proc-send-command (frm &rest args) (defsubst mu4e~proc-send-command (frm &rest args)
"Send as command to the mu server process; start the process if needed." "Send as command to the mu server process; start the process if needed."
(unless (mu4e~proc-is-running) (unless (mu4e~proc-is-running)
(mu4e~proc-start)) (mu4e~proc-start))
@ -298,8 +295,7 @@ terminates."
(mu4e-log 'to-server "%s" cmd) (mu4e-log 'to-server "%s" cmd)
(process-send-string mu4e~proc-process (concat cmd "\n")))) (process-send-string mu4e~proc-process (concat cmd "\n"))))
(defsubst mu4e--docid-msgid-param (docid-or-msgid)
(defun mu4e--docid-msgid-param (docid-or-msgid)
"Construct a backend parameter based on DOCID-OR-MSGID." "Construct a backend parameter based on DOCID-OR-MSGID."
(format (format
(if (stringp docid-or-msgid) (if (stringp docid-or-msgid)