forked from rschmukler/doom.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path+functions.el
188 lines (167 loc) · 6.86 KB
/
+functions.el
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
;;; private/rschmukler/functions.el -*- lexical-binding: t; -*-
(defun rschmukler/neotree-project-root-dir-or-current-dir ()
"Open NeoTree using the project root, using projectile, or the
current buffer directory."
(interactive)
(let ((project-dir (ignore-errors (projectile-project-root)))
(file-name (buffer-file-name))
(neo-smart-open t))
(if (neo-global--window-exists-p)
(neotree-hide)
(progn
(neotree-show)
(if project-dir
(neotree-dir project-dir))
(if file-name
(neotree-find file-name))))))
(defun wc/switch-to-mru-buffer ()
"Switches to the most recently used buffer, including visible buffers."
(interactive)
(setq current-buffer-name (buffer-name (current-buffer)))
(setq buffer-candidates (remove-if #'(lambda (buffer) (string-match-p current-buffer-name (buffer-name buffer))) (buffer-list)))
(wc/do-switch-to-mru-buffer buffer-candidates))
(defun wc/do-switch-to-mru-buffer (buffer-candidates)
(setq buffer-candidate (car buffer-candidates))
(setq rest (cdr buffer-candidates))
(if (string-match-p current-buffer-name (buffer-name buffer-candidate))
(wc/do-switch-to--buffer rest)
(if (eq 0 (list-length buffer-candidates))
(message "No more buffer candidates.")
(if (wc/file-buffer-p buffer-candidate)
(switch-to-buffer buffer-candidate)
(wc/do-switch-to-mru-buffer rest)))))
(defun wc/file-buffer-p (buffer-candidate)
"Returns t if the buffer argument is backed by a file and is therefore presumably a code buffer."
(interactive)
(let ((buff-name (buffer-name buffer-candidate))
(buff-mode (wc/buffer-major-mode buffer-candidate)))
(not (or (string-match-p "*" buff-name)
(member buff-mode '(neotree-mode dired-mode))))))
(defun wc/buffer-major-mode (buffer-handle)
"Returns a buffer's active major-mode."
(with-current-buffer buffer-handle major-mode))
(defun wpc/find-or-create-clojure-or-clojurescript-repl ()
(interactive)
(require 'projectile)
(with-current-buffer (current-buffer)
(let ((buffer-name (wpc/buffer-name-for-clojure-mode major-mode))
(repl-function (wpc/repl-function-for-clojure-mode major-mode)))
(if (get-buffer buffer-name)
(switch-to-buffer buffer-name)
(funcall repl-function)))))
(defun wpc/buffer-name-for-clojure-mode (mode)
(require 'projectile)
(let* ((project-name (projectile-project-name))
(cljs-name (concat "*cider-repl CLJS " project-name "*"))
(clj-name (concat "*cider-repl " project-name "*")))
(cond ((eq mode 'clojurescript-mode) cljs-name)
((eq mode 'clojure-mode) clj-name)
((eq mode 'clojurec-mode) cljs-name))))
(defun wpc/repl-function-for-clojure-mode (mode)
(require 'projectile)
(let ((project-name (projectile-project-name))
(cljs-fn #'cider-jack-in-clojurescript)
(clj-fn #'cider-jack-in))
(cond ((eq mode 'clojurescript-mode) cljs-fn)
((eq mode 'clojure-mode) clj-fn)
((eq mode 'clojurec-mode) cljs-fn))))
(defun wpc/reindent-defun-and-align-clojure-map ()
(interactive)
(call-interactively #'paredit-reindent-defun)
(call-interactively #'clojure-align))
(defun rs/projectile-switch-project-workspace ()
"Use projectile prompt to find or switch projects in a workspace tab."
(interactive)
(require 'projectile)
(ivy-read
(projectile-prepend-project-name "Switch to project: ") projectile-known-projects
:preselect (and (projectile-project-p)
(abbreviate-file-name (projectile-project-root)))
:action
(lambda (project-path)
(let ((project-name
(file-name-nondirectory
(directory-file-name (file-name-directory project-path)))
))
(progn
(if (+workspace-exists-p project-name)
(+workspace-switch project-name)
(progn (+workspace-switch project-name t)
(counsel-projectile-switch-project-action project-path)))
(+tmux/run (concat "tt " project-name)))))))
(defun urbint/format-haskell-source ()
(interactive)
(let ((output-buffer (generate-new-buffer "brittany-out"))
(config-file-path
(concat (string-trim
(shell-command-to-string "stack path --project-root"))
"/brittany.yaml")))
(when (= 0 (call-process-region
(point-min) (point-max)
"stack"
nil output-buffer nil
"exec" "--" "brittany" "--config-file" config-file-path))
(let ((pt (point))
(wst (window-start))
(formatted-source (with-current-buffer output-buffer
(buffer-string))))
(erase-buffer)
(insert formatted-source)
(goto-char pt)
(set-window-start nil wst)))))
(defun empire/haskell/module->test ()
"Jump from a module to a test."
(let ((filename (->> buffer-file-name
(s-replace "/src/" "/test/")
(s-replace ".hs" "Test.hs")
find-file)))
(make-directory (f-dirname filename) t)
(find-file filename)))
(defun empire/haskell/test->module ()
"Jump from a test to a module."
(let ((filename (->> buffer-file-name
(s-replace "/test/" "/src/")
(s-replace "Test.hs" ".hs")
)))
(make-directory (f-dirname filename) t)
(find-file filename)))
(defun empire/haskell/test<->module ()
"Toggle between test and module in Haskell."
(interactive)
(if (s-contains? "/src/" buffer-file-name)
(empire/haskell/module->test)
(empire/haskell/test->module)))
(defmacro define-move-and-insert
(name &rest body)
`(defun ,name (count &optional vcount skip-empty-lines)
;; Following interactive form taken from the source for `evil-insert'
(interactive
(list (prefix-numeric-value current-prefix-arg)
(and (evil-visual-state-p)
(memq (evil-visual-type) '(line block))
(save-excursion
(let ((m (mark)))
;; go to upper-left corner temporarily so
;; `count-lines' yields accurate results
(evil-visual-rotate 'upper-left)
(prog1 (count-lines evil-visual-beginning evil-visual-end)
(set-mark m)))))
(evil-visual-state-p)))
(atomic-change-group
,@body
(evil-insert count vcount skip-empty-lines))))
(define-move-and-insert grfn/insert-at-sexp-end
(when (not (equal (get-char) "("))
(backward-up-list))
(forward-sexp)
(backward-char))
(define-move-and-insert grfn/insert-at-sexp-start
(backward-up-list)
(forward-char))
(define-move-and-insert grfn/insert-at-form-start
(backward-sexp)
(backward-char)
(insert " "))
(define-move-and-insert grfn/insert-at-form-end
(forward-sexp)
(insert " "))