summary refs log tree commit diff
path: root/emacs.d/custom/fcuny-org.el
blob: 6841e17dca60e2890eb1ff06daee179a3e4ee03d (plain) (blame)
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
(require 'fcuny-vars)

(use-package dash
  :ensure t)

(use-package org
  :ensure t
  :after (dash)
  :hook ((org-mode              . (lambda () (setq fill-column 120)))
         (org-mode              . turn-on-auto-fill)
         (org-mode              . org-indent-mode)
         (org-capture-mode-hook . delete-other-windows)
         (org-mode              . org-hide-block-all))
  :bind (("C-c c" . org-capture)
         ("C-c a" . org-agenda)
         ("C-c o" . hydra-org-menu/body))
  :config
  (progn ()
    (org-babel-do-load-languages
     'org-babel-load-languages
     '((shell . t)
       (python . t)
       (emacs-lisp . t))))

  (custom-set-faces
   '(org-done ((t (:foreground "DarkGreen"
                               :weight normal
                               :strike-through t))))
   '(org-headline-done
     ((((class color) (min-colors 16) (background dark))
       (:foreground "LightSalmon" :strike-through t)))))

  (defhydra hydra-org-menu (:hint nil)
    "
^go to              ^    ^action         ^
^-------------------^    ^---------------^
_g i_: go to inbox       _s_: save files
_g p_: go to personal
_g w_: go to work
_g n_: go to notes
"
    ("g i" (find-file-other-window org-default-inbox-file))
    ("g p" (find-file-other-window org-default-tasks-file))
    ("g w" (find-file-other-window org-default-work-tasks-file))
    ("g n" (find-file-other-window org-default-work-notes-file))
    ("s" (org-save-all-org-buffers)))

  :custom
  (org-directory (expand-file-name "~/Documents/notebooks"))
  (org-default-tasks-file (concat org-directory "/tasks.org"))
  (org-default-journal-file (concat org-directory "/journal.org"))
  (org-default-notes-file (concat org-directory "/notes.org"))
  (org-default-work-tasks-file (concat org-directory "/work-tasks.org"))
  (org-default-work-journal-file (concat org-directory "/work-journal.org"))
  (org-default-work-notes-file (concat org-directory "/work-notes.org"))
  (org-default-inbox-file (concat org-directory "/inbox.org"))

  ;; cosmetic
  (org-ellipsis "…")
  (org-pretty-entities t)
  (org-startup-indented nil)
  (org-blank-before-new-entry nil)

  ;;
  (org-log-done 'time)

  ;; look
  (org-agenda-window-setup 'current-window)

  ;; priorities
  (org-priority-start-cycle-with-default nil) ;; Start one over/under default value.
  (org-lowest-priority ?D)
  (org-default-priority ?D) ;; Ensures unset tasks have low priority.

  ;; how to manage tasks
  (org-todo-keywords '((sequence "TODO(t)" "STARTED(s)" "DELEGATED(D)" "|" "DONE(d)")
                       (sequence "SOMEDAY" "|" "CANCELED(c)")))

  ;; agenda related
  (calendar-week-start-day 1)      ;; org-mode uses calendar for the date picker, and I want this to start on Monday
  (org-agenda-start-on-weekday 1)  ;; this is specific to org-agenda
  (org-agenda-files `(,org-default-tasks-file
                      ,org-default-work-tasks-file))

  (org-agenda-custom-commands
   (quote
    (("h" "Hotlist (what's important and due soon)"
      ((tags-todo "DEADLINE<\"<+0d>\""
                  ((org-agenda-overriding-header "OVERDUE")))
       (tags-todo "DEADLINE>=\"<+0d>\"+DEADLINE<=\"<+1w>\""
                  ((org-agenda-overriding-header "DUE IN NEXT 7 DAYS")))
       (tags-todo "DEADLINE=\"\"+PRIORITY={A}|DEADLINE>\"<+1w>\"+PRIORITY={A}"
                  ((org-agenda-overriding-header "HIGH PRIORITY"))))
      ((org-agenda-todo-ignore-scheduled 'future)
       (org-agenda-sorting-strategy '(deadline-up))))

     ("w" . "Work...")

     ("wf" "Work Today"
      ((tags-todo "DEADLINE=\"<+0d>\""
                  ((org-agenda-overriding-header "Due today")
                   (org-agenda-skip-function
                    '(org-agenda-skip-entry-if 'notdeadline))
                   (org-agenda-sorting-strategy '(priority-down))))
       (tags-todo "DEADLINE<\"<+0d>\""
                  ((org-agenda-overriding-header "Overdue")
                   (org-agenda-skip-function
                    '(org-agenda-skip-entry-if 'notdeadline))
                   (org-agenda-sorting-strategy '(priority-down))))
       (tags-todo "TODO={STARTED}"
                  ((org-agenda-overriding-header "Started")
                   (org-agenda-sorting-strategy '(priority-down))))
       (agenda ""
               ((org-agenda-entry-types '(:scheduled))
                (org-agenda-overriding-header "Scheduled")
                (org-agenda-skip-function
                 '(org-agenda-skip-entry-if 'todo 'done))
                (org-agenda-sorting-strategy
                 '(priority-down time-down))
                (org-agenda-span 'day)
                (org-agenda-start-on-weekday nil)
                (org-agenda-time-grid nil)))

       (tags "TODO={MEETING}+CREATED>\"<-7d>\""
             ((org-agenda-overriding-header "Recent meetings (last 7 days)")))
       (tags "TODO={NOTE}+CREATED>\"<-7d>\""
             ((org-agenda-overriding-header "Recent notes (last 7 days)"))))
      ((org-agenda-files `(,org-default-work-tasks-file))
       (org-agenda-sorting-strategy '(deadline-up))))

     ("wp" "For 1:1"
      ((tags "people&TODO={TODO\\|STARTED}"
             ((org-agenda-overriding-header "Tasks for 1:1")))
       ((org-agenda-files `(,org-default-work-tasks-file)))))

     ("wt" "Triage unscheduled tasks"
      ((tags "work&TODO<>\"\"&TODO<>{DONE\\|CANCELED\\|NOTE\\|MEETING\\|PROJECT}"
             ((org-agenda-overriding-header "Unscheduled tasks: ")
              (org-agenda-skip-function
               (quote
                (org-agenda-skip-entry-if
                 (quote scheduled)
                 (quote deadline)
                 (quote timestamp)
                 (quote regexp)
                 "\\* \\(SOMEDAY\\)")))
              (org-agenda-sorting-strategy
               (quote
                (category-up)))
              (org-agenda-prefix-format "%-11c%5(fcuny/org-todo-age) "))))
      ((org-agenda-files `(,org-default-tasks-file
                           ,org-default-work-tasks-file))))

     ("wP" "Work project"
      ((tags "TODO={PROJECT}"
             ((org-agenda-overriding-header "All work projects"))))
      ((org-agenda-files `(,org-default-work-tasks-file))))

     ("ws" "Someday tasks"
      ((tags-todo "TODO={SOMEDAY}"
                  ((org-agenda-overriding-header "NO DUE DATE / SOMEDAY")
                   (org-agenda-skip-function
                    '(org-agenda-skip-entry-if 'deadline)))))
      ((org-agenda-files `(,org-default-work-tasks-file))))

     ("p" . "Personal ...")
     ("pt" "Personal Today"
      ((tags-todo "DEADLINE=\"<+0d>\""
                  ((org-agenda-overriding-header "Due today")
                   (org-agenda-skip-function
                    '(org-agenda-skip-entry-if 'notdeadline))
                   (org-agenda-sorting-strategy '(priority-down))))
       (tags-todo "DEADLINE<\"<+0d>\""
                  ((org-agenda-overriding-header "Overdue")
                   (org-agenda-skip-function
                    '(org-agenda-skip-entry-if 'notdeadline))
                   (org-agenda-sorting-strategy '(priority-down))))
       (tags-todo "TODO={STARTED}"
                  ((org-agenda-overriding-header "Started")
                   (org-agenda-sorting-strategy '(priority-down))))
       (tags "TODO={NOTE}+CREATED>\"<-7d>\""
             ((org-agenda-overriding-header "Recent notes (last 7 days)")))
       (agenda ""
               ((org-agenda-entry-types '(:scheduled))
                (org-agenda-overriding-header "Scheduled")
                (org-agenda-skip-function
                 '(org-agenda-skip-entry-if 'todo 'done))
                (org-agenda-sorting-strategy
                 '(priority-down time-down))
                (org-agenda-span 'day)
                (org-agenda-start-on-weekday nil)
                (org-agenda-time-grid nil))))
      ((org-agenda-files `(,org-default-tasks-file
                           ,org-default-notes-file))
       (org-agenda-sorting-strategy '(deadline-up)))))))

  ;; org babel related
  ;; prevent the conversion of spaces into tabs (necessary for Python code exports)
  (org-src-fontify-natively t)
  (org-fontify-done-headline t)
  (org-src-preserve-indentation t)
  (org-edit-src-content-indentation t)

  ;; behavior
  ;; I want to follow links on RET
  (org-return-follows-link t)
  (org-enforce-todo-dependencies t)
  (org-export-with-toc nil)
  (org-export-with-section-numbers nil)

  ;; A few abbreviations I use regularly
  (org-link-abbrev-alist
   '(("src"  . "~/workspace/%s")
     ("jira" . "https://jira.twitter.biz/browse/%s")
     ("ph"   . "https://phabricator.twitter.biz/%s")
     ("go"   . "http://go/%s")))

  ;; entries
  (org-blank-before-new-entry (quote ((heading . nil)
				      (plain-list-item . nil))))

  ;; see https://github.com/abo-abo/swiper/issues/986
  (org-goto-interface 'outline-path-completion)

  ;; refile and capture
  (org-refile-use-outline-path 'file)
  (org-outline-path-complete-in-steps nil)
  (org-refile-allow-creating-parent-nodes 'confirm)
  (org-refile-targets (quote ((org-default-tasks-file      :level . 0)
                              (org-default-work-tasks-file :level . 0)
                              (org-default-notes-file      :level . 0)
                              (org-default-work-notes-file :level . 0))))

  (org-reverse-note-order t)

  (org-capture-templates
   `(("t" "Add task" entry
      (file ,org-default-inbox-file)
      "* TODO %?\n:PROPERTIES:\n:ID: %(shell-command-to-string \"uuidgen\"):CREATED: %U\n:END:\n")

     ("n" "Note" entry
      (file ,org-default-inbox-file)
      "* %?\n:PROPERTIES:\n:ID: %(shell-command-to-string \"uuidgen\"):CREATED: %U\n:END:\n")

     ("j" "Journal" entry
      (file+olp+datetree ,org-default-journal-file)
      "* %?\n:PROPERTIES:\n:ID: %(shell-command-to-string \"uuidgen\"):CREATED: %U\n:END:\n" :tree-type month)

     ("m" "Meeting notes" entry
      (file ,org-default-inbox-file)
      "* %? %^g:meeting:\n:PROPERTIES:\n:ID: %(shell-command-to-string \"uuidgen\"):CREATED: %U\n:END:\n")

     ("i" "Interview notes" entry
      (file ,org-default-inbox-file)
      (file ,(concat fcuny/path-emacs-etc "/interview.org")))

     ("J" "Work Journal" entry
      (file+olp+datetree ,org-default-work-journal-file)
      "* %?\n:PROPERTIES:\n:ID: %(shell-command-to-string \"uuidgen\"):CREATED: %U\n:END:\n" :tree-type month))))

(use-package htmlize
  :ensure t)

;; can't use it for now, my version of org is too old
;(use-package orgit
;  :ensure t)

(defun fcuny/org-todo-age-time (&optional pos)
  (let ((stamp (org-entry-get (or pos (point)) "CREATED" t)))
    (when stamp
      (time-subtract (current-time)
                     (org-time-string-to-time
                      (org-entry-get (or pos (point)) "CREATED" t))))))

(defun fcuny/org-todo-age (&optional pos)
  (let ((days (time-to-number-of-days (fcuny/org-todo-age-time pos))))
    (cond
     ((< days 1)   "today")
     ((< days 7)   (format "%dd" days))
     ((< days 30)  (format "%.1fw" (/ days 7.0)))
     ((< days 358) (format "%.1fM" (/ days 30.0)))
     (t            (format "%.1fY" (/ days 365.0))))))

(defun fcuny/org-refile-to-datetree (&optional file)
"Refile current subtree to a datetree in FILE corresponding to it's timestamp.
The current time is used if the entry has no timestamp. If FILE
is nil, refile in the current file."
  (interactive)
  (require 'org-datetree)
  (let* ((file (cond
                (file (find-file-noselect file))
                (t (current-buffer))))
         (datetree-date (or (org-entry-get nil "CREATED" t)
                            (org-read-date t nil "now")))
         (date (org-date-to-gregorian datetree-date)))
    (org-refile nil nil (list nil (buffer-file-name file) nil
                              (with-current-buffer file
                                (save-excursion
                                  (org-datetree-find-date-create date)
                                  (point)))))))

(defun fcuny/org-refile-to-work-journal ()
"Refile current subtree to a datetree in work journal corresponding to it's timestamp.
The current time is used if the entry has no timestamp."
  (interactive)
  (fcuny/org-refile-to-datetree org-default-work-journal-file))

(defun fcuny/org-subtree-region ()
  "Return a list of the start and end of a subtree."
  (save-excursion
    (list (progn (org-back-to-heading) (point))
          (progn (org-end-of-subtree)  (point)))))

(defun fcuny/org-refile-directly (file-dest)
  "Move the current subtree to the end of FILE-DEST.
If SHOW-AFTER is non-nil, show the destination window,
otherwise, this destination buffer is not shown."
  (interactive "fDestination: ")

  (defun dump-it (file contents)
    (find-file-other-window file-dest)
    (goto-char (point-max))
    (insert "\n" contents))

  (save-excursion
    (let* ((region (fcuny/org-subtree-region))
           (contents (buffer-substring (first region) (second region))))
      (apply 'kill-region region)
      (save-window-excursion (dump-it file-dest contents)))))

(provide 'fcuny-org)