From fba0274c23070fc007350b6221fc7c9dd0108bb4 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Mon, 17 Oct 2022 16:11:06 +1100 Subject: using one single list for all handlers - also fixing hmm-define-external-xdg-handler to use start process to handle file names with special chars --- hmm.el | 108 +++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/hmm.el b/hmm.el index f7bb7da..fd3e919 100644 --- a/hmm.el +++ b/hmm.el @@ -2,9 +2,6 @@ (defvar hmm-web-search-engines nil "hmm web search engines.") (defvar hmm-web-browsers nil "hmm web browers.") -(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-handlers '(:query nil :url nil :file nil)) (defmacro hmm-define-web-search-engine (engine browser) @@ -20,24 +17,30 @@ (format ,(plist-get engine :format) query))) (hmm-add-handler '(:command ,function-name) :query)))) -(defmacro hmm-define-external-handler (xdg-handler) +(defmacro hmm-define-external-xdg-handler (xdg-handler) (let* ((description (format "%s - %s" (plist-get xdg-handler :name) (plist-get xdg-handler :description))) (function-name (intern (format "hmm-%s" (plist-get xdg-handler :xdg-desktop-name)))) - (command `(replace-regexp-in-string - "%[fFuU]" arg - ,(plist-get xdg-handler :external-command))) + (external-command + (mapcar (lambda (token) + (if (string-match "^%[fFuU]$" token) + `arg + token)) + (split-string (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)) + (apply 'start-process + (append + (list (format ,(format "%s-%%s" function-name) arg) + (format ,(format "*%s-%%s*" function-name) arg)) + ,(cons 'list external-command)))) (hmm-add-handler ',handler :file)))) (defun hmm-add-handler (handler handling) @@ -57,23 +60,28 @@ 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))) - ((and (derived-mode-p 'dired-mode) - (dired-get-filename nil t)) - (hmm-file (dired-get-filename nil t))) - ((and (derived-mode-p 'org-mode) - (my-org-link-at-point)) - (hmm-url (my-org-link-at-point))) - ((get-text-property (point) 'shr-url) - (hmm-url (get-text-property (point) 'shr-url))) - ((thing-at-point 'symbol) - (hmm-query (thing-at-point 'symbol))) - ((buffer-file-name) - (hmm-file (buffer-file-name))) - (t (error "Cannot guess what to do with what.")))) + (let ((thing)) + (cond ((setq thing (thing-at-point-url-at-point)) + (hmm-url thing)) + ((setq thing (thing-at-point-file-at-point)) + (hmm-file thing)) + ((and (derived-mode-p 'dired-mode) + (setq thing (dired-get-filename nil t))) + (hmm-file thing)) + ((derived-mode-p 'dired-mode) + (hmm-file (expand-file-name default-directory))) + ((and (derived-mode-p 'org-mode) + (setq thing (my-org-link-at-point))) + (hmm-url thing)) + ((setq thing (get-text-property (point) 'shr-url)) + (hmm-url thing)) + ((setq thing (thing-at-point 'symbol)) + (hmm-query thing)) + ((setq thing (buffer-file-name)) + (hmm-file thing)) + ((setq thing (expand-file-name default-directory)) + (hmm-file thing)) + (t (error "Cannot guess what to do with what."))))) (defun hmm () (interactive) @@ -119,31 +127,45 @@ If it is a url, display a list of url handlers." (plist-get handler :command)) (seq-filter (lambda (handler) - (and (string-match (or (plist-get handler :regex) ".*") thing) - (or (member mimetype - (plist-get handler :mimetypes)) - (and (null (plist-get handler :mimetypes)) - (not (plist-get handler :xdg-desktop-name)))))) + (and + (hmm-thing-match thing type handler) + (or (member mimetype + (plist-get handler :mimetypes)) + (and (null (plist-get handler :mimetypes)) + (not (plist-get handler :xdg-desktop-name)))))) (plist-get hmm-handlers type))))) +(defun hmm-thing-match (thing type handler) + (let ((regex (plist-get handler :regex))) + (cond ((eq type :url) + (let ((schemes (plist-get handler :schemes)) + (url (url-generic-parse-url thing)) + (url-no-scheme)) + (and + (or (null schemes) + (member (url-type url) schemes)) + (or (not regex) + (string-match + regex + (progn + (setf (url-type url) nil + (url-fullness url) nil) + (setq url-no-scheme (url-recreate-url url)))))))) + (t (or (not regex) + (string-match regex thing)))))) + (defun hmm-update-handlers () (interactive) - (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-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 (handler) (hmm-add-handler handler :url)) - hmm-url-handlers) - (mapc (lambda (handler) (hmm-add-handler handler :file)) - hmm-file-handlers)) + (eval `(hmm-define-external-xdg-handler ,xdg-handler))) + (dolist (browser hmm-web-browsers) + (hmm-add-handler + (list :command (plist-get browser :command) + :schemes (list "http" "https")) + :url))) (defun hmm-get-xdg-handlers () (seq-filter -- cgit v1.2.3