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