Skip to content

Commit

Permalink
Mitigate el-patch-pre-validate-hook boilerplate
Browse files Browse the repository at this point in the history
See #10.
  • Loading branch information
raxod502 committed Jul 23, 2017
1 parent 3175de1 commit bbc5edc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ The format is based on [Keep a Changelog].
linting, and table-of-contents updating.
* `el-patch` now checks for byte-compilation and checkdoc warnings on
Travis CI ([#7](https://github.com/raxod502/el-patch/issues/7)).
* New macro `el-patch-feature` (unrelated to the old obsolete
`el-patch-feature` macro that was removed) which is a convenient way
to add feature-requiring functions to `el-patch-pre-validate-hook`
([#10](https://github.com/raxod502/el-patch/issues/10)).
* New user option `el-patch-require-function` for advanced users only.
It allows you to integrate `el-patch-feature` with your package
manager, and other things like that.

### Removed
* Deprecated patch directive `el-patch-feature` has been removed.
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ even enable a custom minor mode). This hook is run before
`el-patch-validate-all`, and also before `el-patch-validate` when you
provide a prefix argument.

Since defining some patches after a feature is loaded is such a common
operation, `el-patch` provides a convenience macro for it:
`el-patch-feature`. You can call this macro with an (unquoted) feature
name, and it will create a function that loads that feature, and add
it to `el-patch-pre-validate-hook` for you.

If you don't want all of your patches to be defined all the time, you
can put some functions in `el-patch-post-validate-hook` to disable
them again. For some examples of how to use these hooks, check out
Expand Down
30 changes: 30 additions & 0 deletions el-patch.el
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ before evaluating the new one."
:type 'boolean
:group 'el-patch)

(defcustom el-patch-require-function #'require
"Function to `require' a feature in `el-patch-pre-validate-hook'.
This is passed all of the arguments of `el-patch-feature' as
quoted literals, and it should load the feature. This function
might be useful if, for example, some of your features are
provided by lazy-installed packages, and those packages need to
be installed before the features can be loaded."
:type 'function
:group 'el-patch)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Internal variables

Expand Down Expand Up @@ -525,6 +535,26 @@ etc., which may contain patch directives."
(indent defun))
`(el-patch--definition ',(cons #'define-minor-mode args)))

;; For convenience.

;;;###autoload
(defmacro el-patch-feature (feature &rest args)
"Declare that some patches are only defined after FEATURE is loaded.
This is a convenience macro that creates a function for invoking
`require' on that feature, and then adds it to
`el-patch-pre-validate-hook' so that your patches are loaded and
`el-patch' can properly validate them.
FEATURE should be an unquoted symbol. ARGS, if given, are passed
as quoted literals along with FEATURE to
`el-patch-require-function' when `el-patch-validate-all' is
called."
(let ((defun-name (intern (format "el-patch-require-%S" feature))))
`(progn
(defun ,defun-name ()
(apply el-patch-require-function ',feature ',args))
(add-hook 'el-patch-pre-validate-hook #',defun-name))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Patch directives

Expand Down

0 comments on commit bbc5edc

Please sign in to comment.