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