-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
scimax-utils.el
412 lines (343 loc) · 11.6 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
;;; scimax-utils.el --- Utility functions scimax cannot live without
;;; Commentary:
;;
;;; Code:
(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))
(defun kill-buffer-no-hook ()
"Kill buffer with no kill-buffer-hook."
(interactive)
(let ((kill-buffer-hook '()))
(kill-buffer)))
;;;###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)))
;; * Misc
;; 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)
;; * avy jump commands
(defun avy-jump-to-word-in-line (&optional arg)
"Jump to a word in the current line."
(interactive)
(avy-with word-jump
(avy-process
(let ((p '())
(e (line-end-position)))
(save-excursion
(goto-char (line-beginning-position))
(push (point) p)
(while (< (point) e)
(forward-word)
(save-excursion
(backward-word)
(push (point) p)))
(reverse p)))
(avy--style-fn avy-style))))
(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)
(save-excursion
(backward-paragraph)
(push (+ 1 (point)) p)))
(reverse p)))
(avy--style-fn avy-style))))
;; * profile me
(unless (memq system-type '(windows-nt ms-dos))
(defun scimax-profile ()
"Run `esup' on the scimax init file to profile it."
(interactive)
(require 'esup)
(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
(defun 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))))))))
(eval
`(s-format ,fmt 'aget (list ,@agetter)))))
;; * scimax describe
(defun scimax-describe ()
"Open an org-buffer describing the scimax setup."
(interactive)
(pop-to-buffer "*scimax*")
(erase-buffer)
(org-mode)
(insert (f-string "* System info
- System type :: ${system-type}
- System configuration :: ${system-configuration}
- Window system :: ${window-system}
- Emacs version :: ${emacs-version}
- Package user dir :: ${package-user-dir}
- User dir :: ${user-emacs-directory}
- imagemagick support :: ${(image-type-available-p 'imagemagick)}
- image types :: ${(imagemagick-types)}
- gnutls available :: ${(gnutls-available-p)}
- Org-version :: ${(org-version)}\n")
"\n- exec-path (this is what Emacs knows)\n"
(cl-loop for path in exec-path concat
(f-string " - ${path}\n"))
"\n- system path (this is what your shell knows)\n"
(cl-loop for p in (split-string (getenv "PATH") ":")
concat
(format " - %s\n" p)))
(let* ((default-directory scimax-dir)
(branch (string-trim (shell-command-to-string "git rev-parse --abbrev-ref HEAD")))
(commit (string-trim (shell-command-to-string "git rev-parse HEAD")))
(remote (string-trim (shell-command-to-string "git config --get remote.origin.url"))))
(insert (f-string "\n* scimax
- scimax-dir :: ${scimax-dir}")
(f-string "
- git branch :: ${branch}
- git commit :: ${commit}
- git remote :: ${remote}\n"))
(insert "\n* Executables\n"
(f-string "
- emacs :: ${(executable-find \"emacs\")}
- git :: ${(executable-find \"git\")}
- python :: ${(executable-find \"python\")}")
(f-string "
- emacs :: ${(executable-find \"emacs\")}
- git :: ${(executable-find \"git\")}
- git version :: ${(string-trim (shell-command-to-string \"git --version\"))}
- ssh :: ${(executable-find \"ssh\")}
- ssh version :: ${(string-trim (shell-command-to-string \"ssh -V\"))}
- python :: ${(executable-find \"python\")}
- latex :: ${(executable-find \"latex\")}
- For more latex info click [[elisp:scimax-latex-setup]]
- Searching tools
- grep :: ${(executable-find \"grep\")}
- ag :: ${(executable-find \"ag\")}
- pt :: ${(executable-find \"pt\")}
- find :: ${(executable-find \"find\")}
- locate :: ${(executable-find \"locate\")}
- Graphics
- convert :: ${(executable-find \"convert\"}
- mogrify :: ${(executable-find \"mogrify\"}
"))
;; the end
(goto-char (point-min))))
(defun scimax-github ()
"Open the GitHUB repo."
(interactive)
(browse-url "https://github.com/jkitchin/scimax"))
(defun scimax-github-issues ()
"Open the GitHUB repo issues page."
(interactive)
(browse-url "https://github.com/jkitchin/scimax/issues"))
;; * screenshots
;; adapted from [[https://vmtyler.com/applescript-markdown-ready-screenshots/][AppleScript Markdown-Ready Screenshots | VMTyler.com]]
(defun screenshot (&optional arg)
"Take a screenshot and insert org link.
screencapture starts in window capture mode. press space bar to
toggle it to mouse select. with prefix arg, minimize emacs first.
with double prefix arg, prompt for filename. Only works on macOS."
(interactive "P")
(when arg
(suspend-frame))
(unless (f-directory? "screenshots")
(make-directory "screenshots"))
(sit-for 0.2)
(let ((fname (if (<= (prefix-numeric-value arg) 4)
(concat (format-time-string "date-%d-%m-%Y-time-%H-%M-%S" (current-time)) ".png")
(read-file-name "filename to save in: "))))
(do-applescript
(mapconcat
'identity
(list (format "set screenshotFilePath to \"%s\"" (expand-file-name fname "screenshots"))
"do shell script \"screencapture \" & \"-i \" & \" \" & quoted form of screenshotFilePath"
(concat "set result to \"[[./" fname "]]\"")
"set the clipboard to result")
"\n"))
(insert (format "\n\n#+attr_org: :width %s\n[[./%s]]\n\n"
(min 800
;; sometimes identify returns 0, and I want 800 in that case.
(max 800 (string-to-number
(cl-first
(split-string
(cl-third
(split-string
(string-trim
(shell-command-to-string
(format "identify %s" fname)))))
"x")))))
(concat "screenshots/"
fname)))
(org-redisplay-inline-images)
(raise-frame)))
(defun pngpaste (&optional arg)
"Paste the clipboard image into org-mode.
Relies on https://github.com/jcsalterego/pngpaste. With prefix
ARG prompt for filename, else generate one. images are saved in
./screenshots. That directory is created if necessary. "
(interactive "P")
(setq png
(if arg
(read-file-name "PNG: ")
(format-time-string "./screenshots/%Y-%m-%d-%H-%M-%S.png" (current-time))))
(unless (file-directory-p "./screenshots")
(make-directory "./screenshots"))
(when (eq 0 (shell-command (format "pngpaste %s" png)))
(insert (format "#+attr_org: :width %s\n"
(min 800 (string-to-number
(cl-first
(split-string
(cl-third
(split-string
(string-trim
(shell-command-to-string
(format "identify %s" png)))))
"x"))))))
(insert (format "[[%s]]\n" (if (file-name-absolute-p png)
png
(concat "./" png))))
;; redraw so you can see them
(org-redisplay-inline-images)))
(defun tesseract (&optional arg)
"Take a screenshot and insert org link.
with prefix arg, minimize emacs first.
With a double prefix, prompt for the filename.
Only works on macOS."
(interactive "P")
(when arg
(suspend-frame))
(unless (f-directory? "screenshots")
(make-directory "screenshots"))
(sit-for 0.2)
(let* ((fname (if (<= (prefix-numeric-value arg) 4)
(concat (format-time-string "./screenshots/date-%d-%m-%Y-time-%H-%M-%S" (current-time)) ".png")
(read-file-name "filename to save in: ")))
(tmptext (make-temp-file "tesseract-"))
(applescript (mapconcat
'identity
(list (format "set screenshotFilePath to \"%s\"" (expand-file-name fname))
"do shell script \"screencapture \" & \"-s\" & \" \" & quoted form of screenshotFilePath"
(concat "set result to \"[[" fname "]]\"")
"set the clipboard to result")
"\n"))
(cmd (format-spec "tesseract %s %f && cat %f.txt"
`((?s . ,fname)
(?f . ,tmptext)))))
(do-applescript applescript)
(insert (s-trim (shell-command-to-string cmd)))
(insert (format "\n#+attr_org: :width 600\n[[./%s]]\n\n" fname))
(org-redisplay-inline-images)
(raise-frame)))
;; * The end
(provide 'scimax-utils)
;;; scimax-utils.el ends here