-
Notifications
You must be signed in to change notification settings - Fork 47
/
Sacha.org
24581 lines (22131 loc) · 912 KB
/
Sacha.org
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
#+TITLE: Sacha Chua's Emacs configuration
#+OPTIONS: toc:nil h:4
#+SETUPFILE: ~/vendor/org-html-themes/org/theme-readtheorg-sachac.setup
#+STARTUP: showeverything
#+PROPERTY: header-args:emacs-lisp+ :tangle yes :results silent :exports code :eval never-export
#+ELEVENTY_COLLECTIONS: _posts
#+ELEVENTY_BASE_DIR: ~/proj/static-blog/
#+FILETAGS: :emacs:
#+ELEVENTY_CATEGORIES+: emacs
#+ELEVENTY_LAYOUT: layouts/post
#+LINK: dotemacs https://sachachua.com/dotemacs#%s
#+ELEVENTY_BASE_URL: https://sachachua.com
Last exported: {{{modification-time(%Y-%m-%d %H:%M)}}}
#+TOC: headlines 4
* About this file
:PROPERTIES:
:CUSTOM_ID: babel-init
:END:
<<babel-init>>
This is my personal config. It's really long, but that's partly
because I sometimes leave blog posts in it as commentary, and also
because I've got a lot of little customizations that I might not even
remember. =) If you want to see a table of contents and other
useful niceties, go to http://sachachua.com/dotemacs . Other links for
this page: [[https://raw.githubusercontent.com/sachac/.emacs.d/gh-pages/Sacha.org][Org Mode version]], [[http://github.com/sachac/.emacs.d/][Github repository]]
If you're new to Emacs Lisp, you probably don't want to copy and paste
large chunks of this code. Instead, copy small parts of it (always
making sure to copy a complete set of parentheses) into your
~*scratch*~ buffer or some other buffer in ~emacs-lisp-mode~. Use ~M-x
eval-buffer~ to evaluate the code and see if you like the way that
Emacs behaves. See [[https://www.gnu.org/software/emacs/manual/html_mono/eintr.html][An Introduction to Programming in Emacs Lisp]] for
more details on Emacs Lisp. You can also find the manual by using ~C-h
i~ (~info~) and choosing "Emacs Lisp Intro".
I've installed a lot of packages. See the [[*Add%20package%20sources][package sources]] section to
add the repositories to your configuration. When you see ~use-package~
and a package name you might like, you can use ~M-x package-install~
to install the package of that name.
If you're viewing the Org file, you can open source code blocks (those
are the ones in ~begin_src~) in a separate buffer by moving your point
inside them and typing ~C-c '~ (~org-edit-special~). This opens another
buffer in =emacs-lisp-mode=, so you can use =M-x eval-buffer= to load
the changes. If you want to explore how functions work, use =M-x
edebug-defun= to set up debugging for that function, and then call it.
You can learn more about edebug in the [[http://www.gnu.org/software/emacs/manual/html_node/elisp/Edebug.html][Emacs Lisp]] manual.
I like using ~(setq ...)~ more than Customize because I can neatly
organize my configuration that way. Ditto for ~use-package~ - I mostly
use it to group together package-related config without lots of
~with-eval-after-load~ calls, and it also makes declaring keybindings
easier.
Here's my init.el:
#+begin_src emacs-lisp :tangle "~/.config/emacs/init.el" :eval never-export
(load-file "~/sync/emacs/Sacha.el")
(load-file "~/sync/cloud/.emacs.secrets")
(put 'narrow-to-region 'disabled nil)
(put 'list-timers 'disabled nil)
(server-mode 1)
#+end_src
Sacha.el is what =M-x org-babel-tangle= (=C-c C-v t=) produces.
*A note about Org updates:* I like running Org Mode from checked-out
source code instead of package.el. I add the Lisp directories to my
=load-path=, and I also use the =:load-path= option in my first
=use-package org= call to set the load path. One of those is probably
doing the trick and the other one is redundant, but maybe it's a
belt-and-suspenders sort of thing. Using the git checkout also makes
upgrading Org easy. All I have to do is =git pull; make=, and stuff
happens in an external Emacs process. Since I create =Sacha.el= via
=org-babel-tangle=, my Emacs config can load =Sacha.el= without
loading Org first.
** Debugging tips
If things break, I can use:
- ~check-parens~ to look for mismatched parentheses
- package:bug-hunter to bisect my configuration
- ~trace-function-background~ to get information printed to a buffer
- ~profiler-start~ to find out more about slow functions
** Starting up
:PROPERTIES:
:CUSTOM_ID: starting-up
:END:
Here's how we start:
#+NAME: startup
#+begin_src emacs-lisp
;; -*- 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)
#+END_SRC
** Emacs initialization
:PROPERTIES:
:CUSTOM_ID: emacs-initialization
:END:
*** Add package sources
:PROPERTIES:
:CUSTOM_ID: add-package-sources
:END:
#+BEGIN_SRC emacs-lisp
(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))
#+END_SRC
Use =M-x package-refresh-contents= to reload the list of packages
after adding these for the first time.
*** 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
#+BEGIN_SRC emacs-lisp
(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)
#+END_SRC
** Personal information
:PROPERTIES:
:CUSTOM_ID: personal-information
:END:
#+BEGIN_SRC emacs-lisp
(setq user-full-name "Sacha Chua"
user-mail-address "[email protected]")
#+END_SRC
** System information
:PROPERTIES:
:CUSTOM_ID: system-information
:END:
#+NAME: system-info
#+begin_src emacs-lisp
(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
#+end_src
** Reload
:PROPERTIES:
:CUSTOM_ID: reload
:END:
#+begin_src emacs-lisp
(defun my-reload-emacs-configuration ()
(interactive)
(load-file "~/proj/.emacs.d/Sacha.el"))
#+end_src
** 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.
#+BEGIN_SRC emacs-lisp
(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)))
#+END_SRC
Disk space is cheap. Save lots.
#+BEGIN_SRC emacs-lisp
(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)))
#+END_SRC
** History
:PROPERTIES:
:CUSTOM_ID: history
:END:
From http://www.wisdomandwonder.com/wp-content/uploads/2014/03/C3F.html:
#+BEGIN_SRC emacs-lisp
(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))
#+END_SRC
** 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.)
#+BEGIN_SRC emacs-lisp
(tool-bar-mode -1)
#+END_SRC
** 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.
#+BEGIN_SRC emacs-lisp
(fset 'yes-or-no-p 'y-or-n-p)
#+END_SRC
** 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.
#+BEGIN_SRC emacs-lisp
(use-package miniedit
:commands minibuffer-edit
:init (miniedit-install))
#+END_SRC
** Killing text
:PROPERTIES:
:CUSTOM_ID: killing-text
:END:
#+begin_src emacs-lisp
(setq kill-ring-max 1000)
#+end_src
From https://github.com/itsjeyd/emacs-config/blob/emacs24/init.el
#+BEGIN_SRC emacs-lisp
(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)))))
#+END_SRC
* Keybindings
:PROPERTIES:
:CUSTOM_ID: keybindings
:END:
#+begin_src emacs-lisp
(repeat-mode 1)
#+end_src
** Embark :embark:
:PROPERTIES:
:CUSTOM_ID: embark
:END:
#+begin_src emacs-lisp
(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))
#+end_src
Things I'm getting used to using:
- ~C-. c~ on an Org Mode source block to copy the contents
In addition to the entries below, I also have Embark-related code in other places in my config:
- [[file:~/sync/emacs/Sacha.org::#consult-omni][consult-omni]]
- [[file:~/sync/emacs/Sacha.org::#quickly-jump-to-positions][Quickly jump to positions]]
- [[file:~/sync/emacs/Sacha.org::*Making it easier to add a category to a blog post][Making it easier to add a category to a blog post]]
- [[file:~/sync/emacs/Sacha.org::#embark-11ty][embark-11ty]]
- [[file:~/sync/emacs/Sacha.org::#git-projects][Using an Emacs Lisp macro to define quick custom Org Mode links to project files; plus URLs and search]] (2024)
- [[file:~/sync/emacs/Sacha.org::#web-link][Copy web link]]
- [[file:~/sync/emacs/Sacha.org::*Quickly search my code][Quickly search my code]]
- [[file:~/sync/emacs/Sacha.org::*Tip from Omar: embark-around-action-hooks][Tip from Omar: embark-around-action-hooks]]
- [[file:~/sync/emacs/Sacha.org::#edit-list][Edit list]]
- [[file:~/sync/emacs/Sacha.org::#spookfox-babel][Running the current Org Mode Babel Javascript block from Emacs using Spookfox]] (2024)
- [[file:~/sync/emacs/Sacha.org::#act-on-current-message-with-embark][Act on current message with Embark]]
*** 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.
#+begin_src emacs-lisp
(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)))
#+end_src
#+CAPTION: Screenshot of QR code for the link at point
#+ATTR_HTML: :data-link t
[[file:images/qr-code.svg]]
*** TODO Using Embark to act on video
:PROPERTIES:
:CUSTOM_ID: embark-video
:END:
#+begin_src emacs-lisp
(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)))
#+end_src
*** Using Embark to act on audio
:PROPERTIES:
:CUSTOM_ID: embark-audio
:END:
#+begin_src emacs-lisp
(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)))
#+end_src
*** Using Embark to insert files as Org INCLUDEs
:PROPERTIES:
:CUSTOM_ID: using-embark-to-insert-files-as-org-includes
:END:
#+begin_src emacs-lisp
(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))
#+end_src
*** 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
#+begin_src emacs-lisp :var 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)))))
#+end_src
*** Whichkey and Embark
From https://github.com/oantolin/embark/wiki/Additional-Configuration#use-which-key-like-a-key-menu-prompt
#+begin_src emacs-lisp
(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))
#+end_src
*** 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
#+begin_src emacs-lisp
(defun my-sketch-insert-file-as-link (f)
(interactive "fSketch: ")
(insert (org-link-make-string (concat "sketch:" (file-name-nondirectory f))) "\n"))
#+end_src
#+begin_src emacs-lisp
(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))
#+end_src
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
#+begin_src emacs-lisp
(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)))
#+end_src
*** Embark and subed
:PROPERTIES:
:CUSTOM_ID: embark-subed
:END:
#+begin_src emacs-lisp
(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))))
#+end_src
*** Embark, symbols, and casual-symbol-overlay
:PROPERTIES:
:CUSTOM_ID: casual-symbol-overlay
:END:
Link: http://yummymelon.com/devnull/announcing-casual-symbol-overlay.html
#+begin_src emacs-lisp
(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)))
#+end_src
** Extended command list
CLOSED: [2022-01-03 Mon 21:01]
:PROPERTIES:
:CREATED: [2021-04-27 Tue 23:09]
:END:
This code allows me to select a command from a short list of functions so that I can prompt my memory better. I wonder if this makes sense considering transient and hydra make keyboard shortcuts easier.
#+begin_src emacs-lisp :eval no :tangle no
(my-execute-extended-command-from-list nil '(org-capture consult-buffer))
#+end_src
#+begin_src emacs-lisp
;;; 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))))))))