From d82a74c3da50200a7868022c86e11080235e3272 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 28 Apr 2017 14:25:04 +0200 Subject: Emacs: Improve hook setup with hook-helpers --- emacs/.emacs.d/init.org | 66 +++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/emacs/.emacs.d/init.org b/emacs/.emacs.d/init.org index 098883f0..9b39b279 100644 --- a/emacs/.emacs.d/init.org +++ b/emacs/.emacs.d/init.org @@ -81,6 +81,18 @@ the buffer to be empty. 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 @@ with spaces. Perfect! :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 @@ guesses the correct settings for me. #+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 @@ seems to work perfectly well for me. :bind (("C-" . 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 @@ Go has a few packages to inter-operate with other emacs packages. :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 which I trigger in every lispy-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 @@ Interactive elisp :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 @@ Interactive elisp 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 @@ A REPL thing (and more) for 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 @@ A REPL thing (and more) for Lisp. :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 @@ completions, besides other IDE-like things. :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 @@ nice, when I remember to use it. (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 -- cgit 1.4.1