From aab08e87d24b8e264ec6a35014776ba79e598355 Mon Sep 17 00:00:00 2001 From: Dale Sedivec Date: Thu, 30 Mar 2023 18:56:31 -0500 Subject: [PATCH] Make el-patch-get use the only variant by default I make significant use of `el-patch-validate` non-interactively in my init file. These usages broke when I updated to the latest version of el-patch, because an invocation that used to work such as (el-patch-validate 'some-function 'defun 'nomsg) was now unable to look up the patch I had just applied to `some-function`. I believe this is happening because `el-patch-validate` was allowing you to omit `variant`, and `nil` is not a possible variant. From reading the code, I believe all variants will be a cons of `(advice name)`. Therefore when `el-patch-validate` calls `(el-patch-get 'some-function 'defun nil)`, it will never get a result, and `el-patch-validate` will always fail. `(nil)` *could* perhaps be called "the default variant", but I didn't see it hard coded anywhere else in el-patch, so I wasn't about to start here. Instead, I made `el-patch-get` (which also makes the `variant` argument optional) use the one and only variant present for the given name. If more than one variant is present, and the caller didn't supply `variant`, `el-patch-get` now signals an error. I picked `wrong-type-argument` because it was the standard error closest in spirit to what I was trying to convey, and I didn't feel that this case called for defining a whole new error. --- CHANGELOG.md | 7 +++++++ el-patch.el | 24 ++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 848b992..7ec48ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog]. +## Unreleased +### Enhancements +* `el-patch-get` will try and return the only variant of a function + when you don't provide a specific `variant`. This makes it so that + you can call functions such as `el-patch-validate` non-interactively + without providing `variant`. + ## 3.0 (released 2022-04-17) ### Breaking changes * The arguments to `el-patch-feature` after the feature name are no diff --git a/el-patch.el b/el-patch.el index a1dfc99..365f425 100644 --- a/el-patch.el +++ b/el-patch.el @@ -1059,10 +1059,26 @@ and TYPE is a symbol `defun', `defmacro', etc. If the patch could not be found, return nil. If VARIANT is provided, select that variant of the patch. This is -useful only if patches were defined using `el-patch-variant'." - (condition-case nil - (gethash variant (gethash type (gethash name el-patch--patches))) - (error nil))) +useful only if patches were defined using `el-patch-variant'. If +VARIANT is not provided and more than one variant is present for +the patch, `wrong-type-argument' is signaled." + (let* ((types (gethash name el-patch--patches)) + (variants (and types (gethash type types)))) + (cond + ((not variants) + nil) + (variant + (gethash variant variants)) + (t + (let (only-value) + (maphash (lambda (_ value) + (when only-value + (error 'wrong-type-argument + "Must choose a variant for `%s' type %s." + name type)) + (setq only-value value)) + variants) + only-value))))) (defun el-patch--select-patch () "Use `completing-read' to select a patched function.