discussion and development of piem
 help / color / mirror / code / Atom feed
* [PATCH 00/10] lei-q transient
@ 2021-10-25  3:56 Kyle Meyer
  2021-10-25  3:56 ` [PATCH 01/10] piem-lei-query-show: Fix capitalization in error message Kyle Meyer
                   ` (9 more replies)
  0 siblings, 10 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

The series at <20210605211402.20304-1-kyle@kyleam.com> added intial
support for lei-q, but it left the transient interface as a to-do.
Patch 6 of this series adds that, making it possible to pass relevant
lei-q options to the call.

  [ 1/10] piem-lei-query-show: Fix capitalization in error message
  [ 2/10] piem-lei-known-mid-p: Use with-output-to-string
  [ 3/10] lei: Introduce option to set "lei" executable
  [ 4/10] lei: Add call-process wrapper for inserting lei output
  [ 5/10] piem-lei-insert-output: Signal an error if lei call fails
  [ 6/10] lei: Add transient wrapper for piem-lei-query
  [ 7/10] lei: Set piem-lei-buffer-query for mid-based query functions
  [ 8/10] piem: Add function that returns public-inbox's configuration file
  [ 9/10] lei q: Offer candidates for --include and --only
  [10/10] lei: Rename piem-lei-show-mid to piem-lei-buffer-mid

 TODO        |   2 -
 piem-lei.el | 236 ++++++++++++++++++++++++++++++++++++++++++++--------
 piem.el     |  11 ++-
 3 files changed, 211 insertions(+), 38 deletions(-)


base-commit: 4122fe3c6e350182d40a527d4392edb08e1dcb92
-- 
2.33.1


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 01/10] piem-lei-query-show: Fix capitalization in error message
  2021-10-25  3:56 [PATCH 00/10] lei-q transient Kyle Meyer
@ 2021-10-25  3:56 ` Kyle Meyer
  2021-10-25  3:56 ` [PATCH 02/10] piem-lei-known-mid-p: Use with-output-to-string Kyle Meyer
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

---
 piem-lei.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/piem-lei.el b/piem-lei.el
index 6bd977d8..611b3243 100644
--- a/piem-lei.el
+++ b/piem-lei.el
@@ -251,7 +251,7 @@ (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")))
    '(display-buffer-below-selected
      (inhibit-same-window . t)
      (window-height . 0.8))))
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 02/10] piem-lei-known-mid-p: Use with-output-to-string
  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 ` Kyle Meyer
  2021-10-25  3:56 ` [PATCH 03/10] lei: Introduce option to set "lei" executable Kyle Meyer
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

piem-lei-known-mid-p does essentially the same thing as
piem-notmuch-known-mid-p, so collect output in a consistent way.
---
 piem-lei.el | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/piem-lei.el b/piem-lei.el
index 611b3243..6fb659c9 100644
--- a/piem-lei.el
+++ b/piem-lei.el
@@ -579,10 +579,10 @@ (defun piem-lei-known-mid-p (mid)
   "Return non-nil if MID is known to lei.
 The message ID should not include have surrounding brackets."
   (not (string-empty-p
-        (with-temp-buffer
-          (call-process "lei" nil '(t nil) nil
-                        "q" "--format=ldjson" (concat "mid:" mid))
-          (buffer-string)))))
+        (with-output-to-string
+          (call-process "lei"
+                        nil (list standard-output nil) nil
+                        "q" "--format=ldjson" (concat "mid:" mid))))))
 
 (defun piem-lei-mid-to-thread (mid)
   "Return a function that inserts an mbox for MID's thread."
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 03/10] lei: Introduce option to set "lei" executable
  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 ` Kyle Meyer
  2021-10-25  3:56 ` [PATCH 04/10] lei: Add call-process wrapper for inserting lei output Kyle Meyer
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

There are -executable options for git and b4, so for consistency add
one for lei.  And for things like Guix that prefer to expand
executables to a full path, this makes it easier because there's just
one spot to patch.
---
 piem-lei.el | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/piem-lei.el b/piem-lei.el
