]> git.donarmstrong.com Git - lib.git/blob - emacs_el/configuration/org-mode-configuration.el
update org configuration
[lib.git] / emacs_el / configuration / org-mode-configuration.el
1 (require 'org-id)
2 (require 'reftex)
3 (require 'gnus)
4
5 ;; The following lines are always needed. Choose your own keys.
6 (add-to-list 'auto-mode-alist '("\\.\\(org\\|org_archive\\|txt\\)$" . org-mode))
7 (require 'org)
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)
13
14 ;; agenda configuration
15 ;; Do not dim blocked tasks
16 (setq org-agenda-dim-blocked-tasks nil)
17
18 ;; Compact the block agenda view
19 (setq org-agenda-compact-blocks t)
20
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))))
30               (" " "Agenda"
31                ((agenda "" nil)
32                 (tags "REFILE"
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
39                              '(category-keep))))
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
45                              '(category-keep))))
46                 (tags-todo "-CANCELLED/!NEXT"
47                            ((org-agenda-overriding-header (concat "Project Next Tasks"
48                                                                   (if bh/hide-scheduled-and-waiting-next-tasks
49                                                                       ""
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
61                                                                       ""
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
68                              '(category-keep))))
69                 (tags-todo "-REFILE-CANCELLED-WAITING-HOLD/!"
70                            ((org-agenda-overriding-header (concat "Standalone Tasks"
71                                                                   (if bh/hide-scheduled-and-waiting-next-tasks
72                                                                       ""
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
79                              '(category-keep))))
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)))
86                 (tags "-REFILE/"
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))))
90                nil))))
91
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")))
104
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) %TAGS %PRIORITY %TODO %13SCHEDULED %13DEADLINE %6Effort{:}  %CLOCKSUM")
107
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         ("h" "Habit" entry (file "~/projects/notes/refile.org")
129          "* 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")
130         )
131       )
132
133 ;; Remove empty LOGBOOK drawers on clock out
134 (defun bh/remove-empty-drawer-on-clock-out ()
135   (interactive)
136   (save-excursion
137     (beginning-of-line 0)
138     (org-remove-empty-drawer-at "LOGBOOK" (point))))
139
140 (defun my/org-add-id ()
141   (interactive)
142   (save-excursion
143     (if (org-current-level)
144         ()
145       (forward-char 1)
146       )
147     (org-id-get-create)
148     )
149 )
150
151 ; org mode configuration from http://doc.norang.ca/org-mode.html
152 ;; Custom Key Bindings
153 (global-set-key (kbd "<f12>") 'org-agenda)
154 (global-set-key (kbd "<f5>") 'bh/org-todo)
155 (global-set-key (kbd "<S-f5>") 'bh/widen)
156 (global-set-key (kbd "<f7>") 'bh/set-truncate-lines)
157 (global-set-key (kbd "<f8>") 'org-cycle-agenda-files)
158 (global-set-key (kbd "<f9> <f9>") 'bh/show-org-agenda)
159 (global-set-key (kbd "<f9> b") 'bbdb)
160 (global-set-key (kbd "<f9> c") 'calendar)
161 (global-set-key (kbd "<f9> f") 'boxquote-insert-file)
162 (global-set-key (kbd "<f9> h") 'bh/hide-other)
163 (global-set-key (kbd "<f9> n") 'bh/toggle-next-task-display)
164 (global-set-key (kbd "<f9> w") 'widen)
165
166 (global-set-key (kbd "<f9> I") 'bh/punch-in)
167 (global-set-key (kbd "<f9> O") 'bh/punch-out)
168
169 (global-set-key (kbd "<f9> o") 'bh/make-org-scratch)
170
171 (global-set-key (kbd "<f9> r") 'boxquote-region)
172 (global-set-key (kbd "<f9> s") 'bh/switch-to-scratch)
173
174 (global-set-key (kbd "<f9> t") 'bh/insert-inactive-timestamp)
175 (global-set-key (kbd "<f9> T") 'bh/toggle-insert-inactive-timestamp)
176
177 (global-set-key (kbd "<f9> v") 'visible-mode)
178 (global-set-key (kbd "<f9> l") 'org-toggle-link-display)
179 (global-set-key (kbd "<f9> SPC") 'bh/clock-in-last-task)
180 (global-set-key (kbd "C-<f9>") 'previous-buffer)
181 (global-set-key (kbd "M-<f9>") 'org-toggle-inline-images)
182 (global-set-key (kbd "C-x n r") 'narrow-to-region)
183 (global-set-key (kbd "C-<f10>") 'next-buffer)
184 (global-set-key (kbd "<f11>") 'org-clock-goto)
185 (global-set-key (kbd "C-<f11>") 'org-clock-in)
186 (global-set-key (kbd "C-s-<f12>") 'bh/save-then-publish)
187 (global-set-key (kbd "C-c c") 'org-capture)
188
189 (defun bh/hide-other ()
190   (interactive)
191   (save-excursion
192     (org-back-to-heading 'invisible-ok)
193     (hide-other)
194     (org-cycle)
195     (org-cycle)
196     (org-cycle)))
197
198 (defun bh/set-truncate-lines ()
199   "Toggle value of truncate-lines and refresh window display."
200   (interactive)
201   (setq truncate-lines (not truncate-lines))
202   ;; now refresh window display (an idiom from simple.el):
203   (save-excursion
204     (set-window-start (selected-window)
205                       (window-start (selected-window)))))
206
207 (defun bh/make-org-scratch ()
208   (interactive)
209   (find-file "/tmp/publish/scratch.org")
210   (gnus-make-directory "/tmp/publish"))
211
212 (defun bh/switch-to-scratch ()
213   (interactive)
214   (switch-to-buffer "*scratch*"))
215
216 (setq org-use-fast-todo-selection t)
217 (setq org-treat-S-cursor-todo-selection-as-state-change nil)
218
219 (setq org-todo-keywords
220       (quote ((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d)")
221               (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" "PHONE" "MEETING"))))
222
223 (setq org-todo-keyword-faces
224       (quote (("TODO" :foreground "red" :weight bold)
225               ("NEXT" :foreground "blue" :weight bold)
226               ("DONE" :foreground "forest green" :weight bold)
227               ("WAITING" :foreground "orange" :weight bold)
228               ("HOLD" :foreground "magenta" :weight bold)
229               ("CANCELLED" :foreground "forest green" :weight bold)
230               ("MEETING" :foreground "forest green" :weight bold)
231               ("PHONE" :foreground "forest green" :weight bold))))
232
233 (setq org-todo-state-tags-triggers
234       (quote (("CANCELLED" ("CANCELLED" . t))
235               ("WAITING" ("WAITING" . t))
236               ("HOLD" ("WAITING") ("HOLD" . t))
237               (done ("WAITING") ("HOLD"))
238               ("TODO" ("WAITING") ("CANCELLED") ("HOLD"))
239               ("NEXT" ("WAITING") ("CANCELLED") ("HOLD"))
240               ("DONE" ("WAITING") ("CANCELLED") ("HOLD")))))
241
242
243
244 (add-hook 'org-clock-out-hook 'bh/remove-empty-drawer-on-clock-out 'append)
245 ; add ids on creation of nodes
246 (add-hook 'org-capture-prepare-finalize-hook 'my/org-add-id)
247 ; create function to create headlines in file. This comes from
248 ; http://stackoverflow.com/questions/13340616/assign-ids-to-every-entry-in-org-mode
249 (defun my/org-add-ids-to-headlines-in-file ()
250   "Add ID properties to all headlines in the current file which
251 do not already have one."
252   (interactive)
253   (org-map-entries 'org-id-get-create))
254 ; if we wanted to do this to every buffer, do the following:
255 ; (add-hook 'org-mode-hook
256 ;           (lambda ()
257 ;             (add-hook 'before-save-hook 'my/org-add-ids-to-headlines-in-file nil 'local)))
258
259
260 ; resolve clocks after 10 minutes of idle; use xprintidle
261 ; (setq org-clock-idle-time 10)
262 ; (setq org-clock-x11idle-program-name "xprintidle")
263
264 ; this is from http://doc.norang.ca/org-mode.html#Capture
265 ; use C-M-r for org mode capture
266 (global-set-key (kbd "C-M-r") 'org-capture)
267
268 ; Targets include this file and any file contributing to the agenda - up to 9 levels deep
269 (setq org-refile-targets (quote ((nil :maxlevel . 9)
270                                  (org-agenda-files :maxlevel . 9))))
271
272 ; Use full outline paths for refile targets - we file directly with IDO
273 (setq org-refile-use-outline-path t)
274
275 ; Targets complete directly with IDO
276 (setq org-outline-path-complete-in-steps nil)
277
278 ; Allow refile to create parent tasks with confirmation
279 (setq org-refile-allow-creating-parent-nodes (quote confirm))
280
281 ; Use IDO for both buffer and file completion and ido-everywhere to t
282 (setq org-completion-use-ido t)
283 (setq ido-everywhere t)
284 (setq ido-max-directory-size 100000)
285 (ido-mode (quote both))
286 ; Use the current window when visiting files and buffers with ido
287 (setq ido-default-file-method 'selected-window)
288 (setq ido-default-buffer-method 'selected-window)
289 ; Use the current window for indirect buffer display
290 (setq org-indirect-buffer-display 'current-window)
291
292
293 ;;;; Refile settings
294 ; Exclude DONE state tasks from refile targets
295 (defun bh/verify-refile-target ()
296   "Exclude todo keywords with a done state from refile targets"
297   (not (member (nth 2 (org-heading-components)) org-done-keywords)))
298
299 (setq org-refile-target-verify-function 'bh/verify-refile-target)
300
301 ;; ensure that emacsclient will show just the note to be edited when invoked
302 ;; from Mutt, and that it will shut down emacsclient once finished;
303 ;; fallback to legacy behavior when not invoked via org-protocol.
304 (require 'org-protocol)
305 (add-hook 'org-capture-mode-hook 'delete-other-windows)
306 (setq my-org-protocol-flag nil)
307 (defadvice org-capture-finalize (after delete-frame-at-end activate)
308   "Delete frame at remember finalization"
309   (progn (if my-org-protocol-flag (delete-frame))
310          (setq my-org-protocol-flag nil)))
311 (defadvice org-capture-refile (around delete-frame-after-refile activate)
312   "Delete frame at remember refile"
313   (if my-org-protocol-flag
314       (progn
315         (setq my-org-protocol-flag nil)
316         ad-do-it
317         (delete-frame))
318     ad-do-it)
319   )
320 (defadvice org-capture-kill (after delete-frame-at-end activate)
321   "Delete frame at remember abort"
322   (progn (if my-org-protocol-flag (delete-frame))
323          (setq my-org-protocol-flag nil)))
324 (defadvice org-protocol-capture (before set-org-protocol-flag activate)
325   (setq my-org-protocol-flag t))
326
327
328 ;; org modules
329 (add-to-list 'org-modules 'org-habit)
330
331 ; this comes from http://upsilon.cc/~zack/blog/posts/2010/02/integrating_Mutt_with_Org-mode/
332 (defun open-mail-in-mutt (message)
333   "Open a mail message in Mutt, using an external terminal.
334
335 Message can be specified either by a path pointing inside a
336 Maildir, or by Message-ID."
337   (interactive "MPath or Message-ID: ")
338   (shell-command
339    (format "faf xterm -e \"%s %s\""
340        (substitute-in-file-name "$HOME/bin/mutt_open") message)))
341
342 ;; add support for "mutt:ID" links
343 (org-add-link-type "mutt" 'open-mail-in-mutt)
344
345 (defun my-org-mode-setup ()
346   (load-library "reftex")
347   (and (buffer-file-name)
348        (file-exists-p (buffer-file-name))
349        (progn
350          (reftex-parse-all)
351          (reftex-set-cite-format
352           '((?b . "[[bib::%l][%l-bib]]")
353             (?n . "[[note::%l][%l-notes]]")
354             (?c . "\\cite{%l}")
355             (?h . "*** %t\n:PROPERTIES:\n:Custom_ID: %l\n:END:\n[[papers:%l][%l paper]]")))
356          ))
357   (define-key org-mode-map (kbd "C-c )") 'reftex-citation)
358   (define-key org-mode-map (kbd "C-c [") 'reftex-citation)
359   (define-key org-mode-map (kbd "C-c (") 'org-mode-reftex-search)
360   (define-key org-mode-map (kbd "C-c 0") 'reftex-view-crossref)
361   )
362 (add-hook 'org-mode-hook 'my-org-mode-setup)
363
364 (defun org-mode-reftex-search ()
365   (interactive)
366   (org-open-link-from-string (format "[[notes:%s]]" (reftex-citation t))))
367
368 (defun open-research-paper (bibtexkey)
369   "Open a paper by bibtex key"
370   (interactive "bibtex key: ")
371   (shell-command
372    (format "%s %s"
373        (substitute-in-file-name "$HOME/bin/bibtex_to_paper") bibtexkey)))
374 (org-add-link-type "papers" 'open-research-paper)
375
376 ; I pretty much always want hiearchical checkboxes
377 (setq org-hierachical-checkbox-statistics nil)
378  
379 ;; stolen from
380 ;; http://www-public.it-sudparis.eu/~berger_o/weblog/2012/03/23/how-to-manage-and-export-bibliographic-notesrefs-in-org-mode/
381 (defun my-rtcite-export-handler (path desc format)
382   (message "my-rtcite-export-handler is called : path = %s, desc = %s, format = %s" path desc format)
383   (let* ((search (when (string-match "::#?\\(.+\\)\\'" path)
384                    (match-string 1 path)))
385          (path (substring path 0 (match-beginning 0))))
386     (cond ((eq format 'latex)
387            (if (or (not desc) 
388                    (equal 0 (search "rtcite:" desc)))
389                (format "\\cite{%s}" search)
390              (format "\\cite[%s]{%s}" desc search))))))
391
392 (org-add-link-type "rtcite" 
393                    'org-bibtex-open
394                    'my-rtcite-export-handler)
395
396
397 (setq-default org-mobile-directory "/rzlab.ucr.edu:/sites/dav.donarmstrong.com/root/org/")
398 (setq-default org-directory "/home/don/org-mode/")
399 (setq-default org-mobile-inbox-for-pull "/home/don/org-mode/from-mobile.org")
400
401 ;; org mode ical export
402 (setq org-icalendar-timezone "America/Los_Angeles")
403 (setq org-icalendar-use-scheduled '(todo-start event-if-todo))
404 (setq org-icalendar-store-UID t)
405
406 ;; org babel support
407 (org-babel-do-load-languages
408  'org-babel-load-languages
409  '((emacs-lisp . t )
410    (R . t)
411    (latex . t)))
412 ;; org-babel-by-backend
413 (defmacro org-babel-by-backend (&rest body)
414    `(case (if (boundp 'backend) 
415               (org-export-backend-name backend)
416             nil) ,@body))
417
418
419
420 ;; org latex
421 ;; stolen from http://kieranhealy.org/esk/kjhealy.html
422 (require 'org-latex)   
423 ;; Choose either listings or minted for exporting source code blocks.
424 ;; Using minted (as here) requires pygments be installed. To use the
425 ;; default listings package instead, use
426 ;; (setq org-latex-listings t)
427 ;; and change references to "minted" below to "listings"
428 ; (setq org-latex-listings 'minted)
429
430 ;; default settings for minted code blocks
431 (setq org-latex-minted-options
432       '(;("frame" "single")
433         ("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.
434         ("fontsize" "\\small")
435         ))
436 ;; turn off the default toc behavior; deal with it properly in headers to files.
437 (defun org-latex-no-toc (depth)  
438   (when depth
439     (format "%% Org-mode is exporting headings to %s levels.\n"
440             depth)))
441 (setq org-latex-format-toc-function 'org-latex-no-toc)
442
443 (require 'ox-latex)
444 (add-to-list 'org-latex-classes
445              '("memarticle"
446                "\\documentclass[11pt,oneside,article]{memoir}\n"
447                ("\\section{%s}" . "\\section*{%s}")
448                ("\\subsection{%s}" . "\\subsection*{%s}")
449                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
450                ("\\paragraph{%s}" . "\\paragraph*{%s}")
451                ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
452
453 (add-to-list 'org-latex-classes
454              '("membook"
455                "\\documentclass[11pt,oneside]{memoir}\n"
456                ("\\chapter{%s}" . "\\chapter*{%s}")
457                ("\\section{%s}" . "\\section*{%s}")
458                ("\\subsection{%s}" . "\\subsection*{%s}")
459                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
460
461 (add-to-list 'org-latex-classes
462              '("letter"
463                "\\documentclass[11pt]{letter}
464 [NO-DEFAULT-PACKAGES]
465 [PACKAGES]
466 [EXTRA]"
467        ("\\section{%s}" . "\\section*{%s}")
468                ("\\subsection{%s}" . "\\subsection*{%s}")
469                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
470                ("\\paragraph{%s}" . "\\paragraph*{%s}")
471                ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
472
473 ;; Originally taken from Bruno Tavernier: http://thread.gmane.org/gmane.emacs.orgmode/31150/focus=31432
474 ;; but adapted to use latexmk 4.22 or higher.  
475 (setq org-latex-pdf-process '("latexmk -pdflatex=xelatex -bibtex -use-make -pdf %f"))
476
477 ;; Default packages included in /every/ tex file, latex, pdflatex or xelatex
478 (setq org-latex-default-packages-alist
479       '())
480 (setq org-latex-packages-alist
481       '(("" "graphicx" t)
482         ("" "fontspec" t)
483         ("" "xunicode" t)
484         ("" "hyperref" t)
485         ("" "url" t)
486         ("" "rotating" t)
487         ("" "longtable" nil)
488         ("" "float" )))
489
490 (defun org-create-formula--latex-header ()
491   "Return LaTeX header appropriate for previewing a LaTeX snippet."
492   (let ((info (org-combine-plists (org-export--get-global-options
493                                    (org-export-get-backend 'latex))
494                                   (org-export--get-inbuffer-options
495                                    (org-export-get-backend 'latex)))))
496     (org-latex-guess-babel-language
497      (org-latex-guess-inputenc
498       (org-splice-latex-header
499        org-format-latex-header
500        org-latex-default-packages-alist
501        nil t
502        (plist-get info :latex-header)))
503      info)))
504
505
506 ; support ignoring headers in org mode export to latex
507 ; from http://article.gmane.org/gmane.emacs.orgmode/67692
508 (defadvice org-latex-headline (around my-latex-skip-headlines
509                                       (headline contents info) activate)
510   (if (member "ignoreheading" (org-element-property :tags headline))
511       (setq ad-return-value contents)
512     ad-do-it))
513
514
515 ;; helper functions
516 (defun bh/is-project-p ()
517   "Any task with a todo keyword subtask"
518   (save-restriction
519     (widen)
520     (let ((has-subtask)
521           (subtree-end (save-excursion (org-end-of-subtree t)))
522           (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
523       (save-excursion
524         (forward-line 1)
525         (while (and (not has-subtask)
526                     (< (point) subtree-end)
527                     (re-search-forward "^\*+ " subtree-end t))
528           (when (member (org-get-todo-state) org-todo-keywords-1)
529             (setq has-subtask t))))
530       (and is-a-task has-subtask))))
531
532 (defun bh/is-project-subtree-p ()
533   "Any task with a todo keyword that is in a project subtree.
534 Callers of this function already widen the buffer view."
535   (let ((task (save-excursion (org-back-to-heading 'invisible-ok)
536                               (point))))
537     (save-excursion
538       (bh/find-project-task)
539       (if (equal (point) task)
540           nil
541         t))))
542
543 (defun bh/is-task-p ()
544   "Any task with a todo keyword and no subtask"
545   (save-restriction
546     (widen)
547     (let ((has-subtask)
548           (subtree-end (save-excursion (org-end-of-subtree t)))
549           (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
550       (save-excursion
551         (forward-line 1)
552         (while (and (not has-subtask)
553                     (< (point) subtree-end)
554                     (re-search-forward "^\*+ " subtree-end t))
555           (when (member (org-get-todo-state) org-todo-keywords-1)
556             (setq has-subtask t))))
557       (and is-a-task (not has-subtask)))))
558
559 (defun bh/is-subproject-p ()
560   "Any task which is a subtask of another project"
561   (let ((is-subproject)
562         (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
563     (save-excursion
564       (while (and (not is-subproject) (org-up-heading-safe))
565         (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
566           (setq is-subproject t))))
567     (and is-a-task is-subproject)))
568
569 (defun bh/list-sublevels-for-projects-indented ()
570   "Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
571   This is normally used by skipping functions where this variable is already local to the agenda."
572   (if (marker-buffer org-agenda-restrict-begin)
573       (setq org-tags-match-list-sublevels 'indented)
574     (setq org-tags-match-list-sublevels nil))
575   nil)
576
577 (defun bh/list-sublevels-for-projects ()
578   "Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
579   This is normally used by skipping functions where this variable is already local to the agenda."
580   (if (marker-buffer org-agenda-restrict-begin)
581       (setq org-tags-match-list-sublevels t)
582     (setq org-tags-match-list-sublevels nil))
583   nil)
584
585 (defvar bh/hide-scheduled-and-waiting-next-tasks t)
586
587 (defun bh/toggle-next-task-display ()
588   (interactive)
589   (setq bh/hide-scheduled-and-waiting-next-tasks (not bh/hide-scheduled-and-waiting-next-tasks))
590   (when  (equal major-mode 'org-agenda-mode)
591     (org-agenda-redo))
592   (message "%s WAITING and SCHEDULED NEXT Tasks" (if bh/hide-scheduled-and-waiting-next-tasks "Hide" "Show")))
593
594 (defun bh/skip-stuck-projects ()
595   "Skip trees that are not stuck projects"
596   (save-restriction
597     (widen)
598     (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
599       (if (bh/is-project-p)
600           (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
601                  (has-next ))
602             (save-excursion
603               (forward-line 1)
604               (while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
605                 (unless (member "WAITING" (org-get-tags-at))
606                   (setq has-next t))))
607             (if has-next
608                 nil
609               next-headline)) ; a stuck project, has subtasks but no next task
610         nil))))
611
612 (defun bh/skip-non-stuck-projects ()
613   "Skip trees that are not stuck projects"
614   ;; (bh/list-sublevels-for-projects-indented)
615   (save-restriction
616     (widen)
617     (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
618       (if (bh/is-project-p)
619           (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
620                  (has-next ))
621             (save-excursion
622               (forward-line 1)
623               (while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
624                 (unless (member "WAITING" (org-get-tags-at))
625                   (setq has-next t))))
626             (if has-next
627                 next-headline
628               nil)) ; a stuck project, has subtasks but no next task
629         next-headline))))
630
631 (defun bh/skip-non-projects ()
632   "Skip trees that are not projects"
633   ;; (bh/list-sublevels-for-projects-indented)
634   (if (save-excursion (bh/skip-non-stuck-projects))
635       (save-restriction
636         (widen)
637         (let ((subtree-end (save-excursion (org-end-of-subtree t))))
638           (cond
639            ((bh/is-project-p)
640             nil)
641            ((and (bh/is-project-subtree-p) (not (bh/is-task-p)))
642             nil)
643            (t
644             subtree-end))))
645     (save-excursion (org-end-of-subtree t))))
646
647 (defun bh/skip-project-trees-and-habits ()
648   "Skip trees that are projects"
649   (save-restriction
650     (widen)
651     (let ((subtree-end (save-excursion (org-end-of-subtree t))))
652       (cond
653        ((bh/is-project-p)
654         subtree-end)
655        ((org-is-habit-p)
656         subtree-end)
657        (t
658         nil)))))
659
660 (defun bh/skip-projects-and-habits-and-single-tasks ()
661   "Skip trees that are projects, tasks that are habits, single non-project tasks"
662   (save-restriction
663     (widen)
664     (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
665       (cond
666        ((org-is-habit-p)
667         next-headline)
668        ((and bh/hide-scheduled-and-waiting-next-tasks
669              (member "WAITING" (org-get-tags-at)))
670         next-headline)
671        ((bh/is-project-p)
672         next-headline)
673        ((and (bh/is-task-p) (not (bh/is-project-subtree-p)))
674         next-headline)
675        (t
676         nil)))))
677
678 (defun bh/skip-project-tasks-maybe ()
679   "Show tasks related to the current restriction.
680 When restricted to a project, skip project and sub project tasks, habits, NEXT tasks, and loose tasks.
681 When not restricted, skip project and sub-project tasks, habits, and project related tasks."
682   (save-restriction
683     (widen)
684     (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
685            (next-headline (save-excursion (or (outline-next-heading) (point-max))))
686            (limit-to-project (marker-buffer org-agenda-restrict-begin)))
687       (cond
688        ((bh/is-project-p)
689         next-headline)
690        ((org-is-habit-p)
691         subtree-end)
692        ((and (not limit-to-project)
693              (bh/is-project-subtree-p))
694         subtree-end)
695        ((and limit-to-project
696              (bh/is-project-subtree-p)
697              (member (org-get-todo-state) (list "NEXT")))
698         subtree-end)
699        (t
700         nil)))))
701
702 (defun bh/skip-project-tasks ()
703   "Show non-project tasks.
704 Skip project and sub-project tasks, habits, and project related tasks."
705   (save-restriction
706     (widen)
707     (let* ((subtree-end (save-excursion (org-end-of-subtree t))))
708       (cond
709        ((bh/is-project-p)
710         subtree-end)
711        ((org-is-habit-p)
712         subtree-end)
713        ((bh/is-project-subtree-p)
714         subtree-end)
715        (t
716         nil)))))
717
718 (defun bh/skip-non-project-tasks ()
719   "Show project tasks.
720 Skip project and sub-project tasks, habits, and loose non-project tasks."
721   (save-restriction
722     (widen)
723     (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
724            (next-headline (save-excursion (or (outline-next-heading) (point-max)))))
725       (cond
726        ((bh/is-project-p)
727         next-headline)
728        ((org-is-habit-p)
729         subtree-end)
730        ((and (bh/is-project-subtree-p)
731              (member (org-get-todo-state) (list "NEXT")))
732         subtree-end)
733        ((not (bh/is-project-subtree-p))
734         subtree-end)
735        (t
736         nil)))))
737
738 (defun bh/skip-projects-and-habits ()
739   "Skip trees that are projects and tasks that are habits"
740   (save-restriction
741     (widen)
742     (let ((subtree-end (save-excursion (org-end-of-subtree t))))
743       (cond
744        ((bh/is-project-p)
745         subtree-end)
746        ((org-is-habit-p)
747         subtree-end)
748        (t
749         nil)))))
750
751 (defun bh/skip-non-subprojects ()
752   "Skip trees that are not projects"
753   (let ((next-headline (save-excursion (outline-next-heading))))
754     (if (bh/is-subproject-p)
755         nil
756       next-headline)))
757 ;
758 ;; Resume clocking task when emacs is restarted
759 (org-clock-persistence-insinuate)
760 ;;
761 ;; Show lot of clocking history so it's easy to pick items off the C-F11 list
762 (setq org-clock-history-length 23)
763 ;; Resume clocking task on clock-in if the clock is open
764 (setq org-clock-in-resume t)
765 ;; Change tasks to NEXT when clocking in
766 (setq org-clock-in-switch-to-state 'bh/clock-in-to-next)
767 ;; Separate drawers for clocking and logs
768 (setq org-drawers (quote ("PROPERTIES" "LOGBOOK")))
769 ;; Save clock data and state changes and notes in the LOGBOOK drawer
770 (setq org-clock-into-drawer t)
771 ;; Sometimes I change tasks I'm clocking quickly - this removes clocked tasks with 0:00 duration
772 (setq org-clock-out-remove-zero-time-clocks t)
773 ;; Clock out when moving task to a done state
774 (setq org-clock-out-when-done t)
775 ;; Save the running clock and all clock history when exiting Emacs, load it on startup
776 (setq org-clock-persist t)
777 ;; Do not prompt to resume an active clock
778 (setq org-clock-persist-query-resume nil)
779 ;; Enable auto clock resolution for finding open clocks
780 (setq org-clock-auto-clock-resolution (quote when-no-clock-is-running))
781 ;; Include current clocking task in clock reports
782 (setq org-clock-report-include-clocking-task t)
783
784
785 (defvar bh/keep-clock-running nil)
786
787 (defun bh/clock-in-to-next (kw)
788   "Switch a task from TODO to NEXT when clocking in.
789 Skips capture tasks, projects, and subprojects.
790 Switch projects and subprojects from NEXT back to TODO"
791   (when (not (and (boundp 'org-capture-mode) org-capture-mode))
792     (cond
793      ((and (member (org-get-todo-state) (list "TODO"))
794            (bh/is-task-p))
795       "NEXT")
796      ((and (member (org-get-todo-state) (list "NEXT"))
797            (bh/is-project-p))
798       "TODO"))))
799
800 (defun bh/find-project-task ()
801   "Move point to the parent (project) task if any"
802   (save-restriction
803     (widen)
804     (let ((parent-task (save-excursion (org-back-to-heading 'invisible-ok) (point))))
805       (while (org-up-heading-safe)
806         (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
807           (setq parent-task (point))))
808       (goto-char parent-task)
809       parent-task)))
810
811 (defun bh/punch-in (arg)
812   "Start continuous clocking and set the default task to the
813 selected task.  If no task is selected set the Organization task
814 as the default task."
815   (interactive "p")
816   (setq bh/keep-clock-running t)
817   (if (equal major-mode 'org-agenda-mode)
818       ;;
819       ;; We're in the agenda
820       ;;
821       (let* ((marker (org-get-at-bol 'org-hd-marker))
822              (tags (org-with-point-at marker (org-get-tags-at))))
823         (if (and (eq arg 4) tags)
824             (org-agenda-clock-in '(16))
825           (bh/clock-in-organization-task-as-default)))
826     ;;
827     ;; We are not in the agenda
828     ;;
829     (save-restriction
830       (widen)
831       ; Find the tags on the current task
832       (if (and (equal major-mode 'org-mode) (not (org-before-first-heading-p)) (eq arg 4))
833           (org-clock-in '(16))
834         (bh/clock-in-organization-task-as-default)))))
835
836 (defun bh/punch-out ()
837   (interactive)
838   (setq bh/keep-clock-running nil)
839   (when (org-clock-is-active)
840     (org-clock-out))
841   (org-agenda-remove-restriction-lock))
842
843 (defun bh/clock-in-default-task ()
844   (save-excursion
845     (org-with-point-at org-clock-default-task
846       (org-clock-in))))
847
848 (defun bh/clock-in-parent-task ()
849   "Move point to the parent (project) task if any and clock in"
850   (let ((parent-task))
851     (save-excursion
852       (save-restriction
853         (widen)
854         (while (and (not parent-task) (org-up-heading-safe))
855           (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
856             (setq parent-task (point))))
857         (if parent-task
858             (org-with-point-at parent-task
859               (org-clock-in))
860           (when bh/keep-clock-running
861             (bh/clock-in-default-task)))))))
862
863 (defvar bh/organization-task-id "e22cb8bf-07c7-408b-8f60-ff3aadac95e4")
864
865 (defun bh/clock-in-organization-task-as-default ()
866   (interactive)
867   (org-with-point-at (org-id-find bh/organization-task-id 'marker)
868     (org-clock-in '(16))))
869
870 (defun bh/clock-out-maybe ()
871   (when (and bh/keep-clock-running
872              (not org-clock-clocking-in)
873              (marker-buffer org-clock-default-task)
874              (not org-clock-resolving-clocks-due-to-idleness))
875     (bh/clock-in-parent-task)))
876
877 (add-hook 'org-clock-out-hook 'bh/clock-out-maybe 'append)
878
879 (require 'org-id)
880 (defun bh/clock-in-task-by-id (id)
881   "Clock in a task by id"
882   (org-with-point-at (org-id-find id 'marker)
883     (org-clock-in nil)))
884
885 (defun bh/clock-in-last-task (arg)
886   "Clock in the interrupted task if there is one
887 Skip the default task and get the next one.
888 A prefix arg forces clock in of the default task."
889   (interactive "p")
890   (let ((clock-in-to-task
891          (cond
892           ((eq arg 4) org-clock-default-task)
893           ((and (org-clock-is-active)
894                 (equal org-clock-default-task (cadr org-clock-history)))
895            (caddr org-clock-history))
896           ((org-clock-is-active) (cadr org-clock-history))
897           ((equal org-clock-default-task (car org-clock-history)) (cadr org-clock-history))
898           (t (car org-clock-history)))))
899     (widen)
900     (org-with-point-at clock-in-to-task
901       (org-clock-in nil))))
902