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