discussion and development of piem
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: piem@inbox.kyleam.com
Subject: [PATCH 06/10] lei: Add transient wrapper for piem-lei-query
Date: Sun, 24 Oct 2021 23:56:26 -0400	[thread overview]
Message-ID: <20211025035630.297598-7-kyle@kyleam.com> (raw)
In-Reply-To: <20211025035630.297598-1-kyle@kyleam.com>

Expose (most if not all) relevant arguments of lei-q in a new
transient.  The only somewhat tricky part here is propagating the
original arguments so that piem-lei-query-thread and
piem-lei-query-show can find messages that require a non-default
source (e.g., an unregistered external or a remote source when there
are local externals configured).

While remote operations work, the current design is still focused on a
setup where externals are configured locally, as described in
<20210605211402.20304-1-kyle@kyleam.com> (e.g., there's no attempt to
limit the number of times the server is hit).

Using the key 's' (for search) is unfortunate given the
command name is `lei q', but I think that's better than using 'q',
which is pretty widely used for "quit" in Emacs buffers.
---
 TODO        |   2 -
 piem-lei.el | 110 +++++++++++++++++++++++++++++++++++++++++++++-------
 piem.el     |   3 +-
 3 files changed, 98 insertions(+), 17 deletions(-)

diff --git a/TODO b/TODO
index 94ecd8d5..97d2d7f6 100644
--- a/TODO
+++ b/TODO
@@ -40,5 +40,3 @@ See <https://public-inbox.org/meta/20210224204950.GA2076@dcvr> and
 ** show: support replying
 
 ** transients for some other lei commands
-
-** transient wrapper around piem-lei-query
diff --git a/piem-lei.el b/piem-lei.el
index f3b16b65..1051bb5c 100644
--- a/piem-lei.el
+++ b/piem-lei.el
@@ -96,6 +96,12 @@ (defface piem-lei-show-cited-text-4
 (defvar-local piem-lei-show-mid nil
   "Message ID shown in current buffer.")
 
+(defvar-local piem-lei-buffer-args nil
+  "Non-query arguments that lei was called with.")
+
+(defvar-local piem-lei-buffer-query nil
+  "Query arguments that `lei q' was called with.")
+
 (defun piem-lei-show--fontify-headers ()
   (save-excursion
     (let (last-value-face)
@@ -125,8 +131,11 @@ (defun piem-lei-show--fontify-headers ()
                                'font-lock-face last-value-face))
           (forward-line))))))
 
-(defun piem-lei-show (mid &optional display)
+(defun piem-lei-show (mid &optional args display)
   "Show message for MID.
+
+ARGS is passed to the underlying `lei q' call.
+
 When called non-interactively, return the buffer but do not display it
 unless DISPLAY is non-nil."
   (interactive
@@ -136,12 +145,13 @@ (defun piem-lei-show (mid &optional display)
     (let ((inhibit-read-only t))
       (erase-buffer)
       (piem-lei-insert-output
-       (list "q" "--format=text" (concat "mid:" mid)))
+       (append (list "q" "--format=text") args (list (concat "mid:" mid))))
       (goto-char (point-min))
       (when (looking-at-p "# blob:")
         (delete-region (line-beginning-position)
                        (1+ (line-end-position))))
       (piem-lei-show-mode)
+      (setq piem-lei-buffer-args args)
       (setq piem-lei-show-mid mid)
       (piem-lei-show--fontify-headers))
     (if display
@@ -157,7 +167,7 @@ (defvar piem-lei-show-mode-font-lock-keywords
 
 (defvar piem-lei-show-mode-map
   (let ((map (make-sparse-keymap)))
-    (define-key map "s" #'piem-lei-query)
+    (define-key map "s" #'piem-lei-q)
     (define-key map "t" #'piem-lei-query-thread)
     map)
   "Keymap for `piem-lei-show-mode'.")
@@ -216,17 +226,18 @@ (defun piem-lei-query--format-date (data)
      'font-lock-face 'piem-lei-query-date)))
 
 ;;;###autoload
-(defun piem-lei-query (query)
-  "Call `lei q' with QUERY.
+(defun piem-lei-query (query &optional args)
+  "Call `lei q' with QUERY and ARGS.
 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))))
