-
Notifications
You must be signed in to change notification settings - Fork 47
/
Sacha.el
23374 lines (20392 loc) · 886 KB
/
Sacha.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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Starting up
;; :PROPERTIES:
;; :CUSTOM_ID: starting-up
;; :END:
;; Here's how we start:
;; #+NAME: startup
;; [[file:Sacha.org::startup][startup]]
;; -*- lexical-binding: t -*-
;; This sets up the load path so that we can override it
(setq warning-suppress-log-types '((package reinitialization))) (package-initialize)
(add-to-list 'load-path "/usr/local/share/emacs/site-lisp")
(add-to-list 'load-path "~/vendor/org-mode/lisp")
(add-to-list 'load-path "~/vendor/org-mode/contrib/lisp")
(setq custom-file "~/.config/emacs/custom-settings.el")
(setq use-package-always-ensure t)
(load custom-file t)
;; startup ends here
;; Add package sources
;; :PROPERTIES:
;; :CUSTOM_ID: add-package-sources
;; :END:
;; [[file:Sacha.org::#add-package-sources][Add package sources:1]]
(unless (assoc-default "melpa" package-archives)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t))
(unless (assoc-default "nongnu" package-archives)
(add-to-list 'package-archives '("nongnu" . "https://elpa.nongnu.org/nongnu/") t))
;; Add package sources:1 ends here
;; Add my elisp directory and other files
;; :PROPERTIES:
;; :CUSTOM_ID: add-my-elisp-directory-and-other-files
;; :END:
;; Sometimes I load files outside the package system. As long as they're
;; in a directory in my =load-path=, Emacs can find them.
;; #+NAME: package-setup
;; [[file:Sacha.org::package-setup][package-setup]]
(add-to-list 'load-path "~/elisp")
(setq use-package-verbose t)
(setq use-package-always-ensure t)
(require 'use-package)
(use-package quelpa)
(use-package quelpa-use-package)
(quelpa-use-package-activate-advice)
(setq load-prefer-newer t)
;; package-setup ends here
;; Personal information
;; :PROPERTIES:
;; :CUSTOM_ID: personal-information
;; :END:
;; [[file:Sacha.org::#personal-information][Personal information:1]]
(setq user-full-name "Sacha Chua"
user-mail-address "[email protected]")
;; Personal information:1 ends here
;; System information
;; :PROPERTIES:
;; :CUSTOM_ID: system-information
;; :END:
;; #+NAME: system-info
;; [[file:Sacha.org::system-info][system-info]]
(defvar my-laptop-p (or (equal (system-name) "sacha-x230") (equal (system-name) "sacha-p52")))
(defvar my-server-p (and (equal (system-name) "localhost") (equal user-login-name "sacha")))
(defvar my-phone-p (not (null (getenv "ANDROID_ROOT")))
"If non-nil, GNU Emacs is running on Termux.")
(when my-phone-p (setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3"))
(global-auto-revert-mode) ; simplifies syncing
;; system-info ends here
;; Reload
;; :PROPERTIES:
;; :CUSTOM_ID: reload
;; :END:
;; [[file:Sacha.org::#reload][Reload:1]]
(defun my-reload-emacs-configuration ()
(interactive)
(load-file "~/proj/.emacs.d/Sacha.el"))
;; Reload:1 ends here
;; Backups
;; :PROPERTIES:
;; :CUSTOM_ID: backups
;; :END:
;; This is one of the things people usually want to change right away. By default, Emacs saves backup files in the current directory. These are the files ending in =~= that are cluttering up your directory lists. The following code stashes them all in =~/.config/emacs/backups=, where I can find them with =C-x C-f= (=find-file=) if I really need to.
;; [[file:Sacha.org::#backups][Backups:1]]
(setq backup-directory-alist '(("." . "~/.config/emacs/backups")))
(with-eval-after-load 'tramp
(add-to-list 'tramp-backup-directory-alist
(cons tramp-file-name-regexp nil)))
;; Backups:1 ends here
;; Disk space is cheap. Save lots.
;; [[file:Sacha.org::#backups][Backups:2]]
(setq delete-old-versions -1)
(setq version-control t)
(setq vc-make-backup-files t)
(setq auto-save-file-name-transforms '((".*" "~/.config/emacs/auto-save-list/" t)))
;; Backups:2 ends here
;; History
;; :PROPERTIES:
;; :CUSTOM_ID: history
;; :END:
;; From http://www.wisdomandwonder.com/wp-content/uploads/2014/03/C3F.html:
;; [[file:Sacha.org::#history][History:1]]
(setq savehist-file "~/.config/emacs/savehist")
(savehist-mode 1)
(setq history-length t)
(setq history-delete-duplicates t)
(setq savehist-save-minibuffer-history 1)
(setq savehist-additional-variables
'(kill-ring
search-ring
regexp-search-ring))
;; History:1 ends here
;; Disabling the toolbar
;; :PROPERTIES:
;; :ID: 440c0b9a-9068-450b-89a3-a20c8ec1f447
;; :DRILL_LAST_INTERVAL: 3.86
;; :DRILL_REPEATS_SINCE_FAIL: 2
;; :DRILL_TOTAL_REPEATS: 1
;; :DRILL_FAILURE_COUNT: 0
;; :DRILL_AVERAGE_QUALITY: 3.0
;; :DRILL_EASE: 2.36
;; :DRILL_LAST_QUALITY: 3
;; :DRILL_LAST_REVIEWED: [2013-02-27 Wed 23:14]
;; :CUSTOM_ID: windows-configuration
;; :END:
;; When you're starting out, the tool bar can be very helpful. [[http://sachachua.com/blog/2014/03/emacs-basics-using-mouse/][(Emacs Basics: Using the Mouse]]). Eventually, you may want to reclaim that extra little bit of screenspace. The following code turns that thing off. (Although I changed my mind about the menu - I want that again.)
;; [[file:Sacha.org::#windows-configuration][Disabling the toolbar:1]]
(tool-bar-mode -1)
;; Disabling the toolbar:1 ends here
;; Change "yes or no" to "y or n"
;; :PROPERTIES:
;; :CUSTOM_ID: change-yes-or-no-to-y-or-n
;; :END:
;; Lazy people like me never want to type "yes" when "y" will suffice.
;; [[file:Sacha.org::#change-yes-or-no-to-y-or-n][Change "yes or no" to "y or n":1]]
(fset 'yes-or-no-p 'y-or-n-p)
;; Change "yes or no" to "y or n":1 ends here
;; Minibuffer editing - more space!
;; :PROPERTIES:
;; :CUSTOM_ID: minibuffer-editing-more-space
;; :END:
;; Sometimes you want to be able to do fancy things with the text
;; that you're entering into the minibuffer. Sometimes you just want
;; to be able to read it, especially when it comes to lots of text.
;; This binds =C-M-e= in a minibuffer) so that you can edit the
;; contents of the minibuffer before submitting it.
;; [[file:Sacha.org::#minibuffer-editing-more-space][Minibuffer editing - more space!:1]]
(use-package miniedit
:commands minibuffer-edit
:init (miniedit-install))
;; Minibuffer editing - more space!:1 ends here
;; Killing text
;; :PROPERTIES:
;; :CUSTOM_ID: killing-text
;; :END:
;; [[file:Sacha.org::#killing-text][Killing text:1]]
(setq kill-ring-max 1000)
;; Killing text:1 ends here
;; From https://github.com/itsjeyd/emacs-config/blob/emacs24/init.el
;; [[file:Sacha.org::#killing-text][Killing text:2]]
(defadvice kill-region (before slick-cut activate compile)
"When called interactively with no active region, kill a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(list (line-beginning-position)
(line-beginning-position 2)))))
;; Killing text:2 ends here
;; Keybindings
;; :PROPERTIES:
;; :CUSTOM_ID: keybindings
;; :END:
;; [[file:Sacha.org::#keybindings][Keybindings:1]]
(repeat-mode 1)
;; Keybindings:1 ends here
;; Embark :embark:
;; :PROPERTIES:
;; :CUSTOM_ID: embark
;; :END:
;; [[file:Sacha.org::#embark][Embark:1]]
(use-package embark
:after org
:load-path "~/vendor/embark"
; :quelpa (embark :fetcher github :repo "oantolin/embark")
:config
(setq embark-prompter 'embark-keymap-prompter)
(add-to-list 'embark-target-finders 'my-embark-org-element)
(add-to-list 'embark-target-finders 'my-embark-subed-timestamp)
(add-to-list 'embark-target-injection-hooks '(my-journal-post embark--allow-edit))
(with-eval-after-load 'subed
(defvar-keymap embark-subed-timestamp-actions
:doc "Subed timestamp actions"
:parent subed-mode-map
"." #'my-subed-set-timestamp-to-mpv-position
"c" #'my-subed-copy-timestamp-dwim
"<up>" #'my-subed-adjust-timestamp/my-subed-adjust-timestamp-up
"w" #'my-waveform-subed-show-after-time
"<down>" #'my-subed-adjust-timestamp/my-subed-adjust-timestamp-down))
(defvar-keymap embark-sketch-actions
:doc "Org Mode sketch-related actions"
:parent org-mode-map
"o" #'my-sketch-insert-file-as-link
"v" #'my-geeqie-view)
(defvar-keymap embark-journal-actions
:doc "Journal"
"e" #'my-journal-edit)
(add-to-list 'embark-keymap-alist '(sketch . embark-sketch-actions))
(add-to-list 'embark-keymap-alist '(subed-timestamp . embark-subed-timestamp-actions))
(add-to-list 'embark-keymap-alist '(journal . embark-journal-actions))
:bind
(("C-." . embark-act)
("C-;" . embark-act)
:map vertico-map
(("M-e" . embark-export))
:map minibuffer-local-map
(("C-c e" . embark-act)
("M-e" . embark-export)
("C-;" . embark-act)
("C-<tab>" . embark-select)
("C-SPC" . (lambda () (interactive) (embark-select) (vertico-next))))
:map embark-collect-mode-map
(("C-c e" . embark-act)
("C-;" . embark-act)
("C-<tab>" . embark-select))
:map embark-general-map
(("j" . my-journal-post)
("m" . my-stream-message)
("M-w" . (lambda (s) (interactive "MString: ") (kill-new s))))
:map embark-symbol-map
("r" . erefactor-rename-symbol-in-buffer)
:map embark-url-map
("c" . my-caption-show)
))
(with-eval-after-load 'embark-org
(define-key embark-org-src-block-map
"i" #'my-org-fix-block-indentation))
;; Embark:1 ends here
;; Using Embark and qrencode to show a QR code for the Org Mode link at point :emacs:org:
;; :PROPERTIES:
;; :CUSTOM_ID: embark-qr
;; :EXPORT_DATE: 2024-01-10T15:46:11-0500
;; :EXPORT_ELEVENTY_PERMALINK: /blog/2024/01/using-embark-and-qrencode-to-show-a-qr-code-for-the-org-mode-link-at-point/
;; :EXPORT_ELEVENTY_FILE_NAME: blog/2024/01/using-embark-and-qrencode-to-show-a-qr-code-for-the-org-mode-link-at-point/
;; :EXPORT_MODIFIED: 2024-01-12T07:31:44-0500
;; :END:
;; #+begin_update
;; [2024-01-12]: Added some code to display the QR code on the right side.
;; #+end_update
;; John Kitchin includes [[https://www.youtube.com/watch?v=rGGAr1AWkTc][little QR codes in his videos]]. I
;; thought that was a neat touch that makes it easier for
;; people to jump to a link while they're watching. I'd like to
;; make it easier to show QR codes too. The following code lets
;; me show a QR code for the Org link at point. Since many of
;; my links use custom Org link types that aren't that useful
;; for people to scan, the code reuses the link resolution code
;; from [[dotemacs:web-link]] so that I can get the regular
;; ~https:~ link.
;; [[file:Sacha.org::#embark-qr][Using Embark and qrencode to show a QR code for the Org Mode link at point:1]]
(defun my-org-link-qr (url)
"Display a QR code for URL in a buffer."
(let ((buf (save-window-excursion (qrencode--encode-to-buffer (my-org-stored-link-as-url url)))))
(if (> (frame-width) 80)
(display-buffer-in-side-window buf '((side . right)))
(display-buffer buf))))
(use-package qrencode
:config
(with-eval-after-load 'embark-org
(define-key embark-org-link-map (kbd "q") #'my-org-link-qr)))
;; Using Embark and qrencode to show a QR code for the Org Mode link at point:1 ends here
;; TODO Using Embark to act on video
;; :PROPERTIES:
;; :CUSTOM_ID: embark-video
;; :END:
;; [[file:Sacha.org::#embark-video][Using Embark to act on video:1]]
(defun my-embark-video ()
"Match video."
(let ((extensions "youtu\\.?be\\|\\(webm\\|mp4\\|flv\\)$"))
(if-let ((link (and (derived-mode-p 'org-mode)
(org-element-context))))
(when (eq (org-element-type link) 'link)
(cond
((string-match extensions (org-element-property :path link))
(cons 'video (org-element-property :path link)))))
(when (and (derived-mode-p 'dired-mode)
(string-match extensions (dired-get-filename)))
(cons 'video (dired-get-filename))))))
(with-eval-after-load 'embark
(add-to-list 'embark-target-finders 'my-embark-video)
(defvar-keymap my-embark-video-actions
:doc "video"
"d" #'my-deepgram-recognize-audio
"$" #'my-deepgram-cost
"m" #'mpv-play
"c" #'my-caption-show
"w" #'my-audio-text
"W" #'waveform-show)
(add-to-list 'embark-keymap-alist '(video . my-embark-video-actions)))
;; Using Embark to act on video:1 ends here
;; Using Embark to act on audio
;; :PROPERTIES:
;; :CUSTOM_ID: embark-audio
;; :END:
;; [[file:Sacha.org::#embark-audio][Using Embark to act on audio:1]]
(defun my-embark-audio ()
"Match audio."
(let ((extensions "m4a\\|mp3\\|wav\\|ogg\\|opus"))
(if-let ((link (and (derived-mode-p 'org-mode)
(org-element-context))))
(when (eq (org-element-type link) 'link)
(cond
((string-match extensions (org-element-property :path link))
(cons 'audio (org-element-property :path link)))))
(when (and (derived-mode-p 'dired-mode)
(string-match extensions (dired-get-filename)))
(cons 'audio (dired-get-filename))))))
(defun my-audio-text (file &optional insert)
"Get the text for FILE audio.
If called interactively, copy to the kill ring."
(interactive (list (read-file-name "Audio: ")))
(let (text)
(cond
((file-exists-p (concat (file-name-sans-extension file) ".txt"))
(with-temp-buffer
(insert-file-contents (concat (file-name-sans-extension file) ".txt"))
(setq text (buffer-string))))
;; no txt yet, is there a vtt?
((file-exists-p (concat (file-name-sans-extension file) ".vtt"))
(setq text (subed-subtitle-list-text
(subed-parse-file (concat (file-name-sans-extension file) ".vtt")))))
;; no VTT, let's recognize it
(t
(my-deepgram-recognize-audio file)
(when (file-exists-p (concat (file-name-sans-extension file) ".vtt"))
(setq text (subed-subtitle-list-text
(subed-parse-file (concat (file-name-sans-extension file) ".vtt")))))))
(when text
(when (called-interactively-p 'any)
(if insert
(insert text "\n")
(kill-new text)))
text)))
(defun my-open-in-audacity (file)
(interactive "FFile: ")
(start-process "audacity" nil "audacity" file))
(with-eval-after-load 'embark
(add-to-list 'embark-target-finders 'my-embark-audio)
(defvar-keymap my-embark-audio-actions
:doc "audio"
"a" #'my-open-in-audacity
"d" #'my-deepgram-recognize-audio
"$" #'my-deepgram-cost
"D" #'my-audio-braindump-reprocess
"m" #'mpv-play
"w" #'my-audio-text
"W" #'waveform-show)
(add-to-list 'embark-keymap-alist '(audio . my-embark-audio-actions)))
;; Using Embark to act on audio:1 ends here
;; Using Embark to insert files as Org INCLUDEs
;; :PROPERTIES:
;; :CUSTOM_ID: using-embark-to-insert-files-as-org-includes
;; :END:
;; [[file:Sacha.org::#using-embark-to-insert-files-as-org-includes][Using Embark to insert files as Org INCLUDEs:1]]
(defun my-insert-file-as-org-include (file)
(interactive "fFile: ")
(set-text-properties 0 (length file) nil file)
(let ((mode (assoc-default file auto-mode-alist 'string-match)))
(insert
(org-link-make-string (concat "file:" file) (concat "Download " (file-name-nondirectory file))) "\n"
"#+begin_my_details " (file-name-nondirectory file) "\n"
(format "#+INCLUDE: %s" (prin1-to-string file))
(if mode
(concat " src " (replace-regexp-in-string "-mode$" "" (symbol-name mode)))
"")
"\n"
"#+end_my_details\n")))
(defun my-transform-org-link-to-include ()
(interactive)
(let ((link (org-element-lineage (org-element-context) '(link) t))
(mode (assoc-default (org-element-property :path link) auto-mode-alist 'string-match)))
(when link
(delete-region (org-element-property :begin link)
(org-element-property :end link))
(my-insert-file-as-org-include (org-element-property :path link)))))
(with-eval-after-load 'embark
(define-key embark-file-map "O" #'my-insert-file-as-org-include))
;; Using Embark to insert files as Org INCLUDEs:1 ends here
;; Using Embark to offer context-sensitive actions for Org elements
;; :PROPERTIES:
;; :CUSTOM_ID: using-embark-to-offer-context-sensitive-actions-for-org-elements
;; :END:
;; #+NAME: embark
;; [[file:Sacha.org::embark][embark]]
(let ((foo '"bar"))
(defun my-embark-org-element ()
"Target an Org Mode element at point."
(save-window-excursion
(save-excursion
(save-restriction
(when (derived-mode-p 'org-agenda-mode)
(org-goto-marker-or-bmk (org-get-at-bol 'org-marker))
(org-back-to-heading))
(when (derived-mode-p 'org-mode)
(let* ((context ;; Borrowed from org-open-at-point
;; Only consider supported types, even if they are not the
;; closest one.
(org-element-lineage (org-element-context)
'(headline src-block link) t))
(type (org-element-type context))
(value (org-element-property :value context)))
(cond ((eq type 'headline)
(cons 'org-heading (org-element-property :title context)))
((eq type 'src-block)
(cons 'org-src-block (org-element-property :name context)))
((eq type 'link)
(cons 'url (org-element-property :raw-link context))))))))))
(defun my-embark-org-src-block-copy-noweb-reference (element)
(kill-new (if (org-element-property element :parameters)
(format "<<%s(%s)>>" (org-element-property element :name)
(org-element-property element :parameters))
(format "<<%s>>" (org-element-property element :parameters)))))
)
;; embark ends here
;; Whichkey and Embark
;; From https://github.com/oantolin/embark/wiki/Additional-Configuration#use-which-key-like-a-key-menu-prompt
;; [[file:Sacha.org::*Whichkey and Embark][Whichkey and Embark:1]]
(defun embark-which-key-indicator ()
"An embark indicator that displays keymaps using which-key.
The which-key help message will show the type and value of the
current target followed by an ellipsis if there are further
targets."
(lambda (&optional keymap targets prefix)
(if (null keymap)
(which-key--hide-popup-ignore-command)
(which-key--show-keymap
(if (eq (plist-get (car targets) :type) 'embark-become)
"Become"
(format "Act on %s '%s'%s"
(plist-get (car targets) :type)
(embark--truncate-target (plist-get (car targets) :target))
(if (cdr targets) "…" "")))
(if prefix
(pcase (lookup-key keymap prefix 'accept-default)
((and (pred keymapp) km) km)
(_ (key-binding prefix 'accept-default)))
keymap)
nil nil t (lambda (binding)
(not (string-suffix-p "-argument" (cdr binding))))))))
(setq embark-indicators
'(embark-which-key-indicator
embark-highlight-indicator
embark-isearch-highlight-indicator))
(defun embark-hide-which-key-indicator (fn &rest args)
"Hide the which-key indicator immediately when using the completing-read prompter."
(which-key--hide-popup-ignore-command)
(let ((embark-indicators
(remq #'embark-which-key-indicator embark-indicators)))
(apply fn args)))
(with-eval-after-load 'embark
(advice-add #'embark-completing-read-prompter
:around #'embark-hide-which-key-indicator))
;; Whichkey and Embark:1 ends here
;; Embark and images :image:
;; :PROPERTIES:
;; :CATEGORY: Sacha
;; :EXPORT_MODIFIED: 2024-09-25T14:31:49-0400
;; :CUSTOM_ID: embark-image
;; :END:
;; #+begin_update
;; [2024-09-25]: added attachment handling
;; #+end_update
;; [[file:Sacha.org::#embark-image][Embark and images:1]]
(defun my-sketch-insert-file-as-link (f)
(interactive "fSketch: ")
(insert (org-link-make-string (concat "sketch:" (file-name-nondirectory f))) "\n"))
;; Embark and images:1 ends here
;; [[file:Sacha.org::#embark-image][Embark and images:2]]
(defun my-embark-image ()
"Match images."
(let ((extensions "\\(png\\|jpg\\|svg\\|gif\\)\\'"))
(if-let ((link (and (derived-mode-p 'org-mode)
(org-element-context))))
(when (eq (org-element-type link) 'link)
(cond
((string= "attachment" (org-element-property :type link))
(cons 'image (expand-file-name (org-element-property :path link)
(org-attach-dir))))
((string-match "sketch" (org-element-property :type link))
(cons 'image (my-get-sketch-filename (org-element-property :path link))))
((string-match extensions (org-element-property :path link))
(cons 'image (org-element-property :path link)))))
(when (and (derived-mode-p 'dired-mode)
(string-match extensions (dired-get-filename)))
(cons 'image (dired-get-filename))))))
(with-eval-after-load 'embark
(add-to-list 'embark-target-finders 'my-embark-image))
;; Embark and images:2 ends here
;; I want to:
;; - open images in an annotation program, maybe [[https://github.com/phase1geo/Annotator][com.github.phase1geo.annotator]]
;; - open images in [[https://krita.org/en/][Krita]]
;; - replace with latest screenshot
;; - copy text to kill ring
;; - insert text as details block
;; [[file:Sacha.org::#embark-image][Embark and images:3]]
(defun my-image-open-in-annotator (file)
(interactive "FImage: ")
(start-process "annotator" nil "com.github.phase1geo.annotator" (expand-file-name file)))
(defun my-image-open-in-krita (file)
(interactive "FImage: ")
(start-process "krita" nil "krita" "--nosplash" (expand-file-name file)))
(defun my-image-open-in-inkscape (file)
(interactive "FImage: ")
(start-process "inkscape" nil "inkscape" (expand-file-name file)))
(defun my-image-open-in-gimp (file)
(interactive "FImage: ")
(start-process "gimp" nil "gimp" (expand-file-name file)))
(defun my-open-in-firefox (file)
(interactive "FItem: ")
(start-process "firefox" nil "firefox" (if (string-match "^http" file) file (expand-file-name file))))
(defvar my-image-autocrop-border 10)
(defun my-image-autocrop (filename &optional border)
(interactive "FFile: ")
(setq border (or border my-image-autocrop-border))
(let ((args (append '("-trim")
(if border `("-bordercolor" "#FFFFFF" "-border" ,(number-to-string border)))
(list "+repage" filename))))
(apply #'call-process "mogrify" nil nil nil args)))
(defun my-image-recognize (file)
(interactive "FFile: ")
(let ((data (json-parse-string
(if (file-exists-p (concat (file-name-sans-extension file) ".json"))
(with-temp-buffer
(insert-file-contents (concat (file-name-sans-extension file) ".json"))
(buffer-string))
(with-temp-file (concat (file-name-sans-extension file) ".json")
(call-process "gcloud" nil t nil "ml" "vision" "detect-document" (expand-file-name file))
(buffer-string)))
:object-type 'alist)))
(if (assoc-default 'responses data)
(assoc-default 'text (assoc-default 'fullTextAnnotation (elt (assoc-default 'responses data) 0)))
(assoc-default 'description (elt (assoc-default 'textAnnotations data) 0)))))
(defun my-image-recognize-get-new-filename (file)
(interactive "FFile: ")
(if-let* ((text (my-image-recognize file))
(id (and (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]" text)
(match-string 0 text)))
(data (and id (my-journal-get-by-zidstring id))))
(concat id " " (plist-get data :Note) "." (file-name-extension file))))
(defun my-image-recognize-and-rename (file)
(interactive "FFile: ")
(let ((new-name (expand-file-name (my-image-recognize-get-new-filename file)
(file-name-directory file))))
(rename-file file new-name t)
new-name))
(defun my-image-store (file &optional do-move)
"Copy or move this image into public or private sketches as needed."
(unless (string-match "^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9] " (file-name-nondirectory file))
(setq file (my-image-recognize-and-rename file)))
(when (string-match "^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9] " (file-name-nondirectory file))
(funcall
(if do-move
'rename-file
'copy-file)
file
(expand-file-name
(file-name-nondirectory file)
(if (string-match "#private" (file-name-nondirectory file))
my-private-sketches-directory
(car my-sketch-directories)))
t)))
(defun my-image-copy-text (file)
(interactive "FImage: ")
(kill-new (my-image-recognize file)))
(defun my-image-insert-text-as-details (file)
(interactive "FImage: ")
(when (and (derived-mode-p 'org-mode)
(eq (org-element-type (org-element-context)) 'link))
(goto-char (org-element-end (org-element-context))))
(insert "\n#+begin_my_details\n" (my-image-recognize file) "\n#+end_my_details\n"))
(with-eval-after-load 'embark
(defvar-keymap my-embark-image-actions
:doc "Images"
"k" #'my-image-open-in-krita
"a" #'my-image-open-in-annotator
"i" #'my-image-open-in-inkscape
"w" #'my-image-copy-text
"c" #'my-image-autocrop
"g" #'my-image-open-in-gimp
"f" #'my-open-in-firefox
"s" #'my-image-store
"r" #'my-image-recognize-and-rename
"C" #'my-image-recolor
"d" #'my-image-insert-text-as-details)
(add-to-list 'embark-keymap-alist '(image . my-embark-image-actions)))
;; Embark and images:3 ends here
;; Embark and subed
;; :PROPERTIES:
;; :CUSTOM_ID: embark-subed
;; :END:
;; [[file:Sacha.org::#embark-subed][Embark and subed:1]]
(defun my-subed-set-timestamp-to-mpv-position (&optional rest)
(interactive)
(skip-chars-backward "0-9:,.")
(when (looking-at "\\(\\([0-9]+\\):\\)?\\([0-9]+\\):\\([0-9]+\\)\\.\\([0-9]+\\)")
(replace-match (save-match-data (subed-msecs-to-timestamp subed-mpv-playback-position)) t t)))
(defun my-embark-subed-timestamp ()
(save-excursion
(skip-chars-backward "0-9:,.")
(when (looking-at "\\(\\([0-9]+\\):\\)?\\([0-9]+\\):\\([0-9]+\\)\\.\\([0-9]+\\)")
(list 'subed-timestamp
(propertize
(match-string 0)
'ms (compile-media-timestamp-to-msecs (match-string 0))
'position (if (bolp) 'start 'stop))))))
(defun my-subed-adjust-timestamp (offset)
(interactive (list -100))
(save-excursion
(skip-chars-backward "0-9:,.")
(when (looking-at subed-vtt--regexp-timestamp)
(let ((new-ts (+ (subed-vtt--timestamp-to-msecs (match-string 0)) offset)))
(replace-match (save-match-data
(subed-vtt--msecs-to-timestamp new-ts)))
(my-waveform-subed-show-after-time)
new-ts))))
(defun my-subed-adjust-timestamp-up (offset)
(interactive (list 100))
(subed-mpv-jump (my-subed-adjust-timestamp (- offset))))
(defun my-subed-adjust-timestamp-down (offset)
(interactive (list -100))
(subed-mpv-jump (my-subed-adjust-timestamp (- offset))))
(defhydra my-subed-adjust-timestamp ()
("<up>" my-subed-adjust-timestamp-up "Up" :exit nil)
("<down>" my-subed-adjust-timestamp-down "Down" :exit nil))
(defun my-subed-copy-timestamp-from-previous ()
(interactive)
(let ((ms (save-excursion (subed-backward-subtitle-time-stop) (subed-subtitle-msecs-stop))))
(subed-set-subtitle-time-start ms)))
(defun my-subed-copy-timestamp-to-next ()
(interactive)
(let ((ms (subed-subtitle-msecs-stop)))
(save-excursion
(subed-forward-subtitle-time-stop) (subed-set-subtitle-time-start ms))))
(defun my-subed-copy-timestamp-dwim ()
(interactive)
(save-excursion
(skip-chars-backward "0-9:,.")
(if (bolp)
(my-subed-copy-timestamp-from-previous)
(my-subed-copy-timestamp-to-next))))
;; Embark and subed:1 ends here
;; Embark, symbols, and casual-symbol-overlay
;; :PROPERTIES:
;; :CUSTOM_ID: casual-symbol-overlay
;; :END:
;; Link: http://yummymelon.com/devnull/announcing-casual-symbol-overlay.html
;; [[file:Sacha.org::#casual-symbol-overlay][Embark, symbols, and casual-symbol-overlay:1]]
(use-package casual-symbol-overlay
:if my-laptop-p
:after embark
:init
(with-eval-after-load 'embark
(keymap-set embark-symbol-map "z" #'casual-symbol-overlay-tmenu)))
;; Embark, symbols, and casual-symbol-overlay:1 ends here
;; [[file:Sacha.org::*Extended command list][Extended command list:2]]
;;; Mostly the same as my/read-extended-command-from-list
(defun my-read-extended-command-from-list (list)
"Read command name to invoke in `execute-extended-command'."
(minibuffer-with-setup-hook
(lambda ()
(add-hook 'post-self-insert-hook
(lambda ()
(setq execute-extended-command--last-typed
(minibuffer-contents)))
nil 'local)
(setq-local minibuffer-default-add-function
(lambda ()
;; Get a command name at point in the original buffer
;; to propose it after M-n.
(let ((def (with-current-buffer
(window-buffer (minibuffer-selected-window))
(and (commandp (function-called-at-point))
(format "%S" (function-called-at-point)))))
(all (sort (minibuffer-default-add-completions)
#'string<)))
(if def
(cons def (delete def all))
all)))))
;; Read a string, completing from and restricting to the set of
;; all defined commands. Don't provide any initial input.
;; Save the command read on the extended-command history list.
(completing-read
(concat (cond
((eq current-prefix-arg '-) "- ")
((and (consp current-prefix-arg)
(eq (car current-prefix-arg) 4)) "C-u ")
((and (consp current-prefix-arg)
(integerp (car current-prefix-arg)))
(format "%d " (car current-prefix-arg)))
((integerp current-prefix-arg)
(format "%d " current-prefix-arg)))
;; This isn't strictly correct if `execute-extended-command'
;; is bound to anything else (e.g. [menu]).
;; It could use (key-description (this-single-command-keys)),
;; but actually a prompt other than "M-x" would be confusing,
;; because "M-x" is a well-known prompt to read a command
;; and it serves as a shorthand for "Extended command: ".
"M-x ")
(lambda (string pred action)
(if (and suggest-key-bindings (eq action 'metadata))
'(metadata
(affixation-function . read-extended-command--affixation)
(category . command))
(complete-with-action action list string pred)))
#'commandp t nil 'extended-command-history)))
;;; Mostly the same as execute-extended-command
(defun my-execute-extended-command-from-list (prefixarg &optional command-name typed)
;; Based on Fexecute_extended_command in keyboard.c of Emacs.
;; Aaron S. Hawley <aaron.s.hawley(at)gmail.com> 2009-08-24
"Read a command name, then read the arguments and call the command.
To pass a prefix argument to the command you are
invoking, give a prefix argument to `execute-extended-command'."
(declare (interactive-only command-execute))
;; FIXME: Remember the actual text typed by the user before completion,
;; so that we don't later on suggest the same shortening.
(interactive
(let ((execute-extended-command--last-typed nil))
(list current-prefix-arg
(if (and command-name (listp command-name))
(my-read-extended-command-from-list command-name)
(read-extended-command))
execute-extended-command--last-typed)))
;; Emacs<24 calling-convention was with a single `prefixarg' argument.
(when (listp command-name)
(let ((current-prefix-arg prefixarg) ; for prompt
(execute-extended-command--last-typed nil))
(setq command-name
(if command-name
(my/read-extended-command-from-list command-name)
(read-extended-command)))
(setq typed execute-extended-command--last-typed)))
(let* ((function (and (stringp command-name) (intern-soft command-name)))
(binding (and suggest-key-bindings
(not executing-kbd-macro)
(where-is-internal function overriding-local-map t))))
(unless (commandp function)
(error "`%s' is not a valid command name" command-name))
;; Some features, such as novice.el, rely on this-command-keys
;; including M-x COMMAND-NAME RET.
(set--this-command-keys (concat "\M-x" (symbol-name function) "\r"))
(setq this-command function)
;; Normally `real-this-command' should never be changed, but here we really
;; want to pretend that M-x <cmd> RET is nothing more than a "key
;; binding" for <cmd>, so the command the user really wanted to run is
;; `function' and not `execute-extended-command'. The difference is
;; visible in cases such as M-x <cmd> RET and then C-x z (bug#11506).
(setq real-this-command function)
(let ((prefix-arg prefixarg))
(command-execute function 'record))
;; If enabled, show which key runs this command.
;; But first wait, and skip the message if there is input.
(let* ((waited
;; If this command displayed something in the echo area;
;; wait a few seconds, then display our suggestion message.
;; FIXME: Wait *after* running post-command-hook!
;; FIXME: If execute-extended-command--shorter were
;; faster, we could compute the result here first too.
(when (and suggest-key-bindings
(or binding
(and extended-command-suggest-shorter typed)))
(sit-for (cond
((zerop (length (current-message))) 0)
((numberp suggest-key-bindings) suggest-key-bindings)
(t 2))))))
(when (and waited (not (consp unread-command-events)))
(unless (or (not extended-command-suggest-shorter)
binding executing-kbd-macro (not (symbolp function))
(<= (length (symbol-name function)) 2))
;; There's no binding for CMD. Let's try and find the shortest
;; string to use in M-x.
;; FIXME: Can be slow. Cache it maybe?
(while-no-input
(setq binding (execute-extended-command--shorter
(symbol-name function) typed))))
(when binding
(with-temp-message
(format-message "You can run the command `%s' with %s"
function
(if (stringp binding)
(concat "M-x " binding " RET")
(key-description binding)))
(sit-for (if (numberp suggest-key-bindings)
suggest-key-bindings
2))))))))
;; Extended command list:2 ends here
;; Menus
;; :PROPERTIES:
;; :CUSTOM_ID: menus
;; :END:
;; Handy when I'm in tablet mode.
;; [[file:Sacha.org::#menus][Menus:1]]
(define-key-after global-map [menu-bar my-menu] (cons "Shortcuts" (make-sparse-keymap "Custom shortcuts")) 'tools)
(define-key global-map [menu-bar my-menu journal] '("Show journal entries" . my-show-missing-journal-entries))
(define-key global-map [menu-bar my-menu agenda] '("Org agenda" . (lambda () (interactive) (org-agenda nil "a"))))
(define-key global-map [menu-bar my-menu audio] '("Process audio" . (lambda () (interactive) (shell-command "~/bin/process-audio &"))))
(define-key global-map [menu-bar my-menu new-index-card] '("New index card" . (lambda () (interactive)
(my-org-sketch-edit (my-prepare-index-card-template)))))
;; Menus:1 ends here
;; Context menus
;; :PROPERTIES:
;; :CUSTOM_ID: context-menus
;; :END:
;; [[file:Sacha.org::#context-menus][Context menus:1]]
(add-hook 'text-mode-hook 'context-menu-mode)
(with-eval-after-load 'dired
(add-hook 'dired-mode-hook 'context-menu-mode))
(add-hook 'shell-mode-hook 'context-menu-mode)
;; Context menus:1 ends here
;; Repeatable commands
;; :PROPERTIES:
;; :CUSTOM_ID: repeatable-commands
;; :END:
;; Based on http://oremacs.com/2015/01/14/repeatable-commands/ . Modified to
;; accept =nil= as the first value if you don't want the keymap to run a
;; command by default, and to use =kbd= for the keybinding definitions.
;; [[file:Sacha.org::#repeatable-commands][Repeatable commands:1]]
(defun my-def-rep-command (alist)
"Return a lambda that calls the first function of ALIST.
It sets the transient map to all functions of ALIST,
allowing you to repeat those functions as needed."
(let ((keymap (make-sparse-keymap))
(func (cdar alist)))
(mapc (lambda (x)
(when x
(define-key keymap (kbd (car x)) (cdr x))))
alist)
(lambda (arg)
(interactive "p")
(when func
(funcall func arg))
(set-transient-map keymap t))))
;; Repeatable commands:1 ends here
;; Hydra keyboard shortcuts
;; :PROPERTIES:
;; :CUSTOM_ID: hydras
;; :END:
;; package:hydra offers customizable shortcuts. package:transient is another option.
;; [[file:Sacha.org::#hydras][Hydra keyboard shortcuts:1]]
(use-package hydra :commands defhydra)
(use-package use-package-hydra)
(if my-laptop-p
(use-package hydra-posframe
:if my-laptop-p :after hydra
:vc (:url "https://github.com/Ladicle/hydra-posframe")
))
;; Hydra keyboard shortcuts:1 ends here
;; [[file:Sacha.org::#hydras][Hydra keyboard shortcuts:2]]
(with-eval-after-load 'hydra
(defhydra my-window-movement ()
("<left>" windmove-left)
("<right>" windmove-right)
("<down>" windmove-down)
("<up>" windmove-up)
("y" other-window "other")
("h" switch-window "switch-window")
("b" consult-buffer "buffer")
("f" find-file "file")