]> git.donarmstrong.com Git - lib.git/blob - emacs_el/configuration/don-configuration.org
2f3d1ce282300936955489f6984ff358fc72092e
[lib.git] / emacs_el / configuration / don-configuration.org
1 #+PROPERTY: header-args:emacs-lisp :tangle don-configuration.el
2 * Load debugger
3
4 # if for some reason, things get pear-shaped, we want to be able to
5 # enter the debugger by sending -USR2 to emacs
6
7 #+BEGIN_SRC emacs-lisp
8 (setq debug-on-event 'siguser2)
9 #+END_SRC
10 * Initial startup stuff
11 ** Disable startup screen
12 #+BEGIN_SRC emacs-lisp
13   (setq inhibit-startup-screen t)
14 #+END_SRC
15 ** Disable cluter
16 #+BEGIN_SRC emacs-lisp
17   ; (if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
18   (if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
19   (if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
20 #+END_SRC
21 ** Fullscreen
22 #+BEGIN_SRC emacs-lisp
23   (setq frame-resize-pixelwise t)
24   (add-to-list 'default-frame-alist '(fullscreen . maximixed))
25 #+END_SRC
26 * Package management
27 ** package repositories and package manager
28 Borrowed from https://github.com/nilcons/emacs-use-package-fast/ to
29 load  [[https://github.com/jwiegley/use-package/][use-package]] even faster
30 #+BEGIN_SRC emacs-lisp
31   (setq package-enable-at-startup nil)
32   (setq package--init-file-ensured t)
33   (eval-and-compile
34     (setq use-package-verbose (not (bound-and-true-p byte-compile-current-file))))
35   (mapc #'(lambda (add) (add-to-list 'load-path add))
36     (eval-when-compile
37       (package-initialize)
38       (unless (package-installed-p 'use-package)
39         (package-refresh-contents)
40         (package-install 'use-package))
41       (let ((package-user-dir-real (file-truename package-user-dir)))
42         ;; The reverse is necessary, because outside we mapc
43         ;; add-to-list element-by-element, which reverses.
44         (nreverse (apply #'nconc
45                  ;; Only keep package.el provided loadpaths.
46                  (mapcar #'(lambda (path)
47                      (if (string-prefix-p package-user-dir-real path)
48                          (list path)
49                        nil))
50                      load-path))))))
51
52   (setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
53                ("melpa" . "https://melpa.org/packages/")
54                ("org" . "http://orgmode.org/elpa/") ))
55   (require 'use-package)
56   (require 'diminish)
57   (require 'bind-key)
58 #+END_SRC
59 ** Paradox
60 #+BEGIN_SRC emacs-lisp
61   (use-package paradox
62     :ensure paradox
63     :commands (paradox-upgrade-packages paradox-list-packages)
64     :config (setq paradox-execute-asynchronously t)
65     )
66 #+END_SRC
67 * Add library paths
68
69 #+BEGIN_SRC emacs-lisp
70   (add-to-list 'load-path '"~/lib/emacs_el/")
71   (add-to-list 'load-path '"~/lib/emacs_el/magit-annex")
72 #+END_SRC
73 * Disable custom-vars
74 #+BEGIN_SRC emacs-lisp
75   ;; Set the custom file to /dev/null and don't bother to load it
76   (setq custom-file "/dev/null")
77 #+END_SRC
78 * Misc functions
79 ** with-library
80 #+BEGIN_SRC emacs-lisp
81 ;; From http://www.emacswiki.org/emacs/LoadingLispFiles
82 ;; execute conditional code when loading libraries
83 (defmacro with-library (symbol &rest body)
84   `(when (require ,symbol nil t)
85      ,@body))
86 (put 'with-library 'lisp-indent-function 1)
87 #+END_SRC
88
89 * Variables
90 ** Safe Local Variables
91 #+BEGIN_SRC emacs-lisp
92   (setq safe-local-variable-values 
93         (quote ((auto-save-default)
94                 (make-backup-files)
95                 (cperl-indent-level . 4)
96                 (indent-level . 4)
97                 (indent-tabs-mode . f)
98                 )))
99 #+END_SRC
100 * Memory
101 #+BEGIN_SRC emacs-lisp
102   (setq global-mark-ring-max 128
103         mark-ring-max 128
104         kill-ring-max 128)
105
106   (defun don/minibuffer-setup-hook ()
107     (setq gc-cons-threshold most-positive-fixnum))
108
109   (defun don/minibuffer-exit-hook ()
110     (setq gc-cons-threshold 1048576))
111
112   (add-hook 'minibuffer-setup-hook #'don/minibuffer-setup-hook)
113   (add-hook 'minibuffer-exit-hook #'don/minibuffer-exit-hook)
114 #+END_SRC
115 * Modules
116 ** Flyspell 🐝 
117 #+BEGIN_SRC emacs-lisp
118   (use-package flyspell
119     :ensure t
120     :diminish flyspell-mode 🐝
121     :config
122     (add-hook 'text-mode-hook 'turn-on-flyspell)
123     (add-hook 'c-mode-common-hook 'flyspell-prog-mode)
124     (add-hook 'cperl-mode-hook 'flyspell-prog-mode)
125     (add-hook 'tcl-mode-hook 'flyspell-prog-mode)
126     :init
127     (setq ispell-program-name "ispell")
128     )
129
130 #+END_SRC
131 ** Winnermode
132 #+BEGIN_SRC emacs-lisp
133   (winner-mode 1)
134 #+END_SRC
135 ** Eyebrowse
136
137 #+BEGIN_SRC emacs-lisp
138   ;; (use-package eyebrowse
139   ;;   :ensure t
140   ;;   :diminish eyebrowse-mode
141   ;;   :init (setq eyebrowse-keymap-prefix (kbd "C-c C-\\"))
142   ;;   :config (progn
143   ;;             (setq eyebrowse-wrap-around t)
144   ;;             (eyebrowse-mode t)
145   ;; 
146   ;;             (defun my/eyebrowse-new-window-config ()
147   ;;               (interactive)
148   ;;               (let ((done nil))
149   ;;                 (dotimes (i 10)
150   ;;                   ;; start at 1 run till 0
151   ;;                   (let ((j (mod (+ i 1) 10)))
152   ;;                     (when (and (not done)
153   ;;                                (not (eyebrowse--window-config-present-p j)))
154   ;;                       (eyebrowse-switch-to-window-config j)
155   ;;                       (call-interactively 'eyebrowse-rename-window-config2 j)
156   ;;                       (setq done t)
157   ;;                       ))
158   ;;                   )))
159   ;; 
160   ;;             ;; I don't use latex-preview-pane
161   ;;             ;; (require 'latex-preview-pane)
162   ;;             ;; (defun my/close-latex-preview-pane-before-eyebrowse-switch ()
163   ;;             ;;   ;; latex-preview-pane uses window-parameters which are
164   ;;             ;;   ;; not preserved by eyebrowse, so we close the preview
165   ;;             ;;   ;; pane before switching, it will be regenerated when we
166   ;;             ;;   ;; edit the TeX file.
167   ;;             ;;   (when (lpp/window-containing-preview)
168   ;;             ;;     (delete-window (lpp/window-containing-preview))))
169   ;; 
170   ;;             ;; (add-to-list 'eyebrowse-pre-window-switch-hook
171   ;;             ;;              #'my/close-latex-preview-pane-before-eyebrowse-switch)
172   ;; 
173   ;;             ;; (my/set-menu-key "["  #'my/eyebrowse-new-window-config)
174   ;;             ;; (my/set-menu-key ";"  #'eyebrowse-prev-window-config)
175   ;;             ;; (my/set-menu-key "'"  #'eyebrowse-next-window-config)
176   ;;             ;; (my/set-menu-key "]"  #'eyebrowse-close-window-config)
177   ;;             ;; (my/set-menu-key "\\" #'eyebrowse-rename-window-config)
178   ;;             )
179   ;;   )
180 #+END_SRC
181
182 ** Window handling
183
184 *** Splitting
185 #+BEGIN_SRC emacs-lisp
186   (defun my/vsplit-last-buffer ()
187     "Split the window vertically and display the previous buffer."
188     (interactive)
189     (split-window-vertically)
190     (other-window 1 nil)
191     (switch-to-next-buffer))
192
193   (defun my/hsplit-last-buffer ()
194     "Split the window horizontally and display the previous buffer."
195     (interactive)
196     (split-window-horizontally)
197     (other-window 1 nil)
198     (switch-to-next-buffer))
199
200   (bind-key "C-x 2" 'my/vsplit-last-buffer)
201   (bind-key "C-x 3" 'my/hsplit-last-buffer)
202
203   (setq split-width-threshold  100)
204   (setq split-height-threshold 60)
205
206   (defun my/split-window-prefer-vertically (window)
207     "If there's only one window (excluding any possibly active
208            minibuffer), then split the current window horizontally."
209     (if (and (one-window-p t)
210              (not (active-minibuffer-window))
211              ( < (frame-width) (frame-height))
212              )
213         (let ((split-width-threshold nil))
214           (split-window-sensibly window))
215       (split-window-sensibly window)))
216
217   (setq split-window-preferred-function #'my/split-window-prefer-vertically)
218   (setq window-combination-resize t)
219 #+END_SRC
220
221 *** Compilation window
222
223 If there is no compilation window, open one at the bottom, spanning
224 the complete width of the frame. Otherwise, reuse existing window. In
225 the former case, if there was no error the window closes
226 automatically.
227
228 #+BEGIN_SRC emacs-lisp
229   (add-to-list 'display-buffer-alist
230                `(,(rx bos "*compilation*" eos)
231                  (display-buffer-reuse-window
232                   display-buffer-in-side-window)
233                  (reusable-frames . visible)
234                  (side            . bottom)
235                  (window-height   . 0.4)))
236 #+END_SRC
237
238 #+BEGIN_SRC emacs-lisp
239   (defun my/compilation-exit-autoclose (status code msg)
240     ;; If M-x compile exists with a 0
241     (when (and (eq status 'exit) (zerop code))
242       ;; and delete the *compilation* window
243       (let ((compilation-window (get-buffer-window (get-buffer "*compilation*"))))
244         (when (and (not (window-at-side-p compilation-window 'top))
245                    (window-at-side-p compilation-window 'left)
246                    (window-at-side-p compilation-window 'right))
247           (delete-window compilation-window))))
248     ;; Always return the anticipated result of compilation-exit-message-function
249     (cons msg code))
250
251   ;; Specify my function (maybe I should have done a lambda function)
252   (setq compilation-exit-message-function #'my/compilation-exit-autoclose)
253 #+END_SRC
254
255 If you change the variable ~compilation-scroll-output~ to a ~non-nil~
256 value, the compilation buffer scrolls automatically to follow the
257 output. If the value is ~first-error~, scrolling stops when the first
258 error appears, leaving point at that error. For any other non-nil
259 value, scrolling continues until there is no more output.
260
261 #+BEGIN_SRC emacs-lisp
262   (setq compilation-scroll-output 'first-error)
263 #+END_SRC
264
265 ** Mode line cleaning
266 *** Diminish
267 #+BEGIN_SRC emacs-lisp
268   (use-package diminish
269     :ensure t)
270 #+END_SRC
271
272 *** Delight 
273 #+BEGIN_SRC emacs-lisp
274   (use-package delight
275     :ensure t)
276 #+END_SRC
277
278 ** Jumping
279 *** Avy
280 #+BEGIN_SRC emacs-lisp
281   (use-package avy
282     :ensure t
283     :bind (("C-c C-<SPC>" . avy-goto-word-or-subword-1)
284            ("C-c j j" . avy-goto-word-or-subword-1)
285            ("M-g g" . avy-goto-line))
286     :config (progn (setq avy-background t))
287     )
288 #+END_SRC
289 *** Ace-link (jumping to links)
290 #+BEGIN_SRC emacs-lisp
291   (use-package ace-link
292     :ensure t
293     ; bind o in most modes
294     :config (ace-link-setup-default))
295 #+END_SRC
296 *** Jumping through edit points (goto-chg)
297 #+BEGIN_SRC emacs-lisp
298   (use-package goto-chg
299     :ensure t
300     :bind (("C-c j ," . goto-last-change)
301            ("C-c j ." . goto-last-change-reverse))
302     )
303 #+END_SRC
304 *** Jumping to bookmarks (visible bookmarks, bm)
305 #+BEGIN_SRC emacs-lisp
306   (use-package bm
307     :ensure t
308     :bind (("C-c j b ." . bm-next)
309            ("C-c j b ," . bm-previous)
310            ("C-c j b SPC" . bm-toggle)))
311 #+END_SRC
312
313 ** Snippets
314 *** Yasnippet
315 #+BEGIN_SRC emacs-lisp
316   (use-package yasnippet
317     :ensure t
318     :diminish yas-minor-mode
319     :config (progn
320               (yas-global-mode)
321               (setq yas-verbosity 1)
322               (define-key yas-minor-mode-map (kbd "<tab>") nil)
323               (define-key yas-minor-mode-map (kbd "TAB") nil)
324               (define-key yas-minor-mode-map (kbd "<backtab>") 'yas-expand)
325               (setq yas-snippet-dirs '("~/lib/emacs_el/snippets/"
326                                        "~/lib/emacs_el/yasnippet-snippets/snippets/"))
327               (add-to-list 'hippie-expand-try-functions-list
328                                'yas-hippie-try-expand)
329               )
330     )
331 #+END_SRC
332 *** Auto-YASnippet
333 #+BEGIN_SRC emacs-lisp
334   (use-package auto-yasnippet
335     :bind (("H-w" . aya-create)
336            ("H-y" . aya-expand)
337            )
338     )
339 #+END_SRC
340 ** Tinyprocmail
341
342 #+BEGIN_SRC emacs-lisp
343   ;; load tinyprocmail
344   (use-package tinyprocmail
345     :ensure f
346     :config (with-library 'tinyprocmail
347               ;; (setq tinyprocmail--procmail-version "v3.22")
348               (add-hook 'tinyprocmail--load-hook 'tinyprocmail-install))
349   )
350 #+END_SRC
351
352 ** Magit
353 #+BEGIN_SRC emacs-lisp :tangle don-configuration.el
354   (use-package magit
355     :ensure t
356     :bind (("C-x g" . magit-status)
357            ("C-x C-g" . magit-status))
358     :config
359     ;; don't verify where we are pushing
360     (setq magit-push-always-verify nil)
361     ;; refine diffs always (hilight words)
362     (setq magit-diff-refine-hunk nil)
363     ;; load magit-annex
364     (setq load-path
365           (append '("~/lib/emacs_el/magit-annex")
366                   load-path))
367     ;; load magit-vcsh
368     (setq load-path
369           (append '("~/lib/emacs_el/magit-vcsh")
370                   load-path))
371     )
372   (use-package magit-annex
373     :ensure t
374   )
375   (use-package magit-vcsh
376     :ensure f ; currently not in melpa, so don't try to install
377   )
378 #+END_SRC
379
380 ** Perl
381 #+BEGIN_SRC emacs-lisp
382   (use-package cperl-mode
383     :config
384     (progn
385       ;; Use c-mode for perl .xs files
386       (add-to-list 'auto-mode-alist '("\\.xs\\'" . c-mode))
387       (add-to-list 'auto-mode-alist '("\\.\\([pP][Llm]\\|al\\)\\'" . cperl-mode))
388       (add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
389       (add-to-list 'interpreter-mode-alist '("perl5" . cperl-mode))
390       (add-to-list 'interpreter-mode-alist '("miniperl" . cperl-mode))
391       (setq cperl-hairy t
392             cperl-indent-level 4
393             cperl-auto-newline nil
394             cperl-auto-newline-after-colon nil
395             cperl-continued-statement-offset 4
396             cperl-brace-offset -1
397             cperl-continued-brace-offset 0
398             cperl-label-offset -4
399             cperl-highlight-variables-indiscriminately t
400             cperl-electric-lbrace-space nil
401             cperl-indent-parens-as-block nil
402             cperl-close-paren-offset -1
403             cperl-tab-always-indent t)
404       ;;(add-hook 'cperl-mode-hook (lambda () (cperl-set-style "PerlStyle")))
405   ))
406 #+END_SRC
407
408 ** Helm
409 #+BEGIN_SRC emacs-lisp
410   (use-package helm
411     :ensure t
412     :config
413     (helm-mode 1)
414     (define-key global-map [remap find-file] 'helm-find-files)
415     (define-key global-map [remap occur] 'helm-occur)
416     (define-key global-map [remap list-buffers] 'helm-buffers-list)
417     (define-key global-map [remap dabbrev-expand] 'helm-dabbrev)
418     (global-set-key (kbd "M-x") 'helm-M-x)
419     (unless (boundp 'completion-in-region-function)
420       (define-key lisp-interaction-mode-map [remap completion-at-point] 'helm-lisp-completion-at-point)
421       (define-key emacs-lisp-mode-map       [remap completion-at-point] 'helm-lisp-completion-at-point))
422     (add-hook 'kill-emacs-hook #'(lambda () (and (file-exists-p "$TMP") (delete-file "$TMP"))))
423   )
424 #+END_SRC
425 *** Helm Flx
426
427  [[https://github.com/PythonNut/helm-flx][helm-flx]] implements intelligent helm fuzzy sorting, provided by [[https://github.com/lewang/flx][flx]].
428
429  #+BEGIN_SRC emacs-lisp
430  (use-package helm-flx
431    :ensure t
432    :config (progn
433              ;; these are helm configs, but they kind of fit here nicely
434              (setq helm-M-x-fuzzy-match                  t
435                    helm-bookmark-show-location           t
436                    helm-buffers-fuzzy-matching           t
437                    helm-completion-in-region-fuzzy-match t
438                    helm-file-cache-fuzzy-match           t
439                    helm-imenu-fuzzy-match                t
440                    helm-mode-fuzzy-match                 t
441                    helm-locate-fuzzy-match               nil
442                    helm-quick-update                     t
443                    helm-recentf-fuzzy-match              nil
444                    helm-semantic-fuzzy-match             t)
445              (helm-flx-mode +1)))
446  #+END_SRC
447 *** Helm Swoop
448 #+BEGIN_SRC emacs-lisp
449
450   ;;; stolen from https://github.com/malb/emacs.d/blob/master/malb.org
451   (defun malb/helm-swoop-pre-fill ()
452     (thing-at-point 'symbol))
453   (setq malb/helm-swoop-ignore-major-mode '(dired-mode
454         paradox-menu-mode doc-view-mode pdf-view-mode
455         mu4e-headers-mode org-mode markdown-mode latex-mode
456         ein:notebook-multilang-mode))
457
458   (defun malb/swoop-or-search ()
459     (interactive)
460     (if (or (> (buffer-size) 1048576) ;; helm-swoop can be slow on big buffers
461             (memq major-mode malb/helm-swoop-ignore-major-mode))
462         (isearch-forward)
463       (helm-swoop)))
464
465   (use-package helm-swoop
466     :ensure t
467     :bind (("C-c o" . helm-multi-swoop-org)
468            ("C-s" . malb/swoop-or-search)
469            ("C-M-s" . helm-multi-swoop-all))
470     :config (progn
471             
472               (setq helm-swoop-pre-input-function  #'malb/helm-swoop-pre-fill
473                     helm-swoop-split-with-multiple-windows nil
474                     helm-swoop-split-direction #'split-window-horizontally
475                     helm-swoop-split-window-function 'helm-default-display-buffer
476                     helm-swoop-speed-or-color t)
477
478               ;; https://emacs.stackexchange.com/questions/28790/helm-swoop-how-to-make-it-behave-more-like-isearch
479               (defun malb/helm-swoop-C-s ()
480                 (interactive)
481                 (if (boundp 'helm-swoop-pattern)
482                     (if (equal helm-swoop-pattern "")
483                         (previous-history-element 1)
484                       (helm-next-line))
485                   (helm-next-line)))
486
487               (bind-key "C-S-s" #'helm-swoop-from-isearch isearch-mode-map)
488               (bind-key "C-S-s" #'helm-multi-swoop-all-from-helm-swoop helm-swoop-map)
489               (bind-key "C-r"   #'helm-previous-line helm-swoop-map)
490               (bind-key "C-s"   #'malb/helm-swoop-C-s helm-swoop-map)
491               (bind-key "C-r"   #'helm-previous-line helm-multi-swoop-map)
492               (bind-key "C-s"   #'malb/helm-swoop-C-s helm-multi-swoop-map))
493     )
494   
495 #+END_SRC
496 *** Helm Ag
497 #+BEGIN_SRC emacs-lisp
498 (use-package helm-ag
499   :ensure t
500   :config (setq helm-ag-base-command "ag --nocolor --nogroup"
501                 helm-ag-command-option "--all-text"
502                 helm-ag-insert-at-point 'symbol
503                 helm-ag-fuzzy-match t
504                 helm-ag-use-temp-buffer t
505                 helm-ag-use-grep-ignore-list t
506                 helm-ag-use-agignore t))
507 #+END_SRC
508 *** Helm Descbinds
509 #+BEGIN_SRC emacs-lisp
510   (use-package helm-descbinds
511     :ensure t
512     :bind ("C-h b" . helm-descbinds)
513     :init (fset 'describe-bindings 'helm-descbinds))
514 #+END_SRC
515
516 *** Helm YaSnippet
517 #+BEGIN_SRC emacs-lisp
518   (use-package helm-c-yasnippet
519     :ensure t
520     :bind ("C-c h y" .  helm-yas-complete)
521     :config (progn
522               (setq helm-yas-space-match-any-greedy t)))
523 #+END_SRC
524 *** Helm Org Rifle
525 #+BEGIN_SRC emacs-lisp
526   (use-package helm-org-rifle
527     :ensure t
528     :config (progn
529               (defun malb/helm-org-rifle-agenda-files (arg)
530                 (interactive "p")
531                 (let ((current-prefix-arg nil))
532                   (cond
533                    ((equal arg 4) (call-interactively #'helm-org-rifle-agenda-files nil))
534                    ((equal arg 16) (helm-org-rifle-occur-agenda-files))
535                    (t (helm-org-agenda-files-headings)))))))
536 #+END_SRC
537 *** Helm Google
538 This can be used to link things pretty quickly if necessary
539 #+BEGIN_SRC emacs-lisp
540   (use-package helm-google
541     :ensure t
542     :bind ("C-c h g" . helm-google)
543     :config
544     (progn (add-to-list 'helm-google-actions
545                         '("Copy URL" . (lambda (candidate)
546                                          (let ((url
547                                                 (replace-regexp-in-string
548                                                  "https://.*q=\\(.*\\)\&sa=.*"
549                                                  "\\1" candidate)))
550                                            (kill-new url))))
551                         t
552                         )
553          
554            (add-to-list 'helm-google-actions
555                         '("Org Store Link" . (lambda (candidate)
556                                                (let ((title (car (split-string candidate "[\n]+")))
557                                                      (url
558                                                       (replace-regexp-in-string
559                                                        "https://.*q=\\(.*\\)\&sa=.*"
560                                                        "\\1" candidate)))
561                                                  (push (list url title) org-stored-links))))
562                         t)
563            ))
564 #+END_SRC
565 ** Zap to char
566 #+BEGIN_SRC emacs-lisp
567   (use-package avy-zap
568     :ensure t
569     :bind ("M-z" . avy-zap-up-to-char-dwim))
570 #+END_SRC
571 ** Hydra
572 #+BEGIN_SRC emacs-lisp :tangle don-configuration.el
573 (require 'don-hydra)
574 #+END_SRC
575
576 ** Tramp
577 #+BEGIN_SRC emacs-lisp
578   (add-to-list 'tramp-methods '("vcsh"
579                                 (tramp-login-program "vcsh")
580                                 (tramp-login-args
581                                  (("enter")
582                                   ("%h")))
583                                 (tramp-remote-shell "/bin/sh")
584                                 (tramp-remote-shell-args
585                                  ("-c"))))
586 #+END_SRC
587 ** Reftex
588 #+BEGIN_SRC emacs-lisp
589   (use-package reftex
590     :ensure t
591     :config
592     (setq-default reftex-default-bibliography
593                     '("~/projects/research/references.bib")))
594 #+END_SRC
595 ** BibTex
596 #+BEGIN_SRC emacs-lisp
597   (use-package bibtex
598     :config (setq bibtex-user-optional-fields
599                   (quote (("annote" "Personal annotation (ignored)")
600                           ("abstract" "")
601                   ("pmid" "")
602                   ("doi" ""))))
603     )
604
605 #+END_SRC
606 ** LaTeX
607 #+BEGIN_SRC emacs-lisp
608   (use-package tex
609     :defer t
610     :ensure auctex
611     :config
612     ; (add-to-list 'TeX-style-path '"/home/don/lib/emacs_el/auctex/style")
613     ;; REFTEX (much enhanced management of cross-ref, labels, etc)
614     ;; http://www.strw.leidenuniv.nl/~dominik/Tools/reftex/
615     ; (autoload 'reftex-mode     "reftex" "RefTeX Minor Mode" t)
616     ; (autoload 'turn-on-reftex  "reftex" "RefTeX Minor Mode" nil)
617     ; (autoload 'reftex-citation "reftex-cite" "Make citation" nil)
618     ; (autoload 'reftex-index-phrase-mode "reftex-index" "Phrase mode" t)
619     (add-hook 'LaTeX-mode-hook 'turn-on-reftex)   ; with AUCTeX LaTeX mode
620     (add-hook 'latex-mode-hook 'turn-on-reftex)   ; with Emacs latex mode
621     (add-hook 'LaTeX-mode-hook 'outline-minor-mode)   ; with AUCTeX LaTeX mode
622     (add-hook 'latex-mode-hook 'outline-minor-mode)   ; with Emacs latex mode
623
624     (setq-default reftex-plug-into-AUCTeX t)
625     ;; support fake section headers
626     (setq TeX-outline-extra
627           '(("%chapter" 1)
628             ("%section" 2)
629             ("%subsection" 3)
630             ("%subsubsection" 4)
631             ("%paragraph" 5)))
632     ;; add font locking to the headers
633     (font-lock-add-keywords
634      'latex-mode
635      '(("^%\\(chapter\\|\\(sub\\|subsub\\)?section\\|paragraph\\)"
636         0 'font-lock-keyword-face t)
637        ("^%chapter{\\(.*\\)}"       1 'font-latex-sectioning-1-face t)
638        ("^%section{\\(.*\\)}"       1 'font-latex-sectioning-2-face t)
639        ("^%subsection{\\(.*\\)}"    1 'font-latex-sectioning-3-face t)
640        ("^%subsubsection{\\(.*\\)}" 1 'font-latex-sectioning-4-face t)
641        ("^%paragraph{\\(.*\\)}"     1 'font-latex-sectioning-5-face t)))
642
643     ;; use smart quotes by default instead of `` and ''
644     ;; taken from http://kieranhealy.org/esk/kjhealy.html
645     (setq TeX-open-quote "“")
646     (setq TeX-close-quote "”")
647
648     ;; (TeX-add-style-hook
649     ;;  "latex"
650     ;;  (lambda ()
651     ;;    (TeX-add-symbols
652     ;;     '("DLA" 1))))
653     ;; (custom-set-variables
654     ;;  '(font-latex-user-keyword-classes 
655     ;;    '(("fixme" 
656     ;;       ("DLA" "RZ")
657     ;;       font-lock-function-name-face 2 (command 1 t))))
658     ;; ) 
659     (setq-default TeX-parse-self t)
660     (setq-default TeX-auto-save t)
661     (setq-default TeX-master nil)
662     (eval-after-load
663         "latex"
664       '(TeX-add-style-hook
665         "cleveref"
666         (lambda ()
667           (if (boundp 'reftex-ref-style-alist)
668               (add-to-list
669                'reftex-ref-style-alist
670                '("Cleveref" "cleveref"
671                  (("\\cref" ?c) ("\\Cref" ?C) ("\\cpageref" ?d) ("\\Cpageref" ?D)))))
672           (reftex-ref-style-activate "Cleveref")
673           (TeX-add-symbols
674            '("cref" TeX-arg-ref)
675            '("Cref" TeX-arg-ref)
676            '("cpageref" TeX-arg-ref)
677            '("Cpageref" TeX-arg-ref)))))
678     (eval-after-load
679         "latex"
680       '(add-to-list 'LaTeX-fill-excluded-macros
681                     '("Sexpr")))
682
683     (use-package font-latex
684       :config
685       (setq font-latex-match-reference-keywords
686             '(
687               ("fref" "{")
688               ("Fref" "{")
689               ("citep" "{")
690               ("citet" "{")
691               ("acs" "{")
692               ("acsp" "{")
693               ("ac" "{")
694               ("acp" "{")
695               ("acl" "{")
696               ("aclp" "{")
697               ("acsu" "{")
698               ("aclu" "{")
699               ("acused" "{")
700               ("DLA" "{")
701               ("RZ" "{")
702               ("OM" "{")
703               ("DL" "{")
704               ("fixme" "{"))
705             )
706       )
707     (setq font-latex-fontify-script nil)
708     (setq font-latex-fontify-sectioning (quote color))
709     (setq font-latex-script-display (quote (nil)))
710   )
711
712 #+END_SRC
713 ** ESS
714 #+BEGIN_SRC emacs-lisp
715   (use-package ess
716     :ensure t
717     :config
718     (defun ess-change-directory (path)
719       "Set the current working directory to PATH for both *R* and Emacs."
720       (interactive "DDirectory to change to: ")
721     
722       (when (file-exists-p path)
723         (ess-command (concat "setwd(\"" path "\")\n"))
724         ;; use file-name-as-directory to ensure it has trailing /
725         (setq default-directory (file-name-as-directory path))))
726     (add-hook 'ess-mode-hook 'flyspell-prog-mode)
727     ;; outlining support for ess modes
728     (add-hook
729      'ess-mode-hook
730      '(lambda ()
731         (outline-minor-mode)
732         (setq outline-regexp "\\(^#\\{4,5\\} \\)\\|\\(^[a-zA-Z0-9_\.]+ ?<- ?function\\)")
733         (defun outline-level ()
734           (cond ((looking-at "^##### ") 1)
735                 ((looking-at "^#### ") 2)
736                 ((looking-at "^[a-zA-Z0-9_\.]+ ?<- ?function(.*{") 3)
737                 (t 1000)))
738         ))
739     (add-hook 'ess-mode-hook
740               '(lambda ()
741                  (local-set-key (kbd "C-c C-R")
742                                 'dla/ess-region-remote-eval)))
743
744     ;; Don't restore history or save workspace image
745     '(inferior-R-args "--no-restore-history --no-save")
746     )
747 #+END_SRC
748
749 ** Rainbowmode
750 From http://julien.danjou.info/projects/emacs-packages#rainbow-mode, this colorizes color strings
751
752 #+BEGIN_SRC emacs-lisp
753   (use-package rainbow-mode
754     ;; add ess to the x major mode
755     :config (add-to-list 'rainbow-x-colors-major-mode-list 'ESS[S])
756     (add-to-list 'rainbow-x-colors-major-mode-list 'ESS[R])
757   )
758 #+END_SRC
759
760 ** Polymode
761 #+BEGIN_SRC emacs-lisp
762   (use-package polymode
763     :config
764     (use-package poly-R)
765     (use-package poly-noweb)
766     (use-package poly-markdown)
767     :mode ("\\.Snw" . poly-noweb+r-mode)
768     :mode ("\\.Rnw" . poly-noweb+r-mode)
769     :mode ("\\.Rmd" . poly-markdown+r-mode)
770     )
771 #+END_SRC
772
773 ** Outlining
774 *** Outline magic
775 #+BEGIN_SRC emacs-lisp
776   (use-package outline-magic)
777 #+END_SRC
778 ** Writeroom Mode
779 #+BEGIN_SRC emacs-lisp
780   (use-package writeroom-mode
781     :config
782     (defun my/writing-mode ()
783       "Start my writing mode; enable visual-line-mode and auto-fill-mode"
784       (interactive)
785       (if writeroom-mode
786           (progn
787             (writeroom-mode -1)
788             (visual-line-mode -1)
789             (auto-fill-mode -1)
790             (visual-fill-column-mode -1)
791             )
792         (visual-line-mode 1)
793         (auto-fill-mode 1)
794         (visual-fill-column-mode 1)
795         (writeroom-mode 1))
796       )
797     )
798 #+END_SRC
799 ** GhostText/Atomic Chrome
800 #+BEGIN_SRC emacs-lisp
801   (use-package atomic-chrome
802     :config
803     (ignore-errors (atomic-chrome-start-server))
804     (setq atomic-chrome-buffer-open-style 'full)
805     )
806 #+END_SRC
807 ** Multiple Cursors
808    :PROPERTIES:
809    :ID:       6fcf218b-a762-4c37-9339-a8202ddeb544
810    :END:
811 [[https://github.com/magnars/multiple-cursors.el][Multiple Cursors]]
812 #+BEGIN_SRC emacs-lisp
813   (use-package multiple-cursors
814     :bind (("C-;" . mc/mark-all-dwim)
815            ("C-<" . mc/mark-previous-like-this)
816            ("C->" . mc/mark-next-like-this)
817            ("C-S-c C-S-c" . mc/edit-lines))
818     )
819 #+END_SRC
820 ** Web Mode
821 #+BEGIN_SRC emacs-lisp
822   (use-package web-mode
823     :config
824     (add-to-list 'auto-mode-alist '("\\.tmpl\\'" . web-mode))
825     (setq web-mode-enable-engine-detection t)
826     (setq web-mode-engines-alist
827           '(("template-toolkit" . "\\.tmpl\\'")))
828     )
829 #+END_SRC
830 * Email
831 ** Message Mode
832 #+BEGIN_SRC emacs-lisp
833   (use-package post
834     :config
835     (setq post-email-address "don@donarmstrong.com")
836     (setq post-kill-quoted-sig nil)
837     (setq post-mail-message "mutt\\(ng\\|\\)-[a-z0-9]+-[0-9]+-.*")
838     (setq post-uses-fill-mode nil)
839     )
840 #+END_SRC
841 ** Mutt
842 *** Message-mode
843 #+BEGIN_SRC emacs-lisp
844   (use-package message
845
846     :mode "muttng-[a-z0-9]+-[0-9]+-"
847     :mode "mutt-[a-z0-9]+-[0-9]+-"
848     :hook 'my/message-mode-settings
849     :hook 'turn-on-flyspell
850     :bind (:map message-mode-map
851                 ("C-c C-a" . my/post-attach-file))
852     :config 
853     (defun my/message-mode-settings ()
854       (font-lock-add-keywords nil
855                               '(("^[ \t]*>[ \t]*>[ \t]*>.*$"
856                                  (0 'message-multiply-quoted-text-face))
857                                 ("^[ \t]*>[ \t]*>.*$"
858                                  (0 'message-double-quoted-text-face))))
859       )
860
861     (defun my/post-attach-file ()
862       "Prompt for an attachment."
863       (interactive)
864       (let ((file (read-file-name "Attach file: " nil nil t nil))
865             (description (string-read "Description: ")))
866         (my/header-attach-file file description)))
867
868     (defun my/header-attach-file (file description)
869       "Attach a FILE to the current message (works with Mutt).
870     Argument DESCRIPTION MIME description."
871       (interactive "fAttach file: \nsDescription: ")
872       (when (> (length file) 0)
873         (save-excursion
874           (save-match-data
875             (save-restriction
876               (widen)
877               (goto-char (point-min))
878               (search-forward-regexp "^$")
879               (insert (concat "Attach: " (replace-regexp-in-string "\\([[:space:]\\]\\)" "\\\\\\1" (file-truename file)) " "
880                               description "\n"))
881               (message (concat "Attached '" file "'."))
882               (setq post-has-attachment t))))))
883
884     (setq mail-yank-prefix "> ")
885   )
886 #+END_SRC
887 *** Muttrc mode
888 #+BEGIN_SRC emacs-lisp
889   (use-package muttrc-mode
890     :mode "muttngrc"
891     :mode "muttrc"
892   )
893
894 #+END_SRC
895 * Base emacs
896 ** Reverting buffers
897 #+BEGIN_SRC emacs-lisp
898   (setq global-auto-revert-non-file-buffers t
899         global-auto-revert-ignore-modes '(pdf-view-mode)
900         auto-revert-verbose nil)
901   (global-auto-revert-mode 1)
902 #+END_SRC
903 * Org Mode
904 ** Use-package and load things
905 #+BEGIN_SRC emacs-lisp
906
907   (use-package org
908     :config 
909
910 #+END_SRC
911 ** Agenda Configuration
912 #+BEGIN_SRC emacs-lisp
913   :mode ("\\.\\(org\\|org_archive\\|txt\\)\\'" . org-mode)
914   :bind (("C-c l"  . org-store-link)
915          ("C-c a"  . org-agenda)
916          ("C-c b"  . org-iswitchb))
917   :config
918   (setq-default org-log-done 'time)
919   (setq-default org-agenda-ndays 5)
920
921   ;; agenda configuration
922   ;; Do not dim blocked tasks
923   (setq org-agenda-dim-blocked-tasks nil)
924   (setq org-agenda-inhibit-startup t)
925   (setq org-agenda-use-tag-inheritance nil)
926
927   ;; Compact the block agenda view
928   (setq org-agenda-compact-blocks t)
929
930   ;; Custom agenda command definitions
931   (setq org-agenda-custom-commands
932         (quote (("N" "Notes" tags "NOTE"
933                  ((org-agenda-overriding-header "Notes")
934                   (org-tags-match-list-sublevels t)))
935                 ("h" "Habits" tags-todo "STYLE=\"habit\""
936                  ((org-agenda-overriding-header "Habits")
937                   (org-agenda-sorting-strategy
938                    '(todo-state-down effort-up category-keep))))
939                 (" " "Agenda"
940                  ((agenda "" nil)
941                   (tags "REFILE"
942                         ((org-agenda-overriding-header "Tasks to Refile")
943                          (org-tags-match-list-sublevels nil)))
944                   (tags-todo "-CANCELLED/!"
945                              ((org-agenda-overriding-header "Stuck Projects")
946                               (org-agenda-skip-function 'bh/skip-non-stuck-projects)
947                               (org-agenda-sorting-strategy
948                                '(category-keep))))
949                   (tags-todo "-HOLD-CANCELLED/!"
950                              ((org-agenda-overriding-header "Projects")
951                               (org-agenda-skip-function 'bh/skip-non-projects)
952                               (org-tags-match-list-sublevels 'indented)
953                               (org-agenda-sorting-strategy
954                                '(category-keep))))
955                   (tags-todo "-CANCELLED/!NEXT"
956                              ((org-agenda-overriding-header (concat "Project Next Tasks"
957                                                                     (if bh/hide-scheduled-and-waiting-next-tasks
958                                                                         ""
959                                                                       " (including WAITING and SCHEDULED tasks)")))
960                               (org-agenda-skip-function 'bh/skip-projects-and-habits-and-single-tasks)
961                               (org-tags-match-list-sublevels t)
962                               (org-agenda-todo-ignore-scheduled bh/hide-scheduled-and-waiting-next-tasks)
963                               (org-agenda-todo-ignore-deadlines bh/hide-scheduled-and-waiting-next-tasks)
964                               (org-agenda-todo-ignore-with-date bh/hide-scheduled-and-waiting-next-tasks)
965                               (org-agenda-sorting-strategy
966                                '(todo-state-down effort-up category-keep))))
967                   (tags-todo "-REFILE-CANCELLED-WAITING-HOLD/!"
968                              ((org-agenda-overriding-header (concat "Project Subtasks"
969                                                                     (if bh/hide-scheduled-and-waiting-next-tasks
970                                                                         ""
971                                                                       " (including WAITING and SCHEDULED tasks)")))
972                               (org-agenda-skip-function 'bh/skip-non-project-tasks)
973                               (org-agenda-todo-ignore-scheduled bh/hide-scheduled-and-waiting-next-tasks)
974                               (org-agenda-todo-ignore-deadlines bh/hide-scheduled-and-waiting-next-tasks)
975                               (org-agenda-todo-ignore-with-date bh/hide-scheduled-and-waiting-next-tasks)
976                               (org-agenda-sorting-strategy
977                                '(category-keep))))
978                   (tags-todo "-REFILE-CANCELLED-WAITING-HOLD/!"
979                              ((org-agenda-overriding-header (concat "Standalone Tasks"
980                                                                     (if bh/hide-scheduled-and-waiting-next-tasks
981                                                                         ""
982                                                                       " (including WAITING and SCHEDULED tasks)")))
983                               (org-agenda-skip-function 'bh/skip-project-tasks)
984                               (org-agenda-todo-ignore-scheduled bh/hide-scheduled-and-waiting-next-tasks)
985                               (org-agenda-todo-ignore-deadlines bh/hide-scheduled-and-waiting-next-tasks)
986                               (org-agenda-todo-ignore-with-date bh/hide-scheduled-and-waiting-next-tasks)
987                               (org-agenda-sorting-strategy
988                                '(category-keep))))
989                   (tags-todo "-CANCELLED+WAITING|HOLD/!"
990                              ((org-agenda-overriding-header "Waiting and Postponed Tasks")
991                               (org-agenda-skip-function 'bh/skip-stuck-projects)
992                               (org-tags-match-list-sublevels nil)
993                               (org-agenda-todo-ignore-scheduled t)
994                               (org-agenda-todo-ignore-deadlines t)))
995                   (tags "-REFILE/"
996                         ((org-agenda-overriding-header "Tasks to Archive")
997                          (org-agenda-skip-function 'bh/skip-non-archivable-tasks)
998                          (org-tags-match-list-sublevels nil))))
999                  nil))))
1000
1001   ; org mode agenda files
1002   (setq org-agenda-files
1003         (quote ("~/projects/org-notes/debbugs.org"
1004             "~/projects/org-notes/notes.org"
1005             "~/projects/org-notes/holidays.org"
1006             "~/projects/org-notes/refile.org"
1007             "~/projects/org-notes/diary.org"
1008             "~/projects/org-notes/ool.org"
1009             "~/projects/org-notes/sndservers.org"
1010             "~/projects/org-notes/chaim.org"
1011             "~/projects/org-notes/wildman.org"
1012             "~/projects/org-notes/uddin.org"
1013             "~/projects/org-notes/reviews.org"
1014             "~/org-mode/from-mobile.org"
1015             "~/projects/org-notes/fh.org")))
1016
1017   (set-register ?n (cons 'file "~/projects/org-notes/notes.org"))
1018   (set-register ?r (cons 'file "~/projects/org-notes/refile.org"))
1019   (set-register ?o (cons 'file "~/projects/org-notes/ool.org"))
1020   (set-register ?s (cons 'file "~/projects/org-notes/sndservers.org"))
1021   (set-register ?c (cons 'file "~/projects/org-notes/chaim.org"))
1022   (set-register ?w (cons 'file "~/projects/org-notes/wildman.org"))
1023   (set-register ?u (cons 'file "~/projects/org-notes/uddin.org"))
1024   (set-register ?R (cons 'file "~/projects/reviews/reviews.org"))
1025   (set-register ?d (cons 'file "~/projects/org-notes/diary.org"))
1026   ; from https://emacs.stackexchange.com/questions/909/how-can-i-have-an-agenda-timeline-view-of-multiple-files
1027   (defun org-agenda-timeline-all (&optional arg)
1028     (interactive "P")
1029     (with-temp-buffer
1030       (dolist (org-agenda-file org-agenda-files)
1031         (insert-file-contents org-agenda-file nil)
1032         (end-of-buffer)
1033         (newline))
1034       (write-file "/tmp/timeline.org")
1035       (org-agenda arg "L")))
1036   (define-key org-mode-map (kbd "C-c t") 'org-agenda-timeline-all)
1037
1038 #+END_SRC
1039 ** General config
1040 #+BEGIN_SRC emacs-lisp
1041   (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")))
1042   (setq org-columns-default-format "%40ITEM(Task) %6Effort{:} %CLOCKSUM %PRIORITY %TODO %13SCHEDULED %13DEADLINE %TAGS")
1043
1044   (setq org-default-notes-file "~/projects/org-notes/notes.org")
1045   (setq org-id-link-to-org-use-id 'use-existing)
1046 #+END_SRC
1047 ** Capture Templates
1048 #+BEGIN_SRC emacs-lisp
1049   (setq org-capture-templates  ;; mail-specific note template, identified by "m"
1050         '(("m" "Mail" entry (file "~/projects/org-notes/refile.org")
1051            "* %?\n\n  Source: %u, [[%:link][%:description]]\n  %:initial")
1052           ("t" "todo" entry (file "~/projects/org-notes/refile.org")
1053            "* TODO %?\n  :PROPERTIES:\n  :END:\n  :LOGBOOK:\n  :END:\n%U\n%a\n" :clock-in t :clock-resume t)
1054           ("r" "respond" entry (file "~/projects/org-notes/refile.org")
1055            "* NEXT Respond to %:from on %:subject\nSCHEDULED: %t\n%U\n%a\n" :clock-in t :clock-resume t :immediate-finish t)
1056           ("n" "note" entry (file "~/projects/org-notes/refile.org")
1057            "* %? :NOTE:\n%U\n%a\n" :clock-in t :clock-resume t)
1058           ("s" "schedule" entry (file "~/projects/org-notes/refile.org")
1059            "* %? :cal:\n%^{scheduled:}t\n%U\n%a\n" :clock-in t :clock-resume t)
1060           ("j" "Journal" entry (file+datetree "~/projects/org-notes/diary.org")
1061            "* %?\n%U\n" :clock-in t :clock-resume t)
1062           ("w" "org-protocol" entry (file "~/projects/org-notes/refile.org")
1063            "* TODO Review %c\n%U\n" :immediate-finish t)
1064           ("M" "Meeting" entry (file "~/projects/org-notes/refile.org")
1065            "* MEETING with %? :MEETING:\n%U" :clock-in t :clock-resume t)
1066           ("S" "Seminar" entry (file "~/projects/org-notes/refile.org")
1067            "* SEMINAR notes %? :SEMINAR:\n%U" :clock-in t :clock-resume t)
1068           ("P" "Paper to read" entry (file+headline "~/projects/research/papers_to_read.org" "Refile")
1069            "* TODO Get/Read %? \n%U" :clock-in t :clock-resume t)
1070           ("p" "Phone call" entry (file "~/projects/org-notes/refile.org")
1071            "* PHONE %? :PHONE:\n%U" :clock-in t :clock-resume t)
1072           ("J" "job" entry (file "~/projects/org-notes/refile.org")
1073            "* 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)
1074           ("h" "Habit" entry (file "~/projects/org-notes/refile.org")
1075            "* 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")
1076           )
1077         )
1078
1079   ;; Remove empty LOGBOOK drawers on clock out
1080   (defun bh/remove-empty-drawer-on-clock-out ()
1081     (interactive)
1082     (save-excursion
1083       (beginning-of-line 0)
1084       (org-remove-empty-drawer-at (point))))
1085
1086   (defun my/org-add-id ()
1087     (interactive)
1088     (save-excursion
1089       (if (org-current-level)
1090           ()
1091         (forward-char 1)
1092         )
1093       (org-id-get-create)
1094       )
1095   )
1096
1097 #+END_SRC
1098 ** Org mode key bindings
1099 #+BEGIN_SRC emacs-lisp
1100   ; org mode configuration from http://doc.norang.ca/org-mode.html
1101   ;; Custom Key Bindings
1102   (global-set-key (kbd "<f12>") 'org-agenda)
1103   (global-set-key (kbd "<f5>") 'bh/org-todo)
1104   (global-set-key (kbd "<S-f5>") 'bh/widen)
1105   (global-set-key (kbd "<f7>") 'bh/set-truncate-lines)
1106   (global-set-key (kbd "<f8>") 'org-cycle-agenda-files)
1107   (global-set-key (kbd "<f9> <f9>") 'bh/show-org-agenda)
1108   (global-set-key (kbd "<f9> b") 'bbdb)
1109   (global-set-key (kbd "<f9> c") 'calendar)
1110   (global-set-key (kbd "<f9> f") 'boxquote-insert-file)
1111   (global-set-key (kbd "<f9> h") 'bh/hide-other)
1112   (global-set-key (kbd "<f9> n") 'bh/toggle-next-task-display)
1113   (global-set-key (kbd "<f9> w") 'widen)
1114
1115   ; change the outline mode prefix from C-c @ to C-c C-2
1116   (setq outline-minor-mode-prefix "C-c C-2")
1117   ;(add-hook 'outline-minor-mode-hook
1118   ;          (lambda () (local-set-key (kbd "C-c C-2")
1119   ;                                    outline-mode-prefix-map)))
1120
1121   (global-set-key (kbd "<f9> I") 'bh/punch-in)
1122   (global-set-key (kbd "<f9> O") 'bh/punch-out)
1123
1124   (global-set-key (kbd "<f9> o") 'bh/make-org-scratch)
1125
1126   (global-set-key (kbd "<f9> r") 'boxquote-region)
1127   (global-set-key (kbd "<f9> s") 'bh/switch-to-scratch)
1128
1129   (global-set-key (kbd "<f9> t") 'bh/insert-inactive-timestamp)
1130   (global-set-key (kbd "<f9> T") 'bh/toggle-insert-inactive-timestamp)
1131
1132   (global-set-key (kbd "<f9> v") 'visible-mode)
1133   (global-set-key (kbd "<f9> l") 'org-toggle-link-display)
1134   (global-set-key (kbd "<f9> SPC") 'bh/clock-in-last-task)
1135   (global-set-key (kbd "C-<f9>") 'previous-buffer)
1136   (global-set-key (kbd "M-<f9>") 'org-toggle-inline-images)
1137   (global-set-key (kbd "C-x n r") 'narrow-to-region)
1138   (global-set-key (kbd "C-<f10>") 'next-buffer)
1139   (global-set-key (kbd "<f11>") 'org-clock-goto)
1140   (global-set-key (kbd "C-<f11>") 'org-clock-in)
1141   (global-set-key (kbd "C-s-<f12>") 'bh/save-then-publish)
1142   (global-set-key (kbd "C-c c") 'org-capture)
1143
1144 #+END_SRC
1145 ** Utility Functions
1146 #+BEGIN_SRC emacs-lisp
1147   (defun bh/hide-other ()
1148     (interactive)
1149     (save-excursion
1150       (org-back-to-heading 'invisible-ok)
1151       (hide-other)
1152       (org-cycle)
1153       (org-cycle)
1154       (org-cycle)))
1155
1156   (defun bh/set-truncate-lines ()
1157     "Toggle value of truncate-lines and refresh window display."
1158     (interactive)
1159     (setq truncate-lines (not truncate-lines))
1160     ;; now refresh window display (an idiom from simple.el):
1161     (save-excursion
1162       (set-window-start (selected-window)
1163                         (window-start (selected-window)))))
1164
1165   (defun bh/make-org-scratch ()
1166     (interactive)
1167     (find-file "/tmp/publish/scratch.org")
1168     (gnus-make-directory "/tmp/publish"))
1169
1170   (defun bh/switch-to-scratch ()
1171     (interactive)
1172     (switch-to-buffer "*scratch*"))
1173
1174   (setq org-use-fast-todo-selection t)
1175   (setq org-treat-S-cursor-todo-selection-as-state-change nil)
1176
1177   ; create function to create headlines in file. This comes from
1178   ; http://stackoverflow.com/questions/13340616/assign-ids-to-every-entry-in-org-mode
1179   (defun my/org-add-ids-to-headlines-in-file ()
1180     "Add ID properties to all headlines in the current file which
1181   do not already have one."
1182     (interactive)
1183     (org-map-entries 'org-id-get-create))
1184   ; if we wanted to do this to every buffer, do the following:
1185   ; (add-hook 'org-mode-hook
1186   ;           (lambda ()
1187   ;             (add-hook 'before-save-hook 'my/org-add-ids-to-headlines-in-file nil 'local)))
1188 #+END_SRC
1189 ** Keywords (TODO)
1190 #+BEGIN_SRC emacs-lisp
1191   (setq org-todo-keywords
1192         (quote ((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d)")
1193                 (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" "PHONE" "MEETING"))))
1194
1195   (setq org-todo-keyword-faces
1196         (quote (("TODO" :foreground "red" :weight bold)
1197                 ("NEXT" :foreground "blue" :weight bold)
1198                 ("DONE" :foreground "forest green" :weight bold)
1199                 ("WAITING" :foreground "orange" :weight bold)
1200                 ("HOLD" :foreground "magenta" :weight bold)
1201                 ("CANCELLED" :foreground "forest green" :weight bold)
1202                 ("MEETING" :foreground "forest green" :weight bold)
1203                 ("PHONE" :foreground "forest green" :weight bold))))
1204
1205   (setq org-todo-state-tags-triggers
1206         (quote (("CANCELLED" ("CANCELLED" . t))
1207                 ("WAITING" ("WAITING" . t))
1208                 ("HOLD" ("WAITING") ("HOLD" . t))
1209                 (done ("WAITING") ("HOLD"))
1210                 ("TODO" ("WAITING") ("CANCELLED") ("HOLD"))
1211                 ("NEXT" ("WAITING") ("CANCELLED") ("HOLD"))
1212                 ("DONE" ("WAITING") ("CANCELLED") ("HOLD")))))
1213
1214
1215
1216   ; (add-hook 'org-clock-out-hook 'bh/remove-empty-drawer-on-clock-out 'append)
1217   ; add ids on creation of nodes
1218   (add-hook 'org-capture-prepare-finalize-hook 'my/org-add-id)
1219
1220
1221   ; resolve clocks after 10 minutes of idle; use xprintidle
1222   ; (setq org-clock-idle-time 10)
1223   ; (setq org-clock-x11idle-program-name "xprintidle")
1224
1225   ; this is from http://doc.norang.ca/org-mode.html#Capture
1226   ; use C-M-r for org mode capture
1227   (global-set-key (kbd "C-M-r") 'org-capture)
1228
1229   ; Targets include this file and any file contributing to the agenda - up to 9 levels deep
1230   (setq org-refile-targets (quote ((nil :maxlevel . 9)
1231                                    (org-agenda-files :maxlevel . 9))))
1232
1233   ; Use full outline paths for refile targets - we file directly with IDO
1234   (setq org-refile-use-outline-path t)
1235
1236   ; Targets complete directly with IDO
1237   (setq org-outline-path-complete-in-steps nil)
1238
1239   ; Allow refile to create parent tasks with confirmation
1240   (setq org-refile-allow-creating-parent-nodes (quote confirm))
1241
1242   ; ; Use IDO for both buffer and file completion and ido-everywhere to t
1243   ; (setq org-completion-use-ido t)
1244   ; (setq ido-everywhere t)
1245   ; (setq ido-max-directory-size 100000)
1246   ; (ido-mode (quote both))
1247   ; ; Use the current window when visiting files and buffers with ido
1248   ; (setq ido-default-file-method 'selected-window)
1249   ; (setq ido-default-buffer-method 'selected-window)
1250   ; ; Use the current window for indirect buffer display
1251   ; (setq org-indirect-buffer-display 'current-window)
1252
1253
1254   ;;;; Refile settings
1255   ; Exclude DONE state tasks from refile targets
1256   (defun bh/verify-refile-target ()
1257     "Exclude todo keywords with a done state from refile targets"
1258     (not (member (nth 2 (org-heading-components)) org-done-keywords)))
1259
1260   (setq org-refile-target-verify-function 'bh/verify-refile-target)
1261
1262   ;; ensure that emacsclient will show just the note to be edited when invoked
1263   ;; from Mutt, and that it will shut down emacsclient once finished;
1264   ;; fallback to legacy behavior when not invoked via org-protocol.
1265   (require 'org-protocol)
1266   ; (add-hook 'org-capture-mode-hook 'delete-other-windows)
1267   (setq my-org-protocol-flag nil)
1268   (defadvice org-capture-finalize (after delete-frame-at-end activate)
1269     "Delete frame at remember finalization"
1270     (progn (if my-org-protocol-flag (delete-frame))
1271            (setq my-org-protocol-flag nil)))
1272   (defadvice org-capture-refile (around delete-frame-after-refile activate)
1273     "Delete frame at remember refile"
1274     (if my-org-protocol-flag
1275         (progn
1276           (setq my-org-protocol-flag nil)
1277           ad-do-it
1278           (delete-frame))
1279       ad-do-it)
1280     )
1281   (defadvice org-capture-kill (after delete-frame-at-end activate)
1282     "Delete frame at remember abort"
1283     (progn (if my-org-protocol-flag (delete-frame))
1284            (setq my-org-protocol-flag nil)))
1285   (defadvice org-protocol-capture (before set-org-protocol-flag activate)
1286     (setq my-org-protocol-flag t))
1287
1288   (defadvice org-insert-todo-heading (after dla/create-id activate)
1289     (org-id-get-create)
1290     )
1291
1292   ;; org modules
1293   (add-to-list 'org-modules 'org-habit)
1294
1295   ; this comes from http://upsilon.cc/~zack/blog/posts/2010/02/integrating_Mutt_with_Org-mode/
1296   (defun open-mail-in-mutt (message)
1297     "Open a mail message in Mutt, using an external terminal.
1298
1299   Message can be specified either by a path pointing inside a
1300   Maildir, or by Message-ID."
1301     (interactive "MPath or Message-ID: ")
1302     (shell-command
1303      (format "faf xterm -e \"%s %s\""
1304          (substitute-in-file-name "$HOME/bin/mutt_open") message)))
1305
1306   ;; add support for "mutt:ID" links
1307   (org-add-link-type "mutt" 'open-mail-in-mutt)
1308
1309   (defun my-org-mode-setup ()
1310     ; (load-library "reftex")
1311     (and (buffer-file-name)
1312          (file-exists-p (buffer-file-name))
1313          (progn
1314            ; (reftex-parse-all)
1315            (reftex-set-cite-format
1316             '((?b . "[[bib:%l][%l-bib]]")
1317               (?n . "[[notes:%l][%l-notes]]")
1318               (?c . "\\cite{%l}")
1319               (?h . "*** %t\n:PROPERTIES:\n:Custom_ID: %l\n:END:\n[[papers:%l][%l xoj]] [[papers-pdf:%l][pdf]]")))
1320            ))
1321     (define-key org-mode-map (kbd "C-c )") 'reftex-citation)
1322     (define-key org-mode-map (kbd "C-c [") 'reftex-citation)
1323     (define-key org-mode-map (kbd "C-c (") 'org-mode-reftex-search)
1324     (define-key org-mode-map (kbd "C-c 0") 'reftex-view-crossref)
1325     )
1326   (add-hook 'org-mode-hook 'my-org-mode-setup)
1327
1328   (defun org-mode-reftex-search ()
1329     (interactive)
1330     (org-open-link-from-string (format "[[notes:%s]]" (first (reftex-citation t)))))
1331
1332   (defun open-research-paper (bibtexkey)
1333     "Open a paper by bibtex key"
1334     (interactive "bibtex key: ")
1335     (shell-command
1336      (format "%s %s"
1337          (substitute-in-file-name "$HOME/bin/bibtex_to_paper") bibtexkey)))
1338   (org-add-link-type "papers" 'open-research-paper)
1339   (defun open-research-paper-pdf (bibtexkey)
1340     "Open a paper pdf by bibtex key"
1341     (interactive "bibtex key: ")
1342     (shell-command
1343      (format "%s -p evince_annot %s"
1344          (substitute-in-file-name "$HOME/bin/bibtex_to_paper") bibtexkey)))
1345   (org-add-link-type "papers-pdf" 'open-research-paper-pdf)
1346
1347   (add-to-list 'org-link-abbrev-alist
1348                '("notes" .
1349                  "~/projects/research/paper_notes.org::#%s"))
1350
1351   ; I pretty much always want hiearchical checkboxes
1352   (setq org-hierachical-checkbox-statistics nil)
1353
1354   ;; Add \begin{equation}\end{equation} templates to the org mode easy templates
1355   (add-to-list 'org-structure-template-alist
1356                '("E" "\\begin{equation}\n?\n\\end{equation}"))
1357
1358    ;; stolen from
1359   ;; http://www-public.it-sudparis.eu/~berger_o/weblog/2012/03/23/how-to-manage-and-export-bibliographic-notesrefs-in-org-mode/
1360   (defun my-rtcite-export-handler (path desc format)
1361     (message "my-rtcite-export-handler is called : path = %s, desc = %s, format = %s" path desc format)
1362     (let* ((search (when (string-match "::#?\\(.+\\)\\'" path)
1363                      (match-string 1 path)))
1364            (path (substring path 0 (match-beginning 0))))
1365       (cond ((eq format 'latex)
1366              (if (or (not desc) 
1367                      (equal 0 (search "rtcite:" desc)))
1368                  (format "\\cite{%s}" search)
1369                (format "\\cite[%s]{%s}" desc search))))))
1370
1371   (org-add-link-type "rtcite" 
1372                      'org-bibtex-open
1373                      'my-rtcite-export-handler)
1374
1375
1376 #+END_SRC
1377 ** Org Mobile Configuration
1378 #+BEGIN_SRC emacs-lisp
1379   (setq-default org-mobile-directory "/linnode.donarmstrong.com:/sites/dav.donarmstrong.com/root/org/")
1380   (when (string= system-name "linnode")
1381     (setq-default org-mobile-directory "/sites/dav.donarmstrong.com/root/org/"))
1382   (setq-default org-directory "/home/don/org-mode/")
1383   (setq-default org-mobile-inbox-for-pull "/home/don/org-mode/from-mobile.org")
1384
1385 #+END_SRC
1386 ** Org iCal Support
1387 #+BEGIN_SRC emacs-lisp
1388   ;; org mode ical export
1389   (setq org-icalendar-timezone "America/Los_Angeles")
1390   (setq org-icalendar-use-scheduled '(todo-start event-if-todo))
1391   ;; we already add the id manually
1392   (setq org-icalendar-store-UID t)
1393
1394 #+END_SRC
1395 ** General Org Babel Configuration
1396 #+BEGIN_SRC emacs-lisp
1397   ;; org babel support
1398   (org-babel-do-load-languages
1399    'org-babel-load-languages
1400    '((emacs-lisp . t )
1401      (R . t)
1402      (latex . t)
1403      (ditaa . t)
1404      (dot . t)
1405      ))
1406   ;; use graphviz-dot for dot things
1407   (add-to-list 'org-src-lang-modes '("dot" . graphviz-dot))
1408   ;; org-babel-by-backend
1409   (defmacro org-babel-by-backend (&rest body)
1410      `(case (if (boundp 'backend) 
1411                 (org-export-backend-name backend)
1412               nil) ,@body))
1413
1414   (defun my/fix-inline-images ()
1415     (when org-inline-image-overlays
1416       (org-redisplay-inline-images)))
1417
1418   (add-hook 'org-babel-after-execute-hook
1419              'my/fix-inline-images)
1420
1421 #+END_SRC
1422 ** LaTeX configuration
1423    :PROPERTIES:
1424    :ID:       7135ba17-6a50-4eed-84ca-b90afa5b12f8
1425    :END:
1426 #+BEGIN_SRC emacs-lisp
1427   (require 'ox-latex)
1428   (add-to-list 'org-latex-classes
1429            '("memarticle"
1430          "\\documentclass[11pt,oneside,article]{memoir}\n"
1431          ("\\section{%s}" . "\\section*{%s}")
1432          ("\\subsection{%s}" . "\\subsection*{%s}")
1433          ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
1434          ("\\paragraph{%s}" . "\\paragraph*{%s}")
1435          ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
1436
1437   (setq org-beamer-outline-frame-options "")
1438   (add-to-list 'org-latex-classes
1439            '("beamer"
1440          "\\documentclass[ignorenonframetext]{beamer}
1441   [NO-DEFAULT-PACKAGES]
1442   [PACKAGES]
1443   [EXTRA]"
1444          ("\\section{%s}" . "\\section*{%s}")
1445          ("\\subsection{%s}" . "\\subsection*{%s}")
1446          ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
1447          ("\\paragraph{%s}" . "\\paragraph*{%s}")
1448          ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
1449
1450   (add-to-list 'org-latex-classes
1451            '("membook"
1452          "\\documentclass[11pt,oneside]{memoir}\n"
1453          ("\\chapter{%s}" . "\\chapter*{%s}")
1454          ("\\section{%s}" . "\\section*{%s}")
1455          ("\\subsection{%s}" . "\\subsection*{%s}")
1456          ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
1457
1458   (add-to-list 'org-latex-classes
1459            '("letter"
1460          "\\documentclass[11pt]{letter}
1461   [NO-DEFAULT-PACKAGES]
1462   [PACKAGES]
1463   [EXTRA]"
1464      ("\\section{%s}" . "\\section*{%s}")
1465          ("\\subsection{%s}" . "\\subsection*{%s}")
1466          ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
1467          ("\\paragraph{%s}" . "\\paragraph*{%s}")
1468          ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
1469
1470   (add-to-list 'org-latex-classes
1471            '("dlacv"
1472          "\\documentclass{dlacv}
1473   [NO-DEFAULT-PACKAGES]
1474   [NO-PACKAGES]
1475   [NO-EXTRA]"
1476          ("\\section{%s}" . "\\section*{%s}")
1477          ("\\subsection{%s}" . "\\subsection*{%s}")
1478          ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
1479          ("\\paragraph{%s}" . "\\paragraph*{%s}")
1480          ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
1481
1482
1483   (add-to-list 'org-latex-classes
1484            '("dlaresume"
1485          "\\documentclass{dlaresume}
1486   [NO-DEFAULT-PACKAGES]
1487   [NO-PACKAGES]
1488   [NO-EXTRA]"
1489          ("\\section{%s}" . "\\section*{%s}")
1490          ("\\subsection{%s}" . "\\subsection*{%s}")
1491          ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
1492          ("\\paragraph{%s}" . "\\paragraph*{%s}")
1493          ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
1494
1495
1496   ;; Originally taken from Bruno Tavernier: http://thread.gmane.org/gmane.emacs.orgmode/31150/focus=31432
1497   ;; but adapted to use latexmk 4.22 or higher.  
1498   (setq org-latex-pdf-process '("latexmk -f -pdflatex=xelatex -bibtex -use-make -pdf %f"))
1499
1500   ;; Default packages included in /every/ tex file, latex, pdflatex or xelatex
1501   (setq org-latex-default-packages-alist
1502     '(("" "amsmath" t)
1503       ("" "unicode-math" t)
1504       ))
1505   (setq org-latex-packages-alist
1506     '(("" "graphicx" t)
1507       ("" "fontspec" t)
1508       ("" "xunicode" t)
1509       ("" "hyperref" t)
1510       ("" "url" t)
1511       ("" "rotating" t)
1512       ("" "longtable" nil)
1513       ("" "float" )))
1514
1515   ;; make equations larger
1516   (setq org-format-latex-options (plist-put org-format-latex-options :scale 2.0))
1517
1518   (defun org-create-formula--latex-header ()
1519     "Return LaTeX header appropriate for previewing a LaTeX snippet."
1520     (let ((info (org-combine-plists (org-export--get-global-options
1521              (org-export-get-backend 'latex))
1522             (org-export--get-inbuffer-options
1523              (org-export-get-backend 'latex)))))
1524       (org-latex-guess-babel-language
1525        (org-latex-guess-inputenc
1526     (org-splice-latex-header
1527      org-format-latex-header
1528      org-latex-default-packages-alist
1529      nil t
1530      (plist-get info :latex-header)))
1531        info)))
1532
1533
1534   ; support ignoring headers in org mode export to latex
1535   ; from http://article.gmane.org/gmane.emacs.orgmode/67692
1536   (defadvice org-latex-headline (around my-latex-skip-headlines
1537                     (headline contents info) activate)
1538     (if (member "ignoreheading" (org-element-property :tags headline))
1539     (setq ad-return-value contents)
1540       ad-do-it))
1541
1542   ;; keep latex logfiles
1543
1544   (setq org-latex-remove-logfiles nil)
1545
1546   ;; Resume clocking task when emacs is restarted
1547   (org-clock-persistence-insinuate)
1548   ;;
1549   ;; Show lot of clocking history so it's easy to pick items off the C-F11 list
1550   (setq org-clock-history-length 23)
1551   ;; Resume clocking task on clock-in if the clock is open
1552   (setq org-clock-in-resume t)
1553   ;; Change tasks to NEXT when clocking in; this avoids clocking in when
1554   ;; there are things like PHONE calls
1555   (setq org-clock-in-switch-to-state 'bh/clock-in-to-next)
1556   ;; Separate drawers for clocking and logs
1557   (setq org-drawers (quote ("PROPERTIES" "LOGBOOK")))
1558   ;; Save clock data and state changes and notes in the LOGBOOK drawer
1559   (setq org-clock-into-drawer t)
1560   (setq org-log-into-drawer t)
1561   ;; Sometimes I change tasks I'm clocking quickly - this removes clocked tasks with 0:00 duration
1562   (setq org-clock-out-remove-zero-time-clocks t)
1563   ;; Clock out when moving task to a done state
1564   (setq org-clock-out-when-done t)
1565   ;; Save the running clock and all clock history when exiting Emacs, load it on startup
1566   (setq org-clock-persist t)
1567   ;; Do not prompt to resume an active clock
1568   (setq org-clock-persist-query-resume nil)
1569   ;; Enable auto clock resolution for finding open clocks
1570   (setq org-clock-auto-clock-resolution (quote when-no-clock-is-running))
1571   ;; Include current clocking task in clock reports
1572   (setq org-clock-report-include-clocking-task t)
1573
1574   ;; the cache seems to be broken
1575   (setq org-element-use-cache nil)
1576
1577   (defvar bh/keep-clock-running nil)
1578
1579   (defun bh/is-task-p ()
1580     "Any task with a todo keyword and no subtask"
1581     (save-restriction
1582       (widen)
1583       (let ((has-subtask)
1584             (subtree-end (save-excursion (org-end-of-subtree t)))
1585             (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
1586         (save-excursion
1587           (forward-line 1)
1588           (while (and (not has-subtask)
1589                       (< (point) subtree-end)
1590                       (re-search-forward "^\*+ " subtree-end t))
1591             (when (member (org-get-todo-state) org-todo-keywords-1)
1592               (setq has-subtask t))))
1593         (and is-a-task (not has-subtask)))))
1594   (defun bh/is-project-p ()
1595     "Any task with a todo keyword subtask"
1596     (save-restriction
1597       (widen)
1598       (let ((has-subtask)
1599             (subtree-end (save-excursion (org-end-of-subtree t)))
1600             (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
1601         (save-excursion
1602           (forward-line 1)
1603           (while (and (not has-subtask)
1604                       (< (point) subtree-end)
1605                       (re-search-forward "^\*+ " subtree-end t))
1606             (when (member (org-get-todo-state) org-todo-keywords-1)
1607               (setq has-subtask t))))
1608         (and is-a-task has-subtask))))
1609
1610   (defun bh/is-subproject-p ()
1611     "Any task which is a subtask of another project"
1612     (let ((is-subproject)
1613           (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
1614       (save-excursion
1615         (while (and (not is-subproject) (org-up-heading-safe))
1616           (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
1617             (setq is-subproject t))))
1618       (and is-a-task is-subproject)))
1619
1620
1621   (defun bh/clock-in-to-next (kw)
1622     "Switch a task from TODO to NEXT when clocking in.
1623   Skips capture tasks, projects, and subprojects.
1624   Switch projects and subprojects from NEXT back to TODO"
1625     (when (not (and (boundp 'org-capture-mode) org-capture-mode))
1626       (cond
1627        ((and (member (org-get-todo-state) (list "TODO"))
1628          (bh/is-task-p))
1629     "NEXT")
1630        ((and (member (org-get-todo-state) (list "NEXT"))
1631          (bh/is-project-p))
1632     "TODO"))))
1633
1634   (defun bh/punch-in (arg)
1635     "Start continuous clocking and set the default task to the
1636   selected task.  If no task is selected set the Organization task
1637   as the default task."
1638     (interactive "p")
1639     (setq bh/keep-clock-running t)
1640     (if (equal major-mode 'org-agenda-mode)
1641     ;;
1642     ;; We're in the agenda
1643     ;;
1644     (let* ((marker (org-get-at-bol 'org-hd-marker))
1645            (tags (org-with-point-at marker (org-get-tags-at))))
1646       (if (and (eq arg 4) tags)
1647           (org-agenda-clock-in '(16))
1648         (bh/clock-in-organization-task-as-default)))
1649       ;;
1650       ;; We are not in the agenda
1651       ;;
1652       (save-restriction
1653     (widen)
1654     ; Find the tags on the current task
1655     (if (and (equal major-mode 'org-mode) (not (org-before-first-heading-p)) (eq arg 4))
1656         (org-clock-in '(16))
1657       (bh/clock-in-organization-task-as-default)))))
1658
1659   (defun bh/punch-out ()
1660     (interactive)
1661     (setq bh/keep-clock-running nil)
1662     (when (org-clock-is-active)
1663       (org-clock-out))
1664     (org-agenda-remove-restriction-lock))
1665
1666   (defun bh/clock-in-default-task ()
1667     (save-excursion
1668       (org-with-point-at org-clock-default-task
1669     (org-clock-in))))
1670
1671   (defun bh/clock-in-parent-task ()
1672     "Move point to the parent (project) task if any and clock in"
1673     (let ((parent-task))
1674       (save-excursion
1675     (save-restriction
1676       (widen)
1677       (while (and (not parent-task) (org-up-heading-safe))
1678         (when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
1679           (setq parent-task (point))))
1680       (if parent-task
1681           (org-with-point-at parent-task
1682         (org-clock-in))
1683         (when bh/keep-clock-running
1684           (bh/clock-in-default-task)))))))
1685
1686   (defvar bh/organization-task-id "e22cb8bf-07c7-408b-8f60-ff3aadac95e4")
1687
1688   (defun bh/clock-in-organization-task-as-default ()
1689     (interactive)
1690     (org-with-point-at (org-id-find bh/organization-task-id 'marker)
1691       (org-clock-in '(16))))
1692
1693   (defun bh/clock-out-maybe ()
1694     (when (and bh/keep-clock-running
1695            (not org-clock-clocking-in)
1696            (marker-buffer org-clock-default-task)
1697            (not org-clock-resolving-clocks-due-to-idleness))
1698       (bh/clock-in-parent-task)))
1699
1700   ; (add-hook 'org-clock-out-hook 'bh/clock-out-maybe 'append)
1701
1702   (require 'org-id)
1703   (defun bh/clock-in-task-by-id (id)
1704     "Clock in a task by id"
1705     (org-with-point-at (org-id-find id 'marker)
1706       (org-clock-in nil)))
1707
1708   (defun bh/clock-in-last-task (arg)
1709     "Clock in the interrupted task if there is one
1710   Skip the default task and get the next one.
1711   A prefix arg forces clock in of the default task."
1712     (interactive "p")
1713     (let ((clock-in-to-task
1714        (cond
1715         ((eq arg 4) org-clock-default-task)
1716         ((and (org-clock-is-active)
1717           (equal org-clock-default-task (cadr org-clock-history)))
1718          (caddr org-clock-history))
1719         ((org-clock-is-active) (cadr org-clock-history))
1720         ((equal org-clock-default-task (car org-clock-history)) (cadr org-clock-history))
1721         (t (car org-clock-history)))))
1722       (widen)
1723       (org-with-point-at clock-in-to-task
1724     (org-clock-in nil))))
1725
1726
1727   (defun org-export-to-ods ()
1728     (interactive)
1729     (let ((csv-file "data.csv"))
1730       (org-table-export csv-file "orgtbl-to-csv")
1731       (org-odt-convert csv-file "ods" 'open)))
1732
1733   ; allow for zero-width-space to be a break in regexp too
1734   ; (setcar org-emphasis-regexp-components "​ [:space:] \t('\"{")
1735   ; (setcar (nthcdr 1 org-emphasis-regexp-components) "​ [:space:]- \t.,:!?;'\")}\\")
1736   ; (org-set-emph-re 'org-emphasis-regexp-components org-emphasis-regexp-components)
1737
1738   ;; support inserting screen shots
1739   (defun my/org-insert-screenshot ()
1740     "Take a screenshot into a time stamped unique-named file in the
1741   same directory as the org-buffer and insert a link to this file."
1742     (interactive)
1743     (defvar my/org-insert-screenshot/filename)
1744     (setq my/org-insert-screenshot/filename
1745       (read-file-name
1746        "Screenshot to insert: "
1747        nil
1748        (concat (buffer-file-name) "_" (format-time-string "%Y%m%d_%H%M%S") ".png")
1749        )
1750       )
1751     (call-process "import" nil nil nil my/org-insert-screenshot/filename)
1752     (insert (concat "[[" my/org-insert-screenshot/filename "]]"))
1753     (org-display-inline-images))
1754
1755   (defun my/fix-inline-images ()
1756     (when org-inline-image-overlays
1757       (org-redisplay-inline-images)))
1758
1759   (add-hook 'org-babel-after-execute-hook 'my/fix-inline-images)
1760
1761   ;; use xelatex to preview with imagemagick
1762   (add-to-list 'org-preview-latex-process-alist
1763            '(xelateximagemagick
1764         :programs ("xelatex" "convert")
1765         :description "pdf > png"
1766         :message "you need to install xelatex and imagemagick"
1767         :use-xcolor t
1768         :image-input-type "pdf"
1769         :image-output-type "png"
1770         :image-size-adjust (1.0 . 1.0)
1771         :latex-compiler ("xelatex -interaction nonstopmode -output-directory %o %f")
1772         :image-converter ("convert -density %D -trim -antialias %f -quality 100 %O"))
1773            )
1774   ;; use xelatex by default
1775   (setq org-preview-latex-default-process 'xelateximagemagick)
1776
1777   ; from http://orgmode.org/Changes.html
1778   (defun my/org-repair-property-drawers ()
1779     "Fix properties drawers in current buffer.
1780    Ignore non Org buffers."
1781     (interactive)
1782     (when (eq major-mode 'org-mode)
1783       (org-with-wide-buffer
1784        (goto-char (point-min))
1785        (let ((case-fold-search t)
1786          (inline-re (and (featurep 'org-inlinetask)
1787                  (concat (org-inlinetask-outline-regexp)
1788                      "END[ \t]*$"))))
1789      (org-map-entries
1790       (lambda ()
1791         (unless (and inline-re (org-looking-at-p inline-re))
1792           (save-excursion
1793         (let ((end (save-excursion (outline-next-heading) (point))))
1794           (forward-line)
1795           (when (org-looking-at-p org-planning-line-re) (forward-line))
1796           (when (and (< (point) end)
1797                  (not (org-looking-at-p org-property-drawer-re))
1798                  (save-excursion
1799                    (and (re-search-forward org-property-drawer-re end t)
1800                     (eq (org-element-type
1801                      (save-match-data (org-element-at-point)))
1802                     'drawer))))
1803             (insert (delete-and-extract-region
1804                  (match-beginning 0)
1805                  (min (1+ (match-end 0)) end)))
1806             (unless (bolp) (insert "\n"))))))))))))
1807
1808 #+END_SRC
1809 ** Org-Gcal
1810 #+BEGIN_SRC emacs-lisp
1811   (use-package calfw
1812     :ensure f
1813     )
1814   (use-package calfw-org
1815     :ensure f
1816     )
1817   (use-package org-gcal
1818     :ensure f
1819     :config '((if (file-readable-p "~/.hide/org_gcal.el")
1820                   (load-file "~/.hide/org_gcal.el"))
1821               )
1822     )
1823 #+END_SRC
1824 ** appt integration
1825 #+BEGIN_SRC emacs-lisp
1826   (use-package appt
1827     :config
1828     ;; Show notification 10 minutes before event
1829     (setq appt-message-warning-time 10)
1830     ;; Disable multiple reminders
1831     (setq appt-display-interval appt-message-warning-time)
1832     (setq appt-display-mode-line nil)
1833   
1834     ;; add automatic reminders for appointments
1835     (defun my/org-agenda-to-appt ()
1836       (interactive)
1837       (setq appt-time-msg-list nil)
1838       (org-agenda-to-appt))
1839     ;; add reminders when starting emacs
1840     (my/org-agenda-to-appt)
1841     ;; when rebuilding the agenda
1842     (defadvice  org-agenda-redo (after org-agenda-redo-add-appts)
1843       "Pressing `r' on the agenda will also add appointments."
1844       (my/org-agenda-to-appt)
1845       )
1846     ;; when saving all org buffers
1847     (defadvice org-save-all-org-buffers (after org-save-all-org-buffers-add-appts)
1848       "Re-add appts after saving all org buffers"
1849       (my/org-agenda-to-appt))
1850     ;; Display appointments as a window manager notification
1851     (setq appt-disp-window-function 'my/appt-display)
1852     (setq appt-delete-window-function (lambda () t))
1853   
1854     (setq my/appt-notification-app (concat (getenv "HOME") "/bin/appt_notification"))
1855   
1856     (defun my/appt-display (min-to-app new-time msg)
1857       (if (atom min-to-app)
1858       (start-process "my/appt-notification-app" nil my/appt-notification-app min-to-app msg)
1859     (dolist (i (number-sequence 0 (1- (length min-to-app))))
1860       (start-process "my/appt-notification-app" nil my/appt-notification-app
1861                      (nth i min-to-app) (nth i msg))))
1862       )
1863     )
1864
1865
1866 #+END_SRC
1867 ** End use-package
1868 #+BEGIN_SRC emacs-lisp
1869   )
1870 #+END_SRC
1871 * Keybindings
1872 ** Override other things
1873 #+BEGIN_SRC emacs-lisp
1874   ; apparently things like to step on C-;, so we'll use a hack from
1875   ; http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs/5340797#5340797 to fix this
1876
1877   (defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
1878
1879   ; use mc everywhere
1880   (define-key my-keys-minor-mode-map (kbd "C-;") 'mc/mark-all-dwim)
1881   ;; use outline mode keybindings everywhere
1882   ;; (define-key my-keys-minor-mode-map (kbd "C-;") 'my/mydra-outline/body)
1883
1884   (define-minor-mode my-keys-minor-mode
1885     "A minor mode so that my key settings override annoying major modes."
1886     t " my-keys" 'my-keys-minor-mode-map)
1887
1888   (my-keys-minor-mode 1)
1889   (defun my-minibuffer-setup-hook ()
1890     (my-keys-minor-mode 0))
1891
1892   (add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-hook)
1893   (defadvice load (after give-my-keybindings-priority)
1894     "Try to ensure that my keybindings always have priority."
1895     (if (not (eq (car (car minor-mode-map-alist)) 'my-keys-minor-mode))
1896         (let ((mykeys (assq 'my-keys-minor-mode minor-mode-map-alist)))
1897           (assq-delete-all 'my-keys-minor-mode minor-mode-map-alist)
1898           (add-to-list 'minor-mode-map-alist mykeys))))
1899   (ad-activate 'load)
1900 #+END_SRC
1901 ** Goto line
1902 #+BEGIN_SRC emacs-lisp
1903   (global-unset-key "\M-g")
1904   (global-set-key (kbd "M-g l") 'goto-line)
1905 #+END_SRC
1906 * Debian
1907 ** debian-changelog
1908 #+BEGIN_SRC emacs-lisp
1909   (use-package debian-changelog
1910     :mode "debian/changelog"
1911     :config
1912     (setq debian-changelog-mailing-address "don@debian.org")
1913     (setq debian-changelog-full-name "Don Armstrong"))
1914 #+END_SRC
1915 * Misc (uncharacterized)
1916 #+BEGIN_SRC emacs-lisp
1917   (setq calendar-latitude 38.6)
1918   (setq calendar-longitude -121.5)
1919   (setq case-fold-search t)
1920   (setq confirm-kill-emacs (quote y-or-n-p))
1921   (setq cperl-lazy-help-time nil)
1922   (display-time)
1923   (setq display-time-24hr-format t)
1924   (setq display-time-day-and-date t)
1925   (display-time-mode 1)
1926   (global-font-lock-mode 1)
1927   (icomplete-mode 1)
1928   (setq log-edit-keep-buffer t)
1929   (setq mail-user-agent (quote sendmail-user-agent))
1930   (setq markdown-enable-math t)
1931   (setq markdown-follow-wiki-link-on-enter nil)
1932   (setq mutt-alias-file-list (quote ("~/.mutt/aliases" "~/.mail_aliases")))
1933   (setq ps-footer-font-size (quote (8 . 10)))
1934   (setq ps-header-font-size (quote (8 . 10)))
1935   (setq ps-header-title-font-size (quote (10 . 10)))
1936   (setq ps-line-number-color "blue")
1937   (setq ps-print-footer t)
1938   (setq ps-print-footer-frame nil)
1939   (setq ps-print-only-one-header t)
1940   (setq sentence-end "[.?!][]\"')]*\\($\\|   \\| \\)[    
1941   ]*")
1942   (setq sentence-end-double-space nil)
1943   ; enable matching parenthesis
1944   (show-paren-mode 1)
1945   (tool-bar-mode -1)
1946   (setq user-mail-address "don@donarmstrong.com")
1947   (setq vc-delete-logbuf-window nil)
1948   (setq vc-follow-symlinks t)
1949
1950   ;; use git before SVN; use CVS earlier, because I have CVS
1951   ;; repositories inside of git directories
1952   (setq vc-handled-backends (quote (CVS Git RCS SVN SCCS Bzr Hg Mtn Arch)))
1953
1954   ;; switch back to the old primary selection method
1955   (setq x-select-enable-clipboard nil)
1956   (setq x-select-enable-primary t)
1957   ; (setq mouse-drag-copy-region t)
1958
1959   (fset 'perl-mode 'cperl-mode)
1960   ;;(load-file "cperl-mode.el")
1961
1962   (require 'vcl-mode)
1963
1964   (global-set-key "\C-xp" 'server-edit)
1965
1966   (setq-default auto-mode-alist (cons '("\.wml$" . 
1967                     (lambda () (html-mode) (auto-fill-mode)))
1968                   auto-mode-alist))
1969
1970
1971   ; use markdown mode for mdwn files
1972   (add-to-list 'auto-mode-alist '("\\.mdwn$" . markdown-mode))
1973   (add-to-list 'auto-mode-alist '("\\.md$" . markdown-mode))
1974
1975
1976   ;; tramp configuration
1977   (setq tramp-use-ssh-controlmaster-options nil)
1978
1979   ;; self-insert-command hack.
1980   ;;   Without this, "if<SP>" expands to
1981   ;;   if ( -!-) {
1982   ;;   }
1983   ;;   which really should be,
1984   ;;   if (-!-) {
1985   ;;   }
1986
1987
1988
1989   ;(load-library "php-mode")
1990
1991   (setq-default c-indent-level 4)
1992   (setq-default c-brace-imaginary-offset 0)
1993   (setq-default c-brace-offset -4)
1994   (setq-default c-argdecl-indent 4)
1995   (setq-default c-label-offset -4)
1996   (setq-default c-continued-statement-offset 4)
1997   ; tabs are annoying
1998   (setq-default indent-tabs-mode nil)
1999   (setq-default tab-width 4)
2000
2001
2002   ;; (autoload 'php-mode "php-mode" "PHP editing mode" t)
2003   ;; (add-to-list 'auto-mode-alist '("\\.php3?\\'" . php-mode))
2004   ;; (add-to-list 'auto-mode-alist '("\\.phtml?\\'" . php-mode))
2005   ;; (add-to-list 'auto-mode-alist '("\\.php?\\'" . php-mode))
2006   ;; (add-to-list 'auto-mode-alist '("\\.php4?\\'" . php-mode))
2007
2008
2009   (defun insert-date ()
2010     "Insert date at point."
2011     (interactive)
2012     (insert (format-time-string "%A, %B %e, %Y %k:%M:%S %Z")))
2013   (global-set-key "\C-[d" 'insert-date)
2014
2015   (defun unfill-paragraph (arg)
2016     "Pull this whole paragraph up onto one line."
2017     (interactive "*p")
2018     (let ((fill-column 10000))
2019       (fill-paragraph arg))
2020     )
2021
2022   (column-number-mode t)
2023
2024   ; (require 'mode-compile)
2025
2026   (defadvice server-process-filter (after post-mode-message first activate)
2027     "If the buffer is in post mode, overwrite the server-edit
2028       message with a post-save-current-buffer-and-exit message."
2029     (if (eq major-mode 'post-mode)
2030         (message
2031          (substitute-command-keys "Type \\[describe-mode] for help composing; \\[post-save-current-buffer-and-exit] when done."))))
2032                       ; This is also needed to see the magic message.  Set to a higher
2033                       ; number if you have a faster computer or read slower than me.
2034   '(font-lock-verbose 1000)
2035   ;(setq-default server-temp-file-regexp "mutt\(-\|ng-\)")
2036   ; (add-hook 'server-switch-hook 
2037   ;     (function (lambda()
2038   ;             (cond ((string-match "Post" mode-name)
2039   ;                (post-goto-body)))
2040   ;             set-buffer-file-coding-system 'utf-8
2041   ;             )))
2042   ; 
2043
2044   (add-hook 'post-mode-hook
2045         (auto-fill-mode nil)
2046         )
2047   ; abbrev mode settings
2048   ; load abbreviations from 
2049   (setq abbrev-file-name       
2050         "~/.emacs_abbrev_def")
2051
2052   ; read the abbrev file if it exists
2053   (if (file-exists-p abbrev-file-name)
2054       (quietly-read-abbrev-file))
2055
2056   ; for now, use abbrev mode everywhere
2057   (setq default-abbrev-mode t)
2058
2059
2060   (defun insert-function-documentation ()
2061     "Insert function documentation"
2062     (interactive)
2063     (insert-file-contents "/home/don/lib/templates/perl_function_documentation" nil))
2064   (global-set-key "\M-f" 'insert-function-documentation)
2065
2066   (eval-after-load "lilypond-mode" 
2067     '(progn
2068        (load-library "lyqi-mode")
2069        (define-key LilyPond-mode-map "\C-cq" 'lyqi-mode)))
2070
2071   (autoload 'spamassassin-mode "spamassassin-mode" nil t)
2072
2073   (desktop-load-default)
2074   (desktop-read)
2075   '(icomplete-mode on)
2076   (custom-set-faces
2077    ;; custom-set-faces was added by Custom.
2078    ;; If you edit it by hand, you could mess it up, so be careful.
2079    ;; Your init file should contain only one such instance.
2080    ;; If there is more than one, they won't work right.
2081    '(menu ((((type x-toolkit)) (:background "black" :foreground "grey90")))))
2082
2083
2084   (put 'upcase-region 'disabled nil)
2085   (put 'downcase-region 'disabled nil)
2086   (put 'narrow-to-region 'disabled nil)
2087
2088   ; (defun turn-on-flyspell ()
2089   ;    "Force flyspell-mode on using a positive arg.  For use in hooks."
2090   ;    (interactive)
2091   ;    (flyspell-mode 1))
2092
2093
2094    ; Outline-minor-mode key map
2095    (define-prefix-command 'cm-map nil "Outline-")
2096    ; HIDE
2097    (define-key cm-map "q" 'hide-sublevels)    ; Hide everything but the top-level headings
2098    (define-key cm-map "t" 'hide-body)         ; Hide everything but headings (all body lines)
2099    (define-key cm-map "o" 'hide-other)        ; Hide other branches
2100    (define-key cm-map "c" 'hide-entry)        ; Hide this entry's body
2101    (define-key cm-map "l" 'hide-leaves)       ; Hide body lines in this entry and sub-entries
2102    (define-key cm-map "d" 'hide-subtree)      ; Hide everything in this entry and sub-entries
2103    ; SHOW
2104    (define-key cm-map "a" 'show-all)          ; Show (expand) everything
2105    (define-key cm-map "e" 'show-entry)        ; Show this heading's body
2106    (define-key cm-map "i" 'show-children)     ; Show this heading's immediate child sub-headings
2107    (define-key cm-map "k" 'show-branches)     ; Show all sub-headings under this heading
2108    (define-key cm-map "s" 'show-subtree)      ; Show (expand) everything in this heading & below
2109    ; MOVE
2110    (define-key cm-map "u" 'outline-up-heading)                ; Up
2111    (define-key cm-map "n" 'outline-next-visible-heading)      ; Next
2112    (define-key cm-map "p" 'outline-previous-visible-heading)  ; Previous
2113    (define-key cm-map "f" 'outline-forward-same-level)        ; Forward - same level
2114    (define-key cm-map "b" 'outline-backward-same-level)       ; Backward - same level
2115    (global-set-key "\M-o" cm-map)
2116
2117   ; ediff configuration
2118   ; don't use the multi-window configuration
2119   (setq ediff-window-setup-function 'ediff-setup-windows-plain)
2120
2121   ; fix up css mode to not be silly
2122   ; from http://www.stokebloke.com/wordpress/2008/03/21/css-mode-indent-buffer-fix/
2123   (setq cssm-indent-level 4)
2124   (setq cssm-newline-before-closing-bracket t)
2125   (setq cssm-indent-function #'cssm-c-style-indenter)
2126   (setq cssm-mirror-mode nil)
2127
2128   (require 'multi-web-mode)
2129   (setq mweb-default-major-mode 'html-mode)
2130   (setq mweb-tags '((php-mode "<\\?php\\|<\\? \\|<\\?=" "\\?>")
2131                     (js-mode "<script +\\(type=\"text/javascript\"\\|language=\"javascript\"\\)[^>]*>" "</script>")
2132                     (css-mode "<style +type=\"text/css\"[^>]*>" "</style>")))
2133   (setq mweb-filename-extensions '("php" "htm" "html" "ctp" "phtml" "php4" "php5"))
2134   (multi-web-global-mode 1)
2135
2136   ;;; alias the new `flymake-report-status-slim' to
2137   ;;; `flymake-report-status'
2138   (defalias 'flymake-report-status 'flymake-report-status-slim)
2139   (defun flymake-report-status-slim (e-w &optional status)
2140     "Show \"slim\" flymake status in mode line."
2141     (when e-w
2142       (setq flymake-mode-line-e-w e-w))
2143     (when status
2144       (setq flymake-mode-line-status status))
2145     (let* ((mode-line " Φ"))
2146       (when (> (length flymake-mode-line-e-w) 0)
2147         (setq mode-line (concat mode-line ":" flymake-mode-line-e-w)))
2148       (setq mode-line (concat mode-line flymake-mode-line-status))
2149       (setq flymake-mode-line mode-line)
2150       (force-mode-line-update)))
2151
2152   ; load sql-indent when sql is loaded
2153   (eval-after-load "sql"
2154     '(load-library "sql-indent"))
2155
2156   ; fix up tmux xterm keys
2157   ; stolen from http://unix.stackexchange.com/questions/24414/shift-arrow-not-working-in-emacs-within-tmux
2158   (defun fix-up-tmux-keys ()
2159       "Fix up tmux xterm keys"
2160       (if (getenv "TMUX")
2161           (progn
2162             (let ((x 2) (tkey ""))
2163               (while (<= x 8)
2164                 ;; shift
2165                 (if (= x 2)
2166                     (setq tkey "S-"))
2167                 ;; alt
2168                 (if (= x 3)
2169                     (setq tkey "M-"))
2170                 ;; alt + shift
2171                 (if (= x 4)
2172                     (setq tkey "M-S-"))
2173                 ;; ctrl
2174                 (if (= x 5)
2175                     (setq tkey "C-"))
2176                 ;; ctrl + shift
2177                 (if (= x 6)
2178                     (setq tkey "C-S-"))
2179                 ;; ctrl + alt
2180                 (if (= x 7)
2181                     (setq tkey "C-M-"))
2182                 ;; ctrl + alt + shift
2183                 (if (= x 8)
2184                     (setq tkey "C-M-S-"))
2185
2186                 ;; arrows
2187                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d A" x)) (kbd (format "%s<up>" tkey)))
2188                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d B" x)) (kbd (format "%s<down>" tkey)))
2189                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d C" x)) (kbd (format "%s<right>" tkey)))
2190                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d D" x)) (kbd (format "%s<left>" tkey)))
2191                 ;; home
2192                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d H" x)) (kbd (format "%s<home>" tkey)))
2193                 ;; end
2194                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d F" x)) (kbd (format "%s<end>" tkey)))
2195                 ;; page up
2196                 (define-key key-translation-map (kbd (format "M-[ 5 ; %d ~" x)) (kbd (format "%s<prior>" tkey)))
2197                 ;; page down
2198                 (define-key key-translation-map (kbd (format "M-[ 6 ; %d ~" x)) (kbd (format "%s<next>" tkey)))
2199                 ;; insert
2200                 (define-key key-translation-map (kbd (format "M-[ 2 ; %d ~" x)) (kbd (format "%s<delete>" tkey)))
2201                 ;; delete
2202                 (define-key key-translation-map (kbd (format "M-[ 3 ; %d ~" x)) (kbd (format "%s<delete>" tkey)))
2203                 ;; f1
2204                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d P" x)) (kbd (format "%s<f1>" tkey)))
2205                 ;; f2
2206                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d Q" x)) (kbd (format "%s<f2>" tkey)))
2207                 ;; f3
2208                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d R" x)) (kbd (format "%s<f3>" tkey)))
2209                 ;; f4
2210                 (define-key key-translation-map (kbd (format "M-[ 1 ; %d S" x)) (kbd (format "%s<f4>" tkey)))
2211                 ;; f5
2212                 (define-key key-translation-map (kbd (format "M-[ 15 ; %d ~" x)) (kbd (format "%s<f5>" tkey)))
2213                 ;; f6
2214                 (define-key key-translation-map (kbd (format "M-[ 17 ; %d ~" x)) (kbd (format "%s<f6>" tkey)))
2215                 ;; f7
2216                 (define-key key-translation-map (kbd (format "M-[ 18 ; %d ~" x)) (kbd (format "%s<f7>" tkey)))
2217                 ;; f8
2218                 (define-key key-translation-map (kbd (format "M-[ 19 ; %d ~" x)) (kbd (format "%s<f8>" tkey)))
2219                 ;; f9
2220                 (define-key key-translation-map (kbd (format "M-[ 20 ; %d ~" x)) (kbd (format "%s<f9>" tkey)))
2221                 ;; f10
2222                 (define-key key-translation-map (kbd (format "M-[ 21 ; %d ~" x)) (kbd (format "%s<f10>" tkey)))
2223                 ;; f11
2224                 (define-key key-translation-map (kbd (format "M-[ 23 ; %d ~" x)) (kbd (format "%s<f11>" tkey)))
2225                 ;; f12
2226                 (define-key key-translation-map (kbd (format "M-[ 24 ; %d ~" x)) (kbd (format "%s<f12>" tkey)))
2227                 ;; f13
2228                 (define-key key-translation-map (kbd (format "M-[ 25 ; %d ~" x)) (kbd (format "%s<f13>" tkey)))
2229                 ;; f14
2230                 (define-key key-translation-map (kbd (format "M-[ 26 ; %d ~" x)) (kbd (format "%s<f14>" tkey)))
2231                 ;; f15
2232                 (define-key key-translation-map (kbd (format "M-[ 28 ; %d ~" x)) (kbd (format "%s<f15>" tkey)))
2233                 ;; f16
2234                 (define-key key-translation-map (kbd (format "M-[ 29 ; %d ~" x)) (kbd (format "%s<f16>" tkey)))
2235                 ;; f17
2236                 (define-key key-translation-map (kbd (format "M-[ 31 ; %d ~" x)) (kbd (format "%s<f17>" tkey)))
2237                 ;; f18
2238                 (define-key key-translation-map (kbd (format "M-[ 32 ; %d ~" x)) (kbd (format "%s<f18>" tkey)))
2239                 ;; f19
2240                 (define-key key-translation-map (kbd (format "M-[ 33 ; %d ~" x)) (kbd (format "%s<f19>" tkey)))
2241                 ;; f20
2242                 (define-key key-translation-map (kbd (format "M-[ 34 ; %d ~" x)) (kbd (format "%s<f20>" tkey)))
2243
2244                 (setq x (+ x 1))
2245                 ))
2246             )
2247         )
2248       )
2249   ; (add-hook 'tty-setup-hook 'fix-up-tmux-keys)
2250
2251   ; procmailmode configuration
2252   (load "procmail_mode")
2253
2254   (load "mode-line-cleaner")
2255
2256   (defadvice ask-user-about-supersession-threat (around ask-user-about-supersession-threat-if-necessary)
2257     "Call ask-user-about-supersession-threat only if the buffer is actually obsolete."
2258     (if (or (buffer-modified-p)
2259             (verify-visited-file-modtime)
2260             (< (* 8 1024 1024) (buffer-size))
2261             (/= 0 (call-process-region 1 (+ 1 (buffer-size)) "diff" nil nil nil "-q" (buffer-file-name) "-")))
2262         ad-do-it
2263       (clear-visited-file-modtime)
2264       (not-modified)))
2265   (ad-activate 'ask-user-about-supersession-threat)
2266
2267   ; apparently things like to step on C-;, so we'll use a hack from
2268   ; http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs/5340797#5340797 to fix this
2269
2270   (defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
2271
2272   ; use iedit everywhere
2273   (define-key my-keys-minor-mode-map (kbd "C-;") 'mc/mark-all-dwim)
2274
2275   (define-minor-mode my-keys-minor-mode
2276     "A minor mode so that my key settings override annoying major modes."
2277     t " my-keys" 'my-keys-minor-mode-map)
2278
2279   (my-keys-minor-mode 1)
2280   (defun my-minibuffer-setup-hook ()
2281     (my-keys-minor-mode 0))
2282
2283   (add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-hook)
2284   (defadvice load (after give-my-keybindings-priority)
2285     "Try to ensure that my keybindings always have priority."
2286     (if (not (eq (car (car minor-mode-map-alist)) 'my-keys-minor-mode))
2287         (let ((mykeys (assq 'my-keys-minor-mode minor-mode-map-alist)))
2288           (assq-delete-all 'my-keys-minor-mode minor-mode-map-alist)
2289           (add-to-list 'minor-mode-map-alist mykeys))))
2290   (ad-activate 'load)
2291   (global-set-key "\M- " 'hippie-expand)
2292
2293 #+END_SRC
2294
2295 * Server
2296 #+BEGIN_SRC emacs-lisp
2297   (unless (server-running-p)
2298   (server-start))
2299 #+END_SRC
2300
2301
2302
2303 * END
2304 #+BEGIN_SRC emacs-lisp
2305   (provide 'don-configuration)
2306 #+END_SRC