aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-10-14 13:21:32 +1100
committerYuchen Pei <hi@ypei.me>2022-10-14 13:21:32 +1100
commitd8ed5b455f1d916d1539c3f8b9a1669640e5585e (patch)
treef96c2056413adcc2db10f7ea4b2e3f40e2dce154
parent54ea91260151fc1f5cdca9af584f179edd8299a9 (diff)
adding url / file / query handlers
-rw-r--r--hmm.el104
1 files changed, 75 insertions, 29 deletions
diff --git a/hmm.el b/hmm.el
index 10e0e42..2e11423 100644
--- a/hmm.el
+++ b/hmm.el
@@ -1,9 +1,9 @@
;; -*- lexical-binding: t; -*-
-(defvar hmm-search-engines nil "hmm web search engines.")
+(defvar hmm-web-search-engines nil "hmm web search engines.")
(defvar hmm-web-browsers nil "hmm web browers.")
(defvar hmm-query-handlers nil "hmm query handlers.")
-(defvar hmm-favourite-commands nil)
+(defvar hmm-commands '(:query nil :url nil :file nil))
(defmacro hmm-define-web-search-engine (engine browser)
(let* ((engine-name (plist-get engine :name))
@@ -16,38 +16,84 @@
(interactive ,(format "sSearch %s in %s for: " engine-name browser-name))
(,(plist-get browser :command)
(format ,(plist-get engine :format) query)))
- (hmm-add-to-favourite-commands ',function-name))))
+ (hmm-add-command '(:command ,function-name) :query))))
-(defun hmm-add-to-favourite-commands (command)
- (add-to-list 'hmm-favourite-commands command))
+(defun hmm-add-command (command handling)
+ (let ((commands (plist-get hmm-commands handling)))
+ (add-to-list 'commands command)
+ (plist-put hmm-commands handling commands)))
-(defun hmm-search-region (from to)
+(defun hmm-query-region (from to)
(interactive "r")
(unless (region-active-p) (error "Region not active."))
(let* ((query (buffer-substring from to)))
(hmm-search-internal query)))
-(defun hmm-search (query)
+(defun hmm-thing-at-point ()
+ (interactive)
+ "Prompt for what to do with thing at point.
+
+If it is a file, display a list of file handlers.
+If it is a url, display a list of url handlers."
+ (cond ((thing-at-point-url-at-point)
+ (hmm-url (thing-at-point-url-at-point)))
+ ((thing-at-point-file-at-point)
+ (hmm-file (thing-at-point-file-at-point)))
+ (t (hmm-query (word-at-point)))))
+
+(defun hmm-query (query)
(interactive "sQuery: ")
- (hmm-search-internal query))
-
-(defun hmm-search-internal (query)
- (let ((selected (completing-read
- (format "Search %s using: " query)
- (mapcar
- (lambda (command)
- (format "%s (%s)" command
- (if (documentation command)
- (car
- (split-string
- (documentation command)
- "\n"))
- "Undocumented")))
- hmm-favourite-commands))))
- (funcall (intern (car (split-string selected " ("))) query)))
-
-(dolist (engine hmm-web-search-engines)
- (dolist (browser hmm-web-browsers)
- (eval `(hmm-define-web-search-engine ,engine ,browser))))
-
-(mapc 'hmm-add-to-favourite-commands hmm-query-handlers)
+ (hmm-open-internal query :query "Search %s using: "))
+
+(defun hmm-url (url)
+ (interactive (list (read-string "URL: " (thing-at-point-url-at-point))))
+ (hmm-open-internal url :url "Handle url %s using: "))
+
+(defun hmm-file (file)
+ (interactive (list (read-string "File: " (thing-at-point-file-at-point))))
+ (hmm-open-internal file :file "Handle file %s using: "))
+
+(defun hmm-open-internal (thing type prompt)
+ (let ((selected
+ (completing-read
+ (format prompt thing)
+ (mapcar
+ (lambda (command)
+ (format "%s (%s)" command
+ (if (documentation command)
+ (car
+ (split-string
+ (documentation command)
+ "\n"))
+ "Undocumented")))
+ (hmm-filter-commands thing type)))))
+ (funcall (intern (car (split-string selected " ("))) thing)))
+
+(defun hmm-filter-commands (thing type)
+ (mapcar
+ (lambda (handler)
+ (plist-get handler :command))
+ (seq-filter
+ (lambda (handler)
+ (string-match (or (plist-get handler :regex) ".*") thing))
+ (plist-get hmm-commands type))))
+
+(defun hmm-update-commands ()
+ (interactive)
+ (setq hmm-commands '(:query nil :url nil :file nil))
+ (dolist (engine hmm-web-search-engines)
+ (dolist (browser hmm-web-browsers)
+ (eval `(hmm-define-web-search-engine ,engine ,browser))))
+ (mapc (lambda (command) (hmm-add-command command :query))
+ hmm-query-handlers)
+ (mapc (lambda (browser)
+ (add-to-list 'hmm-url-handlers
+ (list :command (plist-get browser :command)
+ :regex "^http\\(s\\)?://.*$")))
+ hmm-web-browsers)
+ (mapc (lambda (command) (hmm-add-command command :url))
+ hmm-url-handlers))
+
+(hmm-update-commands)
+
+(provide 'hmm)