This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
init-utils.el
210 lines (175 loc) · 6.19 KB
/
init-utils.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
;;; -*- lexical-binding: t -*-
;;; My custom Emacs lisp functions
(require 'cl)
(require 'grep)
(defun bw-turn-on-auto-fill ()
"Enables auto-fill"
(interactive)
(auto-fill-mode 1))
(defun bw-turn-off-auto-fill ()
"Disables auto-fill"
(interactive)
(auto-fill-mode -1))
(defun bw-enable-hl-line-mode ()
"Enables hl-line-mode"
(interactive)
(hl-line-mode 1))
(defun bw-turn-on-flymake-mode ()
"Turns on flymake-mode locally"
(interactive)
(flymake-mode 1))
(defun bw/turn-on-electric-indent-mode ()
"Turns on electric-indent-mode"
(interactive)
(electric-indent-mode 1))
(defun bw/turn-off-electric-indent-mode ()
"Turns off electric-indent-mode"
(interactive)
(electric-indent-mode -1))
(defun bw-locate-library-dir (library)
"Locates the directory containing a loaded library"
(file-name-directory (locate-library library)))
(defun bw-add-to-load-path (dir)
"Adds `dir` to load-path"
(add-to-list 'load-path dir))
(defun bw-add-to-custom-theme-load-path (dir)
"Adds `dir` to custom-theme-load-path"
(add-to-list 'custom-theme-load-path dir))
(defun bw-join-dirs (prefix suffix)
"Joins `prefix` and `suffix` into a directory"
(file-name-as-directory (concat prefix suffix)))
(defun bw-turn-on-subword-mode ()
"Turns on subword mode for a buffer"
(subword-mode 1))
;; from: http://stackoverflow.com/a/7934783
(defun beautify-json ()
"Indents and pretties JSON structures"
(interactive)
(let ((b (if mark-active (min (point) (mark)) (point-min)))
(e (if mark-active (max (point) (mark)) (point-max))))
(shell-command-on-region b e
"python -mjson.tool" (current-buffer) t)))
(defun bw-occur-non-ascii-chars ()
"Finds characters that aren't in the displayable range for ASCII"
(interactive)
(occur "[^\000-\177]"))
;; from:
;; https://github.com/technomancy/emacs-starter-kit/blob/31c2465712485a54aba6a3ef6d1bef9b564f8f37/starter-kit-defuns.el#L179
(defun sudo-edit (&optional arg)
"Edit this file as sudo"
(interactive "P")
(if (or arg (not buffer-file-name))
(find-file (concat "/sudo::" (ido-read-file-name "File: ")))
(find-alternate-file (concat "/sudo::" buffer-file-name))))
(defun chomp (str)
"Chomp leading and tailing whitespace from STR."
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'" str)
(setq str (replace-match "" t t str))) str)
(defun get-keychain-password (account-name)
"Gets `account` keychain password from OS X Keychain"
(interactive "sAccount name: ")
(chomp
(shell-command-to-string
(concat
"security 2>&1 >/dev/null find-generic-password -ga "
account-name
"| sed 's/^password: \\\"\\(.*\\)\\\"/\\1/'"))))
;; gotten from:
;; http://stackoverflow.com/questions/12492/pretty-printing-xml-files-on-emacs
(defun bf-pretty-print-xml-region (begin end)
"Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this. The function inserts linebreaks to separate tags that have
nothing but whitespace between them. It then indents the markup
by using nxml's indentation rules."
(interactive "r")
(save-excursion
(nxml-mode)
(goto-char begin)
(while (search-forward-regexp "\>[ \\t]*\<" nil t)
(backward-char) (insert "\n"))
(indent-region begin end)))
(defun bw-require-list (items)
"Takes a list of items to require"
(interactive)
(dolist (item items)
(require `,item nil t)))
;; http://paste.lisp.org/display/129008
(defun quit-or-hide ()
(interactive)
(if (boundp 'server-name)
(if (> (length server-clients) 1)
(delete-frame)
(make-frame-invisible nil t))
(bw-kill-emacs)))
(defun bw-kill-emacs ()
"If this buffer is a client, just kill it, otherwise confirm
the quit."
(interactive)
(if server-buffer-clients
(server-edit)
(cond ((y-or-n-p "Quit Emacs? ")
(save-buffers-kill-terminal)))))
;; http://andrewcoxtech.blogspot.co.uk/2009/11/inserting-bom-into-file.html
(defun bw-insert-bom()
"Inserts a valid UTF8 byte order mark"
(interactive)
(save-excursion
(goto-char (point-min))
(ucs-insert (string-to-number "FEFF" 16))
(message "BOM inserted")))
(defun bw-read-string-at-point ()
"Grabs the `symbol` currently at point"
(interactive)
(symbol-name (symbol-at-point)))
(defun bw-is-image (name)
(member (file-name-extension name) '("jpg" "png" "gif" "jpeg")))
(defun bw-completing-read (prompt index)
(ido-completing-read prompt index)
;;(grizzl-completing-read prompt (grizzl-make-index index))
)
(defun bw-evil-escape-if-next-char (c)
"Watches the next letter. If c, then switch to Evil's normal mode; otherwise insert a k and forward unpressed key to unread-command events"
(self-insert-command 1)
(let ((next-key (read-event)))
(if (= c next-key)
(progn
(delete-backward-char 1)
(do-evil-esc))
(setq unread-command-events (list next-key)))))
(defun bw-evil-escape-if-next-char-is-j (arg)
"Wrapper around escape-if-next-char and the character j"
(interactive "p")
(if (= arg 1)
(bw-evil-escape-if-next-char ?j)
(self-insert-command arg)))
(defun bw-open-term (&optional arg)
"Opens an ansi-term with value of $TERM - force new ansi-term
with prefix"
(interactive "p")
(if (or (not (get-buffer "*ansi-term*")) (= arg 4))
(ansi-term (getenv "SHELL"))
(switch-to-buffer "*ansi-term*")))
(defun bw-eproject-ido-switch-buffers ()
"Like ido-switch-buffer, but for the current eproject."
(interactive)
(if (not (eproject-root))
(error "No active project was found")
(switch-to-buffer
(bw-completing-read
(concat (eproject-name) " buffers: ")
(mapcar #'buffer-name (--eproject-buffers))))))
(defun --eproject-buffers ()
(when (eproject-root)
(cdr (assoc (eproject-root) (eproject--project-buffers)))))
(defun bw-switch-to-scratch ()
"Switch to the scratch buffer if it exists"
(interactive)
(switch-to-buffer "*scratch*"))
;; Gotten from:
;; http://whattheemacsd.com/appearance.el-01.html
(defmacro rename-modeline (package-name mode new-name)
`(eval-after-load ,package-name
'(defadvice ,mode (after rename-modeline activate)
(setq mode-name ,new-name))))
(provide 'init-utils)