index 6fb659c9..904bf972 100644
--- a/piem-lei.el
+++ b/piem-lei.el
@@ -33,6 +33,10 @@ (defgroup piem-lei nil
   "lei integration for piem."
   :group 'piem)
 
+(defcustom piem-lei-lei-executable "lei"
+  "Which lei executable to use."
+  :type 'string)
+
 \f
 ;;;; Message display
 
@@ -120,7 +124,7 @@ (defun piem-lei-show (mid &optional display)
   (with-current-buffer (get-buffer-create "*lei-show*")
     (let ((inhibit-read-only t))
       (erase-buffer)
-      (call-process "lei" nil '(t nil) nil
+      (call-process piem-lei-lei-executable nil '(t nil) nil
                     "q" "--format=text" (concat "mid:" mid))
       (goto-char (point-min))
       (when (looking-at-p "# blob:")
@@ -210,7 +214,7 @@ (defun piem-lei-query (query)
   (with-current-buffer (get-buffer-create "*lei-query*")
     (let ((inhibit-read-only t))
       (erase-buffer)
-      (apply #'call-process "lei" nil '(t nil) nil
+      (apply #'call-process piem-lei-lei-executable nil '(t nil) nil
              "q" "--format=ldjson" query)
       (goto-char (point-min))
       (while (not (eobp))
@@ -483,7 +487,7 @@ (defun piem-lei-query--format-thread-marker (level)
 
 (defun piem-lei-query--slurp (args)
   (with-temp-buffer
-    (apply #'call-process "lei" nil '(t nil) nil
+    (apply #'call-process piem-lei-lei-executable nil '(t nil) nil
            "q" "--format=ldjson" args)
     (goto-char (point-min))
     (let (items)
@@ -570,7 +574,7 @@ (defun piem-lei-get-inbox ()
   "Return inbox name from a lei buffer."
   (when-let ((mid (piem-lei-get-mid)))
     (with-temp-buffer
-      (call-process "lei" nil '(t nil) nil
+      (call-process piem-lei-lei-executable nil '(t nil) nil
                     "q" "--format=mboxrd" (concat "mid:" mid))
       (goto-char (point-min))
       (piem-inbox-by-header-match))))
@@ -580,7 +584,7 @@ (defun piem-lei-known-mid-p (mid)
 The message ID should not include have surrounding brackets."
   (not (string-empty-p
         (with-output-to-string
-          (call-process "lei"
+          (call-process piem-lei-lei-executable
                         nil (list standard-output nil) nil
                         "q" "--format=ldjson" (concat "mid:" mid))))))
 
@@ -588,7 +592,7 @@ (defun piem-lei-mid-to-thread (mid)
   "Return a function that inserts an mbox for MID's thread."
   (when (piem-lei-known-mid-p mid)
     (lambda ()
-      (call-process "lei" nil '(t nil) nil
+      (call-process piem-lei-lei-executable nil '(t nil) nil
                     "q" "--format=mboxrd" "--threads"
                     (concat "mid:" mid)))))
 
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 04/10] lei: Add call-process wrapper for inserting lei output
  2021-10-25  3:56 [PATCH 00/10] lei-q transient Kyle Meyer
                   ` (2 preceding siblings ...)
  2021-10-25  3:56 ` [PATCH 03/10] lei: Introduce option to set "lei" executable Kyle Meyer
@ 2021-10-25  3:56 ` Kyle Meyer
  2021-10-25  3:56 ` [PATCH 05/10] piem-lei-insert-output: Signal an error if lei call fails Kyle Meyer
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

This is a bit more readable, and it introduces a single spot where
error handling can be added.
---
 piem-lei.el | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/piem-lei.el b/piem-lei.el
index 904bf972..5f381173 100644
--- a/piem-lei.el
+++ b/piem-lei.el
@@ -37,6 +37,15 @@ (defcustom piem-lei-lei-executable "lei"
   "Which lei executable to use."
   :type 'string)
 
+\f
+;;;; Helpers
+
+(defun piem-lei-insert-output (args &optional buffer)
+  "Call lei with ARGS and insert standard output in BUFFER.
+If BUFFER is nil, the current buffer is used."
+  (apply #'call-process piem-lei-lei-executable nil
+         (list (or buffer t) nil) nil args))
+
 \f
 ;;;; Message display
 
@@ -124,8 +133,8 @@ (defun piem-lei-show (mid &optional display)
   (with-current-buffer (get-buffer-create "*lei-show*")
     (let ((inhibit-read-only t))
       (erase-buffer)
-      (call-process piem-lei-lei-executable nil '(t nil) nil
-                    "q" "--format=text" (concat "mid:" mid))
+      (piem-lei-insert-output
+       (list "q" "--format=text" (concat "mid:" mid)))
       (goto-char (point-min))
       (when (looking-at-p "# blob:")
         (delete-region (line-beginning-position)
@@ -214,8 +223,8 @@ (defun piem-lei-query (query)
   (with-current-buffer (get-buffer-create "*lei-query*")
     (let ((inhibit-read-only t))
       (erase-buffer)
-      (apply #'call-process piem-lei-lei-executable nil '(t nil) nil
-             "q" "--format=ldjson" query)
+      (piem-lei-insert-output
+       (list "q" "--format=ldjson" query))
       (goto-char (point-min))
       (while (not (eobp))
         (let ((data (piem-lei-query--read-json-item)))
@@ -487,8 +496,8 @@ (defun piem-lei-query--format-thread-marker (level)
 
 (defun piem-lei-query--slurp (args)
   (with-temp-buffer
-    (apply #'call-process piem-lei-lei-executable nil '(t nil) nil
-           "q" "--format=ldjson" args)
+    (piem-lei-insert-output
+     (append (list "q" "--format=ldjson") args))
     (goto-char (point-min))
     (let (items)
       (while (not (eobp))
@@ -574,8 +583,8 @@ (defun piem-lei-get-inbox ()
   "Return inbox name from a lei buffer."
   (when-let ((mid (piem-lei-get-mid)))
     (with-temp-buffer
-      (call-process piem-lei-lei-executable nil '(t nil) nil
-                    "q" "--format=mboxrd" (concat "mid:" mid))
+      (piem-lei-insert-output
+       (list "q" "--format=mboxrd" (concat "mid:" mid)))
       (goto-char (point-min))
       (piem-inbox-by-header-match))))
 
@@ -584,17 +593,16 @@ (defun piem-lei-known-mid-p (mid)
 The message ID should not include have surrounding brackets."
   (not (string-empty-p
         (with-output-to-string
-          (call-process piem-lei-lei-executable
-                        nil (list standard-output nil) nil
-                        "q" "--format=ldjson" (concat "mid:" mid))))))
+          (piem-lei-insert-output
+           (list "q" "--format=ldjson" (concat "mid:" mid))
+           standard-output)))))
 
 (defun piem-lei-mid-to-thread (mid)
   "Return a function that inserts an mbox for MID's thread."
   (when (piem-lei-known-mid-p mid)
     (lambda ()
-      (call-process piem-lei-lei-executable nil '(t nil) nil
-                    "q" "--format=mboxrd" "--threads"
-                    (concat "mid:" mid)))))
+      (piem-lei-insert-output
+       (list "q" "--format=mboxrd" "--threads" (concat "mid:" mid))))))
 
 ;;;###autoload
 (define-minor-mode piem-lei-mode
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 05/10] piem-lei-insert-output: Signal an error if lei call fails
  2021-10-25  3:56 [PATCH 00/10] lei-q transient Kyle Meyer
                   ` (3 preceding siblings ...)
  2021-10-25  3:56 ` [PATCH 04/10] lei: Add call-process wrapper for inserting lei output Kyle Meyer
@ 2021-10-25  3:56 ` Kyle Meyer
  2021-10-25  3:56 ` [PATCH 06/10] lei: Add transient wrapper for piem-lei-query Kyle Meyer
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

None of the current calls to lei should have a non-zero exit, even
when they come up empty.
---
 piem-lei.el | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/piem-lei.el b/piem-lei.el
index 5f381173..f3b16b65 100644
--- a/piem-lei.el
+++ b/piem-lei.el
@@ -43,8 +43,10 @@ (defcustom piem-lei-lei-executable "lei"
 (defun piem-lei-insert-output (args &optional buffer)
   "Call lei with ARGS and insert standard output in BUFFER.
 If BUFFER is nil, the current buffer is used."
-  (apply #'call-process piem-lei-lei-executable nil
-         (list (or buffer t) nil) nil args))
+  (unless (= 0 (apply #'call-process piem-lei-lei-executable nil
+                      (list (or buffer t) nil) nil args))
+    ;; TODO: Add debugging option for capturing stderr.
+    (error "Calling %s with %S failed" piem-lei-lei-executable args)))
 
 \f
 ;;;; Message display
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 06/10] lei: Add transient wrapper for piem-lei-query
  2021-10-25  3:56 [PATCH 00/10] lei-q transient Kyle Meyer
                   ` (4 preceding siblings ...)
  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
  2021-10-25  3:56 ` [PATCH 07/10] lei: Set piem-lei-buffer-query for mid-based query functions Kyle Meyer
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

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


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 07/10] lei: Set piem-lei-buffer-query for mid-based query functions
  2021-10-25  3:56 [PATCH 00/10] lei-q transient Kyle Meyer
                   ` (5 preceding siblings ...)
  2021-10-25  3:56 ` [PATCH 06/10] lei: Add transient wrapper for piem-lei-query Kyle Meyer
@ 2021-10-25  3:56 ` Kyle Meyer
  2021-10-25  3:56 ` [PATCH 08/10] piem: Add function that returns public-inbox's configuration file Kyle Meyer
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

piem-lei-query sets piem-lei-buffer-query to the query it's called
with.  Like piem-lei-query, piem-lei-query-thread produces a
piem-lei-query-mode buffer, but it doesn't set this variable.

Make piem-lei-query-thread do so for consistency (though I don't have
a concrete use in mind).  And I guess piem-lei-show might as well set
this variable too.
---
 piem-lei.el | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/piem-lei.el b/piem-lei.el
index 1051bb5c..e575f4f3 100644
--- a/piem-lei.el
+++ b/piem-lei.el
@@ -142,10 +142,11 @@ (defun piem-lei-show (mid &optional args display)
    (list (read-string "Message ID: " nil nil (piem-mid))
          'display))
   (with-current-buffer (get-buffer-create "*lei-show*")
-    (let ((inhibit-read-only t))
+    (let ((inhibit-read-only t)
+          (query (list (concat "mid:" mid))))
       (erase-buffer)
       (piem-lei-insert-output
-       (append (list "q" "--format=text") args (list (concat "mid:" mid))))
+       (append (list "q" "--format=text") args query))
       (goto-char (point-min))
       (when (looking-at-p "# blob:")
         (delete-region (line-beginning-position)
@@ -153,6 +154,7 @@ (defun piem-lei-show (mid &optional args display)
       (piem-lei-show-mode)
       (setq piem-lei-buffer-args args)
       (setq piem-lei-show-mid mid)
+      (setq  piem-lei-buffer-query query)
       (piem-lei-show--fontify-headers))
     (if display
         (pop-to-buffer (current-buffer))
@@ -596,8 +598,9 @@ (defun piem-lei-query-thread (mid &optional args)
    (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
-                   (append args (list "--threads") (list (concat "mid:" mid)))))
+  (let* ((query (list (concat "mid:" mid)))
+         (records (piem-lei-query--slurp
+                   (append args (list "--threads") query)))
          (msgs (piem-lei-query--thread records))
          depths pt-final subject-prev)
     (with-current-buffer (get-buffer-create "*lei-thread*")
@@ -651,6 +654,7 @@ (defun piem-lei-query-thread (mid &optional args)
       (piem-lei-query-mode)
       (setq piem-lei-buffer-args args)
       (setq piem-lei-show-mid mid)
+      (setq  piem-lei-buffer-query query)
       (pop-to-buffer-same-window (current-buffer)))))
 
 \f
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 08/10] piem: Add function that returns public-inbox's configuration file
  2021-10-25  3:56 [PATCH 00/10] lei-q transient Kyle Meyer
                   ` (6 preceding siblings ...)
  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 ` 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
  9 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

piem--merge-config-inboxes determines public-inbox's configuration
file by using PI_CONFIG if set, falling back to the hardcoded
location.  piem-lei.el will need to do the same, so move the logic
into a function.
---
 piem.el | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/piem.el b/piem.el
index 31b9c71c..afe6b7af 100644
--- a/piem.el
+++ b/piem.el
@@ -373,9 +373,13 @@ (defun piem--git-config-list (&optional file)
 
 (defvar piem--inboxes 'unset)
 
+(defun piem-public-inbox-config-file ()
+  "Return public-inbox's configuration file."
+  (or (getenv "PI_CONFIG")
+      (expand-file-name "~/.public-inbox/config")))
+
 (defun piem--merge-config-inboxes ()
-  (let ((cfg-file (or (getenv "PI_CONFIG")
-                      (expand-file-name "~/.public-inbox/config"))))
+  (let ((cfg-file (piem-public-inbox-config-file)))
     (if (not (file-readable-p cfg-file))
         (setq piem--inboxes piem-inboxes)
       (let ((case-fold-search t)
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 09/10] lei q: Offer candidates for --include and --only
  2021-10-25  3:56 [PATCH 00/10] lei-q transient Kyle Meyer
                   ` (7 preceding siblings ...)
  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 ` Kyle Meyer
  2021-10-25  3:56 ` [PATCH 10/10] lei: Rename piem-lei-show-mid to piem-lei-buffer-mid Kyle Meyer
  9 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

The --include option of lei-q enables searching external sources that
are not already registered, whether they are local inboxes or remote
URLs.  --only also does this, along with restricting the results to
the specified sources.  As such, registered inboxes make sense as
values for --only, in _addition_ to any values the make sense for
--include.
---
 piem-lei.el | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 72 insertions(+), 2 deletions(-)

diff --git a/piem-lei.el b/piem-lei.el
index e575f4f3..2f09c387 100644
--- a/piem-lei.el
+++ b/piem-lei.el
@@ -382,6 +382,71 @@ (define-derived-mode piem-lei-query-mode special-mode "lei-query"
 \f
 ;;;;; lei-q transient
 
+(defun piem-lei-externals ()
+  "Return configured externals."
+  (seq-remove
+   (lambda (e) (string-prefix-p "boost=" e))
+   (split-string
+    (with-output-to-string
+      (piem-lei-insert-output
+       (list "ls-external" "-z") standard-output))
+    "\0" t)))
+
+(defun piem-lei-inboxdir-urls ()
+  "Return hash table mapping each inboxdir to its URL.
+These values correspond to local inboxes that are configured via
+public-inbox's configuration."
+  (let ((case-fold-search t)
+        (pi-cfg (piem--git-config-list (piem-public-inbox-config-file)))
+        inboxdir-urls)
+    (maphash
+     (lambda (key val)
+       (when (string-match
+              (rx string-start "publicinbox."
+                  (group (one-or-more not-newline)) "."
+                  (or "inboxdir" "mainrepo")
+                  string-end)
+              key)
+         (push (cons (car val)
+                     (when-let ((url (car (gethash
+                                           (format "publicinbox.%s.url"
+                                                   (match-string 1 key))
+                                           pi-cfg))))
+                       (piem--ensure-trailing-slash url)))
+               inboxdir-urls)))
+     pi-cfg)
+    inboxdir-urls))
+
+(defun piem-lei-external-sources (&optional include-registered)
+  "Return a list of known external sources.
+Unless INCLUDE-REGISTERED is non-nil, the result does not include
+sources that have already been registered with lei as an
+external (via `lei add-external')."
+  (let ((inboxdir-urls (piem-lei-inboxdir-urls)))
+    (nconc
+     (let ((inboxdirs (mapcar #'car inboxdir-urls)))
+       (if include-registered
+           inboxdirs
+         (cl-set-difference inboxdirs (piem-lei-externals) :test #'equal)))
+     (cl-set-difference
+      (delq nil
+            (mapcar (lambda (x)
+                      (when-let ((url (plist-get (cdr x) :url)))
+                        ;; lei-add-external normalizes URLs to
+                        ;; have a trailing slash.
+                        (piem--ensure-trailing-slash url)))
+                    (piem-merged-inboxes)))
+      (delq nil (mapcar #'cdr inboxdir-urls))
+      :test #'equal))))
+
+(defun piem-lei-read-external-source (prompt &optional default history)
+  (completing-read prompt (piem-lei-external-sources)
+                   nil nil nil history default))
+
+(defun piem-lei-read-external-source-all (prompt &optional default history)
+  (completing-read prompt (piem-lei-external-sources t)
+                   nil nil nil history default))
+
 (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))
@@ -389,17 +454,22 @@ (defun piem-lei-q-read-sort-key (&rest _ignore)
     (?l "relevance")
     (?d "docid")))
 
+;; TODO: Support reading multiple values.
 (transient-define-argument piem-lei-q:--include ()
   :description "Include external in search"
   :class 'transient-option
   :shortarg "-I"
-  :argument "--include=")
+  :argument "--include="
+  :reader #'piem-lei-read-external-source)
+
 
+;; TODO: Support reading multiple values.
 (transient-define-argument piem-lei-q:--only ()
   :description "Search only this location"
   :class 'transient-option
   :shortarg "-O"
-  :argument "--only=")
+  :argument "--only="
+  :reader #'piem-lei-read-external-source-all)
 
 (transient-define-argument piem-lei-q:--sort ()
   :description "Sort key for results"
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 10/10] lei: Rename piem-lei-show-mid to piem-lei-buffer-mid
  2021-10-25  3:56 [PATCH 00/10] lei-q transient Kyle Meyer
                   ` (8 preceding siblings ...)
  2021-10-25  3:56 ` [PATCH 09/10] lei q: Offer candidates for --include and --only Kyle Meyer
@ 2021-10-25  3:56 ` Kyle Meyer
  2021-10-26  2:49   ` Kyle Meyer
  9 siblings, 1 reply; 12+ messages in thread
From: Kyle Meyer @ 2021-10-25  3:56 UTC (permalink / raw)
  To: piem

The new name aligns more closely with the piem-lei-buffer-query and
piem-lei-buffer-args buffer-local variables introduced in the previous
commit.
---
 piem-lei.el | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/piem-lei.el b/piem-lei.el
index 2f09c387..ffd72f39 100644
--- a/piem-lei.el
+++ b/piem-lei.el
@@ -93,7 +93,7 @@ (defface piem-lei-show-cited-text-4
   '((t :inherit message-cited-text-4))
   "Face for 4th-level cited text in `piem-lei-show-mode' buffers.")
 
-(defvar-local piem-lei-show-mid nil
+(defvar-local piem-lei-buffer-mid nil
   "Message ID shown in current buffer.")
 
 (defvar-local piem-lei-buffer-args nil
@@ -153,7 +153,7 @@ (defun piem-lei-show (mid &optional args display)
                        (1+ (line-end-position))))
       (piem-lei-show-mode)
       (setq piem-lei-buffer-args args)
-      (setq piem-lei-show-mid mid)
+      (setq piem-lei-buffer-mid mid)
       (setq  piem-lei-buffer-query query)
       (piem-lei-show--fontify-headers))
     (if display
@@ -339,7 +339,7 @@ (defun piem-lei-query-show-or-scroll-up (arg)
       (let ((w (piem-lei-query--get-visible-message-window)))
         (if (and w
                  (equal (with-current-buffer (window-buffer w)
-                          piem-lei-show-mid)
+                          piem-lei-buffer-mid)
                         mid))
             (with-selected-window w
               (scroll-up-command arg))
@@ -723,7 +723,7 @@ (defun piem-lei-query-thread (mid &optional args)
       (goto-char (or pt-final (point-min)))
       (piem-lei-query-mode)
       (setq piem-lei-buffer-args args)
-      (setq piem-lei-show-mid mid)
+      (setq piem-lei-buffer-mid mid)
       (setq  piem-lei-buffer-query query)
       (pop-to-buffer-same-window (current-buffer)))))
 
@@ -733,7 +733,7 @@ (defun piem-lei-query-thread (mid &optional args)
 (defun piem-lei-get-mid ()
   "Return the message ID of a lei buffer."
   (cond ((derived-mode-p 'piem-lei-show-mode)
-         piem-lei-show-mid)
+         piem-lei-buffer-mid)
         ((derived-mode-p 'piem-lei-query-mode)
          (piem-lei-query-get-mid))))
 
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH 10/10] lei: Rename piem-lei-show-mid to piem-lei-buffer-mid
  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
  0 siblings, 0 replies; 12+ messages in thread
From: Kyle Meyer @ 2021-10-26  2:49 UTC (permalink / raw)
  To: piem

Kyle Meyer writes:

> The new name aligns more closely with the piem-lei-buffer-query and
> piem-lei-buffer-args buffer-local variables introduced in the previous
> commit.

Doh, that was the "previous commit" when I originally wrote that
message, but that's no longer the case.  Will reword when applying.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-10-26  2:49 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 06/10] lei: Add transient wrapper for piem-lei-query Kyle Meyer
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

Code repositories for project(s) associated with this public 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).