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