about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/wpc/functions.el
blob: d8419ad77157e01c5590c95e2fe836571123a931 (plain) (blame)
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
;; functions.el --- Helper functions for my Emacs development -*- lexical-binding: t -*-
;; Author: William Carroll <wpcarro@gmail.com>

;;; Commentary:
;; This file hopefully contains friendly APIs that making ELisp development more enjoyable.

;;; Code:
(require 'evil)
(require 'projectile)
(require 'paredit)
(require 'term)
(require 'f)
(require 'yasnippet)
(require 'ido)

(defun wpc/evil-window-vsplit-right ()
  (interactive)
  (evil-window-vsplit)
  (windmove-right))

(defun wpc/evil-window-split-down ()
  (interactive)
  (evil-window-split)
  (windmove-down))

(defun wpc/reindent-defun-and-align-clojure-map ()
  (interactive)
  (call-interactively #'paredit-reindent-defun)
  (call-interactively #'clojure-align))

(defun wpc/find-file ()
  "Prefer project-based file-finding if inside of project; otherwise gracefully fallback."
  (interactive)
  (with-current-buffer (current-buffer)
    (if (projectile-project-p)
        (call-interactively #'projectile-find-file)
      (call-interactively #'find-file))))

(defun wpc/find-or-create-js-test ()
  (->> buffer-file-name
       (s-chop-suffix ".js")
       (s-append ".test.js")
       (find-file)))

(defun wpc/find-or-create-js-module ()
  (->> buffer-file-name
       (s-chop-suffix ".test.js")
       (s-append ".js")
       (find-file)))

(defun wpc/find-or-create-js-store ()
  (->> buffer-file-name
       (s-replace "index.js" "store.js")
       (find-file)))

(defun wpc/find-or-create-js-component ()
  (->> buffer-file-name
       (s-replace "store.js" "index.js")
       (find-file)))

(defun wpc/bind-ido-keys ()
  "Adds custom KBDs for ido. This function is recommended in the ido source code."
  (define-key ido-completion-map (kbd "<tab>") #'ido-next-match)
  (define-key ido-completion-map (kbd "<backtab>") #'ido-prev-match))

(defun wpc/toggle-between-js-test-and-module ()
  "Toggle between a Javascript test or module."
  (interactive)
  (if (s-ends-with? ".test.js" buffer-file-name)
      (wpc/find-or-create-js-module)
    (if (s-ends-with? ".js" buffer-file-name)
        (wpc/find-or-create-js-test)
      (message "Not in a Javascript file. Exiting..."))))

(defun wpc/toggle-between-js-component-and-store ()
  "Toggle between a React component and its Redux store."
  (interactive)
  (if (s-ends-with? "index.js" buffer-file-name)
      (wpc/find-or-create-js-store)
    (if (or (s-ends-with? "store.js" buffer-file-name)
            (s-ends-with? "store.test.js" buffer-file-name))
        (wpc/find-or-create-js-component)
      (message "Not in a React/Redux file. Exiting..."))))

(defun wpc/read-file-as-string (filename)
  (with-temp-buffer
    (insert-file-contents filename)
    (s-trim (buffer-string))))

(defun wpc/create-snippet ()
  "Creates a window split and then opens the Yasnippet editor."
  (interactive)
  (evil-window-vsplit)
  (call-interactively #'yas-new-snippet))

(defun wpc/edit-init-el ()
  "Creates a window split and then edits the init.el file."
  (interactive)
  (evil-window-vsplit)
  (find-file "~/.emacs.d/init.el"))

(defun wpc/jump-to-parent-file ()
  "Jumps to a React store or component's parent file. Useful for store or index file."
  (interactive)
  (-> buffer-file-name
      f-dirname
      (f-join "..")
      (f-join (f-filename buffer-file-name))
      find-file))

(defun wpc/tmux-emacs-windmove (dir)
  "Move windows in a Tmux-friendly way."
  (let* ((dir->opts '((left . ("-L" . windmove-left))
                      (right . ("-R" . windmove-right))
                      (above . ("-U" . windmove-up))
                      (below . ("-D" . windmove-down))))
         (opts (alist-get dir dir->opts))
         (tmux-opt (car opts))
         (emacs-fn (cdr opts)))
    (if (window-in-direction dir)
        (funcall emacs-fn)
      (shell-command (format "tmux select-pane %s" tmux-opt)))))

(defun wpc/tmux-emacs-windmove-left ()
  (interactive)
  (wpc/tmux-emacs-windmove 'left))

(defun wpc/tmux-emacs-windmove-right ()
  (interactive)
  (wpc/tmux-emacs-windmove 'right))

(defun wpc/tmux-emacs-windmove-up ()
  (interactive)
  (wpc/tmux-emacs-windmove 'above))

(defun wpc/tmux-emacs-windmove-down ()
  (interactive)
  (wpc/tmux-emacs-windmove 'below))

(defun wpc/get-window-by-buffername (buffername)
  "Finds a window by the name of the buffer it's hosting."
  (let ((buffer (get-buffer buffername)))
    (when buffer
        (get-buffer-window buffer))))

(defun wpc/add-earmuffs (x)
  "Returns X surrounded by asterisks."
  (format "*%s*" x))

(defun wpc/get-default-shell ()
  (or explicit-shell-file-name
      (getenv "SHELL")
      (getenv "ESHELL")))

(defun wpc/find-terminal-buffer ()
  (get-buffer (wpc/add-earmuffs wpc/terminal-name)))

(defun wpc/find-terminal-window ()
  (wpc/get-window-by-buffername (wpc/add-earmuffs wpc/terminal-name)))

(defun wpc/create-terminal-session ()
  (wpc/evil-window-vsplit-right)
  (ansi-term (wpc/get-default-shell) wpc/terminal-name))

(defun wpc/toggle-terminal ()
  "Toggles a custom terminal session in Emacs."
  (interactive)
  (let ((window (wpc/find-terminal-window)))
    (if window
        (delete-window window)
      (wpc/find-or-create-terminal))))

(defun wpc/find-or-create-terminal ()
  (let ((buffer (wpc/find-terminal-buffer)))
    (if buffer
        (display-buffer buffer)
      (wpc/create-terminal-session))))

(defun wpc/put-file-name-on-clipboard ()
  "Put the current file name on the clipboard"
  (interactive)
  (let ((filename (if (equal major-mode 'dired-mode)
                      default-directory
                    (buffer-file-name))))
    (when filename
      (with-temp-buffer
        (insert filename)
        (clipboard-kill-region (point-min) (point-max)))
      (message filename))))

(defun wpc/evil-replace-under-point ()
  "Faster than typing %s//thing/g"
  (interactive)
  (save-excursion
    (evil-ex (concat "%s/\\b" (symbol-name (symbol-at-point)) "\\b/"))))

(defun wpc/disable-linum-mode ()
  "Convenience function defined to make adding hooks easier without a lambda."
  (linum-mode -1))

(defun wpc/disable-company-mode ()
  "Convenience function defined to make adding hooks easier without a lambda."
  (company-mode -1))

(defun wpc/toggle-term-mode ()
  "Toggle between term-line-mode and temr-char-mode."
  (if (term-in-line-mode)
      (term-char-mode)
    (term-line-mode)))

(defun buffer-dirname ()
  "Return the directory name of the current buffer as a string."
  (->> buffer-file-name
       f-dirname
       f-filename))

(provide 'functions)
;;; functions.el ends here