;; kwcall.el --- Keyword-index `funcall' -*- lexical-binding: t -*- ;; Author: Philip Kaludercic ;; Version: $Id: kwcall.el,v 1.1 2023/03/20 20:34:58 oj14ozun Exp $ ;;; Commentary ;; Some functions in Emacs Lisp have too many argument, due to ;; historical circumstances (eg. `quail-define-package') that make ;; them difficult to invoke. This is a little and brittle hack to ;; convert optional arguments into keyword arguments. ;;; Code: ;; Example function: (defun foo (one two three) (list three two one)) ;; The trick: (defun kwcall (fn &rest keys) "Call FN with indexed parameter names KEYS." (unless (symbolp fn) (error "Expected %S to be a symbol" fn)) (let* ((arglist (help-function-arglist fn t)) args) (unless (listp arglist) (error "Failed to get determine arglist")) (when (memq '&rest arglist) (error ":funcall is not compatible with varargs")) (dolist (arg arglist) (push (plist-get keys (intern (format ":%s" arg))) args)) (apply fn (nreverse args)))) ;; Example invocation: (kwcall #'foo :three 3 :one 'I :two "two") ;;=> (3 "two" I)