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