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