-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinit-org.el
2047 lines (1806 loc) · 82.5 KB
/
init-org.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
;; init-org.el --- My config for org mode -*- lexical-binding: t -*-
(defvar my-init-org-start (current-time))
(require 'init-autoloads nil t)
(unless (featurep 'postpone)
(call-interactively 'postpone-pre))
;; テキストファイルを Org Mode で開きます.
(push '("\\.txt$" . org-mode) auto-mode-alist)
;; Font lock を使う
(add-hook 'org-mode-hook #'turn-on-font-lock)
(keymap-global-set "C-c r" 'org-capture)
(keymap-global-set "C-c l" 'org-store-link)
(keymap-global-set "C-c a" 'org-agenda)
(with-eval-after-load "org"
(defvar my-org-modules org-modules) ;; Tricky!!
;; (setq org-modules-loaded t) ;; not a good way
(setq org-modules nil)
(unless noninteractive
(run-with-idle-timer (+ 8 my-default-loading-delay)
nil #'my-org-modules-activate)) ;; will take 350[ms]
;; タイトルを少し強調
(custom-set-faces
'(org-document-title ((t (:foreground "RoyalBlue1" :bold t :height 1.2))))
'(org-document-info ((t (:foreground "DodgerBlue1" :height 1.0)))))
;; 関連モジュールの読み込み
(autoload 'org-eldoc-load "org-eldoc" nil t)
(defun my-org-eldoc-load ()
"Set up org-eldoc documentation function."
(interactive)
(add-function :before-until (local 'eldoc-documentation-function)
#'org-eldoc-documentation-function))
;; 少なくとも org 9.5 では問題が発生しなくなったので,advice 停止.
;; (advice-add 'org-eldoc-load :override #'my-org-eldoc-load)
(add-hook 'org-mode-hook #'org-eldoc-load)
;; org ファイルの集中管理
(setq org-directory (concat (getenv "SYNCROOT") "/org/"))
;; org-store-link で heading に自動的に挿入される id を使う
(setq org-id-link-to-org-use-id t)
;; アーカイブファイルの名称を指定
(setq org-archive-location "%s_archive::")
;; タイムスタンプによるログ収集設定 DONE 時に CLOSED: を記入.
(setq org-log-done 'time) ; 'time 以外に,'(done), '(state) を指定できる
;; ログをドロアーに入れる
(setq org-log-into-drawer t)
;; indent を electric-indent-mode の振る舞いに合わせる
;; (setq org-adapt-indentation t) ;; t の場合,ドロアがインデントされる.
;; Set checksum program path for windows
(when (eq window-system 'w32)
(setq org-mobile-checksum-binary (concat (getenv "SYNCROOT") "/do/cksum.exe")))
;; Set default table export format
(setq org-table-export-default-format "orgtbl-to-csv")
;; Toggle inline images display at startup
(setq org-startup-with-inline-images t)
;; dvipng
(setq org-export-with-LaTeX-fragments t)
;; 数式をハイライト
(setq org-highlight-latex-and-related '(latex entities))
;; orgバッファ内の全ての動的ブロックを保存直前に変更する
;; (add-hook 'before-save-hook #'org-update-all-dblocks)
;; アンダースコアをエクスポートしない(_{}で明示的に表現できる)
(setq org-export-with-sub-superscripts nil)
;; #+options: \n:t と同じ
(setq org-export-preserve-breaks t)
;; タイマーの音
;; (lsetq org-clock-sound "");
;; org-clock の計測時間をモードラインではなくタイトルに表示する
(setq org-clock-clocked-in-display 'frame-title)
;; 1分未満は記録しない
(setq org-clock-out-remove-zero-time-clocks t)
;; 再起動後に clock を復帰させる(clock-out で抜けない限り終了中の期間も計上されてしまう)
;; check also org-clock-persist in org-clock.el
(org-clock-persistence-insinuate)
;; org-clock-out 時にステータスを変える(also configure org-todo-keywords)
(defun my-promote-todo-revision (state)
(cond ((member state '("TODO")) "REV1")
((member state '("REV1")) "REV2")
((member state '("REV2")) "REV3")
(t state)))
;; (setq org-clock-out-switch-to-state #'my-promote-todo-revision)
;; undo 時に reveal して表示を改善する
;; (defun ad:org:undo (&optional _ARG)
;; (when (and (eq major-mode 'org-mode)
;; (not (org-before-first-heading-p)))
;; (org-overview)
;; (org-reveal)
;; (org-cycle-hide-drawers 'all)
;; (org-show-entry)
;; (show-children)
;; (org-show-siblings)))
;; (advice-add 'undo :after #'ad:org:undo)
;; 非表示状態の領域への書き込みを防ぐ
;; "Editing in invisible areas is prohibited, make them visible first"
(setq org-catch-invisible-edits 'show-and-error)
(defun ad:org-return (f &optional arg)
"An extension for checking invisible editing when you hit the enter."
(interactive "P")
(org-check-before-invisible-edit 'insert)
(apply f arg))
(advice-add 'org-return :around #'ad:org-return)
;; ブリッツにアルファベットを使う
(setq org-list-allow-alphabetical t)
;; - を優先.親のブリッツ表示を継承させない
(setq org-list-demote-modify-bullet
'(("+" . "-")
("*" . "-")
("1." . "-")
("1)" . "-")
("A)" . "-")
("B)" . "-")
("a)" . "-")
("b)" . "-")
("A." . "-")
("B." . "-")
("a." . "-")
("b." . "-")))
;; 完了したタスクの配色を変える
;; https://fuco1.github.io/2017-05-25-Fontify-done-checkbox-items-in-org-mode.html
(font-lock-add-keywords
'org-mode
`(("^[ \t]*\\(?:[-+*]\\|[0-9]+[).]\\)[ \t]+\\(\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[\\(?:X\\|\\([0-9]+\\)/\\2\\)\\][^\n]*\n\\)"
1 'org-headline-done prepend))
'append)
;; プロパティ等を自動的閉じる.
(defun my-org-hide-drawers ()
"Hide all drawers in an org tree."
(interactive)
(save-mark-and-excursion
(beginning-of-line)
(unless (looking-at-p org-drawer-regexp)
(org-cycle-hide-drawers 'subtree))))
(add-hook 'org-tab-first-hook 'my-org-hide-drawers)
;; CSV指定でテーブルを出力する.
(defun my-org-table-export ()
(interactive)
(org-table-export nil "orgtbl-to-csv"))
;; すべてのチェックボックスの cookies を更新する
(defun my-do-org-update-staistics-cookies ()
(interactive)
(message "Update statistics...")
(org-update-statistics-cookies 'all)
(let ((inhibit-message t)
(message-log-max nil))
(save-buffer))
(message "Update statistics...done"))
(keymap-set org-mode-map "C-c f 2" 'my-do-org-update-staistics-cookies)
;; C-c & が yasnippet にオーバーライドされているのを張り替える
(keymap-set org-mode-map "C-c 4" 'org-mark-ring-goto)
;; (org-transpose-element) が割り当てられているので取り返す.
(org-defkey org-mode-map "\C-\M-t" 'beginning-of-buffer))
(with-eval-after-load "ox"
(add-to-list 'org-modules 'ox-odt)
(add-to-list 'org-modules 'ox-org)
(add-to-list 'org-modules 'ox-json)) ;; FIXME
(with-eval-after-load "org-tempo"
;; 空行のとき "<" をインデントさせない
(defun ad:org-tempo-complete-tag (f &rest arg)
(if (save-excursion
(beginning-of-line)
(looking-at "<"))
(let ((indent-line-function 'ignore))
(apply f arg))
(apply f arg)))
(advice-add 'org-tempo-complete-tag :around #'ad:org-tempo-complete-tag))
;; (Thanks to @conao3)
;; but when using `flet', byte-compile will warn a malformed function
;; and using `cl-flet' will not provide us the expected result...
;; (when (require 'cl-lib nil t)
;; (defun ad:org-tempo-complete-tag (f &rest arg)
;; (if (save-excursion
;; (beginning-of-line)
;; (looking-at "<"))
;; (cl-flet ((indent-according-to-mode () #'ignore))
;; (apply f arg))
;; (apply f arg)))
;; (advice-add 'org-tempo-complete-tag :around #'ad:org-tempo-complete-tag))
(with-eval-after-load "org-tempo"
(defun my-org-tempo-add-block (entry)
"Add block entry from `org-structure-template-alist'."
(let* ((key (format "<%s" (car entry)))
(name (cdr entry))
(special nil)) ;; FIXED
(tempo-define-template
(format "org-%s" (replace-regexp-in-string " " "-" name))
`(,(format "#+begin_%s%s" name (if special " " ""))
,(when special 'p) '> n '> ,(unless special 'p) n
,(format "#+end_%s" (car (split-string name " ")))'
>)
key
(format "Insert a %s block" name)
'org-tempo-tags)))
;; 更新
(advice-add 'org-tempo-add-block :override #'my-org-tempo-add-block)
;; 反映
(org-tempo-add-templates))
(with-eval-after-load "org-clock"
;; nil or 'history ならば,org-onit が org-clock-out を実行する.
(setq org-clock-persist 'history) ;; {nil, t, 'clock, 'history}
(setq org-clock-in-resume t)
(setq org-clock-persist-query-resume nil)
(advice-add 'org-clock-load :around #'ad:suppress-message)
;; 終了時に clock を止める.
(defun my-org-clock-out-and-save-when-exit ()
"Save buffers and stop clocking when kill emacs."
(when (org-clocking-p)
(org-clock-out)
(save-some-buffers t)))
;; implemented in `org-onit.el'. No need to hook this.
;; (add-hook 'kill-emacs-hook #'my-org-clock-out-and-save-when-exit)
)
(with-eval-after-load "org-table"
;; エコー表示前に保存する
(defun ad:org-table-field-info (_arg)
(save-buffer))
(advice-add 'org-table-field-info :before #'ad:org-table-field-info))
;; ~/Dropbox/Public は第三者に探索される可能性があるので要注意
;; default = ~/org.ics
;; C-c C-e i org-export-icalendar-this-file
;; C-c C-e I org-export-icalendar-all-agenda-files
;; C-c C-e c org-export-icalendar-all-combine-agenda-files
(when (autoload-if-found '(my-ox-icalendar
my-async-ox-icalendar my-ox-icalendar-cleanup)
"ox-icalendar" nil t)
(with-eval-after-load "org"
(keymap-set org-mode-map "C-c f 1" 'my-ox-upload-icalendar))
(with-eval-after-load "ox-icalendar"
(defvar org-ical-file-in-orz-server nil) ;; see private.el
;; 生成するカレンダーファイルを指定
;; 以下の設定では,このファイルを一時ファイルとして使う(削除する)
(setq org-icalendar-combined-agenda-file "~/Desktop/org-ical.ics")
;; iCal の説明文
(setq org-icalendar-combined-description "OrgModeのスケジュール出力")
;; カレンダーに適切なタイムゾーンを設定する(google 用には nil が必要)
(setq org-icalendar-timezone "Asia/Tokyo")
;; DONE になった TODO はアジェンダから除外する
(setq org-icalendar-include-todo t)
;; 通常は,<>--<> で区間付き予定をつくる.非改行入力で日付がNoteに入らない
(setq org-icalendar-use-scheduled '(event-if-todo))
;; DL 付きで終日予定にする:締め切り日(スタンプで時間を指定しないこと)
;; (setq org-icalendar-use-deadline '(event-if-todo event-if-not-todo))
(setq org-icalendar-use-deadline '(event-if-todo))
(defun my-ox-upload-icalendar ()
(interactive)
(when (and org-ical-file-in-orz-server
(eq system-type 'darwin))
(if (require 'async nil t)
(my-async-ox-icalendar)
(my-ox-icalendar))))
(defun my-ox-icalendar ()
(let ((message-log-max nil)
(org-agenda-files '("~/Dropbox/org/org-ical.org")))
;; org-icalendar-export-to-ics を使うとクリップボードが荒れる
(org-icalendar-combine-agenda-files))
;; 自サーバにアップロード
(message "Uploading...")
(if (eq 0 (shell-command
(concat "scp -o ConnectTimeout=5 "
org-icalendar-combined-agenda-file " "
org-ical-file-in-orz-server)))
(message "Uploading...done")
(message "Uploading...miss!"))
(my-ox-icalendar-cleanup))
(defun my-async-ox-icalendar ()
(message "[async] Uploading...")
(async-start
`(lambda ()
(when (and (load "~/.emacs" nil t)
(load "~/.emacs.d/lisp/init-org.el" nil t)
(require 'org nil t)
(require 'org-agenda nil t))
(setq org-agenda-files '("~/Dropbox/org/org-ical.org"))
(if (file-exists-p
(expand-file-name org-icalendar-combined-agenda-file))
1
(let ((ical (org-icalendar-combine-agenda-files))
(result (shell-command
(concat "scp -o ConnectTimeout=5 "
',org-icalendar-combined-agenda-file " "
',org-ical-file-in-orz-server))))
(my-ox-icalendar-cleanup)
result))))
(lambda (result)
(unless (active-minibuffer-window)
(message (format "[async] Uploading...%s"
(cond ((eq result 0) "done")
((eq result 1) "skipped")
(t "miss!"))))))))
(defun my-ox-icalendar-cleanup ()
(interactive)
(when (file-exists-p
(expand-file-name org-icalendar-combined-agenda-file))
(shell-command-to-string
(concat "rm -rf " org-icalendar-combined-agenda-file))))))
(with-eval-after-load "org"
(setq org-use-speed-commands t)
(when (version< (org-version) "9.4.6")
(defvaralias 'org-speed-commands 'org-speed-commands-user))
;; "C"(org-shifttab) をオーバーライド
(add-to-list 'org-speed-commands '("C" org-copy-subtree))
(add-to-list 'org-speed-commands '("d" my-done-with-update-list))
;; (add-to-list 'org-speed-commands '("S" call-interactively 'widen))
(add-to-list 'org-speed-commands
'("D" my-org-todo-complete-no-repeat "DONE"))
;; (add-to-list 'org-speed-commands '("N" org-shiftmetadown))
;; (add-to-list 'org-speed-commands '("P" org-shiftmetaup))
(add-to-list 'org-speed-commands '("H" my-hugo-export-upload))
(add-to-list 'org-speed-commands '("h" org-hugo-export-wim-to-md))
(add-to-list 'org-speed-commands '("E" my-export-subtree-as-html))
(add-to-list 'org-speed-commands '("." my-org-deadline-today))
(add-to-list 'org-speed-commands '("!" my-org-default-property))
(add-to-list 'org-speed-commands '("y" my-org-yank))
(add-to-list 'org-speed-commands '("x" my-org-move-subtree-to-the-last))
(add-to-list 'org-speed-commands
'("$" call-interactively 'org-archive-subtree))
;; done にして,apptを更新する
(defun my-done-with-update-list ()
(interactive)
(org-todo "DONE")
(my-org-agenda-to-appt))
;; 周期タクスを終了させます.
(defun my-org-todo-complete-no-repeat (&optional ARG)
(interactive "P")
(when (org-get-repeat)
(org-cancel-repeater))
(if (eq (current-buffer) org-agenda-buffer)
(org-agenda-todo ARG)
(org-todo ARG)))
(defun my-org-replace-punc-in-buffer ()
"Replace \",\" and \".\" with \"、\" and \"。\" in a buffer."
(interactive)
(goto-char (point-min))
(while (re-search-forward "\\(,\\)\\|\\(.\\)" nil :noerror)
(let ((w (match-string-no-properties 0)))
(cond ((equal w ",") (replace-match "、"))
((equal w ".") (replace-match "。"))))))
(defun my-org-replace-punc-in-tree ()
"Replace \",\" and \".\" with \"、\" and \"。\" in an org tree."
(interactive)
(org-back-to-heading t)
(let* ((element (org-element-at-point))
(begin (org-element-property :begin element))
(end (org-element-property :end element)))
(when (eq (org-element-type element) 'headline)
(goto-char begin)
(while (re-search-forward "\\(,\\)\\|\\(.\\)" end :noerror)
(let ((w (match-string-no-properties 0)))
(cond ((equal w ",") (replace-match "、"))
((equal w ".") (replace-match "。")))))
(goto-char begin))))
;; Hugo の記事を書き出し&アップロード
(defun my-hugo-export-upload ()
"Export subtree for Hugo and upload the engty."
(when (member (buffer-name) '("imadenale.org" "archive.org"))
(if (not (org-entry-is-done-p))
(message "The state of the entry is not \"DONE\" yet.")
(my-org-replace-punc-in-tree)
(save-buffer)
;; (let ((outfile (org-hugo-export-wim-to-md)))
;; (sit-for 2)
;; (when (and outfile
;; (file-exists-p outfile))
;; (switch-to-buffer
;; (find-file-noselect outfile)
;; (my-org-replace-punc-in-buffer))))
(org-hugo-export-wim-to-md)
(let ((command "/Users/taka/Dropbox/scripts/push-hugo.sh")
(filename (org-entry-get (point) "EXPORT_FILE_NAME"))
(exported (format "[ox-hugo] \"%s\" has been exported."
(nth 4 (org-heading-components)))))
(when filename
;; (when (file-exists-p (concat outfile ".md"))
;; (switch-to-buffer
;; (find-file-noselect (concat outfile ".md"))
;; (my-org-replace-punc-in-buffer)
;; (save-buffer)))
(save-excursion
(save-restriction
(outline-up-heading 1)
(setq filename
(concat (nth 4 (org-heading-components)) "/" filename))
(setq command (concat command " -e " (downcase filename)))))
(message "[hugo] %s" command)
(if (require 'async nil t)
(progn
(message "%s\n[async] Uploading..." exported)
(async-start
`(lambda () (shell-command-to-string ',command))
`(lambda (result)
(message "%s\n[async] Uploading...%s"
',exported (when result "done"))
(message "[log] %s" result))))
(message "%s\nUploading..." exported)
(message "%s" (shell-command-to-string command))
(message "%s\nUploading...done" exported)))))))
;; カーソル位置のサブツリーをデスクトップにHTMLエクスポートする
(defun my-export-subtree-as-html ()
(interactive)
(let ((file "~/Desktop/note.html"))
(org-export-to-file 'html file nil t)
(org-open-file file)))
;; 締切を今日にする.agenda から起動したカレンダー内では "C-." でOK(標準)
(defun my-org-deadline-today ()
(when (org-entry-is-todo-p)
(let ((date (org-entry-get (point) "DEADLINE"))
(today (format-time-string "%F")))
(org-deadline 'deadline
(if date
(format "<%s%s"
today
(substring date 11 (string-width date)))
(format "<%s>" today))))))
;; 現在のツリーを畳んでから同じレベルの最後の要素として移動する
(defcustom my-org-move-subtree-to-the-last-after-hook nil""
:type 'hook :group 'org)
(defun my-org-move-subtree-to-the-last ()
"Move the current heading to the last one of the same level."
(interactive)
(let ((cnt 0) beg)
(org-back-to-heading)
(outline-hide-subtree)
(setq beg (point))
(while (and (funcall 'org-get-next-sibling)
(looking-at org-outline-regexp))
(setq cnt (1+ cnt)))
(goto-char beg)
(when (> cnt 0)
(org-move-subtree-down cnt)
(goto-char beg)))
(run-hooks 'my-org-move-subtree-to-the-last-after-hook)))
(with-eval-after-load "org"
;; Font lock を使う
;; (global-font-lock-mode 1) ;; see org-mode-hook
;; ウィンドウの端で折り返す
(setq org-startup-truncated nil)
;; サブツリー以下の * を略式表示する
(setq org-hide-leading-stars t)
;; Color setting for TODO keywords
;; Color for priorities
;; (setq org-priority-faces
;; '(("?A" :foreground "#E01B4C" :background "#FFFFFF" :weight bold)
;; ("?B" :foreground "#1739BF" :background "#FFFFFF" :weight bold)
;; ("?C" :foreground "#575757" :background "#FFFFFF" :weight bold)))
;; Color setting for Tags
;; #CC3333
(setq org-todo-keyword-faces
'(("FOCUS" :foreground "#FF0000" :background "#FFCC66")
("BUG" :foreground "#FF0000" :background "#FFCC66")
("CHECK" :foreground "#FF9900" :background "#FFF0F0" :underline t)
("ICAL" :foreground "#33CC66")
("APPROVED" :foreground "#66CC66")
("QUESTION" :foreground "#FF0000")
("WAIT" :foreground "#CCCCCC" :background "#666666")
("MAIL" :foreground "#CC3300" :background "#FFEE99")
("PLAN" :foreground "#FF6600")
("PLAN2" :foreground "#FFFFFF" :background "#FF6600")
("REV1" :foreground "#3366FF")
("REV2" :foreground "#3366FF" :background "#99CCFF")
("REV3" :foreground "#FFFFFF" :background "#3366FF")
("SLEEP" :foreground "#9999CC")))
;; (:foreground "#0000FF" :bold t) ; default. do NOT put this bottom
(setq org-tag-faces
'(("Achievement" :foreground "#66CC66")
("Bug" :foreground "#FF0000")
("Report" :foreground "#66CC66")
("Background" :foreground "#66CC99")
("Chore" :foreground "#6699CC")
("project" :foreground "#6666CC")
("read" :foreground "#6666CC")
("book" :foreground "#6666CC")
("Doing" :foreground "#FF0000")
("Draft" :foreground "#9933CC") ;; Draft(r1,r2,r3)->Review(1,2)
("Review" :foreground "#6633CC")
("Revisit" :foreground "#6633CC")
("Redmine" :foreground "#CC6666")
("Ongoing" :foreground "#CC6666") ; for non scheduled/reminder
("Template" :foreground "#66CC66")
("Repeat" :foreground "#CC9999") ; for interval tasks
("Mag" :foreground "#9966CC")
("buy" :foreground "#9966CC")
("pay" :foreground "#CC6699")
("try" :foreground "#FF3366")
("secret" :foreground "#FF0000")
("emacs" :foreground "#6633CC")
("note" :foreground "#6633CC")
("print" :foreground "#6633CC")
("study" :foreground "#6666CC")
("Implements" :foreground "#CC9999" :weight bold)
("Coding" :foreground "#CC9999")
("Editing" :foreground "#CC9999" :weight bold)
("work" :foreground "#CC9999" :weight bold)
("Survey" :foreground "#CC9999" :weight bold)
("Home" :foreground "#CC9999" :weight bold)
("Open" :foreground "#CC9999" :weight bold)
("Blog" :foreground "#9966CC")
("story" :foreground "#FF7D7D")
("plan" :foreground "#FF7D7D")
("issue" :foreground "#FF7D7D")
("Test" :foreground "#FF0000" :weight bold)
("attach" :foreground "#FF0000")
("drill" :foreground "#66BB66" :underline t)
("DEBUG" :foreground "#FFFFFF" :background "#9966CC")
("EVENT" :foreground "#FFFFFF" :background "#9966CC")
("Thinking" :foreground "#FFFFFF" :background "#96A9FF")
("Schedule" :foreground "#FFFFFF" :background "#FF7D7D")
("INPUT" :foreground "#FFFFFF" :background "#CC6666")
("OUTPUT" :foreground "#FFFFFF" :background "#66CC99")
("CYCLE" :foreground "#FFFFFF" :background "#6699CC")
("weekend" :foreground "#FFFFFF" :background "#CC6666")
("Log" :foreground "#008500"))))
(with-eval-after-load "org"
(setq org-todo-keywords
'((sequence "TODO(t)" "PLAN(p)" "PLAN2(P)" "|" "DONE(d)")
(sequence "FOCUS(f)" "CHECK(C)" "ICAL(c)" "|" "DONE(d)")
(sequence "WAIT(w)" "SLEEP(s)" "QUESTION(q)" "|" "DONE(d)")
(sequence "REV1(1)" "REV2(2)" "REV3(3)" "|" "APPROVED(a@/!)")))
;; Global counting of TODO items
(setq org-hierarchical-todo-statistics nil)
;; Global counting of checked TODO items
(setq org-hierarchical-checkbox-statistics nil)
;; block-update-time
(defun org-dblock-write:block-update-time (params)
(let ((fmt (or (plist-get params :format) "%Y-%m-%d")))
(insert "" (format-time-string fmt (current-time))))))
(with-eval-after-load "org"
(setq org-image-actual-width '(256))
(add-to-list 'image-file-name-extensions "jp2")
;; (add-to-list 'image-file-name-extensions "j2c")
(add-to-list 'image-file-name-extensions "bmp")
(add-to-list 'image-file-name-extensions "psd"))
(push '("[rR][eE][aA][dD][mM][eE]" . org-mode) auto-mode-alist)
(with-eval-after-load "org"
(defun my-lowercase-org-keywords ()
"Lower case Org keywords and block identifiers."
(interactive)
(save-excursion
(goto-char (point-min))
(let ((case-fold-search nil)
(count 0))
(while (re-search-forward
"\\(?1:#\\+[A-Z_]+\\(?:_[[:alpha:]]+\\)*\\)\\(?:[ :=~’”]\\|$\\)"
nil :noerror)
(setq count (1+ count))
(let* ((prev (match-string-no-properties 1))
(new (downcase prev)))
(replace-match new :fixedcase nil nil 1)
(message "Updated(%d): %s => %s" count prev new)))
(message "Lower-cased %d matches" count)))))
(with-eval-after-load "org"
;; Select from Preferences: { Funk | Glass | ... | Purr | Pop ... }
(defvar ns-default-notification-sound "Pop")
(defvar ns-alerter-command (concat (getenv "HOME") "/Dropbox/bin/alerter")
"Path to alerter command. see https://github.com/vjeantet/alerter")
(setq ns-alerter-command 'script) ;; the alerter is not work for now(2024-02-18).
(unless ns-alerter-command
(setq ns-alerter-command "")) ;; FIXME
(when (or (eq ns-alerter-command 'script)
(executable-find ns-alerter-command))
(setq org-show-notification-handler #'my-desktop-notification-handler)))
(unless noninteractive
(with-eval-after-load "org"
(let ((file "~/Dropbox/org/db/daily.org"))
(when (and (file-exists-p file)
(require 'utility nil t))
(my-set-alarms-from-file file) ;; init
(add-hook 'after-save-hook #'my-update-alarms-from-file))))) ;; update
(with-eval-after-load "org"
(defun my-countdown-timer-notify ()
;; (when mode-line-format
;; (my-mode-line-off))
(when ns-alerter-command
(setq org-show-notification-handler #'my-desktop-notification-handler))
(remove-hook 'org-timer-done-hook #'my-countdown-timer-notify)
(remove-hook 'org-timer-stop-hook #'my-countdown-timer-notify)
(my-desktop-notification "### Expired! ###" "Time is up!" t "Glass"))
(defalias 'run-timer 'my-countdown-timer)
(defun my-countdown-timer ()
(interactive)
;; (unless mode-line-format
;; (my-mode-line-on))
(when (eq org-show-notification-handler #'my-desktop-notification-handler)
(setq org-show-notification-handler nil))
(with-temp-buffer
(org-mode)
(insert "* Countdown")
(add-hook 'org-timer-done-hook #'my-countdown-timer-notify)
(add-hook 'org-timer-stop-hook #'my-countdown-timer-notify)
(org-timer-set-timer))))
(when (autoload-if-found '(org-mode my-load-echo-org-link)
"org" nil t)
(add-hook 'org-mode-hook #'my-load-echo-org-link)
(with-eval-after-load "org"
(defvar my-org-link-prompt "Link:")
(defun my-echo-org-link ()
(when (org-in-regexp org-link-bracket-re 1)
(let ((l (length my-org-link-prompt))
(msg (org-link-unescape (match-string-no-properties 1))))
(put-text-property 0 l 'face 'minibuffer-prompt my-org-link-prompt)
(eldoc-message (format "%s %s" my-org-link-prompt msg)))))
(defun my-load-echo-org-link ()
(add-function :before-until (local 'eldoc-documentation-function)
#'my-echo-org-link)
;; (setq-local eldoc-documentation-function #'my-echo-org-link)
)))
(with-eval-after-load "org"
(org-defkey org-mode-map (kbd "M-p") #'my-org-meta-next)
(org-defkey org-mode-map (kbd "M-n") #'my-org-meta-previous)
(org-defkey org-mode-map (kbd "M-b") #'my-org-meta-backward)
(org-defkey org-mode-map (kbd "M-f") #'my-org-meta-forward))
(defun my-org-item-has-child-p ()
"Return t, if the item has at least a child item."
(save-excursion
(beginning-of-line)
(org-list-has-child-p (point) (org-list-struct))))
(defun my-org-heading-has-child-p ()
"Return t, if the heading has at least a child heading."
(save-excursion
(org-goto-first-child)))
(defun my-org-meta-previous ()
"Move item or subtree down, otherwise `scroll-up'."
(interactive)
(cond ((org-at-item-p)
(call-interactively 'org-move-item-down))
((or (looking-at org-heading-regexp)
(and (org-at-heading-p) (eolp)))
(call-interactively 'org-move-subtree-down))
((org-at-table-p)
(call-interactively 'org-table-move-row))
(t nil))) ;; (call-interactively 'scroll-up)
(defun my-org-meta-next ()
"Move item or subtree up, otherwise `scroll-down'."
(interactive)
(cond ((org-at-item-p)
(call-interactively 'org-move-item-up))
((or (looking-at org-heading-regexp)
(and (org-at-heading-p) (eolp)))
(call-interactively 'org-move-subtree-up))
((org-at-table-p)
(org-call-with-arg 'org-table-move-row 'up))
(t nil))) ;; (call-interactively 'scroll-down))))
(defvar my-org-promote-demote-independently nil)
(defun my-inherit-struct-p ()
(and (not my-org-promote-demote-independently)
(or (my-org-item-has-child-p) (my-org-heading-has-child-p))))
(defun my-org-at-meta-fb-p ()
"Return t, if the cursor stay at item, heading, or table."
(or (org-at-item-p)
(looking-at org-heading-regexp)
(and (org-at-heading-p) (eolp))
(org-at-table-p)))
(defun my-org-meta-forward ()
(interactive)
(if (my-org-at-meta-fb-p)
(if (my-inherit-struct-p)
(org-shiftmetaright)
(org-metaright)) ;; FIXME similar check to my-org-at-meta-fb-p
(if (and (fboundp 'syntax-subword-mode)
syntax-subword-mode)
(call-interactively 'syntax-subword-forward)
(forward-word))))
(defun my-org-meta-backward ()
(interactive)
(if (my-org-at-meta-fb-p)
(if (my-inherit-struct-p)
(org-shiftmetaleft)
(org-metaleft)) ;; FIXME similar check to my-org-at-meta-fb-p
(if (and (fboundp 'syntax-subword-mode)
syntax-subword-mode)
(call-interactively 'syntax-subword-backward)
(backward-word))))
(defun my-org-table-copy-as (&optional format)
"Copy converted table."
(interactive)
(let ((format (or format
(org-entry-get (point) "<tab>LE_EXPORT_FORMAT" t)
org-table-export-default-format)))
(if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
(let ((transform (intern (match-string 1 format)))
(params (and (match-end 2)
(read (concat "(" (match-string 2 format) ")"))))
(table (org-table-to-lisp)))
(if (not (org-at-table-p))
(user-error "The cursor is not at a table")
(with-temp-buffer
(insert (funcall transform table params) "\n")
(clipboard-kill-ring-save (point-min) (point-max)))))
(user-error "<tab>LE_EXPORT_FORMAT invalid"))))
(defun my-org-table-convert-to (&optional format)
"Convert a table to FORMAT.
If FORMAT is nil, it is set equal to a property value specified
by \"<tab>LE_EXPORT_FORMAT\" or `org-table-export-default-format'.
Converted table is copied to kill ring for further use.
The core part is extracted from `org-table-export'."
(interactive)
(let ((format (or format
(org-entry-get (point) "<tab>LE_EXPORT_FORMAT" t)
org-table-export-default-format)))
(if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
(let ((transform (intern (match-string 1 format)))
(params (and (match-end 2)
(read (concat "(" (match-string 2 format) ")"))))
(table (org-table-to-lisp)))
(if (not (org-at-table-p))
(user-error "The cursor is not at a table")
(kill-region (org-table-begin) (org-table-end))
(let ((begin (point)))
(insert (funcall transform table params))
(clipboard-kill-ring-save begin (point))
(insert "\n"))))
(user-error "<tab>LE_EXPORT_FORMAT invalid"))))
(with-eval-after-load "eldoc"
(defvar my-eldoc-disable-in-org-block nil)
(defun ad:eldoc-print-current-symbol-info (f &optional interactive)
"Run `eldoc' when the cursor is NOT located in org source block."
(interactive '(t))
(unless (or my-eldoc-disable-in-org-block
(and (eq major-mode 'org-mode)
(eq (car (org-element-at-point)) 'src-block)))
(funcall f interactive)))
(advice-add 'eldoc-print-current-symbol-info :around
#'ad:eldoc-print-current-symbol-info))
(with-eval-after-load "org"
(advice-add 'org-reveal :around #'ad:org-reveal))
(defface my-org-emphasis-bold
'((default :inherit bold)
(((class color) (min-colors 88) (background light))
:foreground "#5b5caf" :background "#e6ebfa") ;; #a60000 #4E4F97 #c7e9fa
(((class color) (min-colors 88) (background dark))
:foreground "#99B2FF")) ;; #ff8059 #BCBCDB #6666D6 #879EE2
"My bold emphasis for Org.")
(defface my-org-emphasis-italic
'((default :inherit italic)
(((class color) (min-colors 88) (background light))
:foreground "#005e00" :background "#B4EAB4")
(((class color) (min-colors 88) (background dark))
:foreground "#44bc44"))
"My italic emphasis for Org.")
(defface my-org-emphasis-underline
'((default :inherit underline)
(((class color) (min-colors 88) (background light))
:foreground "#813e00")
(((class color) (min-colors 88) (background dark))
:foreground "#d0bc00"))
"My underline emphasis for Org.")
(defface my-org-emphasis-strike-through
'((((class color) (min-colors 88) (background light))
:strike-through "#972500" :foreground "#505050")
(((class color) (min-colors 88) (background dark))
:strike-through "#ef8b50" :foreground "#a8a8a8"))
"My strike-through emphasis for Org.")
(with-eval-after-load "org"
(custom-set-variables ;; call org-set-emph-re
'(org-emphasis-alist '(("~" org-code verbatim)
("=" org-verbatim verbatim)
("*" my-org-emphasis-bold)
("/" my-org-emphasis-italic)
("_" my-org-emphasis-underline)
("+" my-org-emphasis-strike-through))))
(custom-set-faces
'(org-code
((t (:foreground "red" :background "pink" :inherit shadow))))
'(org-verbatim
((t (:foreground "#ff6059" :background "PeachPuff" :inherit shadow)))))
(when (featurep 'org-extra-emphasis)
(org-extra-emphasis-update))) ;; to apply configured `org-emphasis-alist'
(with-eval-after-load "org"
(keymap-set org-mode-map "C-c x" #'my-org-move-item-end)
(keymap-set org-mode-map "C-c X" #'my-org-move-item-begin))
(when (autoload-if-found '(org-capture)
"org-capture" nil t)
(with-eval-after-load "org"
;; キャプチャ時に作成日時をプロパティに入れる
;; Thanks to https://emacs.stackexchange.com/questions/21291/add-created-timestamp-to-logbook
(defun my-org-default-property ()
"Set the creation date and org-id."
(interactive)
(my-org-set-created-property)
(org-id-get-create))
(defvar my-org-created-property-name "CREATED"
"The name of the org-mode property.
This user property stores the creation date of the entry")
(defun my-org-set-created-property (&optional active NAME)
"Set a property on the entry giving the creation time.
By default the property is called CREATED. If given the `NAME'
argument will be used instead. If the property already exists, it
will not be modified."
(interactive)
(let* ((created (or NAME my-org-created-property-name))
(fmt (if active "<%s>" "[%s]"))
(now (format fmt (format-time-string "%Y-%m-%d %a %H:%M")))
(field (org-entry-get (point) created nil)))
(unless (or field (equal "" field))
(org-set-property created now)
(org-cycle-hide-drawers 'children))))
(defun ad:org-insert-todo-heading (_arg &optional _force-heading)
(unless (org-at-item-checkbox-p)
(my-org-default-property)))
(advice-add 'org-insert-todo-heading :after #'ad:org-insert-todo-heading))
(with-eval-after-load "org-capture"
(defun my-toggle-org-block-visibility ()
"Testing..."
(interactive)
(when (looking-at org-drawer-regexp)
(org-flag-drawer ; toggle block visibility
(not (get-char-property (match-end 0) 'invisible)))))
(add-hook 'org-capture-before-finalize-hook #'my-org-set-created-property)
;; 2010-06-13 の形式では,タグとして認識されない
(defun get-current-date-tags () (format-time-string "%Y%m%d"))
(setq org-default-notes-file (concat org-directory "next.org"))
(defvar org-capture-academic-file (concat org-directory "academic.org"))
(defvar org-capture-ical-file (concat org-directory "org-ical.org"))
(defvar org-capture-buffer-file (concat org-directory "db/buffer.org"))
(defvar org-capture-notes-file (concat org-directory "db/note.org"))
(defvar org-capture-english-file (concat org-directory "db/english.org"))
(defvar org-capture-diary-file (concat org-directory "log/diary.org"))
(defvar org-capture-article-file (concat org-directory "db/article.org"))
(defvar org-capture-blog-file
(concat org-directory "blog/entries/imadenale.org"))
;; see org.pdf:p73
(setq org-capture-templates
`(("t" "TODO 項目を INBOX に貼り付ける" entry
(file+headline ,org-default-notes-file "INBOX") "** TODO %?\n")
("a" "記事リストにエントリー" entry
(file+headline ,org-capture-article-file "INBOX")
"** READ %?\n\t")
("c" "同期カレンダーにエントリー" entry
(file+headline ,org-capture-ical-file "Scheduled")
"** TODO %?\n\t")
("d" "Doingタグ付きのタスクをInboxに投げる" entry
(file+headline ,org-default-notes-file "INBOX")
"** TODO %? :Doing:\n - \n"
:clock-in t
:clock-keep t)
("l" "本日のチェックリスト" entry
(file+headline ,org-capture-diary-file "Today")
"** FOCUS 本日のチェックリスト %T\n(起床時間の記録)[[http://www.hayaoki-seikatsu.com/users/takaxp/][早起き日記]] \n(朝食)\n - [ ] %?\n(昼食)\n(帰宅/夕食)\n----\n(研究速報)\n - [ ] \n")
("i" "アイディアを書き込む" entry (file+headline ,org-default-notes-file "INBOX")
"** %?\n - \n\t%U")
("b" "Create new post for imadenale blog" entry
(file+headline ,org-capture-blog-file ,(format-time-string "%Y"))
"** TODO \n:PROPERTIES:\n:EXPORT_FILE_NAME: %?\n:EXPORT_HUGO_TAGS: \n:EXPORT_HUGO_LASTMOD: \n:EXPORT_HUGO_IMAGES: \n:END:\n{{< tweet user=\"takaxp\" id=\"\" >}}\n")
("B" "Create new post for imadenale blog (UUID)" entry
(file+headline ,org-capture-blog-file ,(format-time-string "%Y"))
"** TODO %?\n:PROPERTIES:\n:EXPORT_FILE_NAME: %(uuid-string)\n:EXPORT_HUGO_TAGS: \n:EXPORT_HUGO_LASTMOD: \n:EXPORT_HUGO_IMAGES: \n:END:\n{{< tweet user=\"takaxp\" id=\"\" >}}\n")
;; ("b" "Bug タグ付きの TODO 項目を貼り付ける" entry
;; (file+headline ,org-default-notes-file "INBOX")
;; "** TODO %? :bug:\n %i\n %a %t")
("T" "時間付きエントリー" entry (file+headline ,org-default-notes-file "INBOX")
"** %? %T--\n")
("n" "ノートとしてINBOXに貼り付ける" entry
(file+headline ,org-default-notes-file "INBOX")
"** %? :note:\n\t%U")
("D" "「ドラッカー365の金言」をノートする" entry
(file+headline ,org-capture-notes-file "The Daily Drucker")
"** 「%?」\nDrucker) \n - \n - \nACTION POINT:\n - \nQUESTION:\n - \n")
("r" ,(concat "研究ノートを " org-capture-academic-file
" に書き込む")
entry (file+headline ,org-capture-academic-file "Survey")
"** %? :note:\n# \n - \n\t%U")
("`" ,(concat "ノートをバッファ " org-capture-buffer-file
" に書き込む")
entry (file+headline ,org-capture-buffer-file "Buffers")
"** %(get-random-string 16) %U\n\n%?\n\n----")
("w" ,(concat "英単語を " org-capture-english-file
" に書き込む") entry
(file+headline ,org-capture-english-file "WORDS")
"** %? :%(get-current-date-tags):\n「」\n - ")
("g" ,(concat "英語ノートを " org-capture-english-file
" に書き込む")
entry (file+headline ,org-capture-english-file "GRAMMER")
"** %? :%(get-current-date-tags):\n\n%U")
))))
(with-eval-after-load "org"
;; アジェンダ作成対象(指定しないとagendaが生成されない)
;; ここを間違うと,MobileOrg, iCal export もうまくいかない
(dolist (file (mapcar
(lambda (arg)
(concat (getenv "SYNCROOT") "/org/" arg))
'("org-ical.org" "next.org" "db/cooking.org" "minutes/wg1.org"
"db/daily.org" "db/trigger.org" "academic.org" "tr/work.org"
"org2ja.org" "itr.org" "db/books.org")))
(when (file-exists-p (expand-file-name file))
(add-to-list 'org-agenda-files file 'append)))
(when (eq system-type 'windows-nt) ;; FIXME
(setq org-agenda-files '("~/Dropbox/org/next.org"))))
(with-eval-after-load "org-agenda"
;; sorting strategy
(setq org-agenda-sorting-strategy