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