From d835d8bf8c1fa2158aa34fd4d99e9fdd3449c593 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Mon, 12 Sep 2016 19:59:26 +0200 Subject: use-package: Add :if-installed keyword With the :if-installed keyword it is possible to check, if a package is installed before binding keys or creating autoloads. This helps of optional packages that overwrites built-in keys (like helm with M-x). --- init.d/main.org | 61 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/init.d/main.org b/init.d/main.org index 1ec2e9f..a3efc99 100644 --- a/init.d/main.org +++ b/init.d/main.org @@ -54,7 +54,39 @@ requirement for this configuration, so I install it here. (unless (package-installed-p 'use-package) (package-install 'use-package)) (setq use-package-verbose t) - (require 'use-package)) + (require 'use-package) + <>) +#+end_src + +*** :if-installed + +I extend the basic set of use-package keywords with a new +one: =:if-installed t= only enables the bind and auto-load hooks if +the package is installed (checked with =package-installed-p=). It is +a shorter way to write =:if (package-installed-p "")=. You +can give another package name to check as argument instead of =t=. + +#+begin_src emacs-lisp :noweb-ref use-package :tangle no + (setq use-package-keywords (cons :if-installed use-package-keywords)) + + (defun use-package-normalize/:if-installed (name keyword args) + (if (null args) + t + (use-package-only-one (symbol-name keyword) args + (lambda (label arg) + (if (symbolp arg) + arg + (use-package-error + (concat ":if-installed wants an optional package name " + "(an unquoted symbol name)"))))))) + + (defun use-package-handler/:if-installed (name keyword ensure rest state) + (let ((body (use-package-process-keywords name rest state)) + (package-name (or (and (eq ensure t) (use-package-as-symbol name)) ensure))) + `((if (package-installed-p ',package-name) + (progn ,@body) + ,(when (bound-and-true-p use-package-verbose) + `(message ,(format "Configuring package %s...SKIPPED" package-name))))))) #+end_src ** auto-compile @@ -1185,8 +1217,8 @@ the window. It is not optimal, because it does not display the possibility. Maybe I will replace it with a custom hydra. #+begin_src emacs-lisp -(when (locate-library "ace-window") (use-package ace-window + :if-installed t :bind (("C-x o" . ace-window) ("C-x C-o" . aw-flip-window)) :config @@ -1198,7 +1230,7 @@ possibility. Maybe I will replace it with a custom hydra. (?v aw-split-window-vert " Ace - Split Vert Window") (?h aw-split-window-horz " Ace - Split Horz Window") (?i delete-other-windows " Ace - Maximize Window") - (?o delete-other-windows))))) + (?o delete-other-windows)))) #+end_src ** auto-complete @@ -1209,18 +1241,19 @@ when pressing the shortcut (but the auto-complete-mode has to be enabled before). #+begin_src emacs-lisp - (use-package auto-complete - :bind ("M-" . auto-complete) - :demand t - :diminish auto-complete-mode - :config - (require 'auto-complete-config) - (ac-config-default) + (use-package auto-complete + :if-installed t + :bind ("M-" . auto-complete) + :demand t + :diminish auto-complete-mode + :config + (require 'auto-complete-config) + (ac-config-default) - (setq ac-comphist-file (alex/cache-file "ac-comphist.dat") - ac-quick-help-delay 1.0) + (setq ac-comphist-file (alex/cache-file "ac-comphist.dat") + ac-quick-help-delay 1.0) - (global-auto-complete-mode t)) + (global-auto-complete-mode t)) #+end_src ** TODO company-mode @@ -1410,6 +1443,7 @@ every time I start emacs. #+begin_src emacs-lisp (use-package helm + :if-installed t :bind (("M-x" . helm-M-x) ("M-X" . execute-extended-command) ("C-x f" . helm-mini) @@ -1417,7 +1451,6 @@ every time I start emacs. ("C-x C-b" . helm-buffers-list) ("M-y" . helm-show-kill-ring)) :demand t - :ensure t :diminish helm-mode :config <> -- cgit v1.2.3