-
Notifications
You must be signed in to change notification settings - Fork 70
/
emacs.fnl
125 lines (116 loc) · 4.58 KB
/
emacs.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
(fn emacsclient-exe []
"Locate emacsclient executable."
(-> "Emacs"
hs.application.find
(: :path)
(: :gsub "Emacs.app" "bin/emacsclient")))
(fn capture [is-note]
"Activates org-capture"
(let [key (if is-note "\"z\"" "")
current-app (hs.window.focusedWindow)
pid (.. "\"" (: current-app :pid) "\" ")
title (.. "\"" (: current-app :title) "\" ")
run-str (..
(emacsclient-exe)
" -c -F '(quote (name . \"capture\"))'"
" -e '(spacehammer-activate-capture-frame "
pid title key " )' &")
timer (hs.timer.delayed.new .1 (fn [] (io.popen run-str)))]
(: timer :start)))
(fn edit-with-emacs []
"Executes emacsclient, evaluating a special elisp function in spacehammer.el
(it must be pre-loaded), passing PID, title and display-id of the caller."
(let [current-app (: (hs.window.focusedWindow) :application)
pid (.. "\"" (: current-app :pid) "\"")
title (.. "\"" (: current-app :title) "\"")
screen (.. "\"" (: (hs.screen.mainScreen) :id) "\"")
run-str (..
(emacsclient-exe)
" -e '(spacehammer-edit-with-emacs "
pid " " title " " screen " )' &")
prev (hs.pasteboard.changeCount)
_ (hs.eventtap.keyStroke [:cmd] :c)
next (hs.pasteboard.changeCount)]
(when (= prev next) ; Pasteboard was not updated so no text was selected
(hs.eventtap.keyStroke [:cmd] :a) ; select all and then copy
(hs.eventtap.keyStroke [:cmd] :c))
(io.popen run-str)
(hs.application.open :Emacs)))
(fn run-emacs-fn
[elisp-fn args]
"Executes given elisp function in emacsclient. If args table present, passes
them into the function."
(let [args-lst (when args (.. " '" (table.concat args " '")))
run-str (.. (emacsclient-exe)
" -e \"(funcall '" elisp-fn
(if args-lst args-lst " &")
")\" &")]
(io.popen run-str)))
(fn full-screen
[]
"Switches to current instance of GUI Emacs and makes its frame fullscreen"
(hs.application.launchOrFocus :Emacs)
(run-emacs-fn
(..
"(lambda ())"
"(spacemacs/toggle-fullscreen-frame-on)"
"(spacehammer/fix-frame)")))
(fn vertical-split-with-emacs
[]
"Creates vertical split with Emacs window sitting next to the current app"
(let [windows (require :windows)
cur-app (-?> (hs.window.focusedWindow) (: :application) (: :name))
rect-left [0 0 .5 1]
rect-right [.5 0 .5 1]
elisp (.. "(lambda ()"
" (spacemacs/toggle-fullscreen-frame-off) "
" (spacemacs/maximize-horizontally) "
" (spacemacs/maximize-vertically))")]
(run-emacs-fn elisp)
(hs.timer.doAfter
.2
(fn []
(if (= cur-app :Emacs)
(do
(windows.rect rect-left)
(windows.jump-to-last-window)
(windows.rect rect-right))
(do
(windows.rect rect-right)
(hs.application.launchOrFocus :Emacs)
(windows.rect rect-left)))))))
(fn switch-to-app [pid]
"Don't remove! - this is callable from Emacs See: `spacehammer/switch-to-app`
in spacehammer.el "
(let [app (hs.application.applicationForPID (tonumber pid))]
(when app (: app :activate))))
(fn switch-to-app-and-paste-from-clipboard [pid]
"Don't remove! - this is callable from Emacs See:
`spacehammer/finish-edit-with-emacs` in spacehammer.el."
(let [app (hs.application.applicationForPID (tonumber pid))]
(when app
(: app :activate)
(hs.timer.doAfter
0.001
(fn [] (: app :selectMenuItem [:Edit :Paste]))))))
(fn maximize
[]
"Maximizes Emacs GUI window after a short delay."
(hs.timer.doAfter
1.5
(fn []
(let [app (hs.application.find :Emacs)
windows (require :windows)
modal (require :lib.modal)]
(when app
(: app :activate)
(windows.maximize-window-frame))))))
{:capture capture
:edit-with-emacs edit-with-emacs
:full-screen full-screen
:maximize maximize
:note (fn [] (capture true))
:switchToApp switch-to-app
:switchToAppAndPasteFromClipboard switch-to-app-and-paste-from-clipboard
:vertical-split-with-emacs vertical-split-with-emacs
:run-emacs-fn run-emacs-fn}