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