|
| 1 | +;;; clj-refactor-compat.el --- Functions from newer Emacs versions for compatibility -*- lexical-binding: t -*- |
| 2 | + |
| 3 | +;; Copyright © 2012-2016 Magnar Sveen |
| 4 | +;; Copyright © 2014-2016 Magnar Sveen, Lars Andersen, Benedek Fazekas |
| 5 | + |
| 6 | +;; This program is free software; you can redistribute it and/or |
| 7 | +;; modify it under the terms of the GNU General Public License |
| 8 | +;; as published by the Free Software Foundation; either version 3 |
| 9 | +;; of the License, or (at your option) any later version. |
| 10 | + |
| 11 | +;; This program is distributed in the hope that it will be useful, |
| 12 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +;; GNU General Public License for more details. |
| 15 | + |
| 16 | +;; You should have received a copy of the GNU General Public License |
| 17 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +;;; Commentary: |
| 20 | + |
| 21 | +;; Pretty much everything here's copied from subr-x for compatibility with |
| 22 | +;; Emacs 24.4. |
| 23 | + |
| 24 | +;;; Code: |
| 25 | + |
| 26 | +(eval-and-compile |
| 27 | + |
| 28 | + (unless (fboundp 'internal--thread-argument) |
| 29 | + (defmacro internal--thread-argument (first? &rest forms) |
| 30 | + "Internal implementation for `thread-first' and `thread-last'. |
| 31 | +When Argument FIRST? is non-nil argument is threaded first, else |
| 32 | +last. FORMS are the expressions to be threaded." |
| 33 | + (pcase forms |
| 34 | + (`(,x (,f . ,args) . ,rest) |
| 35 | + `(internal--thread-argument |
| 36 | + ,first? ,(if first? `(,f ,x ,@args) `(,f ,@args ,x)) ,@rest)) |
| 37 | + (`(,x ,f . ,rest) `(internal--thread-argument ,first? (,f ,x) ,@rest)) |
| 38 | + (_ (car forms))))) |
| 39 | + |
| 40 | + (unless (fboundp 'thread-first) |
| 41 | + (defmacro thread-first (&rest forms) |
| 42 | + "Thread FORMS elements as the first argument of their successor. |
| 43 | +Example: |
| 44 | + (thread-first |
| 45 | + 5 |
| 46 | + (+ 20) |
| 47 | + (/ 25) |
| 48 | + - |
| 49 | + (+ 40)) |
| 50 | +Is equivalent to: |
| 51 | + (+ (- (/ (+ 5 20) 25)) 40) |
| 52 | +Note how the single `-' got converted into a list before |
| 53 | +threading." |
| 54 | + (declare (indent 1) |
| 55 | + (debug (form &rest [&or symbolp (sexp &rest form)]))) |
| 56 | + `(internal--thread-argument t ,@forms))) |
| 57 | + |
| 58 | + (unless (fboundp 'thread-last) |
| 59 | + (defmacro thread-last (&rest forms) |
| 60 | + "Thread FORMS elements as the last argument of their successor. |
| 61 | +Example: |
| 62 | + (thread-last |
| 63 | + 5 |
| 64 | + (+ 20) |
| 65 | + (/ 25) |
| 66 | + - |
| 67 | + (+ 40)) |
| 68 | +Is equivalent to: |
| 69 | + (+ 40 (- (/ 25 (+ 20 5)))) |
| 70 | +Note how the single `-' got converted into a list before |
| 71 | +threading." |
| 72 | + (declare (indent 1) (debug thread-first)) |
| 73 | + `(internal--thread-argument nil ,@forms)))) |
| 74 | + |
| 75 | + |
| 76 | +(eval-and-compile |
| 77 | + |
| 78 | + (unless (fboundp 'internal--listify) |
| 79 | + |
| 80 | + (defsubst internal--listify (elt) |
| 81 | + "Wrap ELT in a list if it is not one." |
| 82 | + (if (not (listp elt)) |
| 83 | + (list elt) |
| 84 | + elt))) |
| 85 | + |
| 86 | + (unless (fboundp 'internal--check-binding) |
| 87 | + |
| 88 | + (defsubst internal--check-binding (binding) |
| 89 | + "Check BINDING is properly formed." |
| 90 | + (when (> (length binding) 2) |
| 91 | + (signal |
| 92 | + 'error |
| 93 | + (cons "`let' bindings can have only one value-form" binding))) |
| 94 | + binding)) |
| 95 | + |
| 96 | + (unless (fboundp 'internal--build-binding-value-form) |
| 97 | + |
| 98 | + (defsubst internal--build-binding-value-form (binding prev-var) |
| 99 | + "Build the conditional value form for BINDING using PREV-VAR." |
| 100 | + `(,(car binding) (and ,prev-var ,(cadr binding))))) |
| 101 | + |
| 102 | + (unless (fboundp 'internal--build-binding) |
| 103 | + |
| 104 | + (defun internal--build-binding (binding prev-var) |
| 105 | + "Check and build a single BINDING with PREV-VAR." |
| 106 | + (thread-first |
| 107 | + binding |
| 108 | + internal--listify |
| 109 | + internal--check-binding |
| 110 | + (internal--build-binding-value-form prev-var)))) |
| 111 | + |
| 112 | + (unless (fboundp 'internal--build-bindings) |
| 113 | + |
| 114 | + (defun internal--build-bindings (bindings) |
| 115 | + "Check and build conditional value forms for BINDINGS." |
| 116 | + (let ((prev-var t)) |
| 117 | + (mapcar (lambda (binding) |
| 118 | + (let ((binding (internal--build-binding binding prev-var))) |
| 119 | + (setq prev-var (car binding)) |
| 120 | + binding)) |
| 121 | + bindings))))) |
| 122 | + |
| 123 | +(eval-and-compile |
| 124 | + |
| 125 | + (unless (fboundp 'if-let) |
| 126 | + (defmacro if-let (bindings then &rest else) |
| 127 | + "Process BINDINGS and if all values are non-nil eval THEN, else ELSE. |
| 128 | +Argument BINDINGS is a list of tuples whose car is a symbol to be |
| 129 | +bound and (optionally) used in THEN, and its cadr is a sexp to be |
| 130 | +evalled to set symbol's value. In the special case you only want |
| 131 | +to bind a single value, BINDINGS can just be a plain tuple." |
| 132 | + (declare (indent 2) |
| 133 | + (debug ([&or (&rest (symbolp form)) (symbolp form)] form body))) |
| 134 | + (when (and (<= (length bindings) 2) |
| 135 | + (not (listp (car bindings)))) |
| 136 | + ;; Adjust the single binding case |
| 137 | + (setq bindings (list bindings))) |
| 138 | + `(let* ,(internal--build-bindings bindings) |
| 139 | + (if ,(car (internal--listify (car (last bindings)))) |
| 140 | + ,then |
| 141 | + ,@else)))) |
| 142 | + |
| 143 | + (unless (fboundp 'when-let) |
| 144 | + (defmacro when-let (bindings &rest body) |
| 145 | + "Process BINDINGS and if all values are non-nil eval BODY. |
| 146 | +Argument BINDINGS is a list of tuples whose car is a symbol to be |
| 147 | +bound and (optionally) used in BODY, and its cadr is a sexp to be |
| 148 | +evalled to set symbol's value. In the special case you only want |
| 149 | +to bind a single value, BINDINGS can just be a plain tuple." |
| 150 | + (declare (indent 1) (debug if-let)) |
| 151 | + (list 'if-let bindings (macroexp-progn body))))) |
| 152 | + |
| 153 | +(provide 'clj-refactor-compat) |
| 154 | +;;; clj-refactor-compat.el ends here |
0 commit comments