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