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 11BFC6DE1403 for ; Tue, 8 Mar 2016 09:13:10 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: 0.373 X-Spam-Level: X-Spam-Status: No, score=0.373 tagged_above=-999 required=5 tests=[AWL=0.440, 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 0jVazW2UDITF for ; Tue, 8 Mar 2016 09:13:08 -0800 (PST) Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by arlo.cworth.org (Postfix) with ESMTPS id 8B77B6DE01FF for ; Tue, 8 Mar 2016 09:13:07 -0800 (PST) Received: by mail-wm0-f67.google.com with SMTP id l68so5321441wml.3 for ; Tue, 08 Mar 2016 09:13:07 -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=BPxiJfT4/8TedppqeXT/kyUUP2eBHpNERC4OTtJICHA=; b=uufg7cdcmVl5cTFsLqS+wntF8Pc5YKikkCCd5MUAYCiqETTi/7aXwRmaasvCFYAfTu jHlu67vmTz0r/KFvhspDrmN+CLwkKIjfsVz2HL1NoSw7ddxuDWYWy1zxC4mZIrDJ5EPo 19jZ1y+wSRNwSly2dvaBzPI2MVfBejDxGKQZM65iyI7cTdpSHS8fDnhxXMiJfHGgCgpU frUtNXaOAqIZJUlmLMH96fiGKNB+aycsyiNESCu0yuDzQdqdo2li1cpyEDl5fez+Ewr4 t3Y2g+JEyv6kFoibwSBDl0YVMuIMgllw4WYC9OUvCjye6z+7Vdivm9/4omF7B7V28Vsx zirg== 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=BPxiJfT4/8TedppqeXT/kyUUP2eBHpNERC4OTtJICHA=; b=EBApaRUnQQE/+qMmmyZHe8xrpS6AL0bH/2M5hITxJVjNv4XKzvUeJmDUtexyi3fUfI xCX4co4Lm6Za5zc+ralRFU6IPJyNoEHD37n6SZfGCjtdOHeLPDDJ4ltF9woL1vsyj7Vy 4YFbe/ee+lFyAb7hjjW/FpLFPus/FZ3QgI20QFMRv2lQ+/bIJwu5aEcA04aDxFd+UxEW vvVgSEMHUNGWuHolQqLmj0f2/P3SueNxKOwIa6PMFnFBNaEtS8jYG4e6zybf0uUlbG4T L8iCMyrDSLshq30miPVRECvmOg2wEY38b5a3iog6eQyK7ANFMbJsxAig5gMfHwIFmhxK hccA== X-Gm-Message-State: AD7BkJL+lHB4IhEjV9HQ4GfeEER7KJffw4GKHREti3yUEykzwHX0W8UhFg69bnEKUWCaLA== X-Received: by 10.28.9.71 with SMTP id 68mr20248218wmj.33.1457457186291; Tue, 08 Mar 2016 09:13:06 -0800 (PST) Received: from disaster-area.hh.sledj.net (disaster-area.hh.sledj.net. [81.149.164.25]) by smtp.gmail.com with ESMTPSA id l135sm4369544wmb.13.2016.03.08.09.13.05 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Mar 2016 09:13:05 -0800 (PST) Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id 89ea9002 for ; Tue, 8 Mar 2016 17:12:59 +0000 (UTC) From: David Edmondson To: notmuch@notmuchmail.org Subject: [PATCH v1 3/3] emacs: Improve the acquisition of text parts. Date: Tue, 8 Mar 2016 17:12:59 +0000 Message-Id: <1457457179-4707-4-git-send-email-dme@dme.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1457457179-4707-1-git-send-email-dme@dme.org> References: <1457457179-4707-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: Tue, 08 Mar 2016 17:13:10 -0000 `notmuch-get-bodypart-text' assumed that it is always possible to acquire text/* parts via the sexp output format. This is not true if the part in question has a content type of application/octet-stream but is being interpreted as text/* based on the extension of the part filename. Rework `notmuch-get-bodypart-text' to use the raw output format to address this and make the implementation common with that of `notmuch-get-bodypart-binary'. --- emacs/notmuch-lib.el | 73 ++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el index 89c01a5..75a3706 100644 --- a/emacs/notmuch-lib.el +++ b/emacs/notmuch-lib.el @@ -537,6 +537,34 @@ the given type." (lambda (part) (notmuch-match-content-type (plist-get part :content-type) type)) parts)) +(defun notmuch--get-bodypart-raw (msg part process-crypto binaryp cache) + (let* ((plist-elem (if binaryp :content-binary :content)) + (data (or (plist-get part plist-elem) + (with-temp-buffer + ;; Emacs internally uses a UTF-8-like multibyte string + ;; representation by default (regardless of the coding + ;; system, which only affects how it goes from outside data + ;; to this internal representation). This *almost* never + ;; matters. Annoyingly, it does matter if we use this data + ;; in an image descriptor, since Emacs will use its internal + ;; data buffer directly and this multibyte representation + ;; corrupts binary image formats. Since the caller is + ;; asking for binary data, a unibyte string is a more + ;; appropriate representation anyway. + (when binaryp + (set-buffer-multibyte nil)) + (let ((args `("show" "--format=raw" + ,(format "--part=%s" (plist-get part :id)) + ,@(when process-crypto '("--decrypt")) + ,(notmuch-id-to-query (plist-get msg :id)))) + (coding-system-for-read + (if binaryp 'no-conversion 'utf-8))) + (apply #'call-process notmuch-command nil '(t nil) nil args) + (buffer-string)))))) + (when (and cache data) + (plist-put part plist-elem data)) + data)) + (defun notmuch-get-bodypart-binary (msg part process-crypto &optional cache) "Return the unprocessed content of PART in MSG as a unibyte string. @@ -547,57 +575,18 @@ this does no charset conversion. If CACHE is non-nil, the content of this part will be saved in MSG (if it isn't already)." - (let ((data (plist-get part :binary-content))) - (when (not data) - (let ((args `("show" "--format=raw" - ,(format "--part=%d" (plist-get part :id)) - ,@(when process-crypto '("--decrypt")) - ,(notmuch-id-to-query (plist-get msg :id))))) - (with-temp-buffer - ;; Emacs internally uses a UTF-8-like multibyte string - ;; representation by default (regardless of the coding - ;; system, which only affects how it goes from outside data - ;; to this internal representation). This *almost* never - ;; matters. Annoyingly, it does matter if we use this data - ;; in an image descriptor, since Emacs will use its internal - ;; data buffer directly and this multibyte representation - ;; corrupts binary image formats. Since the caller is - ;; asking for binary data, a unibyte string is a more - ;; appropriate representation anyway. - (set-buffer-multibyte nil) - (let ((coding-system-for-read 'no-conversion)) - (apply #'call-process notmuch-command nil '(t nil) nil args) - (setq data (buffer-string))))) - (when cache - ;; Cheat. part is non-nil, and `plist-put' always modifies - ;; the list in place if it's non-nil. - (plist-put part :binary-content data))) - data)) + (notmuch--get-bodypart-raw msg part process-crypto t cache)) (defun notmuch-get-bodypart-text (msg part process-crypto &optional cache) "Return the text content of PART in MSG. This returns the content of the given part as a multibyte Lisp string after performing content transfer decoding and any -necessary charset decoding. It is an error to use this for -non-text/* parts. +necessary charset decoding. If CACHE is non-nil, the content of this part will be saved in MSG (if it isn't already)." - (let ((content (plist-get part :content))) - (when (not content) - ;; Use show --format=sexp to fetch decoded content - (let* ((args `("show" "--format=sexp" "--include-html" - ,(format "--part=%s" (plist-get part :id)) - ,@(when process-crypto '("--decrypt")) - ,(notmuch-id-to-query (plist-get msg :id)))) - (npart (apply #'notmuch-call-notmuch-sexp args))) - (setq content (plist-get npart :content)) - (when (not content) - (error "Internal error: No :content from %S" args))) - (when cache - (plist-put part :content content))) - content)) + (notmuch--get-bodypart-raw msg part process-crypto nil cache)) ;; Workaround: The call to `mm-display-part' below triggers a bug in ;; Emacs 24 if it attempts to use the shr renderer to display an HTML -- 2.1.4