aboutsummaryrefslogtreecommitdiffstats
path: root/hammerspoon
diff options
context:
space:
mode:
Diffstat (limited to 'hammerspoon')
-rw-r--r--hammerspoon/.hammerspoon/init.fnl120
-rw-r--r--hammerspoon/.hammerspoon/init.lua6
2 files changed, 126 insertions, 0 deletions
diff --git a/hammerspoon/.hammerspoon/init.fnl b/hammerspoon/.hammerspoon/init.fnl
new file mode 100644
index 0000000..fa9045f
--- /dev/null
+++ b/hammerspoon/.hammerspoon/init.fnl
@@ -0,0 +1,120 @@
+(fn show-window-information []
+ (let [window (hs.window.focusedWindow)]
+ (if (= window nil)
+ (hs.alert.show "no window")
+ (hs.alert.show (.. "Title: " (window:title) "\n"
+ "Application: " (: (window:application) :name))))))
+
+(hs.hotkey.bind ["cmd" "alt" "ctrl"] "e" show-window-information)
+(hs.hotkey.bind ["cmd" "shift"] "r" hs.reload)
+(hs.hotkey.bind ["cmd"] "l" hs.caffeinate.startScreensaver)
+
+(fn make-config-path-watcher []
+ (hs.pathwatcher.new
+ (.. (os.getenv "HOME")
+ "/code/personal/dotfiles/hammerspoon/.hammerspoon")
+ (fn [] (print "Reload!") (hs.reload))))
+
+(let [watcher (make-config-path-watcher)]
+ (watcher:start))
+
+;;; Layout -- Automatically put windows in their place.
+
+(fn apply-layout []
+ (let [my-screen (hs.screen.primaryScreen)
+ screen-mode (: my-screen :currentMode)
+ geom [(. screen-mode "w") (. screen-mode "h")]
+ max (. hs "layout" "maximized")]
+ (case geom
+ [3440 1440] (hs.layout.apply
+ [["Emacs" nil nil (. hs "layout" "left70") nil nil]
+ ["WezTerm" nil nil (. hs "layout" "right30") nil nil]
+ ["Glide" nil nil max nil nil]])
+
+ [5120 1440] (hs.layout.apply
+ [["Glide" nil nil (. hs "layout" "left25") nil nil]
+ ["Emacs" nil nil (hs.geometry.rect 0.25 0 0.5 1) nil nil]
+ ["WezTerm" nil nil (. hs "layout" "right25") nil nil]])
+
+ _ (hs.layout.apply
+ [["Glide" nil nil max nil nil]
+ ["Emacs" nil nil max nil nil]
+ ["WezTerm" nil nil max nil nil]]))))
+
+(fn make-application-watcher []
+ (fn match-application [name event-type application]
+ (when (and (= event-type (. hs :application :watcher :launched))
+ (or (= name "Emacs")
+ (= name "WezTerm")
+ (= name "Glide")))
+ (apply-layout)))
+ (hs.application.watcher.new match-application))
+
+(fn make-screen-watcher []
+ (hs.screen.watcher.new apply-layout))
+
+(apply-layout)
+
+(let [watcher (make-application-watcher)]
+ (watcher:start))
+(let [watcher (make-screen-watcher)]
+ (watcher:start))
+
+;;; Mouse -- Automatically set primary button based on mouse connected.
+
+(fn set-mouse-buttons! [swap?]
+ (let [task (hs.task.new
+ "/usr/bin/defaults" nil []
+ ["-currentHost"
+ "write"
+ ".GlobalPreferences"
+ "com.apple.mouse.swapLeftRightButton"
+ "-bool"
+ (tostring swap?)])]
+ (task:start)
+ (task:waitUntilExit))
+ (print (.. "Changed mouse button to: " (if swap? "left" "right"))))
+
+(fn make-mouse-watcher []
+ (fn match-device [{:eventType event-type :productName name}]
+ (let [connected? (= event-type "added")]
+ (print (.. "Device " (if connected? "" "dis") "connected: " name))
+ (when (= name "USB Optical Mouse")
+ (set-mouse-buttons! connected?))))
+ (hs.usb.watcher.new match-device))
+
+(fn mouse-connected? []
+ (fn find-mouse-device [devices]
+ (if (= (next devices) nil)
+ false
+ (let [device (table.remove devices 1)]
+ (if (= (. device "productName") "USB Optical Mouse")
+ true
+ (tail! (find-mouse-device devices))))))
+ (find-mouse-device (hs.usb.attachedDevices)))
+
+(set-mouse-buttons! (mouse-connected?))
+
+(let [watcher (make-mouse-watcher)]
+ (watcher:start))
+
+;;; Fennel Repl
+;; From https://linktohack.com/posts/a-fennel-repl-for-hammer-spoon/
+
+(local fennel (require :fennel))
+
+(let [coro (coroutine.create (. fennel :repl :repl))]
+ (coroutine.resume coro {:readChunk (fn []
+ (let [input (coroutine.yield)]
+ (.. input "\n")))
+ :onValues (fn [xs]
+ (print (table.concat xs "\t")))
+ :onError (fn [_ msg]
+ (print msg))})
+ (set hs._consoleInputPreparser (fn [s]
+ (coroutine.resume coro s)
+ "")))
+
+;;; Done
+
+(hs.alert.show "Loaded!" [] (hs.screen.primaryScreen) 0.5)
diff --git a/hammerspoon/.hammerspoon/init.lua b/hammerspoon/.hammerspoon/init.lua
new file mode 100644
index 0000000..b21f91a
--- /dev/null
+++ b/hammerspoon/.hammerspoon/init.lua
@@ -0,0 +1,6 @@
+if hs.fs.displayName("fennel.lua") == nil then
+ print("Downloading fennel")
+ os.execute("curl -o fennel.lua https://fennel-lang.org/downloads/fennel-1.6.1.lua")
+end
+
+require("fennel").install().dofile('init.fnl')