Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 1CA616DE00DD for ; Fri, 15 Jan 2016 05:35:06 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: 0.471 X-Spam-Level: X-Spam-Status: No, score=0.471 tagged_above=-999 required=5 tests=[AWL=0.538, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_NEUTRAL=0.652, UNPARSEABLE_RELAY=0.001] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 02VRpVpTmo2n for ; Fri, 15 Jan 2016 05:35:04 -0800 (PST) Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by arlo.cworth.org (Postfix) with ESMTPS id 0BDFA6DE1607 for ; Fri, 15 Jan 2016 05:35:04 -0800 (PST) Received: by mail-wm0-f66.google.com with SMTP id u188so2889276wmu.0 for ; Fri, 15 Jan 2016 05:35:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dme-org.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=rg9a1wZyTmC3l9iFL/5Mls/NcpgAG88Xg0d0XF9v3eY=; b=JP3TCF9y5tsMw/yFhOWkiynbUA2xaQwtNSoGw8T8u1HB1NnuCBwpKLaceVTfpjpDl3 kxrnmH1F+dbbGkV4HaKCZeeynJOjKdgeyBZgMEQTuIbU4teU/thUNseJr3FgxpNtq5kc mydkz0jljEwzFVG8aS/lkNpvAr0kH8RNVH4hGxUwl4MJRhsqGZ/8/EC6yBR0KzJVtLbz 8J0CarGB81w4aKY+KE/v9D0iA1LrtH9JGsckRX+l3lq6ie60SVTcWbw2JbqECDwnLZCp 6mYf6KhaZIahQ3KNg6MdwmmY90wlCd7LDRetLTuQbMvJNpcK3kVIg2pcbqnZhF6vx0/h /weA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=rg9a1wZyTmC3l9iFL/5Mls/NcpgAG88Xg0d0XF9v3eY=; b=YWgHxAfN6AF6W1pEpSWNTPFoyg6KOUybtBP9mXym4DVFKjCt4NrlvbfkER1cffR3b4 US4D7Vibp+vHlEH0JBJXxDJgNlNsIPv6EyhAfLl4XL11Bi3eXf41KOtK/Yfc/T9cQdex FVoJokc2V+4fCjBH79+h6OkaMhYGMnYJ7cf9JHq5l7d5qvJmr/Vd/cd0eV8TftXgD39u N4Ucpuam9Hes4IeM9S8DX7zjc/89CiEzUm/NRDxgZ+T5z5JWpBXPUl7QjMRjP3v0C7ah /sfvu6RNy8VFELCDSMSW0F9DE1kd9ugEds1v1HZ+jct4WVuqKB5Za2PWr9HxyZhp9lbN 8FKA== X-Gm-Message-State: ALoCoQmENMQUULb36dXpULT5t47eMSfoF5D7zpJ4NGr66raIdVC0JBGkX2u6+8JuDitPWQ/1xQ+brVtg69VwNwm9CLEEGYiUAQ== X-Received: by 10.194.114.106 with SMTP id jf10mr12021610wjb.149.1452864902738; Fri, 15 Jan 2016 05:35:02 -0800 (PST) Received: from disaster-area.hh.sledj.net ([2a01:348:1a2:1:ea39:35ff:fe2c:a227]) by smtp.gmail.com with ESMTPSA id id1sm10648439wjb.19.2016.01.15.05.35.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jan 2016 05:35:01 -0800 (PST) Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id f596f93b for ; Fri, 15 Jan 2016 13:34:58 +0000 (UTC) From: David Edmondson To: notmuch@notmuchmail.org Subject: [PATCH v1] emacs: Allow part preferences to depend on message content. Date: Fri, 15 Jan 2016 13:34:58 +0000 Message-Id: <1452864898-9719-2-git-send-email-dme@dme.org> X-Mailer: git-send-email 2.6.3 In-Reply-To: <1452864898-9719-1-git-send-email-dme@dme.org> References: <1452864898-9719-1-git-send-email-dme@dme.org> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jan 2016 13:35:06 -0000 Currently the preference for which sub-part of a multipart/alternative part is shown is global. Allow to the user to override the settings on a per-message basis by providing two new options: - the ability to call a function that has access to the message to return the discouraged type list, - a simple evaluation environment to reduce the need for most users to write their own function. The original approach is retained as the default. --- emacs/notmuch-lib.el | 39 +++++++++++++++++++++++++++++++++++---- emacs/notmuch-mua.el | 2 +- emacs/notmuch-show.el | 2 +- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el index 89c01a5..2b9d108 100644 --- a/emacs/notmuch-lib.el +++ b/emacs/notmuch-lib.el @@ -520,11 +520,42 @@ This replaces spaces, percents, and double quotes in STR with "multipart/related" )) -(defun notmuch-multipart/alternative-choose (types) - "Return a list of preferred types from the given list of types" +(defun notmuch-multipart/alternative-determine-discouraged-1 (msg directive) + (let* ((headers (plist-get msg :headers)) + ;; Headers that we make available: + (from (plist-get headers :From)) + (subject (plist-get headers :Subject)) + (to (plist-get headers :To)) + (cc (plist-get headers :Cc))) + + (eval directive))) + +(defun notmuch-multipart/alternative-determine-discouraged (msg) + "Return the discouraged alternatives for the specified message." + (cond + ;; If a function, return the result of calling it. + ((functionp notmuch-multipart/alternative-discouraged) + (funcall notmuch-multipart/alternative-discouraged msg)) + + ;; If the first element is a string, return the list. This matches + ;; with the default setting of + ;; `notmuch-multipart/alternative-discouraged'. + ((and (listp notmuch-multipart/alternative-discouraged) + (stringp (car notmuch-multipart/alternative-discouraged))) + notmuch-multipart/alternative-discouraged) + + ;; New style pattern matcher. + (t + (notmuch-multipart/alternative-determine-discouraged-1 + msg notmuch-multipart/alternative-discouraged)))) + +(defun notmuch-multipart/alternative-choose (msg types) + "Return a list of preferred types from the given list of types +for this message, if present." ;; Based on `mm-preferred-alternative-precedence'. - (let ((seq types)) - (dolist (pref (reverse notmuch-multipart/alternative-discouraged)) + (let ((discouraged (notmuch-multipart/alternative-determine-discouraged msg)) + (seq types)) + (dolist (pref (reverse discouraged)) (dolist (elem (copy-sequence seq)) (when (string-match pref elem) (setq seq (nconc (delete elem seq) (list elem)))))) diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el index 5462f54..8244258 100644 --- a/emacs/notmuch-mua.el +++ b/emacs/notmuch-mua.el @@ -147,7 +147,7 @@ Note that these functions use `mail-citation-hook' if that is non-nil." if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative") collect (let* ((subparts (plist-get part :content)) (types (mapcar (lambda (part) (plist-get part :content-type)) subparts)) - (chosen-type (car (notmuch-multipart/alternative-choose types)))) + (chosen-type (car (notmuch-multipart/alternative-choose nil types)))) (loop for part in (reverse subparts) if (notmuch-match-content-type (plist-get part :content-type) chosen-type) return part)) diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el index 3345878..2ec30a8 100644 --- a/emacs/notmuch-show.el +++ b/emacs/notmuch-show.el @@ -612,7 +612,7 @@ will return nil if the CID is unknown or cannot be retrieved." (plist-get part :content))) (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth button) - (let ((chosen-type (car (notmuch-multipart/alternative-choose (notmuch-show-multipart/*-to-list part)))) + (let ((chosen-type (car (notmuch-multipart/alternative-choose msg (notmuch-show-multipart/*-to-list part)))) (inner-parts (plist-get part :content)) (start (point))) ;; This inserts all parts of the chosen type rather than just one, -- 2.6.3