5 ;; The following lines are always needed. Choose your own keys.
6 (add-to-list 'auto-mode-alist '("\\.\\(org\\|org_archive\\|txt\\)$" . org-mode))
8 (global-set-key "\C-cl" 'org-store-link)
9 (global-set-key "\C-ca" 'org-agenda)
10 (global-set-key "\C-cb" 'org-iswitchb)
11 (setq-default org-log-done 'time)
12 (setq-default org-agenda-ndays 5)
14 ;; agenda configuration
15 ;; Do not dim blocked tasks
16 (setq org-agenda-dim-blocked-tasks nil)
18 ;; Compact the block agenda view
19 (setq org-agenda-compact-blocks t)
21 ;; Custom agenda command definitions
22 (setq org-agenda-custom-commands
23 (quote (("N" "Notes" tags "NOTE"
24 ((org-agenda-overriding-header "Notes")
25 (org-tags-match-list-sublevels t)))
26 ("h" "Habits" tags-todo "STYLE=\"habit\""
27 ((org-agenda-overriding-header "Habits")
28 (org-agenda-sorting-strategy
29 '(todo-state-down effort-up category-keep))))
33 ((org-agenda-overriding-header "Tasks to Refile")
34 (org-tags-match-list-sublevels nil)))
35 (tags-todo "-CANCELLED/!"
36 ((org-agenda-overriding-header "Stuck Projects")
37 (org-agenda-skip-function 'bh/skip-non-stuck-projects)
38 (org-agenda-sorting-strategy
40 (tags-todo "-HOLD-CANCELLED/!"
41 ((org-agenda-overriding-header "Projects")
42 (org-agenda-skip-function 'bh/skip-non-projects)
43 (org-tags-match-list-sublevels 'indented)
44 (org-agenda-sorting-strategy
46 (tags-todo "-CANCELLED/!NEXT"
47 ((org-agenda-overriding-header (concat "Project Next Tasks"
48 (if bh/hide-scheduled-and-waiting-next-tasks
50 " (including WAITING and SCHEDULED tasks)")))
51 (org-agenda-skip-function 'bh/skip-projects-and-habits-and-single-tasks)
52 (org-tags-match-list-sublevels t)
53 (org-agenda-todo-ignore-scheduled bh/hide-scheduled-and-waiting-next-tasks)
54 (org-agenda-todo-ignore-deadlines bh/hide-scheduled-and-waiting-next-tasks)
55 (org-agenda-todo-ignore-with-date bh/hide-scheduled-and-waiting-next-tasks)
56 (org-agenda-sorting-strategy
57 '(todo-state-down effort-up category-keep))))
58 (tags-todo "-REFILE-CANCELLED-WAITING-HOLD/!"
59 ((org-agenda-overriding-header (concat "Project Subtasks"
60 (if bh/hide-scheduled-and-waiting-next-tasks
62 " (including WAITING and SCHEDULED tasks)")))
63 (org-agenda-skip-function 'bh/skip-non-project-tasks)
64 (org-agenda-todo-ignore-scheduled bh/hide-scheduled-and-waiting-next-tasks)
65 (org-agenda-todo-ignore-deadlines bh/hide-scheduled-and-waiting-next-tasks)
66 (org-agenda-todo-ignore-with-date bh/hide-scheduled-and-waiting-next-tasks)
67 (org-agenda-sorting-strategy
69 (tags-todo "-REFILE-CANCELLED-WAITING-HOLD/!"
70 ((org-agenda-overriding-header (concat "Standalone Tasks"
71 (if bh/hide-scheduled-and-waiting-next-tasks
73 " (including WAITING and SCHEDULED tasks)")))
74 (org-agenda-skip-function 'bh/skip-project-tasks)
75 (org-agenda-todo-ignore-scheduled bh/hide-scheduled-and-waiting-next-tasks)
76 (org-agenda-todo-ignore-deadlines bh/hide-scheduled-and-waiting-next-tasks)
77 (org-agenda-todo-ignore-with-date bh/hide-scheduled-and-waiting-next-tasks)
78 (org-agenda-sorting-strategy
80 (tags-todo "-CANCELLED+WAITING|HOLD/!"
81 ((org-agenda-overriding-header "Waiting and Postponed Tasks")
82 (org-agenda-skip-function 'bh/skip-stuck-projects)
83 (org-tags-match-list-sublevels nil)
84 (org-agenda-todo-ignore-scheduled t)
85 (org-agenda-todo-ignore-deadlines t)))
87 ((org-agenda-overriding-header "Tasks to Archive")
88 (org-agenda-skip-function 'bh/skip-non-archivable-tasks)
89 (org-tags-match-list-sublevels nil))))
92 ; org mode agenda files
93 (setq org-agenda-files
94 (quote ("~/projects/debbugs/debbugs.org"
95 "~/projects/notes/notes.org"
96 "~/projects/notes/refile.org"
97 "~/projects/notes/diary.org"
98 "~/projects/origins_of_life/ool.org"
99 "~/projects/sysadmin/sndservers/sndservers.org"
100 "~/projects/chaim/chaim.org"
101 "~/projects/chaim/papers/gwas_paper_2012/gwas_paper.org"
102 "~/projects/reviews/reviews.org"
103 "~/projects/fh/fh.org")))
105 (setq org-global-properties '(("Effort_ALL 0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
106 (setq org-columns-default-format "%40ITEM(Task) %6Effort{:} %CLOCKSUM %PRIORITY %TODO %13SCHEDULED %13DEADLINE %TAGS")
108 (setq org-default-notes-file "~/projects/notes/notes.org")
109 (setq org-capture-templates ;; mail-specific note template, identified by "m"
110 '(("m" "Mail" entry (file+headline "~/projects/notes/refile.org" "Mail")
111 "* %?\n\n Source: %u, %c\n %i")
112 ("t" "todo" entry (file "~/projects/notes/refile.org")
113 "* TODO %?\n%U\n%a\n" :clock-in t :clock-resume t)
114 ("r" "respond" entry (file "~/projects/notes/refile.org")
115 "* NEXT Respond to %:from on %:subject\nSCHEDULED: %t\n%U\n%a\n" :clock-in t :clock-resume t :immediate-finish t)
116 ("n" "note" entry (file "~/projects/notes/refile.org")
117 "* %? :NOTE:\n%U\n%a\n" :clock-in t :clock-resume t)
118 ("s" "schedule" entry (file "~/projects/notes/refile.org")
119 "* %? \n%^{scheduled:}t\n%U\n%a\n" :clock-in t :clock-resume t)
120 ("j" "Journal" entry (file+datetree "~/projects/notes/diary.org")
121 "* %?\n%U\n" :clock-in t :clock-resume t)
122 ("w" "org-protocol" entry (file "~/projects/notes/refile.org")
123 "* TODO Review %c\n%U\n" :immediate-finish t)
124 ("M" "Meeting" entry (file "~/projects/notes/refile.org")
125 "* MEETING with %? :MEETING:\n%U" :clock-in t :clock-resume t)
126 ("p" "Phone call" entry (file "~/projects/notes/refile.org")
127 "* PHONE %? :PHONE:\n%U" :clock-in t :clock-resume t)
128 ("J" "job" entry (file "~/projects/notes/refile.org")
129 "* TODO Apply for %a%? :job:\nSCHEDULED: %(format-time-string \"<%Y-%m-%d %a 17:00-17:30>\")\n%U\n" :clock-in t :clock-resume t)
130 ("h" "Habit" entry (file "~/projects/notes/refile.org")
131 "* NEXT %?\n%U\n%a\nSCHEDULED: %(format-time-string \"<%Y-%m-%d %a .+1d/3d>\")\n:PROPERTIES:\n:STYLE: habit\n:REPEAT_TO_STATE: NEXT\n:END:\n")
135 ;; Remove empty LOGBOOK drawers on clock out
136 (defun bh/remove-empty-drawer-on-clock-out ()
139 (beginning-of-line 0)
140 (org-remove-empty-drawer-at "LOGBOOK" (point))))
142 (defun my/org-add-id ()
145 (if (org-current-level)
153 ; org mode configuration from http://doc.norang.ca/org-mode.html
154 ;; Custom Key Bindings
155 (global-set-key (kbd "<f12>") 'org-agenda)
156 (global-set-key (kbd "<f5>") 'bh/org-todo)
157 (global-set-key (kbd "<S-f5>") 'bh/widen)
158 (global-set-key (kbd "<f7>") 'bh/set-truncate-lines)
159 (global-set-key (kbd "<f8>") 'org-cycle-agenda-files)
160 (global-set-key (kbd "<f9> <f9>") 'bh/show-org-agenda)
161 (global-set-key (kbd "<f9> b") 'bbdb)
162 (global-set-key (kbd "<f9> c") 'calendar)
163 (global-set-key (kbd "<f9> f") 'boxquote-insert-file)
164 (global-set-key (kbd "<f9> h") 'bh/hide-other)
165 (global-set-key (kbd "<f9> n") 'bh/toggle-next-task-display)
166 (global-set-key (kbd "<f9> w") 'widen)
168 ; change the outline mode prefix from C-c @ to C-c C-2
169 (setq outline-minor-mode-prefix "C-c C-2")
170 (add-hook 'outline-minor-mode-hook
171 (lambda () (local-set-key (kbd "C-c C-2")
172 outline-mode-prefix-map)))
174 (global-set-key (kbd "<f9> I") 'bh/punch-in)
175 (global-set-key (kbd "<f9> O") 'bh/punch-out)
177 (global-set-key (kbd "<f9> o") 'bh/make-org-scratch)
179 (global-set-key (kbd "<f9> r") 'boxquote-region)
180 (global-set-key (kbd "<f9> s") 'bh/switch-to-scratch)
182 (global-set-key (kbd "<f9> t") 'bh/insert-inactive-timestamp)
183 (global-set-key (kbd "<f9> T") 'bh/toggle-insert-inactive-timestamp)
185 (global-set-key (kbd "<f9> v") 'visible-mode)
186 (global-set-key (kbd "<f9> l") 'org-toggle-link-display)
187 (global-set-key (kbd "<f9> SPC") 'bh/clock-in-last-task)
188 (global-set-key (kbd "C-<f9>") 'previous-buffer)
189 (global-set-key (kbd "M-<f9>") 'org-toggle-inline-images)
190 (global-set-key (kbd "C-x n r") 'narrow-to-region)
191 (global-set-key (kbd "C-<f10>") 'next-buffer)
192 (global-set-key (kbd "<f11>") 'org-clock-goto)
193 (global-set-key (kbd "C-<f11>") 'org-clock-in)
194 (global-set-key (kbd "C-s-<f12>") 'bh/save-then-publish)
195 (global-set-key (kbd "C-c c") 'org-capture)
197 (defun bh/hide-other ()
200 (org-back-to-heading 'invisible-ok)
206 (defun bh/set-truncate-lines ()
207 "Toggle value of truncate-lines and refresh window display."
209 (setq truncate-lines (not truncate-lines))
210 ;; now refresh window display (an idiom from simple.el):
212 (set-window-start (selected-window)
213 (window-start (selected-window)))))
215 (defun bh/make-org-scratch ()
217 (find-file "/tmp/publish/scratch.org")
218 (gnus-make-directory "/tmp/publish"))
220 (defun bh/switch-to-scratch ()
222 (switch-to-buffer "*scratch*"))
224 (setq org-use-fast-todo-selection t)
225 (setq org-treat-S-cursor-todo-selection-as-state-change nil)
227 (setq org-todo-keywords
228 (quote ((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d)")
229 (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" "PHONE" "MEETING"))))
231 (setq org-todo-keyword-faces
232 (quote (("TODO" :foreground "red" :weight bold)
233 ("NEXT" :foreground "blue" :weight bold)
234 ("DONE" :foreground "forest green" :weight bold)
235 ("WAITING" :foreground "orange" :weight bold)
236 ("HOLD" :foreground "magenta" :weight bold)
237 ("CANCELLED" :foreground "forest green" :weight bold)
238 ("MEETING" :foreground "forest green" :weight bold)
239 ("PHONE" :foreground "forest green" :weight bold))))
241 (setq org-todo-state-tags-triggers
242 (quote (("CANCELLED" ("CANCELLED" . t))
243 ("WAITING" ("WAITING" . t))
244 ("HOLD" ("WAITING") ("HOLD" . t))
245 (done ("WAITING") ("HOLD"))
246 ("TODO" ("WAITING") ("CANCELLED") ("HOLD"))
247 ("NEXT" ("WAITING") ("CANCELLED") ("HOLD"))
248 ("DONE" ("WAITING") ("CANCELLED") ("HOLD")))))
252 (add-hook 'org-clock-out-hook 'bh/remove-empty-drawer-on-clock-out 'append)
253 ; add ids on creation of nodes
254 (add-hook 'org-capture-prepare-finalize-hook 'my/org-add-id)
255 ; create function to create headlines in file. This comes from
256 ; http://stackoverflow.com/questions/13340616/assign-ids-to-every-entry-in-org-mode
257 (defun my/org-add-ids-to-headlines-in-file ()
258 "Add ID properties to all headlines in the current file which
259 do not already have one."
261 (org-map-entries 'org-id-get-create))
262 ; if we wanted to do this to every buffer, do the following:
263 ; (add-hook 'org-mode-hook
265 ; (add-hook 'before-save-hook 'my/org-add-ids-to-headlines-in-file nil 'local)))
268 ; resolve clocks after 10 minutes of idle; use xprintidle
269 ; (setq org-clock-idle-time 10)
270 ; (setq org-clock-x11idle-program-name "xprintidle")
272 ; this is from http://doc.norang.ca/org-mode.html#Capture
273 ; use C-M-r for org mode capture
274 (global-set-key (kbd "C-M-r") 'org-capture)
276 ; Targets include this file and any file contributing to the agenda - up to 9 levels deep
277 (setq org-refile-targets (quote ((nil :maxlevel . 9)
278 (org-agenda-files :maxlevel . 9))))
280 ; Use full outline paths for refile targets - we file directly with IDO
281 (setq org-refile-use-outline-path t)
283 ; Targets complete directly with IDO
284 (setq org-outline-path-complete-in-steps nil)
286 ; Allow refile to create parent tasks with confirmation
287 (setq org-refile-allow-creating-parent-nodes (quote confirm))
289 ; ; Use IDO for both buffer and file completion and ido-everywhere to t
290 ; (setq org-completion-use-ido t)
291 ; (setq ido-everywhere t)
292 ; (setq ido-max-directory-size 100000)
293 ; (ido-mode (quote both))
294 ; ; Use the current window when visiting files and buffers with ido
295 ; (setq ido-default-file-method 'selected-window)
296 ; (setq ido-default-buffer-method 'selected-window)
297 ; ; Use the current window for indirect buffer display
298 ; (setq org-indirect-buffer-display 'current-window)
302 ; Exclude DONE state tasks from refile targets
303 (defun bh/verify-refile-target ()
304 "Exclude todo keywords with a done state from refile targets"
305 (not (member (nth 2 (org-heading-components)) org-done-keywords)))
307 (setq org-refile-target-verify-function 'bh/verify-refile-target)
309 ;; ensure that emacsclient will show just the note to be edited when invoked
310 ;; from Mutt, and that it will shut down emacsclient once finished;
311 ;; fallback to legacy behavior when not invoked via org-protocol.
312 (require 'org-protocol)
313 (add-hook 'org-capture-mode-hook 'delete-other-windows)
314 (setq my-org-protocol-flag nil)
315 (defadvice org-capture-finalize (after delete-frame-at-end activate)
316 "Delete frame at remember finalization"
317 (progn (if my-org-protocol-flag (delete-frame))
318 (setq my-org-protocol-flag nil)))
319 (defadvice org-capture-refile (around delete-frame-after-refile activate)
320 "Delete frame at remember refile"
321 (if my-org-protocol-flag
323 (setq my-org-protocol-flag nil)
328 (defadvice org-capture-kill (after delete-frame-at-end activate)
329 "Delete frame at remember abort"
330 (progn (if my-org-protocol-flag (delete-frame))
331 (setq my-org-protocol-flag nil)))
332 (defadvice org-protocol-capture (before set-org-protocol-flag activate)
333 (setq my-org-protocol-flag t))
335 (defadvice org-insert-todo-heading (after dla/create-id activate)
340 (add-to-list 'org-modules 'org-habit)
342 ; this comes from http://upsilon.cc/~zack/blog/posts/2010/02/integrating_Mutt_with_Org-mode/
343 (defun open-mail-in-mutt (message)
344 "Open a mail message in Mutt, using an external terminal.
346 Message can be specified either by a path pointing inside a
347 Maildir, or by Message-ID."
348 (interactive "MPath or Message-ID: ")
350 (format "faf xterm -e \"%s %s\""
351 (substitute-in-file-name "$HOME/bin/mutt_open") message)))
353 ;; add support for "mutt:ID" links
354 (org-add-link-type "mutt" 'open-mail-in-mutt)
356 (defun my-org-mode-setup ()
357 (load-library "reftex")
358 (and (buffer-file-name)
359 (file-exists-p (buffer-file-name))
362 (reftex-set-cite-format
363 '((?b . "[[bib:%l][%l-bib]]")
364 (?n . "[[notes:%l][%l-notes]]")
366 (?h . "*** %t\n:PROPERTIES:\n:Custom_ID: %l\n:END:\n[[papers:%l][%l xoj]] [[papers-pdf:%l][pdf]]")))
368 (define-key org-mode-map (kbd "C-c )") 'reftex-citation)
369 (define-key org-mode-map (kbd "C-c [") 'reftex-citation)
370 (define-key org-mode-map (kbd "C-c (") 'org-mode-reftex-search)
371 (define-key org-mode-map (kbd "C-c 0") 'reftex-view-crossref)
373 (add-hook 'org-mode-hook 'my-org-mode-setup)
375 (defun org-mode-reftex-search ()
377 (org-open-link-from-string (format "[[notes:%s]]" (first (reftex-citation t)))))
379 (defun open-research-paper (bibtexkey)
380 "Open a paper by bibtex key"
381 (interactive "bibtex key: ")
384 (substitute-in-file-name "$HOME/bin/bibtex_to_paper") bibtexkey)))
385 (org-add-link-type "papers" 'open-research-paper)
386 (defun open-research-paper-pdf (bibtexkey)
387 "Open a paper pdf by bibtex key"
388 (interactive "bibtex key: ")
390 (format "%s -p evince %s"
391 (substitute-in-file-name "$HOME/bin/bibtex_to_paper") bibtexkey)))
392 (org-add-link-type "papers-pdf" 'open-research-paper-pdf)
394 (add-to-list 'org-link-abbrev-alist
396 "~/projects/research/paper_notes.org::#%s"))
398 ; I pretty much always want hiearchical checkboxes
399 (setq org-hierachical-checkbox-statistics nil)
402 ;; http://www-public.it-sudparis.eu/~berger_o/weblog/2012/03/23/how-to-manage-and-export-bibliographic-notesrefs-in-org-mode/
403 (defun my-rtcite-export-handler (path desc format)
404 (message "my-rtcite-export-handler is called : path = %s, desc = %s, format = %s" path desc format)
405 (let* ((search (when (string-match "::#?\\(.+\\)\\'" path)
406 (match-string 1 path)))
407 (path (substring path 0 (match-beginning 0))))
408 (cond ((eq format 'latex)
410 (equal 0 (search "rtcite:" desc)))
411 (format "\\cite{%s}" search)
412 (format "\\cite[%s]{%s}" desc search))))))
414 (org-add-link-type "rtcite"
416 'my-rtcite-export-handler)
419 (setq-default org-mobile-directory "/rzlab.ucr.edu:/sites/dav.donarmstrong.com/root/org/")
420 (setq-default org-directory "/home/don/org-mode/")
421 (setq-default org-mobile-inbox-for-pull "/home/don/org-mode/from-mobile.org")
423 ;; org mode ical export
424 (setq org-icalendar-timezone "America/Los_Angeles")
425 (setq org-icalendar-use-scheduled '(todo-start event-if-todo))
426 ;; we already add the id manually
427 ;; (setq org-icalendar-store-UID t)
430 (org-babel-do-load-languages
431 'org-babel-load-languages
437 ;; org-babel-by-backend
438 (defmacro org-babel-by-backend (&rest body)
439 `(case (if (boundp 'backend)
440 (org-export-backend-name backend)
446 ;; ;; stolen from http://kieranhealy.org/esk/kjhealy.html
447 ;; (require 'org-latex)
448 ;; ;; Choose either listings or minted for exporting source code blocks.
449 ;; ;; Using minted (as here) requires pygments be installed. To use the
450 ;; ;; default listings package instead, use
451 ;; ;; (setq org-latex-listings t)
452 ;; ;; and change references to "minted" below to "listings"
453 ;; ; (setq org-latex-listings 'minted)
455 ;; ;; default settings for minted code blocks
456 ;; (setq org-latex-minted-options
457 ;; '(;("frame" "single")
458 ;; ("bgcolor" "bg") ; bg will need to be defined in the preamble of your document. It's defined in org-preamble-pdflatex.sty and org-preamble-xelatex.sty below.
459 ;; ("fontsize" "\\small")
461 ;; ;; turn off the default toc behavior; deal with it properly in headers to files.
462 ;; (defun org-latex-no-toc (depth)
464 ;; (format "%% Org-mode is exporting headings to %s levels.\n"
466 ;; (setq org-latex-format-toc-function 'org-latex-no-toc)
469 (add-to-list 'org-latex-classes
471 "\\documentclass[11pt,oneside,article]{memoir}\n"
472 ("\\section{%s}" . "\\section*{%s}")
473 ("\\subsection{%s}" . "\\subsection*{%s}")
474 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
475 ("\\paragraph{%s}" . "\\paragraph*{%s}")
476 ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
478 (setq org-beamer-outline-frame-options "")
480 (add-to-list 'org-latex-classes
482 "\\documentclass[ignorenonframetext]{beamer}
483 [NO-DEFAULT-PACKAGES]
486 ("\\section{%s}" . "\\section*{%s}")
487 ("\\subsection{%s}" . "\\subsection*{%s}")
488 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
489 ("\\paragraph{%s}" . "\\paragraph*{%s}")
490 ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
492 (add-to-list 'org-latex-classes
494 "\\documentclass[11pt,oneside]{memoir}\n"
495 ("\\chapter{%s}" . "\\chapter*{%s}")
496 ("\\section{%s}" . "\\section*{%s}")
497 ("\\subsection{%s}" . "\\subsection*{%s}")
498 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
500 (add-to-list 'org-latex-classes
502 "\\documentclass[11pt]{letter}
503 [NO-DEFAULT-PACKAGES]
506 ("\\section{%s}" . "\\section*{%s}")
507 ("\\subsection{%s}" . "\\subsection*{%s}")
508 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
509 ("\\paragraph{%s}" . "\\paragraph*{%s}")
510 ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
512 ;; Originally taken from Bruno Tavernier: http://thread.gmane.org/gmane.emacs.orgmode/31150/focus=31432
513 ;; but adapted to use latexmk 4.22 or higher.
514 (setq org-latex-pdf-process '("latexmk -pdflatex=xelatex -bibtex -use-make -pdf %f"))
516 ;; Default packages included in /every/ tex file, latex, pdflatex or xelatex
517 (setq org-latex-default-packages-alist
519 (setq org-latex-packages-alist
529 (defun org-create-formula--latex-header ()
530 "Return LaTeX header appropriate for previewing a LaTeX snippet."
531 (let ((info (org-combine-plists (org-export--get-global-options
532 (org-export-get-backend 'latex))
533 (org-export--get-inbuffer-options
534 (org-export-get-backend 'latex)))))
535 (org-latex-guess-babel-language
536 (org-latex-guess-inputenc
537 (org-splice-latex-header
538 org-format-latex-header
539 org-latex-default-packages-alist
541 (plist-get info :latex-header)))
545 ; support ignoring headers in org mode export to latex
546 ; from http://article.gmane.org/gmane.emacs.orgmode/67692
547 (defadvice org-latex-headline (around my-latex-skip-headlines
548 (headline contents info) activate)
549 (if (member "ignoreheading" (org-element-property :tags headline))
550 (setq ad-return-value contents)
553 ;; keep latex logfiles
555 (setq org-latex-remove-logfiles nil)
558 (defun bh/is-project-p ()
559 "Any task with a todo keyword subtask"
563 (subtree-end (save-excursion (org-end-of-subtree t)))
564 (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
567 (while (and (not has-subtask)
568 (< (point) subtree-end)
569 (re-search-forward "^\*+ " subtree-end t))
570 (when (member (org-get-todo-state) org-todo-keywords-1)
571 (setq has-subtask t))))
572 (and is-a-task has-subtask))))
574 (defun bh/is-project-subtree-p ()
575 "Any task with a todo keyword that is in a project subtree.
576 Callers of this function already widen the buffer view."
577 (let ((task (save-excursion (org-back-to-heading 'invisible-ok)
580 (bh/find-project-task)
581 (if (equal (point) task)
585 (defun bh/is-task-p ()
586 "Any task with a todo keyword and no subtask"
590 (subtree-end (save-excursion (org-end-of-subtree t)))
591 (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
594 (while (and (not has-subtask)
595 (< (point) subtree-end)
596 (re-search-forward "^\*+ " subtree-end t))
597 (when (member (org-get-todo-state) org-todo-keywords-1)
598 (setq has-subtask t))))
599 (and is-a-task (not has-subtask)))))
601 (defun bh/is-subproject-p ()
602 "Any task which is a subtask of another project"
603 (let ((is-subproject)
604 (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
606 (while (and (not is-subproject) (org-up-heading-safe))
607 (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
608 (setq is-subproject t))))
609 (and is-a-task is-subproject)))
611 (defun bh/list-sublevels-for-projects-indented ()
612 "Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
613 This is normally used by skipping functions where this variable is already local to the agenda."
614 (if (marker-buffer org-agenda-restrict-begin)
615 (setq org-tags-match-list-sublevels 'indented)
616 (setq org-tags-match-list-sublevels nil))
619 (defun bh/list-sublevels-for-projects ()
620 "Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
621 This is normally used by skipping functions where this variable is already local to the agenda."
622 (if (marker-buffer org-agenda-restrict-begin)
623 (setq org-tags-match-list-sublevels t)
624 (setq org-tags-match-list-sublevels nil))
627 (defvar bh/hide-scheduled-and-waiting-next-tasks t)
629 (defun bh/toggle-next-task-display ()
631 (setq bh/hide-scheduled-and-waiting-next-tasks (not bh/hide-scheduled-and-waiting-next-tasks))
632 (when (equal major-mode 'org-agenda-mode)
634 (message "%s WAITING and SCHEDULED NEXT Tasks" (if bh/hide-scheduled-and-waiting-next-tasks "Hide" "Show")))
636 (defun bh/skip-stuck-projects ()
637 "Skip trees that are not stuck projects"
640 (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
641 (if (bh/is-project-p)
642 (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
646 (while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
647 (unless (member "WAITING" (org-get-tags-at))
651 next-headline)) ; a stuck project, has subtasks but no next task
654 (defun bh/skip-non-stuck-projects ()
655 "Skip trees that are not stuck projects"
656 ;; (bh/list-sublevels-for-projects-indented)
659 (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
660 (if (bh/is-project-p)
661 (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
665 (while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
666 (unless (member "WAITING" (org-get-tags-at))
670 nil)) ; a stuck project, has subtasks but no next task
673 (defun bh/skip-non-projects ()
674 "Skip trees that are not projects"
675 ;; (bh/list-sublevels-for-projects-indented)
676 (if (save-excursion (bh/skip-non-stuck-projects))
679 (let ((subtree-end (save-excursion (org-end-of-subtree t))))
683 ((and (bh/is-project-subtree-p) (not (bh/is-task-p)))
687 (save-excursion (org-end-of-subtree t))))
689 (defun bh/skip-project-trees-and-habits ()
690 "Skip trees that are projects"
693 (let ((subtree-end (save-excursion (org-end-of-subtree t))))
702 (defun bh/skip-projects-and-habits-and-single-tasks ()
703 "Skip trees that are projects, tasks that are habits, single non-project tasks"
706 (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
710 ((and bh/hide-scheduled-and-waiting-next-tasks
711 (member "WAITING" (org-get-tags-at)))
715 ((and (bh/is-task-p) (not (bh/is-project-subtree-p)))
720 (defun bh/skip-project-tasks-maybe ()
721 "Show tasks related to the current restriction.
722 When restricted to a project, skip project and sub project tasks, habits, NEXT tasks, and loose tasks.
723 When not restricted, skip project and sub-project tasks, habits, and project related tasks."
726 (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
727 (next-headline (save-excursion (or (outline-next-heading) (point-max))))
728 (limit-to-project (marker-buffer org-agenda-restrict-begin)))
734 ((and (not limit-to-project)
735 (bh/is-project-subtree-p))
737 ((and limit-to-project
738 (bh/is-project-subtree-p)
739 (member (org-get-todo-state) (list "NEXT")))
744 (defun bh/skip-project-tasks ()
745 "Show non-project tasks.
746 Skip project and sub-project tasks, habits, and project related tasks."
749 (let* ((subtree-end (save-excursion (org-end-of-subtree t))))
755 ((bh/is-project-subtree-p)
760 (defun bh/skip-non-project-tasks ()
762 Skip project and sub-project tasks, habits, and loose non-project tasks."
765 (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
766 (next-headline (save-excursion (or (outline-next-heading) (point-max)))))
772 ((and (bh/is-project-subtree-p)
773 (member (org-get-todo-state) (list "NEXT")))
775 ((not (bh/is-project-subtree-p))
780 (defun bh/skip-projects-and-habits ()
781 "Skip trees that are projects and tasks that are habits"
784 (let ((subtree-end (save-excursion (org-end-of-subtree t))))
793 (defun bh/skip-non-subprojects ()
794 "Skip trees that are not projects"
795 (let ((next-headline (save-excursion (outline-next-heading))))
796 (if (bh/is-subproject-p)
800 ;; Resume clocking task when emacs is restarted
801 (org-clock-persistence-insinuate)
803 ;; Show lot of clocking history so it's easy to pick items off the C-F11 list
804 (setq org-clock-history-length 23)
805 ;; Resume clocking task on clock-in if the clock is open
806 (setq org-clock-in-resume t)
807 ;; Change tasks to NEXT when clocking in
808 (setq org-clock-in-switch-to-state 'bh/clock-in-to-next)
809 ;; Separate drawers for clocking and logs
810 (setq org-drawers (quote ("PROPERTIES" "LOGBOOK")))
811 ;; Save clock data and state changes and notes in the LOGBOOK drawer
812 (setq org-clock-into-drawer t)
813 (setq org-log-into-drawer t)
814 ;; Sometimes I change tasks I'm clocking quickly - this removes clocked tasks with 0:00 duration
815 (setq org-clock-out-remove-zero-time-clocks t)
816 ;; Clock out when moving task to a done state
817 (setq org-clock-out-when-done t)
818 ;; Save the running clock and all clock history when exiting Emacs, load it on startup
819 (setq org-clock-persist t)
820 ;; Do not prompt to resume an active clock
821 (setq org-clock-persist-query-resume nil)
822 ;; Enable auto clock resolution for finding open clocks
823 (setq org-clock-auto-clock-resolution (quote when-no-clock-is-running))
824 ;; Include current clocking task in clock reports
825 (setq org-clock-report-include-clocking-task t)
828 (defvar bh/keep-clock-running nil)
830 (defun bh/clock-in-to-next (kw)
831 "Switch a task from TODO to NEXT when clocking in.
832 Skips capture tasks, projects, and subprojects.
833 Switch projects and subprojects from NEXT back to TODO"
834 (when (not (and (boundp 'org-capture-mode) org-capture-mode))
836 ((and (member (org-get-todo-state) (list "TODO"))
839 ((and (member (org-get-todo-state) (list "NEXT"))
843 (defun bh/find-project-task ()
844 "Move point to the parent (project) task if any"
847 (let ((parent-task (save-excursion (org-back-to-heading 'invisible-ok) (point))))
848 (while (org-up-heading-safe)
849 (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
850 (setq parent-task (point))))
851 (goto-char parent-task)
854 (defun bh/punch-in (arg)
855 "Start continuous clocking and set the default task to the
856 selected task. If no task is selected set the Organization task
857 as the default task."
859 (setq bh/keep-clock-running t)
860 (if (equal major-mode 'org-agenda-mode)
862 ;; We're in the agenda
864 (let* ((marker (org-get-at-bol 'org-hd-marker))
865 (tags (org-with-point-at marker (org-get-tags-at))))
866 (if (and (eq arg 4) tags)
867 (org-agenda-clock-in '(16))
868 (bh/clock-in-organization-task-as-default)))
870 ;; We are not in the agenda
874 ; Find the tags on the current task
875 (if (and (equal major-mode 'org-mode) (not (org-before-first-heading-p)) (eq arg 4))
877 (bh/clock-in-organization-task-as-default)))))
879 (defun bh/punch-out ()
881 (setq bh/keep-clock-running nil)
882 (when (org-clock-is-active)
884 (org-agenda-remove-restriction-lock))
886 (defun bh/clock-in-default-task ()
888 (org-with-point-at org-clock-default-task
891 (defun bh/clock-in-parent-task ()
892 "Move point to the parent (project) task if any and clock in"
897 (while (and (not parent-task) (org-up-heading-safe))
898 (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
899 (setq parent-task (point))))
901 (org-with-point-at parent-task
903 (when bh/keep-clock-running
904 (bh/clock-in-default-task)))))))
906 (defvar bh/organization-task-id "e22cb8bf-07c7-408b-8f60-ff3aadac95e4")
908 (defun bh/clock-in-organization-task-as-default ()
910 (org-with-point-at (org-id-find bh/organization-task-id 'marker)
911 (org-clock-in '(16))))
913 (defun bh/clock-out-maybe ()
914 (when (and bh/keep-clock-running
915 (not org-clock-clocking-in)
916 (marker-buffer org-clock-default-task)
917 (not org-clock-resolving-clocks-due-to-idleness))
918 (bh/clock-in-parent-task)))
920 (add-hook 'org-clock-out-hook 'bh/clock-out-maybe 'append)
923 (defun bh/clock-in-task-by-id (id)
924 "Clock in a task by id"
925 (org-with-point-at (org-id-find id 'marker)
928 (defun bh/clock-in-last-task (arg)
929 "Clock in the interrupted task if there is one
930 Skip the default task and get the next one.
931 A prefix arg forces clock in of the default task."
933 (let ((clock-in-to-task
935 ((eq arg 4) org-clock-default-task)
936 ((and (org-clock-is-active)
937 (equal org-clock-default-task (cadr org-clock-history)))
938 (caddr org-clock-history))
939 ((org-clock-is-active) (cadr org-clock-history))
940 ((equal org-clock-default-task (car org-clock-history)) (cadr org-clock-history))
941 (t (car org-clock-history)))))
943 (org-with-point-at clock-in-to-task
944 (org-clock-in nil))))
947 ; allow for zero-width-space to be a break in regexp too
948 (setcar org-emphasis-regexp-components " [:space:] \t('\"{")
949 (setcar (nthcdr 1 org-emphasis-regexp-components) " [:space:]- \t.,:!?;'\")}\\")
950 (org-set-emph-re 'org-emphasis-regexp-components org-emphasis-regexp-components)