From f30e75e3b14b3cb955d2dcd11f210cfaceef07b6 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <3946dbb956afcd005cba0f1899acae5a74a109d4.1682849409.git.yantar92@posteo.net> References: <3946dbb956afcd005cba0f1899acae5a74a109d4.1682849409.git.yantar92@posteo.net> From: Ihor Radchenko Date: Mon, 3 Apr 2023 10:40:00 +0200 Subject: [PATCH v4 2/8] org-compat: Add optional compat.el support * lisp/org-compat.el (compat): Load Compat library, when available. (org-compat-function): (org-compat-call): Add compatibility macros available even when Compat is not available (Org is a part of Emacs). --- lisp/org-compat.el | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lisp/org-compat.el b/lisp/org-compat.el index 34b27546d..9c286961a 100644 --- a/lisp/org-compat.el +++ b/lisp/org-compat.el @@ -94,6 +94,48 @@ (defvar org-table-tab-recognizes-table.el) (defvar org-table1-hline-regexp) (defvar org-fold-core-style) + +;;; compat.el + +;; Do not throw an error when not available - assume latest Emacs +;; version (built-in Org). +(require 'compat nil 'noerror) + +;; Provide compatibility macros when we are a part of Emacs. +;; See https://elpa.gnu.org/packages/doc/compat.html#Usage + +(defmacro org-compat-function (fun) + "Return compatibility function symbol for FUN. + +If the Emacs version provides a sufficiently recent version of +FUN, the symbol FUN is returned itself. Otherwise the macro +returns the symbol of a compatibility function which supports the +behavior and calling convention of the current stable Emacs +version. For example Compat 29.1 will provide compatibility +functions which implement the behavior and calling convention of +Emacs 29.1. + +See also `org-compat-call' to directly call compatibility functions." + (let ((compat (intern (format "compat--%s" fun)))) + `#',(if (fboundp compat) compat fun))) + +(defmacro org-compat-call (fun &rest args) + "Call compatibility function or macro FUN with ARGS. + +A good example function is `plist-get' which was extended with an +additional predicate argument in Emacs 29.1. The compatibility +function, which supports this additional argument, can be +obtained via (compat-function plist-get) and called +via (compat-call plist-get plist prop predicate). It is not +possible to directly call (plist-get plist prop predicate) on +Emacs older than 29.1, since the original `plist-get' function +does not yet support the predicate argument. Note that the +Compat library never overrides existing functions. + +See also `org-compat-function' to lookup compatibility functions." + (let ((compat (intern (format "compat--%s" fun)))) + `(,(if (fboundp compat) compat fun) ,@args))) + ;;; Emacs < 29 compatibility -- 2.40.0