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