forked from agzam/spacehammer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.fnl
213 lines (181 loc) · 6.29 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
;; Copyright (c) 2017-2020 Ag Ibragimov & Contributors
;;
;;; Author: Ag Ibragimov <agzam.ibragimov@gmail.com>
;;
;;; Contributors:
;; Jay Zawrotny <jayzawrotny@gmail.com>
;;
;;; URL: https://github.com/agzam/spacehammer
;;
;;; License: MIT
;;
(hs.ipc.cliInstall) ; ensure CLI installed
(local fennel (require :fennel))
(local {:contains? contains?
:for-each for-each
:map map
:merge merge
:reduce reduce
:split split
:some some} (require :lib.functional))
(require-macros :lib.macros)
;; 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 (fn
[str style seconds]
(hs.alert.show str
style
(hs.screen.primaryScreen)
seconds)))
(global fw hs.window.focusedWindow)
(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.fnl to ~/.spacehammer/config.fnl
(when (not (file-exists? (.. customdir "/config.fnl")))
(log.d "Copying ~/.hammerspoon/config.fnl to ~/.spacehammer/config.fnl")
(hs.fs.mkdir customdir)
(copy-file (.. homedir "/.hammerspoon/config.fnl")
(.. customdir "/config.fnl")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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)]
(or (contains? "fnl" ext)
(contains? "lua" ext))))
(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
:lib.bind
:lib.modal
:lib.apps])
;; 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) {})))