hammerspoon local-ization
change
commit 8b0da080148f3c3be6ae16ea26580ae4bdbec0d6
author Alpha Chen <alpha@kejadlen.dev>
date
parent e0c3ebbc
diff --git a/.hammerspoon/handle-urls.fnl b/.hammerspoon/handle-urls.fnl
new file mode 100644
index 00000000..632b4dfb
--- /dev/null
+++ b/.hammerspoon/handle-urls.fnl
@@ -0,0 +1,63 @@
+;; https://stackoverflow.com/questions/39464668/how-to-get-bundle-id-of-mac-application
+;;   osascript -e 'id of app "SomeApp"'
+;;   mdls -name kMDItemCFBundleIdentifier -r SomeApp.app
+;; (. (hs.application.infoForBundlePath "/path/to/app") :CFBundleIdentifier)
+
+(local bundle-ids {:firefox-dev :org.mozilla.firefoxdeveloperedition
+                   :firefox :org.mozilla.firefox
+                   :safari :com.apple.Safari
+                   :zoom :us.zoom.xos})
+
+(var handlers [])
+
+(fn run [bin url]
+  (let [prog (.. bin " '" url "'")
+        handle (io.popen prog)]
+    (handle:read :*a)))
+
+(fn de-utm [url]
+  (run "~/.dotfiles/bin/de-utm" url))
+
+(fn follow-redirects [url]
+  (run "curl --location --silent -o /dev/null -w %{url_effective}" url))
+
+(fn open-url [app url]
+  (let [bundle-id (. bundle-ids app)]
+    (hs.urlevent.openURLWithBundle url bundle-id)))
+
+(fn open-with [app]
+  (partial open-url app))
+
+(fn url->handler [url]
+  (accumulate [acc nil _ [pat handler] (ipairs handlers) &until acc]
+    (if (url:find pat) #(handler url) nil)))
+
+(fn handle-url [url]
+  (let [handler (or (url->handler url) #(open-url :default url))]
+    (handler)))
+
+(fn setup [{:bundle-ids extra-bundle-ids :handlers extra-handlers}]
+  (let [default-handlers [["^https://.*%.?zoom.us/j/%d+" (open-with :zoom)]
+                          ["^https://doi.org/"
+                           #(open-url :default (.. "https://sci-hub.st/" $1))]]]
+    (each [k v (pairs (or extra-bundle-ids {}))]
+      (tset bundle-ids k v))
+    (set handlers (hs.fnutils.concat extra-handlers default-handlers))))
+
+(fn start []
+  (set hs.urlevent.httpCallback
+       (fn [scheme host params url]
+         (let [clean-url (de-utm url)]
+           (handle-url clean-url)))))
+
+;; example usage
+;;
+;; (let [{: setup : start : open-with} (require :handle-urls)]
+;;   (setup {:bundle-ids {:default :org.mozilla.firefoxdeveloperedition}
+;;           :handlers [["^https://.*%.?discnw.org/" (open-with :safari)]
+;;                      ["^https://squareup.com/" (open-with :safari)]
+;;                      ["^https://.*%.?bulletin.com/" (open-with :safari)]]})
+;;   (start))
+
+{: setup : start : open-url : open-with}
+
diff --git a/.hammerspoon/hotkey.fnl b/.hammerspoon/hotkey.fnl
index 59810df9..11b76549 100644
--- a/.hammerspoon/hotkey.fnl
+++ b/.hammerspoon/hotkey.fnl
@@ -1,9 +1,15 @@
+(local mash [:cmd :alt :ctrl])
+(local smash [:shift :cmd :alt :ctrl])
+
 (fn modal-bind [mods key message bindings]
   (let [modal (hs.hotkey.modal.new mods key message)]
-    (tset modal :entered #(hs.timer.doAfter 0.5 #(modal:exit)))
-    ; (tset modal :exited #(hs.alert :exited))
+    (tset modal :entered #(hs.timer.doAfter 1 #(modal:exit)))
+    ;; (tset modal :exited #(hs.alert :bye))
     (each [_ [mods key message f] (ipairs bindings)]
-      (modal:bind mods key message #(do (f) (modal:exit))))))
+      (modal:bind mods key message
+                  #(do
+                     (f)
+                     (modal:exit))))))
 
-{:mash [:cmd :alt :ctrl] :smash [:cmd :alt :ctrl :shift] : modal-bind}
+{: mash : smash : modal-bind}
 
diff --git a/.hammerspoon/init.fnl b/.hammerspoon/init.fnl
index fb979f11..23b1db05 100644
--- a/.hammerspoon/init.fnl
+++ b/.hammerspoon/init.fnl
@@ -1,8 +1,8 @@
+(local {: mash : smash : modal-bind} (require :hotkey))
+
 (local log (hs.logger.new :log :info))
 (set hs.logger.defaultLogLevel :info)
 
-(local {: mash : smash : modal-bind} (require :hotkey))
-
 (set hs.window.animationDuration 0.0)
 (hs.loadSpoon :MiroWindowsManager)
 ;; (set spoon.MiroWindowsManager.fullScreenSizes [1 (/ 4 3) 2]) ; only fullscreen
@@ -59,41 +59,7 @@
     (hs.eventtap.keyStroke [:cmd] :v)
     (hs.pasteboard.setContents prev-pasteboard)))
 
-(modal-bind mash "," "," [[mash :l :linkify linkify]])
-
-;; https://stackoverflow.com/questions/39464668/how-to-get-bundle-id-of-mac-application
-;;   osascript -e 'id of app "SomeApp"'
-;;   mdls -name kMDItemCFBundleIdentifier -r SomeApp.app
-
-;; fnlfmt: skip
-(local bundle-ids {:firefox :org.mozilla.firefoxdeveloperedition
-                   :firefox :org.mozilla.firefox
-                   :safari  :com.apple.Safari
-                   :zoom    :us.zoom.xos})
-
-(fn handle [url orig-url]
-  (let [open-with (fn [app]
-                    (partial #(hs.urlevent.openURLWithBundle $2 $1)
-                             (. bundle-ids app)))
-        open-url #((open-with $1) url)]
-    (if (url:find "^https://.*[.]zoom.us/j/%d+") (open-url :zoom)
-        (url:find "^https://.*[.]discnw.org/") (open-url :safari)
-        (url:find "^https://squareup.com/") (open-url :safari)
-        (url:find "^https://.*[.]bulletin.com/") (open-url :safari)
-        (string.find orig-url "^https://doi.org/")
-        ((open-with :firefox) (.. "https://sci-hub.st/" orig-url))
-        (string.find orig-url "^https://.*[.]usps.com/")
-        ((open-with :firefox) orig-url) (open-url :safari))))
-
-(set hs.urlevent.httpCallback
-     (fn [scheme host params url]
-       (let [run #(: (io.popen (.. $1 " '" $2 "'")) :read :*a)
-             de-utm (partial run "~/.dotfiles/bin/de-utm")
-             redirect (partial run "curl -Ls -o /dev/null -w %{url_effective}")
-             orig-url (de-utm url)
-             redirected (de-utm (redirect orig-url))]
-         ;; (hs.urlevent.openURLWithBundle orig-url bundle-ids.firefox))))
-         (handle redirected orig-url))))
+(modal-bind mash "," nil [[mash :l nil linkify]])
 
 ;;; Elgato Key Light Air