-
Notifications
You must be signed in to change notification settings - Fork 70
/
core.fnl
238 lines (200 loc) · 6.97 KB
/
core.fnl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
(hs.ipc.cliInstall) ; ensure CLI installed
(local fennel (require :fennel))
(require :lib.globals)
(local {:contains? contains?
:for-each for-each
:map map
:merge merge
:reduce reduce
:split split
:some some} (require :lib.functional))
(local atom (require :lib.atom))
(require-macros :lib.macros)
(require-macros :lib.advice.macros)
;; Add compatability with spoons as the spoon global may not exist at
;; this point until a spoon is loaded. It will exist if a spoon is
;; loaded from init.lua
(global spoon (or _G.spoon {}))
;; Make ~/.spacehammer folder override repo files
(local homedir (os.getenv "HOME"))
(local customdir (.. homedir "/.spacehammer"))
(tset fennel :path (.. customdir "/?.fnl;" fennel.path))
(local log (hs.logger.new "\tcore.fnl\t" "debug"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; defaults
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(set hs.hints.style :vimperator)
(set hs.hints.showTitleThresh 4)
(set hs.hints.titleMaxSize 10)
(set hs.hints.fontSize 30)
(set hs.window.animationDuration 0.2)
"
alert :: str, { style }, seconds -> nil
Shortcut for showing an alert on the primary screen for a specified duration
Takes a message string, a style table, and the number of seconds to show alert
Returns nil. This function causes side-effects.
"
(global alert
(afn
alert
[str style seconds]
"
Global alert function used for spacehammer modals and reload
alerts after config reloads
"
(hs.alert.show str
style
(hs.screen.primaryScreen)
seconds)))
(global fw hs.window.focusedWindow)
(global pprint (fn [x] (print (fennel.view x))))
(global get-config
(afn get-config
[]
"
Returns the global config object, or error if called early
"
(error "get-config can only be called after all modules have initialized")))
(fn file-exists?
[filepath]
"
Determine if a file exists and is readable.
Takes a file path string
Returns true if file is readable
"
(let [file (io.open filepath "r")]
(when file
(io.close file))
(~= file nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; create custom config file if it doesn't exist
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(fn copy-file
[source dest]
"
Copies the contents of a source file to a destination file.
Takes a source file path and a destination file path.
Returns nil
"
(let [default-config (io.open source "r")
custom-config (io.open dest "a")]
(each [line _ (: default-config :lines)]
(: custom-config :write (.. line "\n")))
(: custom-config :close)
(: default-config :close)))
;; If ~/.spacehammer/config.fnl does not exist
;; - Create ~/.spacehammer dir
;; - Copy default ~/.hammerspoon/config.example.fnl to ~/.spacehammer/config.fnl
(let [example-path (.. hs.configdir "/config.example.fnl")
target-path (.. customdir "/config.fnl")]
(when (not (file-exists? target-path))
(log.d (.. "Copying " example-path " to " target-path))
(hs.fs.mkdir customdir)
(copy-file example-path target-path)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; auto reload config
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(fn source-filename?
[file]
"
Determine if a file is not an emacs backup file which starts with \".#\"
Takes a file path string
Returns true if it's a source file and not an emacs backup file.
"
(not (string.match file ".#")))
(fn source-extension?
[file]
"
Determine if a file is a .fnl or .lua file
Takes a file string
Returns true if file extension ends in .fnl or .lua
"
(let [ext (split "%p" file)]
(and
(or (contains? "fnl" ext)
(contains? "lua" ext))
(not (string.match file "-test%..*$")))))
(fn source-updated?
[file]
"
Determine if a file is a valid source file that we can load
Takes a file string path
Returns true if file is not an emacs backup and is a .fnl or .lua type.
"
(and (source-filename? file)
(source-extension? file)))
(fn config-reloader
[files]
"
If the list of files contains some hammerspoon or spacehammer source files:
reload hammerspoon
Takes a list of files from our config file watcher.
Performs side effect of reloading hammerspoon.
Returns nil
"
(when (some source-updated? files)
(hs.console.clearConsole)
(hs.reload)))
(fn watch-files
[dir]
"
Watches hammerspoon or spacehammer source files. When a file updates we reload
hammerspoon.
Takes a directory to watch.
Returns a function to stop the watcher.
"
(let [watcher (hs.pathwatcher.new dir config-reloader)]
(: watcher :start)
(fn []
(: watcher :stop))))
;; Create a global config-files-watcher. Calling it stops the default watcher
(global config-files-watcher (watch-files hs.configdir))
(when (file-exists? (.. customdir "/config.fnl"))
(global custom-files-watcher (watch-files customdir)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set utility keybindings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; toggle hs.console with Ctrl+Cmd+~
(hs.hotkey.bind
[:ctrl :cmd] "`" nil
(fn []
(if-let
[console (hs.console.hswindow)]
(when (= console (hs.console.hswindow))
(hs.closeConsole))
(hs.openConsole))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Load custom init.fnl file (if it exists)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(let [custom-init-file (.. customdir "/init.fnl")]
(when (file-exists? custom-init-file)
(fennel.dofile custom-init-file)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Initialize core modules
;; - Requires each module
;; - Calls module.init and provides config.fnl table
;; - Stores global reference to all initialized resources to prevent garbage
;; collection.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(local config (require :config))
;; Initialize our modules that depend on config
(local modules [:lib.hyper
:vim
:windows
:apps
:lib.bind
:lib.modal
:lib.apps])
(defadvice get-config-impl
[]
:override get-config
"Returns global config obj"
config)
;; Create a global reference so services like hs.application.watcher
;; do not get garbage collected.
(global resources
(->> modules
(map (fn [path]
(let [module (require path)]
{path (module.init config)})))
(reduce #(merge $1 $2) {})))