[RFC][PATCH v2] emacs: Replace `notmuch-search-result-format' evaluation.
authorDavid Edmondson <dme@dme.org>
Tue, 25 Nov 2014 09:06:56 +0000 (09:06 +0000)
committerW. Trevor King <wking@tremily.us>
Sat, 20 Aug 2016 21:46:50 +0000 (14:46 -0700)
4f/f5cbf90ed6e5b0eae8e6b8623d7f9bd2bdb05c [new file with mode: 0644]

diff --git a/4f/f5cbf90ed6e5b0eae8e6b8623d7f9bd2bdb05c b/4f/f5cbf90ed6e5b0eae8e6b8623d7f9bd2bdb05c
new file mode 100644 (file)
index 0000000..43c7c33
--- /dev/null
@@ -0,0 +1,407 @@
+Return-Path: <dme@dme.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id D7A20431FC3\r
+       for <notmuch@notmuchmail.org>; Tue, 25 Nov 2014 01:07:07 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.699\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.699 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7, UNPARSEABLE_RELAY=0.001]\r
+       autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id Pv2Yf5HeDRTb for <notmuch@notmuchmail.org>;\r
+       Tue, 25 Nov 2014 01:07:00 -0800 (PST)\r
+Received: from mail-wg0-f50.google.com (mail-wg0-f50.google.com\r
+ [74.125.82.50])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 1316F431FAF   for <notmuch@notmuchmail.org>; Tue, 25 Nov 2014 01:07:00 -0800\r
+ (PST)\r
+Received: by mail-wg0-f50.google.com with SMTP id k14so228219wgh.23\r
+       for <notmuch@notmuchmail.org>; Tue, 25 Nov 2014 01:06:59 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=1e100.net; s=20130820;\r
+       h=x-gm-message-state:user-agent:from:to:subject:date:message-id;\r
+       bh=qNqAimSRgAqUvWo9JF+eLK4arCODnursqIvPaWAs0Rc=;\r
+       b=f1aP94TylBERCxZo3X69WSnqxT+he1v69y9lJtjXKSrcNawt0cSltkdV6x9xHZTsYb\r
+       qkeGq0UAYSqhgQTGUCXIC0Z1z6xQCWHZ/72yGuFwl/Dg+aI49m6YhLaM+7HqVeJqzDaw\r
+       D5XF+BVpamaZvFDndBmr3w4dvHehAbHfCo3Nc5HpV30rzCP/7jRlSHolqwu3vw3nK8pi\r
+       Lh/yKjbcaqwOoi+OqO2KzYx0pIYs+F1xc11ZR2BwBwZZi3vZD1mkcYwPn3broEfTHRv0\r
+       qF/468AbothFn5PO+BGi/OxN1tR3w9npVF205H/uGxXXYqGnlx3bz8qzbbeRVGDhUGsT\r
+       7/Yw==\r
+X-Gm-Message-State:\r
+ ALoCoQlPWy1vcm8NEQ9LcepslS0y/vlKKaGrBWsVbKwNHOt9IYSkAZO2bNXlXKC9HS9Pj6pZAXpb\r
+X-Received: by 10.180.92.129 with SMTP id cm1mr6687547wib.33.1416906418764;\r
+       Tue, 25 Nov 2014 01:06:58 -0800 (PST)\r
+Received: from disaster-area.hh.sledj.net\r
+       ([2a01:348:1a2:1:ea39:35ff:fe2c:a227])\r
+       by mx.google.com with ESMTPSA id s9sm1913937wiz.12.2014.11.25.01.06.57\r
+       for <notmuch@notmuchmail.org>\r
+       (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+       Tue, 25 Nov 2014 01:06:58 -0800 (PST)\r
+Received: from localhost (30000@localhost [local]);\r
+       by localhost (OpenSMTPD) with ESMTPA id af847ce3;\r
+       for <notmuch@notmuchmail.org>; Tue, 25 Nov 2014 09:06:56 +0000 (UTC)\r
+User-Agent: OpenSMTPD enqueuer (Demoostik)\r
+From: David Edmondson <dme@dme.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC][PATCH v2] emacs: Replace `notmuch-search-result-format'\r
+       evaluation.\r
+Date: Tue, 25 Nov 2014 09:06:56 +0000\r
+Message-Id: <1416906416-3952-1-git-send-email-dme@dme.org>\r
+X-Mailer: git-send-email 2.1.3\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 25 Nov 2014 09:07:08 -0000\r
+\r
+Alternative implementation of code that interprets\r
+`notmuch-search-result-format' to insert the results of a search.\r
+---\r
+\r
+Added some more documentation, as per bremner.\r
+\r
+ emacs/notmuch.el | 300 +++++++++++++++++++++++++++++++++----------------------\r
+ 1 file changed, 178 insertions(+), 122 deletions(-)\r
+\r
+diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+index 218486a..eb79a29 100644\r
+--- a/emacs/notmuch.el\r
++++ b/emacs/notmuch.el\r
+@@ -66,23 +66,103 @@\r
+     "Placeholder variable when notmuch-version.el[c] is not available."))\r
\r
+ (defcustom notmuch-search-result-format\r
+-  `(("date" . "%12s ")\r
+-    ("count" . "%-7s ")\r
+-    ("authors" . "%-20s ")\r
+-    ("subject" . "%s ")\r
+-    ("tags" . "(%s)"))\r
+-  "Search result formatting. Supported fields are:\r
+-      date, count, authors, subject, tags\r
+-For example:\r
+-      (setq notmuch-search-result-format \(\(\"authors\" . \"%-40s\"\)\r
+-                                           \(\"subject\" . \"%s\"\)\)\)\r
+-Line breaks are permitted in format strings (though this is\r
+-currently experimental).  Note that a line break at the end of an\r
+-\"authors\" field will get elided if the authors list is long;\r
+-place it instead at the beginning of the following field.  To\r
+-enter a line break when setting this variable with setq, use \\n.\r
+-To enter a line break in customize, press \\[quoted-insert] C-j."\r
+-  :type '(alist :key-type (string) :value-type (string))\r
++  '((:width -12 :date_relative) " "\r
++    (:width -7 (:concat "[" :matched "/" :total "]")) " "\r
++    ;; This splitting will not be necessary once\r
++    ;; id:1414172643-28270-1-git-send-email-dme@dme.org is integrated.\r
++    (:width 40 (:eval (let* ((split-authors (split-string\r
++                                           (plist-get notmuch-thread :authors)\r
++                                           "|" t split-string-default-separators))\r
++                           ;; There will always be matched authors.\r
++                           (matched-authors (car split-authors))\r
++                           ;; There may not be non-matched authors.\r
++                           (non-matched-authors (mapconcat #'identity (cdr split-authors) " ")))\r
++                      (notmuch-search-format-authors matched-authors non-matched-authors)))) " "\r
++    :subject " "\r
++    "(" (:eval (notmuch-tag-format-tags (plist-get notmuch-thread :tags)\r
++                                      (plist-get notmuch-thread :orig-tags)))\r
++    ")" "\n")\r
++\r
++  "Template for displaying search results.\r
++\r
++The value is a list of items to insert in the search\r
++results. Individual items should be in one of the following\r
++forms:\r
++\r
++A string that is inserted directly (e.g. \" \").\r
++\r
++A number that is inserted directly (e.g. 5).\r
++\r
++A list that is recursively evaluated (e.g. `(:concat FORM1\r
++ FORM2)').\r
++\r
++A function that is evaluated with a single argument, the current\r
++ thread.\r
++\r
++A symbol corresponding to an attribute of the\r
++ thread. Currently available attributes include:\r
++\r
++ :date_relative -- a user-readable rendering of the Date: header\r
++ of the first matching message in the thread, as a string.\r
++\r
++ :timestamp -- the number of seconds since the Epoch, 1970-01-01\r
++ 00:00:00 +0000 (UTC), corresponding to the Date: header of the\r
++ first matching message in the thread.\r
++\r
++ :subject -- the subject of the first matching message in the\r
++ thread as a string.\r
++\r
++ :authors -- a comma separated string containing a list of the\r
++ authors of messages in the thread. If there are non-matching\r
++ authors (i.e. the thread contains messages which did not match\r
++ the search terms and those messages have authors who are not\r
++ also authors of messages that did match the search terms) then\r
++ the matching and non-matching authors are separated by a `|'\r
++ symbol in the results in place of a comma.\r
++\r
++ :matched -- the number of messages that matched search terms in\r
++ the thread (a number).\r
++\r
++ :total -- the total number of messages in the thread (i.e. the\r
++ count of both matching and non-matching messages).\r
++\r
++ :tags -- a list of tags associated with messages in the\r
++ thread. Each tag is included as a distinct string.\r
++\r
++ :orig-tags -- a list of tags that were associated with messages\r
++ in the thread when the search originally took place.\r
++\r
++ :query -- a list with two elements. The first element is a query\r
++ string that will return all of the matching messages in the\r
++ current thread. The second element is a query string that will\r
++ return all of the non-matching messages in the current thread.\r
++\r
++ :thread -- the thread id of the matching thread, as a string.\r
++\r
++ For a full list of the attributes available, see the source code\r
++ for notmuch itself.\r
++\r
++A directive that describes how to evaluate the remainder of the\r
++ list. The following directives are available:\r
++\r
++ `:concat FORM...' -- evaluate each element of FORM and return\r
++ the concatenated results.\r
++\r
++ `:width WIDTH FORM' -- evaluate FORM and return the result\r
++ truncated to WIDTH characters. The result will be padded with\r
++ spaces to WIDTH characters, with padding at on the left if the\r
++ WIDTH is negative.\r
++\r
++ `:tag TAG FORM' -- if the thread has the tag TAG, insert the\r
++ result of evaluating FORM.\r
++\r
++ `:eval COMPLEX-FORM' -- evaluate COMPLEX-FORM as emacs lisp and\r
++ evaluate the result.\r
++\r
++During the evaluation of `notmuch-search-result-format', the\r
++symbol `notmuch-thread' is bound to the current thread (a\r
++property list), which can then be used in `:eval' forms."\r
++  :type 'list\r
+   :group 'notmuch-search)\r
\r
+ ;; The name of this variable `notmuch-init-file' is consistent with the\r
+@@ -672,109 +752,84 @@ foreground and blue background."\r
+       ;; Reverse the list so earlier entries take precedence\r
+       (reverse notmuch-search-line-faces)))\r
\r
+-(defun notmuch-search-author-propertize (authors)\r
+-  "Split `authors' into matching and non-matching authors and\r
+-propertize appropriately. If no boundary between authors and\r
+-non-authors is found, assume that all of the authors match."\r
+-  (if (string-match "\\(.*\\)|\\(.*\\)" authors)\r
+-      (concat (propertize (concat (match-string 1 authors) ",")\r
+-                        'face 'notmuch-search-matching-authors)\r
+-            (propertize (match-string 2 authors)\r
+-                        'face 'notmuch-search-non-matching-authors))\r
+-    (propertize authors 'face 'notmuch-search-matching-authors)))\r
+-\r
+-(defun notmuch-search-insert-authors (format-string authors)\r
+-  ;; Save the match data to avoid interfering with\r
+-  ;; `notmuch-search-process-filter'.\r
+-  (save-match-data\r
+-    (let* ((formatted-authors (format format-string authors))\r
+-         (formatted-sample (format format-string ""))\r
+-         (visible-string formatted-authors)\r
+-         (invisible-string "")\r
+-         (padding ""))\r
+-\r
+-      ;; Truncate the author string to fit the specification.\r
+-      (if (> (length formatted-authors)\r
+-           (length formatted-sample))\r
+-        (let ((visible-length (- (length formatted-sample)\r
+-                                 (length "... "))))\r
+-          ;; Truncate the visible string according to the width of\r
+-          ;; the display string.\r
+-          (setq visible-string (substring formatted-authors 0 visible-length)\r
+-                invisible-string (substring formatted-authors visible-length))\r
+-          ;; If possible, truncate the visible string at a natural\r
+-          ;; break (comma or pipe), as incremental search doesn't\r
+-          ;; match across the visible/invisible border.\r
+-          (when (string-match "\\(.*\\)\\([,|] \\)\\([^,|]*\\)" visible-string)\r
+-            ;; Second clause is destructive on `visible-string', so\r
+-            ;; order is important.\r
+-            (setq invisible-string (concat (match-string 3 visible-string)\r
+-                                           invisible-string)\r
+-                  visible-string (concat (match-string 1 visible-string)\r
+-                                         (match-string 2 visible-string))))\r
+-          ;; `visible-string' may be shorter than the space allowed\r
+-          ;; by `format-string'. If so we must insert some padding\r
+-          ;; after `invisible-string'.\r
+-          (setq padding (make-string (- (length formatted-sample)\r
+-                                        (length visible-string)\r
+-                                        (length "..."))\r
+-                                     ? ))))\r
+-\r
+-      ;; Use different faces to show matching and non-matching authors.\r
+-      (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)\r
+-        ;; The visible string contains both matching and\r
+-        ;; non-matching authors.\r
+-        (setq visible-string (notmuch-search-author-propertize visible-string)\r
+-              ;; The invisible string must contain only non-matching\r
+-              ;; authors, as the visible-string contains both.\r
+-              invisible-string (propertize invisible-string\r
+-                                           'face 'notmuch-search-non-matching-authors))\r
+-      ;; The visible string contains only matching authors.\r
+-      (setq visible-string (propertize visible-string\r
+-                                       'face 'notmuch-search-matching-authors)\r
+-            ;; The invisible string may contain both matching and\r
+-            ;; non-matching authors.\r
+-            invisible-string (notmuch-search-author-propertize invisible-string)))\r
+-\r
+-      ;; If there is any invisible text, add it as a tooltip to the\r
+-      ;; visible text.\r
+-      (when (not (string= invisible-string ""))\r
+-      (setq visible-string (propertize visible-string 'help-echo (concat "..." invisible-string))))\r
+-\r
+-      ;; Insert the visible and, if present, invisible author strings.\r
+-      (insert visible-string)\r
+-      (when (not (string= invisible-string ""))\r
+-      (let ((start (point))\r
+-            overlay)\r
+-        (insert invisible-string)\r
+-        (setq overlay (make-overlay start (point)))\r
+-        (overlay-put overlay 'invisible 'ellipsis)\r
+-        (overlay-put overlay 'isearch-open-invisible #'delete-overlay)))\r
+-      (insert padding))))\r
+-\r
+-(defun notmuch-search-insert-field (field format-string result)\r
++(defun notmuch-search-format-authors (matched non-matched)\r
++  (if (string-equal "" non-matched)\r
++      (propertize matched 'face 'notmuch-search-matching-authors)\r
++    (concat (propertize (concat matched ", ")\r
++                      'face 'notmuch-search-matching-authors)\r
++          (propertize non-matched\r
++                      'face 'notmuch-search-non-matching-authors))))\r
++\r
++(defun notmuch-search-make-width (width string)\r
++  (let ((neg (< 0 width))\r
++      (width (abs width))\r
++      (orig-len (length string)))\r
++\r
++    (if (< width orig-len)\r
++      ;; A sub-set of the string will be visible.\r
++      (let* ((ellipsis "...")\r
++             (visible-len (- width (length ellipsis)))\r
++             (visible-string (substring string 0 visible-len))\r
++             (invisible-string (substring string visible-len)))\r
++\r
++        (concat (propertize (concat visible-string\r
++                                    ;; The ellipsis should share the\r
++                                    ;; face of the preceding\r
++                                    ;; character.\r
++                                    (propertize ellipsis 'face\r
++                                                (get-text-property visible-len 'face string)))\r
++                            'help-echo (concat ellipsis invisible-string))))\r
++\r
++      ;; All of the string is visible - pad it.\r
++      (concat (if neg string)\r
++            (make-string (- width orig-len) ? )\r
++            (if neg "" string)))))\r
++\r
++(defun notmuch-search-elem-repr (elem thread)\r
+   (cond\r
+-   ((string-equal field "date")\r
+-    (insert (propertize (format format-string (plist-get result :date_relative))\r
+-                      'face 'notmuch-search-date)))\r
+-   ((string-equal field "count")\r
+-    (insert (propertize (format format-string\r
+-                              (format "[%s/%s]" (plist-get result :matched)\r
+-                                      (plist-get result :total)))\r
+-                      'face 'notmuch-search-count)))\r
+-   ((string-equal field "subject")\r
+-    (insert (propertize (format format-string\r
+-                              (notmuch-sanitize (plist-get result :subject)))\r
+-                      'face 'notmuch-search-subject)))\r
+-\r
+-   ((string-equal field "authors")\r
+-    (notmuch-search-insert-authors\r
+-     format-string (notmuch-sanitize (plist-get result :authors))))\r
+-\r
+-   ((string-equal field "tags")\r
+-    (let ((tags (plist-get result :tags))\r
+-        (orig-tags (plist-get result :orig-tags)))\r
+-      (insert (format format-string (notmuch-tag-format-tags tags orig-tags)))))))\r
++   ((not elem)\r
++    "")\r
++   \r
++   ((numberp elem)\r
++    (format "%d" elem))\r
++\r
++   ((stringp elem)\r
++    elem)\r
++\r
++   ((functionp elem)\r
++    (funcall elem thread))\r
++\r
++   ((listp elem)\r
++    (let ((op (car elem))\r
++        (rest (cdr elem)))\r
++      (case op\r
++      (:concat\r
++       (mapconcat (lambda (inner-elem)\r
++                    (notmuch-search-elem-repr inner-elem thread))\r
++                  rest ""))\r
++\r
++      (:width\r
++       (notmuch-search-make-width (car rest)\r
++                                  (notmuch-search-elem-repr (cadr rest) thread)))\r
++\r
++      (:tag\r
++       (when (member (car rest) (plist-get thread :tags))\r
++         (notmuch-search-elem-repr (cadr rest) thread)))\r
++\r
++      (:eval\r
++       (notmuch-search-elem-repr (apply #'eval rest) thread))\r
++\r
++      (otherwise\r
++       (mapconcat\r
++        (lambda (inner-elem) (notmuch-search-elem-repr inner-elem thread))\r
++        elem "")))))\r
++\r
++   (t\r
++    (let ((val (plist-get thread elem)))\r
++      (if val\r
++        (notmuch-search-elem-repr val thread)\r
++      (message "Unknown message attribute in `notmuch-search-elem-repr': %s" elem)\r
++      "")))))\r
\r
+ (defun notmuch-search-show-result (result pos)\r
+   "Insert RESULT at POS."\r
+@@ -782,9 +837,10 @@ non-authors is found, assume that all of the authors match."\r
+   (unless (= (plist-get result :matched) 0)\r
+     (save-excursion\r
+       (goto-char pos)\r
+-      (dolist (spec notmuch-search-result-format)\r
+-      (notmuch-search-insert-field (car spec) (cdr spec) result))\r
+-      (insert "\n")\r
++       ;; `notmuch-thread' is a well known symbol for functions to\r
++       ;; use during evaluation of `notmuch-search-result-format'.\r
++      (insert (let ((notmuch-thread result))\r
++              (notmuch-search-elem-repr notmuch-search-result-format notmuch-thread)))\r
+       (notmuch-search-color-line pos (point) (plist-get result :tags))\r
+       (put-text-property pos (point) 'notmuch-search-result result))))\r
\r
+-- \r
+2.1.3\r
+\r