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