discussion and development of piem
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: piem@inbox.kyleam.com
Subject: [PATCH 09/10] lei q: Offer candidates for --include and --only
Date: Sun, 24 Oct 2021 23:56:29 -0400	[thread overview]
Message-ID: <20211025035630.297598-10-kyle@kyleam.com> (raw)
In-Reply-To: <20211025035630.297598-1-kyle@kyleam.com>

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


  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 ` [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 ` Kyle Meyer [this message]
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-10-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=piem@inbox.kyleam.com \
    --subject='Re: [PATCH 09/10] lei q: Offer candidates for --include and --only' \
    /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).