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