-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.el
885 lines (756 loc) · 31.3 KB
/
config.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
;;; config.el --- Configuration for Joe's private layer.
;;; Commentary:
;;
;;; Code:
(when (eq system-type 'darwin)
(defvar mac-option-modifier)
(defvar mac-command-modifier)
(setq mac-option-modifier 'meta
mac-command-modifier 'super)
(defvar my:mac-modifier-state 'mac
"Toggle between BUILT-IN and USB.")
(defun my:toggle-mac-modifiers ()
(interactive)
(if (eq my:mac-modifier-state 'usb)
(progn
(setq my:mac-modifier-state 'built-in
mac-option-modifier 'control
mac-command-modifier 'meta)
(message "Mac modifier keys set for Mac keyboard."))
(setq my:mac-modifier-state 'usb
mac-option-modifier 'meta
mac-command-modifier 'super)
(message "Mac modifier keys set for USB keyboard.")))
(setq my:mac-modifier-state 'built-in
mac-option-modifier 'control
mac-command-modifier 'meta)
(message "Mac modifier keys set for Mac keyboard."))
(defun my:back-to-indentation-or-beginning ()
"Move point to first non-whitespace char or `beginning-of-line'."
(interactive)
(let ((oldpos (point)))
(back-to-indentation)
(and (= oldpos (point))
(beginning-of-line))))
(defun unfill-paragraph ()
"Replace newline characters with a single space."
(interactive)
(let ((fill-column 90002000))
(if mark-active
(fill-region (region-beginning) (region-end))
(fill-paragraph))))
(defun my:new-blah-buffer ()
"Open up a guaranteed new blah (scratch) buffer."
(interactive)
(switch-to-buffer (cl-loop for num from 0
for name = (format "blah-%03i" num)
while (get-buffer name)
finally return name)))
(defun shell-command-on-buffer (command &optional keep-buffer-p)
"Prompt and run a COMMAND on the buffer.
By default, `shell-command-on-buffer' will replace the contents
of the buffer with the output of COMMAND. If KEEP-BUFFER-P is
non-nil, keep the original buffer content."
(interactive (list (read-shell-command "Shell command on buffer: ")))
(shell-command-on-region
(point-min) (point-max)
command
'use-current-buffer
(not keep-buffer-p)))
(defun shell-command-on-buffer ()
"Prompt and run a command on the buffer, replace the text with the output."
(interactive)
(shell-command-on-region
(point-min) (point-max)
(read-shell-command "Shell command on buffer: ")
'current-buffer
'replace-text))
(defun my:switch-to-blah-buffer ()
"Switch to a blah buffer, or create a new one."
(interactive)
(cl-loop for buffer in (buffer-list)
if (string-match "blah-.+" (buffer-name buffer))
return (switch-to-buffer buffer)
finally do (my:new-blah-buffer)))
(setq-default evil-escape-key-sequence "jk")
(setq-default sentence-end-double-space t)
(defun my:nuke-all-buffers ()
"Kill all buffers, leaving only *scratch* only."
(interactive)
(mapc
(lambda (buffer)
(kill-buffer buffer))
(buffer-list))
(delete-other-windows))
;; (with-eval-after-load 'lisp-mode
;; (require 'pp)
;; (defun my-pp-eval-last-sexp-in-current-buffer ()
;; (interactive)
;; (pp-eval-last-sexp t))
;; (spacemacs/set-leader-keys-for-major-mode 'lisp-interaction-mode
;; "ep" 'my-pp-eval-last-sexp-in-current-buffer
;; "eP" 'pp-eval-last-sexp))
;; I always hit this by mistake to get to `describe-char' and I'm tired of
;; seeing the GNU license
(global-set-key (kbd "C-h C-c") 'describe-key-briefly)
;; UTF-8 everywhere
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
;; .dir-local.el tweaks
;;
;; See http://stackoverflow.com/questions/5147060/ for explanation of
;; why we're creating a new hook. tl;dr: wierd interaction between
;; `set-auto-mode' and `hack-local-variables'
(add-hook 'hack-local-variables-hook 'my:run-local-vars-mode-hook)
(defun my:run-local-vars-mode-hook ()
"Hook for all major-modes after processing local variables.
Creates a hook for all major modes.
e.g. `python-mode-local-vars-hook',
`emacs-lisp-mode-local-vars-hook'"
(run-hooks (intern (format "%s-local-vars-hook" (symbol-name major-mode)))))
(defvar my:use-jinja-for-html-p nil
"Use `jinja2-mode' if non-nil, otherwise `html-mode'.
Primarily for use in .dir-locals.el")
(defun my:maybe-choose-jinja2-mode ()
(when my:use-jinja-for-html-p
(jinja2-mode)))
(add-hook 'html-mode-local-vars-hook 'my:maybe-choose-jinja2-mode)
;; Custom keymaps
(defvar joe-map (make-keymap))
(defun joe/set-leader-keys (key def &rest bindings)
"Add KEY and DEF as key bindings under `joe-map'.
The key for `joe-map' is `joe-leader-key'. KEY should be a
string suitable for passing to `kbd', and it should not include
the leaders. DEF is most likely a quoted command. See
`define-key' for more information about the possible choices for
DEF. This function simply uses `define-key' to add the bindings.
For convenience, BINDINGS is additional KEY DEF pairs. For
example,
\(joe/set-leader-keys
\"a\" 'command1
\"c\" 'command2
\"bb\" 'command3\)"
(while key
(define-key joe-map (kbd key) def)
(setq key (pop bindings) def (pop bindings))))
(spacemacs/set-leader-keys
"," joe-map)
(defun my:find-file-builder (name path)
"Create function to open PATH."
`(defun ,name()
(interactive)
(find-file ,path)))
(defun my:make-file-shortcuts-in-dir (dir bindings)
"Create shortcuts to DIR for file BINDINGS.
DIR is a root directory path. BINDINGS is a list of 3-element
lists, with a binding shortcut, a symbol for the function name
and the file path relative to DIR."
(declare (indent 1))
(cl-loop for (binding function-name path) in bindings
do
(let ((file-finder-defun (my:find-file-builder
function-name (concat dir path))))
(eval file-finder-defun)
(joe/set-leader-keys binding function-name))))
(my:make-file-shortcuts-in-dir "~/.dotfiles/"
'(("fdc" my:open-joe-config "layers/joe/config.el")
("fdi" my:open-i3-conf "tag-linux/config/i3/config")
("fdm" my:open-my-org "layers/joe/local/my-org.el")
("fdp" my:open-joe-packages "layers/joe/packages.el")
("fdt" my:open-tmux-conf "tmux.conf")
("fdT" my:open-joe-tlp "layers/joe/local/tlp/tlp.el")
("fdzz" my:open-zshrc "zsh/.zshrc")
("fdzk" my:open-zshrc-keys "zsh/.zshrc.keys")
("fdzp" my:open-zsh-plugins "zsh/.zshrc.plugins")
("fdze" my:open-zshenv "zsh/.zshenv")
("fdzr" my:open-zsh-prompts "zsh/prompts/prompt_pure_setup")
))
(my:make-file-shortcuts-in-dir "~/gdrive/"
'(("fgg" my:open-gtd "org/gtd.org")
("fgw" my:open-goog "gorg/goog.org")
("fgj" my:open-journal "org/journal.org")
("fgs" my:open-sandlot "gorg/sandlot.org")
("fgr" my:open-refile "org/refile.org")
("fgt" my:open-refile "gorg/today.org")
("fgp" my:open-people "org/people.org")
("fgl" my:open-ledger "financials/personal.ledger")
("fgc" my:open-joe-checklist "org/checklists/checklist.org")
("fgR" my:open-refile-work "org/work-refile.org")
("fgd" my:open-org-drill "drill/programming.org")))
(defun operate-on-point-or-region (fn)
"Get the current unspaced string at point.
Replace with the return value of the function FN"
(let (pos1 pos2 meat excerpt)
(if (and transient-mark-mode mark-active)
(setq pos1 (region-beginning)
pos2 (region-end))
(setq pos1 (car (bounds-of-thing-at-point 'symbol))
pos2 (cdr (bounds-of-thing-at-point 'symbol))))
(setq excerpt (buffer-substring-no-properties pos1 pos2))
(setq meat (funcall fn excerpt))
(delete-region pos1 pos2)
(insert meat)))
(defun my:replace-or-add-to-alist (alist-var elem)
"Replace the first entry whose `car' `equal's (car ELEM) in ALIST-VAR with ELEM.
ALIST-VAR must be a symbol. If no \(car entry\) in ALIST-VAR
equals the `car' of ELEM, then prepend ELEM to ALIST-VAR.
The return value is ELEM.
\(my:replace-or-add-to-alist 'an-alist '(\"key\" \"data\")\)"
(let ((alist (symbol-value alist-var)))
(if (assoc (car elem) alist)
(setcdr (assoc (car elem) alist)
(cdr elem))
(set alist-var (cons elem alist)))
elem))
(defun my:pretty-print-xml-region (begin end)
"Pretty format XML markup in region BEGIN END."
(interactive "r")
(save-excursion
;; split <foo><bar> or </foo><bar>, but not <foo></foo>
(goto-char begin)
(while (search-forward-regexp ">[ \t]*<[^/]" end t)
(backward-char 2) (insert "\n") (incf end))
;; split <foo/></foo> and </foo></foo>
(goto-char begin)
(while (search-forward-regexp "<.*?/.*?>[ \t]*<" end t)
(backward-char) (insert "\n") (incf end))
;; put xml namespace decls on newline
(goto-char begin)
(while (search-forward-regexp "\\(<\\([a-zA-Z][-:A-Za-z0-9]*\\)\\|['\"]\\) \\(xmlns[=:]\\)" end t)
(goto-char (match-end 0))
(backward-char 6) (insert "\n") (incf end))
(indent-region begin end nil)))
(with-eval-after-load 's
(defun my:snake-case-at-point-or-region ()
"Snake_case the current word or text selection."
(interactive)
(operate-on-point-or-region 's-snake-case))
(defun my:dasherise-at-point-or-region ()
"Dasherise-the-current CamelCase or snake_case word or text selection."
(interactive)
(operate-on-point-or-region 's-dashed-words))
(defun my:upper-camelcase-at-point-or-region ()
"UpperCamelCaseTheCurrent dashed-or-snake_case_words or any words in text selection."
(interactive)
(operate-on-point-or-region 's-upper-camel-case))
(defun my:lower-camelcase-at-point-or-region ()
"LowerCamelCaseTheCurrent dashed or snake_case word or any words in text selection."
(interactive)
(operate-on-point-or-region 's-lower-camel-case))
(defun my:humanize-at-point-or-region ()
"Humanize variable names, insert spaces instead of - or _ or un-CamelCase humps to spaced words."
(interactive)
(operate-on-point-or-region 's-capitalized-words))
(defun my:titleized-at-point-or-region ()
"Convert snaked, dashed, underscored, camelcase, or spaced words in region to Title Case."
(interactive)
(operate-on-point-or-region 's-titleized-words))
(defun my:flush-blank-lines ()
"Flush blank lines."
(interactive)
(flush-lines "^\s*$" nil nil t))
(joe/set-leader-keys
"bb" 'my:switch-to-blah-buffer
"bB" 'my:new-blah-buffer
"nb" 'my:nuke-all-buffers
"ss" 'my:snake-case-at-point-or-region
"sd" 'my:dasherise-at-point-or-region
"scu" 'my:upper-camelcase-at-point-or-region
"scl" 'my:lower-camelcase-at-point-or-region
"sh" 'my:humanize-at-point-or-region
"st" 'my:titleized-at-point-or-region
"tm" 'my:toggle-mac-modifiers
"xf" 'my:flush-blank-lines))
(defun my:insert-newline-above-and-follow ()
(interactive)
(beginning-of-line)
(insert "\n")
(forward-line -1))
(defun my:insert-newline-below-and-follow ()
(interactive)
(end-of-line)
(insert "\n"))
(defun my:insert-newline-above-and-stay ()
(interactive)
(save-excursion
(my:insert-newline-above-and-follow)))
(defun my:insert-newline-below-and-stay ()
(interactive)
(save-excursion (my:insert-newline-below-and-follow)))
(evil-leader/set-key "iSr" 'yas-reload-all)
(defun revbufs ()
"Revert all buffers."
(interactive)
(let ((conflicts '())
(orphans '())
(reverts '())
(report-buf (get-buffer-create "*revbufs*")))
;; Process the buffers.
(mapc (function
(lambda (buf)
(let ((file-name (buffer-file-name buf)))
(cond
;; If buf is the report buf, ignore it.
((eq buf report-buf) nil)
;; If buf is not a file buf, ignore it.
((not file-name) nil)
;; If buf file doesn't exist, buf is an orphan.
((not (file-exists-p file-name))
(setq orphans (nconc orphans (list buf))))
;; If file modified since buf visit, buf is either a conflict
;; (if it's modified) or we should revert it.
((not (verify-visited-file-modtime buf))
(if (buffer-modified-p buf)
(setq conflicts (nconc conflicts (list buf)))
(with-current-buffer buf
(revert-buffer t t))
(setq reverts (nconc reverts (list buf)))))))))
(copy-sequence (buffer-list)))
;; Prepare the report buffer.
(with-current-buffer report-buf
(setq buffer-read-only nil
truncate-lines t)
(delete-region (point-min) (point-max))
(insert (revbufs-format-list conflicts "CONFLICTS")
(revbufs-format-list orphans "ORPHANS")
(revbufs-format-list reverts "REVERTS"))
(goto-char (point-min))
(setq buffer-read-only t))
(bury-buffer report-buf)
;; Print message in echo area.
(if (or conflicts orphans)
(progn
(display-buffer report-buf)
(message
(concat
(format "Reverted %s with"
(revbufs-quantity (length reverts) "buffer"))
(if conflicts
(format " %s%s"
(revbufs-quantity (length conflicts) "conflict")
(if orphans " and" "")))
(if orphans
(format " %s"
(revbufs-quantity (length orphans) "orphan"))))))
(if reverts
(message "Reverted %s." (revbufs-quantity (length reverts) "buffer"))
(message "No buffers need reverting.")))))
(defun revbufs-format-list (list label)
"Format LIST of buffers with LABEL."
(if list
(concat label
(format " (%s):\n" (length list))
(mapconcat
(function
(lambda (buf)
(format " %-20s %s\n"
(buffer-name buf)
(buffer-file-name buf))))
list
""))
""))
(defun revbufs-quantity (num what)
(format "%d %s%s" num what (if (= num 1) "" "s")))
(defun my:ffap-with-line ()
(interactive)
;; TODO: handle case where point is positioned on the number.
(my:ffap-with-line-from-string (ffap-string-at-point)))
(defun my:ffap-with-line-from-string (file-name-and-line)
"Given a file path with a line number, go to the file at that line number.
FILE-NAME-AND-LINE is assumed to be of the form file/path.el:32.
If there is no line number, drop back to `find-file-at-point'."
(message "my:ffap-with-line-from-string")
(if (string-match "\\(.*\\):\\([0-9]+\\)" file-name-and-line)
(let ((file-path (match-string 1 file-name-and-line))
(line-number (match-string 2 file-name-and-line))
;; There's no way to get ffap to process just a string.
;; `ffap-file-at-point' insists on parsing the thing at point by
;; itself. This is okay as long as `ffap-string-at-point' does what
;; we want.
(full-path (ffap-file-at-point)))
(find-file-other-window full-path)
(and line-number (goto-line (string-to-number line-number))))
(find-file-other-window (ffap-file-at-point))))
(with-eval-after-load 'evil
(define-key evil-normal-state-map "gf" #'my:ffap-with-line))
(defun my:get-relative-name (file-name)
"Gets the relative FILE-NAME from a projectile directory or $HOME.
If file-name isn't underneath a projectile root or $HOME return
the expanded, absolute FILE-NAME."
(let* ((projectile-root (condition-case nil
(projectile-project-root)
(error nil)))
(home-directory (expand-file-name "~/"))
(full-file-name (expand-file-name file-name))
(base-directory
(or projectile-root
(and (s-starts-with-p home-directory full-file-name)
home-directory)
nil)))
(if base-directory
(file-relative-name full-file-name base-directory)
full-file-name)))
(defun my:get-buffer-file-name ()
"Gets a reasonable file name for different types of buffers.
If no reasonable file-name is possible, return empty string."
(if (equal major-mode 'dired-mode)
default-directory
(or (buffer-file-name) "")))
(defun my:copy-file-name-relative-to-clipboard ()
"Copy the current buffer file name to the clipboard.
Gets the file name relative to the projectile directory or $HOME
directory."
(interactive)
(message "%s" (kill-new (my:get-relative-name (my:get-buffer-file-name)))))
(defun my:copy-file-name-absolute-to-clipboard ()
"Copy the current buffer file name to the clipboard."
(interactive)
(message "%s" (kill-new (my:get-buffer-file-name))))
(joe/set-leader-keys
"yf" #'my:copy-file-name-relative-to-clipboard
"yF" #'my:copy-file-name-absolute-to-clipboard
)
(defvar my:org-to-html-convert-command
(cond
((eq system-type 'darwin)
(concat
"osascript -e 'the clipboard as \"HTML\"' "
"| perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))' "
"| pandoc -f html -t json "
"| pandoc -f json -t org"
))
((eq system-type 'gnu/linux)
(concat "NO_AT_BRIDGE=1; clipcli.py text/html "
"| pandoc -f html -t json "
"| pandoc -f json -t org")))
"Shell command to convert HTML to org.")
(defun my:chomp-trailing-newlines (string)
"Collapses trailing newlines to a single newline."
;; \\' is a special elisp regexp form for end of string.
(replace-regexp-in-string "\n+\\'" "\n" string))
(defun my:replace-nbsp-with-space (string)
"Replaces NBSP with regular spaces."
(s-replace-all '((" " . " ")
(" " . " "))
string))
(defun my:remove-null-from-string (string)
"Remove the null char \0 from a string."
(s-replace "