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