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