* 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,17 +311,27 @@ 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))
(val (plist-get msg field))
(str
(case field (case field
(:subject (:subject
(concat ;; prefix subject with a thread indicator (concat ;; prefix subject with a thread indicator
@ -327,45 +342,29 @@ if provided, or at the end of the buffer otherwise."
((:to :from :cc :bcc) (mu4e~headers-contact-str val)) ((:to :from :cc :bcc) (mu4e~headers-contact-str val))
;; if we (ie. `user-mail-address' is the 'From', show ;; if we (ie. `user-mail-address' is the 'From', show
;; 'To', otherwise show From ;; 'To', otherwise show From
(:from-or-to (:from-or-to (mu4e~headers-from-or-to msg))
(let* ((from-lst (plist-get msg :from))
(from (and from-lst (cdar from-lst))))
(if (and from
(string-match mu4e-user-mail-address-regexp
from))
(concat (or (cdr-safe
mu4e-headers-from-or-to-prefix))
(mu4e~headers-contact-str (plist-get msg :to)))
(concat (or (car-safe
mu4e-headers-from-or-to-prefix))
(mu4e~headers-contact-str from-lst)))))
(:date (format-time-string mu4e-headers-date-format val)) (:date (format-time-string mu4e-headers-date-format val))
(:flags (propertize (mu4e~headers-flags-str val) (:flags (propertize (mu4e~headers-flags-str val)
'help-echo (format "%S" val))) 'help-echo (format "%S" val)))
(:size (mu4e-display-size val)) (:size (mu4e-display-size val))
(t (mu4e-error "Unsupported header field (%S)" field))))) (t (mu4e-error "Unsupported header field (%S)" field))))
(when str (when str
(setq line
(concat line
(if (not width) (if (not width)
str str
(truncate-string-to-width str width 0 ?\s t))))) (truncate-string-to-width str width 0 ?\s t)) " ")))))
mu4e-headers-fields " ")) ;; now, propertize it.
(flags (plist-get msg :flags)) (setq line (propertize line 'face
(line (cond (case (car-safe (plist-get msg :flags))
((member 'draft flags) ('draft 'mu4e-draft-face)
(propertize line 'face 'mu4e-draft-face)) ('trash 'mu4e-trashed-face)
((member 'trashed flags) ((unread new) 'mu4e-unread-face)
(propertize line 'face 'mu4e-trashed-face)) ('flagged 'mu4e-flagged-face)
((or (member 'unread flags) (member 'new flags)) ((replied passed) 'mu4e-replied-face)
(propertize line 'face 'mu4e-unread-face)) (t 'mu4e-header-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)
(let* ((b (string-match mu4e~cookie-matcher-rx mu4e~proc-buf)) (ignore-errors ;; an error would e.g. when proc is killed in the middel
(sexp-len (let ((b (string-match mu4e~cookie-matcher-rx mu4e~proc-buf)) (sexp-len) (objcons))
(when b (string-to-number (match-string 1 mu4e~proc-buf) 16)))) (when b
(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
;; in the middle
(read-from-string
(decode-coding-string (substring mu4e~proc-buf 0 sexp-len) (decode-coding-string (substring mu4e~proc-buf 0 sexp-len)
'utf-8 t))))) '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)