]> git.donarmstrong.com Git - lib.git/blob - emacs_el/configuration/org-mode-configuration.el
handle org-mobile push on rzlab
[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 (when (string= system-name "rzlab.ucr.edu")
432   (setq-default org-mobile-directory "/sites/dav.donarmstrong.com/root/org/"))
433 (setq-default org-directory "/home/don/org-mode/")
434 (setq-default org-mobile-inbox-for-pull "/home/don/org-mode/from-mobile.org")
435
436 ;; org mode ical export
437 (setq org-icalendar-timezone "America/Los_Angeles")
438 (setq org-icalendar-use-scheduled '(todo-start event-if-todo))
439 ;; we already add the id manually
440 ;; (setq org-icalendar-store-UID t)
441
442 ;; org babel support
443 (org-babel-do-load-languages
444  'org-babel-load-languages
445  '((emacs-lisp . t )
446    (R . t)
447    (latex . t)
448    (ditaa . t)
449    ))
450 ;; org-babel-by-backend
451 (defmacro org-babel-by-backend (&rest body)
452    `(case (if (boundp 'backend) 
453               (org-export-backend-name backend)
454             nil) ,@body))
455
456
457
458 ;; ;; org latex
459 ;; ;; stolen from http://kieranhealy.org/esk/kjhealy.html
460 ;; (require 'org-latex)   
461 ;; ;; Choose either listings or minted for exporting source code blocks.
462 ;; ;; Using minted (as here) requires pygments be installed. To use the
463 ;; ;; default listings package instead, use
464 ;; ;; (setq org-latex-listings t)
465 ;; ;; and change references to "minted" below to "listings"
466 ;; ; (setq org-latex-listings 'minted)
467 ;; 
468 ;; ;; default settings for minted code blocks
469 ;; (setq org-latex-minted-options
470 ;;       '(;("frame" "single")
471 ;;         ("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.
472 ;;         ("fontsize" "\\small")
473 ;;         ))
474 ;; ;; turn off the default toc behavior; deal with it properly in headers to files.
475 ;; (defun org-latex-no-toc (depth)  
476 ;;   (when depth
477 ;;     (format "%% Org-mode is exporting headings to %s levels.\n"
478 ;;             depth)))
479 ;; (setq org-latex-format-toc-function 'org-latex-no-toc)
480
481 (require 'ox-latex)
482 (add-to-list 'org-latex-classes
483              '("memarticle"
484                "\\documentclass[11pt,oneside,article]{memoir}\n"
485                ("\\section{%s}" . "\\section*{%s}")
486                ("\\subsection{%s}" . "\\subsection*{%s}")
487                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
488                ("\\paragraph{%s}" . "\\paragraph*{%s}")
489                ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
490
491 (setq org-beamer-outline-frame-options "")
492 (require 'ox-beamer)
493 (add-to-list 'org-latex-classes
494              '("beamer"
495                "\\documentclass[ignorenonframetext]{beamer}
496 [NO-DEFAULT-PACKAGES]
497 [PACKAGES]
498 [EXTRA]"
499                ("\\section{%s}" . "\\section*{%s}")
500                ("\\subsection{%s}" . "\\subsection*{%s}")
501                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
502                ("\\paragraph{%s}" . "\\paragraph*{%s}")
503                ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
504
505 (add-to-list 'org-latex-classes
506              '("membook"
507                "\\documentclass[11pt,oneside]{memoir}\n"
508                ("\\chapter{%s}" . "\\chapter*{%s}")
509                ("\\section{%s}" . "\\section*{%s}")
510                ("\\subsection{%s}" . "\\subsection*{%s}")
511                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
512
513 (add-to-list 'org-latex-classes
514              '("letter"
515                "\\documentclass[11pt]{letter}
516 [NO-DEFAULT-PACKAGES]
517 [PACKAGES]
518 [EXTRA]"
519        ("\\section{%s}" . "\\section*{%s}")
520                ("\\subsection{%s}" . "\\subsection*{%s}")
521                ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
522                ("\\paragraph{%s}" . "\\paragraph*{%s}")
523                ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
524
525 ;; Originally taken from Bruno Tavernier: http://thread.gmane.org/gmane.emacs.orgmode/31150/focus=31432
526 ;; but adapted to use latexmk 4.22 or higher.  
527 (setq org-latex-pdf-process '("latexmk -pdflatex=xelatex -bibtex -use-make -pdf %f"))
528
529 ;; Default packages included in /every/ tex file, latex, pdflatex or xelatex
530 (setq org-latex-default-packages-alist
531       '())
532 (setq org-latex-packages-alist
533       '(("" "graphicx" t)
534         ("" "fontspec" t)
535         ("" "xunicode" t)
536         ("" "hyperref" t)
537         ("" "url" t)
538         ("" "rotating" t)
539         ("" "longtable" nil)
540         ("" "float" )))
541
542 (defun org-create-formula--latex-header ()
543   "Return LaTeX header appropriate for previewing a LaTeX snippet."
544   (let ((info (org-combine-plists (org-export--get-global-options
545                                    (org-export-get-backend 'latex))
546                                   (org-export--get-inbuffer-options
547                                    (org-export-get-backend 'latex)))))
548     (org-latex-guess-babel-language
549      (org-latex-guess-inputenc
550       (org-splice-latex-header
551        org-format-latex-header
552        org-latex-default-packages-alist
553        nil t
554        (plist-get info :latex-header)))
555      info)))
556
557
558 ; support ignoring headers in org mode export to latex
559 ; from http://article.gmane.org/gmane.emacs.orgmode/67692
560 (defadvice org-latex-headline (around my-latex-skip-headlines
561                                       (headline contents info) activate)
562   (if (member "ignoreheading" (org-element-property :tags headline))
563       (setq ad-return-value contents)
564     ad-do-it))
565
566 ;; keep latex logfiles
567
568 (setq org-latex-remove-logfiles nil)
569
570 ;; helper functions
571 (defun bh/is-project-p ()
572   "Any task with a todo keyword subtask"
573   (save-restriction
574     (widen)
575     (let ((has-subtask)
576           (subtree-end (save-excursion (org-end-of-subtree t)))
577           (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
578       (save-excursion
579         (forward-line 1)
580         (while (and (not has-subtask)
581                     (< (point) subtree-end)
582                     (re-search-forward "^\*+ " subtree-end t))
583           (when (member (org-get-todo-state) org-todo-keywords-1)
584             (setq has-subtask t))))
585       (and is-a-task has-subtask))))
586
587 (defun bh/is-project-subtree-p ()
588   "Any task with a todo keyword that is in a project subtree.
589 Callers of this function already widen the buffer view."
590   (let ((task (save-excursion (org-back-to-heading 'invisible-ok)
591                               (point))))
592     (save-excursion
593       (bh/find-project-task)
594       (if (equal (point) task)
595           nil
596         t))))
597
598 (defun bh/is-task-p ()
599   "Any task with a todo keyword and no subtask"
600   (save-restriction
601     (widen)
602     (let ((has-subtask)
603           (subtree-end (save-excursion (org-end-of-subtree t)))
604           (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
605       (save-excursion
606         (forward-line 1)
607         (while (and (not has-subtask)
608                     (< (point) subtree-end)
609                     (re-search-forward "^\*+ " subtree-end t))
610           (when (member (org-get-todo-state) org-todo-keywords-1)
611             (setq has-subtask t))))
612       (and is-a-task (not has-subtask)))))
613
614 (defun bh/is-subproject-p ()
615   "Any task which is a subtask of another project"
616   (let ((is-subproject)
617         (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
618     (save-excursion
619       (while (and (not is-subproject) (org-up-heading-safe))
620         (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
621           (setq is-subproject t))))
622     (and is-a-task is-subproject)))
623
624 (defun bh/list-sublevels-for-projects-indented ()
625   "Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
626   This is normally used by skipping functions where this variable is already local to the agenda."
627   (if (marker-buffer org-agenda-restrict-begin)
628       (setq org-tags-match-list-sublevels 'indented)
629     (setq org-tags-match-list-sublevels nil))
630   nil)
631
632 (defun bh/list-sublevels-for-projects ()
633   "Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
634   This is normally used by skipping functions where this variable is already local to the agenda."
635   (if (marker-buffer org-agenda-restrict-begin)
636       (setq org-tags-match-list-sublevels t)
637     (setq org-tags-match-list-sublevels nil))
638   nil)
639
640 (defvar bh/hide-scheduled-and-waiting-next-tasks t)
641
642 (defun bh/toggle-next-task-display ()
643   (interactive)
644   (setq bh/hide-scheduled-and-waiting-next-tasks (not bh/hide-scheduled-and-waiting-next-tasks))
645   (when  (equal major-mode 'org-agenda-mode)
646     (org-agenda-redo))
647   (message "%s WAITING and SCHEDULED NEXT Tasks" (if bh/hide-scheduled-and-waiting-next-tasks "Hide" "Show")))
648
649 (defun bh/skip-stuck-projects ()
650   "Skip trees that are not stuck projects"
651   (save-restriction
652     (widen)
653     (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
654       (if (bh/is-project-p)
655           (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
656                  (has-next ))
657             (save-excursion
658               (forward-line 1)
659               (while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
660                 (unless (member "WAITING" (org-get-tags-at))
661                   (setq has-next t))))
662             (if has-next
663                 nil
664               next-headline)) ; a stuck project, has subtasks but no next task
665         nil))))
666
667 (defun bh/skip-non-stuck-projects ()
668   "Skip trees that are not stuck projects"
669   ;; (bh/list-sublevels-for-projects-indented)
670   (save-restriction
671     (widen)
672     (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
673       (if (bh/is-project-p)
674           (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
675                  (has-next ))
676             (save-excursion
677               (forward-line 1)
678               (while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
679                 (unless (member "WAITING" (org-get-tags-at))
680                   (setq has-next t))))
681             (if has-next
682                 next-headline
683               nil)) ; a stuck project, has subtasks but no next task
684         next-headline))))
685
686 (defun bh/skip-non-projects ()
687   "Skip trees that are not projects"
688   ;; (bh/list-sublevels-for-projects-indented)
689   (if (save-excursion (bh/skip-non-stuck-projects))
690       (save-restriction
691         (widen)
692         (let ((subtree-end (save-excursion (org-end-of-subtree t))))
693           (cond
694            ((bh/is-project-p)
695             nil)
696            ((and (bh/is-project-subtree-p) (not (bh/is-task-p)))
697             nil)
698            (t
699             subtree-end))))
700     (save-excursion (org-end-of-subtree t))))
701
702 (defun bh/skip-project-trees-and-habits ()
703   "Skip trees that are projects"
704   (save-restriction
705     (widen)
706     (let ((subtree-end (save-excursion (org-end-of-subtree t))))
707       (cond
708        ((bh/is-project-p)
709         subtree-end)
710        ((org-is-habit-p)
711         subtree-end)
712        (t
713         nil)))))
714
715 (defun bh/skip-projects-and-habits-and-single-tasks ()
716   "Skip trees that are projects, tasks that are habits, single non-project tasks"
717   (save-restriction
718     (widen)
719     (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
720       (cond
721        ((org-is-habit-p)
722         next-headline)
723        ((and bh/hide-scheduled-and-waiting-next-tasks
724              (member "WAITING" (org-get-tags-at)))
725         next-headline)
726        ((bh/is-project-p)
727         next-headline)
728        ((and (bh/is-task-p) (not (bh/is-project-subtree-p)))
729         next-headline)
730        (t
731         nil)))))
732
733 (defun bh/skip-project-tasks-maybe ()
734   "Show tasks related to the current restriction.
735 When restricted to a project, skip project and sub project tasks, habits, NEXT tasks, and loose tasks.
736 When not restricted, skip project and sub-project tasks, habits, and project related tasks."
737   (save-restriction
738     (widen)
739     (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
740            (next-headline (save-excursion (or (outline-next-heading) (point-max))))
741            (limit-to-project (marker-buffer org-agenda-restrict-begin)))
742       (cond
743        ((bh/is-project-p)
744         next-headline)
745        ((org-is-habit-p)
746         subtree-end)
747        ((and (not limit-to-project)
748              (bh/is-project-subtree-p))
749         subtree-end)
750        ((and limit-to-project
751              (bh/is-project-subtree-p)
752              (member (org-get-todo-state) (list "NEXT")))
753         subtree-end)
754        (t
755         nil)))))
756
757 (defun bh/skip-project-tasks ()
758   "Show non-project tasks.
759 Skip project and sub-project tasks, habits, and project related tasks."
760   (save-restriction
761     (widen)
762     (let* ((subtree-end (save-excursion (org-end-of-subtree t))))
763       (cond
764        ((bh/is-project-p)
765         subtree-end)
766        ((org-is-habit-p)
767         subtree-end)
768        ((bh/is-project-subtree-p)
769         subtree-end)
770        (t
771         nil)))))
772
773 (defun bh/skip-non-project-tasks ()
774   "Show project tasks.
775 Skip project and sub-project tasks, habits, and loose non-project tasks."
776   (save-restriction
777     (widen)
778     (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
779            (next-headline (save-excursion (or (outline-next-heading) (point-max)))))
780       (cond
781        ((bh/is-project-p)
782         next-headline)
783        ((org-is-habit-p)
784         subtree-end)
785        ((and (bh/is-project-subtree-p)
786              (member (org-get-todo-state) (list "NEXT")))
787         subtree-end)
788        ((not (bh/is-project-subtree-p))
789         subtree-end)
790        (t
791         nil)))))
792
793 (defun bh/skip-projects-and-habits ()
794   "Skip trees that are projects and tasks that are habits"
795   (save-restriction
796     (widen)
797     (let ((subtree-end (save-excursion (org-end-of-subtree t))))
798       (cond
799        ((bh/is-project-p)
800         subtree-end)
801        ((org-is-habit-p)
802         subtree-end)
803        (t
804         nil)))))
805
806 (defun bh/skip-non-subprojects ()
807   "Skip trees that are not projects"
808   (let ((next-headline (save-excursion (outline-next-heading))))
809     (if (bh/is-subproject-p)
810         nil
811       next-headline)))
812 ;
813 ;; Resume clocking task when emacs is restarted
814 (org-clock-persistence-insinuate)
815 ;;
816 ;; Show lot of clocking history so it's easy to pick items off the C-F11 list
817 (setq org-clock-history-length 23)
818 ;; Resume clocking task on clock-in if the clock is open
819 (setq org-clock-in-resume t)
820 ;; Change tasks to NEXT when clocking in
821 (setq org-clock-in-switch-to-state 'bh/clock-in-to-next)
822 ;; Separate drawers for clocking and logs
823 (setq org-drawers (quote ("PROPERTIES" "LOGBOOK")))
824 ;; Save clock data and state changes and notes in the LOGBOOK drawer
825 (setq org-clock-into-drawer t)
826 (setq org-log-into-drawer t)
827 ;; Sometimes I change tasks I'm clocking quickly - this removes clocked tasks with 0:00 duration
828 (setq org-clock-out-remove-zero-time-clocks t)
829 ;; Clock out when moving task to a done state
830 (setq org-clock-out-when-done t)
831 ;; Save the running clock and all clock history when exiting Emacs, load it on startup
832 (setq org-clock-persist t)
833 ;; Do not prompt to resume an active clock
834 (setq org-clock-persist-query-resume nil)
835 ;; Enable auto clock resolution for finding open clocks
836 (setq org-clock-auto-clock-resolution (quote when-no-clock-is-running))
837 ;; Include current clocking task in clock reports
838 (setq org-clock-report-include-clocking-task t)
839
840
841 (defvar bh/keep-clock-running nil)
842
843 (defun bh/clock-in-to-next (kw)
844   "Switch a task from TODO to NEXT when clocking in.
845 Skips capture tasks, projects, and subprojects.
846 Switch projects and subprojects from NEXT back to TODO"
847   (when (not (and (boundp 'org-capture-mode) org-capture-mode))
848     (cond
849      ((and (member (org-get-todo-state) (list "TODO"))
850            (bh/is-task-p))
851       "NEXT")
852      ((and (member (org-get-todo-state) (list "NEXT"))
853            (bh/is-project-p))
854       "TODO"))))
855
856 (defun bh/find-project-task ()
857   "Move point to the parent (project) task if any"
858   (save-restriction
859     (widen)
860     (let ((parent-task (save-excursion (org-back-to-heading 'invisible-ok) (point))))
861       (while (org-up-heading-safe)
862         (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
863           (setq parent-task (point))))
864       (goto-char parent-task)
865       parent-task)))
866
867 (defun bh/punch-in (arg)
868   "Start continuous clocking and set the default task to the
869 selected task.  If no task is selected set the Organization task
870 as the default task."
871   (interactive "p")
872   (setq bh/keep-clock-running t)
873   (if (equal major-mode 'org-agenda-mode)
874       ;;
875       ;; We're in the agenda
876       ;;
877       (let* ((marker (org-get-at-bol 'org-hd-marker))
878              (tags (org-with-point-at marker (org-get-tags-at))))
879         (if (and (eq arg 4) tags)
880             (org-agenda-clock-in '(16))
881           (bh/clock-in-organization-task-as-default)))
882     ;;
883     ;; We are not in the agenda
884     ;;
885     (save-restriction
886       (widen)
887       ; Find the tags on the current task
888       (if (and (equal major-mode 'org-mode) (not (org-before-first-heading-p)) (eq arg 4))
889           (org-clock-in '(16))
890         (bh/clock-in-organization-task-as-default)))))
891
892 (defun bh/punch-out ()
893   (interactive)
894   (setq bh/keep-clock-running nil)
895   (when (org-clock-is-active)
896     (org-clock-out))
897   (org-agenda-remove-restriction-lock))
898
899 (defun bh/clock-in-default-task ()
900   (save-excursion
901     (org-with-point-at org-clock-default-task
902       (org-clock-in))))
903
904 (defun bh/clock-in-parent-task ()
905   "Move point to the parent (project) task if any and clock in"
906   (let ((parent-task))
907     (save-excursion
908       (save-restriction
909         (widen)
910         (while (and (not parent-task) (org-up-heading-safe))
911           (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
912             (setq parent-task (point))))
913         (if parent-task
914             (org-with-point-at parent-task
915               (org-clock-in))
916           (when bh/keep-clock-running
917             (bh/clock-in-default-task)))))))
918
919 (defvar bh/organization-task-id "e22cb8bf-07c7-408b-8f60-ff3aadac95e4")
920
921 (defun bh/clock-in-organization-task-as-default ()
922   (interactive)
923   (org-with-point-at (org-id-find bh/organization-task-id 'marker)
924     (org-clock-in '(16))))
925
926 (defun bh/clock-out-maybe ()
927   (when (and bh/keep-clock-running
928              (not org-clock-clocking-in)
929              (marker-buffer org-clock-default-task)
930              (not org-clock-resolving-clocks-due-to-idleness))
931     (bh/clock-in-parent-task)))
932
933 (add-hook 'org-clock-out-hook 'bh/clock-out-maybe 'append)
934
935 (require 'org-id)
936 (defun bh/clock-in-task-by-id (id)
937   "Clock in a task by id"
938   (org-with-point-at (org-id-find id 'marker)
939     (org-clock-in nil)))
940
941 (defun bh/clock-in-last-task (arg)
942   "Clock in the interrupted task if there is one
943 Skip the default task and get the next one.
944 A prefix arg forces clock in of the default task."
945   (interactive "p")
946   (let ((clock-in-to-task
947          (cond
948           ((eq arg 4) org-clock-default-task)
949           ((and (org-clock-is-active)
950                 (equal org-clock-default-task (cadr org-clock-history)))
951            (caddr org-clock-history))
952           ((org-clock-is-active) (cadr org-clock-history))
953           ((equal org-clock-default-task (car org-clock-history)) (cadr org-clock-history))
954           (t (car org-clock-history)))))
955     (widen)
956     (org-with-point-at clock-in-to-task
957       (org-clock-in nil))))
958
959
960 ; allow for zero-width-space to be a break in regexp too
961 (setcar org-emphasis-regexp-components "​ [:space:] \t('\"{")
962 (setcar (nthcdr 1 org-emphasis-regexp-components) "​ [:space:]- \t.,:!?;'\")}\\")
963 (org-set-emph-re 'org-emphasis-regexp-components org-emphasis-regexp-components)