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