+          (read-string "Query: " "d:20.days.ago.. " 'piem-lei-query-history))
+         (transient-args 'piem-lei-q)))
   (with-current-buffer (get-buffer-create "*lei-query*")
     (let ((inhibit-read-only t))
       (erase-buffer)
       (piem-lei-insert-output
-       (list "q" "--format=ldjson" query))
+       (append (list "q" "--format=ldjson") args query))
       (goto-char (point-min))
       (while (not (eobp))
         (let ((data (piem-lei-query--read-json-item)))
@@ -251,6 +262,8 @@ (defun piem-lei-query (query)
       (insert "End of lei-q results"))
     (goto-char (point-min))
     (piem-lei-query-mode)
+    (setq piem-lei-buffer-args args)
+    (setq piem-lei-buffer-query query)
     (pop-to-buffer-same-window (current-buffer))))
 
 (defun piem-lei-query-get-mid (&optional pos)
@@ -266,7 +279,8 @@ (defun piem-lei-query-show ()
   (display-buffer
    (piem-lei-show
     (or (piem-lei-query-get-mid)
-        (user-error "No message ID associated with current line")))
+        (user-error "No message ID associated with current line"))
+    piem-lei-buffer-args)
    '(display-buffer-below-selected
      (inhibit-same-window . t)
      (window-height . 0.8))))
@@ -349,7 +363,7 @@ (defvar piem-lei-query-mode-map
     (define-key map (kbd "SPC") #'piem-lei-query-show-or-scroll-up)
     (define-key map "n" #'piem-lei-query-next-line)
     (define-key map "p" #'piem-lei-query-previous-line)
-    (define-key map "s" #'piem-lei-query)
+    (define-key map "s" #'piem-lei-q)
     (define-key map "t" #'piem-lei-query-thread)
     map)
   "Keymap for `piem-lei-query-mode'.")
@@ -363,6 +377,70 @@ (define-derived-mode piem-lei-query-mode special-mode "lei-query"
   (setq buffer-read-only t)
   (setq-local line-move-visual t))
 
+\f
+;;;;; lei-q transient
+
+(defun piem-lei-q-read-sort-key (&rest _ignore)
+  (pcase (read-char-choice "re[c]eived re[l]evance [d]ocid "
+                           (list ?c ?l ?d))
+    (?c "received")
+    (?l "relevance")
+    (?d "docid")))
+
+(transient-define-argument piem-lei-q:--include ()
+  :description "Include external in search"
+  :class 'transient-option
+  :shortarg "-I"
+  :argument "--include=")
+
+(transient-define-argument piem-lei-q:--only ()
+  :description "Search only this location"
+  :class 'transient-option
+  :shortarg "-O"
+  :argument "--only=")
+
+(transient-define-argument piem-lei-q:--sort ()
+  :description "Sort key for results"
+  :class 'transient-option
+  :shortarg "-s"
+  :argument "--sort="
+  :reader #'piem-lei-q-read-sort-key)
+
+(transient-define-argument piem-lei-q:--limit ()
+  :description "Limit number of matches (default: 10000)"
+  :class 'transient-option
+  :shortarg "-n"
+  :argument "--limit="
+  :reader #'transient-read-number-N+)
+
+(transient-define-argument piem-lei-q:--offset ()
+  :description "Shift start of results (default: 0)"
+  :class 'transient-option
+  :shortarg "-N"
+  :argument "--offset="
+  :reader #'transient-read-number-N0)
+
+;;;###autoload (autoload 'piem-lei-q "piem-lei" nil t)
+(transient-define-prefix piem-lei-q ()
+  "Search for messages with `lei q'."
+  :man-page "lei-q"
+  :incompatible '(("--remote" "--no-remote")
+                  ("--no-externals" "--no-local"))
+  ["Arguments"
+   (piem-lei-q:--include)
+   (piem-lei-q:--only)
+   ("-g" "Match locations literally" "--globoff")
+   ("xe" "Exclude results from externals" "--no-externals")
+   ("xl" "Exclude results from local sources" "--no-local")
+   ("xr" "Exclude results from remote sources" "--no-remote")
+   ("+r" "Include results from remote sources" "--remote")
+   (piem-lei-q:--sort)
+   ("-r" "Reverse search results" "--reverse")
+   (piem-lei-q:--limit)
+   (piem-lei-q:--offset)]
+  ["Actions"
+   ("s" "Search with lei" piem-lei-query)])
+
 \f
 ;;;;; Threading
 
@@ -511,13 +589,15 @@ (defun piem-lei-query--slurp (args)
         (forward-line))
       (nreverse items))))
 
-(defun piem-lei-query-thread (mid)
-  "Show thread containing message MID."
+(defun piem-lei-query-thread (mid &optional args)
+  "Show thread containing message MID.
+ARGS is passed to the underlying `lei q' call."
   (interactive
-   (list (or (piem-lei-get-mid)
-             (read-string "Message ID: " nil nil (piem-mid)))))
+   (if-let ((mid (piem-lei-get-mid)))
+       (list mid piem-lei-buffer-args)
+     (list (read-string "Message ID: " nil nil (piem-mid)) nil)))
   (let* ((records (piem-lei-query--slurp
-                   (list "--threads" (concat "mid:" mid))))
+                   (append args (list "--threads") (list (concat "mid:" mid)))))
          (msgs (piem-lei-query--thread records))
          depths pt-final subject-prev)
     (with-current-buffer (get-buffer-create "*lei-thread*")
@@ -569,6 +649,8 @@ (defun piem-lei-query-thread (mid)
         (insert "End of lei-q results"))
       (goto-char (or pt-final (point-min)))
       (piem-lei-query-mode)
+      (setq piem-lei-buffer-args args)
+      (setq piem-lei-show-mid mid)
       (pop-to-buffer-same-window (current-buffer)))))
 
 \f
diff --git a/piem.el b/piem.el
index 16ccdfc5..31b9c71c 100644
--- a/piem.el
+++ b/piem.el
@@ -986,7 +986,8 @@ (transient-define-prefix piem-dispatch ()
   [[("a" "apply patch" piem-am)
     ("b" "call b4-am" piem-b4-am)]
    [("i" "inject thread into maildir" piem-inject-thread-into-maildir)
-    ("l" "copy public-inbox link" piem-copy-mid-url)]])
+    ("l" "copy public-inbox link" piem-copy-mid-url)]
+   [("s" "search with lei" piem-lei-q)]])
 
 \f
 
-- 
2.33.1


  parent reply	other threads:[~2021-10-25  3:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-25  3:56 [PATCH 00/10] lei-q transient Kyle Meyer
2021-10-25  3:56 ` [PATCH 01/10] piem-lei-query-show: Fix capitalization in error message Kyle Meyer
2021-10-25  3:56 ` [PATCH 02/10] piem-lei-known-mid-p: Use with-output-to-string Kyle Meyer
2021-10-25  3:56 ` [PATCH 03/10] lei: Introduce option to set "lei" executable Kyle Meyer
2021-10-25  3:56 ` [PATCH 04/10] lei: Add call-process wrapper for inserting lei output Kyle Meyer
2021-10-25  3:56 ` [PATCH 05/10] piem-lei-insert-output: Signal an error if lei call fails Kyle Meyer
2021-10-25  3:56 ` Kyle Meyer [this message]
2021-10-25  3:56 ` [PATCH 07/10] lei: Set piem-lei-buffer-query for mid-based query functions Kyle Meyer
2021-10-25  3:56 ` [PATCH 08/10] piem: Add function that returns public-inbox's configuration file Kyle Meyer
2021-10-25  3:56 ` [PATCH 09/10] lei q: Offer candidates for --include and --only Kyle Meyer
2021-10-25  3:56 ` [PATCH 10/10] lei: Rename piem-lei-show-mid to piem-lei-buffer-mid Kyle Meyer
2021-10-26  2:49   ` Kyle Meyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://git.kyleam.com/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211025035630.297598-7-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=piem@inbox.kyleam.com \
    --subject='Re: [PATCH 06/10] lei: Add transient wrapper for piem-lei-query' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://git.kyleam.com/piem/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).