config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

compat-25.el (9581B)


      1 ;;; compat-25.el --- Functionality added in Emacs 25.1 -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2021-2024 Free Software Foundation, Inc.
      4 
      5 ;; This program is free software; you can redistribute it and/or modify
      6 ;; it under the terms of the GNU General Public License as published by
      7 ;; the Free Software Foundation, either version 3 of the License, or
      8 ;; (at your option) any later version.
      9 
     10 ;; This program is distributed in the hope that it will be useful,
     11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 ;; GNU General Public License for more details.
     14 
     15 ;; You should have received a copy of the GNU General Public License
     16 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 
     18 ;;; Commentary:
     19 
     20 ;; Functionality added in Emacs 25.1, needed by older Emacs versions.
     21 
     22 ;;; Code:
     23 
     24 (eval-when-compile (load "compat-macs.el" nil t t))
     25 
     26 (compat-version "25.1")
     27 
     28 ;;;; Defined in alloc.c
     29 
     30 (compat-defun bool-vector (&rest objects) ;; <compat-tests:bool-vector>
     31   "Return a new bool-vector with specified arguments as elements.
     32 Allows any number of arguments, including zero.
     33 usage: (bool-vector &rest OBJECTS)"
     34   (let ((vec (make-bool-vector (length objects) nil))
     35         (i 0))
     36     (while objects
     37       (when (car objects)
     38         (aset vec i t))
     39       (setq objects (cdr objects)
     40             i (1+ i)))
     41     vec))
     42 
     43 ;;;; Defined in editfns.c
     44 
     45 (compat-defalias format-message format) ;; <compat-tests:format-message>
     46 
     47 ;;;; Defined in fileio.c
     48 
     49 (compat-defun directory-name-p (name) ;; <compat-tests:directory-name-p>
     50   "Return non-nil if NAME ends with a directory separator character."
     51   (eq (eval-when-compile
     52         (if (memq system-type '(cygwin windows-nt ms-dos))
     53             ?\\ ?/))
     54       (aref name (1- (length name)))))
     55 
     56 ;;;; Defined in doc.c
     57 
     58 (compat-defvar text-quoting-style nil ;; <compat-tests:text-quoting-style>
     59   "Style to use for single quotes in help and messages.
     60 
     61 The value of this variable determines substitution of grave accents
     62 and apostrophes in help output (but not for display of Info
     63 manuals) and in functions like `message' and `format-message', but not
     64 in `format'.
     65 
     66 The value should be one of these symbols:
     67   `curve':    quote with curved single quotes ‘like this’.
     68   `straight': quote with straight apostrophes \\='like this\\='.
     69   `grave':    quote with grave accent and apostrophe \\=`like this\\=';
     70               i.e., do not alter the original quote marks.
     71   nil:        like `curve' if curved single quotes are displayable,
     72               and like `grave' otherwise.  This is the default.
     73 
     74 You should never read the value of this variable directly from a Lisp
     75 program.  Use the function `text-quoting-style' instead, as that will
     76 compute the correct value for the current terminal in the nil case.")
     77 
     78 ;;;; Defined in simple.el
     79 
     80 ;; `save-excursion' behaved like `save-mark-and-excursion' before 25.1.
     81 (compat-defalias save-mark-and-excursion save-excursion) ;; <compat-tests:save-mark-and-excursion>
     82 
     83 (declare-function region-bounds nil) ;; Defined in compat-26.el
     84 (compat-defun region-noncontiguous-p () ;; <compat-tests:region-noncontiguous-p>
     85   "Return non-nil if the region contains several pieces.
     86 An example is a rectangular region handled as a list of
     87 separate contiguous regions for each line."
     88   (let ((bounds (region-bounds))) (and (cdr bounds) bounds)))
     89 
     90 ;;;; Defined in subr.el
     91 
     92 (compat-defun string-greaterp (string1 string2) ;; <compat-tests:string-greaterp>
     93   "Return non-nil if STRING1 is greater than STRING2 in lexicographic order.
     94 Case is significant.
     95 Symbols are also allowed; their print names are used instead."
     96   (string-lessp string2 string1))
     97 
     98 (compat-defmacro with-file-modes (modes &rest body) ;; <compat-tests:with-file-modes>
     99   "Execute BODY with default file permissions temporarily set to MODES.
    100 MODES is as for `set-default-file-modes'."
    101   (declare (indent 1) (debug t))
    102   (let ((umask (make-symbol "umask")))
    103     `(let ((,umask (default-file-modes)))
    104        (unwind-protect
    105            (progn
    106              (set-default-file-modes ,modes)
    107              ,@body)
    108          (set-default-file-modes ,umask)))))
    109 
    110 (compat-defmacro if-let (spec then &rest else) ;; <compat-tests:if-let>
    111   "Bind variables according to SPEC and evaluate THEN or ELSE.
    112 Evaluate each binding in turn, as in `let*', stopping if a
    113 binding value is nil.  If all are non-nil return the value of
    114 THEN, otherwise the last form in ELSE.
    115 
    116 Each element of SPEC is a list (SYMBOL VALUEFORM) that binds
    117 SYMBOL to the value of VALUEFORM.  An element can additionally be
    118 of the form (VALUEFORM), which is evaluated and checked for nil;
    119 i.e. SYMBOL can be omitted if only the test result is of
    120 interest.  It can also be of the form SYMBOL, then the binding of
    121 SYMBOL is checked for nil.
    122 
    123 As a special case, interprets a SPEC of the form \(SYMBOL SOMETHING)
    124 like \((SYMBOL SOMETHING)).  This exists for backward compatibility
    125 with an old syntax that accepted only one binding."
    126   (declare (indent 2)
    127            (debug ([&or (symbolp form)
    128                         (&rest [&or symbolp (symbolp form) (form)])]
    129                    body)))
    130   (when (and (<= (length spec) 2) (not (listp (car spec))))
    131     ;; Adjust the single binding case
    132     (setq spec (list spec)))
    133   (let ((empty (make-symbol "s"))
    134         (last t) list)
    135     (dolist (var spec)
    136       (push `(,(if (cdr var) (car var) empty)
    137               (and ,last ,(if (cdr var) (cadr var) (car var))))
    138             list)
    139       (when (or (cdr var) (consp (car var)))
    140         (setq last (caar list))))
    141     `(let* ,(nreverse list)
    142        (if ,(caar list) ,then ,@else))))
    143 
    144 (compat-defmacro when-let (spec &rest body) ;; <compat-tests:when-let>
    145   "Bind variables according to SPEC and conditionally evaluate BODY.
    146 Evaluate each binding in turn, stopping if a binding value is nil.
    147 If all are non-nil, return the value of the last form in BODY.
    148 
    149 The variable list SPEC is the same as in `if-let'."
    150   (declare (indent 1) (debug if-let))
    151   (list 'if-let spec (macroexp-progn body)))
    152 
    153 ;;;; Defined in subr-x.el
    154 
    155 (compat-defun hash-table-empty-p (hash-table) ;; <compat-tests:hash-table-empty-p>
    156   "Check whether HASH-TABLE is empty (has 0 elements)."
    157   (zerop (hash-table-count hash-table)))
    158 
    159 (compat-defmacro thread-first (&rest forms) ;; <compat-tests:thread-first>
    160   "Thread FORMS elements as the first argument of their successor.
    161 Example:
    162     (thread-first
    163       5
    164       (+ 20)
    165       (/ 25)
    166       -
    167       (+ 40))
    168 Is equivalent to:
    169     (+ (- (/ (+ 5 20) 25)) 40)
    170 Note how the single `-' got converted into a list before
    171 threading."
    172   (declare (indent 1)
    173            (debug (form &rest [&or symbolp (sexp &rest form)])))
    174   (let ((body (car forms)))
    175     (dolist (form (cdr forms))
    176       (when (symbolp form)
    177         (setq form (list form)))
    178       (setq body (append (list (car form))
    179                          (list body)
    180                          (cdr form))))
    181     body))
    182 
    183 (compat-defmacro thread-last (&rest forms) ;; <compat-tests:thread-last>
    184   "Thread FORMS elements as the last argument of their successor.
    185 Example:
    186     (thread-last
    187       5
    188       (+ 20)
    189       (/ 25)
    190       -
    191       (+ 40))
    192 Is equivalent to:
    193     (+ 40 (- (/ 25 (+ 20 5))))
    194 Note how the single `-' got converted into a list before
    195 threading."
    196   (declare (indent 1) (debug thread-first))
    197   (let ((body (car forms)))
    198     (dolist (form (cdr forms))
    199       (when (symbolp form)
    200         (setq form (list form)))
    201       (setq body (append form (list body))))
    202     body))
    203 
    204 ;;;; Defined in macroexp.el
    205 
    206 (compat-defun macroexp-parse-body (body) ;; <compat-tests:macroexp-parse-body>
    207   "Parse a function BODY into (DECLARATIONS . EXPS)."
    208   (let ((decls ()))
    209     (while (and (cdr body)
    210                 (let ((e (car body)))
    211                   (or (stringp e)
    212                       (memq (car-safe e)
    213                             '(:documentation declare interactive cl-declare)))))
    214       (push (pop body) decls))
    215     (cons (nreverse decls) body)))
    216 
    217 (compat-defun macroexp-quote (v) ;; <compat-tests:macroexp-quote>
    218   "Return an expression E such that `(eval E)' is V.
    219 
    220 E is either V or (quote V) depending on whether V evaluates to
    221 itself or not."
    222   (if (and (not (consp v))
    223            (or (keywordp v)
    224                (not (symbolp v))
    225                (memq v '(nil t))))
    226       v
    227     (list 'quote v)))
    228 
    229 (compat-defun macroexpand-1 (form &optional environment) ;; <compat-tests:macroexpand-1>
    230   "Perform (at most) one step of macro expansion."
    231   (cond
    232    ((consp form)
    233     (let* ((head (car form))
    234            (env-expander (assq head environment)))
    235       (if env-expander
    236           (if (cdr env-expander)
    237               (apply (cdr env-expander) (cdr form))
    238             form)
    239         (if (not (and (symbolp head) (fboundp head)))
    240             form
    241           (let ((def (autoload-do-load (symbol-function head) head 'macro)))
    242             (cond
    243              ;; Follow alias, but only for macros, otherwise we may end up
    244              ;; skipping an important compiler-macro (e.g. cl--block-wrapper).
    245              ((and (symbolp def) (macrop def)) (cons def (cdr form)))
    246              ((not (consp def)) form)
    247              (t
    248               (if (eq 'macro (car def))
    249                   (apply (cdr def) (cdr form))
    250                 form))))))))
    251    (t form)))
    252 
    253 ;;;; Defined in minibuffer.el
    254 
    255 (compat-defun completion--category-override (category tag) ;; <compat-tests:completion-metadata-get>
    256   "Return completion category override for CATEGORY and TAG."
    257   (assq tag (cdr (assq category completion-category-overrides))))
    258 
    259 (provide 'compat-25)
    260 ;;; compat-25.el ends here