From e85b19dba86bc3a5937376a0001453dbe3a5dc24 Mon Sep 17 00:00:00 2001 From: Max Nikulin Date: Wed, 5 Jul 2023 22:50:44 +0700 Subject: [PATCH v2 2/2] org-compat.el: `org--flatten-tree' for Emacs-26 compatibility * lisp/org-compat.el (org--flatten-tree): New internal compatibility alias or function based on `flatten-tree' added to Emacs-27.1. * lisp/org-protocol.el (org-protocol-flatten): Convert to an obsolete function alias. (org-protocol-flatten-greedy): Use the `org--flatten-tree' new compatibility alias or function instead of `org-protocol-flatten'. * lisp/org-src.el (org-src--get-known-shells): Make the function compatible with Emacs-26 by using `org--flatten-tree' instead of `flatten-tree'. Reported as: Justin to emacs-orgmode. [PATCH] org-src: flatten-tree is Emacs 27.1; require compat. Tue, 4 Jul 2023 19:21:05 -0400. https://list.orgmode.org/0288575b-fde8-0e7d-ac74-1f0ac93ce56f@vallon.homeip.net --- lisp/org-compat.el | 19 +++++++++++++++++++ lisp/org-protocol.el | 24 +++++++++--------------- lisp/org-src.el | 2 +- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/lisp/org-compat.el b/lisp/org-compat.el index c5ab655d2..bbda04a15 100644 --- a/lisp/org-compat.el +++ b/lisp/org-compat.el @@ -211,6 +211,25 @@ (defmacro org-combine-change-calls (_beg _end &rest body) `(progn ,@body)) (defalias 'org-combine-change-calls 'combine-change-calls)) +;; `flatten-tree' was added in Emacs 27.1. +(if (fboundp 'flatten-tree) + (defalias 'org--flatten-tree #'flatten-tree) + ;; The implementation is taken from Emacs subr.el 8664ba18c7c5. + (defun org--flatten-tree (tree) + "Return a \"flattened\" copy of TREE. + +A `flatten-tree' polyfill for compatibility with Emacs versions +older than 27.1" + (let (elems) + (while (consp tree) + (let ((elem (pop tree))) + (while (consp elem) + (push (cdr elem) tree) + (setq elem (car elem))) + (if elem (push elem elems)))) + (if tree (push tree elems)) + (nreverse elems)))) + (if (version< emacs-version "27.1") (defsubst org-replace-buffer-contents (source &optional _max-secs _max-costs) (replace-buffer-contents source)) diff --git a/lisp/org-protocol.el b/lisp/org-protocol.el index 2b07a377e..d6cc39aa9 100644 --- a/lisp/org-protocol.el +++ b/lisp/org-protocol.el @@ -328,7 +328,7 @@ (defun org-protocol-flatten-greedy (param-list &optional strip-path replacement) Greedy handlers might receive a list like this from emacsclient: \((\"/dir/org-protocol:/greedy:/~/path1\" (23 . 12)) (\"/dir/param\")) where \"/dir/\" is the absolute path to emacsclient's working directory. This -function transforms it into a flat list using `org-protocol-flatten' and +function transforms it into a flat list using `flatten-tree' and transforms the elements of that list as follows: If STRIP-PATH is non-nil, remove the \"/dir/\" prefix from all members of @@ -343,9 +343,9 @@ (defun org-protocol-flatten-greedy (param-list &optional strip-path replacement) `org-protocol-reverse-list-of-files' was set to t and the returned list will reflect that. emacsclient's first parameter will be the first one in the returned list." - (let* ((l (org-protocol-flatten (if org-protocol-reverse-list-of-files - param-list - (reverse param-list)))) + (let* ((l (org--flatten-tree (if org-protocol-reverse-list-of-files + param-list + (reverse param-list)))) (trigger (car l)) (len 0) dir @@ -368,21 +368,15 @@ (defun org-protocol-flatten-greedy (param-list &optional strip-path replacement) ret) l))) -;; `flatten-tree' was added in Emacs 27.1. -(defalias 'org-protocol-flatten - (if (fboundp 'flatten-tree) 'flatten-tree - (lambda (list) - "Transform LIST into a flat list. +(define-obsolete-function-alias 'org-protocol-flatten + (if (fboundp 'flatten-tree) 'flatten-tree 'org--flatten-tree) + "9.7" + "Transform LIST into a flat list. Greedy handlers might receive a list like this from emacsclient: \((\"/dir/org-protocol:/greedy:/~/path1\" (23 . 12)) (\"/dir/param\")) where \"/dir/\" is the absolute path to emacsclients working directory. -This function transforms it into a flat list." - (if list - (if (consp list) - (append (org-protocol-flatten (car list)) - (org-protocol-flatten (cdr list))) - (list list)))))) +This function transforms it into a flat list.") (defun org-protocol-parse-parameters (info &optional new-style default-order) "Return a property list of parameters from INFO. diff --git a/lisp/org-src.el b/lisp/org-src.el index e1f7d50dc..7edbe0570 100644 --- a/lisp/org-src.el +++ b/lisp/org-src.el @@ -208,7 +208,7 @@ (defun org-src--get-known-shells () The shells are associated with `sh-mode'." (mapcar (lambda (shell) (cons (symbol-name shell) 'sh)) - (delete-dups (flatten-tree sh-ancestor-alist)))) + (delete-dups (org--flatten-tree sh-ancestor-alist)))) (defcustom org-src-lang-modes `(("C" . c) -- 2.25.1