-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtkb-experimental.el
3200 lines (2837 loc) · 120 KB
/
tkb-experimental.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; tkb-experimental.el -- Experimental -*- coding-system: utf-8 -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; #$ Outputs the current filename. It's not supposed to be used in lisp code.
(message "%s" #$)
;;; Highlight cursor line: too annoying.
;;(global-hl-line-mode +1)
;;(global-hl-line-mode -1)
(eval-after-load "w3m"
'(define-key w3m-mode-map "f" #'w3m-find-file))
(defun tkb-unhex-region ()
(interactive)
(let* ((s (buffer-substring (point) (mark)))
(s* (url-unhex-string s t)))
(kill-region (point) (mark))
(insert s*)))
(defun tkb-unhex-current-kill ()
"Unhex url from current kill."
(interactive)
(let* ((s (current-kill 0))
(s (url-unhex-string s t)))
(kill-new s)))
(tkb-keys ((kbd "C-c k U h") #'tkb-unhex-current-kill))
(defun tkb-rst-print-subheads ()
"Print the recommend underlines with levels for the underline characters
recommended by the ReST quickref: http://tinyurl.com/47lkhk"
(interactive)
(with-work-buffer " *ReST heads"
(cl-loop for c
;; recommended list from the rst quickref: http://tinyurl.com/47lkhk
across "=-`:'\"~^_*+#<>"
for i from 1
do (let* ((s (format "Depth %d" i))
(u (make-string (length s) c)))
(insert (format "%s\n%s\n\n" s u))))))
(defun tkb-select-frame ()
(interactive)
(let* ((frames (cl-loop for frame in (frame-list)
collect (cons (frame-parameter frame 'name)
frame)
into frames finally return frames))
(frame-name (completing-read "Frame? " frames))
(frame (cdr (assoc-string frame-name frames))))
(raise-frame frame)
(select-frame frame)))
(defun tkb-select-frame-popup ()
(interactive)
(let* ((frames (cl-loop for frame in (frame-list)
collect (cons (frame-parameter frame 'name)
frame)
into frames finally return frames))
(frame (x-popup-menu t `("Pick a frame" ("frames" ,@frames)))))
(when frame
(raise-frame frame)
(select-frame-set-input-focus frame)
)))
(tkb-keys ((kbd "C-c A") 'tkb-select-frame-popup))
(defun tkb-show-frame-size-and-position ()
(interactive)
(destructuring-bind (x-offset . y-offset) (frame-position)
(let ((height (frame-height))
(width (frame-width)))
(message "%dx%d at %d,%d" width height x-offset y-offset))))
(defun tkb-load-file ()
"Load the current file into emacs lisp"
(interactive)
(load-file (buffer-file-name)))
(tkb-keys :keymap emacs-lisp-mode-map
((kbd "C-x L") #'tkb-load-file))
(defun tkb-locate-file (filename)
(interactive "M")
(message "file: %s" (locate-file filename load-path (get-load-suffixes))))
(when t ; Using org-capture now.
(progn ; Config for mobile org
;; Set to the location of your Org files on your local system
(setq org-directory "~/Org")
;; (setq org-adapt-indentation t) ;; No, maybe not.
(eval-after-load "org-mobile"
'(progn
;; Set to the name of the file where new notes will be stored
(setq org-mobile-inbox-for-pull "~/Org/flagged.org")
;; Set to <your Dropbox root directory>/MobileOrg.
(setq org-mobile-directory "~/Dropbox/Apps/MobileOrg")
(push "~/Org" org-mobile-files))))
(defconst tkb-org-journal (expand-file-name "~/Repos/tkb-notes/Org/journal.org"))
(defconst tkb-org-contacts (expand-file-name "~/Repos/tkb-notes/Org/contacts.org"))
(defconst tkb-org-books-read (expand-file-name "~/Repos/tkb-notes/Books/read.org"))
(defconst tkb-org-health (expand-file-name "~/Repos/tkb-notes/Health/interactions.org"))
(defconst tkb-org-notes (expand-file-name "~/Repos/tkb-notes/Org/notes.org"))
(defconst tkb-org-rpg (expand-file-name "~/Repos/tkb-notes/Org/rpg.org"))
(defconst tkb-org-tasks (expand-file-name "~/Repos/tkb-notes/Org/tasks.org"))
(defconst tkb-org-video (expand-file-name "~/Repos/tkb-notes/Org/video.org"))
(defconst tkb-org-blog-ideas (expand-file-name "~/Repos/tkb-notes/Org/blog-ideas.org"))
(defconst tkb-org-gaming-ideas (expand-file-name "~/Repos/tkb-notes/RPG/gaming-ideas.org"))
(defconst tkb-org-mpl-journal (expand-file-name "~/Repos/tkb-notes/MPL/Org/journal.org"))
(defconst tkb-org-mhst-journal (expand-file-name "~/Repos/tkb-notes/MPL/MHST/Org/mhst-journal.org"))
(defconst tkb-org-mpl-contacts (expand-file-name "~/Repos/tkb-notes/MPL/Org/contacts.org"))
(defconst tkb-org-mpl-notes (expand-file-name "~/Repos/tkb-notes/MPL/Org/notes.org"))
(defconst tkb-org-mpl-tasks (expand-file-name "~/Repos/tkb-notes/MPL/Org/tasks.org"))
(tkb-keys ((kbd "C-c k o c") #'org-capture))
(defvar tkb-org-year (format-time-string "%Y")
"The year the current emacs session was started, for use with org-capture.")
(defun tkb-org-capture-advice-update-year (&optional goto keys)
"Update ‘tkb-org-year’ and update the entry for adding a book in
‘org-capture-templates’ to use the new value."
(let ((new-year (format-time-string "%Y")))
(unless (string-equal tkb-org-year new-year)
(setf tkb-org-year new-year)
(setf (--> "b" (assoc it org-capture-templates)
(assoc 'file+olp it) (nth 2 it))
tkb-org-year))))
(advice-add 'org-capture :before #'tkb-org-capture-advice-update-year)
(setq org-capture-templates
`(("X" "EXPERIMENT" entry
(file+olp+datetree ,(expand-file-name "~/current/org/loud-experiment.org"))
"*** %^{Title} %U\n%i\n%?\n")
("b" "Add book about to read" entry
(file+olp ,(expand-file-name tkb-org-books-read)
,(format-time-string "%Y") "Read")
"*** : %c" :prepend t)
("j" "Journal" entry
(file+headline ,tkb-org-journal "Journal")
"* %^{Title} %U\n%i%?")
("c" "Contacts Log" entry
(file+headline ,tkb-org-contacts "Contacts")
"* %^{Title} %U\n%i%?\n")
("B" "Blog Ideas" entry
(file ,tkb-org-blog-ideas)
"* TODO %^{Title} %U\n%i%?\n")
("g" "Gaming Ideas" entry
(file ,tkb-org-gaming-ideas)
"* %^{Title} %U\n%i%?\n")
("h" "Health" entry
(file ,tkb-org-health)
"* %^{Title} %U\n%i%?\n")
("n" "Notes" entry
(file+headline ,tkb-org-notes "Notes")
"\n\n* %^{Title} %U\n%i\n%?\n%a\n\n")
("r" "RPG" entry
(file+headline ,tkb-org-rpg "RPG")
"\n\n* %^{Title} %U\n%i\n%?\n%a\n\n")
("t" "Tasks" entry
(file+headline ,tkb-org-tasks "Tasks")
"* TODO %^{Title} %U\n%i\n%?\n%a\n")
("v" "Video" entry
(file+headline ,tkb-org-video "Video")
"* TODO %^{Title} %U\n%^C%i%?\n")
("J" "MPL Journal" entry
(file+headline ,tkb-org-mpl-journal "MPL Journal")
"* %^{Title} %U\n%i\n%?\n")
("M" "MHST Journal" entry
(file+headline ,tkb-org-mhst-journal "MHST Journal")
"* %^{Title} %U\n%i\n%?\n")
("C" "MPL Contacts Log" entry
(file+headline ,tkb-org-mpl-contacts "MPL Contacts")
"* %^{Title} %U\n%i\n%?\n")
("N" "MPL Notes" entry
(file+headline ,tkb-org-mpl-notes "MPL Notes")
"\n\n* %^{Title} %U\n%i\n%?\n%a\n\n")
("T" "MPL Tasks" entry
(file+headline ,tkb-org-mpl-tasks "MPL Tasks")
"* TODO %^{Title} %U\n%i\n%?\n%a\n")))
;;(defvar tkb-org-files-map (make-sparse-keymap))
(define-prefix-command 'tkb-org-files-map)
(global-set-key (kbd "C-C k o F") 'tkb-org-files-map)
(progn
(defun tkb-find-org-blog-ideas ()
"Find Org Blog Ideas"
(interactive)
(find-file tkb-org-blog-ideas))
(global-set-key (kbd "C-c k o F B") #'tkb-find-org-blog-ideas)
(defun tkb-find-org-journal ()
"Find Org TKB's Journal"
(interactive)
(find-file tkb-org-journal))
(global-set-key (kbd "C-c k o F j") #'tkb-find-org-journal)
(defun tkb-find-org-contacts ()
"Find Org TKB's Contacts"
(interactive)
(find-file tkb-org-contacts))
(global-set-key (kbd "C-c k o F c") #'tkb-find-org-contacts)
(defun tkb-find-org-health ()
"Find Org TKB's Health"
(interactive)
(find-file tkb-org-health))
(global-set-key (kbd "C-c k o F h") #'tkb-find-org-health)
(defun tkb-find-org-notes ()
"Find Org TKB's Notes"
(interactive)
(find-file tkb-org-notes))
(global-set-key (kbd "C-c k o F n") #'tkb-find-org-notes)
(defun tkb-find-org-rpg ()
"Find Org TKB's RPG"
(interactive)
(find-file tkb-org-rpg))
(global-set-key (kbd "C-c k o F r") #'tkb-find-org-rpg)
(defun tkb-find-org-tasks ()
"Find Org TKB's Tasks"
(interactive)
(find-file tkb-org-tasks))
(global-set-key (kbd "C-c k o F t") #'tkb-find-org-tasks)
(defun tkb-find-org-video ()
"Find Org TKB's Video"
(interactive)
(find-file tkb-org-video))
(global-set-key (kbd "C-c k o F v") #'tkb-find-org-video)
(defun tkb-find-org-mpl-journal ()
"Find Org MPL Journal"
(interactive)
(find-file tkb-org-mpl-journal))
(global-set-key (kbd "C-c k o F J") #'tkb-find-org-mpl-journal)
(defun tkb-find-org-mhst-journal ()
"Find Org MHST Journal"
(interactive)
(find-file tkb-org-mhst-journal))
(global-set-key (kbd "C-c k o F M") #'tkb-find-org-mhst-journal)
(defun tkb-find-org-mpl-contacts ()
"Find Org MPL Contacts"
(interactive)
(find-file tkb-org-mpl-contacts))
(global-set-key (kbd "C-c k o F C") #'tkb-find-org-mpl-contacts)
(defun tkb-find-org-mpl-notes ()
"Find Org MPL Notes"
(interactive)
(find-file tkb-org-mpl-notes))
(global-set-key (kbd "C-c k o F N") #'tkb-find-org-mpl-notes)
(defun tkb-find-org-mpl-tasks ()
"Find Org MPL Tasks"
(interactive)
(find-file tkb-org-mpl-tasks))
(global-set-key (kbd "C-c k o F T") #'tkb-find-org-mpl-tasks))
(tkb-check-bindings (list (kbd "C-c k o C-c")))
(defun tkb-find-org-log-file ()
"Look in the current directory or its parents for a file named *-log.org
and return it."
;; So I can us M-x tkb-find-file-org-log and not bring up the
;; debugger on the call to error when testing.
(interactive)
(let ((default-directory default-directory)
(original-directory default-directory)
log-files)
(while (not (setq log-files (file-expand-wildcards "*-log.org" t)))
(cd "..")
(when (string-equal default-directory "/")
(error "unable to find *-log.org starting at %s"
original-directory)))
(car log-files)))
(defun tkb-find-file-org-log ()
"Look in the current directory or its parents for a file named *-log.org
and switch to a buffer visiting it."
(interactive)
(let* ((org-file (tkb-find-org-log-file)))
(find-file org-file)))
(tkb-keys ((kbd "C-c k o f") #'tkb-find-file-org-log))
;; (defalias 'x #'tkb-find-file-org-log)
(defun tkb-add-org-log ()
"Look in the current directory or its parents for a file named *-log.org
and add a log entry to it."
(interactive)
(let* ((org-file (tkb-find-org-log-file))
(org-capture-templates
`(("l" "Log" entry (file+headline ,org-file "Log")
"* %^{Title} %U\n%i\n%?\n"))))
(org-capture)))
(tkb-keys ((kbd "C-c k o l") #'tkb-add-org-log))
(tkb-keys ((kbd "C-c k o C-p") #'org-move-subtree-up))
(tkb-keys ((kbd "C-c k o C-n") #'org-move-subtree-down)))
(progn
;; See Info: (org)Activation.
;; The following lines are always needed. Choose your own keys.
(add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
(tkb-keys ((kbd "C-c k o s") #'org-store-link)
((kbd "C-c k o a") #'org-agenda))
(add-hook 'org-mode-hook 'turn-on-font-lock) ; org-mode buffers only
(setq org-startup-indented t))
(setq org-use-sub-superscripts '{}
org-export-with-sub-superscripts '{})
(tkb-keys ((kbd "C-c k o TAB") #'org-global-cycle))
(defun tkb-toggle-trailing-whitespace-display ()
(interactive)
(setq show-trailing-whitespace (not show-trailing-whitespace)))
(defun increment-filename (prefix &optional suffix always start)
"Generate a unique filename using PREFIX and optionally SUFFIX"
(let* ((suffix (if suffix suffix ""))
(start (if start start 0))
(sep1 "_")
(sep2 "_")
(fileprefix (concat prefix)))
(cl-loop for i from start
;; The zeroth filename doesn't have the number.
for testname = (if always
(format "%s%s%d%s" fileprefix sep2 i suffix)
(format "%s%s" fileprefix suffix))
then (format "%s%s%d%s" fileprefix sep2 i suffix)
until (not (file-exists-p testname))
finally return testname)))
(defun increment-filename-date (prefix &optional suffix always date)
"Generate a unique filename using PREFIX and optionally SUFFIX"
(let* ((suffix (if suffix suffix ""))
(sep1 "_")
(sep2 "_")
(date (if date
(if (listp date)
(format-time-string "%F" date)
date)
(format-time-string "%F")))
(fileprefix (concat prefix sep1 date)))
(cl-loop for i from 0
;; The zeroth filename doesn't have the number.
for testname = (if always
(format "%s%s%d%s" fileprefix sep2 i suffix)
(format "%s%s" fileprefix suffix))
then (format "%s%s%d%s" fileprefix sep2 i suffix)
until (not (file-exists-p testname))
finally return testname)))
(defun increment-filename-date-prefix (prefix &optional suffix always date)
"Generate a unique filename using PREFIX and optionally SUFFIX"
(let* ((suffix (if suffix suffix ""))
(sep1 "_")
(sep2 "_")
(date (if date
(if (listp date)
(format-time-string "%F" date)
date)
(format-time-string "%F")))
(dirname (file-name-directory prefix))
(filename (file-name-nondirectory prefix))
(fileprefix (concat dirname date sep1 filename)))
(cl-loop for i from 0
;; The zeroth filename doesn't have the number, unless ALWAYS
for testname = (if always
(format "%s%s%d%s" fileprefix sep2 i suffix)
(format "%s%s" fileprefix suffix))
then (format "%s%s%d%s" fileprefix sep2 i suffix)
until (not (file-exists-p testname))
finally return testname)))
(defun rename-buffer-uniquely ()
"Rename the current buffer to something that will be unique."
;; (rename-buffer x t) and (generate-new-buffer-name) only work if
;; buffers with the names to be avoided already exist. I want
;; something so I can rename a buffer so I can have things like two
;; greps running at once.
(interactive)
(rename-buffer (cl-loop with bufname =
(progn
(if (string-match (rx (and
string-start
(group (+ anything))
(group
(? "[0-9]+"))
string-end))
(buffer-name))
(match-string 1)
(buffer-name)))
for i from 1
with newname = (format "%s-%d" bufname i)
while (get-buffer newname)
finally return newname)))
(setq longlines-show-hard-newlines t)
(progn
;; http://jfm3-repl.blogspot.com/2006/06/emacs-tricks-1-completion.html
(require 'completion)
(dynamic-completion-mode)
(global-set-key (kbd "M-<return>") 'complete))
(unless (or window-system (member system-name '("consp.org")))
(load "tkb-tty-colors.el"))
(setq tkb-color-list '(("black" "white")
("white" "black")
("green" "black")
("black" "green")
("black" "wheat")
("black" "navajowhite1")))
(defun tkb-next-colors ()
"Goto the next next set of background and foreground colors."
(interactive)
(let* ((fg (frame-parameter nil 'foreground-color))
(bg (frame-parameter nil 'background-color))
(colors (list fg bg))
(x (cadr (member colors tkb-color-list))))
(if x
(cl-destructuring-bind (nfg nbg) x
(set-frame-parameter nil 'foreground-color nfg)
(set-frame-parameter nil 'background-color nbg)
(message "(%s %s" nfg nbg))
(cl-destructuring-bind (nfg nbg) (car tkb-color-list)
(set-frame-parameter nil 'foreground-color nfg)
(set-frame-parameter nil 'background-color nbg)
(message "(%s %s" nfg nbg)))))
(defun tkb-wheat ()
"Set foreground to black and background to wheat. See also ‘tkb-bad-wheat’."
(interactive)
(set-frame-parameter nil 'foreground-color "black")
(set-frame-parameter nil 'background-color "wheat"))
(defun tkb-bad-wheat ()
"Modify faces that are bad after ‘tkb-wheat’ to be white on black."
(interactive)
(let ((bad-wheat
'(completions-highlight
ffap
go-guru-hl-identifier-face
header-line-highlight
highlight
info-index-match
isearch
magit-diff-lines-boundary
magit-diff-lines-heading
match
mode-line-highlight
monky-log-head-label-bookmarks
monky-queue-negative-guard
mouse-drag-and-drop-region
next-error
next-error-message
query-replace
region
rst-level-1-face
rst-level-10-face
rst-level-2-face
rst-level-3-face
rst-level-4-face
rst-level-5-face
rst-level-6-face
rst-level-7-face
rst-level-8-face
rst-level-9-face
show-paren-mismatch
shr-mark
shr-selected-link
sldb-catch-tag-face
slime-highlight-face
speedbar-separator-face
transient-active-infix
tty-menu-enabled-face
widget-field
widget-single-line-field
xref-match
)))
(mapc #'(lambda (face)
(set-face-attribute face nil ':foreground "lightred"
':background "black"))
bad-wheat)))
(defun tkb-console-faces ()
(interactive)
(set-face-attribute 'font-lock-function-name-face nil ':background "green")
(set-face-attribute 'magit-diff-removed-highlight nil ':foreground "black"))
(defun nothing ()
;; Why did I need this???
(interactive)
nil)
(unless window-system
(xterm-mouse-mode 1))
(when nil
;; Apparently eval doesn't macroexpand the function part of (function ...)
(defmacro lambda* (&rest rest)
(let ((buf (get-buffer-create "*lambda**")))
(pp rest buf)
(let ((e (cl-transform-lambda rest nil)))
(pp e buf)
(let ((l `(lambda ,@(cdr e))))
(pp l buf)
(eval l)))))
(funcall (lambda* (&optional &key a &key (b 2)) (list a b)) :a 1)
((lambda* (&optional &key a &key (b 2)) (list a b))))
(defun tkb-python-indent-statement ()
(interactive)
(let (b e)
(save-excursion
(python-beginning-of-statement)
(setq b (point))
(python-end-of-statement)
(setq e (point))
(python-indent-region b e))))
(eval-after-load "python"
'
(progn
(tkb-keys :keymap python-mode-map
([(control meta ?q)] #'tkb-python-indent-statement))
))
;; This isn't working
(defadvice python-load-file (before tkb-py-load-file (file-name) activate)
"Expand FILE-NAME using `expand-file-name'."
(setq file-name (expand-file-name file-name)))
;; Should be part of MSWoe???
(setq potential-pythons
["c:/Python26/python.exe" "c:/Python25/python.exe" "python"])
(when-file (p potential-pythons)
(setq python-command p
python-python-command p))
(defun tkb-toggle-pythons ()
(interactive)
(let ((old python-command))
(if (not (string-equal python-command "python"))
(setq python-command "python")
(setq python-command
(find-if #'file-executable-p
potential-pythons)))
(message "Switching pythons from %s to %s" old python-command)))
(eval-after-load "rst.el"
'(begin
(modify-syntax-entry ?“ "(" rst-mode-syntax-table)
(modify-syntax-entry ?” ")" rst-mode-syntax-table)
(modify-syntax-entry ?‘ "(" rst-mode-syntax-table)
(modify-syntax-entry ?’ ")" rst-mode-syntax-table)
(custom-set-faces
'(rst-level-1-face ((t (:background "red"))) t)
'(rst-level-2-face ((t (:background "blue"))) t)
'(rst-level-3-face ((t (:background "yellow"))) t)
'(rst-level-4-face ((t (:background "magenta"))) t)
'(rst-level-5-face ((t (:background "white"))) t))))
(defun tkb-continue-line (n)
(interactive "P")
(if n
(setq n (prefix-numeric-value n))
(setq n 79))
(let ((start-char (save-excursion (beginning-of-line)
(buffer-substring (point) (1+ (point))))))
(save-excursion
(end-of-line)
(while (< (current-column) 80)
(insert start-char)))))
(tkb-keys ((kbd "C-c k c") #'tkb-continue-line))
(when-directory (d (expand-file-name "~/lib/data/fortunes"))
(setq fortune-dir d))
(setq fortune-file "~/lib/data/fortunes")
(tkb-keys ((kbd "C-c k h") #'hyperspec-lookup))
(autoload 'fortune-in-buffer "fortune")
(defun tkb-fortune-string (&optional file)
(interactive
(list
(if current-prefix-arg
(fortune-ask-file)
fortune-dir))) ;was fortune-file
(save-excursion
(fortune-in-buffer t file)
(set-buffer fortune-buffer-name)
(let* ((fortune (buffer-string)))
;;(substitute ?\ ?\n fortune)
fortune)))
(defconst tkb-random-me
["Crazy K Ranch"
"Sorry to say"
"Simple is as simple does"
"WTF!"
"I have SEEN the CONSING!!"
"Yow!"
"Yow! Legally-imposed CULTURE-reduction is CABBAGE-BRAINED!"
])
(defun tkb-random-me ()
(interactive)
(aref tkb-random-me (random (length tkb-random-me))))
(modify-coding-system-alist 'file "\\.rst\\'" 'utf-8-unix)
(modify-coding-system-alist 'file "\\.org\\'" 'utf-8-unix)
;; This is for unison. Unfortunately, you can't include the .unison.
(modify-coding-system-alist 'file "\\.prf\\'" 'utf-8-unix)
;; http://www.neverfriday.com/sweetfriday/2008/06/emacs-tip-word-counting-with-a.html
(defun wc ()
(interactive)
(message "Word count: %s" (how-many "\\w+" (point-min) (point-max))))
(defun tkb-goto-info-node (arg)
"Goto an Info node in normal text, specified as \"Info: (FILE)Node.\"
where the \"FILE\" is optional and the \".\" can also be a \",\"."
(interactive "P")
(require 'info)
(save-excursion
(when (re-search-forward "Info: \\(\\((.*)\\)*[^.,]*\\)" nil t)
(let ((s (match-string 1)))
(when (y-or-n-p (concat "Goto Info node " s " ? "))
(Info-goto-node s arg))))))
(tkb-keys ((kbd "C-c k i") #'tkb-goto-info-node))
;; Info: (info)Go to node
;; http://article.gmane.org/gmane.emacs.macintosh.osx/107
(standard-display-8bit 128 255)
;; http://nex-3.com/posts/45-efficient-window-switching-in-emacs
(tkb-keys :just-warn
([M-left] 'windmove-left) ; move to left windnow
([M-right] 'windmove-right) ; move to right window
([M-up] 'windmove-up) ; move to upper window
([M-down] 'windmove-down) ; move to downer window
)
(defun tkb-move-frame-left ()
"Move the current frame left by 1/10th the width of the physical montor."
(interactive)
(let* ((left (frame-parameter nil 'left))
(monitor-display-width (caddr (frame-monitor-attribute 'geometry)))
(tenth-width (/ monitor-display-width 10))
(new-left (- left tenth-width)))
(set-frame-parameter nil 'left new-left)))
(tkb-keys ((kbd "C-c k F l") #'tkb-move-frame-left))
(defun tkb-move-frame-right ()
"Move the current frame right by 1/10th the width of the physical montor."
(interactive)
(let* ((left (frame-parameter nil 'left))
(monitor-display-width (caddr (frame-monitor-attribute 'geometry)))
(tenth-width (/ monitor-display-width 10))
(new-left (+ left tenth-width)))
(set-frame-parameter nil 'left new-left)))
(tkb-keys ((kbd "C-c k F r") #'tkb-move-frame-right))
(defadvice browse-url (before tkb-advise-browse-url activate)
(let ((all-args (ad-get-args 0))
(rest-args (ad-get-args 1)))
(when (and (not rest-args)
(boundp 'fork)
(symbol-value 'fork))
(ad-set-args 1 (symbol-value 'fork)))
(message "tkb-advise-browse-url: bound: %S args: %S" (boundp 'fork) args)))
(defun tkb-browse-url-eww (url &optional args)
"Invoke the eww browser (inside emacs) on URL. It the optional second
argument ARGS is true open in a new buffer."
(message "args: %S" args)
(eww url (and args 4)))
(defun tkb-toggle-eww ()
(interactive)
(cond ((eq browse-url-browser-function 'browse-url-default-browser)
(message "Switching to EWW for opening URLs.")
(setq browse-url-browser-function #'tkb-browse-url-eww))
(t
(message "Switching to browser-url-default-browser for opening URLs.")
(setq browse-url-browser-function #'browse-url-default-browser))))
(defun tkb-cleanup-title (title)
(let ((s
(substitute ?- 32
(remove-if-not #'(lambda (c) (or (memq c '(?- 32 ?.))
(and (<= ?a c)
(<= c ?z))
(and (<= ?0 c)
(<= c ?9))))
(downcase title)))))
(while (string-match "\\([^[:alnum:]]\\)\\1+" s)
(let ((c (match-string 1 s))
(b (match-beginning 0))
(e (match-end 0)))
(setq s (concat (substring s 0 (1+ b)) (substring s e)))))
(while (string-match "\\.-" s)
(setq s (concat (substring s 0 (match-beginning 0))
"-"
(substring s (match-end 0)))))
s))
(defun tkb-insert-rst-section (title char &optional above)
(let* ((s (tkb-rst-section-underline title char)))
(when above (insert s "\n"))
(insert title "\n" s "\n")))
(defun tkb-rst-section-underline (title char)
(make-string (length title) char))
;; http://www.unicode.org/Public/UNIDATA/UnicodeData.txt
;; describe-char-unicodedata-file
(defun tkb-load-unicode-txt ()
(let* ((udf-url "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt")
(udf-dest "~/tmp/UnicodeData.txt")
(udf-dir (file-name-directory udf-dest)))
(if (file-readable-p udf-dest)
(setq describe-char-unicodedata-file udf-dest)
(when (y-or-n-p (format "You need to download %s ! Do it? " udf-url))
;; Really weird: wget -O 'file' complains that file doesn't exist!
(unless (file-directory-p udf-dir)
(make-directory udf-dir t))
(let ((cmd (format "cd ~/tmp/ && wget -O %s --progress=dot '%s' &"
udf-dest udf-url)))
(message "cmd: %s" cmd)
(shell-command cmd)
(setq describe-char-unicodedata-file "~/tmp/UnicodeData.txt"))))))
(eval-after-load "tkb-last" #'tkb-load-unicode-txt)
(defun fmt-duration (time)
(cl-flet ((f (duration unit)
(when duration (format (if (floatp duration)
"%f%s"
"%d%s")
duration unit))))
(cl-destructuring-bind (hi lo ms) time
(let ((s (+ hi lo))
(x "")
(d nil)
(h nil)
(m nil))
(when (>= s 86400)
(setq d (/ s 86400)
s (% s 86400)))
(when (>= s 3600)
(setq h (/ s 3600)
s (% s 3600)))
(when (>= s 60)
(setq m (/ s 60)
s (% s 60)))
(unless (zerop ms)
(setq s (+ s (/ ms 1000000.0))))
(mapconcat 'identity
(remove-if #'null
(list (f d "d") (f h "h") (f m "m") (f s "s")))
" ")))))
(defmacro time (&rest body)
`(let ((start (current-time)))
(unwind-protect
(progn ,@body)
(let* ((end (current-time))
(delta (time-subtract end start))
(timebuf (get-buffer-create " *timing*")))
(display-buffer timebuf)
(princ (format "%s\n" (fmt-duration delta)) timebuf)))))
(defun tkb-blog (title tags category date &optional title-prefix no-increment)
;; This is obsolete.
"Create a blog entry, prompting for various values and creating the
appropriate directory structure."
;;(interactive "sTitle: \nsTags: \nDCategory: \nsDate: ")
(interactive (list
(read-string "Title: ")
(read-string "Tags: ")
(read-directory-name "Category: " "~/myblog/entries/")
(read-string "Date: ")))
(let* ((date-time (if (listp date)
date
(if (empty-string-p date)
nil
(tkb-parse-iso-date date))))
(no-spaces (tkb-cleanup-title title))
(filename (funcall
(if no-increment
#'(lambda (&rest args)
(apply #'concatenate `(string ,@args)))
#'increment-filename)
(concat (file-name-as-directory
(expand-file-name category "~/myblog/entries/"))
no-spaces)
".rst" nil))
(dirname (file-name-directory filename))
(published (format-time-string "#published %Y-%m-%d %H:%M:%S"
date-time)))
(if (not (file-directory-p dirname))
(make-directory dirname 'and-parents))
;; FIXME: warn about existing files, ask to increment???
(find-file filename)
(unless (file-exists-p filename)
(if title-prefix
(insert title-prefix))
(insert title)
(insert "\n")
(insert published)
(insert "\n")
(unless (zerop (length tags))
(insert (concat "#tags " tags))
(insert "\n")))))
(defun tkb-reading-monthly (date)
(interactive "sDate: ")
(let* ((date-time (if (empty-string-p date)
(current-time)
(tkb-parse-iso-date date)))
(tags "recent reading")
(category (format-time-string "books/read/%Y/%m" date-time)))
(tkb-blog (format-time-string "Recent Reading: %B %Y" date-time)
tags category date-time nil t)))
(defun tkb-reading (authors tags date)
"Create a blog entry about my recent reading"
(interactive "sAuthors: \nsTags: \nsDate: ")
(let* ((date-time (if (empty-string-p date)
(current-time)
(tkb-parse-iso-date date)))
(tags (concat "recent reading" (if (empty-string-p tags)
""
(concat "," tags))))
(category (format-time-string "books/read/%Y/%m" date-time)))
(tkb-blog authors tags category date-time "Recent Reading: ")))
(defun tkb-viewing (titles tags date)
"Create a blog entry about my recent viewing"
(interactive "sTitles: \nsTags: \nsDate: ")
(let* ((date-time (if (empty-string-p date)
(current-time)
(tkb-parse-iso-date date)))
(tags (concat "recent viewing" (if (empty-string-p tags)
""
(concat "," tags))))
(category (format-time-string "media/viewing/%Y/%m" date-time)))
(tkb-blog titles tags category date-time "Recent Viewing: ")))
(when-directory (d (expand-file-name "~/lib/emacs/others/misc"))
;; used by my hooks for rst and formerly asciidoc
(add-to-list 'load-path d)
;; look at https://github.com/ndw/xmlunicode for xmlunicode.el
;; xmlunicode-character-list.el. xmlunicode.el provides the
;; "smart-unicode-*" functions.
(load-library "unichars")
(load-library "xmlunicode")
(defun tkb-describe-character (before)
"Describe the character after point (before if a prefix was specified)
if it is a Unicode character."
(interactive "P")
(let* ((char (if before (char-before) (char-after)))
(info (assoc (encode-char char 'ucs) unicode-character-list))
(info (if info (cons (format "#x%X" (car info)) info)
"Not in this list, try 'C-c k D' (describe-char)")))
(message "%S" info)))
(tkb-keys ((kbd "C-c k d") #'tkb-describe-character))
(defun tkb-insert-unicode-description (before)
"Insert a description of the character after point (before if a prefix was
specified) if it is a Unicode character."
(interactive "P")
(let* ((char (if before (char-before) (char-after)))
(name (get-char-code-property char 'name))
(old-name (get-char-code-property char 'old-name))
(desc (format "U+%04X %c %s%s" char char name
(if old-name (concat " (" old-name ")") ""))))
(if before (delete-char -1) (delete-char 1))
(insert desc)))
(tkb-keys ((kbd "C-c k U") #'tkb-insert-unicode-description))
(define-minor-mode tkb-smart-unicode-mode
"Toggle smart unicode punctuation" nil " ♻⚔☣☥☸◉⅙✽☮" ; "✘▧▧⚅☑☢☹☺♠♥♦♣♨"
:keymap '(("\"" . unicode-smart-double-quote)
("'" . unicode-smart-single-quote)
("-" . unicode-smart-hyphen)
("." . unicode-smart-period)))
(defadvice unicode-smart-hyphen (after tkb-after-unicode-smart-hyphen last
activate compile)
(tkb-describe-character t))
)
(when nil ;retiring doc-mode 2019-11-03
(autoload 'doc-mode "doc-mode")
(add-to-list 'auto-mode-alist '("\\.adc$" . doc-mode))
(eval-after-load "compile"
'(add-to-list 'compilation-error-regexp-alist
'("^ERROR:[ \t]*\\([[:alpha:]][-[:alnum:].]+\\):[ \t]*line[ \t]*\\([0-9]+\\):" 1 2)))
(eval-after-load "doc-mode" '
(progn
(defun tkb-asciidoc-version-increment ()
(interactive)
(save-excursion
(goto-char (point-min))
(when (re-search-forward "^v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\),[ \t]+\\(.+\\)"
nil t)
(let* ((part3 (match-string 3))
(date (match-string 4))
(n (string-to-number part3))
(new3 (format "%d" (1+ n))))
(when (and (not (string-equal date (tkb-timestamp)))
(y-or-n-p (format "Sure replace %s with %s? " part3 new3)))
(replace-match new3 nil t nil 3)
(replace-match (tkb-timestamp) nil t nil 4))
nil))))
(defun bind-doc-mode-keys ()
(set-language-environment "utf-8")
(define-key doc-mode-map "\"" 'unicode-smart-double-quote)
(define-key doc-mode-map "'" 'unicode-smart-single-quote)
(define-key doc-mode-map "-" 'unicode-smart-hyphen)
(define-key doc-mode-map "." 'unicode-smart-period)
;; display UniChar menu when in doc mode
(define-key doc-mode-map [menu-bar unichar]
(cons "UniChar" unicode-character-menu-map))
;; set input method to "xml" (xmlunicode) when in doc mode
(set-input-method 'xml))
(defun tkb-doc-mode-hook ()
(add-hook 'write-contents-functions #'tkb-asciidoc-version-increment))
(add-hook 'doc-mode-hook #'bind-doc-mode-keys)
(add-hook 'doc-mode-hook #'tkb-doc-mode-hook)
)))
(progn
;; Yank from emacs in screen
(defun tkb-yank-from-screened-emacs ()
(interactive)
(let ((text (current-kill 0)))
(when (string-match "^\\(.*\\)\\\\\n[ \t]*\\(.*\\)$" text)
(insert (match-string 1 text))
(insert (match-string 2 text)))))
;; This doesn't handle multiple lines properly
(defun tkb-cleanup-yank-from-screened-emacs ()
(interactive)
(let ((text (current-kill 0)))
(while (string-match "\\`\\(.*\\)\\\\\n\\([[:ascii:][:nonascii:]]*\\)\\'" text)
(setq text (concat (match-string 1 text) (match-string 2 text))))
(kill-new text))))
(defun t:kill-host-from-url ()
(interactive)
(require 'url-parse)
(let* ((url (current-kill 0))
(host (url-host (url-generic-parse-url url))))
(kill-new host)
(message "Killed %s\nto %s" url host)))
(tkb-keys ((kbd "C-c k u h") #'t:kill-host-from-url))
(when nil
;; 2023-06-27: I no longer remember what I was trying to do with this
;; function, so I'm going to steal it's keybinding for something else.
(defun tkb-fill-list ()
(interactive)
(save-excursion
(let ((paragraph-start "\f\\|[ ]*$\\|\\(?:[*\f]+\\)")
(paragraph-separate "[ \f]*$\\|\\(?:[*\f]+\\)"))
(message "Buffer local variables: \n****\n%S\n****\n" (buffer-local-variables))
(sit-for 1)
(message "start: %S sep: %S" paragraph-start paragraph-separate)
(sit-for 1)
(mark-paragraph)
(narrow-to-region (point) (mark))
(sit-for 1)
(fill-paragraph nil))))
(tkb-keys ((kbd "C-c k f") 'tkb-fill-list)))
;; Gimme some register compatibility binding love!
(tkb-keys
((kbd "C-x /") 'point-to-register) ; C-x / became C-x r SPC
((kbd "C-x j") 'jump-to-register) ; C-x j became C-x r j
((kbd "C-x x") 'copy-to-register) ; C-x x became C-x r s
((kbd "C-x g") 'insert-register) ; C-x g became C-x r i
)
(setq list-faces-sample-text