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