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