aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-10-17 10:55:42 +1100
committerYuchen Pei <hi@ypei.me>2022-10-17 10:55:42 +1100
commit04bb89d40de71f9522210ed0dd6aa6673f6c4db1 (patch)
treefba45d5283405026373aa55aaf6cedea6ba8a18b
parent6d98df1f54c6891d9847e329cb7bd84c8c9f3221 (diff)
rename commands to handlers.
-rw-r--r--hmm.el66
1 files changed, 36 insertions, 30 deletions
diff --git a/hmm.el b/hmm.el
index 92b1fe0..f7bb7da 100644
--- a/hmm.el
+++ b/hmm.el
@@ -5,7 +5,7 @@
(defvar hmm-file-handlers nil "hmm file handlers.")
(defvar hmm-query-handlers nil "hmm query handlers.")
(defvar hmm-url-handlers nil "hmm url handlers.")
-(defvar hmm-commands '(:query nil :url nil :file nil))
+(defvar hmm-handlers '(:query nil :url nil :file nil))
(defmacro hmm-define-web-search-engine (engine browser)
(let* ((engine-name (plist-get engine :name))
@@ -18,32 +18,32 @@
(interactive ,(format "sSearch %s in %s for: " engine-name browser-name))
(,(plist-get browser :command)
(format ,(plist-get engine :format) query)))
- (hmm-add-command '(:command ,function-name) :query))))
+ (hmm-add-handler '(:command ,function-name) :query))))
-(defmacro hmm-define-external-command (xdg-command)
+(defmacro hmm-define-external-handler (xdg-handler)
(let* ((description (format "%s - %s"
- (plist-get xdg-command :name)
- (plist-get xdg-command :description)))
+ (plist-get xdg-handler :name)
+ (plist-get xdg-handler :description)))
(function-name (intern
(format "hmm-%s"
- (plist-get xdg-command :xdg-desktop-name))))
+ (plist-get xdg-handler :xdg-desktop-name))))
(command `(replace-regexp-in-string
"%[fFuU]" arg
- ,(plist-get xdg-command :external-command)))
- (name (plist-get xdg-command :name))
- (handler (plist-put xdg-command :command function-name)))
+ ,(plist-get xdg-handler :external-command)))
+ (name (plist-get xdg-handler :name))
+ (handler (plist-put xdg-handler :command function-name)))
`(progn
(defun ,function-name (arg)
,description
(interactive ,(format "sRun %s with: " name))
(start-process-shell-command
(concat ,name " " arg) nil ,command))
- (hmm-add-command ',handler :file))))
+ (hmm-add-handler ',handler :file))))
-(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-add-handler (handler handling)
+ (let ((handlers (plist-get hmm-handlers handling)))
+ (add-to-list 'handlers handler)
+ (plist-put hmm-handlers handling handlers)))
(defun hmm-thing-in-region (from to)
(interactive "r")
@@ -110,7 +110,7 @@ If it is a url, display a list of url handlers."
"\n"))
"Undocumented")))
(hmm-filter-commands thing type)))))
- (funcall (intern (car (split-string selected " ("))) thing)))
+ (funcall-interactively (intern (car (split-string selected " ("))) thing)))
(defun hmm-filter-commands (thing type)
(let ((mimetype (hmm-file-mime-type thing)))
@@ -124,28 +124,28 @@ If it is a url, display a list of url handlers."
(plist-get handler :mimetypes))
(and (null (plist-get handler :mimetypes))
(not (plist-get handler :xdg-desktop-name))))))
- (plist-get hmm-commands type)))))
+ (plist-get hmm-handlers type)))))
-(defun hmm-update-commands ()
+(defun hmm-update-handlers ()
(interactive)
- (setq hmm-commands '(:query nil :url nil :file nil))
+ (setq hmm-handlers '(: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))))
- (dolist (xdg-command (hmm-get-xdg-commands))
- (eval `(hmm-define-external-command ,xdg-command)))
- (dolist (command hmm-query-handlers) (hmm-add-command command :query))
+ (dolist (xdg-handler (hmm-get-xdg-handlers))
+ (eval `(hmm-define-external-handler ,xdg-handler)))
+ (dolist (handler hmm-query-handlers) (hmm-add-handler handler :query))
(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))
+ (mapc (lambda (handler) (hmm-add-handler handler :url))
hmm-url-handlers)
- (mapc (lambda (command) (hmm-add-command command :file))
+ (mapc (lambda (handler) (hmm-add-handler handler :file))
hmm-file-handlers))
-(defun hmm-get-xdg-commands ()
+(defun hmm-get-xdg-handlers ()
(seq-filter
'identity
(mapcar
@@ -171,11 +171,17 @@ If it is a url, display a list of url handlers."
(cons (xdg-data-home) (xdg-data-dirs))))
(defun hmm-file-mime-type (file)
- (when (file-exists-p file)
- (replace-regexp-in-string
- "^.*: \\(.*\\); .*$" "\\1"
- (with-temp-buffer
- (call-process "file" nil '(t t) nil "-Li" file)
- (string-trim (buffer-string))))))
+ "Returns (or print if called interactively) mimetype of FILE."
+ (interactive)
+ (let ((out))
+ (when (file-exists-p file)
+ (replace-regexp-in-string
+ "^.*: \\(.*\\); .*$" "\\1"
+ (with-temp-buffer
+ (call-process "file" nil '(t t) nil "-Li" file)
+ (setq out (string-trim (buffer-string)))
+ (if (called-interactively-p)
+ (message out)
+ out))))))
(provide 'hmm)