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