-
Notifications
You must be signed in to change notification settings - Fork 5
/
utility.el
3142 lines (2817 loc) · 104 KB
/
utility.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
;; utility.el --- My utility.el -*- lexical-binding: t -*-
;; "my-" and "ad:" functions associated with my 'init.el'
(unless (featurep 'postpone)
(call-interactively 'postpone-pre))
(unless noninteractive
(defvar my-utility-start (current-time)))
;;;###autoload
(defun ad:measure-exec-time (f &rest arg)
"If `measure-exec-time-list' is non-nil, measure exe time for each function."
(if measure-exec-time-list
(let ((inhibit-message nil)
(message-log-max 5000)
(begin (current-time)))
(apply f arg)
(message
(format "--- %.3f[ms] %S"
(* 1000 (float-time (time-subtract (current-time) begin)))
(if (byte-code-function-p f)
nil ;; not includes closure
f)))) ;; FIXME
(apply f arg)))
;;;###autoload
(defun ad:do-after-load-evaluation (abs-file)
"Evaluate all `eval-after-load' forms, if any, for ABS-FILE.
ABS-FILE, a string, should be the absolute true name of a file just loaded.
This function is called directly from the C code."
;; Run the relevant eval-after-load forms.
(dolist (a-l-element after-load-alist)
(when (and (stringp (car a-l-element))
(string-match-p (car a-l-element) abs-file))
;; discard the file name regexp
(mapc #'funcall (cdr a-l-element))))
;; Complain when the user uses obsolete files.
(when (string-match-p "/obsolete/[^/]*\\'" abs-file)
;; Maybe we should just use display-warning? This seems yucky...
(let* ((file (file-name-nondirectory abs-file))
(package (intern (substring file 0
(string-match "\\.elc?\\>" file))
obarray))
(msg (unless (memq package my-exclude-deprecated-packages)
(format "Package %s is deprecated" package)))
(fun (lambda (msg) (message "%s" msg))))
(when (or (not (fboundp 'byte-compile-warning-enabled-p))
(byte-compile-warning-enabled-p 'obsolete package))
(cond
((bound-and-true-p byte-compile-current-file)
;; Don't warn about obsolete files using other obsolete files.
(unless (and (stringp byte-compile-current-file)
(string-match-p "/obsolete/[^/]*\\'"
(expand-file-name
byte-compile-current-file
byte-compile-root-dir)))
(byte-compile-warn "%s" msg)))
(noninteractive (funcall fun msg)) ;; No timer will be run!
(t (run-with-idle-timer 0 nil fun msg))))))
;; Finally, run any other hook.
(run-hook-with-args 'after-load-functions abs-file))
;;;###autoload
(defun future-time-p (time)
"Return non-nil if provided TIME formed of \"10:00\" is the future time."
(not (time-less-p
(apply 'encode-time
(let ((t1 (decode-time))
(t2 (parse-time-string time)))
(setf (nth 0 t1) 0)
(setf (nth 1 t1) (nth 1 t2))
(setf (nth 2 t1) (nth 2 t2))
t1))
(current-time))))
;; For instance,
;; (when (future-time-p "10:00") (run-at-time...))
;; (eval-when-compile
;; (message "Loading gcmh... %s" (featurep 'gcmh))
;; (require 'gcmh))
(defvar my-gcmh-idlegc-p nil)
;;;###autoload
(defun ad:garbage-collect (f)
(unless my-gcmh-idlegc-p
(message "[gcmh] Garbage collecting...")
(message "[gcmh] Garbage collecting...done (%.3fs)"
(gcmh-time (funcall f)))))
;;;###autoload
(defun ad:gcmh-idle-garbage-collect (f)
(let ((my-gcmh-idlegc-p t))
(funcall f)))
;;;###autoload
(defun my-gcmh-activate ()
(cancel-timer my-gcmh-timer)
(gcmh-mode 1))
;;;###autoload
(defun my-native-comp-p ()
(when (fboundp 'native-comp-available-p)
(native-comp-available-p)))
;;;###autoload
(defun my-native-comp-packages-done ()
(message "Native Compilation...done"))
;;;###autoload
(defun my-emacs-lisp-mode-conf ()
(setq-local indent-tabs-mode t)
(setq-local tab-width 8)
(setq indent-line-function 'lisp-indent-line))
;;;###autoload
(defun my-org-hide-drawers-all ()
(when (eq major-mode 'org-mode)
(org-cycle-hide-drawers 'all)))
;;;###autoload
(defun my-auto-revert-activate ()
(global-auto-revert-mode 1)
(remove-hook 'find-file-hook #'my-auto-revert-activate))
;; see also a configuration of `directory-abbrev-alist'
;;;###autoload
(defun my-shorten-default-directory ()
"Enforce to replace \"/home/...\" with \"~/\"."
(setq default-directory (abbreviate-file-name default-directory)))
;;;###autoload
(defun my-private-conf-activate ()
(cancel-timer my-private-conf-timer)
;; (require 'epa)
(when (and (file-exists-p "~/Dropbox/config/private.el.gpg")
(eq system-type 'darwin)
(not (featurep 'private)))
(unless (ignore-errors (require 'private "private.el.gpg" t))
(user-error "GPG decryption error (private.el)"))))
;;;###autolaod
(defun my-lock-secret-buffer (&optional file)
(when (and (stringp file)
(buffer-live-p (get-buffer file)))
(kill-buffer file)
(let ((message-log-max nil))
(message "--- %s is locked." file))))
;;;###autoload
(defun my-isearch-ime-deactivate-sticky ()
(unless (region-active-p)
(mac-ime-deactivate-sticky)))
;;;###autoload
(defun my-toggle-ime-ns ()
"Toggle IME."
(interactive)
(if (my-ime-active-p) (my-ime-off) (my-ime-on)))
;;;###autoload
(defun my-working-text-face-on ()
(if (or isearch-mode
(minibufferp))
(custom-set-faces
'(ns-working-text-face nil))
(custom-set-faces
'(ns-working-text-face
((((background dark))
:background "#594d5d" :underline "LightSlateBlue")
(t (:background "#fff0de" :underline "gray20")))))))
;;;###autoload
(defun my-working-text-face-off ()
(if (or isearch-mode
(minibufferp))
(custom-set-faces
'(ns-working-text-face nil))
(custom-set-faces
'(ns-working-text-face
((((background dark)) :background "#484c5c" :underline "white")
(t (:background "#DEEDFF" :underline "DarkOrchid3")))))))
;;;###autoload
(defun my-ns-org-heading-auto-ascii ()
"IME off, when the cursor on org headings."
;; (message "%s" (frame-focus-state (selected-frame)))
(when (and
(fboundp 'frame-focus-state)
(frame-focus-state)
(eq major-mode 'org-mode)
(boundp 'org-agenda-buffer-name)
(or (looking-at org-heading-regexp)
(equal (buffer-name) org-agenda-buffer-name))
(my-ime-active-p))
(my-ime-off)))
;;;###autoload
(defun ad:mark-sexp (f &optional arg allow-extend)
"Set mark ARG sexps from point.
When the cursor is at the end of line or before a whitespace, set ARG -1."
(interactive "P\np")
(funcall f (if (and (not (bolp))
(not (eq (preceding-char) ?\ ))
(not (memq (following-char) '(?\( ?\< ?\[ ?\{)))
(or (eolp)
(eq (following-char) ?\ )
(memq (preceding-char) '(?\) ?\> ?\] ?\}))))
-1 arg)
allow-extend))
;;;###autoload
(defun ad:er:mark-sexp (f &optional arg allow-extend)
"If the cursor is on a symbol, expand the region along the symbol."
(interactive "P\np")
(if (and (not (use-region-p))
(symbol-at-point)
(not (memq (following-char) '(?\( ?\< ?\[ ?\{)))
(not (memq (preceding-char) '(?\) ?\> ?\] ?\}))))
(er/mark-symbol)
(funcall f arg allow-extend)))
;;;###autoload
(defun my-bm-save-all ()
(bm-buffer-save-all)
(bm-repository-save))
;;;###autoload
(defun my-toggle-bm ()
"bm-toggle with updating history"
(interactive)
(let ((bm (concat
(buffer-name) "::"
(if (and (equal major-mode 'org-mode)
(not (org-before-first-heading-p)))
(nth 4 (org-heading-components))
(format "%s" (line-number-at-pos))))))
(if (bm-bookmark-at (point))
(bookmark-delete bm)
(bookmark-set bm)))
(bm-toggle)
(bm-buffer-save-all)
(bm-repository-save))
;;;###autoload
(defun my-bm-next ()
"bm-next with org-mode"
(interactive)
(bm-next)
(when (and (equal major-mode 'org-mode)
(not (org-before-first-heading-p)))
(widen)
(org-overview)
(org-reveal)
(org-cycle-hide-drawers 'all)
(org-show-entry)
(show-children)
(org-show-siblings)))
;;;###autoload
(defun counsel-bm-get-list (bookmark-overlays)
(-map (lambda (bm)
(with-current-buffer (overlay-buffer bm)
(let* ((line (replace-regexp-in-string
"\n$" ""
(buffer-substring (overlay-start bm)
(overlay-end bm))))
;; line numbers start on 1
(line-num
(+ 1 (count-lines (point-min) (overlay-start bm))))
(name (format "%s:%d - %s" (buffer-name) line-num line)))
`(,name . ,bm))))
bookmark-overlays))
;;;###autoload
(defun counsel-bm ()
(interactive)
(let* ((bm-list (counsel-bm-get-list (bm-overlays-lifo-order t)))
(bm-hash-table (make-hash-table :test 'equal))
(search-list (-map (lambda (bm) (car bm)) bm-list)))
(-each bm-list (lambda (bm)
(puthash (car bm) (cdr bm) bm-hash-table)
))
(ivy-read "Find bookmark(bm.el): "
search-list
:require-match t
:keymap counsel-describe-map
:action (lambda (chosen)
(let ((bookmark (gethash chosen bm-hash-table)))
(switch-to-buffer (overlay-buffer bookmark))
(bm-goto bookmark)
))
:sort t)))
;;;###autoload
(defun ad:bm-show-mode ()
"Enable truncate mode when showing bm list."
(toggle-truncate-lines 1))
;;;###autoload
(defun my-centered-cursor-activate () (centered-cursor-mode 1))
;;;###autoload
(defun my-centered-cursor-deactivate () (centered-cursor-mode -1))
;;;###autoload
(defun my-smart-mark-activate ()
(smart-mark-mode 1)
(remove-hook 'find-file-hook #'my-smart-mark-activate))
;;;###autoload
(defun ad:smart-mark-restore-cursor ()
"Restore cursor position saved just before mark."
(when smart-mark-point-before-mark
(when (> smart-mark-point-before-mark 1)
;; To avoid to jump to the beginning of the buffer
(goto-char smart-mark-point-before-mark))
(setq smart-mark-point-before-mark nil)))
;;;###autoload
(defun ad:smart-mark-set-restore-before-mark (&rest _arg)
(unless (memq this-command
'(er/expand-region er/mark-symbol er/contract-region))
(setq smart-mark-point-before-mark (point))))
;;;###autoload
(defun ad:er:keyboard-quit ()
(when (memq last-command '(er/expand-region er/contract-region))
(when smart-mark-point-before-mark
(goto-char smart-mark-point-before-mark))))
;;;###autoload
(defun ad:er:pre:keyboard-quit ()
(when (memq last-command '(er/expand-region er/contract-region))
(er/contract-region 0)
;; (when (> smart-mark-point-before-mark 1) ;; FIXME
;; (goto-char smart-mark-point-before-mark))
))
;;;###autoload
(defun my-syntax-subword-activate (&rest arg)
(unless (featurep 'syntax-subword)
(global-syntax-subword-mode 1))
(advice-remove 'forward-word #'my-syntax-subword-activate)
(advice-remove 'backward-word #'my-syntax-subword-activate)
arg)
;;;###autoload
(defun ad:syntax-subword-kill (&optional n)
"Replace `kill-region' with `delete-region'."
(interactive "^p")
(let ((beg (point))
(end (save-excursion (syntax-subword-forward n) (point))))
(delete-region beg end)))
;;;###autoload
(defun my-time-stamp ()
(setq time-stamp-format
(if (eq major-mode 'org-mode)
"[%Y-%02m-%02d %3a]" ;; "%04y %02H:%02M"
"%Y-%02m-%02d"))
(if (boundp 'org-tree-slide-mode)
(unless org-tree-slide-mode
(time-stamp))
(time-stamp)))
;;;###autoload
(defun ad:isearch-mode (f forward &optional regexp op-fun recursive-edit
regexp-function)
(if (and transient-mark-mode mark-active (not (eq (mark) (point))))
(progn
(isearch-update-ring (buffer-substring-no-properties (mark) (point)))
(deactivate-mark)
(funcall f forward regexp op-fun recursive-edit regexp-function)
(if (not forward)
(isearch-repeat-backward)
(goto-char (mark))
(isearch-repeat-forward)))
(funcall f forward regexp op-fun recursive-edit regexp-function)))
;;;###autoload
(defun my-orgalist-activate ()
(when (require 'orgalist nil t)
(orgalist-mode 1))) ;; originally orgstruct-mode
;;;###autoload
(defun ad:add-change-log-entry-other-window ()
(when view-mode
(View-exit-and-edit)))
;;;###autoload
(defun org-info-ja (&optional node)
"(Japanese) Read documentation for Org-mode in the info system.
With optional NODE, go directly to that node."
(interactive)
(info (format "(org-ja)%s" (or node ""))))
;;;###autoload
(defun my-json-mode-beautify ()
(when (eq major-mode 'json-mode)
(json-mode-beautify (point-min) (point-max))))
;;;###autoload
(defun my-json-pretty-print-buffer ()
(when (eq major-mode 'json-mode)
(json-pretty-print-buffer)))
;;;###autoload
(defun my-auto-view ()
"Open a file with `view-mode'."
(when (file-exists-p buffer-file-name)
(when (and my-auto-view-regexp
(string-match my-auto-view-regexp buffer-file-name))
(view-mode 1))
(dolist (dir my-auto-view-dirs)
(when (eq 0 (string-match (expand-file-name dir) buffer-file-name))
(view-mode 1)))))
;;;###autoload
(defun my-org-view-next-heading ()
(interactive)
(if (and (derived-mode-p 'org-mode)
(org-at-heading-p))
(org-next-visible-heading 1)
(next-line)))
;;;###autoload
(defun my-org-view-previous-heading ()
(interactive)
(if (and (derived-mode-p 'org-mode)
(org-at-heading-p))
(org-previous-visible-heading 1)
(previous-line)))
;;;###autoload
(defun my-view-tab ()
(interactive)
(if (and (derived-mode-p 'org-mode)
(or (org-at-heading-p)
(org-at-property-drawer-p)))
(let ((view-mode nil))
(org-cycle))
(when (require 'origami nil t)
(origami-toggle-node (current-buffer) (point)))))
;;;###autoload
(defun my-view-shifttab ()
(interactive)
(if (derived-mode-p 'org-mode)
(let ((view-mode nil))
(org-shifttab))
(when (require 'origami nil t)
(origami-toggle-all-nodes (current-buffer)))))
;;;###autoload
(defun my-unlock-view-mode ()
(when view-mode
(View-exit-and-edit)))
;;;###autoload
(defun my-view-exit ()
(interactive)
(if (use-region-p) (my-eval-region) (View-exit)))
;;;###autoload
(defun ad:view--enable () (my-mode-line-on))
;;;###autoload
(defun ad:view--disable () (my-mode-line-off))
;;;###autoload
(defun ad:switch-to-buffer (&rest _arg)
(when (and (not view-mode)
(member (buffer-name) my-auto-view-buffers))
(view-mode 1)))
;;;###autoload
(defun my-web-indent-fold ()
(interactive)
(web-mode-fold-or-unfold)
(web-mode-buffer-indent)
(indent-for-tab-command))
;;;###autoload
(defun my-flyspell-ignore-nonascii (beg end _info)
"incorrect判定をASCIIに限定"
(string-match "[^!-~]" (buffer-substring beg end)))
(add-hook 'flyspell-incorrect-hook #'my-flyspell-ignore-nonascii)
;;;###autoload
(defun my-flyspell-on ()
(cond
((memq major-mode major-mode-with-flyspell)
(turn-on-flyspell))
((memq major-mode major-mode-with-flyspell-prog)
(flyspell-prog-mode))
(t
nil)))
;;;###autoload
(defun my-flyspell-off ()
(when (memq major-mode my-flyspell-target-modes)
(turn-off-flyspell)))
;;;###autoload
(defun ad:YaTeX-insert-begin-end (env region-mode)
"Insert \\begin{mode-name} and \\end{mode-name}.
This works also for other defined begin/end tokens to define the structure."
(setq YaTeX-current-completion-type 'begin)
(let*((ccol (current-column)) beg beg2 exchange
(_arg region-mode) ;for old compatibility
(indent-column (+ ccol YaTeX-environment-indent))(_i 1) _func)
(if (and region-mode (> (point) (mark)))
(progn (exchange-point-and-mark)
(setq exchange t
ccol (current-column)
indent-column (+ ccol YaTeX-environment-indent))))
;;VER2 (insert "\\begin{" env "}" (YaTeX-addin env))
(setq beg (point))
(YaTeX-insert-struc 'begin env)
(setq beg2 (point))
(insert "\n")
(indent-to indent-column)
(save-excursion
;;indent optional argument of \begin{env}, if any
(while (> (point-beginning-of-line) beg)
(skip-chars-forward "\\s " (point-end-of-line))
(indent-to indent-column)
(forward-line -1)))
(require 'yatexenv)
(if region-mode
;;if region-mode, indent all text in the region
(save-excursion
(if (fboundp (intern-soft (concat "YaTeX-enclose-" env)))
(funcall (intern-soft (concat "YaTeX-enclose-" env))
(point) (mark))
(while (< (progn (forward-line 1) (point)) (mark))
(if (eolp) nil
(skip-chars-forward " \t\n")
(indent-to indent-column))))))
(if region-mode (exchange-point-and-mark))
(indent-to ccol)
;;VER2 (insert "\\end{" env "}\n")
(YaTeX-insert-struc 'end env)
(YaTeX-reindent ccol)
(if region-mode
(progn
(insert "\n")
(or exchange (exchange-point-and-mark)))
(goto-char beg2)
(YaTeX-intelligent-newline nil)
(YaTeX-indent-line))
(YaTeX-package-auto-usepackage env 'env)
(if YaTeX-current-position-register
(point-to-register YaTeX-current-position-register))))
;;;###autoload
(defun my-smartparens-mode ()
(smartparens-global-mode)
(remove-hook 'yatex-mode-hook #'my-smartparens-mode)
(remove-hook 'org-mode-hook #'my-smartparens-mode))
;;;###autoload
(defun my-activate-selected ()
(require 'transient nil t)
(selected-global-mode 1)
(selected--on) ;; must call expclitly here
(remove-hook 'activate-mark-hook #'my-activate-selected))
;;;###autoload
(defun my-helpful-variable ()
(interactive)
(let ((thing (symbol-at-point)))
(if (helpful--variable-p thing)
(helpful-variable thing)
(call-interactively 'helpful-variable))))
(defvar my-eval-result "*eval-result*")
;;;###autoload
(defun my-eval-region ()
(interactive)
(when (use-region-p)
(eval-region (region-beginning) (region-end)
(get-buffer-create my-eval-result))
;; Copy the result to kill-ring and print it
(with-current-buffer (get-buffer-create my-eval-result)
(delete-char -1)
(goto-char (point-min))
(delete-blank-lines)
(mark-whole-buffer)
(kill-ring-save (point-min) (point-max))
(message "%s" (car kill-ring))
(erase-buffer))
;; Jump to the end of the region
(goto-char (max (or (mark) 0) (point)))
(deactivate-mark)))
;;;###autoload
(defun my-eval-region-as-function ()
(interactive)
(when (use-region-p)
(let ((region (intern (buffer-substring-no-properties
(region-beginning) (region-end)))))
(funcall region))))
;;;###autoload
(defun my-describe-selected-keymap ()
(interactive)
(describe-keymap 'selected-keymap))
;;;###autoload
(defun my-update-modeline-face ()
(setq my-selected-window-last (frame-selected-window))
;; (message "--- %s" my-selected-window-last)
(unless (minibufferp)
(my-modeline-face (buffer-narrowed-p))))
;;;###autoload
(defun my-modeline-face (buffer-narrowed)
"Update modeline color.
If BUFFER-NARROWED is nil, then change the color to indicating `widen'.
Otherwise, indicating narrowing."
(unless (eq my-buffer-narrowed-last
buffer-narrowed) ;; block unnecessary request
(setq my-buffer-narrowed-last buffer-narrowed)
;; (message "--- %s %s %s" this-command last-command buffer-narrowed)
(when (not (memq this-command '(save-buffer))) ;; FIXME
(if buffer-narrowed
(custom-set-faces
`(mode-line ((t (:background
,(nth 0 my-narrow-modeline)
:foreground
,(nth 1 my-narrow-modeline))))))
(custom-set-faces '(mode-line ((t nil))))))))
;;;###autoload
(defun my-update-modeline-color ()
"Update modeline face of the current selected window.
Call this function at updating `mode-line-mode'."
(when (eq my-selected-window-last (frame-selected-window))
(my-modeline-face (buffer-narrowed-p))))
;;;###autoload
(defun my-reload-mlscroll ()
(mlscroll-mode -1)
(setq mlscroll-border (ceiling (/ moom-font--size 4.0)))
(mlscroll-mode 1))
;;;###autoload
(defun my-mode-line-vc-mode-icon ()
(if (string-match "^ Git:" vc-mode)
(replace-regexp-in-string
"^ Git:" (propertize " " 'face 'mode-line-vc-modified-face) vc-mode)
(replace-regexp-in-string
"^ Git-" (propertize " " 'face 'mode-line-vc-normal-face) vc-mode)))
;;;###autoload
(defun my-open-scratch ()
"Switch the current buffer to \*scratch\* buffer."
(interactive)
(switch-to-buffer "*scratch*"))
;;;###autoload
(defun ad:split-window-below (&optional _size)
"An extention to switch to \*scratch\* buffer after splitting window."
(my-open-scratch))
;;;###autoload
(defun my-change-window-divider ()
(interactive)
(let ((display-table (or buffer-display-table
standard-display-table
(make-display-table))))
(set-display-table-slot display-table 5 ?│)
(set-window-display-table (selected-window) display-table)))
;;;###autoload
(defun my-update-display-line-numbers-face ()
(custom-set-faces
`(line-number-current-line
((t (:bold t :background ,(face-attribute 'hl-line :background)))))))
;;;###autoload
(defun my-display-line-numbers-width ()
(when (< display-line-numbers-width 5)
(setq display-line-numbers-width 5))
(setq moom-display-line-numbers-width (+ 2 display-line-numbers-width)))
;;;###autoload
(defun my-display-line-numbers-mode-on ()
"Trun on `display-line-numbers'."
(interactive)
(if (fboundp 'global-display-line-numbers-mode) ;; 26.1 or later
(unless global-display-line-numbers-mode
(global-display-line-numbers-mode 1)
(line-number-mode -1))
(user-error "The display-line-numbers is NOT supported")))
;;;###autoload
(defun my-display-line-numbers-mode-off ()
"Trun off `display-line-numbers'."
(interactive)
(if (fboundp 'global-display-line-numbers-mode) ;; 26.1 or later
(when global-display-line-numbers-mode
(global-display-line-numbers-mode -1)
(line-number-mode 1))
(user-error "The display-line-numbers is NOT supported")))
;;;###autoload
(defun my-toggle-display-line-numbers-mode ()
"Toggle variable `global-display-line-numbers-mode'."
(interactive)
(if (fboundp 'global-display-line-numbers-mode) ;; 26.1 or later
(let ((flag (if global-display-line-numbers-mode -1 1)))
(global-display-line-numbers-mode flag)
(line-number-mode (- flag)))
(user-error "The display-line-numbers is NOT supported")))
;;;###autoload
(defun my-mic-paren-activate ()
(paren-activate)
(show-paren-mode -1)
(remove-hook 'find-file-hook #'my-mic-paren-activate))
;;;###autoload
(defun ad:mic-paren-highlight (f)
(if (active-minibuffer-window)
(let ((paren-display-message 'never))
(funcall f)
paren-display-message)
(funcall f)))
;;;###autoload
(defun ad:font-lock-mode (&optional _ARG)
(unless (memq major-mode '(vterm-mode))
(font-lock-add-keywords major-mode
;; "[\t]+$" 行末のタブ
'((" " 0 'my-face-b-1 append)
("[ ]+$" 0 'my-face-b-3 append)
("[\t]+$" 0 'my-face-b-2 append)))))
;; 文字エンコーディングの文字列表現
;;;###autoload
(defun my-coding-system-name-mnemonic (coding-system)
(let* ((base (coding-system-base coding-system))
(name (symbol-name base)))
(cond ((string-prefix-p "utf-8" name) "U8")
((string-prefix-p "utf-16" name) "U16")
((string-prefix-p "utf-7" name) "U7")
((string-prefix-p "japanese-shift-jis" name) "SJIS")
((string-match "cp\\([0-9]+\\)" name) (match-string 1 name))
((string-match "japanese-iso-8bit" name) "EUC")
(t "???"))))
;;;###autoload
(defun my-coding-system-bom-mnemonic (coding-system)
(let ((name (symbol-name coding-system)))
(cond ((string-match "be-with-signature" name) "[BE]")
((string-match "le-with-signature" name) "[LE]")
((string-match "-with-signature" name) "[BOM]")
(t ""))))
;;;###autoload
(defun my-mode-line-icon-lock ()
(if view-mode
(concat (icons-in-terminal-faicon
"lock" :face '(:foreground "#FF0000")) " ") ""))
;;;###autoload
(defun my-mode-line-icon-for-file ()
(icons-in-terminal-icon-for-file
(buffer-name) :v-adjust 0.03 :face 'mode-line-file-icon-face))
;;;###autoload
(defun my-buffer-coding-system-mnemonic ()
"Return a mnemonic for `buffer-file-coding-system'."
(let* ((code buffer-file-coding-system)
(name (my-coding-system-name-mnemonic code))
(bom (my-coding-system-bom-mnemonic code)))
(if (version< emacs-version "29.0")
(format "%s %s%s" (my-mode-line-icon-for-file) name bom )
(format "%s%s" name bom ))))
;;;###autoload
(defun my-delight-activate ()
(require 'delight nil t)
(remove-hook 'find-file-hook #'my-delight-activate))
;;;###autoload
(defun my-migemo-activate ()
(when (and (executable-find "cmigemo")
(require 'migemo nil t))
(add-hook 'isearch-mode-hook #'migemo-init)
(migemo-init))
(remove-hook 'isearch-mode-hook #'my-migemo-activate))
(autoload 'calendar-iso-from-absolute "cal-iso" nil t)
(autoload 'calendar-absolute-from-gregorian "calendar" nil t)
;;;###autoload
(defun my-get-week-number ()
"Return the current week number."
(format "%02d"
(car
(calendar-iso-from-absolute
(calendar-absolute-from-gregorian
(list (string-to-number (format-time-string "%m"))
(string-to-number (format-time-string "%d"))
(string-to-number (format-time-string "%y"))))))))
;;;###autoload
(defun my-week-number ()
"Show the current week number."
(interactive)
(message "w%s" (my-get-week-number)))
;;;###autoload
(defun my-empty-booting-header-line ()
(with-current-buffer "*scratch*"
(let ((week (format "W%s: " (my-get-week-number)))
(date (format-time-string "%Y-%m-%d %a.")))
(setq header-line-format
(concat
" No day is a good day. "
week
date
(propertize " "
'display
`(space . (:align-to
,(- (frame-width)
(length week)
(length date))))))))))
;;;###autoload
(defun my-calendar-mark-selected ()
(org-eval-in-calendar '(setq cursor-type nil) t))
;;;###autoload
(defun my:elisp-eldoc (_callback)
"Avoid hiding `hl-line' in `emacs-lisp-mode'."
(when (fboundp 'hl-line-highlight)
(hl-line-highlight)))
;;;###autoload
(defun ad:eldoc-message (f &optional string)
(unless (active-minibuffer-window)
(funcall f string)))
;;;###autoload
(defun my-seq-sort-by (function pred sequence)
"Sort SEQUENCE using PRED as a comparison function.
Elements of SEQUENCE are transformed by FUNCTION before being
sorted. FUNCTION must be a function of one argument."
(seq-sort (lambda (a b)
(funcall pred
(funcall function a)
(funcall function b)))
sequence))
;;;###autoload
(defun ivy--sort-by-len (name candidates)
"Sort CANDIDATES based on similarity of their length with NAME."
(let ((name-len (length name))
(candidates-count (length candidates)))
(if (< 500 candidates-count)
candidates
(seq-sort-by #'length
(lambda (a b)
(< (abs (- name-len a))
(abs (- name-len b))))
candidates))))
;;;###autoload
(defun my-disable-counsel-find-file (&rest args)
"Disable `counsel-find-file' and use the original `find-file' with ARGS."
(let ((completing-read-function #'completing-read-default)
(completion-in-region-function #'completion--in-region))
(apply #'read-file-name-default args)))
;; Common actions for counsel-ag, counsel-fzf, and counsel-recentf
;;;###autoload
(defun my-counsel-fzf-in-default-dir (_arg)
"Search the current directory with fzf."
(counsel-fzf ivy-text default-directory))
;;;###autoload
(defun my-counsel-fzf-in-dir (_arg)
"Search again with new root directory."
(counsel-fzf ivy-text
(read-directory-name
(concat (car (split-string counsel-fzf-cmd))
" in directory: "))))
;;;###autoload
(defun my-counsel-ag-in-dir (_arg)
"Search again with new root directory."
(let ((current-prefix-arg '(4)))
(counsel-ag ivy-text nil ""))) ;; also disable extra-ag-args
;;;###autoload
(defun my-counsel-mark-ring ()
"Browse `mark-ring' interactively.
Obeys `widen-automatically', which see."
(interactive)
(let* ((counsel--mark-ring-calling-point (point))
(marks (copy-sequence mark-ring))
(marks (delete-dups marks))
(marks
;; mark-marker is empty?
(if (equal (mark-marker) (make-marker))
marks
(cons (copy-marker (mark-marker)) marks)))
(candidates (counsel-mark--get-candidates marks)))
(delete-dups candidates) ;; [added] remove duplicated lines
(if candidates
(counsel-mark--ivy-read "Mark: " candidates 'counsel-mark-ring)
(message "Mark ring is empty"))
counsel--mark-ring-calling-point)) ;; To avoid an warning on lexical val.
;;;###autoload
(defun my-pre-prompt-function ()
(cond (window-system
(format "%s%s "
(if my-toggle-modeline-global "" ;; FIXME
(concat (make-string (frame-width) ?\x5F) "\n")) ;; "__"
(cond ((require 'icons-in-terminal nil t)
(icons-in-terminal-material "playlist_add_check"))
((require 'all-the-icons nil t)
(all-the-icons-material "playlist_add_check"))
(t ""))))
;; ((eq system-type 'windows-nt)
;; (format "%s%s "
;; (if my-toggle-modeline-global "" ;; FIXME
;; (concat (make-string (frame-width) ?\x5F) "\n")) ;; "__"
;; ">>"))
(t
(format "%s\n" (make-string (1- (frame-width)) ?\x2D)))))
;;;###autoload
(defun my-truncate-lines-activate ()
"Truncate lines on `imenu-list' buffer."
(toggle-truncate-lines 1))
;;;###autoload
(defun my-imenu-list-update ()
"Expand frame width by `moom-change-frame-width'."
(when (and (memq imenu-list-position '(right left))
(not (get-buffer-window imenu-list-buffer-name t)))
(moom-change-frame-width (+ (frame-width) imenu-list-size))))
;;;###autoload
(defun my-imenu-list-quit-window ()
"Shrink frame width by `moom-change-frame-width'."
(when (and (memq imenu-list-position '(right left))
(not (get-buffer-window imenu-list-buffer-name t)))
(moom-change-frame-width (- (frame-width) imenu-list-size))))
;;;###autoload
(defun my-command-log-mode-activate ()
(interactive)
(keypression-mode 1)
(global-command-log-mode 1)
(when (require 'moom nil t)
(moom-delete-windows)
(moom-change-frame-width 140)
(moom--stay-in-region)
(clm/open-command-log-buffer)))
;;;###autoload
(defun my-command-log-mode-deactivate ()
(interactive)
(keypression-mode -1)
(global-command-log-mode -1)
(when (require 'moom nil t)
(moom-delete-windows)))
;;;###autoload
(defun my-enable-tree-sitter ()
(unless (featurep 'tree-sitter)
(require 'tree-sitter)
(require 'tree-sitter-hl)
(require 'tree-sitter-debug)
(require 'tree-sitter-query)
(require 'tree-sitter-langs))
(tree-sitter-hl-mode))
;;;###autoload
(defun ad:swiper-thing-at-point ()
"`swiper' with `ivy-thing-at-point'."
(interactive)
(let ((thing (if (thing-at-point-looking-at "^\\*+") ;; org heading を除外
nil
(ivy-thing-at-point))))
(when (use-region-p)
(deactivate-mark))
(swiper thing)))