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