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