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