diff --git a/core/core-configuration-layer.el b/core/core-configuration-layer.el index a61f078bd..86eb68b40 100644 --- a/core/core-configuration-layer.el +++ b/core/core-configuration-layer.el @@ -145,6 +145,24 @@ LAYER has to be installed for this method to work properly." "Evaluate the `toggle' slot of passed PKG." (let ((toggle (oref pkg :toggle))) (eval toggle))) +(defmethod cfgl-package-get-safe-owner ((pkg cfgl-package)) + "Safe method to return the name of the layer which owns PKG." + ;; The owner of a package is the first *used* layer in `:owners' slot. + ;; Note: for packages in `configuration-layer--packages' the owner is always + ;; the car of the `:owners' slot. + ;; An example where it is not necessarily the case is in + ;; `helm-spacemacs-help-all-packages' which we can find in the helm + ;; layer. + ;; For performance reason `cfgl-package-get-safe-owner' is not used in the + ;; layer system itself. This functions should be only used outside of the + ;; system in order to safely get the true owner of a layer. + (let ((layers (oref pkg :owners))) + (while (and (consp layers) + (not (configuration-layer/layer-usedp (car layers)))) + (pop layers)) + (when (configuration-layer/layer-usedp (car layers)) + (car layers)))) + (defvar configuration-layer--elpa-archives '(("melpa" . "melpa.org/packages/") ("org" . "orgmode.org/elpa/") diff --git a/tests/core/core-configuration-layer-utest.el b/tests/core/core-configuration-layer-utest.el index c19bedfc3..9d8534dd5 100644 --- a/tests/core/core-configuration-layer-utest.el +++ b/tests/core/core-configuration-layer-utest.el @@ -66,6 +66,20 @@ (package-toggle 'other)) (should (null (cfgl-package-enabledp pkg))))) +;; method: cfgl-package-get-safe-owner + +(ert-deftest test-cfgl-package-get-safe-owner--return-car () + (let ((pkg (cfgl-package "testpkg" :name 'testpkg :owners '(layer1 layer2))) + (configuration-layer--layers `(,(cfgl-layer "layer1" :name 'layer1) + ,(cfgl-layer "layer2" :name 'layer2)))) + (should (eq 'layer1 (cfgl-package-get-safe-owner pkg))))) + +(ert-deftest test-cfgl-package-get-safe-owner--return-cadr () + (let ((pkg (cfgl-package "testpkg" :name 'testpkg :owners '(layer1 layer2))) + ;; layer1 is not used so it cannot be the owner + (configuration-layer--layers `(,(cfgl-layer "layer2" :name 'layer2)))) + (should (eq 'layer2 (cfgl-package-get-safe-owner pkg))))) + ;; --------------------------------------------------------------------------- ;; configuration-layer//resolve-package-archives ;; ---------------------------------------------------------------------------