Elisp: Function Keyword Parameters (Named Parameters)
Function Keyword Parameters (Named Parameters)
Emacs lisp does not support named parameter.
But by emacs convention, it is emulated using Keyword Symbol as parameter names and Property List mechanism.
;; -*- coding: utf-8; lexical-binding: t; -*- (defun my-test (first-arg &rest keyword-args) "demo of using plist as optional named params with default values. keyword-args is any number of pairs of key and value. The following keys are recognized and their default: :a 1 :b nil :c t Any other key value are ignored. version: 2026-01-03" (interactive) (let ((keyword-defaults-alist (list (cons :a 1) (cons :b nil) (cons :c t))) keyword-val-alist) ;; keyword-defaults-alist is a association list, of defaults ;; keyword-val-alist is a association list, of actual values (mapc (lambda (x) (let ((xkey (car x))) (push (cons xkey (if (plist-member keyword-args xkey) (plist-get keyword-args xkey) (alist-get xkey keyword-defaults-alist))) keyword-val-alist))) keyword-defaults-alist) (message "first-arg is %s" first-arg) (message "keyword-val-alist is %s" keyword-val-alist))) ;; s------------------------------ ;; test (my-test 3) "keyword-val-alist is ((:c . t) (:b) (:a . 1))" (my-test 3 :a t) "keyword-val-alist is ((:c . t) (:b) (:a . t))" (my-test 3 :b t) "keyword-val-alist is ((:c . t) (:b . t) (:a . 1))" (my-test 3 :a t :b t) "keyword-val-alist is ((:c . t) (:b . t) (:a . t))" (my-test 3 :a nil :b nil) "keyword-val-alist is ((:c . t) (:b) (:a))" (my-test 3 :a 5 :b 6) "keyword-val-alist is ((:c . t) (:b . 6) (:a . 5))" (my-test 3 :c 4) "keyword-val-alist is ((:c . 4) (:b) (:a . 1))"
Elisp, Function
- Elisp: Define Function
- Elisp: Define a Command
- Elisp: Function Parameters (optional, rest)
- Elisp: Function Keyword Parameters (Named Parameters)
- Elisp: Docstring Markup
- Elisp: Lambda Function
- Elisp: Exit Loop or Function (throw, catch)
- Elisp: Apply Function, funcall, identity
- Elisp: Types of Functions