]> git.donarmstrong.com Git - lib.git/blob - emacs_el/configuration/org-mode-configuration.el
add org-id-get create after insert-todo-heading
[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 (defadvice org-insert-todo-heading (after dla/create-id activate)
328   (org-id-get-create)
329   )
330
331 ;; org modules
332 (add-to-list 'org-modules 'org-habit)
333
334 ; this comes from http://upsilon.cc/~zack/blog/posts/2010/02/integrating_Mutt_with_Org-mode/
335 (defun open-mail-in-mutt (message)
336   "Open a mail message in Mutt, using an external terminal.
337
338 Message can be specified either by a path pointing inside a
339 Maildir, or by Message-ID."
340   (interactive "MPath or Message-ID: ")
341   (shell-command
342    (format "faf xterm -e \"%s %s\""
343        (substitute-in-file-name "$HOME/bin/mutt_open") message)))
344
345 ;; add support for "mutt:ID" links
346 (org-add-link-type "mutt" 'open-mail-in-mutt)
347
348 (defun my-org-mode-setup ()
349   (load-library "reftex")
350   (and (buffer-file-name)
351        (file-exists-p (buffer-file-name))
352        (progn
353          (reftex-parse-all)
354          (reftex-set-cite-format
355           '((?b . "[[bib:%l][%l-bib]]")
356             (?n . "[[notes:%l][%l-notes]]")
357             (?c . "\\cite{%l}")
358             (?h . "*** %t\n:PROPERTIES:\n:Custom_ID: %l\n:END:\n[[papers:%l][%l paper]]")))
359          ))
360   (define-key org-mode-map (kbd "C-c )") 'reftex-citation)
361   (define-key org-mode-map (kbd "C-c [") 'reftex-citation)
362   (define-key org-mode-map (kbd "C-c (") 'org-mode-reftex-search)
363   (define-key org-mode-map (kbd "C-c 0") 'reftex-view-crossref)
364   )
365 (add-hook 'org-mode-hook 'my-org-mode-setup)
366
367 (defun org-mode-reftex-search ()
368   (interactive)
369   (org-open-link-from-string (format "[[notes:%s]]" (first (reftex-citation t)))))
370
371 (defun open-research-paper (bibtexkey)
372   "Open a paper by bibtex key"
373   (interactive "bibtex key: ")
374   (shell-command
375    (format "%s %s"
376        (substitute-in-file-name "$HOME/bin/bibtex_to_paper") bibtexkey)))
377 (org-add-link-type "papers" 'open-research-paper)
378
379 (add-to-list 'org-link-abbrev-alist
380              '("notes" .
381                "~/projects/research/paper_notes.org::#%s"))
382
383 ; I pretty much always want hiearchical checkboxes
384 (setq org-hierachical-checkbox-statistics nil)
385  
386 ;; stolen from
387 ;; http://www-public.it-sudparis.eu/~berger_o/weblog/2012/03/23/how-to-manage-and-export-bibliographic-notesrefs-in-org-mode/
388 (defun my-rtcite-export-handler (path desc format)
389   (message "my-rtcite-export-handler is called : path = %s, desc = %s, format = %s" path desc format)
390   (let* ((search (when (string-match "::#?\\(.+\\)\\'" path)
391                    (match-string 1 path)))
392          (path (substring path 0 (match-beginning 0))))
393     (cond ((eq format 'latex)
394            (if (or (not desc) 
395                    (equal 0 (search "rtcite:" desc)))
396                (format "\\cite{%s}" search)
397              (format "\\cite[%s]{%s}" desc search))))))
398
399 (org-add-link-type "rtcite" 
400                    'org-bibtex-open
401                    'my-rtcite-export-handler)
402
403
404 (setq-default org-mobile-directory "/rzlab.ucr.edu:/sites/dav.donarmstrong.com/root/org/")
405 (setq-default org-directory "/home/don/org-mode/")
406 (setq-default org-mobile-inbox-for-pull "/home/don/org-mode/from-mobile.org")
407
408 ;; org mode ical export
409 (setq org-icalendar-timezone "America/Los_Angeles")
410 (setq org-icalendar-use-scheduled '(todo-start event-if-todo))
411 (setq org-icalendar-store-UID t)
412
413 ;; org babel support
414 (org-babel-do-load-languages
415  'org-babel-load-languages
416  '((emacs-lisp . t )
417    (R . t)
418    (latex . t)))
419 ;; org-babel-by-backend
420 (defmacro org-babel-by-backend (&rest body)
421    `(case (if (boundp 'backend) 
422               (org-export-backend-name backend)
423             nil) ,@body))
424
425
426
427 ;; org latex
428 ;; stolen from http://kieranhealy.org/esk/kjhealy.html
429 (require 'org-latex)   
430 ;; Choose either listings or minted for exporting source code blocks.
431 ;; Using minted (as here) requires pygments be installed. To use the
432 ;; default listings package instead, use
433 ;; (setq org-latex-listings t)
434 ;; and change references to "minted" below to "listings"
435 ; (setq org-latex-listings 'minted)
436
437 ;; default settings for minted code blocks
438 (setq org-latex-minted-options
439       '(;("frame" "single")
440         ("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.
441         ("fontsize" "\\small")
442         ))
443 ;; turn off the default toc behavior; deal with it properly in headers to files.
444 (defun org-latex-no-toc (depth)  
445   (when depth
446     (format "%% Org-mode is exporting headings to %s levels.\n"
447             depth)))
448 (setq org-latex-format-toc-function 'org-latex-no-toc)
449
450 (require 'ox-latex)
451 (add-to-list 'org-latex-classes
452              '("memarticle"
453                "\\documentclass[11pt,oneside,article]{memoir}\n"
454                ("\\section{%s}" . "\\section*{%s}")
455                ("\\subsection{%s}" . "\\subsection*{%s}")
456                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
457                ("\\paragraph{%s}" . "\\paragraph*{%s}")
458                ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
459
460 (add-to-list 'org-latex-classes
461              '("membook"
462                "\\documentclass[11pt,oneside]{memoir}\n"
463                ("\\chapter{%s}" . "\\chapter*{%s}")
464                ("\\section{%s}" . "\\section*{%s}")
465                ("\\subsection{%s}" . "\\subsection*{%s}")
466                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
467
468 (add-to-list 'org-latex-classes
469              '("letter"
470                "\\documentclass[11pt]{letter}
471 [NO-DEFAULT-PACKAGES]
472 [PACKAGES]
473 [EXTRA]"
474        ("\\section{%s}" . "\\section*{%s}")
475                ("\\subsection{%s}" . "\\subsection*{%s}")
476                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
477                ("\\paragraph{%s}" . "\\paragraph*{%s}")
478                ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
479
480 ;; Originally taken from Bruno Tavernier: http://thread.gmane.org/gmane.emacs.orgmode/31150/focus=31432
481 ;; but adapted to use latexmk 4.22 or higher.  
482 (setq org-latex-pdf-process '("latexmk -pdflatex=xelatex -bibtex -use-make -pdf %f"))
483
484 ;; Default packages included in /every/ tex file, latex, pdflatex or xelatex
485 (setq org-latex-default-packages-alist
486       '())
487 (setq org-latex-packages-alist
488       '(("" "graphicx" t)
489         ("" "fontspec" t)
490         ("" "xunicode" t)
491         ("" "hyperref" t)
492         ("" "url" t)
493         ("" "rotating" t)
494         ("" "longtable" nil)
495         ("" "float" )))
496
497 (defun org-create-formula--latex-header ()
498   "Return LaTeX header appropriate for previewing a LaTeX snippet."
499   (let ((info (org-combine-plists (org-export--get-global-options
500                                    (org-export-get-backend 'latex))
501                                   (org-export--get-inbuffer-options
502                                    (org-export-get-backend 'latex)))))
503     (org-latex-guess-babel-language
504      (org-latex-guess-inputenc
505       (org-splice-latex-header
506        org-format-latex-header
507        org-latex-default-packages-alist
508        nil t
509        (plist-get info :latex-header)))
510      info)))
511
512
513 ; support ignoring headers in org mode export to latex
514 ; from http://article.gmane.org/gmane.emacs.orgmode/67692
515 (defadvice org-latex-headline (around my-latex-skip-headlines
516                                       (headline contents info) activate)
517   (if (member "ignoreheading" (org-element-property :tags headline))
518       (setq ad-return-value contents)
519     ad-do-it))
520
521
522 ;; helper functions
523 (defun bh/is-project-p ()
524   "Any task with a todo keyword subtask"
525   (save-restriction
526     (widen)
527     (let ((has-subtask)
528           (subtree-end (save-excursion (org-end-of-subtree t)))
529           (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
530       (save-excursion
531         (forward-line 1)
532         (while (and (not has-subtask)
533                     (< (point) subtree-end)
534                     (re-search-forward "^\*+ " subtree-end t))
535           (when (member (org-get-todo-state) org-todo-keywords-1)
536             (setq has-subtask t))))
537       (and is-a-task has-subtask))))
538
539 (defun bh/is-project-subtree-p ()
540   "Any task with a todo keyword that is in a project subtree.
541 Callers of this function already widen the buffer view."
542   (let ((task (save-excursion (org-back-to-heading 'invisible-ok)
543                               (point))))
544     (save-excursion
545       (bh/find-project-task)
546       (if (equal (point) task)
547           nil
548         t))))
549
550 (defun bh/is-task-p ()
551   "Any task with a todo keyword and no subtask"
552   (save-restriction
553     (widen)
554     (let ((has-subtask)
555           (subtree-end (save-excursion (org-end-of-subtree t)))
556           (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
557       (save-excursion
558         (forward-line 1)
559         (while (and (not has-subtask)
560                     (< (point) subtree-end)
561                     (re-search-forward "^\*+ " subtree-end t))
562           (when (member (org-get-todo-state) org-todo-keywords-1)
563             (setq has-subtask t))))
564       (and is-a-task (not has-subtask)))))
565
566 (defun bh/is-subproject-p ()
567   "Any task which is a subtask of another project"
568   (let ((is-subproject)
569         (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
570     (save-excursion
571       (while (and (not is-subproject) (org-up-heading-safe))
572         (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
573           (setq is-subproject t))))
574     (and is-a-task is-subproject)))
575
576 (defun bh/list-sublevels-for-projects-indented ()
577   "Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
578   This is normally used by skipping functions where this variable is already local to the agenda."
579   (if (marker-buffer org-agenda-restrict-begin)
580       (setq org-tags-match-list-sublevels 'indented)
581     (setq org-tags-match-list-sublevels nil))
582   nil)
583
584 (defun bh/list-sublevels-for-projects ()
585   "Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
586   This is normally used by skipping functions where this variable is already local to the agenda."
587   (if (marker-buffer org-agenda-restrict-begin)
588       (setq org-tags-match-list-sublevels t)
589     (setq org-tags-match-list-sublevels nil))
590   nil)
591
592 (defvar bh/hide-scheduled-and-waiting-next-tasks t)
593
594 (defun bh/toggle-next-task-display ()
595   (interactive)
596   (setq bh/hide-scheduled-and-waiting-next-tasks (not bh/hide-scheduled-and-waiting-next-tasks))
597   (when  (equal major-mode 'org-agenda-mode)
598     (org-agenda-redo))
599   (message "%s WAITING and SCHEDULED NEXT Tasks" (if bh/hide-scheduled-and-waiting-next-tasks "Hide" "Show")))
600
601 (defun bh/skip-stuck-projects ()
602   "Skip trees that are not stuck projects"
603   (save-restriction
604     (widen)
605     (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
606       (if (bh/is-project-p)
607           (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
608                  (has-next ))
609             (save-excursion
610               (forward-line 1)
611               (while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
612                 (unless (member "WAITING" (org-get-tags-at))
613                   (setq has-next t))))
614             (if has-next
615                 nil
616               next-headline)) ; a stuck project, has subtasks but no next task
617         nil))))
618
619 (defun bh/skip-non-stuck-projects ()
620   "Skip trees that are not stuck projects"
621   ;; (bh/list-sublevels-for-projects-indented)
622   (save-restriction
623     (widen)
624     (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
625       (if (bh/is-project-p)
626           (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
627                  (has-next ))
628             (save-excursion
629               (forward-line 1)
630               (while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
631                 (unless (member "WAITING" (org-get-tags-at))
632                   (setq has-next t))))
633             (if has-next
634                 next-headline
635               nil)) ; a stuck project, has subtasks but no next task
636         next-headline))))
637
638 (defun bh/skip-non-projects ()
639   "Skip trees that are not projects"
640   ;; (bh/list-sublevels-for-projects-indented)
641   (if (save-excursion (bh/skip-non-stuck-projects))
642       (save-restriction
643         (widen)
644         (let ((subtree-end (save-excursion (org-end-of-subtree t))))
645           (cond
646            ((bh/is-project-p)
647             nil)
648            ((and (bh/is-project-subtree-p) (not (bh/is-task-p)))
649             nil)
650            (t
651             subtree-end))))
652     (save-excursion (org-end-of-subtree t))))
653
654 (defun bh/skip-project-trees-and-habits ()
655   "Skip trees that are projects"
656   (save-restriction
657     (widen)
658     (let ((subtree-end (save-excursion (org-end-of-subtree t))))
659       (cond
660        ((bh/is-project-p)
661         subtree-end)
662        ((org-is-habit-p)
663         subtree-end)
664        (t
665         nil)))))
666
667 (defun bh/skip-projects-and-habits-and-single-tasks ()
668   "Skip trees that are projects, tasks that are habits, single non-project tasks"
669   (save-restriction
670     (widen)
671     (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
672       (cond
673        ((org-is-habit-p)
674         next-headline)
675        ((and bh/hide-scheduled-and-waiting-next-tasks
676              (member "WAITING" (org-get-tags-at)))
677         next-headline)
678        ((bh/is-project-p)
679         next-headline)
680        ((and (bh/is-task-p) (not (bh/is-project-subtree-p)))
681         next-headline)
682        (t
683         nil)))))
684
685 (defun bh/skip-project-tasks-maybe ()
686   "Show tasks related to the current restriction.
687 When restricted to a project, skip project and sub project tasks, habits, NEXT tasks, and loose tasks.
688 When not restricted, skip project and sub-project tasks, habits, and project related tasks."
689   (save-restriction
690     (widen)
691     (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
692            (next-headline (save-excursion (or (outline-next-heading) (point-max))))
693            (limit-to-project (marker-buffer org-agenda-restrict-begin)))
694       (cond
695        ((bh/is-project-p)
696         next-headline)
697        ((org-is-habit-p)
698         subtree-end)
699        ((and (not limit-to-project)
700              (bh/is-project-subtree-p))
701         subtree-end)
702        ((and limit-to-project
703              (bh/is-project-subtree-p)
704              (member (org-get-todo-state) (list "NEXT")))
705         subtree-end)
706        (t
707         nil)))))
708
709 (defun bh/skip-project-tasks ()
710   "Show non-project tasks.
711 Skip project and sub-project tasks, habits, and project related tasks."
712   (save-restriction
713     (widen)
714     (let* ((subtree-end (save-excursion (org-end-of-subtree t))))
715       (cond
716        ((bh/is-project-p)
717         subtree-end)
718        ((org-is-habit-p)
719         subtree-end)
720        ((bh/is-project-subtree-p)
721         subtree-end)
722        (t
723         nil)))))
724
725 (defun bh/skip-non-project-tasks ()
726   "Show project tasks.
727 Skip project and sub-project tasks, habits, and loose non-project tasks."
728   (save-restriction
729     (widen)
730     (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
731            (next-headline (save-excursion (or (outline-next-heading) (point-max)))))
732       (cond
733        ((bh/is-project-p)
734         next-headline)
735        ((org-is-habit-p)
736         subtree-end)
737        ((and (bh/is-project-subtree-p)
738              (member (org-get-todo-state) (list "NEXT")))
739         subtree-end)
740        ((not (bh/is-project-subtree-p))
741         subtree-end)
742        (t
743         nil)))))
744
745 (defun bh/skip-projects-and-habits ()
746   "Skip trees that are projects and tasks that are habits"
747   (save-restriction
748     (widen)
749     (let ((subtree-end (save-excursion (org-end-of-subtree t))))
750       (cond
751        ((bh/is-project-p)
752         subtree-end)
753        ((org-is-habit-p)
754         subtree-end)
755        (t
756         nil)))))
757
758 (defun bh/skip-non-subprojects ()
759   "Skip trees that are not projects"
760   (let ((next-headline (save-excursion (outline-next-heading))))
761     (if (bh/is-subproject-p)
762         nil
763       next-headline)))
764 ;
765 ;; Resume clocking task when emacs is restarted
766 (org-clock-persistence-insinuate)
767 ;;
768 ;; Show lot of clocking history so it's easy to pick items off the C-F11 list
769 (setq org-clock-history-length 23)
770 ;; Resume clocking task on clock-in if the clock is open
771 (setq org-clock-in-resume t)
772 ;; Change tasks to NEXT when clocking in
773 (setq org-clock-in-switch-to-state 'bh/clock-in-to-next)
774 ;; Separate drawers for clocking and logs
775 (setq org-drawers (quote ("PROPERTIES" "LOGBOOK")))
776 ;; Save clock data and state changes and notes in the LOGBOOK drawer
777 (setq org-clock-into-drawer t)
778 (setq org-log-into-drawer t)
779 ;; Sometimes I change tasks I'm clocking quickly - this removes clocked tasks with 0:00 duration
780 (setq org-clock-out-remove-zero-time-clocks t)
781 ;; Clock out when moving task to a done state
782 (setq org-clock-out-when-done t)
783 ;; Save the running clock and all clock history when exiting Emacs, load it on startup
784 (setq org-clock-persist t)
785 ;; Do not prompt to resume an active clock
786 (setq org-clock-persist-query-resume nil)
787 ;; Enable auto clock resolution for finding open clocks
788 (setq org-clock-auto-clock-resolution (quote when-no-clock-is-running))
789 ;; Include current clocking task in clock reports
790 (setq org-clock-report-include-clocking-task t)
791
792
793 (defvar bh/keep-clock-running nil)
794
795 (defun bh/clock-in-to-next (kw)
796   "Switch a task from TODO to NEXT when clocking in.
797 Skips capture tasks, projects, and subprojects.
798 Switch projects and subprojects from NEXT back to TODO"
799   (when (not (and (boundp 'org-capture-mode) org-capture-mode))
800     (cond
801      ((and (member (org-get-todo-state) (list "TODO"))
802            (bh/is-task-p))
803       "NEXT")
804      ((and (member (org-get-todo-state) (list "NEXT"))
805            (bh/is-project-p))
806       "TODO"))))
807
808 (defun bh/find-project-task ()
809   "Move point to the parent (project) task if any"
810   (save-restriction
811     (widen)
812     (let ((parent-task (save-excursion (org-back-to-heading 'invisible-ok) (point))))
813       (while (org-up-heading-safe)
814         (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
815           (setq parent-task (point))))
816       (goto-char parent-task)
817       parent-task)))
818
819 (defun bh/punch-in (arg)
820   "Start continuous clocking and set the default task to the
821 selected task.  If no task is selected set the Organization task
822 as the default task."
823   (interactive "p")
824   (setq bh/keep-clock-running t)
825   (if (equal major-mode 'org-agenda-mode)
826       ;;
827       ;; We're in the agenda
828       ;;
829       (let* ((marker (org-get-at-bol 'org-hd-marker))
830              (tags (org-with-point-at marker (org-get-tags-at))))
831         (if (and (eq arg 4) tags)
832             (org-agenda-clock-in '(16))
833           (bh/clock-in-organization-task-as-default)))
834     ;;
835     ;; We are not in the agenda
836     ;;
837     (save-restriction
838       (widen)
839       ; Find the tags on the current task
840       (if (and (equal major-mode 'org-mode) (not (org-before-first-heading-p)) (eq arg 4))
841           (org-clock-in '(16))
842         (bh/clock-in-organization-task-as-default)))))
843
844 (defun bh/punch-out ()
845   (interactive)
846   (setq bh/keep-clock-running nil)
847   (when (org-clock-is-active)
848     (org-clock-out))
849   (org-agenda-remove-restriction-lock))
850
851 (defun bh/clock-in-default-task ()
852   (save-excursion
853     (org-with-point-at org-clock-default-task
854       (org-clock-in))))
855
856 (defun bh/clock-in-parent-task ()
857   "Move point to the parent (project) task if any and clock in"
858   (let ((parent-task))
859     (save-excursion
860       (save-restriction
861         (widen)
862         (while (and (not parent-task) (org-up-heading-safe))
863           (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
864             (setq parent-task (point))))
865         (if parent-task
866             (org-with-point-at parent-task
867               (org-clock-in))
868           (when bh/keep-clock-running
869             (bh/clock-in-default-task)))))))
870
871 (defvar bh/organization-task-id "e22cb8bf-07c7-408b-8f60-ff3aadac95e4")
872
873 (defun bh/clock-in-organization-task-as-default ()
874   (interactive)
875   (org-with-point-at (org-id-find bh/organization-task-id 'marker)
876     (org-clock-in '(16))))
877
878 (defun bh/clock-out-maybe ()
879   (when (and bh/keep-clock-running
880              (not org-clock-clocking-in)
881              (marker-buffer org-clock-default-task)
882              (not org-clock-resolving-clocks-due-to-idleness))
883     (bh/clock-in-parent-task)))
884
885 (add-hook 'org-clock-out-hook 'bh/clock-out-maybe 'append)
886
887 (require 'org-id)
888 (defun bh/clock-in-task-by-id (id)
889   "Clock in a task by id"
890   (org-with-point-at (org-id-find id 'marker)
891     (org-clock-in nil)))
892
893 (defun bh/clock-in-last-task (arg)
894   "Clock in the interrupted task if there is one
895 Skip the default task and get the next one.
896 A prefix arg forces clock in of the default task."
897   (interactive "p")
898   (let ((clock-in-to-task
899          (cond
900           ((eq arg 4) org-clock-default-task)
901           ((and (org-clock-is-active)
902                 (equal org-clock-default-task (cadr org-clock-history)))
903            (caddr org-clock-history))
904           ((org-clock-is-active) (cadr org-clock-history))
905           ((equal org-clock-default-task (car org-clock-history)) (cadr org-clock-history))
906           (t (car org-clock-history)))))
907     (widen)
908     (org-with-point-at clock-in-to-task
909       (org-clock-in nil))))
910