-
Notifications
You must be signed in to change notification settings - Fork 5
/
init-win.el
2219 lines (1984 loc) · 84.4 KB
/
init-win.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
;; -*- coding:utf-8-unix; lexical-binding: t; -*-
;; This initialize file should be loaded first in w32 environment.
;; Each setting could be overwritten in .emacs, see AppData/Roming/.emacs.
;; And the .emacs will contain, for insetance,
;; 1. Set my-local-directory in the first line.
;; 2. Set (load (concat my-local-directory "org/config/init-win.el")) in the second line.
;; 3. Overwrite "PATH" and #'counsel-win-app-list in the .emacs if needed.
;; 4. additional packages will be installed to my-installed-packages-dir
;; Note: all local and private settings should be configured in the .emacs.
;; runemacs.exe is extracted from a distributed zip package from
;; https://ftp.jaist.ac.jp/pub/GNU/emacs/windows/
;; Last update: 2024-07-29
(when nil
;; advice of load function
(defadvice load (around require-benchmark activate)
(let* ((before (current-time))
(result ad-do-it)
(after (current-time))
(time (+ (* (- (nth 1 after) (nth 1 before)) 1000.0)
(/ (- (nth 2 after) (nth 2 before)) 1000.0)))
(arg (ad-get-arg 0)))
(message "--- %04d [ms]: (loading) %s" time arg)))
;; advice of require function
(defadvice require (around require-benchmark activate)
"http://memo.sugyan.com/entry/20120105/1325756767"
(let* ((before (current-time))
(result ad-do-it)
(after (current-time))
(time (+ (* (- (nth 1 after) (nth 1 before)) 1000.0)
(/ (- (nth 2 after) (nth 2 before)) 1000.0)))
(arg (ad-get-arg 0)))
(unless (or (memq arg '(cl-lib macroexp))
(> 0.1 time))
(message "--- %04d [ms]: %s" time arg)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar do-profile nil) ;; M-x profiler-report to see the result
(when do-profile (profiler-start 'cpu+mem))
(setq debug-on-error nil)
(setq gc-cons-threshold (* 512 1024 1024))
(defvar my-gc-last 0.0)
(add-hook 'post-gc-hook
#'(lambda ()
(message "GC! > %.4f[sec] (No: %d)"
(- gc-elapsed my-gc-last) gcs-done)
(setq my-gc-last gc-elapsed)))
;; Language, will override default-input-method
(set-language-environment "Japanese")
(set-clipboard-coding-system 'utf-16le) ;; enable copy-and-paste correctly
(set-locale-environment "en_US.UTF-8")
(setq system-time-locale "C") ;; format-time-string %a, not 日 but Sun
(setq inhibit-default-init t)
(setq initial-scratch-message nil
initial-buffer-choice t ;; Starting from *scratch* buffer
initial-major-mode 'fundamental-mode)
(setq byte-compile-warnings '(obsolete))
(setq make-backup-files nil)
(setq auto-save-default nil)
(setq auto-save-list-file-prefix nil)
(setq default-directory "~/")
(setq truncate-line nil
truncate-partial-width-windows nil)
(setq-default tab-width 2)
(setq-default indent-tabs-mode nil)
(setq indent-line-function 'insert-tab)
(setq line-number-display-limit-width 100000)
;; AppData\Roaming\.emacs.d\lisp 以下に各追加パッケージを配置すること
;; smartparens requires dash.el.
(defvar my-installed-packages
'("dash.el" "compat" "smex" "elisp-refs" "s.el" "f.el"
"moom" "swiper" "selected" "expand-region.el" "counsel-osx-app"
"smartparens" "emacs-htmlize" "emacs-undo-fu" "transient" "bsv"
"japanese-holidays" "highlight-symbol.el" "tr-emacs-ime-module/lisp"
"emacs-google-this" "volatile-highlights.el" "hl-todo" "bm"
"replace-from-region" "session" "helpful" "org-appear" "projectile"
"counsel-projectile" "super-save" "org-tree-slide" "delight.el"
"org-mode/lisp" "org-contrib/lisp" "corfu" "prescient.el" "kind-icon"
"vc-defer" "markdown-mode"))
(defvar my-installed-packages-dir "~/.emacs.d/lisp/")
(let ((default-directory (expand-file-name my-installed-packages-dir)))
(add-to-list 'load-path default-directory)
;; (normal-top-level-add-subdirs-to-load-path) ;; Slower than add-to-load
(normal-top-level-add-to-load-path my-installed-packages))
;; Setting Home directory if needed.
;; (setenv "HOME" "C:/Users/******/AppData/Roaming")
;; (setenv "HOME" "c:/cygwin64/home/********")
;; Proxy
;; (setq url-proxy-services
;; '(("http" . "http://hoge.org:8888")
;; ("https" . "https://hoge.org:8888")))
;; For MSYS
;; (when (eq system-type 'windows-nt)
;; (setenv "HOME" "C:\\cygwin64\\home\\takaxp")
;; (setenv "PATH" (concat (getenv "PATH")
;; ";C:\\cygwin64\\usr\\local\\bin"
;; ";C:\\cygwin64\\opt\\bin"
;; ;; ";C:\\msys64\\mingw64\\bin"
;; ";C:\\cygwin64\\usr\\bin"))
;; ;; (setenv "PATH" (concat (getenv "PATH")
;; ;; ";C:\\msys64\\usr\\local\\bin" ";C:\\msys64\\opt\\bin"
;; ;; ";C:\\msys64\\mingw64\\bin" ";C:\\msys64\\usr\\bin"))
;; (setq shell-file-name "C:/cygwin64/bin/bash"))
(unless noninteractive
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
(progn
(setq display-time-format "%H:%M w%V")
(setq display-time-interval 1)
(setq display-time-default-load-average nil)
(display-time-mode 1))
(setq display-time-day-and-date t)
(global-auto-revert-mode 1)
(set-face-background 'fringe (face-background 'default))
(custom-set-faces ;; モードラインの配色
'(mode-line
((t (:background "#7D60AF" :foreground "#FFFFFF" :box nil :height 0.9))))
'(mode-line-inactive
((t (:background "#CCCCCC" :foreground "#FFFFFF" :box nil :height 0.9)))))
;; Basic key-binding
(global-set-key (kbd "C-;") 'comment-dwim) ;; M-; is the defualt
(global-set-key (kbd "C-c g") 'goto-line)
(global-set-key (kbd "C-c c") 'compile)
(global-set-key (kbd "C-M-t") 'beginning-of-buffer)
(global-set-key (kbd "C-M-b") 'end-of-buffer)
(global-set-key (kbd "C-M-p") (lambda () (interactive) (other-window -1)))
(global-set-key (kbd "C-M-n") (lambda () (interactive) (other-window 1)))
(global-set-key (kbd "M-v") 'yank)
(global-set-key (kbd "C-t") 'scroll-down)
(global-set-key (kbd "M-=") 'count-words)
(global-set-key (kbd "RET") 'electric-newline-and-maybe-indent)
(global-set-key (kbd "C-c a") 'org-agenda)
(setq scroll-conservatively 1000)
(setq scroll-step 1)
(setq scroll-preserve-screen-position t) ;; スクロール時にスクリーン内で固定
(setq next-screen-context-lines 10)
(setq mouse-drag-copy-region t)
(setq yank-excluded-properties t)
(setq ring-bell-function 'ignore)
(setq confirm-kill-emacs 'yes-or-no-p)
;; emacs-lisp-mode
(add-hook 'emacs-lisp-mode-hook #'turn-on-font-lock)
;; Linspacing adjustment
(defun my-linespacing ()
(unless (minibufferp)
(setq-local line-spacing 0)))
(add-hook 'buffer-list-update-hook #'my-linespacing)
(add-hook 'org-src-mode-hook #'my-linespacing)
(add-hook 'debugger-mode-hook #'my-linespacing)
;; IME toggle (M-SPC/S-SPC)
(defun my-ime-active-p ()
(if current-input-method t nil))
;; Auto ascii for org-mode headings
(defun my-ime-on ()
"IME ON."
(interactive)
(activate-input-method default-input-method))
(defun my-ime-off ()
"IME OFF."
(interactive)
(deactivate-input-method))
(defun my-toggle-ime ()
"Toggle IME."
(interactive)
(if (my-ime-active-p) (my-ime-off) (my-ime-on)))
(defvar my-ime-before-action nil)
(defun my-ime-on-sticky ()
(when my-ime-before-action
(my-ime-on)))
(defun my-ime-off-sticky ()
(when (setq my-ime-before-action (my-ime-active-p))
(my-ime-off)))
(add-hook 'activate-mark-hook #'my-ime-off-sticky)
(add-hook 'deactivate-mark-hook #'my-ime-on-sticky)
(global-set-key (kbd "M-SPC") 'my-toggle-ime)
(global-set-key (kbd "S-SPC") 'my-toggle-ime)
;; font config
(defun my-ja-font-setter (spec)
(set-fontset-font nil 'japanese-jisx0208 spec)
(set-fontset-font nil 'katakana-jisx0201 spec)
(set-fontset-font nil 'japanese-jisx0212 spec)
(set-fontset-font nil '(#x0080 . #x024F) spec)
(set-fontset-font nil '(#x0370 . #x03FF) spec)
(set-fontset-font nil 'mule-unicode-0100-24ff spec)
(set-fontset-font t 'unicode spec nil 'prepend))
(defun my-ascii-font-setter (spec)
(set-fontset-font nil 'ascii spec))
(let ((font-size 26)
(font-height 100)
(ascii-font "Inconsolata")
(ja-font "Migu 2M")) ;; Meiryo UI, メイリオ
(set-fontset-font t '(#Xe000 . #Xf8ff) "icons-in-terminal")
(my-ascii-font-setter (font-spec :family ascii-font :size font-size))
(my-ja-font-setter
(font-spec :family ja-font :size font-size :height font-height))
(setq face-font-rescale-alist '((".*Inconsolata.*" . 1.0))))
;; カーソル行の色
(defvar my-ime-off-hline-hook nil)
(defvar my-ime-on-hline-hook nil)
(defvar my-hl-permanent-disabled '(dired-mode)
"A list of major modes to disable `hl-line'.")
(defun my-ime-off-hline ()
(my-hl-line-enable)
(let ((dark (eq (frame-parameter nil 'background-mode) 'dark)))
(set-face-background hl-line-face (if dark "#484c5c" "#DEEDFF")))
(run-hooks 'my-ime-off-hline-hook))
(defun my-ime-on-hline ()
(my-hl-line-enable)
(let ((dark (eq (frame-parameter nil 'background-mode) 'dark)))
(set-face-background hl-line-face (if dark "#594d5d" "#fff0de")))
(run-hooks 'my-ime-on-hline-hook))
(add-hook 'input-method-activate-hook #'my-ime-on-hline)
(add-hook 'input-method-deactivate-hook #'my-ime-off-hline)
(defun my-hl-line-disable ()
"Disable `hl-line'."
(hl-line-mode -1))
(defun my-hl-line-enable ()
"Enable `hl-line'."
(unless (or hl-line-mode
(minibufferp)
(memq major-mode my-hl-permanent-disabled))
(hl-line-mode 1)))
(global-hl-line-mode)
;; カーソルの色
(defconst my-cur-color-ime '(:on "#FF9300" :off "#91C3FF"))
(defconst my-cur-type-ime '(:on (bar . 4) :off (bar . 4) :invisible nil))
(setq-default cursor-type (if (my-ime-active-p)
(plist-get my-cur-type-ime :on)
(plist-get my-cur-type-ime :off)))
(defun my-ime-on-cursor ()
(interactive)
(setq cursor-type (plist-get my-cur-type-ime :on))
(set-cursor-color (plist-get my-cur-color-ime :on)))
(defun my-ime-off-cursor ()
(interactive)
(setq cursor-type (plist-get my-cur-type-ime :off))
(set-cursor-color (plist-get my-cur-color-ime :off)))
(add-hook 'input-method-activate-hook #'my-ime-on-cursor)
(add-hook 'input-method-deactivate-hook #'my-ime-off-cursor)
;; for init setup on hline and cursor
(if (my-ime-active-p) (my-ime-on-hline) (my-ime-off-hline))
(if (my-ime-active-p) (my-ime-on-cursor) (my-ime-off-cursor))
;; 特定の文字をバッファ内で強調表示する
;; スペース
(defface my-face-b-1
'((t (:background "gray" :bold t :underline "red")))
nil :group 'font-lock-highlighting-faces)
;; タブだけの行
(defface my-face-b-2
'((t (:background "orange" :bold t :underline "red")))
nil :group 'font-lock-highlighting-faces)
;; 半角スペース
(defface my-face-b-3 '((t (:background "orange")))
nil :group 'font-lock-highlighting-faces)
(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)))))
(advice-add 'font-lock-mode :before #'ad:font-lock-mode)
;; Utilities
(global-set-key (kbd "C-M-<return>") #'my-open-w32-explore)
(global-set-key (kbd "C-M-o") 'my-open-hoge)
(global-set-key (kbd "C-M-s") #'my-open-scratch)
(global-set-key (kbd "C-c 0") 'insert-formatted-current-date)
(defun my-open-w32-explore ()
(interactive)
;; Need to create an alias to cygstart.exe as open.exe
(shell-command-to-string "open ."))
(defun my-open-hoge ()
(interactive)
(unless (featurep 'org)
(require 'org))
(find-file (concat org-directory "next.org")))
(defun my-open-scratch ()
"Switch the current buffer to \*scratch\* buffer."
(interactive)
(switch-to-buffer "*scratch*"))
(defun insert-formatted-current-date ()
"Insert a timestamp at the cursor position."
(interactive)
(insert (format-time-string "%Y-%m-%d")))
(defun my-kill-all-file-buffers ()
"Kill all buffers visiting files."
(interactive)
(dolist (buffer (buffer-list))
(when (or (and (buffer-live-p buffer)
(buffer-file-name buffer))
(and (switch-to-buffer buffer)
(eq major-mode 'dired-mode)
(file-directory-p (dired-current-directory))))
(kill-buffer buffer)))
(delete-windows-on)
(scratch-buffer)
(message "Quit Emacs? (C-c C-x)"))
(when (display-graphic-p)
(global-set-key (kbd "C-x C-c") #'my-kill-all-file-buffers))
(defun my-kill-emacs-when-scratch-buffer ()
(interactive)
(when (equal "*scratch*" (buffer-name))
(save-buffers-kill-emacs)))
(global-set-key (kbd "C-c C-x") #'my-kill-emacs-when-scratch-buffer)
;; isearch with a selected reagion
(defadvice isearch-mode
(around isearch-mode-default-string
(forward &optional regexp op-fun recursive-edit word-p) activate)
(if (and transient-mark-mode mark-active (not (eq (mark) (point))))
(progn
(isearch-update-ring (buffer-substring-no-properties (mark) (point)))
(deactivate-mark)
ad-do-it
(if (not forward)
(isearch-repeat-backward)
(goto-char (mark))
(isearch-repeat-forward)))
ad-do-it))
(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))
(defvar my-loaddefs-file (concat my-installed-packages-dir "loaddefs.el"))
;; (when (file-exists-p my-loaddefs-file)
;; (load my-loaddefs-file)) ;; requires over 40[ms] for about 20 packages
(defun my-update-autoloads ()
(interactive)
(unless (file-exists-p my-loaddefs-file)
(with-temp-buffer
(write-file my-loaddefs-file)))
(mapc (lambda (package)
(make-directory-autoloads
(concat my-installed-packages-dir package) my-loaddefs-file))
my-installed-packages))
(defun my-time-stamp ()
(setq time-stamp-format
(if (eq major-mode 'org-mode)
"[%Y-%02m-%02d %3a %02H:%02M]" ;; "%04y"
"%Y-%02m-%02d"))
(if (boundp 'org-tree-slide-mode)
(unless org-tree-slide-mode
(time-stamp))
(time-stamp)))
(add-hook 'before-save-hook #'my-time-stamp)
(defvar w32-alerter-command nil)
(defun my-desktop-notification-handler (message)
(my-desktop-notification "Emacs" message))
(defun my-org-agenda-to-appt (&optional _force)
"Update `appt-time-mag-list'. Use `async' if possible."
(interactive)
(unless (featurep 'org)
(require 'org))
(unless (active-minibuffer-window)
(org-agenda-to-appt t)
(appt-check)))
(defun my-desktop-notification (title message &optional sticky sound timeout)
"Show a message by `alerter' command."
(start-process
"notification" "*notification*" "python.exe" w32-alerter-command
"--title" title "--message" message
"--sticky" (if sticky "True" "False")))
;; appt-display-format が 'echo でも appt-disp-window-function を呼ぶ
;; Need review
(defun ad:appt-display-message (string mins)
"Display a reminder about an appointment.
The string STRING describes the appointment, due in integer MINS minutes.
The arguments may also be lists, where each element relates to a
separate appointment. The variable `appt-display-format' controls
the format of the visible reminder. If `appt-audible' is non-nil,
also calls `beep' for an audible reminder."
(if appt-audible (beep 1))
;; Backwards compatibility: avoid passing lists to a-d-w-f if not necessary.
(and (listp mins)
(= (length mins) 1)
(setq mins (car mins)
string (car string)))
(cond ((memq appt-display-format '(window echo)) ;; Modified
;; TODO use calendar-month-abbrev-array rather than %b?
(let ((time (format-time-string "%a %b %e ")))
(condition-case err
(funcall appt-disp-window-function
(if (listp mins)
(mapcar #'number-to-string mins)
(number-to-string mins))
time string)
(wrong-type-argument
(if (not (listp mins))
(signal (car err) (cdr err))
(message "Argtype error in `appt-disp-window-function' - \
update it for multiple appts?")
;; Fallback to just displaying the first appt, as we used to.
(funcall appt-disp-window-function
(number-to-string (car mins)) time
(car string))))))
(run-at-time (format "%d sec" appt-display-duration)
nil
appt-delete-window-function))
((eq appt-display-format 'echo) ;; hidden
(message "%s" (if (listp string)
(mapconcat #'identity string "\n")
string)))))
(defun ad:appt-disp-window (min-to-app _new-time appt-msg)
"Extension to support appt-disp-window."
(if (string= min-to-app "0")
(my-desktop-notification "### Expired! ###" appt-msg t "Glass")
(my-desktop-notification
(concat "in " min-to-app " min.") appt-msg nil "Tink")))
(global-set-key (kbd "C-c f 3") #'my-org-agenda-to-appt))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Part A: Scheduling of package loading
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; auto-revert
(defun my-activate-auto-revert ()
(global-auto-revert-mode 1)
(remove-hook 'find-file-hook #'my-activate-auto-revert))
(add-hook 'find-file-hook #'my-activate-auto-revert)
;; tr-ime: IME パッチモジュールの読み込み about 200[ms]
(defun my-activate-tr-ime ()
(when (and (eq window-system 'w32)
(string= module-file-suffix ".dll")
(not (fboundp 'ime-get-mode)))
(require 'tr-ime nil t))
(remove-hook 'find-file-hooks #'my-activate-tr-ime))
(add-hook 'find-file-hook #'my-activate-tr-ime)
;; session
(autoload #'session-initialize "session" "session" t)
(add-hook 'after-init-hook #'session-initialize -10)
;; recentf
(defun my-activate-recentf ()
(when (require 'recentf nil t)
(let ((message-log-max nil))
(recentf-mode 1)))
(remove-hook 'pre-command-hook #'my-activate-recentf))
(add-hook 'pre-command-hook #'my-activate-recentf)
;; Ivy, Counsel, Swiper
(autoload #'counsel-M-x "ivy" "ivy,counsel,swiper" t)
(autoload #'counsel-ag "counsel" "ivy,counsel,swiper" t)
(autoload #'counsel-recentf "ivy" "ivy,counsel,swiper" t)
(autoload #'counsel-ibuffer "ivy" "ivy,counsel,swiper" t)
(autoload #'swiper-thing-at-point "ivy" "ivy,counsel,swiper" t)
(autoload #'counsel-osx-app "counsel-osx-app" "Application Launcher" t)
(global-set-key (kbd "M-x") 'counsel-M-x)
(global-set-key (kbd "C-M-f") 'counsel-ag)
(global-set-key (kbd "C-M-r") 'counsel-recentf)
(global-set-key (kbd "C-x C-b") 'counsel-ibuffer)
(global-set-key (kbd "M-s M-s") 'swiper-thing-at-point)
(global-set-key (kbd "C-M-1") 'counsel-osx-app)
;; bs with bsv.el
(global-set-key (kbd "M-]") 'bs-cycle-next)
(global-set-key (kbd "M-[") 'bs-cycle-previous)
;; moom
(autoload #'moom-move-frame-to-edge-top "moom" "Moom" t)
(autoload #'moom-cycle-frame-height "moom" "Moom" t)
(autoload #'moom-move-frame "moom" "Moom" t)
(autoload #'moom-move-frame-to-center "moom" "Moom" t)
(autoload #'moom-transient-dispatch "moom-transient" "moom dispatcher" t)
(global-set-key (kbd "C-1") 'moom-move-frame-to-edge-top)
(global-set-key (kbd "C-2") 'moom-cycle-frame-height)
(global-set-key (kbd "M-0") 'moom-move-frame)
(global-set-key (kbd "M-2") 'moom-move-frame-to-center)
(global-set-key (kbd "C-c o") #'moom-transient-dispatch)
;; smartparens
(autoload #'smartparens-global-mode "smartparens" "smartparens" t)
(defun my-activate-smartparens ()
(when (require 'smartparens nil t)
(smartparens-global-mode))
(remove-hook 'find-file-hook #'my-activate-smartparens))
(add-hook 'find-file-hook #'my-activate-smartparens)
;; selected
(autoload #'selected-global-mode "selected" "selected" t)
(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))
(add-hook 'activate-mark-hook #'my-activate-selected)
;; helpful
(autoload 'helpful-at-point "helpful" "Help" t)
(autoload 'my-helpful-variable "helpful" "Help" t)
(autoload 'helpful-key "helpful" "Help" t)
(autoload 'helpful-function "helpful" "Help" t)
(autoload 'helpful-variable "helpful" "Help" t)
(autoload 'helpful-macro "helpful" "Help" t)
(global-set-key (kbd "<f1> @") 'helpful-at-point)
(global-set-key (kbd "<f1> k") 'helpful-key)
(global-set-key (kbd "<f1> f") 'helpful-function)
(global-set-key (kbd "<f1> v") 'helpful-variable)
(global-set-key (kbd "<f1> m") 'helpful-macro)
;; hl-todo (depends on compat.el)
(autoload #'global-hl-todo-mode "hl-todo" "hl-todo" t)
;; highlight-symbol
(autoload #'highlight-symbol-mode "highlight-symbol" "highlight-symbol" t)
;; volatile-highlights
(autoload #'volatile-highlights-mode "volatile-highlights" "VHl" t)
;; Postpone: hl-todo.el, highlight-symbol.el, and volatile-highlights.el
(defun my-activate-highlights ()
(when (require 'hl-todo nil t)
(my-hl-todo-light-theme))
(when (require 'highlight-symbol nil t)
(dolist (hook '(emacs-lisp-mode-hook c-mode-common-hook prog-mode-hook))
(add-hook hook #'highlight-symbol-mode)))
(when (require 'volatile-highlights nil t)
(dolist (hook '(org-mode-hook emacs-lisp-mode-hook))
(add-hook hook #'volatile-highlights-mode)))
(remove-hook 'pre-command-hook #'my-activate-highlights))
(add-hook 'pre-command-hook #'my-activate-highlights)
;; eldoc
(defun my-activate-eldoc ()
(when (require 'eldoc nil t)
(dolist (hook '(emacs-lisp-mode-hook org-mode-hook c-mode-common-hook))
(add-hook hook #'turn-on-eldoc-mode)))
(remove-hook 'find-file-hook #'my-activate-eldoc))
(add-hook 'find-file-hook #'my-activate-eldoc)
(add-hook 'org-mode-hook #'my-load-echo-org-link)
;; bm: ファイルオープン時にブックマークを復帰
(autoload #'my-toggle-bm "bm" "bm" t)
(autoload #'my-bm-next "bm" "bm" t)
(autoload #'bm-buffer-restore "bm" "bm" t)
(autoload #'counsel-bm "bm" "bm" t)
(global-set-key (kbd "<f10>") 'my-toggle-bm)
(global-set-key (kbd "<C-f10>") 'my-bm-next)
(global-set-key (kbd "<S-f10>") 'bm-show-all)
(add-hook 'find-file-hook #'bm-buffer-restore)
;; google-this
(autoload #'google-this "google-this" "google-this" t)
(autoload #'my-google-this "google-this" "google-this" t)
;; expand-region
(autoload #'ad:er:mark-sexp "expand-region" nil t)
(advice-add 'mark-sexp :around #'ad:mark-sexp)
(advice-add 'mark-sexp :around #'ad:er:mark-sexp)
;; Tree Sitter
(let* ((elp (expand-file-name my-installed-packages-dir))
(ets (concat elp "emacs-tree-sitter/"))
(tsl (concat elp "tree-sitter-langs/")))
;; (add-to-list 'load-path (concat ets "langs"))
(add-to-list 'load-path (concat ets "core"))
(add-to-list 'load-path (concat ets "lisp"))
(add-to-list 'load-path tsl))
(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))
(dolist (hook '(js-mode-hook))
(add-hook hook #'my-enable-tree-sitter))
;; replace-from-region
(autoload #'query-replace-from-region "replace-from-region" nil t)
(global-set-key (kbd "M-%") 'query-replace-from-region)
;; Undo-fu
(autoload #'undo-fu-only-undo "undo-fu" "Undo" t)
(autoload #'undo-fu-only-redo "undo-fu" "Undo" t)
(global-set-key (kbd "C-/") 'undo-fu-only-undo)
(global-set-key (kbd "C-M-/") 'undo-fu-only-redo)
;; view
(autoload #'my-auto-view "view" "view mode" t)
(add-hook 'find-file-hook #'my-auto-view)
;; org-appear
(autoload 'org-appear-mode "org-appear" nil t)
(add-hook 'org-mode-hook #'org-appear-mode)
;; prettify-symbols-mode
(add-hook 'org-mode-hook #'prettify-symbols-mode)
;; projectile
(defun my-projectile-activate ()
(interactive)
(setq projectile-keymap-prefix (kbd "C-c p"))
(projectile-mode 1)
(remove-hook 'find-file-hook #'my-projectile-activate))
(autoload 'projectile-mode "projectile" nil t)
(add-hook 'find-file-hook #'my-projectile-activate)
;; super-save
(defun my-super-save-activate ()
(interactive)
(super-save-mode 1)
(remove-hook 'find-file-hook #'my-super-save-activate))
(autoload 'super-save-mode "super-save" nil t)
(add-hook 'find-file-hook #'my-super-save-activate)
;; org-tree-slide
(global-set-key (kbd "<f8>") 'org-tree-slide-mode)
(global-set-key (kbd "S-<f8>") 'org-tree-slide-skip-done-toggle)
(autoload 'org-tree-slide-mode "org-tree-slide" nil t)
;; delight
(defun my-delight-activate ()
(require 'delight nil t)
(remove-hook 'find-file-hook #'my-delight-activate))
(add-hook 'find-file-hook #'my-delight-activate)
;; emacsclientw
(defun my-emacsclient-activate ()
(when (require 'server nil t)
(server-force-delete)
(server-start)))
(add-hook 'after-init-hook #'my-emacsclient-activate)
;; corfu
(autoload 'corfu-mode "corfu" nil t)
(add-hook 'emacs-lisp-mode-hook #'corfu-mode)
;; (setq vc-handled-backends nil) ;; vc-mode is too heavy in w32 environment
;; vc-defer
(defun my-vc-defer-activate ()
(require 'vc-defer nil t)
(vc-defer-mode)
(remove-hook 'pre-command-hook #'my-vc-defer-activate))
(add-hook 'pre-command-hook #'my-vc-defer-activate)
;; markdown-mode
(autoload 'markdown-mode "markdown-mode" nil t)
(push '("\\.md$" . markdown-mode) auto-mode-alist)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Part B: Configurations for each package
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(with-eval-after-load "tr-ime"
(tr-ime-advanced-initialize)
;; IM のデフォルトを IME に設定
(setq default-input-method "W32-IME")
;; IME のモードライン表示設定
(setq-default w32-ime-mode-line-state-indicator "[--]")
(setq w32-ime-mode-line-state-indicator-list '("[--]" "[あ]" "[--]"))
;; IME ON/OFF state を全ウィンドウで1つにする
(setq w32-ime-buffer-switch-p nil)
;; IME 初期化
(w32-ime-initialize)
;; IME 制御(yes/no などの入力の時に IME を OFF にする)
(wrap-function-to-control-ime 'universal-argument t nil)
(wrap-function-to-control-ime 'read-string nil nil)
(wrap-function-to-control-ime 'read-char nil nil)
(wrap-function-to-control-ime 'read-from-minibuffer nil nil)
(wrap-function-to-control-ime 'y-or-n-p nil nil)
(wrap-function-to-control-ime 'yes-or-no-p nil nil)
(wrap-function-to-control-ime 'map-y-or-n-p nil nil))
(with-eval-after-load "session"
(add-to-list 'session-globals-exclude 'org-mark-ring)
(setq session-set-file-name-exclude-regexp
"[/\\]\\.overview\\|[/\\]\\.session\\|News[/\\]\\|[/\\]COMMIT_EDITMSG")
;; Change save point of session.el
(setq session-save-file
(expand-file-name (concat my-home-directory ".emacs.d/.session")))
(setq session-initialize '(de-saveplace session keys menus places)
session-globals-include '((kill-ring 100)
(session-file-alist 100 t)
(file-name-history 200)
ivy-dired-history-variable
search-ring
regexp-search-ring))
(setq session-undo-check -1))
(with-eval-after-load "recentf"
(custom-set-variables
'(recentf-max-saved-items 2000)
'(recentf-save-file
(expand-file-name (concat my-home-directory ".emacs.d/recentf")))
'(recentf-auto-cleanup 'never)
'(recentf-exclude
'(".recentf" "bookmarks" "org-recent-headings.dat" "^/tmp\\.*"
"^/private\\.*" "^/var/folders\\.*" "/TAGS$")))
(defun my-recentf-save-list-silence ()
(interactive)
(let ((message-log-max nil))
(recentf-save-list))
(message ""))
(defun my-recentf-cleanup-silence ()
(interactive)
(let ((message-log-max nil))
(recentf-cleanup))
(message ""))
;; (run-with-idle-timer 180 t #'my-recentf-cleanup-silence)
(add-function :before
after-focus-change-function #'my-recentf-save-list-silence))
(with-eval-after-load "ivy"
(require 'swiper)
(require 'counsel)
(global-set-key (kbd "M-y") 'counsel-yank-pop)
(global-set-key (kbd "C-,") 'counsel-mark-ring)
(global-set-key (kbd "C-c i r") 'ivy-resume)
(global-set-key (kbd "<S-f10>") 'counsel-bm)
(setq ivy-initial-inputs-alist
'((org-agenda-refile . "^")
(org-capture-refile . "^")
(counsel-describe-function . "^")
(counsel-describe-variable . "^")
(Man-completion-table . "^")
(woman . "^")))
;; 順不同化
(setf (alist-get t ivy-re-builders-alist) #'ivy--regex-ignore-order)
;; 履歴を使う
(when (require 'smex nil t)
(setq smex-history-length 35)
(setq smex-completion-method 'ivy))
;; 選択候補の先頭に矢印を入れる
(delete '(t . ivy-format-function-default) ivy-format-functions-alist)
(add-to-list 'ivy-format-functions-alist
'(t . ivy-format-function-arrow-line) t)
(custom-set-faces
'(ivy-current-match
((((class color) (background light))
:background "#FFF3F3" :distant-foreground "#000000" :extend t)
(((class color) (background dark))
:background "#404040" :distant-foreground "#abb2bf" :extend t)))
'(ivy-minibuffer-match-face-1
((((class color) (background light)) :foreground "#666666")
(((class color) (background dark)) :foreground "#999999")))
'(ivy-minibuffer-match-face-2
((((class color) (background light)) :foreground "#c03333" :underline t)
(((class color) (background dark)) :foreground "#e04444" :underline t)))
'(ivy-minibuffer-match-face-3
((((class color) (background light)) :foreground "#8585ff" :underline t)
(((class color) (background dark)) :foreground "#7777ff" :underline t)))
'(ivy-minibuffer-match-face-4
((((class color) (background light)) :foreground "#439943" :underline t)
(((class color) (background dark)) :foreground "#33bb33" :underline t))))
;; activate
(ivy-mode 1))
(with-eval-after-load "counsel-osx-app"
(defun counsel-win-app-list ()
;; NOTE MSYS の場合は,第2引数はフルパスではなく実行ファイル名のみ.
'(("Mintty" . "C:/cygwin64/bin/mintty.exe")
("Edge" . "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe")
("Notepad". "C:/WINDOWS/system32/notepad.exe")
("Task manager". "C:/WINDOWS/system32/taskmgr.exe")
("Command Prompt". "C:/WINDOWS/system32/cmd.exe")
("PowerToys" . "C:/Program Files/PowerToys/PowerToys.exe")
("Chrome" . "C:/Program Files/Google/Chrome/Application/chrome.exe")
("Word" . "C:/Program Files (x86)/Microsoft Office/Office16/winword.exe")
("Excel" . "C:/Program Files (x86)/Microsoft Office/Office16/excel.exe")
("PowerPoint" . "C:/Program Files (x86)/Microsoft Office/Office16/powerpnt.exe")
("Outlook" . "C:/Program Files (x86)/Microsoft Office/Office16/outlook.exe")))
(defvar counsel-win-app-launch-cmd
(lambda (app &optional file)
(if (bound-and-true-p file)
(format "%s -a %s" file app)
(format "%s" app))) "")
(defun counsel-osx-app-action-default (app)
"Launch APP using `counsel-win-app-launch-cmd'."
(let ((arg
(cond
((stringp counsel-win-app-launch-cmd)
(format "%s %s" counsel-win-app-launch-cmd app))
((functionp counsel-win-app-launch-cmd)
(funcall counsel-win-app-launch-cmd app))
(t
(user-error
"Could not construct cmd from `counsel-win-app-launch-cmd'")))))
(message "%s" (concat "open " "\"" arg "\""))
;; MSYS2 の場合はファイルパスではなくアプリ名で判定される
;; TODO file check only in Cygwin
(w32-shell-execute "open" arg)
;; (if (file-exists-p arg)
;; (call-process-shell-command (concat "open " "\"" arg "\""))
;; (user-error (format "Could not find \"%s\"" arg)))
))
(defun counsel-osx-app ()
"Launch an application via ivy interface."
(interactive)
(ivy-read "Run application: " (counsel-win-app-list)
:action (counsel-osx-app--use-cdr
counsel-osx-app-action-default)
:caller 'counsel-app)))
(with-eval-after-load "bs"
(when (require 'bsv nil t)
(setq bsv-max-height 5)
(setq bsv-message-timeout 9)))
(with-eval-after-load "moom"
(moom-recommended-keybindings 'all)
(setq moom-lighter "M")
(setq moom-verbose t)
(setq moom-font-ja-scale 1.0)
(moom-mode 1)
;; Font setting with `moom-font'
(when (require 'moom-font nil t)
(moom-font-ja "Migu 2M")
(moom-font-ascii "Inconsolata")
(moom-font-resize 24)))
(with-eval-after-load "moom-transient"
(moom-transient-hide-cursor)
(setq moom-transient-dispatch-sticky nil))
(with-eval-after-load "smartparens"
(setq-default sp-highlight-pair-overlay nil)
(setq-default sp-highlight-wrap-overlay nil)
(setq-default sp-highlight-wrap-tag-overlay nil)
(sp-pair "`" nil :actions :rem)
(sp-pair "'" nil :actions :rem)
(sp-pair "[" nil :actions :rem)
(sp-local-pair 'org-mode "$" "$")
(sp-local-pair 'org-mode "~" "~")
;; (sp-local-pair 'org-mode "[" "]")
;; (sp-local-pair 'org-mode "+" "+")
(sp-local-pair 'org-mode "=" "=")
(sp-local-pair 'org-mode "_" "_")
(sp-local-pair 'yatex-mode "$" "$"))
(with-eval-after-load "selected"
(defvar my-eval-result "*eval-result*")
(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 (mark) (point)))
(deactivate-mark)))
(setq selected-org-mode-map (make-sparse-keymap))
(define-key selected-org-mode-map (kbd "t") #'org-toggle-checkbox)
(define-key selected-org-mode-map (kbd "-") #'my-org-bullet-and-checkbox)
(define-key selected-keymap (kbd "5") #'query-replace-from-region)
(define-key selected-keymap (kbd ";") #'comment-dwim)
(define-key selected-keymap (kbd "e") #'my-eval-region)
(define-key selected-keymap (kbd "g") #'my-google-this)
(when (require 'helpful nil t)
(define-key selected-keymap (kbd "h") #'helpful-at-point)
(define-key selected-keymap (kbd "v") #'my-helpful-variable))
(when (require 'expand-region nil t)
(define-key selected-keymap (kbd "SPC") #'er/expand-region)))
;; helpful
(with-eval-after-load "helpful"
(defun my-helpful-variable ()
(interactive)
(let ((thing (symbol-at-point)))
(if (helpful--variable-p thing)
(helpful-variable thing)
(call-interactively 'helpful-variable))))
(defun ad:helpful-at-point ()
(deactivate-mark))
(advice-add 'helpful-at-point :before #'ad:helpful-at-point))
(with-eval-after-load "hl-todo"
(defun my-hl-todo-reload ()
(interactive)
(global-hl-todo-mode -1)
(global-hl-todo-mode))
(defun my-hl-todo-light-theme ()
(setq hl-todo-exclude-modes nil)
(setq hl-todo-keyword-faces
'(("HOLD" . "#d0bf8f")
("TODO" . "#FF0000")
("NEXT" . "#dca3a3")
("THEM" . "#dc8cc3")
("PROG" . "#7cb8bb")
("OKAY" . "#7cb8bb")
("DONT" . "#5f7f5f")
("FAIL" . "#8c5353")
("DONE" . "SeaGreen")
("NOTE" . "#d0bf8f")
("KLUDGE" . "#d0bf8f")
("HACK" . "#d0bf8f")
("TEMP" . "#d0bf8f")
("FIXME" . "#3030FF")
("XXX+" . "#cc9393")
("\\?\\?\\?+" . "#cc9393")))
(my-hl-todo-reload)))
(with-eval-after-load "highlight-symbol"
(custom-set-variables
'(highlight-symbol-idle-delay 0.5)))
(with-eval-after-load "volatile-highlights"
(set-face-attribute
'vhl/default-face nil :foreground "#FF3333" :background "#FFCDCD")
(volatile-highlights-mode t)
;; ふわっとエフェクトの追加(ペースト時の色 => カーソル色 => 本来色)
(defun my-vhl-change-color ()
(interactive)
(let ((next 0.2)
(reset 0.5)
(colors '("#F8D3D7" "#F2DAE1" "#EBE0EB" "#E5E7F5" "#DEEDFF")))
(dolist (color colors)
(run-at-time next nil
'set-face-attribute
'vhl/default-face
nil :foreground "#FF3333" :background color)
(setq next (+ 0.05 next)))
(run-at-time reset nil 'vhl/clear-all))
(set-face-attribute 'vhl/default-face
nil :foreground "#FF3333"
:background "#FFCDCD"))
(defun my-yank (&optional ARG)
(interactive)
(yank ARG)
(when window-system
(my-vhl-change-color)))
(global-set-key (kbd "M-v") 'my-yank)
(global-set-key (kbd "C-y") 'my-yank)
(with-eval-after-load "org"
(define-key org-mode-map (kbd "C-y") 'my-org-yank)
(defun my-org-yank ()
(interactive)
(org-yank)
(when window-system
(my-vhl-change-color)))))