Hammerspoon - add bindings
[dotfiles.git] / hammerspoon / init.lua
1 -- Sample Hello World 
2 hs.hotkey.bind({"cmd", "alt", "ctrl"}, "W", function()
3   hs.notify.new({title="Hammerspoon", informativeText="Hello World"}):send()
4 --  hs.alert.show("Hello World!")
5 end)
6
7 -- Reload configuration
8 hs.hotkey.bind({"cmd", "alt", "ctrl"}, "R", function()
9   hs.reload()
10 end)
11 hs.alert.show("Config loaded")
12
13 ---- Right click 
14 --hs.hotkey.bind({"cmd,", "alt", "ctrl"}, "l", function()
15 --      hs.eventtap.rightClick(hs.mouse.getAbsolutePosition())
16 --end)
17
18 -- Donw : For firefox vimperator
19 hs.hotkey.bind({"cmd,", "ctrl"}, "n", function()
20       hs.eventtap.keyStroke({}, 'down')
21 end)
22
23 -- Up
24 hs.hotkey.bind({"cmd,", "ctrl"}, "p", function()
25       hs.eventtap.keyStroke({}, 'up')
26 end)
27
28 -- Reacting to application events
29 -- Move all Finder in front
30 function applicationWatcher(appName, eventType, appObject)
31     if (eventType == hs.application.watcher.activated) then
32         if (appName == "Finder") then
33             -- Bring all Finder windows forward when one gets activated
34             appObject:selectMenuItem({"Window", "Bring All to Front"})
35         end
36     end
37 end
38 appWatcher = hs.application.watcher.new(applicationWatcher)
39 appWatcher:start()
40
41
42 -- Window Movement
43 -- https://andrich.blog/2016/11/20/hammerspoon-an-awesome-tool-to-automate-your-mac/
44 -- CTRL + ALT + Left - Move current window to the left half of the screen.
45 -- CTRL + ALT + Right - Move current window to the right half of the screen.
46 -- CTRL + ALT + Up - Go "fullscreen".
47 -- CTRL + ALT + Down - Center window, covering 2/3 of screen size.
48 --
49  
50 function move_window(direction)
51     return function()
52         local win      = hs.window.focusedWindow()
53         local app      = win:application()
54         local app_name = app:name()
55         local f        = win:frame()
56         local screen   = win:screen()
57         local max      = screen:frame()
58  
59         if direction == "left" then
60             f.x = max.x
61             f.y = max.y
62             f.w = max.w / 2
63             f.h = max.h
64         elseif direction == "right" then
65             f.x = max.x + (max.w / 2)
66             f.y = max.y
67             f.w = max.w / 2
68             f.h = max.h
69         elseif direction == "up" then
70             f.x = max.x
71             f.y = max.y 
72             f.w = max.w
73             f.h = max.h / 2
74         elseif direction == "down" then
75             f.x = max.x 
76             f.y = max.y + (max.h / 2)
77             f.w = max.w 
78             f.h = max.h / 2
79         elseif direction == "max" then
80            if f.w > (max.w * 3/4) and f.h > (max.h * 3/4) then
81                 f.x = max.x + (max.w / 6)
82                 f.y = max.y + (max.h / 6)
83                 f.w = max.w * 2 / 3
84                 f.h = max.h * 2 / 3
85            else
86                 f.x = max.x 
87                 f.y = max.y
88                 f.w = max.w 
89                 f.h = max.h
90            end
91         else
92             hs.alert.show("move_window(): Freaky parameter received " .. direction)
93         end
94  
95         win:setFrame(f, 0)
96     end
97 end
98  
99 local hyper = {"ctrl", "cmd"}
100 hs.hotkey.bind(hyper, "h", move_window("left"))
101 hs.hotkey.bind(hyper, "l", move_window("right"))
102 hs.hotkey.bind(hyper, "k", move_window("up"))
103 hs.hotkey.bind(hyper, "j", move_window("down"))
104 hs.hotkey.bind(hyper, "m", move_window("max"))
105
106 -- Search and dictionary
107 -- Past and copy
108 -- App quick startup
109
110 -- Use SpoonInstall,
111 -- http://zzamboni.org/post/using-spoons-in-hammerspoon/
112 -- https://github.com/Hammerspoon/Spoons/raw/master/Spoons/SpoonInstall.spoon.zip
113 if hs.spoons == nil then
114    hs.spoons=require('hs.spoons')
115 end
116
117
118 hs.loadSpoon ("SpoonInstall")
119 spoon.SpoonInstall.use_syncinstall = true
120 INSTALL = spoon.SpoonInstall
121
122 --------------------------------------------------------------------------------
123 -- MouseCircle  http://www.hammerspoon.org/Spoons/MouseCircle.html
124 INSTALL:andUse("MouseCircle",
125                 {
126                    disable = false,
127                    config = {
128                       color = hs.drawing.color.x11.rebeccapurple
129                    },
130                    hotkeys = {
131                       show = { {"cmd", "alt", "ctrl"}, "m"}
132                    }
133                 }
134 )
135
136 --------------------------------------------------------------------------------
137 -- TextClipboardHistory
138 INSTALL:andUse("TextClipboardHistory",
139                {
140                   config = {
141                      show_in_menubar = false,
142                      hist_size = 500,
143                   },
144                   hotkeys = {
145                      toggle_clipboard = { { "cmd", "shift" }, "v"}
146                   },
147                   start = true;
148                }
149 )
150
151
152 --------------------------------------------------------------------------------
153 -- Seal -- http://www.hammerspoon.org/Spoons/Seal.html
154 INSTALL:andUse("Seal",
155                {
156                   hotkeys = { show = { {"cmd", "ctrl", "alt"}, "space" } },
157                   fn = function(s)
158                      s:loadPlugins({"apps", "calc", "safari_bookmarks", "screencapture", "useractions"})
159                      s.plugins.safari_bookmarks.always_open_with_safari = false
160                      s.plugins.useractions.actions =
161                         {
162                            ["Hammerspoon docs webpage"] = {
163                               url = "http://hammerspoon.org/docs/",
164                               icon = hs.image.imageFromName(hs.image.systemImageNames.ApplicationIcon),
165                               -- hotkey = { hyper, "h" }
166                            },
167                            ["Leave corpnet"] = {
168                               fn = function()
169                                  spoon.WiFiTransitions:processTransition('foo', 'corpnet01')
170                               end,
171                               icon = swisscom_logo,
172                            },
173                            ["Arrive in corpnet"] = {
174                               fn = function()
175                                  spoon.WiFiTransitions:processTransition('corpnet01', 'foo')
176                               end,
177                               icon = swisscom_logo,
178                            },
179                            ["Translate using Leo"] = {
180                               url = "http://dict.leo.org/ende/index_de.html#/search=${query}",
181                               icon = 'favicon',
182                               keyword = "leo",
183                            },
184                            ["Tell me something"] = {
185                               keyword = "tellme",
186                               fn = function(str) hs.alert.show(str) end,
187                            }
188                         }
189                      s:refreshAllCommands()
190                   end,
191                   start = true,
192                }
193 )