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