From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:aacc::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms12 with LMTPS id cEoLHDnpu2B9dQAAsNZ9tg (envelope-from ); Sat, 05 Jun 2021 21:14:33 +0000 Received: from out2.migadu.com ([2001:41d0:2:aacc::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id WH0YHDXpu2B+XQAAB5/wlQ (envelope-from ); Sat, 05 Jun 2021 21:14:29 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kyleam.com; s=key1; t=1622927668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9maAxabii+CFzzioyoBuDl+y6fA+9ll+rwsYHbQl5mQ=; b=ZTX3Yqap+zOMCllhOrYZ2vmGPdKvlwuBUDOau7OpVdkJVAqm8jnYSiU/Pc3WZhwe0KLWLH yIZNWZAQVx0wW7U//1Tl58vdfP+L2QbPh/wYBaqvk3HUtUOJYeu3clboF38J5DVtr7woYu y3II+vnGTsTldUJ/szkcHGOXzlzIQBFrxncOvpwniUHvBOl8J7bK2cb/ca8UT1JtrW0YcH eirLbq+5qD+4LHQ+dQ/tbPx8B2QhfmC1qgJGlQjj6mufRE4rGGN7R3SBdo0lwKV9MXL7ot 8iwkK1a5tOJaLFbvXVVy3Poq7JpRIwf97U7WB1io59v9BEVbGrT/X+SGTPlcEw== From: Kyle Meyer To: piem@inbox.kyleam.com Subject: [PATCH 04/18] lei: Add command and mode for displaying overview of search results Date: Sat, 5 Jun 2021 17:13:48 -0400 Message-Id: <20210605211402.20304-5-kyle@kyleam.com> In-Reply-To: <20210605211402.20304-1-kyle@kyleam.com> References: <20210605211402.20304-1-kyle@kyleam.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: kyle@kyleam.com X-TUID: pFfPqaNv53jW The output is intended to resemble search in public-inbox's web interface: an entry for each matching message. This is different from notmuch-search's output in that results are not grouped in their thread. I like notmuch's interface, although I'm not sure that trying to reshape lei-q's JSON output into something like that is worth the code complication or computation cost. The plan is to eventually wire this up to a transient to allow the caller to specify arguments (e.g., --only to restrict the search results to a particular inbox). --- piem-lei.el | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/piem-lei.el b/piem-lei.el index 291964fc..ed153c26 100644 --- a/piem-lei.el +++ b/piem-lei.el @@ -21,6 +21,7 @@ ;;; Code: +(require 'json) (require 'message) (require 'piem) @@ -140,5 +141,72 @@ (define-derived-mode piem-lei-show-mode special-mode "lei-show" (setq font-lock-defaults (list piem-lei-show-mode-font-lock-keywords t)) (setq-local line-move-visual t)) + +;;;; Searching + +(defun piem-lei-query--read-json-item () + (let ((json-object-type 'alist) + (json-array-type 'list) + ;; Using symbols for lei-q's output should be fine, though + ;; it's a little odd for the "t:" field. + (json-key-type 'symbol) + (json-false nil) + (json-null nil)) + (json-read))) + +(defvar piem-lei-query--date-re + (rx string-start + (group (= 4 digit) "-" (= 2 digit) "-" (= 2 digit)) + "T" (group (= 2 digit) ":" (= 2 digit)) ":" (= 2 digit) "Z" + string-end)) + +(defun piem-lei-query--format-date (data) + (let ((date (cdr (assq 'dt data)))) + (if (string-match piem-lei-query--date-re date) + (concat (match-string 1 date) " " (match-string 2 date)) + (error "Date did not match expected format: %S" date)))) + +;;;###autoload +(defun piem-lei-query (query) + "Call `lei q' with QUERY. +QUERY is split according to `split-string-and-unquote'." + (interactive + (list (split-string-and-unquote + (read-string "Query: " "d:20.days.ago.. " 'piem-lei-query-history)))) + (with-current-buffer (get-buffer-create "*lei-query*") + (let ((inhibit-read-only t)) + (erase-buffer) + (apply #'call-process "lei" nil '(t nil) nil + "q" "--format=ldjson" query) + (goto-char (point-min)) + (while (not (eobp)) + (let ((data (piem-lei-query--read-json-item))) + (delete-region (line-beginning-position) (point)) + (insert + (format "%s %3s %-20.20s %s" + (piem-lei-query--format-date data) + (if-let ((pct (cdr (assq 'pct data)))) + (concat (number-to-string (cdr (assq 'pct data))) + "%") + "") + (let ((from (car (cdr (assq 'f data))))) + (or (car from) (cadr from))) + (cdr (assq 's data)))) + (add-text-properties (line-beginning-position) (line-end-position) + (list 'piem-lei-query-result data))) + (forward-line)) + (insert "End of lei-q results")) + (goto-char (point-min)) + (piem-lei-query-mode) + (pop-to-buffer-same-window (current-buffer)))) + +(define-derived-mode piem-lei-query-mode special-mode "lei-query" + "Major mode for displaying overview of `lei q' results." + :group 'piem-lei + (buffer-disable-undo) + (setq truncate-lines t) + (setq buffer-read-only t) + (setq-local line-move-visual t)) + ;;; piem-lei.el ends here (provide 'piem-lei) -- 2.31.1