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