aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-10-17 10:39:36 +1100
committerYuchen Pei <hi@ypei.me>2022-10-17 10:39:36 +1100
commit6d98df1f54c6891d9847e329cb7bd84c8c9f3221 (patch)
tree0378c6ef359f63e1ee964445439ba3ad3c1d179f
parentec40a599449b5f1e119ef18703e5732ea1c0ee66 (diff)
handle more situations
-rw-r--r--hmm.el68
1 files changed, 39 insertions, 29 deletions
diff --git a/hmm.el b/hmm.el
index fa16699..92b1fe0 100644
--- a/hmm.el
+++ b/hmm.el
@@ -21,8 +21,9 @@
(hmm-add-command '(:command ,function-name) :query))))
(defmacro hmm-define-external-command (xdg-command)
- (let* ((description (or (plist-get xdg-command :description)
- (format "Open with %s" (plist-get xdg-command :name))))
+ (let* ((description (format "%s - %s"
+ (plist-get xdg-command :name)
+ (plist-get xdg-command :description)))
(function-name (intern
(format "hmm-%s"
(plist-get xdg-command :xdg-desktop-name))))
@@ -39,18 +40,12 @@
(concat ,name " " arg) nil ,command))
(hmm-add-command ',handler :file))))
-;; (defmacro hmm-define-file-url-handler-from-file-handler (handler)
-;; (let ((function-name (intern
-;; (format "hmm-file-url-%s")))))
-;; `(progn
-;; (defun hmm-file-url)))
-
(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-thing-in-region (from-to)
+(defun hmm-thing-in-region (from to)
(interactive "r")
(unless (region-active-p) (error "Region not active."))
(let* ((query (buffer-substring from to)))
@@ -66,7 +61,27 @@ If it is a url, display a list of url handlers."
(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)))))
+ ((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."))))
+
+(defun hmm ()
+ (interactive)
+ (cond ((region-active-p)
+ (call-interactively 'hmm-thing-in-region))
+ (t (call-interactively 'hmm-thing-at-point))))
+
+(define-key global-map (kbd "C-M-<return>") 'hmm)
(defun hmm-query (query)
(interactive "sQuery: ")
@@ -98,16 +113,18 @@ If it is a url, display a list of url handlers."
(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)
- (or (member (hmm-file-mime-type thing)
+ (let ((mimetype (hmm-file-mime-type thing)))
+ (mapcar
+ (lambda (handler)
+ (plist-get handler :command))
+ (seq-filter
+ (lambda (handler)
+ (and (string-match (or (plist-get handler :regex) ".*") thing)
+ (or (member mimetype
(plist-get handler :mimetypes))
- (not (plist-get handler :xdg-desktop-name))))
- (plist-get hmm-commands type))))
+ (and (null (plist-get handler :mimetypes))
+ (not (plist-get handler :xdg-desktop-name))))))
+ (plist-get hmm-commands type)))))
(defun hmm-update-commands ()
(interactive)
@@ -157,15 +174,8 @@ If it is a url, display a list of url handlers."
(when (file-exists-p file)
(replace-regexp-in-string
"^.*: \\(.*\\); .*$" "\\1"
- (hmm-shell-command-output (format "file -Li %s" file)))))
-
-(defun hmm-shell-command-output (command)
- (let ((inhibit-message t))
- (if (= 0
- (shell-command command))
- (with-current-buffer shell-command-buffer-name
- (string-trim (buffer-string)))
- (error (with-current-buffer shell-command-buffer-name
- (string-trim (buffer-string)))))))
+ (with-temp-buffer
+ (call-process "file" nil '(t t) nil "-Li" file)
+ (string-trim (buffer-string))))))
(provide 'hmm)