summaryrefslogtreecommitdiff
path: root/org-conf.el
blob: 68d4de71ac0b9a9fd2d80a08d40eadd2b4a4ff22 (plain)
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
;; -*- lexical-binding: t; -*-

;;; latex packages

(setq org-latex-packages-alist '(("" "amsfonts" t))
      ;; org-format-latex-options (plist-put org-format-latex-options :scale 1.5)
      )

;;; org-export

(require 'ox)

;;; org capture bookmark

(setq org-capture-bookmark nil)

;;; org adapt indentation

(setq org-adapt-indentation nil)

;;; org modules
(setq org-modules '(ol-gnus ol-info ol-eww))

;;; Hide unnecessary decorations
(setq org-hide-emphasis-markers nil)
(setq org-hide-macro-markers nil)
(setq org-hide-leading-stars nil)

;;; follow link on return
(setq org-return-follows-link t)

;;; org-element

(require 'org-element)

;;; org-clock

(require 'org-clock)

;;; tempo

(require 'org-tempo)

(add-to-list 'org-structure-template-alist (cons "g" "src durand-greek"))
(add-to-list 'org-structure-template-alist (cons "el" "src emacs-lisp"))
;; (add-to-list 'org-modules 'ol-gnus)

;;; Keys

(define-key global-map (vector ?\H-a) #'org-agenda)
(define-key global-map (vector 3 97) #'org-agenda)
(define-key global-map (vector 3 99) #'org-capture)
(define-key global-map (vector 3 ?l) #'org-store-link)

(define-key org-mode-map (vector ?\C-') nil)
(define-key org-mode-map (vector ?\C-,) nil)
(define-key org-mode-map (vector 'C-return) nil)
(define-key org-mode-map (vector 'C-S-return) nil)
(define-key org-mode-map (vector 3 ?\S-l) #'org-toggle-link-display)
(define-key org-mode-map (vector 3 ?\C-\S-l) #'org-insert-last-stored-link)

(declare-function 'durand-pulse-pulse-line "~/.emacs.d/basic.el")

(add-hook 'org-follow-link-hook #'durand-pulse-recenter-top)

;;; Some variables

(setq org-todo-keywords
      '((sequence "TODO(t)" "START(s)" "WORKING(w)" "HARD-WORKING(h)" "ALMOST(a)" "|" "DONE(d)")
        (sequence "TO-THINK(c)" "PENDING(p)" "HARD(r)" "IMPOSSIBLE(i)" "|" "SOLVED(v)"))
      org-tags-column -110
      org-special-ctrl-a/e nil
      org-highlight-latex-and-related '(native latex script entities)
      org-agenda-files (mapcar (lambda (fn) (expand-file-name fn org-directory))
			       (list "notes.org"
				     "agenda.org"
				     "aujourdhui.org"
				     "math_article_links.org"))
      org-log-into-drawer t
      org-highest-priority ?A
      org-lowest-priority ?E
      org-default-priority ?B
      org-pretty-entities t
      org-link-file-path-type 'noabbrev

      )

;;; open link functions

(dolist (scheme '("ftp" "http" "https" "mailto" "news"))
  (org-link-set-parameters scheme
                           :follow
                           (lambda (url &optional arg)
			     (browse-url (concat scheme ":" url) arg))))

(use-package "tablist" 'tablist)
(setq pdf-info-epdfinfo-program "/Users/durand/elisp_packages/pdf-tools/epdfinfo" )
(load-config "org-pdftools.el")
(set 'org-pdftools-link-prefix "pdfview")
(org-pdftools-setup-link)

;;; Archive advice

;;;###autoload
(defun durand-archive-save-and-kill (old-fun &optional find-done)
  "Save and kill the buffer after archiving."
  (let* ((location (org-archive--compute-location
                    (or (org-entry-get nil "ARCHIVE" 'inherit)
                        org-archive-location)))
         (archive-buffer-name (file-name-nondirectory (car location)))
         (opened (get-buffer archive-buffer-name)))
    (funcall old-fun find-done)
    (unless opened
      (when (get-buffer archive-buffer-name)
        (with-current-buffer archive-buffer-name
          (ignore-errors (save-buffer 0)))
        (kill-buffer archive-buffer-name)))))

(advice-add 'org-archive-subtree :around 'durand-archive-save-and-kill)

;;; Archiving

;;;###autoload
(defun durand-org-archive-file-name (file-name)
  "Produce an archive file name.
The rule is as follows:
example.org
example.org_archive
example(1).org_archive
example(2).org_archive
etc."
  (cond
   ((not (or (string-match "org$" file-name)
             (string-match "org_archive$" file-name)))
    (user-error "Not an org file.")))
  (cond
   ((string-match "org$" file-name)
    (replace-match "org_archive" nil nil file-name))
   ((string-match "\\((\\([[:digit:]]+\\))\\).org_archive$" file-name)
    (replace-match
     (format "(%d)"
             (1+ (string-to-number
                  (match-string-no-properties 2 file-name))))
     nil nil file-name 1))
   (t
    (replace-regexp-in-string ".org_archive$" "(1).org_archive" file-name))))

;;;###autoload
(defun durand-org-goto-archive ()
  "Go to the archive file of the current org file, if any.
It will cycle through all archive files of the file.
The rule is as follows:
example.org
example.org_archive
example(1).org_archive
example(2).org_archive
etc."
  (interactive)
  (unless (derived-mode-p 'org-mode)
    (user-error "Not in an org file."))
  (let* ((current-name (buffer-file-name (current-buffer)))
         (base-name (cond
                     ((string-match "org_archive$" current-name)
                      (replace-match "org" nil nil current-name))
                     (t current-name)))
         (base-name (cond
                     ((string-match "\\(([[:digit:]])\\).org$" base-name)
                      (replace-match "" nil nil base-name 1))
                     (t base-name)))
         (next-name (durand-org-archive-file-name current-name))
         (next-name (cond
                     ((file-exists-p next-name) next-name)
                     (t base-name))))
    (find-file next-name)))

;;; Set tags

;; NOTE: Modified by Durand to use `completing-read-multiple' instead of
;; `completing-read'.
;;;###autoload
(defun durand-org-set-tags-command (&optional arg)
  "Set the tags for the current visible entry.

When called with `\\[universal-argument]' prefix argument ARG, \
realign all tags
in the current buffer.

When called with `\\[universal-argument] \\[universal-argument]' prefix argument, \
unconditionally do not
offer the fast tag selection interface.

If a region is active, set tags in the region according to the
setting of `org-loop-over-headlines-in-active-region'.

This function is for interactive use only;
in Lisp code use `org-set-tags' instead."
  (interactive "P")
  (let ((org-use-fast-tag-selection
         (unless (equal '(16) arg) org-use-fast-tag-selection)))
    (cond
     ((equal '(4) arg) (org-align-tags t))
     ((and (org-region-active-p) org-loop-over-headlines-in-active-region)
      (let (org-loop-over-headlines-in-active-region) ;  hint: infinite recursion.
        (org-map-entries
         #'org-set-tags-command
         nil
         (if (eq org-loop-over-headlines-in-active-region 'start-level)
             'region-start-level
           'region)
         (lambda () (when (org-invisible-p) (org-end-of-subtree nil t))))))
     (t
      (save-excursion
        (org-back-to-heading)
        (let* ((all-tags (org-get-tags))
               (table (setq org-last-tags-completion-table
                            (org--tag-add-to-alist
                             (and org-complete-tags-always-offer-all-agenda-tags
                                  (org-global-tags-completion-table
                                   (org-agenda-files)))
                             (or org-current-tag-alist (org-get-buffer-tags)))))
               (current-tags
                (cl-remove-if (lambda (tag) (get-text-property 0 'inherited tag))
                              all-tags))
               (inherited-tags
                (cl-remove-if-not (lambda (tag) (get-text-property 0 'inherited tag))
                                  all-tags))
               (tags
                (replace-regexp-in-string
                 ;; Ignore all forbidden characters in tags.
                 "[^[:alnum:]_@#%]+" ":"
                 (if (or (eq t org-use-fast-tag-selection)
                         (and org-use-fast-tag-selection
                              (delq nil (mapcar #'cdr table))))
                     (org-fast-tag-selection
                      current-tags
                      inherited-tags
                      table
                      (and org-fast-tag-selection-include-todo org-todo-key-alist))
                   (let ((org-add-colon-after-tag-completion (< 1 (length table))))
                     ;; I change this part
                     (org-trim
                      (org-make-tag-string
                       (completing-read-multiple
                        "Tags: "
                        #'org-tags-completion-function
                        nil nil (replace-regexp-in-string
                                 ":" ","
                                 (org-make-tag-string current-tags))
                        'org-tags-history))))))))
          (org-set-tags tags)))))
    ;; `save-excursion' may not replace the point at the right
    ;; position.
    (when (and (save-excursion (skip-chars-backward "*") (bolp))
               (looking-at-p " "))
      (forward-char))))

(advice-add 'org-set-tags-command :override 'durand-org-set-tags-command)

;;; Accounts

;;;###autoload
(defvar durand-org-capture-account-which-list
  '("breakfast" "brunch" "brunverage" "lunch" "dinner" "beverage" "snack" "fruit")
  "The list of purposes of accounts in the capture-template for accounting.")

;;;###autoload
(defun durand-org-capture-account-template ()
  "Org capture template for account."
  (let* ((shop-and-items (durand-org-complete-capture-account))
         (which (completing-read
                 "For what? "
                 durand-org-capture-account-which-list))
         (cost (number-to-string
                (read-number "Cost: " 0)))
         (from (completing-read "From: " '("Cash" "etique")))
         (active-time (format-time-string
                       (cdr org-time-stamp-formats)))
         (inactive-time
          (let ((temp active-time))
            (while (string-match "<\\|>" temp)
              (setf temp
                    (replace-match
                     (cond
                      ((string= (match-string-no-properties 0 temp) "<") "[")
                      ((string= (match-string-no-properties 0 temp) ">") "]"))
                     nil nil temp)))
            temp)))
    (format "%s\n  :PROPERTIES:\n  :cost: %s\n  :FROM: %s\n  :RECORD_TIME: %s\n  :END: \n  %s%%?"
            which cost from inactive-time shop-and-items)))

(require 'org-protocol)

;;; Capture templates

(setq org-capture-templates
      '(("d" "Record Diaries" entry
         (file+olp+datetree "~/org/diary.org")
         "* %?\n  :PROPERTIES:\n  :RECORD_TIME: %U\n  :END:\n\n"
         :jump-to-captured t)
        ("l" "Store links" entry
         (file "/Users/durand/org/math_article_links.org")
         "* TO-THINK %? %(org-insert-time-stamp (org-read-date nil t \"+0d\") nil t)\n%a\n" :kill-buffer t)
        ("L" "for storing webpages" entry
         #'org-determine-link-file
         "* PENDING %(org-filter-title) %(org-determine-tag)\n  :PROPERTIES:\n  :RECORD_TIME: %U\n  :END:\n\n  %(org-filtered-link)\n  %i\n  %?"
         :empty-lines 1
         :kill-buffer t
         :immediate-finish t)
        ("t" "TODO" entry
         (file "~/org/aujourdhui.org")
         "* TODO %? %^{Date to do:}t\n  :PROPERTIES:\n  :RECORD_TIME: %U\n  :END:\n\n"
         :kill-buffer t)
        ;; ("b" "Blog posts" entry
        ;;  (file+headline "~/org/notes.org" "Blog posts")
        ;;  "* %? %(org-insert-time-stamp (org-read-date nil t \"+0d\"))\n%i\n")
        ;; ("a" "Abstractions" entry
        ;;  (file+headline "~/org/wiki.org" "Abstractions")
        ;;  "* ABSTRACT %?\n  :PROPERTIES:\n  :RECORD_TIME: %U\n  :END:\n\n")
        ("A" "Agenda" entry
         (file+headline "~/org/agenda.org" "Agenda")
         "* TODO %?\n  :PROPERTIES:\n  :RECORD_TIME: %U\n  :DURATION: %^{Date: }t\n  :END:\n\n")
        ;; ("y" "YiFu" entry
        ;;  (file+headline "~/org/wiki.org" "Yi Fu Tips")
        ;;  "* MEMO %^{word}\n  :PROPERTIES:\n  :STORY: %\\2\n  :MEANING: %\\3\n  :END:\n** Yi Fu story\n   %^{story}\n** Meaning\n   %^{meaning}"
        ;;  :kill-buffer t
        ;;  :immediate-finish t)
        ("c" "Chansons" entry
         (file+headline "~/org/wiki.org" "Liste de Chansons")
         "* MEMO %^{title}\n  :PROPERTIES:\n  :RECORD_TIME: %U\n  :LINK: [[%^{link}][%^{description}]]\n  :END:\n  %?"
         :jump-to-captured t)
        ;; ("f" "français" entry
        ;;  (file+headline "~/org/français/français.org" "Liste de mots français")
        ;;  "* MEMO %^{mot} :drill:\n  :PROPERTIES:\n  :DRILL_CARD_TYPE: français\n  :RECORD_TIME: %U\n  :MEANING: %^{ce qu'il veut dire}\n  :END:\n\n  MEANING: %\\2\n%?"
        ;;  :jump-to-captured t)
        ("g" "GNUS" entry
         (file "~/org/notes.org")
         "* TO-THINK %:subject\n  :PROPERTIES:\n  :RECORD_TIME: %U\n  :END:\n  %:from\n  %:to\n  %a\n  %?"
         :empty-lines 1
         :kill-buffer t)))

(setq org-capture-templates-contexts
      '(("g" ((in-mode . "gnus-summary-mode")
              (in-mode . "gnus-article-mode")))))

;;; agenda custom commands

(setq org-agenda-custom-commands
        '(("o" "Custom"
           ((agenda ""
                    (;; (org-super-agenda-groups
                     ;;  '((:name "Progress today"
                     ;;     :log t
                     ;;     :order -1)
                     ;;    (:name "Morning Tasks"
                     ;;     :log t
                     ;;     :tag "morning"
                     ;;     :order 1)
                     ;;    (:name "Afternoon Taks"
                     ;;     :log t
                     ;;     :tag "afternoon"
                     ;;     :order 2)
                     ;;    (:name "Night Taks"
                     ;;     :log t
                     ;;     :tag "night"
                     ;;     :order 3)
                     ;;    (:name "Deadlines" :deadline t)
                     ;;    (:name "Health"
                     ;;     :tag "santé"
                     ;;     :log t
                     ;;     :order 5)
                     ;;    (:name "MATH"
                     ;;     :tag "math"
                     ;;     :order -1)
                     ;;    (:name "Très Important"
                     ;;     :priority "A"
                     ;;     :order -1)
                     ;;    (:name "Scheduled"
                     ;;     :and (:scheduled t :not (:priority "A"))
                     ;;     :order 5
                     ;;     :log t)))
                     (org-agenda-span 'day)
                     (org-agenda-sorting-strategy '(priority-down time-up))))
            ;; NOTE: A reading plan
            (tags "a_voir+matin|a_voir+après_midi|a_voir+nuit"
                  ((org-agenda-files '("~/org/notes.org"
                                       "~/org/math_article_links.org"))
                   (org-agenda-overriding-header "À Lire")
                   ;; (org-super-agenda-groups
                   ;;  '((:name "Le Matin" :tag "matin")
                   ;;    (:name "L'après-midi" :tag "après_midi")
                   ;;    (:name "La Nuit" :tag "nuit")))
                   ))
            (todo "TO-THINK"
                  (;; (org-super-agenda-groups
                   ;;  '((:name "À Voir" :tag "a_voir")
                   ;;    (:name "Mathématiques" :tag "math")
                   ;;    (:name "TeX" :tag "tex")
                   ;;    (:name "Question" :tag "question")))
                   (org-agenda-overriding-header "TO-THINK"))))
           ((org-agenda-block-separator nil)))
          ;; ("n" "test"
          ;;  ((durand-agenda-command-test)))
          ;; ("m" "sections with time"
          ;;  ((durand-agenda-command-sections-time)))
          ))

;;; elisp in link confirmation

(setq org-link-elisp-confirm-function 'y-or-n-p)

;;; agenda window setup

(setq org-agenda-window-setup 'other-tab)
;; The following will be ignored since the above is 'other-tab.
(setq org-agenda-restore-windows-after-quit t)

;;; novel addresses
;;;###autoload
(defvar durand-novel-addresses-regexp '("uukanshu"
                                        "ptwxz"
                                        "piaotian"
                                        "101novel"
                                        "booktxt")
  "Regexp for matching a novel website.")

;;;###autoload
(defun org-determine-link-file ()
  "Go to the file to capture to based upon the URL"
  (let* ((link (plist-get org-store-link-plist :link))
         (file-name
          (cond
           ((string-match-p "https?://www.youtube.com" link)
            "youtube_links.org")
           ((or
             (string-match-p "https?://math.stackexchange.com" link)
             (string-match-p "https?://mathoverflow.net/" link))
            "math_article_links.org")
           ((let ((temp durand-novel-addresses-regexp)
                  result)
              (while (and (consp temp) (not result))
                (cond
                 ((string-match-p (car temp) link)
                  (setq result t)))
                (setq temp (cdr temp)))
              result)
            "notes.org")
           ((string-match-p "https?://stacks.math.columbia.edu/" link)
            "math_article_links.org")
           (t
            "notes.org"))))
    (find-file (expand-file-name file-name org-directory))
    (goto-char (point-max))))

;;; filter title in the link
;;;###autoload
(defun org-filtered-link ()
  "Filter out some unnecessary parts in the link description"
  (save-match-data
    (let* ((link (plist-get org-store-link-plist :link))
           (title (plist-get org-store-link-plist :description))
           (filtered (cond ((string-match " - Mathematics Stack Exchange" title)
                            (replace-match "" nil nil title))
                           ((string-match " - YouTube" title)
                            (replace-match "" nil nil title))
                           ((string-match "\\(.*?\\)最新章节列表,\\1无弹窗_UU看书" title)
                            (replace-match "\\1" nil nil title))
                           (t
                            title))))
      (org-link-make-string link filtered))))

;;; Determine tag based upon the URL
;;;###autoload
(defun org-determine-tag ()
  "Determine tag based upon the URL"
  (let ((link (plist-get org-store-link-plist :link)))
    (cond
     ((string-match-p "https?://www.youtube.com" link)
      ":youtube:")
     ((or
       (string-match-p "https?://math.stackexchange.com" link)
       (string-match-p "https?://mathoverflow.net/" link))
      ":stack:web_link:")
     ((let ((temp durand-novel-addresses-regexp)
            result)
        (while (and (consp temp) (not result))
          (cond
           ((string-match-p (car temp) link)
            (setq result t)))
          (setq temp (cdr temp)))
        result)
      ":roman:")
     ((string-match-p "https?://stacks.math.columbia.edu/" link)
      ":web_link:stack:")
     (t
      ":web_link:"))))

;;;###autoload
(defun org-update-novels (&optional desc)
  "Update the html link to a novel, or to a web_link.
If DESC is non-`nil', then it is the description of the new link."
  (interactive)
;; HACK: Refocus the selected frame.
;; I was doing this in the applescript. But for some reason it is messed up. So
;; I let Emacs gain focus by itself now.
  (select-frame-set-input-focus (selected-frame))
  (let* ((tags (completing-read "tag: " '("roman-ARCHIVE"
                                          "web_link-ARCHIVE")
                                nil t))
         (roman-p (string-match "roman" tags))
         (files '("/Users/durand/org/notes.org" "/Users/durand/org/math_article_links.org"))
         (prompt (if roman-p
                     "Chois un roman à mettre à jour: "
                   "Chois un web lien à mettre à jour: "))
         cands)
    (setf cands
          (cl-loop for file in files
                   append (with-current-file file nil
                            (org-map-entries
                             (lambda ()
                               (let ((orig (durand-org-link-info t)))
                                 (list (car orig) (cdr orig) file)))
                             tags))))
    (unless roman-p (setf cands (nreverse cands)))
    (let* ((choix (completing-read prompt cands nil t))
           (item (cl-assoc choix cands :test #'string=))
           (lien (read-string "Le lien: " (current-kill 0 t))))
      (with-current-file (caddr item) nil
        (goto-char (cadr item))
        (org-update-link lien nil nil desc)))))

;;; filter out the title

;;;###autoload
(defun org-filter-title ()
  "Filter out some unnecessary parts of the link title"
  (let ((title (plist-get org-store-link-plist :description)))
    (cond
     ((string-match " - Mathematics Stack Exchange" title)
      (replace-match "" nil nil title))
     ((string-match " - YouTube" title)
      (replace-match "" nil nil title))
     ((string-match "\\(.*?\\)最新章节列表,\\1无弹窗_UU看书" title)
      (replace-match "\\1" nil nil title))
     (t
      title))))

;;; publishing settings

;;;;; Custom preamble for the sidebar

(defvar durand-org-publish-sidebar nil
  "The sidebar that provides the navigation of the website.")

(setq durand-org-publish-sidebar
      "<div class=\"sidebar\">\n\
<a href=\"math-sitemap.html\"> Math </a>\n\
<a href=\"code-sitemap.html\"> Code </a>\n\
<a  href=\"life-sitemap.html\"> Life </a>\n\
<a href=\"index.html\"> Home </a></div>")

;; REVIEW: This might be unnecessary.
(defun durand-org-publish-insert-sidebar (_arg)
  "Return the sidebar."
  durand-org-publish-sidebar)

;;;;;; Add a custom head

(defvar durand-org-publish-css-file nil
  "A custom css-file for publishing.")

(setq durand-org-publish-css-file
      "<link rel=\"stylesheet\" type=\"text/css\" \
href=\"website.css\"/>")

(defvar durand-org-publish-favicon nil
  "A custom favicon for publishing.")

(setq durand-org-publish-favicon
      "<link rel='shortcut icon' \
href='https://jsdurand.xyz/favicon.ico'/>")

;;;;; Projects settings

(setq org-publish-project-alist
      (list
       (list
        "website"
        :components (list "math" "code" "life" "main"))
       (list
        "code"
        :base-directory (directory-file-name
                         (expand-file-name
                          "code" (expand-file-name
                                  "blog" org-directory)))
        :base-extension "org"
        :publishing-directory (directory-file-name
                               (expand-file-name
                                "public" org-directory))
        :publishing-function #'org-html-publish-to-html
        :section-numbers nil
        :with-toc nil
        :with-email t
        :with-creator t
        :auto-sitemap t
        :recursive t
        :sitemap-function #'durand-org-publish-sitemap
        :sitemap-format-entry #'durand-org-publish-sitemap-format
        :sitemap-date-format "Published: %F %a %R"
        :sitemap-filename "code-sitemap.org"
        :sitemap-title "About coding"
        :sitemap-sort-files 'anti-chronologically
        :html-head (concat durand-org-publish-css-file
                           "\n"
                           durand-org-publish-favicon)
        :html-link-home ""
        :html-link-up ""
        :html-home/up-format ""
        :html-preamble #'durand-org-publish-insert-sidebar)
       (list
        "math"
        :base-directory (directory-file-name
                         (expand-file-name
                          "math" (expand-file-name
                                  "blog" org-directory)))
        :base-extension "org"
        :publishing-directory (directory-file-name
                               (expand-file-name
                                "public" org-directory))
        :publishing-function #'org-html-publish-to-html
        :section-numbers nil
        :with-toc nil
        :with-email t
        :with-creator t
        :auto-sitemap t
        :recursive t
        :sitemap-function #'durand-org-publish-sitemap
        :sitemap-format-entry #'durand-org-publish-sitemap-format
        :sitemap-date-format "Published: %F %a %R"
        :sitemap-filename "math-sitemap.org"
        :sitemap-title "Mathematics"
        :sitemap-sort-files 'anti-chronologically
        :html-head (concat durand-org-publish-css-file
                           "\n"
                           durand-org-publish-favicon)
        :html-link-home ""
        :html-link-up ""
        :html-home/up-format ""
        :html-preamble #'durand-org-publish-insert-sidebar)
       (list
        "life"
        :base-directory (directory-file-name
                         (expand-file-name
                          "life" (expand-file-name
                                  "blog" org-directory)))
        :base-extension "org"
        :publishing-directory (directory-file-name
                               (expand-file-name
                                "public" org-directory))
        :publishing-function #'org-html-publish-to-html
        :section-numbers nil
        :with-toc nil
        :with-email t
        :with-creator t
        :auto-sitemap t
        :recursive t
        :sitemap-function #'durand-org-publish-sitemap
        :sitemap-format-entry #'durand-org-publish-sitemap-format
        :sitemap-date-format "Published: %F %a %R"
        :sitemap-filename "life-sitemap.org"
        :sitemap-title "My life"
        :sitemap-sort-files 'anti-chronologically
        :html-head (concat durand-org-publish-css-file
                           "\n"
                           durand-org-publish-favicon)
        :html-link-home ""
        :html-link-up ""
        :html-home/up-format ""
        :html-preamble #'durand-org-publish-insert-sidebar)
       (list
        "main"
        :base-directory (directory-file-name
                         (expand-file-name
                          "blog" org-directory))
        :base-extension "org"
        :publishing-directory (directory-file-name
                               (expand-file-name
                                "public" org-directory))
        :publishing-function #'org-html-publish-to-html
        :section-numbers nil
        :with-toc nil
        :with-email t
        :with-creator t
        :auto-sitemap nil
        :recursive nil
        :html-head (concat durand-org-publish-css-file
                           "\n"
                           durand-org-publish-favicon)
        :html-link-home ""
        :html-link-up ""
        :html-home/up-format ""
        :html-preamble #'durand-org-publish-insert-sidebar)))

;;;;; Sitemap function

;;;;;; A hack to insert some string in the sitemap file.

(defvar durand-sitemap-custom-string-alist nil
  "An association list that relates the title of the sitemap and the string to insert.")

(setq durand-sitemap-custom-string-alist
      (list
       (cons "About coding"
             "This is my coding blog.  It contains my coding experiments, or \
one might think of them as development diaries.")
       (cons "My life"
             "This is my casual blog.  It contains articles about my plain \
life.")
       (cons "Mathematics"
             "My Mathematics-related articles are put here.")))

;;;;;; Custom sitemap function

(defun durand-org-publish-sitemap (title rep)
  "Return the sitemap as a string.
TITLE is the title of the sitemap.

REP is a representation of the files and directories in the
project.  Use such functions as `org-list-to-org' or
`org-list-to-subtree' to transform it."
  (format "#+TITLE: %s\n#+AUTHOR: JSDurand\n%s#+DATE: <%s>\n\n%s\n\n\
#+ATTR_HTML: :border nil :rules nil :frame nil\n\
%s"
          title
          "#+HTML_LINK_UP: index.html"
          (format-time-string "%F %a %R")
          (cdr (assoc title durand-sitemap-custom-string-alist #'string=))
          ;; generate a table
          (org-list-to-generic
           rep
           '(:ustart "|---|"
             :uend "|---|"
             :isep "|---|"))))

;;;;;; Custom sitemap format

;; NOTE: I use a table to style the entries.

;; NOTE: This stores the date in an ugly long format.  But worry not:
;; it will be replaced by a clean form in the post-processing phase.
;; The long form is inserted here so that we can sort the entries
;; precisely.

(defun durand-org-publish-sitemap-format (entry _style project)
  "Format the entry for the sitemap as a table with a date.
ENTRY is the entry file name to format.

STYLE is either 'list or 'tree, which is ignored by us.

PROJECT is the current project."
  (format "| %s | [[file:%s][%s]] |"
          (format-time-string
           "%FT%T%z"
           (org-publish-find-date entry project)
           (current-time-zone))
          entry
          (org-publish-find-title entry project)))

;;;;; Post-processing

;; I post-process the sitemaps in order to generate a section
;; of "latest updates" on the index page.

;;;;;; fix: plist-get not working

;; For some reason the built-in `plist-get' does not work...
(defun durand-org-publish-plist-get (prop plist)
  (let (res)
    (while (consp plist)
      (cond
       ((eq (car plist) prop)
        (setq res (cadr plist))
        (setq plist nil))
       ((setq plist (cddr plist)))))
    res))

;;;;;; Helper for converting the format from `parse-time-string'

(defun durand-org-publish-convert-time (spec)
  "Convert SPEC to a valid time value.
SPEC should be the result of `parse-time-string'.

It is assumed that the year, the month, and the day components
are present."
  (let ((sec (car spec))
        (minute (cadr spec))
        (hour (caddr spec)))
    (encode-time
     (append
      (list
       (or sec 0)
       (or minute 0)
       (or hour 0))
      (cdddr spec)))))

;;;;;; Post process to generate the index page

(defvar durand-org-index-entries-max-num 10
  "The maximal number of entries to show on the index page.")

(autoload #'durand-take (locate-user-emacs-file "common.el"))

(defun durand-org-post-process (project)
  "Generate a proper index page and Atom feeds.
Also shorten the date strings in the sitemap files, and store the
completion information in an attribute."
  (let* ((project-plist (cdr (assoc project org-publish-project-alist #'string=)))
         (components (durand-org-publish-plist-get
                      :components project-plist))
         (sitemap-file (durand-org-publish-plist-get
                        :sitemap-filename project-plist))
         ;; I cheat here
         (publishing-dir (expand-file-name "~/org/public/"))
         (publish-sitemap-file (cond
                                (sitemap-file
                                 (replace-regexp-in-string
                                  "org$" "html"
                                  (expand-file-name sitemap-file publishing-dir)))))
         (index-file (expand-file-name "index.html" publishing-dir))
         contents)
    (cond
     ;; depth-first recursion
     (components
      (setq contents (apply #'append
                      (delete nil (mapcar #'durand-org-post-process components))))
      ;; We only want some items
      (setq contents
            (durand-take
             durand-org-index-entries-max-num
             (sort contents
                   (lambda (x y)
                     (time-less-p
                      (car y) (car x))))))
      ;; If contents is non-nil, we need to process the info in the
      ;; index page.
      (cond
       (contents
        (with-temp-buffer
          (insert-file-contents index-file)
          (goto-char (point-min))
          (search-forward "Latest updates")
          (goto-char (line-end-position))
          (while (search-forward "<table" nil t)
            ;; delete existing tables
            (delete-region
             (match-beginning 0)
             (progn (search-forward "</table>")
                    (match-end 0))))
          (insert "\n")
          (insert "<table cellspacing=\"0\" cellpadding=\"6\">


<colgroup>
<col  class=\"org-right\" />

<col  class=\"org-left\" />
</colgroup>\n\n<tbody>\n")
          (mapc
           (lambda (entry)
             (insert "<tr>")
             (insert "<td class=\"org-right\">")
             (insert (format-time-string "%F" (car entry)))
             (insert "</td>\n")
             (insert "<td class=\"org-left\">")
             (insert (cdr entry))
             (insert "</td>\n</tr>\n"))
           contents)
          (insert "</tbody>\n</table>")
          (setq contents (buffer-substring-no-properties
                          (point-min) (point-max))))
        (write-region contents nil index-file))))
     ((and publish-sitemap-file
           (stringp publish-sitemap-file)
           (file-exists-p publish-sitemap-file))
      (with-temp-buffer
        (insert-file-contents publish-sitemap-file)
        (goto-char (point-min))
        (search-forward "</colgroup>" nil t)
        (let ((regexp (rx-to-string
                       '(seq
                         "<t" (any "hd")
                         " "
                         (one-or-more (not (or ">")))
                         ">")
                       t))
              pos pos-2 temp temp-time)
          (while (re-search-forward regexp nil t)
            (setq pos (point))
            ;; replace the first org-left by org-right
            (save-excursion
              (goto-char (match-beginning 0))
              (setq pos-2 (match-end 0))
              (save-match-data
                (cond
                 ((re-search-forward "left" pos-2 t)
                  (replace-match "right")
                  ;; fix pos
                  (setq pos (1+ pos))))))
            (search-forward "</t")
            (setq pos-2 (match-beginning 0))
            (forward-line 1)
            (cond
             ((re-search-forward 
               (rx-to-string
                '(seq
                  (= 4 (any digit)) "-"
                  (= 2 (any digit)) "-"
                  (= 2 (any digit)) "T")
                t)
               (line-end-position) t)
              (setq temp-time
                    (durand-org-publish-convert-time
                     (parse-time-string
                      (buffer-substring-no-properties
                       (match-beginning 0)
                       (line-end-position))))))
             (t
              (setq
               temp-time
               (durand-org-publish-convert-time
                (parse-time-string
                 (buffer-substring-no-properties
                  pos pos-2))))
              (save-excursion
                (goto-char pos)
                (delete-region pos pos-2)
                (insert (format-time-string "%F" temp-time)))
              (insert
               "<!--"
               (format-time-string
                "%FT%T%z\n" temp-time (current-time-zone))
               "-->\n")
              (write-region nil nil publish-sitemap-file)))
            (setq
             temp
             (cons
              (cons temp-time
                    (progn
                      (re-search-forward regexp)
                      (setq pos (point))
                      (search-forward "</t")
                      (buffer-substring-no-properties
                       pos (match-beginning 0))))
              temp)))
          (reverse temp)))))))

;;;;; Atom feed

;; The Atom feeds are generated in this function.

(defun durand-org-generate-atom-feed (project)
  "Generate an Atom feed for PROJECT."
  ;; TODO 
  )