-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.fnl
More file actions
159 lines (126 loc) · 5.4 KB
/
core.fnl
File metadata and controls
159 lines (126 loc) · 5.4 KB
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
(hs.ipc.cliInstall)
; ensure CLI installed
(local fennel (require :fennel))
(require :lib.globals)
(local {: contains? : for-each : map : merge : reduce : split : 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 ~/.hammerspoon folder override repo files
(local homedir (os.getenv :HOME))
(local customdir (.. homedir :/.hammerspoon))
(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]
(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))
(not= 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)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; auto reload config
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(fn source-filename? [file]
(not (string.match file ".#")))
; if starts with \".#\" then it's an emacs backup file
(fn source-extension? [file]
(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)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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) {})))