This is a mess

I'm sorry.  I really wanted to improve my commit discipline.  I know.  I can't
be fucked to comb this diff and split it into 8 properly-ordered commits, like I
know I should.  I'm not having a good time right now.  We'll do better moving
forward.
This commit is contained in:
Madeleine Sydney
2025-01-31 16:45:37 -07:00
parent f247599853
commit d59c79a8d4
16 changed files with 721 additions and 33 deletions

View File

@@ -6,11 +6,59 @@
(require 'better-jumper)
(defvar syd-lookup-documentation-handlers '()
"An list of lookup handlers used to find documentation. A lookup handler
(defvar syd-lookup-online-documentation-backends
`(("Kagi" . "https://kagi.com/search?q=%s")
("DuckDuckGo" . "https://duckduckgo.com/?q=%s")
("Nixpkgs" . "https://search.nixos.org/packages?query=%s")
("Hackage" . "https://hackage.haskell.org/packages/search?terms=%s"))
"A list of pairs (NAME . BACKEND) describing the various backends
`syd-lookup-online-documentation' may delegate to.
NAME is a string used when speaking to the user about BACKEND.
If BACKEND is an interactive command, it will be called interactively.
If BACKEND is a procedure, it will be called with the search string as the lone
argument.
If BACKEND is a string, the user's browser will be opened to the URL returned by
(format BACKEND QUERY), where QUERY is the appropriately-encoded search
string.")
(defvar syd-lookup-documentation-handlers '(syd-lookup-online-documentation)
"A list of lookup handlers used to find documentation. A lookup handler
receives an identifier, and is expected to return nil on failure, and non-nil on
success. The specific return value is unused outside of the test for nil.")
(defun syd-lookup--prompt-for-online-backend ()
(assoc-string
(completing-read "Search with: "
(mapcar #'car syd-lookup-online-documentation-backends)
nil
t)
syd-lookup-online-documentation-backends))
(cl-defun syd-lookup--call-online-backend (backend &key query-string)
(pcase-let ((`(,name . ,backend-fn) backend))
(cond ((functionp backend-fn) (if (commandp backend-fn)
(call-interactively backend-fn)
(funcall backend-fn query-string)))
((stringp backend-fn)
(browse-url (format backend-fn
(url-encode-url
(read-string (format "Search for (on %s): "
name)
query-string)))))
(t (signal 'wrong-type-argument `("backend" ,backend-fn))))))
;;;###autoload
(cl-defun syd-lookup-online-documentation (backend &key query-string)
(interactive (list (syd-lookup--prompt-for-online-backend)
:query-string (when (use-region-p)
(syd-thing-at-point-or-region))))
(syd-lookup--call-online-backend (syd-lookup--prompt-for-online-backend)
:query-string query-string))
;;;###autoload
(defun syd-lookup-documentation (identifier)
"Try to find documentation on IDENTIFIER, and "
@@ -28,6 +76,8 @@ success. The specific return value is unused outside of the test for nil.")
(category identifier &key (display-fn #'switch-to-buffer))
(let* ((handlers (alist-get category syd-lookup--handlers-by-category))
(origin (point-marker))
;; TODO: If called with a prefix argument, prompt the user to select a
;; handler.
(result (run-hook-wrapped handlers #'syd-lookup--run-handler
identifier origin)))
(message "result wrap hok: %S" result)