-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscimax-utils.el
265 lines (213 loc) · 6.9 KB
/
scimax-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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
;;; scimax-utils.el --- Utility functions scimax cannot live without
;;; Commentary:
;;
;;; Code:
;; * Hotspots
(defcustom scimax-user-hotspot-commands '()
"A-list of hotspots to jump to in `hotspots'.
These are shortcut to commands.
\(\"label\" . command)"
:group 'scimax)
(defcustom scimax-user-hotspot-locations '()
"A-list of hotspot locations to jump to in `hotspots'.
\(\"label\" . \"Path to file\").
These are like bookmarks."
:group 'scimax)
;;;###autoload
(defun hotspots (arg)
"Helm interface to hotspot locations.
This includes user defined
commands (`scimax-user-hotspot-commands'),
locations (`scimax-user-hotspot-locations'), org agenda files,
recent files and bookmarks. You can set a bookmark also."
(interactive "P")
(helm :sources `(((name . "Commands")
(candidates . ,scimax-user-hotspot-commands)
(action . (("Open" . (lambda (x) (funcall x))))))
((name . "My Locations")
(candidates . ,scimax-user-hotspot-locations)
(action . (("Open" . (lambda (x) (find-file x))))))
((name . "My org files")
(candidates . ,org-agenda-files)
(action . (("Open" . (lambda (x) (find-file x))))))
helm-source-recentf
helm-source-bookmarks
helm-source-bookmark-set)))
(add-to-list 'safe-local-eval-forms
'(progn (require 'emacs-keybinding-command-tooltip-mode) (emacs-keybinding-command-tooltip-mode +1)))
;;;###autoload
(defun scimax-help ()
"Open the ‘scimax’ manual in org-mode."
(interactive)
(find-file (expand-file-name
"scimax.org"
scimax-dir)))
;;;###autoload
(defun scimax-info ()
"Open the info manual."
(info "(scimax)")
(require 'emacs-keybinding-command-tooltip-mode)
(emacs-keybinding-command-tooltip-mode +1))
;; * utilities
;;;###autoload
(defun kill-all-buffers ()
"Kill all buffers. Leave one frame open."
(interactive)
(mapc 'kill-buffer (buffer-list))
(delete-other-windows))
;;;###autoload
(defun kill-other-buffers ()
"Kill all other buffers but this one. Leave one frame open."
(interactive)
(mapc 'kill-buffer
(delq (current-buffer) (buffer-list)))
(delete-other-windows))
;;;###autoload
(defun unfill-paragraph ()
"Unfill paragraph at or after point."
(interactive "*")
(let ((fill-column most-positive-fixnum))
(fill-paragraph nil (region-active-p))))
;; * Version control
;; Some new bindings to add to vc-prefix-map
(define-key 'vc-prefix-map "t" 'magit-status)
(define-key 'vc-prefix-map "p" (lambda () (interactive) (vc-git-push nil)))
(define-key 'vc-prefix-map "P" (lambda () (interactive) (vc-git-pull nil)))
;; * Windows
;;;###autoload
(defun explorer ()
"Open Finder or Windows Explorer in the current directory."
(interactive)
(cond
((string= system-type "gnu/linux")
(shell-command "nautilus"))
((string= system-type "darwin")
(shell-command (format "open -b com.apple.finder %s"
(if (buffer-file-name)
(file-name-directory (buffer-file-name))
"~/"))))
((string= system-type "windows-nt")
(shell-command (format "explorer %s"
(replace-regexp-in-string
"/" "\\\\"
(if (buffer-file-name)
(file-name-directory (buffer-file-name))
(expand-file-name "~/"))))))))
(defalias 'finder 'explorer "Alias for `explorer'.")
(defun bash ()
"Open a bash window."
(interactive)
(cond
((string= system-type "gnu/linux")
(shell-command "gnome-terminal"))
((string= system-type "darwin")
(shell-command
(format "open -b com.apple.terminal \"%s\""
(if (buffer-file-name)
(file-name-directory (buffer-file-name))
(expand-file-name default-directory)))))
((string= system-type "windows-nt")
(shell-command "start \"\" \"%SYSTEMDRIVE%\\Program Files\\Git\\bin\\bash.exe\" --login &"))))
;; case on regions
(defun sentence-case-region (r1 r2)
"Capitalize the word at point, and the first word of each
sentence in the region."
(interactive "r")
(save-excursion
(goto-char r1)
(capitalize-word 1)
(while (< (point) r2)
(forward-sentence)
(capitalize-word 1))))
(global-set-key (kbd "M-<backspace>") 'backward-kill-sentence)
(defun avy-jump-to-sentence ()
"Jump to a sentence with avy."
(interactive)
(avy-with my-jumper
(avy--process
(let (p
(e (window-end)))
(save-excursion
(goto-char (window-start))
(push (point) p)
(while (< (point) e)
(forward-sentence)
(save-excursion
(backward-sentence)
(push (point) p)))
(reverse p)))
(avy--style-fn avy-style))))
(defun avy-jump-to-paragraph ()
"Jump to a paragraph with avy."
(interactive)
(avy-with my-jumper
(avy--process
(let (p
(e (window-end)))
(save-excursion
(goto-char (window-start))
(push (point) p)
(while (< (point) e)
(forward-paragraph)
(push (point) p))
(reverse p)))
(avy--style-fn avy-style))))
;; * profile me
(unless (memq system-type '(windows-nt ms-dos))
(require 'esup)
(defun scimax-profile ()
"Run `esup' on the scimax init file to profile it."
(esup (expand-file-name "init.el" scimax-dir))))
(defmacro with-no-new-buffers (&rest body)
"Run BODY, and kill any new buffers created.
Returns whatever BODY would return."
(let ((current-buffers (buffer-list)))
`(prog1
(progn
,@body)
(mapc (lambda (buf)
(unless (-contains? ',current-buffers buf)
(kill-buffer buf)))
(buffer-list)))))
;; * f-strings
(defmacro f-string (fmt)
"Like `s-format' but with format fields in it.
FMT is a string to be expanded against the current lexical
environment. It is like what is used in `s-lex-format', but has
an expanded syntax to allow format-strings. For example:
${user-full-name 20s} will be expanded to the current value of
the variable `user-full-name' in a field 20 characters wide.
(let ((f (sqrt 5))) (f-string \"${f 1.2f}\"))
will render as: 2.24
This function is inspired by the f-strings in Python 3.6, which I
enjoy using a lot.
You can also try putting expressions in for formatting, e.g.:
(let ((a 11)) (f-string \"The sqrt of ${a} is ${(sqrt a) 1.2f}.\"))
will render as \"The sqrt of 11 is 3.32\".
"
(let* ((matches (s-match-strings-all"${\\(?3:\\(?1:[^} ]+\\) *\\(?2:[^}]*\\)\\)}" fmt))
(agetter (cl-loop
for (m0 m1 m2 m3) in matches
collect
`(cons ,m3
,(if (s-starts-with? "(" m3)
;; This means an expression is used
(with-temp-buffer
(insert m3)
(goto-char (point-min))
(let ((expr (read (current-buffer)))
(fmt (s-trim (buffer-substring (point) (point-max)))))
`(format
(format "%%%s" (if (string= ,fmt "")
(if s-lex-value-as-lisp "S" "s")
,fmt))
,expr)))
`(format
(format "%%%s" (if (string= ,m2 "")
(if s-lex-value-as-lisp "S" "s")
,m2))
(symbol-value (intern ,m1))))))))
`(s-format ,fmt 'aget (list ,@agetter))))
;; * The end
(provide 'scimax-utils)
;;; scimax-utils.el ends here