Re: Copying autoloads
Philip Kaludercic <[email protected]>
| Newsgroups | gmane.emacs.erc.general,gmane.emacs.devel |
|---|---|
| Message-ID | <[email protected]> |
Stefan Monnier <[email protected]> writes: > Philip Kaludercic [2022-07-25 18:56:39] wrote: >> (defun get-compatibility-func (name) >> "Return the function NAME of a compatibility alias." >> (let* ((compat (intern-soft (format "compat-%s" name)))) >> (or (symbol-function (if (fboundp name) name compat)) >> (error "No definition for %S could be found" name)))) > > Side note: if `name` is autoloaded this will not return a "valid" value, > in the sense that the (autoload ...) value stored in `symbol-function` > is only usable when placed inside `name` and not elsewhere (e.g. you > can't `funcall` it, and if you store it in another symbol, calling the > function under that other symbol will probably fail). > > Autoloads are somewhat hackish in this sense. Would getting rid of the `symbol-function' simplify the issue? Here would be an updated proposal: --8<---------------cut here---------------start------------->8--- (defun get-compatibility-func (name &optional version) "Return the function NAME of a compatibility alias. If the optional argument VERSION is non-nil, it is interpreted as a string designating from what version on the actual function may be used, even if the function may have existed prior to that." ;; Compat is an ELPA package that provides compatibility code for ;; older versions of Emacs. To avoid overriding or advising ;; existing functionality, updates to existing functions are ;; provided by defining a "prefixed" function, starting with ;; `compat-'. The following code will attempt to return a symbol ;; designating NAME if it is defined (and allowed according to ;; VERSION), otherwise fall back to one provided by Compat. ;; ;; The assumption here is that either the user is tracking a recent ;; enough version of Emacs to have all the necessary definition, or ;; they are using a core package updated from ELPA, in which case ;; Compat has been added as a dependency. Otherwise an error is ;; signalled as early as possible. ;; ;; See https://elpa.gnu.org/packages/compat.html for more details. (let ((compat (intern-soft (format "compat-%s" name))) (func (symbol-function name))) (cond ((and (or (null version) (version<= version emacs-version)) (fboundp name)) name) ((fboundp compat) compat) ((error "No definition for %S could be found" name))))) --8<---------------cut here---------------end--------------->8--- The function could be added to compat.el too, so that any core package that depends on ELPA can make use of it.