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