Emacs: Improve hook setup with hook-helpers
Alan Pearce alan@alanpearce.uk
Fri, 28 Apr 2017 14:25:04 +0200
1 files changed, 36 insertions(+), 30 deletions(-)
jump to
M emacs/.emacs.d/init.org → emacs/.emacs.d/init.org
@@ -81,6 +81,18 @@ use-package-always-ensure t package-enable-at-startup nil) #+END_SRC +** Helpers + +*** Hook Helpers + +An improvement over add-hook with lamda functions that allows +modification and removal, without the boilerplate of an extra function +definition. + +#+BEGIN_SRC emacs-lisp +(use-package hook-helpers) +#+END_SRC + * Customize I don’t really like using customize for normal configuration. Instead, I use it for things that get saved automatically. That’s why @@ -758,8 +770,8 @@ (use-package smart-tabs-mode :defer 1 :config (progn (smart-tabs-insinuate 'c 'cperl 'python) - (add-hook 'php-mode-hook (lambda () - (smart-tabs-mode indent-tabs-mode))))) + (define-hook-helper php-mode () + (smart-tabs-mode indent-tabs-mode)))) #+END_SRC ** editorconfig @@ -777,12 +789,10 @@ #+BEGIN_SRC emacs-lisp (use-package dtrt-indent :config (progn - (defun ap/dtrt-adapt-if-needed () - (unless editorconfig-mode + (define-hook-helper after-change-major-mode () + (unless (and (fboundp editorconfig-mode) + editorconfig-mode) (dtrt-indent-adapt))) - (if (fboundp #'editorconfig-mode) - (add-hook 'after-change-major-mode-hook #'ap/dtrt-adapt-if-needed) - (add-hook 'after-change-major-mode-hook #'dtrt-indent-adapt)) (defadvice dtrt-indent-try-set-offset (after toggle-smart-tabs activate) (smart-tabs-mode (or indent-tabs-mode -1))))) #+END_SRC @@ -940,7 +950,7 @@ :diminish "Cmpl" :bind (("C-<tab>" . company-complete) ("TAB" . company-indent-or-complete-common)) :init (progn - (add-hook 'prog-mode-hook #'company-mode) + (add-hook 'prog-mode-hook #'company-mode-on) (setq company-backends '(company-bbdb company-web-html company-tern company-nxml company-css company-eclim company-semantic company-elisp company-clang company-xcode company-cmake company-capf company-files (company-gtags @@ -1705,10 +1715,9 @@ :commands company-go :config (progn (setq company-go-show-annotation t)) :init (progn - (defun ap/company-go-setup () + (define-hook-helper go-mode () (set (make-local-variable 'company-backends) - '(company-go))) - (add-hook 'go-mode-hook #'ap/company-go-setup))) + '(company-go))))) (use-package go-eldoc :commands go-eldoc-setup @@ -1769,7 +1778,11 @@ "Hook run when entering any Lisp mode." :type 'hook :group 'lisp) -(defun ap/lisp-setup () +(create-hook-helper lisp-mode-setup () + :hooks (emacs-lisp-mode-hook + scheme-mode-hook + lisp-mode-hook + clojure-mode-hook) (run-hooks 'lisp-mode-common-hook)) #+END_SRC @@ -1792,7 +1805,6 @@ Customise the modeline-display of =emacs-lisp-mode=. Then make sure it runs the common lisp hooks. #+BEGIN_SRC emacs-lisp -(add-hook 'emacs-lisp-mode-hook #'ap/lisp-setup) (add-hook 'emacs-lisp-mode-hook #'eldoc-mode) #+END_SRC @@ -1813,8 +1825,8 @@ (use-package ielm :defer t :ensure nil :config (progn - (add-hook 'ielm-mode-hook (lambda () - (run-hooks 'lisp-mode-common-hook))))) + (define-hook-helper ielm-mode () + (run-hooks 'lisp-mode-common-hook)))) #+END_SRC *** Scheme & Lisp @@ -1822,12 +1834,9 @@ I don’t work with these as often as I would like #+BEGIN_SRC emacs-lisp -(add-hook 'scheme-mode-hook #'ap/lisp-setup) -(add-hook 'lisp-mode-hook #'ap/lisp-setup) -(defun set-common-lisp-indentation () +(define-hook-helper lisp-mode () (set (make-local-variable 'lisp-indent-function) #'common-lisp-indent-function)) -(add-hook 'lisp-mode-hook #'set-common-lisp-indentation) #+END_SRC **** geiser @@ -1866,8 +1875,8 @@ #+BEGIN_SRC emacs-lisp (use-package clojure-mode :defer t :init (progn - (add-hook 'cider-repl-mode-hook (lambda () (highlight-changes-mode -1))) - (add-hook 'clojure-mode-hook #'ap/lisp-setup))) + (define-hook-helper cider-repl-mode () + (highlight-changes-mode -1)))) (use-package clj-refactor :defer t @@ -1875,9 +1884,8 @@ :functions (clj-refactor-mode cljr-add-keybindings-with-prefix) :config (progn (cljr-add-keybindings-with-prefix "C-c C-m")) :init (progn - (defun turn-on-clj-refactor-mode () - (clj-refactor-mode 1)) - (add-hook 'clojure-mode-hook #'turn-on-clj-refactor-mode))) + (define-hook-helper clojure-mode () + (clj-refactor-mode 1)))) #+END_SRC **** cider @@ -2025,10 +2033,8 @@ :if (executable-find "tern") :defer 5 :config (progn (setq tern-command (list (executable-find "tern"))) - (defun ap/enable-tern () - (tern-mode 1)) - (add-hook 'js2-mode-hook #'ap/enable-tern) - (add-hook 'web-mode-hook #'ap/enable-tern))) + (add-hook 'js2-mode-hook #'tern-mode-enable) + (add-hook 'web-mode-hook #'tern-mode-enable))) (with-eval-after-load 'tern (use-package company-tern)) @@ -2216,8 +2222,8 @@ eshell-prompt-function (lambda () (concat (eshell/pwd) "\n$ "))) - (add-hook 'eshell-load-hook (lambda () - (bind-key "C-c C-l" #'counsel-esh-history eshell-mode-map))))) + (define-hook-helper eshell-load () + (bind-key "C-c C-l" #'counsel-esh-history eshell-mode-map)))) (use-package em-smart :ensure nil