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