From 04bb89d40de71f9522210ed0dd6aa6673f6c4db1 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Mon, 17 Oct 2022 10:55:42 +1100 Subject: rename commands to handlers. --- hmm.el | 66 ++++++++++++++++++++++++++++++++++++------------------------------ 1 file 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) -- cgit v1.2.3